1 /* Generic ECOFF (Extended-COFF) routines. 2 Copyright (C) 1990-2014 Free Software Foundation, Inc. 3 Original version by Per Bothner. 4 Full support added by Ian Lance Taylor, ian (at) cygnus.com. 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 3 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., 51 Franklin Street - Fifth Floor, Boston, 21 MA 02110-1301, USA. */ 22 23 #include "sysdep.h" 24 #include "bfd.h" 25 #include "bfdlink.h" 26 #include "libbfd.h" 27 #include "aout/ar.h" 28 #include "aout/stab_gnu.h" 29 30 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines 31 some other stuff which we don't want and which conflicts with stuff 32 we do want. */ 33 #include "libaout.h" 34 #include "aout/aout64.h" 35 #undef N_ABS 36 #undef exec_hdr 37 #undef obj_sym_filepos 38 39 #include "coff/internal.h" 40 #include "coff/sym.h" 41 #include "coff/symconst.h" 42 #include "coff/ecoff.h" 43 #include "libcoff.h" 44 #include "libecoff.h" 45 #include "libiberty.h" 46 47 #define streq(a, b) (strcmp ((a), (b)) == 0) 48 #define strneq(a, b, n) (strncmp ((a), (b), (n)) == 0) 49 50 51 /* This stuff is somewhat copied from coffcode.h. */ 53 static asection bfd_debug_section = 54 { 55 /* name, id, index, next, prev, flags, user_set_vma, */ 56 "*DEBUG*", 0, 0, NULL, NULL, 0, 0, 57 /* linker_mark, linker_has_input, gc_mark, compress_status, */ 58 0, 0, 1, 0, 59 /* segment_mark, sec_info_type, use_rela_p, */ 60 0, 0, 0, 61 /* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5, */ 62 0, 0, 0, 0, 0, 0, 63 /* vma, lma, size, rawsize, compressed_size, relax, relax_count, */ 64 0, 0, 0, 0, 0, 0, 0, 65 /* output_offset, output_section, alignment_power, */ 66 0, NULL, 0, 67 /* relocation, orelocation, reloc_count, filepos, rel_filepos, */ 68 NULL, NULL, 0, 0, 0, 69 /* line_filepos, userdata, contents, lineno, lineno_count, */ 70 0, NULL, NULL, NULL, 0, 71 /* entsize, kept_section, moving_line_filepos, */ 72 0, NULL, 0, 73 /* target_index, used_by_bfd, constructor_chain, owner, */ 74 0, NULL, NULL, NULL, 75 /* symbol, */ 76 NULL, 77 /* symbol_ptr_ptr, */ 78 NULL, 79 /* map_head, map_tail */ 80 { NULL }, { NULL } 81 }; 82 83 /* Create an ECOFF object. */ 84 85 bfd_boolean 86 _bfd_ecoff_mkobject (bfd *abfd) 87 { 88 bfd_size_type amt = sizeof (ecoff_data_type); 89 90 abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt); 91 if (abfd->tdata.ecoff_obj_data == NULL) 92 return FALSE; 93 94 return TRUE; 95 } 96 97 /* This is a hook called by coff_real_object_p to create any backend 98 specific information. */ 99 100 void * 101 _bfd_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr) 102 { 103 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr; 104 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr; 105 ecoff_data_type *ecoff; 106 107 if (! _bfd_ecoff_mkobject (abfd)) 108 return NULL; 109 110 ecoff = ecoff_data (abfd); 111 ecoff->gp_size = 8; 112 ecoff->sym_filepos = internal_f->f_symptr; 113 114 if (internal_a != NULL) 115 { 116 int i; 117 118 ecoff->text_start = internal_a->text_start; 119 ecoff->text_end = internal_a->text_start + internal_a->tsize; 120 ecoff->gp = internal_a->gp_value; 121 ecoff->gprmask = internal_a->gprmask; 122 for (i = 0; i < 4; i++) 123 ecoff->cprmask[i] = internal_a->cprmask[i]; 124 ecoff->fprmask = internal_a->fprmask; 125 if (internal_a->magic == ECOFF_AOUT_ZMAGIC) 126 abfd->flags |= D_PAGED; 127 else 128 abfd->flags &=~ D_PAGED; 129 } 130 131 /* It turns out that no special action is required by the MIPS or 132 Alpha ECOFF backends. They have different information in the 133 a.out header, but we just copy it all (e.g., gprmask, cprmask and 134 fprmask) and let the swapping routines ensure that only relevant 135 information is written out. */ 136 137 return (void *) ecoff; 138 } 139 140 /* Initialize a new section. */ 141 142 bfd_boolean 143 _bfd_ecoff_new_section_hook (bfd *abfd, asection *section) 144 { 145 unsigned int i; 146 static struct 147 { 148 const char * name; 149 flagword flags; 150 } 151 section_flags [] = 152 { 153 { _TEXT, SEC_ALLOC | SEC_CODE | SEC_LOAD }, 154 { _INIT, SEC_ALLOC | SEC_CODE | SEC_LOAD }, 155 { _FINI, SEC_ALLOC | SEC_CODE | SEC_LOAD }, 156 { _DATA, SEC_ALLOC | SEC_DATA | SEC_LOAD }, 157 { _SDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD }, 158 { _RDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY}, 159 { _LIT8, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY}, 160 { _LIT4, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY}, 161 { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY}, 162 { _PDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY}, 163 { _BSS, SEC_ALLOC}, 164 { _SBSS, SEC_ALLOC}, 165 /* An Irix 4 shared libary. */ 166 { _LIB, SEC_COFF_SHARED_LIBRARY} 167 }; 168 169 section->alignment_power = 4; 170 171 for (i = 0; i < ARRAY_SIZE (section_flags); i++) 172 if (streq (section->name, section_flags[i].name)) 173 { 174 section->flags |= section_flags[i].flags; 175 break; 176 } 177 178 179 /* Probably any other section name is SEC_NEVER_LOAD, but I'm 180 uncertain about .init on some systems and I don't know how shared 181 libraries work. */ 182 183 return _bfd_generic_new_section_hook (abfd, section); 184 } 185 186 /* Determine the machine architecture and type. This is called from 187 the generic COFF routines. It is the inverse of ecoff_get_magic, 188 below. This could be an ECOFF backend routine, with one version 189 for each target, but there aren't all that many ECOFF targets. */ 190 191 bfd_boolean 192 _bfd_ecoff_set_arch_mach_hook (bfd *abfd, void * filehdr) 193 { 194 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr; 195 enum bfd_architecture arch; 196 unsigned long mach; 197 198 switch (internal_f->f_magic) 199 { 200 case MIPS_MAGIC_1: 201 case MIPS_MAGIC_LITTLE: 202 case MIPS_MAGIC_BIG: 203 arch = bfd_arch_mips; 204 mach = bfd_mach_mips3000; 205 break; 206 207 case MIPS_MAGIC_LITTLE2: 208 case MIPS_MAGIC_BIG2: 209 /* MIPS ISA level 2: the r6000. */ 210 arch = bfd_arch_mips; 211 mach = bfd_mach_mips6000; 212 break; 213 214 case MIPS_MAGIC_LITTLE3: 215 case MIPS_MAGIC_BIG3: 216 /* MIPS ISA level 3: the r4000. */ 217 arch = bfd_arch_mips; 218 mach = bfd_mach_mips4000; 219 break; 220 221 case ALPHA_MAGIC: 222 arch = bfd_arch_alpha; 223 mach = 0; 224 break; 225 226 default: 227 arch = bfd_arch_obscure; 228 mach = 0; 229 break; 230 } 231 232 return bfd_default_set_arch_mach (abfd, arch, mach); 233 } 234 235 bfd_boolean 236 _bfd_ecoff_no_long_sections (bfd *abfd, int enable) 237 { 238 (void) abfd; 239 (void) enable; 240 return FALSE; 241 } 242 243 /* Get the magic number to use based on the architecture and machine. 244 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */ 245 246 static int 247 ecoff_get_magic (bfd *abfd) 248 { 249 int big, little; 250 251 switch (bfd_get_arch (abfd)) 252 { 253 case bfd_arch_mips: 254 switch (bfd_get_mach (abfd)) 255 { 256 default: 257 case 0: 258 case bfd_mach_mips3000: 259 big = MIPS_MAGIC_BIG; 260 little = MIPS_MAGIC_LITTLE; 261 break; 262 263 case bfd_mach_mips6000: 264 big = MIPS_MAGIC_BIG2; 265 little = MIPS_MAGIC_LITTLE2; 266 break; 267 268 case bfd_mach_mips4000: 269 big = MIPS_MAGIC_BIG3; 270 little = MIPS_MAGIC_LITTLE3; 271 break; 272 } 273 274 return bfd_big_endian (abfd) ? big : little; 275 276 case bfd_arch_alpha: 277 return ALPHA_MAGIC; 278 279 default: 280 abort (); 281 return 0; 282 } 283 } 284 285 /* Get the section s_flags to use for a section. */ 286 287 static long 288 ecoff_sec_to_styp_flags (const char *name, flagword flags) 289 { 290 unsigned int i; 291 static struct 292 { 293 const char * name; 294 long flags; 295 } 296 styp_flags [] = 297 { 298 { _TEXT, STYP_TEXT }, 299 { _DATA, STYP_DATA }, 300 { _SDATA, STYP_SDATA }, 301 { _RDATA, STYP_RDATA }, 302 { _LITA, STYP_LITA }, 303 { _LIT8, STYP_LIT8 }, 304 { _LIT4, STYP_LIT4 }, 305 { _BSS, STYP_BSS }, 306 { _SBSS, STYP_SBSS }, 307 { _INIT, STYP_ECOFF_INIT }, 308 { _FINI, STYP_ECOFF_FINI }, 309 { _PDATA, STYP_PDATA }, 310 { _XDATA, STYP_XDATA }, 311 { _LIB, STYP_ECOFF_LIB }, 312 { _GOT, STYP_GOT }, 313 { _HASH, STYP_HASH }, 314 { _DYNAMIC, STYP_DYNAMIC }, 315 { _LIBLIST, STYP_LIBLIST }, 316 { _RELDYN, STYP_RELDYN }, 317 { _CONFLIC, STYP_CONFLIC }, 318 { _DYNSTR, STYP_DYNSTR }, 319 { _DYNSYM, STYP_DYNSYM }, 320 { _RCONST, STYP_RCONST } 321 }; 322 long styp = 0; 323 324 for (i = 0; i < ARRAY_SIZE (styp_flags); i++) 325 if (streq (name, styp_flags[i].name)) 326 { 327 styp = styp_flags[i].flags; 328 break; 329 } 330 331 if (styp == 0) 332 { 333 if (streq (name, _COMMENT)) 334 { 335 styp = STYP_COMMENT; 336 flags &=~ SEC_NEVER_LOAD; 337 } 338 else if (flags & SEC_CODE) 339 styp = STYP_TEXT; 340 else if (flags & SEC_DATA) 341 styp = STYP_DATA; 342 else if (flags & SEC_READONLY) 343 styp = STYP_RDATA; 344 else if (flags & SEC_LOAD) 345 styp = STYP_REG; 346 else 347 styp = STYP_BSS; 348 } 349 350 if (flags & SEC_NEVER_LOAD) 351 styp |= STYP_NOLOAD; 352 353 return styp; 354 } 355 356 /* Get the BFD flags to use for a section. */ 357 358 bfd_boolean 359 _bfd_ecoff_styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED, 360 void * hdr, 361 const char *name ATTRIBUTE_UNUSED, 362 asection *section ATTRIBUTE_UNUSED, 363 flagword * flags_ptr) 364 { 365 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr; 366 long styp_flags = internal_s->s_flags; 367 flagword sec_flags = 0; 368 369 if (styp_flags & STYP_NOLOAD) 370 sec_flags |= SEC_NEVER_LOAD; 371 372 /* For 386 COFF, at least, an unloadable text or data section is 373 actually a shared library section. */ 374 if ((styp_flags & STYP_TEXT) 375 || (styp_flags & STYP_ECOFF_INIT) 376 || (styp_flags & STYP_ECOFF_FINI) 377 || (styp_flags & STYP_DYNAMIC) 378 || (styp_flags & STYP_LIBLIST) 379 || (styp_flags & STYP_RELDYN) 380 || styp_flags == STYP_CONFLIC 381 || (styp_flags & STYP_DYNSTR) 382 || (styp_flags & STYP_DYNSYM) 383 || (styp_flags & STYP_HASH)) 384 { 385 if (sec_flags & SEC_NEVER_LOAD) 386 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY; 387 else 388 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC; 389 } 390 else if ((styp_flags & STYP_DATA) 391 || (styp_flags & STYP_RDATA) 392 || (styp_flags & STYP_SDATA) 393 || styp_flags == STYP_PDATA 394 || styp_flags == STYP_XDATA 395 || (styp_flags & STYP_GOT) 396 || styp_flags == STYP_RCONST) 397 { 398 if (sec_flags & SEC_NEVER_LOAD) 399 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY; 400 else 401 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC; 402 if ((styp_flags & STYP_RDATA) 403 || styp_flags == STYP_PDATA 404 || styp_flags == STYP_RCONST) 405 sec_flags |= SEC_READONLY; 406 } 407 else if ((styp_flags & STYP_BSS) 408 || (styp_flags & STYP_SBSS)) 409 sec_flags |= SEC_ALLOC; 410 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT) 411 sec_flags |= SEC_NEVER_LOAD; 412 else if ((styp_flags & STYP_LITA) 413 || (styp_flags & STYP_LIT8) 414 || (styp_flags & STYP_LIT4)) 415 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY; 416 else if (styp_flags & STYP_ECOFF_LIB) 417 sec_flags |= SEC_COFF_SHARED_LIBRARY; 418 else 419 sec_flags |= SEC_ALLOC | SEC_LOAD; 420 421 * flags_ptr = sec_flags; 422 return TRUE; 423 } 424 425 /* Read in the symbolic header for an ECOFF object file. */ 427 428 static bfd_boolean 429 ecoff_slurp_symbolic_header (bfd *abfd) 430 { 431 const struct ecoff_backend_data * const backend = ecoff_backend (abfd); 432 bfd_size_type external_hdr_size; 433 void * raw = NULL; 434 HDRR *internal_symhdr; 435 436 /* See if we've already read it in. */ 437 if (ecoff_data (abfd)->debug_info.symbolic_header.magic == 438 backend->debug_swap.sym_magic) 439 return TRUE; 440 441 /* See whether there is a symbolic header. */ 442 if (ecoff_data (abfd)->sym_filepos == 0) 443 { 444 bfd_get_symcount (abfd) = 0; 445 return TRUE; 446 } 447 448 /* At this point bfd_get_symcount (abfd) holds the number of symbols 449 as read from the file header, but on ECOFF this is always the 450 size of the symbolic information header. It would be cleaner to 451 handle this when we first read the file in coffgen.c. */ 452 external_hdr_size = backend->debug_swap.external_hdr_size; 453 if (bfd_get_symcount (abfd) != external_hdr_size) 454 { 455 bfd_set_error (bfd_error_bad_value); 456 return FALSE; 457 } 458 459 /* Read the symbolic information header. */ 460 raw = bfd_malloc (external_hdr_size); 461 if (raw == NULL) 462 goto error_return; 463 464 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0 465 || bfd_bread (raw, external_hdr_size, abfd) != external_hdr_size) 466 goto error_return; 467 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header; 468 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr); 469 470 if (internal_symhdr->magic != backend->debug_swap.sym_magic) 471 { 472 bfd_set_error (bfd_error_bad_value); 473 goto error_return; 474 } 475 476 /* Now we can get the correct number of symbols. */ 477 bfd_get_symcount (abfd) = (internal_symhdr->isymMax 478 + internal_symhdr->iextMax); 479 480 if (raw != NULL) 481 free (raw); 482 return TRUE; 483 error_return: 484 if (raw != NULL) 485 free (raw); 486 return FALSE; 487 } 488 489 /* Read in and swap the important symbolic information for an ECOFF 490 object file. This is called by gdb via the read_debug_info entry 491 point in the backend structure. */ 492 493 bfd_boolean 494 _bfd_ecoff_slurp_symbolic_info (bfd *abfd, 495 asection *ignore ATTRIBUTE_UNUSED, 496 struct ecoff_debug_info *debug) 497 { 498 const struct ecoff_backend_data * const backend = ecoff_backend (abfd); 499 HDRR *internal_symhdr; 500 bfd_size_type raw_base; 501 bfd_size_type raw_size; 502 void * raw; 503 bfd_size_type external_fdr_size; 504 char *fraw_src; 505 char *fraw_end; 506 struct fdr *fdr_ptr; 507 bfd_size_type raw_end; 508 bfd_size_type cb_end; 509 bfd_size_type amt; 510 file_ptr pos; 511 512 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info); 513 514 /* Check whether we've already gotten it, and whether there's any to 515 get. */ 516 if (ecoff_data (abfd)->raw_syments != NULL) 517 return TRUE; 518 if (ecoff_data (abfd)->sym_filepos == 0) 519 { 520 bfd_get_symcount (abfd) = 0; 521 return TRUE; 522 } 523 524 if (! ecoff_slurp_symbolic_header (abfd)) 525 return FALSE; 526 527 internal_symhdr = &debug->symbolic_header; 528 529 /* Read all the symbolic information at once. */ 530 raw_base = (ecoff_data (abfd)->sym_filepos 531 + backend->debug_swap.external_hdr_size); 532 533 /* Alpha ecoff makes the determination of raw_size difficult. It has 534 an undocumented debug data section between the symhdr and the first 535 documented section. And the ordering of the sections varies between 536 statically and dynamically linked executables. 537 If bfd supports SEEK_END someday, this code could be simplified. */ 538 raw_end = 0; 539 540 #define UPDATE_RAW_END(start, count, size) \ 541 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \ 542 if (cb_end > raw_end) \ 543 raw_end = cb_end 544 545 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char)); 546 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size); 547 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size); 548 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size); 549 /* eraxxon (at) alumni.rice.edu: ioptMax refers to the size of the 550 optimization symtab, not the number of entries. */ 551 UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char)); 552 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext)); 553 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char)); 554 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char)); 555 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size); 556 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size); 557 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size); 558 559 #undef UPDATE_RAW_END 560 561 raw_size = raw_end - raw_base; 562 if (raw_size == 0) 563 { 564 ecoff_data (abfd)->sym_filepos = 0; 565 return TRUE; 566 } 567 raw = bfd_alloc (abfd, raw_size); 568 if (raw == NULL) 569 return FALSE; 570 571 pos = ecoff_data (abfd)->sym_filepos; 572 pos += backend->debug_swap.external_hdr_size; 573 if (bfd_seek (abfd, pos, SEEK_SET) != 0 574 || bfd_bread (raw, raw_size, abfd) != raw_size) 575 { 576 bfd_release (abfd, raw); 577 return FALSE; 578 } 579 580 ecoff_data (abfd)->raw_syments = raw; 581 582 /* Get pointers for the numeric offsets in the HDRR structure. */ 583 #define FIX(off1, off2, type) \ 584 if (internal_symhdr->off1 == 0) \ 585 debug->off2 = NULL; \ 586 else \ 587 debug->off2 = (type) ((char *) raw \ 588 + (internal_symhdr->off1 \ 589 - raw_base)) 590 591 FIX (cbLineOffset, line, unsigned char *); 592 FIX (cbDnOffset, external_dnr, void *); 593 FIX (cbPdOffset, external_pdr, void *); 594 FIX (cbSymOffset, external_sym, void *); 595 FIX (cbOptOffset, external_opt, void *); 596 FIX (cbAuxOffset, external_aux, union aux_ext *); 597 FIX (cbSsOffset, ss, char *); 598 FIX (cbSsExtOffset, ssext, char *); 599 FIX (cbFdOffset, external_fdr, void *); 600 FIX (cbRfdOffset, external_rfd, void *); 601 FIX (cbExtOffset, external_ext, void *); 602 #undef FIX 603 604 /* I don't want to always swap all the data, because it will just 605 waste time and most programs will never look at it. The only 606 time the linker needs most of the debugging information swapped 607 is when linking big-endian and little-endian MIPS object files 608 together, which is not a common occurrence. 609 610 We need to look at the fdr to deal with a lot of information in 611 the symbols, so we swap them here. */ 612 amt = internal_symhdr->ifdMax; 613 amt *= sizeof (struct fdr); 614 debug->fdr = (FDR *) bfd_alloc (abfd, amt); 615 if (debug->fdr == NULL) 616 return FALSE; 617 external_fdr_size = backend->debug_swap.external_fdr_size; 618 fdr_ptr = debug->fdr; 619 fraw_src = (char *) debug->external_fdr; 620 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size; 621 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++) 622 (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr); 623 624 return TRUE; 625 } 626 627 /* ECOFF symbol table routines. The ECOFF symbol table is described 629 in gcc/mips-tfile.c. */ 630 631 /* ECOFF uses two common sections. One is the usual one, and the 632 other is for small objects. All the small objects are kept 633 together, and then referenced via the gp pointer, which yields 634 faster assembler code. This is what we use for the small common 635 section. */ 636 static asection ecoff_scom_section; 637 static asymbol ecoff_scom_symbol; 638 static asymbol *ecoff_scom_symbol_ptr; 639 640 /* Create an empty symbol. */ 641 642 asymbol * 643 _bfd_ecoff_make_empty_symbol (bfd *abfd) 644 { 645 ecoff_symbol_type *new_symbol; 646 bfd_size_type amt = sizeof (ecoff_symbol_type); 647 648 new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt); 649 if (new_symbol == NULL) 650 return NULL; 651 new_symbol->symbol.section = NULL; 652 new_symbol->fdr = NULL; 653 new_symbol->local = FALSE; 654 new_symbol->native = NULL; 655 new_symbol->symbol.the_bfd = abfd; 656 return &new_symbol->symbol; 657 } 658 659 /* Set the BFD flags and section for an ECOFF symbol. */ 660 661 static bfd_boolean 662 ecoff_set_symbol_info (bfd *abfd, 663 SYMR *ecoff_sym, 664 asymbol *asym, 665 int ext, 666 int weak) 667 { 668 asym->the_bfd = abfd; 669 asym->value = ecoff_sym->value; 670 asym->section = &bfd_debug_section; 671 asym->udata.i = 0; 672 673 /* Most symbol types are just for debugging. */ 674 switch (ecoff_sym->st) 675 { 676 case stGlobal: 677 case stStatic: 678 case stLabel: 679 case stProc: 680 case stStaticProc: 681 break; 682 case stNil: 683 if (ECOFF_IS_STAB (ecoff_sym)) 684 { 685 asym->flags = BSF_DEBUGGING; 686 return TRUE; 687 } 688 break; 689 default: 690 asym->flags = BSF_DEBUGGING; 691 return TRUE; 692 } 693 694 if (weak) 695 asym->flags = BSF_EXPORT | BSF_WEAK; 696 else if (ext) 697 asym->flags = BSF_EXPORT | BSF_GLOBAL; 698 else 699 { 700 asym->flags = BSF_LOCAL; 701 /* Normally, a local stProc symbol will have a corresponding 702 external symbol. We mark the local symbol as a debugging 703 symbol, in order to prevent nm from printing both out. 704 Similarly, we mark stLabel and stabs symbols as debugging 705 symbols. In both cases, we do want to set the value 706 correctly based on the symbol class. */ 707 if (ecoff_sym->st == stProc 708 || ecoff_sym->st == stLabel 709 || ECOFF_IS_STAB (ecoff_sym)) 710 asym->flags |= BSF_DEBUGGING; 711 } 712 713 if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc) 714 asym->flags |= BSF_FUNCTION; 715 716 switch (ecoff_sym->sc) 717 { 718 case scNil: 719 /* Used for compiler generated labels. Leave them in the 720 debugging section, and mark them as local. If BSF_DEBUGGING 721 is set, then nm does not display them for some reason. If no 722 flags are set then the linker whines about them. */ 723 asym->flags = BSF_LOCAL; 724 break; 725 case scText: 726 asym->section = bfd_make_section_old_way (abfd, _TEXT); 727 asym->value -= asym->section->vma; 728 break; 729 case scData: 730 asym->section = bfd_make_section_old_way (abfd, _DATA); 731 asym->value -= asym->section->vma; 732 break; 733 case scBss: 734 asym->section = bfd_make_section_old_way (abfd, _BSS); 735 asym->value -= asym->section->vma; 736 break; 737 case scRegister: 738 asym->flags = BSF_DEBUGGING; 739 break; 740 case scAbs: 741 asym->section = bfd_abs_section_ptr; 742 break; 743 case scUndefined: 744 asym->section = bfd_und_section_ptr; 745 asym->flags = 0; 746 asym->value = 0; 747 break; 748 case scCdbLocal: 749 case scBits: 750 case scCdbSystem: 751 case scRegImage: 752 case scInfo: 753 case scUserStruct: 754 asym->flags = BSF_DEBUGGING; 755 break; 756 case scSData: 757 asym->section = bfd_make_section_old_way (abfd, ".sdata"); 758 asym->value -= asym->section->vma; 759 break; 760 case scSBss: 761 asym->section = bfd_make_section_old_way (abfd, ".sbss"); 762 asym->value -= asym->section->vma; 763 break; 764 case scRData: 765 asym->section = bfd_make_section_old_way (abfd, ".rdata"); 766 asym->value -= asym->section->vma; 767 break; 768 case scVar: 769 asym->flags = BSF_DEBUGGING; 770 break; 771 case scCommon: 772 if (asym->value > ecoff_data (abfd)->gp_size) 773 { 774 asym->section = bfd_com_section_ptr; 775 asym->flags = 0; 776 break; 777 } 778 /* Fall through. */ 779 case scSCommon: 780 if (ecoff_scom_section.name == NULL) 781 { 782 /* Initialize the small common section. */ 783 ecoff_scom_section.name = SCOMMON; 784 ecoff_scom_section.flags = SEC_IS_COMMON; 785 ecoff_scom_section.output_section = &ecoff_scom_section; 786 ecoff_scom_section.symbol = &ecoff_scom_symbol; 787 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr; 788 ecoff_scom_symbol.name = SCOMMON; 789 ecoff_scom_symbol.flags = BSF_SECTION_SYM; 790 ecoff_scom_symbol.section = &ecoff_scom_section; 791 ecoff_scom_symbol_ptr = &ecoff_scom_symbol; 792 } 793 asym->section = &ecoff_scom_section; 794 asym->flags = 0; 795 break; 796 case scVarRegister: 797 case scVariant: 798 asym->flags = BSF_DEBUGGING; 799 break; 800 case scSUndefined: 801 asym->section = bfd_und_section_ptr; 802 asym->flags = 0; 803 asym->value = 0; 804 break; 805 case scInit: 806 asym->section = bfd_make_section_old_way (abfd, ".init"); 807 asym->value -= asym->section->vma; 808 break; 809 case scBasedVar: 810 case scXData: 811 case scPData: 812 asym->flags = BSF_DEBUGGING; 813 break; 814 case scFini: 815 asym->section = bfd_make_section_old_way (abfd, ".fini"); 816 asym->value -= asym->section->vma; 817 break; 818 case scRConst: 819 asym->section = bfd_make_section_old_way (abfd, ".rconst"); 820 asym->value -= asym->section->vma; 821 break; 822 default: 823 break; 824 } 825 826 /* Look for special constructors symbols and make relocation entries 827 in a special construction section. These are produced by the 828 -fgnu-linker argument to g++. */ 829 if (ECOFF_IS_STAB (ecoff_sym)) 830 { 831 switch (ECOFF_UNMARK_STAB (ecoff_sym->index)) 832 { 833 default: 834 break; 835 836 case N_SETA: 837 case N_SETT: 838 case N_SETD: 839 case N_SETB: 840 /* Mark the symbol as a constructor. */ 841 asym->flags |= BSF_CONSTRUCTOR; 842 break; 843 } 844 } 845 return TRUE; 846 } 847 848 /* Read an ECOFF symbol table. */ 849 850 bfd_boolean 851 _bfd_ecoff_slurp_symbol_table (bfd *abfd) 852 { 853 const struct ecoff_backend_data * const backend = ecoff_backend (abfd); 854 const bfd_size_type external_ext_size 855 = backend->debug_swap.external_ext_size; 856 const bfd_size_type external_sym_size 857 = backend->debug_swap.external_sym_size; 858 void (* const swap_ext_in) (bfd *, void *, EXTR *) 859 = backend->debug_swap.swap_ext_in; 860 void (* const swap_sym_in) (bfd *, void *, SYMR *) 861 = backend->debug_swap.swap_sym_in; 862 bfd_size_type internal_size; 863 ecoff_symbol_type *internal; 864 ecoff_symbol_type *internal_ptr; 865 char *eraw_src; 866 char *eraw_end; 867 FDR *fdr_ptr; 868 FDR *fdr_end; 869 870 /* If we've already read in the symbol table, do nothing. */ 871 if (ecoff_data (abfd)->canonical_symbols != NULL) 872 return TRUE; 873 874 /* Get the symbolic information. */ 875 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, 876 &ecoff_data (abfd)->debug_info)) 877 return FALSE; 878 if (bfd_get_symcount (abfd) == 0) 879 return TRUE; 880 881 internal_size = bfd_get_symcount (abfd); 882 internal_size *= sizeof (ecoff_symbol_type); 883 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size); 884 if (internal == NULL) 885 return FALSE; 886 887 internal_ptr = internal; 888 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext; 889 eraw_end = (eraw_src 890 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax 891 * external_ext_size)); 892 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++) 893 { 894 EXTR internal_esym; 895 896 (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym); 897 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext 898 + internal_esym.asym.iss); 899 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym, 900 &internal_ptr->symbol, 1, 901 internal_esym.weakext)) 902 return FALSE; 903 /* The alpha uses a negative ifd field for section symbols. */ 904 if (internal_esym.ifd >= 0) 905 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr 906 + internal_esym.ifd); 907 else 908 internal_ptr->fdr = NULL; 909 internal_ptr->local = FALSE; 910 internal_ptr->native = (void *) eraw_src; 911 } 912 913 /* The local symbols must be accessed via the fdr's, because the 914 string and aux indices are relative to the fdr information. */ 915 fdr_ptr = ecoff_data (abfd)->debug_info.fdr; 916 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax; 917 for (; fdr_ptr < fdr_end; fdr_ptr++) 918 { 919 char *lraw_src; 920 char *lraw_end; 921 922 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym 923 + fdr_ptr->isymBase * external_sym_size); 924 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size; 925 for (; 926 lraw_src < lraw_end; 927 lraw_src += external_sym_size, internal_ptr++) 928 { 929 SYMR internal_sym; 930 931 (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym); 932 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss 933 + fdr_ptr->issBase 934 + internal_sym.iss); 935 if (!ecoff_set_symbol_info (abfd, &internal_sym, 936 &internal_ptr->symbol, 0, 0)) 937 return FALSE; 938 internal_ptr->fdr = fdr_ptr; 939 internal_ptr->local = TRUE; 940 internal_ptr->native = (void *) lraw_src; 941 } 942 } 943 944 ecoff_data (abfd)->canonical_symbols = internal; 945 946 return TRUE; 947 } 948 949 /* Return the amount of space needed for the canonical symbols. */ 950 951 long 952 _bfd_ecoff_get_symtab_upper_bound (bfd *abfd) 953 { 954 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, 955 &ecoff_data (abfd)->debug_info)) 956 return -1; 957 958 if (bfd_get_symcount (abfd) == 0) 959 return 0; 960 961 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *)); 962 } 963 964 /* Get the canonical symbols. */ 965 966 long 967 _bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation) 968 { 969 unsigned int counter = 0; 970 ecoff_symbol_type *symbase; 971 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation; 972 973 if (! _bfd_ecoff_slurp_symbol_table (abfd)) 974 return -1; 975 if (bfd_get_symcount (abfd) == 0) 976 return 0; 977 978 symbase = ecoff_data (abfd)->canonical_symbols; 979 while (counter < bfd_get_symcount (abfd)) 980 { 981 *(location++) = symbase++; 982 counter++; 983 } 984 *location++ = NULL; 985 return bfd_get_symcount (abfd); 986 } 987 988 /* Turn ECOFF type information into a printable string. 989 ecoff_emit_aggregate and ecoff_type_to_string are from 990 gcc/mips-tdump.c, with swapping added and used_ptr removed. */ 991 992 /* Write aggregate information to a string. */ 993 994 static void 995 ecoff_emit_aggregate (bfd *abfd, 996 FDR *fdr, 997 char *string, 998 RNDXR *rndx, 999 long isym, 1000 const char *which) 1001 { 1002 const struct ecoff_debug_swap * const debug_swap = 1003 &ecoff_backend (abfd)->debug_swap; 1004 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info; 1005 unsigned int ifd = rndx->rfd; 1006 unsigned int indx = rndx->index; 1007 const char *name; 1008 1009 if (ifd == 0xfff) 1010 ifd = isym; 1011 1012 /* An ifd of -1 is an opaque type. An escaped index of 0 is a 1013 struct return type of a procedure compiled without -g. */ 1014 if (ifd == 0xffffffff 1015 || (rndx->rfd == 0xfff && indx == 0)) 1016 name = "<undefined>"; 1017 else if (indx == indexNil) 1018 name = "<no name>"; 1019 else 1020 { 1021 SYMR sym; 1022 1023 if (debug_info->external_rfd == NULL) 1024 fdr = debug_info->fdr + ifd; 1025 else 1026 { 1027 RFDT rfd; 1028 1029 (*debug_swap->swap_rfd_in) (abfd, 1030 ((char *) debug_info->external_rfd 1031 + ((fdr->rfdBase + ifd) 1032 * debug_swap->external_rfd_size)), 1033 &rfd); 1034 fdr = debug_info->fdr + rfd; 1035 } 1036 1037 indx += fdr->isymBase; 1038 1039 (*debug_swap->swap_sym_in) (abfd, 1040 ((char *) debug_info->external_sym 1041 + indx * debug_swap->external_sym_size), 1042 &sym); 1043 1044 name = debug_info->ss + fdr->issBase + sym.iss; 1045 } 1046 1047 sprintf (string, 1048 "%s %s { ifd = %u, index = %lu }", 1049 which, name, ifd, 1050 ((unsigned long) indx 1051 + debug_info->symbolic_header.iextMax)); 1052 } 1053 1054 /* Convert the type information to string format. */ 1055 1056 static char * 1057 ecoff_type_to_string (bfd *abfd, FDR *fdr, unsigned int indx) 1058 { 1059 union aux_ext *aux_ptr; 1060 int bigendian; 1061 AUXU u; 1062 struct qual 1063 { 1064 unsigned int type; 1065 int low_bound; 1066 int high_bound; 1067 int stride; 1068 } qualifiers[7]; 1069 unsigned int basic_type; 1070 int i; 1071 char buffer1[1024]; 1072 static char buffer2[1024]; 1073 char *p1 = buffer1; 1074 char *p2 = buffer2; 1075 RNDXR rndx; 1076 1077 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase; 1078 bigendian = fdr->fBigendian; 1079 1080 for (i = 0; i < 7; i++) 1081 { 1082 qualifiers[i].low_bound = 0; 1083 qualifiers[i].high_bound = 0; 1084 qualifiers[i].stride = 0; 1085 } 1086 1087 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1) 1088 return "-1 (no type)"; 1089 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti); 1090 1091 basic_type = u.ti.bt; 1092 qualifiers[0].type = u.ti.tq0; 1093 qualifiers[1].type = u.ti.tq1; 1094 qualifiers[2].type = u.ti.tq2; 1095 qualifiers[3].type = u.ti.tq3; 1096 qualifiers[4].type = u.ti.tq4; 1097 qualifiers[5].type = u.ti.tq5; 1098 qualifiers[6].type = tqNil; 1099 1100 /* Go get the basic type. */ 1101 switch (basic_type) 1102 { 1103 case btNil: /* Undefined. */ 1104 strcpy (p1, "nil"); 1105 break; 1106 1107 case btAdr: /* Address - integer same size as pointer. */ 1108 strcpy (p1, "address"); 1109 break; 1110 1111 case btChar: /* Character. */ 1112 strcpy (p1, "char"); 1113 break; 1114 1115 case btUChar: /* Unsigned character. */ 1116 strcpy (p1, "unsigned char"); 1117 break; 1118 1119 case btShort: /* Short. */ 1120 strcpy (p1, "short"); 1121 break; 1122 1123 case btUShort: /* Unsigned short. */ 1124 strcpy (p1, "unsigned short"); 1125 break; 1126 1127 case btInt: /* Int. */ 1128 strcpy (p1, "int"); 1129 break; 1130 1131 case btUInt: /* Unsigned int. */ 1132 strcpy (p1, "unsigned int"); 1133 break; 1134 1135 case btLong: /* Long. */ 1136 strcpy (p1, "long"); 1137 break; 1138 1139 case btULong: /* Unsigned long. */ 1140 strcpy (p1, "unsigned long"); 1141 break; 1142 1143 case btFloat: /* Float (real). */ 1144 strcpy (p1, "float"); 1145 break; 1146 1147 case btDouble: /* Double (real). */ 1148 strcpy (p1, "double"); 1149 break; 1150 1151 /* Structures add 1-2 aux words: 1152 1st word is [ST_RFDESCAPE, offset] pointer to struct def; 1153 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */ 1154 1155 case btStruct: /* Structure (Record). */ 1156 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx); 1157 ecoff_emit_aggregate (abfd, fdr, p1, &rndx, 1158 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]), 1159 "struct"); 1160 indx++; /* Skip aux words. */ 1161 break; 1162 1163 /* Unions add 1-2 aux words: 1164 1st word is [ST_RFDESCAPE, offset] pointer to union def; 1165 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */ 1166 1167 case btUnion: /* Union. */ 1168 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx); 1169 ecoff_emit_aggregate (abfd, fdr, p1, &rndx, 1170 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]), 1171 "union"); 1172 indx++; /* Skip aux words. */ 1173 break; 1174 1175 /* Enumerations add 1-2 aux words: 1176 1st word is [ST_RFDESCAPE, offset] pointer to enum def; 1177 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */ 1178 1179 case btEnum: /* Enumeration. */ 1180 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx); 1181 ecoff_emit_aggregate (abfd, fdr, p1, &rndx, 1182 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]), 1183 "enum"); 1184 indx++; /* Skip aux words. */ 1185 break; 1186 1187 case btTypedef: /* Defined via a typedef, isymRef points. */ 1188 strcpy (p1, "typedef"); 1189 break; 1190 1191 case btRange: /* Subrange of int. */ 1192 strcpy (p1, "subrange"); 1193 break; 1194 1195 case btSet: /* Pascal sets. */ 1196 strcpy (p1, "set"); 1197 break; 1198 1199 case btComplex: /* Fortran complex. */ 1200 strcpy (p1, "complex"); 1201 break; 1202 1203 case btDComplex: /* Fortran double complex. */ 1204 strcpy (p1, "double complex"); 1205 break; 1206 1207 case btIndirect: /* Forward or unnamed typedef. */ 1208 strcpy (p1, "forward/unamed typedef"); 1209 break; 1210 1211 case btFixedDec: /* Fixed Decimal. */ 1212 strcpy (p1, "fixed decimal"); 1213 break; 1214 1215 case btFloatDec: /* Float Decimal. */ 1216 strcpy (p1, "float decimal"); 1217 break; 1218 1219 case btString: /* Varying Length Character String. */ 1220 strcpy (p1, "string"); 1221 break; 1222 1223 case btBit: /* Aligned Bit String. */ 1224 strcpy (p1, "bit"); 1225 break; 1226 1227 case btPicture: /* Picture. */ 1228 strcpy (p1, "picture"); 1229 break; 1230 1231 case btVoid: /* Void. */ 1232 strcpy (p1, "void"); 1233 break; 1234 1235 default: 1236 sprintf (p1, _("Unknown basic type %d"), (int) basic_type); 1237 break; 1238 } 1239 1240 p1 += strlen (buffer1); 1241 1242 /* If this is a bitfield, get the bitsize. */ 1243 if (u.ti.fBitfield) 1244 { 1245 int bitsize; 1246 1247 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]); 1248 sprintf (p1, " : %d", bitsize); 1249 p1 += strlen (buffer1); 1250 } 1251 1252 /* Deal with any qualifiers. */ 1253 if (qualifiers[0].type != tqNil) 1254 { 1255 /* Snarf up any array bounds in the correct order. Arrays 1256 store 5 successive words in the aux. table: 1257 word 0 RNDXR to type of the bounds (ie, int) 1258 word 1 Current file descriptor index 1259 word 2 low bound 1260 word 3 high bound (or -1 if []) 1261 word 4 stride size in bits. */ 1262 for (i = 0; i < 7; i++) 1263 { 1264 if (qualifiers[i].type == tqArray) 1265 { 1266 qualifiers[i].low_bound = 1267 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]); 1268 qualifiers[i].high_bound = 1269 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]); 1270 qualifiers[i].stride = 1271 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]); 1272 indx += 5; 1273 } 1274 } 1275 1276 /* Now print out the qualifiers. */ 1277 for (i = 0; i < 6; i++) 1278 { 1279 switch (qualifiers[i].type) 1280 { 1281 case tqNil: 1282 case tqMax: 1283 break; 1284 1285 case tqPtr: 1286 strcpy (p2, "ptr to "); 1287 p2 += sizeof ("ptr to ")-1; 1288 break; 1289 1290 case tqVol: 1291 strcpy (p2, "volatile "); 1292 p2 += sizeof ("volatile ")-1; 1293 break; 1294 1295 case tqFar: 1296 strcpy (p2, "far "); 1297 p2 += sizeof ("far ")-1; 1298 break; 1299 1300 case tqProc: 1301 strcpy (p2, "func. ret. "); 1302 p2 += sizeof ("func. ret. "); 1303 break; 1304 1305 case tqArray: 1306 { 1307 int first_array = i; 1308 int j; 1309 1310 /* Print array bounds reversed (ie, in the order the C 1311 programmer writes them). C is such a fun language.... */ 1312 while (i < 5 && qualifiers[i+1].type == tqArray) 1313 i++; 1314 1315 for (j = i; j >= first_array; j--) 1316 { 1317 strcpy (p2, "array ["); 1318 p2 += sizeof ("array [")-1; 1319 if (qualifiers[j].low_bound != 0) 1320 sprintf (p2, 1321 "%ld:%ld {%ld bits}", 1322 (long) qualifiers[j].low_bound, 1323 (long) qualifiers[j].high_bound, 1324 (long) qualifiers[j].stride); 1325 1326 else if (qualifiers[j].high_bound != -1) 1327 sprintf (p2, 1328 "%ld {%ld bits}", 1329 (long) (qualifiers[j].high_bound + 1), 1330 (long) (qualifiers[j].stride)); 1331 1332 else 1333 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride)); 1334 1335 p2 += strlen (p2); 1336 strcpy (p2, "] of "); 1337 p2 += sizeof ("] of ")-1; 1338 } 1339 } 1340 break; 1341 } 1342 } 1343 } 1344 1345 strcpy (p2, buffer1); 1346 return buffer2; 1347 } 1348 1349 /* Return information about ECOFF symbol SYMBOL in RET. */ 1350 1351 void 1352 _bfd_ecoff_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, 1353 asymbol *symbol, 1354 symbol_info *ret) 1355 { 1356 bfd_symbol_info (symbol, ret); 1357 } 1358 1359 /* Return whether this is a local label. */ 1360 1361 bfd_boolean 1362 _bfd_ecoff_bfd_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, 1363 const char *name) 1364 { 1365 return name[0] == '$'; 1366 } 1367 1368 /* Print information about an ECOFF symbol. */ 1369 1370 void 1371 _bfd_ecoff_print_symbol (bfd *abfd, 1372 void * filep, 1373 asymbol *symbol, 1374 bfd_print_symbol_type how) 1375 { 1376 const struct ecoff_debug_swap * const debug_swap 1377 = &ecoff_backend (abfd)->debug_swap; 1378 FILE *file = (FILE *)filep; 1379 1380 switch (how) 1381 { 1382 case bfd_print_symbol_name: 1383 fprintf (file, "%s", symbol->name); 1384 break; 1385 case bfd_print_symbol_more: 1386 if (ecoffsymbol (symbol)->local) 1387 { 1388 SYMR ecoff_sym; 1389 1390 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native, 1391 &ecoff_sym); 1392 fprintf (file, "ecoff local "); 1393 fprintf_vma (file, (bfd_vma) ecoff_sym.value); 1394 fprintf (file, " %x %x", (unsigned) ecoff_sym.st, 1395 (unsigned) ecoff_sym.sc); 1396 } 1397 else 1398 { 1399 EXTR ecoff_ext; 1400 1401 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native, 1402 &ecoff_ext); 1403 fprintf (file, "ecoff extern "); 1404 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value); 1405 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st, 1406 (unsigned) ecoff_ext.asym.sc); 1407 } 1408 break; 1409 case bfd_print_symbol_all: 1410 /* Print out the symbols in a reasonable way. */ 1411 { 1412 char type; 1413 int pos; 1414 EXTR ecoff_ext; 1415 char jmptbl; 1416 char cobol_main; 1417 char weakext; 1418 1419 if (ecoffsymbol (symbol)->local) 1420 { 1421 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native, 1422 &ecoff_ext.asym); 1423 type = 'l'; 1424 pos = ((((char *) ecoffsymbol (symbol)->native 1425 - (char *) ecoff_data (abfd)->debug_info.external_sym) 1426 / debug_swap->external_sym_size) 1427 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax); 1428 jmptbl = ' '; 1429 cobol_main = ' '; 1430 weakext = ' '; 1431 } 1432 else 1433 { 1434 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native, 1435 &ecoff_ext); 1436 type = 'e'; 1437 pos = (((char *) ecoffsymbol (symbol)->native 1438 - (char *) ecoff_data (abfd)->debug_info.external_ext) 1439 / debug_swap->external_ext_size); 1440 jmptbl = ecoff_ext.jmptbl ? 'j' : ' '; 1441 cobol_main = ecoff_ext.cobol_main ? 'c' : ' '; 1442 weakext = ecoff_ext.weakext ? 'w' : ' '; 1443 } 1444 1445 fprintf (file, "[%3d] %c ", 1446 pos, type); 1447 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value); 1448 fprintf (file, " st %x sc %x indx %x %c%c%c %s", 1449 (unsigned) ecoff_ext.asym.st, 1450 (unsigned) ecoff_ext.asym.sc, 1451 (unsigned) ecoff_ext.asym.index, 1452 jmptbl, cobol_main, weakext, 1453 symbol->name); 1454 1455 if (ecoffsymbol (symbol)->fdr != NULL 1456 && ecoff_ext.asym.index != indexNil) 1457 { 1458 FDR *fdr; 1459 unsigned int indx; 1460 int bigendian; 1461 bfd_size_type sym_base; 1462 union aux_ext *aux_base; 1463 1464 fdr = ecoffsymbol (symbol)->fdr; 1465 indx = ecoff_ext.asym.index; 1466 1467 /* sym_base is used to map the fdr relative indices which 1468 appear in the file to the position number which we are 1469 using. */ 1470 sym_base = fdr->isymBase; 1471 if (ecoffsymbol (symbol)->local) 1472 sym_base += 1473 ecoff_data (abfd)->debug_info.symbolic_header.iextMax; 1474 1475 /* aux_base is the start of the aux entries for this file; 1476 asym.index is an offset from this. */ 1477 aux_base = (ecoff_data (abfd)->debug_info.external_aux 1478 + fdr->iauxBase); 1479 1480 /* The aux entries are stored in host byte order; the 1481 order is indicated by a bit in the fdr. */ 1482 bigendian = fdr->fBigendian; 1483 1484 /* This switch is basically from gcc/mips-tdump.c. */ 1485 switch (ecoff_ext.asym.st) 1486 { 1487 case stNil: 1488 case stLabel: 1489 break; 1490 1491 case stFile: 1492 case stBlock: 1493 fprintf (file, _("\n End+1 symbol: %ld"), 1494 (long) (indx + sym_base)); 1495 break; 1496 1497 case stEnd: 1498 if (ecoff_ext.asym.sc == scText 1499 || ecoff_ext.asym.sc == scInfo) 1500 fprintf (file, _("\n First symbol: %ld"), 1501 (long) (indx + sym_base)); 1502 else 1503 fprintf (file, _("\n First symbol: %ld"), 1504 ((long) 1505 (AUX_GET_ISYM (bigendian, 1506 &aux_base[ecoff_ext.asym.index]) 1507 + sym_base))); 1508 break; 1509 1510 case stProc: 1511 case stStaticProc: 1512 if (ECOFF_IS_STAB (&ecoff_ext.asym)) 1513 ; 1514 else if (ecoffsymbol (symbol)->local) 1515 fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"), 1516 ((long) 1517 (AUX_GET_ISYM (bigendian, 1518 &aux_base[ecoff_ext.asym.index]) 1519 + sym_base)), 1520 ecoff_type_to_string (abfd, fdr, indx + 1)); 1521 else 1522 fprintf (file, _("\n Local symbol: %ld"), 1523 ((long) indx 1524 + (long) sym_base 1525 + (ecoff_data (abfd) 1526 ->debug_info.symbolic_header.iextMax))); 1527 break; 1528 1529 case stStruct: 1530 fprintf (file, _("\n struct; End+1 symbol: %ld"), 1531 (long) (indx + sym_base)); 1532 break; 1533 1534 case stUnion: 1535 fprintf (file, _("\n union; End+1 symbol: %ld"), 1536 (long) (indx + sym_base)); 1537 break; 1538 1539 case stEnum: 1540 fprintf (file, _("\n enum; End+1 symbol: %ld"), 1541 (long) (indx + sym_base)); 1542 break; 1543 1544 default: 1545 if (! ECOFF_IS_STAB (&ecoff_ext.asym)) 1546 fprintf (file, _("\n Type: %s"), 1547 ecoff_type_to_string (abfd, fdr, indx)); 1548 break; 1549 } 1550 } 1551 } 1552 break; 1553 } 1554 } 1555 1556 /* Read in the relocs for a section. */ 1558 1559 static bfd_boolean 1560 ecoff_slurp_reloc_table (bfd *abfd, 1561 asection *section, 1562 asymbol **symbols) 1563 { 1564 const struct ecoff_backend_data * const backend = ecoff_backend (abfd); 1565 arelent *internal_relocs; 1566 bfd_size_type external_reloc_size; 1567 bfd_size_type amt; 1568 char *external_relocs; 1569 arelent *rptr; 1570 unsigned int i; 1571 1572 if (section->relocation != NULL 1573 || section->reloc_count == 0 1574 || (section->flags & SEC_CONSTRUCTOR) != 0) 1575 return TRUE; 1576 1577 if (! _bfd_ecoff_slurp_symbol_table (abfd)) 1578 return FALSE; 1579 1580 amt = section->reloc_count; 1581 amt *= sizeof (arelent); 1582 internal_relocs = (arelent *) bfd_alloc (abfd, amt); 1583 1584 external_reloc_size = backend->external_reloc_size; 1585 amt = external_reloc_size * section->reloc_count; 1586 external_relocs = (char *) bfd_alloc (abfd, amt); 1587 if (internal_relocs == NULL || external_relocs == NULL) 1588 return FALSE; 1589 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0) 1590 return FALSE; 1591 if (bfd_bread (external_relocs, amt, abfd) != amt) 1592 return FALSE; 1593 1594 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++) 1595 { 1596 struct internal_reloc intern; 1597 1598 (*backend->swap_reloc_in) (abfd, 1599 external_relocs + i * external_reloc_size, 1600 &intern); 1601 1602 if (intern.r_extern) 1603 { 1604 /* r_symndx is an index into the external symbols. */ 1605 BFD_ASSERT (intern.r_symndx >= 0 1606 && (intern.r_symndx 1607 < (ecoff_data (abfd) 1608 ->debug_info.symbolic_header.iextMax))); 1609 rptr->sym_ptr_ptr = symbols + intern.r_symndx; 1610 rptr->addend = 0; 1611 } 1612 else if (intern.r_symndx == RELOC_SECTION_NONE 1613 || intern.r_symndx == RELOC_SECTION_ABS) 1614 { 1615 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; 1616 rptr->addend = 0; 1617 } 1618 else 1619 { 1620 const char *sec_name; 1621 asection *sec; 1622 1623 /* r_symndx is a section key. */ 1624 switch (intern.r_symndx) 1625 { 1626 case RELOC_SECTION_TEXT: sec_name = _TEXT; break; 1627 case RELOC_SECTION_RDATA: sec_name = _RDATA; break; 1628 case RELOC_SECTION_DATA: sec_name = _DATA; break; 1629 case RELOC_SECTION_SDATA: sec_name = _SDATA; break; 1630 case RELOC_SECTION_SBSS: sec_name = _SBSS; break; 1631 case RELOC_SECTION_BSS: sec_name = _BSS; break; 1632 case RELOC_SECTION_INIT: sec_name = _INIT; break; 1633 case RELOC_SECTION_LIT8: sec_name = _LIT8; break; 1634 case RELOC_SECTION_LIT4: sec_name = _LIT4; break; 1635 case RELOC_SECTION_XDATA: sec_name = _XDATA; break; 1636 case RELOC_SECTION_PDATA: sec_name = _PDATA; break; 1637 case RELOC_SECTION_FINI: sec_name = _FINI; break; 1638 case RELOC_SECTION_LITA: sec_name = _LITA; break; 1639 case RELOC_SECTION_RCONST: sec_name = _RCONST; break; 1640 default: abort (); 1641 } 1642 1643 sec = bfd_get_section_by_name (abfd, sec_name); 1644 if (sec == NULL) 1645 abort (); 1646 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr; 1647 1648 rptr->addend = - bfd_get_section_vma (abfd, sec); 1649 } 1650 1651 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section); 1652 1653 /* Let the backend select the howto field and do any other 1654 required processing. */ 1655 (*backend->adjust_reloc_in) (abfd, &intern, rptr); 1656 } 1657 1658 bfd_release (abfd, external_relocs); 1659 1660 section->relocation = internal_relocs; 1661 1662 return TRUE; 1663 } 1664 1665 /* Get a canonical list of relocs. */ 1666 1667 long 1668 _bfd_ecoff_canonicalize_reloc (bfd *abfd, 1669 asection *section, 1670 arelent **relptr, 1671 asymbol **symbols) 1672 { 1673 unsigned int count; 1674 1675 if (section->flags & SEC_CONSTRUCTOR) 1676 { 1677 arelent_chain *chain; 1678 1679 /* This section has relocs made up by us, not the file, so take 1680 them out of their chain and place them into the data area 1681 provided. */ 1682 for (count = 0, chain = section->constructor_chain; 1683 count < section->reloc_count; 1684 count++, chain = chain->next) 1685 *relptr++ = &chain->relent; 1686 } 1687 else 1688 { 1689 arelent *tblptr; 1690 1691 if (! ecoff_slurp_reloc_table (abfd, section, symbols)) 1692 return -1; 1693 1694 tblptr = section->relocation; 1695 1696 for (count = 0; count < section->reloc_count; count++) 1697 *relptr++ = tblptr++; 1698 } 1699 1700 *relptr = NULL; 1701 1702 return section->reloc_count; 1703 } 1704 1705 /* Provided a BFD, a section and an offset into the section, calculate 1707 and return the name of the source file and the line nearest to the 1708 wanted location. */ 1709 1710 bfd_boolean 1711 _bfd_ecoff_find_nearest_line (bfd *abfd, 1712 asymbol **symbols ATTRIBUTE_UNUSED, 1713 asection *section, 1714 bfd_vma offset, 1715 const char **filename_ptr, 1716 const char **functionname_ptr, 1717 unsigned int *retline_ptr, 1718 unsigned int *discriminator_ptr) 1719 { 1720 const struct ecoff_debug_swap * const debug_swap 1721 = &ecoff_backend (abfd)->debug_swap; 1722 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info; 1723 struct ecoff_find_line *line_info; 1724 1725 /* Make sure we have the FDR's. */ 1726 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info) 1727 || bfd_get_symcount (abfd) == 0) 1728 return FALSE; 1729 1730 if (ecoff_data (abfd)->find_line_info == NULL) 1731 { 1732 bfd_size_type amt = sizeof (struct ecoff_find_line); 1733 1734 ecoff_data (abfd)->find_line_info = 1735 (struct ecoff_find_line *) bfd_zalloc (abfd, amt); 1736 if (ecoff_data (abfd)->find_line_info == NULL) 1737 return FALSE; 1738 } 1739 1740 if (discriminator_ptr) 1741 *discriminator_ptr = 0; 1742 line_info = ecoff_data (abfd)->find_line_info; 1743 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info, 1744 debug_swap, line_info, filename_ptr, 1745 functionname_ptr, retline_ptr); 1746 } 1747 1748 /* Copy private BFD data. This is called by objcopy and strip. We 1750 use it to copy the ECOFF debugging information from one BFD to the 1751 other. It would be theoretically possible to represent the ECOFF 1752 debugging information in the symbol table. However, it would be a 1753 lot of work, and there would be little gain (gas, gdb, and ld 1754 already access the ECOFF debugging information via the 1755 ecoff_debug_info structure, and that structure would have to be 1756 retained in order to support ECOFF debugging in MIPS ELF). 1757 1758 The debugging information for the ECOFF external symbols comes from 1759 the symbol table, so this function only handles the other debugging 1760 information. */ 1761 1762 bfd_boolean 1763 _bfd_ecoff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 1764 { 1765 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info; 1766 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info; 1767 int i; 1768 asymbol **sym_ptr_ptr; 1769 size_t c; 1770 bfd_boolean local; 1771 1772 /* We only want to copy information over if both BFD's use ECOFF 1773 format. */ 1774 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour 1775 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour) 1776 return TRUE; 1777 1778 /* Copy the GP value and the register masks. */ 1779 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp; 1780 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask; 1781 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask; 1782 for (i = 0; i < 3; i++) 1783 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i]; 1784 1785 /* Copy the version stamp. */ 1786 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp; 1787 1788 /* If there are no symbols, don't copy any debugging information. */ 1789 c = bfd_get_symcount (obfd); 1790 sym_ptr_ptr = bfd_get_outsymbols (obfd); 1791 if (c == 0 || sym_ptr_ptr == NULL) 1792 return TRUE; 1793 1794 /* See if there are any local symbols. */ 1795 local = FALSE; 1796 for (; c > 0; c--, sym_ptr_ptr++) 1797 { 1798 if (ecoffsymbol (*sym_ptr_ptr)->local) 1799 { 1800 local = TRUE; 1801 break; 1802 } 1803 } 1804 1805 if (local) 1806 { 1807 /* There are some local symbols. We just bring over all the 1808 debugging information. FIXME: This is not quite the right 1809 thing to do. If the user has asked us to discard all 1810 debugging information, then we are probably going to wind up 1811 keeping it because there will probably be some local symbol 1812 which objcopy did not discard. We should actually break 1813 apart the debugging information and only keep that which 1814 applies to the symbols we want to keep. */ 1815 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax; 1816 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine; 1817 oinfo->line = iinfo->line; 1818 1819 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax; 1820 oinfo->external_dnr = iinfo->external_dnr; 1821 1822 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax; 1823 oinfo->external_pdr = iinfo->external_pdr; 1824 1825 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax; 1826 oinfo->external_sym = iinfo->external_sym; 1827 1828 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax; 1829 oinfo->external_opt = iinfo->external_opt; 1830 1831 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax; 1832 oinfo->external_aux = iinfo->external_aux; 1833 1834 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax; 1835 oinfo->ss = iinfo->ss; 1836 1837 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax; 1838 oinfo->external_fdr = iinfo->external_fdr; 1839 1840 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd; 1841 oinfo->external_rfd = iinfo->external_rfd; 1842 } 1843 else 1844 { 1845 /* We are discarding all the local symbol information. Look 1846 through the external symbols and remove all references to FDR 1847 or aux information. */ 1848 c = bfd_get_symcount (obfd); 1849 sym_ptr_ptr = bfd_get_outsymbols (obfd); 1850 for (; c > 0; c--, sym_ptr_ptr++) 1851 { 1852 EXTR esym; 1853 1854 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in)) 1855 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym); 1856 esym.ifd = ifdNil; 1857 esym.asym.index = indexNil; 1858 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out)) 1859 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native); 1860 } 1861 } 1862 1863 return TRUE; 1864 } 1865 1866 /* Set the architecture. The supported architecture is stored in the 1868 backend pointer. We always set the architecture anyhow, since many 1869 callers ignore the return value. */ 1870 1871 bfd_boolean 1872 _bfd_ecoff_set_arch_mach (bfd *abfd, 1873 enum bfd_architecture arch, 1874 unsigned long machine) 1875 { 1876 bfd_default_set_arch_mach (abfd, arch, machine); 1877 return arch == ecoff_backend (abfd)->arch; 1878 } 1879 1880 /* Get the size of the section headers. */ 1881 1882 int 1883 _bfd_ecoff_sizeof_headers (bfd *abfd, 1884 struct bfd_link_info *info ATTRIBUTE_UNUSED) 1885 { 1886 asection *current; 1887 int c; 1888 int ret; 1889 1890 c = 0; 1891 for (current = abfd->sections; 1892 current != NULL; 1893 current = current->next) 1894 ++c; 1895 1896 ret = (bfd_coff_filhsz (abfd) 1897 + bfd_coff_aoutsz (abfd) 1898 + c * bfd_coff_scnhsz (abfd)); 1899 return (int) BFD_ALIGN (ret, 16); 1900 } 1901 1902 /* Get the contents of a section. */ 1903 1904 bfd_boolean 1905 _bfd_ecoff_get_section_contents (bfd *abfd, 1906 asection *section, 1907 void * location, 1908 file_ptr offset, 1909 bfd_size_type count) 1910 { 1911 return _bfd_generic_get_section_contents (abfd, section, location, 1912 offset, count); 1913 } 1914 1915 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is 1916 called via qsort. */ 1917 1918 static int 1919 ecoff_sort_hdrs (const void * arg1, const void * arg2) 1920 { 1921 const asection *hdr1 = *(const asection **) arg1; 1922 const asection *hdr2 = *(const asection **) arg2; 1923 1924 if ((hdr1->flags & SEC_ALLOC) != 0) 1925 { 1926 if ((hdr2->flags & SEC_ALLOC) == 0) 1927 return -1; 1928 } 1929 else 1930 { 1931 if ((hdr2->flags & SEC_ALLOC) != 0) 1932 return 1; 1933 } 1934 if (hdr1->vma < hdr2->vma) 1935 return -1; 1936 else if (hdr1->vma > hdr2->vma) 1937 return 1; 1938 else 1939 return 0; 1940 } 1941 1942 /* Calculate the file position for each section, and set 1943 reloc_filepos. */ 1944 1945 static bfd_boolean 1946 ecoff_compute_section_file_positions (bfd *abfd) 1947 { 1948 file_ptr sofar, file_sofar; 1949 asection **sorted_hdrs; 1950 asection *current; 1951 unsigned int i; 1952 file_ptr old_sofar; 1953 bfd_boolean rdata_in_text; 1954 bfd_boolean first_data, first_nonalloc; 1955 const bfd_vma round = ecoff_backend (abfd)->round; 1956 bfd_size_type amt; 1957 1958 sofar = _bfd_ecoff_sizeof_headers (abfd, NULL); 1959 file_sofar = sofar; 1960 1961 /* Sort the sections by VMA. */ 1962 amt = abfd->section_count; 1963 amt *= sizeof (asection *); 1964 sorted_hdrs = (asection **) bfd_malloc (amt); 1965 if (sorted_hdrs == NULL) 1966 return FALSE; 1967 for (current = abfd->sections, i = 0; 1968 current != NULL; 1969 current = current->next, i++) 1970 sorted_hdrs[i] = current; 1971 BFD_ASSERT (i == abfd->section_count); 1972 1973 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *), 1974 ecoff_sort_hdrs); 1975 1976 /* Some versions of the OSF linker put the .rdata section in the 1977 text segment, and some do not. */ 1978 rdata_in_text = ecoff_backend (abfd)->rdata_in_text; 1979 if (rdata_in_text) 1980 { 1981 for (i = 0; i < abfd->section_count; i++) 1982 { 1983 current = sorted_hdrs[i]; 1984 if (streq (current->name, _RDATA)) 1985 break; 1986 if ((current->flags & SEC_CODE) == 0 1987 && ! streq (current->name, _PDATA) 1988 && ! streq (current->name, _RCONST)) 1989 { 1990 rdata_in_text = FALSE; 1991 break; 1992 } 1993 } 1994 } 1995 ecoff_data (abfd)->rdata_in_text = rdata_in_text; 1996 1997 first_data = TRUE; 1998 first_nonalloc = TRUE; 1999 for (i = 0; i < abfd->section_count; i++) 2000 { 2001 unsigned int alignment_power; 2002 2003 current = sorted_hdrs[i]; 2004 2005 /* For the Alpha ECOFF .pdata section the lnnoptr field is 2006 supposed to indicate the number of .pdata entries that are 2007 really in the section. Each entry is 8 bytes. We store this 2008 away in line_filepos before increasing the section size. */ 2009 if (streq (current->name, _PDATA)) 2010 current->line_filepos = current->size / 8; 2011 2012 alignment_power = current->alignment_power; 2013 2014 /* On Ultrix, the data sections in an executable file must be 2015 aligned to a page boundary within the file. This does not 2016 affect the section size, though. FIXME: Does this work for 2017 other platforms? It requires some modification for the 2018 Alpha, because .rdata on the Alpha goes with the text, not 2019 the data. */ 2020 if ((abfd->flags & EXEC_P) != 0 2021 && (abfd->flags & D_PAGED) != 0 2022 && ! first_data 2023 && (current->flags & SEC_CODE) == 0 2024 && (! rdata_in_text 2025 || ! streq (current->name, _RDATA)) 2026 && ! streq (current->name, _PDATA) 2027 && ! streq (current->name, _RCONST)) 2028 { 2029 sofar = (sofar + round - 1) &~ (round - 1); 2030 file_sofar = (file_sofar + round - 1) &~ (round - 1); 2031 first_data = FALSE; 2032 } 2033 else if (streq (current->name, _LIB)) 2034 { 2035 /* On Irix 4, the location of contents of the .lib section 2036 from a shared library section is also rounded up to a 2037 page boundary. */ 2038 2039 sofar = (sofar + round - 1) &~ (round - 1); 2040 file_sofar = (file_sofar + round - 1) &~ (round - 1); 2041 } 2042 else if (first_nonalloc 2043 && (current->flags & SEC_ALLOC) == 0 2044 && (abfd->flags & D_PAGED) != 0) 2045 { 2046 /* Skip up to the next page for an unallocated section, such 2047 as the .comment section on the Alpha. This leaves room 2048 for the .bss section. */ 2049 first_nonalloc = FALSE; 2050 sofar = (sofar + round - 1) &~ (round - 1); 2051 file_sofar = (file_sofar + round - 1) &~ (round - 1); 2052 } 2053 2054 /* Align the sections in the file to the same boundary on 2055 which they are aligned in virtual memory. */ 2056 sofar = BFD_ALIGN (sofar, 1 << alignment_power); 2057 if ((current->flags & SEC_HAS_CONTENTS) != 0) 2058 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power); 2059 2060 if ((abfd->flags & D_PAGED) != 0 2061 && (current->flags & SEC_ALLOC) != 0) 2062 { 2063 sofar += (current->vma - sofar) % round; 2064 if ((current->flags & SEC_HAS_CONTENTS) != 0) 2065 file_sofar += (current->vma - file_sofar) % round; 2066 } 2067 2068 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0) 2069 current->filepos = file_sofar; 2070 2071 sofar += current->size; 2072 if ((current->flags & SEC_HAS_CONTENTS) != 0) 2073 file_sofar += current->size; 2074 2075 /* Make sure that this section is of the right size too. */ 2076 old_sofar = sofar; 2077 sofar = BFD_ALIGN (sofar, 1 << alignment_power); 2078 if ((current->flags & SEC_HAS_CONTENTS) != 0) 2079 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power); 2080 current->size += sofar - old_sofar; 2081 } 2082 2083 free (sorted_hdrs); 2084 sorted_hdrs = NULL; 2085 2086 ecoff_data (abfd)->reloc_filepos = file_sofar; 2087 2088 return TRUE; 2089 } 2090 2091 /* Determine the location of the relocs for all the sections in the 2092 output file, as well as the location of the symbolic debugging 2093 information. */ 2094 2095 static bfd_size_type 2096 ecoff_compute_reloc_file_positions (bfd *abfd) 2097 { 2098 const bfd_size_type external_reloc_size = 2099 ecoff_backend (abfd)->external_reloc_size; 2100 file_ptr reloc_base; 2101 bfd_size_type reloc_size; 2102 asection *current; 2103 file_ptr sym_base; 2104 2105 if (! abfd->output_has_begun) 2106 { 2107 if (! ecoff_compute_section_file_positions (abfd)) 2108 abort (); 2109 abfd->output_has_begun = TRUE; 2110 } 2111 2112 reloc_base = ecoff_data (abfd)->reloc_filepos; 2113 2114 reloc_size = 0; 2115 for (current = abfd->sections; 2116 current != NULL; 2117 current = current->next) 2118 { 2119 if (current->reloc_count == 0) 2120 current->rel_filepos = 0; 2121 else 2122 { 2123 bfd_size_type relsize; 2124 2125 current->rel_filepos = reloc_base; 2126 relsize = current->reloc_count * external_reloc_size; 2127 reloc_size += relsize; 2128 reloc_base += relsize; 2129 } 2130 } 2131 2132 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size; 2133 2134 /* At least on Ultrix, the symbol table of an executable file must 2135 be aligned to a page boundary. FIXME: Is this true on other 2136 platforms? */ 2137 if ((abfd->flags & EXEC_P) != 0 2138 && (abfd->flags & D_PAGED) != 0) 2139 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1) 2140 &~ (ecoff_backend (abfd)->round - 1)); 2141 2142 ecoff_data (abfd)->sym_filepos = sym_base; 2143 2144 return reloc_size; 2145 } 2146 2147 /* Set the contents of a section. */ 2148 2149 bfd_boolean 2150 _bfd_ecoff_set_section_contents (bfd *abfd, 2151 asection *section, 2152 const void * location, 2153 file_ptr offset, 2154 bfd_size_type count) 2155 { 2156 file_ptr pos; 2157 2158 /* This must be done first, because bfd_set_section_contents is 2159 going to set output_has_begun to TRUE. */ 2160 if (! abfd->output_has_begun 2161 && ! ecoff_compute_section_file_positions (abfd)) 2162 return FALSE; 2163 2164 /* Handle the .lib section specially so that Irix 4 shared libraries 2165 work out. See coff_set_section_contents in coffcode.h. */ 2166 if (streq (section->name, _LIB)) 2167 { 2168 bfd_byte *rec, *recend; 2169 2170 rec = (bfd_byte *) location; 2171 recend = rec + count; 2172 while (rec < recend) 2173 { 2174 ++section->lma; 2175 rec += bfd_get_32 (abfd, rec) * 4; 2176 } 2177 2178 BFD_ASSERT (rec == recend); 2179 } 2180 2181 if (count == 0) 2182 return TRUE; 2183 2184 pos = section->filepos + offset; 2185 if (bfd_seek (abfd, pos, SEEK_SET) != 0 2186 || bfd_bwrite (location, count, abfd) != count) 2187 return FALSE; 2188 2189 return TRUE; 2190 } 2191 2192 /* Get the GP value for an ECOFF file. This is a hook used by 2193 nlmconv. */ 2194 2195 bfd_vma 2196 bfd_ecoff_get_gp_value (bfd *abfd) 2197 { 2198 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour 2199 || bfd_get_format (abfd) != bfd_object) 2200 { 2201 bfd_set_error (bfd_error_invalid_operation); 2202 return 0; 2203 } 2204 2205 return ecoff_data (abfd)->gp; 2206 } 2207 2208 /* Set the GP value for an ECOFF file. This is a hook used by the 2209 assembler. */ 2210 2211 bfd_boolean 2212 bfd_ecoff_set_gp_value (bfd *abfd, bfd_vma gp_value) 2213 { 2214 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour 2215 || bfd_get_format (abfd) != bfd_object) 2216 { 2217 bfd_set_error (bfd_error_invalid_operation); 2218 return FALSE; 2219 } 2220 2221 ecoff_data (abfd)->gp = gp_value; 2222 2223 return TRUE; 2224 } 2225 2226 /* Set the register masks for an ECOFF file. This is a hook used by 2227 the assembler. */ 2228 2229 bfd_boolean 2230 bfd_ecoff_set_regmasks (bfd *abfd, 2231 unsigned long gprmask, 2232 unsigned long fprmask, 2233 unsigned long *cprmask) 2234 { 2235 ecoff_data_type *tdata; 2236 2237 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour 2238 || bfd_get_format (abfd) != bfd_object) 2239 { 2240 bfd_set_error (bfd_error_invalid_operation); 2241 return FALSE; 2242 } 2243 2244 tdata = ecoff_data (abfd); 2245 tdata->gprmask = gprmask; 2246 tdata->fprmask = fprmask; 2247 if (cprmask != NULL) 2248 { 2249 int i; 2250 2251 for (i = 0; i < 3; i++) 2252 tdata->cprmask[i] = cprmask[i]; 2253 } 2254 2255 return TRUE; 2256 } 2257 2258 /* Get ECOFF EXTR information for an external symbol. This function 2259 is passed to bfd_ecoff_debug_externals. */ 2260 2261 static bfd_boolean 2262 ecoff_get_extr (asymbol *sym, EXTR *esym) 2263 { 2264 ecoff_symbol_type *ecoff_sym_ptr; 2265 bfd *input_bfd; 2266 2267 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour 2268 || ecoffsymbol (sym)->native == NULL) 2269 { 2270 /* Don't include debugging, local, or section symbols. */ 2271 if ((sym->flags & BSF_DEBUGGING) != 0 2272 || (sym->flags & BSF_LOCAL) != 0 2273 || (sym->flags & BSF_SECTION_SYM) != 0) 2274 return FALSE; 2275 2276 esym->jmptbl = 0; 2277 esym->cobol_main = 0; 2278 esym->weakext = (sym->flags & BSF_WEAK) != 0; 2279 esym->reserved = 0; 2280 esym->ifd = ifdNil; 2281 /* FIXME: we can do better than this for st and sc. */ 2282 esym->asym.st = stGlobal; 2283 esym->asym.sc = scAbs; 2284 esym->asym.reserved = 0; 2285 esym->asym.index = indexNil; 2286 return TRUE; 2287 } 2288 2289 ecoff_sym_ptr = ecoffsymbol (sym); 2290 2291 if (ecoff_sym_ptr->local) 2292 return FALSE; 2293 2294 input_bfd = bfd_asymbol_bfd (sym); 2295 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in)) 2296 (input_bfd, ecoff_sym_ptr->native, esym); 2297 2298 /* If the symbol was defined by the linker, then esym will be 2299 undefined but sym will not be. Get a better class for such a 2300 symbol. */ 2301 if ((esym->asym.sc == scUndefined 2302 || esym->asym.sc == scSUndefined) 2303 && ! bfd_is_und_section (bfd_get_section (sym))) 2304 esym->asym.sc = scAbs; 2305 2306 /* Adjust the FDR index for the symbol by that used for the input 2307 BFD. */ 2308 if (esym->ifd != -1) 2309 { 2310 struct ecoff_debug_info *input_debug; 2311 2312 input_debug = &ecoff_data (input_bfd)->debug_info; 2313 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax); 2314 if (input_debug->ifdmap != NULL) 2315 esym->ifd = input_debug->ifdmap[esym->ifd]; 2316 } 2317 2318 return TRUE; 2319 } 2320 2321 /* Set the external symbol index. This routine is passed to 2322 bfd_ecoff_debug_externals. */ 2323 2324 static void 2325 ecoff_set_index (asymbol *sym, bfd_size_type indx) 2326 { 2327 ecoff_set_sym_index (sym, indx); 2328 } 2329 2330 /* Write out an ECOFF file. */ 2331 2332 bfd_boolean 2333 _bfd_ecoff_write_object_contents (bfd *abfd) 2334 { 2335 const struct ecoff_backend_data * const backend = ecoff_backend (abfd); 2336 const bfd_vma round = backend->round; 2337 const bfd_size_type filhsz = bfd_coff_filhsz (abfd); 2338 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd); 2339 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd); 2340 const bfd_size_type external_hdr_size 2341 = backend->debug_swap.external_hdr_size; 2342 const bfd_size_type external_reloc_size = backend->external_reloc_size; 2343 void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *) 2344 = backend->adjust_reloc_out; 2345 void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *) 2346 = backend->swap_reloc_out; 2347 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info; 2348 HDRR * const symhdr = &debug->symbolic_header; 2349 asection *current; 2350 unsigned int count; 2351 bfd_size_type reloc_size; 2352 bfd_size_type text_size; 2353 bfd_vma text_start; 2354 bfd_boolean set_text_start; 2355 bfd_size_type data_size; 2356 bfd_vma data_start; 2357 bfd_boolean set_data_start; 2358 bfd_size_type bss_size; 2359 void * buff = NULL; 2360 void * reloc_buff = NULL; 2361 struct internal_filehdr internal_f; 2362 struct internal_aouthdr internal_a; 2363 int i; 2364 2365 /* Determine where the sections and relocs will go in the output 2366 file. */ 2367 reloc_size = ecoff_compute_reloc_file_positions (abfd); 2368 2369 count = 1; 2370 for (current = abfd->sections; 2371 current != NULL; 2372 current = current->next) 2373 { 2374 current->target_index = count; 2375 ++count; 2376 } 2377 2378 if ((abfd->flags & D_PAGED) != 0) 2379 text_size = _bfd_ecoff_sizeof_headers (abfd, NULL); 2380 else 2381 text_size = 0; 2382 text_start = 0; 2383 set_text_start = FALSE; 2384 data_size = 0; 2385 data_start = 0; 2386 set_data_start = FALSE; 2387 bss_size = 0; 2388 2389 /* Write section headers to the file. */ 2390 2391 /* Allocate buff big enough to hold a section header, 2392 file header, or a.out header. */ 2393 { 2394 bfd_size_type siz; 2395 2396 siz = scnhsz; 2397 if (siz < filhsz) 2398 siz = filhsz; 2399 if (siz < aoutsz) 2400 siz = aoutsz; 2401 buff = bfd_malloc (siz); 2402 if (buff == NULL) 2403 goto error_return; 2404 } 2405 2406 internal_f.f_nscns = 0; 2407 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0) 2408 goto error_return; 2409 2410 for (current = abfd->sections; 2411 current != NULL; 2412 current = current->next) 2413 { 2414 struct internal_scnhdr section; 2415 bfd_vma vma; 2416 2417 ++internal_f.f_nscns; 2418 2419 strncpy (section.s_name, current->name, sizeof section.s_name); 2420 2421 /* This seems to be correct for Irix 4 shared libraries. */ 2422 vma = bfd_get_section_vma (abfd, current); 2423 if (streq (current->name, _LIB)) 2424 section.s_vaddr = 0; 2425 else 2426 section.s_vaddr = vma; 2427 2428 section.s_paddr = current->lma; 2429 section.s_size = current->size; 2430 2431 /* If this section is unloadable then the scnptr will be 0. */ 2432 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 2433 section.s_scnptr = 0; 2434 else 2435 section.s_scnptr = current->filepos; 2436 section.s_relptr = current->rel_filepos; 2437 2438 /* FIXME: the lnnoptr of the .sbss or .sdata section of an 2439 object file produced by the assembler is supposed to point to 2440 information about how much room is required by objects of 2441 various different sizes. I think this only matters if we 2442 want the linker to compute the best size to use, or 2443 something. I don't know what happens if the information is 2444 not present. */ 2445 if (! streq (current->name, _PDATA)) 2446 section.s_lnnoptr = 0; 2447 else 2448 { 2449 /* The Alpha ECOFF .pdata section uses the lnnoptr field to 2450 hold the number of entries in the section (each entry is 2451 8 bytes). We stored this in the line_filepos field in 2452 ecoff_compute_section_file_positions. */ 2453 section.s_lnnoptr = current->line_filepos; 2454 } 2455 2456 section.s_nreloc = current->reloc_count; 2457 section.s_nlnno = 0; 2458 section.s_flags = ecoff_sec_to_styp_flags (current->name, 2459 current->flags); 2460 2461 if (bfd_coff_swap_scnhdr_out (abfd, (void *) §ion, buff) == 0 2462 || bfd_bwrite (buff, scnhsz, abfd) != scnhsz) 2463 goto error_return; 2464 2465 if ((section.s_flags & STYP_TEXT) != 0 2466 || ((section.s_flags & STYP_RDATA) != 0 2467 && ecoff_data (abfd)->rdata_in_text) 2468 || section.s_flags == STYP_PDATA 2469 || (section.s_flags & STYP_DYNAMIC) != 0 2470 || (section.s_flags & STYP_LIBLIST) != 0 2471 || (section.s_flags & STYP_RELDYN) != 0 2472 || section.s_flags == STYP_CONFLIC 2473 || (section.s_flags & STYP_DYNSTR) != 0 2474 || (section.s_flags & STYP_DYNSYM) != 0 2475 || (section.s_flags & STYP_HASH) != 0 2476 || (section.s_flags & STYP_ECOFF_INIT) != 0 2477 || (section.s_flags & STYP_ECOFF_FINI) != 0 2478 || section.s_flags == STYP_RCONST) 2479 { 2480 text_size += current->size; 2481 if (! set_text_start || text_start > vma) 2482 { 2483 text_start = vma; 2484 set_text_start = TRUE; 2485 } 2486 } 2487 else if ((section.s_flags & STYP_RDATA) != 0 2488 || (section.s_flags & STYP_DATA) != 0 2489 || (section.s_flags & STYP_LITA) != 0 2490 || (section.s_flags & STYP_LIT8) != 0 2491 || (section.s_flags & STYP_LIT4) != 0 2492 || (section.s_flags & STYP_SDATA) != 0 2493 || section.s_flags == STYP_XDATA 2494 || (section.s_flags & STYP_GOT) != 0) 2495 { 2496 data_size += current->size; 2497 if (! set_data_start || data_start > vma) 2498 { 2499 data_start = vma; 2500 set_data_start = TRUE; 2501 } 2502 } 2503 else if ((section.s_flags & STYP_BSS) != 0 2504 || (section.s_flags & STYP_SBSS) != 0) 2505 bss_size += current->size; 2506 else if (section.s_flags == 0 2507 || (section.s_flags & STYP_ECOFF_LIB) != 0 2508 || section.s_flags == STYP_COMMENT) 2509 /* Do nothing. */ ; 2510 else 2511 abort (); 2512 } 2513 2514 /* Set up the file header. */ 2515 internal_f.f_magic = ecoff_get_magic (abfd); 2516 2517 /* We will NOT put a fucking timestamp in the header here. Every 2518 time you put it back, I will come in and take it out again. I'm 2519 sorry. This field does not belong here. We fill it with a 0 so 2520 it compares the same but is not a reasonable time. -- 2521 gnu (at) cygnus.com. */ 2522 internal_f.f_timdat = 0; 2523 2524 if (bfd_get_symcount (abfd) != 0) 2525 { 2526 /* The ECOFF f_nsyms field is not actually the number of 2527 symbols, it's the size of symbolic information header. */ 2528 internal_f.f_nsyms = external_hdr_size; 2529 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos; 2530 } 2531 else 2532 { 2533 internal_f.f_nsyms = 0; 2534 internal_f.f_symptr = 0; 2535 } 2536 2537 internal_f.f_opthdr = aoutsz; 2538 2539 internal_f.f_flags = F_LNNO; 2540 if (reloc_size == 0) 2541 internal_f.f_flags |= F_RELFLG; 2542 if (bfd_get_symcount (abfd) == 0) 2543 internal_f.f_flags |= F_LSYMS; 2544 if (abfd->flags & EXEC_P) 2545 internal_f.f_flags |= F_EXEC; 2546 2547 if (bfd_little_endian (abfd)) 2548 internal_f.f_flags |= F_AR32WR; 2549 else 2550 internal_f.f_flags |= F_AR32W; 2551 2552 /* Set up the ``optional'' header. */ 2553 if ((abfd->flags & D_PAGED) != 0) 2554 internal_a.magic = ECOFF_AOUT_ZMAGIC; 2555 else 2556 internal_a.magic = ECOFF_AOUT_OMAGIC; 2557 2558 /* FIXME: Is this really correct? */ 2559 internal_a.vstamp = symhdr->vstamp; 2560 2561 /* At least on Ultrix, these have to be rounded to page boundaries. 2562 FIXME: Is this true on other platforms? */ 2563 if ((abfd->flags & D_PAGED) != 0) 2564 { 2565 internal_a.tsize = (text_size + round - 1) &~ (round - 1); 2566 internal_a.text_start = text_start &~ (round - 1); 2567 internal_a.dsize = (data_size + round - 1) &~ (round - 1); 2568 internal_a.data_start = data_start &~ (round - 1); 2569 } 2570 else 2571 { 2572 internal_a.tsize = text_size; 2573 internal_a.text_start = text_start; 2574 internal_a.dsize = data_size; 2575 internal_a.data_start = data_start; 2576 } 2577 2578 /* On Ultrix, the initial portions of the .sbss and .bss segments 2579 are at the end of the data section. The bsize field in the 2580 optional header records how many bss bytes are required beyond 2581 those in the data section. The value is not rounded to a page 2582 boundary. */ 2583 if (bss_size < internal_a.dsize - data_size) 2584 bss_size = 0; 2585 else 2586 bss_size -= internal_a.dsize - data_size; 2587 internal_a.bsize = bss_size; 2588 internal_a.bss_start = internal_a.data_start + internal_a.dsize; 2589 2590 internal_a.entry = bfd_get_start_address (abfd); 2591 2592 internal_a.gp_value = ecoff_data (abfd)->gp; 2593 2594 internal_a.gprmask = ecoff_data (abfd)->gprmask; 2595 internal_a.fprmask = ecoff_data (abfd)->fprmask; 2596 for (i = 0; i < 4; i++) 2597 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i]; 2598 2599 /* Let the backend adjust the headers if necessary. */ 2600 if (backend->adjust_headers) 2601 { 2602 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a)) 2603 goto error_return; 2604 } 2605 2606 /* Write out the file header and the optional header. */ 2607 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) 2608 goto error_return; 2609 2610 bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff); 2611 if (bfd_bwrite (buff, filhsz, abfd) != filhsz) 2612 goto error_return; 2613 2614 bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff); 2615 if (bfd_bwrite (buff, aoutsz, abfd) != aoutsz) 2616 goto error_return; 2617 2618 /* Build the external symbol information. This must be done before 2619 writing out the relocs so that we know the symbol indices. We 2620 don't do this if this BFD was created by the backend linker, 2621 since it will have already handled the symbols and relocs. */ 2622 if (! ecoff_data (abfd)->linker) 2623 { 2624 symhdr->iextMax = 0; 2625 symhdr->issExtMax = 0; 2626 debug->external_ext = debug->external_ext_end = NULL; 2627 debug->ssext = debug->ssext_end = NULL; 2628 if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap, 2629 (abfd->flags & EXEC_P) == 0, 2630 ecoff_get_extr, ecoff_set_index)) 2631 goto error_return; 2632 2633 /* Write out the relocs. */ 2634 for (current = abfd->sections; 2635 current != NULL; 2636 current = current->next) 2637 { 2638 arelent **reloc_ptr_ptr; 2639 arelent **reloc_end; 2640 char *out_ptr; 2641 bfd_size_type amt; 2642 2643 if (current->reloc_count == 0) 2644 continue; 2645 2646 amt = current->reloc_count * external_reloc_size; 2647 reloc_buff = bfd_alloc (abfd, amt); 2648 if (reloc_buff == NULL) 2649 goto error_return; 2650 2651 reloc_ptr_ptr = current->orelocation; 2652 reloc_end = reloc_ptr_ptr + current->reloc_count; 2653 out_ptr = (char *) reloc_buff; 2654 2655 for (; 2656 reloc_ptr_ptr < reloc_end; 2657 reloc_ptr_ptr++, out_ptr += external_reloc_size) 2658 { 2659 arelent *reloc; 2660 asymbol *sym; 2661 struct internal_reloc in; 2662 2663 memset ((void *) &in, 0, sizeof in); 2664 2665 reloc = *reloc_ptr_ptr; 2666 sym = *reloc->sym_ptr_ptr; 2667 2668 /* If the howto field has not been initialised then skip this reloc. 2669 This assumes that an error message has been issued elsewhere. */ 2670 if (reloc->howto == NULL) 2671 continue; 2672 2673 in.r_vaddr = (reloc->address 2674 + bfd_get_section_vma (abfd, current)); 2675 in.r_type = reloc->howto->type; 2676 2677 if ((sym->flags & BSF_SECTION_SYM) == 0) 2678 { 2679 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr); 2680 in.r_extern = 1; 2681 } 2682 else 2683 { 2684 const char *name; 2685 unsigned int j; 2686 static struct 2687 { 2688 const char * name; 2689 long r_symndx; 2690 } 2691 section_symndx [] = 2692 { 2693 { _TEXT, RELOC_SECTION_TEXT }, 2694 { _RDATA, RELOC_SECTION_RDATA }, 2695 { _DATA, RELOC_SECTION_DATA }, 2696 { _SDATA, RELOC_SECTION_SDATA }, 2697 { _SBSS, RELOC_SECTION_SBSS }, 2698 { _BSS, RELOC_SECTION_BSS }, 2699 { _INIT, RELOC_SECTION_INIT }, 2700 { _LIT8, RELOC_SECTION_LIT8 }, 2701 { _LIT4, RELOC_SECTION_LIT4 }, 2702 { _XDATA, RELOC_SECTION_XDATA }, 2703 { _PDATA, RELOC_SECTION_PDATA }, 2704 { _FINI, RELOC_SECTION_FINI }, 2705 { _LITA, RELOC_SECTION_LITA }, 2706 { "*ABS*", RELOC_SECTION_ABS }, 2707 { _RCONST, RELOC_SECTION_RCONST } 2708 }; 2709 2710 name = bfd_get_section_name (abfd, bfd_get_section (sym)); 2711 2712 for (j = 0; j < ARRAY_SIZE (section_symndx); j++) 2713 if (streq (name, section_symndx[j].name)) 2714 { 2715 in.r_symndx = section_symndx[j].r_symndx; 2716 break; 2717 } 2718 2719 if (j == ARRAY_SIZE (section_symndx)) 2720 abort (); 2721 in.r_extern = 0; 2722 } 2723 2724 (*adjust_reloc_out) (abfd, reloc, &in); 2725 2726 (*swap_reloc_out) (abfd, &in, (void *) out_ptr); 2727 } 2728 2729 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0) 2730 goto error_return; 2731 amt = current->reloc_count * external_reloc_size; 2732 if (bfd_bwrite (reloc_buff, amt, abfd) != amt) 2733 goto error_return; 2734 bfd_release (abfd, reloc_buff); 2735 reloc_buff = NULL; 2736 } 2737 2738 /* Write out the symbolic debugging information. */ 2739 if (bfd_get_symcount (abfd) > 0) 2740 { 2741 /* Write out the debugging information. */ 2742 if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap, 2743 ecoff_data (abfd)->sym_filepos)) 2744 goto error_return; 2745 } 2746 } 2747 2748 /* The .bss section of a demand paged executable must receive an 2749 entire page. If there are symbols, the symbols will start on the 2750 next page. If there are no symbols, we must fill out the page by 2751 hand. */ 2752 if (bfd_get_symcount (abfd) == 0 2753 && (abfd->flags & EXEC_P) != 0 2754 && (abfd->flags & D_PAGED) != 0) 2755 { 2756 char c; 2757 2758 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1, 2759 SEEK_SET) != 0) 2760 goto error_return; 2761 if (bfd_bread (&c, (bfd_size_type) 1, abfd) == 0) 2762 c = 0; 2763 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1, 2764 SEEK_SET) != 0) 2765 goto error_return; 2766 if (bfd_bwrite (&c, (bfd_size_type) 1, abfd) != 1) 2767 goto error_return; 2768 } 2769 2770 if (reloc_buff != NULL) 2771 bfd_release (abfd, reloc_buff); 2772 if (buff != NULL) 2773 free (buff); 2774 return TRUE; 2775 error_return: 2776 if (reloc_buff != NULL) 2777 bfd_release (abfd, reloc_buff); 2778 if (buff != NULL) 2779 free (buff); 2780 return FALSE; 2781 } 2782 2783 /* Archive handling. ECOFF uses what appears to be a unique type of 2785 archive header (armap). The byte ordering of the armap and the 2786 contents are encoded in the name of the armap itself. At least for 2787 now, we only support archives with the same byte ordering in the 2788 armap and the contents. 2789 2790 The first four bytes in the armap are the number of symbol 2791 definitions. This is always a power of two. 2792 2793 This is followed by the symbol definitions. Each symbol definition 2794 occupies 8 bytes. The first four bytes are the offset from the 2795 start of the armap strings to the null-terminated string naming 2796 this symbol. The second four bytes are the file offset to the 2797 archive member which defines this symbol. If the second four bytes 2798 are 0, then this is not actually a symbol definition, and it should 2799 be ignored. 2800 2801 The symbols are hashed into the armap with a closed hashing scheme. 2802 See the functions below for the details of the algorithm. 2803 2804 After the symbol definitions comes four bytes holding the size of 2805 the string table, followed by the string table itself. */ 2806 2807 /* The name of an archive headers looks like this: 2808 __________E[BL]E[BL]_ (with a trailing space). 2809 The trailing space is changed to an X if the archive is changed to 2810 indicate that the armap is out of date. 2811 2812 The Alpha seems to use ________64E[BL]E[BL]_. */ 2813 2814 #define ARMAP_BIG_ENDIAN 'B' 2815 #define ARMAP_LITTLE_ENDIAN 'L' 2816 #define ARMAP_MARKER 'E' 2817 #define ARMAP_START_LENGTH 10 2818 #define ARMAP_HEADER_MARKER_INDEX 10 2819 #define ARMAP_HEADER_ENDIAN_INDEX 11 2820 #define ARMAP_OBJECT_MARKER_INDEX 12 2821 #define ARMAP_OBJECT_ENDIAN_INDEX 13 2822 #define ARMAP_END_INDEX 14 2823 #define ARMAP_END "_ " 2824 2825 /* This is a magic number used in the hashing algorithm. */ 2826 #define ARMAP_HASH_MAGIC 0x9dd68ab5 2827 2828 /* This returns the hash value to use for a string. It also sets 2829 *REHASH to the rehash adjustment if the first slot is taken. SIZE 2830 is the number of entries in the hash table, and HLOG is the log 2831 base 2 of SIZE. */ 2832 2833 static unsigned int 2834 ecoff_armap_hash (const char *s, 2835 unsigned int *rehash, 2836 unsigned int size, 2837 unsigned int hlog) 2838 { 2839 unsigned int hash; 2840 2841 if (hlog == 0) 2842 return 0; 2843 hash = *s++; 2844 while (*s != '\0') 2845 hash = ((hash >> 27) | (hash << 5)) + *s++; 2846 hash *= ARMAP_HASH_MAGIC; 2847 *rehash = (hash & (size - 1)) | 1; 2848 return hash >> (32 - hlog); 2849 } 2850 2851 /* Read in the armap. */ 2852 2853 bfd_boolean 2854 _bfd_ecoff_slurp_armap (bfd *abfd) 2855 { 2856 char nextname[17]; 2857 unsigned int i; 2858 struct areltdata *mapdata; 2859 bfd_size_type parsed_size; 2860 char *raw_armap; 2861 struct artdata *ardata; 2862 unsigned int count; 2863 char *raw_ptr; 2864 carsym *symdef_ptr; 2865 char *stringbase; 2866 bfd_size_type amt; 2867 2868 /* Get the name of the first element. */ 2869 i = bfd_bread ((void *) nextname, (bfd_size_type) 16, abfd); 2870 if (i == 0) 2871 return TRUE; 2872 if (i != 16) 2873 return FALSE; 2874 2875 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0) 2876 return FALSE; 2877 2878 /* Irix 4.0.5F apparently can use either an ECOFF armap or a 2879 standard COFF armap. We could move the ECOFF armap stuff into 2880 bfd_slurp_armap, but that seems inappropriate since no other 2881 target uses this format. Instead, we check directly for a COFF 2882 armap. */ 2883 if (CONST_STRNEQ (nextname, "/ ")) 2884 return bfd_slurp_armap (abfd); 2885 2886 /* See if the first element is an armap. */ 2887 if (! strneq (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH) 2888 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER 2889 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN 2890 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN) 2891 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER 2892 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN 2893 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN) 2894 || ! strneq (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1)) 2895 { 2896 bfd_has_map (abfd) = FALSE; 2897 return TRUE; 2898 } 2899 2900 /* Make sure we have the right byte ordering. */ 2901 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN) 2902 ^ (bfd_header_big_endian (abfd))) 2903 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN) 2904 ^ (bfd_big_endian (abfd)))) 2905 { 2906 bfd_set_error (bfd_error_wrong_format); 2907 return FALSE; 2908 } 2909 2910 /* Read in the armap. */ 2911 ardata = bfd_ardata (abfd); 2912 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd); 2913 if (mapdata == NULL) 2914 return FALSE; 2915 parsed_size = mapdata->parsed_size; 2916 free (mapdata); 2917 2918 raw_armap = (char *) bfd_alloc (abfd, parsed_size); 2919 if (raw_armap == NULL) 2920 return FALSE; 2921 2922 if (bfd_bread ((void *) raw_armap, parsed_size, abfd) != parsed_size) 2923 { 2924 if (bfd_get_error () != bfd_error_system_call) 2925 bfd_set_error (bfd_error_malformed_archive); 2926 bfd_release (abfd, (void *) raw_armap); 2927 return FALSE; 2928 } 2929 2930 ardata->tdata = (void *) raw_armap; 2931 2932 count = H_GET_32 (abfd, raw_armap); 2933 2934 ardata->symdef_count = 0; 2935 ardata->cache = NULL; 2936 2937 /* This code used to overlay the symdefs over the raw archive data, 2938 but that doesn't work on a 64 bit host. */ 2939 stringbase = raw_armap + count * 8 + 8; 2940 2941 #ifdef CHECK_ARMAP_HASH 2942 { 2943 unsigned int hlog; 2944 2945 /* Double check that I have the hashing algorithm right by making 2946 sure that every symbol can be looked up successfully. */ 2947 hlog = 0; 2948 for (i = 1; i < count; i <<= 1) 2949 hlog++; 2950 BFD_ASSERT (i == count); 2951 2952 raw_ptr = raw_armap + 4; 2953 for (i = 0; i < count; i++, raw_ptr += 8) 2954 { 2955 unsigned int name_offset, file_offset; 2956 unsigned int hash, rehash, srch; 2957 2958 name_offset = H_GET_32 (abfd, raw_ptr); 2959 file_offset = H_GET_32 (abfd, (raw_ptr + 4)); 2960 if (file_offset == 0) 2961 continue; 2962 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count, 2963 hlog); 2964 if (hash == i) 2965 continue; 2966 2967 /* See if we can rehash to this location. */ 2968 for (srch = (hash + rehash) & (count - 1); 2969 srch != hash && srch != i; 2970 srch = (srch + rehash) & (count - 1)) 2971 BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0); 2972 BFD_ASSERT (srch == i); 2973 } 2974 } 2975 2976 #endif /* CHECK_ARMAP_HASH */ 2977 2978 raw_ptr = raw_armap + 4; 2979 for (i = 0; i < count; i++, raw_ptr += 8) 2980 if (H_GET_32 (abfd, (raw_ptr + 4)) != 0) 2981 ++ardata->symdef_count; 2982 2983 amt = ardata->symdef_count; 2984 amt *= sizeof (carsym); 2985 symdef_ptr = (carsym *) bfd_alloc (abfd, amt); 2986 if (!symdef_ptr) 2987 return FALSE; 2988 2989 ardata->symdefs = symdef_ptr; 2990 2991 raw_ptr = raw_armap + 4; 2992 for (i = 0; i < count; i++, raw_ptr += 8) 2993 { 2994 unsigned int name_offset, file_offset; 2995 2996 file_offset = H_GET_32 (abfd, (raw_ptr + 4)); 2997 if (file_offset == 0) 2998 continue; 2999 name_offset = H_GET_32 (abfd, raw_ptr); 3000 symdef_ptr->name = stringbase + name_offset; 3001 symdef_ptr->file_offset = file_offset; 3002 ++symdef_ptr; 3003 } 3004 3005 ardata->first_file_filepos = bfd_tell (abfd); 3006 /* Pad to an even boundary. */ 3007 ardata->first_file_filepos += ardata->first_file_filepos % 2; 3008 3009 bfd_has_map (abfd) = TRUE; 3010 3011 return TRUE; 3012 } 3013 3014 /* Write out an armap. */ 3015 3016 bfd_boolean 3017 _bfd_ecoff_write_armap (bfd *abfd, 3018 unsigned int elength, 3019 struct orl *map, 3020 unsigned int orl_count, 3021 int stridx) 3022 { 3023 unsigned int hashsize, hashlog; 3024 bfd_size_type symdefsize; 3025 int padit; 3026 unsigned int stringsize; 3027 unsigned int mapsize; 3028 file_ptr firstreal; 3029 struct ar_hdr hdr; 3030 struct stat statbuf; 3031 unsigned int i; 3032 bfd_byte temp[4]; 3033 bfd_byte *hashtable; 3034 bfd *current; 3035 bfd *last_elt; 3036 3037 /* Ultrix appears to use as a hash table size the least power of two 3038 greater than twice the number of entries. */ 3039 for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++) 3040 ; 3041 hashsize = 1 << hashlog; 3042 3043 symdefsize = hashsize * 8; 3044 padit = stridx % 2; 3045 stringsize = stridx + padit; 3046 3047 /* Include 8 bytes to store symdefsize and stringsize in output. */ 3048 mapsize = symdefsize + stringsize + 8; 3049 3050 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength; 3051 3052 memset ((void *) &hdr, 0, sizeof hdr); 3053 3054 /* Work out the ECOFF armap name. */ 3055 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start); 3056 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER; 3057 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] = 3058 (bfd_header_big_endian (abfd) 3059 ? ARMAP_BIG_ENDIAN 3060 : ARMAP_LITTLE_ENDIAN); 3061 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER; 3062 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] = 3063 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN; 3064 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1); 3065 3066 /* Write the timestamp of the archive header to be just a little bit 3067 later than the timestamp of the file, otherwise the linker will 3068 complain that the index is out of date. Actually, the Ultrix 3069 linker just checks the archive name; the GNU linker may check the 3070 date. */ 3071 stat (abfd->filename, &statbuf); 3072 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld", 3073 (long) (statbuf.st_mtime + 60)); 3074 3075 /* The DECstation uses zeroes for the uid, gid and mode of the 3076 armap. */ 3077 hdr.ar_uid[0] = '0'; 3078 hdr.ar_gid[0] = '0'; 3079 /* Building gcc ends up extracting the armap as a file - twice. */ 3080 hdr.ar_mode[0] = '6'; 3081 hdr.ar_mode[1] = '4'; 3082 hdr.ar_mode[2] = '4'; 3083 3084 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize); 3085 3086 hdr.ar_fmag[0] = '`'; 3087 hdr.ar_fmag[1] = '\012'; 3088 3089 /* Turn all null bytes in the header into spaces. */ 3090 for (i = 0; i < sizeof (struct ar_hdr); i++) 3091 if (((char *) (&hdr))[i] == '\0') 3092 (((char *) (&hdr))[i]) = ' '; 3093 3094 if (bfd_bwrite ((void *) &hdr, (bfd_size_type) sizeof (struct ar_hdr), abfd) 3095 != sizeof (struct ar_hdr)) 3096 return FALSE; 3097 3098 H_PUT_32 (abfd, hashsize, temp); 3099 if (bfd_bwrite ((void *) temp, (bfd_size_type) 4, abfd) != 4) 3100 return FALSE; 3101 3102 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize); 3103 if (!hashtable) 3104 return FALSE; 3105 3106 current = abfd->archive_head; 3107 last_elt = current; 3108 for (i = 0; i < orl_count; i++) 3109 { 3110 unsigned int hash, rehash = 0; 3111 3112 /* Advance firstreal to the file position of this archive 3113 element. */ 3114 if (map[i].u.abfd != last_elt) 3115 { 3116 do 3117 { 3118 firstreal += arelt_size (current) + sizeof (struct ar_hdr); 3119 firstreal += firstreal % 2; 3120 current = current->archive_next; 3121 } 3122 while (current != map[i].u.abfd); 3123 } 3124 3125 last_elt = current; 3126 3127 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog); 3128 if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0) 3129 { 3130 unsigned int srch; 3131 3132 /* The desired slot is already taken. */ 3133 for (srch = (hash + rehash) & (hashsize - 1); 3134 srch != hash; 3135 srch = (srch + rehash) & (hashsize - 1)) 3136 if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0) 3137 break; 3138 3139 BFD_ASSERT (srch != hash); 3140 3141 hash = srch; 3142 } 3143 3144 H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8)); 3145 H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4)); 3146 } 3147 3148 if (bfd_bwrite ((void *) hashtable, symdefsize, abfd) != symdefsize) 3149 return FALSE; 3150 3151 bfd_release (abfd, hashtable); 3152 3153 /* Now write the strings. */ 3154 H_PUT_32 (abfd, stringsize, temp); 3155 if (bfd_bwrite ((void *) temp, (bfd_size_type) 4, abfd) != 4) 3156 return FALSE; 3157 for (i = 0; i < orl_count; i++) 3158 { 3159 bfd_size_type len; 3160 3161 len = strlen (*map[i].name) + 1; 3162 if (bfd_bwrite ((void *) (*map[i].name), len, abfd) != len) 3163 return FALSE; 3164 } 3165 3166 /* The spec sez this should be a newline. But in order to be 3167 bug-compatible for DECstation ar we use a null. */ 3168 if (padit) 3169 { 3170 if (bfd_bwrite ("", (bfd_size_type) 1, abfd) != 1) 3171 return FALSE; 3172 } 3173 3174 return TRUE; 3175 } 3176 3177 /* ECOFF linker code. */ 3179 3180 /* Routine to create an entry in an ECOFF link hash table. */ 3181 3182 static struct bfd_hash_entry * 3183 ecoff_link_hash_newfunc (struct bfd_hash_entry *entry, 3184 struct bfd_hash_table *table, 3185 const char *string) 3186 { 3187 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry; 3188 3189 /* Allocate the structure if it has not already been allocated by a 3190 subclass. */ 3191 if (ret == NULL) 3192 ret = ((struct ecoff_link_hash_entry *) 3193 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry))); 3194 if (ret == NULL) 3195 return NULL; 3196 3197 /* Call the allocation method of the superclass. */ 3198 ret = ((struct ecoff_link_hash_entry *) 3199 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, 3200 table, string)); 3201 3202 if (ret) 3203 { 3204 /* Set local fields. */ 3205 ret->indx = -1; 3206 ret->abfd = NULL; 3207 ret->written = 0; 3208 ret->small = 0; 3209 } 3210 memset ((void *) &ret->esym, 0, sizeof ret->esym); 3211 3212 return (struct bfd_hash_entry *) ret; 3213 } 3214 3215 /* Create an ECOFF link hash table. */ 3216 3217 struct bfd_link_hash_table * 3218 _bfd_ecoff_bfd_link_hash_table_create (bfd *abfd) 3219 { 3220 struct ecoff_link_hash_table *ret; 3221 bfd_size_type amt = sizeof (struct ecoff_link_hash_table); 3222 3223 ret = (struct ecoff_link_hash_table *) bfd_malloc (amt); 3224 if (ret == NULL) 3225 return NULL; 3226 if (!_bfd_link_hash_table_init (&ret->root, abfd, 3227 ecoff_link_hash_newfunc, 3228 sizeof (struct ecoff_link_hash_entry))) 3229 { 3230 free (ret); 3231 return NULL; 3232 } 3233 return &ret->root; 3234 } 3235 3236 /* Look up an entry in an ECOFF link hash table. */ 3237 3238 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \ 3239 ((struct ecoff_link_hash_entry *) \ 3240 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow))) 3241 3242 /* Get the ECOFF link hash table from the info structure. This is 3243 just a cast. */ 3244 3245 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash)) 3246 3247 /* Add the external symbols of an object file to the global linker 3248 hash table. The external symbols and strings we are passed are 3249 just allocated on the stack, and will be discarded. We must 3250 explicitly save any information we may need later on in the link. 3251 We do not want to read the external symbol information again. */ 3252 3253 static bfd_boolean 3254 ecoff_link_add_externals (bfd *abfd, 3255 struct bfd_link_info *info, 3256 void * external_ext, 3257 char *ssext) 3258 { 3259 const struct ecoff_backend_data * const backend = ecoff_backend (abfd); 3260 void (* const swap_ext_in) (bfd *, void *, EXTR *) 3261 = backend->debug_swap.swap_ext_in; 3262 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size; 3263 unsigned long ext_count; 3264 struct bfd_link_hash_entry **sym_hash; 3265 char *ext_ptr; 3266 char *ext_end; 3267 bfd_size_type amt; 3268 3269 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax; 3270 3271 amt = ext_count; 3272 amt *= sizeof (struct bfd_link_hash_entry *); 3273 sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt); 3274 if (!sym_hash) 3275 return FALSE; 3276 ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash; 3277 3278 ext_ptr = (char *) external_ext; 3279 ext_end = ext_ptr + ext_count * external_ext_size; 3280 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++) 3281 { 3282 EXTR esym; 3283 bfd_boolean skip; 3284 bfd_vma value; 3285 asection *section; 3286 const char *name; 3287 struct ecoff_link_hash_entry *h; 3288 3289 *sym_hash = NULL; 3290 3291 (*swap_ext_in) (abfd, (void *) ext_ptr, &esym); 3292 3293 /* Skip debugging symbols. */ 3294 skip = FALSE; 3295 switch (esym.asym.st) 3296 { 3297 case stGlobal: 3298 case stStatic: 3299 case stLabel: 3300 case stProc: 3301 case stStaticProc: 3302 break; 3303 default: 3304 skip = TRUE; 3305 break; 3306 } 3307 3308 if (skip) 3309 continue; 3310 3311 /* Get the information for this symbol. */ 3312 value = esym.asym.value; 3313 switch (esym.asym.sc) 3314 { 3315 default: 3316 case scNil: 3317 case scRegister: 3318 case scCdbLocal: 3319 case scBits: 3320 case scCdbSystem: 3321 case scRegImage: 3322 case scInfo: 3323 case scUserStruct: 3324 case scVar: 3325 case scVarRegister: 3326 case scVariant: 3327 case scBasedVar: 3328 case scXData: 3329 case scPData: 3330 section = NULL; 3331 break; 3332 case scText: 3333 section = bfd_make_section_old_way (abfd, _TEXT); 3334 value -= section->vma; 3335 break; 3336 case scData: 3337 section = bfd_make_section_old_way (abfd, _DATA); 3338 value -= section->vma; 3339 break; 3340 case scBss: 3341 section = bfd_make_section_old_way (abfd, _BSS); 3342 value -= section->vma; 3343 break; 3344 case scAbs: 3345 section = bfd_abs_section_ptr; 3346 break; 3347 case scUndefined: 3348 section = bfd_und_section_ptr; 3349 break; 3350 case scSData: 3351 section = bfd_make_section_old_way (abfd, _SDATA); 3352 value -= section->vma; 3353 break; 3354 case scSBss: 3355 section = bfd_make_section_old_way (abfd, _SBSS); 3356 value -= section->vma; 3357 break; 3358 case scRData: 3359 section = bfd_make_section_old_way (abfd, _RDATA); 3360 value -= section->vma; 3361 break; 3362 case scCommon: 3363 if (value > ecoff_data (abfd)->gp_size) 3364 { 3365 section = bfd_com_section_ptr; 3366 break; 3367 } 3368 /* Fall through. */ 3369 case scSCommon: 3370 if (ecoff_scom_section.name == NULL) 3371 { 3372 /* Initialize the small common section. */ 3373 ecoff_scom_section.name = SCOMMON; 3374 ecoff_scom_section.flags = SEC_IS_COMMON; 3375 ecoff_scom_section.output_section = &ecoff_scom_section; 3376 ecoff_scom_section.symbol = &ecoff_scom_symbol; 3377 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr; 3378 ecoff_scom_symbol.name = SCOMMON; 3379 ecoff_scom_symbol.flags = BSF_SECTION_SYM; 3380 ecoff_scom_symbol.section = &ecoff_scom_section; 3381 ecoff_scom_symbol_ptr = &ecoff_scom_symbol; 3382 } 3383 section = &ecoff_scom_section; 3384 break; 3385 case scSUndefined: 3386 section = bfd_und_section_ptr; 3387 break; 3388 case scInit: 3389 section = bfd_make_section_old_way (abfd, _INIT); 3390 value -= section->vma; 3391 break; 3392 case scFini: 3393 section = bfd_make_section_old_way (abfd, _FINI); 3394 value -= section->vma; 3395 break; 3396 case scRConst: 3397 section = bfd_make_section_old_way (abfd, _RCONST); 3398 value -= section->vma; 3399 break; 3400 } 3401 3402 if (section == NULL) 3403 continue; 3404 3405 name = ssext + esym.asym.iss; 3406 3407 if (! (_bfd_generic_link_add_one_symbol 3408 (info, abfd, name, 3409 (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL), 3410 section, value, NULL, TRUE, TRUE, sym_hash))) 3411 return FALSE; 3412 3413 h = (struct ecoff_link_hash_entry *) *sym_hash; 3414 3415 /* If we are building an ECOFF hash table, save the external 3416 symbol information. */ 3417 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)) 3418 { 3419 if (h->abfd == NULL 3420 || (! bfd_is_und_section (section) 3421 && (! bfd_is_com_section (section) 3422 || (h->root.type != bfd_link_hash_defined 3423 && h->root.type != bfd_link_hash_defweak)))) 3424 { 3425 h->abfd = abfd; 3426 h->esym = esym; 3427 } 3428 3429 /* Remember whether this symbol was small undefined. */ 3430 if (esym.asym.sc == scSUndefined) 3431 h->small = 1; 3432 3433 /* If this symbol was ever small undefined, it needs to wind 3434 up in a GP relative section. We can't control the 3435 section of a defined symbol, but we can control the 3436 section of a common symbol. This case is actually needed 3437 on Ultrix 4.2 to handle the symbol cred in -lckrb. */ 3438 if (h->small 3439 && h->root.type == bfd_link_hash_common 3440 && streq (h->root.u.c.p->section->name, SCOMMON)) 3441 { 3442 h->root.u.c.p->section = bfd_make_section_old_way (abfd, 3443 SCOMMON); 3444 h->root.u.c.p->section->flags = SEC_ALLOC; 3445 if (h->esym.asym.sc == scCommon) 3446 h->esym.asym.sc = scSCommon; 3447 } 3448 } 3449 } 3450 3451 return TRUE; 3452 } 3453 3454 /* Add symbols from an ECOFF object file to the global linker hash 3455 table. */ 3456 3457 static bfd_boolean 3458 ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 3459 { 3460 HDRR *symhdr; 3461 bfd_size_type external_ext_size; 3462 void * external_ext = NULL; 3463 bfd_size_type esize; 3464 char *ssext = NULL; 3465 bfd_boolean result; 3466 3467 if (! ecoff_slurp_symbolic_header (abfd)) 3468 return FALSE; 3469 3470 /* If there are no symbols, we don't want it. */ 3471 if (bfd_get_symcount (abfd) == 0) 3472 return TRUE; 3473 3474 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header; 3475 3476 /* Read in the external symbols and external strings. */ 3477 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size; 3478 esize = symhdr->iextMax * external_ext_size; 3479 external_ext = bfd_malloc (esize); 3480 if (external_ext == NULL && esize != 0) 3481 goto error_return; 3482 3483 if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0 3484 || bfd_bread (external_ext, esize, abfd) != esize) 3485 goto error_return; 3486 3487 ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax); 3488 if (ssext == NULL && symhdr->issExtMax != 0) 3489 goto error_return; 3490 3491 if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0 3492 || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd) 3493 != (bfd_size_type) symhdr->issExtMax)) 3494 goto error_return; 3495 3496 result = ecoff_link_add_externals (abfd, info, external_ext, ssext); 3497 3498 if (ssext != NULL) 3499 free (ssext); 3500 if (external_ext != NULL) 3501 free (external_ext); 3502 return result; 3503 3504 error_return: 3505 if (ssext != NULL) 3506 free (ssext); 3507 if (external_ext != NULL) 3508 free (external_ext); 3509 return FALSE; 3510 } 3511 3512 /* This is called if we used _bfd_generic_link_add_archive_symbols 3513 because we were not dealing with an ECOFF archive. */ 3514 3515 static bfd_boolean 3516 ecoff_link_check_archive_element (bfd *abfd, 3517 struct bfd_link_info *info, 3518 struct bfd_link_hash_entry *h, 3519 const char *name, 3520 bfd_boolean *pneeded) 3521 { 3522 *pneeded = FALSE; 3523 3524 /* Unlike the generic linker, we do not pull in elements because 3525 of common symbols. */ 3526 if (h->type != bfd_link_hash_undefined) 3527 return TRUE; 3528 3529 /* Include this element. */ 3530 if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd)) 3531 return FALSE; 3532 *pneeded = TRUE; 3533 3534 return ecoff_link_add_object_symbols (abfd, info); 3535 } 3536 3537 /* Add the symbols from an archive file to the global hash table. 3538 This looks through the undefined symbols, looks each one up in the 3539 archive hash table, and adds any associated object file. We do not 3540 use _bfd_generic_link_add_archive_symbols because ECOFF archives 3541 already have a hash table, so there is no reason to construct 3542 another one. */ 3543 3544 static bfd_boolean 3545 ecoff_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) 3546 { 3547 const struct ecoff_backend_data * const backend = ecoff_backend (abfd); 3548 const bfd_byte *raw_armap; 3549 struct bfd_link_hash_entry **pundef; 3550 unsigned int armap_count; 3551 unsigned int armap_log; 3552 unsigned int i; 3553 const bfd_byte *hashtable; 3554 const char *stringbase; 3555 3556 if (! bfd_has_map (abfd)) 3557 { 3558 /* An empty archive is a special case. */ 3559 if (bfd_openr_next_archived_file (abfd, NULL) == NULL) 3560 return TRUE; 3561 bfd_set_error (bfd_error_no_armap); 3562 return FALSE; 3563 } 3564 3565 /* If we don't have any raw data for this archive, as can happen on 3566 Irix 4.0.5F, we call the generic routine. 3567 FIXME: We should be more clever about this, since someday tdata 3568 may get to something for a generic archive. */ 3569 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata; 3570 if (raw_armap == NULL) 3571 return (_bfd_generic_link_add_archive_symbols 3572 (abfd, info, ecoff_link_check_archive_element)); 3573 3574 armap_count = H_GET_32 (abfd, raw_armap); 3575 3576 armap_log = 0; 3577 for (i = 1; i < armap_count; i <<= 1) 3578 armap_log++; 3579 BFD_ASSERT (i == armap_count); 3580 3581 hashtable = raw_armap + 4; 3582 stringbase = (const char *) raw_armap + armap_count * 8 + 8; 3583 3584 /* Look through the list of undefined symbols. */ 3585 pundef = &info->hash->undefs; 3586 while (*pundef != NULL) 3587 { 3588 struct bfd_link_hash_entry *h; 3589 unsigned int hash, rehash = 0; 3590 unsigned int file_offset; 3591 const char *name; 3592 bfd *element; 3593 3594 h = *pundef; 3595 3596 /* When a symbol is defined, it is not necessarily removed from 3597 the list. */ 3598 if (h->type != bfd_link_hash_undefined 3599 && h->type != bfd_link_hash_common) 3600 { 3601 /* Remove this entry from the list, for general cleanliness 3602 and because we are going to look through the list again 3603 if we search any more libraries. We can't remove the 3604 entry if it is the tail, because that would lose any 3605 entries we add to the list later on. */ 3606 if (*pundef != info->hash->undefs_tail) 3607 *pundef = (*pundef)->u.undef.next; 3608 else 3609 pundef = &(*pundef)->u.undef.next; 3610 continue; 3611 } 3612 3613 /* Native ECOFF linkers do not pull in archive elements merely 3614 to satisfy common definitions, so neither do we. We leave 3615 them on the list, though, in case we are linking against some 3616 other object format. */ 3617 if (h->type != bfd_link_hash_undefined) 3618 { 3619 pundef = &(*pundef)->u.undef.next; 3620 continue; 3621 } 3622 3623 /* Look for this symbol in the archive hash table. */ 3624 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count, 3625 armap_log); 3626 3627 file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4); 3628 if (file_offset == 0) 3629 { 3630 /* Nothing in this slot. */ 3631 pundef = &(*pundef)->u.undef.next; 3632 continue; 3633 } 3634 3635 name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8)); 3636 if (name[0] != h->root.string[0] 3637 || ! streq (name, h->root.string)) 3638 { 3639 unsigned int srch; 3640 bfd_boolean found; 3641 3642 /* That was the wrong symbol. Try rehashing. */ 3643 found = FALSE; 3644 for (srch = (hash + rehash) & (armap_count - 1); 3645 srch != hash; 3646 srch = (srch + rehash) & (armap_count - 1)) 3647 { 3648 file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4); 3649 if (file_offset == 0) 3650 break; 3651 name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8)); 3652 if (name[0] == h->root.string[0] 3653 && streq (name, h->root.string)) 3654 { 3655 found = TRUE; 3656 break; 3657 } 3658 } 3659 3660 if (! found) 3661 { 3662 pundef = &(*pundef)->u.undef.next; 3663 continue; 3664 } 3665 3666 hash = srch; 3667 } 3668 3669 element = (*backend->get_elt_at_filepos) (abfd, (file_ptr) file_offset); 3670 if (element == NULL) 3671 return FALSE; 3672 3673 if (! bfd_check_format (element, bfd_object)) 3674 return FALSE; 3675 3676 /* Unlike the generic linker, we know that this element provides 3677 a definition for an undefined symbol and we know that we want 3678 to include it. We don't need to check anything. */ 3679 if (!(*info->callbacks 3680 ->add_archive_element) (info, element, name, &element)) 3681 return FALSE; 3682 if (! ecoff_link_add_object_symbols (element, info)) 3683 return FALSE; 3684 3685 pundef = &(*pundef)->u.undef.next; 3686 } 3687 3688 return TRUE; 3689 } 3690 3691 /* Given an ECOFF BFD, add symbols to the global hash table as 3692 appropriate. */ 3693 3694 bfd_boolean 3695 _bfd_ecoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 3696 { 3697 switch (bfd_get_format (abfd)) 3698 { 3699 case bfd_object: 3700 return ecoff_link_add_object_symbols (abfd, info); 3701 case bfd_archive: 3702 return ecoff_link_add_archive_symbols (abfd, info); 3703 default: 3704 bfd_set_error (bfd_error_wrong_format); 3705 return FALSE; 3706 } 3707 } 3708 3709 3710 /* ECOFF final link routines. */ 3712 3713 /* Structure used to pass information to ecoff_link_write_external. */ 3714 3715 struct extsym_info 3716 { 3717 bfd *abfd; 3718 struct bfd_link_info *info; 3719 }; 3720 3721 /* Accumulate the debugging information for an input BFD into the 3722 output BFD. This must read in the symbolic information of the 3723 input BFD. */ 3724 3725 static bfd_boolean 3726 ecoff_final_link_debug_accumulate (bfd *output_bfd, 3727 bfd *input_bfd, 3728 struct bfd_link_info *info, 3729 void * handle) 3730 { 3731 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info; 3732 const struct ecoff_debug_swap * const swap = 3733 &ecoff_backend (input_bfd)->debug_swap; 3734 HDRR *symhdr = &debug->symbolic_header; 3735 bfd_boolean ret; 3736 3737 #define READ(ptr, offset, count, size, type) \ 3738 if (symhdr->count == 0) \ 3739 debug->ptr = NULL; \ 3740 else \ 3741 { \ 3742 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \ 3743 debug->ptr = (type) bfd_malloc (amt); \ 3744 if (debug->ptr == NULL) \ 3745 { \ 3746 ret = FALSE; \ 3747 goto return_something; \ 3748 } \ 3749 if (bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \ 3750 || bfd_bread (debug->ptr, amt, input_bfd) != amt) \ 3751 { \ 3752 ret = FALSE; \ 3753 goto return_something; \ 3754 } \ 3755 } 3756 3757 /* If raw_syments is not NULL, then the data was already by read by 3758 _bfd_ecoff_slurp_symbolic_info. */ 3759 if (ecoff_data (input_bfd)->raw_syments == NULL) 3760 { 3761 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), 3762 unsigned char *); 3763 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *); 3764 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *); 3765 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *); 3766 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *); 3767 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext), 3768 union aux_ext *); 3769 READ (ss, cbSsOffset, issMax, sizeof (char), char *); 3770 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *); 3771 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *); 3772 } 3773 #undef READ 3774 3775 /* We do not read the external strings or the external symbols. */ 3776 3777 ret = (bfd_ecoff_debug_accumulate 3778 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info, 3779 &ecoff_backend (output_bfd)->debug_swap, 3780 input_bfd, debug, swap, info)); 3781 3782 return_something: 3783 if (ecoff_data (input_bfd)->raw_syments == NULL) 3784 { 3785 if (debug->line != NULL) 3786 free (debug->line); 3787 if (debug->external_dnr != NULL) 3788 free (debug->external_dnr); 3789 if (debug->external_pdr != NULL) 3790 free (debug->external_pdr); 3791 if (debug->external_sym != NULL) 3792 free (debug->external_sym); 3793 if (debug->external_opt != NULL) 3794 free (debug->external_opt); 3795 if (debug->external_aux != NULL) 3796 free (debug->external_aux); 3797 if (debug->ss != NULL) 3798 free (debug->ss); 3799 if (debug->external_fdr != NULL) 3800 free (debug->external_fdr); 3801 if (debug->external_rfd != NULL) 3802 free (debug->external_rfd); 3803 3804 /* Make sure we don't accidentally follow one of these pointers 3805 into freed memory. */ 3806 debug->line = NULL; 3807 debug->external_dnr = NULL; 3808 debug->external_pdr = NULL; 3809 debug->external_sym = NULL; 3810 debug->external_opt = NULL; 3811 debug->external_aux = NULL; 3812 debug->ss = NULL; 3813 debug->external_fdr = NULL; 3814 debug->external_rfd = NULL; 3815 } 3816 3817 return ret; 3818 } 3819 3820 /* Relocate and write an ECOFF section into an ECOFF output file. */ 3821 3822 static bfd_boolean 3823 ecoff_indirect_link_order (bfd *output_bfd, 3824 struct bfd_link_info *info, 3825 asection *output_section, 3826 struct bfd_link_order *link_order) 3827 { 3828 asection *input_section; 3829 bfd *input_bfd; 3830 bfd_byte *contents = NULL; 3831 bfd_size_type external_reloc_size; 3832 bfd_size_type external_relocs_size; 3833 void * external_relocs = NULL; 3834 3835 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0); 3836 3837 input_section = link_order->u.indirect.section; 3838 input_bfd = input_section->owner; 3839 if (input_section->size == 0) 3840 return TRUE; 3841 3842 BFD_ASSERT (input_section->output_section == output_section); 3843 BFD_ASSERT (input_section->output_offset == link_order->offset); 3844 BFD_ASSERT (input_section->size == link_order->size); 3845 3846 /* Get the section contents. */ 3847 if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents)) 3848 goto error_return; 3849 3850 /* Get the relocs. If we are relaxing MIPS code, they will already 3851 have been read in. Otherwise, we read them in now. */ 3852 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size; 3853 external_relocs_size = external_reloc_size * input_section->reloc_count; 3854 3855 external_relocs = bfd_malloc (external_relocs_size); 3856 if (external_relocs == NULL && external_relocs_size != 0) 3857 goto error_return; 3858 3859 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0 3860 || (bfd_bread (external_relocs, external_relocs_size, input_bfd) 3861 != external_relocs_size)) 3862 goto error_return; 3863 3864 /* Relocate the section contents. */ 3865 if (! ((*ecoff_backend (input_bfd)->relocate_section) 3866 (output_bfd, info, input_bfd, input_section, contents, 3867 external_relocs))) 3868 goto error_return; 3869 3870 /* Write out the relocated section. */ 3871 if (! bfd_set_section_contents (output_bfd, 3872 output_section, 3873 contents, 3874 input_section->output_offset, 3875 input_section->size)) 3876 goto error_return; 3877 3878 /* If we are producing relocatable output, the relocs were 3879 modified, and we write them out now. We use the reloc_count 3880 field of output_section to keep track of the number of relocs we 3881 have output so far. */ 3882 if (info->relocatable) 3883 { 3884 file_ptr pos = (output_section->rel_filepos 3885 + output_section->reloc_count * external_reloc_size); 3886 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 3887 || (bfd_bwrite (external_relocs, external_relocs_size, output_bfd) 3888 != external_relocs_size)) 3889 goto error_return; 3890 output_section->reloc_count += input_section->reloc_count; 3891 } 3892 3893 if (contents != NULL) 3894 free (contents); 3895 if (external_relocs != NULL) 3896 free (external_relocs); 3897 return TRUE; 3898 3899 error_return: 3900 if (contents != NULL) 3901 free (contents); 3902 if (external_relocs != NULL) 3903 free (external_relocs); 3904 return FALSE; 3905 } 3906 3907 /* Generate a reloc when linking an ECOFF file. This is a reloc 3908 requested by the linker, and does come from any input file. This 3909 is used to build constructor and destructor tables when linking 3910 with -Ur. */ 3911 3912 static bfd_boolean 3913 ecoff_reloc_link_order (bfd *output_bfd, 3914 struct bfd_link_info *info, 3915 asection *output_section, 3916 struct bfd_link_order *link_order) 3917 { 3918 enum bfd_link_order_type type; 3919 asection *section; 3920 bfd_vma addend; 3921 arelent rel; 3922 struct internal_reloc in; 3923 bfd_size_type external_reloc_size; 3924 bfd_byte *rbuf; 3925 bfd_boolean ok; 3926 file_ptr pos; 3927 3928 type = link_order->type; 3929 section = NULL; 3930 addend = link_order->u.reloc.p->addend; 3931 3932 /* We set up an arelent to pass to the backend adjust_reloc_out 3933 routine. */ 3934 rel.address = link_order->offset; 3935 3936 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 3937 if (rel.howto == 0) 3938 { 3939 bfd_set_error (bfd_error_bad_value); 3940 return FALSE; 3941 } 3942 3943 if (type == bfd_section_reloc_link_order) 3944 { 3945 section = link_order->u.reloc.p->u.section; 3946 rel.sym_ptr_ptr = section->symbol_ptr_ptr; 3947 } 3948 else 3949 { 3950 struct bfd_link_hash_entry *h; 3951 3952 /* Treat a reloc against a defined symbol as though it were 3953 actually against the section. */ 3954 h = bfd_wrapped_link_hash_lookup (output_bfd, info, 3955 link_order->u.reloc.p->u.name, 3956 FALSE, FALSE, FALSE); 3957 if (h != NULL 3958 && (h->type == bfd_link_hash_defined 3959 || h->type == bfd_link_hash_defweak)) 3960 { 3961 type = bfd_section_reloc_link_order; 3962 section = h->u.def.section->output_section; 3963 /* It seems that we ought to add the symbol value to the 3964 addend here, but in practice it has already been added 3965 because it was passed to constructor_callback. */ 3966 addend += section->vma + h->u.def.section->output_offset; 3967 } 3968 else 3969 { 3970 /* We can't set up a reloc against a symbol correctly, 3971 because we have no asymbol structure. Currently no 3972 adjust_reloc_out routine cares. */ 3973 rel.sym_ptr_ptr = NULL; 3974 } 3975 } 3976 3977 /* All ECOFF relocs are in-place. Put the addend into the object 3978 file. */ 3979 3980 BFD_ASSERT (rel.howto->partial_inplace); 3981 if (addend != 0) 3982 { 3983 bfd_size_type size; 3984 bfd_reloc_status_type rstat; 3985 bfd_byte *buf; 3986 3987 size = bfd_get_reloc_size (rel.howto); 3988 buf = (bfd_byte *) bfd_zmalloc (size); 3989 if (buf == NULL) 3990 return FALSE; 3991 rstat = _bfd_relocate_contents (rel.howto, output_bfd, 3992 (bfd_vma) addend, buf); 3993 switch (rstat) 3994 { 3995 case bfd_reloc_ok: 3996 break; 3997 default: 3998 case bfd_reloc_outofrange: 3999 abort (); 4000 case bfd_reloc_overflow: 4001 if (! ((*info->callbacks->reloc_overflow) 4002 (info, NULL, 4003 (link_order->type == bfd_section_reloc_link_order 4004 ? bfd_section_name (output_bfd, section) 4005 : link_order->u.reloc.p->u.name), 4006 rel.howto->name, addend, NULL, 4007 NULL, (bfd_vma) 0))) 4008 { 4009 free (buf); 4010 return FALSE; 4011 } 4012 break; 4013 } 4014 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf, 4015 (file_ptr) link_order->offset, size); 4016 free (buf); 4017 if (! ok) 4018 return FALSE; 4019 } 4020 4021 rel.addend = 0; 4022 4023 /* Move the information into an internal_reloc structure. */ 4024 in.r_vaddr = (rel.address 4025 + bfd_get_section_vma (output_bfd, output_section)); 4026 in.r_type = rel.howto->type; 4027 4028 if (type == bfd_symbol_reloc_link_order) 4029 { 4030 struct ecoff_link_hash_entry *h; 4031 4032 h = ((struct ecoff_link_hash_entry *) 4033 bfd_wrapped_link_hash_lookup (output_bfd, info, 4034 link_order->u.reloc.p->u.name, 4035 FALSE, FALSE, TRUE)); 4036 if (h != NULL 4037 && h->indx != -1) 4038 in.r_symndx = h->indx; 4039 else 4040 { 4041 if (! ((*info->callbacks->unattached_reloc) 4042 (info, link_order->u.reloc.p->u.name, NULL, 4043 NULL, (bfd_vma) 0))) 4044 return FALSE; 4045 in.r_symndx = 0; 4046 } 4047 in.r_extern = 1; 4048 } 4049 else 4050 { 4051 const char *name; 4052 unsigned int i; 4053 static struct 4054 { 4055 const char * name; 4056 long r_symndx; 4057 } 4058 section_symndx [] = 4059 { 4060 { _TEXT, RELOC_SECTION_TEXT }, 4061 { _RDATA, RELOC_SECTION_RDATA }, 4062 { _DATA, RELOC_SECTION_DATA }, 4063 { _SDATA, RELOC_SECTION_SDATA }, 4064 { _SBSS, RELOC_SECTION_SBSS }, 4065 { _BSS, RELOC_SECTION_BSS }, 4066 { _INIT, RELOC_SECTION_INIT }, 4067 { _LIT8, RELOC_SECTION_LIT8 }, 4068 { _LIT4, RELOC_SECTION_LIT4 }, 4069 { _XDATA, RELOC_SECTION_XDATA }, 4070 { _PDATA, RELOC_SECTION_PDATA }, 4071 { _FINI, RELOC_SECTION_FINI }, 4072 { _LITA, RELOC_SECTION_LITA }, 4073 { "*ABS*", RELOC_SECTION_ABS }, 4074 { _RCONST, RELOC_SECTION_RCONST } 4075 }; 4076 4077 name = bfd_get_section_name (output_bfd, section); 4078 4079 for (i = 0; i < ARRAY_SIZE (section_symndx); i++) 4080 if (streq (name, section_symndx[i].name)) 4081 { 4082 in.r_symndx = section_symndx[i].r_symndx; 4083 break; 4084 } 4085 4086 if (i == ARRAY_SIZE (section_symndx)) 4087 abort (); 4088 4089 in.r_extern = 0; 4090 } 4091 4092 /* Let the BFD backend adjust the reloc. */ 4093 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in); 4094 4095 /* Get some memory and swap out the reloc. */ 4096 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size; 4097 rbuf = (bfd_byte *) bfd_malloc (external_reloc_size); 4098 if (rbuf == NULL) 4099 return FALSE; 4100 4101 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf); 4102 4103 pos = (output_section->rel_filepos 4104 + output_section->reloc_count * external_reloc_size); 4105 ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0 4106 && (bfd_bwrite ((void *) rbuf, external_reloc_size, output_bfd) 4107 == external_reloc_size)); 4108 4109 if (ok) 4110 ++output_section->reloc_count; 4111 4112 free (rbuf); 4113 4114 return ok; 4115 } 4116 4117 /* Put out information for an external symbol. These come only from 4118 the hash table. */ 4119 4120 static bfd_boolean 4121 ecoff_link_write_external (struct bfd_hash_entry *bh, void * data) 4122 { 4123 struct ecoff_link_hash_entry *h = (struct ecoff_link_hash_entry *) bh; 4124 struct extsym_info *einfo = (struct extsym_info *) data; 4125 bfd *output_bfd = einfo->abfd; 4126 bfd_boolean strip; 4127 4128 if (h->root.type == bfd_link_hash_warning) 4129 { 4130 h = (struct ecoff_link_hash_entry *) h->root.u.i.link; 4131 if (h->root.type == bfd_link_hash_new) 4132 return TRUE; 4133 } 4134 4135 /* We need to check if this symbol is being stripped. */ 4136 if (h->root.type == bfd_link_hash_undefined 4137 || h->root.type == bfd_link_hash_undefweak) 4138 strip = FALSE; 4139 else if (einfo->info->strip == strip_all 4140 || (einfo->info->strip == strip_some 4141 && bfd_hash_lookup (einfo->info->keep_hash, 4142 h->root.root.string, 4143 FALSE, FALSE) == NULL)) 4144 strip = TRUE; 4145 else 4146 strip = FALSE; 4147 4148 if (strip || h->written) 4149 return TRUE; 4150 4151 if (h->abfd == NULL) 4152 { 4153 h->esym.jmptbl = 0; 4154 h->esym.cobol_main = 0; 4155 h->esym.weakext = 0; 4156 h->esym.reserved = 0; 4157 h->esym.ifd = ifdNil; 4158 h->esym.asym.value = 0; 4159 h->esym.asym.st = stGlobal; 4160 4161 if (h->root.type != bfd_link_hash_defined 4162 && h->root.type != bfd_link_hash_defweak) 4163 h->esym.asym.sc = scAbs; 4164 else 4165 { 4166 asection *output_section; 4167 const char *name; 4168 unsigned int i; 4169 static struct 4170 { 4171 const char * name; 4172 int sc; 4173 } 4174 section_storage_classes [] = 4175 { 4176 { _TEXT, scText }, 4177 { _DATA, scData }, 4178 { _SDATA, scSData }, 4179 { _RDATA, scRData }, 4180 { _BSS, scBss }, 4181 { _SBSS, scSBss }, 4182 { _INIT, scInit }, 4183 { _FINI, scFini }, 4184 { _PDATA, scPData }, 4185 { _XDATA, scXData }, 4186 { _RCONST, scRConst } 4187 }; 4188 4189 output_section = h->root.u.def.section->output_section; 4190 name = bfd_section_name (output_section->owner, output_section); 4191 4192 for (i = 0; i < ARRAY_SIZE (section_storage_classes); i++) 4193 if (streq (name, section_storage_classes[i].name)) 4194 { 4195 h->esym.asym.sc = section_storage_classes[i].sc; 4196 break; 4197 } 4198 4199 if (i == ARRAY_SIZE (section_storage_classes)) 4200 h->esym.asym.sc = scAbs; 4201 } 4202 4203 h->esym.asym.reserved = 0; 4204 h->esym.asym.index = indexNil; 4205 } 4206 else if (h->esym.ifd != -1) 4207 { 4208 struct ecoff_debug_info *debug; 4209 4210 /* Adjust the FDR index for the symbol by that used for the 4211 input BFD. */ 4212 debug = &ecoff_data (h->abfd)->debug_info; 4213 BFD_ASSERT (h->esym.ifd >= 0 4214 && h->esym.ifd < debug->symbolic_header.ifdMax); 4215 h->esym.ifd = debug->ifdmap[h->esym.ifd]; 4216 } 4217 4218 switch (h->root.type) 4219 { 4220 default: 4221 case bfd_link_hash_warning: 4222 case bfd_link_hash_new: 4223 abort (); 4224 case bfd_link_hash_undefined: 4225 case bfd_link_hash_undefweak: 4226 if (h->esym.asym.sc != scUndefined 4227 && h->esym.asym.sc != scSUndefined) 4228 h->esym.asym.sc = scUndefined; 4229 break; 4230 case bfd_link_hash_defined: 4231 case bfd_link_hash_defweak: 4232 if (h->esym.asym.sc == scUndefined 4233 || h->esym.asym.sc == scSUndefined) 4234 h->esym.asym.sc = scAbs; 4235 else if (h->esym.asym.sc == scCommon) 4236 h->esym.asym.sc = scBss; 4237 else if (h->esym.asym.sc == scSCommon) 4238 h->esym.asym.sc = scSBss; 4239 h->esym.asym.value = (h->root.u.def.value 4240 + h->root.u.def.section->output_section->vma 4241 + h->root.u.def.section->output_offset); 4242 break; 4243 case bfd_link_hash_common: 4244 if (h->esym.asym.sc != scCommon 4245 && h->esym.asym.sc != scSCommon) 4246 h->esym.asym.sc = scCommon; 4247 h->esym.asym.value = h->root.u.c.size; 4248 break; 4249 case bfd_link_hash_indirect: 4250 /* We ignore these symbols, since the indirected symbol is 4251 already in the hash table. */ 4252 return TRUE; 4253 } 4254 4255 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the 4256 symbol number. */ 4257 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax; 4258 h->written = 1; 4259 4260 return (bfd_ecoff_debug_one_external 4261 (output_bfd, &ecoff_data (output_bfd)->debug_info, 4262 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string, 4263 &h->esym)); 4264 } 4265 4266 /* ECOFF final link routine. This looks through all the input BFDs 4267 and gathers together all the debugging information, and then 4268 processes all the link order information. This may cause it to 4269 close and reopen some input BFDs; I'll see how bad this is. */ 4270 4271 bfd_boolean 4272 _bfd_ecoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info) 4273 { 4274 const struct ecoff_backend_data * const backend = ecoff_backend (abfd); 4275 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info; 4276 HDRR *symhdr; 4277 void * handle; 4278 bfd *input_bfd; 4279 asection *o; 4280 struct bfd_link_order *p; 4281 struct extsym_info einfo; 4282 4283 /* We accumulate the debugging information counts in the symbolic 4284 header. */ 4285 symhdr = &debug->symbolic_header; 4286 symhdr->vstamp = 0; 4287 symhdr->ilineMax = 0; 4288 symhdr->cbLine = 0; 4289 symhdr->idnMax = 0; 4290 symhdr->ipdMax = 0; 4291 symhdr->isymMax = 0; 4292 symhdr->ioptMax = 0; 4293 symhdr->iauxMax = 0; 4294 symhdr->issMax = 0; 4295 symhdr->issExtMax = 0; 4296 symhdr->ifdMax = 0; 4297 symhdr->crfd = 0; 4298 symhdr->iextMax = 0; 4299 4300 /* We accumulate the debugging information itself in the debug_info 4301 structure. */ 4302 debug->line = NULL; 4303 debug->external_dnr = NULL; 4304 debug->external_pdr = NULL; 4305 debug->external_sym = NULL; 4306 debug->external_opt = NULL; 4307 debug->external_aux = NULL; 4308 debug->ss = NULL; 4309 debug->ssext = debug->ssext_end = NULL; 4310 debug->external_fdr = NULL; 4311 debug->external_rfd = NULL; 4312 debug->external_ext = debug->external_ext_end = NULL; 4313 4314 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info); 4315 if (handle == NULL) 4316 return FALSE; 4317 4318 /* Accumulate the debugging symbols from each input BFD. */ 4319 for (input_bfd = info->input_bfds; 4320 input_bfd != NULL; 4321 input_bfd = input_bfd->link.next) 4322 { 4323 bfd_boolean ret; 4324 4325 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour) 4326 { 4327 /* Arbitrarily set the symbolic header vstamp to the vstamp 4328 of the first object file in the link. */ 4329 if (symhdr->vstamp == 0) 4330 symhdr->vstamp 4331 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp; 4332 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info, 4333 handle); 4334 } 4335 else 4336 ret = bfd_ecoff_debug_accumulate_other (handle, abfd, 4337 debug, &backend->debug_swap, 4338 input_bfd, info); 4339 if (! ret) 4340 return FALSE; 4341 4342 /* Combine the register masks. */ 4343 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask; 4344 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask; 4345 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0]; 4346 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1]; 4347 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2]; 4348 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3]; 4349 } 4350 4351 /* Write out the external symbols. */ 4352 einfo.abfd = abfd; 4353 einfo.info = info; 4354 bfd_hash_traverse (&info->hash->table, ecoff_link_write_external, &einfo); 4355 4356 if (info->relocatable) 4357 { 4358 /* We need to make a pass over the link_orders to count up the 4359 number of relocations we will need to output, so that we know 4360 how much space they will take up. */ 4361 for (o = abfd->sections; o != NULL; o = o->next) 4362 { 4363 o->reloc_count = 0; 4364 for (p = o->map_head.link_order; 4365 p != NULL; 4366 p = p->next) 4367 if (p->type == bfd_indirect_link_order) 4368 o->reloc_count += p->u.indirect.section->reloc_count; 4369 else if (p->type == bfd_section_reloc_link_order 4370 || p->type == bfd_symbol_reloc_link_order) 4371 ++o->reloc_count; 4372 } 4373 } 4374 4375 /* Compute the reloc and symbol file positions. */ 4376 ecoff_compute_reloc_file_positions (abfd); 4377 4378 /* Write out the debugging information. */ 4379 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug, 4380 &backend->debug_swap, info, 4381 ecoff_data (abfd)->sym_filepos)) 4382 return FALSE; 4383 4384 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info); 4385 4386 if (info->relocatable) 4387 { 4388 /* Now reset the reloc_count field of the sections in the output 4389 BFD to 0, so that we can use them to keep track of how many 4390 relocs we have output thus far. */ 4391 for (o = abfd->sections; o != NULL; o = o->next) 4392 o->reloc_count = 0; 4393 } 4394 4395 /* Get a value for the GP register. */ 4396 if (ecoff_data (abfd)->gp == 0) 4397 { 4398 struct bfd_link_hash_entry *h; 4399 4400 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE); 4401 if (h != NULL 4402 && h->type == bfd_link_hash_defined) 4403 ecoff_data (abfd)->gp = (h->u.def.value 4404 + h->u.def.section->output_section->vma 4405 + h->u.def.section->output_offset); 4406 else if (info->relocatable) 4407 { 4408 bfd_vma lo; 4409 4410 /* Make up a value. */ 4411 lo = (bfd_vma) -1; 4412 for (o = abfd->sections; o != NULL; o = o->next) 4413 { 4414 if (o->vma < lo 4415 && (streq (o->name, _SBSS) 4416 || streq (o->name, _SDATA) 4417 || streq (o->name, _LIT4) 4418 || streq (o->name, _LIT8) 4419 || streq (o->name, _LITA))) 4420 lo = o->vma; 4421 } 4422 ecoff_data (abfd)->gp = lo + 0x8000; 4423 } 4424 else 4425 { 4426 /* If the relocate_section function needs to do a reloc 4427 involving the GP value, it should make a reloc_dangerous 4428 callback to warn that GP is not defined. */ 4429 } 4430 } 4431 4432 for (o = abfd->sections; o != NULL; o = o->next) 4433 { 4434 for (p = o->map_head.link_order; 4435 p != NULL; 4436 p = p->next) 4437 { 4438 if (p->type == bfd_indirect_link_order 4439 && (bfd_get_flavour (p->u.indirect.section->owner) 4440 == bfd_target_ecoff_flavour)) 4441 { 4442 if (! ecoff_indirect_link_order (abfd, info, o, p)) 4443 return FALSE; 4444 } 4445 else if (p->type == bfd_section_reloc_link_order 4446 || p->type == bfd_symbol_reloc_link_order) 4447 { 4448 if (! ecoff_reloc_link_order (abfd, info, o, p)) 4449 return FALSE; 4450 } 4451 else 4452 { 4453 if (! _bfd_default_link_order (abfd, info, o, p)) 4454 return FALSE; 4455 } 4456 } 4457 } 4458 4459 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax; 4460 4461 ecoff_data (abfd)->linker = TRUE; 4462 4463 return TRUE; 4464 } 4465