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