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