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