| 1 | /* ELF linker support. |
| 2 | Copyright 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of BFD, the Binary File Descriptor library. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 19 | |
| 20 | /* ELF linker code. */ |
| 21 | |
| 22 | /* This struct is used to pass information to routines called via |
| 23 | elf_link_hash_traverse which must return failure. */ |
| 24 | |
| 25 | struct elf_info_failed |
| 26 | { |
| 27 | boolean failed; |
| 28 | struct bfd_link_info *info; |
| 29 | }; |
| 30 | |
| 31 | static boolean elf_link_add_object_symbols |
| 32 | PARAMS ((bfd *, struct bfd_link_info *)); |
| 33 | static boolean elf_link_add_archive_symbols |
| 34 | PARAMS ((bfd *, struct bfd_link_info *)); |
| 35 | static boolean elf_merge_symbol |
| 36 | PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *, |
| 37 | asection **, bfd_vma *, struct elf_link_hash_entry **, |
| 38 | boolean *, boolean *, boolean *)); |
| 39 | static boolean elf_export_symbol |
| 40 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
| 41 | static boolean elf_fix_symbol_flags |
| 42 | PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *)); |
| 43 | static boolean elf_adjust_dynamic_symbol |
| 44 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
| 45 | static boolean elf_link_find_version_dependencies |
| 46 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
| 47 | static boolean elf_link_find_version_dependencies |
| 48 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
| 49 | static boolean elf_link_assign_sym_version |
| 50 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
| 51 | static boolean elf_collect_hash_codes |
| 52 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
| 53 | static boolean elf_link_read_relocs_from_section |
| 54 | PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *)); |
| 55 | static void elf_link_output_relocs |
| 56 | PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *)); |
| 57 | static boolean elf_link_size_reloc_section |
| 58 | PARAMS ((bfd *, Elf_Internal_Shdr *, asection *)); |
| 59 | static void elf_link_adjust_relocs |
| 60 | PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int, |
| 61 | struct elf_link_hash_entry **)); |
| 62 | |
| 63 | /* Given an ELF BFD, add symbols to the global hash table as |
| 64 | appropriate. */ |
| 65 | |
| 66 | boolean |
| 67 | elf_bfd_link_add_symbols (abfd, info) |
| 68 | bfd *abfd; |
| 69 | struct bfd_link_info *info; |
| 70 | { |
| 71 | switch (bfd_get_format (abfd)) |
| 72 | { |
| 73 | case bfd_object: |
| 74 | return elf_link_add_object_symbols (abfd, info); |
| 75 | case bfd_archive: |
| 76 | return elf_link_add_archive_symbols (abfd, info); |
| 77 | default: |
| 78 | bfd_set_error (bfd_error_wrong_format); |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | \f |
| 83 | |
| 84 | /* Add symbols from an ELF archive file to the linker hash table. We |
| 85 | don't use _bfd_generic_link_add_archive_symbols because of a |
| 86 | problem which arises on UnixWare. The UnixWare libc.so is an |
| 87 | archive which includes an entry libc.so.1 which defines a bunch of |
| 88 | symbols. The libc.so archive also includes a number of other |
| 89 | object files, which also define symbols, some of which are the same |
| 90 | as those defined in libc.so.1. Correct linking requires that we |
| 91 | consider each object file in turn, and include it if it defines any |
| 92 | symbols we need. _bfd_generic_link_add_archive_symbols does not do |
| 93 | this; it looks through the list of undefined symbols, and includes |
| 94 | any object file which defines them. When this algorithm is used on |
| 95 | UnixWare, it winds up pulling in libc.so.1 early and defining a |
| 96 | bunch of symbols. This means that some of the other objects in the |
| 97 | archive are not included in the link, which is incorrect since they |
| 98 | precede libc.so.1 in the archive. |
| 99 | |
| 100 | Fortunately, ELF archive handling is simpler than that done by |
| 101 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out |
| 102 | oddities. In ELF, if we find a symbol in the archive map, and the |
| 103 | symbol is currently undefined, we know that we must pull in that |
| 104 | object file. |
| 105 | |
| 106 | Unfortunately, we do have to make multiple passes over the symbol |
| 107 | table until nothing further is resolved. */ |
| 108 | |
| 109 | static boolean |
| 110 | elf_link_add_archive_symbols (abfd, info) |
| 111 | bfd *abfd; |
| 112 | struct bfd_link_info *info; |
| 113 | { |
| 114 | symindex c; |
| 115 | boolean *defined = NULL; |
| 116 | boolean *included = NULL; |
| 117 | carsym *symdefs; |
| 118 | boolean loop; |
| 119 | |
| 120 | if (! bfd_has_map (abfd)) |
| 121 | { |
| 122 | /* An empty archive is a special case. */ |
| 123 | if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL) |
| 124 | return true; |
| 125 | bfd_set_error (bfd_error_no_armap); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | /* Keep track of all symbols we know to be already defined, and all |
| 130 | files we know to be already included. This is to speed up the |
| 131 | second and subsequent passes. */ |
| 132 | c = bfd_ardata (abfd)->symdef_count; |
| 133 | if (c == 0) |
| 134 | return true; |
| 135 | defined = (boolean *) bfd_malloc (c * sizeof (boolean)); |
| 136 | included = (boolean *) bfd_malloc (c * sizeof (boolean)); |
| 137 | if (defined == (boolean *) NULL || included == (boolean *) NULL) |
| 138 | goto error_return; |
| 139 | memset (defined, 0, c * sizeof (boolean)); |
| 140 | memset (included, 0, c * sizeof (boolean)); |
| 141 | |
| 142 | symdefs = bfd_ardata (abfd)->symdefs; |
| 143 | |
| 144 | do |
| 145 | { |
| 146 | file_ptr last; |
| 147 | symindex i; |
| 148 | carsym *symdef; |
| 149 | carsym *symdefend; |
| 150 | |
| 151 | loop = false; |
| 152 | last = -1; |
| 153 | |
| 154 | symdef = symdefs; |
| 155 | symdefend = symdef + c; |
| 156 | for (i = 0; symdef < symdefend; symdef++, i++) |
| 157 | { |
| 158 | struct elf_link_hash_entry *h; |
| 159 | bfd *element; |
| 160 | struct bfd_link_hash_entry *undefs_tail; |
| 161 | symindex mark; |
| 162 | |
| 163 | if (defined[i] || included[i]) |
| 164 | continue; |
| 165 | if (symdef->file_offset == last) |
| 166 | { |
| 167 | included[i] = true; |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | h = elf_link_hash_lookup (elf_hash_table (info), symdef->name, |
| 172 | false, false, false); |
| 173 | |
| 174 | if (h == NULL) |
| 175 | { |
| 176 | char *p, *copy; |
| 177 | |
| 178 | /* If this is a default version (the name contains @@), |
| 179 | look up the symbol again without the version. The |
| 180 | effect is that references to the symbol without the |
| 181 | version will be matched by the default symbol in the |
| 182 | archive. */ |
| 183 | |
| 184 | p = strchr (symdef->name, ELF_VER_CHR); |
| 185 | if (p == NULL || p[1] != ELF_VER_CHR) |
| 186 | continue; |
| 187 | |
| 188 | copy = bfd_alloc (abfd, p - symdef->name + 1); |
| 189 | if (copy == NULL) |
| 190 | goto error_return; |
| 191 | memcpy (copy, symdef->name, p - symdef->name); |
| 192 | copy[p - symdef->name] = '\0'; |
| 193 | |
| 194 | h = elf_link_hash_lookup (elf_hash_table (info), copy, |
| 195 | false, false, false); |
| 196 | |
| 197 | bfd_release (abfd, copy); |
| 198 | } |
| 199 | |
| 200 | if (h == NULL) |
| 201 | continue; |
| 202 | |
| 203 | if (h->root.type != bfd_link_hash_undefined) |
| 204 | { |
| 205 | if (h->root.type != bfd_link_hash_undefweak) |
| 206 | defined[i] = true; |
| 207 | continue; |
| 208 | } |
| 209 | |
| 210 | /* We need to include this archive member. */ |
| 211 | |
| 212 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); |
| 213 | if (element == (bfd *) NULL) |
| 214 | goto error_return; |
| 215 | |
| 216 | if (! bfd_check_format (element, bfd_object)) |
| 217 | goto error_return; |
| 218 | |
| 219 | /* Doublecheck that we have not included this object |
| 220 | already--it should be impossible, but there may be |
| 221 | something wrong with the archive. */ |
| 222 | if (element->archive_pass != 0) |
| 223 | { |
| 224 | bfd_set_error (bfd_error_bad_value); |
| 225 | goto error_return; |
| 226 | } |
| 227 | element->archive_pass = 1; |
| 228 | |
| 229 | undefs_tail = info->hash->undefs_tail; |
| 230 | |
| 231 | if (! (*info->callbacks->add_archive_element) (info, element, |
| 232 | symdef->name)) |
| 233 | goto error_return; |
| 234 | if (! elf_link_add_object_symbols (element, info)) |
| 235 | goto error_return; |
| 236 | |
| 237 | /* If there are any new undefined symbols, we need to make |
| 238 | another pass through the archive in order to see whether |
| 239 | they can be defined. FIXME: This isn't perfect, because |
| 240 | common symbols wind up on undefs_tail and because an |
| 241 | undefined symbol which is defined later on in this pass |
| 242 | does not require another pass. This isn't a bug, but it |
| 243 | does make the code less efficient than it could be. */ |
| 244 | if (undefs_tail != info->hash->undefs_tail) |
| 245 | loop = true; |
| 246 | |
| 247 | /* Look backward to mark all symbols from this object file |
| 248 | which we have already seen in this pass. */ |
| 249 | mark = i; |
| 250 | do |
| 251 | { |
| 252 | included[mark] = true; |
| 253 | if (mark == 0) |
| 254 | break; |
| 255 | --mark; |
| 256 | } |
| 257 | while (symdefs[mark].file_offset == symdef->file_offset); |
| 258 | |
| 259 | /* We mark subsequent symbols from this object file as we go |
| 260 | on through the loop. */ |
| 261 | last = symdef->file_offset; |
| 262 | } |
| 263 | } |
| 264 | while (loop); |
| 265 | |
| 266 | free (defined); |
| 267 | free (included); |
| 268 | |
| 269 | return true; |
| 270 | |
| 271 | error_return: |
| 272 | if (defined != (boolean *) NULL) |
| 273 | free (defined); |
| 274 | if (included != (boolean *) NULL) |
| 275 | free (included); |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | /* This function is called when we want to define a new symbol. It |
| 280 | handles the various cases which arise when we find a definition in |
| 281 | a dynamic object, or when there is already a definition in a |
| 282 | dynamic object. The new symbol is described by NAME, SYM, PSEC, |
| 283 | and PVALUE. We set SYM_HASH to the hash table entry. We set |
| 284 | OVERRIDE if the old symbol is overriding a new definition. We set |
| 285 | TYPE_CHANGE_OK if it is OK for the type to change. We set |
| 286 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to |
| 287 | change, we mean that we shouldn't warn if the type or size does |
| 288 | change. */ |
| 289 | |
| 290 | static boolean |
| 291 | elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash, |
| 292 | override, type_change_ok, size_change_ok) |
| 293 | bfd *abfd; |
| 294 | struct bfd_link_info *info; |
| 295 | const char *name; |
| 296 | Elf_Internal_Sym *sym; |
| 297 | asection **psec; |
| 298 | bfd_vma *pvalue; |
| 299 | struct elf_link_hash_entry **sym_hash; |
| 300 | boolean *override; |
| 301 | boolean *type_change_ok; |
| 302 | boolean *size_change_ok; |
| 303 | { |
| 304 | asection *sec; |
| 305 | struct elf_link_hash_entry *h; |
| 306 | int bind; |
| 307 | bfd *oldbfd; |
| 308 | boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; |
| 309 | |
| 310 | *override = false; |
| 311 | |
| 312 | sec = *psec; |
| 313 | bind = ELF_ST_BIND (sym->st_info); |
| 314 | |
| 315 | if (! bfd_is_und_section (sec)) |
| 316 | h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false); |
| 317 | else |
| 318 | h = ((struct elf_link_hash_entry *) |
| 319 | bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false)); |
| 320 | if (h == NULL) |
| 321 | return false; |
| 322 | *sym_hash = h; |
| 323 | |
| 324 | /* This code is for coping with dynamic objects, and is only useful |
| 325 | if we are doing an ELF link. */ |
| 326 | if (info->hash->creator != abfd->xvec) |
| 327 | return true; |
| 328 | |
| 329 | /* For merging, we only care about real symbols. */ |
| 330 | |
| 331 | while (h->root.type == bfd_link_hash_indirect |
| 332 | || h->root.type == bfd_link_hash_warning) |
| 333 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 334 | |
| 335 | /* If we just created the symbol, mark it as being an ELF symbol. |
| 336 | Other than that, there is nothing to do--there is no merge issue |
| 337 | with a newly defined symbol--so we just return. */ |
| 338 | |
| 339 | if (h->root.type == bfd_link_hash_new) |
| 340 | { |
| 341 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | /* OLDBFD is a BFD associated with the existing symbol. */ |
| 346 | |
| 347 | switch (h->root.type) |
| 348 | { |
| 349 | default: |
| 350 | oldbfd = NULL; |
| 351 | break; |
| 352 | |
| 353 | case bfd_link_hash_undefined: |
| 354 | case bfd_link_hash_undefweak: |
| 355 | oldbfd = h->root.u.undef.abfd; |
| 356 | break; |
| 357 | |
| 358 | case bfd_link_hash_defined: |
| 359 | case bfd_link_hash_defweak: |
| 360 | oldbfd = h->root.u.def.section->owner; |
| 361 | break; |
| 362 | |
| 363 | case bfd_link_hash_common: |
| 364 | oldbfd = h->root.u.c.p->section->owner; |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | /* In cases involving weak versioned symbols, we may wind up trying |
| 369 | to merge a symbol with itself. Catch that here, to avoid the |
| 370 | confusion that results if we try to override a symbol with |
| 371 | itself. The additional tests catch cases like |
| 372 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a |
| 373 | dynamic object, which we do want to handle here. */ |
| 374 | if (abfd == oldbfd |
| 375 | && ((abfd->flags & DYNAMIC) == 0 |
| 376 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)) |
| 377 | return true; |
| 378 | |
| 379 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
| 380 | respectively, is from a dynamic object. */ |
| 381 | |
| 382 | if ((abfd->flags & DYNAMIC) != 0) |
| 383 | newdyn = true; |
| 384 | else |
| 385 | newdyn = false; |
| 386 | |
| 387 | if (oldbfd != NULL) |
| 388 | olddyn = (oldbfd->flags & DYNAMIC) != 0; |
| 389 | else |
| 390 | { |
| 391 | asection *hsec; |
| 392 | |
| 393 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section |
| 394 | indices used by MIPS ELF. */ |
| 395 | switch (h->root.type) |
| 396 | { |
| 397 | default: |
| 398 | hsec = NULL; |
| 399 | break; |
| 400 | |
| 401 | case bfd_link_hash_defined: |
| 402 | case bfd_link_hash_defweak: |
| 403 | hsec = h->root.u.def.section; |
| 404 | break; |
| 405 | |
| 406 | case bfd_link_hash_common: |
| 407 | hsec = h->root.u.c.p->section; |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | if (hsec == NULL) |
| 412 | olddyn = false; |
| 413 | else |
| 414 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; |
| 415 | } |
| 416 | |
| 417 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
| 418 | respectively, appear to be a definition rather than reference. */ |
| 419 | |
| 420 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) |
| 421 | newdef = false; |
| 422 | else |
| 423 | newdef = true; |
| 424 | |
| 425 | if (h->root.type == bfd_link_hash_undefined |
| 426 | || h->root.type == bfd_link_hash_undefweak |
| 427 | || h->root.type == bfd_link_hash_common) |
| 428 | olddef = false; |
| 429 | else |
| 430 | olddef = true; |
| 431 | |
| 432 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
| 433 | symbol, respectively, appears to be a common symbol in a dynamic |
| 434 | object. If a symbol appears in an uninitialized section, and is |
| 435 | not weak, and is not a function, then it may be a common symbol |
| 436 | which was resolved when the dynamic object was created. We want |
| 437 | to treat such symbols specially, because they raise special |
| 438 | considerations when setting the symbol size: if the symbol |
| 439 | appears as a common symbol in a regular object, and the size in |
| 440 | the regular object is larger, we must make sure that we use the |
| 441 | larger size. This problematic case can always be avoided in C, |
| 442 | but it must be handled correctly when using Fortran shared |
| 443 | libraries. |
| 444 | |
| 445 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and |
| 446 | likewise for OLDDYNCOMMON and OLDDEF. |
| 447 | |
| 448 | Note that this test is just a heuristic, and that it is quite |
| 449 | possible to have an uninitialized symbol in a shared object which |
| 450 | is really a definition, rather than a common symbol. This could |
| 451 | lead to some minor confusion when the symbol really is a common |
| 452 | symbol in some regular object. However, I think it will be |
| 453 | harmless. */ |
| 454 | |
| 455 | if (newdyn |
| 456 | && newdef |
| 457 | && (sec->flags & SEC_ALLOC) != 0 |
| 458 | && (sec->flags & SEC_LOAD) == 0 |
| 459 | && sym->st_size > 0 |
| 460 | && bind != STB_WEAK |
| 461 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
| 462 | newdyncommon = true; |
| 463 | else |
| 464 | newdyncommon = false; |
| 465 | |
| 466 | if (olddyn |
| 467 | && olddef |
| 468 | && h->root.type == bfd_link_hash_defined |
| 469 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
| 470 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
| 471 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 |
| 472 | && h->size > 0 |
| 473 | && h->type != STT_FUNC) |
| 474 | olddyncommon = true; |
| 475 | else |
| 476 | olddyncommon = false; |
| 477 | |
| 478 | /* It's OK to change the type if either the existing symbol or the |
| 479 | new symbol is weak. */ |
| 480 | |
| 481 | if (h->root.type == bfd_link_hash_defweak |
| 482 | || h->root.type == bfd_link_hash_undefweak |
| 483 | || bind == STB_WEAK) |
| 484 | *type_change_ok = true; |
| 485 | |
| 486 | /* It's OK to change the size if either the existing symbol or the |
| 487 | new symbol is weak, or if the old symbol is undefined. */ |
| 488 | |
| 489 | if (*type_change_ok |
| 490 | || h->root.type == bfd_link_hash_undefined) |
| 491 | *size_change_ok = true; |
| 492 | |
| 493 | /* If both the old and the new symbols look like common symbols in a |
| 494 | dynamic object, set the size of the symbol to the larger of the |
| 495 | two. */ |
| 496 | |
| 497 | if (olddyncommon |
| 498 | && newdyncommon |
| 499 | && sym->st_size != h->size) |
| 500 | { |
| 501 | /* Since we think we have two common symbols, issue a multiple |
| 502 | common warning if desired. Note that we only warn if the |
| 503 | size is different. If the size is the same, we simply let |
| 504 | the old symbol override the new one as normally happens with |
| 505 | symbols defined in dynamic objects. */ |
| 506 | |
| 507 | if (! ((*info->callbacks->multiple_common) |
| 508 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, |
| 509 | h->size, abfd, bfd_link_hash_common, sym->st_size))) |
| 510 | return false; |
| 511 | |
| 512 | if (sym->st_size > h->size) |
| 513 | h->size = sym->st_size; |
| 514 | |
| 515 | *size_change_ok = true; |
| 516 | } |
| 517 | |
| 518 | /* If we are looking at a dynamic object, and we have found a |
| 519 | definition, we need to see if the symbol was already defined by |
| 520 | some other object. If so, we want to use the existing |
| 521 | definition, and we do not want to report a multiple symbol |
| 522 | definition error; we do this by clobbering *PSEC to be |
| 523 | bfd_und_section_ptr. |
| 524 | |
| 525 | We treat a common symbol as a definition if the symbol in the |
| 526 | shared library is a function, since common symbols always |
| 527 | represent variables; this can cause confusion in principle, but |
| 528 | any such confusion would seem to indicate an erroneous program or |
| 529 | shared library. We also permit a common symbol in a regular |
| 530 | object to override a weak symbol in a shared object. |
| 531 | |
| 532 | We prefer a non-weak definition in a shared library to a weak |
| 533 | definition in the executable. */ |
| 534 | |
| 535 | if (newdyn |
| 536 | && newdef |
| 537 | && (olddef |
| 538 | || (h->root.type == bfd_link_hash_common |
| 539 | && (bind == STB_WEAK |
| 540 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC))) |
| 541 | && (h->root.type != bfd_link_hash_defweak |
| 542 | || bind == STB_WEAK)) |
| 543 | { |
| 544 | *override = true; |
| 545 | newdef = false; |
| 546 | newdyncommon = false; |
| 547 | |
| 548 | *psec = sec = bfd_und_section_ptr; |
| 549 | *size_change_ok = true; |
| 550 | |
| 551 | /* If we get here when the old symbol is a common symbol, then |
| 552 | we are explicitly letting it override a weak symbol or |
| 553 | function in a dynamic object, and we don't want to warn about |
| 554 | a type change. If the old symbol is a defined symbol, a type |
| 555 | change warning may still be appropriate. */ |
| 556 | |
| 557 | if (h->root.type == bfd_link_hash_common) |
| 558 | *type_change_ok = true; |
| 559 | } |
| 560 | |
| 561 | /* Handle the special case of an old common symbol merging with a |
| 562 | new symbol which looks like a common symbol in a shared object. |
| 563 | We change *PSEC and *PVALUE to make the new symbol look like a |
| 564 | common symbol, and let _bfd_generic_link_add_one_symbol will do |
| 565 | the right thing. */ |
| 566 | |
| 567 | if (newdyncommon |
| 568 | && h->root.type == bfd_link_hash_common) |
| 569 | { |
| 570 | *override = true; |
| 571 | newdef = false; |
| 572 | newdyncommon = false; |
| 573 | *pvalue = sym->st_size; |
| 574 | *psec = sec = bfd_com_section_ptr; |
| 575 | *size_change_ok = true; |
| 576 | } |
| 577 | |
| 578 | /* If the old symbol is from a dynamic object, and the new symbol is |
| 579 | a definition which is not from a dynamic object, then the new |
| 580 | symbol overrides the old symbol. Symbols from regular files |
| 581 | always take precedence over symbols from dynamic objects, even if |
| 582 | they are defined after the dynamic object in the link. |
| 583 | |
| 584 | As above, we again permit a common symbol in a regular object to |
| 585 | override a definition in a shared object if the shared object |
| 586 | symbol is a function or is weak. |
| 587 | |
| 588 | As above, we permit a non-weak definition in a shared object to |
| 589 | override a weak definition in a regular object. */ |
| 590 | |
| 591 | if (! newdyn |
| 592 | && (newdef |
| 593 | || (bfd_is_com_section (sec) |
| 594 | && (h->root.type == bfd_link_hash_defweak |
| 595 | || h->type == STT_FUNC))) |
| 596 | && olddyn |
| 597 | && olddef |
| 598 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
| 599 | && (bind != STB_WEAK |
| 600 | || h->root.type == bfd_link_hash_defweak)) |
| 601 | { |
| 602 | /* Change the hash table entry to undefined, and let |
| 603 | _bfd_generic_link_add_one_symbol do the right thing with the |
| 604 | new definition. */ |
| 605 | |
| 606 | h->root.type = bfd_link_hash_undefined; |
| 607 | h->root.u.undef.abfd = h->root.u.def.section->owner; |
| 608 | *size_change_ok = true; |
| 609 | |
| 610 | olddef = false; |
| 611 | olddyncommon = false; |
| 612 | |
| 613 | /* We again permit a type change when a common symbol may be |
| 614 | overriding a function. */ |
| 615 | |
| 616 | if (bfd_is_com_section (sec)) |
| 617 | *type_change_ok = true; |
| 618 | |
| 619 | /* This union may have been set to be non-NULL when this symbol |
| 620 | was seen in a dynamic object. We must force the union to be |
| 621 | NULL, so that it is correct for a regular symbol. */ |
| 622 | |
| 623 | h->verinfo.vertree = NULL; |
| 624 | |
| 625 | /* In this special case, if H is the target of an indirection, |
| 626 | we want the caller to frob with H rather than with the |
| 627 | indirect symbol. That will permit the caller to redefine the |
| 628 | target of the indirection, rather than the indirect symbol |
| 629 | itself. FIXME: This will break the -y option if we store a |
| 630 | symbol with a different name. */ |
| 631 | *sym_hash = h; |
| 632 | } |
| 633 | |
| 634 | /* Handle the special case of a new common symbol merging with an |
| 635 | old symbol that looks like it might be a common symbol defined in |
| 636 | a shared object. Note that we have already handled the case in |
| 637 | which a new common symbol should simply override the definition |
| 638 | in the shared library. */ |
| 639 | |
| 640 | if (! newdyn |
| 641 | && bfd_is_com_section (sec) |
| 642 | && olddyncommon) |
| 643 | { |
| 644 | /* It would be best if we could set the hash table entry to a |
| 645 | common symbol, but we don't know what to use for the section |
| 646 | or the alignment. */ |
| 647 | if (! ((*info->callbacks->multiple_common) |
| 648 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, |
| 649 | h->size, abfd, bfd_link_hash_common, sym->st_size))) |
| 650 | return false; |
| 651 | |
| 652 | /* If the predumed common symbol in the dynamic object is |
| 653 | larger, pretend that the new symbol has its size. */ |
| 654 | |
| 655 | if (h->size > *pvalue) |
| 656 | *pvalue = h->size; |
| 657 | |
| 658 | /* FIXME: We no longer know the alignment required by the symbol |
| 659 | in the dynamic object, so we just wind up using the one from |
| 660 | the regular object. */ |
| 661 | |
| 662 | olddef = false; |
| 663 | olddyncommon = false; |
| 664 | |
| 665 | h->root.type = bfd_link_hash_undefined; |
| 666 | h->root.u.undef.abfd = h->root.u.def.section->owner; |
| 667 | |
| 668 | *size_change_ok = true; |
| 669 | *type_change_ok = true; |
| 670 | |
| 671 | h->verinfo.vertree = NULL; |
| 672 | } |
| 673 | |
| 674 | /* Handle the special case of a weak definition in a regular object |
| 675 | followed by a non-weak definition in a shared object. In this |
| 676 | case, we prefer the definition in the shared object. */ |
| 677 | if (olddef |
| 678 | && h->root.type == bfd_link_hash_defweak |
| 679 | && newdef |
| 680 | && newdyn |
| 681 | && bind != STB_WEAK) |
| 682 | { |
| 683 | /* To make this work we have to frob the flags so that the rest |
| 684 | of the code does not think we are using the regular |
| 685 | definition. */ |
| 686 | h->elf_link_hash_flags &= ~ ELF_LINK_HASH_DEF_REGULAR; |
| 687 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; |
| 688 | |
| 689 | /* If H is the target of an indirection, we want the caller to |
| 690 | use H rather than the indirect symbol. Otherwise if we are |
| 691 | defining a new indirect symbol we will wind up attaching it |
| 692 | to the entry we are overriding. */ |
| 693 | *sym_hash = h; |
| 694 | } |
| 695 | |
| 696 | /* Handle the special case of a non-weak definition in a shared |
| 697 | object followed by a weak definition in a regular object. In |
| 698 | this case we prefer to definition in the shared object. To make |
| 699 | this work we have to tell the caller to not treat the new symbol |
| 700 | as a definition. */ |
| 701 | if (olddef |
| 702 | && olddyn |
| 703 | && h->root.type != bfd_link_hash_defweak |
| 704 | && newdef |
| 705 | && ! newdyn |
| 706 | && bind == STB_WEAK) |
| 707 | *override = true; |
| 708 | |
| 709 | return true; |
| 710 | } |
| 711 | |
| 712 | /* Add symbols from an ELF object file to the linker hash table. */ |
| 713 | |
| 714 | static boolean |
| 715 | elf_link_add_object_symbols (abfd, info) |
| 716 | bfd *abfd; |
| 717 | struct bfd_link_info *info; |
| 718 | { |
| 719 | boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *, |
| 720 | const Elf_Internal_Sym *, |
| 721 | const char **, flagword *, |
| 722 | asection **, bfd_vma *)); |
| 723 | boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *, |
| 724 | asection *, const Elf_Internal_Rela *)); |
| 725 | boolean collect; |
| 726 | Elf_Internal_Shdr *hdr; |
| 727 | size_t symcount; |
| 728 | size_t extsymcount; |
| 729 | size_t extsymoff; |
| 730 | Elf_External_Sym *buf = NULL; |
| 731 | struct elf_link_hash_entry **sym_hash; |
| 732 | boolean dynamic; |
| 733 | bfd_byte *dynver = NULL; |
| 734 | Elf_External_Versym *extversym = NULL; |
| 735 | Elf_External_Versym *ever; |
| 736 | Elf_External_Dyn *dynbuf = NULL; |
| 737 | struct elf_link_hash_entry *weaks; |
| 738 | Elf_External_Sym *esym; |
| 739 | Elf_External_Sym *esymend; |
| 740 | |
| 741 | add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook; |
| 742 | collect = get_elf_backend_data (abfd)->collect; |
| 743 | |
| 744 | if ((abfd->flags & DYNAMIC) == 0) |
| 745 | dynamic = false; |
| 746 | else |
| 747 | { |
| 748 | dynamic = true; |
| 749 | |
| 750 | /* You can't use -r against a dynamic object. Also, there's no |
| 751 | hope of using a dynamic object which does not exactly match |
| 752 | the format of the output file. */ |
| 753 | if (info->relocateable || info->hash->creator != abfd->xvec) |
| 754 | { |
| 755 | bfd_set_error (bfd_error_invalid_operation); |
| 756 | goto error_return; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* As a GNU extension, any input sections which are named |
| 761 | .gnu.warning.SYMBOL are treated as warning symbols for the given |
| 762 | symbol. This differs from .gnu.warning sections, which generate |
| 763 | warnings when they are included in an output file. */ |
| 764 | if (! info->shared) |
| 765 | { |
| 766 | asection *s; |
| 767 | |
| 768 | for (s = abfd->sections; s != NULL; s = s->next) |
| 769 | { |
| 770 | const char *name; |
| 771 | |
| 772 | name = bfd_get_section_name (abfd, s); |
| 773 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) |
| 774 | { |
| 775 | char *msg; |
| 776 | bfd_size_type sz; |
| 777 | |
| 778 | name += sizeof ".gnu.warning." - 1; |
| 779 | |
| 780 | /* If this is a shared object, then look up the symbol |
| 781 | in the hash table. If it is there, and it is already |
| 782 | been defined, then we will not be using the entry |
| 783 | from this shared object, so we don't need to warn. |
| 784 | FIXME: If we see the definition in a regular object |
| 785 | later on, we will warn, but we shouldn't. The only |
| 786 | fix is to keep track of what warnings we are supposed |
| 787 | to emit, and then handle them all at the end of the |
| 788 | link. */ |
| 789 | if (dynamic && abfd->xvec == info->hash->creator) |
| 790 | { |
| 791 | struct elf_link_hash_entry *h; |
| 792 | |
| 793 | h = elf_link_hash_lookup (elf_hash_table (info), name, |
| 794 | false, false, true); |
| 795 | |
| 796 | /* FIXME: What about bfd_link_hash_common? */ |
| 797 | if (h != NULL |
| 798 | && (h->root.type == bfd_link_hash_defined |
| 799 | || h->root.type == bfd_link_hash_defweak)) |
| 800 | { |
| 801 | /* We don't want to issue this warning. Clobber |
| 802 | the section size so that the warning does not |
| 803 | get copied into the output file. */ |
| 804 | s->_raw_size = 0; |
| 805 | continue; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | sz = bfd_section_size (abfd, s); |
| 810 | msg = (char *) bfd_alloc (abfd, sz + 1); |
| 811 | if (msg == NULL) |
| 812 | goto error_return; |
| 813 | |
| 814 | if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz)) |
| 815 | goto error_return; |
| 816 | |
| 817 | msg[sz] = '\0'; |
| 818 | |
| 819 | if (! (_bfd_generic_link_add_one_symbol |
| 820 | (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg, |
| 821 | false, collect, (struct bfd_link_hash_entry **) NULL))) |
| 822 | goto error_return; |
| 823 | |
| 824 | if (! info->relocateable) |
| 825 | { |
| 826 | /* Clobber the section size so that the warning does |
| 827 | not get copied into the output file. */ |
| 828 | s->_raw_size = 0; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | /* If this is a dynamic object, we always link against the .dynsym |
| 835 | symbol table, not the .symtab symbol table. The dynamic linker |
| 836 | will only see the .dynsym symbol table, so there is no reason to |
| 837 | look at .symtab for a dynamic object. */ |
| 838 | |
| 839 | if (! dynamic || elf_dynsymtab (abfd) == 0) |
| 840 | hdr = &elf_tdata (abfd)->symtab_hdr; |
| 841 | else |
| 842 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; |
| 843 | |
| 844 | if (dynamic) |
| 845 | { |
| 846 | /* Read in any version definitions. */ |
| 847 | |
| 848 | if (! _bfd_elf_slurp_version_tables (abfd)) |
| 849 | goto error_return; |
| 850 | |
| 851 | /* Read in the symbol versions, but don't bother to convert them |
| 852 | to internal format. */ |
| 853 | if (elf_dynversym (abfd) != 0) |
| 854 | { |
| 855 | Elf_Internal_Shdr *versymhdr; |
| 856 | |
| 857 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; |
| 858 | extversym = (Elf_External_Versym *) bfd_malloc (hdr->sh_size); |
| 859 | if (extversym == NULL) |
| 860 | goto error_return; |
| 861 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 |
| 862 | || (bfd_read ((PTR) extversym, 1, versymhdr->sh_size, abfd) |
| 863 | != versymhdr->sh_size)) |
| 864 | goto error_return; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
| 869 | |
| 870 | /* The sh_info field of the symtab header tells us where the |
| 871 | external symbols start. We don't care about the local symbols at |
| 872 | this point. */ |
| 873 | if (elf_bad_symtab (abfd)) |
| 874 | { |
| 875 | extsymcount = symcount; |
| 876 | extsymoff = 0; |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | extsymcount = symcount - hdr->sh_info; |
| 881 | extsymoff = hdr->sh_info; |
| 882 | } |
| 883 | |
| 884 | buf = ((Elf_External_Sym *) |
| 885 | bfd_malloc (extsymcount * sizeof (Elf_External_Sym))); |
| 886 | if (buf == NULL && extsymcount != 0) |
| 887 | goto error_return; |
| 888 | |
| 889 | /* We store a pointer to the hash table entry for each external |
| 890 | symbol. */ |
| 891 | sym_hash = ((struct elf_link_hash_entry **) |
| 892 | bfd_alloc (abfd, |
| 893 | extsymcount * sizeof (struct elf_link_hash_entry *))); |
| 894 | if (sym_hash == NULL) |
| 895 | goto error_return; |
| 896 | elf_sym_hashes (abfd) = sym_hash; |
| 897 | |
| 898 | if (! dynamic) |
| 899 | { |
| 900 | /* If we are creating a shared library, create all the dynamic |
| 901 | sections immediately. We need to attach them to something, |
| 902 | so we attach them to this BFD, provided it is the right |
| 903 | format. FIXME: If there are no input BFD's of the same |
| 904 | format as the output, we can't make a shared library. */ |
| 905 | if (info->shared |
| 906 | && ! elf_hash_table (info)->dynamic_sections_created |
| 907 | && abfd->xvec == info->hash->creator) |
| 908 | { |
| 909 | if (! elf_link_create_dynamic_sections (abfd, info)) |
| 910 | goto error_return; |
| 911 | } |
| 912 | } |
| 913 | else |
| 914 | { |
| 915 | asection *s; |
| 916 | boolean add_needed; |
| 917 | const char *name; |
| 918 | bfd_size_type oldsize; |
| 919 | bfd_size_type strindex; |
| 920 | |
| 921 | /* Find the name to use in a DT_NEEDED entry that refers to this |
| 922 | object. If the object has a DT_SONAME entry, we use it. |
| 923 | Otherwise, if the generic linker stuck something in |
| 924 | elf_dt_name, we use that. Otherwise, we just use the file |
| 925 | name. If the generic linker put a null string into |
| 926 | elf_dt_name, we don't make a DT_NEEDED entry at all, even if |
| 927 | there is a DT_SONAME entry. */ |
| 928 | add_needed = true; |
| 929 | name = bfd_get_filename (abfd); |
| 930 | if (elf_dt_name (abfd) != NULL) |
| 931 | { |
| 932 | name = elf_dt_name (abfd); |
| 933 | if (*name == '\0') |
| 934 | add_needed = false; |
| 935 | } |
| 936 | s = bfd_get_section_by_name (abfd, ".dynamic"); |
| 937 | if (s != NULL) |
| 938 | { |
| 939 | Elf_External_Dyn *extdyn; |
| 940 | Elf_External_Dyn *extdynend; |
| 941 | int elfsec; |
| 942 | unsigned long link; |
| 943 | |
| 944 | dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size); |
| 945 | if (dynbuf == NULL) |
| 946 | goto error_return; |
| 947 | |
| 948 | if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, |
| 949 | (file_ptr) 0, s->_raw_size)) |
| 950 | goto error_return; |
| 951 | |
| 952 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); |
| 953 | if (elfsec == -1) |
| 954 | goto error_return; |
| 955 | link = elf_elfsections (abfd)[elfsec]->sh_link; |
| 956 | |
| 957 | { |
| 958 | /* The shared libraries distributed with hpux11 have a bogus |
| 959 | sh_link field for the ".dynamic" section. This code detects |
| 960 | when LINK refers to a section that is not a string table and |
| 961 | tries to find the string table for the ".dynsym" section |
| 962 | instead. */ |
| 963 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[link]; |
| 964 | if (hdr->sh_type != SHT_STRTAB) |
| 965 | { |
| 966 | asection *s = bfd_get_section_by_name (abfd, ".dynsym"); |
| 967 | int elfsec = _bfd_elf_section_from_bfd_section (abfd, s); |
| 968 | if (elfsec == -1) |
| 969 | goto error_return; |
| 970 | link = elf_elfsections (abfd)[elfsec]->sh_link; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | extdyn = dynbuf; |
| 975 | extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn); |
| 976 | for (; extdyn < extdynend; extdyn++) |
| 977 | { |
| 978 | Elf_Internal_Dyn dyn; |
| 979 | |
| 980 | elf_swap_dyn_in (abfd, extdyn, &dyn); |
| 981 | if (dyn.d_tag == DT_SONAME) |
| 982 | { |
| 983 | name = bfd_elf_string_from_elf_section (abfd, link, |
| 984 | dyn.d_un.d_val); |
| 985 | if (name == NULL) |
| 986 | goto error_return; |
| 987 | } |
| 988 | if (dyn.d_tag == DT_NEEDED) |
| 989 | { |
| 990 | struct bfd_link_needed_list *n, **pn; |
| 991 | char *fnm, *anm; |
| 992 | |
| 993 | n = ((struct bfd_link_needed_list *) |
| 994 | bfd_alloc (abfd, sizeof (struct bfd_link_needed_list))); |
| 995 | fnm = bfd_elf_string_from_elf_section (abfd, link, |
| 996 | dyn.d_un.d_val); |
| 997 | if (n == NULL || fnm == NULL) |
| 998 | goto error_return; |
| 999 | anm = bfd_alloc (abfd, strlen (fnm) + 1); |
| 1000 | if (anm == NULL) |
| 1001 | goto error_return; |
| 1002 | strcpy (anm, fnm); |
| 1003 | n->name = anm; |
| 1004 | n->by = abfd; |
| 1005 | n->next = NULL; |
| 1006 | for (pn = &elf_hash_table (info)->needed; |
| 1007 | *pn != NULL; |
| 1008 | pn = &(*pn)->next) |
| 1009 | ; |
| 1010 | *pn = n; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | free (dynbuf); |
| 1015 | dynbuf = NULL; |
| 1016 | } |
| 1017 | |
| 1018 | /* We do not want to include any of the sections in a dynamic |
| 1019 | object in the output file. We hack by simply clobbering the |
| 1020 | list of sections in the BFD. This could be handled more |
| 1021 | cleanly by, say, a new section flag; the existing |
| 1022 | SEC_NEVER_LOAD flag is not the one we want, because that one |
| 1023 | still implies that the section takes up space in the output |
| 1024 | file. */ |
| 1025 | abfd->sections = NULL; |
| 1026 | abfd->section_count = 0; |
| 1027 | |
| 1028 | /* If this is the first dynamic object found in the link, create |
| 1029 | the special sections required for dynamic linking. */ |
| 1030 | if (! elf_hash_table (info)->dynamic_sections_created) |
| 1031 | { |
| 1032 | if (! elf_link_create_dynamic_sections (abfd, info)) |
| 1033 | goto error_return; |
| 1034 | } |
| 1035 | |
| 1036 | if (add_needed) |
| 1037 | { |
| 1038 | /* Add a DT_NEEDED entry for this dynamic object. */ |
| 1039 | oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr); |
| 1040 | strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name, |
| 1041 | true, false); |
| 1042 | if (strindex == (bfd_size_type) -1) |
| 1043 | goto error_return; |
| 1044 | |
| 1045 | if (oldsize == _bfd_stringtab_size (elf_hash_table (info)->dynstr)) |
| 1046 | { |
| 1047 | asection *sdyn; |
| 1048 | Elf_External_Dyn *dyncon, *dynconend; |
| 1049 | |
| 1050 | /* The hash table size did not change, which means that |
| 1051 | the dynamic object name was already entered. If we |
| 1052 | have already included this dynamic object in the |
| 1053 | link, just ignore it. There is no reason to include |
| 1054 | a particular dynamic object more than once. */ |
| 1055 | sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj, |
| 1056 | ".dynamic"); |
| 1057 | BFD_ASSERT (sdyn != NULL); |
| 1058 | |
| 1059 | dyncon = (Elf_External_Dyn *) sdyn->contents; |
| 1060 | dynconend = (Elf_External_Dyn *) (sdyn->contents + |
| 1061 | sdyn->_raw_size); |
| 1062 | for (; dyncon < dynconend; dyncon++) |
| 1063 | { |
| 1064 | Elf_Internal_Dyn dyn; |
| 1065 | |
| 1066 | elf_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon, |
| 1067 | &dyn); |
| 1068 | if (dyn.d_tag == DT_NEEDED |
| 1069 | && dyn.d_un.d_val == strindex) |
| 1070 | { |
| 1071 | if (buf != NULL) |
| 1072 | free (buf); |
| 1073 | if (extversym != NULL) |
| 1074 | free (extversym); |
| 1075 | return true; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
| 1081 | goto error_return; |
| 1082 | } |
| 1083 | |
| 1084 | /* Save the SONAME, if there is one, because sometimes the |
| 1085 | linker emulation code will need to know it. */ |
| 1086 | if (*name == '\0') |
| 1087 | name = bfd_get_filename (abfd); |
| 1088 | elf_dt_name (abfd) = name; |
| 1089 | } |
| 1090 | |
| 1091 | if (bfd_seek (abfd, |
| 1092 | hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym), |
| 1093 | SEEK_SET) != 0 |
| 1094 | || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd) |
| 1095 | != extsymcount * sizeof (Elf_External_Sym))) |
| 1096 | goto error_return; |
| 1097 | |
| 1098 | weaks = NULL; |
| 1099 | |
| 1100 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
| 1101 | esymend = buf + extsymcount; |
| 1102 | for (esym = buf; |
| 1103 | esym < esymend; |
| 1104 | esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) |
| 1105 | { |
| 1106 | Elf_Internal_Sym sym; |
| 1107 | int bind; |
| 1108 | bfd_vma value; |
| 1109 | asection *sec; |
| 1110 | flagword flags; |
| 1111 | const char *name; |
| 1112 | struct elf_link_hash_entry *h; |
| 1113 | boolean definition; |
| 1114 | boolean size_change_ok, type_change_ok; |
| 1115 | boolean new_weakdef; |
| 1116 | unsigned int old_alignment; |
| 1117 | |
| 1118 | elf_swap_symbol_in (abfd, esym, &sym); |
| 1119 | |
| 1120 | flags = BSF_NO_FLAGS; |
| 1121 | sec = NULL; |
| 1122 | value = sym.st_value; |
| 1123 | *sym_hash = NULL; |
| 1124 | |
| 1125 | bind = ELF_ST_BIND (sym.st_info); |
| 1126 | if (bind == STB_LOCAL) |
| 1127 | { |
| 1128 | /* This should be impossible, since ELF requires that all |
| 1129 | global symbols follow all local symbols, and that sh_info |
| 1130 | point to the first global symbol. Unfortunatealy, Irix 5 |
| 1131 | screws this up. */ |
| 1132 | continue; |
| 1133 | } |
| 1134 | else if (bind == STB_GLOBAL) |
| 1135 | { |
| 1136 | if (sym.st_shndx != SHN_UNDEF |
| 1137 | && sym.st_shndx != SHN_COMMON) |
| 1138 | flags = BSF_GLOBAL; |
| 1139 | else |
| 1140 | flags = 0; |
| 1141 | } |
| 1142 | else if (bind == STB_WEAK) |
| 1143 | flags = BSF_WEAK; |
| 1144 | else |
| 1145 | { |
| 1146 | /* Leave it up to the processor backend. */ |
| 1147 | } |
| 1148 | |
| 1149 | if (sym.st_shndx == SHN_UNDEF) |
| 1150 | sec = bfd_und_section_ptr; |
| 1151 | else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE) |
| 1152 | { |
| 1153 | sec = section_from_elf_index (abfd, sym.st_shndx); |
| 1154 | if (sec == NULL) |
| 1155 | sec = bfd_abs_section_ptr; |
| 1156 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
| 1157 | value -= sec->vma; |
| 1158 | } |
| 1159 | else if (sym.st_shndx == SHN_ABS) |
| 1160 | sec = bfd_abs_section_ptr; |
| 1161 | else if (sym.st_shndx == SHN_COMMON) |
| 1162 | { |
| 1163 | sec = bfd_com_section_ptr; |
| 1164 | /* What ELF calls the size we call the value. What ELF |
| 1165 | calls the value we call the alignment. */ |
| 1166 | value = sym.st_size; |
| 1167 | } |
| 1168 | else |
| 1169 | { |
| 1170 | /* Leave it up to the processor backend. */ |
| 1171 | } |
| 1172 | |
| 1173 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name); |
| 1174 | if (name == (const char *) NULL) |
| 1175 | goto error_return; |
| 1176 | |
| 1177 | if (add_symbol_hook) |
| 1178 | { |
| 1179 | if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec, |
| 1180 | &value)) |
| 1181 | goto error_return; |
| 1182 | |
| 1183 | /* The hook function sets the name to NULL if this symbol |
| 1184 | should be skipped for some reason. */ |
| 1185 | if (name == (const char *) NULL) |
| 1186 | continue; |
| 1187 | } |
| 1188 | |
| 1189 | /* Sanity check that all possibilities were handled. */ |
| 1190 | if (sec == (asection *) NULL) |
| 1191 | { |
| 1192 | bfd_set_error (bfd_error_bad_value); |
| 1193 | goto error_return; |
| 1194 | } |
| 1195 | |
| 1196 | if (bfd_is_und_section (sec) |
| 1197 | || bfd_is_com_section (sec)) |
| 1198 | definition = false; |
| 1199 | else |
| 1200 | definition = true; |
| 1201 | |
| 1202 | size_change_ok = false; |
| 1203 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; |
| 1204 | old_alignment = 0; |
| 1205 | if (info->hash->creator->flavour == bfd_target_elf_flavour) |
| 1206 | { |
| 1207 | Elf_Internal_Versym iver; |
| 1208 | unsigned int vernum = 0; |
| 1209 | boolean override; |
| 1210 | |
| 1211 | if (ever != NULL) |
| 1212 | { |
| 1213 | _bfd_elf_swap_versym_in (abfd, ever, &iver); |
| 1214 | vernum = iver.vs_vers & VERSYM_VERSION; |
| 1215 | |
| 1216 | /* If this is a hidden symbol, or if it is not version |
| 1217 | 1, we append the version name to the symbol name. |
| 1218 | However, we do not modify a non-hidden absolute |
| 1219 | symbol, because it might be the version symbol |
| 1220 | itself. FIXME: What if it isn't? */ |
| 1221 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
| 1222 | || (vernum > 1 && ! bfd_is_abs_section (sec))) |
| 1223 | { |
| 1224 | const char *verstr; |
| 1225 | int namelen, newlen; |
| 1226 | char *newname, *p; |
| 1227 | |
| 1228 | if (sym.st_shndx != SHN_UNDEF) |
| 1229 | { |
| 1230 | if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info) |
| 1231 | { |
| 1232 | (*_bfd_error_handler) |
| 1233 | (_("%s: %s: invalid version %u (max %d)"), |
| 1234 | bfd_get_filename (abfd), name, vernum, |
| 1235 | elf_tdata (abfd)->dynverdef_hdr.sh_info); |
| 1236 | bfd_set_error (bfd_error_bad_value); |
| 1237 | goto error_return; |
| 1238 | } |
| 1239 | else if (vernum > 1) |
| 1240 | verstr = |
| 1241 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; |
| 1242 | else |
| 1243 | verstr = ""; |
| 1244 | } |
| 1245 | else |
| 1246 | { |
| 1247 | /* We cannot simply test for the number of |
| 1248 | entries in the VERNEED section since the |
| 1249 | numbers for the needed versions do not start |
| 1250 | at 0. */ |
| 1251 | Elf_Internal_Verneed *t; |
| 1252 | |
| 1253 | verstr = NULL; |
| 1254 | for (t = elf_tdata (abfd)->verref; |
| 1255 | t != NULL; |
| 1256 | t = t->vn_nextref) |
| 1257 | { |
| 1258 | Elf_Internal_Vernaux *a; |
| 1259 | |
| 1260 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
| 1261 | { |
| 1262 | if (a->vna_other == vernum) |
| 1263 | { |
| 1264 | verstr = a->vna_nodename; |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | if (a != NULL) |
| 1269 | break; |
| 1270 | } |
| 1271 | if (verstr == NULL) |
| 1272 | { |
| 1273 | (*_bfd_error_handler) |
| 1274 | (_("%s: %s: invalid needed version %d"), |
| 1275 | bfd_get_filename (abfd), name, vernum); |
| 1276 | bfd_set_error (bfd_error_bad_value); |
| 1277 | goto error_return; |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | namelen = strlen (name); |
| 1282 | newlen = namelen + strlen (verstr) + 2; |
| 1283 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) |
| 1284 | ++newlen; |
| 1285 | |
| 1286 | newname = (char *) bfd_alloc (abfd, newlen); |
| 1287 | if (newname == NULL) |
| 1288 | goto error_return; |
| 1289 | strcpy (newname, name); |
| 1290 | p = newname + namelen; |
| 1291 | *p++ = ELF_VER_CHR; |
| 1292 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) |
| 1293 | *p++ = ELF_VER_CHR; |
| 1294 | strcpy (p, verstr); |
| 1295 | |
| 1296 | name = newname; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value, |
| 1301 | sym_hash, &override, &type_change_ok, |
| 1302 | &size_change_ok)) |
| 1303 | goto error_return; |
| 1304 | |
| 1305 | if (override) |
| 1306 | definition = false; |
| 1307 | |
| 1308 | h = *sym_hash; |
| 1309 | while (h->root.type == bfd_link_hash_indirect |
| 1310 | || h->root.type == bfd_link_hash_warning) |
| 1311 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 1312 | |
| 1313 | /* Remember the old alignment if this is a common symbol, so |
| 1314 | that we don't reduce the alignment later on. We can't |
| 1315 | check later, because _bfd_generic_link_add_one_symbol |
| 1316 | will set a default for the alignment which we want to |
| 1317 | override. */ |
| 1318 | if (h->root.type == bfd_link_hash_common) |
| 1319 | old_alignment = h->root.u.c.p->alignment_power; |
| 1320 | |
| 1321 | if (elf_tdata (abfd)->verdef != NULL |
| 1322 | && ! override |
| 1323 | && vernum > 1 |
| 1324 | && definition) |
| 1325 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; |
| 1326 | } |
| 1327 | |
| 1328 | if (! (_bfd_generic_link_add_one_symbol |
| 1329 | (info, abfd, name, flags, sec, value, (const char *) NULL, |
| 1330 | false, collect, (struct bfd_link_hash_entry **) sym_hash))) |
| 1331 | goto error_return; |
| 1332 | |
| 1333 | h = *sym_hash; |
| 1334 | while (h->root.type == bfd_link_hash_indirect |
| 1335 | || h->root.type == bfd_link_hash_warning) |
| 1336 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 1337 | *sym_hash = h; |
| 1338 | |
| 1339 | new_weakdef = false; |
| 1340 | if (dynamic |
| 1341 | && definition |
| 1342 | && (flags & BSF_WEAK) != 0 |
| 1343 | && ELF_ST_TYPE (sym.st_info) != STT_FUNC |
| 1344 | && info->hash->creator->flavour == bfd_target_elf_flavour |
| 1345 | && h->weakdef == NULL) |
| 1346 | { |
| 1347 | /* Keep a list of all weak defined non function symbols from |
| 1348 | a dynamic object, using the weakdef field. Later in this |
| 1349 | function we will set the weakdef field to the correct |
| 1350 | value. We only put non-function symbols from dynamic |
| 1351 | objects on this list, because that happens to be the only |
| 1352 | time we need to know the normal symbol corresponding to a |
| 1353 | weak symbol, and the information is time consuming to |
| 1354 | figure out. If the weakdef field is not already NULL, |
| 1355 | then this symbol was already defined by some previous |
| 1356 | dynamic object, and we will be using that previous |
| 1357 | definition anyhow. */ |
| 1358 | |
| 1359 | h->weakdef = weaks; |
| 1360 | weaks = h; |
| 1361 | new_weakdef = true; |
| 1362 | } |
| 1363 | |
| 1364 | /* Set the alignment of a common symbol. */ |
| 1365 | if (sym.st_shndx == SHN_COMMON |
| 1366 | && h->root.type == bfd_link_hash_common) |
| 1367 | { |
| 1368 | unsigned int align; |
| 1369 | |
| 1370 | align = bfd_log2 (sym.st_value); |
| 1371 | if (align > old_alignment) |
| 1372 | h->root.u.c.p->alignment_power = align; |
| 1373 | } |
| 1374 | |
| 1375 | if (info->hash->creator->flavour == bfd_target_elf_flavour) |
| 1376 | { |
| 1377 | int old_flags; |
| 1378 | boolean dynsym; |
| 1379 | int new_flag; |
| 1380 | |
| 1381 | /* Remember the symbol size and type. */ |
| 1382 | if (sym.st_size != 0 |
| 1383 | && (definition || h->size == 0)) |
| 1384 | { |
| 1385 | if (h->size != 0 && h->size != sym.st_size && ! size_change_ok) |
| 1386 | (*_bfd_error_handler) |
| 1387 | (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"), |
| 1388 | name, (unsigned long) h->size, (unsigned long) sym.st_size, |
| 1389 | bfd_get_filename (abfd)); |
| 1390 | |
| 1391 | h->size = sym.st_size; |
| 1392 | } |
| 1393 | |
| 1394 | /* If this is a common symbol, then we always want H->SIZE |
| 1395 | to be the size of the common symbol. The code just above |
| 1396 | won't fix the size if a common symbol becomes larger. We |
| 1397 | don't warn about a size change here, because that is |
| 1398 | covered by --warn-common. */ |
| 1399 | if (h->root.type == bfd_link_hash_common) |
| 1400 | h->size = h->root.u.c.size; |
| 1401 | |
| 1402 | if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE |
| 1403 | && (definition || h->type == STT_NOTYPE)) |
| 1404 | { |
| 1405 | if (h->type != STT_NOTYPE |
| 1406 | && h->type != ELF_ST_TYPE (sym.st_info) |
| 1407 | && ! type_change_ok) |
| 1408 | (*_bfd_error_handler) |
| 1409 | (_("Warning: type of symbol `%s' changed from %d to %d in %s"), |
| 1410 | name, h->type, ELF_ST_TYPE (sym.st_info), |
| 1411 | bfd_get_filename (abfd)); |
| 1412 | |
| 1413 | h->type = ELF_ST_TYPE (sym.st_info); |
| 1414 | } |
| 1415 | |
| 1416 | if (sym.st_other != 0 |
| 1417 | && (definition || h->other == 0)) |
| 1418 | h->other = sym.st_other; |
| 1419 | |
| 1420 | /* Set a flag in the hash table entry indicating the type of |
| 1421 | reference or definition we just found. Keep a count of |
| 1422 | the number of dynamic symbols we find. A dynamic symbol |
| 1423 | is one which is referenced or defined by both a regular |
| 1424 | object and a shared object. */ |
| 1425 | old_flags = h->elf_link_hash_flags; |
| 1426 | dynsym = false; |
| 1427 | if (! dynamic) |
| 1428 | { |
| 1429 | if (! definition) |
| 1430 | { |
| 1431 | new_flag = ELF_LINK_HASH_REF_REGULAR; |
| 1432 | if (bind != STB_WEAK) |
| 1433 | new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK; |
| 1434 | } |
| 1435 | else |
| 1436 | new_flag = ELF_LINK_HASH_DEF_REGULAR; |
| 1437 | if (info->shared |
| 1438 | || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC |
| 1439 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0) |
| 1440 | dynsym = true; |
| 1441 | } |
| 1442 | else |
| 1443 | { |
| 1444 | if (! definition) |
| 1445 | new_flag = ELF_LINK_HASH_REF_DYNAMIC; |
| 1446 | else |
| 1447 | new_flag = ELF_LINK_HASH_DEF_DYNAMIC; |
| 1448 | if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR |
| 1449 | | ELF_LINK_HASH_REF_REGULAR)) != 0 |
| 1450 | || (h->weakdef != NULL |
| 1451 | && ! new_weakdef |
| 1452 | && h->weakdef->dynindx != -1)) |
| 1453 | dynsym = true; |
| 1454 | } |
| 1455 | |
| 1456 | h->elf_link_hash_flags |= new_flag; |
| 1457 | |
| 1458 | /* If this symbol has a version, and it is the default |
| 1459 | version, we create an indirect symbol from the default |
| 1460 | name to the fully decorated name. This will cause |
| 1461 | external references which do not specify a version to be |
| 1462 | bound to this version of the symbol. */ |
| 1463 | if (definition) |
| 1464 | { |
| 1465 | char *p; |
| 1466 | |
| 1467 | p = strchr (name, ELF_VER_CHR); |
| 1468 | if (p != NULL && p[1] == ELF_VER_CHR) |
| 1469 | { |
| 1470 | char *shortname; |
| 1471 | struct elf_link_hash_entry *hi; |
| 1472 | boolean override; |
| 1473 | |
| 1474 | shortname = bfd_hash_allocate (&info->hash->table, |
| 1475 | p - name + 1); |
| 1476 | if (shortname == NULL) |
| 1477 | goto error_return; |
| 1478 | strncpy (shortname, name, p - name); |
| 1479 | shortname[p - name] = '\0'; |
| 1480 | |
| 1481 | /* We are going to create a new symbol. Merge it |
| 1482 | with any existing symbol with this name. For the |
| 1483 | purposes of the merge, act as though we were |
| 1484 | defining the symbol we just defined, although we |
| 1485 | actually going to define an indirect symbol. */ |
| 1486 | type_change_ok = false; |
| 1487 | size_change_ok = false; |
| 1488 | if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec, |
| 1489 | &value, &hi, &override, |
| 1490 | &type_change_ok, &size_change_ok)) |
| 1491 | goto error_return; |
| 1492 | |
| 1493 | if (! override) |
| 1494 | { |
| 1495 | if (! (_bfd_generic_link_add_one_symbol |
| 1496 | (info, abfd, shortname, BSF_INDIRECT, |
| 1497 | bfd_ind_section_ptr, (bfd_vma) 0, name, false, |
| 1498 | collect, (struct bfd_link_hash_entry **) &hi))) |
| 1499 | goto error_return; |
| 1500 | } |
| 1501 | else |
| 1502 | { |
| 1503 | /* In this case the symbol named SHORTNAME is |
| 1504 | overriding the indirect symbol we want to |
| 1505 | add. We were planning on making SHORTNAME an |
| 1506 | indirect symbol referring to NAME. SHORTNAME |
| 1507 | is the name without a version. NAME is the |
| 1508 | fully versioned name, and it is the default |
| 1509 | version. |
| 1510 | |
| 1511 | Overriding means that we already saw a |
| 1512 | definition for the symbol SHORTNAME in a |
| 1513 | regular object, and it is overriding the |
| 1514 | symbol defined in the dynamic object. |
| 1515 | |
| 1516 | When this happens, we actually want to change |
| 1517 | NAME, the symbol we just added, to refer to |
| 1518 | SHORTNAME. This will cause references to |
| 1519 | NAME in the shared object to become |
| 1520 | references to SHORTNAME in the regular |
| 1521 | object. This is what we expect when we |
| 1522 | override a function in a shared object: that |
| 1523 | the references in the shared object will be |
| 1524 | mapped to the definition in the regular |
| 1525 | object. */ |
| 1526 | |
| 1527 | while (hi->root.type == bfd_link_hash_indirect |
| 1528 | || hi->root.type == bfd_link_hash_warning) |
| 1529 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; |
| 1530 | |
| 1531 | h->root.type = bfd_link_hash_indirect; |
| 1532 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
| 1533 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) |
| 1534 | { |
| 1535 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC; |
| 1536 | hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; |
| 1537 | if (hi->elf_link_hash_flags |
| 1538 | & (ELF_LINK_HASH_REF_REGULAR |
| 1539 | | ELF_LINK_HASH_DEF_REGULAR)) |
| 1540 | { |
| 1541 | if (! _bfd_elf_link_record_dynamic_symbol (info, |
| 1542 | hi)) |
| 1543 | goto error_return; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | /* Now set HI to H, so that the following code |
| 1548 | will set the other fields correctly. */ |
| 1549 | hi = h; |
| 1550 | } |
| 1551 | |
| 1552 | /* If there is a duplicate definition somewhere, |
| 1553 | then HI may not point to an indirect symbol. We |
| 1554 | will have reported an error to the user in that |
| 1555 | case. */ |
| 1556 | |
| 1557 | if (hi->root.type == bfd_link_hash_indirect) |
| 1558 | { |
| 1559 | struct elf_link_hash_entry *ht; |
| 1560 | |
| 1561 | /* If the symbol became indirect, then we assume |
| 1562 | that we have not seen a definition before. */ |
| 1563 | BFD_ASSERT ((hi->elf_link_hash_flags |
| 1564 | & (ELF_LINK_HASH_DEF_DYNAMIC |
| 1565 | | ELF_LINK_HASH_DEF_REGULAR)) |
| 1566 | == 0); |
| 1567 | |
| 1568 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
| 1569 | |
| 1570 | /* Copy down any references that we may have |
| 1571 | already seen to the symbol which just became |
| 1572 | indirect. */ |
| 1573 | ht->elf_link_hash_flags |= |
| 1574 | (hi->elf_link_hash_flags |
| 1575 | & (ELF_LINK_HASH_REF_DYNAMIC |
| 1576 | | ELF_LINK_HASH_REF_REGULAR |
| 1577 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK |
| 1578 | | ELF_LINK_NON_GOT_REF)); |
| 1579 | |
| 1580 | /* Copy over the global and procedure linkage table |
| 1581 | offset entries. These may have been already set |
| 1582 | up by a check_relocs routine. */ |
| 1583 | if (ht->got.offset == (bfd_vma) -1) |
| 1584 | { |
| 1585 | ht->got.offset = hi->got.offset; |
| 1586 | hi->got.offset = (bfd_vma) -1; |
| 1587 | } |
| 1588 | BFD_ASSERT (hi->got.offset == (bfd_vma) -1); |
| 1589 | |
| 1590 | if (ht->plt.offset == (bfd_vma) -1) |
| 1591 | { |
| 1592 | ht->plt.offset = hi->plt.offset; |
| 1593 | hi->plt.offset = (bfd_vma) -1; |
| 1594 | } |
| 1595 | BFD_ASSERT (hi->plt.offset == (bfd_vma) -1); |
| 1596 | |
| 1597 | if (ht->dynindx == -1) |
| 1598 | { |
| 1599 | ht->dynindx = hi->dynindx; |
| 1600 | ht->dynstr_index = hi->dynstr_index; |
| 1601 | hi->dynindx = -1; |
| 1602 | hi->dynstr_index = 0; |
| 1603 | } |
| 1604 | BFD_ASSERT (hi->dynindx == -1); |
| 1605 | |
| 1606 | /* FIXME: There may be other information to copy |
| 1607 | over for particular targets. */ |
| 1608 | |
| 1609 | /* See if the new flags lead us to realize that |
| 1610 | the symbol must be dynamic. */ |
| 1611 | if (! dynsym) |
| 1612 | { |
| 1613 | if (! dynamic) |
| 1614 | { |
| 1615 | if (info->shared |
| 1616 | || ((hi->elf_link_hash_flags |
| 1617 | & ELF_LINK_HASH_REF_DYNAMIC) |
| 1618 | != 0)) |
| 1619 | dynsym = true; |
| 1620 | } |
| 1621 | else |
| 1622 | { |
| 1623 | if ((hi->elf_link_hash_flags |
| 1624 | & ELF_LINK_HASH_REF_REGULAR) != 0) |
| 1625 | dynsym = true; |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | /* We also need to define an indirection from the |
| 1631 | nondefault version of the symbol. */ |
| 1632 | |
| 1633 | shortname = bfd_hash_allocate (&info->hash->table, |
| 1634 | strlen (name)); |
| 1635 | if (shortname == NULL) |
| 1636 | goto error_return; |
| 1637 | strncpy (shortname, name, p - name); |
| 1638 | strcpy (shortname + (p - name), p + 1); |
| 1639 | |
| 1640 | /* Once again, merge with any existing symbol. */ |
| 1641 | type_change_ok = false; |
| 1642 | size_change_ok = false; |
| 1643 | if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec, |
| 1644 | &value, &hi, &override, |
| 1645 | &type_change_ok, &size_change_ok)) |
| 1646 | goto error_return; |
| 1647 | |
| 1648 | if (override) |
| 1649 | { |
| 1650 | /* Here SHORTNAME is a versioned name, so we |
| 1651 | don't expect to see the type of override we |
| 1652 | do in the case above. */ |
| 1653 | (*_bfd_error_handler) |
| 1654 | (_("%s: warning: unexpected redefinition of `%s'"), |
| 1655 | bfd_get_filename (abfd), shortname); |
| 1656 | } |
| 1657 | else |
| 1658 | { |
| 1659 | if (! (_bfd_generic_link_add_one_symbol |
| 1660 | (info, abfd, shortname, BSF_INDIRECT, |
| 1661 | bfd_ind_section_ptr, (bfd_vma) 0, name, false, |
| 1662 | collect, (struct bfd_link_hash_entry **) &hi))) |
| 1663 | goto error_return; |
| 1664 | |
| 1665 | /* If there is a duplicate definition somewhere, |
| 1666 | then HI may not point to an indirect symbol. |
| 1667 | We will have reported an error to the user in |
| 1668 | that case. */ |
| 1669 | |
| 1670 | if (hi->root.type == bfd_link_hash_indirect) |
| 1671 | { |
| 1672 | /* If the symbol became indirect, then we |
| 1673 | assume that we have not seen a definition |
| 1674 | before. */ |
| 1675 | BFD_ASSERT ((hi->elf_link_hash_flags |
| 1676 | & (ELF_LINK_HASH_DEF_DYNAMIC |
| 1677 | | ELF_LINK_HASH_DEF_REGULAR)) |
| 1678 | == 0); |
| 1679 | |
| 1680 | /* Copy down any references that we may have |
| 1681 | already seen to the symbol which just |
| 1682 | became indirect. */ |
| 1683 | h->elf_link_hash_flags |= |
| 1684 | (hi->elf_link_hash_flags |
| 1685 | & (ELF_LINK_HASH_REF_DYNAMIC |
| 1686 | | ELF_LINK_HASH_REF_REGULAR |
| 1687 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK |
| 1688 | | ELF_LINK_NON_GOT_REF)); |
| 1689 | |
| 1690 | /* Copy over the global and procedure linkage |
| 1691 | table offset entries. These may have been |
| 1692 | already set up by a check_relocs routine. */ |
| 1693 | if (h->got.offset == (bfd_vma) -1) |
| 1694 | { |
| 1695 | h->got.offset = hi->got.offset; |
| 1696 | hi->got.offset = (bfd_vma) -1; |
| 1697 | } |
| 1698 | BFD_ASSERT (hi->got.offset == (bfd_vma) -1); |
| 1699 | |
| 1700 | if (h->plt.offset == (bfd_vma) -1) |
| 1701 | { |
| 1702 | h->plt.offset = hi->plt.offset; |
| 1703 | hi->plt.offset = (bfd_vma) -1; |
| 1704 | } |
| 1705 | BFD_ASSERT (hi->got.offset == (bfd_vma) -1); |
| 1706 | |
| 1707 | if (h->dynindx == -1) |
| 1708 | { |
| 1709 | h->dynindx = hi->dynindx; |
| 1710 | h->dynstr_index = hi->dynstr_index; |
| 1711 | hi->dynindx = -1; |
| 1712 | hi->dynstr_index = 0; |
| 1713 | } |
| 1714 | BFD_ASSERT (hi->dynindx == -1); |
| 1715 | |
| 1716 | /* FIXME: There may be other information to |
| 1717 | copy over for particular targets. */ |
| 1718 | |
| 1719 | /* See if the new flags lead us to realize |
| 1720 | that the symbol must be dynamic. */ |
| 1721 | if (! dynsym) |
| 1722 | { |
| 1723 | if (! dynamic) |
| 1724 | { |
| 1725 | if (info->shared |
| 1726 | || ((hi->elf_link_hash_flags |
| 1727 | & ELF_LINK_HASH_REF_DYNAMIC) |
| 1728 | != 0)) |
| 1729 | dynsym = true; |
| 1730 | } |
| 1731 | else |
| 1732 | { |
| 1733 | if ((hi->elf_link_hash_flags |
| 1734 | & ELF_LINK_HASH_REF_REGULAR) != 0) |
| 1735 | dynsym = true; |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | if (dynsym && h->dynindx == -1) |
| 1744 | { |
| 1745 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) |
| 1746 | goto error_return; |
| 1747 | if (h->weakdef != NULL |
| 1748 | && ! new_weakdef |
| 1749 | && h->weakdef->dynindx == -1) |
| 1750 | { |
| 1751 | if (! _bfd_elf_link_record_dynamic_symbol (info, |
| 1752 | h->weakdef)) |
| 1753 | goto error_return; |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | /* Now set the weakdefs field correctly for all the weak defined |
| 1760 | symbols we found. The only way to do this is to search all the |
| 1761 | symbols. Since we only need the information for non functions in |
| 1762 | dynamic objects, that's the only time we actually put anything on |
| 1763 | the list WEAKS. We need this information so that if a regular |
| 1764 | object refers to a symbol defined weakly in a dynamic object, the |
| 1765 | real symbol in the dynamic object is also put in the dynamic |
| 1766 | symbols; we also must arrange for both symbols to point to the |
| 1767 | same memory location. We could handle the general case of symbol |
| 1768 | aliasing, but a general symbol alias can only be generated in |
| 1769 | assembler code, handling it correctly would be very time |
| 1770 | consuming, and other ELF linkers don't handle general aliasing |
| 1771 | either. */ |
| 1772 | while (weaks != NULL) |
| 1773 | { |
| 1774 | struct elf_link_hash_entry *hlook; |
| 1775 | asection *slook; |
| 1776 | bfd_vma vlook; |
| 1777 | struct elf_link_hash_entry **hpp; |
| 1778 | struct elf_link_hash_entry **hppend; |
| 1779 | |
| 1780 | hlook = weaks; |
| 1781 | weaks = hlook->weakdef; |
| 1782 | hlook->weakdef = NULL; |
| 1783 | |
| 1784 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined |
| 1785 | || hlook->root.type == bfd_link_hash_defweak |
| 1786 | || hlook->root.type == bfd_link_hash_common |
| 1787 | || hlook->root.type == bfd_link_hash_indirect); |
| 1788 | slook = hlook->root.u.def.section; |
| 1789 | vlook = hlook->root.u.def.value; |
| 1790 | |
| 1791 | hpp = elf_sym_hashes (abfd); |
| 1792 | hppend = hpp + extsymcount; |
| 1793 | for (; hpp < hppend; hpp++) |
| 1794 | { |
| 1795 | struct elf_link_hash_entry *h; |
| 1796 | |
| 1797 | h = *hpp; |
| 1798 | if (h != NULL && h != hlook |
| 1799 | && h->root.type == bfd_link_hash_defined |
| 1800 | && h->root.u.def.section == slook |
| 1801 | && h->root.u.def.value == vlook) |
| 1802 | { |
| 1803 | hlook->weakdef = h; |
| 1804 | |
| 1805 | /* If the weak definition is in the list of dynamic |
| 1806 | symbols, make sure the real definition is put there |
| 1807 | as well. */ |
| 1808 | if (hlook->dynindx != -1 |
| 1809 | && h->dynindx == -1) |
| 1810 | { |
| 1811 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) |
| 1812 | goto error_return; |
| 1813 | } |
| 1814 | |
| 1815 | /* If the real definition is in the list of dynamic |
| 1816 | symbols, make sure the weak definition is put there |
| 1817 | as well. If we don't do this, then the dynamic |
| 1818 | loader might not merge the entries for the real |
| 1819 | definition and the weak definition. */ |
| 1820 | if (h->dynindx != -1 |
| 1821 | && hlook->dynindx == -1) |
| 1822 | { |
| 1823 | if (! _bfd_elf_link_record_dynamic_symbol (info, hlook)) |
| 1824 | goto error_return; |
| 1825 | } |
| 1826 | |
| 1827 | break; |
| 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | if (buf != NULL) |
| 1833 | { |
| 1834 | free (buf); |
| 1835 | buf = NULL; |
| 1836 | } |
| 1837 | |
| 1838 | if (extversym != NULL) |
| 1839 | { |
| 1840 | free (extversym); |
| 1841 | extversym = NULL; |
| 1842 | } |
| 1843 | |
| 1844 | /* If this object is the same format as the output object, and it is |
| 1845 | not a shared library, then let the backend look through the |
| 1846 | relocs. |
| 1847 | |
| 1848 | This is required to build global offset table entries and to |
| 1849 | arrange for dynamic relocs. It is not required for the |
| 1850 | particular common case of linking non PIC code, even when linking |
| 1851 | against shared libraries, but unfortunately there is no way of |
| 1852 | knowing whether an object file has been compiled PIC or not. |
| 1853 | Looking through the relocs is not particularly time consuming. |
| 1854 | The problem is that we must either (1) keep the relocs in memory, |
| 1855 | which causes the linker to require additional runtime memory or |
| 1856 | (2) read the relocs twice from the input file, which wastes time. |
| 1857 | This would be a good case for using mmap. |
| 1858 | |
| 1859 | I have no idea how to handle linking PIC code into a file of a |
| 1860 | different format. It probably can't be done. */ |
| 1861 | check_relocs = get_elf_backend_data (abfd)->check_relocs; |
| 1862 | if (! dynamic |
| 1863 | && abfd->xvec == info->hash->creator |
| 1864 | && check_relocs != NULL) |
| 1865 | { |
| 1866 | asection *o; |
| 1867 | |
| 1868 | for (o = abfd->sections; o != NULL; o = o->next) |
| 1869 | { |
| 1870 | Elf_Internal_Rela *internal_relocs; |
| 1871 | boolean ok; |
| 1872 | |
| 1873 | if ((o->flags & SEC_RELOC) == 0 |
| 1874 | || o->reloc_count == 0 |
| 1875 | || ((info->strip == strip_all || info->strip == strip_debugger) |
| 1876 | && (o->flags & SEC_DEBUGGING) != 0) |
| 1877 | || bfd_is_abs_section (o->output_section)) |
| 1878 | continue; |
| 1879 | |
| 1880 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) |
| 1881 | (abfd, o, (PTR) NULL, |
| 1882 | (Elf_Internal_Rela *) NULL, |
| 1883 | info->keep_memory)); |
| 1884 | if (internal_relocs == NULL) |
| 1885 | goto error_return; |
| 1886 | |
| 1887 | ok = (*check_relocs) (abfd, info, o, internal_relocs); |
| 1888 | |
| 1889 | if (! info->keep_memory) |
| 1890 | free (internal_relocs); |
| 1891 | |
| 1892 | if (! ok) |
| 1893 | goto error_return; |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | /* If this is a non-traditional, non-relocateable link, try to |
| 1898 | optimize the handling of the .stab/.stabstr sections. */ |
| 1899 | if (! dynamic |
| 1900 | && ! info->relocateable |
| 1901 | && ! info->traditional_format |
| 1902 | && info->hash->creator->flavour == bfd_target_elf_flavour |
| 1903 | && (info->strip != strip_all && info->strip != strip_debugger)) |
| 1904 | { |
| 1905 | asection *stab, *stabstr; |
| 1906 | |
| 1907 | stab = bfd_get_section_by_name (abfd, ".stab"); |
| 1908 | if (stab != NULL) |
| 1909 | { |
| 1910 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); |
| 1911 | |
| 1912 | if (stabstr != NULL) |
| 1913 | { |
| 1914 | struct bfd_elf_section_data *secdata; |
| 1915 | |
| 1916 | secdata = elf_section_data (stab); |
| 1917 | if (! _bfd_link_section_stabs (abfd, |
| 1918 | &elf_hash_table (info)->stab_info, |
| 1919 | stab, stabstr, |
| 1920 | &secdata->stab_info)) |
| 1921 | goto error_return; |
| 1922 | } |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | return true; |
| 1927 | |
| 1928 | error_return: |
| 1929 | if (buf != NULL) |
| 1930 | free (buf); |
| 1931 | if (dynbuf != NULL) |
| 1932 | free (dynbuf); |
| 1933 | if (dynver != NULL) |
| 1934 | free (dynver); |
| 1935 | if (extversym != NULL) |
| 1936 | free (extversym); |
| 1937 | return false; |
| 1938 | } |
| 1939 | |
| 1940 | /* Create some sections which will be filled in with dynamic linking |
| 1941 | information. ABFD is an input file which requires dynamic sections |
| 1942 | to be created. The dynamic sections take up virtual memory space |
| 1943 | when the final executable is run, so we need to create them before |
| 1944 | addresses are assigned to the output sections. We work out the |
| 1945 | actual contents and size of these sections later. */ |
| 1946 | |
| 1947 | boolean |
| 1948 | elf_link_create_dynamic_sections (abfd, info) |
| 1949 | bfd *abfd; |
| 1950 | struct bfd_link_info *info; |
| 1951 | { |
| 1952 | flagword flags; |
| 1953 | register asection *s; |
| 1954 | struct elf_link_hash_entry *h; |
| 1955 | struct elf_backend_data *bed; |
| 1956 | |
| 1957 | if (elf_hash_table (info)->dynamic_sections_created) |
| 1958 | return true; |
| 1959 | |
| 1960 | /* Make sure that all dynamic sections use the same input BFD. */ |
| 1961 | if (elf_hash_table (info)->dynobj == NULL) |
| 1962 | elf_hash_table (info)->dynobj = abfd; |
| 1963 | else |
| 1964 | abfd = elf_hash_table (info)->dynobj; |
| 1965 | |
| 1966 | /* Note that we set the SEC_IN_MEMORY flag for all of these |
| 1967 | sections. */ |
| 1968 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
| 1969 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); |
| 1970 | |
| 1971 | /* A dynamically linked executable has a .interp section, but a |
| 1972 | shared library does not. */ |
| 1973 | if (! info->shared) |
| 1974 | { |
| 1975 | s = bfd_make_section (abfd, ".interp"); |
| 1976 | if (s == NULL |
| 1977 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) |
| 1978 | return false; |
| 1979 | } |
| 1980 | |
| 1981 | /* Create sections to hold version informations. These are removed |
| 1982 | if they are not needed. */ |
| 1983 | s = bfd_make_section (abfd, ".gnu.version_d"); |
| 1984 | if (s == NULL |
| 1985 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) |
| 1986 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) |
| 1987 | return false; |
| 1988 | |
| 1989 | s = bfd_make_section (abfd, ".gnu.version"); |
| 1990 | if (s == NULL |
| 1991 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) |
| 1992 | || ! bfd_set_section_alignment (abfd, s, 1)) |
| 1993 | return false; |
| 1994 | |
| 1995 | s = bfd_make_section (abfd, ".gnu.version_r"); |
| 1996 | if (s == NULL |
| 1997 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) |
| 1998 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) |
| 1999 | return false; |
| 2000 | |
| 2001 | s = bfd_make_section (abfd, ".dynsym"); |
| 2002 | if (s == NULL |
| 2003 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) |
| 2004 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) |
| 2005 | return false; |
| 2006 | |
| 2007 | s = bfd_make_section (abfd, ".dynstr"); |
| 2008 | if (s == NULL |
| 2009 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) |
| 2010 | return false; |
| 2011 | |
| 2012 | /* Create a strtab to hold the dynamic symbol names. */ |
| 2013 | if (elf_hash_table (info)->dynstr == NULL) |
| 2014 | { |
| 2015 | elf_hash_table (info)->dynstr = elf_stringtab_init (); |
| 2016 | if (elf_hash_table (info)->dynstr == NULL) |
| 2017 | return false; |
| 2018 | } |
| 2019 | |
| 2020 | s = bfd_make_section (abfd, ".dynamic"); |
| 2021 | if (s == NULL |
| 2022 | || ! bfd_set_section_flags (abfd, s, flags) |
| 2023 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) |
| 2024 | return false; |
| 2025 | |
| 2026 | /* The special symbol _DYNAMIC is always set to the start of the |
| 2027 | .dynamic section. This call occurs before we have processed the |
| 2028 | symbols for any dynamic object, so we don't have to worry about |
| 2029 | overriding a dynamic definition. We could set _DYNAMIC in a |
| 2030 | linker script, but we only want to define it if we are, in fact, |
| 2031 | creating a .dynamic section. We don't want to define it if there |
| 2032 | is no .dynamic section, since on some ELF platforms the start up |
| 2033 | code examines it to decide how to initialize the process. */ |
| 2034 | h = NULL; |
| 2035 | if (! (_bfd_generic_link_add_one_symbol |
| 2036 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0, |
| 2037 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, |
| 2038 | (struct bfd_link_hash_entry **) &h))) |
| 2039 | return false; |
| 2040 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
| 2041 | h->type = STT_OBJECT; |
| 2042 | |
| 2043 | if (info->shared |
| 2044 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) |
| 2045 | return false; |
| 2046 | |
| 2047 | bed = get_elf_backend_data (abfd); |
| 2048 | |
| 2049 | s = bfd_make_section (abfd, ".hash"); |
| 2050 | if (s == NULL |
| 2051 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) |
| 2052 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) |
| 2053 | return false; |
| 2054 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; |
| 2055 | |
| 2056 | /* Let the backend create the rest of the sections. This lets the |
| 2057 | backend set the right flags. The backend will normally create |
| 2058 | the .got and .plt sections. */ |
| 2059 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) |
| 2060 | return false; |
| 2061 | |
| 2062 | elf_hash_table (info)->dynamic_sections_created = true; |
| 2063 | |
| 2064 | return true; |
| 2065 | } |
| 2066 | |
| 2067 | /* Add an entry to the .dynamic table. */ |
| 2068 | |
| 2069 | boolean |
| 2070 | elf_add_dynamic_entry (info, tag, val) |
| 2071 | struct bfd_link_info *info; |
| 2072 | bfd_vma tag; |
| 2073 | bfd_vma val; |
| 2074 | { |
| 2075 | Elf_Internal_Dyn dyn; |
| 2076 | bfd *dynobj; |
| 2077 | asection *s; |
| 2078 | size_t newsize; |
| 2079 | bfd_byte *newcontents; |
| 2080 | |
| 2081 | dynobj = elf_hash_table (info)->dynobj; |
| 2082 | |
| 2083 | s = bfd_get_section_by_name (dynobj, ".dynamic"); |
| 2084 | BFD_ASSERT (s != NULL); |
| 2085 | |
| 2086 | newsize = s->_raw_size + sizeof (Elf_External_Dyn); |
| 2087 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
| 2088 | if (newcontents == NULL) |
| 2089 | return false; |
| 2090 | |
| 2091 | dyn.d_tag = tag; |
| 2092 | dyn.d_un.d_val = val; |
| 2093 | elf_swap_dyn_out (dynobj, &dyn, |
| 2094 | (Elf_External_Dyn *) (newcontents + s->_raw_size)); |
| 2095 | |
| 2096 | s->_raw_size = newsize; |
| 2097 | s->contents = newcontents; |
| 2098 | |
| 2099 | return true; |
| 2100 | } |
| 2101 | |
| 2102 | /* Record a new local dynamic symbol. */ |
| 2103 | |
| 2104 | boolean |
| 2105 | elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx) |
| 2106 | struct bfd_link_info *info; |
| 2107 | bfd *input_bfd; |
| 2108 | long input_indx; |
| 2109 | { |
| 2110 | struct elf_link_local_dynamic_entry *entry; |
| 2111 | struct elf_link_hash_table *eht; |
| 2112 | struct bfd_strtab_hash *dynstr; |
| 2113 | Elf_External_Sym esym; |
| 2114 | unsigned long dynstr_index; |
| 2115 | char *name; |
| 2116 | |
| 2117 | /* See if the entry exists already. */ |
| 2118 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) |
| 2119 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) |
| 2120 | return true; |
| 2121 | |
| 2122 | entry = (struct elf_link_local_dynamic_entry *) |
| 2123 | bfd_alloc (input_bfd, sizeof (*entry)); |
| 2124 | if (entry == NULL) |
| 2125 | return false; |
| 2126 | |
| 2127 | /* Go find the symbol, so that we can find it's name. */ |
| 2128 | if (bfd_seek (input_bfd, |
| 2129 | (elf_tdata (input_bfd)->symtab_hdr.sh_offset |
| 2130 | + input_indx * sizeof (Elf_External_Sym)), |
| 2131 | SEEK_SET) != 0 |
| 2132 | || (bfd_read (&esym, sizeof (Elf_External_Sym), 1, input_bfd) |
| 2133 | != sizeof (Elf_External_Sym))) |
| 2134 | return false; |
| 2135 | elf_swap_symbol_in (input_bfd, &esym, &entry->isym); |
| 2136 | |
| 2137 | name = (bfd_elf_string_from_elf_section |
| 2138 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, |
| 2139 | entry->isym.st_name)); |
| 2140 | |
| 2141 | dynstr = elf_hash_table (info)->dynstr; |
| 2142 | if (dynstr == NULL) |
| 2143 | { |
| 2144 | /* Create a strtab to hold the dynamic symbol names. */ |
| 2145 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init (); |
| 2146 | if (dynstr == NULL) |
| 2147 | return false; |
| 2148 | } |
| 2149 | |
| 2150 | dynstr_index = _bfd_stringtab_add (dynstr, name, true, false); |
| 2151 | if (dynstr_index == (unsigned long) -1) |
| 2152 | return false; |
| 2153 | entry->isym.st_name = dynstr_index; |
| 2154 | |
| 2155 | eht = elf_hash_table (info); |
| 2156 | |
| 2157 | entry->next = eht->dynlocal; |
| 2158 | eht->dynlocal = entry; |
| 2159 | entry->input_bfd = input_bfd; |
| 2160 | entry->input_indx = input_indx; |
| 2161 | eht->dynsymcount++; |
| 2162 | |
| 2163 | /* Whatever binding the symbol had before, it's now local. */ |
| 2164 | entry->isym.st_info |
| 2165 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); |
| 2166 | |
| 2167 | /* The dynindx will be set at the end of size_dynamic_sections. */ |
| 2168 | |
| 2169 | return true; |
| 2170 | } |
| 2171 | \f |
| 2172 | |
| 2173 | /* Read and swap the relocs from the section indicated by SHDR. This |
| 2174 | may be either a REL or a RELA section. The relocations are |
| 2175 | translated into RELA relocations and stored in INTERNAL_RELOCS, |
| 2176 | which should have already been allocated to contain enough space. |
| 2177 | The EXTERNAL_RELOCS are a buffer where the external form of the |
| 2178 | relocations should be stored. |
| 2179 | |
| 2180 | Returns false if something goes wrong. */ |
| 2181 | |
| 2182 | static boolean |
| 2183 | elf_link_read_relocs_from_section (abfd, shdr, external_relocs, |
| 2184 | internal_relocs) |
| 2185 | bfd *abfd; |
| 2186 | Elf_Internal_Shdr *shdr; |
| 2187 | PTR external_relocs; |
| 2188 | Elf_Internal_Rela *internal_relocs; |
| 2189 | { |
| 2190 | struct elf_backend_data *bed; |
| 2191 | |
| 2192 | /* If there aren't any relocations, that's OK. */ |
| 2193 | if (!shdr) |
| 2194 | return true; |
| 2195 | |
| 2196 | /* Position ourselves at the start of the section. */ |
| 2197 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) |
| 2198 | return false; |
| 2199 | |
| 2200 | /* Read the relocations. */ |
| 2201 | if (bfd_read (external_relocs, 1, shdr->sh_size, abfd) |
| 2202 | != shdr->sh_size) |
| 2203 | return false; |
| 2204 | |
| 2205 | bed = get_elf_backend_data (abfd); |
| 2206 | |
| 2207 | /* Convert the external relocations to the internal format. */ |
| 2208 | if (shdr->sh_entsize == sizeof (Elf_External_Rel)) |
| 2209 | { |
| 2210 | Elf_External_Rel *erel; |
| 2211 | Elf_External_Rel *erelend; |
| 2212 | Elf_Internal_Rela *irela; |
| 2213 | Elf_Internal_Rel *irel; |
| 2214 | |
| 2215 | erel = (Elf_External_Rel *) external_relocs; |
| 2216 | erelend = erel + shdr->sh_size / shdr->sh_entsize; |
| 2217 | irela = internal_relocs; |
| 2218 | irel = bfd_alloc (abfd, (bed->s->int_rels_per_ext_rel |
| 2219 | * sizeof (Elf_Internal_Rel))); |
| 2220 | for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel) |
| 2221 | { |
| 2222 | unsigned char i; |
| 2223 | |
| 2224 | if (bed->s->swap_reloc_in) |
| 2225 | (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel); |
| 2226 | else |
| 2227 | elf_swap_reloc_in (abfd, erel, irel); |
| 2228 | |
| 2229 | for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i) |
| 2230 | { |
| 2231 | irela[i].r_offset = irel[i].r_offset; |
| 2232 | irela[i].r_info = irel[i].r_info; |
| 2233 | irela[i].r_addend = 0; |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | else |
| 2238 | { |
| 2239 | Elf_External_Rela *erela; |
| 2240 | Elf_External_Rela *erelaend; |
| 2241 | Elf_Internal_Rela *irela; |
| 2242 | |
| 2243 | BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela)); |
| 2244 | |
| 2245 | erela = (Elf_External_Rela *) external_relocs; |
| 2246 | erelaend = erela + shdr->sh_size / shdr->sh_entsize; |
| 2247 | irela = internal_relocs; |
| 2248 | for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel) |
| 2249 | { |
| 2250 | if (bed->s->swap_reloca_in) |
| 2251 | (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela); |
| 2252 | else |
| 2253 | elf_swap_reloca_in (abfd, erela, irela); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | return true; |
| 2258 | } |
| 2259 | |
| 2260 | /* Read and swap the relocs for a section O. They may have been |
| 2261 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are |
| 2262 | not NULL, they are used as buffers to read into. They are known to |
| 2263 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, |
| 2264 | the return value is allocated using either malloc or bfd_alloc, |
| 2265 | according to the KEEP_MEMORY argument. If O has two relocation |
| 2266 | sections (both REL and RELA relocations), then the REL_HDR |
| 2267 | relocations will appear first in INTERNAL_RELOCS, followed by the |
| 2268 | REL_HDR2 relocations. */ |
| 2269 | |
| 2270 | Elf_Internal_Rela * |
| 2271 | NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs, |
| 2272 | keep_memory) |
| 2273 | bfd *abfd; |
| 2274 | asection *o; |
| 2275 | PTR external_relocs; |
| 2276 | Elf_Internal_Rela *internal_relocs; |
| 2277 | boolean keep_memory; |
| 2278 | { |
| 2279 | Elf_Internal_Shdr *rel_hdr; |
| 2280 | PTR alloc1 = NULL; |
| 2281 | Elf_Internal_Rela *alloc2 = NULL; |
| 2282 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
| 2283 | |
| 2284 | if (elf_section_data (o)->relocs != NULL) |
| 2285 | return elf_section_data (o)->relocs; |
| 2286 | |
| 2287 | if (o->reloc_count == 0) |
| 2288 | return NULL; |
| 2289 | |
| 2290 | rel_hdr = &elf_section_data (o)->rel_hdr; |
| 2291 | |
| 2292 | if (internal_relocs == NULL) |
| 2293 | { |
| 2294 | size_t size; |
| 2295 | |
| 2296 | size = (o->reloc_count * bed->s->int_rels_per_ext_rel |
| 2297 | * sizeof (Elf_Internal_Rela)); |
| 2298 | if (keep_memory) |
| 2299 | internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
| 2300 | else |
| 2301 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
| 2302 | if (internal_relocs == NULL) |
| 2303 | goto error_return; |
| 2304 | } |
| 2305 | |
| 2306 | if (external_relocs == NULL) |
| 2307 | { |
| 2308 | size_t size = (size_t) rel_hdr->sh_size; |
| 2309 | |
| 2310 | if (elf_section_data (o)->rel_hdr2) |
| 2311 | size += (size_t) elf_section_data (o)->rel_hdr2->sh_size; |
| 2312 | alloc1 = (PTR) bfd_malloc (size); |
| 2313 | if (alloc1 == NULL) |
| 2314 | goto error_return; |
| 2315 | external_relocs = alloc1; |
| 2316 | } |
| 2317 | |
| 2318 | if (!elf_link_read_relocs_from_section (abfd, rel_hdr, |
| 2319 | external_relocs, |
| 2320 | internal_relocs)) |
| 2321 | goto error_return; |
| 2322 | if (!elf_link_read_relocs_from_section |
| 2323 | (abfd, |
| 2324 | elf_section_data (o)->rel_hdr2, |
| 2325 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, |
| 2326 | internal_relocs + (rel_hdr->sh_size / rel_hdr->sh_entsize |
| 2327 | * bed->s->int_rels_per_ext_rel))) |
| 2328 | goto error_return; |
| 2329 | |
| 2330 | /* Cache the results for next time, if we can. */ |
| 2331 | if (keep_memory) |
| 2332 | elf_section_data (o)->relocs = internal_relocs; |
| 2333 | |
| 2334 | if (alloc1 != NULL) |
| 2335 | free (alloc1); |
| 2336 | |
| 2337 | /* Don't free alloc2, since if it was allocated we are passing it |
| 2338 | back (under the name of internal_relocs). */ |
| 2339 | |
| 2340 | return internal_relocs; |
| 2341 | |
| 2342 | error_return: |
| 2343 | if (alloc1 != NULL) |
| 2344 | free (alloc1); |
| 2345 | if (alloc2 != NULL) |
| 2346 | free (alloc2); |
| 2347 | return NULL; |
| 2348 | } |
| 2349 | \f |
| 2350 | |
| 2351 | /* Record an assignment to a symbol made by a linker script. We need |
| 2352 | this in case some dynamic object refers to this symbol. */ |
| 2353 | |
| 2354 | /*ARGSUSED*/ |
| 2355 | boolean |
| 2356 | NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide) |
| 2357 | bfd *output_bfd ATTRIBUTE_UNUSED; |
| 2358 | struct bfd_link_info *info; |
| 2359 | const char *name; |
| 2360 | boolean provide; |
| 2361 | { |
| 2362 | struct elf_link_hash_entry *h; |
| 2363 | |
| 2364 | if (info->hash->creator->flavour != bfd_target_elf_flavour) |
| 2365 | return true; |
| 2366 | |
| 2367 | h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false); |
| 2368 | if (h == NULL) |
| 2369 | return false; |
| 2370 | |
| 2371 | if (h->root.type == bfd_link_hash_new) |
| 2372 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; |
| 2373 | |
| 2374 | /* If this symbol is being provided by the linker script, and it is |
| 2375 | currently defined by a dynamic object, but not by a regular |
| 2376 | object, then mark it as undefined so that the generic linker will |
| 2377 | force the correct value. */ |
| 2378 | if (provide |
| 2379 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
| 2380 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) |
| 2381 | h->root.type = bfd_link_hash_undefined; |
| 2382 | |
| 2383 | /* If this symbol is not being provided by the linker script, and it is |
| 2384 | currently defined by a dynamic object, but not by a regular object, |
| 2385 | then clear out any version information because the symbol will not be |
| 2386 | associated with the dynamic object any more. */ |
| 2387 | if (!provide |
| 2388 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
| 2389 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) |
| 2390 | h->verinfo.verdef = NULL; |
| 2391 | |
| 2392 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
| 2393 | |
| 2394 | /* When possible, keep the original type of the symbol */ |
| 2395 | if (h->type == STT_NOTYPE) |
| 2396 | h->type = STT_OBJECT; |
| 2397 | |
| 2398 | if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC |
| 2399 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0 |
| 2400 | || info->shared) |
| 2401 | && h->dynindx == -1) |
| 2402 | { |
| 2403 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) |
| 2404 | return false; |
| 2405 | |
| 2406 | /* If this is a weak defined symbol, and we know a corresponding |
| 2407 | real symbol from the same dynamic object, make sure the real |
| 2408 | symbol is also made into a dynamic symbol. */ |
| 2409 | if (h->weakdef != NULL |
| 2410 | && h->weakdef->dynindx == -1) |
| 2411 | { |
| 2412 | if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) |
| 2413 | return false; |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | return true; |
| 2418 | } |
| 2419 | \f |
| 2420 | /* This structure is used to pass information to |
| 2421 | elf_link_assign_sym_version. */ |
| 2422 | |
| 2423 | struct elf_assign_sym_version_info |
| 2424 | { |
| 2425 | /* Output BFD. */ |
| 2426 | bfd *output_bfd; |
| 2427 | /* General link information. */ |
| 2428 | struct bfd_link_info *info; |
| 2429 | /* Version tree. */ |
| 2430 | struct bfd_elf_version_tree *verdefs; |
| 2431 | /* Whether we are exporting all dynamic symbols. */ |
| 2432 | boolean export_dynamic; |
| 2433 | /* Whether we had a failure. */ |
| 2434 | boolean failed; |
| 2435 | }; |
| 2436 | |
| 2437 | /* This structure is used to pass information to |
| 2438 | elf_link_find_version_dependencies. */ |
| 2439 | |
| 2440 | struct elf_find_verdep_info |
| 2441 | { |
| 2442 | /* Output BFD. */ |
| 2443 | bfd *output_bfd; |
| 2444 | /* General link information. */ |
| 2445 | struct bfd_link_info *info; |
| 2446 | /* The number of dependencies. */ |
| 2447 | unsigned int vers; |
| 2448 | /* Whether we had a failure. */ |
| 2449 | boolean failed; |
| 2450 | }; |
| 2451 | |
| 2452 | /* Array used to determine the number of hash table buckets to use |
| 2453 | based on the number of symbols there are. If there are fewer than |
| 2454 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, |
| 2455 | fewer than 37 we use 17 buckets, and so forth. We never use more |
| 2456 | than 32771 buckets. */ |
| 2457 | |
| 2458 | static const size_t elf_buckets[] = |
| 2459 | { |
| 2460 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, |
| 2461 | 16411, 32771, 0 |
| 2462 | }; |
| 2463 | |
| 2464 | /* Compute bucket count for hashing table. We do not use a static set |
| 2465 | of possible tables sizes anymore. Instead we determine for all |
| 2466 | possible reasonable sizes of the table the outcome (i.e., the |
| 2467 | number of collisions etc) and choose the best solution. The |
| 2468 | weighting functions are not too simple to allow the table to grow |
| 2469 | without bounds. Instead one of the weighting factors is the size. |
| 2470 | Therefore the result is always a good payoff between few collisions |
| 2471 | (= short chain lengths) and table size. */ |
| 2472 | static size_t |
| 2473 | compute_bucket_count (info) |
| 2474 | struct bfd_link_info *info; |
| 2475 | { |
| 2476 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
| 2477 | size_t best_size = 0; |
| 2478 | unsigned long int *hashcodes; |
| 2479 | unsigned long int *hashcodesp; |
| 2480 | unsigned long int i; |
| 2481 | |
| 2482 | /* Compute the hash values for all exported symbols. At the same |
| 2483 | time store the values in an array so that we could use them for |
| 2484 | optimizations. */ |
| 2485 | hashcodes = (unsigned long int *) bfd_malloc (dynsymcount |
| 2486 | * sizeof (unsigned long int)); |
| 2487 | if (hashcodes == NULL) |
| 2488 | return 0; |
| 2489 | hashcodesp = hashcodes; |
| 2490 | |
| 2491 | /* Put all hash values in HASHCODES. */ |
| 2492 | elf_link_hash_traverse (elf_hash_table (info), |
| 2493 | elf_collect_hash_codes, &hashcodesp); |
| 2494 | |
| 2495 | /* We have a problem here. The following code to optimize the table |
| 2496 | size requires an integer type with more the 32 bits. If |
| 2497 | BFD_HOST_U_64_BIT is set we know about such a type. */ |
| 2498 | #ifdef BFD_HOST_U_64_BIT |
| 2499 | if (info->optimize == true) |
| 2500 | { |
| 2501 | unsigned long int nsyms = hashcodesp - hashcodes; |
| 2502 | size_t minsize; |
| 2503 | size_t maxsize; |
| 2504 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); |
| 2505 | unsigned long int *counts ; |
| 2506 | |
| 2507 | /* Possible optimization parameters: if we have NSYMS symbols we say |
| 2508 | that the hashing table must at least have NSYMS/4 and at most |
| 2509 | 2*NSYMS buckets. */ |
| 2510 | minsize = nsyms / 4; |
| 2511 | if (minsize == 0) |
| 2512 | minsize = 1; |
| 2513 | best_size = maxsize = nsyms * 2; |
| 2514 | |
| 2515 | /* Create array where we count the collisions in. We must use bfd_malloc |
| 2516 | since the size could be large. */ |
| 2517 | counts = (unsigned long int *) bfd_malloc (maxsize |
| 2518 | * sizeof (unsigned long int)); |
| 2519 | if (counts == NULL) |
| 2520 | { |
| 2521 | free (hashcodes); |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | /* Compute the "optimal" size for the hash table. The criteria is a |
| 2526 | minimal chain length. The minor criteria is (of course) the size |
| 2527 | of the table. */ |
| 2528 | for (i = minsize; i < maxsize; ++i) |
| 2529 | { |
| 2530 | /* Walk through the array of hashcodes and count the collisions. */ |
| 2531 | BFD_HOST_U_64_BIT max; |
| 2532 | unsigned long int j; |
| 2533 | unsigned long int fact; |
| 2534 | |
| 2535 | memset (counts, '\0', i * sizeof (unsigned long int)); |
| 2536 | |
| 2537 | /* Determine how often each hash bucket is used. */ |
| 2538 | for (j = 0; j < nsyms; ++j) |
| 2539 | ++counts[hashcodes[j] % i]; |
| 2540 | |
| 2541 | /* For the weight function we need some information about the |
| 2542 | pagesize on the target. This is information need not be 100% |
| 2543 | accurate. Since this information is not available (so far) we |
| 2544 | define it here to a reasonable default value. If it is crucial |
| 2545 | to have a better value some day simply define this value. */ |
| 2546 | # ifndef BFD_TARGET_PAGESIZE |
| 2547 | # define BFD_TARGET_PAGESIZE (4096) |
| 2548 | # endif |
| 2549 | |
| 2550 | /* We in any case need 2 + NSYMS entries for the size values and |
| 2551 | the chains. */ |
| 2552 | max = (2 + nsyms) * (ARCH_SIZE / 8); |
| 2553 | |
| 2554 | # if 1 |
| 2555 | /* Variant 1: optimize for short chains. We add the squares |
| 2556 | of all the chain lengths (which favous many small chain |
| 2557 | over a few long chains). */ |
| 2558 | for (j = 0; j < i; ++j) |
| 2559 | max += counts[j] * counts[j]; |
| 2560 | |
| 2561 | /* This adds penalties for the overall size of the table. */ |
| 2562 | fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1; |
| 2563 | max *= fact * fact; |
| 2564 | # else |
| 2565 | /* Variant 2: Optimize a lot more for small table. Here we |
| 2566 | also add squares of the size but we also add penalties for |
| 2567 | empty slots (the +1 term). */ |
| 2568 | for (j = 0; j < i; ++j) |
| 2569 | max += (1 + counts[j]) * (1 + counts[j]); |
| 2570 | |
| 2571 | /* The overall size of the table is considered, but not as |
| 2572 | strong as in variant 1, where it is squared. */ |
| 2573 | fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1; |
| 2574 | max *= fact; |
| 2575 | # endif |
| 2576 | |
| 2577 | /* Compare with current best results. */ |
| 2578 | if (max < best_chlen) |
| 2579 | { |
| 2580 | best_chlen = max; |
| 2581 | best_size = i; |
| 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | free (counts); |
| 2586 | } |
| 2587 | else |
| 2588 | #endif /* defined (BFD_HOST_U_64_BIT) */ |
| 2589 | { |
| 2590 | /* This is the fallback solution if no 64bit type is available or if we |
| 2591 | are not supposed to spend much time on optimizations. We select the |
| 2592 | bucket count using a fixed set of numbers. */ |
| 2593 | for (i = 0; elf_buckets[i] != 0; i++) |
| 2594 | { |
| 2595 | best_size = elf_buckets[i]; |
| 2596 | if (dynsymcount < elf_buckets[i + 1]) |
| 2597 | break; |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | /* Free the arrays we needed. */ |
| 2602 | free (hashcodes); |
| 2603 | |
| 2604 | return best_size; |
| 2605 | } |
| 2606 | |
| 2607 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
| 2608 | called by the ELF linker emulation before_allocation routine. We |
| 2609 | must set the sizes of the sections before the linker sets the |
| 2610 | addresses of the various sections. */ |
| 2611 | |
| 2612 | boolean |
| 2613 | NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath, |
| 2614 | export_dynamic, filter_shlib, |
| 2615 | auxiliary_filters, info, sinterpptr, |
| 2616 | verdefs) |
| 2617 | bfd *output_bfd; |
| 2618 | const char *soname; |
| 2619 | const char *rpath; |
| 2620 | boolean export_dynamic; |
| 2621 | const char *filter_shlib; |
| 2622 | const char * const *auxiliary_filters; |
| 2623 | struct bfd_link_info *info; |
| 2624 | asection **sinterpptr; |
| 2625 | struct bfd_elf_version_tree *verdefs; |
| 2626 | { |
| 2627 | bfd_size_type soname_indx; |
| 2628 | bfd *dynobj; |
| 2629 | struct elf_backend_data *bed; |
| 2630 | struct elf_assign_sym_version_info asvinfo; |
| 2631 | |
| 2632 | *sinterpptr = NULL; |
| 2633 | |
| 2634 | soname_indx = (bfd_size_type) -1; |
| 2635 | |
| 2636 | if (info->hash->creator->flavour != bfd_target_elf_flavour) |
| 2637 | return true; |
| 2638 | |
| 2639 | /* The backend may have to create some sections regardless of whether |
| 2640 | we're dynamic or not. */ |
| 2641 | bed = get_elf_backend_data (output_bfd); |
| 2642 | if (bed->elf_backend_always_size_sections |
| 2643 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) |
| 2644 | return false; |
| 2645 | |
| 2646 | dynobj = elf_hash_table (info)->dynobj; |
| 2647 | |
| 2648 | /* If there were no dynamic objects in the link, there is nothing to |
| 2649 | do here. */ |
| 2650 | if (dynobj == NULL) |
| 2651 | return true; |
| 2652 | |
| 2653 | /* If we are supposed to export all symbols into the dynamic symbol |
| 2654 | table (this is not the normal case), then do so. */ |
| 2655 | if (export_dynamic) |
| 2656 | { |
| 2657 | struct elf_info_failed eif; |
| 2658 | |
| 2659 | eif.failed = false; |
| 2660 | eif.info = info; |
| 2661 | elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol, |
| 2662 | (PTR) &eif); |
| 2663 | if (eif.failed) |
| 2664 | return false; |
| 2665 | } |
| 2666 | |
| 2667 | if (elf_hash_table (info)->dynamic_sections_created) |
| 2668 | { |
| 2669 | struct elf_info_failed eif; |
| 2670 | struct elf_link_hash_entry *h; |
| 2671 | bfd_size_type strsize; |
| 2672 | |
| 2673 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); |
| 2674 | BFD_ASSERT (*sinterpptr != NULL || info->shared); |
| 2675 | |
| 2676 | if (soname != NULL) |
| 2677 | { |
| 2678 | soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
| 2679 | soname, true, true); |
| 2680 | if (soname_indx == (bfd_size_type) -1 |
| 2681 | || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) |
| 2682 | return false; |
| 2683 | } |
| 2684 | |
| 2685 | if (info->symbolic) |
| 2686 | { |
| 2687 | if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) |
| 2688 | return false; |
| 2689 | } |
| 2690 | |
| 2691 | if (rpath != NULL) |
| 2692 | { |
| 2693 | bfd_size_type indx; |
| 2694 | |
| 2695 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath, |
| 2696 | true, true); |
| 2697 | if (indx == (bfd_size_type) -1 |
| 2698 | || ! elf_add_dynamic_entry (info, DT_RPATH, indx)) |
| 2699 | return false; |
| 2700 | } |
| 2701 | |
| 2702 | if (filter_shlib != NULL) |
| 2703 | { |
| 2704 | bfd_size_type indx; |
| 2705 | |
| 2706 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
| 2707 | filter_shlib, true, true); |
| 2708 | if (indx == (bfd_size_type) -1 |
| 2709 | || ! elf_add_dynamic_entry (info, DT_FILTER, indx)) |
| 2710 | return false; |
| 2711 | } |
| 2712 | |
| 2713 | if (auxiliary_filters != NULL) |
| 2714 | { |
| 2715 | const char * const *p; |
| 2716 | |
| 2717 | for (p = auxiliary_filters; *p != NULL; p++) |
| 2718 | { |
| 2719 | bfd_size_type indx; |
| 2720 | |
| 2721 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
| 2722 | *p, true, true); |
| 2723 | if (indx == (bfd_size_type) -1 |
| 2724 | || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) |
| 2725 | return false; |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | /* Attach all the symbols to their version information. */ |
| 2730 | asvinfo.output_bfd = output_bfd; |
| 2731 | asvinfo.info = info; |
| 2732 | asvinfo.verdefs = verdefs; |
| 2733 | asvinfo.export_dynamic = export_dynamic; |
| 2734 | asvinfo.failed = false; |
| 2735 | |
| 2736 | elf_link_hash_traverse (elf_hash_table (info), |
| 2737 | elf_link_assign_sym_version, |
| 2738 | (PTR) &asvinfo); |
| 2739 | if (asvinfo.failed) |
| 2740 | return false; |
| 2741 | |
| 2742 | /* Find all symbols which were defined in a dynamic object and make |
| 2743 | the backend pick a reasonable value for them. */ |
| 2744 | eif.failed = false; |
| 2745 | eif.info = info; |
| 2746 | elf_link_hash_traverse (elf_hash_table (info), |
| 2747 | elf_adjust_dynamic_symbol, |
| 2748 | (PTR) &eif); |
| 2749 | if (eif.failed) |
| 2750 | return false; |
| 2751 | |
| 2752 | /* Add some entries to the .dynamic section. We fill in some of the |
| 2753 | values later, in elf_bfd_final_link, but we must add the entries |
| 2754 | now so that we know the final size of the .dynamic section. */ |
| 2755 | |
| 2756 | /* If there are initialization and/or finalization functions to |
| 2757 | call then add the corresponding DT_INIT/DT_FINI entries. */ |
| 2758 | h = (info->init_function |
| 2759 | ? elf_link_hash_lookup (elf_hash_table (info), |
| 2760 | info->init_function, false, |
| 2761 | false, false) |
| 2762 | : NULL); |
| 2763 | if (h != NULL |
| 2764 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR |
| 2765 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) |
| 2766 | { |
| 2767 | if (! elf_add_dynamic_entry (info, DT_INIT, 0)) |
| 2768 | return false; |
| 2769 | } |
| 2770 | h = (info->fini_function |
| 2771 | ? elf_link_hash_lookup (elf_hash_table (info), |
| 2772 | info->fini_function, false, |
| 2773 | false, false) |
| 2774 | : NULL); |
| 2775 | if (h != NULL |
| 2776 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR |
| 2777 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) |
| 2778 | { |
| 2779 | if (! elf_add_dynamic_entry (info, DT_FINI, 0)) |
| 2780 | return false; |
| 2781 | } |
| 2782 | |
| 2783 | strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr); |
| 2784 | if (! elf_add_dynamic_entry (info, DT_HASH, 0) |
| 2785 | || ! elf_add_dynamic_entry (info, DT_STRTAB, 0) |
| 2786 | || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0) |
| 2787 | || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize) |
| 2788 | || ! elf_add_dynamic_entry (info, DT_SYMENT, |
| 2789 | sizeof (Elf_External_Sym))) |
| 2790 | return false; |
| 2791 | } |
| 2792 | |
| 2793 | /* The backend must work out the sizes of all the other dynamic |
| 2794 | sections. */ |
| 2795 | if (bed->elf_backend_size_dynamic_sections |
| 2796 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) |
| 2797 | return false; |
| 2798 | |
| 2799 | if (elf_hash_table (info)->dynamic_sections_created) |
| 2800 | { |
| 2801 | size_t dynsymcount; |
| 2802 | asection *s; |
| 2803 | size_t bucketcount = 0; |
| 2804 | Elf_Internal_Sym isym; |
| 2805 | size_t hash_entry_size; |
| 2806 | |
| 2807 | /* Set up the version definition section. */ |
| 2808 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); |
| 2809 | BFD_ASSERT (s != NULL); |
| 2810 | |
| 2811 | /* We may have created additional version definitions if we are |
| 2812 | just linking a regular application. */ |
| 2813 | verdefs = asvinfo.verdefs; |
| 2814 | |
| 2815 | if (verdefs == NULL) |
| 2816 | _bfd_strip_section_from_output (s); |
| 2817 | else |
| 2818 | { |
| 2819 | unsigned int cdefs; |
| 2820 | bfd_size_type size; |
| 2821 | struct bfd_elf_version_tree *t; |
| 2822 | bfd_byte *p; |
| 2823 | Elf_Internal_Verdef def; |
| 2824 | Elf_Internal_Verdaux defaux; |
| 2825 | |
| 2826 | cdefs = 0; |
| 2827 | size = 0; |
| 2828 | |
| 2829 | /* Make space for the base version. */ |
| 2830 | size += sizeof (Elf_External_Verdef); |
| 2831 | size += sizeof (Elf_External_Verdaux); |
| 2832 | ++cdefs; |
| 2833 | |
| 2834 | for (t = verdefs; t != NULL; t = t->next) |
| 2835 | { |
| 2836 | struct bfd_elf_version_deps *n; |
| 2837 | |
| 2838 | size += sizeof (Elf_External_Verdef); |
| 2839 | size += sizeof (Elf_External_Verdaux); |
| 2840 | ++cdefs; |
| 2841 | |
| 2842 | for (n = t->deps; n != NULL; n = n->next) |
| 2843 | size += sizeof (Elf_External_Verdaux); |
| 2844 | } |
| 2845 | |
| 2846 | s->_raw_size = size; |
| 2847 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); |
| 2848 | if (s->contents == NULL && s->_raw_size != 0) |
| 2849 | return false; |
| 2850 | |
| 2851 | /* Fill in the version definition section. */ |
| 2852 | |
| 2853 | p = s->contents; |
| 2854 | |
| 2855 | def.vd_version = VER_DEF_CURRENT; |
| 2856 | def.vd_flags = VER_FLG_BASE; |
| 2857 | def.vd_ndx = 1; |
| 2858 | def.vd_cnt = 1; |
| 2859 | def.vd_aux = sizeof (Elf_External_Verdef); |
| 2860 | def.vd_next = (sizeof (Elf_External_Verdef) |
| 2861 | + sizeof (Elf_External_Verdaux)); |
| 2862 | |
| 2863 | if (soname_indx != (bfd_size_type) -1) |
| 2864 | { |
| 2865 | def.vd_hash = bfd_elf_hash (soname); |
| 2866 | defaux.vda_name = soname_indx; |
| 2867 | } |
| 2868 | else |
| 2869 | { |
| 2870 | const char *name; |
| 2871 | bfd_size_type indx; |
| 2872 | |
| 2873 | name = output_bfd->filename; |
| 2874 | def.vd_hash = bfd_elf_hash (name); |
| 2875 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
| 2876 | name, true, false); |
| 2877 | if (indx == (bfd_size_type) -1) |
| 2878 | return false; |
| 2879 | defaux.vda_name = indx; |
| 2880 | } |
| 2881 | defaux.vda_next = 0; |
| 2882 | |
| 2883 | _bfd_elf_swap_verdef_out (output_bfd, &def, |
| 2884 | (Elf_External_Verdef *)p); |
| 2885 | p += sizeof (Elf_External_Verdef); |
| 2886 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
| 2887 | (Elf_External_Verdaux *) p); |
| 2888 | p += sizeof (Elf_External_Verdaux); |
| 2889 | |
| 2890 | for (t = verdefs; t != NULL; t = t->next) |
| 2891 | { |
| 2892 | unsigned int cdeps; |
| 2893 | struct bfd_elf_version_deps *n; |
| 2894 | struct elf_link_hash_entry *h; |
| 2895 | |
| 2896 | cdeps = 0; |
| 2897 | for (n = t->deps; n != NULL; n = n->next) |
| 2898 | ++cdeps; |
| 2899 | |
| 2900 | /* Add a symbol representing this version. */ |
| 2901 | h = NULL; |
| 2902 | if (! (_bfd_generic_link_add_one_symbol |
| 2903 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, |
| 2904 | (bfd_vma) 0, (const char *) NULL, false, |
| 2905 | get_elf_backend_data (dynobj)->collect, |
| 2906 | (struct bfd_link_hash_entry **) &h))) |
| 2907 | return false; |
| 2908 | h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF; |
| 2909 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
| 2910 | h->type = STT_OBJECT; |
| 2911 | h->verinfo.vertree = t; |
| 2912 | |
| 2913 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) |
| 2914 | return false; |
| 2915 | |
| 2916 | def.vd_version = VER_DEF_CURRENT; |
| 2917 | def.vd_flags = 0; |
| 2918 | if (t->globals == NULL && t->locals == NULL && ! t->used) |
| 2919 | def.vd_flags |= VER_FLG_WEAK; |
| 2920 | def.vd_ndx = t->vernum + 1; |
| 2921 | def.vd_cnt = cdeps + 1; |
| 2922 | def.vd_hash = bfd_elf_hash (t->name); |
| 2923 | def.vd_aux = sizeof (Elf_External_Verdef); |
| 2924 | if (t->next != NULL) |
| 2925 | def.vd_next = (sizeof (Elf_External_Verdef) |
| 2926 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); |
| 2927 | else |
| 2928 | def.vd_next = 0; |
| 2929 | |
| 2930 | _bfd_elf_swap_verdef_out (output_bfd, &def, |
| 2931 | (Elf_External_Verdef *) p); |
| 2932 | p += sizeof (Elf_External_Verdef); |
| 2933 | |
| 2934 | defaux.vda_name = h->dynstr_index; |
| 2935 | if (t->deps == NULL) |
| 2936 | defaux.vda_next = 0; |
| 2937 | else |
| 2938 | defaux.vda_next = sizeof (Elf_External_Verdaux); |
| 2939 | t->name_indx = defaux.vda_name; |
| 2940 | |
| 2941 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
| 2942 | (Elf_External_Verdaux *) p); |
| 2943 | p += sizeof (Elf_External_Verdaux); |
| 2944 | |
| 2945 | for (n = t->deps; n != NULL; n = n->next) |
| 2946 | { |
| 2947 | if (n->version_needed == NULL) |
| 2948 | { |
| 2949 | /* This can happen if there was an error in the |
| 2950 | version script. */ |
| 2951 | defaux.vda_name = 0; |
| 2952 | } |
| 2953 | else |
| 2954 | defaux.vda_name = n->version_needed->name_indx; |
| 2955 | if (n->next == NULL) |
| 2956 | defaux.vda_next = 0; |
| 2957 | else |
| 2958 | defaux.vda_next = sizeof (Elf_External_Verdaux); |
| 2959 | |
| 2960 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
| 2961 | (Elf_External_Verdaux *) p); |
| 2962 | p += sizeof (Elf_External_Verdaux); |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | if (! elf_add_dynamic_entry (info, DT_VERDEF, 0) |
| 2967 | || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) |
| 2968 | return false; |
| 2969 | |
| 2970 | elf_tdata (output_bfd)->cverdefs = cdefs; |
| 2971 | } |
| 2972 | |
| 2973 | /* Work out the size of the version reference section. */ |
| 2974 | |
| 2975 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); |
| 2976 | BFD_ASSERT (s != NULL); |
| 2977 | { |
| 2978 | struct elf_find_verdep_info sinfo; |
| 2979 | |
| 2980 | sinfo.output_bfd = output_bfd; |
| 2981 | sinfo.info = info; |
| 2982 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; |
| 2983 | if (sinfo.vers == 0) |
| 2984 | sinfo.vers = 1; |
| 2985 | sinfo.failed = false; |
| 2986 | |
| 2987 | elf_link_hash_traverse (elf_hash_table (info), |
| 2988 | elf_link_find_version_dependencies, |
| 2989 | (PTR) &sinfo); |
| 2990 | |
| 2991 | if (elf_tdata (output_bfd)->verref == NULL) |
| 2992 | _bfd_strip_section_from_output (s); |
| 2993 | else |
| 2994 | { |
| 2995 | Elf_Internal_Verneed *t; |
| 2996 | unsigned int size; |
| 2997 | unsigned int crefs; |
| 2998 | bfd_byte *p; |
| 2999 | |
| 3000 | /* Build the version definition section. */ |
| 3001 | size = 0; |
| 3002 | crefs = 0; |
| 3003 | for (t = elf_tdata (output_bfd)->verref; |
| 3004 | t != NULL; |
| 3005 | t = t->vn_nextref) |
| 3006 | { |
| 3007 | Elf_Internal_Vernaux *a; |
| 3008 | |
| 3009 | size += sizeof (Elf_External_Verneed); |
| 3010 | ++crefs; |
| 3011 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
| 3012 | size += sizeof (Elf_External_Vernaux); |
| 3013 | } |
| 3014 | |
| 3015 | s->_raw_size = size; |
| 3016 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, size); |
| 3017 | if (s->contents == NULL) |
| 3018 | return false; |
| 3019 | |
| 3020 | p = s->contents; |
| 3021 | for (t = elf_tdata (output_bfd)->verref; |
| 3022 | t != NULL; |
| 3023 | t = t->vn_nextref) |
| 3024 | { |
| 3025 | unsigned int caux; |
| 3026 | Elf_Internal_Vernaux *a; |
| 3027 | bfd_size_type indx; |
| 3028 | |
| 3029 | caux = 0; |
| 3030 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
| 3031 | ++caux; |
| 3032 | |
| 3033 | t->vn_version = VER_NEED_CURRENT; |
| 3034 | t->vn_cnt = caux; |
| 3035 | if (elf_dt_name (t->vn_bfd) != NULL) |
| 3036 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
| 3037 | elf_dt_name (t->vn_bfd), |
| 3038 | true, false); |
| 3039 | else |
| 3040 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
| 3041 | t->vn_bfd->filename, true, false); |
| 3042 | if (indx == (bfd_size_type) -1) |
| 3043 | return false; |
| 3044 | t->vn_file = indx; |
| 3045 | t->vn_aux = sizeof (Elf_External_Verneed); |
| 3046 | if (t->vn_nextref == NULL) |
| 3047 | t->vn_next = 0; |
| 3048 | else |
| 3049 | t->vn_next = (sizeof (Elf_External_Verneed) |
| 3050 | + caux * sizeof (Elf_External_Vernaux)); |
| 3051 | |
| 3052 | _bfd_elf_swap_verneed_out (output_bfd, t, |
| 3053 | (Elf_External_Verneed *) p); |
| 3054 | p += sizeof (Elf_External_Verneed); |
| 3055 | |
| 3056 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
| 3057 | { |
| 3058 | a->vna_hash = bfd_elf_hash (a->vna_nodename); |
| 3059 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
| 3060 | a->vna_nodename, true, false); |
| 3061 | if (indx == (bfd_size_type) -1) |
| 3062 | return false; |
| 3063 | a->vna_name = indx; |
| 3064 | if (a->vna_nextptr == NULL) |
| 3065 | a->vna_next = 0; |
| 3066 | else |
| 3067 | a->vna_next = sizeof (Elf_External_Vernaux); |
| 3068 | |
| 3069 | _bfd_elf_swap_vernaux_out (output_bfd, a, |
| 3070 | (Elf_External_Vernaux *) p); |
| 3071 | p += sizeof (Elf_External_Vernaux); |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | if (! elf_add_dynamic_entry (info, DT_VERNEED, 0) |
| 3076 | || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) |
| 3077 | return false; |
| 3078 | |
| 3079 | elf_tdata (output_bfd)->cverrefs = crefs; |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | /* Assign dynsym indicies. In a shared library we generate a |
| 3084 | section symbol for each output section, which come first. |
| 3085 | Next come all of the back-end allocated local dynamic syms, |
| 3086 | followed by the rest of the global symbols. */ |
| 3087 | |
| 3088 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); |
| 3089 | |
| 3090 | /* Work out the size of the symbol version section. */ |
| 3091 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); |
| 3092 | BFD_ASSERT (s != NULL); |
| 3093 | if (dynsymcount == 0 |
| 3094 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL)) |
| 3095 | { |
| 3096 | _bfd_strip_section_from_output (s); |
| 3097 | /* The DYNSYMCOUNT might have changed if we were going to |
| 3098 | output a dynamic symbol table entry for S. */ |
| 3099 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); |
| 3100 | } |
| 3101 | else |
| 3102 | { |
| 3103 | s->_raw_size = dynsymcount * sizeof (Elf_External_Versym); |
| 3104 | s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size); |
| 3105 | if (s->contents == NULL) |
| 3106 | return false; |
| 3107 | |
| 3108 | if (! elf_add_dynamic_entry (info, DT_VERSYM, 0)) |
| 3109 | return false; |
| 3110 | } |
| 3111 | |
| 3112 | /* Set the size of the .dynsym and .hash sections. We counted |
| 3113 | the number of dynamic symbols in elf_link_add_object_symbols. |
| 3114 | We will build the contents of .dynsym and .hash when we build |
| 3115 | the final symbol table, because until then we do not know the |
| 3116 | correct value to give the symbols. We built the .dynstr |
| 3117 | section as we went along in elf_link_add_object_symbols. */ |
| 3118 | s = bfd_get_section_by_name (dynobj, ".dynsym"); |
| 3119 | BFD_ASSERT (s != NULL); |
| 3120 | s->_raw_size = dynsymcount * sizeof (Elf_External_Sym); |
| 3121 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); |
| 3122 | if (s->contents == NULL && s->_raw_size != 0) |
| 3123 | return false; |
| 3124 | |
| 3125 | /* The first entry in .dynsym is a dummy symbol. */ |
| 3126 | isym.st_value = 0; |
| 3127 | isym.st_size = 0; |
| 3128 | isym.st_name = 0; |
| 3129 | isym.st_info = 0; |
| 3130 | isym.st_other = 0; |
| 3131 | isym.st_shndx = 0; |
| 3132 | elf_swap_symbol_out (output_bfd, &isym, |
| 3133 | (PTR) (Elf_External_Sym *) s->contents); |
| 3134 | |
| 3135 | /* Compute the size of the hashing table. As a side effect this |
| 3136 | computes the hash values for all the names we export. */ |
| 3137 | bucketcount = compute_bucket_count (info); |
| 3138 | |
| 3139 | s = bfd_get_section_by_name (dynobj, ".hash"); |
| 3140 | BFD_ASSERT (s != NULL); |
| 3141 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; |
| 3142 | s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size); |
| 3143 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); |
| 3144 | if (s->contents == NULL) |
| 3145 | return false; |
| 3146 | memset (s->contents, 0, (size_t) s->_raw_size); |
| 3147 | |
| 3148 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); |
| 3149 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, |
| 3150 | s->contents + hash_entry_size); |
| 3151 | |
| 3152 | elf_hash_table (info)->bucketcount = bucketcount; |
| 3153 | |
| 3154 | s = bfd_get_section_by_name (dynobj, ".dynstr"); |
| 3155 | BFD_ASSERT (s != NULL); |
| 3156 | s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr); |
| 3157 | |
| 3158 | if (! elf_add_dynamic_entry (info, DT_NULL, 0)) |
| 3159 | return false; |
| 3160 | } |
| 3161 | |
| 3162 | return true; |
| 3163 | } |
| 3164 | \f |
| 3165 | /* Fix up the flags for a symbol. This handles various cases which |
| 3166 | can only be fixed after all the input files are seen. This is |
| 3167 | currently called by both adjust_dynamic_symbol and |
| 3168 | assign_sym_version, which is unnecessary but perhaps more robust in |
| 3169 | the face of future changes. */ |
| 3170 | |
| 3171 | static boolean |
| 3172 | elf_fix_symbol_flags (h, eif) |
| 3173 | struct elf_link_hash_entry *h; |
| 3174 | struct elf_info_failed *eif; |
| 3175 | { |
| 3176 | /* If this symbol was mentioned in a non-ELF file, try to set |
| 3177 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to |
| 3178 | permit a non-ELF file to correctly refer to a symbol defined in |
| 3179 | an ELF dynamic object. */ |
| 3180 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0) |
| 3181 | { |
| 3182 | if (h->root.type != bfd_link_hash_defined |
| 3183 | && h->root.type != bfd_link_hash_defweak) |
| 3184 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR |
| 3185 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); |
| 3186 | else |
| 3187 | { |
| 3188 | if (h->root.u.def.section->owner != NULL |
| 3189 | && (bfd_get_flavour (h->root.u.def.section->owner) |
| 3190 | == bfd_target_elf_flavour)) |
| 3191 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR |
| 3192 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); |
| 3193 | else |
| 3194 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
| 3195 | } |
| 3196 | |
| 3197 | if (h->dynindx == -1 |
| 3198 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
| 3199 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)) |
| 3200 | { |
| 3201 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
| 3202 | { |
| 3203 | eif->failed = true; |
| 3204 | return false; |
| 3205 | } |
| 3206 | } |
| 3207 | } |
| 3208 | else |
| 3209 | { |
| 3210 | /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol |
| 3211 | was first seen in a non-ELF file. Fortunately, if the symbol |
| 3212 | was first seen in an ELF file, we're probably OK unless the |
| 3213 | symbol was defined in a non-ELF file. Catch that case here. |
| 3214 | FIXME: We're still in trouble if the symbol was first seen in |
| 3215 | a dynamic object, and then later in a non-ELF regular object. */ |
| 3216 | if ((h->root.type == bfd_link_hash_defined |
| 3217 | || h->root.type == bfd_link_hash_defweak) |
| 3218 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 |
| 3219 | && (h->root.u.def.section->owner != NULL |
| 3220 | ? (bfd_get_flavour (h->root.u.def.section->owner) |
| 3221 | != bfd_target_elf_flavour) |
| 3222 | : (bfd_is_abs_section (h->root.u.def.section) |
| 3223 | && (h->elf_link_hash_flags |
| 3224 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0))) |
| 3225 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
| 3226 | } |
| 3227 | |
| 3228 | /* If this is a final link, and the symbol was defined as a common |
| 3229 | symbol in a regular object file, and there was no definition in |
| 3230 | any dynamic object, then the linker will have allocated space for |
| 3231 | the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR |
| 3232 | flag will not have been set. */ |
| 3233 | if (h->root.type == bfd_link_hash_defined |
| 3234 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 |
| 3235 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 |
| 3236 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 |
| 3237 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
| 3238 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
| 3239 | |
| 3240 | /* If -Bsymbolic was used (which means to bind references to global |
| 3241 | symbols to the definition within the shared object), and this |
| 3242 | symbol was defined in a regular object, then it actually doesn't |
| 3243 | need a PLT entry. */ |
| 3244 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0 |
| 3245 | && eif->info->shared |
| 3246 | && eif->info->symbolic |
| 3247 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) |
| 3248 | { |
| 3249 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT; |
| 3250 | h->plt.offset = (bfd_vma) -1; |
| 3251 | } |
| 3252 | |
| 3253 | /* If this is a weak defined symbol in a dynamic object, and we know |
| 3254 | the real definition in the dynamic object, copy interesting flags |
| 3255 | over to the real definition. */ |
| 3256 | if (h->weakdef != NULL) |
| 3257 | { |
| 3258 | struct elf_link_hash_entry *weakdef; |
| 3259 | |
| 3260 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
| 3261 | || h->root.type == bfd_link_hash_defweak); |
| 3262 | weakdef = h->weakdef; |
| 3263 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined |
| 3264 | || weakdef->root.type == bfd_link_hash_defweak); |
| 3265 | BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC); |
| 3266 | |
| 3267 | /* If the real definition is defined by a regular object file, |
| 3268 | don't do anything special. See the longer description in |
| 3269 | elf_adjust_dynamic_symbol, below. */ |
| 3270 | if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) |
| 3271 | h->weakdef = NULL; |
| 3272 | else |
| 3273 | weakdef->elf_link_hash_flags |= |
| 3274 | (h->elf_link_hash_flags |
| 3275 | & (ELF_LINK_HASH_REF_REGULAR |
| 3276 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK |
| 3277 | | ELF_LINK_NON_GOT_REF)); |
| 3278 | } |
| 3279 | |
| 3280 | return true; |
| 3281 | } |
| 3282 | |
| 3283 | /* Make the backend pick a good value for a dynamic symbol. This is |
| 3284 | called via elf_link_hash_traverse, and also calls itself |
| 3285 | recursively. */ |
| 3286 | |
| 3287 | static boolean |
| 3288 | elf_adjust_dynamic_symbol (h, data) |
| 3289 | struct elf_link_hash_entry *h; |
| 3290 | PTR data; |
| 3291 | { |
| 3292 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
| 3293 | bfd *dynobj; |
| 3294 | struct elf_backend_data *bed; |
| 3295 | |
| 3296 | /* Ignore indirect symbols. These are added by the versioning code. */ |
| 3297 | if (h->root.type == bfd_link_hash_indirect) |
| 3298 | return true; |
| 3299 | |
| 3300 | /* Fix the symbol flags. */ |
| 3301 | if (! elf_fix_symbol_flags (h, eif)) |
| 3302 | return false; |
| 3303 | |
| 3304 | /* If this symbol does not require a PLT entry, and it is not |
| 3305 | defined by a dynamic object, or is not referenced by a regular |
| 3306 | object, ignore it. We do have to handle a weak defined symbol, |
| 3307 | even if no regular object refers to it, if we decided to add it |
| 3308 | to the dynamic symbol table. FIXME: Do we normally need to worry |
| 3309 | about symbols which are defined by one dynamic object and |
| 3310 | referenced by another one? */ |
| 3311 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0 |
| 3312 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 |
| 3313 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 |
| 3314 | || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 |
| 3315 | && (h->weakdef == NULL || h->weakdef->dynindx == -1)))) |
| 3316 | { |
| 3317 | h->plt.offset = (bfd_vma) -1; |
| 3318 | return true; |
| 3319 | } |
| 3320 | |
| 3321 | /* If we've already adjusted this symbol, don't do it again. This |
| 3322 | can happen via a recursive call. */ |
| 3323 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0) |
| 3324 | return true; |
| 3325 | |
| 3326 | /* Don't look at this symbol again. Note that we must set this |
| 3327 | after checking the above conditions, because we may look at a |
| 3328 | symbol once, decide not to do anything, and then get called |
| 3329 | recursively later after REF_REGULAR is set below. */ |
| 3330 | h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED; |
| 3331 | |
| 3332 | /* If this is a weak definition, and we know a real definition, and |
| 3333 | the real symbol is not itself defined by a regular object file, |
| 3334 | then get a good value for the real definition. We handle the |
| 3335 | real symbol first, for the convenience of the backend routine. |
| 3336 | |
| 3337 | Note that there is a confusing case here. If the real definition |
| 3338 | is defined by a regular object file, we don't get the real symbol |
| 3339 | from the dynamic object, but we do get the weak symbol. If the |
| 3340 | processor backend uses a COPY reloc, then if some routine in the |
| 3341 | dynamic object changes the real symbol, we will not see that |
| 3342 | change in the corresponding weak symbol. This is the way other |
| 3343 | ELF linkers work as well, and seems to be a result of the shared |
| 3344 | library model. |
| 3345 | |
| 3346 | I will clarify this issue. Most SVR4 shared libraries define the |
| 3347 | variable _timezone and define timezone as a weak synonym. The |
| 3348 | tzset call changes _timezone. If you write |
| 3349 | extern int timezone; |
| 3350 | int _timezone = 5; |
| 3351 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } |
| 3352 | you might expect that, since timezone is a synonym for _timezone, |
| 3353 | the same number will print both times. However, if the processor |
| 3354 | backend uses a COPY reloc, then actually timezone will be copied |
| 3355 | into your process image, and, since you define _timezone |
| 3356 | yourself, _timezone will not. Thus timezone and _timezone will |
| 3357 | wind up at different memory locations. The tzset call will set |
| 3358 | _timezone, leaving timezone unchanged. */ |
| 3359 | |
| 3360 | if (h->weakdef != NULL) |
| 3361 | { |
| 3362 | /* If we get to this point, we know there is an implicit |
| 3363 | reference by a regular object file via the weak symbol H. |
| 3364 | FIXME: Is this really true? What if the traversal finds |
| 3365 | H->WEAKDEF before it finds H? */ |
| 3366 | h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; |
| 3367 | |
| 3368 | if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif)) |
| 3369 | return false; |
| 3370 | } |
| 3371 | |
| 3372 | /* If a symbol has no type and no size and does not require a PLT |
| 3373 | entry, then we are probably about to do the wrong thing here: we |
| 3374 | are probably going to create a COPY reloc for an empty object. |
| 3375 | This case can arise when a shared object is built with assembly |
| 3376 | code, and the assembly code fails to set the symbol type. */ |
| 3377 | if (h->size == 0 |
| 3378 | && h->type == STT_NOTYPE |
| 3379 | && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0) |
| 3380 | (*_bfd_error_handler) |
| 3381 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
| 3382 | h->root.root.string); |
| 3383 | |
| 3384 | dynobj = elf_hash_table (eif->info)->dynobj; |
| 3385 | bed = get_elf_backend_data (dynobj); |
| 3386 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
| 3387 | { |
| 3388 | eif->failed = true; |
| 3389 | return false; |
| 3390 | } |
| 3391 | |
| 3392 | return true; |
| 3393 | } |
| 3394 | \f |
| 3395 | /* This routine is used to export all defined symbols into the dynamic |
| 3396 | symbol table. It is called via elf_link_hash_traverse. */ |
| 3397 | |
| 3398 | static boolean |
| 3399 | elf_export_symbol (h, data) |
| 3400 | struct elf_link_hash_entry *h; |
| 3401 | PTR data; |
| 3402 | { |
| 3403 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
| 3404 | |
| 3405 | /* Ignore indirect symbols. These are added by the versioning code. */ |
| 3406 | if (h->root.type == bfd_link_hash_indirect) |
| 3407 | return true; |
| 3408 | |
| 3409 | if (h->dynindx == -1 |
| 3410 | && (h->elf_link_hash_flags |
| 3411 | & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0) |
| 3412 | { |
| 3413 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
| 3414 | { |
| 3415 | eif->failed = true; |
| 3416 | return false; |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | return true; |
| 3421 | } |
| 3422 | \f |
| 3423 | /* Look through the symbols which are defined in other shared |
| 3424 | libraries and referenced here. Update the list of version |
| 3425 | dependencies. This will be put into the .gnu.version_r section. |
| 3426 | This function is called via elf_link_hash_traverse. */ |
| 3427 | |
| 3428 | static boolean |
| 3429 | elf_link_find_version_dependencies (h, data) |
| 3430 | struct elf_link_hash_entry *h; |
| 3431 | PTR data; |
| 3432 | { |
| 3433 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
| 3434 | Elf_Internal_Verneed *t; |
| 3435 | Elf_Internal_Vernaux *a; |
| 3436 | |
| 3437 | /* We only care about symbols defined in shared objects with version |
| 3438 | information. */ |
| 3439 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 |
| 3440 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 |
| 3441 | || h->dynindx == -1 |
| 3442 | || h->verinfo.verdef == NULL) |
| 3443 | return true; |
| 3444 | |
| 3445 | /* See if we already know about this version. */ |
| 3446 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) |
| 3447 | { |
| 3448 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) |
| 3449 | continue; |
| 3450 | |
| 3451 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
| 3452 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) |
| 3453 | return true; |
| 3454 | |
| 3455 | break; |
| 3456 | } |
| 3457 | |
| 3458 | /* This is a new version. Add it to tree we are building. */ |
| 3459 | |
| 3460 | if (t == NULL) |
| 3461 | { |
| 3462 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, sizeof *t); |
| 3463 | if (t == NULL) |
| 3464 | { |
| 3465 | rinfo->failed = true; |
| 3466 | return false; |
| 3467 | } |
| 3468 | |
| 3469 | t->vn_bfd = h->verinfo.verdef->vd_bfd; |
| 3470 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; |
| 3471 | elf_tdata (rinfo->output_bfd)->verref = t; |
| 3472 | } |
| 3473 | |
| 3474 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, sizeof *a); |
| 3475 | |
| 3476 | /* Note that we are copying a string pointer here, and testing it |
| 3477 | above. If bfd_elf_string_from_elf_section is ever changed to |
| 3478 | discard the string data when low in memory, this will have to be |
| 3479 | fixed. */ |
| 3480 | a->vna_nodename = h->verinfo.verdef->vd_nodename; |
| 3481 | |
| 3482 | a->vna_flags = h->verinfo.verdef->vd_flags; |
| 3483 | a->vna_nextptr = t->vn_auxptr; |
| 3484 | |
| 3485 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; |
| 3486 | ++rinfo->vers; |
| 3487 | |
| 3488 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; |
| 3489 | |
| 3490 | t->vn_auxptr = a; |
| 3491 | |
| 3492 | return true; |
| 3493 | } |
| 3494 | |
| 3495 | /* Figure out appropriate versions for all the symbols. We may not |
| 3496 | have the version number script until we have read all of the input |
| 3497 | files, so until that point we don't know which symbols should be |
| 3498 | local. This function is called via elf_link_hash_traverse. */ |
| 3499 | |
| 3500 | static boolean |
| 3501 | elf_link_assign_sym_version (h, data) |
| 3502 | struct elf_link_hash_entry *h; |
| 3503 | PTR data; |
| 3504 | { |
| 3505 | struct elf_assign_sym_version_info *sinfo = |
| 3506 | (struct elf_assign_sym_version_info *) data; |
| 3507 | struct bfd_link_info *info = sinfo->info; |
| 3508 | struct elf_info_failed eif; |
| 3509 | char *p; |
| 3510 | |
| 3511 | /* Fix the symbol flags. */ |
| 3512 | eif.failed = false; |
| 3513 | eif.info = info; |
| 3514 | if (! elf_fix_symbol_flags (h, &eif)) |
| 3515 | { |
| 3516 | if (eif.failed) |
| 3517 | sinfo->failed = true; |
| 3518 | return false; |
| 3519 | } |
| 3520 | |
| 3521 | /* We only need version numbers for symbols defined in regular |
| 3522 | objects. */ |
| 3523 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) |
| 3524 | return true; |
| 3525 | |
| 3526 | p = strchr (h->root.root.string, ELF_VER_CHR); |
| 3527 | if (p != NULL && h->verinfo.vertree == NULL) |
| 3528 | { |
| 3529 | struct bfd_elf_version_tree *t; |
| 3530 | boolean hidden; |
| 3531 | |
| 3532 | hidden = true; |
| 3533 | |
| 3534 | /* There are two consecutive ELF_VER_CHR characters if this is |
| 3535 | not a hidden symbol. */ |
| 3536 | ++p; |
| 3537 | if (*p == ELF_VER_CHR) |
| 3538 | { |
| 3539 | hidden = false; |
| 3540 | ++p; |
| 3541 | } |
| 3542 | |
| 3543 | /* If there is no version string, we can just return out. */ |
| 3544 | if (*p == '\0') |
| 3545 | { |
| 3546 | if (hidden) |
| 3547 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; |
| 3548 | return true; |
| 3549 | } |
| 3550 | |
| 3551 | /* Look for the version. If we find it, it is no longer weak. */ |
| 3552 | for (t = sinfo->verdefs; t != NULL; t = t->next) |
| 3553 | { |
| 3554 | if (strcmp (t->name, p) == 0) |
| 3555 | { |
| 3556 | int len; |
| 3557 | char *alc; |
| 3558 | struct bfd_elf_version_expr *d; |
| 3559 | |
| 3560 | len = p - h->root.root.string; |
| 3561 | alc = bfd_alloc (sinfo->output_bfd, len); |
| 3562 | if (alc == NULL) |
| 3563 | return false; |
| 3564 | strncpy (alc, h->root.root.string, len - 1); |
| 3565 | alc[len - 1] = '\0'; |
| 3566 | if (alc[len - 2] == ELF_VER_CHR) |
| 3567 | alc[len - 2] = '\0'; |
| 3568 | |
| 3569 | h->verinfo.vertree = t; |
| 3570 | t->used = true; |
| 3571 | d = NULL; |
| 3572 | |
| 3573 | if (t->globals != NULL) |
| 3574 | { |
| 3575 | for (d = t->globals; d != NULL; d = d->next) |
| 3576 | if ((*d->match) (d, alc)) |
| 3577 | break; |
| 3578 | } |
| 3579 | |
| 3580 | /* See if there is anything to force this symbol to |
| 3581 | local scope. */ |
| 3582 | if (d == NULL && t->locals != NULL) |
| 3583 | { |
| 3584 | for (d = t->locals; d != NULL; d = d->next) |
| 3585 | { |
| 3586 | if ((*d->match) (d, alc)) |
| 3587 | { |
| 3588 | if (h->dynindx != -1 |
| 3589 | && info->shared |
| 3590 | && ! sinfo->export_dynamic) |
| 3591 | { |
| 3592 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
| 3593 | h->elf_link_hash_flags &=~ |
| 3594 | ELF_LINK_HASH_NEEDS_PLT; |
| 3595 | h->dynindx = -1; |
| 3596 | h->plt.offset = (bfd_vma) -1; |
| 3597 | /* FIXME: The name of the symbol has |
| 3598 | already been recorded in the dynamic |
| 3599 | string table section. */ |
| 3600 | } |
| 3601 | |
| 3602 | break; |
| 3603 | } |
| 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | bfd_release (sinfo->output_bfd, alc); |
| 3608 | break; |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | /* If we are building an application, we need to create a |
| 3613 | version node for this version. */ |
| 3614 | if (t == NULL && ! info->shared) |
| 3615 | { |
| 3616 | struct bfd_elf_version_tree **pp; |
| 3617 | int version_index; |
| 3618 | |
| 3619 | /* If we aren't going to export this symbol, we don't need |
| 3620 | to worry about it. */ |
| 3621 | if (h->dynindx == -1) |
| 3622 | return true; |
| 3623 | |
| 3624 | t = ((struct bfd_elf_version_tree *) |
| 3625 | bfd_alloc (sinfo->output_bfd, sizeof *t)); |
| 3626 | if (t == NULL) |
| 3627 | { |
| 3628 | sinfo->failed = true; |
| 3629 | return false; |
| 3630 | } |
| 3631 | |
| 3632 | t->next = NULL; |
| 3633 | t->name = p; |
| 3634 | t->globals = NULL; |
| 3635 | t->locals = NULL; |
| 3636 | t->deps = NULL; |
| 3637 | t->name_indx = (unsigned int) -1; |
| 3638 | t->used = true; |
| 3639 | |
| 3640 | version_index = 1; |
| 3641 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) |
| 3642 | ++version_index; |
| 3643 | t->vernum = version_index; |
| 3644 | |
| 3645 | *pp = t; |
| 3646 | |
| 3647 | h->verinfo.vertree = t; |
| 3648 | } |
| 3649 | else if (t == NULL) |
| 3650 | { |
| 3651 | /* We could not find the version for a symbol when |
| 3652 | generating a shared archive. Return an error. */ |
| 3653 | (*_bfd_error_handler) |
| 3654 | (_("%s: undefined versioned symbol name %s"), |
| 3655 | bfd_get_filename (sinfo->output_bfd), h->root.root.string); |
| 3656 | bfd_set_error (bfd_error_bad_value); |
| 3657 | sinfo->failed = true; |
| 3658 | return false; |
| 3659 | } |
| 3660 | |
| 3661 | if (hidden) |
| 3662 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; |
| 3663 | } |
| 3664 | |
| 3665 | /* If we don't have a version for this symbol, see if we can find |
| 3666 | something. */ |
| 3667 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) |
| 3668 | { |
| 3669 | struct bfd_elf_version_tree *t; |
| 3670 | struct bfd_elf_version_tree *deflt; |
| 3671 | struct bfd_elf_version_expr *d; |
| 3672 | |
| 3673 | /* See if can find what version this symbol is in. If the |
| 3674 | symbol is supposed to be local, then don't actually register |
| 3675 | it. */ |
| 3676 | deflt = NULL; |
| 3677 | for (t = sinfo->verdefs; t != NULL; t = t->next) |
| 3678 | { |
| 3679 | if (t->globals != NULL) |
| 3680 | { |
| 3681 | for (d = t->globals; d != NULL; d = d->next) |
| 3682 | { |
| 3683 | if ((*d->match) (d, h->root.root.string)) |
| 3684 | { |
| 3685 | h->verinfo.vertree = t; |
| 3686 | break; |
| 3687 | } |
| 3688 | } |
| 3689 | |
| 3690 | if (d != NULL) |
| 3691 | break; |
| 3692 | } |
| 3693 | |
| 3694 | if (t->locals != NULL) |
| 3695 | { |
| 3696 | for (d = t->locals; d != NULL; d = d->next) |
| 3697 | { |
| 3698 | if (d->pattern[0] == '*' && d->pattern[1] == '\0') |
| 3699 | deflt = t; |
| 3700 | else if ((*d->match) (d, h->root.root.string)) |
| 3701 | { |
| 3702 | h->verinfo.vertree = t; |
| 3703 | if (h->dynindx != -1 |
| 3704 | && info->shared |
| 3705 | && ! sinfo->export_dynamic) |
| 3706 | { |
| 3707 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
| 3708 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT; |
| 3709 | h->dynindx = -1; |
| 3710 | h->plt.offset = (bfd_vma) -1; |
| 3711 | /* FIXME: The name of the symbol has already |
| 3712 | been recorded in the dynamic string table |
| 3713 | section. */ |
| 3714 | } |
| 3715 | break; |
| 3716 | } |
| 3717 | } |
| 3718 | |
| 3719 | if (d != NULL) |
| 3720 | break; |
| 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | if (deflt != NULL && h->verinfo.vertree == NULL) |
| 3725 | { |
| 3726 | h->verinfo.vertree = deflt; |
| 3727 | if (h->dynindx != -1 |
| 3728 | && info->shared |
| 3729 | && ! sinfo->export_dynamic) |
| 3730 | { |
| 3731 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
| 3732 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT; |
| 3733 | h->dynindx = -1; |
| 3734 | h->plt.offset = (bfd_vma) -1; |
| 3735 | /* FIXME: The name of the symbol has already been |
| 3736 | recorded in the dynamic string table section. */ |
| 3737 | } |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | return true; |
| 3742 | } |
| 3743 | \f |
| 3744 | /* Final phase of ELF linker. */ |
| 3745 | |
| 3746 | /* A structure we use to avoid passing large numbers of arguments. */ |
| 3747 | |
| 3748 | struct elf_final_link_info |
| 3749 | { |
| 3750 | /* General link information. */ |
| 3751 | struct bfd_link_info *info; |
| 3752 | /* Output BFD. */ |
| 3753 | bfd *output_bfd; |
| 3754 | /* Symbol string table. */ |
| 3755 | struct bfd_strtab_hash *symstrtab; |
| 3756 | /* .dynsym section. */ |
| 3757 | asection *dynsym_sec; |
| 3758 | /* .hash section. */ |
| 3759 | asection *hash_sec; |
| 3760 | /* symbol version section (.gnu.version). */ |
| 3761 | asection *symver_sec; |
| 3762 | /* Buffer large enough to hold contents of any section. */ |
| 3763 | bfd_byte *contents; |
| 3764 | /* Buffer large enough to hold external relocs of any section. */ |
| 3765 | PTR external_relocs; |
| 3766 | /* Buffer large enough to hold internal relocs of any section. */ |
| 3767 | Elf_Internal_Rela *internal_relocs; |
| 3768 | /* Buffer large enough to hold external local symbols of any input |
| 3769 | BFD. */ |
| 3770 | Elf_External_Sym *external_syms; |
| 3771 | /* Buffer large enough to hold internal local symbols of any input |
| 3772 | BFD. */ |
| 3773 | Elf_Internal_Sym *internal_syms; |
| 3774 | /* Array large enough to hold a symbol index for each local symbol |
| 3775 | of any input BFD. */ |
| 3776 | long *indices; |
| 3777 | /* Array large enough to hold a section pointer for each local |
| 3778 | symbol of any input BFD. */ |
| 3779 | asection **sections; |
| 3780 | /* Buffer to hold swapped out symbols. */ |
| 3781 | Elf_External_Sym *symbuf; |
| 3782 | /* Number of swapped out symbols in buffer. */ |
| 3783 | size_t symbuf_count; |
| 3784 | /* Number of symbols which fit in symbuf. */ |
| 3785 | size_t symbuf_size; |
| 3786 | }; |
| 3787 | |
| 3788 | static boolean elf_link_output_sym |
| 3789 | PARAMS ((struct elf_final_link_info *, const char *, |
| 3790 | Elf_Internal_Sym *, asection *)); |
| 3791 | static boolean elf_link_flush_output_syms |
| 3792 | PARAMS ((struct elf_final_link_info *)); |
| 3793 | static boolean elf_link_output_extsym |
| 3794 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
| 3795 | static boolean elf_link_input_bfd |
| 3796 | PARAMS ((struct elf_final_link_info *, bfd *)); |
| 3797 | static boolean elf_reloc_link_order |
| 3798 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
| 3799 | struct bfd_link_order *)); |
| 3800 | |
| 3801 | /* This struct is used to pass information to elf_link_output_extsym. */ |
| 3802 | |
| 3803 | struct elf_outext_info |
| 3804 | { |
| 3805 | boolean failed; |
| 3806 | boolean localsyms; |
| 3807 | struct elf_final_link_info *finfo; |
| 3808 | }; |
| 3809 | |
| 3810 | /* Compute the size of, and allocate space for, REL_HDR which is the |
| 3811 | section header for a section containing relocations for O. */ |
| 3812 | |
| 3813 | static boolean |
| 3814 | elf_link_size_reloc_section (abfd, rel_hdr, o) |
| 3815 | bfd *abfd; |
| 3816 | Elf_Internal_Shdr *rel_hdr; |
| 3817 | asection *o; |
| 3818 | { |
| 3819 | register struct elf_link_hash_entry **p, **pend; |
| 3820 | unsigned reloc_count; |
| 3821 | |
| 3822 | /* Figure out how many relocations there will be. */ |
| 3823 | if (rel_hdr == &elf_section_data (o)->rel_hdr) |
| 3824 | reloc_count = elf_section_data (o)->rel_count; |
| 3825 | else |
| 3826 | reloc_count = elf_section_data (o)->rel_count2; |
| 3827 | |
| 3828 | /* That allows us to calculate the size of the section. */ |
| 3829 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; |
| 3830 | |
| 3831 | /* The contents field must last into write_object_contents, so we |
| 3832 | allocate it with bfd_alloc rather than malloc. */ |
| 3833 | rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size); |
| 3834 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
| 3835 | return false; |
| 3836 | |
| 3837 | /* We only allocate one set of hash entries, so we only do it the |
| 3838 | first time we are called. */ |
| 3839 | if (elf_section_data (o)->rel_hashes == NULL) |
| 3840 | { |
| 3841 | p = ((struct elf_link_hash_entry **) |
| 3842 | bfd_malloc (o->reloc_count |
| 3843 | * sizeof (struct elf_link_hash_entry *))); |
| 3844 | if (p == NULL && o->reloc_count != 0) |
| 3845 | return false; |
| 3846 | |
| 3847 | elf_section_data (o)->rel_hashes = p; |
| 3848 | pend = p + o->reloc_count; |
| 3849 | for (; p < pend; p++) |
| 3850 | *p = NULL; |
| 3851 | } |
| 3852 | |
| 3853 | return true; |
| 3854 | } |
| 3855 | |
| 3856 | /* When performing a relocateable link, the input relocations are |
| 3857 | preserved. But, if they reference global symbols, the indices |
| 3858 | referenced must be updated. Update all the relocations in |
| 3859 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ |
| 3860 | |
| 3861 | static void |
| 3862 | elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash) |
| 3863 | bfd *abfd; |
| 3864 | Elf_Internal_Shdr *rel_hdr; |
| 3865 | unsigned int count; |
| 3866 | struct elf_link_hash_entry **rel_hash; |
| 3867 | { |
| 3868 | unsigned int i; |
| 3869 | |
| 3870 | for (i = 0; i < count; i++, rel_hash++) |
| 3871 | { |
| 3872 | if (*rel_hash == NULL) |
| 3873 | continue; |
| 3874 | |
| 3875 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
| 3876 | |
| 3877 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) |
| 3878 | { |
| 3879 | Elf_External_Rel *erel; |
| 3880 | Elf_Internal_Rel irel; |
| 3881 | |
| 3882 | erel = (Elf_External_Rel *) rel_hdr->contents + i; |
| 3883 | elf_swap_reloc_in (abfd, erel, &irel); |
| 3884 | irel.r_info = ELF_R_INFO ((*rel_hash)->indx, |
| 3885 | ELF_R_TYPE (irel.r_info)); |
| 3886 | elf_swap_reloc_out (abfd, &irel, erel); |
| 3887 | } |
| 3888 | else |
| 3889 | { |
| 3890 | Elf_External_Rela *erela; |
| 3891 | Elf_Internal_Rela irela; |
| 3892 | |
| 3893 | BFD_ASSERT (rel_hdr->sh_entsize |
| 3894 | == sizeof (Elf_External_Rela)); |
| 3895 | |
| 3896 | erela = (Elf_External_Rela *) rel_hdr->contents + i; |
| 3897 | elf_swap_reloca_in (abfd, erela, &irela); |
| 3898 | irela.r_info = ELF_R_INFO ((*rel_hash)->indx, |
| 3899 | ELF_R_TYPE (irela.r_info)); |
| 3900 | elf_swap_reloca_out (abfd, &irela, erela); |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | /* Do the final step of an ELF link. */ |
| 3906 | |
| 3907 | boolean |
| 3908 | elf_bfd_final_link (abfd, info) |
| 3909 | bfd *abfd; |
| 3910 | struct bfd_link_info *info; |
| 3911 | { |
| 3912 | boolean dynamic; |
| 3913 | bfd *dynobj; |
| 3914 | struct elf_final_link_info finfo; |
| 3915 | register asection *o; |
| 3916 | register struct bfd_link_order *p; |
| 3917 | register bfd *sub; |
| 3918 | size_t max_contents_size; |
| 3919 | size_t max_external_reloc_size; |
| 3920 | size_t max_internal_reloc_count; |
| 3921 | size_t max_sym_count; |
| 3922 | file_ptr off; |
| 3923 | Elf_Internal_Sym elfsym; |
| 3924 | unsigned int i; |
| 3925 | Elf_Internal_Shdr *symtab_hdr; |
| 3926 | Elf_Internal_Shdr *symstrtab_hdr; |
| 3927 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
| 3928 | struct elf_outext_info eoinfo; |
| 3929 | |
| 3930 | if (info->shared) |
| 3931 | abfd->flags |= DYNAMIC; |
| 3932 | |
| 3933 | dynamic = elf_hash_table (info)->dynamic_sections_created; |
| 3934 | dynobj = elf_hash_table (info)->dynobj; |
| 3935 | |
| 3936 | finfo.info = info; |
| 3937 | finfo.output_bfd = abfd; |
| 3938 | finfo.symstrtab = elf_stringtab_init (); |
| 3939 | if (finfo.symstrtab == NULL) |
| 3940 | return false; |
| 3941 | |
| 3942 | if (! dynamic) |
| 3943 | { |
| 3944 | finfo.dynsym_sec = NULL; |
| 3945 | finfo.hash_sec = NULL; |
| 3946 | finfo.symver_sec = NULL; |
| 3947 | } |
| 3948 | else |
| 3949 | { |
| 3950 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); |
| 3951 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); |
| 3952 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); |
| 3953 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
| 3954 | /* Note that it is OK if symver_sec is NULL. */ |
| 3955 | } |
| 3956 | |
| 3957 | finfo.contents = NULL; |
| 3958 | finfo.external_relocs = NULL; |
| 3959 | finfo.internal_relocs = NULL; |
| 3960 | finfo.external_syms = NULL; |
| 3961 | finfo.internal_syms = NULL; |
| 3962 | finfo.indices = NULL; |
| 3963 | finfo.sections = NULL; |
| 3964 | finfo.symbuf = NULL; |
| 3965 | finfo.symbuf_count = 0; |
| 3966 | |
| 3967 | /* Count up the number of relocations we will output for each output |
| 3968 | section, so that we know the sizes of the reloc sections. We |
| 3969 | also figure out some maximum sizes. */ |
| 3970 | max_contents_size = 0; |
| 3971 | max_external_reloc_size = 0; |
| 3972 | max_internal_reloc_count = 0; |
| 3973 | max_sym_count = 0; |
| 3974 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) |
| 3975 | { |
| 3976 | o->reloc_count = 0; |
| 3977 | |
| 3978 | for (p = o->link_order_head; p != NULL; p = p->next) |
| 3979 | { |
| 3980 | if (p->type == bfd_section_reloc_link_order |
| 3981 | || p->type == bfd_symbol_reloc_link_order) |
| 3982 | ++o->reloc_count; |
| 3983 | else if (p->type == bfd_indirect_link_order) |
| 3984 | { |
| 3985 | asection *sec; |
| 3986 | |
| 3987 | sec = p->u.indirect.section; |
| 3988 | |
| 3989 | /* Mark all sections which are to be included in the |
| 3990 | link. This will normally be every section. We need |
| 3991 | to do this so that we can identify any sections which |
| 3992 | the linker has decided to not include. */ |
| 3993 | sec->linker_mark = true; |
| 3994 | |
| 3995 | if (info->relocateable) |
| 3996 | o->reloc_count += sec->reloc_count; |
| 3997 | |
| 3998 | if (sec->_raw_size > max_contents_size) |
| 3999 | max_contents_size = sec->_raw_size; |
| 4000 | if (sec->_cooked_size > max_contents_size) |
| 4001 | max_contents_size = sec->_cooked_size; |
| 4002 | |
| 4003 | /* We are interested in just local symbols, not all |
| 4004 | symbols. */ |
| 4005 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
| 4006 | && (sec->owner->flags & DYNAMIC) == 0) |
| 4007 | { |
| 4008 | size_t sym_count; |
| 4009 | |
| 4010 | if (elf_bad_symtab (sec->owner)) |
| 4011 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size |
| 4012 | / sizeof (Elf_External_Sym)); |
| 4013 | else |
| 4014 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; |
| 4015 | |
| 4016 | if (sym_count > max_sym_count) |
| 4017 | max_sym_count = sym_count; |
| 4018 | |
| 4019 | if ((sec->flags & SEC_RELOC) != 0) |
| 4020 | { |
| 4021 | size_t ext_size; |
| 4022 | |
| 4023 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; |
| 4024 | if (ext_size > max_external_reloc_size) |
| 4025 | max_external_reloc_size = ext_size; |
| 4026 | if (sec->reloc_count > max_internal_reloc_count) |
| 4027 | max_internal_reloc_count = sec->reloc_count; |
| 4028 | } |
| 4029 | } |
| 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | if (o->reloc_count > 0) |
| 4034 | o->flags |= SEC_RELOC; |
| 4035 | else |
| 4036 | { |
| 4037 | /* Explicitly clear the SEC_RELOC flag. The linker tends to |
| 4038 | set it (this is probably a bug) and if it is set |
| 4039 | assign_section_numbers will create a reloc section. */ |
| 4040 | o->flags &=~ SEC_RELOC; |
| 4041 | } |
| 4042 | |
| 4043 | /* If the SEC_ALLOC flag is not set, force the section VMA to |
| 4044 | zero. This is done in elf_fake_sections as well, but forcing |
| 4045 | the VMA to 0 here will ensure that relocs against these |
| 4046 | sections are handled correctly. */ |
| 4047 | if ((o->flags & SEC_ALLOC) == 0 |
| 4048 | && ! o->user_set_vma) |
| 4049 | o->vma = 0; |
| 4050 | } |
| 4051 | |
| 4052 | /* Figure out the file positions for everything but the symbol table |
| 4053 | and the relocs. We set symcount to force assign_section_numbers |
| 4054 | to create a symbol table. */ |
| 4055 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; |
| 4056 | BFD_ASSERT (! abfd->output_has_begun); |
| 4057 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) |
| 4058 | goto error_return; |
| 4059 | |
| 4060 | /* Figure out how many relocations we will have in each section. |
| 4061 | Just using RELOC_COUNT isn't good enough since that doesn't |
| 4062 | maintain a separate value for REL vs. RELA relocations. */ |
| 4063 | if (info->relocateable) |
| 4064 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
| 4065 | for (o = sub->sections; o != NULL; o = o->next) |
| 4066 | { |
| 4067 | asection *output_section; |
| 4068 | |
| 4069 | if (! o->linker_mark) |
| 4070 | { |
| 4071 | /* This section was omitted from the link. */ |
| 4072 | continue; |
| 4073 | } |
| 4074 | |
| 4075 | output_section = o->output_section; |
| 4076 | |
| 4077 | if (output_section != NULL |
| 4078 | && (o->flags & SEC_RELOC) != 0) |
| 4079 | { |
| 4080 | struct bfd_elf_section_data *esdi |
| 4081 | = elf_section_data (o); |
| 4082 | struct bfd_elf_section_data *esdo |
| 4083 | = elf_section_data (output_section); |
| 4084 | unsigned int *rel_count; |
| 4085 | unsigned int *rel_count2; |
| 4086 | |
| 4087 | /* We must be careful to add the relocation froms the |
| 4088 | input section to the right output count. */ |
| 4089 | if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize) |
| 4090 | { |
| 4091 | rel_count = &esdo->rel_count; |
| 4092 | rel_count2 = &esdo->rel_count2; |
| 4093 | } |
| 4094 | else |
| 4095 | { |
| 4096 | rel_count = &esdo->rel_count2; |
| 4097 | rel_count2 = &esdo->rel_count; |
| 4098 | } |
| 4099 | |
| 4100 | *rel_count += (esdi->rel_hdr.sh_size |
| 4101 | / esdi->rel_hdr.sh_entsize); |
| 4102 | if (esdi->rel_hdr2) |
| 4103 | *rel_count2 += (esdi->rel_hdr2->sh_size |
| 4104 | / esdi->rel_hdr2->sh_entsize); |
| 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | /* That created the reloc sections. Set their sizes, and assign |
| 4109 | them file positions, and allocate some buffers. */ |
| 4110 | for (o = abfd->sections; o != NULL; o = o->next) |
| 4111 | { |
| 4112 | if ((o->flags & SEC_RELOC) != 0) |
| 4113 | { |
| 4114 | if (!elf_link_size_reloc_section (abfd, |
| 4115 | &elf_section_data (o)->rel_hdr, |
| 4116 | o)) |
| 4117 | goto error_return; |
| 4118 | |
| 4119 | if (elf_section_data (o)->rel_hdr2 |
| 4120 | && !elf_link_size_reloc_section (abfd, |
| 4121 | elf_section_data (o)->rel_hdr2, |
| 4122 | o)) |
| 4123 | goto error_return; |
| 4124 | } |
| 4125 | |
| 4126 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them |
| 4127 | to count upwards while actually outputting the relocations. */ |
| 4128 | elf_section_data (o)->rel_count = 0; |
| 4129 | elf_section_data (o)->rel_count2 = 0; |
| 4130 | } |
| 4131 | |
| 4132 | _bfd_elf_assign_file_positions_for_relocs (abfd); |
| 4133 | |
| 4134 | /* We have now assigned file positions for all the sections except |
| 4135 | .symtab and .strtab. We start the .symtab section at the current |
| 4136 | file position, and write directly to it. We build the .strtab |
| 4137 | section in memory. */ |
| 4138 | bfd_get_symcount (abfd) = 0; |
| 4139 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 4140 | /* sh_name is set in prep_headers. */ |
| 4141 | symtab_hdr->sh_type = SHT_SYMTAB; |
| 4142 | symtab_hdr->sh_flags = 0; |
| 4143 | symtab_hdr->sh_addr = 0; |
| 4144 | symtab_hdr->sh_size = 0; |
| 4145 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); |
| 4146 | /* sh_link is set in assign_section_numbers. */ |
| 4147 | /* sh_info is set below. */ |
| 4148 | /* sh_offset is set just below. */ |
| 4149 | symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */ |
| 4150 | |
| 4151 | off = elf_tdata (abfd)->next_file_pos; |
| 4152 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true); |
| 4153 | |
| 4154 | /* Note that at this point elf_tdata (abfd)->next_file_pos is |
| 4155 | incorrect. We do not yet know the size of the .symtab section. |
| 4156 | We correct next_file_pos below, after we do know the size. */ |
| 4157 | |
| 4158 | /* Allocate a buffer to hold swapped out symbols. This is to avoid |
| 4159 | continuously seeking to the right position in the file. */ |
| 4160 | if (! info->keep_memory || max_sym_count < 20) |
| 4161 | finfo.symbuf_size = 20; |
| 4162 | else |
| 4163 | finfo.symbuf_size = max_sym_count; |
| 4164 | finfo.symbuf = ((Elf_External_Sym *) |
| 4165 | bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym))); |
| 4166 | if (finfo.symbuf == NULL) |
| 4167 | goto error_return; |
| 4168 | |
| 4169 | /* Start writing out the symbol table. The first symbol is always a |
| 4170 | dummy symbol. */ |
| 4171 | if (info->strip != strip_all || info->relocateable) |
| 4172 | { |
| 4173 | elfsym.st_value = 0; |
| 4174 | elfsym.st_size = 0; |
| 4175 | elfsym.st_info = 0; |
| 4176 | elfsym.st_other = 0; |
| 4177 | elfsym.st_shndx = SHN_UNDEF; |
| 4178 | if (! elf_link_output_sym (&finfo, (const char *) NULL, |
| 4179 | &elfsym, bfd_und_section_ptr)) |
| 4180 | goto error_return; |
| 4181 | } |
| 4182 | |
| 4183 | #if 0 |
| 4184 | /* Some standard ELF linkers do this, but we don't because it causes |
| 4185 | bootstrap comparison failures. */ |
| 4186 | /* Output a file symbol for the output file as the second symbol. |
| 4187 | We output this even if we are discarding local symbols, although |
| 4188 | I'm not sure if this is correct. */ |
| 4189 | elfsym.st_value = 0; |
| 4190 | elfsym.st_size = 0; |
| 4191 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); |
| 4192 | elfsym.st_other = 0; |
| 4193 | elfsym.st_shndx = SHN_ABS; |
| 4194 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), |
| 4195 | &elfsym, bfd_abs_section_ptr)) |
| 4196 | goto error_return; |
| 4197 | #endif |
| 4198 | |
| 4199 | /* Output a symbol for each section. We output these even if we are |
| 4200 | discarding local symbols, since they are used for relocs. These |
| 4201 | symbols have no names. We store the index of each one in the |
| 4202 | index field of the section, so that we can find it again when |
| 4203 | outputting relocs. */ |
| 4204 | if (info->strip != strip_all || info->relocateable) |
| 4205 | { |
| 4206 | elfsym.st_size = 0; |
| 4207 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); |
| 4208 | elfsym.st_other = 0; |
| 4209 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) |
| 4210 | { |
| 4211 | o = section_from_elf_index (abfd, i); |
| 4212 | if (o != NULL) |
| 4213 | o->target_index = bfd_get_symcount (abfd); |
| 4214 | elfsym.st_shndx = i; |
| 4215 | if (info->relocateable || o == NULL) |
| 4216 | elfsym.st_value = 0; |
| 4217 | else |
| 4218 | elfsym.st_value = o->vma; |
| 4219 | if (! elf_link_output_sym (&finfo, (const char *) NULL, |
| 4220 | &elfsym, o)) |
| 4221 | goto error_return; |
| 4222 | } |
| 4223 | } |
| 4224 | |
| 4225 | /* Allocate some memory to hold information read in from the input |
| 4226 | files. */ |
| 4227 | finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
| 4228 | finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size); |
| 4229 | finfo.internal_relocs = ((Elf_Internal_Rela *) |
| 4230 | bfd_malloc (max_internal_reloc_count |
| 4231 | * sizeof (Elf_Internal_Rela) |
| 4232 | * bed->s->int_rels_per_ext_rel)); |
| 4233 | finfo.external_syms = ((Elf_External_Sym *) |
| 4234 | bfd_malloc (max_sym_count |
| 4235 | * sizeof (Elf_External_Sym))); |
| 4236 | finfo.internal_syms = ((Elf_Internal_Sym *) |
| 4237 | bfd_malloc (max_sym_count |
| 4238 | * sizeof (Elf_Internal_Sym))); |
| 4239 | finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long)); |
| 4240 | finfo.sections = ((asection **) |
| 4241 | bfd_malloc (max_sym_count * sizeof (asection *))); |
| 4242 | if ((finfo.contents == NULL && max_contents_size != 0) |
| 4243 | || (finfo.external_relocs == NULL && max_external_reloc_size != 0) |
| 4244 | || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0) |
| 4245 | || (finfo.external_syms == NULL && max_sym_count != 0) |
| 4246 | || (finfo.internal_syms == NULL && max_sym_count != 0) |
| 4247 | || (finfo.indices == NULL && max_sym_count != 0) |
| 4248 | || (finfo.sections == NULL && max_sym_count != 0)) |
| 4249 | goto error_return; |
| 4250 | |
| 4251 | /* Since ELF permits relocations to be against local symbols, we |
| 4252 | must have the local symbols available when we do the relocations. |
| 4253 | Since we would rather only read the local symbols once, and we |
| 4254 | would rather not keep them in memory, we handle all the |
| 4255 | relocations for a single input file at the same time. |
| 4256 | |
| 4257 | Unfortunately, there is no way to know the total number of local |
| 4258 | symbols until we have seen all of them, and the local symbol |
| 4259 | indices precede the global symbol indices. This means that when |
| 4260 | we are generating relocateable output, and we see a reloc against |
| 4261 | a global symbol, we can not know the symbol index until we have |
| 4262 | finished examining all the local symbols to see which ones we are |
| 4263 | going to output. To deal with this, we keep the relocations in |
| 4264 | memory, and don't output them until the end of the link. This is |
| 4265 | an unfortunate waste of memory, but I don't see a good way around |
| 4266 | it. Fortunately, it only happens when performing a relocateable |
| 4267 | link, which is not the common case. FIXME: If keep_memory is set |
| 4268 | we could write the relocs out and then read them again; I don't |
| 4269 | know how bad the memory loss will be. */ |
| 4270 | |
| 4271 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
| 4272 | sub->output_has_begun = false; |
| 4273 | for (o = abfd->sections; o != NULL; o = o->next) |
| 4274 | { |
| 4275 | for (p = o->link_order_head; p != NULL; p = p->next) |
| 4276 | { |
| 4277 | if (p->type == bfd_indirect_link_order |
| 4278 | && (bfd_get_flavour (p->u.indirect.section->owner) |
| 4279 | == bfd_target_elf_flavour)) |
| 4280 | { |
| 4281 | sub = p->u.indirect.section->owner; |
| 4282 | if (! sub->output_has_begun) |
| 4283 | { |
| 4284 | if (! elf_link_input_bfd (&finfo, sub)) |
| 4285 | goto error_return; |
| 4286 | sub->output_has_begun = true; |
| 4287 | } |
| 4288 | } |
| 4289 | else if (p->type == bfd_section_reloc_link_order |
| 4290 | || p->type == bfd_symbol_reloc_link_order) |
| 4291 | { |
| 4292 | if (! elf_reloc_link_order (abfd, info, o, p)) |
| 4293 | goto error_return; |
| 4294 | } |
| 4295 | else |
| 4296 | { |
| 4297 | if (! _bfd_default_link_order (abfd, info, o, p)) |
| 4298 | goto error_return; |
| 4299 | } |
| 4300 | } |
| 4301 | } |
| 4302 | |
| 4303 | /* That wrote out all the local symbols. Finish up the symbol table |
| 4304 | with the global symbols. */ |
| 4305 | |
| 4306 | if (info->strip != strip_all && info->shared) |
| 4307 | { |
| 4308 | /* Output any global symbols that got converted to local in a |
| 4309 | version script. We do this in a separate step since ELF |
| 4310 | requires all local symbols to appear prior to any global |
| 4311 | symbols. FIXME: We should only do this if some global |
| 4312 | symbols were, in fact, converted to become local. FIXME: |
| 4313 | Will this work correctly with the Irix 5 linker? */ |
| 4314 | eoinfo.failed = false; |
| 4315 | eoinfo.finfo = &finfo; |
| 4316 | eoinfo.localsyms = true; |
| 4317 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, |
| 4318 | (PTR) &eoinfo); |
| 4319 | if (eoinfo.failed) |
| 4320 | return false; |
| 4321 | } |
| 4322 | |
| 4323 | /* The sh_info field records the index of the first non local symbol. */ |
| 4324 | symtab_hdr->sh_info = bfd_get_symcount (abfd); |
| 4325 | |
| 4326 | if (dynamic) |
| 4327 | { |
| 4328 | Elf_Internal_Sym sym; |
| 4329 | Elf_External_Sym *dynsym = |
| 4330 | (Elf_External_Sym *)finfo.dynsym_sec->contents; |
| 4331 | unsigned long last_local = 0; |
| 4332 | |
| 4333 | /* Write out the section symbols for the output sections. */ |
| 4334 | if (info->shared) |
| 4335 | { |
| 4336 | asection *s; |
| 4337 | |
| 4338 | sym.st_size = 0; |
| 4339 | sym.st_name = 0; |
| 4340 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); |
| 4341 | sym.st_other = 0; |
| 4342 | |
| 4343 | for (s = abfd->sections; s != NULL; s = s->next) |
| 4344 | { |
| 4345 | int indx; |
| 4346 | indx = elf_section_data (s)->this_idx; |
| 4347 | BFD_ASSERT (indx > 0); |
| 4348 | sym.st_shndx = indx; |
| 4349 | sym.st_value = s->vma; |
| 4350 | |
| 4351 | elf_swap_symbol_out (abfd, &sym, |
| 4352 | dynsym + elf_section_data (s)->dynindx); |
| 4353 | } |
| 4354 | |
| 4355 | last_local = bfd_count_sections (abfd); |
| 4356 | } |
| 4357 | |
| 4358 | /* Write out the local dynsyms. */ |
| 4359 | if (elf_hash_table (info)->dynlocal) |
| 4360 | { |
| 4361 | struct elf_link_local_dynamic_entry *e; |
| 4362 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) |
| 4363 | { |
| 4364 | asection *s; |
| 4365 | |
| 4366 | sym.st_size = e->isym.st_size; |
| 4367 | sym.st_other = e->isym.st_other; |
| 4368 | |
| 4369 | /* Copy the internal symbol as is. |
| 4370 | Note that we saved a word of storage and overwrote |
| 4371 | the original st_name with the dynstr_index. */ |
| 4372 | sym = e->isym; |
| 4373 | |
| 4374 | if (e->isym.st_shndx > 0 && e->isym.st_shndx < SHN_LORESERVE) |
| 4375 | { |
| 4376 | s = bfd_section_from_elf_index (e->input_bfd, |
| 4377 | e->isym.st_shndx); |
| 4378 | |
| 4379 | sym.st_shndx = |
| 4380 | elf_section_data (s->output_section)->this_idx; |
| 4381 | sym.st_value = (s->output_section->vma |
| 4382 | + s->output_offset |
| 4383 | + e->isym.st_value); |
| 4384 | } |
| 4385 | |
| 4386 | if (last_local < e->dynindx) |
| 4387 | last_local = e->dynindx; |
| 4388 | |
| 4389 | elf_swap_symbol_out (abfd, &sym, dynsym + e->dynindx); |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | elf_section_data (finfo.dynsym_sec->output_section) |
| 4394 | ->this_hdr.sh_info = last_local + 1; |
| 4395 | } |
| 4396 | |
| 4397 | /* We get the global symbols from the hash table. */ |
| 4398 | eoinfo.failed = false; |
| 4399 | eoinfo.localsyms = false; |
| 4400 | eoinfo.finfo = &finfo; |
| 4401 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, |
| 4402 | (PTR) &eoinfo); |
| 4403 | if (eoinfo.failed) |
| 4404 | return false; |
| 4405 | |
| 4406 | /* If backend needs to output some symbols not present in the hash |
| 4407 | table, do it now. */ |
| 4408 | if (bed->elf_backend_output_arch_syms) |
| 4409 | { |
| 4410 | if (! (*bed->elf_backend_output_arch_syms) |
| 4411 | (abfd, info, (PTR) &finfo, |
| 4412 | (boolean (*) PARAMS ((PTR, const char *, |
| 4413 | Elf_Internal_Sym *, asection *))) |
| 4414 | elf_link_output_sym)) |
| 4415 | return false; |
| 4416 | } |
| 4417 | |
| 4418 | /* Flush all symbols to the file. */ |
| 4419 | if (! elf_link_flush_output_syms (&finfo)) |
| 4420 | return false; |
| 4421 | |
| 4422 | /* Now we know the size of the symtab section. */ |
| 4423 | off += symtab_hdr->sh_size; |
| 4424 | |
| 4425 | /* Finish up and write out the symbol string table (.strtab) |
| 4426 | section. */ |
| 4427 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; |
| 4428 | /* sh_name was set in prep_headers. */ |
| 4429 | symstrtab_hdr->sh_type = SHT_STRTAB; |
| 4430 | symstrtab_hdr->sh_flags = 0; |
| 4431 | symstrtab_hdr->sh_addr = 0; |
| 4432 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); |
| 4433 | symstrtab_hdr->sh_entsize = 0; |
| 4434 | symstrtab_hdr->sh_link = 0; |
| 4435 | symstrtab_hdr->sh_info = 0; |
| 4436 | /* sh_offset is set just below. */ |
| 4437 | symstrtab_hdr->sh_addralign = 1; |
| 4438 | |
| 4439 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true); |
| 4440 | elf_tdata (abfd)->next_file_pos = off; |
| 4441 | |
| 4442 | if (bfd_get_symcount (abfd) > 0) |
| 4443 | { |
| 4444 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
| 4445 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) |
| 4446 | return false; |
| 4447 | } |
| 4448 | |
| 4449 | /* Adjust the relocs to have the correct symbol indices. */ |
| 4450 | for (o = abfd->sections; o != NULL; o = o->next) |
| 4451 | { |
| 4452 | if ((o->flags & SEC_RELOC) == 0) |
| 4453 | continue; |
| 4454 | |
| 4455 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, |
| 4456 | elf_section_data (o)->rel_count, |
| 4457 | elf_section_data (o)->rel_hashes); |
| 4458 | if (elf_section_data (o)->rel_hdr2 != NULL) |
| 4459 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, |
| 4460 | elf_section_data (o)->rel_count2, |
| 4461 | (elf_section_data (o)->rel_hashes |
| 4462 | + elf_section_data (o)->rel_count)); |
| 4463 | |
| 4464 | /* Set the reloc_count field to 0 to prevent write_relocs from |
| 4465 | trying to swap the relocs out itself. */ |
| 4466 | o->reloc_count = 0; |
| 4467 | } |
| 4468 | |
| 4469 | /* If we are linking against a dynamic object, or generating a |
| 4470 | shared library, finish up the dynamic linking information. */ |
| 4471 | if (dynamic) |
| 4472 | { |
| 4473 | Elf_External_Dyn *dyncon, *dynconend; |
| 4474 | |
| 4475 | /* Fix up .dynamic entries. */ |
| 4476 | o = bfd_get_section_by_name (dynobj, ".dynamic"); |
| 4477 | BFD_ASSERT (o != NULL); |
| 4478 | |
| 4479 | dyncon = (Elf_External_Dyn *) o->contents; |
| 4480 | dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size); |
| 4481 | for (; dyncon < dynconend; dyncon++) |
| 4482 | { |
| 4483 | Elf_Internal_Dyn dyn; |
| 4484 | const char *name; |
| 4485 | unsigned int type; |
| 4486 | |
| 4487 | elf_swap_dyn_in (dynobj, dyncon, &dyn); |
| 4488 | |
| 4489 | switch (dyn.d_tag) |
| 4490 | { |
| 4491 | default: |
| 4492 | break; |
| 4493 | case DT_INIT: |
| 4494 | name = info->init_function; |
| 4495 | goto get_sym; |
| 4496 | case DT_FINI: |
| 4497 | name = info->fini_function; |
| 4498 | get_sym: |
| 4499 | { |
| 4500 | struct elf_link_hash_entry *h; |
| 4501 | |
| 4502 | h = elf_link_hash_lookup (elf_hash_table (info), name, |
| 4503 | false, false, true); |
| 4504 | if (h != NULL |
| 4505 | && (h->root.type == bfd_link_hash_defined |
| 4506 | || h->root.type == bfd_link_hash_defweak)) |
| 4507 | { |
| 4508 | dyn.d_un.d_val = h->root.u.def.value; |
| 4509 | o = h->root.u.def.section; |
| 4510 | if (o->output_section != NULL) |
| 4511 | dyn.d_un.d_val += (o->output_section->vma |
| 4512 | + o->output_offset); |
| 4513 | else |
| 4514 | { |
| 4515 | /* The symbol is imported from another shared |
| 4516 | library and does not apply to this one. */ |
| 4517 | dyn.d_un.d_val = 0; |
| 4518 | } |
| 4519 | |
| 4520 | elf_swap_dyn_out (dynobj, &dyn, dyncon); |
| 4521 | } |
| 4522 | } |
| 4523 | break; |
| 4524 | |
| 4525 | case DT_HASH: |
| 4526 | name = ".hash"; |
| 4527 | goto get_vma; |
| 4528 | case DT_STRTAB: |
| 4529 | name = ".dynstr"; |
| 4530 | goto get_vma; |
| 4531 | case DT_SYMTAB: |
| 4532 | name = ".dynsym"; |
| 4533 | goto get_vma; |
| 4534 | case DT_VERDEF: |
| 4535 | name = ".gnu.version_d"; |
| 4536 | goto get_vma; |
| 4537 | case DT_VERNEED: |
| 4538 | name = ".gnu.version_r"; |
| 4539 | goto get_vma; |
| 4540 | case DT_VERSYM: |
| 4541 | name = ".gnu.version"; |
| 4542 | get_vma: |
| 4543 | o = bfd_get_section_by_name (abfd, name); |
| 4544 | BFD_ASSERT (o != NULL); |
| 4545 | dyn.d_un.d_ptr = o->vma; |
| 4546 | elf_swap_dyn_out (dynobj, &dyn, dyncon); |
| 4547 | break; |
| 4548 | |
| 4549 | case DT_REL: |
| 4550 | case DT_RELA: |
| 4551 | case DT_RELSZ: |
| 4552 | case DT_RELASZ: |
| 4553 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) |
| 4554 | type = SHT_REL; |
| 4555 | else |
| 4556 | type = SHT_RELA; |
| 4557 | dyn.d_un.d_val = 0; |
| 4558 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) |
| 4559 | { |
| 4560 | Elf_Internal_Shdr *hdr; |
| 4561 | |
| 4562 | hdr = elf_elfsections (abfd)[i]; |
| 4563 | if (hdr->sh_type == type |
| 4564 | && (hdr->sh_flags & SHF_ALLOC) != 0) |
| 4565 | { |
| 4566 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) |
| 4567 | dyn.d_un.d_val += hdr->sh_size; |
| 4568 | else |
| 4569 | { |
| 4570 | if (dyn.d_un.d_val == 0 |
| 4571 | || hdr->sh_addr < dyn.d_un.d_val) |
| 4572 | dyn.d_un.d_val = hdr->sh_addr; |
| 4573 | } |
| 4574 | } |
| 4575 | } |
| 4576 | elf_swap_dyn_out (dynobj, &dyn, dyncon); |
| 4577 | break; |
| 4578 | } |
| 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | /* If we have created any dynamic sections, then output them. */ |
| 4583 | if (dynobj != NULL) |
| 4584 | { |
| 4585 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) |
| 4586 | goto error_return; |
| 4587 | |
| 4588 | for (o = dynobj->sections; o != NULL; o = o->next) |
| 4589 | { |
| 4590 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
| 4591 | || o->_raw_size == 0) |
| 4592 | continue; |
| 4593 | if ((o->flags & SEC_LINKER_CREATED) == 0) |
| 4594 | { |
| 4595 | /* At this point, we are only interested in sections |
| 4596 | created by elf_link_create_dynamic_sections. */ |
| 4597 | continue; |
| 4598 | } |
| 4599 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
| 4600 | != SHT_STRTAB) |
| 4601 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) |
| 4602 | { |
| 4603 | if (! bfd_set_section_contents (abfd, o->output_section, |
| 4604 | o->contents, o->output_offset, |
| 4605 | o->_raw_size)) |
| 4606 | goto error_return; |
| 4607 | } |
| 4608 | else |
| 4609 | { |
| 4610 | file_ptr off; |
| 4611 | |
| 4612 | /* The contents of the .dynstr section are actually in a |
| 4613 | stringtab. */ |
| 4614 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
| 4615 | if (bfd_seek (abfd, off, SEEK_SET) != 0 |
| 4616 | || ! _bfd_stringtab_emit (abfd, |
| 4617 | elf_hash_table (info)->dynstr)) |
| 4618 | goto error_return; |
| 4619 | } |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | /* If we have optimized stabs strings, output them. */ |
| 4624 | if (elf_hash_table (info)->stab_info != NULL) |
| 4625 | { |
| 4626 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) |
| 4627 | goto error_return; |
| 4628 | } |
| 4629 | |
| 4630 | if (finfo.symstrtab != NULL) |
| 4631 | _bfd_stringtab_free (finfo.symstrtab); |
| 4632 | if (finfo.contents != NULL) |
| 4633 | free (finfo.contents); |
| 4634 | if (finfo.external_relocs != NULL) |
| 4635 | free (finfo.external_relocs); |
| 4636 | if (finfo.internal_relocs != NULL) |
| 4637 | free (finfo.internal_relocs); |
| 4638 | if (finfo.external_syms != NULL) |
| 4639 | free (finfo.external_syms); |
| 4640 | if (finfo.internal_syms != NULL) |
| 4641 | free (finfo.internal_syms); |
| 4642 | if (finfo.indices != NULL) |
| 4643 | free (finfo.indices); |
| 4644 | if (finfo.sections != NULL) |
| 4645 | free (finfo.sections); |
| 4646 | if (finfo.symbuf != NULL) |
| 4647 | free (finfo.symbuf); |
| 4648 | for (o = abfd->sections; o != NULL; o = o->next) |
| 4649 | { |
| 4650 | if ((o->flags & SEC_RELOC) != 0 |
| 4651 | && elf_section_data (o)->rel_hashes != NULL) |
| 4652 | free (elf_section_data (o)->rel_hashes); |
| 4653 | } |
| 4654 | |
| 4655 | elf_tdata (abfd)->linker = true; |
| 4656 | |
| 4657 | return true; |
| 4658 | |
| 4659 | error_return: |
| 4660 | if (finfo.symstrtab != NULL) |
| 4661 | _bfd_stringtab_free (finfo.symstrtab); |
| 4662 | if (finfo.contents != NULL) |
| 4663 | free (finfo.contents); |
| 4664 | if (finfo.external_relocs != NULL) |
| 4665 | free (finfo.external_relocs); |
| 4666 | if (finfo.internal_relocs != NULL) |
| 4667 | free (finfo.internal_relocs); |
| 4668 | if (finfo.external_syms != NULL) |
| 4669 | free (finfo.external_syms); |
| 4670 | if (finfo.internal_syms != NULL) |
| 4671 | free (finfo.internal_syms); |
| 4672 | if (finfo.indices != NULL) |
| 4673 | free (finfo.indices); |
| 4674 | if (finfo.sections != NULL) |
| 4675 | free (finfo.sections); |
| 4676 | if (finfo.symbuf != NULL) |
| 4677 | free (finfo.symbuf); |
| 4678 | for (o = abfd->sections; o != NULL; o = o->next) |
| 4679 | { |
| 4680 | if ((o->flags & SEC_RELOC) != 0 |
| 4681 | && elf_section_data (o)->rel_hashes != NULL) |
| 4682 | free (elf_section_data (o)->rel_hashes); |
| 4683 | } |
| 4684 | |
| 4685 | return false; |
| 4686 | } |
| 4687 | |
| 4688 | /* Add a symbol to the output symbol table. */ |
| 4689 | |
| 4690 | static boolean |
| 4691 | elf_link_output_sym (finfo, name, elfsym, input_sec) |
| 4692 | struct elf_final_link_info *finfo; |
| 4693 | const char *name; |
| 4694 | Elf_Internal_Sym *elfsym; |
| 4695 | asection *input_sec; |
| 4696 | { |
| 4697 | boolean (*output_symbol_hook) PARAMS ((bfd *, |
| 4698 | struct bfd_link_info *info, |
| 4699 | const char *, |
| 4700 | Elf_Internal_Sym *, |
| 4701 | asection *)); |
| 4702 | |
| 4703 | output_symbol_hook = get_elf_backend_data (finfo->output_bfd)-> |
| 4704 | elf_backend_link_output_symbol_hook; |
| 4705 | if (output_symbol_hook != NULL) |
| 4706 | { |
| 4707 | if (! ((*output_symbol_hook) |
| 4708 | (finfo->output_bfd, finfo->info, name, elfsym, input_sec))) |
| 4709 | return false; |
| 4710 | } |
| 4711 | |
| 4712 | if (name == (const char *) NULL || *name == '\0') |
| 4713 | elfsym->st_name = 0; |
| 4714 | else if (input_sec->flags & SEC_EXCLUDE) |
| 4715 | elfsym->st_name = 0; |
| 4716 | else |
| 4717 | { |
| 4718 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, |
| 4719 | name, true, |
| 4720 | false); |
| 4721 | if (elfsym->st_name == (unsigned long) -1) |
| 4722 | return false; |
| 4723 | } |
| 4724 | |
| 4725 | if (finfo->symbuf_count >= finfo->symbuf_size) |
| 4726 | { |
| 4727 | if (! elf_link_flush_output_syms (finfo)) |
| 4728 | return false; |
| 4729 | } |
| 4730 | |
| 4731 | elf_swap_symbol_out (finfo->output_bfd, elfsym, |
| 4732 | (PTR) (finfo->symbuf + finfo->symbuf_count)); |
| 4733 | ++finfo->symbuf_count; |
| 4734 | |
| 4735 | ++ bfd_get_symcount (finfo->output_bfd); |
| 4736 | |
| 4737 | return true; |
| 4738 | } |
| 4739 | |
| 4740 | /* Flush the output symbols to the file. */ |
| 4741 | |
| 4742 | static boolean |
| 4743 | elf_link_flush_output_syms (finfo) |
| 4744 | struct elf_final_link_info *finfo; |
| 4745 | { |
| 4746 | if (finfo->symbuf_count > 0) |
| 4747 | { |
| 4748 | Elf_Internal_Shdr *symtab; |
| 4749 | |
| 4750 | symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr; |
| 4751 | |
| 4752 | if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size, |
| 4753 | SEEK_SET) != 0 |
| 4754 | || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count, |
| 4755 | sizeof (Elf_External_Sym), finfo->output_bfd) |
| 4756 | != finfo->symbuf_count * sizeof (Elf_External_Sym))) |
| 4757 | return false; |
| 4758 | |
| 4759 | symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym); |
| 4760 | |
| 4761 | finfo->symbuf_count = 0; |
| 4762 | } |
| 4763 | |
| 4764 | return true; |
| 4765 | } |
| 4766 | |
| 4767 | /* Add an external symbol to the symbol table. This is called from |
| 4768 | the hash table traversal routine. When generating a shared object, |
| 4769 | we go through the symbol table twice. The first time we output |
| 4770 | anything that might have been forced to local scope in a version |
| 4771 | script. The second time we output the symbols that are still |
| 4772 | global symbols. */ |
| 4773 | |
| 4774 | static boolean |
| 4775 | elf_link_output_extsym (h, data) |
| 4776 | struct elf_link_hash_entry *h; |
| 4777 | PTR data; |
| 4778 | { |
| 4779 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
| 4780 | struct elf_final_link_info *finfo = eoinfo->finfo; |
| 4781 | boolean strip; |
| 4782 | Elf_Internal_Sym sym; |
| 4783 | asection *input_sec; |
| 4784 | |
| 4785 | /* Decide whether to output this symbol in this pass. */ |
| 4786 | if (eoinfo->localsyms) |
| 4787 | { |
| 4788 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) |
| 4789 | return true; |
| 4790 | } |
| 4791 | else |
| 4792 | { |
| 4793 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) |
| 4794 | return true; |
| 4795 | } |
| 4796 | |
| 4797 | /* If we are not creating a shared library, and this symbol is |
| 4798 | referenced by a shared library but is not defined anywhere, then |
| 4799 | warn that it is undefined. If we do not do this, the runtime |
| 4800 | linker will complain that the symbol is undefined when the |
| 4801 | program is run. We don't have to worry about symbols that are |
| 4802 | referenced by regular files, because we will already have issued |
| 4803 | warnings for them. */ |
| 4804 | if (! finfo->info->relocateable |
| 4805 | && ! (finfo->info->shared |
| 4806 | && !finfo->info->no_undefined) |
| 4807 | && h->root.type == bfd_link_hash_undefined |
| 4808 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0 |
| 4809 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) |
| 4810 | { |
| 4811 | if (! ((*finfo->info->callbacks->undefined_symbol) |
| 4812 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, |
| 4813 | (asection *) NULL, 0))) |
| 4814 | { |
| 4815 | eoinfo->failed = true; |
| 4816 | return false; |
| 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | /* We don't want to output symbols that have never been mentioned by |
| 4821 | a regular file, or that we have been told to strip. However, if |
| 4822 | h->indx is set to -2, the symbol is used by a reloc and we must |
| 4823 | output it. */ |
| 4824 | if (h->indx == -2) |
| 4825 | strip = false; |
| 4826 | else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
| 4827 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) |
| 4828 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 |
| 4829 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) |
| 4830 | strip = true; |
| 4831 | else if (finfo->info->strip == strip_all |
| 4832 | || (finfo->info->strip == strip_some |
| 4833 | && bfd_hash_lookup (finfo->info->keep_hash, |
| 4834 | h->root.root.string, |
| 4835 | false, false) == NULL)) |
| 4836 | strip = true; |
| 4837 | else |
| 4838 | strip = false; |
| 4839 | |
| 4840 | /* If we're stripping it, and it's not a dynamic symbol, there's |
| 4841 | nothing else to do. */ |
| 4842 | if (strip && h->dynindx == -1) |
| 4843 | return true; |
| 4844 | |
| 4845 | sym.st_value = 0; |
| 4846 | sym.st_size = h->size; |
| 4847 | sym.st_other = h->other; |
| 4848 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) |
| 4849 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
| 4850 | else if (h->root.type == bfd_link_hash_undefweak |
| 4851 | || h->root.type == bfd_link_hash_defweak) |
| 4852 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); |
| 4853 | else |
| 4854 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); |
| 4855 | |
| 4856 | switch (h->root.type) |
| 4857 | { |
| 4858 | default: |
| 4859 | case bfd_link_hash_new: |
| 4860 | abort (); |
| 4861 | return false; |
| 4862 | |
| 4863 | case bfd_link_hash_undefined: |
| 4864 | input_sec = bfd_und_section_ptr; |
| 4865 | sym.st_shndx = SHN_UNDEF; |
| 4866 | break; |
| 4867 | |
| 4868 | case bfd_link_hash_undefweak: |
| 4869 | input_sec = bfd_und_section_ptr; |
| 4870 | sym.st_shndx = SHN_UNDEF; |
| 4871 | break; |
| 4872 | |
| 4873 | case bfd_link_hash_defined: |
| 4874 | case bfd_link_hash_defweak: |
| 4875 | { |
| 4876 | input_sec = h->root.u.def.section; |
| 4877 | if (input_sec->output_section != NULL) |
| 4878 | { |
| 4879 | sym.st_shndx = |
| 4880 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, |
| 4881 | input_sec->output_section); |
| 4882 | if (sym.st_shndx == (unsigned short) -1) |
| 4883 | { |
| 4884 | (*_bfd_error_handler) |
| 4885 | (_("%s: could not find output section %s for input section %s"), |
| 4886 | bfd_get_filename (finfo->output_bfd), |
| 4887 | input_sec->output_section->name, |
| 4888 | input_sec->name); |
| 4889 | eoinfo->failed = true; |
| 4890 | return false; |
| 4891 | } |
| 4892 | |
| 4893 | /* ELF symbols in relocateable files are section relative, |
| 4894 | but in nonrelocateable files they are virtual |
| 4895 | addresses. */ |
| 4896 | sym.st_value = h->root.u.def.value + input_sec->output_offset; |
| 4897 | if (! finfo->info->relocateable) |
| 4898 | sym.st_value += input_sec->output_section->vma; |
| 4899 | } |
| 4900 | else |
| 4901 | { |
| 4902 | BFD_ASSERT (input_sec->owner == NULL |
| 4903 | || (input_sec->owner->flags & DYNAMIC) != 0); |
| 4904 | sym.st_shndx = SHN_UNDEF; |
| 4905 | input_sec = bfd_und_section_ptr; |
| 4906 | } |
| 4907 | } |
| 4908 | break; |
| 4909 | |
| 4910 | case bfd_link_hash_common: |
| 4911 | input_sec = h->root.u.c.p->section; |
| 4912 | sym.st_shndx = SHN_COMMON; |
| 4913 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
| 4914 | break; |
| 4915 | |
| 4916 | case bfd_link_hash_indirect: |
| 4917 | /* These symbols are created by symbol versioning. They point |
| 4918 | to the decorated version of the name. For example, if the |
| 4919 | symbol foo@@GNU_1.2 is the default, which should be used when |
| 4920 | foo is used with no version, then we add an indirect symbol |
| 4921 | foo which points to foo@@GNU_1.2. We ignore these symbols, |
| 4922 | since the indirected symbol is already in the hash table. If |
| 4923 | the indirect symbol is non-ELF, fall through and output it. */ |
| 4924 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) == 0) |
| 4925 | return true; |
| 4926 | |
| 4927 | /* Fall through. */ |
| 4928 | case bfd_link_hash_warning: |
| 4929 | /* We can't represent these symbols in ELF, although a warning |
| 4930 | symbol may have come from a .gnu.warning.SYMBOL section. We |
| 4931 | just put the target symbol in the hash table. If the target |
| 4932 | symbol does not really exist, don't do anything. */ |
| 4933 | if (h->root.u.i.link->type == bfd_link_hash_new) |
| 4934 | return true; |
| 4935 | return (elf_link_output_extsym |
| 4936 | ((struct elf_link_hash_entry *) h->root.u.i.link, data)); |
| 4937 | } |
| 4938 | |
| 4939 | /* Give the processor backend a chance to tweak the symbol value, |
| 4940 | and also to finish up anything that needs to be done for this |
| 4941 | symbol. */ |
| 4942 | if ((h->dynindx != -1 |
| 4943 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) |
| 4944 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
| 4945 | { |
| 4946 | struct elf_backend_data *bed; |
| 4947 | |
| 4948 | bed = get_elf_backend_data (finfo->output_bfd); |
| 4949 | if (! ((*bed->elf_backend_finish_dynamic_symbol) |
| 4950 | (finfo->output_bfd, finfo->info, h, &sym))) |
| 4951 | { |
| 4952 | eoinfo->failed = true; |
| 4953 | return false; |
| 4954 | } |
| 4955 | } |
| 4956 | |
| 4957 | /* If we are marking the symbol as undefined, and there are no |
| 4958 | non-weak references to this symbol from a regular object, then |
| 4959 | mark the symbol as weak undefined; if there are non-weak |
| 4960 | references, mark the symbol as strong. We can't do this earlier, |
| 4961 | because it might not be marked as undefined until the |
| 4962 | finish_dynamic_symbol routine gets through with it. */ |
| 4963 | if (sym.st_shndx == SHN_UNDEF |
| 4964 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 |
| 4965 | && (ELF_ST_BIND(sym.st_info) == STB_GLOBAL |
| 4966 | || ELF_ST_BIND(sym.st_info) == STB_WEAK)) |
| 4967 | { |
| 4968 | int bindtype; |
| 4969 | |
| 4970 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0) |
| 4971 | bindtype = STB_GLOBAL; |
| 4972 | else |
| 4973 | bindtype = STB_WEAK; |
| 4974 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); |
| 4975 | } |
| 4976 | |
| 4977 | /* If this symbol should be put in the .dynsym section, then put it |
| 4978 | there now. We have already know the symbol index. We also fill |
| 4979 | in the entry in the .hash section. */ |
| 4980 | if (h->dynindx != -1 |
| 4981 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
| 4982 | { |
| 4983 | size_t bucketcount; |
| 4984 | size_t bucket; |
| 4985 | size_t hash_entry_size; |
| 4986 | bfd_byte *bucketpos; |
| 4987 | bfd_vma chain; |
| 4988 | |
| 4989 | sym.st_name = h->dynstr_index; |
| 4990 | |
| 4991 | elf_swap_symbol_out (finfo->output_bfd, &sym, |
| 4992 | (PTR) (((Elf_External_Sym *) |
| 4993 | finfo->dynsym_sec->contents) |
| 4994 | + h->dynindx)); |
| 4995 | |
| 4996 | bucketcount = elf_hash_table (finfo->info)->bucketcount; |
| 4997 | bucket = h->elf_hash_value % bucketcount; |
| 4998 | hash_entry_size |
| 4999 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; |
| 5000 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents |
| 5001 | + (bucket + 2) * hash_entry_size); |
| 5002 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); |
| 5003 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); |
| 5004 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, |
| 5005 | ((bfd_byte *) finfo->hash_sec->contents |
| 5006 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
| 5007 | |
| 5008 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) |
| 5009 | { |
| 5010 | Elf_Internal_Versym iversym; |
| 5011 | |
| 5012 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) |
| 5013 | { |
| 5014 | if (h->verinfo.verdef == NULL) |
| 5015 | iversym.vs_vers = 0; |
| 5016 | else |
| 5017 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; |
| 5018 | } |
| 5019 | else |
| 5020 | { |
| 5021 | if (h->verinfo.vertree == NULL) |
| 5022 | iversym.vs_vers = 1; |
| 5023 | else |
| 5024 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; |
| 5025 | } |
| 5026 | |
| 5027 | if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0) |
| 5028 | iversym.vs_vers |= VERSYM_HIDDEN; |
| 5029 | |
| 5030 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, |
| 5031 | (((Elf_External_Versym *) |
| 5032 | finfo->symver_sec->contents) |
| 5033 | + h->dynindx)); |
| 5034 | } |
| 5035 | } |
| 5036 | |
| 5037 | /* If we're stripping it, then it was just a dynamic symbol, and |
| 5038 | there's nothing else to do. */ |
| 5039 | if (strip) |
| 5040 | return true; |
| 5041 | |
| 5042 | h->indx = bfd_get_symcount (finfo->output_bfd); |
| 5043 | |
| 5044 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec)) |
| 5045 | { |
| 5046 | eoinfo->failed = true; |
| 5047 | return false; |
| 5048 | } |
| 5049 | |
| 5050 | return true; |
| 5051 | } |
| 5052 | |
| 5053 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which |
| 5054 | originated from the section given by INPUT_REL_HDR) to the |
| 5055 | OUTPUT_BFD. */ |
| 5056 | |
| 5057 | static void |
| 5058 | elf_link_output_relocs (output_bfd, input_section, input_rel_hdr, |
| 5059 | internal_relocs) |
| 5060 | bfd *output_bfd; |
| 5061 | asection *input_section; |
| 5062 | Elf_Internal_Shdr *input_rel_hdr; |
| 5063 | Elf_Internal_Rela *internal_relocs; |
| 5064 | { |
| 5065 | Elf_Internal_Rela *irela; |
| 5066 | Elf_Internal_Rela *irelaend; |
| 5067 | Elf_Internal_Shdr *output_rel_hdr; |
| 5068 | asection *output_section; |
| 5069 | unsigned int *rel_countp = NULL; |
| 5070 | |
| 5071 | output_section = input_section->output_section; |
| 5072 | output_rel_hdr = NULL; |
| 5073 | |
| 5074 | if (elf_section_data (output_section)->rel_hdr.sh_entsize |
| 5075 | == input_rel_hdr->sh_entsize) |
| 5076 | { |
| 5077 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; |
| 5078 | rel_countp = &elf_section_data (output_section)->rel_count; |
| 5079 | } |
| 5080 | else if (elf_section_data (output_section)->rel_hdr2 |
| 5081 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize |
| 5082 | == input_rel_hdr->sh_entsize)) |
| 5083 | { |
| 5084 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; |
| 5085 | rel_countp = &elf_section_data (output_section)->rel_count2; |
| 5086 | } |
| 5087 | |
| 5088 | BFD_ASSERT (output_rel_hdr != NULL); |
| 5089 | |
| 5090 | irela = internal_relocs; |
| 5091 | irelaend = irela + input_rel_hdr->sh_size / input_rel_hdr->sh_entsize; |
| 5092 | if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) |
| 5093 | { |
| 5094 | Elf_External_Rel *erel; |
| 5095 | |
| 5096 | erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp); |
| 5097 | for (; irela < irelaend; irela++, erel++) |
| 5098 | { |
| 5099 | Elf_Internal_Rel irel; |
| 5100 | |
| 5101 | irel.r_offset = irela->r_offset; |
| 5102 | irel.r_info = irela->r_info; |
| 5103 | BFD_ASSERT (irela->r_addend == 0); |
| 5104 | elf_swap_reloc_out (output_bfd, &irel, erel); |
| 5105 | } |
| 5106 | } |
| 5107 | else |
| 5108 | { |
| 5109 | Elf_External_Rela *erela; |
| 5110 | |
| 5111 | BFD_ASSERT (input_rel_hdr->sh_entsize |
| 5112 | == sizeof (Elf_External_Rela)); |
| 5113 | erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp); |
| 5114 | for (; irela < irelaend; irela++, erela++) |
| 5115 | elf_swap_reloca_out (output_bfd, irela, erela); |
| 5116 | } |
| 5117 | |
| 5118 | /* Bump the counter, so that we know where to add the next set of |
| 5119 | relocations. */ |
| 5120 | *rel_countp += input_rel_hdr->sh_size / input_rel_hdr->sh_entsize; |
| 5121 | } |
| 5122 | |
| 5123 | /* Link an input file into the linker output file. This function |
| 5124 | handles all the sections and relocations of the input file at once. |
| 5125 | This is so that we only have to read the local symbols once, and |
| 5126 | don't have to keep them in memory. */ |
| 5127 | |
| 5128 | static boolean |
| 5129 | elf_link_input_bfd (finfo, input_bfd) |
| 5130 | struct elf_final_link_info *finfo; |
| 5131 | bfd *input_bfd; |
| 5132 | { |
| 5133 | boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *, |
| 5134 | bfd *, asection *, bfd_byte *, |
| 5135 | Elf_Internal_Rela *, |
| 5136 | Elf_Internal_Sym *, asection **)); |
| 5137 | bfd *output_bfd; |
| 5138 | Elf_Internal_Shdr *symtab_hdr; |
| 5139 | size_t locsymcount; |
| 5140 | size_t extsymoff; |
| 5141 | Elf_External_Sym *external_syms; |
| 5142 | Elf_External_Sym *esym; |
| 5143 | Elf_External_Sym *esymend; |
| 5144 | Elf_Internal_Sym *isym; |
| 5145 | long *pindex; |
| 5146 | asection **ppsection; |
| 5147 | asection *o; |
| 5148 | struct elf_backend_data *bed; |
| 5149 | |
| 5150 | output_bfd = finfo->output_bfd; |
| 5151 | bed = get_elf_backend_data (output_bfd); |
| 5152 | relocate_section = bed->elf_backend_relocate_section; |
| 5153 | |
| 5154 | /* If this is a dynamic object, we don't want to do anything here: |
| 5155 | we don't want the local symbols, and we don't want the section |
| 5156 | contents. */ |
| 5157 | if ((input_bfd->flags & DYNAMIC) != 0) |
| 5158 | return true; |
| 5159 | |
| 5160 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 5161 | if (elf_bad_symtab (input_bfd)) |
| 5162 | { |
| 5163 | locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym); |
| 5164 | extsymoff = 0; |
| 5165 | } |
| 5166 | else |
| 5167 | { |
| 5168 | locsymcount = symtab_hdr->sh_info; |
| 5169 | extsymoff = symtab_hdr->sh_info; |
| 5170 | } |
| 5171 | |
| 5172 | /* Read the local symbols. */ |
| 5173 | if (symtab_hdr->contents != NULL) |
| 5174 | external_syms = (Elf_External_Sym *) symtab_hdr->contents; |
| 5175 | else if (locsymcount == 0) |
| 5176 | external_syms = NULL; |
| 5177 | else |
| 5178 | { |
| 5179 | external_syms = finfo->external_syms; |
| 5180 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 |
| 5181 | || (bfd_read (external_syms, sizeof (Elf_External_Sym), |
| 5182 | locsymcount, input_bfd) |
| 5183 | != locsymcount * sizeof (Elf_External_Sym))) |
| 5184 | return false; |
| 5185 | } |
| 5186 | |
| 5187 | /* Swap in the local symbols and write out the ones which we know |
| 5188 | are going into the output file. */ |
| 5189 | esym = external_syms; |
| 5190 | esymend = esym + locsymcount; |
| 5191 | isym = finfo->internal_syms; |
| 5192 | pindex = finfo->indices; |
| 5193 | ppsection = finfo->sections; |
| 5194 | for (; esym < esymend; esym++, isym++, pindex++, ppsection++) |
| 5195 | { |
| 5196 | asection *isec; |
| 5197 | const char *name; |
| 5198 | Elf_Internal_Sym osym; |
| 5199 | |
| 5200 | elf_swap_symbol_in (input_bfd, esym, isym); |
| 5201 | *pindex = -1; |
| 5202 | |
| 5203 | if (elf_bad_symtab (input_bfd)) |
| 5204 | { |
| 5205 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) |
| 5206 | { |
| 5207 | *ppsection = NULL; |
| 5208 | continue; |
| 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | if (isym->st_shndx == SHN_UNDEF) |
| 5213 | isec = bfd_und_section_ptr; |
| 5214 | else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE) |
| 5215 | isec = section_from_elf_index (input_bfd, isym->st_shndx); |
| 5216 | else if (isym->st_shndx == SHN_ABS) |
| 5217 | isec = bfd_abs_section_ptr; |
| 5218 | else if (isym->st_shndx == SHN_COMMON) |
| 5219 | isec = bfd_com_section_ptr; |
| 5220 | else |
| 5221 | { |
| 5222 | /* Who knows? */ |
| 5223 | isec = NULL; |
| 5224 | } |
| 5225 | |
| 5226 | *ppsection = isec; |
| 5227 | |
| 5228 | /* Don't output the first, undefined, symbol. */ |
| 5229 | if (esym == external_syms) |
| 5230 | continue; |
| 5231 | |
| 5232 | /* If we are stripping all symbols, we don't want to output this |
| 5233 | one. */ |
| 5234 | if (finfo->info->strip == strip_all) |
| 5235 | continue; |
| 5236 | |
| 5237 | /* We never output section symbols. Instead, we use the section |
| 5238 | symbol of the corresponding section in the output file. */ |
| 5239 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) |
| 5240 | continue; |
| 5241 | |
| 5242 | /* If we are discarding all local symbols, we don't want to |
| 5243 | output this one. If we are generating a relocateable output |
| 5244 | file, then some of the local symbols may be required by |
| 5245 | relocs; we output them below as we discover that they are |
| 5246 | needed. */ |
| 5247 | if (finfo->info->discard == discard_all) |
| 5248 | continue; |
| 5249 | |
| 5250 | /* If this symbol is defined in a section which we are |
| 5251 | discarding, we don't need to keep it, but note that |
| 5252 | linker_mark is only reliable for sections that have contents. |
| 5253 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE |
| 5254 | as well as linker_mark. */ |
| 5255 | if (isym->st_shndx > 0 |
| 5256 | && isym->st_shndx < SHN_LORESERVE |
| 5257 | && isec != NULL |
| 5258 | && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) |
| 5259 | || (! finfo->info->relocateable |
| 5260 | && (isec->flags & SEC_EXCLUDE) != 0))) |
| 5261 | continue; |
| 5262 | |
| 5263 | /* Get the name of the symbol. */ |
| 5264 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, |
| 5265 | isym->st_name); |
| 5266 | if (name == NULL) |
| 5267 | return false; |
| 5268 | |
| 5269 | /* See if we are discarding symbols with this name. */ |
| 5270 | if ((finfo->info->strip == strip_some |
| 5271 | && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false) |
| 5272 | == NULL)) |
| 5273 | || (finfo->info->discard == discard_l |
| 5274 | && bfd_is_local_label_name (input_bfd, name))) |
| 5275 | continue; |
| 5276 | |
| 5277 | /* If we get here, we are going to output this symbol. */ |
| 5278 | |
| 5279 | osym = *isym; |
| 5280 | |
| 5281 | /* Adjust the section index for the output file. */ |
| 5282 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, |
| 5283 | isec->output_section); |
| 5284 | if (osym.st_shndx == (unsigned short) -1) |
| 5285 | return false; |
| 5286 | |
| 5287 | *pindex = bfd_get_symcount (output_bfd); |
| 5288 | |
| 5289 | /* ELF symbols in relocateable files are section relative, but |
| 5290 | in executable files they are virtual addresses. Note that |
| 5291 | this code assumes that all ELF sections have an associated |
| 5292 | BFD section with a reasonable value for output_offset; below |
| 5293 | we assume that they also have a reasonable value for |
| 5294 | output_section. Any special sections must be set up to meet |
| 5295 | these requirements. */ |
| 5296 | osym.st_value += isec->output_offset; |
| 5297 | if (! finfo->info->relocateable) |
| 5298 | osym.st_value += isec->output_section->vma; |
| 5299 | |
| 5300 | if (! elf_link_output_sym (finfo, name, &osym, isec)) |
| 5301 | return false; |
| 5302 | } |
| 5303 | |
| 5304 | /* Relocate the contents of each section. */ |
| 5305 | for (o = input_bfd->sections; o != NULL; o = o->next) |
| 5306 | { |
| 5307 | bfd_byte *contents; |
| 5308 | |
| 5309 | if (! o->linker_mark) |
| 5310 | { |
| 5311 | /* This section was omitted from the link. */ |
| 5312 | continue; |
| 5313 | } |
| 5314 | |
| 5315 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
| 5316 | || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0)) |
| 5317 | continue; |
| 5318 | |
| 5319 | if ((o->flags & SEC_LINKER_CREATED) != 0) |
| 5320 | { |
| 5321 | /* Section was created by elf_link_create_dynamic_sections |
| 5322 | or somesuch. */ |
| 5323 | continue; |
| 5324 | } |
| 5325 | |
| 5326 | /* Get the contents of the section. They have been cached by a |
| 5327 | relaxation routine. Note that o is a section in an input |
| 5328 | file, so the contents field will not have been set by any of |
| 5329 | the routines which work on output files. */ |
| 5330 | if (elf_section_data (o)->this_hdr.contents != NULL) |
| 5331 | contents = elf_section_data (o)->this_hdr.contents; |
| 5332 | else |
| 5333 | { |
| 5334 | contents = finfo->contents; |
| 5335 | if (! bfd_get_section_contents (input_bfd, o, contents, |
| 5336 | (file_ptr) 0, o->_raw_size)) |
| 5337 | return false; |
| 5338 | } |
| 5339 | |
| 5340 | if ((o->flags & SEC_RELOC) != 0) |
| 5341 | { |
| 5342 | Elf_Internal_Rela *internal_relocs; |
| 5343 | |
| 5344 | /* Get the swapped relocs. */ |
| 5345 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) |
| 5346 | (input_bfd, o, finfo->external_relocs, |
| 5347 | finfo->internal_relocs, false)); |
| 5348 | if (internal_relocs == NULL |
| 5349 | && o->reloc_count > 0) |
| 5350 | return false; |
| 5351 | |
| 5352 | /* Relocate the section by invoking a back end routine. |
| 5353 | |
| 5354 | The back end routine is responsible for adjusting the |
| 5355 | section contents as necessary, and (if using Rela relocs |
| 5356 | and generating a relocateable output file) adjusting the |
| 5357 | reloc addend as necessary. |
| 5358 | |
| 5359 | The back end routine does not have to worry about setting |
| 5360 | the reloc address or the reloc symbol index. |
| 5361 | |
| 5362 | The back end routine is given a pointer to the swapped in |
| 5363 | internal symbols, and can access the hash table entries |
| 5364 | for the external symbols via elf_sym_hashes (input_bfd). |
| 5365 | |
| 5366 | When generating relocateable output, the back end routine |
| 5367 | must handle STB_LOCAL/STT_SECTION symbols specially. The |
| 5368 | output symbol is going to be a section symbol |
| 5369 | corresponding to the output section, which will require |
| 5370 | the addend to be adjusted. */ |
| 5371 | |
| 5372 | if (! (*relocate_section) (output_bfd, finfo->info, |
| 5373 | input_bfd, o, contents, |
| 5374 | internal_relocs, |
| 5375 | finfo->internal_syms, |
| 5376 | finfo->sections)) |
| 5377 | return false; |
| 5378 | |
| 5379 | if (finfo->info->relocateable) |
| 5380 | { |
| 5381 | Elf_Internal_Rela *irela; |
| 5382 | Elf_Internal_Rela *irelaend; |
| 5383 | struct elf_link_hash_entry **rel_hash; |
| 5384 | Elf_Internal_Shdr *input_rel_hdr; |
| 5385 | |
| 5386 | /* Adjust the reloc addresses and symbol indices. */ |
| 5387 | |
| 5388 | irela = internal_relocs; |
| 5389 | irelaend = |
| 5390 | irela + o->reloc_count * bed->s->int_rels_per_ext_rel; |
| 5391 | rel_hash = (elf_section_data (o->output_section)->rel_hashes |
| 5392 | + elf_section_data (o->output_section)->rel_count |
| 5393 | + elf_section_data (o->output_section)->rel_count2); |
| 5394 | for (; irela < irelaend; irela++, rel_hash++) |
| 5395 | { |
| 5396 | unsigned long r_symndx; |
| 5397 | Elf_Internal_Sym *isym; |
| 5398 | asection *sec; |
| 5399 | |
| 5400 | irela->r_offset += o->output_offset; |
| 5401 | |
| 5402 | r_symndx = ELF_R_SYM (irela->r_info); |
| 5403 | |
| 5404 | if (r_symndx == 0) |
| 5405 | continue; |
| 5406 | |
| 5407 | if (r_symndx >= locsymcount |
| 5408 | || (elf_bad_symtab (input_bfd) |
| 5409 | && finfo->sections[r_symndx] == NULL)) |
| 5410 | { |
| 5411 | struct elf_link_hash_entry *rh; |
| 5412 | long indx; |
| 5413 | |
| 5414 | /* This is a reloc against a global symbol. We |
| 5415 | have not yet output all the local symbols, so |
| 5416 | we do not know the symbol index of any global |
| 5417 | symbol. We set the rel_hash entry for this |
| 5418 | reloc to point to the global hash table entry |
| 5419 | for this symbol. The symbol index is then |
| 5420 | set at the end of elf_bfd_final_link. */ |
| 5421 | indx = r_symndx - extsymoff; |
| 5422 | rh = elf_sym_hashes (input_bfd)[indx]; |
| 5423 | while (rh->root.type == bfd_link_hash_indirect |
| 5424 | || rh->root.type == bfd_link_hash_warning) |
| 5425 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; |
| 5426 | |
| 5427 | /* Setting the index to -2 tells |
| 5428 | elf_link_output_extsym that this symbol is |
| 5429 | used by a reloc. */ |
| 5430 | BFD_ASSERT (rh->indx < 0); |
| 5431 | rh->indx = -2; |
| 5432 | |
| 5433 | *rel_hash = rh; |
| 5434 | |
| 5435 | continue; |
| 5436 | } |
| 5437 | |
| 5438 | /* This is a reloc against a local symbol. */ |
| 5439 | |
| 5440 | *rel_hash = NULL; |
| 5441 | isym = finfo->internal_syms + r_symndx; |
| 5442 | sec = finfo->sections[r_symndx]; |
| 5443 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) |
| 5444 | { |
| 5445 | /* I suppose the backend ought to fill in the |
| 5446 | section of any STT_SECTION symbol against a |
| 5447 | processor specific section. If we have |
| 5448 | discarded a section, the output_section will |
| 5449 | be the absolute section. */ |
| 5450 | if (sec != NULL |
| 5451 | && (bfd_is_abs_section (sec) |
| 5452 | || (sec->output_section != NULL |
| 5453 | && bfd_is_abs_section (sec->output_section)))) |
| 5454 | r_symndx = 0; |
| 5455 | else if (sec == NULL || sec->owner == NULL) |
| 5456 | { |
| 5457 | bfd_set_error (bfd_error_bad_value); |
| 5458 | return false; |
| 5459 | } |
| 5460 | else |
| 5461 | { |
| 5462 | r_symndx = sec->output_section->target_index; |
| 5463 | BFD_ASSERT (r_symndx != 0); |
| 5464 | } |
| 5465 | } |
| 5466 | else |
| 5467 | { |
| 5468 | if (finfo->indices[r_symndx] == -1) |
| 5469 | { |
| 5470 | unsigned long link; |
| 5471 | const char *name; |
| 5472 | asection *osec; |
| 5473 | |
| 5474 | if (finfo->info->strip == strip_all) |
| 5475 | { |
| 5476 | /* You can't do ld -r -s. */ |
| 5477 | bfd_set_error (bfd_error_invalid_operation); |
| 5478 | return false; |
| 5479 | } |
| 5480 | |
| 5481 | /* This symbol was skipped earlier, but |
| 5482 | since it is needed by a reloc, we |
| 5483 | must output it now. */ |
| 5484 | link = symtab_hdr->sh_link; |
| 5485 | name = bfd_elf_string_from_elf_section (input_bfd, |
| 5486 | link, |
| 5487 | isym->st_name); |
| 5488 | if (name == NULL) |
| 5489 | return false; |
| 5490 | |
| 5491 | osec = sec->output_section; |
| 5492 | isym->st_shndx = |
| 5493 | _bfd_elf_section_from_bfd_section (output_bfd, |
| 5494 | osec); |
| 5495 | if (isym->st_shndx == (unsigned short) -1) |
| 5496 | return false; |
| 5497 | |
| 5498 | isym->st_value += sec->output_offset; |
| 5499 | if (! finfo->info->relocateable) |
| 5500 | isym->st_value += osec->vma; |
| 5501 | |
| 5502 | finfo->indices[r_symndx] = bfd_get_symcount (output_bfd); |
| 5503 | |
| 5504 | if (! elf_link_output_sym (finfo, name, isym, sec)) |
| 5505 | return false; |
| 5506 | } |
| 5507 | |
| 5508 | r_symndx = finfo->indices[r_symndx]; |
| 5509 | } |
| 5510 | |
| 5511 | irela->r_info = ELF_R_INFO (r_symndx, |
| 5512 | ELF_R_TYPE (irela->r_info)); |
| 5513 | } |
| 5514 | |
| 5515 | /* Swap out the relocs. */ |
| 5516 | input_rel_hdr = &elf_section_data (o)->rel_hdr; |
| 5517 | elf_link_output_relocs (output_bfd, o, |
| 5518 | input_rel_hdr, |
| 5519 | internal_relocs); |
| 5520 | internal_relocs |
| 5521 | += input_rel_hdr->sh_size / input_rel_hdr->sh_entsize; |
| 5522 | input_rel_hdr = elf_section_data (o)->rel_hdr2; |
| 5523 | if (input_rel_hdr) |
| 5524 | elf_link_output_relocs (output_bfd, o, |
| 5525 | input_rel_hdr, |
| 5526 | internal_relocs); |
| 5527 | } |
| 5528 | } |
| 5529 | |
| 5530 | /* Write out the modified section contents. */ |
| 5531 | if (elf_section_data (o)->stab_info == NULL) |
| 5532 | { |
| 5533 | if (! (o->flags & SEC_EXCLUDE) && |
| 5534 | ! bfd_set_section_contents (output_bfd, o->output_section, |
| 5535 | contents, o->output_offset, |
| 5536 | (o->_cooked_size != 0 |
| 5537 | ? o->_cooked_size |
| 5538 | : o->_raw_size))) |
| 5539 | return false; |
| 5540 | } |
| 5541 | else |
| 5542 | { |
| 5543 | if (! (_bfd_write_section_stabs |
| 5544 | (output_bfd, &elf_hash_table (finfo->info)->stab_info, |
| 5545 | o, &elf_section_data (o)->stab_info, contents))) |
| 5546 | return false; |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | return true; |
| 5551 | } |
| 5552 | |
| 5553 | /* Generate a reloc when linking an ELF file. This is a reloc |
| 5554 | requested by the linker, and does come from any input file. This |
| 5555 | is used to build constructor and destructor tables when linking |
| 5556 | with -Ur. */ |
| 5557 | |
| 5558 | static boolean |
| 5559 | elf_reloc_link_order (output_bfd, info, output_section, link_order) |
| 5560 | bfd *output_bfd; |
| 5561 | struct bfd_link_info *info; |
| 5562 | asection *output_section; |
| 5563 | struct bfd_link_order *link_order; |
| 5564 | { |
| 5565 | reloc_howto_type *howto; |
| 5566 | long indx; |
| 5567 | bfd_vma offset; |
| 5568 | bfd_vma addend; |
| 5569 | struct elf_link_hash_entry **rel_hash_ptr; |
| 5570 | Elf_Internal_Shdr *rel_hdr; |
| 5571 | |
| 5572 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); |
| 5573 | if (howto == NULL) |
| 5574 | { |
| 5575 | bfd_set_error (bfd_error_bad_value); |
| 5576 | return false; |
| 5577 | } |
| 5578 | |
| 5579 | addend = link_order->u.reloc.p->addend; |
| 5580 | |
| 5581 | /* Figure out the symbol index. */ |
| 5582 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes |
| 5583 | + elf_section_data (output_section)->rel_count |
| 5584 | + elf_section_data (output_section)->rel_count2); |
| 5585 | if (link_order->type == bfd_section_reloc_link_order) |
| 5586 | { |
| 5587 | indx = link_order->u.reloc.p->u.section->target_index; |
| 5588 | BFD_ASSERT (indx != 0); |
| 5589 | *rel_hash_ptr = NULL; |
| 5590 | } |
| 5591 | else |
| 5592 | { |
| 5593 | struct elf_link_hash_entry *h; |
| 5594 | |
| 5595 | /* Treat a reloc against a defined symbol as though it were |
| 5596 | actually against the section. */ |
| 5597 | h = ((struct elf_link_hash_entry *) |
| 5598 | bfd_wrapped_link_hash_lookup (output_bfd, info, |
| 5599 | link_order->u.reloc.p->u.name, |
| 5600 | false, false, true)); |
| 5601 | if (h != NULL |
| 5602 | && (h->root.type == bfd_link_hash_defined |
| 5603 | || h->root.type == bfd_link_hash_defweak)) |
| 5604 | { |
| 5605 | asection *section; |
| 5606 | |
| 5607 | section = h->root.u.def.section; |
| 5608 | indx = section->output_section->target_index; |
| 5609 | *rel_hash_ptr = NULL; |
| 5610 | /* It seems that we ought to add the symbol value to the |
| 5611 | addend here, but in practice it has already been added |
| 5612 | because it was passed to constructor_callback. */ |
| 5613 | addend += section->output_section->vma + section->output_offset; |
| 5614 | } |
| 5615 | else if (h != NULL) |
| 5616 | { |
| 5617 | /* Setting the index to -2 tells elf_link_output_extsym that |
| 5618 | this symbol is used by a reloc. */ |
| 5619 | h->indx = -2; |
| 5620 | *rel_hash_ptr = h; |
| 5621 | indx = 0; |
| 5622 | } |
| 5623 | else |
| 5624 | { |
| 5625 | if (! ((*info->callbacks->unattached_reloc) |
| 5626 | (info, link_order->u.reloc.p->u.name, (bfd *) NULL, |
| 5627 | (asection *) NULL, (bfd_vma) 0))) |
| 5628 | return false; |
| 5629 | indx = 0; |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | /* If this is an inplace reloc, we must write the addend into the |
| 5634 | object file. */ |
| 5635 | if (howto->partial_inplace && addend != 0) |
| 5636 | { |
| 5637 | bfd_size_type size; |
| 5638 | bfd_reloc_status_type rstat; |
| 5639 | bfd_byte *buf; |
| 5640 | boolean ok; |
| 5641 | |
| 5642 | size = bfd_get_reloc_size (howto); |
| 5643 | buf = (bfd_byte *) bfd_zmalloc (size); |
| 5644 | if (buf == (bfd_byte *) NULL) |
| 5645 | return false; |
| 5646 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); |
| 5647 | switch (rstat) |
| 5648 | { |
| 5649 | case bfd_reloc_ok: |
| 5650 | break; |
| 5651 | default: |
| 5652 | case bfd_reloc_outofrange: |
| 5653 | abort (); |
| 5654 | case bfd_reloc_overflow: |
| 5655 | if (! ((*info->callbacks->reloc_overflow) |
| 5656 | (info, |
| 5657 | (link_order->type == bfd_section_reloc_link_order |
| 5658 | ? bfd_section_name (output_bfd, |
| 5659 | link_order->u.reloc.p->u.section) |
| 5660 | : link_order->u.reloc.p->u.name), |
| 5661 | howto->name, addend, (bfd *) NULL, (asection *) NULL, |
| 5662 | (bfd_vma) 0))) |
| 5663 | { |
| 5664 | free (buf); |
| 5665 | return false; |
| 5666 | } |
| 5667 | break; |
| 5668 | } |
| 5669 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, |
| 5670 | (file_ptr) link_order->offset, size); |
| 5671 | free (buf); |
| 5672 | if (! ok) |
| 5673 | return false; |
| 5674 | } |
| 5675 | |
| 5676 | /* The address of a reloc is relative to the section in a |
| 5677 | relocateable file, and is a virtual address in an executable |
| 5678 | file. */ |
| 5679 | offset = link_order->offset; |
| 5680 | if (! info->relocateable) |
| 5681 | offset += output_section->vma; |
| 5682 | |
| 5683 | rel_hdr = &elf_section_data (output_section)->rel_hdr; |
| 5684 | |
| 5685 | if (rel_hdr->sh_type == SHT_REL) |
| 5686 | { |
| 5687 | Elf_Internal_Rel irel; |
| 5688 | Elf_External_Rel *erel; |
| 5689 | |
| 5690 | irel.r_offset = offset; |
| 5691 | irel.r_info = ELF_R_INFO (indx, howto->type); |
| 5692 | erel = ((Elf_External_Rel *) rel_hdr->contents |
| 5693 | + elf_section_data (output_section)->rel_count); |
| 5694 | elf_swap_reloc_out (output_bfd, &irel, erel); |
| 5695 | } |
| 5696 | else |
| 5697 | { |
| 5698 | Elf_Internal_Rela irela; |
| 5699 | Elf_External_Rela *erela; |
| 5700 | |
| 5701 | irela.r_offset = offset; |
| 5702 | irela.r_info = ELF_R_INFO (indx, howto->type); |
| 5703 | irela.r_addend = addend; |
| 5704 | erela = ((Elf_External_Rela *) rel_hdr->contents |
| 5705 | + elf_section_data (output_section)->rel_count); |
| 5706 | elf_swap_reloca_out (output_bfd, &irela, erela); |
| 5707 | } |
| 5708 | |
| 5709 | ++elf_section_data (output_section)->rel_count; |
| 5710 | |
| 5711 | return true; |
| 5712 | } |
| 5713 | |
| 5714 | \f |
| 5715 | /* Allocate a pointer to live in a linker created section. */ |
| 5716 | |
| 5717 | boolean |
| 5718 | elf_create_pointer_linker_section (abfd, info, lsect, h, rel) |
| 5719 | bfd *abfd; |
| 5720 | struct bfd_link_info *info; |
| 5721 | elf_linker_section_t *lsect; |
| 5722 | struct elf_link_hash_entry *h; |
| 5723 | const Elf_Internal_Rela *rel; |
| 5724 | { |
| 5725 | elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL; |
| 5726 | elf_linker_section_pointers_t *linker_section_ptr; |
| 5727 | unsigned long r_symndx = ELF_R_SYM (rel->r_info);; |
| 5728 | |
| 5729 | BFD_ASSERT (lsect != NULL); |
| 5730 | |
| 5731 | /* Is this a global symbol? */ |
| 5732 | if (h != NULL) |
| 5733 | { |
| 5734 | /* Has this symbol already been allocated, if so, our work is done */ |
| 5735 | if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer, |
| 5736 | rel->r_addend, |
| 5737 | lsect->which)) |
| 5738 | return true; |
| 5739 | |
| 5740 | ptr_linker_section_ptr = &h->linker_section_pointer; |
| 5741 | /* Make sure this symbol is output as a dynamic symbol. */ |
| 5742 | if (h->dynindx == -1) |
| 5743 | { |
| 5744 | if (! elf_link_record_dynamic_symbol (info, h)) |
| 5745 | return false; |
| 5746 | } |
| 5747 | |
| 5748 | if (lsect->rel_section) |
| 5749 | lsect->rel_section->_raw_size += sizeof (Elf_External_Rela); |
| 5750 | } |
| 5751 | |
| 5752 | else /* Allocation of a pointer to a local symbol */ |
| 5753 | { |
| 5754 | elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd); |
| 5755 | |
| 5756 | /* Allocate a table to hold the local symbols if first time */ |
| 5757 | if (!ptr) |
| 5758 | { |
| 5759 | unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info; |
| 5760 | register unsigned int i; |
| 5761 | |
| 5762 | ptr = (elf_linker_section_pointers_t **) |
| 5763 | bfd_alloc (abfd, num_symbols * sizeof (elf_linker_section_pointers_t *)); |
| 5764 | |
| 5765 | if (!ptr) |
| 5766 | return false; |
| 5767 | |
| 5768 | elf_local_ptr_offsets (abfd) = ptr; |
| 5769 | for (i = 0; i < num_symbols; i++) |
| 5770 | ptr[i] = (elf_linker_section_pointers_t *)0; |
| 5771 | } |
| 5772 | |
| 5773 | /* Has this symbol already been allocated, if so, our work is done */ |
| 5774 | if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx], |
| 5775 | rel->r_addend, |
| 5776 | lsect->which)) |
| 5777 | return true; |
| 5778 | |
| 5779 | ptr_linker_section_ptr = &ptr[r_symndx]; |
| 5780 | |
| 5781 | if (info->shared) |
| 5782 | { |
| 5783 | /* If we are generating a shared object, we need to |
| 5784 | output a R_<xxx>_RELATIVE reloc so that the |
| 5785 | dynamic linker can adjust this GOT entry. */ |
| 5786 | BFD_ASSERT (lsect->rel_section != NULL); |
| 5787 | lsect->rel_section->_raw_size += sizeof (Elf_External_Rela); |
| 5788 | } |
| 5789 | } |
| 5790 | |
| 5791 | /* Allocate space for a pointer in the linker section, and allocate a new pointer record |
| 5792 | from internal memory. */ |
| 5793 | BFD_ASSERT (ptr_linker_section_ptr != NULL); |
| 5794 | linker_section_ptr = (elf_linker_section_pointers_t *) |
| 5795 | bfd_alloc (abfd, sizeof (elf_linker_section_pointers_t)); |
| 5796 | |
| 5797 | if (!linker_section_ptr) |
| 5798 | return false; |
| 5799 | |
| 5800 | linker_section_ptr->next = *ptr_linker_section_ptr; |
| 5801 | linker_section_ptr->addend = rel->r_addend; |
| 5802 | linker_section_ptr->which = lsect->which; |
| 5803 | linker_section_ptr->written_address_p = false; |
| 5804 | *ptr_linker_section_ptr = linker_section_ptr; |
| 5805 | |
| 5806 | #if 0 |
| 5807 | if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset) |
| 5808 | { |
| 5809 | linker_section_ptr->offset = lsect->section->_raw_size - lsect->hole_size + (ARCH_SIZE / 8); |
| 5810 | lsect->hole_offset += ARCH_SIZE / 8; |
| 5811 | lsect->sym_offset += ARCH_SIZE / 8; |
| 5812 | if (lsect->sym_hash) /* Bump up symbol value if needed */ |
| 5813 | { |
| 5814 | lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8; |
| 5815 | #ifdef DEBUG |
| 5816 | fprintf (stderr, "Bump up %s by %ld, current value = %ld\n", |
| 5817 | lsect->sym_hash->root.root.string, |
| 5818 | (long)ARCH_SIZE / 8, |
| 5819 | (long)lsect->sym_hash->root.u.def.value); |
| 5820 | #endif |
| 5821 | } |
| 5822 | } |
| 5823 | else |
| 5824 | #endif |
| 5825 | linker_section_ptr->offset = lsect->section->_raw_size; |
| 5826 | |
| 5827 | lsect->section->_raw_size += ARCH_SIZE / 8; |
| 5828 | |
| 5829 | #ifdef DEBUG |
| 5830 | fprintf (stderr, "Create pointer in linker section %s, offset = %ld, section size = %ld\n", |
| 5831 | lsect->name, (long)linker_section_ptr->offset, (long)lsect->section->_raw_size); |
| 5832 | #endif |
| 5833 | |
| 5834 | return true; |
| 5835 | } |
| 5836 | |
| 5837 | \f |
| 5838 | #if ARCH_SIZE==64 |
| 5839 | #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR) |
| 5840 | #endif |
| 5841 | #if ARCH_SIZE==32 |
| 5842 | #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR) |
| 5843 | #endif |
| 5844 | |
| 5845 | /* Fill in the address for a pointer generated in alinker section. */ |
| 5846 | |
| 5847 | bfd_vma |
| 5848 | elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, relocation, rel, relative_reloc) |
| 5849 | bfd *output_bfd; |
| 5850 | bfd *input_bfd; |
| 5851 | struct bfd_link_info *info; |
| 5852 | elf_linker_section_t *lsect; |
| 5853 | struct elf_link_hash_entry *h; |
| 5854 | bfd_vma relocation; |
| 5855 | const Elf_Internal_Rela *rel; |
| 5856 | int relative_reloc; |
| 5857 | { |
| 5858 | elf_linker_section_pointers_t *linker_section_ptr; |
| 5859 | |
| 5860 | BFD_ASSERT (lsect != NULL); |
| 5861 | |
| 5862 | if (h != NULL) /* global symbol */ |
| 5863 | { |
| 5864 | linker_section_ptr = _bfd_elf_find_pointer_linker_section (h->linker_section_pointer, |
| 5865 | rel->r_addend, |
| 5866 | lsect->which); |
| 5867 | |
| 5868 | BFD_ASSERT (linker_section_ptr != NULL); |
| 5869 | |
| 5870 | if (! elf_hash_table (info)->dynamic_sections_created |
| 5871 | || (info->shared |
| 5872 | && info->symbolic |
| 5873 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) |
| 5874 | { |
| 5875 | /* This is actually a static link, or it is a |
| 5876 | -Bsymbolic link and the symbol is defined |
| 5877 | locally. We must initialize this entry in the |
| 5878 | global section. |
| 5879 | |
| 5880 | When doing a dynamic link, we create a .rela.<xxx> |
| 5881 | relocation entry to initialize the value. This |
| 5882 | is done in the finish_dynamic_symbol routine. */ |
| 5883 | if (!linker_section_ptr->written_address_p) |
| 5884 | { |
| 5885 | linker_section_ptr->written_address_p = true; |
| 5886 | bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend, |
| 5887 | lsect->section->contents + linker_section_ptr->offset); |
| 5888 | } |
| 5889 | } |
| 5890 | } |
| 5891 | else /* local symbol */ |
| 5892 | { |
| 5893 | unsigned long r_symndx = ELF_R_SYM (rel->r_info); |
| 5894 | BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL); |
| 5895 | BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL); |
| 5896 | linker_section_ptr = _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd)[r_symndx], |
| 5897 | rel->r_addend, |
| 5898 | lsect->which); |
| 5899 | |
| 5900 | BFD_ASSERT (linker_section_ptr != NULL); |
| 5901 | |
| 5902 | /* Write out pointer if it hasn't been rewritten out before */ |
| 5903 | if (!linker_section_ptr->written_address_p) |
| 5904 | { |
| 5905 | linker_section_ptr->written_address_p = true; |
| 5906 | bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend, |
| 5907 | lsect->section->contents + linker_section_ptr->offset); |
| 5908 | |
| 5909 | if (info->shared) |
| 5910 | { |
| 5911 | asection *srel = lsect->rel_section; |
| 5912 | Elf_Internal_Rela outrel; |
| 5913 | |
| 5914 | /* We need to generate a relative reloc for the dynamic linker. */ |
| 5915 | if (!srel) |
| 5916 | lsect->rel_section = srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj, |
| 5917 | lsect->rel_name); |
| 5918 | |
| 5919 | BFD_ASSERT (srel != NULL); |
| 5920 | |
| 5921 | outrel.r_offset = (lsect->section->output_section->vma |
| 5922 | + lsect->section->output_offset |
| 5923 | + linker_section_ptr->offset); |
| 5924 | outrel.r_info = ELF_R_INFO (0, relative_reloc); |
| 5925 | outrel.r_addend = 0; |
| 5926 | elf_swap_reloca_out (output_bfd, &outrel, |
| 5927 | (((Elf_External_Rela *) |
| 5928 | lsect->section->contents) |
| 5929 | + elf_section_data (lsect->section)->rel_count)); |
| 5930 | ++elf_section_data (lsect->section)->rel_count; |
| 5931 | } |
| 5932 | } |
| 5933 | } |
| 5934 | |
| 5935 | relocation = (lsect->section->output_offset |
| 5936 | + linker_section_ptr->offset |
| 5937 | - lsect->hole_offset |
| 5938 | - lsect->sym_offset); |
| 5939 | |
| 5940 | #ifdef DEBUG |
| 5941 | fprintf (stderr, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n", |
| 5942 | lsect->name, (long)relocation, (long)relocation); |
| 5943 | #endif |
| 5944 | |
| 5945 | /* Subtract out the addend, because it will get added back in by the normal |
| 5946 | processing. */ |
| 5947 | return relocation - linker_section_ptr->addend; |
| 5948 | } |
| 5949 | \f |
| 5950 | /* Garbage collect unused sections. */ |
| 5951 | |
| 5952 | static boolean elf_gc_mark |
| 5953 | PARAMS ((struct bfd_link_info *info, asection *sec, |
| 5954 | asection * (*gc_mark_hook) |
| 5955 | PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *, |
| 5956 | struct elf_link_hash_entry *, Elf_Internal_Sym *)))); |
| 5957 | |
| 5958 | static boolean elf_gc_sweep |
| 5959 | PARAMS ((struct bfd_link_info *info, |
| 5960 | boolean (*gc_sweep_hook) |
| 5961 | PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o, |
| 5962 | const Elf_Internal_Rela *relocs)))); |
| 5963 | |
| 5964 | static boolean elf_gc_sweep_symbol |
| 5965 | PARAMS ((struct elf_link_hash_entry *h, PTR idxptr)); |
| 5966 | |
| 5967 | static boolean elf_gc_allocate_got_offsets |
| 5968 | PARAMS ((struct elf_link_hash_entry *h, PTR offarg)); |
| 5969 | |
| 5970 | static boolean elf_gc_propagate_vtable_entries_used |
| 5971 | PARAMS ((struct elf_link_hash_entry *h, PTR dummy)); |
| 5972 | |
| 5973 | static boolean elf_gc_smash_unused_vtentry_relocs |
| 5974 | PARAMS ((struct elf_link_hash_entry *h, PTR dummy)); |
| 5975 | |
| 5976 | /* The mark phase of garbage collection. For a given section, mark |
| 5977 | it, and all the sections which define symbols to which it refers. */ |
| 5978 | |
| 5979 | static boolean |
| 5980 | elf_gc_mark (info, sec, gc_mark_hook) |
| 5981 | struct bfd_link_info *info; |
| 5982 | asection *sec; |
| 5983 | asection * (*gc_mark_hook) |
| 5984 | PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *, |
| 5985 | struct elf_link_hash_entry *, Elf_Internal_Sym *)); |
| 5986 | { |
| 5987 | boolean ret = true; |
| 5988 | |
| 5989 | sec->gc_mark = 1; |
| 5990 | |
| 5991 | /* Look through the section relocs. */ |
| 5992 | |
| 5993 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
| 5994 | { |
| 5995 | Elf_Internal_Rela *relstart, *rel, *relend; |
| 5996 | Elf_Internal_Shdr *symtab_hdr; |
| 5997 | struct elf_link_hash_entry **sym_hashes; |
| 5998 | size_t nlocsyms; |
| 5999 | size_t extsymoff; |
| 6000 | Elf_External_Sym *locsyms, *freesyms = NULL; |
| 6001 | bfd *input_bfd = sec->owner; |
| 6002 | struct elf_backend_data *bed = get_elf_backend_data (input_bfd); |
| 6003 | |
| 6004 | /* GCFIXME: how to arrange so that relocs and symbols are not |
| 6005 | reread continually? */ |
| 6006 | |
| 6007 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 6008 | sym_hashes = elf_sym_hashes (input_bfd); |
| 6009 | |
| 6010 | /* Read the local symbols. */ |
| 6011 | if (elf_bad_symtab (input_bfd)) |
| 6012 | { |
| 6013 | nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym); |
| 6014 | extsymoff = 0; |
| 6015 | } |
| 6016 | else |
| 6017 | extsymoff = nlocsyms = symtab_hdr->sh_info; |
| 6018 | if (symtab_hdr->contents) |
| 6019 | locsyms = (Elf_External_Sym *) symtab_hdr->contents; |
| 6020 | else if (nlocsyms == 0) |
| 6021 | locsyms = NULL; |
| 6022 | else |
| 6023 | { |
| 6024 | locsyms = freesyms = |
| 6025 | bfd_malloc (nlocsyms * sizeof (Elf_External_Sym)); |
| 6026 | if (freesyms == NULL |
| 6027 | || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 |
| 6028 | || (bfd_read (locsyms, sizeof (Elf_External_Sym), |
| 6029 | nlocsyms, input_bfd) |
| 6030 | != nlocsyms * sizeof (Elf_External_Sym))) |
| 6031 | { |
| 6032 | ret = false; |
| 6033 | goto out1; |
| 6034 | } |
| 6035 | } |
| 6036 | |
| 6037 | /* Read the relocations. */ |
| 6038 | relstart = (NAME(_bfd_elf,link_read_relocs) |
| 6039 | (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, |
| 6040 | info->keep_memory)); |
| 6041 | if (relstart == NULL) |
| 6042 | { |
| 6043 | ret = false; |
| 6044 | goto out1; |
| 6045 | } |
| 6046 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; |
| 6047 | |
| 6048 | for (rel = relstart; rel < relend; rel++) |
| 6049 | { |
| 6050 | unsigned long r_symndx; |
| 6051 | asection *rsec; |
| 6052 | struct elf_link_hash_entry *h; |
| 6053 | Elf_Internal_Sym s; |
| 6054 | |
| 6055 | r_symndx = ELF_R_SYM (rel->r_info); |
| 6056 | if (r_symndx == 0) |
| 6057 | continue; |
| 6058 | |
| 6059 | if (elf_bad_symtab (sec->owner)) |
| 6060 | { |
| 6061 | elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s); |
| 6062 | if (ELF_ST_BIND (s.st_info) == STB_LOCAL) |
| 6063 | rsec = (*gc_mark_hook)(sec->owner, info, rel, NULL, &s); |
| 6064 | else |
| 6065 | { |
| 6066 | h = sym_hashes[r_symndx - extsymoff]; |
| 6067 | rsec = (*gc_mark_hook)(sec->owner, info, rel, h, NULL); |
| 6068 | } |
| 6069 | } |
| 6070 | else if (r_symndx >= nlocsyms) |
| 6071 | { |
| 6072 | h = sym_hashes[r_symndx - extsymoff]; |
| 6073 | rsec = (*gc_mark_hook)(sec->owner, info, rel, h, NULL); |
| 6074 | } |
| 6075 | else |
| 6076 | { |
| 6077 | elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s); |
| 6078 | rsec = (*gc_mark_hook)(sec->owner, info, rel, NULL, &s); |
| 6079 | } |
| 6080 | |
| 6081 | if (rsec && !rsec->gc_mark) |
| 6082 | if (!elf_gc_mark (info, rsec, gc_mark_hook)) |
| 6083 | { |
| 6084 | ret = false; |
| 6085 | goto out2; |
| 6086 | } |
| 6087 | } |
| 6088 | |
| 6089 | out2: |
| 6090 | if (!info->keep_memory) |
| 6091 | free (relstart); |
| 6092 | out1: |
| 6093 | if (freesyms) |
| 6094 | free (freesyms); |
| 6095 | } |
| 6096 | |
| 6097 | return ret; |
| 6098 | } |
| 6099 | |
| 6100 | /* The sweep phase of garbage collection. Remove all garbage sections. */ |
| 6101 | |
| 6102 | static boolean |
| 6103 | elf_gc_sweep (info, gc_sweep_hook) |
| 6104 | struct bfd_link_info *info; |
| 6105 | boolean (*gc_sweep_hook) |
| 6106 | PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o, |
| 6107 | const Elf_Internal_Rela *relocs)); |
| 6108 | { |
| 6109 | bfd *sub; |
| 6110 | |
| 6111 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
| 6112 | { |
| 6113 | asection *o; |
| 6114 | |
| 6115 | for (o = sub->sections; o != NULL; o = o->next) |
| 6116 | { |
| 6117 | /* Keep special sections. Keep .debug sections. */ |
| 6118 | if ((o->flags & SEC_LINKER_CREATED) |
| 6119 | || (o->flags & SEC_DEBUGGING)) |
| 6120 | o->gc_mark = 1; |
| 6121 | |
| 6122 | if (o->gc_mark) |
| 6123 | continue; |
| 6124 | |
| 6125 | /* Skip sweeping sections already excluded. */ |
| 6126 | if (o->flags & SEC_EXCLUDE) |
| 6127 | continue; |
| 6128 | |
| 6129 | /* Since this is early in the link process, it is simple |
| 6130 | to remove a section from the output. */ |
| 6131 | o->flags |= SEC_EXCLUDE; |
| 6132 | |
| 6133 | /* But we also have to update some of the relocation |
| 6134 | info we collected before. */ |
| 6135 | if (gc_sweep_hook |
| 6136 | && (o->flags & SEC_RELOC) && o->reloc_count > 0) |
| 6137 | { |
| 6138 | Elf_Internal_Rela *internal_relocs; |
| 6139 | boolean r; |
| 6140 | |
| 6141 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) |
| 6142 | (o->owner, o, NULL, NULL, info->keep_memory)); |
| 6143 | if (internal_relocs == NULL) |
| 6144 | return false; |
| 6145 | |
| 6146 | r = (*gc_sweep_hook)(o->owner, info, o, internal_relocs); |
| 6147 | |
| 6148 | if (!info->keep_memory) |
| 6149 | free (internal_relocs); |
| 6150 | |
| 6151 | if (!r) |
| 6152 | return false; |
| 6153 | } |
| 6154 | } |
| 6155 | } |
| 6156 | |
| 6157 | /* Remove the symbols that were in the swept sections from the dynamic |
| 6158 | symbol table. GCFIXME: Anyone know how to get them out of the |
| 6159 | static symbol table as well? */ |
| 6160 | { |
| 6161 | int i = 0; |
| 6162 | |
| 6163 | elf_link_hash_traverse (elf_hash_table (info), |
| 6164 | elf_gc_sweep_symbol, |
| 6165 | (PTR) &i); |
| 6166 | |
| 6167 | elf_hash_table (info)->dynsymcount = i; |
| 6168 | } |
| 6169 | |
| 6170 | return true; |
| 6171 | } |
| 6172 | |
| 6173 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
| 6174 | |
| 6175 | static boolean |
| 6176 | elf_gc_sweep_symbol (h, idxptr) |
| 6177 | struct elf_link_hash_entry *h; |
| 6178 | PTR idxptr; |
| 6179 | { |
| 6180 | int *idx = (int *) idxptr; |
| 6181 | |
| 6182 | if (h->dynindx != -1 |
| 6183 | && ((h->root.type != bfd_link_hash_defined |
| 6184 | && h->root.type != bfd_link_hash_defweak) |
| 6185 | || h->root.u.def.section->gc_mark)) |
| 6186 | h->dynindx = (*idx)++; |
| 6187 | |
| 6188 | return true; |
| 6189 | } |
| 6190 | |
| 6191 | /* Propogate collected vtable information. This is called through |
| 6192 | elf_link_hash_traverse. */ |
| 6193 | |
| 6194 | static boolean |
| 6195 | elf_gc_propagate_vtable_entries_used (h, okp) |
| 6196 | struct elf_link_hash_entry *h; |
| 6197 | PTR okp; |
| 6198 | { |
| 6199 | /* Those that are not vtables. */ |
| 6200 | if (h->vtable_parent == NULL) |
| 6201 | return true; |
| 6202 | |
| 6203 | /* Those vtables that do not have parents, we cannot merge. */ |
| 6204 | if (h->vtable_parent == (struct elf_link_hash_entry *) -1) |
| 6205 | return true; |
| 6206 | |
| 6207 | /* If we've already been done, exit. */ |
| 6208 | if (h->vtable_entries_used && h->vtable_entries_used[-1]) |
| 6209 | return true; |
| 6210 | |
| 6211 | /* Make sure the parent's table is up to date. */ |
| 6212 | elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp); |
| 6213 | |
| 6214 | if (h->vtable_entries_used == NULL) |
| 6215 | { |
| 6216 | /* None of this table's entries were referenced. Re-use the |
| 6217 | parent's table. */ |
| 6218 | h->vtable_entries_used = h->vtable_parent->vtable_entries_used; |
| 6219 | h->vtable_entries_size = h->vtable_parent->vtable_entries_size; |
| 6220 | } |
| 6221 | else |
| 6222 | { |
| 6223 | size_t n; |
| 6224 | boolean *cu, *pu; |
| 6225 | |
| 6226 | /* Or the parent's entries into ours. */ |
| 6227 | cu = h->vtable_entries_used; |
| 6228 | cu[-1] = true; |
| 6229 | pu = h->vtable_parent->vtable_entries_used; |
| 6230 | if (pu != NULL) |
| 6231 | { |
| 6232 | n = h->vtable_parent->vtable_entries_size / FILE_ALIGN; |
| 6233 | while (--n != 0) |
| 6234 | { |
| 6235 | if (*pu) *cu = true; |
| 6236 | pu++, cu++; |
| 6237 | } |
| 6238 | } |
| 6239 | } |
| 6240 | |
| 6241 | return true; |
| 6242 | } |
| 6243 | |
| 6244 | static boolean |
| 6245 | elf_gc_smash_unused_vtentry_relocs (h, okp) |
| 6246 | struct elf_link_hash_entry *h; |
| 6247 | PTR okp; |
| 6248 | { |
| 6249 | asection *sec; |
| 6250 | bfd_vma hstart, hend; |
| 6251 | Elf_Internal_Rela *relstart, *relend, *rel; |
| 6252 | struct elf_backend_data *bed; |
| 6253 | |
| 6254 | /* Take care of both those symbols that do not describe vtables as |
| 6255 | well as those that are not loaded. */ |
| 6256 | if (h->vtable_parent == NULL) |
| 6257 | return true; |
| 6258 | |
| 6259 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
| 6260 | || h->root.type == bfd_link_hash_defweak); |
| 6261 | |
| 6262 | sec = h->root.u.def.section; |
| 6263 | hstart = h->root.u.def.value; |
| 6264 | hend = hstart + h->size; |
| 6265 | |
| 6266 | relstart = (NAME(_bfd_elf,link_read_relocs) |
| 6267 | (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true)); |
| 6268 | if (!relstart) |
| 6269 | return *(boolean *)okp = false; |
| 6270 | bed = get_elf_backend_data (sec->owner); |
| 6271 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; |
| 6272 | |
| 6273 | for (rel = relstart; rel < relend; ++rel) |
| 6274 | if (rel->r_offset >= hstart && rel->r_offset < hend) |
| 6275 | { |
| 6276 | /* If the entry is in use, do nothing. */ |
| 6277 | if (h->vtable_entries_used |
| 6278 | && (rel->r_offset - hstart) < h->vtable_entries_size) |
| 6279 | { |
| 6280 | bfd_vma entry = (rel->r_offset - hstart) / FILE_ALIGN; |
| 6281 | if (h->vtable_entries_used[entry]) |
| 6282 | continue; |
| 6283 | } |
| 6284 | /* Otherwise, kill it. */ |
| 6285 | rel->r_offset = rel->r_info = rel->r_addend = 0; |
| 6286 | } |
| 6287 | |
| 6288 | return true; |
| 6289 | } |
| 6290 | |
| 6291 | /* Do mark and sweep of unused sections. */ |
| 6292 | |
| 6293 | boolean |
| 6294 | elf_gc_sections (abfd, info) |
| 6295 | bfd *abfd; |
| 6296 | struct bfd_link_info *info; |
| 6297 | { |
| 6298 | boolean ok = true; |
| 6299 | bfd *sub; |
| 6300 | asection * (*gc_mark_hook) |
| 6301 | PARAMS ((bfd *abfd, struct bfd_link_info *, Elf_Internal_Rela *, |
| 6302 | struct elf_link_hash_entry *h, Elf_Internal_Sym *)); |
| 6303 | |
| 6304 | if (!get_elf_backend_data (abfd)->can_gc_sections |
| 6305 | || info->relocateable |
| 6306 | || elf_hash_table (info)->dynamic_sections_created) |
| 6307 | return true; |
| 6308 | |
| 6309 | /* Apply transitive closure to the vtable entry usage info. */ |
| 6310 | elf_link_hash_traverse (elf_hash_table (info), |
| 6311 | elf_gc_propagate_vtable_entries_used, |
| 6312 | (PTR) &ok); |
| 6313 | if (!ok) |
| 6314 | return false; |
| 6315 | |
| 6316 | /* Kill the vtable relocations that were not used. */ |
| 6317 | elf_link_hash_traverse (elf_hash_table (info), |
| 6318 | elf_gc_smash_unused_vtentry_relocs, |
| 6319 | (PTR) &ok); |
| 6320 | if (!ok) |
| 6321 | return false; |
| 6322 | |
| 6323 | /* Grovel through relocs to find out who stays ... */ |
| 6324 | |
| 6325 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; |
| 6326 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
| 6327 | { |
| 6328 | asection *o; |
| 6329 | for (o = sub->sections; o != NULL; o = o->next) |
| 6330 | { |
| 6331 | if (o->flags & SEC_KEEP) |
| 6332 | if (!elf_gc_mark (info, o, gc_mark_hook)) |
| 6333 | return false; |
| 6334 | } |
| 6335 | } |
| 6336 | |
| 6337 | /* ... and mark SEC_EXCLUDE for those that go. */ |
| 6338 | if (!elf_gc_sweep(info, get_elf_backend_data (abfd)->gc_sweep_hook)) |
| 6339 | return false; |
| 6340 | |
| 6341 | return true; |
| 6342 | } |
| 6343 | \f |
| 6344 | /* Called from check_relocs to record the existance of a VTINHERIT reloc. */ |
| 6345 | |
| 6346 | boolean |
| 6347 | elf_gc_record_vtinherit (abfd, sec, h, offset) |
| 6348 | bfd *abfd; |
| 6349 | asection *sec; |
| 6350 | struct elf_link_hash_entry *h; |
| 6351 | bfd_vma offset; |
| 6352 | { |
| 6353 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; |
| 6354 | struct elf_link_hash_entry **search, *child; |
| 6355 | bfd_size_type extsymcount; |
| 6356 | |
| 6357 | /* The sh_info field of the symtab header tells us where the |
| 6358 | external symbols start. We don't care about the local symbols at |
| 6359 | this point. */ |
| 6360 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym); |
| 6361 | if (!elf_bad_symtab (abfd)) |
| 6362 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; |
| 6363 | |
| 6364 | sym_hashes = elf_sym_hashes (abfd); |
| 6365 | sym_hashes_end = sym_hashes + extsymcount; |
| 6366 | |
| 6367 | /* Hunt down the child symbol, which is in this section at the same |
| 6368 | offset as the relocation. */ |
| 6369 | for (search = sym_hashes; search != sym_hashes_end; ++search) |
| 6370 | { |
| 6371 | if ((child = *search) != NULL |
| 6372 | && (child->root.type == bfd_link_hash_defined |
| 6373 | || child->root.type == bfd_link_hash_defweak) |
| 6374 | && child->root.u.def.section == sec |
| 6375 | && child->root.u.def.value == offset) |
| 6376 | goto win; |
| 6377 | } |
| 6378 | |
| 6379 | (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT", |
| 6380 | bfd_get_filename (abfd), sec->name, |
| 6381 | (unsigned long)offset); |
| 6382 | bfd_set_error (bfd_error_invalid_operation); |
| 6383 | return false; |
| 6384 | |
| 6385 | win: |
| 6386 | if (!h) |
| 6387 | { |
| 6388 | /* This *should* only be the absolute section. It could potentially |
| 6389 | be that someone has defined a non-global vtable though, which |
| 6390 | would be bad. It isn't worth paging in the local symbols to be |
| 6391 | sure though; that case should simply be handled by the assembler. */ |
| 6392 | |
| 6393 | child->vtable_parent = (struct elf_link_hash_entry *) -1; |
| 6394 | } |
| 6395 | else |
| 6396 | child->vtable_parent = h; |
| 6397 | |
| 6398 | return true; |
| 6399 | } |
| 6400 | |
| 6401 | /* Called from check_relocs to record the existance of a VTENTRY reloc. */ |
| 6402 | |
| 6403 | boolean |
| 6404 | elf_gc_record_vtentry (abfd, sec, h, addend) |
| 6405 | bfd *abfd ATTRIBUTE_UNUSED; |
| 6406 | asection *sec ATTRIBUTE_UNUSED; |
| 6407 | struct elf_link_hash_entry *h; |
| 6408 | bfd_vma addend; |
| 6409 | { |
| 6410 | if (addend >= h->vtable_entries_size) |
| 6411 | { |
| 6412 | size_t size, bytes; |
| 6413 | boolean *ptr = h->vtable_entries_used; |
| 6414 | |
| 6415 | /* While the symbol is undefined, we have to be prepared to handle |
| 6416 | a zero size. */ |
| 6417 | if (h->root.type == bfd_link_hash_undefined) |
| 6418 | size = addend; |
| 6419 | else |
| 6420 | { |
| 6421 | size = h->size; |
| 6422 | if (size < addend) |
| 6423 | { |
| 6424 | /* Oops! We've got a reference past the defined end of |
| 6425 | the table. This is probably a bug -- shall we warn? */ |
| 6426 | size = addend; |
| 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | /* Allocate one extra entry for use as a "done" flag for the |
| 6431 | consolidation pass. */ |
| 6432 | bytes = (size / FILE_ALIGN + 1) * sizeof (boolean); |
| 6433 | |
| 6434 | if (ptr) |
| 6435 | { |
| 6436 | ptr = bfd_realloc (ptr - 1, bytes); |
| 6437 | |
| 6438 | if (ptr != NULL) |
| 6439 | { |
| 6440 | size_t oldbytes; |
| 6441 | |
| 6442 | oldbytes = (h->vtable_entries_size/FILE_ALIGN + 1) * sizeof (boolean); |
| 6443 | memset (((char *)ptr) + oldbytes, 0, bytes - oldbytes); |
| 6444 | } |
| 6445 | } |
| 6446 | else |
| 6447 | ptr = bfd_zmalloc (bytes); |
| 6448 | |
| 6449 | if (ptr == NULL) |
| 6450 | return false; |
| 6451 | |
| 6452 | /* And arrange for that done flag to be at index -1. */ |
| 6453 | h->vtable_entries_used = ptr + 1; |
| 6454 | h->vtable_entries_size = size; |
| 6455 | } |
| 6456 | |
| 6457 | h->vtable_entries_used[addend / FILE_ALIGN] = true; |
| 6458 | |
| 6459 | return true; |
| 6460 | } |
| 6461 | |
| 6462 | /* And an accompanying bit to work out final got entry offsets once |
| 6463 | we're done. Should be called from final_link. */ |
| 6464 | |
| 6465 | boolean |
| 6466 | elf_gc_common_finalize_got_offsets (abfd, info) |
| 6467 | bfd *abfd; |
| 6468 | struct bfd_link_info *info; |
| 6469 | { |
| 6470 | bfd *i; |
| 6471 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
| 6472 | bfd_vma gotoff; |
| 6473 | |
| 6474 | /* The GOT offset is relative to the .got section, but the GOT header is |
| 6475 | put into the .got.plt section, if the backend uses it. */ |
| 6476 | if (bed->want_got_plt) |
| 6477 | gotoff = 0; |
| 6478 | else |
| 6479 | gotoff = bed->got_header_size; |
| 6480 | |
| 6481 | /* Do the local .got entries first. */ |
| 6482 | for (i = info->input_bfds; i; i = i->link_next) |
| 6483 | { |
| 6484 | bfd_signed_vma *local_got = elf_local_got_refcounts (i); |
| 6485 | bfd_size_type j, locsymcount; |
| 6486 | Elf_Internal_Shdr *symtab_hdr; |
| 6487 | |
| 6488 | if (!local_got) |
| 6489 | continue; |
| 6490 | |
| 6491 | symtab_hdr = &elf_tdata (i)->symtab_hdr; |
| 6492 | if (elf_bad_symtab (i)) |
| 6493 | locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym); |
| 6494 | else |
| 6495 | locsymcount = symtab_hdr->sh_info; |
| 6496 | |
| 6497 | for (j = 0; j < locsymcount; ++j) |
| 6498 | { |
| 6499 | if (local_got[j] > 0) |
| 6500 | { |
| 6501 | local_got[j] = gotoff; |
| 6502 | gotoff += ARCH_SIZE / 8; |
| 6503 | } |
| 6504 | else |
| 6505 | local_got[j] = (bfd_vma) -1; |
| 6506 | } |
| 6507 | } |
| 6508 | |
| 6509 | /* Then the global .got and .plt entries. */ |
| 6510 | elf_link_hash_traverse (elf_hash_table (info), |
| 6511 | elf_gc_allocate_got_offsets, |
| 6512 | (PTR) &gotoff); |
| 6513 | return true; |
| 6514 | } |
| 6515 | |
| 6516 | /* We need a special top-level link routine to convert got reference counts |
| 6517 | to real got offsets. */ |
| 6518 | |
| 6519 | static boolean |
| 6520 | elf_gc_allocate_got_offsets (h, offarg) |
| 6521 | struct elf_link_hash_entry *h; |
| 6522 | PTR offarg; |
| 6523 | { |
| 6524 | bfd_vma *off = (bfd_vma *) offarg; |
| 6525 | |
| 6526 | if (h->got.refcount > 0) |
| 6527 | { |
| 6528 | h->got.offset = off[0]; |
| 6529 | off[0] += ARCH_SIZE / 8; |
| 6530 | } |
| 6531 | else |
| 6532 | h->got.offset = (bfd_vma) -1; |
| 6533 | |
| 6534 | return true; |
| 6535 | } |
| 6536 | |
| 6537 | /* Many folk need no more in the way of final link than this, once |
| 6538 | got entry reference counting is enabled. */ |
| 6539 | |
| 6540 | boolean |
| 6541 | elf_gc_common_final_link (abfd, info) |
| 6542 | bfd *abfd; |
| 6543 | struct bfd_link_info *info; |
| 6544 | { |
| 6545 | if (!elf_gc_common_finalize_got_offsets (abfd, info)) |
| 6546 | return false; |
| 6547 | |
| 6548 | /* Invoke the regular ELF backend linker to do all the work. */ |
| 6549 | return elf_bfd_final_link (abfd, info); |
| 6550 | } |
| 6551 | |
| 6552 | /* This function will be called though elf_link_hash_traverse to store |
| 6553 | all hash value of the exported symbols in an array. */ |
| 6554 | |
| 6555 | static boolean |
| 6556 | elf_collect_hash_codes (h, data) |
| 6557 | struct elf_link_hash_entry *h; |
| 6558 | PTR data; |
| 6559 | { |
| 6560 | unsigned long **valuep = (unsigned long **) data; |
| 6561 | const char *name; |
| 6562 | char *p; |
| 6563 | unsigned long ha; |
| 6564 | char *alc = NULL; |
| 6565 | |
| 6566 | /* Ignore indirect symbols. These are added by the versioning code. */ |
| 6567 | if (h->dynindx == -1) |
| 6568 | return true; |
| 6569 | |
| 6570 | name = h->root.root.string; |
| 6571 | p = strchr (name, ELF_VER_CHR); |
| 6572 | if (p != NULL) |
| 6573 | { |
| 6574 | alc = bfd_malloc (p - name + 1); |
| 6575 | memcpy (alc, name, p - name); |
| 6576 | alc[p - name] = '\0'; |
| 6577 | name = alc; |
| 6578 | } |
| 6579 | |
| 6580 | /* Compute the hash value. */ |
| 6581 | ha = bfd_elf_hash (name); |
| 6582 | |
| 6583 | /* Store the found hash value in the array given as the argument. */ |
| 6584 | *(*valuep)++ = ha; |
| 6585 | |
| 6586 | /* And store it in the struct so that we can put it in the hash table |
| 6587 | later. */ |
| 6588 | h->elf_hash_value = ha; |
| 6589 | |
| 6590 | if (alc != NULL) |
| 6591 | free (alc); |
| 6592 | |
| 6593 | return true; |
| 6594 | } |