1 /* COFF specific linker code. 2 Copyright (C) 1994-2018 Free Software Foundation, Inc. 3 Written by Ian Lance Taylor, Cygnus Support. 4 5 This file is part of BFD, the Binary File Descriptor library. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 20 MA 02110-1301, USA. */ 21 22 /* This file contains the COFF backend linker code. */ 23 24 #include "sysdep.h" 25 #include "bfd.h" 26 #include "bfdlink.h" 27 #include "libbfd.h" 28 #include "coff/internal.h" 29 #include "libcoff.h" 30 #include "safe-ctype.h" 31 32 static bfd_boolean coff_link_add_object_symbols (bfd *, struct bfd_link_info *); 33 static bfd_boolean coff_link_check_archive_element 34 (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *, 35 bfd_boolean *); 36 static bfd_boolean coff_link_add_symbols (bfd *, struct bfd_link_info *); 37 38 /* Return TRUE if SYM is a weak, external symbol. */ 39 #define IS_WEAK_EXTERNAL(abfd, sym) \ 40 ((sym).n_sclass == C_WEAKEXT \ 41 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK)) 42 43 /* Return TRUE if SYM is an external symbol. */ 44 #define IS_EXTERNAL(abfd, sym) \ 45 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym)) 46 47 /* Define macros so that the ISFCN, et. al., macros work correctly. 48 These macros are defined in include/coff/internal.h in terms of 49 N_TMASK, etc. These definitions require a user to define local 50 variables with the appropriate names, and with values from the 51 coff_data (abfd) structure. */ 52 53 #define N_TMASK n_tmask 54 #define N_BTSHFT n_btshft 55 #define N_BTMASK n_btmask 56 57 /* Create an entry in a COFF linker hash table. */ 58 59 struct bfd_hash_entry * 60 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry, 61 struct bfd_hash_table *table, 62 const char *string) 63 { 64 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry; 65 66 /* Allocate the structure if it has not already been allocated by a 67 subclass. */ 68 if (ret == (struct coff_link_hash_entry *) NULL) 69 ret = ((struct coff_link_hash_entry *) 70 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry))); 71 if (ret == (struct coff_link_hash_entry *) NULL) 72 return (struct bfd_hash_entry *) ret; 73 74 /* Call the allocation method of the superclass. */ 75 ret = ((struct coff_link_hash_entry *) 76 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, 77 table, string)); 78 if (ret != (struct coff_link_hash_entry *) NULL) 79 { 80 /* Set local fields. */ 81 ret->indx = -1; 82 ret->type = T_NULL; 83 ret->symbol_class = C_NULL; 84 ret->numaux = 0; 85 ret->auxbfd = NULL; 86 ret->aux = NULL; 87 } 88 89 return (struct bfd_hash_entry *) ret; 90 } 91 92 /* Initialize a COFF linker hash table. */ 93 94 bfd_boolean 95 _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table, 96 bfd *abfd, 97 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 98 struct bfd_hash_table *, 99 const char *), 100 unsigned int entsize) 101 { 102 memset (&table->stab_info, 0, sizeof (table->stab_info)); 103 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); 104 } 105 106 /* Create a COFF linker hash table. */ 107 108 struct bfd_link_hash_table * 109 _bfd_coff_link_hash_table_create (bfd *abfd) 110 { 111 struct coff_link_hash_table *ret; 112 bfd_size_type amt = sizeof (struct coff_link_hash_table); 113 114 ret = (struct coff_link_hash_table *) bfd_malloc (amt); 115 if (ret == NULL) 116 return NULL; 117 118 if (! _bfd_coff_link_hash_table_init (ret, abfd, 119 _bfd_coff_link_hash_newfunc, 120 sizeof (struct coff_link_hash_entry))) 121 { 122 free (ret); 123 return (struct bfd_link_hash_table *) NULL; 124 } 125 return &ret->root; 126 } 127 128 /* Create an entry in a COFF debug merge hash table. */ 129 130 struct bfd_hash_entry * 131 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry, 132 struct bfd_hash_table *table, 133 const char *string) 134 { 135 struct coff_debug_merge_hash_entry *ret = 136 (struct coff_debug_merge_hash_entry *) entry; 137 138 /* Allocate the structure if it has not already been allocated by a 139 subclass. */ 140 if (ret == (struct coff_debug_merge_hash_entry *) NULL) 141 ret = ((struct coff_debug_merge_hash_entry *) 142 bfd_hash_allocate (table, 143 sizeof (struct coff_debug_merge_hash_entry))); 144 if (ret == (struct coff_debug_merge_hash_entry *) NULL) 145 return (struct bfd_hash_entry *) ret; 146 147 /* Call the allocation method of the superclass. */ 148 ret = ((struct coff_debug_merge_hash_entry *) 149 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); 150 if (ret != (struct coff_debug_merge_hash_entry *) NULL) 151 { 152 /* Set local fields. */ 153 ret->types = NULL; 154 } 155 156 return (struct bfd_hash_entry *) ret; 157 } 158 159 /* Given a COFF BFD, add symbols to the global hash table as 160 appropriate. */ 161 162 bfd_boolean 163 _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 164 { 165 switch (bfd_get_format (abfd)) 166 { 167 case bfd_object: 168 return coff_link_add_object_symbols (abfd, info); 169 case bfd_archive: 170 return _bfd_generic_link_add_archive_symbols 171 (abfd, info, coff_link_check_archive_element); 172 default: 173 bfd_set_error (bfd_error_wrong_format); 174 return FALSE; 175 } 176 } 177 178 /* Add symbols from a COFF object file. */ 179 180 static bfd_boolean 181 coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 182 { 183 if (! _bfd_coff_get_external_symbols (abfd)) 184 return FALSE; 185 if (! coff_link_add_symbols (abfd, info)) 186 return FALSE; 187 188 if (! info->keep_memory 189 && ! _bfd_coff_free_symbols (abfd)) 190 return FALSE; 191 192 return TRUE; 193 } 194 195 /* Check a single archive element to see if we need to include it in 196 the link. *PNEEDED is set according to whether this element is 197 needed in the link or not. This is called via 198 _bfd_generic_link_add_archive_symbols. */ 199 200 static bfd_boolean 201 coff_link_check_archive_element (bfd *abfd, 202 struct bfd_link_info *info, 203 struct bfd_link_hash_entry *h, 204 const char *name, 205 bfd_boolean *pneeded) 206 { 207 *pneeded = FALSE; 208 209 /* We are only interested in symbols that are currently undefined. 210 If a symbol is currently known to be common, COFF linkers do not 211 bring in an object file which defines it. */ 212 if (h->type != bfd_link_hash_undefined) 213 return TRUE; 214 215 /* PR 22369 - Skip non COFF objects in the archive. */ 216 if (! bfd_family_coff (abfd)) 217 return TRUE; 218 219 /* Include this element? */ 220 if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd)) 221 return TRUE; 222 *pneeded = TRUE; 223 224 return coff_link_add_object_symbols (abfd, info); 225 } 226 227 /* Add all the symbols from an object file to the hash table. */ 228 229 static bfd_boolean 230 coff_link_add_symbols (bfd *abfd, 231 struct bfd_link_info *info) 232 { 233 unsigned int n_tmask = coff_data (abfd)->local_n_tmask; 234 unsigned int n_btshft = coff_data (abfd)->local_n_btshft; 235 unsigned int n_btmask = coff_data (abfd)->local_n_btmask; 236 bfd_boolean keep_syms; 237 bfd_boolean default_copy; 238 bfd_size_type symcount; 239 struct coff_link_hash_entry **sym_hash; 240 bfd_size_type symesz; 241 bfd_byte *esym; 242 bfd_byte *esym_end; 243 bfd_size_type amt; 244 245 symcount = obj_raw_syment_count (abfd); 246 247 if (symcount == 0) 248 return TRUE; /* Nothing to do. */ 249 250 /* Keep the symbols during this function, in case the linker needs 251 to read the generic symbols in order to report an error message. */ 252 keep_syms = obj_coff_keep_syms (abfd); 253 obj_coff_keep_syms (abfd) = TRUE; 254 255 if (info->keep_memory) 256 default_copy = FALSE; 257 else 258 default_copy = TRUE; 259 260 /* We keep a list of the linker hash table entries that correspond 261 to particular symbols. */ 262 amt = symcount * sizeof (struct coff_link_hash_entry *); 263 sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt); 264 if (sym_hash == NULL) 265 goto error_return; 266 obj_coff_sym_hashes (abfd) = sym_hash; 267 268 symesz = bfd_coff_symesz (abfd); 269 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd)); 270 esym = (bfd_byte *) obj_coff_external_syms (abfd); 271 esym_end = esym + symcount * symesz; 272 while (esym < esym_end) 273 { 274 struct internal_syment sym; 275 enum coff_symbol_classification classification; 276 bfd_boolean copy; 277 278 bfd_coff_swap_sym_in (abfd, esym, &sym); 279 280 classification = bfd_coff_classify_symbol (abfd, &sym); 281 if (classification != COFF_SYMBOL_LOCAL) 282 { 283 const char *name; 284 char buf[SYMNMLEN + 1]; 285 flagword flags; 286 asection *section; 287 bfd_vma value; 288 bfd_boolean addit; 289 290 /* This symbol is externally visible. */ 291 292 name = _bfd_coff_internal_syment_name (abfd, &sym, buf); 293 if (name == NULL) 294 goto error_return; 295 296 /* We must copy the name into memory if we got it from the 297 syment itself, rather than the string table. */ 298 copy = default_copy; 299 if (sym._n._n_n._n_zeroes != 0 300 || sym._n._n_n._n_offset == 0) 301 copy = TRUE; 302 303 value = sym.n_value; 304 305 switch (classification) 306 { 307 default: 308 abort (); 309 310 case COFF_SYMBOL_GLOBAL: 311 flags = BSF_EXPORT | BSF_GLOBAL; 312 section = coff_section_from_bfd_index (abfd, sym.n_scnum); 313 if (! obj_pe (abfd)) 314 value -= section->vma; 315 break; 316 317 case COFF_SYMBOL_UNDEFINED: 318 flags = 0; 319 section = bfd_und_section_ptr; 320 break; 321 322 case COFF_SYMBOL_COMMON: 323 flags = BSF_GLOBAL; 324 section = bfd_com_section_ptr; 325 break; 326 327 case COFF_SYMBOL_PE_SECTION: 328 flags = BSF_SECTION_SYM | BSF_GLOBAL; 329 section = coff_section_from_bfd_index (abfd, sym.n_scnum); 330 break; 331 } 332 333 if (IS_WEAK_EXTERNAL (abfd, sym)) 334 flags = BSF_WEAK; 335 336 addit = TRUE; 337 338 /* In the PE format, section symbols actually refer to the 339 start of the output section. We handle them specially 340 here. */ 341 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0) 342 { 343 *sym_hash = coff_link_hash_lookup (coff_hash_table (info), 344 name, FALSE, copy, FALSE); 345 if (*sym_hash != NULL) 346 { 347 if (((*sym_hash)->coff_link_hash_flags 348 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0 349 && (*sym_hash)->root.type != bfd_link_hash_undefined 350 && (*sym_hash)->root.type != bfd_link_hash_undefweak) 351 _bfd_error_handler 352 (_("warning: symbol `%s' is both section and non-section"), 353 name); 354 355 addit = FALSE; 356 } 357 } 358 359 /* The Microsoft Visual C compiler does string pooling by 360 hashing the constants to an internal symbol name, and 361 relying on the linker comdat support to discard 362 duplicate names. However, if one string is a literal and 363 one is a data initializer, one will end up in the .data 364 section and one will end up in the .rdata section. The 365 Microsoft linker will combine them into the .data 366 section, which seems to be wrong since it might cause the 367 literal to change. 368 369 As long as there are no external references to the 370 symbols, which there shouldn't be, we can treat the .data 371 and .rdata instances as separate symbols. The comdat 372 code in the linker will do the appropriate merging. Here 373 we avoid getting a multiple definition error for one of 374 these special symbols. 375 376 FIXME: I don't think this will work in the case where 377 there are two object files which use the constants as a 378 literal and two object files which use it as a data 379 initializer. One or the other of the second object files 380 is going to wind up with an inappropriate reference. */ 381 if (obj_pe (abfd) 382 && (classification == COFF_SYMBOL_GLOBAL 383 || classification == COFF_SYMBOL_PE_SECTION) 384 && coff_section_data (abfd, section) != NULL 385 && coff_section_data (abfd, section)->comdat != NULL 386 && CONST_STRNEQ (name, "??_") 387 && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0) 388 { 389 if (*sym_hash == NULL) 390 *sym_hash = coff_link_hash_lookup (coff_hash_table (info), 391 name, FALSE, copy, FALSE); 392 if (*sym_hash != NULL 393 && (*sym_hash)->root.type == bfd_link_hash_defined 394 && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL 395 && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name, 396 coff_section_data (abfd, section)->comdat->name) == 0) 397 addit = FALSE; 398 } 399 400 if (addit) 401 { 402 if (! (bfd_coff_link_add_one_symbol 403 (info, abfd, name, flags, section, value, 404 (const char *) NULL, copy, FALSE, 405 (struct bfd_link_hash_entry **) sym_hash))) 406 goto error_return; 407 } 408 409 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0) 410 (*sym_hash)->coff_link_hash_flags |= 411 COFF_LINK_HASH_PE_SECTION_SYMBOL; 412 413 /* Limit the alignment of a common symbol to the possible 414 alignment of a section. There is no point to permitting 415 a higher alignment for a common symbol: we can not 416 guarantee it, and it may cause us to allocate extra space 417 in the common section. */ 418 if (section == bfd_com_section_ptr 419 && (*sym_hash)->root.type == bfd_link_hash_common 420 && ((*sym_hash)->root.u.c.p->alignment_power 421 > bfd_coff_default_section_alignment_power (abfd))) 422 (*sym_hash)->root.u.c.p->alignment_power 423 = bfd_coff_default_section_alignment_power (abfd); 424 425 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)) 426 { 427 /* If we don't have any symbol information currently in 428 the hash table, or if we are looking at a symbol 429 definition, then update the symbol class and type in 430 the hash table. */ 431 if (((*sym_hash)->symbol_class == C_NULL 432 && (*sym_hash)->type == T_NULL) 433 || sym.n_scnum != 0 434 || (sym.n_value != 0 435 && (*sym_hash)->root.type != bfd_link_hash_defined 436 && (*sym_hash)->root.type != bfd_link_hash_defweak)) 437 { 438 (*sym_hash)->symbol_class = sym.n_sclass; 439 if (sym.n_type != T_NULL) 440 { 441 /* We want to warn if the type changed, but not 442 if it changed from an unspecified type. 443 Testing the whole type byte may work, but the 444 change from (e.g.) a function of unspecified 445 type to function of known type also wants to 446 skip the warning. */ 447 if ((*sym_hash)->type != T_NULL 448 && (*sym_hash)->type != sym.n_type 449 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type) 450 && (BTYPE ((*sym_hash)->type) == T_NULL 451 || BTYPE (sym.n_type) == T_NULL))) 452 _bfd_error_handler 453 /* xgettext: c-format */ 454 (_("warning: type of symbol `%s' changed" 455 " from %d to %d in %pB"), 456 name, (*sym_hash)->type, sym.n_type, abfd); 457 458 /* We don't want to change from a meaningful 459 base type to a null one, but if we know 460 nothing, take what little we might now know. */ 461 if (BTYPE (sym.n_type) != T_NULL 462 || (*sym_hash)->type == T_NULL) 463 (*sym_hash)->type = sym.n_type; 464 } 465 (*sym_hash)->auxbfd = abfd; 466 if (sym.n_numaux != 0) 467 { 468 union internal_auxent *alloc; 469 unsigned int i; 470 bfd_byte *eaux; 471 union internal_auxent *iaux; 472 473 (*sym_hash)->numaux = sym.n_numaux; 474 alloc = ((union internal_auxent *) 475 bfd_hash_allocate (&info->hash->table, 476 (sym.n_numaux 477 * sizeof (*alloc)))); 478 if (alloc == NULL) 479 goto error_return; 480 for (i = 0, eaux = esym + symesz, iaux = alloc; 481 i < sym.n_numaux; 482 i++, eaux += symesz, iaux++) 483 bfd_coff_swap_aux_in (abfd, eaux, sym.n_type, 484 sym.n_sclass, (int) i, 485 sym.n_numaux, iaux); 486 (*sym_hash)->aux = alloc; 487 } 488 } 489 } 490 491 if (classification == COFF_SYMBOL_PE_SECTION 492 && (*sym_hash)->numaux != 0) 493 { 494 /* Some PE sections (such as .bss) have a zero size in 495 the section header, but a non-zero size in the AUX 496 record. Correct that here. 497 498 FIXME: This is not at all the right place to do this. 499 For example, it won't help objdump. This needs to be 500 done when we swap in the section header. */ 501 BFD_ASSERT ((*sym_hash)->numaux == 1); 502 if (section->size == 0) 503 section->size = (*sym_hash)->aux[0].x_scn.x_scnlen; 504 505 /* FIXME: We could test whether the section sizes 506 matches the size in the aux entry, but apparently 507 that sometimes fails unexpectedly. */ 508 } 509 } 510 511 esym += (sym.n_numaux + 1) * symesz; 512 sym_hash += sym.n_numaux + 1; 513 } 514 515 /* If this is a non-traditional, non-relocatable link, try to 516 optimize the handling of any .stab/.stabstr sections. */ 517 if (! bfd_link_relocatable (info) 518 && ! info->traditional_format 519 && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd) 520 && (info->strip != strip_all && info->strip != strip_debugger)) 521 { 522 asection *stabstr; 523 524 stabstr = bfd_get_section_by_name (abfd, ".stabstr"); 525 526 if (stabstr != NULL) 527 { 528 bfd_size_type string_offset = 0; 529 asection *stab; 530 531 for (stab = abfd->sections; stab; stab = stab->next) 532 if (CONST_STRNEQ (stab->name, ".stab") 533 && (!stab->name[5] 534 || (stab->name[5] == '.' && ISDIGIT (stab->name[6])))) 535 { 536 struct coff_link_hash_table *table; 537 struct coff_section_tdata *secdata 538 = coff_section_data (abfd, stab); 539 540 if (secdata == NULL) 541 { 542 amt = sizeof (struct coff_section_tdata); 543 stab->used_by_bfd = bfd_zalloc (abfd, amt); 544 if (stab->used_by_bfd == NULL) 545 goto error_return; 546 secdata = coff_section_data (abfd, stab); 547 } 548 549 table = coff_hash_table (info); 550 551 if (! _bfd_link_section_stabs (abfd, &table->stab_info, 552 stab, stabstr, 553 &secdata->stab_info, 554 &string_offset)) 555 goto error_return; 556 } 557 } 558 } 559 560 obj_coff_keep_syms (abfd) = keep_syms; 561 562 return TRUE; 563 564 error_return: 565 obj_coff_keep_syms (abfd) = keep_syms; 566 return FALSE; 567 } 568 569 /* Do the final link step. */ 570 571 bfd_boolean 572 _bfd_coff_final_link (bfd *abfd, 573 struct bfd_link_info *info) 574 { 575 bfd_size_type symesz; 576 struct coff_final_link_info flaginfo; 577 bfd_boolean debug_merge_allocated; 578 bfd_boolean long_section_names; 579 asection *o; 580 struct bfd_link_order *p; 581 bfd_size_type max_sym_count; 582 bfd_size_type max_lineno_count; 583 bfd_size_type max_reloc_count; 584 bfd_size_type max_output_reloc_count; 585 bfd_size_type max_contents_size; 586 file_ptr rel_filepos; 587 unsigned int relsz; 588 file_ptr line_filepos; 589 unsigned int linesz; 590 bfd *sub; 591 bfd_byte *external_relocs = NULL; 592 char strbuf[STRING_SIZE_SIZE]; 593 bfd_size_type amt; 594 595 symesz = bfd_coff_symesz (abfd); 596 597 flaginfo.info = info; 598 flaginfo.output_bfd = abfd; 599 flaginfo.strtab = NULL; 600 flaginfo.section_info = NULL; 601 flaginfo.last_file_index = -1; 602 flaginfo.last_bf_index = -1; 603 flaginfo.internal_syms = NULL; 604 flaginfo.sec_ptrs = NULL; 605 flaginfo.sym_indices = NULL; 606 flaginfo.outsyms = NULL; 607 flaginfo.linenos = NULL; 608 flaginfo.contents = NULL; 609 flaginfo.external_relocs = NULL; 610 flaginfo.internal_relocs = NULL; 611 flaginfo.global_to_static = FALSE; 612 debug_merge_allocated = FALSE; 613 614 coff_data (abfd)->link_info = info; 615 616 flaginfo.strtab = _bfd_stringtab_init (); 617 if (flaginfo.strtab == NULL) 618 goto error_return; 619 620 if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge)) 621 goto error_return; 622 debug_merge_allocated = TRUE; 623 624 /* Compute the file positions for all the sections. */ 625 if (! abfd->output_has_begun) 626 { 627 if (! bfd_coff_compute_section_file_positions (abfd)) 628 goto error_return; 629 } 630 631 /* Count the line numbers and relocation entries required for the 632 output file. Set the file positions for the relocs. */ 633 rel_filepos = obj_relocbase (abfd); 634 relsz = bfd_coff_relsz (abfd); 635 max_contents_size = 0; 636 max_lineno_count = 0; 637 max_reloc_count = 0; 638 639 long_section_names = FALSE; 640 for (o = abfd->sections; o != NULL; o = o->next) 641 { 642 o->reloc_count = 0; 643 o->lineno_count = 0; 644 for (p = o->map_head.link_order; p != NULL; p = p->next) 645 { 646 if (p->type == bfd_indirect_link_order) 647 { 648 asection *sec; 649 650 sec = p->u.indirect.section; 651 652 /* Mark all sections which are to be included in the 653 link. This will normally be every section. We need 654 to do this so that we can identify any sections which 655 the linker has decided to not include. */ 656 sec->linker_mark = TRUE; 657 658 if (info->strip == strip_none 659 || info->strip == strip_some) 660 o->lineno_count += sec->lineno_count; 661 662 if (bfd_link_relocatable (info)) 663 o->reloc_count += sec->reloc_count; 664 665 if (sec->rawsize > max_contents_size) 666 max_contents_size = sec->rawsize; 667 if (sec->size > max_contents_size) 668 max_contents_size = sec->size; 669 if (sec->lineno_count > max_lineno_count) 670 max_lineno_count = sec->lineno_count; 671 if (sec->reloc_count > max_reloc_count) 672 max_reloc_count = sec->reloc_count; 673 } 674 else if (bfd_link_relocatable (info) 675 && (p->type == bfd_section_reloc_link_order 676 || p->type == bfd_symbol_reloc_link_order)) 677 ++o->reloc_count; 678 } 679 if (o->reloc_count == 0) 680 o->rel_filepos = 0; 681 else 682 { 683 o->flags |= SEC_RELOC; 684 o->rel_filepos = rel_filepos; 685 rel_filepos += o->reloc_count * relsz; 686 /* In PE COFF, if there are at least 0xffff relocations an 687 extra relocation will be written out to encode the count. */ 688 if (obj_pe (abfd) && o->reloc_count >= 0xffff) 689 rel_filepos += relsz; 690 } 691 692 if (bfd_coff_long_section_names (abfd) 693 && strlen (o->name) > SCNNMLEN) 694 { 695 /* This section has a long name which must go in the string 696 table. This must correspond to the code in 697 coff_write_object_contents which puts the string index 698 into the s_name field of the section header. That is why 699 we pass hash as FALSE. */ 700 if (_bfd_stringtab_add (flaginfo.strtab, o->name, FALSE, FALSE) 701 == (bfd_size_type) -1) 702 goto error_return; 703 long_section_names = TRUE; 704 } 705 } 706 707 /* If doing a relocatable link, allocate space for the pointers we 708 need to keep. */ 709 if (bfd_link_relocatable (info)) 710 { 711 unsigned int i; 712 713 /* We use section_count + 1, rather than section_count, because 714 the target_index fields are 1 based. */ 715 amt = abfd->section_count + 1; 716 amt *= sizeof (struct coff_link_section_info); 717 flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt); 718 if (flaginfo.section_info == NULL) 719 goto error_return; 720 for (i = 0; i <= abfd->section_count; i++) 721 { 722 flaginfo.section_info[i].relocs = NULL; 723 flaginfo.section_info[i].rel_hashes = NULL; 724 } 725 } 726 727 /* We now know the size of the relocs, so we can determine the file 728 positions of the line numbers. */ 729 line_filepos = rel_filepos; 730 linesz = bfd_coff_linesz (abfd); 731 max_output_reloc_count = 0; 732 for (o = abfd->sections; o != NULL; o = o->next) 733 { 734 if (o->lineno_count == 0) 735 o->line_filepos = 0; 736 else 737 { 738 o->line_filepos = line_filepos; 739 line_filepos += o->lineno_count * linesz; 740 } 741 742 if (o->reloc_count != 0) 743 { 744 /* We don't know the indices of global symbols until we have 745 written out all the local symbols. For each section in 746 the output file, we keep an array of pointers to hash 747 table entries. Each entry in the array corresponds to a 748 reloc. When we find a reloc against a global symbol, we 749 set the corresponding entry in this array so that we can 750 fix up the symbol index after we have written out all the 751 local symbols. 752 753 Because of this problem, we also keep the relocs in 754 memory until the end of the link. This wastes memory, 755 but only when doing a relocatable link, which is not the 756 common case. */ 757 BFD_ASSERT (bfd_link_relocatable (info)); 758 amt = o->reloc_count; 759 amt *= sizeof (struct internal_reloc); 760 flaginfo.section_info[o->target_index].relocs = 761 (struct internal_reloc *) bfd_malloc (amt); 762 amt = o->reloc_count; 763 amt *= sizeof (struct coff_link_hash_entry *); 764 flaginfo.section_info[o->target_index].rel_hashes = 765 (struct coff_link_hash_entry **) bfd_malloc (amt); 766 if (flaginfo.section_info[o->target_index].relocs == NULL 767 || flaginfo.section_info[o->target_index].rel_hashes == NULL) 768 goto error_return; 769 770 if (o->reloc_count > max_output_reloc_count) 771 max_output_reloc_count = o->reloc_count; 772 } 773 774 /* Reset the reloc and lineno counts, so that we can use them to 775 count the number of entries we have output so far. */ 776 o->reloc_count = 0; 777 o->lineno_count = 0; 778 } 779 780 obj_sym_filepos (abfd) = line_filepos; 781 782 /* Figure out the largest number of symbols in an input BFD. Take 783 the opportunity to clear the output_has_begun fields of all the 784 input BFD's. */ 785 max_sym_count = 0; 786 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 787 { 788 size_t sz; 789 790 sub->output_has_begun = FALSE; 791 sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2; 792 if (sz > max_sym_count) 793 max_sym_count = sz; 794 } 795 796 /* Allocate some buffers used while linking. */ 797 amt = max_sym_count * sizeof (struct internal_syment); 798 flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt); 799 amt = max_sym_count * sizeof (asection *); 800 flaginfo.sec_ptrs = (asection **) bfd_malloc (amt); 801 amt = max_sym_count * sizeof (long); 802 flaginfo.sym_indices = (long int *) bfd_malloc (amt); 803 flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz); 804 amt = max_lineno_count * bfd_coff_linesz (abfd); 805 flaginfo.linenos = (bfd_byte *) bfd_malloc (amt); 806 flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); 807 amt = max_reloc_count * relsz; 808 flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt); 809 if (! bfd_link_relocatable (info)) 810 { 811 amt = max_reloc_count * sizeof (struct internal_reloc); 812 flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt); 813 } 814 if ((flaginfo.internal_syms == NULL && max_sym_count > 0) 815 || (flaginfo.sec_ptrs == NULL && max_sym_count > 0) 816 || (flaginfo.sym_indices == NULL && max_sym_count > 0) 817 || flaginfo.outsyms == NULL 818 || (flaginfo.linenos == NULL && max_lineno_count > 0) 819 || (flaginfo.contents == NULL && max_contents_size > 0) 820 || (flaginfo.external_relocs == NULL && max_reloc_count > 0) 821 || (! bfd_link_relocatable (info) 822 && flaginfo.internal_relocs == NULL 823 && max_reloc_count > 0)) 824 goto error_return; 825 826 /* We now know the position of everything in the file, except that 827 we don't know the size of the symbol table and therefore we don't 828 know where the string table starts. We just build the string 829 table in memory as we go along. We process all the relocations 830 for a single input file at once. */ 831 obj_raw_syment_count (abfd) = 0; 832 833 if (coff_backend_info (abfd)->_bfd_coff_start_final_link) 834 { 835 if (! bfd_coff_start_final_link (abfd, info)) 836 goto error_return; 837 } 838 839 for (o = abfd->sections; o != NULL; o = o->next) 840 { 841 for (p = o->map_head.link_order; p != NULL; p = p->next) 842 { 843 if (p->type == bfd_indirect_link_order 844 && bfd_family_coff (p->u.indirect.section->owner)) 845 { 846 sub = p->u.indirect.section->owner; 847 if (! bfd_coff_link_output_has_begun (sub, & flaginfo)) 848 { 849 if (! _bfd_coff_link_input_bfd (&flaginfo, sub)) 850 goto error_return; 851 sub->output_has_begun = TRUE; 852 } 853 } 854 else if (p->type == bfd_section_reloc_link_order 855 || p->type == bfd_symbol_reloc_link_order) 856 { 857 if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p)) 858 goto error_return; 859 } 860 else 861 { 862 if (! _bfd_default_link_order (abfd, info, o, p)) 863 goto error_return; 864 } 865 } 866 } 867 868 if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all) 869 { 870 /* Add local symbols from foreign inputs. */ 871 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 872 { 873 unsigned int i; 874 875 if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub)) 876 continue; 877 for (i = 0; i < bfd_get_symcount (sub); ++i) 878 { 879 asymbol *sym = bfd_get_outsymbols (sub) [i]; 880 file_ptr pos; 881 struct internal_syment isym; 882 union internal_auxent iaux; 883 bfd_size_type string_size = 0, indx; 884 bfd_vma written = 0; 885 bfd_boolean rewrite = FALSE, hash; 886 887 if (! (sym->flags & BSF_LOCAL) 888 || (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC 889 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC 890 | BSF_SYNTHETIC)) 891 || ((sym->flags & BSF_DEBUGGING) 892 && ! (sym->flags & BSF_FILE))) 893 continue; 894 895 /* See if we are discarding symbols with this name. */ 896 if ((flaginfo.info->strip == strip_some 897 && (bfd_hash_lookup (flaginfo.info->keep_hash, 898 bfd_asymbol_name(sym), FALSE, FALSE) 899 == NULL)) 900 || (((flaginfo.info->discard == discard_sec_merge 901 && (bfd_get_section (sym)->flags & SEC_MERGE) 902 && ! bfd_link_relocatable (flaginfo.info)) 903 || flaginfo.info->discard == discard_l) 904 && bfd_is_local_label_name (sub, bfd_asymbol_name(sym)))) 905 continue; 906 907 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) 908 * symesz; 909 if (bfd_seek (abfd, pos, SEEK_SET) != 0) 910 goto error_return; 911 if (! coff_write_alien_symbol(abfd, sym, &isym, &iaux, &written, 912 &string_size, NULL, NULL)) 913 goto error_return; 914 915 hash = !flaginfo.info->traditional_format; 916 917 if (string_size >= 6 && isym.n_sclass == C_FILE 918 && ! isym._n._n_n._n_zeroes && isym.n_numaux) 919 { 920 indx = _bfd_stringtab_add (flaginfo.strtab, ".file", hash, 921 FALSE); 922 if (indx == (bfd_size_type) -1) 923 goto error_return; 924 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; 925 bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms); 926 if (bfd_seek (abfd, pos, SEEK_SET) != 0 927 || bfd_bwrite (flaginfo.outsyms, symesz, 928 abfd) != symesz) 929 goto error_return; 930 string_size -= 6; 931 } 932 933 if (string_size) 934 { 935 indx = _bfd_stringtab_add (flaginfo.strtab, 936 bfd_asymbol_name (sym), hash, 937 FALSE); 938 if (indx == (bfd_size_type) -1) 939 goto error_return; 940 if (isym.n_sclass != C_FILE) 941 { 942 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; 943 bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms); 944 rewrite = TRUE; 945 } 946 else 947 { 948 BFD_ASSERT (isym.n_numaux == 1); 949 iaux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx; 950 bfd_coff_swap_aux_out (abfd, &iaux, isym.n_type, C_FILE, 951 0, 1, flaginfo.outsyms + symesz); 952 if (bfd_seek (abfd, pos + symesz, SEEK_SET) != 0 953 || bfd_bwrite (flaginfo.outsyms + symesz, symesz, 954 abfd) != symesz) 955 goto error_return; 956 } 957 } 958 959 if (isym.n_sclass == C_FILE) 960 { 961 if (flaginfo.last_file_index != -1) 962 { 963 flaginfo.last_file.n_value = obj_raw_syment_count (abfd); 964 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file, 965 flaginfo.outsyms); 966 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index 967 * symesz; 968 rewrite = TRUE; 969 } 970 flaginfo.last_file_index = obj_raw_syment_count (abfd); 971 flaginfo.last_file = isym; 972 } 973 974 if (rewrite 975 && (bfd_seek (abfd, pos, SEEK_SET) != 0 976 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)) 977 goto error_return; 978 979 obj_raw_syment_count (abfd) += written; 980 } 981 } 982 } 983 984 if (! bfd_coff_final_link_postscript (abfd, & flaginfo)) 985 goto error_return; 986 987 /* Free up the buffers used by _bfd_coff_link_input_bfd. */ 988 989 coff_debug_merge_hash_table_free (&flaginfo.debug_merge); 990 debug_merge_allocated = FALSE; 991 992 if (flaginfo.internal_syms != NULL) 993 { 994 free (flaginfo.internal_syms); 995 flaginfo.internal_syms = NULL; 996 } 997 if (flaginfo.sec_ptrs != NULL) 998 { 999 free (flaginfo.sec_ptrs); 1000 flaginfo.sec_ptrs = NULL; 1001 } 1002 if (flaginfo.sym_indices != NULL) 1003 { 1004 free (flaginfo.sym_indices); 1005 flaginfo.sym_indices = NULL; 1006 } 1007 if (flaginfo.linenos != NULL) 1008 { 1009 free (flaginfo.linenos); 1010 flaginfo.linenos = NULL; 1011 } 1012 if (flaginfo.contents != NULL) 1013 { 1014 free (flaginfo.contents); 1015 flaginfo.contents = NULL; 1016 } 1017 if (flaginfo.external_relocs != NULL) 1018 { 1019 free (flaginfo.external_relocs); 1020 flaginfo.external_relocs = NULL; 1021 } 1022 if (flaginfo.internal_relocs != NULL) 1023 { 1024 free (flaginfo.internal_relocs); 1025 flaginfo.internal_relocs = NULL; 1026 } 1027 1028 /* The value of the last C_FILE symbol is supposed to be the symbol 1029 index of the first external symbol. Write it out again if 1030 necessary. */ 1031 if (flaginfo.last_file_index != -1 1032 && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd)) 1033 { 1034 file_ptr pos; 1035 1036 flaginfo.last_file.n_value = obj_raw_syment_count (abfd); 1037 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file, 1038 flaginfo.outsyms); 1039 1040 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz; 1041 if (bfd_seek (abfd, pos, SEEK_SET) != 0 1042 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz) 1043 return FALSE; 1044 } 1045 1046 /* If doing task linking (ld --task-link) then make a pass through the 1047 global symbols, writing out any that are defined, and making them 1048 static. */ 1049 if (info->task_link) 1050 { 1051 flaginfo.failed = FALSE; 1052 coff_link_hash_traverse (coff_hash_table (info), 1053 _bfd_coff_write_task_globals, &flaginfo); 1054 if (flaginfo.failed) 1055 goto error_return; 1056 } 1057 1058 /* Write out the global symbols. */ 1059 flaginfo.failed = FALSE; 1060 bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo); 1061 if (flaginfo.failed) 1062 goto error_return; 1063 1064 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */ 1065 if (flaginfo.outsyms != NULL) 1066 { 1067 free (flaginfo.outsyms); 1068 flaginfo.outsyms = NULL; 1069 } 1070 1071 if (bfd_link_relocatable (info) && max_output_reloc_count > 0) 1072 { 1073 /* Now that we have written out all the global symbols, we know 1074 the symbol indices to use for relocs against them, and we can 1075 finally write out the relocs. */ 1076 amt = max_output_reloc_count * relsz; 1077 external_relocs = (bfd_byte *) bfd_malloc (amt); 1078 if (external_relocs == NULL) 1079 goto error_return; 1080 1081 for (o = abfd->sections; o != NULL; o = o->next) 1082 { 1083 struct internal_reloc *irel; 1084 struct internal_reloc *irelend; 1085 struct coff_link_hash_entry **rel_hash; 1086 bfd_byte *erel; 1087 1088 if (o->reloc_count == 0) 1089 continue; 1090 1091 irel = flaginfo.section_info[o->target_index].relocs; 1092 irelend = irel + o->reloc_count; 1093 rel_hash = flaginfo.section_info[o->target_index].rel_hashes; 1094 erel = external_relocs; 1095 for (; irel < irelend; irel++, rel_hash++, erel += relsz) 1096 { 1097 if (*rel_hash != NULL) 1098 { 1099 BFD_ASSERT ((*rel_hash)->indx >= 0); 1100 irel->r_symndx = (*rel_hash)->indx; 1101 } 1102 bfd_coff_swap_reloc_out (abfd, irel, erel); 1103 } 1104 1105 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0) 1106 goto error_return; 1107 if (obj_pe (abfd) && o->reloc_count >= 0xffff) 1108 { 1109 /* In PE COFF, write the count of relocs as the first 1110 reloc. The header overflow bit will be set 1111 elsewhere. */ 1112 struct internal_reloc incount; 1113 bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz); 1114 1115 memset (&incount, 0, sizeof (incount)); 1116 incount.r_vaddr = o->reloc_count + 1; 1117 bfd_coff_swap_reloc_out (abfd, &incount, excount); 1118 if (bfd_bwrite (excount, relsz, abfd) != relsz) 1119 /* We'll leak, but it's an error anyway. */ 1120 goto error_return; 1121 free (excount); 1122 } 1123 if (bfd_bwrite (external_relocs, 1124 (bfd_size_type) relsz * o->reloc_count, abfd) 1125 != (bfd_size_type) relsz * o->reloc_count) 1126 goto error_return; 1127 } 1128 1129 free (external_relocs); 1130 external_relocs = NULL; 1131 } 1132 1133 /* Free up the section information. */ 1134 if (flaginfo.section_info != NULL) 1135 { 1136 unsigned int i; 1137 1138 for (i = 0; i < abfd->section_count; i++) 1139 { 1140 if (flaginfo.section_info[i].relocs != NULL) 1141 free (flaginfo.section_info[i].relocs); 1142 if (flaginfo.section_info[i].rel_hashes != NULL) 1143 free (flaginfo.section_info[i].rel_hashes); 1144 } 1145 free (flaginfo.section_info); 1146 flaginfo.section_info = NULL; 1147 } 1148 1149 /* If we have optimized stabs strings, output them. */ 1150 if (coff_hash_table (info)->stab_info.stabstr != NULL) 1151 { 1152 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info)) 1153 return FALSE; 1154 } 1155 1156 /* Write out the string table. */ 1157 if (obj_raw_syment_count (abfd) != 0 || long_section_names) 1158 { 1159 file_ptr pos; 1160 1161 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz; 1162 if (bfd_seek (abfd, pos, SEEK_SET) != 0) 1163 return FALSE; 1164 1165 #if STRING_SIZE_SIZE == 4 1166 H_PUT_32 (abfd, 1167 _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE, 1168 strbuf); 1169 #else 1170 #error Change H_PUT_32 above 1171 #endif 1172 1173 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd) 1174 != STRING_SIZE_SIZE) 1175 return FALSE; 1176 1177 if (! _bfd_stringtab_emit (abfd, flaginfo.strtab)) 1178 return FALSE; 1179 1180 obj_coff_strings_written (abfd) = TRUE; 1181 } 1182 1183 _bfd_stringtab_free (flaginfo.strtab); 1184 1185 /* Setting bfd_get_symcount to 0 will cause write_object_contents to 1186 not try to write out the symbols. */ 1187 bfd_get_symcount (abfd) = 0; 1188 1189 return TRUE; 1190 1191 error_return: 1192 if (debug_merge_allocated) 1193 coff_debug_merge_hash_table_free (&flaginfo.debug_merge); 1194 if (flaginfo.strtab != NULL) 1195 _bfd_stringtab_free (flaginfo.strtab); 1196 if (flaginfo.section_info != NULL) 1197 { 1198 unsigned int i; 1199 1200 for (i = 0; i < abfd->section_count; i++) 1201 { 1202 if (flaginfo.section_info[i].relocs != NULL) 1203 free (flaginfo.section_info[i].relocs); 1204 if (flaginfo.section_info[i].rel_hashes != NULL) 1205 free (flaginfo.section_info[i].rel_hashes); 1206 } 1207 free (flaginfo.section_info); 1208 } 1209 if (flaginfo.internal_syms != NULL) 1210 free (flaginfo.internal_syms); 1211 if (flaginfo.sec_ptrs != NULL) 1212 free (flaginfo.sec_ptrs); 1213 if (flaginfo.sym_indices != NULL) 1214 free (flaginfo.sym_indices); 1215 if (flaginfo.outsyms != NULL) 1216 free (flaginfo.outsyms); 1217 if (flaginfo.linenos != NULL) 1218 free (flaginfo.linenos); 1219 if (flaginfo.contents != NULL) 1220 free (flaginfo.contents); 1221 if (flaginfo.external_relocs != NULL) 1222 free (flaginfo.external_relocs); 1223 if (flaginfo.internal_relocs != NULL) 1224 free (flaginfo.internal_relocs); 1225 if (external_relocs != NULL) 1226 free (external_relocs); 1227 return FALSE; 1228 } 1229 1230 /* Parse out a -heap <reserved>,<commit> line. */ 1231 1232 static char * 1233 dores_com (char *ptr, bfd *output_bfd, int heap) 1234 { 1235 if (coff_data(output_bfd)->pe) 1236 { 1237 int val = strtoul (ptr, &ptr, 0); 1238 1239 if (heap) 1240 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val; 1241 else 1242 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val; 1243 1244 if (ptr[0] == ',') 1245 { 1246 val = strtoul (ptr+1, &ptr, 0); 1247 if (heap) 1248 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val; 1249 else 1250 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val; 1251 } 1252 } 1253 return ptr; 1254 } 1255 1256 static char * 1257 get_name (char *ptr, char **dst) 1258 { 1259 while (*ptr == ' ') 1260 ptr++; 1261 *dst = ptr; 1262 while (*ptr && *ptr != ' ') 1263 ptr++; 1264 *ptr = 0; 1265 return ptr+1; 1266 } 1267 1268 /* Process any magic embedded commands in a section called .drectve. */ 1269 1270 static int 1271 process_embedded_commands (bfd *output_bfd, 1272 struct bfd_link_info *info ATTRIBUTE_UNUSED, 1273 bfd *abfd) 1274 { 1275 asection *sec = bfd_get_section_by_name (abfd, ".drectve"); 1276 char *s; 1277 char *e; 1278 bfd_byte *copy; 1279 1280 if (!sec) 1281 return 1; 1282 1283 if (!bfd_malloc_and_get_section (abfd, sec, ©)) 1284 { 1285 if (copy != NULL) 1286 free (copy); 1287 return 0; 1288 } 1289 e = (char *) copy + sec->size; 1290 1291 for (s = (char *) copy; s < e ; ) 1292 { 1293 if (s[0] != '-') 1294 { 1295 s++; 1296 continue; 1297 } 1298 if (CONST_STRNEQ (s, "-attr")) 1299 { 1300 char *name; 1301 char *attribs; 1302 asection *asec; 1303 int loop = 1; 1304 int had_write = 0; 1305 int had_exec= 0; 1306 1307 s += 5; 1308 s = get_name (s, &name); 1309 s = get_name (s, &attribs); 1310 1311 while (loop) 1312 { 1313 switch (*attribs++) 1314 { 1315 case 'W': 1316 had_write = 1; 1317 break; 1318 case 'R': 1319 break; 1320 case 'S': 1321 break; 1322 case 'X': 1323 had_exec = 1; 1324 break; 1325 default: 1326 loop = 0; 1327 } 1328 } 1329 asec = bfd_get_section_by_name (abfd, name); 1330 if (asec) 1331 { 1332 if (had_exec) 1333 asec->flags |= SEC_CODE; 1334 if (!had_write) 1335 asec->flags |= SEC_READONLY; 1336 } 1337 } 1338 else if (CONST_STRNEQ (s, "-heap")) 1339 s = dores_com (s + 5, output_bfd, 1); 1340 1341 else if (CONST_STRNEQ (s, "-stack")) 1342 s = dores_com (s + 6, output_bfd, 0); 1343 1344 /* GNU extension for aligned commons. */ 1345 else if (CONST_STRNEQ (s, "-aligncomm:")) 1346 { 1347 /* Common symbols must be aligned on reading, as it 1348 is too late to do anything here, after they have 1349 already been allocated, so just skip the directive. */ 1350 s += 11; 1351 } 1352 1353 else 1354 s++; 1355 } 1356 free (copy); 1357 return 1; 1358 } 1359 1360 /* Place a marker against all symbols which are used by relocations. 1361 This marker can be picked up by the 'do we skip this symbol ?' 1362 loop in _bfd_coff_link_input_bfd() and used to prevent skipping 1363 that symbol. */ 1364 1365 static void 1366 mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd) 1367 { 1368 asection * a; 1369 1370 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0) 1371 return; 1372 1373 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next) 1374 { 1375 struct internal_reloc * internal_relocs; 1376 struct internal_reloc * irel; 1377 struct internal_reloc * irelend; 1378 1379 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1 1380 || a->linker_mark == 0) 1381 continue; 1382 /* Don't mark relocs in excluded sections. */ 1383 if (a->output_section == bfd_abs_section_ptr) 1384 continue; 1385 1386 /* Read in the relocs. */ 1387 internal_relocs = _bfd_coff_read_internal_relocs 1388 (input_bfd, a, FALSE, 1389 flaginfo->external_relocs, 1390 bfd_link_relocatable (flaginfo->info), 1391 (bfd_link_relocatable (flaginfo->info) 1392 ? (flaginfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count) 1393 : flaginfo->internal_relocs) 1394 ); 1395 1396 if (internal_relocs == NULL) 1397 continue; 1398 1399 irel = internal_relocs; 1400 irelend = irel + a->reloc_count; 1401 1402 /* Place a mark in the sym_indices array (whose entries have 1403 been initialised to 0) for all of the symbols that are used 1404 in the relocation table. This will then be picked up in the 1405 skip/don't-skip pass. */ 1406 for (; irel < irelend; irel++) 1407 if ((unsigned long) irel->r_symndx < obj_raw_syment_count (input_bfd)) 1408 flaginfo->sym_indices[irel->r_symndx] = -1; 1409 } 1410 } 1411 1412 /* Link an input file into the linker output file. This function 1413 handles all the sections and relocations of the input file at once. */ 1414 1415 bfd_boolean 1416 _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd) 1417 { 1418 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask; 1419 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft; 1420 bfd_boolean (*adjust_symndx) 1421 (bfd *, struct bfd_link_info *, bfd *, asection *, 1422 struct internal_reloc *, bfd_boolean *); 1423 bfd *output_bfd; 1424 const char *strings; 1425 bfd_size_type syment_base; 1426 bfd_boolean copy, hash; 1427 bfd_size_type isymesz; 1428 bfd_size_type osymesz; 1429 bfd_size_type linesz; 1430 bfd_byte *esym; 1431 bfd_byte *esym_end; 1432 struct internal_syment *isymp; 1433 asection **secpp; 1434 long *indexp; 1435 unsigned long output_index; 1436 bfd_byte *outsym; 1437 struct coff_link_hash_entry **sym_hash; 1438 asection *o; 1439 1440 /* Move all the symbols to the output file. */ 1441 1442 output_bfd = flaginfo->output_bfd; 1443 strings = NULL; 1444 syment_base = obj_raw_syment_count (output_bfd); 1445 isymesz = bfd_coff_symesz (input_bfd); 1446 osymesz = bfd_coff_symesz (output_bfd); 1447 linesz = bfd_coff_linesz (input_bfd); 1448 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd)); 1449 1450 copy = FALSE; 1451 if (! flaginfo->info->keep_memory) 1452 copy = TRUE; 1453 hash = TRUE; 1454 if (flaginfo->info->traditional_format) 1455 hash = FALSE; 1456 1457 if (! _bfd_coff_get_external_symbols (input_bfd)) 1458 return FALSE; 1459 1460 esym = (bfd_byte *) obj_coff_external_syms (input_bfd); 1461 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; 1462 isymp = flaginfo->internal_syms; 1463 secpp = flaginfo->sec_ptrs; 1464 indexp = flaginfo->sym_indices; 1465 output_index = syment_base; 1466 outsym = flaginfo->outsyms; 1467 1468 if (coff_data (output_bfd)->pe 1469 && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd)) 1470 return FALSE; 1471 1472 /* If we are going to perform relocations and also strip/discard some 1473 symbols then we must make sure that we do not strip/discard those 1474 symbols that are going to be involved in the relocations. */ 1475 if (( flaginfo->info->strip != strip_none 1476 || flaginfo->info->discard != discard_none) 1477 && bfd_link_relocatable (flaginfo->info)) 1478 { 1479 /* Mark the symbol array as 'not-used'. */ 1480 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp); 1481 1482 mark_relocs (flaginfo, input_bfd); 1483 } 1484 1485 while (esym < esym_end) 1486 { 1487 struct internal_syment isym; 1488 enum coff_symbol_classification classification; 1489 bfd_boolean skip; 1490 bfd_boolean global; 1491 bfd_boolean dont_skip_symbol; 1492 int add; 1493 1494 bfd_coff_swap_sym_in (input_bfd, esym, isymp); 1495 1496 /* Make a copy of *isymp so that the relocate_section function 1497 always sees the original values. This is more reliable than 1498 always recomputing the symbol value even if we are stripping 1499 the symbol. */ 1500 isym = *isymp; 1501 1502 classification = bfd_coff_classify_symbol (input_bfd, &isym); 1503 switch (classification) 1504 { 1505 default: 1506 abort (); 1507 case COFF_SYMBOL_GLOBAL: 1508 case COFF_SYMBOL_PE_SECTION: 1509 case COFF_SYMBOL_LOCAL: 1510 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum); 1511 break; 1512 case COFF_SYMBOL_COMMON: 1513 *secpp = bfd_com_section_ptr; 1514 break; 1515 case COFF_SYMBOL_UNDEFINED: 1516 *secpp = bfd_und_section_ptr; 1517 break; 1518 } 1519 1520 /* Extract the flag indicating if this symbol is used by a 1521 relocation. */ 1522 if ((flaginfo->info->strip != strip_none 1523 || flaginfo->info->discard != discard_none) 1524 && bfd_link_relocatable (flaginfo->info)) 1525 dont_skip_symbol = *indexp; 1526 else 1527 dont_skip_symbol = FALSE; 1528 1529 *indexp = -1; 1530 1531 skip = FALSE; 1532 global = FALSE; 1533 add = 1 + isym.n_numaux; 1534 1535 /* If we are stripping all symbols, we want to skip this one. */ 1536 if (flaginfo->info->strip == strip_all && ! dont_skip_symbol) 1537 skip = TRUE; 1538 1539 if (! skip) 1540 { 1541 switch (classification) 1542 { 1543 default: 1544 abort (); 1545 case COFF_SYMBOL_GLOBAL: 1546 case COFF_SYMBOL_COMMON: 1547 case COFF_SYMBOL_PE_SECTION: 1548 /* This is a global symbol. Global symbols come at the 1549 end of the symbol table, so skip them for now. 1550 Locally defined function symbols, however, are an 1551 exception, and are not moved to the end. */ 1552 global = TRUE; 1553 if (! ISFCN (isym.n_type)) 1554 skip = TRUE; 1555 break; 1556 1557 case COFF_SYMBOL_UNDEFINED: 1558 /* Undefined symbols are left for the end. */ 1559 global = TRUE; 1560 skip = TRUE; 1561 break; 1562 1563 case COFF_SYMBOL_LOCAL: 1564 /* This is a local symbol. Skip it if we are discarding 1565 local symbols. */ 1566 if (flaginfo->info->discard == discard_all && ! dont_skip_symbol) 1567 skip = TRUE; 1568 break; 1569 } 1570 } 1571 1572 #ifndef COFF_WITH_PE 1573 /* Skip section symbols for sections which are not going to be 1574 emitted. */ 1575 if (!skip 1576 && !dont_skip_symbol 1577 && isym.n_sclass == C_STAT 1578 && isym.n_type == T_NULL 1579 && isym.n_numaux > 0 1580 && ((*secpp)->output_section == bfd_abs_section_ptr 1581 || bfd_section_removed_from_list (output_bfd, 1582 (*secpp)->output_section))) 1583 skip = TRUE; 1584 #endif 1585 1586 /* If we stripping debugging symbols, and this is a debugging 1587 symbol, then skip it. FIXME: gas sets the section to N_ABS 1588 for some types of debugging symbols; I don't know if this is 1589 a bug or not. In any case, we handle it here. */ 1590 if (! skip 1591 && flaginfo->info->strip == strip_debugger 1592 && ! dont_skip_symbol 1593 && (isym.n_scnum == N_DEBUG 1594 || (isym.n_scnum == N_ABS 1595 && (isym.n_sclass == C_AUTO 1596 || isym.n_sclass == C_REG 1597 || isym.n_sclass == C_MOS 1598 || isym.n_sclass == C_MOE 1599 || isym.n_sclass == C_MOU 1600 || isym.n_sclass == C_ARG 1601 || isym.n_sclass == C_REGPARM 1602 || isym.n_sclass == C_FIELD 1603 || isym.n_sclass == C_EOS)))) 1604 skip = TRUE; 1605 1606 /* If some symbols are stripped based on the name, work out the 1607 name and decide whether to skip this symbol. */ 1608 if (! skip 1609 && (flaginfo->info->strip == strip_some 1610 || flaginfo->info->discard == discard_l)) 1611 { 1612 const char *name; 1613 char buf[SYMNMLEN + 1]; 1614 1615 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf); 1616 if (name == NULL) 1617 return FALSE; 1618 1619 if (! dont_skip_symbol 1620 && ((flaginfo->info->strip == strip_some 1621 && (bfd_hash_lookup (flaginfo->info->keep_hash, name, FALSE, 1622 FALSE) == NULL)) 1623 || (! global 1624 && flaginfo->info->discard == discard_l 1625 && bfd_is_local_label_name (input_bfd, name)))) 1626 skip = TRUE; 1627 } 1628 1629 /* If this is an enum, struct, or union tag, see if we have 1630 already output an identical type. */ 1631 if (! skip 1632 && !flaginfo->info->traditional_format 1633 && (isym.n_sclass == C_ENTAG 1634 || isym.n_sclass == C_STRTAG 1635 || isym.n_sclass == C_UNTAG) 1636 && isym.n_numaux == 1) 1637 { 1638 const char *name; 1639 char buf[SYMNMLEN + 1]; 1640 struct coff_debug_merge_hash_entry *mh; 1641 struct coff_debug_merge_type *mt; 1642 union internal_auxent aux; 1643 struct coff_debug_merge_element **epp; 1644 bfd_byte *esl, *eslend; 1645 struct internal_syment *islp; 1646 bfd_size_type amt; 1647 1648 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf); 1649 if (name == NULL) 1650 return FALSE; 1651 1652 /* Ignore fake names invented by compiler; treat them all as 1653 the same name. */ 1654 if (*name == '~' || *name == '.' || *name == '$' 1655 || (*name == bfd_get_symbol_leading_char (input_bfd) 1656 && (name[1] == '~' || name[1] == '.' || name[1] == '$'))) 1657 name = ""; 1658 1659 mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name, 1660 TRUE, TRUE); 1661 if (mh == NULL) 1662 return FALSE; 1663 1664 /* Allocate memory to hold type information. If this turns 1665 out to be a duplicate, we pass this address to 1666 bfd_release. */ 1667 amt = sizeof (struct coff_debug_merge_type); 1668 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt); 1669 if (mt == NULL) 1670 return FALSE; 1671 mt->type_class = isym.n_sclass; 1672 1673 /* Pick up the aux entry, which points to the end of the tag 1674 entries. */ 1675 bfd_coff_swap_aux_in (input_bfd, (esym + isymesz), 1676 isym.n_type, isym.n_sclass, 0, isym.n_numaux, 1677 &aux); 1678 1679 /* Gather the elements. */ 1680 epp = &mt->elements; 1681 mt->elements = NULL; 1682 islp = isymp + 2; 1683 esl = esym + 2 * isymesz; 1684 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd) 1685 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz); 1686 while (esl < eslend) 1687 { 1688 const char *elename; 1689 char elebuf[SYMNMLEN + 1]; 1690 char *name_copy; 1691 1692 bfd_coff_swap_sym_in (input_bfd, esl, islp); 1693 1694 amt = sizeof (struct coff_debug_merge_element); 1695 *epp = (struct coff_debug_merge_element *) 1696 bfd_alloc (input_bfd, amt); 1697 if (*epp == NULL) 1698 return FALSE; 1699 1700 elename = _bfd_coff_internal_syment_name (input_bfd, islp, 1701 elebuf); 1702 if (elename == NULL) 1703 return FALSE; 1704 1705 amt = strlen (elename) + 1; 1706 name_copy = (char *) bfd_alloc (input_bfd, amt); 1707 if (name_copy == NULL) 1708 return FALSE; 1709 strcpy (name_copy, elename); 1710 1711 (*epp)->name = name_copy; 1712 (*epp)->type = islp->n_type; 1713 (*epp)->tagndx = 0; 1714 if (islp->n_numaux >= 1 1715 && islp->n_type != T_NULL 1716 && islp->n_sclass != C_EOS) 1717 { 1718 union internal_auxent eleaux; 1719 long indx; 1720 1721 bfd_coff_swap_aux_in (input_bfd, (esl + isymesz), 1722 islp->n_type, islp->n_sclass, 0, 1723 islp->n_numaux, &eleaux); 1724 indx = eleaux.x_sym.x_tagndx.l; 1725 1726 /* FIXME: If this tagndx entry refers to a symbol 1727 defined later in this file, we just ignore it. 1728 Handling this correctly would be tedious, and may 1729 not be required. */ 1730 if (indx > 0 1731 && (indx 1732 < ((esym - 1733 (bfd_byte *) obj_coff_external_syms (input_bfd)) 1734 / (long) isymesz))) 1735 { 1736 (*epp)->tagndx = flaginfo->sym_indices[indx]; 1737 if ((*epp)->tagndx < 0) 1738 (*epp)->tagndx = 0; 1739 } 1740 } 1741 epp = &(*epp)->next; 1742 *epp = NULL; 1743 1744 esl += (islp->n_numaux + 1) * isymesz; 1745 islp += islp->n_numaux + 1; 1746 } 1747 1748 /* See if we already have a definition which matches this 1749 type. We always output the type if it has no elements, 1750 for simplicity. */ 1751 if (mt->elements == NULL) 1752 bfd_release (input_bfd, mt); 1753 else 1754 { 1755 struct coff_debug_merge_type *mtl; 1756 1757 for (mtl = mh->types; mtl != NULL; mtl = mtl->next) 1758 { 1759 struct coff_debug_merge_element *me, *mel; 1760 1761 if (mtl->type_class != mt->type_class) 1762 continue; 1763 1764 for (me = mt->elements, mel = mtl->elements; 1765 me != NULL && mel != NULL; 1766 me = me->next, mel = mel->next) 1767 { 1768 if (strcmp (me->name, mel->name) != 0 1769 || me->type != mel->type 1770 || me->tagndx != mel->tagndx) 1771 break; 1772 } 1773 1774 if (me == NULL && mel == NULL) 1775 break; 1776 } 1777 1778 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base) 1779 { 1780 /* This is the first definition of this type. */ 1781 mt->indx = output_index; 1782 mt->next = mh->types; 1783 mh->types = mt; 1784 } 1785 else 1786 { 1787 /* This is a redefinition which can be merged. */ 1788 bfd_release (input_bfd, mt); 1789 *indexp = mtl->indx; 1790 add = (eslend - esym) / isymesz; 1791 skip = TRUE; 1792 } 1793 } 1794 } 1795 1796 /* We now know whether we are to skip this symbol or not. */ 1797 if (! skip) 1798 { 1799 /* Adjust the symbol in order to output it. */ 1800 1801 if (isym._n._n_n._n_zeroes == 0 1802 && isym._n._n_n._n_offset != 0) 1803 { 1804 const char *name; 1805 bfd_size_type indx; 1806 1807 /* This symbol has a long name. Enter it in the string 1808 table we are building. Note that we do not check 1809 bfd_coff_symname_in_debug. That is only true for 1810 XCOFF, and XCOFF requires different linking code 1811 anyhow. */ 1812 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL); 1813 if (name == NULL) 1814 return FALSE; 1815 indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy); 1816 if (indx == (bfd_size_type) -1) 1817 return FALSE; 1818 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; 1819 } 1820 1821 switch (isym.n_sclass) 1822 { 1823 case C_AUTO: 1824 case C_MOS: 1825 case C_EOS: 1826 case C_MOE: 1827 case C_MOU: 1828 case C_UNTAG: 1829 case C_STRTAG: 1830 case C_ENTAG: 1831 case C_TPDEF: 1832 case C_ARG: 1833 case C_USTATIC: 1834 case C_REG: 1835 case C_REGPARM: 1836 case C_FIELD: 1837 /* The symbol value should not be modified. */ 1838 break; 1839 1840 case C_FCN: 1841 if (obj_pe (input_bfd) 1842 && memcmp (isym.n_name, ".bf", sizeof ".bf") != 0 1843 && isym.n_scnum > 0) 1844 { 1845 /* For PE, .lf and .ef get their value left alone, 1846 while .bf gets relocated. However, they all have 1847 "real" section numbers, and need to be moved into 1848 the new section. */ 1849 isym.n_scnum = (*secpp)->output_section->target_index; 1850 break; 1851 } 1852 /* Fall through. */ 1853 default: 1854 case C_LABEL: /* Not completely sure about these 2 */ 1855 case C_EXTDEF: 1856 case C_BLOCK: 1857 case C_EFCN: 1858 case C_NULL: 1859 case C_EXT: 1860 case C_STAT: 1861 case C_SECTION: 1862 case C_NT_WEAK: 1863 /* Compute new symbol location. */ 1864 if (isym.n_scnum > 0) 1865 { 1866 isym.n_scnum = (*secpp)->output_section->target_index; 1867 isym.n_value += (*secpp)->output_offset; 1868 if (! obj_pe (input_bfd)) 1869 isym.n_value -= (*secpp)->vma; 1870 if (! obj_pe (flaginfo->output_bfd)) 1871 isym.n_value += (*secpp)->output_section->vma; 1872 } 1873 break; 1874 1875 case C_FILE: 1876 /* The value of a C_FILE symbol is the symbol index of 1877 the next C_FILE symbol. The value of the last C_FILE 1878 symbol is the symbol index to the first external 1879 symbol (actually, coff_renumber_symbols does not get 1880 this right--it just sets the value of the last C_FILE 1881 symbol to zero--and nobody has ever complained about 1882 it). We try to get this right, below, just before we 1883 write the symbols out, but in the general case we may 1884 have to write the symbol out twice. */ 1885 if (flaginfo->last_file_index != -1 1886 && flaginfo->last_file.n_value != (bfd_vma) output_index) 1887 { 1888 /* We must correct the value of the last C_FILE 1889 entry. */ 1890 flaginfo->last_file.n_value = output_index; 1891 if ((bfd_size_type) flaginfo->last_file_index >= syment_base) 1892 { 1893 /* The last C_FILE symbol is in this input file. */ 1894 bfd_coff_swap_sym_out (output_bfd, 1895 &flaginfo->last_file, 1896 (flaginfo->outsyms 1897 + ((flaginfo->last_file_index 1898 - syment_base) 1899 * osymesz))); 1900 } 1901 else 1902 { 1903 file_ptr pos; 1904 1905 /* We have already written out the last C_FILE 1906 symbol. We need to write it out again. We 1907 borrow *outsym temporarily. */ 1908 bfd_coff_swap_sym_out (output_bfd, 1909 &flaginfo->last_file, outsym); 1910 pos = obj_sym_filepos (output_bfd); 1911 pos += flaginfo->last_file_index * osymesz; 1912 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 1913 || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz) 1914 return FALSE; 1915 } 1916 } 1917 1918 flaginfo->last_file_index = output_index; 1919 flaginfo->last_file = isym; 1920 break; 1921 } 1922 1923 /* If doing task linking, convert normal global function symbols to 1924 static functions. */ 1925 if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym)) 1926 isym.n_sclass = C_STAT; 1927 1928 /* Output the symbol. */ 1929 bfd_coff_swap_sym_out (output_bfd, &isym, outsym); 1930 1931 *indexp = output_index; 1932 1933 if (global) 1934 { 1935 long indx; 1936 struct coff_link_hash_entry *h; 1937 1938 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd)) 1939 / isymesz); 1940 h = obj_coff_sym_hashes (input_bfd)[indx]; 1941 if (h == NULL) 1942 { 1943 /* This can happen if there were errors earlier in 1944 the link. */ 1945 bfd_set_error (bfd_error_bad_value); 1946 return FALSE; 1947 } 1948 h->indx = output_index; 1949 } 1950 1951 output_index += add; 1952 outsym += add * osymesz; 1953 } 1954 1955 esym += add * isymesz; 1956 isymp += add; 1957 ++secpp; 1958 ++indexp; 1959 for (--add; add > 0; --add) 1960 { 1961 *secpp++ = NULL; 1962 *indexp++ = -1; 1963 } 1964 } 1965 1966 /* Fix up the aux entries. This must be done in a separate pass, 1967 because we don't know the correct symbol indices until we have 1968 already decided which symbols we are going to keep. */ 1969 esym = (bfd_byte *) obj_coff_external_syms (input_bfd); 1970 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; 1971 isymp = flaginfo->internal_syms; 1972 indexp = flaginfo->sym_indices; 1973 sym_hash = obj_coff_sym_hashes (input_bfd); 1974 outsym = flaginfo->outsyms; 1975 1976 while (esym < esym_end) 1977 { 1978 int add; 1979 1980 add = 1 + isymp->n_numaux; 1981 1982 if ((*indexp < 0 1983 || (bfd_size_type) *indexp < syment_base) 1984 && (*sym_hash == NULL 1985 || (*sym_hash)->auxbfd != input_bfd)) 1986 esym += add * isymesz; 1987 else 1988 { 1989 struct coff_link_hash_entry *h; 1990 int i; 1991 1992 h = NULL; 1993 if (*indexp < 0) 1994 { 1995 h = *sym_hash; 1996 1997 /* The m68k-motorola-sysv assembler will sometimes 1998 generate two symbols with the same name, but only one 1999 will have aux entries. */ 2000 BFD_ASSERT (isymp->n_numaux == 0 2001 || h->numaux == 0 2002 || h->numaux == isymp->n_numaux); 2003 } 2004 2005 esym += isymesz; 2006 2007 if (h == NULL) 2008 outsym += osymesz; 2009 2010 /* Handle the aux entries. This handling is based on 2011 coff_pointerize_aux. I don't know if it always correct. */ 2012 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++) 2013 { 2014 union internal_auxent aux; 2015 union internal_auxent *auxp; 2016 2017 if (h != NULL && h->aux != NULL && (h->numaux > i)) 2018 auxp = h->aux + i; 2019 else 2020 { 2021 bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type, 2022 isymp->n_sclass, i, isymp->n_numaux, &aux); 2023 auxp = &aux; 2024 } 2025 2026 if (isymp->n_sclass == C_FILE) 2027 { 2028 /* If this is a long filename, we must put it in the 2029 string table. */ 2030 if (auxp->x_file.x_n.x_zeroes == 0 2031 && auxp->x_file.x_n.x_offset != 0) 2032 { 2033 const char *filename; 2034 bfd_size_type indx; 2035 2036 BFD_ASSERT (auxp->x_file.x_n.x_offset 2037 >= STRING_SIZE_SIZE); 2038 if (strings == NULL) 2039 { 2040 strings = _bfd_coff_read_string_table (input_bfd); 2041 if (strings == NULL) 2042 return FALSE; 2043 } 2044 if ((bfd_size_type) auxp->x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd)) 2045 filename = _("<corrupt>"); 2046 else 2047 filename = strings + auxp->x_file.x_n.x_offset; 2048 indx = _bfd_stringtab_add (flaginfo->strtab, filename, 2049 hash, copy); 2050 if (indx == (bfd_size_type) -1) 2051 return FALSE; 2052 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx; 2053 } 2054 } 2055 else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL) 2056 && isymp->n_sclass != C_NT_WEAK) 2057 { 2058 unsigned long indx; 2059 2060 if (ISFCN (isymp->n_type) 2061 || ISTAG (isymp->n_sclass) 2062 || isymp->n_sclass == C_BLOCK 2063 || isymp->n_sclass == C_FCN) 2064 { 2065 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l; 2066 if (indx > 0 2067 && indx < obj_raw_syment_count (input_bfd)) 2068 { 2069 /* We look forward through the symbol for 2070 the index of the next symbol we are going 2071 to include. I don't know if this is 2072 entirely right. */ 2073 while ((flaginfo->sym_indices[indx] < 0 2074 || ((bfd_size_type) flaginfo->sym_indices[indx] 2075 < syment_base)) 2076 && indx < obj_raw_syment_count (input_bfd)) 2077 ++indx; 2078 if (indx >= obj_raw_syment_count (input_bfd)) 2079 indx = output_index; 2080 else 2081 indx = flaginfo->sym_indices[indx]; 2082 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx; 2083 } 2084 } 2085 2086 indx = auxp->x_sym.x_tagndx.l; 2087 if (indx > 0 && indx < obj_raw_syment_count (input_bfd)) 2088 { 2089 long symindx; 2090 2091 symindx = flaginfo->sym_indices[indx]; 2092 if (symindx < 0) 2093 auxp->x_sym.x_tagndx.l = 0; 2094 else 2095 auxp->x_sym.x_tagndx.l = symindx; 2096 } 2097 2098 /* The .bf symbols are supposed to be linked through 2099 the endndx field. We need to carry this list 2100 across object files. */ 2101 if (i == 0 2102 && h == NULL 2103 && isymp->n_sclass == C_FCN 2104 && (isymp->_n._n_n._n_zeroes != 0 2105 || isymp->_n._n_n._n_offset == 0) 2106 && isymp->_n._n_name[0] == '.' 2107 && isymp->_n._n_name[1] == 'b' 2108 && isymp->_n._n_name[2] == 'f' 2109 && isymp->_n._n_name[3] == '\0') 2110 { 2111 if (flaginfo->last_bf_index != -1) 2112 { 2113 flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l = 2114 *indexp; 2115 2116 if ((bfd_size_type) flaginfo->last_bf_index 2117 >= syment_base) 2118 { 2119 void *auxout; 2120 2121 /* The last .bf symbol is in this input 2122 file. This will only happen if the 2123 assembler did not set up the .bf 2124 endndx symbols correctly. */ 2125 auxout = (flaginfo->outsyms 2126 + ((flaginfo->last_bf_index 2127 - syment_base) 2128 * osymesz)); 2129 2130 bfd_coff_swap_aux_out (output_bfd, 2131 &flaginfo->last_bf, 2132 isymp->n_type, 2133 isymp->n_sclass, 2134 0, isymp->n_numaux, 2135 auxout); 2136 } 2137 else 2138 { 2139 file_ptr pos; 2140 2141 /* We have already written out the last 2142 .bf aux entry. We need to write it 2143 out again. We borrow *outsym 2144 temporarily. FIXME: This case should 2145 be made faster. */ 2146 bfd_coff_swap_aux_out (output_bfd, 2147 &flaginfo->last_bf, 2148 isymp->n_type, 2149 isymp->n_sclass, 2150 0, isymp->n_numaux, 2151 outsym); 2152 pos = obj_sym_filepos (output_bfd); 2153 pos += flaginfo->last_bf_index * osymesz; 2154 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 2155 || (bfd_bwrite (outsym, osymesz, output_bfd) 2156 != osymesz)) 2157 return FALSE; 2158 } 2159 } 2160 2161 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0) 2162 flaginfo->last_bf_index = -1; 2163 else 2164 { 2165 /* The endndx field of this aux entry must 2166 be updated with the symbol number of the 2167 next .bf symbol. */ 2168 flaginfo->last_bf = *auxp; 2169 flaginfo->last_bf_index = (((outsym - flaginfo->outsyms) 2170 / osymesz) 2171 + syment_base); 2172 } 2173 } 2174 } 2175 2176 if (h == NULL) 2177 { 2178 bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type, 2179 isymp->n_sclass, i, isymp->n_numaux, 2180 outsym); 2181 outsym += osymesz; 2182 } 2183 2184 esym += isymesz; 2185 } 2186 } 2187 2188 indexp += add; 2189 isymp += add; 2190 sym_hash += add; 2191 } 2192 2193 /* Relocate the line numbers, unless we are stripping them. */ 2194 if (flaginfo->info->strip == strip_none 2195 || flaginfo->info->strip == strip_some) 2196 { 2197 for (o = input_bfd->sections; o != NULL; o = o->next) 2198 { 2199 bfd_vma offset; 2200 bfd_byte *eline; 2201 bfd_byte *elineend; 2202 bfd_byte *oeline; 2203 bfd_boolean skipping; 2204 file_ptr pos; 2205 bfd_size_type amt; 2206 2207 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then 2208 build_link_order in ldwrite.c will not have created a 2209 link order, which means that we will not have seen this 2210 input section in _bfd_coff_final_link, which means that 2211 we will not have allocated space for the line numbers of 2212 this section. I don't think line numbers can be 2213 meaningful for a section which does not have 2214 SEC_HAS_CONTENTS set, but, if they do, this must be 2215 changed. */ 2216 if (o->lineno_count == 0 2217 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0) 2218 continue; 2219 2220 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0 2221 || bfd_bread (flaginfo->linenos, linesz * o->lineno_count, 2222 input_bfd) != linesz * o->lineno_count) 2223 return FALSE; 2224 2225 offset = o->output_section->vma + o->output_offset - o->vma; 2226 eline = flaginfo->linenos; 2227 oeline = flaginfo->linenos; 2228 elineend = eline + linesz * o->lineno_count; 2229 skipping = FALSE; 2230 for (; eline < elineend; eline += linesz) 2231 { 2232 struct internal_lineno iline; 2233 2234 bfd_coff_swap_lineno_in (input_bfd, eline, &iline); 2235 2236 if (iline.l_lnno != 0) 2237 iline.l_addr.l_paddr += offset; 2238 else if (iline.l_addr.l_symndx >= 0 2239 && ((unsigned long) iline.l_addr.l_symndx 2240 < obj_raw_syment_count (input_bfd))) 2241 { 2242 long indx; 2243 2244 indx = flaginfo->sym_indices[iline.l_addr.l_symndx]; 2245 2246 if (indx < 0) 2247 { 2248 /* These line numbers are attached to a symbol 2249 which we are stripping. We must discard the 2250 line numbers because reading them back with 2251 no associated symbol (or associating them all 2252 with symbol #0) will fail. We can't regain 2253 the space in the output file, but at least 2254 they're dense. */ 2255 skipping = TRUE; 2256 } 2257 else 2258 { 2259 struct internal_syment is; 2260 union internal_auxent ia; 2261 2262 /* Fix up the lnnoptr field in the aux entry of 2263 the symbol. It turns out that we can't do 2264 this when we modify the symbol aux entries, 2265 because gas sometimes screws up the lnnoptr 2266 field and makes it an offset from the start 2267 of the line numbers rather than an absolute 2268 file index. */ 2269 bfd_coff_swap_sym_in (output_bfd, 2270 (flaginfo->outsyms 2271 + ((indx - syment_base) 2272 * osymesz)), &is); 2273 if ((ISFCN (is.n_type) 2274 || is.n_sclass == C_BLOCK) 2275 && is.n_numaux >= 1) 2276 { 2277 void *auxptr; 2278 2279 auxptr = (flaginfo->outsyms 2280 + ((indx - syment_base + 1) 2281 * osymesz)); 2282 bfd_coff_swap_aux_in (output_bfd, auxptr, 2283 is.n_type, is.n_sclass, 2284 0, is.n_numaux, &ia); 2285 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr = 2286 (o->output_section->line_filepos 2287 + o->output_section->lineno_count * linesz 2288 + eline - flaginfo->linenos); 2289 bfd_coff_swap_aux_out (output_bfd, &ia, 2290 is.n_type, is.n_sclass, 0, 2291 is.n_numaux, auxptr); 2292 } 2293 2294 skipping = FALSE; 2295 } 2296 2297 iline.l_addr.l_symndx = indx; 2298 } 2299 2300 if (!skipping) 2301 { 2302 bfd_coff_swap_lineno_out (output_bfd, &iline, oeline); 2303 oeline += linesz; 2304 } 2305 } 2306 2307 pos = o->output_section->line_filepos; 2308 pos += o->output_section->lineno_count * linesz; 2309 amt = oeline - flaginfo->linenos; 2310 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 2311 || bfd_bwrite (flaginfo->linenos, amt, output_bfd) != amt) 2312 return FALSE; 2313 2314 o->output_section->lineno_count += amt / linesz; 2315 } 2316 } 2317 2318 /* If we swapped out a C_FILE symbol, guess that the next C_FILE 2319 symbol will be the first symbol in the next input file. In the 2320 normal case, this will save us from writing out the C_FILE symbol 2321 again. */ 2322 if (flaginfo->last_file_index != -1 2323 && (bfd_size_type) flaginfo->last_file_index >= syment_base) 2324 { 2325 flaginfo->last_file.n_value = output_index; 2326 bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file, 2327 (flaginfo->outsyms 2328 + ((flaginfo->last_file_index - syment_base) 2329 * osymesz))); 2330 } 2331 2332 /* Write the modified symbols to the output file. */ 2333 if (outsym > flaginfo->outsyms) 2334 { 2335 file_ptr pos; 2336 bfd_size_type amt; 2337 2338 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz; 2339 amt = outsym - flaginfo->outsyms; 2340 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 2341 || bfd_bwrite (flaginfo->outsyms, amt, output_bfd) != amt) 2342 return FALSE; 2343 2344 BFD_ASSERT ((obj_raw_syment_count (output_bfd) 2345 + (outsym - flaginfo->outsyms) / osymesz) 2346 == output_index); 2347 2348 obj_raw_syment_count (output_bfd) = output_index; 2349 } 2350 2351 /* Relocate the contents of each section. */ 2352 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx; 2353 for (o = input_bfd->sections; o != NULL; o = o->next) 2354 { 2355 bfd_byte *contents; 2356 struct coff_section_tdata *secdata; 2357 2358 if (! o->linker_mark) 2359 /* This section was omitted from the link. */ 2360 continue; 2361 2362 if ((o->flags & SEC_LINKER_CREATED) != 0) 2363 continue; 2364 2365 if ((o->flags & SEC_HAS_CONTENTS) == 0 2366 || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) 2367 { 2368 if ((o->flags & SEC_RELOC) != 0 2369 && o->reloc_count != 0) 2370 { 2371 _bfd_error_handler 2372 /* xgettext: c-format */ 2373 (_("%pB: relocs in section `%pA', but it has no contents"), 2374 input_bfd, o); 2375 bfd_set_error (bfd_error_no_contents); 2376 return FALSE; 2377 } 2378 2379 continue; 2380 } 2381 2382 secdata = coff_section_data (input_bfd, o); 2383 if (secdata != NULL && secdata->contents != NULL) 2384 contents = secdata->contents; 2385 else 2386 { 2387 contents = flaginfo->contents; 2388 if (! bfd_get_full_section_contents (input_bfd, o, &contents)) 2389 return FALSE; 2390 } 2391 2392 if ((o->flags & SEC_RELOC) != 0) 2393 { 2394 int target_index; 2395 struct internal_reloc *internal_relocs; 2396 struct internal_reloc *irel; 2397 2398 /* Read in the relocs. */ 2399 target_index = o->output_section->target_index; 2400 internal_relocs = (_bfd_coff_read_internal_relocs 2401 (input_bfd, o, FALSE, flaginfo->external_relocs, 2402 bfd_link_relocatable (flaginfo->info), 2403 (bfd_link_relocatable (flaginfo->info) 2404 ? (flaginfo->section_info[target_index].relocs 2405 + o->output_section->reloc_count) 2406 : flaginfo->internal_relocs))); 2407 if (internal_relocs == NULL 2408 && o->reloc_count > 0) 2409 return FALSE; 2410 2411 /* Run through the relocs looking for relocs against symbols 2412 coming from discarded sections and complain about them. */ 2413 irel = internal_relocs; 2414 for (; irel < &internal_relocs[o->reloc_count]; irel++) 2415 { 2416 struct coff_link_hash_entry *h; 2417 asection *ps = NULL; 2418 long symndx = irel->r_symndx; 2419 if (symndx < 0) 2420 continue; 2421 h = obj_coff_sym_hashes (input_bfd)[symndx]; 2422 if (h == NULL) 2423 continue; 2424 while (h->root.type == bfd_link_hash_indirect 2425 || h->root.type == bfd_link_hash_warning) 2426 h = (struct coff_link_hash_entry *) h->root.u.i.link; 2427 if (h->root.type == bfd_link_hash_defined 2428 || h->root.type == bfd_link_hash_defweak) 2429 ps = h->root.u.def.section; 2430 if (ps == NULL) 2431 continue; 2432 /* Complain if definition comes from an excluded section. */ 2433 if (ps->flags & SEC_EXCLUDE) 2434 (*flaginfo->info->callbacks->einfo) 2435 /* xgettext: c-format */ 2436 (_("%X`%s' referenced in section `%pA' of %pB: " 2437 "defined in discarded section `%pA' of %pB\n"), 2438 h->root.root.string, o, input_bfd, ps, ps->owner); 2439 } 2440 2441 /* Call processor specific code to relocate the section 2442 contents. */ 2443 if (! bfd_coff_relocate_section (output_bfd, flaginfo->info, 2444 input_bfd, o, 2445 contents, 2446 internal_relocs, 2447 flaginfo->internal_syms, 2448 flaginfo->sec_ptrs)) 2449 return FALSE; 2450 2451 if (bfd_link_relocatable (flaginfo->info)) 2452 { 2453 bfd_vma offset; 2454 struct internal_reloc *irelend; 2455 struct coff_link_hash_entry **rel_hash; 2456 2457 offset = o->output_section->vma + o->output_offset - o->vma; 2458 irel = internal_relocs; 2459 irelend = irel + o->reloc_count; 2460 rel_hash = (flaginfo->section_info[target_index].rel_hashes 2461 + o->output_section->reloc_count); 2462 for (; irel < irelend; irel++, rel_hash++) 2463 { 2464 struct coff_link_hash_entry *h; 2465 bfd_boolean adjusted; 2466 2467 *rel_hash = NULL; 2468 2469 /* Adjust the reloc address and symbol index. */ 2470 irel->r_vaddr += offset; 2471 2472 if (irel->r_symndx == -1) 2473 continue; 2474 2475 if (adjust_symndx) 2476 { 2477 if (! (*adjust_symndx) (output_bfd, flaginfo->info, 2478 input_bfd, o, irel, 2479 &adjusted)) 2480 return FALSE; 2481 if (adjusted) 2482 continue; 2483 } 2484 2485 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx]; 2486 if (h != NULL) 2487 { 2488 /* This is a global symbol. */ 2489 if (h->indx >= 0) 2490 irel->r_symndx = h->indx; 2491 else 2492 { 2493 /* This symbol is being written at the end 2494 of the file, and we do not yet know the 2495 symbol index. We save the pointer to the 2496 hash table entry in the rel_hash list. 2497 We set the indx field to -2 to indicate 2498 that this symbol must not be stripped. */ 2499 *rel_hash = h; 2500 h->indx = -2; 2501 } 2502 } 2503 else 2504 { 2505 long indx; 2506 2507 indx = flaginfo->sym_indices[irel->r_symndx]; 2508 if (indx != -1) 2509 irel->r_symndx = indx; 2510 else 2511 { 2512 struct internal_syment *is; 2513 const char *name; 2514 char buf[SYMNMLEN + 1]; 2515 2516 /* This reloc is against a symbol we are 2517 stripping. This should have been handled 2518 by the 'dont_skip_symbol' code in the while 2519 loop at the top of this function. */ 2520 is = flaginfo->internal_syms + irel->r_symndx; 2521 2522 name = (_bfd_coff_internal_syment_name 2523 (input_bfd, is, buf)); 2524 if (name == NULL) 2525 return FALSE; 2526 2527 (*flaginfo->info->callbacks->unattached_reloc) 2528 (flaginfo->info, name, input_bfd, o, irel->r_vaddr); 2529 } 2530 } 2531 } 2532 2533 o->output_section->reloc_count += o->reloc_count; 2534 } 2535 } 2536 2537 /* Write out the modified section contents. */ 2538 if (secdata == NULL || secdata->stab_info == NULL) 2539 { 2540 file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd); 2541 if (! bfd_set_section_contents (output_bfd, o->output_section, 2542 contents, loc, o->size)) 2543 return FALSE; 2544 } 2545 else 2546 { 2547 if (! (_bfd_write_section_stabs 2548 (output_bfd, &coff_hash_table (flaginfo->info)->stab_info, 2549 o, &secdata->stab_info, contents))) 2550 return FALSE; 2551 } 2552 } 2553 2554 if (! flaginfo->info->keep_memory 2555 && ! _bfd_coff_free_symbols (input_bfd)) 2556 return FALSE; 2557 2558 return TRUE; 2559 } 2560 2561 /* Write out a global symbol. Called via bfd_hash_traverse. */ 2562 2563 bfd_boolean 2564 _bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data) 2565 { 2566 struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh; 2567 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data; 2568 bfd *output_bfd; 2569 struct internal_syment isym; 2570 bfd_size_type symesz; 2571 unsigned int i; 2572 file_ptr pos; 2573 2574 output_bfd = flaginfo->output_bfd; 2575 2576 if (h->root.type == bfd_link_hash_warning) 2577 { 2578 h = (struct coff_link_hash_entry *) h->root.u.i.link; 2579 if (h->root.type == bfd_link_hash_new) 2580 return TRUE; 2581 } 2582 2583 if (h->indx >= 0) 2584 return TRUE; 2585 2586 if (h->indx != -2 2587 && (flaginfo->info->strip == strip_all 2588 || (flaginfo->info->strip == strip_some 2589 && (bfd_hash_lookup (flaginfo->info->keep_hash, 2590 h->root.root.string, FALSE, FALSE) 2591 == NULL)))) 2592 return TRUE; 2593 2594 switch (h->root.type) 2595 { 2596 default: 2597 case bfd_link_hash_new: 2598 case bfd_link_hash_warning: 2599 abort (); 2600 return FALSE; 2601 2602 case bfd_link_hash_undefined: 2603 case bfd_link_hash_undefweak: 2604 isym.n_scnum = N_UNDEF; 2605 isym.n_value = 0; 2606 break; 2607 2608 case bfd_link_hash_defined: 2609 case bfd_link_hash_defweak: 2610 { 2611 asection *sec; 2612 2613 sec = h->root.u.def.section->output_section; 2614 if (bfd_is_abs_section (sec)) 2615 isym.n_scnum = N_ABS; 2616 else 2617 isym.n_scnum = sec->target_index; 2618 isym.n_value = (h->root.u.def.value 2619 + h->root.u.def.section->output_offset); 2620 if (! obj_pe (flaginfo->output_bfd)) 2621 isym.n_value += sec->vma; 2622 } 2623 break; 2624 2625 case bfd_link_hash_common: 2626 isym.n_scnum = N_UNDEF; 2627 isym.n_value = h->root.u.c.size; 2628 break; 2629 2630 case bfd_link_hash_indirect: 2631 /* Just ignore these. They can't be handled anyhow. */ 2632 return TRUE; 2633 } 2634 2635 if (strlen (h->root.root.string) <= SYMNMLEN) 2636 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN); 2637 else 2638 { 2639 bfd_boolean hash; 2640 bfd_size_type indx; 2641 2642 hash = TRUE; 2643 if (flaginfo->info->traditional_format) 2644 hash = FALSE; 2645 indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash, 2646 FALSE); 2647 if (indx == (bfd_size_type) -1) 2648 { 2649 flaginfo->failed = TRUE; 2650 return FALSE; 2651 } 2652 isym._n._n_n._n_zeroes = 0; 2653 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; 2654 } 2655 2656 isym.n_sclass = h->symbol_class; 2657 isym.n_type = h->type; 2658 2659 if (isym.n_sclass == C_NULL) 2660 isym.n_sclass = C_EXT; 2661 2662 /* If doing task linking and this is the pass where we convert 2663 defined globals to statics, then do that conversion now. If the 2664 symbol is not being converted, just ignore it and it will be 2665 output during a later pass. */ 2666 if (flaginfo->global_to_static) 2667 { 2668 if (! IS_EXTERNAL (output_bfd, isym)) 2669 return TRUE; 2670 2671 isym.n_sclass = C_STAT; 2672 } 2673 2674 /* When a weak symbol is not overridden by a strong one, 2675 turn it into an external symbol when not building a 2676 shared or relocatable object. */ 2677 if (! bfd_link_pic (flaginfo->info) 2678 && ! bfd_link_relocatable (flaginfo->info) 2679 && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym)) 2680 isym.n_sclass = C_EXT; 2681 2682 isym.n_numaux = h->numaux; 2683 2684 bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms); 2685 2686 symesz = bfd_coff_symesz (output_bfd); 2687 2688 pos = obj_sym_filepos (output_bfd); 2689 pos += obj_raw_syment_count (output_bfd) * symesz; 2690 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 2691 || bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz) 2692 { 2693 flaginfo->failed = TRUE; 2694 return FALSE; 2695 } 2696 2697 h->indx = obj_raw_syment_count (output_bfd); 2698 2699 ++obj_raw_syment_count (output_bfd); 2700 2701 /* Write out any associated aux entries. Most of the aux entries 2702 will have been modified in _bfd_coff_link_input_bfd. We have to 2703 handle section aux entries here, now that we have the final 2704 relocation and line number counts. */ 2705 for (i = 0; i < isym.n_numaux; i++) 2706 { 2707 union internal_auxent *auxp; 2708 2709 auxp = h->aux + i; 2710 2711 /* Look for a section aux entry here using the same tests that 2712 coff_swap_aux_out uses. */ 2713 if (i == 0 2714 && (isym.n_sclass == C_STAT 2715 || isym.n_sclass == C_HIDDEN) 2716 && isym.n_type == T_NULL 2717 && (h->root.type == bfd_link_hash_defined 2718 || h->root.type == bfd_link_hash_defweak)) 2719 { 2720 asection *sec; 2721 2722 sec = h->root.u.def.section->output_section; 2723 if (sec != NULL) 2724 { 2725 auxp->x_scn.x_scnlen = sec->size; 2726 2727 /* For PE, an overflow on the final link reportedly does 2728 not matter. FIXME: Why not? */ 2729 if (sec->reloc_count > 0xffff 2730 && (! obj_pe (output_bfd) 2731 || bfd_link_relocatable (flaginfo->info))) 2732 _bfd_error_handler 2733 /* xgettext: c-format */ 2734 (_("%pB: %pA: reloc overflow: %#x > 0xffff"), 2735 output_bfd, sec, sec->reloc_count); 2736 2737 if (sec->lineno_count > 0xffff 2738 && (! obj_pe (output_bfd) 2739 || bfd_link_relocatable (flaginfo->info))) 2740 _bfd_error_handler 2741 /* xgettext: c-format */ 2742 (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"), 2743 output_bfd, sec, sec->lineno_count); 2744 2745 auxp->x_scn.x_nreloc = sec->reloc_count; 2746 auxp->x_scn.x_nlinno = sec->lineno_count; 2747 auxp->x_scn.x_checksum = 0; 2748 auxp->x_scn.x_associated = 0; 2749 auxp->x_scn.x_comdat = 0; 2750 } 2751 } 2752 2753 bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type, 2754 isym.n_sclass, (int) i, isym.n_numaux, 2755 flaginfo->outsyms); 2756 if (bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz) 2757 { 2758 flaginfo->failed = TRUE; 2759 return FALSE; 2760 } 2761 ++obj_raw_syment_count (output_bfd); 2762 } 2763 2764 return TRUE; 2765 } 2766 2767 /* Write out task global symbols, converting them to statics. Called 2768 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do 2769 the dirty work, if the symbol we are processing needs conversion. */ 2770 2771 bfd_boolean 2772 _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data) 2773 { 2774 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data; 2775 bfd_boolean rtnval = TRUE; 2776 bfd_boolean save_global_to_static; 2777 2778 if (h->root.type == bfd_link_hash_warning) 2779 h = (struct coff_link_hash_entry *) h->root.u.i.link; 2780 2781 if (h->indx < 0) 2782 { 2783 switch (h->root.type) 2784 { 2785 case bfd_link_hash_defined: 2786 case bfd_link_hash_defweak: 2787 save_global_to_static = flaginfo->global_to_static; 2788 flaginfo->global_to_static = TRUE; 2789 rtnval = _bfd_coff_write_global_sym (&h->root.root, data); 2790 flaginfo->global_to_static = save_global_to_static; 2791 break; 2792 default: 2793 break; 2794 } 2795 } 2796 return (rtnval); 2797 } 2798 2799 /* Handle a link order which is supposed to generate a reloc. */ 2800 2801 bfd_boolean 2802 _bfd_coff_reloc_link_order (bfd *output_bfd, 2803 struct coff_final_link_info *flaginfo, 2804 asection *output_section, 2805 struct bfd_link_order *link_order) 2806 { 2807 reloc_howto_type *howto; 2808 struct internal_reloc *irel; 2809 struct coff_link_hash_entry **rel_hash_ptr; 2810 2811 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 2812 if (howto == NULL) 2813 { 2814 bfd_set_error (bfd_error_bad_value); 2815 return FALSE; 2816 } 2817 2818 if (link_order->u.reloc.p->addend != 0) 2819 { 2820 bfd_size_type size; 2821 bfd_byte *buf; 2822 bfd_reloc_status_type rstat; 2823 bfd_boolean ok; 2824 file_ptr loc; 2825 2826 size = bfd_get_reloc_size (howto); 2827 buf = (bfd_byte *) bfd_zmalloc (size); 2828 if (buf == NULL && size != 0) 2829 return FALSE; 2830 2831 rstat = _bfd_relocate_contents (howto, output_bfd, 2832 (bfd_vma) link_order->u.reloc.p->addend,\ 2833 buf); 2834 switch (rstat) 2835 { 2836 case bfd_reloc_ok: 2837 break; 2838 default: 2839 case bfd_reloc_outofrange: 2840 abort (); 2841 case bfd_reloc_overflow: 2842 (*flaginfo->info->callbacks->reloc_overflow) 2843 (flaginfo->info, NULL, 2844 (link_order->type == bfd_section_reloc_link_order 2845 ? bfd_section_name (output_bfd, 2846 link_order->u.reloc.p->u.section) 2847 : link_order->u.reloc.p->u.name), 2848 howto->name, link_order->u.reloc.p->addend, 2849 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0); 2850 break; 2851 } 2852 loc = link_order->offset * bfd_octets_per_byte (output_bfd); 2853 ok = bfd_set_section_contents (output_bfd, output_section, buf, 2854 loc, size); 2855 free (buf); 2856 if (! ok) 2857 return FALSE; 2858 } 2859 2860 /* Store the reloc information in the right place. It will get 2861 swapped and written out at the end of the final_link routine. */ 2862 irel = (flaginfo->section_info[output_section->target_index].relocs 2863 + output_section->reloc_count); 2864 rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes 2865 + output_section->reloc_count); 2866 2867 memset (irel, 0, sizeof (struct internal_reloc)); 2868 *rel_hash_ptr = NULL; 2869 2870 irel->r_vaddr = output_section->vma + link_order->offset; 2871 2872 if (link_order->type == bfd_section_reloc_link_order) 2873 { 2874 /* We need to somehow locate a symbol in the right section. The 2875 symbol must either have a value of zero, or we must adjust 2876 the addend by the value of the symbol. FIXME: Write this 2877 when we need it. The old linker couldn't handle this anyhow. */ 2878 abort (); 2879 *rel_hash_ptr = NULL; 2880 irel->r_symndx = 0; 2881 } 2882 else 2883 { 2884 struct coff_link_hash_entry *h; 2885 2886 h = ((struct coff_link_hash_entry *) 2887 bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info, 2888 link_order->u.reloc.p->u.name, 2889 FALSE, FALSE, TRUE)); 2890 if (h != NULL) 2891 { 2892 if (h->indx >= 0) 2893 irel->r_symndx = h->indx; 2894 else 2895 { 2896 /* Set the index to -2 to force this symbol to get 2897 written out. */ 2898 h->indx = -2; 2899 *rel_hash_ptr = h; 2900 irel->r_symndx = 0; 2901 } 2902 } 2903 else 2904 { 2905 (*flaginfo->info->callbacks->unattached_reloc) 2906 (flaginfo->info, link_order->u.reloc.p->u.name, 2907 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0); 2908 irel->r_symndx = 0; 2909 } 2910 } 2911 2912 /* FIXME: Is this always right? */ 2913 irel->r_type = howto->type; 2914 2915 /* r_size is only used on the RS/6000, which needs its own linker 2916 routines anyhow. r_extern is only used for ECOFF. */ 2917 2918 /* FIXME: What is the right value for r_offset? Is zero OK? */ 2919 ++output_section->reloc_count; 2920 2921 return TRUE; 2922 } 2923 2924 /* A basic reloc handling routine which may be used by processors with 2925 simple relocs. */ 2926 2927 bfd_boolean 2928 _bfd_coff_generic_relocate_section (bfd *output_bfd, 2929 struct bfd_link_info *info, 2930 bfd *input_bfd, 2931 asection *input_section, 2932 bfd_byte *contents, 2933 struct internal_reloc *relocs, 2934 struct internal_syment *syms, 2935 asection **sections) 2936 { 2937 struct internal_reloc *rel; 2938 struct internal_reloc *relend; 2939 2940 rel = relocs; 2941 relend = rel + input_section->reloc_count; 2942 for (; rel < relend; rel++) 2943 { 2944 long symndx; 2945 struct coff_link_hash_entry *h; 2946 struct internal_syment *sym; 2947 bfd_vma addend; 2948 bfd_vma val; 2949 asection *sec; 2950 reloc_howto_type *howto; 2951 bfd_reloc_status_type rstat; 2952 2953 symndx = rel->r_symndx; 2954 2955 if (symndx == -1) 2956 { 2957 h = NULL; 2958 sym = NULL; 2959 } 2960 else if (symndx < 0 2961 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd)) 2962 { 2963 _bfd_error_handler 2964 /* xgettext: c-format */ 2965 (_("%pB: illegal symbol index %ld in relocs"), input_bfd, symndx); 2966 return FALSE; 2967 } 2968 else 2969 { 2970 h = obj_coff_sym_hashes (input_bfd)[symndx]; 2971 sym = syms + symndx; 2972 } 2973 2974 /* COFF treats common symbols in one of two ways. Either the 2975 size of the symbol is included in the section contents, or it 2976 is not. We assume that the size is not included, and force 2977 the rtype_to_howto function to adjust the addend as needed. */ 2978 if (sym != NULL && sym->n_scnum != 0) 2979 addend = - sym->n_value; 2980 else 2981 addend = 0; 2982 2983 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h, 2984 sym, &addend); 2985 if (howto == NULL) 2986 return FALSE; 2987 2988 /* If we are doing a relocatable link, then we can just ignore 2989 a PC relative reloc that is pcrel_offset. It will already 2990 have the correct value. If this is not a relocatable link, 2991 then we should ignore the symbol value. */ 2992 if (howto->pc_relative && howto->pcrel_offset) 2993 { 2994 if (bfd_link_relocatable (info)) 2995 continue; 2996 if (sym != NULL && sym->n_scnum != 0) 2997 addend += sym->n_value; 2998 } 2999 3000 val = 0; 3001 sec = NULL; 3002 if (h == NULL) 3003 { 3004 if (symndx == -1) 3005 { 3006 sec = bfd_abs_section_ptr; 3007 val = 0; 3008 } 3009 else 3010 { 3011 sec = sections[symndx]; 3012 3013 /* PR 19623: Relocations against symbols in 3014 the absolute sections should ignored. */ 3015 if (bfd_is_abs_section (sec)) 3016 continue; 3017 3018 val = (sec->output_section->vma 3019 + sec->output_offset 3020 + sym->n_value); 3021 if (! obj_pe (input_bfd)) 3022 val -= sec->vma; 3023 } 3024 } 3025 else 3026 { 3027 if (h->root.type == bfd_link_hash_defined 3028 || h->root.type == bfd_link_hash_defweak) 3029 { 3030 /* Defined weak symbols are a GNU extension. */ 3031 sec = h->root.u.def.section; 3032 val = (h->root.u.def.value 3033 + sec->output_section->vma 3034 + sec->output_offset); 3035 } 3036 3037 else if (h->root.type == bfd_link_hash_undefweak) 3038 { 3039 if (h->symbol_class == C_NT_WEAK && h->numaux == 1) 3040 { 3041 /* See _Microsoft Portable Executable and Common Object 3042 File Format Specification_, section 5.5.3. 3043 Note that weak symbols without aux records are a GNU 3044 extension. 3045 FIXME: All weak externals are treated as having 3046 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1). 3047 These behave as per SVR4 ABI: A library member 3048 will resolve a weak external only if a normal 3049 external causes the library member to be linked. 3050 See also linker.c: generic_link_check_archive_element. */ 3051 struct coff_link_hash_entry *h2 = 3052 h->auxbfd->tdata.coff_obj_data->sym_hashes[ 3053 h->aux->x_sym.x_tagndx.l]; 3054 3055 if (!h2 || h2->root.type == bfd_link_hash_undefined) 3056 { 3057 sec = bfd_abs_section_ptr; 3058 val = 0; 3059 } 3060 else 3061 { 3062 sec = h2->root.u.def.section; 3063 val = h2->root.u.def.value 3064 + sec->output_section->vma + sec->output_offset; 3065 } 3066 } 3067 else 3068 /* This is a GNU extension. */ 3069 val = 0; 3070 } 3071 3072 else if (! bfd_link_relocatable (info)) 3073 (*info->callbacks->undefined_symbol) 3074 (info, h->root.root.string, input_bfd, input_section, 3075 rel->r_vaddr - input_section->vma, TRUE); 3076 } 3077 3078 /* If the input section defining the symbol has been discarded 3079 then zero this reloc field. */ 3080 if (sec != NULL && discarded_section (sec)) 3081 { 3082 _bfd_clear_contents (howto, input_bfd, input_section, 3083 contents + (rel->r_vaddr - input_section->vma)); 3084 continue; 3085 } 3086 3087 if (info->base_file) 3088 { 3089 /* Emit a reloc if the backend thinks it needs it. */ 3090 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto)) 3091 { 3092 /* Relocation to a symbol in a section which isn't 3093 absolute. We output the address here to a file. 3094 This file is then read by dlltool when generating the 3095 reloc section. Note that the base file is not 3096 portable between systems. We write out a bfd_vma here, 3097 and dlltool reads in a bfd_vma. */ 3098 bfd_vma addr = (rel->r_vaddr 3099 - input_section->vma 3100 + input_section->output_offset 3101 + input_section->output_section->vma); 3102 if (coff_data (output_bfd)->pe) 3103 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase; 3104 if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file) 3105 != sizeof (bfd_vma)) 3106 { 3107 bfd_set_error (bfd_error_system_call); 3108 return FALSE; 3109 } 3110 } 3111 } 3112 3113 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section, 3114 contents, 3115 rel->r_vaddr - input_section->vma, 3116 val, addend); 3117 3118 switch (rstat) 3119 { 3120 default: 3121 abort (); 3122 case bfd_reloc_ok: 3123 break; 3124 case bfd_reloc_outofrange: 3125 _bfd_error_handler 3126 /* xgettext: c-format */ 3127 (_("%pB: bad reloc address %#" PRIx64 " in section `%pA'"), 3128 input_bfd, (uint64_t) rel->r_vaddr, input_section); 3129 return FALSE; 3130 case bfd_reloc_overflow: 3131 { 3132 const char *name; 3133 char buf[SYMNMLEN + 1]; 3134 3135 if (symndx == -1) 3136 name = "*ABS*"; 3137 else if (h != NULL) 3138 name = NULL; 3139 else 3140 { 3141 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf); 3142 if (name == NULL) 3143 return FALSE; 3144 } 3145 3146 (*info->callbacks->reloc_overflow) 3147 (info, (h ? &h->root : NULL), name, howto->name, 3148 (bfd_vma) 0, input_bfd, input_section, 3149 rel->r_vaddr - input_section->vma); 3150 } 3151 } 3152 } 3153 return TRUE; 3154 } 3155