| 1 | /* Support for Intel 960 COFF and Motorola 88k BCS COFF (and maybe others) */ |
| 2 | |
| 3 | /* Copyright (C) 1990, 1991 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of BFD, the Binary File Diddler. |
| 6 | |
| 7 | BFD is free software; you can redistribute it and/or modify it under the |
| 8 | terms of the GNU General Public License as published by the Free Software |
| 9 | Foundation; either version 1, or (at your option) any later version. |
| 10 | |
| 11 | BFD is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 14 | details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License along with |
| 17 | BFD; see the file COPYING. If not, write to the Free Software Foundation, |
| 18 | 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | /* $Id$ */ |
| 22 | /* Most of this hacked by Steve Chamberlain, steve@cygnus.com */ |
| 23 | |
| 24 | #include "archures.h" /* Machine architectures and types */ |
| 25 | |
| 26 | /* SUPPRESS 558 */ |
| 27 | /* SUPPRESS 590 */ |
| 28 | /* SUPPRESS 529 */ |
| 29 | /* SUPPRESS 530 */ |
| 30 | |
| 31 | /* Align an address upward to a boundary, expressed as a number of bytes. |
| 32 | E.g. align to an 8-byte boundary with argument of 8. */ |
| 33 | #define ALIGN(this, boundary) \ |
| 34 | ((( (this) + ((boundary) -1)) & (~((boundary)-1)))) |
| 35 | |
| 36 | /* Align an address upward to a power of two. Argument is the power |
| 37 | of two, e.g. 8-byte alignment uses argument of 3 (8 == 2^3). */ |
| 38 | #define i960_align(addr, align) \ |
| 39 | ( ((addr) + ((1<<(align))-1)) & (-1 << (align))) |
| 40 | |
| 41 | #define sp(x) bfd_h_put_x(abfd, x, &x) |
| 42 | |
| 43 | #define PUTWORD bfd_h_put_32 |
| 44 | #define PUTHALF bfd_h_put_16 |
| 45 | #ifndef I960 |
| 46 | #define GDB_EXPORT static |
| 47 | #else |
| 48 | #define GDB_EXPORT /* nothing */ |
| 49 | #endif |
| 50 | |
| 51 | PROTO(static void,force_indices_file_symbol_relative,(bfd *abfd, |
| 52 | struct internal_syment *symtab)); |
| 53 | |
| 54 | \f |
| 55 | /* void warning(); */ |
| 56 | extern asection abs_section; |
| 57 | |
| 58 | static int |
| 59 | DEFUN(get_index,(symbol), |
| 60 | asymbol *symbol) |
| 61 | { |
| 62 | return (int) symbol->value; |
| 63 | } |
| 64 | |
| 65 | static void |
| 66 | DEFUN(set_index,(symbol, idx), |
| 67 | asymbol *symbol AND |
| 68 | unsigned int idx) |
| 69 | { |
| 70 | symbol->value = idx; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | /* All the swapping routines: |
| 78 | */ |
| 79 | |
| 80 | |
| 81 | static void |
| 82 | DEFUN(bfd_swap_reloc_in,(abfd, reloc_src, reloc_dst), |
| 83 | bfd *abfd AND |
| 84 | RELOC *reloc_src AND |
| 85 | struct internal_reloc *reloc_dst) |
| 86 | { |
| 87 | reloc_dst->r_vaddr = bfd_h_get_32(abfd, reloc_src->r_vaddr); |
| 88 | reloc_dst->r_symndx = bfd_h_get_32(abfd, reloc_src->r_symndx); |
| 89 | reloc_dst->r_type = bfd_h_get_16(abfd, reloc_src->r_type); |
| 90 | #if M88 |
| 91 | reloc_dst->r_offset = bfd_h_get_16(abfd, reloc_src->r_offset); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static void |
| 97 | DEFUN(bfd_swap_reloc_out,(abfd, reloc_src, reloc_dst), |
| 98 | bfd *abfd AND |
| 99 | struct internal_reloc *reloc_src AND |
| 100 | struct external_reloc *reloc_dst) |
| 101 | { |
| 102 | bfd_h_put_32(abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr); |
| 103 | bfd_h_put_32(abfd, reloc_src->r_symndx, reloc_dst->r_symndx); |
| 104 | bfd_h_put_16(abfd, reloc_src->r_type, reloc_dst->r_type); |
| 105 | #if M88 |
| 106 | bfd_h_put_16(abfd, reloc_src->r_offset, reloc_dst->r_offset); |
| 107 | #endif |
| 108 | |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | DEFUN(bfd_swap_filehdr_in,(abfd, filehdr_src, filehdr_dst), |
| 113 | bfd *abfd AND |
| 114 | FILHDR *filehdr_src AND |
| 115 | struct internal_filehdr *filehdr_dst) |
| 116 | { |
| 117 | filehdr_dst->f_magic = bfd_h_get_16(abfd, filehdr_src->f_magic); |
| 118 | filehdr_dst->f_nscns = bfd_h_get_16(abfd,filehdr_src-> f_nscns); |
| 119 | filehdr_dst->f_timdat = bfd_h_get_32(abfd,filehdr_src-> f_timdat); |
| 120 | filehdr_dst->f_symptr = bfd_h_get_32(abfd,filehdr_src-> f_symptr); |
| 121 | filehdr_dst->f_nsyms = bfd_h_get_32(abfd,filehdr_src-> f_nsyms); |
| 122 | filehdr_dst->f_opthdr = bfd_h_get_16(abfd,filehdr_src-> f_opthdr); |
| 123 | filehdr_dst->f_flags = bfd_h_get_16(abfd,filehdr_src-> f_flags); |
| 124 | } |
| 125 | |
| 126 | GDB_EXPORT void |
| 127 | DEFUN(bfd_swap_filehdr_out,(abfd, filehdr_in, filehdr_out), |
| 128 | bfd *abfd AND |
| 129 | struct internal_filehdr *filehdr_in AND |
| 130 | FILHDR *filehdr_out) |
| 131 | { |
| 132 | bfd_h_put_16(abfd, filehdr_in->f_magic, filehdr_out->f_magic); |
| 133 | bfd_h_put_16(abfd, filehdr_in->f_nscns, filehdr_out->f_nscns); |
| 134 | bfd_h_put_32(abfd, filehdr_in->f_timdat, filehdr_out->f_timdat); |
| 135 | bfd_h_put_32(abfd, filehdr_in->f_symptr, filehdr_out->f_symptr); |
| 136 | bfd_h_put_32(abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms); |
| 137 | bfd_h_put_16(abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr); |
| 138 | bfd_h_put_16(abfd, filehdr_in->f_flags, filehdr_out->f_flags); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | static void |
| 144 | DEFUN(coff_swap_sym_in,(abfd, ext, in), |
| 145 | bfd *abfd AND |
| 146 | SYMENT *ext AND |
| 147 | struct internal_syment *in) |
| 148 | { |
| 149 | if( ext->e.e_name[0] == 0) { |
| 150 | in->_n._n_n._n_zeroes = 0; |
| 151 | in->_n._n_n._n_offset = bfd_h_get_32(abfd, ext->e.e.e_offset); |
| 152 | } |
| 153 | else { |
| 154 | memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN); |
| 155 | } |
| 156 | in->n_value = bfd_h_get_32(abfd, ext->e_value); |
| 157 | in->n_scnum = bfd_h_get_16(abfd, ext->e_scnum); |
| 158 | if (sizeof(ext->e_type) == 2){ |
| 159 | in->n_type = bfd_h_get_16(abfd, ext->e_type); |
| 160 | } |
| 161 | else { |
| 162 | in->n_type = bfd_h_get_32(abfd, ext->e_type); |
| 163 | } |
| 164 | in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass); |
| 165 | in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux); |
| 166 | } |
| 167 | |
| 168 | GDB_EXPORT void |
| 169 | DEFUN(coff_swap_sym_out,(abfd,in, ext), |
| 170 | bfd *abfd AND |
| 171 | struct internal_syment *in AND |
| 172 | SYMENT *ext) |
| 173 | { |
| 174 | if(in->_n._n_name[0] == 0) { |
| 175 | bfd_h_put_32(abfd, 0, ext->e.e.e_zeroes); |
| 176 | bfd_h_put_32(abfd, in->_n._n_n._n_offset, ext->e.e.e_offset); |
| 177 | } |
| 178 | else { |
| 179 | memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN); |
| 180 | } |
| 181 | bfd_h_put_32(abfd, in->n_value , ext->e_value); |
| 182 | bfd_h_put_16(abfd, in->n_scnum , ext->e_scnum); |
| 183 | if (sizeof(ext->e_type) == 2) |
| 184 | { |
| 185 | bfd_h_put_16(abfd, in->n_type , ext->e_type); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | bfd_h_put_32(abfd, in->n_type , ext->e_type); |
| 190 | } |
| 191 | bfd_h_put_8(abfd, in->n_sclass , ext->e_sclass); |
| 192 | bfd_h_put_8(abfd, in->n_numaux , ext->e_numaux); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | DEFUN(coff_swap_aux_in,(abfd, ext, type, class, in), |
| 197 | bfd *abfd AND |
| 198 | AUXENT *ext AND |
| 199 | int type AND |
| 200 | int class AND |
| 201 | union internal_auxent *in) |
| 202 | { |
| 203 | switch (class) { |
| 204 | case C_FILE: |
| 205 | if (ext->x_file.x_fname[0] == 0) { |
| 206 | in->x_file.x_n.x_zeroes = 0; |
| 207 | in->x_file.x_n.x_offset = bfd_h_get_32(abfd, ext->x_file.x_n.x_offset); |
| 208 | } else { |
| 209 | memcpy (in->x_file.x_fname, ext->x_file.x_fname, |
| 210 | sizeof (in->x_file.x_fname)); |
| 211 | } |
| 212 | |
| 213 | break; |
| 214 | case C_STAT: |
| 215 | #ifdef C_LEAFSTAT |
| 216 | case C_LEAFSTAT: |
| 217 | #endif |
| 218 | case C_HIDDEN: |
| 219 | if (type == T_NULL) { |
| 220 | in->x_scn.x_scnlen = bfd_h_get_32(abfd, ext->x_scn.x_scnlen); |
| 221 | in->x_scn.x_nreloc = bfd_h_get_16(abfd, ext->x_scn.x_nreloc); |
| 222 | in->x_scn.x_nlinno = bfd_h_get_16(abfd, ext->x_scn.x_nlinno); |
| 223 | break; |
| 224 | } |
| 225 | default: |
| 226 | in->x_sym.x_tagndx = bfd_h_get_32(abfd, ext->x_sym.x_tagndx); |
| 227 | in->x_sym.x_tvndx = bfd_h_get_16(abfd, ext->x_sym.x_tvndx); |
| 228 | |
| 229 | if (ISARY(type) || class == C_BLOCK) { |
| 230 | in->x_sym.x_fcnary.x_ary.x_dimen[0] = bfd_h_get_16(abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]); |
| 231 | in->x_sym.x_fcnary.x_ary.x_dimen[1] = bfd_h_get_16(abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]); |
| 232 | in->x_sym.x_fcnary.x_ary.x_dimen[2] = bfd_h_get_16(abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]); |
| 233 | in->x_sym.x_fcnary.x_ary.x_dimen[3] = bfd_h_get_16(abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]); |
| 234 | } |
| 235 | else { |
| 236 | in->x_sym.x_fcnary.x_fcn.x_lnnoptr = bfd_h_get_32(abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr); |
| 237 | in->x_sym.x_fcnary.x_fcn.x_endndx = bfd_h_get_32(abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx); |
| 238 | } |
| 239 | if (ISFCN(type)) { |
| 240 | in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, ext->x_sym.x_misc.x_fsize); |
| 241 | } |
| 242 | else { |
| 243 | in->x_sym.x_misc.x_lnsz.x_lnno = bfd_h_get_16(abfd, ext->x_sym.x_misc.x_lnsz.x_lnno); |
| 244 | in->x_sym.x_misc.x_lnsz.x_size = bfd_h_get_16(abfd, ext->x_sym.x_misc.x_lnsz.x_size); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | GDB_EXPORT void |
| 250 | DEFUN(coff_swap_aux_out,(abfd, in, type, class, ext), |
| 251 | bfd *abfd AND |
| 252 | union internal_auxent *in AND |
| 253 | int type AND |
| 254 | int class AND |
| 255 | AUXENT *ext) |
| 256 | { |
| 257 | switch (class) { |
| 258 | case C_FILE: |
| 259 | if (in->x_file.x_fname[0] == 0) { |
| 260 | PUTWORD(abfd, 0, ext->x_file.x_n.x_zeroes ); |
| 261 | PUTWORD(abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset); |
| 262 | } |
| 263 | |
| 264 | break; |
| 265 | case C_STAT: |
| 266 | #ifdef C_LEAFSTAT |
| 267 | case C_LEAFSTAT: |
| 268 | #endif |
| 269 | case C_HIDDEN: |
| 270 | if (type == T_NULL) { |
| 271 | PUTWORD(abfd, in->x_scn.x_scnlen, ext->x_scn.x_scnlen); |
| 272 | PUTWORD(abfd, in->x_scn.x_nreloc, ext->x_scn.x_nreloc); |
| 273 | PUTWORD(abfd, in->x_scn.x_nlinno, ext->x_scn.x_nlinno); |
| 274 | break; |
| 275 | } |
| 276 | default: |
| 277 | PUTWORD(abfd, in->x_sym.x_tagndx, ext->x_sym.x_tagndx); |
| 278 | PUTWORD(abfd, in->x_sym.x_tvndx , ext->x_sym.x_tvndx); |
| 279 | |
| 280 | if (ISARY(type) || class == C_BLOCK) { |
| 281 | bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],ext->x_sym.x_fcnary.x_ary.x_dimen[0]); |
| 282 | bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],ext->x_sym.x_fcnary.x_ary.x_dimen[1]); |
| 283 | bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],ext->x_sym.x_fcnary.x_ary.x_dimen[2]); |
| 284 | bfd_h_put_16(abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],ext->x_sym.x_fcnary.x_ary.x_dimen[3]); |
| 285 | } |
| 286 | else { |
| 287 | PUTWORD(abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr); |
| 288 | PUTWORD(abfd, in->x_sym.x_fcnary.x_fcn.x_endndx, ext->x_sym.x_fcnary.x_fcn.x_endndx); |
| 289 | } |
| 290 | if (ISFCN(type)) { |
| 291 | PUTWORD(abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize); |
| 292 | } |
| 293 | else { |
| 294 | bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext->x_sym.x_misc.x_lnsz.x_lnno); |
| 295 | bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_size, ext->x_sym.x_misc.x_lnsz.x_size); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | GDB_EXPORT void |
| 301 | DEFUN(coff_swap_lineno_in,(abfd, ext, in), |
| 302 | bfd *abfd AND |
| 303 | LINENO *ext AND |
| 304 | struct internal_lineno *in) |
| 305 | { |
| 306 | in->l_addr.l_symndx = bfd_h_get_32(abfd, ext->l_addr.l_symndx); |
| 307 | in->l_lnno = bfd_h_get_16(abfd, ext->l_lnno); |
| 308 | } |
| 309 | |
| 310 | GDB_EXPORT void |
| 311 | DEFUN(coff_swap_lineno_out,(abfd, in, ext), |
| 312 | bfd *abfd AND |
| 313 | struct internal_lineno *in AND |
| 314 | struct external_lineno *ext) |
| 315 | { |
| 316 | PUTWORD(abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx); |
| 317 | PUTHALF(abfd, in->l_lnno, ext->l_lnno); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | GDB_EXPORT void |
| 324 | DEFUN(bfd_swap_aouthdr_in,(abfd, aouthdr_ext, aouthdr_int), |
| 325 | bfd *abfd AND |
| 326 | AOUTHDR *aouthdr_ext AND |
| 327 | struct internal_aouthdr *aouthdr_int) |
| 328 | { |
| 329 | aouthdr_int->magic = bfd_h_get_16(abfd, aouthdr_ext->magic); |
| 330 | aouthdr_int->vstamp = bfd_h_get_16(abfd, aouthdr_ext->vstamp); |
| 331 | aouthdr_int->tsize = bfd_h_get_32(abfd, aouthdr_ext->tsize); |
| 332 | aouthdr_int->dsize = bfd_h_get_32(abfd, aouthdr_ext->dsize); |
| 333 | aouthdr_int->bsize = bfd_h_get_32(abfd, aouthdr_ext->bsize); |
| 334 | aouthdr_int->entry = bfd_h_get_32(abfd, aouthdr_ext->entry); |
| 335 | aouthdr_int->text_start = bfd_h_get_32(abfd, aouthdr_ext->text_start); |
| 336 | aouthdr_int->data_start = bfd_h_get_32(abfd, aouthdr_ext->data_start); |
| 337 | #ifdef I960 |
| 338 | aouthdr_int->tagentries = bfd_h_get_32(abfd, aouthdr_ext->tagentries); |
| 339 | #endif |
| 340 | } |
| 341 | |
| 342 | GDB_EXPORT void |
| 343 | DEFUN(bfd_swap_aouthdr_out,(abfd, aouthdr_in, aouthdr_out), |
| 344 | bfd *abfd AND |
| 345 | struct internal_aouthdr *aouthdr_in AND |
| 346 | AOUTHDR *aouthdr_out) |
| 347 | { |
| 348 | bfd_h_put_16(abfd, aouthdr_in->magic, aouthdr_out->magic); |
| 349 | bfd_h_put_16(abfd, aouthdr_in->vstamp, aouthdr_out->vstamp); |
| 350 | bfd_h_put_32(abfd, aouthdr_in->tsize, aouthdr_out->tsize); |
| 351 | bfd_h_put_32(abfd, aouthdr_in->dsize, aouthdr_out->dsize); |
| 352 | bfd_h_put_32(abfd, aouthdr_in->bsize, aouthdr_out->bsize); |
| 353 | bfd_h_put_32(abfd, aouthdr_in->entry, aouthdr_out->entry); |
| 354 | bfd_h_put_32(abfd, aouthdr_in->text_start, aouthdr_out->text_start); |
| 355 | bfd_h_put_32(abfd, aouthdr_in->data_start, aouthdr_out->data_start); |
| 356 | #ifdef I960 |
| 357 | bfd_h_put_32(abfd, aouthdr_in->tagentries, aouthdr_out->tagentries); |
| 358 | #endif |
| 359 | } |
| 360 | |
| 361 | GDB_EXPORT void |
| 362 | DEFUN(coff_swap_scnhdr_in,(abfd, scnhdr_ext, scnhdr_int), |
| 363 | bfd *abfd AND |
| 364 | SCNHDR *scnhdr_ext AND |
| 365 | struct internal_scnhdr *scnhdr_int) |
| 366 | { |
| 367 | memcpy(scnhdr_int->s_name, scnhdr_ext->s_name, sizeof(scnhdr_int->s_name)); |
| 368 | scnhdr_int->s_vaddr = bfd_h_get_32(abfd, scnhdr_ext->s_vaddr); |
| 369 | scnhdr_int->s_paddr = bfd_h_get_32(abfd, scnhdr_ext->s_paddr); |
| 370 | scnhdr_int->s_size = bfd_h_get_32(abfd, scnhdr_ext->s_size); |
| 371 | scnhdr_int->s_scnptr = bfd_h_get_32(abfd, scnhdr_ext->s_scnptr); |
| 372 | scnhdr_int->s_relptr = bfd_h_get_32(abfd, scnhdr_ext->s_relptr); |
| 373 | scnhdr_int->s_lnnoptr = bfd_h_get_32(abfd, scnhdr_ext->s_lnnoptr); |
| 374 | scnhdr_int->s_nreloc = bfd_h_get_16(abfd, scnhdr_ext->s_nreloc); |
| 375 | scnhdr_int->s_nlnno = bfd_h_get_16(abfd, scnhdr_ext->s_nlnno); |
| 376 | scnhdr_int->s_flags = bfd_h_get_32(abfd, scnhdr_ext->s_flags); |
| 377 | #ifdef I960 |
| 378 | scnhdr_int->s_align = bfd_h_get_32(abfd, scnhdr_ext->s_align); |
| 379 | #endif |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | DEFUN(swap_scnhdr_out,(abfd, scnhdr_int, scnhdr_ext), |
| 384 | bfd *abfd AND |
| 385 | struct internal_scnhdr *scnhdr_int AND |
| 386 | SCNHDR *scnhdr_ext) |
| 387 | { |
| 388 | memcpy(scnhdr_ext->s_name, scnhdr_int->s_name, sizeof(scnhdr_int->s_name)); |
| 389 | PUTWORD(abfd, scnhdr_int->s_vaddr, scnhdr_ext->s_vaddr); |
| 390 | PUTWORD(abfd, scnhdr_int->s_paddr, scnhdr_ext->s_paddr); |
| 391 | PUTWORD(abfd, scnhdr_int->s_size, scnhdr_ext->s_size); |
| 392 | PUTWORD(abfd, scnhdr_int->s_scnptr, scnhdr_ext->s_scnptr); |
| 393 | PUTWORD(abfd, scnhdr_int->s_relptr, scnhdr_ext->s_relptr); |
| 394 | PUTWORD(abfd, scnhdr_int->s_lnnoptr, scnhdr_ext->s_lnnoptr); |
| 395 | PUTWORD(abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc); |
| 396 | PUTHALF(abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno); |
| 397 | PUTHALF(abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags); |
| 398 | #ifdef I960 |
| 399 | PUTWORD(abfd, scnhdr_int->s_align, scnhdr_ext->s_align); |
| 400 | #endif |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | initialize a section structure with information peculiar to this |
| 405 | particular implementation of coff |
| 406 | */ |
| 407 | |
| 408 | static boolean |
| 409 | DEFUN(coff_new_section_hook,(abfd_ignore, section_ignore), |
| 410 | bfd *abfd_ignore AND |
| 411 | asection *section_ignore) |
| 412 | { |
| 413 | #ifdef MC88MAGIC |
| 414 | /* FIXME, shouldn't this ifdef be on something that says we are |
| 415 | actually COMPILING FOR an 88K coff file, rather than simply |
| 416 | knowing its magic number? */ |
| 417 | /* Align to at least 16 bytes */ |
| 418 | section_ignore->alignment_power = 4; |
| 419 | #endif |
| 420 | #if M68 |
| 421 | section_ignore->alignment_power = 3; |
| 422 | #endif |
| 423 | #if I386 |
| 424 | section_ignore->alignment_power = 2; |
| 425 | #endif |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | /* Take a section header read from a coff file (in HOST byte order), |
| 430 | and make a BFD "section" out of it. */ |
| 431 | static boolean |
| 432 | DEFUN(make_a_section_from_file,(abfd, hdr), |
| 433 | bfd *abfd AND |
| 434 | struct internal_scnhdr *hdr) |
| 435 | { |
| 436 | asection *return_section; |
| 437 | |
| 438 | { |
| 439 | /* Assorted wastage to null-terminate the name, thanks AT&T! */ |
| 440 | char *name = bfd_alloc(abfd, sizeof (hdr->s_name)+1); |
| 441 | if (name == NULL) { |
| 442 | bfd_error = no_memory; |
| 443 | return false; |
| 444 | } |
| 445 | strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name)); |
| 446 | name[sizeof (hdr->s_name)] = 0; |
| 447 | |
| 448 | return_section = bfd_make_section(abfd, name); |
| 449 | } |
| 450 | |
| 451 | /* s_paddr is presumed to be = to s_vaddr */ |
| 452 | #define assign(to, from) return_section->to = hdr->from |
| 453 | assign(vma, s_vaddr); |
| 454 | /* assign (vma, s_vaddr); */ |
| 455 | assign(size, s_size); |
| 456 | assign(filepos, s_scnptr); |
| 457 | assign(rel_filepos, s_relptr); |
| 458 | assign(reloc_count, s_nreloc); |
| 459 | #ifdef I960 |
| 460 | { |
| 461 | /* FIXME, use a temp var rather than alignment_power */ |
| 462 | assign(alignment_power, s_align); |
| 463 | { |
| 464 | unsigned int i; |
| 465 | for (i = 0; i < 32; i++) { |
| 466 | if ((1 << i) >= (int) (return_section->alignment_power)) { |
| 467 | return_section->alignment_power = i; |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | #endif |
| 474 | assign(line_filepos, s_lnnoptr); |
| 475 | /* |
| 476 | return_section->linesize = hdr->s_nlnno * sizeof (struct lineno); |
| 477 | */ |
| 478 | |
| 479 | #undef assign |
| 480 | return_section->lineno_count = hdr->s_nlnno; |
| 481 | return_section->userdata = NULL; |
| 482 | return_section->next = (asection *) NULL; |
| 483 | return_section->flags = 0; |
| 484 | if ((hdr->s_flags & STYP_TEXT) || (hdr->s_flags & STYP_DATA)) |
| 485 | return_section->flags = (SEC_LOAD | SEC_ALLOC); |
| 486 | else if (hdr->s_flags & STYP_BSS) |
| 487 | return_section->flags = SEC_ALLOC; |
| 488 | |
| 489 | if (hdr->s_nreloc != 0) |
| 490 | return_section->flags |= SEC_RELOC; |
| 491 | if (hdr->s_scnptr != 0) |
| 492 | return_section->flags |= SEC_HAS_CONTENTS; |
| 493 | return true; |
| 494 | } |
| 495 | static boolean |
| 496 | DEFUN(coff_mkobject,(abfd), |
| 497 | bfd *abfd) |
| 498 | { |
| 499 | set_tdata (abfd, bfd_zalloc (abfd,sizeof(coff_data_type))); |
| 500 | if (coff_data(abfd) == 0) { |
| 501 | bfd_error = no_memory; |
| 502 | return false; |
| 503 | } |
| 504 | coff_data(abfd)->relocbase = 0; |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | static |
| 509 | bfd_target * |
| 510 | DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a), |
| 511 | bfd *abfd AND |
| 512 | unsigned nscns AND |
| 513 | struct internal_filehdr *internal_f AND |
| 514 | struct internal_aouthdr *internal_a) |
| 515 | { |
| 516 | coff_data_type *coff; |
| 517 | |
| 518 | size_t readsize; /* length of file_info */ |
| 519 | SCNHDR *external_sections; |
| 520 | |
| 521 | /* Build a play area */ |
| 522 | if (coff_mkobject(abfd) != true) |
| 523 | return 0; |
| 524 | coff = coff_data(abfd); |
| 525 | |
| 526 | |
| 527 | external_sections = (SCNHDR *)bfd_alloc(abfd, readsize = (nscns * SCNHSZ)); |
| 528 | if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) { |
| 529 | goto fail; |
| 530 | } |
| 531 | |
| 532 | |
| 533 | |
| 534 | /* Now copy data as required; construct all asections etc */ |
| 535 | coff->symbol_index_slew = 0; |
| 536 | coff->relocbase =0; |
| 537 | coff->raw_syment_count = 0; |
| 538 | coff->raw_linenos = 0; |
| 539 | coff->raw_syments = 0; |
| 540 | coff->sym_filepos =0; |
| 541 | coff->flags = internal_f->f_flags; |
| 542 | if (nscns != 0) { |
| 543 | unsigned int i; |
| 544 | for (i = 0; i < nscns; i++) { |
| 545 | struct internal_scnhdr tmp; |
| 546 | coff_swap_scnhdr_in(abfd, external_sections + i, &tmp); |
| 547 | make_a_section_from_file(abfd,&tmp); |
| 548 | } |
| 549 | } |
| 550 | /* Determine the machine architecture and type. */ |
| 551 | abfd->obj_machine = 0; |
| 552 | switch (internal_f->f_magic) { |
| 553 | #ifdef I386MAGIC |
| 554 | case I386MAGIC: |
| 555 | abfd->obj_arch = bfd_arch_i386; |
| 556 | abfd->obj_machine = 0; |
| 557 | break; |
| 558 | #endif |
| 559 | #ifdef MIPS |
| 560 | case MIPS_MAGIC_1: |
| 561 | case MIPS_MAGIC_2: |
| 562 | case MIPS_MAGIC_3: |
| 563 | abfd->obj_arch = bfd_arch_mips; |
| 564 | abfd->obj_machine = 0; |
| 565 | break; |
| 566 | #endif |
| 567 | |
| 568 | #ifdef MC68MAGIC |
| 569 | case MC68MAGIC: |
| 570 | case M68MAGIC: |
| 571 | abfd->obj_arch = bfd_arch_m68k; |
| 572 | abfd->obj_machine = 68020; |
| 573 | break; |
| 574 | #endif |
| 575 | #ifdef MC88MAGIC |
| 576 | case MC88MAGIC: |
| 577 | case MC88DMAGIC: |
| 578 | case MC88OMAGIC: |
| 579 | abfd->obj_arch = bfd_arch_m88k; |
| 580 | abfd->obj_machine = 88100; |
| 581 | break; |
| 582 | #endif |
| 583 | #ifdef I960 |
| 584 | #ifdef I960ROMAGIC |
| 585 | case I960ROMAGIC: |
| 586 | case I960RWMAGIC: |
| 587 | abfd->obj_arch = bfd_arch_i960; |
| 588 | switch (F_I960TYPE & internal_f->f_flags) |
| 589 | { |
| 590 | default: |
| 591 | case F_I960CORE: |
| 592 | abfd->obj_machine = bfd_mach_i960_core; |
| 593 | break; |
| 594 | case F_I960KB: |
| 595 | abfd->obj_machine = bfd_mach_i960_kb_sb; |
| 596 | break; |
| 597 | case F_I960MC: |
| 598 | abfd->obj_machine = bfd_mach_i960_mc; |
| 599 | break; |
| 600 | case F_I960XA: |
| 601 | abfd->obj_machine = bfd_mach_i960_xa; |
| 602 | break; |
| 603 | case F_I960CA: |
| 604 | abfd->obj_machine = bfd_mach_i960_ca; |
| 605 | break; |
| 606 | case F_I960KA: |
| 607 | abfd->obj_machine = bfd_mach_i960_ka_sa; |
| 608 | break; |
| 609 | |
| 610 | } |
| 611 | break; |
| 612 | #endif |
| 613 | #endif |
| 614 | |
| 615 | default: /* Unreadable input file type */ |
| 616 | abfd->obj_arch = bfd_arch_obscure; |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | if (!(internal_f->f_flags & F_RELFLG)) |
| 621 | abfd->flags |= HAS_RELOC; |
| 622 | if ((internal_f->f_flags & F_EXEC)) |
| 623 | abfd->flags |= EXEC_P; |
| 624 | if (!(internal_f->f_flags & F_LNNO)) |
| 625 | abfd->flags |= HAS_LINENO; |
| 626 | if (!(internal_f->f_flags & F_LSYMS)) |
| 627 | abfd->flags |= HAS_LOCALS; |
| 628 | |
| 629 | |
| 630 | bfd_get_symcount(abfd) = internal_f->f_nsyms; |
| 631 | if (internal_f->f_nsyms) |
| 632 | abfd->flags |= HAS_SYMS; |
| 633 | |
| 634 | coff->sym_filepos = internal_f->f_symptr; |
| 635 | |
| 636 | |
| 637 | |
| 638 | coff->symbols = (coff_symbol_type *) NULL; |
| 639 | bfd_get_start_address(abfd) = internal_f->f_opthdr ? internal_a->entry : 0; |
| 640 | |
| 641 | return abfd->xvec; |
| 642 | fail: |
| 643 | bfd_release(abfd, coff); |
| 644 | return (bfd_target *)NULL; |
| 645 | } |
| 646 | |
| 647 | static bfd_target * |
| 648 | DEFUN(coff_object_p,(abfd), |
| 649 | bfd *abfd) |
| 650 | { |
| 651 | int nscns; |
| 652 | FILHDR filehdr; |
| 653 | AOUTHDR opthdr; |
| 654 | struct internal_filehdr internal_f; |
| 655 | struct internal_aouthdr internal_a; |
| 656 | |
| 657 | bfd_error = system_call_error; |
| 658 | |
| 659 | /* figure out how much to read */ |
| 660 | if (bfd_read((PTR) &filehdr, 1, FILHSZ, abfd) != FILHSZ) |
| 661 | return 0; |
| 662 | |
| 663 | bfd_swap_filehdr_in(abfd, &filehdr, &internal_f); |
| 664 | |
| 665 | if (BADMAG(internal_f)) { |
| 666 | bfd_error = wrong_format; |
| 667 | return 0; |
| 668 | } |
| 669 | nscns =internal_f.f_nscns; |
| 670 | |
| 671 | if (internal_f.f_opthdr) { |
| 672 | if (bfd_read((PTR) &opthdr, 1,AOUTSZ, abfd) != AOUTSZ) { |
| 673 | return 0; |
| 674 | } |
| 675 | bfd_swap_aouthdr_in(abfd, &opthdr, &internal_a); |
| 676 | } |
| 677 | |
| 678 | /* Seek past the opt hdr stuff */ |
| 679 | bfd_seek(abfd, internal_f.f_opthdr + FILHSZ, SEEK_SET); |
| 680 | |
| 681 | /* if the optional header is NULL or not the correct size then |
| 682 | quit; the only difference I can see between m88k dgux headers (MC88DMAGIC) |
| 683 | and Intel 960 readwrite headers (I960WRMAGIC) is that the |
| 684 | optional header is of a different size. |
| 685 | |
| 686 | But the mips keeps extra stuff in it's opthdr, so dont check |
| 687 | when doing that |
| 688 | */ |
| 689 | |
| 690 | #ifndef MIPS |
| 691 | if (internal_f.f_opthdr != 0 && AOUTSZ != internal_f.f_opthdr) |
| 692 | return (bfd_target *)NULL; |
| 693 | #endif |
| 694 | |
| 695 | return coff_real_object_p(abfd, nscns, &internal_f, &internal_a); |
| 696 | } |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | /* |
| 702 | Takes a bfd and a symbol, returns a pointer to the coff specific area |
| 703 | of the symbol if there is one. |
| 704 | */ |
| 705 | static coff_symbol_type * |
| 706 | DEFUN(coff_symbol_from,(abfd, symbol), |
| 707 | bfd *abfd AND |
| 708 | asymbol *symbol) |
| 709 | { |
| 710 | if (symbol->the_bfd->xvec->flavour != bfd_target_coff_flavour_enum) |
| 711 | return (coff_symbol_type *)NULL; |
| 712 | |
| 713 | if (symbol->the_bfd->tdata == (PTR)NULL) |
| 714 | return (coff_symbol_type *)NULL; |
| 715 | |
| 716 | return (coff_symbol_type *) symbol; |
| 717 | } |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | static void |
| 726 | DEFUN(coff_count_linenumbers,(abfd), |
| 727 | bfd *abfd) |
| 728 | { |
| 729 | unsigned int limit = bfd_get_symcount(abfd); |
| 730 | unsigned int i; |
| 731 | asymbol **p; |
| 732 | { |
| 733 | asection *s = abfd->sections->output_section; |
| 734 | while (s) { |
| 735 | BFD_ASSERT(s->lineno_count == 0); |
| 736 | s = s->next; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | |
| 741 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) { |
| 742 | asymbol *q_maybe = *p; |
| 743 | if (q_maybe->the_bfd->xvec->flavour == bfd_target_coff_flavour_enum) { |
| 744 | coff_symbol_type *q = coffsymbol(q_maybe); |
| 745 | if (q->lineno) { |
| 746 | /* |
| 747 | This symbol has a linenumber, increment the owning |
| 748 | section's linenumber count |
| 749 | */ |
| 750 | alent *l = q->lineno; |
| 751 | q->symbol.section->output_section->lineno_count++; |
| 752 | l++; |
| 753 | while (l->line_number) { |
| 754 | q->symbol.section->output_section->lineno_count++; |
| 755 | l++; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | This function returns true if the supplied SYMENT has an AUXENT with |
| 764 | a tagndx field which should be relocated. |
| 765 | |
| 766 | The coff book says that all auxents have this and should be moved, |
| 767 | but all the actual implementations I've looked at do this .. |
| 768 | (sac@cygnus.com) |
| 769 | |
| 770 | */ |
| 771 | static boolean |
| 772 | DEFUN(uses_x_sym_x_tagndx_p,(abfd, native), |
| 773 | bfd *abfd AND |
| 774 | struct internal_syment *native) |
| 775 | { |
| 776 | if (BTYPE(native->n_type) == T_STRUCT) return true; |
| 777 | if (BTYPE(native->n_type) == T_UNION) return true; |
| 778 | if (BTYPE(native->n_type) == T_ENUM) return true; |
| 779 | return false; |
| 780 | } |
| 781 | |
| 782 | |
| 783 | /* |
| 784 | This procedure runs through the native entries in a coff symbol table |
| 785 | and links up all the elements which should point to one another, in |
| 786 | particular these are: |
| 787 | |
| 788 | strtag, entag and untags have an auxent endindex which points to the |
| 789 | first syment after the .eos. This is simple to do, we just keep a |
| 790 | pointer to the symbol with the most recent pending strtag and patch it |
| 791 | when we see the eos. This works since coff structs are never nested. |
| 792 | |
| 793 | ISFCN type entries have an endindex which points to the next static or |
| 794 | extern in the table, thereby skipping the function contents. |
| 795 | The coff book says that an ISFCN's tagindex |
| 796 | points to the first .bf for the function, so far I havn't seen it |
| 797 | used. We do this using the same mechanism as strtags. |
| 798 | |
| 799 | Each file entry has a value which points to the next file entry, |
| 800 | the last file entry points to the first extern symbol in the table |
| 801 | which is not an ISFCN. |
| 802 | |
| 803 | Each .bb entry points to the matching .eb entry, but these are nested |
| 804 | so we keep a stack of them. |
| 805 | |
| 806 | The tagndx of .eos items points to the strtag attached to them, this |
| 807 | is simply the last_tagndx again. |
| 808 | |
| 809 | The tagndx of items with type strtag point to the defining struct. |
| 810 | This bit is complicated; We know that a struct ref and def must be |
| 811 | within the same file, so all the natives will be in the same vector. |
| 812 | This means that we can subtracts two pointers and get the index |
| 813 | differences between to items, used to work out the true index of the |
| 814 | target. |
| 815 | |
| 816 | We store in the name field of each syment the actual native index |
| 817 | applied so we can dig it out through a pointer. */ |
| 818 | |
| 819 | static void |
| 820 | DEFUN(coff_mangle_symbols,(bfd_ptr), |
| 821 | bfd *bfd_ptr) |
| 822 | { |
| 823 | unsigned int symbol_count = bfd_get_symcount(bfd_ptr); |
| 824 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
| 825 | struct internal_syment *last_tagndx = (struct internal_syment *)NULL; |
| 826 | struct internal_syment *last_file = (struct internal_syment *)NULL; |
| 827 | struct internal_syment *last_fcn = (struct internal_syment *)NULL; |
| 828 | struct internal_syment *block_stack[50]; |
| 829 | struct internal_syment **last_block = &block_stack[0]; |
| 830 | boolean first_time = true; |
| 831 | unsigned int symbol_index; |
| 832 | unsigned int native_index = 0; |
| 833 | |
| 834 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) { |
| 835 | coff_symbol_type *coff_symbol_ptr = |
| 836 | coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]); |
| 837 | if (coff_symbol_ptr == (coff_symbol_type *)NULL) { |
| 838 | /* |
| 839 | This symbol has no coff information in it, it will take up |
| 840 | only one slot in the output symbol table |
| 841 | */ |
| 842 | native_index++; |
| 843 | } |
| 844 | else { |
| 845 | struct internal_syment *syment = coff_symbol_ptr->native; |
| 846 | if (syment == (struct internal_syment *)NULL) { |
| 847 | native_index++; |
| 848 | } |
| 849 | else { |
| 850 | /* Normalize the symbol flags */ |
| 851 | if (coff_symbol_ptr->symbol.flags & BSF_FORT_COMM) { |
| 852 | /* a common symbol is undefined with a value */ |
| 853 | syment->n_scnum = N_UNDEF; |
| 854 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 855 | } |
| 856 | else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) { |
| 857 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 858 | } |
| 859 | else if (coff_symbol_ptr->symbol.flags & BSF_UNDEFINED) { |
| 860 | syment->n_scnum = N_UNDEF; |
| 861 | syment->n_value = 0; |
| 862 | } |
| 863 | else if (coff_symbol_ptr->symbol.flags & BSF_ABSOLUTE) { |
| 864 | syment->n_scnum = N_ABS; |
| 865 | syment->n_value = coff_symbol_ptr->symbol.value; |
| 866 | } |
| 867 | else { |
| 868 | syment->n_scnum = |
| 869 | coff_symbol_ptr->symbol.section->output_section->index+1; |
| 870 | |
| 871 | syment->n_value = |
| 872 | coff_symbol_ptr->symbol.value + |
| 873 | coff_symbol_ptr->symbol.section->output_offset + |
| 874 | coff_symbol_ptr->symbol.section->output_section->vma; |
| 875 | } |
| 876 | |
| 877 | |
| 878 | /* If this symbol ties up something then do it */ |
| 879 | |
| 880 | if (syment->n_sclass == C_FILE && last_file != (struct internal_syment *)NULL) |
| 881 | { |
| 882 | last_file->n_value = native_index; |
| 883 | } |
| 884 | else if ((syment->n_sclass == C_EXT |
| 885 | || syment->n_sclass == C_STAT |
| 886 | #ifdef C_LEAFEXT |
| 887 | || syment->n_sclass == C_LEAFEXT |
| 888 | || syment->n_sclass == C_LEAFSTAT |
| 889 | #endif |
| 890 | ) |
| 891 | && last_fcn != (struct internal_syment *)NULL) |
| 892 | { |
| 893 | union internal_auxent *auxent = (union internal_auxent *)(last_fcn+1); |
| 894 | auxent->x_sym.x_fcnary.x_fcn.x_endndx = native_index; |
| 895 | last_fcn = (struct internal_syment *)NULL; |
| 896 | |
| 897 | } |
| 898 | else if (syment->n_sclass == C_EOS && last_tagndx != (struct internal_syment*)NULL) |
| 899 | { |
| 900 | union internal_auxent *auxent = (union internal_auxent *)(last_tagndx+1); |
| 901 | /* Remember that we keep the native index in the offset |
| 902 | so patch the beginning of the struct to point to this |
| 903 | */ |
| 904 | auxent->x_sym.x_tagndx = last_tagndx->_n._n_n._n_offset; |
| 905 | auxent->x_sym.x_fcnary.x_fcn.x_endndx = syment->n_numaux + 1 + native_index; |
| 906 | /* Now point the eos to the structure */ |
| 907 | auxent = (union internal_auxent *)(syment+1); |
| 908 | auxent->x_sym.x_tagndx = last_tagndx->_n._n_n._n_offset; |
| 909 | } |
| 910 | else if (syment->n_sclass == C_BLOCK |
| 911 | && coff_symbol_ptr->symbol.name[1] == 'e') |
| 912 | { |
| 913 | union internal_auxent *auxent = (union internal_auxent *)((*(--last_block))+1); |
| 914 | auxent->x_sym.x_fcnary.x_fcn.x_endndx = native_index + syment->n_numaux + 1; |
| 915 | } |
| 916 | if (syment->n_sclass == C_EXT |
| 917 | && !ISFCN(syment->n_type) |
| 918 | && first_time == true |
| 919 | && last_file != (struct internal_syment *)NULL) { |
| 920 | /* This is the first external symbol seen which isn't a |
| 921 | function place it in the last .file entry */ |
| 922 | last_file->n_value = native_index; |
| 923 | first_time = false; |
| 924 | } |
| 925 | #ifdef C_LEAFPROC |
| 926 | if (syment->n_sclass == C_LEAFPROC && |
| 927 | syment->n_numaux == 2) { |
| 928 | union internal_auxent *auxent = (union internal_auxent *)(syment+2); |
| 929 | /* This is the definition of a leaf proc, we'll relocate the |
| 930 | address */ |
| 931 | auxent->x_bal.x_balntry = |
| 932 | coff_symbol_ptr->symbol.section->output_offset + |
| 933 | coff_symbol_ptr->symbol.section->output_section->vma + |
| 934 | auxent->x_bal.x_balntry ; |
| 935 | } |
| 936 | #endif |
| 937 | /* If this symbol needs to be tied up then remember some facts */ |
| 938 | if (syment->n_sclass == C_FILE) |
| 939 | { |
| 940 | last_file = syment; |
| 941 | } |
| 942 | if (syment->n_numaux != 0) { |
| 943 | /* |
| 944 | If this symbol would like to point to something in the |
| 945 | future then remember where it is |
| 946 | */ |
| 947 | if (uses_x_sym_x_tagndx_p(bfd_ptr, syment)) { |
| 948 | /* |
| 949 | If this is a ref to a structure then we'll tie it up |
| 950 | now - there are never any forward refs for one |
| 951 | */ |
| 952 | if (syment->n_sclass == C_STRTAG || |
| 953 | syment->n_sclass == C_ENTAG || |
| 954 | syment->n_sclass == C_UNTAG) { |
| 955 | last_tagndx = syment; |
| 956 | } |
| 957 | else { |
| 958 | /* |
| 959 | This is a ref to a structure - the structure must |
| 960 | have been defined within the same file, and previous |
| 961 | to this point, so we can deduce the new tagndx |
| 962 | directly. |
| 963 | */ |
| 964 | union internal_auxent *auxent = (union internal_auxent *)(syment+1); |
| 965 | bfd *bfd_ptr = coff_symbol_ptr->symbol.the_bfd; |
| 966 | struct internal_syment *base = obj_raw_syments(bfd_ptr); |
| 967 | auxent->x_sym.x_tagndx = base[auxent->x_sym.x_tagndx]._n._n_n._n_offset; |
| 968 | |
| 969 | |
| 970 | } |
| 971 | } |
| 972 | if (ISFCN(syment->n_type)) { |
| 973 | last_fcn = syment; |
| 974 | } |
| 975 | if (syment->n_sclass == C_BLOCK |
| 976 | && coff_symbol_ptr->symbol.name[1] == 'b') |
| 977 | { |
| 978 | *last_block++ = syment; |
| 979 | } |
| 980 | } |
| 981 | syment->_n._n_n._n_offset = native_index; |
| 982 | native_index = native_index + 1 + syment->n_numaux; |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | |
| 989 | static void |
| 990 | DEFUN(coff_write_symbols,(abfd), |
| 991 | bfd *abfd) |
| 992 | { |
| 993 | unsigned int i; |
| 994 | unsigned int limit = bfd_get_symcount(abfd); |
| 995 | unsigned int written = 0; |
| 996 | struct internal_syment dummy; |
| 997 | asymbol **p; |
| 998 | unsigned int string_size = 0; |
| 999 | |
| 1000 | |
| 1001 | /* Seek to the right place */ |
| 1002 | bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET); |
| 1003 | |
| 1004 | /* Output all the symbols we have */ |
| 1005 | |
| 1006 | written = 0; |
| 1007 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) { |
| 1008 | asymbol *symbol = *p; |
| 1009 | coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol); |
| 1010 | |
| 1011 | unsigned int j; |
| 1012 | struct internal_syment *native; |
| 1013 | if (c_symbol == (coff_symbol_type *) NULL || |
| 1014 | c_symbol->native == (struct internal_syment *) NULL) { |
| 1015 | /* |
| 1016 | This symbol has been created by the loader, or come from a non |
| 1017 | coff format. It has no native element to inherit, make our |
| 1018 | own |
| 1019 | */ |
| 1020 | |
| 1021 | native = &dummy; |
| 1022 | native->n_type = T_NULL; |
| 1023 | #ifdef I960 |
| 1024 | native->n_flags = 0; |
| 1025 | #endif |
| 1026 | if (symbol->flags & BSF_ABSOLUTE) { |
| 1027 | native->n_scnum = N_ABS; |
| 1028 | native->n_value = symbol->value; |
| 1029 | } |
| 1030 | else if (symbol->flags & (BSF_UNDEFINED | BSF_FORT_COMM)) { |
| 1031 | native->n_scnum = N_UNDEF; |
| 1032 | native->n_value = symbol->value; |
| 1033 | } |
| 1034 | else if (symbol->flags & BSF_DEBUGGING) { |
| 1035 | /* |
| 1036 | remove name so it doesn't take up any space |
| 1037 | */ |
| 1038 | symbol->name = ""; |
| 1039 | continue; |
| 1040 | } |
| 1041 | else { |
| 1042 | native->n_scnum = symbol->section->output_section->index + |
| 1043 | 1; |
| 1044 | native->n_value = symbol->value + |
| 1045 | symbol->section->output_section->vma + |
| 1046 | symbol->section->output_offset; |
| 1047 | #ifdef I960 |
| 1048 | /* Copy the any flags from the the file hdr into the symbol */ |
| 1049 | { |
| 1050 | coff_symbol_type *c = coff_symbol_from(abfd, symbol); |
| 1051 | if (c != (coff_symbol_type *)NULL) { |
| 1052 | native->n_flags = c->symbol.the_bfd->flags; |
| 1053 | } |
| 1054 | } |
| 1055 | #endif |
| 1056 | } |
| 1057 | |
| 1058 | #ifdef HASPAD1 |
| 1059 | native->pad1[0] = 0; |
| 1060 | native->pad1[0] = 0; |
| 1061 | #endif |
| 1062 | |
| 1063 | native->n_type = 0; |
| 1064 | if (symbol->flags & BSF_LOCAL) |
| 1065 | native->n_sclass = C_STAT; |
| 1066 | else |
| 1067 | native->n_sclass = C_EXT; |
| 1068 | native->n_numaux = 0; |
| 1069 | } |
| 1070 | else |
| 1071 | /* |
| 1072 | Does this symbol have an ascociated line number - if so then |
| 1073 | make it remember this symbol index. Also tag the auxent of |
| 1074 | this symbol to point to the right place in the lineno table |
| 1075 | */ |
| 1076 | { |
| 1077 | alent *lineno = c_symbol->lineno; |
| 1078 | native = c_symbol->native; |
| 1079 | if (lineno) { |
| 1080 | unsigned int count = 0; |
| 1081 | lineno[count].u.offset = written; |
| 1082 | if (native->n_numaux) { |
| 1083 | union internal_auxent *a = (union internal_auxent *) (native + 1); |
| 1084 | |
| 1085 | a->x_sym.x_fcnary.x_fcn.x_lnnoptr = |
| 1086 | c_symbol->symbol.section->output_section->moving_line_filepos; |
| 1087 | } |
| 1088 | /* |
| 1089 | And count and relocate all other linenumbers |
| 1090 | */ |
| 1091 | count++; |
| 1092 | while (lineno[count].line_number) { |
| 1093 | lineno[count].u.offset += |
| 1094 | c_symbol->symbol.section->output_section->vma + |
| 1095 | c_symbol->symbol.section->output_offset; |
| 1096 | count++; |
| 1097 | } |
| 1098 | c_symbol->symbol.section->output_section->moving_line_filepos += |
| 1099 | count * LINESZ; |
| 1100 | |
| 1101 | } |
| 1102 | } /* if symbol new to coff */ |
| 1103 | |
| 1104 | /* Fix the symbol names */ |
| 1105 | { |
| 1106 | unsigned int name_length; |
| 1107 | if (symbol->name == (char *) NULL) { |
| 1108 | /* |
| 1109 | coff symbols always have names, so we'll make one up |
| 1110 | */ |
| 1111 | symbol->name = "strange"; |
| 1112 | } |
| 1113 | name_length = strlen(symbol->name); |
| 1114 | if (name_length <= SYMNMLEN) { |
| 1115 | /* This name will fit into the symbol neatly */ |
| 1116 | strncpy(native->_n._n_name, symbol->name, SYMNMLEN); |
| 1117 | } |
| 1118 | else { |
| 1119 | native->_n._n_n._n_offset = string_size + 4; |
| 1120 | native->_n._n_n._n_zeroes = 0; |
| 1121 | string_size += name_length + 1; |
| 1122 | } |
| 1123 | { |
| 1124 | unsigned int numaux = native->n_numaux; |
| 1125 | int type = native->n_type; |
| 1126 | int class = native->n_sclass; |
| 1127 | SYMENT buf; |
| 1128 | coff_swap_sym_out(abfd, native, &buf); |
| 1129 | bfd_write((PTR)& buf, 1, SYMESZ, abfd); |
| 1130 | for (j = 0; j != native->n_numaux; |
| 1131 | j++) { |
| 1132 | AUXENT buf1; |
| 1133 | coff_swap_aux_out(abfd, |
| 1134 | (union internal_auxent *)(native + j + 1), type, class, &buf1); |
| 1135 | bfd_write((PTR) (native + j + 1), 1, AUXESZ, abfd); |
| 1136 | } |
| 1137 | /* |
| 1138 | Reuse somewhere in the symbol to keep the index |
| 1139 | */ |
| 1140 | set_index(symbol, written); |
| 1141 | written += 1 + numaux; |
| 1142 | } |
| 1143 | } |
| 1144 | } /* for each out symbol */ |
| 1145 | |
| 1146 | bfd_get_symcount(abfd) = written; |
| 1147 | /* Now write out strings */ |
| 1148 | |
| 1149 | if (string_size) { |
| 1150 | unsigned int size = string_size + 4; |
| 1151 | size = size; |
| 1152 | bfd_write((PTR) &size, 1, sizeof(size), abfd); |
| 1153 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) { |
| 1154 | asymbol *q = *p; |
| 1155 | size_t name_length = strlen(q->name); |
| 1156 | if (name_length > SYMNMLEN) { |
| 1157 | bfd_write((PTR) (q->name), 1, name_length + 1, abfd); |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | else { |
| 1162 | /* We would normally not write anything here, but we'll write |
| 1163 | out 4 so that any stupid coff reader which tries to read |
| 1164 | the string table even when there isn't one won't croak. |
| 1165 | */ |
| 1166 | |
| 1167 | uint32e_type size = 4; |
| 1168 | size = size; |
| 1169 | bfd_write((PTR)&size, 1, sizeof(size), abfd); |
| 1170 | |
| 1171 | } |
| 1172 | |
| 1173 | } |
| 1174 | |
| 1175 | static void |
| 1176 | coff_write_relocs(abfd) |
| 1177 | bfd *abfd; |
| 1178 | { |
| 1179 | asection *s; |
| 1180 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) { |
| 1181 | unsigned int i; |
| 1182 | struct external_reloc dst; |
| 1183 | |
| 1184 | arelent **p = s->orelocation; |
| 1185 | bfd_seek(abfd, s->rel_filepos, SEEK_SET); |
| 1186 | for (i = 0; i < s->reloc_count; i++) { |
| 1187 | struct internal_reloc n; |
| 1188 | arelent *q = p[i]; |
| 1189 | memset((PTR)&n, 0, sizeof(n)); |
| 1190 | n.r_vaddr = q->address + s->vma; |
| 1191 | if (q->sym_ptr_ptr) { |
| 1192 | n.r_symndx = get_index((*(q->sym_ptr_ptr))); |
| 1193 | } |
| 1194 | #ifdef SELECT_RELOC |
| 1195 | /* Work out reloc type from what is required */ |
| 1196 | SELECT_RELOC(n.r_type, q->howto); |
| 1197 | #else |
| 1198 | n.r_type = q->howto->type; |
| 1199 | #endif |
| 1200 | bfd_swap_reloc_out(abfd, &n, &dst); |
| 1201 | bfd_write((PTR) &n, 1, RELSZ, abfd); |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | static void |
| 1207 | DEFUN(coff_write_linenumbers,(abfd), |
| 1208 | bfd *abfd) |
| 1209 | { |
| 1210 | asection *s; |
| 1211 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) { |
| 1212 | if (s->lineno_count) { |
| 1213 | asymbol **q = abfd->outsymbols; |
| 1214 | bfd_seek(abfd, s->line_filepos, SEEK_SET); |
| 1215 | /* Find all the linenumbers in this section */ |
| 1216 | while (*q) { |
| 1217 | asymbol *p = *q; |
| 1218 | alent *l = BFD_SEND(p->the_bfd, _get_lineno, (p->the_bfd, p)); |
| 1219 | if (l) { |
| 1220 | /* Found a linenumber entry, output */ |
| 1221 | struct internal_lineno out; |
| 1222 | LINENO buff; |
| 1223 | bzero( (PTR)&out, sizeof(out)); |
| 1224 | out.l_lnno = 0; |
| 1225 | out.l_addr.l_symndx = l->u.offset; |
| 1226 | coff_swap_lineno_out(abfd, &out, &buff); |
| 1227 | bfd_write((PTR) &buff, 1, LINESZ, abfd); |
| 1228 | l++; |
| 1229 | while (l->line_number) { |
| 1230 | out.l_lnno = l->line_number; |
| 1231 | out.l_addr.l_symndx = l->u.offset; |
| 1232 | coff_swap_lineno_out(abfd, &out, &buff); |
| 1233 | bfd_write((PTR) &buff, 1, LINESZ, abfd); |
| 1234 | l++; |
| 1235 | } |
| 1236 | } |
| 1237 | q++; |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | |
| 1244 | static asymbol * |
| 1245 | coff_make_empty_symbol(abfd) |
| 1246 | bfd *abfd; |
| 1247 | { |
| 1248 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type)); |
| 1249 | if (new == NULL) { |
| 1250 | bfd_error = no_memory; |
| 1251 | return (NULL); |
| 1252 | } /* on error */ |
| 1253 | new->native = 0; |
| 1254 | new->lineno = (alent *) NULL; |
| 1255 | new->symbol.the_bfd = abfd; |
| 1256 | return &new->symbol; |
| 1257 | } |
| 1258 | |
| 1259 | static void |
| 1260 | coff_print_symbol(ignore_abfd, file, symbol, how) |
| 1261 | bfd *ignore_abfd; |
| 1262 | FILE *file; |
| 1263 | asymbol *symbol; |
| 1264 | bfd_print_symbol_enum_type how; |
| 1265 | { |
| 1266 | switch (how) { |
| 1267 | case bfd_print_symbol_name_enum: |
| 1268 | fprintf(file, "%s", symbol->name); |
| 1269 | break; |
| 1270 | case bfd_print_symbol_type_enum: |
| 1271 | fprintf(file, "coff %lx %lx", (unsigned long) coffsymbol(symbol)->native, |
| 1272 | (unsigned long) coffsymbol(symbol)->lineno); |
| 1273 | break; |
| 1274 | case bfd_print_symbol_all_enum: |
| 1275 | { |
| 1276 | CONST char *section_name = symbol->section == (asection *) NULL ? |
| 1277 | "*abs" : symbol->section->name; |
| 1278 | bfd_print_symbol_vandf((PTR) file, symbol); |
| 1279 | |
| 1280 | fprintf(file, " %-5s %s %s %s", |
| 1281 | section_name, |
| 1282 | coffsymbol(symbol)->native ? "n" : "g", |
| 1283 | coffsymbol(symbol)->lineno ? "l" : " ", |
| 1284 | symbol->name); |
| 1285 | } |
| 1286 | |
| 1287 | |
| 1288 | break; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | static alent * |
| 1293 | coff_get_lineno(ignore_abfd, symbol) |
| 1294 | bfd *ignore_abfd; |
| 1295 | asymbol *symbol; |
| 1296 | { |
| 1297 | return coffsymbol(symbol)->lineno; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | Set flags and magic number of a coff file from architecture and machine |
| 1302 | type. Result is true if we can represent the arch&type, false if not. |
| 1303 | */ |
| 1304 | static boolean |
| 1305 | coff_set_flags(abfd, magicp, flagsp) |
| 1306 | bfd *abfd; |
| 1307 | unsigned *magicp, |
| 1308 | *flagsp; |
| 1309 | { |
| 1310 | |
| 1311 | switch (abfd->obj_arch) { |
| 1312 | |
| 1313 | #ifdef I960ROMAGIC |
| 1314 | |
| 1315 | case bfd_arch_i960: |
| 1316 | |
| 1317 | { |
| 1318 | unsigned flags; |
| 1319 | *magicp = I960ROMAGIC; |
| 1320 | /* |
| 1321 | ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC : |
| 1322 | I960RWMAGIC); FIXME??? |
| 1323 | */ |
| 1324 | switch (abfd->obj_machine) { |
| 1325 | case bfd_mach_i960_core: |
| 1326 | flags = F_I960CORE; |
| 1327 | break; |
| 1328 | case bfd_mach_i960_kb_sb: |
| 1329 | flags = F_I960KB; |
| 1330 | break; |
| 1331 | case bfd_mach_i960_mc: |
| 1332 | flags = F_I960MC; |
| 1333 | break; |
| 1334 | case bfd_mach_i960_xa: |
| 1335 | flags = F_I960XA; |
| 1336 | break; |
| 1337 | case bfd_mach_i960_ca: |
| 1338 | flags = F_I960CA; |
| 1339 | break; |
| 1340 | case bfd_mach_i960_ka_sa: |
| 1341 | flags = F_I960KA; |
| 1342 | break; |
| 1343 | default: |
| 1344 | return false; |
| 1345 | } |
| 1346 | *flagsp = flags; |
| 1347 | return true; |
| 1348 | } |
| 1349 | break; |
| 1350 | #endif |
| 1351 | #ifdef MIPS |
| 1352 | case bfd_arch_mips: |
| 1353 | *magicp = MIPS_MAGIC_2; |
| 1354 | return true; |
| 1355 | break; |
| 1356 | #endif |
| 1357 | #ifdef I386MAGIC |
| 1358 | case bfd_arch_i386: |
| 1359 | *magicp = I386MAGIC; |
| 1360 | return true; |
| 1361 | #endif |
| 1362 | #ifdef MC68MAGIC |
| 1363 | case bfd_arch_m68k: |
| 1364 | *magicp = MC68MAGIC; |
| 1365 | return true; |
| 1366 | #endif |
| 1367 | |
| 1368 | #ifdef MC88MAGIC |
| 1369 | case bfd_arch_m88k: |
| 1370 | *magicp = MC88OMAGIC; |
| 1371 | return true; |
| 1372 | break; |
| 1373 | #endif |
| 1374 | |
| 1375 | default: /* Unknown architecture */ |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | |
| 1383 | static boolean |
| 1384 | coff_set_arch_mach(abfd, arch, machine) |
| 1385 | bfd *abfd; |
| 1386 | enum bfd_architecture arch; |
| 1387 | unsigned long machine; |
| 1388 | { |
| 1389 | unsigned dummy1, |
| 1390 | dummy2; |
| 1391 | abfd->obj_arch = arch; |
| 1392 | abfd->obj_machine = machine; |
| 1393 | if (arch != bfd_arch_unknown && |
| 1394 | coff_set_flags(abfd, &dummy1, &dummy2) != true) |
| 1395 | return false; /* We can't represent this type */ |
| 1396 | return true; /* We're easy ... */ |
| 1397 | } |
| 1398 | |
| 1399 | |
| 1400 | /* Calculate the file position for each section. */ |
| 1401 | |
| 1402 | static void |
| 1403 | coff_compute_section_file_positions(abfd) |
| 1404 | bfd *abfd; |
| 1405 | { |
| 1406 | asection *current; |
| 1407 | file_ptr sofar = FILHSZ; |
| 1408 | if (bfd_get_start_address(abfd)) { |
| 1409 | /* |
| 1410 | A start address may have been added to the original file. In this |
| 1411 | case it will need an optional header to record it. |
| 1412 | */ |
| 1413 | abfd->flags |= EXEC_P; |
| 1414 | } |
| 1415 | if (abfd->flags & EXEC_P) |
| 1416 | sofar += AOUTSZ; |
| 1417 | |
| 1418 | |
| 1419 | sofar += abfd->section_count * SCNHSZ; |
| 1420 | |
| 1421 | for (current = abfd->sections; current != NULL; current = |
| 1422 | current->next) { |
| 1423 | /* Only deal with sections which have contents */ |
| 1424 | if (!(current->flags & SEC_HAS_CONTENTS)) |
| 1425 | continue; |
| 1426 | |
| 1427 | /* Align the sections in the file to the same boundary on |
| 1428 | which they are aligned in virtual memory. I960 doesn't |
| 1429 | do this (FIXME) so we can stay in sync with Intel. 960 |
| 1430 | doesn't yet page from files... */ |
| 1431 | #ifndef I960 |
| 1432 | sofar = ALIGN(sofar, 1 << current->alignment_power); |
| 1433 | #endif |
| 1434 | /* FIXME, in demand paged files, the low order bits of the file |
| 1435 | offset must match the low order bits of the virtual address. |
| 1436 | "Low order" is apparently implementation defined. Add code |
| 1437 | here to round sofar up to match the virtual address. */ |
| 1438 | |
| 1439 | current->filepos = sofar; |
| 1440 | sofar += current->size; |
| 1441 | } |
| 1442 | obj_relocbase(abfd) = sofar; |
| 1443 | } |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | /* SUPPRESS 558 */ |
| 1449 | /* SUPPRESS 529 */ |
| 1450 | static boolean |
| 1451 | DEFUN(coff_write_object_contents,(abfd), |
| 1452 | bfd *abfd) |
| 1453 | { |
| 1454 | asection *current; |
| 1455 | boolean hasrelocs = false; |
| 1456 | boolean haslinno = false; |
| 1457 | file_ptr reloc_base; |
| 1458 | file_ptr lineno_base; |
| 1459 | file_ptr sym_base; |
| 1460 | file_ptr scn_base; |
| 1461 | file_ptr data_base; |
| 1462 | unsigned long reloc_size = 0; |
| 1463 | unsigned long lnno_size = 0; |
| 1464 | asection *text_sec = NULL; |
| 1465 | asection *data_sec = NULL; |
| 1466 | asection *bss_sec = NULL; |
| 1467 | |
| 1468 | struct internal_filehdr internal_f; |
| 1469 | struct internal_aouthdr internal_a; |
| 1470 | |
| 1471 | struct icofdata *coff = obj_icof(abfd); |
| 1472 | |
| 1473 | |
| 1474 | bfd_error = system_call_error; |
| 1475 | |
| 1476 | |
| 1477 | if(abfd->output_has_begun == false) { |
| 1478 | coff_compute_section_file_positions(abfd); |
| 1479 | } |
| 1480 | |
| 1481 | if (abfd->sections != (asection *)NULL) { |
| 1482 | scn_base = abfd->sections->filepos; |
| 1483 | } |
| 1484 | else { |
| 1485 | scn_base = 0; |
| 1486 | } |
| 1487 | if (bfd_seek(abfd, scn_base, SEEK_SET) != 0) |
| 1488 | return false; |
| 1489 | reloc_base = obj_relocbase(abfd); |
| 1490 | |
| 1491 | /* Make a pass through the symbol table to count line number entries and |
| 1492 | put them into the correct asections */ |
| 1493 | |
| 1494 | coff_count_linenumbers(abfd); |
| 1495 | data_base = scn_base; |
| 1496 | |
| 1497 | /* Work out the size of the reloc and linno areas */ |
| 1498 | |
| 1499 | for (current = abfd->sections; current != NULL; current = current->next) { |
| 1500 | reloc_size += current->reloc_count * RELSZ; |
| 1501 | lnno_size += current->lineno_count * LINESZ; |
| 1502 | data_base += SCNHSZ; |
| 1503 | } |
| 1504 | |
| 1505 | lineno_base = reloc_base + reloc_size; |
| 1506 | sym_base = lineno_base + lnno_size; |
| 1507 | |
| 1508 | /* Indicate in each section->line_filepos its actual file address */ |
| 1509 | for (current = abfd->sections; current != NULL; current = current->next) { |
| 1510 | if (current->lineno_count) { |
| 1511 | current->line_filepos = lineno_base; |
| 1512 | current->moving_line_filepos = lineno_base; |
| 1513 | lineno_base += current->lineno_count * LINESZ; |
| 1514 | } |
| 1515 | else { |
| 1516 | current->line_filepos = 0; |
| 1517 | } |
| 1518 | if (current->reloc_count) { |
| 1519 | current->rel_filepos = reloc_base; |
| 1520 | reloc_base += current->reloc_count * sizeof(struct internal_reloc); |
| 1521 | } |
| 1522 | else { |
| 1523 | current->rel_filepos = 0; |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | /* Write section headers to the file. */ |
| 1528 | |
| 1529 | bfd_seek(abfd, |
| 1530 | (file_ptr) ((abfd->flags & EXEC_P) ? |
| 1531 | (FILHSZ + AOUTSZ) : FILHSZ), |
| 1532 | SEEK_SET); |
| 1533 | |
| 1534 | { |
| 1535 | #if 0 |
| 1536 | unsigned int pad = abfd->flags & D_PAGED ? data_base : 0; |
| 1537 | #endif |
| 1538 | unsigned int pad = 0; |
| 1539 | |
| 1540 | for (current = abfd->sections; current != NULL; current = current->next) { |
| 1541 | struct internal_scnhdr section; |
| 1542 | strncpy(&(section.s_name[0]), current->name, 8); |
| 1543 | section.s_vaddr = current->vma + pad; |
| 1544 | section.s_paddr = current->vma + pad; |
| 1545 | section.s_size = current->size - pad; |
| 1546 | /* |
| 1547 | If this section has no size or is unloadable then the scnptr |
| 1548 | will be 0 too |
| 1549 | */ |
| 1550 | if (current->size - pad == 0 || |
| 1551 | (current->flags & SEC_LOAD) == 0) { |
| 1552 | section.s_scnptr = 0; |
| 1553 | |
| 1554 | } |
| 1555 | else { |
| 1556 | section.s_scnptr = current->filepos; |
| 1557 | } |
| 1558 | section.s_relptr = current->rel_filepos; |
| 1559 | section.s_lnnoptr = current->line_filepos; |
| 1560 | section.s_nreloc = current->reloc_count; |
| 1561 | section.s_nlnno = current->lineno_count; |
| 1562 | if (current->reloc_count != 0) |
| 1563 | hasrelocs = true; |
| 1564 | if (current->lineno_count != 0) |
| 1565 | haslinno = true; |
| 1566 | |
| 1567 | if (!strcmp(current->name, _TEXT)) { |
| 1568 | text_sec = current; |
| 1569 | section.s_flags = STYP_TEXT; /* kinda stupid */ |
| 1570 | } |
| 1571 | else if (!strcmp(current->name, _DATA)) { |
| 1572 | data_sec = current; |
| 1573 | section.s_flags = STYP_DATA; /* kinda stupid */ |
| 1574 | } |
| 1575 | else if (!strcmp(current->name, _BSS)) { |
| 1576 | bss_sec = current; |
| 1577 | section.s_flags = STYP_BSS; /* kinda stupid */ |
| 1578 | } |
| 1579 | |
| 1580 | |
| 1581 | #ifdef I960 |
| 1582 | section.s_align = (current->alignment_power |
| 1583 | ? 1 << current->alignment_power |
| 1584 | : 0); |
| 1585 | |
| 1586 | #endif |
| 1587 | { |
| 1588 | SCNHDR buff; |
| 1589 | |
| 1590 | swap_scnhdr_out(abfd, §ion, &buff); |
| 1591 | bfd_write((PTR) (&buff), 1, SCNHSZ, abfd); |
| 1592 | |
| 1593 | } |
| 1594 | pad = 0; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | /* OK, now set up the filehdr... */ |
| 1599 | internal_f.f_nscns = abfd->section_count; |
| 1600 | /* |
| 1601 | We will NOT put a fucking timestamp in the header here. Every time you |
| 1602 | put it back, I will come in and take it out again. I'm sorry. This |
| 1603 | field does not belong here. We fill it with a 0 so it compares the |
| 1604 | same but is not a reasonable time. -- gnu@cygnus.com |
| 1605 | */ |
| 1606 | /* |
| 1607 | Well, I like it, so I'm conditionally compiling it in. |
| 1608 | steve@cygnus.com |
| 1609 | */ |
| 1610 | #ifdef COFF_TIMESTAMP |
| 1611 | internal_f.f_timdat = time(0); |
| 1612 | #else |
| 1613 | internal_f.f_timdat = 0; |
| 1614 | #endif |
| 1615 | |
| 1616 | if (bfd_get_symcount(abfd) != 0) |
| 1617 | internal_f.f_symptr = sym_base; |
| 1618 | else |
| 1619 | internal_f.f_symptr = 0; |
| 1620 | |
| 1621 | internal_f.f_flags = 0; |
| 1622 | |
| 1623 | if (abfd->flags & EXEC_P) |
| 1624 | internal_f.f_opthdr = AOUTSZ; |
| 1625 | else |
| 1626 | internal_f.f_opthdr = 0; |
| 1627 | |
| 1628 | if (!hasrelocs) |
| 1629 | internal_f.f_flags |= F_RELFLG; |
| 1630 | if (!haslinno) |
| 1631 | internal_f.f_flags |= F_LNNO; |
| 1632 | if (0 == bfd_get_symcount(abfd)) |
| 1633 | internal_f.f_flags |= F_LSYMS; |
| 1634 | if (abfd->flags & EXEC_P) |
| 1635 | internal_f.f_flags |= F_EXEC; |
| 1636 | #if M88 |
| 1637 | internal_f.f_flags |= F_AR32W; |
| 1638 | #else |
| 1639 | if (!abfd->xvec->byteorder_big_p) |
| 1640 | internal_f.f_flags |= F_AR32WR; |
| 1641 | #endif |
| 1642 | /* |
| 1643 | FIXME, should do something about the other byte orders and |
| 1644 | architectures. |
| 1645 | */ |
| 1646 | |
| 1647 | /* Set up architecture-dependent stuff */ |
| 1648 | |
| 1649 | { int magic = 0; |
| 1650 | int flags = 0; |
| 1651 | coff_set_flags(abfd, &magic, &flags); |
| 1652 | internal_f.f_magic = magic; |
| 1653 | internal_f.f_flags = flags; |
| 1654 | |
| 1655 | /* ...and the "opt"hdr... */ |
| 1656 | |
| 1657 | #ifdef I960 |
| 1658 | internal_a.magic = (magic == I960ROMAGIC ? NMAGIC : OMAGIC); |
| 1659 | #endif |
| 1660 | #if M88 |
| 1661 | internal_a.magic = PAGEMAGICBCS; |
| 1662 | #endif |
| 1663 | } |
| 1664 | /* Now should write relocs, strings, syms */ |
| 1665 | obj_sym_filepos(abfd) = sym_base; |
| 1666 | |
| 1667 | if (bfd_get_symcount(abfd) != 0) { |
| 1668 | coff_mangle_symbols(abfd); |
| 1669 | coff_write_symbols(abfd); |
| 1670 | coff_write_linenumbers(abfd); |
| 1671 | coff_write_relocs(abfd); |
| 1672 | } |
| 1673 | if (text_sec) { |
| 1674 | internal_a.tsize = text_sec->size; |
| 1675 | internal_a.text_start =text_sec->size ? text_sec->vma : 0; |
| 1676 | } |
| 1677 | if (data_sec) { |
| 1678 | internal_a.dsize = data_sec->size; |
| 1679 | internal_a.data_start = data_sec->size ? data_sec->vma : 0; |
| 1680 | } |
| 1681 | if (bss_sec) { |
| 1682 | internal_a.bsize = bss_sec->size; |
| 1683 | } |
| 1684 | |
| 1685 | internal_a.entry = bfd_get_start_address(abfd); |
| 1686 | internal_f.f_nsyms = bfd_get_symcount(abfd); |
| 1687 | |
| 1688 | /* now write them */ |
| 1689 | if (bfd_seek(abfd, 0L, SEEK_SET) != 0) |
| 1690 | return false; |
| 1691 | { |
| 1692 | FILHDR buff; |
| 1693 | bfd_swap_filehdr_out(abfd, &internal_f, &buff); |
| 1694 | bfd_write((PTR) &buff, 1, FILHSZ, abfd); |
| 1695 | } |
| 1696 | if (abfd->flags & EXEC_P) { |
| 1697 | AOUTHDR buff; |
| 1698 | bfd_swap_aouthdr_out(abfd, &internal_a, &buff); |
| 1699 | bfd_write((PTR) &buff, 1, AOUTSZ, abfd); |
| 1700 | } |
| 1701 | return true; |
| 1702 | } |
| 1703 | |
| 1704 | static boolean |
| 1705 | coff_set_section_contents(abfd, section, location, offset, count) |
| 1706 | bfd *abfd; |
| 1707 | sec_ptr section; |
| 1708 | PTR location; |
| 1709 | file_ptr offset; |
| 1710 | size_t count; |
| 1711 | { |
| 1712 | if (abfd->output_has_begun == false) /* set by bfd.c handler */ |
| 1713 | coff_compute_section_file_positions(abfd); |
| 1714 | |
| 1715 | bfd_seek(abfd, (file_ptr) (section->filepos + offset), SEEK_SET); |
| 1716 | |
| 1717 | if (count != 0) { |
| 1718 | return (bfd_write(location, 1, count, abfd) == count) ? true : false; |
| 1719 | } |
| 1720 | return true; |
| 1721 | } |
| 1722 | #if 0 |
| 1723 | static boolean |
| 1724 | coff_close_and_cleanup(abfd) |
| 1725 | bfd *abfd; |
| 1726 | { |
| 1727 | if (!bfd_read_p(abfd)) |
| 1728 | switch (abfd->format) { |
| 1729 | case bfd_archive: |
| 1730 | if (!_bfd_write_archive_contents(abfd)) |
| 1731 | return false; |
| 1732 | break; |
| 1733 | case bfd_object: |
| 1734 | if (!coff_write_object_contents(abfd)) |
| 1735 | return false; |
| 1736 | break; |
| 1737 | default: |
| 1738 | bfd_error = invalid_operation; |
| 1739 | return false; |
| 1740 | } |
| 1741 | |
| 1742 | /* We depend on bfd_close to free all the memory on the obstack. */ |
| 1743 | /* FIXME if bfd_release is not using obstacks! */ |
| 1744 | return true; |
| 1745 | } |
| 1746 | |
| 1747 | #endif |
| 1748 | static PTR |
| 1749 | buy_and_read(abfd, where, seek_direction, size) |
| 1750 | bfd *abfd; |
| 1751 | file_ptr where; |
| 1752 | int seek_direction; |
| 1753 | size_t size; |
| 1754 | { |
| 1755 | PTR area = (PTR) bfd_alloc(abfd, size); |
| 1756 | if (!area) { |
| 1757 | bfd_error = no_memory; |
| 1758 | return (NULL); |
| 1759 | } |
| 1760 | bfd_seek(abfd, where, seek_direction); |
| 1761 | if (bfd_read(area, 1, size, abfd) != size) { |
| 1762 | bfd_error = system_call_error; |
| 1763 | return (NULL); |
| 1764 | } /* on error */ |
| 1765 | return (area); |
| 1766 | } /* buy_and_read() */ |
| 1767 | |
| 1768 | static void |
| 1769 | DEFUN(offset_symbol_indices,(abfd, symtab, count, offset), |
| 1770 | bfd *abfd AND |
| 1771 | struct internal_syment *symtab AND |
| 1772 | unsigned long count AND |
| 1773 | long offset) |
| 1774 | { |
| 1775 | struct internal_syment *end = symtab + count; |
| 1776 | for (; symtab < end; ++symtab) { |
| 1777 | if (symtab->n_sclass == C_FILE) { |
| 1778 | symtab->n_value = 0; |
| 1779 | } |
| 1780 | else if (symtab->n_sclass == C_ALIAS) { |
| 1781 | /* |
| 1782 | These guys have indices in their values. |
| 1783 | */ |
| 1784 | symtab->n_value = symtab->n_value + offset; |
| 1785 | } |
| 1786 | else if (symtab->n_numaux) { |
| 1787 | /* |
| 1788 | anybody else without an aux, has no indices. |
| 1789 | */ |
| 1790 | |
| 1791 | if (symtab->n_sclass == C_EOS |
| 1792 | || (BTYPE(symtab->n_type) == T_STRUCT |
| 1793 | && symtab->n_sclass != C_STRTAG) |
| 1794 | || BTYPE(symtab->n_type) == T_UNION |
| 1795 | || BTYPE(symtab->n_type) == T_ENUM) { |
| 1796 | /* If the tagndx is 0 then the struct hasn't really been |
| 1797 | defined, so leave it alone */ |
| 1798 | |
| 1799 | if(((union internal_auxent *) (symtab + 1))->x_sym.x_tagndx != 0) { |
| 1800 | ((union internal_auxent *) (symtab + 1))->x_sym.x_tagndx += offset; |
| 1801 | } |
| 1802 | |
| 1803 | } /* These guys have a tagndx */ |
| 1804 | if (symtab->n_sclass == C_STRTAG |
| 1805 | || symtab->n_sclass == C_UNTAG |
| 1806 | || symtab->n_sclass == C_ENTAG |
| 1807 | || symtab->n_sclass == C_BLOCK |
| 1808 | || symtab->n_sclass == C_FCN |
| 1809 | || ISFCN(symtab->n_type)) { |
| 1810 | |
| 1811 | ((union internal_auxent *) (symtab + |
| 1812 | 1))->x_sym.x_fcnary.x_fcn.x_endndx |
| 1813 | += offset; |
| 1814 | |
| 1815 | } /* These guys have an endndx */ |
| 1816 | #ifndef I960 |
| 1817 | if (ISFCN(symtab->n_type)) { |
| 1818 | ((union internal_auxent *) (symtab + 1))->x_sym.x_tvndx += offset; |
| 1819 | } /* These guys have a tvndx. I think... |
| 1820 | (FIXME) */ |
| 1821 | #endif /* Not I960 */ |
| 1822 | |
| 1823 | } /* if value, else if aux */ |
| 1824 | symtab += symtab->n_numaux; |
| 1825 | } /* walk the symtab */ |
| 1826 | |
| 1827 | return; |
| 1828 | } /* offset_symbol_indices() */ |
| 1829 | |
| 1830 | /* |
| 1831 | read a symbol table into freshly mallocated memory, swap it, and knit the |
| 1832 | symbol names into a normalized form. By normalized here I mean that all |
| 1833 | symbols have an n_offset pointer that points to a NULL terminated string. |
| 1834 | Oh, and the first symbol MUST be a C_FILE. If there wasn't one there |
| 1835 | before, put one there. |
| 1836 | */ |
| 1837 | |
| 1838 | static struct internal_syment * |
| 1839 | DEFUN(get_normalized_symtab,(abfd), |
| 1840 | bfd *abfd) |
| 1841 | { |
| 1842 | |
| 1843 | struct internal_syment *internal; |
| 1844 | struct internal_syment *internal_ptr; |
| 1845 | struct internal_syment *internal_end; |
| 1846 | SYMENT *raw; |
| 1847 | SYMENT *raw_src; |
| 1848 | SYMENT *raw_end; |
| 1849 | char *string_table = NULL; |
| 1850 | unsigned long size; |
| 1851 | char string_table_size_buffer[4]; |
| 1852 | unsigned long string_table_size = 0; |
| 1853 | unsigned int raw_size; |
| 1854 | if (obj_raw_syments(abfd) != (struct internal_syment *)NULL) { |
| 1855 | return obj_raw_syments(abfd); |
| 1856 | } |
| 1857 | if ((size = bfd_get_symcount(abfd) * sizeof(struct internal_syment)) == 0) { |
| 1858 | bfd_error = no_symbols; |
| 1859 | return (NULL); |
| 1860 | } |
| 1861 | |
| 1862 | internal = (struct internal_syment *)bfd_alloc(abfd, size); |
| 1863 | internal_end = internal + bfd_get_symcount(abfd); |
| 1864 | |
| 1865 | raw_size = bfd_get_symcount(abfd) * SYMESZ; |
| 1866 | raw = (SYMENT *)bfd_alloc(abfd,raw_size); |
| 1867 | |
| 1868 | if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1 |
| 1869 | || bfd_read((PTR)raw, raw_size, 1, abfd) != raw_size) { |
| 1870 | bfd_error = system_call_error; |
| 1871 | return (NULL); |
| 1872 | } |
| 1873 | /* mark the end of the symbols */ |
| 1874 | raw_end = raw + bfd_get_symcount(abfd); |
| 1875 | /* |
| 1876 | FIXME SOMEDAY. A string table size of zero is very weird, but |
| 1877 | probably possible. If one shows up, it will probably kill us. |
| 1878 | */ |
| 1879 | |
| 1880 | /* Swap all the raw entries */ |
| 1881 | for (raw_src = raw, internal_ptr = internal; raw_src < raw_end; raw_src++, internal_ptr++) { |
| 1882 | unsigned int i; |
| 1883 | coff_swap_sym_in(abfd, raw_src,internal_ptr); |
| 1884 | for (i = internal_ptr->n_numaux; i; --i, raw_src++, internal_ptr++) { |
| 1885 | coff_swap_aux_in(abfd, (AUXENT *)(raw_src +1), internal_ptr->n_type, |
| 1886 | internal_ptr->n_sclass, (union |
| 1887 | internal_auxent *)(internal_ptr +1)); |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | /* Free all the raw stuff */ |
| 1892 | bfd_release(abfd, raw_src); |
| 1893 | |
| 1894 | for (internal_ptr = internal; internal_ptr < internal_end; internal_ptr ++) { |
| 1895 | |
| 1896 | if (internal_ptr->_n._n_n._n_zeroes != 0) { |
| 1897 | /* |
| 1898 | This is a "short" name. Make it long. |
| 1899 | */ |
| 1900 | unsigned long i = 0; |
| 1901 | char *newstring = NULL; |
| 1902 | /* |
| 1903 | find the length of this string without walking into memory |
| 1904 | that isn't ours. |
| 1905 | */ |
| 1906 | |
| 1907 | for (i = 0; i < 8; ++i) { |
| 1908 | if (internal_ptr->_n._n_name[i] == '\0') { |
| 1909 | break; |
| 1910 | } /* if end of string */ |
| 1911 | } /* possible lengths of this string. */ |
| 1912 | |
| 1913 | if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) { |
| 1914 | bfd_error = no_memory; |
| 1915 | return (NULL); |
| 1916 | } /* on error */ |
| 1917 | bzero(newstring, i); |
| 1918 | strncpy(newstring, internal_ptr->_n._n_name, i-1); |
| 1919 | internal_ptr->_n._n_n._n_offset = (int) newstring; |
| 1920 | internal_ptr->_n._n_n._n_zeroes = 0; |
| 1921 | |
| 1922 | } |
| 1923 | else { |
| 1924 | if (string_table == NULL) { |
| 1925 | /* |
| 1926 | NOTE: we don't read the string table until now because we |
| 1927 | don't necessarily know that we have one until now. |
| 1928 | */ |
| 1929 | /* |
| 1930 | At this point we should be "seek"'d to the end of the |
| 1931 | symbols === the symbol table size. |
| 1932 | */ |
| 1933 | |
| 1934 | if (bfd_read((char *) string_table_size_buffer, |
| 1935 | sizeof(string_table_size_buffer), |
| 1936 | 1, abfd) != sizeof(string_table_size)) { |
| 1937 | bfd_error = system_call_error; |
| 1938 | return (NULL); |
| 1939 | } /* on error */ |
| 1940 | |
| 1941 | string_table_size = bfd_h_get_32(abfd, string_table_size_buffer); |
| 1942 | |
| 1943 | if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) { |
| 1944 | bfd_error = no_memory; |
| 1945 | return (NULL); |
| 1946 | } /* on mallocation error */ |
| 1947 | if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) { |
| 1948 | bfd_error = system_call_error; |
| 1949 | return (NULL); |
| 1950 | } /* on error */ |
| 1951 | } /* have not yet read the string table. */ |
| 1952 | /* |
| 1953 | This is a long name already. Just point it at the string in |
| 1954 | memory. |
| 1955 | */ |
| 1956 | internal_ptr->_n._n_n._n_offset = (int) (string_table - 4 + internal_ptr->_n._n_n._n_offset); |
| 1957 | |
| 1958 | } /* switch on type of symbol name */ |
| 1959 | |
| 1960 | internal_ptr += internal_ptr->n_numaux; |
| 1961 | } /* for each symbol */ |
| 1962 | #if 0 |
| 1963 | #ifndef GNU960 |
| 1964 | /* I'm not sure of the repercussions of this, so the Intel |
| 1965 | folks will always do the force |
| 1966 | */ |
| 1967 | if (obj_symbol_slew(abfd) > 0) |
| 1968 | force_indices_file_symbol_relative(abfd, internal); |
| 1969 | #else |
| 1970 | force_indices_file_symbol_relative(abfd, internal); |
| 1971 | #endif |
| 1972 | #endif |
| 1973 | obj_raw_syments(abfd) = internal; |
| 1974 | obj_string_table(abfd) = string_table; |
| 1975 | |
| 1976 | return (internal); |
| 1977 | } /* get_normalized_symtab() */ |
| 1978 | |
| 1979 | static |
| 1980 | struct sec * |
| 1981 | DEFUN(section_from_bfd_index,(abfd, index), |
| 1982 | bfd *abfd AND |
| 1983 | int index) |
| 1984 | { |
| 1985 | if (index > 0) { |
| 1986 | struct sec *answer = abfd->sections; |
| 1987 | while (--index) { |
| 1988 | answer = answer->next; |
| 1989 | } |
| 1990 | return answer; |
| 1991 | } |
| 1992 | return 0; |
| 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | |
| 1997 | |
| 1998 | static boolean |
| 1999 | coff_slurp_line_table(abfd, asect) |
| 2000 | bfd *abfd; |
| 2001 | asection *asect; |
| 2002 | { |
| 2003 | LINENO *native_lineno; |
| 2004 | alent *lineno_cache; |
| 2005 | |
| 2006 | BFD_ASSERT(asect->lineno == (alent *) NULL); |
| 2007 | |
| 2008 | native_lineno = (LINENO *) buy_and_read(abfd, |
| 2009 | asect->line_filepos, |
| 2010 | SEEK_SET, |
| 2011 | (size_t) (LINESZ * |
| 2012 | asect->lineno_count)); |
| 2013 | lineno_cache = |
| 2014 | (alent *) bfd_alloc(abfd, (size_t) ((asect->lineno_count + 1) * sizeof(alent))); |
| 2015 | if (lineno_cache == NULL) { |
| 2016 | bfd_error = no_memory; |
| 2017 | return false; |
| 2018 | } else { |
| 2019 | unsigned int counter = 0; |
| 2020 | alent *cache_ptr = lineno_cache; |
| 2021 | LINENO *src = native_lineno; |
| 2022 | |
| 2023 | while (counter < asect->lineno_count) { |
| 2024 | struct internal_lineno dst; |
| 2025 | coff_swap_lineno_in(abfd, src, &dst); |
| 2026 | cache_ptr->line_number = dst.l_lnno; |
| 2027 | |
| 2028 | if (cache_ptr->line_number == 0) { |
| 2029 | coff_symbol_type *sym = |
| 2030 | (coff_symbol_type *) (dst.l_addr.l_symndx |
| 2031 | + obj_symbol_slew(abfd) |
| 2032 | + obj_raw_syments(abfd))->_n._n_n._n_zeroes; |
| 2033 | cache_ptr->u.sym = (asymbol *) sym; |
| 2034 | sym->lineno = cache_ptr; |
| 2035 | } |
| 2036 | else { |
| 2037 | cache_ptr->u.offset = dst.l_addr.l_paddr |
| 2038 | - bfd_section_vma(abfd, asect); |
| 2039 | } /* If no linenumber expect a symbol index */ |
| 2040 | |
| 2041 | cache_ptr++; |
| 2042 | src++; |
| 2043 | counter++; |
| 2044 | } |
| 2045 | cache_ptr->line_number = 0; |
| 2046 | |
| 2047 | } |
| 2048 | asect->lineno = lineno_cache; |
| 2049 | /* FIXME, free native_lineno here, or use alloca or something. */ |
| 2050 | return true; |
| 2051 | } /* coff_slurp_line_table() */ |
| 2052 | |
| 2053 | static boolean |
| 2054 | DEFUN(coff_slurp_symbol_table,(abfd), |
| 2055 | bfd *abfd) |
| 2056 | { |
| 2057 | struct internal_syment *native_symbols; |
| 2058 | coff_symbol_type *cached_area; |
| 2059 | unsigned int *table_ptr; |
| 2060 | |
| 2061 | unsigned int number_of_symbols = 0; |
| 2062 | if (obj_symbols(abfd)) |
| 2063 | return true; |
| 2064 | bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET); |
| 2065 | |
| 2066 | /* Read in the symbol table */ |
| 2067 | if ((native_symbols = get_normalized_symtab(abfd)) == NULL) { |
| 2068 | return (false); |
| 2069 | } /* on error */ |
| 2070 | |
| 2071 | |
| 2072 | /* Allocate enough room for all the symbols in cached form */ |
| 2073 | cached_area = |
| 2074 | (coff_symbol_type *) |
| 2075 | bfd_alloc(abfd, (size_t) (bfd_get_symcount(abfd) * sizeof(coff_symbol_type))); |
| 2076 | |
| 2077 | if (cached_area == NULL) { |
| 2078 | bfd_error = no_memory; |
| 2079 | return false; |
| 2080 | } /* on error */ |
| 2081 | table_ptr = |
| 2082 | (unsigned int *) |
| 2083 | bfd_alloc(abfd, (size_t) (bfd_get_symcount(abfd) * sizeof(unsigned int))); |
| 2084 | |
| 2085 | if (table_ptr == NULL) { |
| 2086 | bfd_error = no_memory; |
| 2087 | return false; |
| 2088 | } else { |
| 2089 | coff_symbol_type *dst = cached_area; |
| 2090 | unsigned int last_native_index = bfd_get_symcount(abfd); |
| 2091 | unsigned int this_index = 0; |
| 2092 | while (this_index < last_native_index) { |
| 2093 | struct internal_syment *src = native_symbols + this_index; |
| 2094 | table_ptr[this_index] = number_of_symbols; |
| 2095 | dst->symbol.the_bfd = abfd; |
| 2096 | |
| 2097 | dst->symbol.name = (char *)(src->_n._n_n._n_offset); |
| 2098 | /* |
| 2099 | We use the native name field to point to the cached field |
| 2100 | */ |
| 2101 | src->_n._n_n._n_zeroes = (int) dst; |
| 2102 | dst->symbol.section = section_from_bfd_index(abfd, |
| 2103 | src->n_scnum); |
| 2104 | switch (src->n_sclass) { |
| 2105 | #ifdef I960 |
| 2106 | case C_LEAFEXT: |
| 2107 | #if 0 |
| 2108 | dst->symbol.value = src->n_value - dst->symbol.section->vma; |
| 2109 | dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL; |
| 2110 | dst->symbol.flags |= BSF_NOT_AT_END; |
| 2111 | #endif |
| 2112 | /* Fall through to next case */ |
| 2113 | |
| 2114 | #endif |
| 2115 | |
| 2116 | case C_EXT: |
| 2117 | if ((src->n_scnum) == 0) { |
| 2118 | if ((src->n_value) == 0) { |
| 2119 | dst->symbol.flags = BSF_UNDEFINED; |
| 2120 | dst->symbol.value= 0; |
| 2121 | } |
| 2122 | else { |
| 2123 | dst->symbol.flags = BSF_FORT_COMM; |
| 2124 | dst->symbol.value = (src->n_value); |
| 2125 | } |
| 2126 | } |
| 2127 | else { |
| 2128 | /* |
| 2129 | Base the value as an index from the base of the |
| 2130 | section |
| 2131 | */ |
| 2132 | if (dst->symbol.section == (asection *) NULL) { |
| 2133 | dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL | BSF_ABSOLUTE; |
| 2134 | dst->symbol.value = src->n_value; |
| 2135 | } |
| 2136 | else { |
| 2137 | dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL; |
| 2138 | dst->symbol.value = src->n_value - dst->symbol.section->vma; |
| 2139 | } |
| 2140 | if (ISFCN((src->n_type))) { |
| 2141 | /* |
| 2142 | A function ext does not go at the end of a file |
| 2143 | */ |
| 2144 | dst->symbol.flags |= BSF_NOT_AT_END; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | break; |
| 2149 | case C_STAT: /* static */ |
| 2150 | #ifdef I960 |
| 2151 | case C_LEAFSTAT: /* static leaf procedure */ |
| 2152 | #endif |
| 2153 | case C_LABEL: /* label */ |
| 2154 | dst->symbol.flags = BSF_LOCAL; |
| 2155 | /* |
| 2156 | Base the value as an index from the base of the section |
| 2157 | */ |
| 2158 | dst->symbol.value = (src->n_value) - dst->symbol.section->vma; |
| 2159 | break; |
| 2160 | |
| 2161 | case C_MOS: /* member of structure */ |
| 2162 | case C_EOS: /* end of structure */ |
| 2163 | case C_REGPARM: /* register parameter */ |
| 2164 | case C_REG: /* register variable */ |
| 2165 | #ifdef C_AUTOARG |
| 2166 | case C_AUTOARG: /* 960-specific storage class */ |
| 2167 | #endif |
| 2168 | case C_TPDEF: /* type definition */ |
| 2169 | |
| 2170 | case C_ARG: |
| 2171 | case C_AUTO: /* automatic variable */ |
| 2172 | case C_FIELD: /* bit field */ |
| 2173 | case C_ENTAG: /* enumeration tag */ |
| 2174 | case C_MOE: /* member of enumeration */ |
| 2175 | case C_MOU: /* member of union */ |
| 2176 | case C_UNTAG: /* union tag */ |
| 2177 | |
| 2178 | dst->symbol.flags = BSF_DEBUGGING; |
| 2179 | dst->symbol.value = (src->n_value); |
| 2180 | break; |
| 2181 | |
| 2182 | case C_FILE: /* file name */ |
| 2183 | case C_STRTAG: /* structure tag */ |
| 2184 | dst->symbol.flags = BSF_DEBUGGING; |
| 2185 | dst->symbol.value = (src->n_value); |
| 2186 | |
| 2187 | break; |
| 2188 | case C_BLOCK: /* ".bb" or ".eb" */ |
| 2189 | case C_FCN: /* ".bf" or ".ef" */ |
| 2190 | dst->symbol.flags = BSF_LOCAL; |
| 2191 | /* |
| 2192 | Base the value as an index from the base of the section |
| 2193 | */ |
| 2194 | dst->symbol.value = (src->n_value) - dst->symbol.section->vma; |
| 2195 | |
| 2196 | break; |
| 2197 | case C_EFCN: /* physical end of function */ |
| 2198 | case C_NULL: |
| 2199 | case C_EXTDEF: /* external definition */ |
| 2200 | case C_ULABEL: /* undefined label */ |
| 2201 | case C_USTATIC: /* undefined static */ |
| 2202 | case C_LINE: /* line # reformatted as symbol table entry */ |
| 2203 | case C_ALIAS: /* duplicate tag */ |
| 2204 | case C_HIDDEN: /* ext symbol in dmert public lib */ |
| 2205 | |
| 2206 | default: |
| 2207 | |
| 2208 | abort(); |
| 2209 | dst->symbol.flags = BSF_DEBUGGING; |
| 2210 | dst->symbol.value = (src->n_value); |
| 2211 | |
| 2212 | break; |
| 2213 | } |
| 2214 | |
| 2215 | BFD_ASSERT(dst->symbol.flags != 0); |
| 2216 | |
| 2217 | dst->native = src; |
| 2218 | |
| 2219 | dst->symbol.udata = 0; |
| 2220 | dst->lineno = (alent *) NULL; |
| 2221 | this_index += (src->n_numaux) + 1; |
| 2222 | dst++; |
| 2223 | number_of_symbols++; |
| 2224 | } /* walk the native symtab */ |
| 2225 | } /* bfdize the native symtab */ |
| 2226 | |
| 2227 | obj_symbols(abfd) = cached_area; |
| 2228 | obj_raw_syments(abfd) = native_symbols; |
| 2229 | |
| 2230 | bfd_get_symcount(abfd) = number_of_symbols; |
| 2231 | obj_convert(abfd) = table_ptr; |
| 2232 | /* Slurp the line tables for each section too */ |
| 2233 | { |
| 2234 | asection *p; |
| 2235 | p = abfd->sections; |
| 2236 | while (p) { |
| 2237 | coff_slurp_line_table(abfd, p); |
| 2238 | p = p->next; |
| 2239 | } |
| 2240 | } |
| 2241 | return true; |
| 2242 | } /* coff_slurp_symbol_table() */ |
| 2243 | |
| 2244 | static unsigned int |
| 2245 | coff_get_symtab_upper_bound(abfd) |
| 2246 | bfd *abfd; |
| 2247 | { |
| 2248 | if (!coff_slurp_symbol_table(abfd)) |
| 2249 | return 0; |
| 2250 | |
| 2251 | return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *)); |
| 2252 | } |
| 2253 | |
| 2254 | |
| 2255 | static unsigned int |
| 2256 | coff_get_symtab(abfd, alocation) |
| 2257 | bfd *abfd; |
| 2258 | asymbol **alocation; |
| 2259 | { |
| 2260 | unsigned int counter = 0; |
| 2261 | coff_symbol_type *symbase; |
| 2262 | coff_symbol_type **location = (coff_symbol_type **) (alocation); |
| 2263 | if (!coff_slurp_symbol_table(abfd)) |
| 2264 | return 0; |
| 2265 | |
| 2266 | for (symbase = obj_symbols(abfd); counter++ < bfd_get_symcount(abfd);) |
| 2267 | *(location++) = symbase++; |
| 2268 | *location++ = 0; |
| 2269 | return bfd_get_symcount(abfd); |
| 2270 | } |
| 2271 | |
| 2272 | static unsigned int |
| 2273 | coff_get_reloc_upper_bound(abfd, asect) |
| 2274 | bfd *abfd; |
| 2275 | sec_ptr asect; |
| 2276 | { |
| 2277 | if (bfd_get_format(abfd) != bfd_object) { |
| 2278 | bfd_error = invalid_operation; |
| 2279 | return 0; |
| 2280 | } |
| 2281 | return (asect->reloc_count + 1) * sizeof(arelent *); |
| 2282 | } |
| 2283 | |
| 2284 | static boolean |
| 2285 | DEFUN(coff_slurp_reloc_table,(abfd, asect, symbols), |
| 2286 | bfd *abfd AND |
| 2287 | sec_ptr asect AND |
| 2288 | asymbol **symbols) |
| 2289 | { |
| 2290 | RELOC *native_relocs; |
| 2291 | arelent *reloc_cache; |
| 2292 | if (asect->relocation) |
| 2293 | return true; |
| 2294 | if (asect->reloc_count == 0) |
| 2295 | return true; |
| 2296 | if (!coff_slurp_symbol_table(abfd)) |
| 2297 | return false; |
| 2298 | native_relocs = |
| 2299 | (RELOC *) buy_and_read(abfd, |
| 2300 | asect->rel_filepos, |
| 2301 | SEEK_SET, |
| 2302 | (size_t) (RELSZ * |
| 2303 | asect->reloc_count)); |
| 2304 | reloc_cache = (arelent *) |
| 2305 | bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent))); |
| 2306 | |
| 2307 | if (reloc_cache == NULL) { |
| 2308 | bfd_error = no_memory; |
| 2309 | return false; |
| 2310 | } { /* on error */ |
| 2311 | arelent *cache_ptr; |
| 2312 | RELOC *src; |
| 2313 | for (cache_ptr = reloc_cache, |
| 2314 | src = native_relocs; |
| 2315 | cache_ptr < reloc_cache + asect->reloc_count; |
| 2316 | cache_ptr++, |
| 2317 | src++) { |
| 2318 | struct internal_reloc dst; |
| 2319 | asymbol *ptr; |
| 2320 | bfd_swap_reloc_in(abfd, src, &dst); |
| 2321 | dst.r_symndx += obj_symbol_slew(abfd); |
| 2322 | cache_ptr->sym_ptr_ptr = symbols + obj_convert(abfd)[dst.r_symndx]; |
| 2323 | |
| 2324 | ptr = *(cache_ptr->sym_ptr_ptr); |
| 2325 | cache_ptr->address = dst.r_vaddr; |
| 2326 | /* |
| 2327 | The symbols definitions that we have read in have been |
| 2328 | relocated as if their sections started at 0. But the offsets |
| 2329 | refering to the symbols in the raw data have not been |
| 2330 | modified, so we have to have a negative addend to compensate. |
| 2331 | |
| 2332 | Note that symbols which used to be common must be left alone |
| 2333 | */ |
| 2334 | |
| 2335 | if (ptr->the_bfd == abfd |
| 2336 | && ptr->section != (asection *) NULL |
| 2337 | && ((ptr->flags & BSF_OLD_COMMON)== 0)) |
| 2338 | { |
| 2339 | cache_ptr->addend = -(ptr->section->vma + ptr->value); |
| 2340 | } |
| 2341 | else { |
| 2342 | cache_ptr->addend = 0; |
| 2343 | } |
| 2344 | |
| 2345 | cache_ptr->address -= asect->vma; |
| 2346 | |
| 2347 | cache_ptr->section = (asection *) NULL; |
| 2348 | |
| 2349 | #if I386 |
| 2350 | cache_ptr->howto = howto_table + dst.r_type; |
| 2351 | #endif |
| 2352 | #if I960 |
| 2353 | cache_ptr->howto = howto_table + dst.r_type; |
| 2354 | #endif |
| 2355 | #if M68 |
| 2356 | cache_ptr->howto = howto_table + dst.r_type - R_RELBYTE; |
| 2357 | #endif |
| 2358 | #if M88 |
| 2359 | if (dst.r_type >= R_PCR16L && dst.r_type <= R_VRT32) { |
| 2360 | cache_ptr->howto = howto_table + dst.r_type - R_PCR16L; |
| 2361 | cache_ptr->addend += dst.r_offset << 16; |
| 2362 | } |
| 2363 | else { |
| 2364 | BFD_ASSERT(0); |
| 2365 | } |
| 2366 | #endif |
| 2367 | |
| 2368 | } |
| 2369 | |
| 2370 | } |
| 2371 | |
| 2372 | asect->relocation = reloc_cache; |
| 2373 | return true; |
| 2374 | } |
| 2375 | |
| 2376 | |
| 2377 | /* This is stupid. This function should be a boolean predicate */ |
| 2378 | static unsigned int |
| 2379 | coff_canonicalize_reloc(abfd, section, relptr, symbols) |
| 2380 | bfd *abfd; |
| 2381 | sec_ptr section; |
| 2382 | arelent **relptr; |
| 2383 | asymbol **symbols; |
| 2384 | { |
| 2385 | arelent *tblptr = section->relocation; |
| 2386 | unsigned int count = 0; |
| 2387 | if (!(tblptr || coff_slurp_reloc_table(abfd, section, symbols))) |
| 2388 | return 0; |
| 2389 | tblptr = section->relocation; |
| 2390 | if (!tblptr) |
| 2391 | return 0; |
| 2392 | |
| 2393 | for (; count++ < section->reloc_count;) |
| 2394 | *relptr++ = tblptr++; |
| 2395 | |
| 2396 | *relptr = 0; |
| 2397 | |
| 2398 | return section->reloc_count; |
| 2399 | } |
| 2400 | |
| 2401 | |
| 2402 | /* |
| 2403 | provided a bfd, a section and an offset into the section, calculate and |
| 2404 | return the name of the source file and the line nearest to the wanted |
| 2405 | location. |
| 2406 | */ |
| 2407 | |
| 2408 | static boolean |
| 2409 | DEFUN(coff_find_nearest_line,(abfd, |
| 2410 | section, |
| 2411 | symbols, |
| 2412 | offset, |
| 2413 | filename_ptr, |
| 2414 | functionname_ptr, |
| 2415 | line_ptr), |
| 2416 | bfd *abfd AND |
| 2417 | asection *section AND |
| 2418 | asymbol **symbols AND |
| 2419 | bfd_vma offset AND |
| 2420 | CONST char **filename_ptr AND |
| 2421 | CONST char **functionname_ptr AND |
| 2422 | unsigned int *line_ptr) |
| 2423 | { |
| 2424 | static bfd *cache_abfd; |
| 2425 | static asection *cache_section; |
| 2426 | static bfd_vma cache_offset; |
| 2427 | static unsigned int cache_i; |
| 2428 | static alent *cache_l; |
| 2429 | |
| 2430 | unsigned int i = 0; |
| 2431 | struct icofdata *cof = obj_icof(abfd); |
| 2432 | /* Run through the raw syments if available */ |
| 2433 | struct internal_syment *p; |
| 2434 | alent *l; |
| 2435 | unsigned int line_base = 0; |
| 2436 | |
| 2437 | |
| 2438 | *filename_ptr = 0; |
| 2439 | *functionname_ptr = 0; |
| 2440 | *line_ptr = 0; |
| 2441 | |
| 2442 | /* Don't try and find line numbers in a non coff file */ |
| 2443 | if (abfd->xvec->flavour != bfd_target_coff_flavour_enum) |
| 2444 | return false; |
| 2445 | |
| 2446 | if (cof == (struct icofdata *)NULL) |
| 2447 | return false; |
| 2448 | |
| 2449 | p = cof->raw_syments; |
| 2450 | /* |
| 2451 | I don't know for sure what's right, but this isn't it. First off, an |
| 2452 | object file may not have any C_FILE's in it. After |
| 2453 | get_normalized_symtab(), it should have at least 1, the one I put |
| 2454 | there, but otherwise, all bets are off. Point #2, the first C_FILE |
| 2455 | isn't necessarily the right C_FILE because any given object may have |
| 2456 | many. I think you'll have to track sections as they coelesce in order |
| 2457 | to find the C_STAT symbol for this section. Then you'll have to work |
| 2458 | backwards to find the previous C_FILE, or choke if you get to a C_STAT |
| 2459 | for the same kind of section. That will mean that the original object |
| 2460 | file didn't have a C_FILE. xoxorich. |
| 2461 | */ |
| 2462 | |
| 2463 | /* |
| 2464 | #ifdef WEREBEINGPEDANTIC |
| 2465 | return false; |
| 2466 | #endif |
| 2467 | */ |
| 2468 | for (i = 0; i < cof->raw_syment_count; i++) { |
| 2469 | if (p->n_sclass == C_FILE) { |
| 2470 | /* File name is embeded in auxent */ |
| 2471 | /* |
| 2472 | This isn't right. The fname should probably be normalized |
| 2473 | during get_normalized_symtab(). In any case, what was here |
| 2474 | wasn't right because a SYMENT.n_name isn't an |
| 2475 | AUXENT.x_file.x_fname. xoxorich. |
| 2476 | */ |
| 2477 | |
| 2478 | *filename_ptr = ((AUXENT *) (p + 1))->x_file.x_fname; |
| 2479 | break; |
| 2480 | } |
| 2481 | p += 1 + p->n_numaux; |
| 2482 | } |
| 2483 | /* Now wander though the raw linenumbers of the section */ |
| 2484 | /* |
| 2485 | If this is the same bfd as we were previously called with and this is |
| 2486 | the same section, and the offset we want is further down then we can |
| 2487 | prime the lookup loop |
| 2488 | */ |
| 2489 | if (abfd == cache_abfd && |
| 2490 | section == cache_section && |
| 2491 | offset >= cache_offset) { |
| 2492 | i = cache_i; |
| 2493 | l = cache_l; |
| 2494 | } |
| 2495 | else { |
| 2496 | i = 0; |
| 2497 | l = section->lineno; |
| 2498 | } |
| 2499 | |
| 2500 | for (; i < section->lineno_count; i++) { |
| 2501 | if (l->line_number == 0) { |
| 2502 | /* Get the symbol this line number points at */ |
| 2503 | coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym); |
| 2504 | *functionname_ptr = coff->symbol.name; |
| 2505 | if (coff->native) { |
| 2506 | struct internal_syment *s = coff->native; |
| 2507 | s = s + 1 + s->n_numaux; |
| 2508 | /* |
| 2509 | S should now point to the .bf of the function |
| 2510 | */ |
| 2511 | if (s->n_numaux) { |
| 2512 | /* |
| 2513 | The linenumber is stored in the auxent |
| 2514 | */ |
| 2515 | union internal_auxent *a = (union internal_auxent *) (s + 1); |
| 2516 | line_base = a->x_sym.x_misc.x_lnsz.x_lnno; |
| 2517 | } |
| 2518 | } |
| 2519 | } |
| 2520 | else { |
| 2521 | if (l->u.offset > offset) |
| 2522 | break; |
| 2523 | *line_ptr = l->line_number + line_base + 1; |
| 2524 | } |
| 2525 | l++; |
| 2526 | } |
| 2527 | |
| 2528 | cache_abfd = abfd; |
| 2529 | cache_section = section; |
| 2530 | cache_offset = offset; |
| 2531 | cache_i = i; |
| 2532 | cache_l = l; |
| 2533 | return true; |
| 2534 | } |
| 2535 | |
| 2536 | #ifdef GNU960 |
| 2537 | file_ptr |
| 2538 | coff_sym_filepos(abfd) |
| 2539 | bfd *abfd; |
| 2540 | { |
| 2541 | return obj_sym_filepos(abfd); |
| 2542 | } |
| 2543 | #endif |
| 2544 | |
| 2545 | |
| 2546 | static int |
| 2547 | DEFUN(coff_sizeof_headers,(abfd, reloc), |
| 2548 | bfd *abfd AND |
| 2549 | boolean reloc) |
| 2550 | { |
| 2551 | size_t size; |
| 2552 | |
| 2553 | if (reloc == false) { |
| 2554 | size = FILHSZ + AOUTSZ; |
| 2555 | } |
| 2556 | else { |
| 2557 | size = FILHSZ; |
| 2558 | } |
| 2559 | |
| 2560 | size += abfd->section_count * SCNHSZ; |
| 2561 | return size; |
| 2562 | } |
| 2563 | |
| 2564 | |
| 2565 | #define coff_core_file_failing_command _bfd_dummy_core_file_failing_command |
| 2566 | #define coff_core_file_failing_signal _bfd_dummy_core_file_failing_signal |
| 2567 | #define coff_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p |
| 2568 | #define coff_slurp_armap bfd_slurp_coff_armap |
| 2569 | #define coff_slurp_extended_name_table _bfd_slurp_extended_name_table |
| 2570 | #define coff_truncate_arname bfd_dont_truncate_arname |
| 2571 | #define coff_openr_next_archived_file bfd_generic_openr_next_archived_file |
| 2572 | #define coff_generic_stat_arch_elt bfd_generic_stat_arch_elt |
| 2573 | #define coff_get_section_contents bfd_generic_get_section_contents |
| 2574 | #define coff_close_and_cleanup bfd_generic_close_and_cleanup |