1 /* BFD backend for SunOS binaries. 2 Copyright (C) 1990-2014 Free Software Foundation, Inc. 3 Written by 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 #define TARGETNAME "a.out-sunos-big" 23 24 /* Do not "beautify" the CONCAT* macro args. Traditional C will not 25 remove whitespace added here, and thus will fail to concatenate 26 the tokens. */ 27 #define MY(OP) CONCAT2 (sparc_aout_sunos_be_,OP) 28 29 #include "sysdep.h" 30 #include "bfd.h" 31 #include "bfdlink.h" 32 #include "libaout.h" 33 34 /* ??? Where should this go? */ 35 #define MACHTYPE_OK(mtype) \ 36 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \ 37 || ((mtype) == M_SPARCLET \ 38 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \ 39 || ((mtype) == M_SPARCLITE_LE \ 40 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \ 41 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \ 42 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL)) 43 44 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound 45 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab 46 #define MY_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab 47 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound 48 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc 49 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create 50 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols 51 #define MY_add_one_symbol sunos_add_one_symbol 52 #define MY_link_dynamic_object sunos_link_dynamic_object 53 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol 54 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc 55 #define MY_finish_dynamic_link sunos_finish_dynamic_link 56 57 static bfd_boolean sunos_add_dynamic_symbols (bfd *, struct bfd_link_info *, struct external_nlist **, bfd_size_type *, char **); 58 static bfd_boolean sunos_add_one_symbol (struct bfd_link_info *, bfd *, const char *, flagword, asection *, bfd_vma, const char *, bfd_boolean, bfd_boolean, struct bfd_link_hash_entry **); 59 static bfd_boolean sunos_link_dynamic_object (struct bfd_link_info *, bfd *); 60 static bfd_boolean sunos_write_dynamic_symbol (bfd *, struct bfd_link_info *, struct aout_link_hash_entry *); 61 static bfd_boolean sunos_check_dynamic_reloc (struct bfd_link_info *, bfd *, asection *, struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *, bfd_vma *); 62 static bfd_boolean sunos_finish_dynamic_link (bfd *, struct bfd_link_info *); 63 static struct bfd_link_hash_table *sunos_link_hash_table_create (bfd *); 64 static long sunos_get_dynamic_symtab_upper_bound (bfd *); 65 static long sunos_canonicalize_dynamic_symtab (bfd *, asymbol **); 66 static long sunos_get_dynamic_reloc_upper_bound (bfd *); 67 static long sunos_canonicalize_dynamic_reloc (bfd *, arelent **, asymbol **); 68 69 /* Include the usual a.out support. */ 70 #include "aoutf1.h" 71 72 /* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro. */ 73 #undef valid 74 75 /* SunOS shared library support. We store a pointer to this structure 76 in obj_aout_dynamic_info (abfd). */ 77 78 struct sunos_dynamic_info 79 { 80 /* Whether we found any dynamic information. */ 81 bfd_boolean valid; 82 /* Dynamic information. */ 83 struct internal_sun4_dynamic_link dyninfo; 84 /* Number of dynamic symbols. */ 85 unsigned long dynsym_count; 86 /* Read in nlists for dynamic symbols. */ 87 struct external_nlist *dynsym; 88 /* asymbol structures for dynamic symbols. */ 89 aout_symbol_type *canonical_dynsym; 90 /* Read in dynamic string table. */ 91 char *dynstr; 92 /* Number of dynamic relocs. */ 93 unsigned long dynrel_count; 94 /* Read in dynamic relocs. This may be reloc_std_external or 95 reloc_ext_external. */ 96 void * dynrel; 97 /* arelent structures for dynamic relocs. */ 98 arelent *canonical_dynrel; 99 }; 100 101 /* The hash table of dynamic symbols is composed of two word entries. 102 See include/aout/sun4.h for details. */ 103 104 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD) 105 106 /* Read in the basic dynamic information. This locates the __DYNAMIC 107 structure and uses it to find the dynamic_link structure. It 108 creates and saves a sunos_dynamic_info structure. If it can't find 109 __DYNAMIC, it sets the valid field of the sunos_dynamic_info 110 structure to FALSE to avoid doing this work again. */ 111 112 static bfd_boolean 113 sunos_read_dynamic_info (bfd *abfd) 114 { 115 struct sunos_dynamic_info *info; 116 asection *dynsec; 117 bfd_vma dynoff; 118 struct external_sun4_dynamic dyninfo; 119 unsigned long dynver; 120 struct external_sun4_dynamic_link linkinfo; 121 bfd_size_type amt; 122 123 if (obj_aout_dynamic_info (abfd) != NULL) 124 return TRUE; 125 126 if ((abfd->flags & DYNAMIC) == 0) 127 { 128 bfd_set_error (bfd_error_invalid_operation); 129 return FALSE; 130 } 131 132 amt = sizeof (struct sunos_dynamic_info); 133 info = bfd_zalloc (abfd, amt); 134 if (!info) 135 return FALSE; 136 info->valid = FALSE; 137 info->dynsym = NULL; 138 info->dynstr = NULL; 139 info->canonical_dynsym = NULL; 140 info->dynrel = NULL; 141 info->canonical_dynrel = NULL; 142 obj_aout_dynamic_info (abfd) = (void *) info; 143 144 /* This code used to look for the __DYNAMIC symbol to locate the dynamic 145 linking information. 146 However this inhibits recovering the dynamic symbols from a 147 stripped object file, so blindly assume that the dynamic linking 148 information is located at the start of the data section. 149 We could verify this assumption later by looking through the dynamic 150 symbols for the __DYNAMIC symbol. */ 151 if ((abfd->flags & DYNAMIC) == 0) 152 return TRUE; 153 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (void *) &dyninfo, 154 (file_ptr) 0, 155 (bfd_size_type) sizeof dyninfo)) 156 return TRUE; 157 158 dynver = GET_WORD (abfd, dyninfo.ld_version); 159 if (dynver != 2 && dynver != 3) 160 return TRUE; 161 162 dynoff = GET_WORD (abfd, dyninfo.ld); 163 164 /* dynoff is a virtual address. It is probably always in the .data 165 section, but this code should work even if it moves. */ 166 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd))) 167 dynsec = obj_textsec (abfd); 168 else 169 dynsec = obj_datasec (abfd); 170 dynoff -= bfd_get_section_vma (abfd, dynsec); 171 if (dynoff > dynsec->size) 172 return TRUE; 173 174 /* This executable appears to be dynamically linked in a way that we 175 can understand. */ 176 if (! bfd_get_section_contents (abfd, dynsec, (void *) &linkinfo, 177 (file_ptr) dynoff, 178 (bfd_size_type) sizeof linkinfo)) 179 return TRUE; 180 181 /* Swap in the dynamic link information. */ 182 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded); 183 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need); 184 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules); 185 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got); 186 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt); 187 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel); 188 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash); 189 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab); 190 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash); 191 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets); 192 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols); 193 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size); 194 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text); 195 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz); 196 197 /* Reportedly the addresses need to be offset by the size of the 198 exec header in an NMAGIC file. */ 199 if (adata (abfd).magic == n_magic) 200 { 201 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size; 202 203 info->dyninfo.ld_need += exec_bytes_size; 204 info->dyninfo.ld_rules += exec_bytes_size; 205 info->dyninfo.ld_rel += exec_bytes_size; 206 info->dyninfo.ld_hash += exec_bytes_size; 207 info->dyninfo.ld_stab += exec_bytes_size; 208 info->dyninfo.ld_symbols += exec_bytes_size; 209 } 210 211 /* The only way to get the size of the symbol information appears to 212 be to determine the distance between it and the string table. */ 213 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab) 214 / EXTERNAL_NLIST_SIZE); 215 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE 216 == (unsigned long) (info->dyninfo.ld_symbols 217 - info->dyninfo.ld_stab)); 218 219 /* Similarly, the relocs end at the hash table. */ 220 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel) 221 / obj_reloc_entry_size (abfd)); 222 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd) 223 == (unsigned long) (info->dyninfo.ld_hash 224 - info->dyninfo.ld_rel)); 225 226 info->valid = TRUE; 227 228 return TRUE; 229 } 230 231 /* Return the amount of memory required for the dynamic symbols. */ 232 233 static long 234 sunos_get_dynamic_symtab_upper_bound (bfd *abfd) 235 { 236 struct sunos_dynamic_info *info; 237 238 if (! sunos_read_dynamic_info (abfd)) 239 return -1; 240 241 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd); 242 if (! info->valid) 243 { 244 bfd_set_error (bfd_error_no_symbols); 245 return -1; 246 } 247 248 return (info->dynsym_count + 1) * sizeof (asymbol *); 249 } 250 251 /* Read the external dynamic symbols. */ 252 253 static bfd_boolean 254 sunos_slurp_dynamic_symtab (bfd *abfd) 255 { 256 struct sunos_dynamic_info *info; 257 bfd_size_type amt; 258 259 /* Get the general dynamic information. */ 260 if (obj_aout_dynamic_info (abfd) == NULL) 261 { 262 if (! sunos_read_dynamic_info (abfd)) 263 return FALSE; 264 } 265 266 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd); 267 if (! info->valid) 268 { 269 bfd_set_error (bfd_error_no_symbols); 270 return FALSE; 271 } 272 273 /* Get the dynamic nlist structures. */ 274 if (info->dynsym == NULL) 275 { 276 amt = (bfd_size_type) info->dynsym_count * EXTERNAL_NLIST_SIZE; 277 info->dynsym = bfd_alloc (abfd, amt); 278 if (info->dynsym == NULL && info->dynsym_count != 0) 279 return FALSE; 280 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_stab, SEEK_SET) != 0 281 || bfd_bread ((void *) info->dynsym, amt, abfd) != amt) 282 { 283 if (info->dynsym != NULL) 284 { 285 bfd_release (abfd, info->dynsym); 286 info->dynsym = NULL; 287 } 288 return FALSE; 289 } 290 } 291 292 /* Get the dynamic strings. */ 293 if (info->dynstr == NULL) 294 { 295 amt = info->dyninfo.ld_symb_size; 296 info->dynstr = bfd_alloc (abfd, amt); 297 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0) 298 return FALSE; 299 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_symbols, SEEK_SET) != 0 300 || bfd_bread ((void *) info->dynstr, amt, abfd) != amt) 301 { 302 if (info->dynstr != NULL) 303 { 304 bfd_release (abfd, info->dynstr); 305 info->dynstr = NULL; 306 } 307 return FALSE; 308 } 309 } 310 311 return TRUE; 312 } 313 314 /* Read in the dynamic symbols. */ 315 316 static long 317 sunos_canonicalize_dynamic_symtab (bfd *abfd, asymbol **storage) 318 { 319 struct sunos_dynamic_info *info; 320 unsigned long i; 321 322 if (! sunos_slurp_dynamic_symtab (abfd)) 323 return -1; 324 325 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd); 326 327 #ifdef CHECK_DYNAMIC_HASH 328 /* Check my understanding of the dynamic hash table by making sure 329 that each symbol can be located in the hash table. */ 330 { 331 bfd_size_type table_size; 332 bfd_byte *table; 333 bfd_size_type i; 334 335 if (info->dyninfo.ld_buckets > info->dynsym_count) 336 abort (); 337 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash; 338 table = bfd_malloc (table_size); 339 if (table == NULL && table_size != 0) 340 abort (); 341 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_hash, SEEK_SET) != 0 342 || bfd_bread ((void *) table, table_size, abfd) != table_size) 343 abort (); 344 for (i = 0; i < info->dynsym_count; i++) 345 { 346 unsigned char *name; 347 unsigned long hash; 348 349 name = ((unsigned char *) info->dynstr 350 + GET_WORD (abfd, info->dynsym[i].e_strx)); 351 hash = 0; 352 while (*name != '\0') 353 hash = (hash << 1) + *name++; 354 hash &= 0x7fffffff; 355 hash %= info->dyninfo.ld_buckets; 356 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i) 357 { 358 hash = GET_WORD (abfd, 359 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD); 360 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE) 361 abort (); 362 } 363 } 364 free (table); 365 } 366 #endif /* CHECK_DYNAMIC_HASH */ 367 368 /* Get the asymbol structures corresponding to the dynamic nlist 369 structures. */ 370 if (info->canonical_dynsym == NULL) 371 { 372 bfd_size_type size; 373 bfd_size_type strsize = info->dyninfo.ld_symb_size; 374 375 size = (bfd_size_type) info->dynsym_count * sizeof (aout_symbol_type); 376 info->canonical_dynsym = bfd_alloc (abfd, size); 377 if (info->canonical_dynsym == NULL && info->dynsym_count != 0) 378 return -1; 379 380 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym, 381 info->dynsym, 382 (bfd_size_type) info->dynsym_count, 383 info->dynstr, strsize, TRUE)) 384 { 385 if (info->canonical_dynsym != NULL) 386 { 387 bfd_release (abfd, info->canonical_dynsym); 388 info->canonical_dynsym = NULL; 389 } 390 return -1; 391 } 392 } 393 394 /* Return pointers to the dynamic asymbol structures. */ 395 for (i = 0; i < info->dynsym_count; i++) 396 *storage++ = (asymbol *) (info->canonical_dynsym + i); 397 *storage = NULL; 398 399 return info->dynsym_count; 400 } 401 402 /* Return the amount of memory required for the dynamic relocs. */ 403 404 static long 405 sunos_get_dynamic_reloc_upper_bound (bfd *abfd) 406 { 407 struct sunos_dynamic_info *info; 408 409 if (! sunos_read_dynamic_info (abfd)) 410 return -1; 411 412 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd); 413 if (! info->valid) 414 { 415 bfd_set_error (bfd_error_no_symbols); 416 return -1; 417 } 418 419 return (info->dynrel_count + 1) * sizeof (arelent *); 420 } 421 422 /* Read in the dynamic relocs. */ 423 424 static long 425 sunos_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage, asymbol **syms) 426 { 427 struct sunos_dynamic_info *info; 428 unsigned long i; 429 bfd_size_type size; 430 431 /* Get the general dynamic information. */ 432 if (obj_aout_dynamic_info (abfd) == NULL) 433 { 434 if (! sunos_read_dynamic_info (abfd)) 435 return -1; 436 } 437 438 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd); 439 if (! info->valid) 440 { 441 bfd_set_error (bfd_error_no_symbols); 442 return -1; 443 } 444 445 /* Get the dynamic reloc information. */ 446 if (info->dynrel == NULL) 447 { 448 size = (bfd_size_type) info->dynrel_count * obj_reloc_entry_size (abfd); 449 info->dynrel = bfd_alloc (abfd, size); 450 if (info->dynrel == NULL && size != 0) 451 return -1; 452 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_rel, SEEK_SET) != 0 453 || bfd_bread ((void *) info->dynrel, size, abfd) != size) 454 { 455 if (info->dynrel != NULL) 456 { 457 bfd_release (abfd, info->dynrel); 458 info->dynrel = NULL; 459 } 460 return -1; 461 } 462 } 463 464 /* Get the arelent structures corresponding to the dynamic reloc 465 information. */ 466 if (info->canonical_dynrel == NULL) 467 { 468 arelent *to; 469 470 size = (bfd_size_type) info->dynrel_count * sizeof (arelent); 471 info->canonical_dynrel = bfd_alloc (abfd, size); 472 if (info->canonical_dynrel == NULL && info->dynrel_count != 0) 473 return -1; 474 475 to = info->canonical_dynrel; 476 477 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE) 478 { 479 struct reloc_ext_external *p; 480 struct reloc_ext_external *pend; 481 482 p = (struct reloc_ext_external *) info->dynrel; 483 pend = p + info->dynrel_count; 484 for (; p < pend; p++, to++) 485 NAME (aout, swap_ext_reloc_in) (abfd, p, to, syms, 486 (bfd_size_type) info->dynsym_count); 487 } 488 else 489 { 490 struct reloc_std_external *p; 491 struct reloc_std_external *pend; 492 493 p = (struct reloc_std_external *) info->dynrel; 494 pend = p + info->dynrel_count; 495 for (; p < pend; p++, to++) 496 NAME (aout, swap_std_reloc_in) (abfd, p, to, syms, 497 (bfd_size_type) info->dynsym_count); 498 } 499 } 500 501 /* Return pointers to the dynamic arelent structures. */ 502 for (i = 0; i < info->dynrel_count; i++) 503 *storage++ = info->canonical_dynrel + i; 504 *storage = NULL; 505 506 return info->dynrel_count; 507 } 508 509 /* Code to handle linking of SunOS shared libraries. */ 511 512 /* A SPARC procedure linkage table entry is 12 bytes. The first entry 513 in the table is a jump which is filled in by the runtime linker. 514 The remaining entries are branches back to the first entry, 515 followed by an index into the relocation table encoded to look like 516 a sethi of %g0. */ 517 518 #define SPARC_PLT_ENTRY_SIZE (12) 519 520 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] = 521 { 522 /* sethi %hi(0),%g1; address filled in by runtime linker. */ 523 0x3, 0, 0, 0, 524 /* jmp %g1; offset filled in by runtime linker. */ 525 0x81, 0xc0, 0x60, 0, 526 /* nop */ 527 0x1, 0, 0, 0 528 }; 529 530 /* save %sp, -96, %sp */ 531 #define SPARC_PLT_ENTRY_WORD0 ((bfd_vma) 0x9de3bfa0) 532 /* call; address filled in later. */ 533 #define SPARC_PLT_ENTRY_WORD1 ((bfd_vma) 0x40000000) 534 /* sethi; reloc index filled in later. */ 535 #define SPARC_PLT_ENTRY_WORD2 ((bfd_vma) 0x01000000) 536 537 /* This sequence is used when for the jump table entry to a defined 538 symbol in a complete executable. It is used when linking PIC 539 compiled code which is not being put into a shared library. */ 540 /* sethi <address to be filled in later>, %g1 */ 541 #define SPARC_PLT_PIC_WORD0 ((bfd_vma) 0x03000000) 542 /* jmp %g1 + <address to be filled in later> */ 543 #define SPARC_PLT_PIC_WORD1 ((bfd_vma) 0x81c06000) 544 /* nop */ 545 #define SPARC_PLT_PIC_WORD2 ((bfd_vma) 0x01000000) 546 547 /* An m68k procedure linkage table entry is 8 bytes. The first entry 548 in the table is a jump which is filled in the by the runtime 549 linker. The remaining entries are branches back to the first 550 entry, followed by a two byte index into the relocation table. */ 551 552 #define M68K_PLT_ENTRY_SIZE (8) 553 554 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] = 555 { 556 /* jmps @# */ 557 0x4e, 0xf9, 558 /* Filled in by runtime linker with a magic address. */ 559 0, 0, 0, 0, 560 /* Not used? */ 561 0, 0 562 }; 563 564 /* bsrl */ 565 #define M68K_PLT_ENTRY_WORD0 ((bfd_vma) 0x61ff) 566 /* Remaining words filled in later. */ 567 568 /* An entry in the SunOS linker hash table. */ 569 570 struct sunos_link_hash_entry 571 { 572 struct aout_link_hash_entry root; 573 574 /* If this is a dynamic symbol, this is its index into the dynamic 575 symbol table. This is initialized to -1. As the linker looks at 576 the input files, it changes this to -2 if it will be added to the 577 dynamic symbol table. After all the input files have been seen, 578 the linker will know whether to build a dynamic symbol table; if 579 it does build one, this becomes the index into the table. */ 580 long dynindx; 581 582 /* If this is a dynamic symbol, this is the index of the name in the 583 dynamic symbol string table. */ 584 long dynstr_index; 585 586 /* The offset into the global offset table used for this symbol. If 587 the symbol does not require a GOT entry, this is 0. */ 588 bfd_vma got_offset; 589 590 /* The offset into the procedure linkage table used for this symbol. 591 If the symbol does not require a PLT entry, this is 0. */ 592 bfd_vma plt_offset; 593 594 /* Some linker flags. */ 595 unsigned char flags; 596 /* Symbol is referenced by a regular object. */ 597 #define SUNOS_REF_REGULAR 01 598 /* Symbol is defined by a regular object. */ 599 #define SUNOS_DEF_REGULAR 02 600 /* Symbol is referenced by a dynamic object. */ 601 #define SUNOS_REF_DYNAMIC 04 602 /* Symbol is defined by a dynamic object. */ 603 #define SUNOS_DEF_DYNAMIC 010 604 /* Symbol is a constructor symbol in a regular object. */ 605 #define SUNOS_CONSTRUCTOR 020 606 }; 607 608 /* The SunOS linker hash table. */ 609 610 struct sunos_link_hash_table 611 { 612 struct aout_link_hash_table root; 613 614 /* The object which holds the dynamic sections. */ 615 bfd *dynobj; 616 617 /* Whether we have created the dynamic sections. */ 618 bfd_boolean dynamic_sections_created; 619 620 /* Whether we need the dynamic sections. */ 621 bfd_boolean dynamic_sections_needed; 622 623 /* Whether we need the .got table. */ 624 bfd_boolean got_needed; 625 626 /* The number of dynamic symbols. */ 627 size_t dynsymcount; 628 629 /* The number of buckets in the hash table. */ 630 size_t bucketcount; 631 632 /* The list of dynamic objects needed by dynamic objects included in 633 the link. */ 634 struct bfd_link_needed_list *needed; 635 636 /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section. */ 637 bfd_vma got_base; 638 }; 639 640 /* Routine to create an entry in an SunOS link hash table. */ 641 642 static struct bfd_hash_entry * 643 sunos_link_hash_newfunc (struct bfd_hash_entry *entry, 644 struct bfd_hash_table *table, 645 const char *string) 646 { 647 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry; 648 649 /* Allocate the structure if it has not already been allocated by a 650 subclass. */ 651 if (ret == NULL) 652 ret = bfd_hash_allocate (table, sizeof (* ret)); 653 if (ret == NULL) 654 return NULL; 655 656 /* Call the allocation method of the superclass. */ 657 ret = ((struct sunos_link_hash_entry *) 658 NAME (aout, link_hash_newfunc) ((struct bfd_hash_entry *) ret, 659 table, string)); 660 if (ret != NULL) 661 { 662 /* Set local fields. */ 663 ret->dynindx = -1; 664 ret->dynstr_index = -1; 665 ret->got_offset = 0; 666 ret->plt_offset = 0; 667 ret->flags = 0; 668 } 669 670 return (struct bfd_hash_entry *) ret; 671 } 672 673 /* Create a SunOS link hash table. */ 674 675 static struct bfd_link_hash_table * 676 sunos_link_hash_table_create (bfd *abfd) 677 { 678 struct sunos_link_hash_table *ret; 679 bfd_size_type amt = sizeof (struct sunos_link_hash_table); 680 681 ret = bfd_zmalloc (amt); 682 if (ret == NULL) 683 return NULL; 684 if (!NAME (aout, link_hash_table_init) (&ret->root, abfd, 685 sunos_link_hash_newfunc, 686 sizeof (struct sunos_link_hash_entry))) 687 { 688 free (ret); 689 return NULL; 690 } 691 692 return &ret->root.root; 693 } 694 695 /* Look up an entry in an SunOS link hash table. */ 696 697 #define sunos_link_hash_lookup(table, string, create, copy, follow) \ 698 ((struct sunos_link_hash_entry *) \ 699 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\ 700 (follow))) 701 702 /* Traverse a SunOS link hash table. */ 703 704 #define sunos_link_hash_traverse(table, func, info) \ 705 (aout_link_hash_traverse \ 706 (&(table)->root, \ 707 (bfd_boolean (*) (struct aout_link_hash_entry *, void *)) (func), \ 708 (info))) 709 710 /* Get the SunOS link hash table from the info structure. This is 711 just a cast. */ 712 713 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash)) 714 715 /* Create the dynamic sections needed if we are linking against a 716 dynamic object, or if we are linking PIC compiled code. ABFD is a 717 bfd we can attach the dynamic sections to. The linker script will 718 look for these special sections names and put them in the right 719 place in the output file. See include/aout/sun4.h for more details 720 of the dynamic linking information. */ 721 722 static bfd_boolean 723 sunos_create_dynamic_sections (bfd *abfd, 724 struct bfd_link_info *info, 725 bfd_boolean needed) 726 { 727 asection *s; 728 729 if (! sunos_hash_table (info)->dynamic_sections_created) 730 { 731 flagword flags; 732 733 sunos_hash_table (info)->dynobj = abfd; 734 735 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY 736 | SEC_LINKER_CREATED); 737 738 /* The .dynamic section holds the basic dynamic information: the 739 sun4_dynamic structure, the dynamic debugger information, and 740 the sun4_dynamic_link structure. */ 741 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); 742 if (s == NULL 743 || ! bfd_set_section_alignment (abfd, s, 2)) 744 return FALSE; 745 746 /* The .got section holds the global offset table. The address 747 is put in the ld_got field. */ 748 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); 749 if (s == NULL 750 || ! bfd_set_section_alignment (abfd, s, 2)) 751 return FALSE; 752 753 /* The .plt section holds the procedure linkage table. The 754 address is put in the ld_plt field. */ 755 s = bfd_make_section_anyway_with_flags (abfd, ".plt", flags | SEC_CODE); 756 if (s == NULL 757 || ! bfd_set_section_alignment (abfd, s, 2)) 758 return FALSE; 759 760 /* The .dynrel section holds the dynamic relocs. The address is 761 put in the ld_rel field. */ 762 s = bfd_make_section_anyway_with_flags (abfd, ".dynrel", 763 flags | SEC_READONLY); 764 if (s == NULL 765 || ! bfd_set_section_alignment (abfd, s, 2)) 766 return FALSE; 767 768 /* The .hash section holds the dynamic hash table. The address 769 is put in the ld_hash field. */ 770 s = bfd_make_section_anyway_with_flags (abfd, ".hash", 771 flags | SEC_READONLY); 772 if (s == NULL 773 || ! bfd_set_section_alignment (abfd, s, 2)) 774 return FALSE; 775 776 /* The .dynsym section holds the dynamic symbols. The address 777 is put in the ld_stab field. */ 778 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", 779 flags | SEC_READONLY); 780 if (s == NULL 781 || ! bfd_set_section_alignment (abfd, s, 2)) 782 return FALSE; 783 784 /* The .dynstr section holds the dynamic symbol string table. 785 The address is put in the ld_symbols field. */ 786 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", 787 flags | SEC_READONLY); 788 if (s == NULL 789 || ! bfd_set_section_alignment (abfd, s, 2)) 790 return FALSE; 791 792 sunos_hash_table (info)->dynamic_sections_created = TRUE; 793 } 794 795 if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed) 796 || info->shared) 797 { 798 bfd *dynobj; 799 800 dynobj = sunos_hash_table (info)->dynobj; 801 802 s = bfd_get_linker_section (dynobj, ".got"); 803 if (s->size == 0) 804 s->size = BYTES_IN_WORD; 805 806 sunos_hash_table (info)->dynamic_sections_needed = TRUE; 807 sunos_hash_table (info)->got_needed = TRUE; 808 } 809 810 return TRUE; 811 } 812 813 /* Add dynamic symbols during a link. This is called by the a.out 814 backend linker for each object it encounters. */ 815 816 static bfd_boolean 817 sunos_add_dynamic_symbols (bfd *abfd, 818 struct bfd_link_info *info, 819 struct external_nlist **symsp, 820 bfd_size_type *sym_countp, 821 char **stringsp) 822 { 823 bfd *dynobj; 824 struct sunos_dynamic_info *dinfo; 825 unsigned long need; 826 827 /* Make sure we have all the required sections. */ 828 if (info->output_bfd->xvec == abfd->xvec) 829 { 830 if (! sunos_create_dynamic_sections (abfd, info, 831 ((abfd->flags & DYNAMIC) != 0 832 && !info->relocatable))) 833 return FALSE; 834 } 835 836 /* There is nothing else to do for a normal object. */ 837 if ((abfd->flags & DYNAMIC) == 0) 838 return TRUE; 839 840 dynobj = sunos_hash_table (info)->dynobj; 841 842 /* We do not want to include the sections in a dynamic object in the 843 output file. We hack by simply clobbering the list of sections 844 in the BFD. This could be handled more cleanly by, say, a new 845 section flag; the existing SEC_NEVER_LOAD flag is not the one we 846 want, because that one still implies that the section takes up 847 space in the output file. If this is the first object we have 848 seen, we must preserve the dynamic sections we just created. */ 849 if (abfd != dynobj) 850 abfd->sections = NULL; 851 else 852 { 853 asection *s; 854 855 for (s = abfd->sections; s != NULL; s = s->next) 856 { 857 if ((s->flags & SEC_LINKER_CREATED) == 0) 858 bfd_section_list_remove (abfd, s); 859 } 860 } 861 862 /* The native linker seems to just ignore dynamic objects when -r is 863 used. */ 864 if (info->relocatable) 865 return TRUE; 866 867 /* There's no hope of using a dynamic object which does not exactly 868 match the format of the output file. */ 869 if (info->output_bfd->xvec != abfd->xvec) 870 { 871 bfd_set_error (bfd_error_invalid_operation); 872 return FALSE; 873 } 874 875 /* Make sure we have a .need and a .rules sections. These are only 876 needed if there really is a dynamic object in the link, so they 877 are not added by sunos_create_dynamic_sections. */ 878 if (bfd_get_section_by_name (dynobj, ".need") == NULL) 879 { 880 /* The .need section holds the list of names of shared objets 881 which must be included at runtime. The address of this 882 section is put in the ld_need field. */ 883 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS 884 | SEC_IN_MEMORY | SEC_READONLY); 885 asection *s = bfd_make_section_with_flags (dynobj, ".need", flags); 886 if (s == NULL 887 || ! bfd_set_section_alignment (dynobj, s, 2)) 888 return FALSE; 889 } 890 891 if (bfd_get_section_by_name (dynobj, ".rules") == NULL) 892 { 893 /* The .rules section holds the path to search for shared 894 objects. The address of this section is put in the ld_rules 895 field. */ 896 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS 897 | SEC_IN_MEMORY | SEC_READONLY); 898 asection *s = bfd_make_section_with_flags (dynobj, ".rules", flags); 899 if (s == NULL 900 || ! bfd_set_section_alignment (dynobj, s, 2)) 901 return FALSE; 902 } 903 904 /* Pick up the dynamic symbols and return them to the caller. */ 905 if (! sunos_slurp_dynamic_symtab (abfd)) 906 return FALSE; 907 908 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd); 909 *symsp = dinfo->dynsym; 910 *sym_countp = dinfo->dynsym_count; 911 *stringsp = dinfo->dynstr; 912 913 /* Record information about any other objects needed by this one. */ 914 need = dinfo->dyninfo.ld_need; 915 while (need != 0) 916 { 917 bfd_byte buf[16]; 918 unsigned long name, flags; 919 unsigned short major_vno, minor_vno; 920 struct bfd_link_needed_list *needed, **pp; 921 char *namebuf, *p; 922 bfd_size_type alc; 923 bfd_byte b; 924 char *namecopy; 925 926 if (bfd_seek (abfd, (file_ptr) need, SEEK_SET) != 0 927 || bfd_bread (buf, (bfd_size_type) 16, abfd) != 16) 928 return FALSE; 929 930 /* For the format of an ld_need entry, see aout/sun4.h. We 931 should probably define structs for this manipulation. */ 932 name = bfd_get_32 (abfd, buf); 933 flags = bfd_get_32 (abfd, buf + 4); 934 major_vno = (unsigned short) bfd_get_16 (abfd, buf + 8); 935 minor_vno = (unsigned short) bfd_get_16 (abfd, buf + 10); 936 need = bfd_get_32 (abfd, buf + 12); 937 938 alc = sizeof (struct bfd_link_needed_list); 939 needed = bfd_alloc (abfd, alc); 940 if (needed == NULL) 941 return FALSE; 942 needed->by = abfd; 943 944 /* We return the name as [-l]name[.maj][.min]. */ 945 alc = 30; 946 namebuf = bfd_malloc (alc + 1); 947 if (namebuf == NULL) 948 return FALSE; 949 p = namebuf; 950 951 if ((flags & 0x80000000) != 0) 952 { 953 *p++ = '-'; 954 *p++ = 'l'; 955 } 956 if (bfd_seek (abfd, (file_ptr) name, SEEK_SET) != 0) 957 { 958 free (namebuf); 959 return FALSE; 960 } 961 962 do 963 { 964 if (bfd_bread (&b, (bfd_size_type) 1, abfd) != 1) 965 { 966 free (namebuf); 967 return FALSE; 968 } 969 970 if ((bfd_size_type) (p - namebuf) >= alc) 971 { 972 char *n; 973 974 alc *= 2; 975 n = bfd_realloc (namebuf, alc + 1); 976 if (n == NULL) 977 { 978 free (namebuf); 979 return FALSE; 980 } 981 p = n + (p - namebuf); 982 namebuf = n; 983 } 984 985 *p++ = b; 986 } 987 while (b != '\0'); 988 989 if (major_vno == 0) 990 *p = '\0'; 991 else 992 { 993 char majbuf[30]; 994 char minbuf[30]; 995 996 sprintf (majbuf, ".%d", major_vno); 997 if (minor_vno == 0) 998 minbuf[0] = '\0'; 999 else 1000 sprintf (minbuf, ".%d", minor_vno); 1001 1002 if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc) 1003 { 1004 char *n; 1005 1006 alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf); 1007 n = bfd_realloc (namebuf, alc + 1); 1008 if (n == NULL) 1009 { 1010 free (namebuf); 1011 return FALSE; 1012 } 1013 p = n + (p - namebuf); 1014 namebuf = n; 1015 } 1016 1017 strcpy (p, majbuf); 1018 strcat (p, minbuf); 1019 } 1020 1021 namecopy = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1); 1022 if (namecopy == NULL) 1023 { 1024 free (namebuf); 1025 return FALSE; 1026 } 1027 strcpy (namecopy, namebuf); 1028 free (namebuf); 1029 needed->name = namecopy; 1030 1031 needed->next = NULL; 1032 1033 for (pp = &sunos_hash_table (info)->needed; 1034 *pp != NULL; 1035 pp = &(*pp)->next) 1036 ; 1037 *pp = needed; 1038 } 1039 1040 return TRUE; 1041 } 1042 1043 /* Function to add a single symbol to the linker hash table. This is 1044 a wrapper around _bfd_generic_link_add_one_symbol which handles the 1045 tweaking needed for dynamic linking support. */ 1046 1047 static bfd_boolean 1048 sunos_add_one_symbol (struct bfd_link_info *info, 1049 bfd *abfd, 1050 const char *name, 1051 flagword flags, 1052 asection *section, 1053 bfd_vma value, 1054 const char *string, 1055 bfd_boolean copy, 1056 bfd_boolean collect, 1057 struct bfd_link_hash_entry **hashp) 1058 { 1059 struct sunos_link_hash_entry *h; 1060 int new_flag; 1061 1062 if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0 1063 || ! bfd_is_und_section (section)) 1064 h = sunos_link_hash_lookup (sunos_hash_table (info), name, TRUE, copy, 1065 FALSE); 1066 else 1067 h = ((struct sunos_link_hash_entry *) 1068 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE)); 1069 if (h == NULL) 1070 return FALSE; 1071 1072 if (hashp != NULL) 1073 *hashp = (struct bfd_link_hash_entry *) h; 1074 1075 /* Treat a common symbol in a dynamic object as defined in the .bss 1076 section of the dynamic object. We don't want to allocate space 1077 for it in our process image. */ 1078 if ((abfd->flags & DYNAMIC) != 0 1079 && bfd_is_com_section (section)) 1080 section = obj_bsssec (abfd); 1081 1082 if (! bfd_is_und_section (section) 1083 && h->root.root.type != bfd_link_hash_new 1084 && h->root.root.type != bfd_link_hash_undefined 1085 && h->root.root.type != bfd_link_hash_defweak) 1086 { 1087 /* We are defining the symbol, and it is already defined. This 1088 is a potential multiple definition error. */ 1089 if ((abfd->flags & DYNAMIC) != 0) 1090 { 1091 /* The definition we are adding is from a dynamic object. 1092 We do not want this new definition to override the 1093 existing definition, so we pretend it is just a 1094 reference. */ 1095 section = bfd_und_section_ptr; 1096 } 1097 else if (h->root.root.type == bfd_link_hash_defined 1098 && h->root.root.u.def.section->owner != NULL 1099 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0) 1100 { 1101 /* The existing definition is from a dynamic object. We 1102 want to override it with the definition we just found. 1103 Clobber the existing definition. */ 1104 h->root.root.type = bfd_link_hash_undefined; 1105 h->root.root.u.undef.abfd = h->root.root.u.def.section->owner; 1106 } 1107 else if (h->root.root.type == bfd_link_hash_common 1108 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0) 1109 { 1110 /* The existing definition is from a dynamic object. We 1111 want to override it with the definition we just found. 1112 Clobber the existing definition. We can't set it to new, 1113 because it is on the undefined list. */ 1114 h->root.root.type = bfd_link_hash_undefined; 1115 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner; 1116 } 1117 } 1118 1119 if ((abfd->flags & DYNAMIC) != 0 1120 && abfd->xvec == info->output_bfd->xvec 1121 && (h->flags & SUNOS_CONSTRUCTOR) != 0) 1122 /* The existing symbol is a constructor symbol, and this symbol 1123 is from a dynamic object. A constructor symbol is actually a 1124 definition, although the type will be bfd_link_hash_undefined 1125 at this point. We want to ignore the definition from the 1126 dynamic object. */ 1127 section = bfd_und_section_ptr; 1128 else if ((flags & BSF_CONSTRUCTOR) != 0 1129 && (abfd->flags & DYNAMIC) == 0 1130 && h->root.root.type == bfd_link_hash_defined 1131 && h->root.root.u.def.section->owner != NULL 1132 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0) 1133 /* The existing symbol is defined by a dynamic object, and this 1134 is a constructor symbol. As above, we want to force the use 1135 of the constructor symbol from the regular object. */ 1136 h->root.root.type = bfd_link_hash_new; 1137 1138 /* Do the usual procedure for adding a symbol. */ 1139 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section, 1140 value, string, copy, collect, 1141 hashp)) 1142 return FALSE; 1143 1144 if (abfd->xvec == info->output_bfd->xvec) 1145 { 1146 /* Set a flag in the hash table entry indicating the type of 1147 reference or definition we just found. Keep a count of the 1148 number of dynamic symbols we find. A dynamic symbol is one 1149 which is referenced or defined by both a regular object and a 1150 shared object. */ 1151 if ((abfd->flags & DYNAMIC) == 0) 1152 { 1153 if (bfd_is_und_section (section)) 1154 new_flag = SUNOS_REF_REGULAR; 1155 else 1156 new_flag = SUNOS_DEF_REGULAR; 1157 } 1158 else 1159 { 1160 if (bfd_is_und_section (section)) 1161 new_flag = SUNOS_REF_DYNAMIC; 1162 else 1163 new_flag = SUNOS_DEF_DYNAMIC; 1164 } 1165 h->flags |= new_flag; 1166 1167 if (h->dynindx == -1 1168 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0) 1169 { 1170 ++sunos_hash_table (info)->dynsymcount; 1171 h->dynindx = -2; 1172 } 1173 1174 if ((flags & BSF_CONSTRUCTOR) != 0 1175 && (abfd->flags & DYNAMIC) == 0) 1176 h->flags |= SUNOS_CONSTRUCTOR; 1177 } 1178 1179 return TRUE; 1180 } 1181 1182 extern const bfd_target MY (vec); 1183 1184 /* Return the list of objects needed by BFD. */ 1185 1186 struct bfd_link_needed_list * 1187 bfd_sunos_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, 1188 struct bfd_link_info *info) 1189 { 1190 if (info->output_bfd->xvec != &MY (vec)) 1191 return NULL; 1192 return sunos_hash_table (info)->needed; 1193 } 1194 1195 /* Record an assignment made to a symbol by a linker script. We need 1196 this in case some dynamic object refers to this symbol. */ 1197 1198 bfd_boolean 1199 bfd_sunos_record_link_assignment (bfd *output_bfd, 1200 struct bfd_link_info *info, 1201 const char *name) 1202 { 1203 struct sunos_link_hash_entry *h; 1204 1205 if (output_bfd->xvec != &MY(vec)) 1206 return TRUE; 1207 1208 /* This is called after we have examined all the input objects. If 1209 the symbol does not exist, it merely means that no object refers 1210 to it, and we can just ignore it at this point. */ 1211 h = sunos_link_hash_lookup (sunos_hash_table (info), name, 1212 FALSE, FALSE, FALSE); 1213 if (h == NULL) 1214 return TRUE; 1215 1216 /* In a shared library, the __DYNAMIC symbol does not appear in the 1217 dynamic symbol table. */ 1218 if (! info->shared || strcmp (name, "__DYNAMIC") != 0) 1219 { 1220 h->flags |= SUNOS_DEF_REGULAR; 1221 1222 if (h->dynindx == -1) 1223 { 1224 ++sunos_hash_table (info)->dynsymcount; 1225 h->dynindx = -2; 1226 } 1227 } 1228 1229 return TRUE; 1230 } 1231 1232 /* Scan the relocs for an input section using standard relocs. We 1233 need to figure out what to do for each reloc against a dynamic 1234 symbol. If the symbol is in the .text section, an entry is made in 1235 the procedure linkage table. Note that this will do the wrong 1236 thing if the symbol is actually data; I don't think the Sun 3 1237 native linker handles this case correctly either. If the symbol is 1238 not in the .text section, we must preserve the reloc as a dynamic 1239 reloc. FIXME: We should also handle the PIC relocs here by 1240 building global offset table entries. */ 1241 1242 static bfd_boolean 1243 sunos_scan_std_relocs (struct bfd_link_info *info, 1244 bfd *abfd, 1245 asection *sec ATTRIBUTE_UNUSED, 1246 const struct reloc_std_external *relocs, 1247 bfd_size_type rel_size) 1248 { 1249 bfd *dynobj; 1250 asection *splt = NULL; 1251 asection *srel = NULL; 1252 struct sunos_link_hash_entry **sym_hashes; 1253 const struct reloc_std_external *rel, *relend; 1254 1255 /* We only know how to handle m68k plt entries. */ 1256 if (bfd_get_arch (abfd) != bfd_arch_m68k) 1257 { 1258 bfd_set_error (bfd_error_invalid_target); 1259 return FALSE; 1260 } 1261 1262 dynobj = NULL; 1263 1264 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd); 1265 1266 relend = relocs + rel_size / RELOC_STD_SIZE; 1267 for (rel = relocs; rel < relend; rel++) 1268 { 1269 int r_index; 1270 struct sunos_link_hash_entry *h; 1271 1272 /* We only want relocs against external symbols. */ 1273 if (bfd_header_big_endian (abfd)) 1274 { 1275 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0) 1276 continue; 1277 } 1278 else 1279 { 1280 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0) 1281 continue; 1282 } 1283 1284 /* Get the symbol index. */ 1285 if (bfd_header_big_endian (abfd)) 1286 r_index = ((rel->r_index[0] << 16) 1287 | (rel->r_index[1] << 8) 1288 | rel->r_index[2]); 1289 else 1290 r_index = ((rel->r_index[2] << 16) 1291 | (rel->r_index[1] << 8) 1292 | rel->r_index[0]); 1293 1294 /* Get the hash table entry. */ 1295 h = sym_hashes[r_index]; 1296 if (h == NULL) 1297 /* This should not normally happen, but it will in any case 1298 be caught in the relocation phase. */ 1299 continue; 1300 1301 /* At this point common symbols have already been allocated, so 1302 we don't have to worry about them. We need to consider that 1303 we may have already seen this symbol and marked it undefined; 1304 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC 1305 will be zero. */ 1306 if (h->root.root.type != bfd_link_hash_defined 1307 && h->root.root.type != bfd_link_hash_defweak 1308 && h->root.root.type != bfd_link_hash_undefined) 1309 continue; 1310 1311 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0 1312 || (h->flags & SUNOS_DEF_REGULAR) != 0) 1313 continue; 1314 1315 if (dynobj == NULL) 1316 { 1317 asection *sgot; 1318 1319 if (! sunos_create_dynamic_sections (abfd, info, FALSE)) 1320 return FALSE; 1321 dynobj = sunos_hash_table (info)->dynobj; 1322 splt = bfd_get_linker_section (dynobj, ".plt"); 1323 srel = bfd_get_linker_section (dynobj, ".dynrel"); 1324 BFD_ASSERT (splt != NULL && srel != NULL); 1325 1326 sgot = bfd_get_linker_section (dynobj, ".got"); 1327 BFD_ASSERT (sgot != NULL); 1328 if (sgot->size == 0) 1329 sgot->size = BYTES_IN_WORD; 1330 sunos_hash_table (info)->got_needed = TRUE; 1331 } 1332 1333 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0); 1334 BFD_ASSERT (h->plt_offset != 0 1335 || ((h->root.root.type == bfd_link_hash_defined 1336 || h->root.root.type == bfd_link_hash_defweak) 1337 ? (h->root.root.u.def.section->owner->flags 1338 & DYNAMIC) != 0 1339 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0)); 1340 1341 /* This reloc is against a symbol defined only by a dynamic 1342 object. */ 1343 if (h->root.root.type == bfd_link_hash_undefined) 1344 /* Presumably this symbol was marked as being undefined by 1345 an earlier reloc. */ 1346 srel->size += RELOC_STD_SIZE; 1347 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0) 1348 { 1349 bfd *sub; 1350 1351 /* This reloc is not in the .text section. It must be 1352 copied into the dynamic relocs. We mark the symbol as 1353 being undefined. */ 1354 srel->size += RELOC_STD_SIZE; 1355 sub = h->root.root.u.def.section->owner; 1356 h->root.root.type = bfd_link_hash_undefined; 1357 h->root.root.u.undef.abfd = sub; 1358 } 1359 else 1360 { 1361 /* This symbol is in the .text section. We must give it an 1362 entry in the procedure linkage table, if we have not 1363 already done so. We change the definition of the symbol 1364 to the .plt section; this will cause relocs against it to 1365 be handled correctly. */ 1366 if (h->plt_offset == 0) 1367 { 1368 if (splt->size == 0) 1369 splt->size = M68K_PLT_ENTRY_SIZE; 1370 h->plt_offset = splt->size; 1371 1372 if ((h->flags & SUNOS_DEF_REGULAR) == 0) 1373 { 1374 h->root.root.u.def.section = splt; 1375 h->root.root.u.def.value = splt->size; 1376 } 1377 1378 splt->size += M68K_PLT_ENTRY_SIZE; 1379 1380 /* We may also need a dynamic reloc entry. */ 1381 if ((h->flags & SUNOS_DEF_REGULAR) == 0) 1382 srel->size += RELOC_STD_SIZE; 1383 } 1384 } 1385 } 1386 1387 return TRUE; 1388 } 1389 1390 /* Scan the relocs for an input section using extended relocs. We 1391 need to figure out what to do for each reloc against a dynamic 1392 symbol. If the reloc is a WDISP30, and the symbol is in the .text 1393 section, an entry is made in the procedure linkage table. 1394 Otherwise, we must preserve the reloc as a dynamic reloc. */ 1395 1396 static bfd_boolean 1397 sunos_scan_ext_relocs (struct bfd_link_info *info, 1398 bfd *abfd, 1399 asection *sec ATTRIBUTE_UNUSED, 1400 const struct reloc_ext_external *relocs, 1401 bfd_size_type rel_size) 1402 { 1403 bfd *dynobj; 1404 struct sunos_link_hash_entry **sym_hashes; 1405 const struct reloc_ext_external *rel, *relend; 1406 asection *splt = NULL; 1407 asection *sgot = NULL; 1408 asection *srel = NULL; 1409 bfd_size_type amt; 1410 1411 /* We only know how to handle SPARC plt entries. */ 1412 if (bfd_get_arch (abfd) != bfd_arch_sparc) 1413 { 1414 bfd_set_error (bfd_error_invalid_target); 1415 return FALSE; 1416 } 1417 1418 dynobj = NULL; 1419 1420 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd); 1421 1422 relend = relocs + rel_size / RELOC_EXT_SIZE; 1423 for (rel = relocs; rel < relend; rel++) 1424 { 1425 unsigned int r_index; 1426 int r_extern; 1427 int r_type; 1428 struct sunos_link_hash_entry *h = NULL; 1429 1430 /* Swap in the reloc information. */ 1431 if (bfd_header_big_endian (abfd)) 1432 { 1433 r_index = ((rel->r_index[0] << 16) 1434 | (rel->r_index[1] << 8) 1435 | rel->r_index[2]); 1436 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG)); 1437 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG) 1438 >> RELOC_EXT_BITS_TYPE_SH_BIG); 1439 } 1440 else 1441 { 1442 r_index = ((rel->r_index[2] << 16) 1443 | (rel->r_index[1] << 8) 1444 | rel->r_index[0]); 1445 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE)); 1446 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE) 1447 >> RELOC_EXT_BITS_TYPE_SH_LITTLE); 1448 } 1449 1450 if (r_extern) 1451 { 1452 h = sym_hashes[r_index]; 1453 if (h == NULL) 1454 { 1455 /* This should not normally happen, but it will in any 1456 case be caught in the relocation phase. */ 1457 continue; 1458 } 1459 } 1460 1461 /* If this is a base relative reloc, we need to make an entry in 1462 the .got section. */ 1463 if (r_type == RELOC_BASE10 1464 || r_type == RELOC_BASE13 1465 || r_type == RELOC_BASE22) 1466 { 1467 if (dynobj == NULL) 1468 { 1469 if (! sunos_create_dynamic_sections (abfd, info, FALSE)) 1470 return FALSE; 1471 dynobj = sunos_hash_table (info)->dynobj; 1472 splt = bfd_get_linker_section (dynobj, ".plt"); 1473 sgot = bfd_get_linker_section (dynobj, ".got"); 1474 srel = bfd_get_linker_section (dynobj, ".dynrel"); 1475 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL); 1476 1477 /* Make sure we have an initial entry in the .got table. */ 1478 if (sgot->size == 0) 1479 sgot->size = BYTES_IN_WORD; 1480 sunos_hash_table (info)->got_needed = TRUE; 1481 } 1482 1483 if (r_extern) 1484 { 1485 if (h->got_offset != 0) 1486 continue; 1487 1488 h->got_offset = sgot->size; 1489 } 1490 else 1491 { 1492 if (r_index >= bfd_get_symcount (abfd)) 1493 /* This is abnormal, but should be caught in the 1494 relocation phase. */ 1495 continue; 1496 1497 if (adata (abfd).local_got_offsets == NULL) 1498 { 1499 amt = bfd_get_symcount (abfd); 1500 amt *= sizeof (bfd_vma); 1501 adata (abfd).local_got_offsets = bfd_zalloc (abfd, amt); 1502 if (adata (abfd).local_got_offsets == NULL) 1503 return FALSE; 1504 } 1505 1506 if (adata (abfd).local_got_offsets[r_index] != 0) 1507 continue; 1508 1509 adata (abfd).local_got_offsets[r_index] = sgot->size; 1510 } 1511 1512 sgot->size += BYTES_IN_WORD; 1513 1514 /* If we are making a shared library, or if the symbol is 1515 defined by a dynamic object, we will need a dynamic reloc 1516 entry. */ 1517 if (info->shared 1518 || (h != NULL 1519 && (h->flags & SUNOS_DEF_DYNAMIC) != 0 1520 && (h->flags & SUNOS_DEF_REGULAR) == 0)) 1521 srel->size += RELOC_EXT_SIZE; 1522 1523 continue; 1524 } 1525 1526 /* Otherwise, we are only interested in relocs against symbols 1527 defined in dynamic objects but not in regular objects. We 1528 only need to consider relocs against external symbols. */ 1529 if (! r_extern) 1530 { 1531 /* But, if we are creating a shared library, we need to 1532 generate an absolute reloc. */ 1533 if (info->shared) 1534 { 1535 if (dynobj == NULL) 1536 { 1537 if (! sunos_create_dynamic_sections (abfd, info, TRUE)) 1538 return FALSE; 1539 dynobj = sunos_hash_table (info)->dynobj; 1540 splt = bfd_get_linker_section (dynobj, ".plt"); 1541 sgot = bfd_get_linker_section (dynobj, ".got"); 1542 srel = bfd_get_linker_section (dynobj, ".dynrel"); 1543 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL); 1544 } 1545 1546 srel->size += RELOC_EXT_SIZE; 1547 } 1548 1549 continue; 1550 } 1551 1552 /* At this point common symbols have already been allocated, so 1553 we don't have to worry about them. We need to consider that 1554 we may have already seen this symbol and marked it undefined; 1555 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC 1556 will be zero. */ 1557 if (h->root.root.type != bfd_link_hash_defined 1558 && h->root.root.type != bfd_link_hash_defweak 1559 && h->root.root.type != bfd_link_hash_undefined) 1560 continue; 1561 1562 if (r_type != RELOC_JMP_TBL 1563 && ! info->shared 1564 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0 1565 || (h->flags & SUNOS_DEF_REGULAR) != 0)) 1566 continue; 1567 1568 if (r_type == RELOC_JMP_TBL 1569 && ! info->shared 1570 && (h->flags & SUNOS_DEF_DYNAMIC) == 0 1571 && (h->flags & SUNOS_DEF_REGULAR) == 0) 1572 { 1573 /* This symbol is apparently undefined. Don't do anything 1574 here; just let the relocation routine report an undefined 1575 symbol. */ 1576 continue; 1577 } 1578 1579 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0) 1580 continue; 1581 1582 if (dynobj == NULL) 1583 { 1584 if (! sunos_create_dynamic_sections (abfd, info, FALSE)) 1585 return FALSE; 1586 dynobj = sunos_hash_table (info)->dynobj; 1587 splt = bfd_get_linker_section (dynobj, ".plt"); 1588 sgot = bfd_get_linker_section (dynobj, ".got"); 1589 srel = bfd_get_linker_section (dynobj, ".dynrel"); 1590 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL); 1591 1592 /* Make sure we have an initial entry in the .got table. */ 1593 if (sgot->size == 0) 1594 sgot->size = BYTES_IN_WORD; 1595 sunos_hash_table (info)->got_needed = TRUE; 1596 } 1597 1598 BFD_ASSERT (r_type == RELOC_JMP_TBL 1599 || info->shared 1600 || (h->flags & SUNOS_REF_REGULAR) != 0); 1601 BFD_ASSERT (r_type == RELOC_JMP_TBL 1602 || info->shared 1603 || h->plt_offset != 0 1604 || ((h->root.root.type == bfd_link_hash_defined 1605 || h->root.root.type == bfd_link_hash_defweak) 1606 ? (h->root.root.u.def.section->owner->flags 1607 & DYNAMIC) != 0 1608 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0)); 1609 1610 /* This reloc is against a symbol defined only by a dynamic 1611 object, or it is a jump table reloc from PIC compiled code. */ 1612 1613 if (r_type != RELOC_JMP_TBL 1614 && h->root.root.type == bfd_link_hash_undefined) 1615 /* Presumably this symbol was marked as being undefined by 1616 an earlier reloc. */ 1617 srel->size += RELOC_EXT_SIZE; 1618 1619 else if (r_type != RELOC_JMP_TBL 1620 && (h->root.root.u.def.section->flags & SEC_CODE) == 0) 1621 { 1622 bfd *sub; 1623 1624 /* This reloc is not in the .text section. It must be 1625 copied into the dynamic relocs. We mark the symbol as 1626 being undefined. */ 1627 srel->size += RELOC_EXT_SIZE; 1628 if ((h->flags & SUNOS_DEF_REGULAR) == 0) 1629 { 1630 sub = h->root.root.u.def.section->owner; 1631 h->root.root.type = bfd_link_hash_undefined; 1632 h->root.root.u.undef.abfd = sub; 1633 } 1634 } 1635 else 1636 { 1637 /* This symbol is in the .text section. We must give it an 1638 entry in the procedure linkage table, if we have not 1639 already done so. We change the definition of the symbol 1640 to the .plt section; this will cause relocs against it to 1641 be handled correctly. */ 1642 if (h->plt_offset == 0) 1643 { 1644 if (splt->size == 0) 1645 splt->size = SPARC_PLT_ENTRY_SIZE; 1646 h->plt_offset = splt->size; 1647 1648 if ((h->flags & SUNOS_DEF_REGULAR) == 0) 1649 { 1650 if (h->root.root.type == bfd_link_hash_undefined) 1651 h->root.root.type = bfd_link_hash_defined; 1652 h->root.root.u.def.section = splt; 1653 h->root.root.u.def.value = splt->size; 1654 } 1655 1656 splt->size += SPARC_PLT_ENTRY_SIZE; 1657 1658 /* We will also need a dynamic reloc entry, unless this 1659 is a JMP_TBL reloc produced by linking PIC compiled 1660 code, and we are not making a shared library. */ 1661 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0) 1662 srel->size += RELOC_EXT_SIZE; 1663 } 1664 1665 /* If we are creating a shared library, we need to copy over 1666 any reloc other than a jump table reloc. */ 1667 if (info->shared && r_type != RELOC_JMP_TBL) 1668 srel->size += RELOC_EXT_SIZE; 1669 } 1670 } 1671 1672 return TRUE; 1673 } 1674 1675 /* Scan the relocs for an input section. */ 1676 1677 static bfd_boolean 1678 sunos_scan_relocs (struct bfd_link_info *info, 1679 bfd *abfd, 1680 asection *sec, 1681 bfd_size_type rel_size) 1682 { 1683 void * relocs; 1684 void * free_relocs = NULL; 1685 1686 if (rel_size == 0) 1687 return TRUE; 1688 1689 if (! info->keep_memory) 1690 relocs = free_relocs = bfd_malloc (rel_size); 1691 else 1692 { 1693 struct aout_section_data_struct *n; 1694 bfd_size_type amt = sizeof (struct aout_section_data_struct); 1695 1696 n = bfd_alloc (abfd, amt); 1697 if (n == NULL) 1698 relocs = NULL; 1699 else 1700 { 1701 set_aout_section_data (sec, n); 1702 relocs = bfd_malloc (rel_size); 1703 aout_section_data (sec)->relocs = relocs; 1704 } 1705 } 1706 if (relocs == NULL) 1707 return FALSE; 1708 1709 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0 1710 || bfd_bread (relocs, rel_size, abfd) != rel_size) 1711 goto error_return; 1712 1713 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE) 1714 { 1715 if (! sunos_scan_std_relocs (info, abfd, sec, 1716 (struct reloc_std_external *) relocs, 1717 rel_size)) 1718 goto error_return; 1719 } 1720 else 1721 { 1722 if (! sunos_scan_ext_relocs (info, abfd, sec, 1723 (struct reloc_ext_external *) relocs, 1724 rel_size)) 1725 goto error_return; 1726 } 1727 1728 if (free_relocs != NULL) 1729 free (free_relocs); 1730 1731 return TRUE; 1732 1733 error_return: 1734 if (free_relocs != NULL) 1735 free (free_relocs); 1736 return FALSE; 1737 } 1738 1739 /* Build the hash table of dynamic symbols, and to mark as written all 1740 symbols from dynamic objects which we do not plan to write out. */ 1741 1742 static bfd_boolean 1743 sunos_scan_dynamic_symbol (struct sunos_link_hash_entry *h, void * data) 1744 { 1745 struct bfd_link_info *info = (struct bfd_link_info *) data; 1746 1747 /* Set the written flag for symbols we do not want to write out as 1748 part of the regular symbol table. This is all symbols which are 1749 not defined in a regular object file. For some reason symbols 1750 which are referenced by a regular object and defined by a dynamic 1751 object do not seem to show up in the regular symbol table. It is 1752 possible for a symbol to have only SUNOS_REF_REGULAR set here, it 1753 is an undefined symbol which was turned into a common symbol 1754 because it was found in an archive object which was not included 1755 in the link. */ 1756 if ((h->flags & SUNOS_DEF_REGULAR) == 0 1757 && (h->flags & SUNOS_DEF_DYNAMIC) != 0 1758 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0) 1759 h->root.written = TRUE; 1760 1761 /* If this symbol is defined by a dynamic object and referenced by a 1762 regular object, see whether we gave it a reasonable value while 1763 scanning the relocs. */ 1764 if ((h->flags & SUNOS_DEF_REGULAR) == 0 1765 && (h->flags & SUNOS_DEF_DYNAMIC) != 0 1766 && (h->flags & SUNOS_REF_REGULAR) != 0) 1767 { 1768 if ((h->root.root.type == bfd_link_hash_defined 1769 || h->root.root.type == bfd_link_hash_defweak) 1770 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0) 1771 && h->root.root.u.def.section->output_section == NULL) 1772 { 1773 bfd *sub; 1774 1775 /* This symbol is currently defined in a dynamic section 1776 which is not being put into the output file. This 1777 implies that there is no reloc against the symbol. I'm 1778 not sure why this case would ever occur. In any case, we 1779 change the symbol to be undefined. */ 1780 sub = h->root.root.u.def.section->owner; 1781 h->root.root.type = bfd_link_hash_undefined; 1782 h->root.root.u.undef.abfd = sub; 1783 } 1784 } 1785 1786 /* If this symbol is defined or referenced by a regular file, add it 1787 to the dynamic symbols. */ 1788 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0) 1789 { 1790 asection *s; 1791 size_t len; 1792 bfd_byte *contents; 1793 unsigned char *name; 1794 unsigned long hash; 1795 bfd *dynobj; 1796 1797 BFD_ASSERT (h->dynindx == -2); 1798 1799 dynobj = sunos_hash_table (info)->dynobj; 1800 1801 h->dynindx = sunos_hash_table (info)->dynsymcount; 1802 ++sunos_hash_table (info)->dynsymcount; 1803 1804 len = strlen (h->root.root.root.string); 1805 1806 /* We don't bother to construct a BFD hash table for the strings 1807 which are the names of the dynamic symbols. Using a hash 1808 table for the regular symbols is beneficial, because the 1809 regular symbols includes the debugging symbols, which have 1810 long names and are often duplicated in several object files. 1811 There are no debugging symbols in the dynamic symbols. */ 1812 s = bfd_get_linker_section (dynobj, ".dynstr"); 1813 BFD_ASSERT (s != NULL); 1814 contents = bfd_realloc (s->contents, s->size + len + 1); 1815 if (contents == NULL) 1816 return FALSE; 1817 s->contents = contents; 1818 1819 h->dynstr_index = s->size; 1820 strcpy ((char *) contents + s->size, h->root.root.root.string); 1821 s->size += len + 1; 1822 1823 /* Add it to the dynamic hash table. */ 1824 name = (unsigned char *) h->root.root.root.string; 1825 hash = 0; 1826 while (*name != '\0') 1827 hash = (hash << 1) + *name++; 1828 hash &= 0x7fffffff; 1829 hash %= sunos_hash_table (info)->bucketcount; 1830 1831 s = bfd_get_linker_section (dynobj, ".hash"); 1832 BFD_ASSERT (s != NULL); 1833 1834 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1) 1835 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE); 1836 else 1837 { 1838 bfd_vma next; 1839 1840 next = GET_WORD (dynobj, 1841 (s->contents 1842 + hash * HASH_ENTRY_SIZE 1843 + BYTES_IN_WORD)); 1844 PUT_WORD (dynobj, s->size / HASH_ENTRY_SIZE, 1845 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD); 1846 PUT_WORD (dynobj, h->dynindx, s->contents + s->size); 1847 PUT_WORD (dynobj, next, s->contents + s->size + BYTES_IN_WORD); 1848 s->size += HASH_ENTRY_SIZE; 1849 } 1850 } 1851 1852 return TRUE; 1853 } 1854 1855 /* Set up the sizes and contents of the dynamic sections created in 1856 sunos_add_dynamic_symbols. This is called by the SunOS linker 1857 emulation before_allocation routine. We must set the sizes of the 1858 sections before the linker sets the addresses of the various 1859 sections. This unfortunately requires reading all the relocs so 1860 that we can work out which ones need to become dynamic relocs. If 1861 info->keep_memory is TRUE, we keep the relocs in memory; otherwise, 1862 we discard them, and will read them again later. */ 1863 1864 bfd_boolean 1865 bfd_sunos_size_dynamic_sections (bfd *output_bfd, 1866 struct bfd_link_info *info, 1867 asection **sdynptr, 1868 asection **sneedptr, 1869 asection **srulesptr) 1870 { 1871 bfd *dynobj; 1872 bfd_size_type dynsymcount; 1873 struct sunos_link_hash_entry *h; 1874 asection *s; 1875 size_t bucketcount; 1876 bfd_size_type hashalloc; 1877 size_t i; 1878 bfd *sub; 1879 1880 *sdynptr = NULL; 1881 *sneedptr = NULL; 1882 *srulesptr = NULL; 1883 1884 if (info->relocatable) 1885 return TRUE; 1886 1887 if (output_bfd->xvec != &MY(vec)) 1888 return TRUE; 1889 1890 /* Look through all the input BFD's and read their relocs. It would 1891 be better if we didn't have to do this, but there is no other way 1892 to determine the number of dynamic relocs we need, and, more 1893 importantly, there is no other way to know which symbols should 1894 get an entry in the procedure linkage table. */ 1895 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 1896 { 1897 if ((sub->flags & DYNAMIC) == 0 1898 && sub->xvec == output_bfd->xvec) 1899 { 1900 if (! sunos_scan_relocs (info, sub, obj_textsec (sub), 1901 exec_hdr (sub)->a_trsize) 1902 || ! sunos_scan_relocs (info, sub, obj_datasec (sub), 1903 exec_hdr (sub)->a_drsize)) 1904 return FALSE; 1905 } 1906 } 1907 1908 dynobj = sunos_hash_table (info)->dynobj; 1909 dynsymcount = sunos_hash_table (info)->dynsymcount; 1910 1911 /* If there were no dynamic objects in the link, and we don't need 1912 to build a global offset table, there is nothing to do here. */ 1913 if (! sunos_hash_table (info)->dynamic_sections_needed 1914 && ! sunos_hash_table (info)->got_needed) 1915 return TRUE; 1916 1917 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */ 1918 h = sunos_link_hash_lookup (sunos_hash_table (info), 1919 "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE); 1920 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0) 1921 { 1922 h->flags |= SUNOS_DEF_REGULAR; 1923 if (h->dynindx == -1) 1924 { 1925 ++sunos_hash_table (info)->dynsymcount; 1926 h->dynindx = -2; 1927 } 1928 s = bfd_get_linker_section (dynobj, ".got"); 1929 BFD_ASSERT (s != NULL); 1930 h->root.root.type = bfd_link_hash_defined; 1931 h->root.root.u.def.section = s; 1932 1933 /* If the .got section is more than 0x1000 bytes, we set 1934 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section, 1935 so that 13 bit relocations have a greater chance of working. */ 1936 if (s->size >= 0x1000) 1937 h->root.root.u.def.value = 0x1000; 1938 else 1939 h->root.root.u.def.value = 0; 1940 1941 sunos_hash_table (info)->got_base = h->root.root.u.def.value; 1942 } 1943 1944 /* If there are any shared objects in the link, then we need to set 1945 up the dynamic linking information. */ 1946 if (sunos_hash_table (info)->dynamic_sections_needed) 1947 { 1948 *sdynptr = bfd_get_linker_section (dynobj, ".dynamic"); 1949 1950 /* The .dynamic section is always the same size. */ 1951 s = *sdynptr; 1952 BFD_ASSERT (s != NULL); 1953 s->size = (sizeof (struct external_sun4_dynamic) 1954 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE 1955 + sizeof (struct external_sun4_dynamic_link)); 1956 1957 /* Set the size of the .dynsym and .hash sections. We counted 1958 the number of dynamic symbols as we read the input files. We 1959 will build the dynamic symbol table (.dynsym) and the hash 1960 table (.hash) when we build the final symbol table, because 1961 until then we do not know the correct value to give the 1962 symbols. We build the dynamic symbol string table (.dynstr) 1963 in a traversal of the symbol table using 1964 sunos_scan_dynamic_symbol. */ 1965 s = bfd_get_linker_section (dynobj, ".dynsym"); 1966 BFD_ASSERT (s != NULL); 1967 s->size = dynsymcount * sizeof (struct external_nlist); 1968 s->contents = bfd_alloc (output_bfd, s->size); 1969 if (s->contents == NULL && s->size != 0) 1970 return FALSE; 1971 1972 /* The number of buckets is just the number of symbols divided 1973 by four. To compute the final size of the hash table, we 1974 must actually compute the hash table. Normally we need 1975 exactly as many entries in the hash table as there are 1976 dynamic symbols, but if some of the buckets are not used we 1977 will need additional entries. In the worst case, every 1978 symbol will hash to the same bucket, and we will need 1979 BUCKETCOUNT - 1 extra entries. */ 1980 if (dynsymcount >= 4) 1981 bucketcount = dynsymcount / 4; 1982 else if (dynsymcount > 0) 1983 bucketcount = dynsymcount; 1984 else 1985 bucketcount = 1; 1986 s = bfd_get_linker_section (dynobj, ".hash"); 1987 BFD_ASSERT (s != NULL); 1988 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE; 1989 s->contents = bfd_zalloc (dynobj, hashalloc); 1990 if (s->contents == NULL && dynsymcount > 0) 1991 return FALSE; 1992 for (i = 0; i < bucketcount; i++) 1993 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE); 1994 s->size = bucketcount * HASH_ENTRY_SIZE; 1995 1996 sunos_hash_table (info)->bucketcount = bucketcount; 1997 1998 /* Scan all the symbols, place them in the dynamic symbol table, 1999 and build the dynamic hash table. We reuse dynsymcount as a 2000 counter for the number of symbols we have added so far. */ 2001 sunos_hash_table (info)->dynsymcount = 0; 2002 sunos_link_hash_traverse (sunos_hash_table (info), 2003 sunos_scan_dynamic_symbol, 2004 (void *) info); 2005 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount); 2006 2007 /* The SunOS native linker seems to align the total size of the 2008 symbol strings to a multiple of 8. I don't know if this is 2009 important, but it can't hurt much. */ 2010 s = bfd_get_linker_section (dynobj, ".dynstr"); 2011 BFD_ASSERT (s != NULL); 2012 if ((s->size & 7) != 0) 2013 { 2014 bfd_size_type add; 2015 bfd_byte *contents; 2016 2017 add = 8 - (s->size & 7); 2018 contents = bfd_realloc (s->contents, s->size + add); 2019 if (contents == NULL) 2020 return FALSE; 2021 memset (contents + s->size, 0, (size_t) add); 2022 s->contents = contents; 2023 s->size += add; 2024 } 2025 } 2026 2027 /* Now that we have worked out the sizes of the procedure linkage 2028 table and the dynamic relocs, allocate storage for them. */ 2029 s = bfd_get_linker_section (dynobj, ".plt"); 2030 BFD_ASSERT (s != NULL); 2031 if (s->size != 0) 2032 { 2033 s->contents = bfd_alloc (dynobj, s->size); 2034 if (s->contents == NULL) 2035 return FALSE; 2036 2037 /* Fill in the first entry in the table. */ 2038 switch (bfd_get_arch (dynobj)) 2039 { 2040 case bfd_arch_sparc: 2041 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE); 2042 break; 2043 2044 case bfd_arch_m68k: 2045 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE); 2046 break; 2047 2048 default: 2049 abort (); 2050 } 2051 } 2052 2053 s = bfd_get_linker_section (dynobj, ".dynrel"); 2054 if (s->size != 0) 2055 { 2056 s->contents = bfd_alloc (dynobj, s->size); 2057 if (s->contents == NULL) 2058 return FALSE; 2059 } 2060 /* We use the reloc_count field to keep track of how many of the 2061 relocs we have output so far. */ 2062 s->reloc_count = 0; 2063 2064 /* Make space for the global offset table. */ 2065 s = bfd_get_linker_section (dynobj, ".got"); 2066 s->contents = bfd_alloc (dynobj, s->size); 2067 if (s->contents == NULL) 2068 return FALSE; 2069 2070 *sneedptr = bfd_get_section_by_name (dynobj, ".need"); 2071 *srulesptr = bfd_get_section_by_name (dynobj, ".rules"); 2072 2073 return TRUE; 2074 } 2075 2076 /* Link a dynamic object. We actually don't have anything to do at 2077 this point. This entry point exists to prevent the regular linker 2078 code from doing anything with the object. */ 2079 2080 static bfd_boolean 2081 sunos_link_dynamic_object (struct bfd_link_info *info ATTRIBUTE_UNUSED, 2082 bfd *abfd ATTRIBUTE_UNUSED) 2083 { 2084 return TRUE; 2085 } 2086 2087 /* Write out a dynamic symbol. This is called by the final traversal 2088 over the symbol table. */ 2089 2090 static bfd_boolean 2091 sunos_write_dynamic_symbol (bfd *output_bfd, 2092 struct bfd_link_info *info, 2093 struct aout_link_hash_entry *harg) 2094 { 2095 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg; 2096 int type; 2097 bfd_vma val; 2098 asection *s; 2099 struct external_nlist *outsym; 2100 2101 /* If this symbol is in the procedure linkage table, fill in the 2102 table entry. */ 2103 if (h->plt_offset != 0) 2104 { 2105 bfd *dynobj; 2106 asection *splt; 2107 bfd_byte *p; 2108 bfd_vma r_address; 2109 2110 dynobj = sunos_hash_table (info)->dynobj; 2111 splt = bfd_get_linker_section (dynobj, ".plt"); 2112 p = splt->contents + h->plt_offset; 2113 2114 s = bfd_get_linker_section (dynobj, ".dynrel"); 2115 2116 r_address = (splt->output_section->vma 2117 + splt->output_offset 2118 + h->plt_offset); 2119 2120 switch (bfd_get_arch (output_bfd)) 2121 { 2122 case bfd_arch_sparc: 2123 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0) 2124 { 2125 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p); 2126 bfd_put_32 (output_bfd, 2127 (SPARC_PLT_ENTRY_WORD1 2128 + (((- (h->plt_offset + 4) >> 2) 2129 & 0x3fffffff))), 2130 p + 4); 2131 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count, 2132 p + 8); 2133 } 2134 else 2135 { 2136 val = (h->root.root.u.def.section->output_section->vma 2137 + h->root.root.u.def.section->output_offset 2138 + h->root.root.u.def.value); 2139 bfd_put_32 (output_bfd, 2140 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff), 2141 p); 2142 bfd_put_32 (output_bfd, 2143 SPARC_PLT_PIC_WORD1 + (val & 0x3ff), 2144 p + 4); 2145 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8); 2146 } 2147 break; 2148 2149 case bfd_arch_m68k: 2150 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0) 2151 abort (); 2152 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p); 2153 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2); 2154 bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6); 2155 r_address += 2; 2156 break; 2157 2158 default: 2159 abort (); 2160 } 2161 2162 /* We also need to add a jump table reloc, unless this is the 2163 result of a JMP_TBL reloc from PIC compiled code. */ 2164 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0) 2165 { 2166 BFD_ASSERT (h->dynindx >= 0); 2167 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) 2168 < s->size); 2169 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd); 2170 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE) 2171 { 2172 struct reloc_std_external *srel; 2173 2174 srel = (struct reloc_std_external *) p; 2175 PUT_WORD (output_bfd, r_address, srel->r_address); 2176 if (bfd_header_big_endian (output_bfd)) 2177 { 2178 srel->r_index[0] = (bfd_byte) (h->dynindx >> 16); 2179 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8); 2180 srel->r_index[2] = (bfd_byte) (h->dynindx); 2181 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG 2182 | RELOC_STD_BITS_JMPTABLE_BIG); 2183 } 2184 else 2185 { 2186 srel->r_index[2] = (bfd_byte) (h->dynindx >> 16); 2187 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8); 2188 srel->r_index[0] = (bfd_byte)h->dynindx; 2189 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE 2190 | RELOC_STD_BITS_JMPTABLE_LITTLE); 2191 } 2192 } 2193 else 2194 { 2195 struct reloc_ext_external *erel; 2196 2197 erel = (struct reloc_ext_external *) p; 2198 PUT_WORD (output_bfd, r_address, erel->r_address); 2199 if (bfd_header_big_endian (output_bfd)) 2200 { 2201 erel->r_index[0] = (bfd_byte) (h->dynindx >> 16); 2202 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8); 2203 erel->r_index[2] = (bfd_byte)h->dynindx; 2204 erel->r_type[0] = 2205 (RELOC_EXT_BITS_EXTERN_BIG 2206 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG)); 2207 } 2208 else 2209 { 2210 erel->r_index[2] = (bfd_byte) (h->dynindx >> 16); 2211 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8); 2212 erel->r_index[0] = (bfd_byte)h->dynindx; 2213 erel->r_type[0] = 2214 (RELOC_EXT_BITS_EXTERN_LITTLE 2215 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE)); 2216 } 2217 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend); 2218 } 2219 2220 ++s->reloc_count; 2221 } 2222 } 2223 2224 /* If this is not a dynamic symbol, we don't have to do anything 2225 else. We only check this after handling the PLT entry, because 2226 we can have a PLT entry for a nondynamic symbol when linking PIC 2227 compiled code from a regular object. */ 2228 if (h->dynindx < 0) 2229 return TRUE; 2230 2231 switch (h->root.root.type) 2232 { 2233 default: 2234 case bfd_link_hash_new: 2235 abort (); 2236 /* Avoid variable not initialized warnings. */ 2237 return TRUE; 2238 case bfd_link_hash_undefined: 2239 type = N_UNDF | N_EXT; 2240 val = 0; 2241 break; 2242 case bfd_link_hash_defined: 2243 case bfd_link_hash_defweak: 2244 { 2245 asection *sec; 2246 asection *output_section; 2247 2248 sec = h->root.root.u.def.section; 2249 output_section = sec->output_section; 2250 BFD_ASSERT (bfd_is_abs_section (output_section) 2251 || output_section->owner == output_bfd); 2252 if (h->plt_offset != 0 2253 && (h->flags & SUNOS_DEF_REGULAR) == 0) 2254 { 2255 type = N_UNDF | N_EXT; 2256 val = 0; 2257 } 2258 else 2259 { 2260 if (output_section == obj_textsec (output_bfd)) 2261 type = (h->root.root.type == bfd_link_hash_defined 2262 ? N_TEXT 2263 : N_WEAKT); 2264 else if (output_section == obj_datasec (output_bfd)) 2265 type = (h->root.root.type == bfd_link_hash_defined 2266 ? N_DATA 2267 : N_WEAKD); 2268 else if (output_section == obj_bsssec (output_bfd)) 2269 type = (h->root.root.type == bfd_link_hash_defined 2270 ? N_BSS 2271 : N_WEAKB); 2272 else 2273 type = (h->root.root.type == bfd_link_hash_defined 2274 ? N_ABS 2275 : N_WEAKA); 2276 type |= N_EXT; 2277 val = (h->root.root.u.def.value 2278 + output_section->vma 2279 + sec->output_offset); 2280 } 2281 } 2282 break; 2283 case bfd_link_hash_common: 2284 type = N_UNDF | N_EXT; 2285 val = h->root.root.u.c.size; 2286 break; 2287 case bfd_link_hash_undefweak: 2288 type = N_WEAKU; 2289 val = 0; 2290 break; 2291 case bfd_link_hash_indirect: 2292 case bfd_link_hash_warning: 2293 /* FIXME: Ignore these for now. The circumstances under which 2294 they should be written out are not clear to me. */ 2295 return TRUE; 2296 } 2297 2298 s = bfd_get_linker_section (sunos_hash_table (info)->dynobj, ".dynsym"); 2299 BFD_ASSERT (s != NULL); 2300 outsym = ((struct external_nlist *) 2301 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE)); 2302 2303 H_PUT_8 (output_bfd, type, outsym->e_type); 2304 H_PUT_8 (output_bfd, 0, outsym->e_other); 2305 2306 /* FIXME: The native linker doesn't use 0 for desc. It seems to use 2307 one less than the desc value in the shared library, although that 2308 seems unlikely. */ 2309 H_PUT_16 (output_bfd, 0, outsym->e_desc); 2310 2311 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx); 2312 PUT_WORD (output_bfd, val, outsym->e_value); 2313 2314 return TRUE; 2315 } 2316 2317 /* This is called for each reloc against an external symbol. If this 2318 is a reloc which are are going to copy as a dynamic reloc, then 2319 copy it over, and tell the caller to not bother processing this 2320 reloc. */ 2321 2322 static bfd_boolean 2323 sunos_check_dynamic_reloc (struct bfd_link_info *info, 2324 bfd *input_bfd, 2325 asection *input_section, 2326 struct aout_link_hash_entry *harg, 2327 void * reloc, 2328 bfd_byte *contents ATTRIBUTE_UNUSED, 2329 bfd_boolean *skip, 2330 bfd_vma *relocationp) 2331 { 2332 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg; 2333 bfd *dynobj; 2334 bfd_boolean baserel; 2335 bfd_boolean jmptbl; 2336 bfd_boolean pcrel; 2337 asection *s; 2338 bfd_byte *p; 2339 long indx; 2340 2341 *skip = FALSE; 2342 2343 dynobj = sunos_hash_table (info)->dynobj; 2344 2345 if (h != NULL 2346 && h->plt_offset != 0 2347 && (info->shared 2348 || (h->flags & SUNOS_DEF_REGULAR) == 0)) 2349 { 2350 asection *splt; 2351 2352 /* Redirect the relocation to the PLT entry. */ 2353 splt = bfd_get_linker_section (dynobj, ".plt"); 2354 *relocationp = (splt->output_section->vma 2355 + splt->output_offset 2356 + h->plt_offset); 2357 } 2358 2359 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE) 2360 { 2361 struct reloc_std_external *srel; 2362 2363 srel = (struct reloc_std_external *) reloc; 2364 if (bfd_header_big_endian (input_bfd)) 2365 { 2366 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG)); 2367 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG)); 2368 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG)); 2369 } 2370 else 2371 { 2372 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE)); 2373 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE)); 2374 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE)); 2375 } 2376 } 2377 else 2378 { 2379 struct reloc_ext_external *erel; 2380 int r_type; 2381 2382 erel = (struct reloc_ext_external *) reloc; 2383 if (bfd_header_big_endian (input_bfd)) 2384 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG) 2385 >> RELOC_EXT_BITS_TYPE_SH_BIG); 2386 else 2387 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE) 2388 >> RELOC_EXT_BITS_TYPE_SH_LITTLE); 2389 baserel = (r_type == RELOC_BASE10 2390 || r_type == RELOC_BASE13 2391 || r_type == RELOC_BASE22); 2392 jmptbl = r_type == RELOC_JMP_TBL; 2393 pcrel = (r_type == RELOC_DISP8 2394 || r_type == RELOC_DISP16 2395 || r_type == RELOC_DISP32 2396 || r_type == RELOC_WDISP30 2397 || r_type == RELOC_WDISP22); 2398 /* We don't consider the PC10 and PC22 types to be PC relative, 2399 because they are pcrel_offset. */ 2400 } 2401 2402 if (baserel) 2403 { 2404 bfd_vma *got_offsetp; 2405 asection *sgot; 2406 2407 if (h != NULL) 2408 got_offsetp = &h->got_offset; 2409 else if (adata (input_bfd).local_got_offsets == NULL) 2410 got_offsetp = NULL; 2411 else 2412 { 2413 struct reloc_std_external *srel; 2414 int r_index; 2415 2416 srel = (struct reloc_std_external *) reloc; 2417 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE) 2418 { 2419 if (bfd_header_big_endian (input_bfd)) 2420 r_index = ((srel->r_index[0] << 16) 2421 | (srel->r_index[1] << 8) 2422 | srel->r_index[2]); 2423 else 2424 r_index = ((srel->r_index[2] << 16) 2425 | (srel->r_index[1] << 8) 2426 | srel->r_index[0]); 2427 } 2428 else 2429 { 2430 struct reloc_ext_external *erel; 2431 2432 erel = (struct reloc_ext_external *) reloc; 2433 if (bfd_header_big_endian (input_bfd)) 2434 r_index = ((erel->r_index[0] << 16) 2435 | (erel->r_index[1] << 8) 2436 | erel->r_index[2]); 2437 else 2438 r_index = ((erel->r_index[2] << 16) 2439 | (erel->r_index[1] << 8) 2440 | erel->r_index[0]); 2441 } 2442 2443 got_offsetp = adata (input_bfd).local_got_offsets + r_index; 2444 } 2445 2446 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0); 2447 2448 sgot = bfd_get_linker_section (dynobj, ".got"); 2449 2450 /* We set the least significant bit to indicate whether we have 2451 already initialized the GOT entry. */ 2452 if ((*got_offsetp & 1) == 0) 2453 { 2454 if (h == NULL 2455 || (! info->shared 2456 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0 2457 || (h->flags & SUNOS_DEF_REGULAR) != 0))) 2458 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp); 2459 else 2460 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp); 2461 2462 if (info->shared 2463 || (h != NULL 2464 && (h->flags & SUNOS_DEF_DYNAMIC) != 0 2465 && (h->flags & SUNOS_DEF_REGULAR) == 0)) 2466 { 2467 /* We need to create a GLOB_DAT or 32 reloc to tell the 2468 dynamic linker to fill in this entry in the table. */ 2469 2470 s = bfd_get_linker_section (dynobj, ".dynrel"); 2471 BFD_ASSERT (s != NULL); 2472 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) 2473 < s->size); 2474 2475 p = (s->contents 2476 + s->reloc_count * obj_reloc_entry_size (dynobj)); 2477 2478 if (h != NULL) 2479 indx = h->dynindx; 2480 else 2481 indx = 0; 2482 2483 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE) 2484 { 2485 struct reloc_std_external *srel; 2486 2487 srel = (struct reloc_std_external *) p; 2488 PUT_WORD (dynobj, 2489 (*got_offsetp 2490 + sgot->output_section->vma 2491 + sgot->output_offset), 2492 srel->r_address); 2493 if (bfd_header_big_endian (dynobj)) 2494 { 2495 srel->r_index[0] = (bfd_byte) (indx >> 16); 2496 srel->r_index[1] = (bfd_byte) (indx >> 8); 2497 srel->r_index[2] = (bfd_byte)indx; 2498 if (h == NULL) 2499 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG; 2500 else 2501 srel->r_type[0] = 2502 (RELOC_STD_BITS_EXTERN_BIG 2503 | RELOC_STD_BITS_BASEREL_BIG 2504 | RELOC_STD_BITS_RELATIVE_BIG 2505 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG)); 2506 } 2507 else 2508 { 2509 srel->r_index[2] = (bfd_byte) (indx >> 16); 2510 srel->r_index[1] = (bfd_byte) (indx >> 8); 2511 srel->r_index[0] = (bfd_byte)indx; 2512 if (h == NULL) 2513 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE; 2514 else 2515 srel->r_type[0] = 2516 (RELOC_STD_BITS_EXTERN_LITTLE 2517 | RELOC_STD_BITS_BASEREL_LITTLE 2518 | RELOC_STD_BITS_RELATIVE_LITTLE 2519 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE)); 2520 } 2521 } 2522 else 2523 { 2524 struct reloc_ext_external *erel; 2525 2526 erel = (struct reloc_ext_external *) p; 2527 PUT_WORD (dynobj, 2528 (*got_offsetp 2529 + sgot->output_section->vma 2530 + sgot->output_offset), 2531 erel->r_address); 2532 if (bfd_header_big_endian (dynobj)) 2533 { 2534 erel->r_index[0] = (bfd_byte) (indx >> 16); 2535 erel->r_index[1] = (bfd_byte) (indx >> 8); 2536 erel->r_index[2] = (bfd_byte)indx; 2537 if (h == NULL) 2538 erel->r_type[0] = 2539 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG; 2540 else 2541 erel->r_type[0] = 2542 (RELOC_EXT_BITS_EXTERN_BIG 2543 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG)); 2544 } 2545 else 2546 { 2547 erel->r_index[2] = (bfd_byte) (indx >> 16); 2548 erel->r_index[1] = (bfd_byte) (indx >> 8); 2549 erel->r_index[0] = (bfd_byte)indx; 2550 if (h == NULL) 2551 erel->r_type[0] = 2552 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE; 2553 else 2554 erel->r_type[0] = 2555 (RELOC_EXT_BITS_EXTERN_LITTLE 2556 | (RELOC_GLOB_DAT 2557 << RELOC_EXT_BITS_TYPE_SH_LITTLE)); 2558 } 2559 PUT_WORD (dynobj, 0, erel->r_addend); 2560 } 2561 2562 ++s->reloc_count; 2563 } 2564 2565 *got_offsetp |= 1; 2566 } 2567 2568 *relocationp = (sgot->vma 2569 + (*got_offsetp &~ (bfd_vma) 1) 2570 - sunos_hash_table (info)->got_base); 2571 2572 /* There is nothing else to do for a base relative reloc. */ 2573 return TRUE; 2574 } 2575 2576 if (! sunos_hash_table (info)->dynamic_sections_needed) 2577 return TRUE; 2578 if (! info->shared) 2579 { 2580 if (h == NULL 2581 || h->dynindx == -1 2582 || h->root.root.type != bfd_link_hash_undefined 2583 || (h->flags & SUNOS_DEF_REGULAR) != 0 2584 || (h->flags & SUNOS_DEF_DYNAMIC) == 0 2585 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0) 2586 return TRUE; 2587 } 2588 else 2589 { 2590 if (h != NULL 2591 && (h->dynindx == -1 2592 || jmptbl 2593 || strcmp (h->root.root.root.string, 2594 "__GLOBAL_OFFSET_TABLE_") == 0)) 2595 return TRUE; 2596 } 2597 2598 /* It looks like this is a reloc we are supposed to copy. */ 2599 2600 s = bfd_get_linker_section (dynobj, ".dynrel"); 2601 BFD_ASSERT (s != NULL); 2602 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->size); 2603 2604 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj); 2605 2606 /* Copy the reloc over. */ 2607 memcpy (p, reloc, obj_reloc_entry_size (dynobj)); 2608 2609 if (h != NULL) 2610 indx = h->dynindx; 2611 else 2612 indx = 0; 2613 2614 /* Adjust the address and symbol index. */ 2615 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE) 2616 { 2617 struct reloc_std_external *srel; 2618 2619 srel = (struct reloc_std_external *) p; 2620 PUT_WORD (dynobj, 2621 (GET_WORD (dynobj, srel->r_address) 2622 + input_section->output_section->vma 2623 + input_section->output_offset), 2624 srel->r_address); 2625 if (bfd_header_big_endian (dynobj)) 2626 { 2627 srel->r_index[0] = (bfd_byte) (indx >> 16); 2628 srel->r_index[1] = (bfd_byte) (indx >> 8); 2629 srel->r_index[2] = (bfd_byte)indx; 2630 } 2631 else 2632 { 2633 srel->r_index[2] = (bfd_byte) (indx >> 16); 2634 srel->r_index[1] = (bfd_byte) (indx >> 8); 2635 srel->r_index[0] = (bfd_byte)indx; 2636 } 2637 /* FIXME: We may have to change the addend for a PC relative 2638 reloc. */ 2639 } 2640 else 2641 { 2642 struct reloc_ext_external *erel; 2643 2644 erel = (struct reloc_ext_external *) p; 2645 PUT_WORD (dynobj, 2646 (GET_WORD (dynobj, erel->r_address) 2647 + input_section->output_section->vma 2648 + input_section->output_offset), 2649 erel->r_address); 2650 if (bfd_header_big_endian (dynobj)) 2651 { 2652 erel->r_index[0] = (bfd_byte) (indx >> 16); 2653 erel->r_index[1] = (bfd_byte) (indx >> 8); 2654 erel->r_index[2] = (bfd_byte)indx; 2655 } 2656 else 2657 { 2658 erel->r_index[2] = (bfd_byte) (indx >> 16); 2659 erel->r_index[1] = (bfd_byte) (indx >> 8); 2660 erel->r_index[0] = (bfd_byte)indx; 2661 } 2662 if (pcrel && h != NULL) 2663 { 2664 /* Adjust the addend for the change in address. */ 2665 PUT_WORD (dynobj, 2666 (GET_WORD (dynobj, erel->r_addend) 2667 - (input_section->output_section->vma 2668 + input_section->output_offset 2669 - input_section->vma)), 2670 erel->r_addend); 2671 } 2672 } 2673 2674 ++s->reloc_count; 2675 2676 if (h != NULL) 2677 *skip = TRUE; 2678 2679 return TRUE; 2680 } 2681 2682 /* Finish up the dynamic linking information. */ 2683 2684 static bfd_boolean 2685 sunos_finish_dynamic_link (bfd *abfd, struct bfd_link_info *info) 2686 { 2687 bfd *dynobj; 2688 asection *o; 2689 asection *s; 2690 asection *sdyn; 2691 2692 if (! sunos_hash_table (info)->dynamic_sections_needed 2693 && ! sunos_hash_table (info)->got_needed) 2694 return TRUE; 2695 2696 dynobj = sunos_hash_table (info)->dynobj; 2697 2698 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 2699 BFD_ASSERT (sdyn != NULL); 2700 2701 /* Finish up the .need section. The linker emulation code filled it 2702 in, but with offsets from the start of the section instead of 2703 real addresses. Now that we know the section location, we can 2704 fill in the final values. */ 2705 s = bfd_get_section_by_name (dynobj, ".need"); 2706 if (s != NULL && s->size != 0) 2707 { 2708 file_ptr filepos; 2709 bfd_byte *p; 2710 2711 filepos = s->output_section->filepos + s->output_offset; 2712 p = s->contents; 2713 while (1) 2714 { 2715 bfd_vma val; 2716 2717 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p); 2718 val = GET_WORD (dynobj, p + 12); 2719 if (val == 0) 2720 break; 2721 PUT_WORD (dynobj, val + filepos, p + 12); 2722 p += 16; 2723 } 2724 } 2725 2726 /* The first entry in the .got section is the address of the 2727 dynamic information, unless this is a shared library. */ 2728 s = bfd_get_linker_section (dynobj, ".got"); 2729 BFD_ASSERT (s != NULL); 2730 if (info->shared || sdyn->size == 0) 2731 PUT_WORD (dynobj, 0, s->contents); 2732 else 2733 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset, 2734 s->contents); 2735 2736 for (o = dynobj->sections; o != NULL; o = o->next) 2737 { 2738 if ((o->flags & SEC_HAS_CONTENTS) != 0 2739 && o->contents != NULL) 2740 { 2741 BFD_ASSERT (o->output_section != NULL 2742 && o->output_section->owner == abfd); 2743 if (! bfd_set_section_contents (abfd, o->output_section, 2744 o->contents, 2745 (file_ptr) o->output_offset, 2746 o->size)) 2747 return FALSE; 2748 } 2749 } 2750 2751 if (sdyn->size > 0) 2752 { 2753 struct external_sun4_dynamic esd; 2754 struct external_sun4_dynamic_link esdl; 2755 file_ptr pos; 2756 2757 /* Finish up the dynamic link information. */ 2758 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version); 2759 PUT_WORD (dynobj, 2760 sdyn->output_section->vma + sdyn->output_offset + sizeof esd, 2761 esd.ldd); 2762 PUT_WORD (dynobj, 2763 (sdyn->output_section->vma 2764 + sdyn->output_offset 2765 + sizeof esd 2766 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE), 2767 esd.ld); 2768 2769 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd, 2770 (file_ptr) sdyn->output_offset, 2771 (bfd_size_type) sizeof esd)) 2772 return FALSE; 2773 2774 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded); 2775 2776 s = bfd_get_section_by_name (dynobj, ".need"); 2777 if (s == NULL || s->size == 0) 2778 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need); 2779 else 2780 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset, 2781 esdl.ld_need); 2782 2783 s = bfd_get_section_by_name (dynobj, ".rules"); 2784 if (s == NULL || s->size == 0) 2785 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules); 2786 else 2787 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset, 2788 esdl.ld_rules); 2789 2790 s = bfd_get_linker_section (dynobj, ".got"); 2791 BFD_ASSERT (s != NULL); 2792 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, 2793 esdl.ld_got); 2794 2795 s = bfd_get_linker_section (dynobj, ".plt"); 2796 BFD_ASSERT (s != NULL); 2797 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, 2798 esdl.ld_plt); 2799 PUT_WORD (dynobj, s->size, esdl.ld_plt_sz); 2800 2801 s = bfd_get_linker_section (dynobj, ".dynrel"); 2802 BFD_ASSERT (s != NULL); 2803 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) 2804 == s->size); 2805 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset, 2806 esdl.ld_rel); 2807 2808 s = bfd_get_linker_section (dynobj, ".hash"); 2809 BFD_ASSERT (s != NULL); 2810 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset, 2811 esdl.ld_hash); 2812 2813 s = bfd_get_linker_section (dynobj, ".dynsym"); 2814 BFD_ASSERT (s != NULL); 2815 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset, 2816 esdl.ld_stab); 2817 2818 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash); 2819 2820 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount, 2821 esdl.ld_buckets); 2822 2823 s = bfd_get_linker_section (dynobj, ".dynstr"); 2824 BFD_ASSERT (s != NULL); 2825 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset, 2826 esdl.ld_symbols); 2827 PUT_WORD (dynobj, s->size, esdl.ld_symb_size); 2828 2829 /* The size of the text area is the size of the .text section 2830 rounded up to a page boundary. FIXME: Should the page size be 2831 conditional on something? */ 2832 PUT_WORD (dynobj, 2833 BFD_ALIGN (obj_textsec (abfd)->size, 0x2000), 2834 esdl.ld_text); 2835 2836 pos = sdyn->output_offset; 2837 pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE; 2838 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl, 2839 pos, (bfd_size_type) sizeof esdl)) 2840 return FALSE; 2841 2842 abfd->flags |= DYNAMIC; 2843 } 2844 2845 return TRUE; 2846 } 2847