| 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 PARAMS ((bfd *)); |
| 31 | static const bfd_target * os9k_object_p PARAMS ((bfd *)); |
| 32 | static int os9k_sizeof_headers PARAMS ((bfd *, boolean)); |
| 33 | boolean os9k_swap_exec_header_in PARAMS ((bfd *, mh_com *, struct internal_exec *)); |
| 34 | |
| 35 | /* Swaps the information in an executable header taken from a raw byte |
| 36 | stream memory image, into the internal exec_header structure. */ |
| 37 | boolean |
| 38 | os9k_swap_exec_header_in (abfd, raw_bytes, execp) |
| 39 | bfd *abfd; |
| 40 | mh_com *raw_bytes; |
| 41 | struct internal_exec *execp; |
| 42 | { |
| 43 | mh_com *bytes = (mh_com *) raw_bytes; |
| 44 | unsigned int dload, dmemsize, dmemstart; |
| 45 | |
| 46 | /* Now fill in fields in the execp, from the bytes in the raw data. */ |
| 47 | execp->a_info = H_GET_16 (abfd, bytes->m_sync); |
| 48 | execp->a_syms = 0; |
| 49 | execp->a_entry = H_GET_32 (abfd, bytes->m_exec); |
| 50 | execp->a_talign = 2; |
| 51 | execp->a_dalign = 2; |
| 52 | execp->a_balign = 2; |
| 53 | |
| 54 | dload = H_GET_32 (abfd, bytes->m_idata); |
| 55 | execp->a_data = dload + 8; |
| 56 | |
| 57 | if (bfd_seek (abfd, (file_ptr) dload, SEEK_SET) != 0 |
| 58 | || (bfd_bread (&dmemstart, (bfd_size_type) sizeof (dmemstart), abfd) |
| 59 | != sizeof (dmemstart)) |
| 60 | || (bfd_bread (&dmemsize, (bfd_size_type) sizeof (dmemsize), abfd) |
| 61 | != sizeof (dmemsize))) |
| 62 | return false; |
| 63 | |
| 64 | execp->a_tload = 0; |
| 65 | execp->a_dload = H_GET_32 (abfd, (unsigned char *) &dmemstart); |
| 66 | execp->a_text = dload - execp->a_tload; |
| 67 | execp->a_data = H_GET_32 (abfd, (unsigned char *) &dmemsize); |
| 68 | execp->a_bss = H_GET_32 (abfd, bytes->m_data) - execp->a_data; |
| 69 | |
| 70 | execp->a_trsize = 0; |
| 71 | execp->a_drsize = 0; |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | #if 0 |
| 77 | /* Swaps the information in an internal exec header structure into the |
| 78 | supplied buffer ready for writing to disk. */ |
| 79 | |
| 80 | void os9k_swap_exec_header_out |
| 81 | PARAMS ((bfd *, struct internal_exec *, struct mh_com *)); |
| 82 | |
| 83 | void |
| 84 | os9k_swap_exec_header_out (abfd, execp, raw_bytes) |
| 85 | bfd *abfd; |
| 86 | struct internal_exec *execp; |
| 87 | mh_com *raw_bytes; |
| 88 | { |
| 89 | mh_com *bytes = (mh_com *) raw_bytes; |
| 90 | |
| 91 | /* Now fill in fields in the raw data, from the fields in the exec struct. */ |
| 92 | H_PUT_32 (abfd, execp->a_info, bytes->e_info); |
| 93 | H_PUT_32 (abfd, execp->a_text, bytes->e_text); |
| 94 | H_PUT_32 (abfd, execp->a_data, bytes->e_data); |
| 95 | H_PUT_32 (abfd, execp->a_bss, bytes->e_bss); |
| 96 | H_PUT_32 (abfd, execp->a_syms, bytes->e_syms); |
| 97 | H_PUT_32 (abfd, execp->a_entry, bytes->e_entry); |
| 98 | H_PUT_32 (abfd, execp->a_trsize, bytes->e_trsize); |
| 99 | H_PUT_32 (abfd, execp->a_drsize, bytes->e_drsize); |
| 100 | H_PUT_32 (abfd, execp->a_tload, bytes->e_tload); |
| 101 | H_PUT_32 (abfd, execp->a_dload, bytes->e_dload); |
| 102 | bytes->e_talign[0] = execp->a_talign; |
| 103 | bytes->e_dalign[0] = execp->a_dalign; |
| 104 | bytes->e_balign[0] = execp->a_balign; |
| 105 | bytes->e_relaxable[0] = execp->a_relaxable; |
| 106 | } |
| 107 | |
| 108 | #endif /* 0 */ |
| 109 | |
| 110 | static const bfd_target * |
| 111 | os9k_object_p (abfd) |
| 112 | bfd *abfd; |
| 113 | { |
| 114 | struct internal_exec anexec; |
| 115 | mh_com exec_bytes; |
| 116 | |
| 117 | if (bfd_bread ((PTR) &exec_bytes, (bfd_size_type) MHCOM_BYTES_SIZE, abfd) |
| 118 | != MHCOM_BYTES_SIZE) |
| 119 | { |
| 120 | if (bfd_get_error () != bfd_error_system_call) |
| 121 | bfd_set_error (bfd_error_wrong_format); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | anexec.a_info = H_GET_16 (abfd, exec_bytes.m_sync); |
| 126 | if (N_BADMAG (anexec)) |
| 127 | { |
| 128 | bfd_set_error (bfd_error_wrong_format); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | if (! os9k_swap_exec_header_in (abfd, &exec_bytes, &anexec)) |
| 133 | { |
| 134 | if (bfd_get_error () != bfd_error_system_call) |
| 135 | bfd_set_error (bfd_error_wrong_format); |
| 136 | return NULL; |
| 137 | } |
| 138 | return aout_32_some_aout_object_p (abfd, &anexec, os9k_callback); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /* Finish up the opening of a b.out file for reading. Fill in all the |
| 143 | fields that are not handled by common code. */ |
| 144 | |
| 145 | static const bfd_target * |
| 146 | os9k_callback (abfd) |
| 147 | bfd *abfd; |
| 148 | { |
| 149 | struct internal_exec *execp = exec_hdr (abfd); |
| 150 | unsigned long bss_start; |
| 151 | |
| 152 | /* Architecture and machine type. */ |
| 153 | bfd_set_arch_mach (abfd, bfd_arch_i386, 0); |
| 154 | |
| 155 | /* The positions of the string table and symbol table. */ |
| 156 | obj_str_filepos (abfd) = 0; |
| 157 | obj_sym_filepos (abfd) = 0; |
| 158 | |
| 159 | /* The alignments of the sections. */ |
| 160 | obj_textsec (abfd)->alignment_power = execp->a_talign; |
| 161 | obj_datasec (abfd)->alignment_power = execp->a_dalign; |
| 162 | obj_bsssec (abfd)->alignment_power = execp->a_balign; |
| 163 | |
| 164 | /* The starting addresses of the sections. */ |
| 165 | obj_textsec (abfd)->vma = execp->a_tload; |
| 166 | obj_datasec (abfd)->vma = execp->a_dload; |
| 167 | |
| 168 | /* And reload the sizes, since the aout module zaps them. */ |
| 169 | obj_textsec (abfd)->_raw_size = execp->a_text; |
| 170 | |
| 171 | bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section. */ |
| 172 | obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign); |
| 173 | |
| 174 | /* The file positions of the sections. */ |
| 175 | obj_textsec (abfd)->filepos = execp->a_entry; |
| 176 | obj_datasec (abfd)->filepos = execp->a_dload; |
| 177 | |
| 178 | /* The file positions of the relocation info *** |
| 179 | obj_textsec (abfd)->rel_filepos = N_TROFF(*execp); |
| 180 | obj_datasec (abfd)->rel_filepos = N_DROFF(*execp); */ |
| 181 | |
| 182 | adata (abfd).page_size = 1; /* Not applicable. */ |
| 183 | adata (abfd).segment_size = 1;/* Not applicable. */ |
| 184 | adata (abfd).exec_bytes_size = MHCOM_BYTES_SIZE; |
| 185 | |
| 186 | return abfd->xvec; |
| 187 | } |
| 188 | |
| 189 | #if 0 |
| 190 | struct bout_data_struct |
| 191 | { |
| 192 | struct aoutdata a; |
| 193 | struct internal_exec e; |
| 194 | }; |
| 195 | |
| 196 | static boolean |
| 197 | os9k_mkobject (abfd) |
| 198 | bfd *abfd; |
| 199 | { |
| 200 | struct bout_data_struct *rawptr; |
| 201 | bfd_size_type amt = sizeof (struct bout_data_struct); |
| 202 | |
| 203 | rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, amt); |
| 204 | if (rawptr == NULL) |
| 205 | return false; |
| 206 | |
| 207 | abfd->tdata.bout_data = rawptr; |
| 208 | exec_hdr (abfd) = &rawptr->e; |
| 209 | |
| 210 | obj_textsec (abfd) = (asection *) NULL; |
| 211 | obj_datasec (abfd) = (asection *) NULL; |
| 212 | obj_bsssec (abfd) = (asection *) NULL; |
| 213 | |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | static boolean |
| 218 | os9k_write_object_contents (abfd) |
| 219 | bfd *abfd; |
| 220 | { |
| 221 | struct external_exec swapped_hdr; |
| 222 | |
| 223 | if (! aout_32_make_sections (abfd)) |
| 224 | return false; |
| 225 | |
| 226 | exec_hdr (abfd)->a_info = BMAGIC; |
| 227 | |
| 228 | exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size; |
| 229 | exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size; |
| 230 | exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size; |
| 231 | exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist); |
| 232 | exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd); |
| 233 | exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) * |
| 234 | sizeof (struct relocation_info)); |
| 235 | exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) * |
| 236 | sizeof (struct relocation_info)); |
| 237 | |
| 238 | exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power; |
| 239 | exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power; |
| 240 | exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power; |
| 241 | |
| 242 | exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma; |
| 243 | exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma; |
| 244 | |
| 245 | bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr); |
| 246 | |
| 247 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 |
| 248 | || bfd_bwrite ((PTR) & swapped_hdr, (bfd_size_type) EXEC_BYTES_SIZE, |
| 249 | abfd) != EXEC_BYTES_SIZE) |
| 250 | return false; |
| 251 | |
| 252 | /* Now write out reloc info, followed by syms and strings. */ |
| 253 | if (bfd_get_symcount (abfd) != 0) |
| 254 | { |
| 255 | if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET) |
| 256 | != 0) |
| 257 | return false; |
| 258 | |
| 259 | if (!aout_32_write_syms (abfd)) |
| 260 | return false; |
| 261 | |
| 262 | if (bfd_seek (abfd, (file_ptr) (N_TROFF (*exec_hdr (abfd))), SEEK_SET) |
| 263 | != 0) |
| 264 | return false; |
| 265 | |
| 266 | if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) |
| 267 | return false; |
| 268 | if (bfd_seek (abfd, (file_ptr) (N_DROFF (*exec_hdr (abfd))), SEEK_SET) |
| 269 | != 0) |
| 270 | return false; |
| 271 | |
| 272 | if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd))) |
| 273 | return false; |
| 274 | } |
| 275 | return true; |
| 276 | } |
| 277 | \f |
| 278 | static boolean |
| 279 | os9k_set_section_contents (abfd, section, location, offset, count) |
| 280 | bfd *abfd; |
| 281 | sec_ptr section; |
| 282 | unsigned char *location; |
| 283 | file_ptr offset; |
| 284 | int count; |
| 285 | { |
| 286 | |
| 287 | if (abfd->output_has_begun == false) |
| 288 | { /* set by bfd.c handler */ |
| 289 | if (! aout_32_make_sections (abfd)) |
| 290 | return false; |
| 291 | |
| 292 | obj_textsec (abfd)->filepos = sizeof (struct internal_exec); |
| 293 | obj_datasec (abfd)->filepos = obj_textsec (abfd)->filepos |
| 294 | + obj_textsec (abfd)->_raw_size; |
| 295 | |
| 296 | } |
| 297 | /* Regardless, once we know what we're doing, we might as well get going. */ |
| 298 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0) |
| 299 | return false; |
| 300 | |
| 301 | if (count != 0) |
| 302 | return bfd_bwrite ((PTR) location, (bfd_size_type) count, abfd) == count; |
| 303 | |
| 304 | return true; |
| 305 | } |
| 306 | #endif /* 0 */ |
| 307 | |
| 308 | static int |
| 309 | os9k_sizeof_headers (ignore_abfd, ignore) |
| 310 | bfd *ignore_abfd ATTRIBUTE_UNUSED; |
| 311 | boolean ignore ATTRIBUTE_UNUSED; |
| 312 | { |
| 313 | return sizeof (struct internal_exec); |
| 314 | } |
| 315 | |
| 316 | \f |
| 317 | |
| 318 | #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info |
| 319 | |
| 320 | #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol |
| 321 | |
| 322 | #define aout_32_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup |
| 323 | |
| 324 | #define aout_32_get_section_contents_in_window \ |
| 325 | _bfd_generic_get_section_contents_in_window |
| 326 | |
| 327 | #define os9k_bfd_get_relocated_section_contents \ |
| 328 | bfd_generic_get_relocated_section_contents |
| 329 | #define os9k_bfd_relax_section bfd_generic_relax_section |
| 330 | #define os9k_bfd_gc_sections bfd_generic_gc_sections |
| 331 | #define os9k_bfd_merge_sections bfd_generic_merge_sections |
| 332 | #define os9k_bfd_discard_group bfd_generic_discard_group |
| 333 | #define os9k_bfd_link_hash_table_create _bfd_generic_link_hash_table_create |
| 334 | #define os9k_bfd_link_hash_table_free _bfd_generic_link_hash_table_free |
| 335 | #define os9k_bfd_link_add_symbols _bfd_generic_link_add_symbols |
| 336 | #define os9k_bfd_link_just_syms _bfd_generic_link_just_syms |
| 337 | #define os9k_bfd_final_link _bfd_generic_final_link |
| 338 | #define os9k_bfd_link_split_section _bfd_generic_link_split_section |
| 339 | |
| 340 | const bfd_target i386os9k_vec = |
| 341 | { |
| 342 | "i386os9k", /* name */ |
| 343 | bfd_target_os9k_flavour, |
| 344 | BFD_ENDIAN_LITTLE, /* data byte order is little */ |
| 345 | BFD_ENDIAN_LITTLE, /* hdr byte order is little */ |
| 346 | (HAS_RELOC | EXEC_P | WP_TEXT), /* object flags */ |
| 347 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD), /* section flags */ |
| 348 | 0, /* symbol leading char */ |
| 349 | ' ', /* ar_pad_char */ |
| 350 | 16, /* ar_max_namelen */ |
| 351 | |
| 352 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
| 353 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
| 354 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ |
| 355 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
| 356 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
| 357 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ |
| 358 | {_bfd_dummy_target, os9k_object_p, /* bfd_check_format */ |
| 359 | bfd_generic_archive_p, _bfd_dummy_target}, |
| 360 | {bfd_false, bfd_false, /* bfd_set_format */ |
| 361 | _bfd_generic_mkarchive, bfd_false}, |
| 362 | {bfd_false, bfd_false, /* bfd_write_contents */ |
| 363 | _bfd_write_archive_contents, bfd_false}, |
| 364 | |
| 365 | BFD_JUMP_TABLE_GENERIC (aout_32), |
| 366 | BFD_JUMP_TABLE_COPY (_bfd_generic), |
| 367 | BFD_JUMP_TABLE_CORE (_bfd_nocore), |
| 368 | BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd), |
| 369 | BFD_JUMP_TABLE_SYMBOLS (aout_32), |
| 370 | BFD_JUMP_TABLE_RELOCS (aout_32), |
| 371 | BFD_JUMP_TABLE_WRITE (aout_32), |
| 372 | BFD_JUMP_TABLE_LINK (os9k), |
| 373 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
| 374 | |
| 375 | NULL, |
| 376 | |
| 377 | (PTR) 0, |
| 378 | }; |