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