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