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