| 1 | /* BFD back-end for os9000 i386 binaries.
|
|---|
| 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001, 2002
|
|---|
| 3 | Free Software Foundation, Inc.
|
|---|
| 4 | Written by Cygnus Support.
|
|---|
| 5 |
|
|---|
| 6 | This file is part of BFD, the Binary File Descriptor library.
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | it under the terms of the GNU General Public License as published by
|
|---|
| 10 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 11 | (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | GNU General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License
|
|---|
| 19 | along with this program; if not, write to the Free Software
|
|---|
| 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | #include "bfd.h"
|
|---|
| 24 | #include "sysdep.h"
|
|---|
| 25 | #include "libbfd.h"
|
|---|
| 26 | #include "bfdlink.h"
|
|---|
| 27 | #include "libaout.h" /* BFD a.out internal data structures */
|
|---|
| 28 | #include "os9k.h"
|
|---|
| 29 |
|
|---|
| 30 | static const bfd_target * os9k_callback
|
|---|
| 31 | PARAMS ((bfd *));
|
|---|
| 32 | static const bfd_target * os9k_object_p
|
|---|
| 33 | PARAMS ((bfd *));
|
|---|
| 34 | static int os9k_sizeof_headers
|
|---|
| 35 | PARAMS ((bfd *, bfd_boolean));
|
|---|
| 36 | bfd_boolean os9k_swap_exec_header_in
|
|---|
| 37 | PARAMS ((bfd *, mh_com *, struct internal_exec *));
|
|---|
| 38 |
|
|---|
| 39 | /* Swaps the information in an executable header taken from a raw byte
|
|---|
| 40 | stream memory image, into the internal exec_header structure. */
|
|---|
| 41 | bfd_boolean
|
|---|
| 42 | os9k_swap_exec_header_in (abfd, raw_bytes, execp)
|
|---|
| 43 | bfd *abfd;
|
|---|
| 44 | mh_com *raw_bytes;
|
|---|
| 45 | struct internal_exec *execp;
|
|---|
| 46 | {
|
|---|
| 47 | mh_com *bytes = (mh_com *) raw_bytes;
|
|---|
| 48 | unsigned int dload, dmemsize, dmemstart;
|
|---|
| 49 |
|
|---|
| 50 | /* Now fill in fields in the execp, from the bytes in the raw data. */
|
|---|
| 51 | execp->a_info = H_GET_16 (abfd, bytes->m_sync);
|
|---|
| 52 | execp->a_syms = 0;
|
|---|
| 53 | execp->a_entry = H_GET_32 (abfd, bytes->m_exec);
|
|---|
| 54 | execp->a_talign = 2;
|
|---|
| 55 | execp->a_dalign = 2;
|
|---|
| 56 | execp->a_balign = 2;
|
|---|
| 57 |
|
|---|
| 58 | dload = H_GET_32 (abfd, bytes->m_idata);
|
|---|
| 59 | execp->a_data = dload + 8;
|
|---|
| 60 |
|
|---|
| 61 | if (bfd_seek (abfd, (file_ptr) dload, SEEK_SET) != 0
|
|---|
| 62 | || (bfd_bread (&dmemstart, (bfd_size_type) sizeof (dmemstart), abfd)
|
|---|
| 63 | != sizeof (dmemstart))
|
|---|
| 64 | || (bfd_bread (&dmemsize, (bfd_size_type) sizeof (dmemsize), abfd)
|
|---|
| 65 | != sizeof (dmemsize)))
|
|---|
| 66 | return FALSE;
|
|---|
| 67 |
|
|---|
| 68 | execp->a_tload = 0;
|
|---|
| 69 | execp->a_dload = H_GET_32 (abfd, (unsigned char *) &dmemstart);
|
|---|
| 70 | execp->a_text = dload - execp->a_tload;
|
|---|
| 71 | execp->a_data = H_GET_32 (abfd, (unsigned char *) &dmemsize);
|
|---|
| 72 | execp->a_bss = H_GET_32 (abfd, bytes->m_data) - execp->a_data;
|
|---|
| 73 |
|
|---|
| 74 | execp->a_trsize = 0;
|
|---|
| 75 | execp->a_drsize = 0;
|
|---|
| 76 |
|
|---|
| 77 | return TRUE;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | #if 0
|
|---|
| 81 | /* Swaps the information in an internal exec header structure into the
|
|---|
| 82 | supplied buffer ready for writing to disk. */
|
|---|
| 83 |
|
|---|
| 84 | void os9k_swap_exec_header_out
|
|---|
| 85 | PARAMS ((bfd *, struct internal_exec *, struct mh_com *));
|
|---|
| 86 |
|
|---|
| 87 | void
|
|---|
| 88 | os9k_swap_exec_header_out (abfd, execp, raw_bytes)
|
|---|
| 89 | bfd *abfd;
|
|---|
| 90 | struct internal_exec *execp;
|
|---|
| 91 | mh_com *raw_bytes;
|
|---|
| 92 | {
|
|---|
| 93 | mh_com *bytes = (mh_com *) raw_bytes;
|
|---|
| 94 |
|
|---|
| 95 | /* Now fill in fields in the raw data, from the fields in the exec struct. */
|
|---|
| 96 | H_PUT_32 (abfd, execp->a_info, bytes->e_info);
|
|---|
| 97 | H_PUT_32 (abfd, execp->a_text, bytes->e_text);
|
|---|
| 98 | H_PUT_32 (abfd, execp->a_data, bytes->e_data);
|
|---|
| 99 | H_PUT_32 (abfd, execp->a_bss, bytes->e_bss);
|
|---|
| 100 | H_PUT_32 (abfd, execp->a_syms, bytes->e_syms);
|
|---|
| 101 | H_PUT_32 (abfd, execp->a_entry, bytes->e_entry);
|
|---|
| 102 | H_PUT_32 (abfd, execp->a_trsize, bytes->e_trsize);
|
|---|
| 103 | H_PUT_32 (abfd, execp->a_drsize, bytes->e_drsize);
|
|---|
| 104 | H_PUT_32 (abfd, execp->a_tload, bytes->e_tload);
|
|---|
| 105 | H_PUT_32 (abfd, execp->a_dload, bytes->e_dload);
|
|---|
| 106 | bytes->e_talign[0] = execp->a_talign;
|
|---|
| 107 | bytes->e_dalign[0] = execp->a_dalign;
|
|---|
| 108 | bytes->e_balign[0] = execp->a_balign;
|
|---|
| 109 | bytes->e_relaxable[0] = execp->a_relaxable;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | #endif /* 0 */
|
|---|
| 113 |
|
|---|
| 114 | static const bfd_target *
|
|---|
| 115 | os9k_object_p (abfd)
|
|---|
| 116 | bfd *abfd;
|
|---|
| 117 | {
|
|---|
| 118 | struct internal_exec anexec;
|
|---|
| 119 | mh_com exec_bytes;
|
|---|
| 120 |
|
|---|
| 121 | if (bfd_bread ((PTR) &exec_bytes, (bfd_size_type) MHCOM_BYTES_SIZE, abfd)
|
|---|
| 122 | != MHCOM_BYTES_SIZE)
|
|---|
| 123 | {
|
|---|
| 124 | if (bfd_get_error () != bfd_error_system_call)
|
|---|
| 125 | bfd_set_error (bfd_error_wrong_format);
|
|---|
| 126 | return 0;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | anexec.a_info = H_GET_16 (abfd, exec_bytes.m_sync);
|
|---|
| 130 | if (N_BADMAG (anexec))
|
|---|
| 131 | {
|
|---|
| 132 | bfd_set_error (bfd_error_wrong_format);
|
|---|
| 133 | return 0;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | if (! os9k_swap_exec_header_in (abfd, &exec_bytes, &anexec))
|
|---|
| 137 | {
|
|---|
| 138 | if (bfd_get_error () != bfd_error_system_call)
|
|---|
| 139 | bfd_set_error (bfd_error_wrong_format);
|
|---|
| 140 | return NULL;
|
|---|
| 141 | }
|
|---|
| 142 | return aout_32_some_aout_object_p (abfd, &anexec, os9k_callback);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | /* Finish up the opening of a b.out file for reading. Fill in all the
|
|---|
| 147 | fields that are not handled by common code. */
|
|---|
| 148 |
|
|---|
| 149 | static const bfd_target *
|
|---|
| 150 | os9k_callback (abfd)
|
|---|
| 151 | bfd *abfd;
|
|---|
| 152 | {
|
|---|
| 153 | struct internal_exec *execp = exec_hdr (abfd);
|
|---|
| 154 | unsigned long bss_start;
|
|---|
| 155 |
|
|---|
| 156 | /* Architecture and machine type. */
|
|---|
| 157 | bfd_set_arch_mach (abfd, bfd_arch_i386, 0);
|
|---|
| 158 |
|
|---|
| 159 | /* The positions of the string table and symbol table. */
|
|---|
| 160 | obj_str_filepos (abfd) = 0;
|
|---|
| 161 | obj_sym_filepos (abfd) = 0;
|
|---|
| 162 |
|
|---|
| 163 | /* The alignments of the sections. */
|
|---|
| 164 | obj_textsec (abfd)->alignment_power = execp->a_talign;
|
|---|
| 165 | obj_datasec (abfd)->alignment_power = execp->a_dalign;
|
|---|
| 166 | obj_bsssec (abfd)->alignment_power = execp->a_balign;
|
|---|
| 167 |
|
|---|
| 168 | /* The starting addresses of the sections. */
|
|---|
| 169 | obj_textsec (abfd)->vma = execp->a_tload;
|
|---|
| 170 | obj_datasec (abfd)->vma = execp->a_dload;
|
|---|
| 171 |
|
|---|
| 172 | /* And reload the sizes, since the aout module zaps them. */
|
|---|
| 173 | obj_textsec (abfd)->_raw_size = execp->a_text;
|
|---|
| 174 |
|
|---|
| 175 | bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section. */
|
|---|
| 176 | obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign);
|
|---|
| 177 |
|
|---|
| 178 | /* The file positions of the sections. */
|
|---|
| 179 | obj_textsec (abfd)->filepos = execp->a_entry;
|
|---|
| 180 | obj_datasec (abfd)->filepos = execp->a_dload;
|
|---|
| 181 |
|
|---|
| 182 | /* The file positions of the relocation info ***
|
|---|
| 183 | obj_textsec (abfd)->rel_filepos = N_TROFF(*execp);
|
|---|
| 184 | obj_datasec (abfd)->rel_filepos = N_DROFF(*execp); */
|
|---|
| 185 |
|
|---|
| 186 | adata (abfd).page_size = 1; /* Not applicable. */
|
|---|
| 187 | adata (abfd).segment_size = 1;/* Not applicable. */
|
|---|
| 188 | adata (abfd).exec_bytes_size = MHCOM_BYTES_SIZE;
|
|---|
| 189 |
|
|---|
| 190 | return abfd->xvec;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | #if 0
|
|---|
| 194 | struct bout_data_struct
|
|---|
| 195 | {
|
|---|
| 196 | struct aoutdata a;
|
|---|
| 197 | struct internal_exec e;
|
|---|
| 198 | };
|
|---|
| 199 |
|
|---|
| 200 | static bfd_boolean
|
|---|
| 201 | os9k_mkobject (abfd)
|
|---|
| 202 | bfd *abfd;
|
|---|
| 203 | {
|
|---|
| 204 | struct bout_data_struct *rawptr;
|
|---|
| 205 | bfd_size_type amt = sizeof (struct bout_data_struct);
|
|---|
| 206 |
|
|---|
| 207 | rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, amt);
|
|---|
| 208 | if (rawptr == NULL)
|
|---|
| 209 | return FALSE;
|
|---|
| 210 |
|
|---|
| 211 | abfd->tdata.bout_data = rawptr;
|
|---|
| 212 | exec_hdr (abfd) = &rawptr->e;
|
|---|
| 213 |
|
|---|
| 214 | obj_textsec (abfd) = (asection *) NULL;
|
|---|
| 215 | obj_datasec (abfd) = (asection *) NULL;
|
|---|
| 216 | obj_bsssec (abfd) = (asection *) NULL;
|
|---|
| 217 |
|
|---|
| 218 | return TRUE;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | static bfd_boolean
|
|---|
| 222 | os9k_write_object_contents (abfd)
|
|---|
| 223 | bfd *abfd;
|
|---|
| 224 | {
|
|---|
| 225 | struct external_exec swapped_hdr;
|
|---|
| 226 |
|
|---|
| 227 | if (! aout_32_make_sections (abfd))
|
|---|
| 228 | return FALSE;
|
|---|
| 229 |
|
|---|
| 230 | exec_hdr (abfd)->a_info = BMAGIC;
|
|---|
| 231 |
|
|---|
| 232 | exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size;
|
|---|
| 233 | exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size;
|
|---|
| 234 | exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size;
|
|---|
| 235 | exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
|
|---|
| 236 | exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
|
|---|
| 237 | exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
|
|---|
| 238 | sizeof (struct relocation_info));
|
|---|
| 239 | exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
|
|---|
| 240 | sizeof (struct relocation_info));
|
|---|
| 241 |
|
|---|
| 242 | exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
|
|---|
| 243 | exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
|
|---|
| 244 | exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
|
|---|
| 245 |
|
|---|
| 246 | exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
|
|---|
| 247 | exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
|
|---|
| 248 |
|
|---|
| 249 | bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
|
|---|
| 250 |
|
|---|
| 251 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
|
|---|
| 252 | || bfd_bwrite ((PTR) & swapped_hdr, (bfd_size_type) EXEC_BYTES_SIZE,
|
|---|
| 253 | abfd) != EXEC_BYTES_SIZE)
|
|---|
| 254 | return FALSE;
|
|---|
| 255 |
|
|---|
| 256 | /* Now write out reloc info, followed by syms and strings. */
|
|---|
| 257 | if (bfd_get_symcount (abfd) != 0)
|
|---|
| 258 | {
|
|---|
| 259 | if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET)
|
|---|
| 260 | != 0)
|
|---|
| 261 | return FALSE;
|
|---|
| 262 |
|
|---|
| 263 | if (!aout_32_write_syms (abfd))
|
|---|
| 264 | return FALSE;
|
|---|
| 265 |
|
|---|
| 266 | if (bfd_seek (abfd, (file_ptr) (N_TROFF (*exec_hdr (abfd))), SEEK_SET)
|
|---|
| 267 | != 0)
|
|---|
| 268 | return FALSE;
|
|---|
| 269 |
|
|---|
| 270 | if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd)))
|
|---|
| 271 | return FALSE;
|
|---|
| 272 | if (bfd_seek (abfd, (file_ptr) (N_DROFF (*exec_hdr (abfd))), SEEK_SET)
|
|---|
| 273 | != 0)
|
|---|
| 274 | return FALSE;
|
|---|
| 275 |
|
|---|
| 276 | if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd)))
|
|---|
| 277 | return FALSE;
|
|---|
| 278 | }
|
|---|
| 279 | return TRUE;
|
|---|
| 280 | }
|
|---|
| 281 | |
|---|
|
|---|