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 | ||
23 | /* This struct is used to pass information to routines called via | |
24 | elf_link_hash_traverse which must return failure. */ | |
25 | ||
26 | struct elf_info_failed | |
27 | { | |
b34976b6 | 28 | bfd_boolean failed; |
252b5132 | 29 | struct bfd_link_info *info; |
bc2b6df7 | 30 | struct bfd_elf_version_tree *verdefs; |
252b5132 RH |
31 | }; |
32 | ||
b34976b6 | 33 | static bfd_boolean is_global_data_symbol_definition |
a7b97311 | 34 | PARAMS ((bfd *, Elf_Internal_Sym *)); |
b34976b6 | 35 | static bfd_boolean elf_link_is_defined_archive_symbol |
a7b97311 | 36 | PARAMS ((bfd *, carsym *)); |
b34976b6 | 37 | static bfd_boolean elf_link_add_object_symbols |
252b5132 | 38 | PARAMS ((bfd *, struct bfd_link_info *)); |
b34976b6 | 39 | static bfd_boolean elf_link_add_archive_symbols |
252b5132 | 40 | PARAMS ((bfd *, struct bfd_link_info *)); |
b34976b6 | 41 | static bfd_boolean elf_merge_symbol |
215007a6 L |
42 | PARAMS ((bfd *, struct bfd_link_info *, const char *, |
43 | Elf_Internal_Sym *, asection **, bfd_vma *, | |
b34976b6 | 44 | struct elf_link_hash_entry **, bfd_boolean *, bfd_boolean *, |
1b1fe8fe | 45 | bfd_boolean *, bfd_boolean *, bfd_boolean)); |
b34976b6 | 46 | static bfd_boolean elf_add_default_symbol |
215007a6 L |
47 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, |
48 | const char *, Elf_Internal_Sym *, asection **, bfd_vma *, | |
b34976b6 AM |
49 | bfd_boolean *, bfd_boolean, bfd_boolean)); |
50 | static bfd_boolean elf_export_symbol | |
252b5132 | 51 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 52 | static bfd_boolean elf_finalize_dynstr |
2b0f7ef9 | 53 | PARAMS ((bfd *, struct bfd_link_info *)); |
b34976b6 | 54 | static bfd_boolean elf_fix_symbol_flags |
252b5132 | 55 | PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *)); |
b34976b6 | 56 | static bfd_boolean elf_adjust_dynamic_symbol |
252b5132 | 57 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 58 | static bfd_boolean elf_link_find_version_dependencies |
252b5132 | 59 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 60 | static bfd_boolean elf_link_assign_sym_version |
252b5132 | 61 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 62 | static bfd_boolean elf_collect_hash_codes |
252b5132 | 63 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 64 | static bfd_boolean elf_link_read_relocs_from_section |
6b5bd373 | 65 | PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *)); |
a7b97311 AM |
66 | static size_t compute_bucket_count |
67 | PARAMS ((struct bfd_link_info *)); | |
b34976b6 | 68 | static bfd_boolean elf_link_output_relocs |
23bc299b | 69 | PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *)); |
b34976b6 | 70 | static bfd_boolean elf_link_size_reloc_section |
23bc299b | 71 | PARAMS ((bfd *, Elf_Internal_Shdr *, asection *)); |
3e932841 KH |
72 | static void elf_link_adjust_relocs |
73 | PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int, | |
31367b81 | 74 | struct elf_link_hash_entry **)); |
db6751f2 JJ |
75 | static int elf_link_sort_cmp1 |
76 | PARAMS ((const void *, const void *)); | |
77 | static int elf_link_sort_cmp2 | |
78 | PARAMS ((const void *, const void *)); | |
79 | static size_t elf_link_sort_relocs | |
80 | PARAMS ((bfd *, struct bfd_link_info *, asection **)); | |
b34976b6 | 81 | static bfd_boolean elf_section_ignore_discarded_relocs |
73d074b4 | 82 | PARAMS ((asection *)); |
252b5132 RH |
83 | |
84 | /* Given an ELF BFD, add symbols to the global hash table as | |
85 | appropriate. */ | |
86 | ||
b34976b6 | 87 | bfd_boolean |
252b5132 RH |
88 | elf_bfd_link_add_symbols (abfd, info) |
89 | bfd *abfd; | |
90 | struct bfd_link_info *info; | |
91 | { | |
92 | switch (bfd_get_format (abfd)) | |
93 | { | |
94 | case bfd_object: | |
95 | return elf_link_add_object_symbols (abfd, info); | |
96 | case bfd_archive: | |
97 | return elf_link_add_archive_symbols (abfd, info); | |
98 | default: | |
99 | bfd_set_error (bfd_error_wrong_format); | |
b34976b6 | 100 | return FALSE; |
252b5132 RH |
101 | } |
102 | } | |
103 | \f | |
b34976b6 AM |
104 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ |
105 | static bfd_boolean | |
7da9d88f | 106 | is_global_data_symbol_definition (abfd, sym) |
86033394 | 107 | bfd * abfd ATTRIBUTE_UNUSED; |
48dfb430 NC |
108 | Elf_Internal_Sym * sym; |
109 | { | |
110 | /* Local symbols do not count, but target specific ones might. */ | |
111 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
112 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
b34976b6 | 113 | return FALSE; |
48dfb430 | 114 | |
7da9d88f NC |
115 | /* Function symbols do not count. */ |
116 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
b34976b6 | 117 | return FALSE; |
7da9d88f | 118 | |
48dfb430 NC |
119 | /* If the section is undefined, then so is the symbol. */ |
120 | if (sym->st_shndx == SHN_UNDEF) | |
b34976b6 | 121 | return FALSE; |
3e932841 | 122 | |
48dfb430 NC |
123 | /* If the symbol is defined in the common section, then |
124 | it is a common definition and so does not count. */ | |
125 | if (sym->st_shndx == SHN_COMMON) | |
b34976b6 | 126 | return FALSE; |
48dfb430 NC |
127 | |
128 | /* If the symbol is in a target specific section then we | |
129 | must rely upon the backend to tell us what it is. */ | |
130 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
131 | /* FIXME - this function is not coded yet: | |
3e932841 | 132 | |
48dfb430 | 133 | return _bfd_is_global_symbol_definition (abfd, sym); |
3e932841 | 134 | |
48dfb430 NC |
135 | Instead for now assume that the definition is not global, |
136 | Even if this is wrong, at least the linker will behave | |
137 | in the same way that it used to do. */ | |
b34976b6 | 138 | return FALSE; |
3e932841 | 139 | |
b34976b6 | 140 | return TRUE; |
48dfb430 NC |
141 | } |
142 | ||
a3a8c91d | 143 | /* Search the symbol table of the archive element of the archive ABFD |
4e8a9624 | 144 | whose archive map contains a mention of SYMDEF, and determine if |
a3a8c91d | 145 | the symbol is defined in this element. */ |
b34976b6 | 146 | static bfd_boolean |
a3a8c91d NC |
147 | elf_link_is_defined_archive_symbol (abfd, symdef) |
148 | bfd * abfd; | |
149 | carsym * symdef; | |
150 | { | |
151 | Elf_Internal_Shdr * hdr; | |
dc810e39 AM |
152 | bfd_size_type symcount; |
153 | bfd_size_type extsymcount; | |
154 | bfd_size_type extsymoff; | |
6cdc0ccc AM |
155 | Elf_Internal_Sym *isymbuf; |
156 | Elf_Internal_Sym *isym; | |
157 | Elf_Internal_Sym *isymend; | |
b34976b6 | 158 | bfd_boolean result; |
3e932841 | 159 | |
a3a8c91d NC |
160 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); |
161 | if (abfd == (bfd *) NULL) | |
b34976b6 | 162 | return FALSE; |
a3a8c91d NC |
163 | |
164 | if (! bfd_check_format (abfd, bfd_object)) | |
b34976b6 | 165 | return FALSE; |
a3a8c91d | 166 | |
48dfb430 NC |
167 | /* If we have already included the element containing this symbol in the |
168 | link then we do not need to include it again. Just claim that any symbol | |
169 | it contains is not a definition, so that our caller will not decide to | |
170 | (re)include this element. */ | |
171 | if (abfd->archive_pass) | |
b34976b6 | 172 | return FALSE; |
3e932841 | 173 | |
a3a8c91d NC |
174 | /* Select the appropriate symbol table. */ |
175 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
6cdc0ccc | 176 | hdr = &elf_tdata (abfd)->symtab_hdr; |
a3a8c91d | 177 | else |
6cdc0ccc | 178 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; |
a3a8c91d NC |
179 | |
180 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); | |
181 | ||
182 | /* The sh_info field of the symtab header tells us where the | |
183 | external symbols start. We don't care about the local symbols. */ | |
184 | if (elf_bad_symtab (abfd)) | |
185 | { | |
186 | extsymcount = symcount; | |
187 | extsymoff = 0; | |
188 | } | |
189 | else | |
190 | { | |
191 | extsymcount = symcount - hdr->sh_info; | |
192 | extsymoff = hdr->sh_info; | |
193 | } | |
194 | ||
6cdc0ccc | 195 | if (extsymcount == 0) |
b34976b6 | 196 | return FALSE; |
a3a8c91d | 197 | |
6cdc0ccc AM |
198 | /* Read in the symbol table. */ |
199 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
200 | NULL, NULL, NULL); | |
201 | if (isymbuf == NULL) | |
b34976b6 | 202 | return FALSE; |
a3a8c91d NC |
203 | |
204 | /* Scan the symbol table looking for SYMDEF. */ | |
b34976b6 | 205 | result = FALSE; |
6cdc0ccc | 206 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) |
a3a8c91d | 207 | { |
6cdc0ccc | 208 | const char *name; |
a3a8c91d | 209 | |
6cdc0ccc AM |
210 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, |
211 | isym->st_name); | |
a3a8c91d NC |
212 | if (name == (const char *) NULL) |
213 | break; | |
214 | ||
215 | if (strcmp (name, symdef->name) == 0) | |
216 | { | |
6cdc0ccc | 217 | result = is_global_data_symbol_definition (abfd, isym); |
a3a8c91d NC |
218 | break; |
219 | } | |
220 | } | |
221 | ||
6cdc0ccc | 222 | free (isymbuf); |
3e932841 | 223 | |
a3a8c91d NC |
224 | return result; |
225 | } | |
226 | \f | |
252b5132 RH |
227 | /* Add symbols from an ELF archive file to the linker hash table. We |
228 | don't use _bfd_generic_link_add_archive_symbols because of a | |
229 | problem which arises on UnixWare. The UnixWare libc.so is an | |
230 | archive which includes an entry libc.so.1 which defines a bunch of | |
231 | symbols. The libc.so archive also includes a number of other | |
232 | object files, which also define symbols, some of which are the same | |
233 | as those defined in libc.so.1. Correct linking requires that we | |
234 | consider each object file in turn, and include it if it defines any | |
235 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
236 | this; it looks through the list of undefined symbols, and includes | |
237 | any object file which defines them. When this algorithm is used on | |
238 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
239 | bunch of symbols. This means that some of the other objects in the | |
240 | archive are not included in the link, which is incorrect since they | |
241 | precede libc.so.1 in the archive. | |
242 | ||
243 | Fortunately, ELF archive handling is simpler than that done by | |
244 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
245 | oddities. In ELF, if we find a symbol in the archive map, and the | |
246 | symbol is currently undefined, we know that we must pull in that | |
247 | object file. | |
248 | ||
249 | Unfortunately, we do have to make multiple passes over the symbol | |
250 | table until nothing further is resolved. */ | |
251 | ||
b34976b6 | 252 | static bfd_boolean |
252b5132 RH |
253 | elf_link_add_archive_symbols (abfd, info) |
254 | bfd *abfd; | |
255 | struct bfd_link_info *info; | |
256 | { | |
257 | symindex c; | |
b34976b6 AM |
258 | bfd_boolean *defined = NULL; |
259 | bfd_boolean *included = NULL; | |
252b5132 | 260 | carsym *symdefs; |
b34976b6 | 261 | bfd_boolean loop; |
dc810e39 | 262 | bfd_size_type amt; |
252b5132 RH |
263 | |
264 | if (! bfd_has_map (abfd)) | |
265 | { | |
266 | /* An empty archive is a special case. */ | |
267 | if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL) | |
b34976b6 | 268 | return TRUE; |
252b5132 | 269 | bfd_set_error (bfd_error_no_armap); |
b34976b6 | 270 | return FALSE; |
252b5132 RH |
271 | } |
272 | ||
273 | /* Keep track of all symbols we know to be already defined, and all | |
274 | files we know to be already included. This is to speed up the | |
275 | second and subsequent passes. */ | |
276 | c = bfd_ardata (abfd)->symdef_count; | |
277 | if (c == 0) | |
b34976b6 | 278 | return TRUE; |
dc810e39 | 279 | amt = c; |
b34976b6 AM |
280 | amt *= sizeof (bfd_boolean); |
281 | defined = (bfd_boolean *) bfd_zmalloc (amt); | |
282 | included = (bfd_boolean *) bfd_zmalloc (amt); | |
283 | if (defined == (bfd_boolean *) NULL || included == (bfd_boolean *) NULL) | |
252b5132 | 284 | goto error_return; |
252b5132 RH |
285 | |
286 | symdefs = bfd_ardata (abfd)->symdefs; | |
287 | ||
288 | do | |
289 | { | |
290 | file_ptr last; | |
291 | symindex i; | |
292 | carsym *symdef; | |
293 | carsym *symdefend; | |
294 | ||
b34976b6 | 295 | loop = FALSE; |
252b5132 RH |
296 | last = -1; |
297 | ||
298 | symdef = symdefs; | |
299 | symdefend = symdef + c; | |
300 | for (i = 0; symdef < symdefend; symdef++, i++) | |
301 | { | |
302 | struct elf_link_hash_entry *h; | |
303 | bfd *element; | |
304 | struct bfd_link_hash_entry *undefs_tail; | |
305 | symindex mark; | |
306 | ||
307 | if (defined[i] || included[i]) | |
308 | continue; | |
309 | if (symdef->file_offset == last) | |
310 | { | |
b34976b6 | 311 | included[i] = TRUE; |
252b5132 RH |
312 | continue; |
313 | } | |
314 | ||
315 | h = elf_link_hash_lookup (elf_hash_table (info), symdef->name, | |
b34976b6 | 316 | FALSE, FALSE, FALSE); |
252b5132 RH |
317 | |
318 | if (h == NULL) | |
319 | { | |
320 | char *p, *copy; | |
48fc70a2 | 321 | size_t len, first; |
252b5132 RH |
322 | |
323 | /* If this is a default version (the name contains @@), | |
48fc70a2 AM |
324 | look up the symbol again with only one `@' as well |
325 | as without the version. The effect is that references | |
326 | to the symbol with and without the version will be | |
327 | matched by the default symbol in the archive. */ | |
252b5132 RH |
328 | |
329 | p = strchr (symdef->name, ELF_VER_CHR); | |
330 | if (p == NULL || p[1] != ELF_VER_CHR) | |
331 | continue; | |
332 | ||
48fc70a2 AM |
333 | /* First check with only one `@'. */ |
334 | len = strlen (symdef->name); | |
335 | copy = bfd_alloc (abfd, (bfd_size_type) len); | |
252b5132 RH |
336 | if (copy == NULL) |
337 | goto error_return; | |
48fc70a2 AM |
338 | first = p - symdef->name + 1; |
339 | memcpy (copy, symdef->name, first); | |
340 | memcpy (copy + first, symdef->name + first + 1, len - first); | |
252b5132 RH |
341 | |
342 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
b34976b6 | 343 | FALSE, FALSE, FALSE); |
252b5132 | 344 | |
48fc70a2 | 345 | if (h == NULL) |
58821868 | 346 | { |
48fc70a2 AM |
347 | /* We also need to check references to the symbol |
348 | without the version. */ | |
349 | ||
350 | copy[first - 1] = '\0'; | |
351 | h = elf_link_hash_lookup (elf_hash_table (info), | |
b34976b6 | 352 | copy, FALSE, FALSE, FALSE); |
48fc70a2 AM |
353 | } |
354 | ||
252b5132 RH |
355 | bfd_release (abfd, copy); |
356 | } | |
357 | ||
358 | if (h == NULL) | |
359 | continue; | |
360 | ||
a3a8c91d NC |
361 | if (h->root.type == bfd_link_hash_common) |
362 | { | |
363 | /* We currently have a common symbol. The archive map contains | |
364 | a reference to this symbol, so we may want to include it. We | |
365 | only want to include it however, if this archive element | |
366 | contains a definition of the symbol, not just another common | |
367 | declaration of it. | |
368 | ||
369 | Unfortunately some archivers (including GNU ar) will put | |
370 | declarations of common symbols into their archive maps, as | |
371 | well as real definitions, so we cannot just go by the archive | |
372 | map alone. Instead we must read in the element's symbol | |
373 | table and check that to see what kind of symbol definition | |
374 | this is. */ | |
375 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
376 | continue; | |
377 | } | |
378 | else if (h->root.type != bfd_link_hash_undefined) | |
252b5132 RH |
379 | { |
380 | if (h->root.type != bfd_link_hash_undefweak) | |
b34976b6 | 381 | defined[i] = TRUE; |
252b5132 RH |
382 | continue; |
383 | } | |
384 | ||
385 | /* We need to include this archive member. */ | |
252b5132 RH |
386 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); |
387 | if (element == (bfd *) NULL) | |
388 | goto error_return; | |
389 | ||
390 | if (! bfd_check_format (element, bfd_object)) | |
391 | goto error_return; | |
392 | ||
393 | /* Doublecheck that we have not included this object | |
394 | already--it should be impossible, but there may be | |
395 | something wrong with the archive. */ | |
396 | if (element->archive_pass != 0) | |
397 | { | |
398 | bfd_set_error (bfd_error_bad_value); | |
399 | goto error_return; | |
400 | } | |
401 | element->archive_pass = 1; | |
402 | ||
403 | undefs_tail = info->hash->undefs_tail; | |
404 | ||
405 | if (! (*info->callbacks->add_archive_element) (info, element, | |
406 | symdef->name)) | |
407 | goto error_return; | |
408 | if (! elf_link_add_object_symbols (element, info)) | |
409 | goto error_return; | |
410 | ||
411 | /* If there are any new undefined symbols, we need to make | |
412 | another pass through the archive in order to see whether | |
413 | they can be defined. FIXME: This isn't perfect, because | |
414 | common symbols wind up on undefs_tail and because an | |
415 | undefined symbol which is defined later on in this pass | |
416 | does not require another pass. This isn't a bug, but it | |
417 | does make the code less efficient than it could be. */ | |
418 | if (undefs_tail != info->hash->undefs_tail) | |
b34976b6 | 419 | loop = TRUE; |
252b5132 RH |
420 | |
421 | /* Look backward to mark all symbols from this object file | |
422 | which we have already seen in this pass. */ | |
423 | mark = i; | |
424 | do | |
425 | { | |
b34976b6 | 426 | included[mark] = TRUE; |
252b5132 RH |
427 | if (mark == 0) |
428 | break; | |
429 | --mark; | |
430 | } | |
431 | while (symdefs[mark].file_offset == symdef->file_offset); | |
432 | ||
433 | /* We mark subsequent symbols from this object file as we go | |
434 | on through the loop. */ | |
435 | last = symdef->file_offset; | |
436 | } | |
437 | } | |
438 | while (loop); | |
439 | ||
440 | free (defined); | |
441 | free (included); | |
442 | ||
b34976b6 | 443 | return TRUE; |
252b5132 RH |
444 | |
445 | error_return: | |
b34976b6 | 446 | if (defined != (bfd_boolean *) NULL) |
252b5132 | 447 | free (defined); |
b34976b6 | 448 | if (included != (bfd_boolean *) NULL) |
252b5132 | 449 | free (included); |
b34976b6 | 450 | return FALSE; |
252b5132 RH |
451 | } |
452 | ||
453 | /* This function is called when we want to define a new symbol. It | |
454 | handles the various cases which arise when we find a definition in | |
455 | a dynamic object, or when there is already a definition in a | |
456 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
457 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
458 | OVERRIDE if the old symbol is overriding a new definition. We set | |
459 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
460 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
461 | change, we mean that we shouldn't warn if the type or size does | |
456981d7 L |
462 | change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of |
463 | a shared object. */ | |
252b5132 | 464 | |
b34976b6 | 465 | static bfd_boolean |
1b1fe8fe | 466 | elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash, skip, |
456981d7 | 467 | override, type_change_ok, size_change_ok, dt_needed) |
252b5132 RH |
468 | bfd *abfd; |
469 | struct bfd_link_info *info; | |
470 | const char *name; | |
471 | Elf_Internal_Sym *sym; | |
472 | asection **psec; | |
473 | bfd_vma *pvalue; | |
474 | struct elf_link_hash_entry **sym_hash; | |
1b1fe8fe | 475 | bfd_boolean *skip; |
b34976b6 AM |
476 | bfd_boolean *override; |
477 | bfd_boolean *type_change_ok; | |
478 | bfd_boolean *size_change_ok; | |
479 | bfd_boolean dt_needed; | |
252b5132 RH |
480 | { |
481 | asection *sec; | |
482 | struct elf_link_hash_entry *h; | |
3c0a515d | 483 | struct elf_link_hash_entry *flip; |
252b5132 RH |
484 | int bind; |
485 | bfd *oldbfd; | |
b34976b6 | 486 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; |
252b5132 | 487 | |
1b1fe8fe | 488 | *skip = FALSE; |
b34976b6 | 489 | *override = FALSE; |
252b5132 RH |
490 | |
491 | sec = *psec; | |
492 | bind = ELF_ST_BIND (sym->st_info); | |
493 | ||
494 | if (! bfd_is_und_section (sec)) | |
b34976b6 | 495 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); |
252b5132 RH |
496 | else |
497 | h = ((struct elf_link_hash_entry *) | |
b34976b6 | 498 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); |
252b5132 | 499 | if (h == NULL) |
b34976b6 | 500 | return FALSE; |
252b5132 RH |
501 | *sym_hash = h; |
502 | ||
503 | /* This code is for coping with dynamic objects, and is only useful | |
504 | if we are doing an ELF link. */ | |
505 | if (info->hash->creator != abfd->xvec) | |
b34976b6 | 506 | return TRUE; |
252b5132 RH |
507 | |
508 | /* For merging, we only care about real symbols. */ | |
509 | ||
510 | while (h->root.type == bfd_link_hash_indirect | |
511 | || h->root.type == bfd_link_hash_warning) | |
512 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
513 | ||
514 | /* If we just created the symbol, mark it as being an ELF symbol. | |
515 | Other than that, there is nothing to do--there is no merge issue | |
516 | with a newly defined symbol--so we just return. */ | |
517 | ||
518 | if (h->root.type == bfd_link_hash_new) | |
519 | { | |
520 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; | |
b34976b6 | 521 | return TRUE; |
252b5132 RH |
522 | } |
523 | ||
524 | /* OLDBFD is a BFD associated with the existing symbol. */ | |
525 | ||
526 | switch (h->root.type) | |
527 | { | |
528 | default: | |
529 | oldbfd = NULL; | |
530 | break; | |
531 | ||
532 | case bfd_link_hash_undefined: | |
533 | case bfd_link_hash_undefweak: | |
534 | oldbfd = h->root.u.undef.abfd; | |
535 | break; | |
536 | ||
537 | case bfd_link_hash_defined: | |
538 | case bfd_link_hash_defweak: | |
539 | oldbfd = h->root.u.def.section->owner; | |
540 | break; | |
541 | ||
542 | case bfd_link_hash_common: | |
543 | oldbfd = h->root.u.c.p->section->owner; | |
544 | break; | |
545 | } | |
546 | ||
b4536acd ILT |
547 | /* In cases involving weak versioned symbols, we may wind up trying |
548 | to merge a symbol with itself. Catch that here, to avoid the | |
549 | confusion that results if we try to override a symbol with | |
accc7f69 ILT |
550 | itself. The additional tests catch cases like |
551 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
552 | dynamic object, which we do want to handle here. */ | |
553 | if (abfd == oldbfd | |
554 | && ((abfd->flags & DYNAMIC) == 0 | |
555 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
b34976b6 | 556 | return TRUE; |
b4536acd | 557 | |
252b5132 RH |
558 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
559 | respectively, is from a dynamic object. */ | |
560 | ||
561 | if ((abfd->flags & DYNAMIC) != 0) | |
b34976b6 | 562 | newdyn = TRUE; |
252b5132 | 563 | else |
b34976b6 | 564 | newdyn = FALSE; |
252b5132 | 565 | |
0035bd7b ILT |
566 | if (oldbfd != NULL) |
567 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
252b5132 | 568 | else |
0035bd7b ILT |
569 | { |
570 | asection *hsec; | |
571 | ||
572 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
c44233aa | 573 | indices used by MIPS ELF. */ |
0035bd7b ILT |
574 | switch (h->root.type) |
575 | { | |
576 | default: | |
577 | hsec = NULL; | |
578 | break; | |
579 | ||
580 | case bfd_link_hash_defined: | |
581 | case bfd_link_hash_defweak: | |
582 | hsec = h->root.u.def.section; | |
583 | break; | |
584 | ||
585 | case bfd_link_hash_common: | |
586 | hsec = h->root.u.c.p->section; | |
587 | break; | |
588 | } | |
589 | ||
590 | if (hsec == NULL) | |
b34976b6 | 591 | olddyn = FALSE; |
0035bd7b ILT |
592 | else |
593 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
594 | } | |
252b5132 RH |
595 | |
596 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, | |
597 | respectively, appear to be a definition rather than reference. */ | |
598 | ||
599 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
b34976b6 | 600 | newdef = FALSE; |
252b5132 | 601 | else |
b34976b6 | 602 | newdef = TRUE; |
252b5132 RH |
603 | |
604 | if (h->root.type == bfd_link_hash_undefined | |
605 | || h->root.type == bfd_link_hash_undefweak | |
606 | || h->root.type == bfd_link_hash_common) | |
b34976b6 | 607 | olddef = FALSE; |
252b5132 | 608 | else |
b34976b6 | 609 | olddef = TRUE; |
252b5132 | 610 | |
1b1fe8fe L |
611 | /* We need to rememeber if a symbol has a definition in a dynamic |
612 | object or is weak in all dynamic objects. Internal and hidden | |
613 | visibility will make it unavailable to dynamic objects. */ | |
614 | if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0) | |
615 | { | |
616 | if (!bfd_is_und_section (sec)) | |
617 | h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF; | |
618 | else | |
619 | { | |
620 | /* Check if this symbol is weak in all dynamic objects. If it | |
621 | is the first time we see it in a dynamic object, we mark | |
622 | if it is weak. Otherwise, we clear it. */ | |
623 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0) | |
624 | { | |
625 | if (bind == STB_WEAK) | |
626 | h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK; | |
627 | } | |
628 | else if (bind != STB_WEAK) | |
629 | h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK; | |
630 | } | |
631 | } | |
632 | ||
633 | /* If the old symbol has non-default visibility, we ignore the new | |
634 | definition from a dynamic object. */ | |
635 | if (newdyn | |
636 | && ELF_ST_VISIBILITY (h->other) | |
637 | && !bfd_is_und_section (sec)) | |
638 | { | |
639 | *skip = TRUE; | |
640 | /* Make sure this symbol is dynamic. */ | |
641 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
d31e3dfe L |
642 | /* A protected symbol has external availability. Make sure it is |
643 | recorded as dynamic. | |
644 | ||
645 | FIXME: Should we check type and size for protected symbol? */ | |
646 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
647 | return _bfd_elf_link_record_dynamic_symbol (info, h); | |
648 | else | |
649 | return TRUE; | |
1b1fe8fe L |
650 | } |
651 | else if (!newdyn | |
652 | && ELF_ST_VISIBILITY (sym->st_other) | |
653 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) | |
654 | { | |
655 | /* If the new symbol with non-default visibility comes from a | |
656 | relocatable file and the old definition comes from a dynamic | |
657 | object, we remove the old definition. */ | |
d31e3dfe L |
658 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) |
659 | h = *sym_hash; | |
1b1fe8fe L |
660 | h->root.type = bfd_link_hash_new; |
661 | h->root.u.undef.abfd = NULL; | |
d31e3dfe L |
662 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) |
663 | { | |
664 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC; | |
665 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
666 | } | |
1b1fe8fe L |
667 | /* FIXME: Should we check type and size for protected symbol? */ |
668 | h->size = 0; | |
669 | h->type = 0; | |
670 | return TRUE; | |
671 | } | |
672 | ||
252b5132 RH |
673 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
674 | symbol, respectively, appears to be a common symbol in a dynamic | |
675 | object. If a symbol appears in an uninitialized section, and is | |
676 | not weak, and is not a function, then it may be a common symbol | |
677 | which was resolved when the dynamic object was created. We want | |
678 | to treat such symbols specially, because they raise special | |
679 | considerations when setting the symbol size: if the symbol | |
680 | appears as a common symbol in a regular object, and the size in | |
681 | the regular object is larger, we must make sure that we use the | |
682 | larger size. This problematic case can always be avoided in C, | |
683 | but it must be handled correctly when using Fortran shared | |
684 | libraries. | |
685 | ||
686 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
687 | likewise for OLDDYNCOMMON and OLDDEF. | |
688 | ||
689 | Note that this test is just a heuristic, and that it is quite | |
690 | possible to have an uninitialized symbol in a shared object which | |
691 | is really a definition, rather than a common symbol. This could | |
692 | lead to some minor confusion when the symbol really is a common | |
693 | symbol in some regular object. However, I think it will be | |
694 | harmless. */ | |
695 | ||
696 | if (newdyn | |
697 | && newdef | |
698 | && (sec->flags & SEC_ALLOC) != 0 | |
699 | && (sec->flags & SEC_LOAD) == 0 | |
700 | && sym->st_size > 0 | |
701 | && bind != STB_WEAK | |
702 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) | |
b34976b6 | 703 | newdyncommon = TRUE; |
252b5132 | 704 | else |
b34976b6 | 705 | newdyncommon = FALSE; |
252b5132 RH |
706 | |
707 | if (olddyn | |
708 | && olddef | |
709 | && h->root.type == bfd_link_hash_defined | |
710 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
711 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 | |
712 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
713 | && h->size > 0 | |
714 | && h->type != STT_FUNC) | |
b34976b6 | 715 | olddyncommon = TRUE; |
252b5132 | 716 | else |
b34976b6 | 717 | olddyncommon = FALSE; |
252b5132 RH |
718 | |
719 | /* It's OK to change the type if either the existing symbol or the | |
456981d7 L |
720 | new symbol is weak unless it comes from a DT_NEEDED entry of |
721 | a shared object, in which case, the DT_NEEDED entry may not be | |
3e932841 | 722 | required at the run time. */ |
252b5132 | 723 | |
456981d7 | 724 | if ((! dt_needed && h->root.type == bfd_link_hash_defweak) |
252b5132 RH |
725 | || h->root.type == bfd_link_hash_undefweak |
726 | || bind == STB_WEAK) | |
b34976b6 | 727 | *type_change_ok = TRUE; |
252b5132 RH |
728 | |
729 | /* It's OK to change the size if either the existing symbol or the | |
730 | new symbol is weak, or if the old symbol is undefined. */ | |
731 | ||
732 | if (*type_change_ok | |
733 | || h->root.type == bfd_link_hash_undefined) | |
b34976b6 | 734 | *size_change_ok = TRUE; |
252b5132 RH |
735 | |
736 | /* If both the old and the new symbols look like common symbols in a | |
737 | dynamic object, set the size of the symbol to the larger of the | |
738 | two. */ | |
739 | ||
740 | if (olddyncommon | |
741 | && newdyncommon | |
742 | && sym->st_size != h->size) | |
743 | { | |
744 | /* Since we think we have two common symbols, issue a multiple | |
c44233aa AM |
745 | common warning if desired. Note that we only warn if the |
746 | size is different. If the size is the same, we simply let | |
747 | the old symbol override the new one as normally happens with | |
748 | symbols defined in dynamic objects. */ | |
252b5132 RH |
749 | |
750 | if (! ((*info->callbacks->multiple_common) | |
751 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
752 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
b34976b6 | 753 | return FALSE; |
252b5132 RH |
754 | |
755 | if (sym->st_size > h->size) | |
756 | h->size = sym->st_size; | |
757 | ||
b34976b6 | 758 | *size_change_ok = TRUE; |
252b5132 RH |
759 | } |
760 | ||
761 | /* If we are looking at a dynamic object, and we have found a | |
762 | definition, we need to see if the symbol was already defined by | |
763 | some other object. If so, we want to use the existing | |
764 | definition, and we do not want to report a multiple symbol | |
765 | definition error; we do this by clobbering *PSEC to be | |
766 | bfd_und_section_ptr. | |
767 | ||
768 | We treat a common symbol as a definition if the symbol in the | |
769 | shared library is a function, since common symbols always | |
770 | represent variables; this can cause confusion in principle, but | |
771 | any such confusion would seem to indicate an erroneous program or | |
772 | shared library. We also permit a common symbol in a regular | |
0525d26e ILT |
773 | object to override a weak symbol in a shared object. |
774 | ||
775 | We prefer a non-weak definition in a shared library to a weak | |
456981d7 L |
776 | definition in the executable unless it comes from a DT_NEEDED |
777 | entry of a shared object, in which case, the DT_NEEDED entry | |
3e932841 | 778 | may not be required at the run time. */ |
252b5132 RH |
779 | |
780 | if (newdyn | |
781 | && newdef | |
782 | && (olddef | |
783 | || (h->root.type == bfd_link_hash_common | |
784 | && (bind == STB_WEAK | |
0525d26e | 785 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC))) |
3e932841 | 786 | && (h->root.type != bfd_link_hash_defweak |
456981d7 | 787 | || dt_needed |
0525d26e | 788 | || bind == STB_WEAK)) |
252b5132 | 789 | { |
b34976b6 AM |
790 | *override = TRUE; |
791 | newdef = FALSE; | |
792 | newdyncommon = FALSE; | |
252b5132 RH |
793 | |
794 | *psec = sec = bfd_und_section_ptr; | |
b34976b6 | 795 | *size_change_ok = TRUE; |
252b5132 RH |
796 | |
797 | /* If we get here when the old symbol is a common symbol, then | |
c44233aa AM |
798 | we are explicitly letting it override a weak symbol or |
799 | function in a dynamic object, and we don't want to warn about | |
800 | a type change. If the old symbol is a defined symbol, a type | |
801 | change warning may still be appropriate. */ | |
252b5132 RH |
802 | |
803 | if (h->root.type == bfd_link_hash_common) | |
b34976b6 | 804 | *type_change_ok = TRUE; |
252b5132 RH |
805 | } |
806 | ||
807 | /* Handle the special case of an old common symbol merging with a | |
808 | new symbol which looks like a common symbol in a shared object. | |
809 | We change *PSEC and *PVALUE to make the new symbol look like a | |
810 | common symbol, and let _bfd_generic_link_add_one_symbol will do | |
811 | the right thing. */ | |
812 | ||
813 | if (newdyncommon | |
814 | && h->root.type == bfd_link_hash_common) | |
815 | { | |
b34976b6 AM |
816 | *override = TRUE; |
817 | newdef = FALSE; | |
818 | newdyncommon = FALSE; | |
252b5132 RH |
819 | *pvalue = sym->st_size; |
820 | *psec = sec = bfd_com_section_ptr; | |
b34976b6 | 821 | *size_change_ok = TRUE; |
252b5132 RH |
822 | } |
823 | ||
824 | /* If the old symbol is from a dynamic object, and the new symbol is | |
825 | a definition which is not from a dynamic object, then the new | |
826 | symbol overrides the old symbol. Symbols from regular files | |
827 | always take precedence over symbols from dynamic objects, even if | |
828 | they are defined after the dynamic object in the link. | |
829 | ||
830 | As above, we again permit a common symbol in a regular object to | |
831 | override a definition in a shared object if the shared object | |
0525d26e ILT |
832 | symbol is a function or is weak. |
833 | ||
834 | As above, we permit a non-weak definition in a shared object to | |
835 | override a weak definition in a regular object. */ | |
252b5132 | 836 | |
3c0a515d | 837 | flip = NULL; |
252b5132 RH |
838 | if (! newdyn |
839 | && (newdef | |
840 | || (bfd_is_com_section (sec) | |
841 | && (h->root.type == bfd_link_hash_defweak | |
842 | || h->type == STT_FUNC))) | |
843 | && olddyn | |
844 | && olddef | |
0525d26e ILT |
845 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
846 | && (bind != STB_WEAK | |
847 | || h->root.type == bfd_link_hash_defweak)) | |
252b5132 RH |
848 | { |
849 | /* Change the hash table entry to undefined, and let | |
850 | _bfd_generic_link_add_one_symbol do the right thing with the | |
851 | new definition. */ | |
852 | ||
853 | h->root.type = bfd_link_hash_undefined; | |
854 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
b34976b6 | 855 | *size_change_ok = TRUE; |
252b5132 | 856 | |
b34976b6 AM |
857 | olddef = FALSE; |
858 | olddyncommon = FALSE; | |
252b5132 RH |
859 | |
860 | /* We again permit a type change when a common symbol may be | |
c44233aa | 861 | overriding a function. */ |
252b5132 RH |
862 | |
863 | if (bfd_is_com_section (sec)) | |
b34976b6 | 864 | *type_change_ok = TRUE; |
252b5132 | 865 | |
3c0a515d L |
866 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) |
867 | flip = *sym_hash; | |
868 | else | |
869 | /* This union may have been set to be non-NULL when this symbol | |
870 | was seen in a dynamic object. We must force the union to be | |
871 | NULL, so that it is correct for a regular symbol. */ | |
872 | h->verinfo.vertree = NULL; | |
252b5132 RH |
873 | } |
874 | ||
875 | /* Handle the special case of a new common symbol merging with an | |
876 | old symbol that looks like it might be a common symbol defined in | |
877 | a shared object. Note that we have already handled the case in | |
878 | which a new common symbol should simply override the definition | |
879 | in the shared library. */ | |
880 | ||
881 | if (! newdyn | |
882 | && bfd_is_com_section (sec) | |
883 | && olddyncommon) | |
884 | { | |
885 | /* It would be best if we could set the hash table entry to a | |
886 | common symbol, but we don't know what to use for the section | |
887 | or the alignment. */ | |
888 | if (! ((*info->callbacks->multiple_common) | |
889 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
890 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
b34976b6 | 891 | return FALSE; |
252b5132 RH |
892 | |
893 | /* If the predumed common symbol in the dynamic object is | |
c44233aa | 894 | larger, pretend that the new symbol has its size. */ |
252b5132 RH |
895 | |
896 | if (h->size > *pvalue) | |
897 | *pvalue = h->size; | |
898 | ||
899 | /* FIXME: We no longer know the alignment required by the symbol | |
900 | in the dynamic object, so we just wind up using the one from | |
901 | the regular object. */ | |
902 | ||
b34976b6 AM |
903 | olddef = FALSE; |
904 | olddyncommon = FALSE; | |
252b5132 RH |
905 | |
906 | h->root.type = bfd_link_hash_undefined; | |
907 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
908 | ||
b34976b6 AM |
909 | *size_change_ok = TRUE; |
910 | *type_change_ok = TRUE; | |
252b5132 | 911 | |
3c0a515d L |
912 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) |
913 | flip = *sym_hash; | |
914 | else | |
915 | h->verinfo.vertree = NULL; | |
916 | } | |
917 | ||
918 | if (flip != NULL) | |
919 | { | |
920 | /* Handle the case where we had a versioned symbol in a dynamic | |
921 | library and now find a definition in a normal object. In this | |
922 | case, we make the versioned symbol point to the normal one. */ | |
f006af20 | 923 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
3c0a515d | 924 | flip->root.type = h->root.type; |
3c0a515d L |
925 | h->root.type = bfd_link_hash_indirect; |
926 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
f006af20 L |
927 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); |
928 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
3c0a515d L |
929 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) |
930 | { | |
931 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC; | |
932 | flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
933 | } | |
252b5132 RH |
934 | } |
935 | ||
0525d26e ILT |
936 | /* Handle the special case of a weak definition in a regular object |
937 | followed by a non-weak definition in a shared object. In this | |
456981d7 L |
938 | case, we prefer the definition in the shared object unless it |
939 | comes from a DT_NEEDED entry of a shared object, in which case, | |
3e932841 | 940 | the DT_NEEDED entry may not be required at the run time. */ |
0525d26e | 941 | if (olddef |
456981d7 | 942 | && ! dt_needed |
0525d26e ILT |
943 | && h->root.type == bfd_link_hash_defweak |
944 | && newdef | |
945 | && newdyn | |
946 | && bind != STB_WEAK) | |
b4536acd ILT |
947 | { |
948 | /* To make this work we have to frob the flags so that the rest | |
c44233aa AM |
949 | of the code does not think we are using the regular |
950 | definition. */ | |
64df8d0b ILT |
951 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) |
952 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
953 | else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) | |
954 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
955 | h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR | |
956 | | ELF_LINK_HASH_DEF_DYNAMIC); | |
b4536acd ILT |
957 | |
958 | /* If H is the target of an indirection, we want the caller to | |
c44233aa AM |
959 | use H rather than the indirect symbol. Otherwise if we are |
960 | defining a new indirect symbol we will wind up attaching it | |
961 | to the entry we are overriding. */ | |
b4536acd ILT |
962 | *sym_hash = h; |
963 | } | |
0525d26e ILT |
964 | |
965 | /* Handle the special case of a non-weak definition in a shared | |
966 | object followed by a weak definition in a regular object. In | |
3c0a515d | 967 | this case we prefer the definition in the shared object. To make |
0525d26e ILT |
968 | this work we have to tell the caller to not treat the new symbol |
969 | as a definition. */ | |
970 | if (olddef | |
971 | && olddyn | |
972 | && h->root.type != bfd_link_hash_defweak | |
973 | && newdef | |
974 | && ! newdyn | |
975 | && bind == STB_WEAK) | |
b34976b6 | 976 | *override = TRUE; |
0525d26e | 977 | |
b34976b6 | 978 | return TRUE; |
252b5132 RH |
979 | } |
980 | ||
215007a6 L |
981 | /* This function is called to create an indirect symbol from the |
982 | default for the symbol with the default version if needed. The | |
871da94f | 983 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We |
215007a6 L |
984 | set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED |
985 | indicates if it comes from a DT_NEEDED entry of a shared object. */ | |
986 | ||
b34976b6 | 987 | static bfd_boolean |
871da94f | 988 | elf_add_default_symbol (abfd, info, h, name, sym, psec, value, |
215007a6 L |
989 | dynsym, override, dt_needed) |
990 | bfd *abfd; | |
991 | struct bfd_link_info *info; | |
992 | struct elf_link_hash_entry *h; | |
993 | const char *name; | |
994 | Elf_Internal_Sym *sym; | |
871da94f | 995 | asection **psec; |
215007a6 | 996 | bfd_vma *value; |
b34976b6 AM |
997 | bfd_boolean *dynsym; |
998 | bfd_boolean override; | |
999 | bfd_boolean dt_needed; | |
215007a6 | 1000 | { |
b34976b6 AM |
1001 | bfd_boolean type_change_ok; |
1002 | bfd_boolean size_change_ok; | |
1b1fe8fe | 1003 | bfd_boolean skip; |
215007a6 L |
1004 | char *shortname; |
1005 | struct elf_link_hash_entry *hi; | |
14a793b2 | 1006 | struct bfd_link_hash_entry *bh; |
215007a6 | 1007 | struct elf_backend_data *bed; |
b34976b6 AM |
1008 | bfd_boolean collect; |
1009 | bfd_boolean dynamic; | |
215007a6 | 1010 | char *p; |
d4c88bbb | 1011 | size_t len, shortlen; |
871da94f | 1012 | asection *sec; |
215007a6 L |
1013 | |
1014 | /* If this symbol has a version, and it is the default version, we | |
1015 | create an indirect symbol from the default name to the fully | |
1016 | decorated name. This will cause external references which do not | |
1017 | specify a version to be bound to this version of the symbol. */ | |
1018 | p = strchr (name, ELF_VER_CHR); | |
1019 | if (p == NULL || p[1] != ELF_VER_CHR) | |
b34976b6 | 1020 | return TRUE; |
215007a6 L |
1021 | |
1022 | if (override) | |
1023 | { | |
1024 | /* We are overridden by an old defition. We need to check if we | |
d4c88bbb | 1025 | need to create the indirect symbol from the default name. */ |
b34976b6 AM |
1026 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, |
1027 | FALSE, FALSE); | |
215007a6 L |
1028 | BFD_ASSERT (hi != NULL); |
1029 | if (hi == h) | |
b34976b6 | 1030 | return TRUE; |
215007a6 L |
1031 | while (hi->root.type == bfd_link_hash_indirect |
1032 | || hi->root.type == bfd_link_hash_warning) | |
1033 | { | |
1034 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1035 | if (hi == h) | |
b34976b6 | 1036 | return TRUE; |
215007a6 L |
1037 | } |
1038 | } | |
1039 | ||
1040 | bed = get_elf_backend_data (abfd); | |
1041 | collect = bed->collect; | |
1042 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1043 | ||
d4c88bbb AM |
1044 | shortlen = p - name; |
1045 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
215007a6 | 1046 | if (shortname == NULL) |
b34976b6 | 1047 | return FALSE; |
d4c88bbb AM |
1048 | memcpy (shortname, name, shortlen); |
1049 | shortname[shortlen] = '\0'; | |
215007a6 L |
1050 | |
1051 | /* We are going to create a new symbol. Merge it with any existing | |
1052 | symbol with this name. For the purposes of the merge, act as | |
1053 | though we were defining the symbol we just defined, although we | |
1054 | actually going to define an indirect symbol. */ | |
b34976b6 AM |
1055 | type_change_ok = FALSE; |
1056 | size_change_ok = FALSE; | |
871da94f L |
1057 | sec = *psec; |
1058 | if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1b1fe8fe | 1059 | &hi, &skip, &override, &type_change_ok, |
215007a6 | 1060 | &size_change_ok, dt_needed)) |
b34976b6 | 1061 | return FALSE; |
215007a6 | 1062 | |
d31e3dfe L |
1063 | if (skip) |
1064 | return TRUE; | |
1065 | ||
215007a6 L |
1066 | if (! override) |
1067 | { | |
14a793b2 | 1068 | bh = &hi->root; |
215007a6 L |
1069 | if (! (_bfd_generic_link_add_one_symbol |
1070 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
b34976b6 AM |
1071 | (bfd_vma) 0, name, FALSE, collect, &bh))) |
1072 | return FALSE; | |
14a793b2 | 1073 | hi = (struct elf_link_hash_entry *) bh; |
215007a6 L |
1074 | } |
1075 | else | |
1076 | { | |
1077 | /* In this case the symbol named SHORTNAME is overriding the | |
1078 | indirect symbol we want to add. We were planning on making | |
1079 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1080 | is the name without a version. NAME is the fully versioned | |
1081 | name, and it is the default version. | |
1082 | ||
1083 | Overriding means that we already saw a definition for the | |
1084 | symbol SHORTNAME in a regular object, and it is overriding | |
1085 | the symbol defined in the dynamic object. | |
1086 | ||
1087 | When this happens, we actually want to change NAME, the | |
1088 | symbol we just added, to refer to SHORTNAME. This will cause | |
1089 | references to NAME in the shared object to become references | |
1090 | to SHORTNAME in the regular object. This is what we expect | |
1091 | when we override a function in a shared object: that the | |
1092 | references in the shared object will be mapped to the | |
1093 | definition in the regular object. */ | |
1094 | ||
1095 | while (hi->root.type == bfd_link_hash_indirect | |
1096 | || hi->root.type == bfd_link_hash_warning) | |
1097 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1098 | ||
1099 | h->root.type = bfd_link_hash_indirect; | |
1100 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
1101 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
1102 | { | |
1103 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC; | |
1104 | hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1105 | if (hi->elf_link_hash_flags | |
1106 | & (ELF_LINK_HASH_REF_REGULAR | |
1107 | | ELF_LINK_HASH_DEF_REGULAR)) | |
1108 | { | |
1109 | if (! _bfd_elf_link_record_dynamic_symbol (info, hi)) | |
b34976b6 | 1110 | return FALSE; |
215007a6 L |
1111 | } |
1112 | } | |
1113 | ||
1114 | /* Now set HI to H, so that the following code will set the | |
c44233aa | 1115 | other fields correctly. */ |
215007a6 L |
1116 | hi = h; |
1117 | } | |
1118 | ||
1119 | /* If there is a duplicate definition somewhere, then HI may not | |
1120 | point to an indirect symbol. We will have reported an error to | |
1121 | the user in that case. */ | |
1122 | ||
1123 | if (hi->root.type == bfd_link_hash_indirect) | |
1124 | { | |
1125 | struct elf_link_hash_entry *ht; | |
1126 | ||
1127 | /* If the symbol became indirect, then we assume that we have | |
1128 | not seen a definition before. */ | |
1129 | BFD_ASSERT ((hi->elf_link_hash_flags | |
1130 | & (ELF_LINK_HASH_DEF_DYNAMIC | |
1131 | | ELF_LINK_HASH_DEF_REGULAR)) == 0); | |
1132 | ||
1133 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
b48fa14c | 1134 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); |
215007a6 L |
1135 | |
1136 | /* See if the new flags lead us to realize that the symbol must | |
1137 | be dynamic. */ | |
1138 | if (! *dynsym) | |
1139 | { | |
1140 | if (! dynamic) | |
1141 | { | |
1142 | if (info->shared | |
1143 | || ((hi->elf_link_hash_flags | |
1144 | & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
b34976b6 | 1145 | *dynsym = TRUE; |
215007a6 L |
1146 | } |
1147 | else | |
1148 | { | |
1149 | if ((hi->elf_link_hash_flags | |
1150 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
b34976b6 | 1151 | *dynsym = TRUE; |
215007a6 L |
1152 | } |
1153 | } | |
1154 | } | |
1155 | ||
1156 | /* We also need to define an indirection from the nondefault version | |
1157 | of the symbol. */ | |
1158 | ||
d4c88bbb AM |
1159 | len = strlen (name); |
1160 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
215007a6 | 1161 | if (shortname == NULL) |
b34976b6 | 1162 | return FALSE; |
d4c88bbb AM |
1163 | memcpy (shortname, name, shortlen); |
1164 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
215007a6 L |
1165 | |
1166 | /* Once again, merge with any existing symbol. */ | |
b34976b6 AM |
1167 | type_change_ok = FALSE; |
1168 | size_change_ok = FALSE; | |
871da94f L |
1169 | sec = *psec; |
1170 | if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1b1fe8fe | 1171 | &hi, &skip, &override, &type_change_ok, |
215007a6 | 1172 | &size_change_ok, dt_needed)) |
b34976b6 | 1173 | return FALSE; |
215007a6 | 1174 | |
d31e3dfe L |
1175 | if (skip) |
1176 | return TRUE; | |
1177 | ||
215007a6 L |
1178 | if (override) |
1179 | { | |
1180 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
d8bbe778 L |
1181 | the type of override we do in the case above unless it is |
1182 | overridden by a versioned definiton. */ | |
1183 | if (hi->root.type != bfd_link_hash_defined | |
1184 | && hi->root.type != bfd_link_hash_defweak) | |
1185 | (*_bfd_error_handler) | |
1186 | (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"), | |
1187 | bfd_archive_filename (abfd), shortname); | |
215007a6 L |
1188 | } |
1189 | else | |
1190 | { | |
14a793b2 | 1191 | bh = &hi->root; |
215007a6 L |
1192 | if (! (_bfd_generic_link_add_one_symbol |
1193 | (info, abfd, shortname, BSF_INDIRECT, | |
b34976b6 AM |
1194 | bfd_ind_section_ptr, (bfd_vma) 0, name, FALSE, collect, &bh))) |
1195 | return FALSE; | |
14a793b2 | 1196 | hi = (struct elf_link_hash_entry *) bh; |
215007a6 L |
1197 | |
1198 | /* If there is a duplicate definition somewhere, then HI may not | |
1199 | point to an indirect symbol. We will have reported an error | |
1200 | to the user in that case. */ | |
1201 | ||
1202 | if (hi->root.type == bfd_link_hash_indirect) | |
1203 | { | |
1204 | /* If the symbol became indirect, then we assume that we have | |
1205 | not seen a definition before. */ | |
1206 | BFD_ASSERT ((hi->elf_link_hash_flags | |
1207 | & (ELF_LINK_HASH_DEF_DYNAMIC | |
1208 | | ELF_LINK_HASH_DEF_REGULAR)) == 0); | |
1209 | ||
b48fa14c | 1210 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); |
215007a6 L |
1211 | |
1212 | /* See if the new flags lead us to realize that the symbol | |
1213 | must be dynamic. */ | |
1214 | if (! *dynsym) | |
1215 | { | |
1216 | if (! dynamic) | |
1217 | { | |
1218 | if (info->shared | |
1219 | || ((hi->elf_link_hash_flags | |
1220 | & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
b34976b6 | 1221 | *dynsym = TRUE; |
215007a6 L |
1222 | } |
1223 | else | |
1224 | { | |
1225 | if ((hi->elf_link_hash_flags | |
1226 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
b34976b6 | 1227 | *dynsym = TRUE; |
215007a6 L |
1228 | } |
1229 | } | |
1230 | } | |
1231 | } | |
1232 | ||
b34976b6 | 1233 | return TRUE; |
215007a6 L |
1234 | } |
1235 | ||
252b5132 RH |
1236 | /* Add symbols from an ELF object file to the linker hash table. */ |
1237 | ||
b34976b6 | 1238 | static bfd_boolean |
252b5132 RH |
1239 | elf_link_add_object_symbols (abfd, info) |
1240 | bfd *abfd; | |
1241 | struct bfd_link_info *info; | |
1242 | { | |
b34976b6 AM |
1243 | bfd_boolean (*add_symbol_hook) |
1244 | PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *, | |
1245 | const char **, flagword *, asection **, bfd_vma *)); | |
1246 | bfd_boolean (*check_relocs) | |
1247 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
1248 | const Elf_Internal_Rela *)); | |
1249 | bfd_boolean collect; | |
252b5132 | 1250 | Elf_Internal_Shdr *hdr; |
dc810e39 AM |
1251 | bfd_size_type symcount; |
1252 | bfd_size_type extsymcount; | |
1253 | bfd_size_type extsymoff; | |
252b5132 | 1254 | struct elf_link_hash_entry **sym_hash; |
b34976b6 | 1255 | bfd_boolean dynamic; |
252b5132 RH |
1256 | Elf_External_Versym *extversym = NULL; |
1257 | Elf_External_Versym *ever; | |
252b5132 | 1258 | struct elf_link_hash_entry *weaks; |
9147e853 JJ |
1259 | struct elf_link_hash_entry **nondeflt_vers = NULL; |
1260 | bfd_size_type nondeflt_vers_cnt = 0; | |
6cdc0ccc AM |
1261 | Elf_Internal_Sym *isymbuf = NULL; |
1262 | Elf_Internal_Sym *isym; | |
1263 | Elf_Internal_Sym *isymend; | |
c61b8717 | 1264 | struct elf_backend_data *bed; |
b34976b6 | 1265 | bfd_boolean dt_needed; |
8ea2e4bd | 1266 | struct elf_link_hash_table * hash_table; |
dc810e39 | 1267 | bfd_size_type amt; |
8ea2e4bd NC |
1268 | |
1269 | hash_table = elf_hash_table (info); | |
252b5132 | 1270 | |
c61b8717 RH |
1271 | bed = get_elf_backend_data (abfd); |
1272 | add_symbol_hook = bed->elf_add_symbol_hook; | |
1273 | collect = bed->collect; | |
252b5132 RH |
1274 | |
1275 | if ((abfd->flags & DYNAMIC) == 0) | |
b34976b6 | 1276 | dynamic = FALSE; |
252b5132 RH |
1277 | else |
1278 | { | |
b34976b6 | 1279 | dynamic = TRUE; |
252b5132 RH |
1280 | |
1281 | /* You can't use -r against a dynamic object. Also, there's no | |
1282 | hope of using a dynamic object which does not exactly match | |
1283 | the format of the output file. */ | |
1284 | if (info->relocateable || info->hash->creator != abfd->xvec) | |
1285 | { | |
1286 | bfd_set_error (bfd_error_invalid_operation); | |
1287 | goto error_return; | |
1288 | } | |
1289 | } | |
1290 | ||
1291 | /* As a GNU extension, any input sections which are named | |
1292 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
1293 | symbol. This differs from .gnu.warning sections, which generate | |
1294 | warnings when they are included in an output file. */ | |
1295 | if (! info->shared) | |
1296 | { | |
1297 | asection *s; | |
1298 | ||
1299 | for (s = abfd->sections; s != NULL; s = s->next) | |
1300 | { | |
1301 | const char *name; | |
1302 | ||
1303 | name = bfd_get_section_name (abfd, s); | |
1304 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
1305 | { | |
1306 | char *msg; | |
1307 | bfd_size_type sz; | |
1308 | ||
1309 | name += sizeof ".gnu.warning." - 1; | |
1310 | ||
1311 | /* If this is a shared object, then look up the symbol | |
1312 | in the hash table. If it is there, and it is already | |
1313 | been defined, then we will not be using the entry | |
1314 | from this shared object, so we don't need to warn. | |
1315 | FIXME: If we see the definition in a regular object | |
1316 | later on, we will warn, but we shouldn't. The only | |
1317 | fix is to keep track of what warnings we are supposed | |
1318 | to emit, and then handle them all at the end of the | |
1319 | link. */ | |
1320 | if (dynamic && abfd->xvec == info->hash->creator) | |
1321 | { | |
1322 | struct elf_link_hash_entry *h; | |
1323 | ||
8ea2e4bd | 1324 | h = elf_link_hash_lookup (hash_table, name, |
b34976b6 | 1325 | FALSE, FALSE, TRUE); |
252b5132 RH |
1326 | |
1327 | /* FIXME: What about bfd_link_hash_common? */ | |
1328 | if (h != NULL | |
1329 | && (h->root.type == bfd_link_hash_defined | |
1330 | || h->root.type == bfd_link_hash_defweak)) | |
1331 | { | |
1332 | /* We don't want to issue this warning. Clobber | |
c44233aa AM |
1333 | the section size so that the warning does not |
1334 | get copied into the output file. */ | |
252b5132 RH |
1335 | s->_raw_size = 0; |
1336 | continue; | |
1337 | } | |
1338 | } | |
1339 | ||
1340 | sz = bfd_section_size (abfd, s); | |
1341 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
1342 | if (msg == NULL) | |
1343 | goto error_return; | |
1344 | ||
1345 | if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz)) | |
1346 | goto error_return; | |
1347 | ||
1348 | msg[sz] = '\0'; | |
1349 | ||
1350 | if (! (_bfd_generic_link_add_one_symbol | |
1351 | (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg, | |
b34976b6 | 1352 | FALSE, collect, (struct bfd_link_hash_entry **) NULL))) |
252b5132 RH |
1353 | goto error_return; |
1354 | ||
1355 | if (! info->relocateable) | |
1356 | { | |
1357 | /* Clobber the section size so that the warning does | |
c44233aa | 1358 | not get copied into the output file. */ |
252b5132 RH |
1359 | s->_raw_size = 0; |
1360 | } | |
1361 | } | |
1362 | } | |
1363 | } | |
1364 | ||
b34976b6 | 1365 | dt_needed = FALSE; |
252b5132 RH |
1366 | if (! dynamic) |
1367 | { | |
1368 | /* If we are creating a shared library, create all the dynamic | |
c44233aa AM |
1369 | sections immediately. We need to attach them to something, |
1370 | so we attach them to this BFD, provided it is the right | |
1371 | format. FIXME: If there are no input BFD's of the same | |
1372 | format as the output, we can't make a shared library. */ | |
252b5132 | 1373 | if (info->shared |
8ea2e4bd NC |
1374 | && is_elf_hash_table (info) |
1375 | && ! hash_table->dynamic_sections_created | |
252b5132 RH |
1376 | && abfd->xvec == info->hash->creator) |
1377 | { | |
1378 | if (! elf_link_create_dynamic_sections (abfd, info)) | |
1379 | goto error_return; | |
1380 | } | |
1381 | } | |
8ea2e4bd NC |
1382 | else if (! is_elf_hash_table (info)) |
1383 | goto error_return; | |
252b5132 RH |
1384 | else |
1385 | { | |
1386 | asection *s; | |
b34976b6 | 1387 | bfd_boolean add_needed; |
252b5132 RH |
1388 | const char *name; |
1389 | bfd_size_type oldsize; | |
1390 | bfd_size_type strindex; | |
5d603aa9 | 1391 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
252b5132 | 1392 | |
366f2964 AM |
1393 | /* ld --just-symbols and dynamic objects don't mix very well. |
1394 | Test for --just-symbols by looking at info set up by | |
1395 | _bfd_elf_link_just_syms. */ | |
1396 | if ((s = abfd->sections) != NULL | |
68bfbfcc | 1397 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) |
366f2964 AM |
1398 | goto error_return; |
1399 | ||
252b5132 RH |
1400 | /* Find the name to use in a DT_NEEDED entry that refers to this |
1401 | object. If the object has a DT_SONAME entry, we use it. | |
1402 | Otherwise, if the generic linker stuck something in | |
1403 | elf_dt_name, we use that. Otherwise, we just use the file | |
1404 | name. If the generic linker put a null string into | |
1405 | elf_dt_name, we don't make a DT_NEEDED entry at all, even if | |
1406 | there is a DT_SONAME entry. */ | |
b34976b6 | 1407 | add_needed = TRUE; |
7913c838 | 1408 | name = bfd_get_filename (abfd); |
252b5132 RH |
1409 | if (elf_dt_name (abfd) != NULL) |
1410 | { | |
1411 | name = elf_dt_name (abfd); | |
1412 | if (*name == '\0') | |
74816898 L |
1413 | { |
1414 | if (elf_dt_soname (abfd) != NULL) | |
b34976b6 | 1415 | dt_needed = TRUE; |
74816898 | 1416 | |
b34976b6 | 1417 | add_needed = FALSE; |
74816898 | 1418 | } |
252b5132 RH |
1419 | } |
1420 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1421 | if (s != NULL) | |
1422 | { | |
6cdc0ccc | 1423 | Elf_External_Dyn *dynbuf = NULL; |
252b5132 RH |
1424 | Elf_External_Dyn *extdyn; |
1425 | Elf_External_Dyn *extdynend; | |
1426 | int elfsec; | |
dc810e39 | 1427 | unsigned long shlink; |
252b5132 | 1428 | |
dc810e39 | 1429 | dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size); |
252b5132 RH |
1430 | if (dynbuf == NULL) |
1431 | goto error_return; | |
1432 | ||
1433 | if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, | |
1434 | (file_ptr) 0, s->_raw_size)) | |
6cdc0ccc | 1435 | goto error_free_dyn; |
252b5132 RH |
1436 | |
1437 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
1438 | if (elfsec == -1) | |
6cdc0ccc | 1439 | goto error_free_dyn; |
dc810e39 | 1440 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1441 | |
1442 | extdyn = dynbuf; | |
1443 | extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn); | |
1444 | for (; extdyn < extdynend; extdyn++) | |
1445 | { | |
1446 | Elf_Internal_Dyn dyn; | |
1447 | ||
1448 | elf_swap_dyn_in (abfd, extdyn, &dyn); | |
1449 | if (dyn.d_tag == DT_SONAME) | |
1450 | { | |
dc810e39 AM |
1451 | unsigned int tagv = dyn.d_un.d_val; |
1452 | name = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
252b5132 | 1453 | if (name == NULL) |
6cdc0ccc | 1454 | goto error_free_dyn; |
252b5132 RH |
1455 | } |
1456 | if (dyn.d_tag == DT_NEEDED) | |
1457 | { | |
1458 | struct bfd_link_needed_list *n, **pn; | |
1459 | char *fnm, *anm; | |
dc810e39 | 1460 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1461 | |
dc810e39 AM |
1462 | amt = sizeof (struct bfd_link_needed_list); |
1463 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); | |
1464 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
252b5132 | 1465 | if (n == NULL || fnm == NULL) |
6cdc0ccc | 1466 | goto error_free_dyn; |
d4c88bbb AM |
1467 | amt = strlen (fnm) + 1; |
1468 | anm = bfd_alloc (abfd, amt); | |
252b5132 | 1469 | if (anm == NULL) |
6cdc0ccc | 1470 | goto error_free_dyn; |
d4c88bbb | 1471 | memcpy (anm, fnm, (size_t) amt); |
252b5132 RH |
1472 | n->name = anm; |
1473 | n->by = abfd; | |
1474 | n->next = NULL; | |
8ea2e4bd | 1475 | for (pn = & hash_table->needed; |
252b5132 RH |
1476 | *pn != NULL; |
1477 | pn = &(*pn)->next) | |
1478 | ; | |
1479 | *pn = n; | |
1480 | } | |
a963dc6a L |
1481 | if (dyn.d_tag == DT_RUNPATH) |
1482 | { | |
1483 | struct bfd_link_needed_list *n, **pn; | |
1484 | char *fnm, *anm; | |
dc810e39 | 1485 | unsigned int tagv = dyn.d_un.d_val; |
a963dc6a | 1486 | |
dc810e39 AM |
1487 | amt = sizeof (struct bfd_link_needed_list); |
1488 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); | |
1489 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
a963dc6a | 1490 | if (n == NULL || fnm == NULL) |
6cdc0ccc | 1491 | goto error_free_dyn; |
d4c88bbb AM |
1492 | amt = strlen (fnm) + 1; |
1493 | anm = bfd_alloc (abfd, amt); | |
a963dc6a | 1494 | if (anm == NULL) |
6cdc0ccc | 1495 | goto error_free_dyn; |
d4c88bbb | 1496 | memcpy (anm, fnm, (size_t) amt); |
a963dc6a L |
1497 | n->name = anm; |
1498 | n->by = abfd; | |
1499 | n->next = NULL; | |
5d603aa9 | 1500 | for (pn = & runpath; |
a963dc6a L |
1501 | *pn != NULL; |
1502 | pn = &(*pn)->next) | |
1503 | ; | |
1504 | *pn = n; | |
a963dc6a | 1505 | } |
3e932841 | 1506 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ |
a963dc6a | 1507 | if (!runpath && dyn.d_tag == DT_RPATH) |
c44233aa | 1508 | { |
a963dc6a L |
1509 | struct bfd_link_needed_list *n, **pn; |
1510 | char *fnm, *anm; | |
dc810e39 | 1511 | unsigned int tagv = dyn.d_un.d_val; |
a963dc6a | 1512 | |
dc810e39 AM |
1513 | amt = sizeof (struct bfd_link_needed_list); |
1514 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); | |
1515 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
a963dc6a | 1516 | if (n == NULL || fnm == NULL) |
6cdc0ccc | 1517 | goto error_free_dyn; |
d4c88bbb AM |
1518 | amt = strlen (fnm) + 1; |
1519 | anm = bfd_alloc (abfd, amt); | |
a963dc6a | 1520 | if (anm == NULL) |
6cdc0ccc AM |
1521 | { |
1522 | error_free_dyn: | |
1523 | free (dynbuf); | |
1524 | goto error_return; | |
1525 | } | |
d4c88bbb | 1526 | memcpy (anm, fnm, (size_t) amt); |
a963dc6a L |
1527 | n->name = anm; |
1528 | n->by = abfd; | |
1529 | n->next = NULL; | |
5d603aa9 | 1530 | for (pn = & rpath; |
a963dc6a L |
1531 | *pn != NULL; |
1532 | pn = &(*pn)->next) | |
1533 | ; | |
1534 | *pn = n; | |
a963dc6a | 1535 | } |
252b5132 RH |
1536 | } |
1537 | ||
1538 | free (dynbuf); | |
252b5132 RH |
1539 | } |
1540 | ||
5d603aa9 L |
1541 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that |
1542 | frees all more recently bfd_alloc'd blocks as well. */ | |
1543 | if (runpath) | |
1544 | rpath = runpath; | |
1545 | ||
1546 | if (rpath) | |
1547 | { | |
1548 | struct bfd_link_needed_list **pn; | |
1549 | for (pn = & hash_table->runpath; | |
1550 | *pn != NULL; | |
1551 | pn = &(*pn)->next) | |
1552 | ; | |
1553 | *pn = rpath; | |
1554 | } | |
1555 | ||
252b5132 RH |
1556 | /* We do not want to include any of the sections in a dynamic |
1557 | object in the output file. We hack by simply clobbering the | |
1558 | list of sections in the BFD. This could be handled more | |
1559 | cleanly by, say, a new section flag; the existing | |
1560 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
1561 | still implies that the section takes up space in the output | |
1562 | file. */ | |
c601ffdb | 1563 | bfd_section_list_clear (abfd); |
252b5132 RH |
1564 | |
1565 | /* If this is the first dynamic object found in the link, create | |
1566 | the special sections required for dynamic linking. */ | |
8ea2e4bd NC |
1567 | if (! hash_table->dynamic_sections_created) |
1568 | if (! elf_link_create_dynamic_sections (abfd, info)) | |
1569 | goto error_return; | |
252b5132 RH |
1570 | |
1571 | if (add_needed) | |
1572 | { | |
1573 | /* Add a DT_NEEDED entry for this dynamic object. */ | |
2b0f7ef9 | 1574 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); |
b34976b6 | 1575 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, FALSE); |
252b5132 RH |
1576 | if (strindex == (bfd_size_type) -1) |
1577 | goto error_return; | |
1578 | ||
2b0f7ef9 | 1579 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) |
252b5132 RH |
1580 | { |
1581 | asection *sdyn; | |
1582 | Elf_External_Dyn *dyncon, *dynconend; | |
1583 | ||
1584 | /* The hash table size did not change, which means that | |
1585 | the dynamic object name was already entered. If we | |
1586 | have already included this dynamic object in the | |
1587 | link, just ignore it. There is no reason to include | |
1588 | a particular dynamic object more than once. */ | |
8ea2e4bd | 1589 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); |
252b5132 RH |
1590 | BFD_ASSERT (sdyn != NULL); |
1591 | ||
1592 | dyncon = (Elf_External_Dyn *) sdyn->contents; | |
1593 | dynconend = (Elf_External_Dyn *) (sdyn->contents + | |
1594 | sdyn->_raw_size); | |
1595 | for (; dyncon < dynconend; dyncon++) | |
1596 | { | |
1597 | Elf_Internal_Dyn dyn; | |
1598 | ||
8ea2e4bd | 1599 | elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn); |
252b5132 RH |
1600 | if (dyn.d_tag == DT_NEEDED |
1601 | && dyn.d_un.d_val == strindex) | |
1602 | { | |
2b0f7ef9 | 1603 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); |
b34976b6 | 1604 | return TRUE; |
252b5132 RH |
1605 | } |
1606 | } | |
1607 | } | |
1608 | ||
dc810e39 | 1609 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex)) |
252b5132 RH |
1610 | goto error_return; |
1611 | } | |
1612 | ||
1613 | /* Save the SONAME, if there is one, because sometimes the | |
c44233aa | 1614 | linker emulation code will need to know it. */ |
252b5132 | 1615 | if (*name == '\0') |
210ba1e8 | 1616 | name = basename (bfd_get_filename (abfd)); |
252b5132 RH |
1617 | elf_dt_name (abfd) = name; |
1618 | } | |
1619 | ||
6cdc0ccc AM |
1620 | /* If this is a dynamic object, we always link against the .dynsym |
1621 | symbol table, not the .symtab symbol table. The dynamic linker | |
1622 | will only see the .dynsym symbol table, so there is no reason to | |
1623 | look at .symtab for a dynamic object. */ | |
1624 | ||
1625 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
1626 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
1627 | else | |
1628 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1629 | ||
1630 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); | |
1631 | ||
1632 | /* The sh_info field of the symtab header tells us where the | |
1633 | external symbols start. We don't care about the local symbols at | |
1634 | this point. */ | |
1635 | if (elf_bad_symtab (abfd)) | |
1636 | { | |
1637 | extsymcount = symcount; | |
1638 | extsymoff = 0; | |
1639 | } | |
1640 | else | |
1641 | { | |
1642 | extsymcount = symcount - hdr->sh_info; | |
1643 | extsymoff = hdr->sh_info; | |
1644 | } | |
252b5132 | 1645 | |
6cdc0ccc AM |
1646 | sym_hash = NULL; |
1647 | if (extsymcount != 0) | |
9ad5cbcf | 1648 | { |
6cdc0ccc AM |
1649 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, |
1650 | NULL, NULL, NULL); | |
1651 | if (isymbuf == NULL) | |
9ad5cbcf | 1652 | goto error_return; |
6cdc0ccc AM |
1653 | |
1654 | /* We store a pointer to the hash table entry for each external | |
1655 | symbol. */ | |
1656 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
1657 | sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt); | |
1658 | if (sym_hash == NULL) | |
1659 | goto error_free_sym; | |
1660 | elf_sym_hashes (abfd) = sym_hash; | |
1661 | } | |
1662 | ||
1663 | if (dynamic) | |
1664 | { | |
1665 | /* Read in any version definitions. */ | |
1666 | if (! _bfd_elf_slurp_version_tables (abfd)) | |
1667 | goto error_free_sym; | |
1668 | ||
1669 | /* Read in the symbol versions, but don't bother to convert them | |
1670 | to internal format. */ | |
1671 | if (elf_dynversym (abfd) != 0) | |
1672 | { | |
1673 | Elf_Internal_Shdr *versymhdr; | |
1674 | ||
1675 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
1676 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); | |
1677 | if (extversym == NULL) | |
1678 | goto error_free_sym; | |
1679 | amt = versymhdr->sh_size; | |
1680 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
1681 | || bfd_bread ((PTR) extversym, amt, abfd) != amt) | |
1682 | goto error_free_vers; | |
1683 | } | |
9ad5cbcf AM |
1684 | } |
1685 | ||
252b5132 RH |
1686 | weaks = NULL; |
1687 | ||
1688 | ever = extversym != NULL ? extversym + extsymoff : NULL; | |
6cdc0ccc AM |
1689 | for (isym = isymbuf, isymend = isymbuf + extsymcount; |
1690 | isym < isymend; | |
1691 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
252b5132 | 1692 | { |
252b5132 RH |
1693 | int bind; |
1694 | bfd_vma value; | |
1695 | asection *sec; | |
1696 | flagword flags; | |
1697 | const char *name; | |
1698 | struct elf_link_hash_entry *h; | |
b34976b6 | 1699 | bfd_boolean definition; |
f7661995 NC |
1700 | bfd_boolean size_change_ok; |
1701 | bfd_boolean type_change_ok; | |
b34976b6 | 1702 | bfd_boolean new_weakdef; |
b34976b6 | 1703 | bfd_boolean override; |
f7661995 NC |
1704 | unsigned int old_alignment; |
1705 | bfd *old_bfd; | |
215007a6 | 1706 | |
b34976b6 | 1707 | override = FALSE; |
252b5132 | 1708 | |
252b5132 RH |
1709 | flags = BSF_NO_FLAGS; |
1710 | sec = NULL; | |
6cdc0ccc | 1711 | value = isym->st_value; |
252b5132 RH |
1712 | *sym_hash = NULL; |
1713 | ||
6cdc0ccc | 1714 | bind = ELF_ST_BIND (isym->st_info); |
252b5132 RH |
1715 | if (bind == STB_LOCAL) |
1716 | { | |
1717 | /* This should be impossible, since ELF requires that all | |
1718 | global symbols follow all local symbols, and that sh_info | |
1719 | point to the first global symbol. Unfortunatealy, Irix 5 | |
1720 | screws this up. */ | |
1721 | continue; | |
1722 | } | |
1723 | else if (bind == STB_GLOBAL) | |
1724 | { | |
6cdc0ccc AM |
1725 | if (isym->st_shndx != SHN_UNDEF |
1726 | && isym->st_shndx != SHN_COMMON) | |
252b5132 | 1727 | flags = BSF_GLOBAL; |
252b5132 RH |
1728 | } |
1729 | else if (bind == STB_WEAK) | |
1730 | flags = BSF_WEAK; | |
1731 | else | |
1732 | { | |
1733 | /* Leave it up to the processor backend. */ | |
1734 | } | |
1735 | ||
6cdc0ccc | 1736 | if (isym->st_shndx == SHN_UNDEF) |
252b5132 | 1737 | sec = bfd_und_section_ptr; |
6cdc0ccc | 1738 | else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) |
252b5132 | 1739 | { |
6cdc0ccc | 1740 | sec = section_from_elf_index (abfd, isym->st_shndx); |
252b5132 RH |
1741 | if (sec == NULL) |
1742 | sec = bfd_abs_section_ptr; | |
1743 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) | |
1744 | value -= sec->vma; | |
1745 | } | |
6cdc0ccc | 1746 | else if (isym->st_shndx == SHN_ABS) |
252b5132 | 1747 | sec = bfd_abs_section_ptr; |
6cdc0ccc | 1748 | else if (isym->st_shndx == SHN_COMMON) |
252b5132 RH |
1749 | { |
1750 | sec = bfd_com_section_ptr; | |
1751 | /* What ELF calls the size we call the value. What ELF | |
1752 | calls the value we call the alignment. */ | |
6cdc0ccc | 1753 | value = isym->st_size; |
252b5132 RH |
1754 | } |
1755 | else | |
1756 | { | |
1757 | /* Leave it up to the processor backend. */ | |
1758 | } | |
1759 | ||
6cdc0ccc AM |
1760 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, |
1761 | isym->st_name); | |
252b5132 | 1762 | if (name == (const char *) NULL) |
6cdc0ccc | 1763 | goto error_free_vers; |
252b5132 | 1764 | |
6cdc0ccc AM |
1765 | if (isym->st_shndx == SHN_COMMON |
1766 | && ELF_ST_TYPE (isym->st_info) == STT_TLS) | |
13ae64f3 JJ |
1767 | { |
1768 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
1769 | ||
1770 | if (tcomm == NULL) | |
1771 | { | |
1772 | tcomm = bfd_make_section (abfd, ".tcommon"); | |
1773 | if (tcomm == NULL | |
1774 | || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC | |
1775 | | SEC_IS_COMMON | |
1776 | | SEC_LINKER_CREATED | |
1777 | | SEC_THREAD_LOCAL))) | |
6cdc0ccc | 1778 | goto error_free_vers; |
13ae64f3 JJ |
1779 | } |
1780 | sec = tcomm; | |
1781 | } | |
1782 | else if (add_symbol_hook) | |
252b5132 | 1783 | { |
6cdc0ccc | 1784 | if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec, |
252b5132 | 1785 | &value)) |
6cdc0ccc | 1786 | goto error_free_vers; |
252b5132 RH |
1787 | |
1788 | /* The hook function sets the name to NULL if this symbol | |
1789 | should be skipped for some reason. */ | |
1790 | if (name == (const char *) NULL) | |
1791 | continue; | |
1792 | } | |
1793 | ||
1794 | /* Sanity check that all possibilities were handled. */ | |
1795 | if (sec == (asection *) NULL) | |
1796 | { | |
1797 | bfd_set_error (bfd_error_bad_value); | |
6cdc0ccc | 1798 | goto error_free_vers; |
252b5132 RH |
1799 | } |
1800 | ||
1801 | if (bfd_is_und_section (sec) | |
1802 | || bfd_is_com_section (sec)) | |
b34976b6 | 1803 | definition = FALSE; |
252b5132 | 1804 | else |
b34976b6 | 1805 | definition = TRUE; |
252b5132 | 1806 | |
b34976b6 | 1807 | size_change_ok = FALSE; |
252b5132 RH |
1808 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; |
1809 | old_alignment = 0; | |
f7661995 NC |
1810 | old_bfd = NULL; |
1811 | ||
252b5132 RH |
1812 | if (info->hash->creator->flavour == bfd_target_elf_flavour) |
1813 | { | |
1814 | Elf_Internal_Versym iver; | |
1815 | unsigned int vernum = 0; | |
1b1fe8fe | 1816 | bfd_boolean skip; |
252b5132 RH |
1817 | |
1818 | if (ever != NULL) | |
1819 | { | |
1820 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
1821 | vernum = iver.vs_vers & VERSYM_VERSION; | |
1822 | ||
1823 | /* If this is a hidden symbol, or if it is not version | |
c44233aa AM |
1824 | 1, we append the version name to the symbol name. |
1825 | However, we do not modify a non-hidden absolute | |
1826 | symbol, because it might be the version symbol | |
1827 | itself. FIXME: What if it isn't? */ | |
252b5132 RH |
1828 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
1829 | || (vernum > 1 && ! bfd_is_abs_section (sec))) | |
1830 | { | |
1831 | const char *verstr; | |
d4c88bbb | 1832 | size_t namelen, verlen, newlen; |
252b5132 RH |
1833 | char *newname, *p; |
1834 | ||
6cdc0ccc | 1835 | if (isym->st_shndx != SHN_UNDEF) |
252b5132 RH |
1836 | { |
1837 | if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info) | |
1838 | { | |
1839 | (*_bfd_error_handler) | |
1840 | (_("%s: %s: invalid version %u (max %d)"), | |
8f615d07 | 1841 | bfd_archive_filename (abfd), name, vernum, |
252b5132 RH |
1842 | elf_tdata (abfd)->dynverdef_hdr.sh_info); |
1843 | bfd_set_error (bfd_error_bad_value); | |
6cdc0ccc | 1844 | goto error_free_vers; |
252b5132 RH |
1845 | } |
1846 | else if (vernum > 1) | |
1847 | verstr = | |
1848 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1849 | else | |
1850 | verstr = ""; | |
1851 | } | |
1852 | else | |
1853 | { | |
1854 | /* We cannot simply test for the number of | |
1855 | entries in the VERNEED section since the | |
1856 | numbers for the needed versions do not start | |
1857 | at 0. */ | |
1858 | Elf_Internal_Verneed *t; | |
1859 | ||
1860 | verstr = NULL; | |
1861 | for (t = elf_tdata (abfd)->verref; | |
1862 | t != NULL; | |
1863 | t = t->vn_nextref) | |
1864 | { | |
1865 | Elf_Internal_Vernaux *a; | |
1866 | ||
1867 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1868 | { | |
1869 | if (a->vna_other == vernum) | |
1870 | { | |
1871 | verstr = a->vna_nodename; | |
1872 | break; | |
1873 | } | |
1874 | } | |
1875 | if (a != NULL) | |
1876 | break; | |
1877 | } | |
1878 | if (verstr == NULL) | |
1879 | { | |
1880 | (*_bfd_error_handler) | |
1881 | (_("%s: %s: invalid needed version %d"), | |
8f615d07 | 1882 | bfd_archive_filename (abfd), name, vernum); |
252b5132 | 1883 | bfd_set_error (bfd_error_bad_value); |
6cdc0ccc | 1884 | goto error_free_vers; |
252b5132 RH |
1885 | } |
1886 | } | |
1887 | ||
1888 | namelen = strlen (name); | |
d4c88bbb AM |
1889 | verlen = strlen (verstr); |
1890 | newlen = namelen + verlen + 2; | |
1891 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
6cdc0ccc | 1892 | && isym->st_shndx != SHN_UNDEF) |
252b5132 RH |
1893 | ++newlen; |
1894 | ||
d4c88bbb | 1895 | newname = (char *) bfd_alloc (abfd, (bfd_size_type) newlen); |
252b5132 | 1896 | if (newname == NULL) |
6cdc0ccc | 1897 | goto error_free_vers; |
d4c88bbb | 1898 | memcpy (newname, name, namelen); |
252b5132 RH |
1899 | p = newname + namelen; |
1900 | *p++ = ELF_VER_CHR; | |
1287d1cc ILT |
1901 | /* If this is a defined non-hidden version symbol, |
1902 | we add another @ to the name. This indicates the | |
1903 | default version of the symbol. */ | |
1904 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
6cdc0ccc | 1905 | && isym->st_shndx != SHN_UNDEF) |
252b5132 | 1906 | *p++ = ELF_VER_CHR; |
d4c88bbb | 1907 | memcpy (p, verstr, verlen + 1); |
252b5132 RH |
1908 | |
1909 | name = newname; | |
1910 | } | |
1911 | } | |
1912 | ||
6cdc0ccc | 1913 | if (! elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
1b1fe8fe L |
1914 | sym_hash, &skip, &override, |
1915 | &type_change_ok, &size_change_ok, | |
1916 | dt_needed)) | |
6cdc0ccc | 1917 | goto error_free_vers; |
252b5132 | 1918 | |
1b1fe8fe L |
1919 | if (skip) |
1920 | continue; | |
1921 | ||
252b5132 | 1922 | if (override) |
b34976b6 | 1923 | definition = FALSE; |
252b5132 RH |
1924 | |
1925 | h = *sym_hash; | |
1926 | while (h->root.type == bfd_link_hash_indirect | |
1927 | || h->root.type == bfd_link_hash_warning) | |
1928 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1929 | ||
1930 | /* Remember the old alignment if this is a common symbol, so | |
c44233aa AM |
1931 | that we don't reduce the alignment later on. We can't |
1932 | check later, because _bfd_generic_link_add_one_symbol | |
1933 | will set a default for the alignment which we want to | |
f7661995 NC |
1934 | override. We also remember the old bfd where the existing |
1935 | definition comes from. */ | |
1936 | switch (h->root.type) | |
1937 | { | |
1938 | default: | |
1939 | break; | |
1940 | ||
1941 | case bfd_link_hash_defined: | |
1942 | case bfd_link_hash_defweak: | |
1943 | old_bfd = h->root.u.def.section->owner; | |
1944 | break; | |
1945 | ||
1946 | case bfd_link_hash_common: | |
1947 | old_bfd = h->root.u.c.p->section->owner; | |
1948 | old_alignment = h->root.u.c.p->alignment_power; | |
1949 | break; | |
1950 | } | |
252b5132 RH |
1951 | |
1952 | if (elf_tdata (abfd)->verdef != NULL | |
1953 | && ! override | |
1954 | && vernum > 1 | |
1955 | && definition) | |
1956 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
1957 | } | |
1958 | ||
1959 | if (! (_bfd_generic_link_add_one_symbol | |
1960 | (info, abfd, name, flags, sec, value, (const char *) NULL, | |
b34976b6 | 1961 | FALSE, collect, (struct bfd_link_hash_entry **) sym_hash))) |
6cdc0ccc | 1962 | goto error_free_vers; |
252b5132 RH |
1963 | |
1964 | h = *sym_hash; | |
1965 | while (h->root.type == bfd_link_hash_indirect | |
1966 | || h->root.type == bfd_link_hash_warning) | |
1967 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1968 | *sym_hash = h; | |
1969 | ||
b34976b6 | 1970 | new_weakdef = FALSE; |
252b5132 RH |
1971 | if (dynamic |
1972 | && definition | |
1973 | && (flags & BSF_WEAK) != 0 | |
6cdc0ccc | 1974 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC |
252b5132 RH |
1975 | && info->hash->creator->flavour == bfd_target_elf_flavour |
1976 | && h->weakdef == NULL) | |
1977 | { | |
1978 | /* Keep a list of all weak defined non function symbols from | |
1979 | a dynamic object, using the weakdef field. Later in this | |
1980 | function we will set the weakdef field to the correct | |
1981 | value. We only put non-function symbols from dynamic | |
1982 | objects on this list, because that happens to be the only | |
1983 | time we need to know the normal symbol corresponding to a | |
1984 | weak symbol, and the information is time consuming to | |
1985 | figure out. If the weakdef field is not already NULL, | |
1986 | then this symbol was already defined by some previous | |
1987 | dynamic object, and we will be using that previous | |
1988 | definition anyhow. */ | |
1989 | ||
1990 | h->weakdef = weaks; | |
1991 | weaks = h; | |
b34976b6 | 1992 | new_weakdef = TRUE; |
252b5132 RH |
1993 | } |
1994 | ||
1995 | /* Set the alignment of a common symbol. */ | |
6cdc0ccc | 1996 | if (isym->st_shndx == SHN_COMMON |
252b5132 RH |
1997 | && h->root.type == bfd_link_hash_common) |
1998 | { | |
1999 | unsigned int align; | |
2000 | ||
6cdc0ccc | 2001 | align = bfd_log2 (isym->st_value); |
724982f6 NC |
2002 | if (align > old_alignment |
2003 | /* Permit an alignment power of zero if an alignment of one | |
2004 | is specified and no other alignments have been specified. */ | |
6cdc0ccc | 2005 | || (isym->st_value == 1 && old_alignment == 0)) |
252b5132 | 2006 | h->root.u.c.p->alignment_power = align; |
f7661995 NC |
2007 | else |
2008 | h->root.u.c.p->alignment_power = old_alignment; | |
252b5132 RH |
2009 | } |
2010 | ||
2011 | if (info->hash->creator->flavour == bfd_target_elf_flavour) | |
2012 | { | |
2013 | int old_flags; | |
b34976b6 | 2014 | bfd_boolean dynsym; |
252b5132 RH |
2015 | int new_flag; |
2016 | ||
f7661995 NC |
2017 | /* Check the alignment when a common symbol is involved. This |
2018 | can change when a common symbol is overriden by a normal | |
2019 | definition or a common symbol is ignored due to the old | |
2020 | normal definition. We need to make sure the maximum | |
2021 | alignment is maintained. */ | |
2022 | if ((old_alignment || isym->st_shndx == SHN_COMMON) | |
2023 | && h->root.type != bfd_link_hash_common) | |
2024 | { | |
ca4fa240 NC |
2025 | unsigned int common_align; |
2026 | unsigned int normal_align; | |
2027 | unsigned int symbol_align; | |
2028 | bfd *normal_bfd; | |
2029 | bfd *common_bfd; | |
f7661995 NC |
2030 | |
2031 | symbol_align = ffs (h->root.u.def.value) - 1; | |
2032 | if ((h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
2033 | { | |
2034 | normal_align = h->root.u.def.section->alignment_power; | |
2035 | if (normal_align > symbol_align) | |
2036 | normal_align = symbol_align; | |
2037 | } | |
2038 | else | |
2039 | normal_align = symbol_align; | |
2040 | ||
2041 | if (old_alignment) | |
ca4fa240 NC |
2042 | { |
2043 | common_align = old_alignment; | |
2044 | common_bfd = old_bfd; | |
2045 | normal_bfd = abfd; | |
2046 | } | |
f7661995 | 2047 | else |
ca4fa240 NC |
2048 | { |
2049 | common_align = bfd_log2 (isym->st_value); | |
2050 | common_bfd = abfd; | |
2051 | normal_bfd = old_bfd; | |
2052 | } | |
f7661995 NC |
2053 | |
2054 | if (normal_align < common_align) | |
2055 | (*_bfd_error_handler) | |
2056 | (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"), | |
ca4fa240 NC |
2057 | 1 << normal_align, |
2058 | name, | |
2059 | bfd_archive_filename (normal_bfd), | |
2060 | 1 << common_align, | |
2061 | bfd_archive_filename (common_bfd)); | |
f7661995 NC |
2062 | } |
2063 | ||
252b5132 | 2064 | /* Remember the symbol size and type. */ |
6cdc0ccc | 2065 | if (isym->st_size != 0 |
252b5132 RH |
2066 | && (definition || h->size == 0)) |
2067 | { | |
6cdc0ccc | 2068 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) |
252b5132 | 2069 | (*_bfd_error_handler) |
f7661995 | 2070 | (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"), |
6cdc0ccc | 2071 | name, (unsigned long) h->size, |
f7661995 NC |
2072 | bfd_archive_filename (old_bfd), |
2073 | (unsigned long) isym->st_size, | |
2074 | bfd_archive_filename (abfd)); | |
252b5132 | 2075 | |
6cdc0ccc | 2076 | h->size = isym->st_size; |
252b5132 RH |
2077 | } |
2078 | ||
2079 | /* If this is a common symbol, then we always want H->SIZE | |
c44233aa AM |
2080 | to be the size of the common symbol. The code just above |
2081 | won't fix the size if a common symbol becomes larger. We | |
2082 | don't warn about a size change here, because that is | |
2083 | covered by --warn-common. */ | |
252b5132 RH |
2084 | if (h->root.type == bfd_link_hash_common) |
2085 | h->size = h->root.u.c.size; | |
2086 | ||
6cdc0ccc | 2087 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE |
252b5132 RH |
2088 | && (definition || h->type == STT_NOTYPE)) |
2089 | { | |
2090 | if (h->type != STT_NOTYPE | |
6cdc0ccc | 2091 | && h->type != ELF_ST_TYPE (isym->st_info) |
252b5132 RH |
2092 | && ! type_change_ok) |
2093 | (*_bfd_error_handler) | |
2094 | (_("Warning: type of symbol `%s' changed from %d to %d in %s"), | |
6cdc0ccc | 2095 | name, h->type, ELF_ST_TYPE (isym->st_info), |
8f615d07 | 2096 | bfd_archive_filename (abfd)); |
252b5132 | 2097 | |
6cdc0ccc | 2098 | h->type = ELF_ST_TYPE (isym->st_info); |
252b5132 RH |
2099 | } |
2100 | ||
1b1fe8fe L |
2101 | /* If st_other has a processor-specific meaning, specific |
2102 | code might be needed here. We never merge the visibility | |
2103 | attribute with the one from a dynamic object. */ | |
2104 | if (isym->st_other != 0 && !dynamic) | |
7a13edea | 2105 | { |
5941afc1 | 2106 | unsigned char hvis, symvis, other, nvis; |
7c25b72c RH |
2107 | |
2108 | /* Take the balance of OTHER from the definition. */ | |
2109 | other = (definition ? isym->st_other : h->other); | |
2110 | other &= ~ ELF_ST_VISIBILITY (-1); | |
2111 | ||
7a13edea | 2112 | /* Combine visibilities, using the most constraining one. */ |
7c25b72c RH |
2113 | hvis = ELF_ST_VISIBILITY (h->other); |
2114 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
5941afc1 L |
2115 | if (! hvis) |
2116 | nvis = symvis; | |
2117 | else if (! symvis) | |
2118 | nvis = hvis; | |
2119 | else | |
2120 | nvis = hvis < symvis ? hvis : symvis; | |
7c25b72c | 2121 | |
5941afc1 | 2122 | h->other = other | nvis; |
7a13edea | 2123 | } |
252b5132 RH |
2124 | |
2125 | /* Set a flag in the hash table entry indicating the type of | |
2126 | reference or definition we just found. Keep a count of | |
2127 | the number of dynamic symbols we find. A dynamic symbol | |
2128 | is one which is referenced or defined by both a regular | |
2129 | object and a shared object. */ | |
2130 | old_flags = h->elf_link_hash_flags; | |
b34976b6 | 2131 | dynsym = FALSE; |
252b5132 RH |
2132 | if (! dynamic) |
2133 | { | |
2134 | if (! definition) | |
2135 | { | |
2136 | new_flag = ELF_LINK_HASH_REF_REGULAR; | |
2137 | if (bind != STB_WEAK) | |
2138 | new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK; | |
2139 | } | |
2140 | else | |
2141 | new_flag = ELF_LINK_HASH_DEF_REGULAR; | |
2142 | if (info->shared | |
2143 | || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
2144 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0) | |
b34976b6 | 2145 | dynsym = TRUE; |
252b5132 RH |
2146 | } |
2147 | else | |
2148 | { | |
2149 | if (! definition) | |
2150 | new_flag = ELF_LINK_HASH_REF_DYNAMIC; | |
2151 | else | |
2152 | new_flag = ELF_LINK_HASH_DEF_DYNAMIC; | |
2153 | if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR | |
2154 | | ELF_LINK_HASH_REF_REGULAR)) != 0 | |
2155 | || (h->weakdef != NULL | |
2156 | && ! new_weakdef | |
2157 | && h->weakdef->dynindx != -1)) | |
b34976b6 | 2158 | dynsym = TRUE; |
252b5132 RH |
2159 | } |
2160 | ||
2161 | h->elf_link_hash_flags |= new_flag; | |
2162 | ||
215007a6 L |
2163 | /* Check to see if we need to add an indirect symbol for |
2164 | the default name. */ | |
051b8577 | 2165 | if (definition || h->root.type == bfd_link_hash_common) |
6cdc0ccc | 2166 | if (! elf_add_default_symbol (abfd, info, h, name, isym, |
215007a6 L |
2167 | &sec, &value, &dynsym, |
2168 | override, dt_needed)) | |
6cdc0ccc | 2169 | goto error_free_vers; |
252b5132 | 2170 | |
1b1fe8fe | 2171 | if (definition && !dynamic) |
9147e853 JJ |
2172 | { |
2173 | char *p = strchr (name, ELF_VER_CHR); | |
2174 | if (p != NULL && p[1] != ELF_VER_CHR) | |
2175 | { | |
2176 | /* Queue non-default versions so that .symver x, x@FOO | |
2177 | aliases can be checked. */ | |
2178 | if (! nondeflt_vers) | |
2179 | { | |
2180 | amt = (isymend - isym + 1) | |
2181 | * sizeof (struct elf_link_hash_entry *); | |
2182 | nondeflt_vers = bfd_malloc (amt); | |
2183 | } | |
2184 | nondeflt_vers [nondeflt_vers_cnt++] = h; | |
2185 | } | |
2186 | } | |
2187 | ||
252b5132 RH |
2188 | if (dynsym && h->dynindx == -1) |
2189 | { | |
2190 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
6cdc0ccc | 2191 | goto error_free_vers; |
252b5132 RH |
2192 | if (h->weakdef != NULL |
2193 | && ! new_weakdef | |
2194 | && h->weakdef->dynindx == -1) | |
2195 | { | |
a7b97311 | 2196 | if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) |
6cdc0ccc | 2197 | goto error_free_vers; |
252b5132 RH |
2198 | } |
2199 | } | |
38048eb9 | 2200 | else if (dynsym && h->dynindx != -1) |
0444bdd4 L |
2201 | /* If the symbol already has a dynamic index, but |
2202 | visibility says it should not be visible, turn it into | |
2203 | a local symbol. */ | |
2204 | switch (ELF_ST_VISIBILITY (h->other)) | |
2205 | { | |
2206 | case STV_INTERNAL: | |
3e932841 | 2207 | case STV_HIDDEN: |
b34976b6 | 2208 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
0444bdd4 L |
2209 | break; |
2210 | } | |
74816898 L |
2211 | |
2212 | if (dt_needed && definition | |
2213 | && (h->elf_link_hash_flags | |
2214 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
2215 | { | |
2216 | bfd_size_type oldsize; | |
2217 | bfd_size_type strindex; | |
2218 | ||
8ea2e4bd | 2219 | if (! is_elf_hash_table (info)) |
6cdc0ccc | 2220 | goto error_free_vers; |
8ea2e4bd | 2221 | |
74816898 | 2222 | /* The symbol from a DT_NEEDED object is referenced from |
c44233aa | 2223 | the regular object to create a dynamic executable. We |
3e932841 | 2224 | have to make sure there is a DT_NEEDED entry for it. */ |
74816898 | 2225 | |
b34976b6 | 2226 | dt_needed = FALSE; |
2b0f7ef9 JJ |
2227 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); |
2228 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, | |
b34976b6 | 2229 | elf_dt_soname (abfd), FALSE); |
74816898 | 2230 | if (strindex == (bfd_size_type) -1) |
6cdc0ccc | 2231 | goto error_free_vers; |
74816898 | 2232 | |
2b0f7ef9 | 2233 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) |
74816898 L |
2234 | { |
2235 | asection *sdyn; | |
2236 | Elf_External_Dyn *dyncon, *dynconend; | |
2237 | ||
8ea2e4bd | 2238 | sdyn = bfd_get_section_by_name (hash_table->dynobj, |
74816898 L |
2239 | ".dynamic"); |
2240 | BFD_ASSERT (sdyn != NULL); | |
2241 | ||
2242 | dyncon = (Elf_External_Dyn *) sdyn->contents; | |
2243 | dynconend = (Elf_External_Dyn *) (sdyn->contents + | |
2244 | sdyn->_raw_size); | |
2245 | for (; dyncon < dynconend; dyncon++) | |
2246 | { | |
2247 | Elf_Internal_Dyn dyn; | |
2248 | ||
8ea2e4bd | 2249 | elf_swap_dyn_in (hash_table->dynobj, |
74816898 L |
2250 | dyncon, &dyn); |
2251 | BFD_ASSERT (dyn.d_tag != DT_NEEDED || | |
2252 | dyn.d_un.d_val != strindex); | |
2253 | } | |
2254 | } | |
2255 | ||
dc810e39 | 2256 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex)) |
6cdc0ccc | 2257 | goto error_free_vers; |
74816898 | 2258 | } |
252b5132 RH |
2259 | } |
2260 | } | |
2261 | ||
9147e853 JJ |
2262 | /* Now that all the symbols from this input file are created, handle |
2263 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
2264 | if (nondeflt_vers != NULL) | |
2265 | { | |
2266 | bfd_size_type cnt, symidx; | |
2267 | ||
2268 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
2269 | { | |
2270 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
2271 | char *shortname, *p; | |
2272 | ||
2273 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
2274 | if (p == NULL | |
2275 | || (h->root.type != bfd_link_hash_defined | |
2276 | && h->root.type != bfd_link_hash_defweak)) | |
2277 | continue; | |
2278 | ||
2279 | amt = p - h->root.root.string; | |
2280 | shortname = bfd_malloc (amt + 1); | |
2281 | memcpy (shortname, h->root.root.string, amt); | |
2282 | shortname[amt] = '\0'; | |
2283 | ||
2284 | hi = (struct elf_link_hash_entry *) | |
2285 | bfd_link_hash_lookup (info->hash, shortname, | |
2286 | FALSE, FALSE, FALSE); | |
2287 | if (hi != NULL | |
2288 | && hi->root.type == h->root.type | |
2289 | && hi->root.u.def.value == h->root.u.def.value | |
2290 | && hi->root.u.def.section == h->root.u.def.section) | |
2291 | { | |
2292 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
2293 | hi->root.type = bfd_link_hash_indirect; | |
2294 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
2295 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); | |
2296 | sym_hash = elf_sym_hashes (abfd); | |
2297 | if (sym_hash) | |
2298 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
2299 | if (sym_hash[symidx] == hi) | |
2300 | { | |
2301 | sym_hash[symidx] = h; | |
2302 | break; | |
2303 | } | |
2304 | } | |
2305 | free (shortname); | |
2306 | } | |
2307 | free (nondeflt_vers); | |
2308 | nondeflt_vers = NULL; | |
2309 | } | |
2310 | ||
6cdc0ccc AM |
2311 | if (extversym != NULL) |
2312 | { | |
2313 | free (extversym); | |
2314 | extversym = NULL; | |
2315 | } | |
2316 | ||
2317 | if (isymbuf != NULL) | |
2318 | free (isymbuf); | |
2319 | isymbuf = NULL; | |
2320 | ||
252b5132 RH |
2321 | /* Now set the weakdefs field correctly for all the weak defined |
2322 | symbols we found. The only way to do this is to search all the | |
2323 | symbols. Since we only need the information for non functions in | |
2324 | dynamic objects, that's the only time we actually put anything on | |
2325 | the list WEAKS. We need this information so that if a regular | |
2326 | object refers to a symbol defined weakly in a dynamic object, the | |
2327 | real symbol in the dynamic object is also put in the dynamic | |
2328 | symbols; we also must arrange for both symbols to point to the | |
2329 | same memory location. We could handle the general case of symbol | |
2330 | aliasing, but a general symbol alias can only be generated in | |
2331 | assembler code, handling it correctly would be very time | |
2332 | consuming, and other ELF linkers don't handle general aliasing | |
2333 | either. */ | |
2334 | while (weaks != NULL) | |
2335 | { | |
2336 | struct elf_link_hash_entry *hlook; | |
2337 | asection *slook; | |
2338 | bfd_vma vlook; | |
2339 | struct elf_link_hash_entry **hpp; | |
2340 | struct elf_link_hash_entry **hppend; | |
2341 | ||
2342 | hlook = weaks; | |
2343 | weaks = hlook->weakdef; | |
2344 | hlook->weakdef = NULL; | |
2345 | ||
2346 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
2347 | || hlook->root.type == bfd_link_hash_defweak | |
2348 | || hlook->root.type == bfd_link_hash_common | |
2349 | || hlook->root.type == bfd_link_hash_indirect); | |
2350 | slook = hlook->root.u.def.section; | |
2351 | vlook = hlook->root.u.def.value; | |
2352 | ||
2353 | hpp = elf_sym_hashes (abfd); | |
2354 | hppend = hpp + extsymcount; | |
2355 | for (; hpp < hppend; hpp++) | |
2356 | { | |
2357 | struct elf_link_hash_entry *h; | |
2358 | ||
2359 | h = *hpp; | |
2360 | if (h != NULL && h != hlook | |
2361 | && h->root.type == bfd_link_hash_defined | |
2362 | && h->root.u.def.section == slook | |
2363 | && h->root.u.def.value == vlook) | |
2364 | { | |
2365 | hlook->weakdef = h; | |
2366 | ||
2367 | /* If the weak definition is in the list of dynamic | |
2368 | symbols, make sure the real definition is put there | |
2369 | as well. */ | |
2370 | if (hlook->dynindx != -1 | |
2371 | && h->dynindx == -1) | |
2372 | { | |
2373 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
2374 | goto error_return; | |
2375 | } | |
2376 | ||
2377 | /* If the real definition is in the list of dynamic | |
c44233aa AM |
2378 | symbols, make sure the weak definition is put there |
2379 | as well. If we don't do this, then the dynamic | |
2380 | loader might not merge the entries for the real | |
2381 | definition and the weak definition. */ | |
252b5132 RH |
2382 | if (h->dynindx != -1 |
2383 | && hlook->dynindx == -1) | |
2384 | { | |
2385 | if (! _bfd_elf_link_record_dynamic_symbol (info, hlook)) | |
2386 | goto error_return; | |
2387 | } | |
252b5132 RH |
2388 | break; |
2389 | } | |
2390 | } | |
2391 | } | |
2392 | ||
252b5132 RH |
2393 | /* If this object is the same format as the output object, and it is |
2394 | not a shared library, then let the backend look through the | |
2395 | relocs. | |
2396 | ||
2397 | This is required to build global offset table entries and to | |
2398 | arrange for dynamic relocs. It is not required for the | |
2399 | particular common case of linking non PIC code, even when linking | |
2400 | against shared libraries, but unfortunately there is no way of | |
2401 | knowing whether an object file has been compiled PIC or not. | |
2402 | Looking through the relocs is not particularly time consuming. | |
2403 | The problem is that we must either (1) keep the relocs in memory, | |
2404 | which causes the linker to require additional runtime memory or | |
2405 | (2) read the relocs twice from the input file, which wastes time. | |
2406 | This would be a good case for using mmap. | |
2407 | ||
2408 | I have no idea how to handle linking PIC code into a file of a | |
2409 | different format. It probably can't be done. */ | |
2410 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
2411 | if (! dynamic | |
2412 | && abfd->xvec == info->hash->creator | |
2413 | && check_relocs != NULL) | |
2414 | { | |
2415 | asection *o; | |
2416 | ||
2417 | for (o = abfd->sections; o != NULL; o = o->next) | |
2418 | { | |
2419 | Elf_Internal_Rela *internal_relocs; | |
b34976b6 | 2420 | bfd_boolean ok; |
252b5132 RH |
2421 | |
2422 | if ((o->flags & SEC_RELOC) == 0 | |
2423 | || o->reloc_count == 0 | |
2424 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
2425 | && (o->flags & SEC_DEBUGGING) != 0) | |
2426 | || bfd_is_abs_section (o->output_section)) | |
2427 | continue; | |
2428 | ||
2429 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) | |
2430 | (abfd, o, (PTR) NULL, | |
2431 | (Elf_Internal_Rela *) NULL, | |
2432 | info->keep_memory)); | |
2433 | if (internal_relocs == NULL) | |
2434 | goto error_return; | |
2435 | ||
2436 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
2437 | ||
6cdc0ccc | 2438 | if (elf_section_data (o)->relocs != internal_relocs) |
252b5132 RH |
2439 | free (internal_relocs); |
2440 | ||
2441 | if (! ok) | |
2442 | goto error_return; | |
2443 | } | |
2444 | } | |
2445 | ||
d6fe2dc1 AM |
2446 | /* If this is a non-traditional link, try to optimize the handling |
2447 | of the .stab/.stabstr sections. */ | |
252b5132 | 2448 | if (! dynamic |
252b5132 RH |
2449 | && ! info->traditional_format |
2450 | && info->hash->creator->flavour == bfd_target_elf_flavour | |
8ea2e4bd | 2451 | && is_elf_hash_table (info) |
252b5132 RH |
2452 | && (info->strip != strip_all && info->strip != strip_debugger)) |
2453 | { | |
2454 | asection *stab, *stabstr; | |
2455 | ||
2456 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
2d653fc7 AM |
2457 | if (stab != NULL |
2458 | && (stab->flags & SEC_MERGE) == 0 | |
2459 | && !bfd_is_abs_section (stab->output_section)) | |
252b5132 RH |
2460 | { |
2461 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
2462 | ||
2463 | if (stabstr != NULL) | |
2464 | { | |
2465 | struct bfd_elf_section_data *secdata; | |
2466 | ||
2467 | secdata = elf_section_data (stab); | |
2468 | if (! _bfd_link_section_stabs (abfd, | |
8ea2e4bd | 2469 | & hash_table->stab_info, |
252b5132 | 2470 | stab, stabstr, |
65765700 | 2471 | &secdata->sec_info)) |
252b5132 | 2472 | goto error_return; |
65765700 | 2473 | if (secdata->sec_info) |
68bfbfcc | 2474 | stab->sec_info_type = ELF_INFO_TYPE_STABS; |
252b5132 RH |
2475 | } |
2476 | } | |
2477 | } | |
2478 | ||
8ea2e4bd NC |
2479 | if (! info->relocateable && ! dynamic |
2480 | && is_elf_hash_table (info)) | |
f5fa8ca2 JJ |
2481 | { |
2482 | asection *s; | |
2483 | ||
2484 | for (s = abfd->sections; s != NULL; s = s->next) | |
2d653fc7 AM |
2485 | if ((s->flags & SEC_MERGE) != 0 |
2486 | && !bfd_is_abs_section (s->output_section)) | |
65765700 JJ |
2487 | { |
2488 | struct bfd_elf_section_data *secdata; | |
2489 | ||
2490 | secdata = elf_section_data (s); | |
2491 | if (! _bfd_merge_section (abfd, | |
2492 | & hash_table->merge_info, | |
2493 | s, &secdata->sec_info)) | |
2494 | goto error_return; | |
2495 | else if (secdata->sec_info) | |
68bfbfcc | 2496 | s->sec_info_type = ELF_INFO_TYPE_MERGE; |
65765700 | 2497 | } |
f5fa8ca2 JJ |
2498 | } |
2499 | ||
f5d44ba0 AM |
2500 | if (is_elf_hash_table (info)) |
2501 | { | |
2502 | /* Add this bfd to the loaded list. */ | |
2503 | struct elf_link_loaded_list *n; | |
2504 | ||
2505 | n = ((struct elf_link_loaded_list *) | |
2506 | bfd_alloc (abfd, sizeof (struct elf_link_loaded_list))); | |
2507 | if (n == NULL) | |
2508 | goto error_return; | |
2509 | n->abfd = abfd; | |
2510 | n->next = hash_table->loaded; | |
2511 | hash_table->loaded = n; | |
2512 | } | |
2513 | ||
b34976b6 | 2514 | return TRUE; |
252b5132 | 2515 | |
6cdc0ccc | 2516 | error_free_vers: |
9147e853 JJ |
2517 | if (nondeflt_vers != NULL) |
2518 | free (nondeflt_vers); | |
252b5132 RH |
2519 | if (extversym != NULL) |
2520 | free (extversym); | |
6cdc0ccc AM |
2521 | error_free_sym: |
2522 | if (isymbuf != NULL) | |
2523 | free (isymbuf); | |
2524 | error_return: | |
b34976b6 | 2525 | return FALSE; |
252b5132 RH |
2526 | } |
2527 | ||
2528 | /* Create some sections which will be filled in with dynamic linking | |
2529 | information. ABFD is an input file which requires dynamic sections | |
2530 | to be created. The dynamic sections take up virtual memory space | |
2531 | when the final executable is run, so we need to create them before | |
2532 | addresses are assigned to the output sections. We work out the | |
2533 | actual contents and size of these sections later. */ | |
2534 | ||
b34976b6 | 2535 | bfd_boolean |
252b5132 RH |
2536 | elf_link_create_dynamic_sections (abfd, info) |
2537 | bfd *abfd; | |
2538 | struct bfd_link_info *info; | |
2539 | { | |
2540 | flagword flags; | |
2541 | register asection *s; | |
2542 | struct elf_link_hash_entry *h; | |
14a793b2 | 2543 | struct bfd_link_hash_entry *bh; |
252b5132 RH |
2544 | struct elf_backend_data *bed; |
2545 | ||
8ea2e4bd | 2546 | if (! is_elf_hash_table (info)) |
b34976b6 | 2547 | return FALSE; |
8ea2e4bd | 2548 | |
252b5132 | 2549 | if (elf_hash_table (info)->dynamic_sections_created) |
b34976b6 | 2550 | return TRUE; |
252b5132 RH |
2551 | |
2552 | /* Make sure that all dynamic sections use the same input BFD. */ | |
2553 | if (elf_hash_table (info)->dynobj == NULL) | |
2554 | elf_hash_table (info)->dynobj = abfd; | |
2555 | else | |
2556 | abfd = elf_hash_table (info)->dynobj; | |
2557 | ||
2558 | /* Note that we set the SEC_IN_MEMORY flag for all of these | |
2559 | sections. */ | |
2560 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
2561 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
2562 | ||
2563 | /* A dynamically linked executable has a .interp section, but a | |
2564 | shared library does not. */ | |
2565 | if (! info->shared) | |
2566 | { | |
2567 | s = bfd_make_section (abfd, ".interp"); | |
2568 | if (s == NULL | |
2569 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
b34976b6 | 2570 | return FALSE; |
252b5132 RH |
2571 | } |
2572 | ||
65765700 JJ |
2573 | if (! info->traditional_format |
2574 | && info->hash->creator->flavour == bfd_target_elf_flavour) | |
2575 | { | |
2576 | s = bfd_make_section (abfd, ".eh_frame_hdr"); | |
2577 | if (s == NULL | |
2578 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
2579 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
b34976b6 | 2580 | return FALSE; |
126495ed | 2581 | elf_hash_table (info)->eh_info.hdr_sec = s; |
65765700 JJ |
2582 | } |
2583 | ||
252b5132 RH |
2584 | /* Create sections to hold version informations. These are removed |
2585 | if they are not needed. */ | |
2586 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
2587 | if (s == NULL | |
2588 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
2589 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
b34976b6 | 2590 | return FALSE; |
252b5132 RH |
2591 | |
2592 | s = bfd_make_section (abfd, ".gnu.version"); | |
2593 | if (s == NULL | |
2594 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
2595 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
b34976b6 | 2596 | return FALSE; |
252b5132 RH |
2597 | |
2598 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
2599 | if (s == NULL | |
2600 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
2601 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
b34976b6 | 2602 | return FALSE; |
252b5132 RH |
2603 | |
2604 | s = bfd_make_section (abfd, ".dynsym"); | |
2605 | if (s == NULL | |
2606 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
2607 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
b34976b6 | 2608 | return FALSE; |
252b5132 RH |
2609 | |
2610 | s = bfd_make_section (abfd, ".dynstr"); | |
2611 | if (s == NULL | |
2612 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
b34976b6 | 2613 | return FALSE; |
252b5132 RH |
2614 | |
2615 | /* Create a strtab to hold the dynamic symbol names. */ | |
2616 | if (elf_hash_table (info)->dynstr == NULL) | |
2617 | { | |
2b0f7ef9 | 2618 | elf_hash_table (info)->dynstr = _bfd_elf_strtab_init (); |
252b5132 | 2619 | if (elf_hash_table (info)->dynstr == NULL) |
b34976b6 | 2620 | return FALSE; |
252b5132 RH |
2621 | } |
2622 | ||
2623 | s = bfd_make_section (abfd, ".dynamic"); | |
2624 | if (s == NULL | |
2625 | || ! bfd_set_section_flags (abfd, s, flags) | |
2626 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
b34976b6 | 2627 | return FALSE; |
252b5132 RH |
2628 | |
2629 | /* The special symbol _DYNAMIC is always set to the start of the | |
2630 | .dynamic section. This call occurs before we have processed the | |
2631 | symbols for any dynamic object, so we don't have to worry about | |
2632 | overriding a dynamic definition. We could set _DYNAMIC in a | |
2633 | linker script, but we only want to define it if we are, in fact, | |
2634 | creating a .dynamic section. We don't want to define it if there | |
2635 | is no .dynamic section, since on some ELF platforms the start up | |
2636 | code examines it to decide how to initialize the process. */ | |
14a793b2 | 2637 | bh = NULL; |
252b5132 RH |
2638 | if (! (_bfd_generic_link_add_one_symbol |
2639 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0, | |
b34976b6 AM |
2640 | (const char *) 0, FALSE, get_elf_backend_data (abfd)->collect, &bh))) |
2641 | return FALSE; | |
14a793b2 | 2642 | h = (struct elf_link_hash_entry *) bh; |
252b5132 RH |
2643 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
2644 | h->type = STT_OBJECT; | |
2645 | ||
2646 | if (info->shared | |
2647 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
b34976b6 | 2648 | return FALSE; |
252b5132 | 2649 | |
c7ac6ff8 MM |
2650 | bed = get_elf_backend_data (abfd); |
2651 | ||
252b5132 RH |
2652 | s = bfd_make_section (abfd, ".hash"); |
2653 | if (s == NULL | |
2654 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
2655 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
b34976b6 | 2656 | return FALSE; |
c7ac6ff8 | 2657 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; |
252b5132 RH |
2658 | |
2659 | /* Let the backend create the rest of the sections. This lets the | |
2660 | backend set the right flags. The backend will normally create | |
2661 | the .got and .plt sections. */ | |
252b5132 | 2662 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) |
b34976b6 | 2663 | return FALSE; |
252b5132 | 2664 | |
b34976b6 | 2665 | elf_hash_table (info)->dynamic_sections_created = TRUE; |
252b5132 | 2666 | |
b34976b6 | 2667 | return TRUE; |
252b5132 RH |
2668 | } |
2669 | ||
2670 | /* Add an entry to the .dynamic table. */ | |
2671 | ||
b34976b6 | 2672 | bfd_boolean |
252b5132 RH |
2673 | elf_add_dynamic_entry (info, tag, val) |
2674 | struct bfd_link_info *info; | |
2675 | bfd_vma tag; | |
2676 | bfd_vma val; | |
2677 | { | |
2678 | Elf_Internal_Dyn dyn; | |
2679 | bfd *dynobj; | |
2680 | asection *s; | |
dc810e39 | 2681 | bfd_size_type newsize; |
252b5132 RH |
2682 | bfd_byte *newcontents; |
2683 | ||
8ea2e4bd | 2684 | if (! is_elf_hash_table (info)) |
b34976b6 | 2685 | return FALSE; |
8ea2e4bd | 2686 | |
252b5132 RH |
2687 | dynobj = elf_hash_table (info)->dynobj; |
2688 | ||
2689 | s = bfd_get_section_by_name (dynobj, ".dynamic"); | |
2690 | BFD_ASSERT (s != NULL); | |
2691 | ||
2692 | newsize = s->_raw_size + sizeof (Elf_External_Dyn); | |
2693 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); | |
2694 | if (newcontents == NULL) | |
b34976b6 | 2695 | return FALSE; |
252b5132 RH |
2696 | |
2697 | dyn.d_tag = tag; | |
2698 | dyn.d_un.d_val = val; | |
2699 | elf_swap_dyn_out (dynobj, &dyn, | |
2700 | (Elf_External_Dyn *) (newcontents + s->_raw_size)); | |
2701 | ||
2702 | s->_raw_size = newsize; | |
2703 | s->contents = newcontents; | |
2704 | ||
b34976b6 | 2705 | return TRUE; |
252b5132 RH |
2706 | } |
2707 | \f | |
6b5bd373 MM |
2708 | /* Read and swap the relocs from the section indicated by SHDR. This |
2709 | may be either a REL or a RELA section. The relocations are | |
2710 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2711 | which should have already been allocated to contain enough space. | |
2712 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2713 | relocations should be stored. | |
2714 | ||
b34976b6 | 2715 | Returns FALSE if something goes wrong. */ |
6b5bd373 | 2716 | |
b34976b6 | 2717 | static bfd_boolean |
6b5bd373 MM |
2718 | elf_link_read_relocs_from_section (abfd, shdr, external_relocs, |
2719 | internal_relocs) | |
2720 | bfd *abfd; | |
2721 | Elf_Internal_Shdr *shdr; | |
2722 | PTR external_relocs; | |
2723 | Elf_Internal_Rela *internal_relocs; | |
2724 | { | |
c7ac6ff8 | 2725 | struct elf_backend_data *bed; |
947216bf AM |
2726 | void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *)); |
2727 | const bfd_byte *erela; | |
2728 | const bfd_byte *erelaend; | |
2729 | Elf_Internal_Rela *irela; | |
c7ac6ff8 | 2730 | |
6b5bd373 MM |
2731 | /* If there aren't any relocations, that's OK. */ |
2732 | if (!shdr) | |
b34976b6 | 2733 | return TRUE; |
6b5bd373 MM |
2734 | |
2735 | /* Position ourselves at the start of the section. */ | |
2736 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
b34976b6 | 2737 | return FALSE; |
6b5bd373 MM |
2738 | |
2739 | /* Read the relocations. */ | |
dc810e39 | 2740 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) |
b34976b6 | 2741 | return FALSE; |
6b5bd373 | 2742 | |
c7ac6ff8 MM |
2743 | bed = get_elf_backend_data (abfd); |
2744 | ||
6b5bd373 MM |
2745 | /* Convert the external relocations to the internal format. */ |
2746 | if (shdr->sh_entsize == sizeof (Elf_External_Rel)) | |
947216bf AM |
2747 | swap_in = bed->s->swap_reloc_in; |
2748 | else if (shdr->sh_entsize == sizeof (Elf_External_Rela)) | |
2749 | swap_in = bed->s->swap_reloca_in; | |
6b5bd373 | 2750 | else |
4fabe71e AM |
2751 | { |
2752 | bfd_set_error (bfd_error_wrong_format); | |
2753 | return FALSE; | |
2754 | } | |
6b5bd373 | 2755 | |
947216bf AM |
2756 | erela = external_relocs; |
2757 | erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize; | |
2758 | irela = internal_relocs; | |
2759 | while (erela < erelaend) | |
2760 | { | |
2761 | (*swap_in) (abfd, erela, irela); | |
2762 | irela += bed->s->int_rels_per_ext_rel; | |
2763 | erela += shdr->sh_entsize; | |
6b5bd373 MM |
2764 | } |
2765 | ||
b34976b6 | 2766 | return TRUE; |
6b5bd373 MM |
2767 | } |
2768 | ||
23bc299b MM |
2769 | /* Read and swap the relocs for a section O. They may have been |
2770 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2771 | not NULL, they are used as buffers to read into. They are known to | |
2772 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2773 | the return value is allocated using either malloc or bfd_alloc, | |
2774 | according to the KEEP_MEMORY argument. If O has two relocation | |
2775 | sections (both REL and RELA relocations), then the REL_HDR | |
2776 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
2777 | REL_HDR2 relocations. */ | |
252b5132 RH |
2778 | |
2779 | Elf_Internal_Rela * | |
2780 | NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs, | |
2781 | keep_memory) | |
2782 | bfd *abfd; | |
2783 | asection *o; | |
2784 | PTR external_relocs; | |
2785 | Elf_Internal_Rela *internal_relocs; | |
b34976b6 | 2786 | bfd_boolean keep_memory; |
252b5132 RH |
2787 | { |
2788 | Elf_Internal_Shdr *rel_hdr; | |
2789 | PTR alloc1 = NULL; | |
2790 | Elf_Internal_Rela *alloc2 = NULL; | |
c7ac6ff8 | 2791 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
2792 | |
2793 | if (elf_section_data (o)->relocs != NULL) | |
2794 | return elf_section_data (o)->relocs; | |
2795 | ||
2796 | if (o->reloc_count == 0) | |
2797 | return NULL; | |
2798 | ||
2799 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2800 | ||
2801 | if (internal_relocs == NULL) | |
2802 | { | |
dc810e39 | 2803 | bfd_size_type size; |
252b5132 | 2804 | |
dc810e39 AM |
2805 | size = o->reloc_count; |
2806 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
252b5132 RH |
2807 | if (keep_memory) |
2808 | internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size); | |
2809 | else | |
2810 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); | |
2811 | if (internal_relocs == NULL) | |
2812 | goto error_return; | |
2813 | } | |
2814 | ||
2815 | if (external_relocs == NULL) | |
2816 | { | |
dc810e39 | 2817 | bfd_size_type size = rel_hdr->sh_size; |
6b5bd373 MM |
2818 | |
2819 | if (elf_section_data (o)->rel_hdr2) | |
dc810e39 | 2820 | size += elf_section_data (o)->rel_hdr2->sh_size; |
6b5bd373 | 2821 | alloc1 = (PTR) bfd_malloc (size); |
252b5132 RH |
2822 | if (alloc1 == NULL) |
2823 | goto error_return; | |
2824 | external_relocs = alloc1; | |
2825 | } | |
2826 | ||
6b5bd373 MM |
2827 | if (!elf_link_read_relocs_from_section (abfd, rel_hdr, |
2828 | external_relocs, | |
2829 | internal_relocs)) | |
2830 | goto error_return; | |
3e932841 KH |
2831 | if (!elf_link_read_relocs_from_section |
2832 | (abfd, | |
6b5bd373 | 2833 | elf_section_data (o)->rel_hdr2, |
2f5116e2 | 2834 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, |
d9bc7a44 | 2835 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) |
c7ac6ff8 | 2836 | * bed->s->int_rels_per_ext_rel))) |
252b5132 | 2837 | goto error_return; |
252b5132 RH |
2838 | |
2839 | /* Cache the results for next time, if we can. */ | |
2840 | if (keep_memory) | |
2841 | elf_section_data (o)->relocs = internal_relocs; | |
2842 | ||
2843 | if (alloc1 != NULL) | |
2844 | free (alloc1); | |
2845 | ||
2846 | /* Don't free alloc2, since if it was allocated we are passing it | |
2847 | back (under the name of internal_relocs). */ | |
2848 | ||
2849 | return internal_relocs; | |
2850 | ||
2851 | error_return: | |
2852 | if (alloc1 != NULL) | |
2853 | free (alloc1); | |
2854 | if (alloc2 != NULL) | |
2855 | free (alloc2); | |
2856 | return NULL; | |
2857 | } | |
2858 | \f | |
252b5132 RH |
2859 | /* Record an assignment to a symbol made by a linker script. We need |
2860 | this in case some dynamic object refers to this symbol. */ | |
2861 | ||
b34976b6 | 2862 | bfd_boolean |
252b5132 | 2863 | NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide) |
7442e600 | 2864 | bfd *output_bfd ATTRIBUTE_UNUSED; |
252b5132 RH |
2865 | struct bfd_link_info *info; |
2866 | const char *name; | |
b34976b6 | 2867 | bfd_boolean provide; |
252b5132 RH |
2868 | { |
2869 | struct elf_link_hash_entry *h; | |
2870 | ||
2871 | if (info->hash->creator->flavour != bfd_target_elf_flavour) | |
b34976b6 | 2872 | return TRUE; |
252b5132 | 2873 | |
b34976b6 | 2874 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE); |
252b5132 | 2875 | if (h == NULL) |
b34976b6 | 2876 | return FALSE; |
252b5132 RH |
2877 | |
2878 | if (h->root.type == bfd_link_hash_new) | |
a7b97311 | 2879 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; |
252b5132 RH |
2880 | |
2881 | /* If this symbol is being provided by the linker script, and it is | |
2882 | currently defined by a dynamic object, but not by a regular | |
2883 | object, then mark it as undefined so that the generic linker will | |
2884 | force the correct value. */ | |
2885 | if (provide | |
2886 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2887 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2888 | h->root.type = bfd_link_hash_undefined; | |
2889 | ||
2890 | /* If this symbol is not being provided by the linker script, and it is | |
2891 | currently defined by a dynamic object, but not by a regular object, | |
2892 | then clear out any version information because the symbol will not be | |
2893 | associated with the dynamic object any more. */ | |
2894 | if (!provide | |
2895 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2896 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2897 | h->verinfo.verdef = NULL; | |
2898 | ||
2899 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
994819d2 | 2900 | |
252b5132 RH |
2901 | if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC |
2902 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0 | |
2903 | || info->shared) | |
2904 | && h->dynindx == -1) | |
2905 | { | |
2906 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
b34976b6 | 2907 | return FALSE; |
252b5132 RH |
2908 | |
2909 | /* If this is a weak defined symbol, and we know a corresponding | |
2910 | real symbol from the same dynamic object, make sure the real | |
2911 | symbol is also made into a dynamic symbol. */ | |
2912 | if (h->weakdef != NULL | |
2913 | && h->weakdef->dynindx == -1) | |
2914 | { | |
2915 | if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) | |
b34976b6 | 2916 | return FALSE; |
252b5132 RH |
2917 | } |
2918 | } | |
2919 | ||
b34976b6 | 2920 | return TRUE; |
252b5132 RH |
2921 | } |
2922 | \f | |
2923 | /* This structure is used to pass information to | |
2924 | elf_link_assign_sym_version. */ | |
2925 | ||
2926 | struct elf_assign_sym_version_info | |
2927 | { | |
2928 | /* Output BFD. */ | |
2929 | bfd *output_bfd; | |
2930 | /* General link information. */ | |
2931 | struct bfd_link_info *info; | |
2932 | /* Version tree. */ | |
2933 | struct bfd_elf_version_tree *verdefs; | |
252b5132 | 2934 | /* Whether we had a failure. */ |
b34976b6 | 2935 | bfd_boolean failed; |
252b5132 RH |
2936 | }; |
2937 | ||
2938 | /* This structure is used to pass information to | |
2939 | elf_link_find_version_dependencies. */ | |
2940 | ||
2941 | struct elf_find_verdep_info | |
2942 | { | |
2943 | /* Output BFD. */ | |
2944 | bfd *output_bfd; | |
2945 | /* General link information. */ | |
2946 | struct bfd_link_info *info; | |
2947 | /* The number of dependencies. */ | |
2948 | unsigned int vers; | |
2949 | /* Whether we had a failure. */ | |
b34976b6 | 2950 | bfd_boolean failed; |
252b5132 RH |
2951 | }; |
2952 | ||
2953 | /* Array used to determine the number of hash table buckets to use | |
2954 | based on the number of symbols there are. If there are fewer than | |
2955 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
2956 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
2957 | than 32771 buckets. */ | |
2958 | ||
2959 | static const size_t elf_buckets[] = | |
2960 | { | |
2961 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
2962 | 16411, 32771, 0 | |
2963 | }; | |
2964 | ||
2965 | /* Compute bucket count for hashing table. We do not use a static set | |
2966 | of possible tables sizes anymore. Instead we determine for all | |
2967 | possible reasonable sizes of the table the outcome (i.e., the | |
2968 | number of collisions etc) and choose the best solution. The | |
2969 | weighting functions are not too simple to allow the table to grow | |
2970 | without bounds. Instead one of the weighting factors is the size. | |
2971 | Therefore the result is always a good payoff between few collisions | |
2972 | (= short chain lengths) and table size. */ | |
2973 | static size_t | |
2974 | compute_bucket_count (info) | |
2975 | struct bfd_link_info *info; | |
2976 | { | |
2977 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
7442e600 | 2978 | size_t best_size = 0; |
252b5132 RH |
2979 | unsigned long int *hashcodes; |
2980 | unsigned long int *hashcodesp; | |
2981 | unsigned long int i; | |
dc810e39 | 2982 | bfd_size_type amt; |
252b5132 RH |
2983 | |
2984 | /* Compute the hash values for all exported symbols. At the same | |
2985 | time store the values in an array so that we could use them for | |
2986 | optimizations. */ | |
dc810e39 AM |
2987 | amt = dynsymcount; |
2988 | amt *= sizeof (unsigned long int); | |
2989 | hashcodes = (unsigned long int *) bfd_malloc (amt); | |
252b5132 RH |
2990 | if (hashcodes == NULL) |
2991 | return 0; | |
2992 | hashcodesp = hashcodes; | |
2993 | ||
2994 | /* Put all hash values in HASHCODES. */ | |
2995 | elf_link_hash_traverse (elf_hash_table (info), | |
2996 | elf_collect_hash_codes, &hashcodesp); | |
2997 | ||
58821868 AM |
2998 | /* We have a problem here. The following code to optimize the table |
2999 | size requires an integer type with more the 32 bits. If | |
3000 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
252b5132 | 3001 | #ifdef BFD_HOST_U_64_BIT |
82e51918 | 3002 | if (info->optimize) |
252b5132 RH |
3003 | { |
3004 | unsigned long int nsyms = hashcodesp - hashcodes; | |
3005 | size_t minsize; | |
3006 | size_t maxsize; | |
3007 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
3008 | unsigned long int *counts ; | |
3009 | ||
3010 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
3011 | that the hashing table must at least have NSYMS/4 and at most | |
3012 | 2*NSYMS buckets. */ | |
3013 | minsize = nsyms / 4; | |
3014 | if (minsize == 0) | |
3015 | minsize = 1; | |
3016 | best_size = maxsize = nsyms * 2; | |
3017 | ||
3018 | /* Create array where we count the collisions in. We must use bfd_malloc | |
3019 | since the size could be large. */ | |
dc810e39 AM |
3020 | amt = maxsize; |
3021 | amt *= sizeof (unsigned long int); | |
3022 | counts = (unsigned long int *) bfd_malloc (amt); | |
252b5132 RH |
3023 | if (counts == NULL) |
3024 | { | |
3025 | free (hashcodes); | |
3026 | return 0; | |
3027 | } | |
3028 | ||
3029 | /* Compute the "optimal" size for the hash table. The criteria is a | |
3030 | minimal chain length. The minor criteria is (of course) the size | |
3031 | of the table. */ | |
3032 | for (i = minsize; i < maxsize; ++i) | |
3033 | { | |
3034 | /* Walk through the array of hashcodes and count the collisions. */ | |
3035 | BFD_HOST_U_64_BIT max; | |
3036 | unsigned long int j; | |
3037 | unsigned long int fact; | |
3038 | ||
3039 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
3040 | ||
3041 | /* Determine how often each hash bucket is used. */ | |
3042 | for (j = 0; j < nsyms; ++j) | |
3043 | ++counts[hashcodes[j] % i]; | |
3044 | ||
3045 | /* For the weight function we need some information about the | |
3046 | pagesize on the target. This is information need not be 100% | |
3047 | accurate. Since this information is not available (so far) we | |
3048 | define it here to a reasonable default value. If it is crucial | |
3049 | to have a better value some day simply define this value. */ | |
3050 | # ifndef BFD_TARGET_PAGESIZE | |
3051 | # define BFD_TARGET_PAGESIZE (4096) | |
3052 | # endif | |
3053 | ||
3054 | /* We in any case need 2 + NSYMS entries for the size values and | |
3055 | the chains. */ | |
3056 | max = (2 + nsyms) * (ARCH_SIZE / 8); | |
3057 | ||
3058 | # if 1 | |
3059 | /* Variant 1: optimize for short chains. We add the squares | |
3060 | of all the chain lengths (which favous many small chain | |
3061 | over a few long chains). */ | |
3062 | for (j = 0; j < i; ++j) | |
3063 | max += counts[j] * counts[j]; | |
3064 | ||
3065 | /* This adds penalties for the overall size of the table. */ | |
3066 | fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1; | |
3067 | max *= fact * fact; | |
3068 | # else | |
3069 | /* Variant 2: Optimize a lot more for small table. Here we | |
3070 | also add squares of the size but we also add penalties for | |
3071 | empty slots (the +1 term). */ | |
3072 | for (j = 0; j < i; ++j) | |
3073 | max += (1 + counts[j]) * (1 + counts[j]); | |
3074 | ||
3075 | /* The overall size of the table is considered, but not as | |
3076 | strong as in variant 1, where it is squared. */ | |
3077 | fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1; | |
3078 | max *= fact; | |
3079 | # endif | |
3080 | ||
3081 | /* Compare with current best results. */ | |
3082 | if (max < best_chlen) | |
3083 | { | |
3084 | best_chlen = max; | |
3085 | best_size = i; | |
3086 | } | |
3087 | } | |
3088 | ||
3089 | free (counts); | |
3090 | } | |
3091 | else | |
3092 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
3093 | { | |
3094 | /* This is the fallback solution if no 64bit type is available or if we | |
3095 | are not supposed to spend much time on optimizations. We select the | |
3096 | bucket count using a fixed set of numbers. */ | |
3097 | for (i = 0; elf_buckets[i] != 0; i++) | |
3098 | { | |
3099 | best_size = elf_buckets[i]; | |
3100 | if (dynsymcount < elf_buckets[i + 1]) | |
3101 | break; | |
3102 | } | |
3103 | } | |
3104 | ||
3105 | /* Free the arrays we needed. */ | |
3106 | free (hashcodes); | |
3107 | ||
3108 | return best_size; | |
3109 | } | |
3110 | ||
3111 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
3112 | called by the ELF linker emulation before_allocation routine. We | |
3113 | must set the sizes of the sections before the linker sets the | |
3114 | addresses of the various sections. */ | |
3115 | ||
b34976b6 | 3116 | bfd_boolean |
252b5132 | 3117 | NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath, |
99293407 | 3118 | filter_shlib, |
252b5132 RH |
3119 | auxiliary_filters, info, sinterpptr, |
3120 | verdefs) | |
3121 | bfd *output_bfd; | |
3122 | const char *soname; | |
3123 | const char *rpath; | |
252b5132 RH |
3124 | const char *filter_shlib; |
3125 | const char * const *auxiliary_filters; | |
3126 | struct bfd_link_info *info; | |
3127 | asection **sinterpptr; | |
3128 | struct bfd_elf_version_tree *verdefs; | |
3129 | { | |
3130 | bfd_size_type soname_indx; | |
25e27870 | 3131 | bfd *dynobj; |
252b5132 | 3132 | struct elf_backend_data *bed; |
252b5132 RH |
3133 | struct elf_assign_sym_version_info asvinfo; |
3134 | ||
3135 | *sinterpptr = NULL; | |
3136 | ||
3137 | soname_indx = (bfd_size_type) -1; | |
3138 | ||
3139 | if (info->hash->creator->flavour != bfd_target_elf_flavour) | |
b34976b6 | 3140 | return TRUE; |
252b5132 | 3141 | |
8ea2e4bd | 3142 | if (! is_elf_hash_table (info)) |
b34976b6 | 3143 | return TRUE; |
8ea2e4bd | 3144 | |
51b64d56 AM |
3145 | /* Any syms created from now on start with -1 in |
3146 | got.refcount/offset and plt.refcount/offset. */ | |
5cab59f6 | 3147 | elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset; |
51b64d56 | 3148 | |
252b5132 RH |
3149 | /* The backend may have to create some sections regardless of whether |
3150 | we're dynamic or not. */ | |
3151 | bed = get_elf_backend_data (output_bfd); | |
3152 | if (bed->elf_backend_always_size_sections | |
3153 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
b34976b6 | 3154 | return FALSE; |
252b5132 RH |
3155 | |
3156 | dynobj = elf_hash_table (info)->dynobj; | |
3157 | ||
3158 | /* If there were no dynamic objects in the link, there is nothing to | |
3159 | do here. */ | |
3160 | if (dynobj == NULL) | |
b34976b6 | 3161 | return TRUE; |
252b5132 | 3162 | |
68f69152 | 3163 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
b34976b6 | 3164 | return FALSE; |
68f69152 | 3165 | |
252b5132 RH |
3166 | if (elf_hash_table (info)->dynamic_sections_created) |
3167 | { | |
3168 | struct elf_info_failed eif; | |
3169 | struct elf_link_hash_entry *h; | |
fc8c40a0 | 3170 | asection *dynstr; |
31941635 L |
3171 | struct bfd_elf_version_tree *t; |
3172 | struct bfd_elf_version_expr *d; | |
b34976b6 | 3173 | bfd_boolean all_defined; |
252b5132 RH |
3174 | |
3175 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
3176 | BFD_ASSERT (*sinterpptr != NULL || info->shared); | |
3177 | ||
3178 | if (soname != NULL) | |
3179 | { | |
2b0f7ef9 | 3180 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, |
b34976b6 | 3181 | soname, TRUE); |
252b5132 | 3182 | if (soname_indx == (bfd_size_type) -1 |
dc810e39 AM |
3183 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME, |
3184 | soname_indx)) | |
b34976b6 | 3185 | return FALSE; |
252b5132 RH |
3186 | } |
3187 | ||
3188 | if (info->symbolic) | |
3189 | { | |
dc810e39 AM |
3190 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC, |
3191 | (bfd_vma) 0)) | |
b34976b6 | 3192 | return FALSE; |
d6cf2879 | 3193 | info->flags |= DF_SYMBOLIC; |
252b5132 RH |
3194 | } |
3195 | ||
3196 | if (rpath != NULL) | |
3197 | { | |
3198 | bfd_size_type indx; | |
3199 | ||
2b0f7ef9 | 3200 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, |
b34976b6 | 3201 | TRUE); |
2b0f7ef9 JJ |
3202 | if (info->new_dtags) |
3203 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
252b5132 | 3204 | if (indx == (bfd_size_type) -1 |
dc810e39 | 3205 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx) |
c25373b7 | 3206 | || (info->new_dtags |
dc810e39 AM |
3207 | && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH, |
3208 | indx))) | |
b34976b6 | 3209 | return FALSE; |
252b5132 RH |
3210 | } |
3211 | ||
3212 | if (filter_shlib != NULL) | |
3213 | { | |
3214 | bfd_size_type indx; | |
3215 | ||
2b0f7ef9 | 3216 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, |
b34976b6 | 3217 | filter_shlib, TRUE); |
252b5132 | 3218 | if (indx == (bfd_size_type) -1 |
dc810e39 | 3219 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx)) |
b34976b6 | 3220 | return FALSE; |
252b5132 RH |
3221 | } |
3222 | ||
3223 | if (auxiliary_filters != NULL) | |
3224 | { | |
3225 | const char * const *p; | |
3226 | ||
3227 | for (p = auxiliary_filters; *p != NULL; p++) | |
3228 | { | |
3229 | bfd_size_type indx; | |
3230 | ||
2b0f7ef9 | 3231 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, |
b34976b6 | 3232 | *p, TRUE); |
252b5132 | 3233 | if (indx == (bfd_size_type) -1 |
dc810e39 AM |
3234 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY, |
3235 | indx)) | |
b34976b6 | 3236 | return FALSE; |
252b5132 RH |
3237 | } |
3238 | } | |
3239 | ||
391a809a | 3240 | eif.info = info; |
bc2b6df7 | 3241 | eif.verdefs = verdefs; |
b34976b6 | 3242 | eif.failed = FALSE; |
391a809a | 3243 | |
ea44b734 | 3244 | /* If we are supposed to export all symbols into the dynamic symbol |
c44233aa | 3245 | table (this is not the normal case), then do so. */ |
99293407 | 3246 | if (info->export_dynamic) |
ea44b734 | 3247 | { |
ea44b734 | 3248 | elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol, |
c44233aa | 3249 | (PTR) &eif); |
ea44b734 | 3250 | if (eif.failed) |
b34976b6 | 3251 | return FALSE; |
ea44b734 RH |
3252 | } |
3253 | ||
31941635 L |
3254 | /* Make all global versions with definiton. */ |
3255 | for (t = verdefs; t != NULL; t = t->next) | |
3256 | for (d = t->globals; d != NULL; d = d->next) | |
3257 | if (!d->symver && strchr (d->pattern, '*') == NULL) | |
3258 | { | |
3259 | const char *verstr, *name; | |
3260 | size_t namelen, verlen, newlen; | |
3261 | char *newname, *p; | |
3262 | struct elf_link_hash_entry *newh; | |
3263 | ||
3264 | name = d->pattern; | |
3265 | namelen = strlen (name); | |
3266 | verstr = t->name; | |
3267 | verlen = strlen (verstr); | |
b34976b6 | 3268 | newlen = namelen + verlen + 3; |
31941635 L |
3269 | |
3270 | newname = (char *) bfd_malloc ((bfd_size_type) newlen); | |
3271 | if (newname == NULL) | |
b34976b6 | 3272 | return FALSE; |
31941635 L |
3273 | memcpy (newname, name, namelen); |
3274 | ||
3275 | /* Check the hidden versioned definition. */ | |
3276 | p = newname + namelen; | |
3277 | *p++ = ELF_VER_CHR; | |
3278 | memcpy (p, verstr, verlen + 1); | |
3279 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
b34976b6 AM |
3280 | newname, FALSE, FALSE, |
3281 | FALSE); | |
31941635 L |
3282 | if (newh == NULL |
3283 | || (newh->root.type != bfd_link_hash_defined | |
3284 | && newh->root.type != bfd_link_hash_defweak)) | |
3285 | { | |
3286 | /* Check the default versioned definition. */ | |
3287 | *p++ = ELF_VER_CHR; | |
3288 | memcpy (p, verstr, verlen + 1); | |
3289 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
b34976b6 AM |
3290 | newname, FALSE, FALSE, |
3291 | FALSE); | |
31941635 L |
3292 | } |
3293 | free (newname); | |
3294 | ||
c828c771 L |
3295 | /* Mark this version if there is a definition and it is |
3296 | not defined in a shared object. */ | |
31941635 | 3297 | if (newh != NULL |
c828c771 L |
3298 | && ((newh->elf_link_hash_flags |
3299 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0) | |
31941635 L |
3300 | && (newh->root.type == bfd_link_hash_defined |
3301 | || newh->root.type == bfd_link_hash_defweak)) | |
3302 | d->symver = 1; | |
3303 | } | |
3304 | ||
252b5132 RH |
3305 | /* Attach all the symbols to their version information. */ |
3306 | asvinfo.output_bfd = output_bfd; | |
3307 | asvinfo.info = info; | |
3308 | asvinfo.verdefs = verdefs; | |
b34976b6 | 3309 | asvinfo.failed = FALSE; |
252b5132 RH |
3310 | |
3311 | elf_link_hash_traverse (elf_hash_table (info), | |
3312 | elf_link_assign_sym_version, | |
3313 | (PTR) &asvinfo); | |
3314 | if (asvinfo.failed) | |
b34976b6 | 3315 | return FALSE; |
252b5132 | 3316 | |
31941635 L |
3317 | if (!info->allow_undefined_version) |
3318 | { | |
3319 | /* Check if all global versions have a definiton. */ | |
b34976b6 | 3320 | all_defined = TRUE; |
31941635 L |
3321 | for (t = verdefs; t != NULL; t = t->next) |
3322 | for (d = t->globals; d != NULL; d = d->next) | |
3323 | if (!d->symver && !d->script | |
3324 | && strchr (d->pattern, '*') == NULL) | |
3325 | { | |
3326 | (*_bfd_error_handler) | |
3327 | (_("%s: undefined version: %s"), | |
3328 | d->pattern, t->name); | |
b34976b6 | 3329 | all_defined = FALSE; |
31941635 L |
3330 | } |
3331 | ||
3332 | if (!all_defined) | |
3333 | { | |
3334 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 3335 | return FALSE; |
31941635 L |
3336 | } |
3337 | } | |
3338 | ||
252b5132 RH |
3339 | /* Find all symbols which were defined in a dynamic object and make |
3340 | the backend pick a reasonable value for them. */ | |
252b5132 RH |
3341 | elf_link_hash_traverse (elf_hash_table (info), |
3342 | elf_adjust_dynamic_symbol, | |
3343 | (PTR) &eif); | |
3344 | if (eif.failed) | |
b34976b6 | 3345 | return FALSE; |
252b5132 RH |
3346 | |
3347 | /* Add some entries to the .dynamic section. We fill in some of the | |
3348 | values later, in elf_bfd_final_link, but we must add the entries | |
3349 | now so that we know the final size of the .dynamic section. */ | |
f0c2e336 MM |
3350 | |
3351 | /* If there are initialization and/or finalization functions to | |
3352 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
3353 | h = (info->init_function | |
3e932841 | 3354 | ? elf_link_hash_lookup (elf_hash_table (info), |
b34976b6 AM |
3355 | info->init_function, FALSE, |
3356 | FALSE, FALSE) | |
f0c2e336 | 3357 | : NULL); |
252b5132 RH |
3358 | if (h != NULL |
3359 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
3360 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
3361 | { | |
dc810e39 | 3362 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0)) |
b34976b6 | 3363 | return FALSE; |
252b5132 | 3364 | } |
f0c2e336 | 3365 | h = (info->fini_function |
3e932841 | 3366 | ? elf_link_hash_lookup (elf_hash_table (info), |
b34976b6 AM |
3367 | info->fini_function, FALSE, |
3368 | FALSE, FALSE) | |
f0c2e336 | 3369 | : NULL); |
252b5132 RH |
3370 | if (h != NULL |
3371 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
3372 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
3373 | { | |
dc810e39 | 3374 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0)) |
b34976b6 | 3375 | return FALSE; |
252b5132 | 3376 | } |
f0c2e336 | 3377 | |
fa7ea4d8 AM |
3378 | if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL) |
3379 | { | |
3380 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
3381 | if (info->shared) | |
3382 | { | |
3383 | bfd *sub; | |
3384 | asection *o; | |
3385 | ||
3386 | for (sub = info->input_bfds; sub != NULL; | |
3387 | sub = sub->link_next) | |
3388 | for (o = sub->sections; o != NULL; o = o->next) | |
3389 | if (elf_section_data (o)->this_hdr.sh_type | |
3390 | == SHT_PREINIT_ARRAY) | |
3391 | { | |
3392 | (*_bfd_error_handler) | |
3393 | (_("%s: .preinit_array section is not allowed in DSO"), | |
58821868 | 3394 | bfd_archive_filename (sub)); |
fa7ea4d8 AM |
3395 | break; |
3396 | } | |
60166579 | 3397 | |
36b4f6e7 | 3398 | bfd_set_error (bfd_error_nonrepresentable_section); |
b34976b6 | 3399 | return FALSE; |
fa7ea4d8 AM |
3400 | } |
3401 | ||
3402 | if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY, | |
3403 | (bfd_vma) 0) | |
3404 | || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ, | |
3405 | (bfd_vma) 0)) | |
b34976b6 | 3406 | return FALSE; |
fa7ea4d8 AM |
3407 | } |
3408 | if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL) | |
3409 | { | |
3410 | if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY, | |
3411 | (bfd_vma) 0) | |
3412 | || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ, | |
3413 | (bfd_vma) 0)) | |
b34976b6 | 3414 | return FALSE; |
fa7ea4d8 AM |
3415 | } |
3416 | if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL) | |
3417 | { | |
3418 | if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY, | |
3419 | (bfd_vma) 0) | |
3420 | || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ, | |
3421 | (bfd_vma) 0)) | |
b34976b6 | 3422 | return FALSE; |
fa7ea4d8 | 3423 | } |
30831527 | 3424 | |
fc8c40a0 AM |
3425 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); |
3426 | /* If .dynstr is excluded from the link, we don't want any of | |
3427 | these tags. Strictly, we should be checking each section | |
3428 | individually; This quick check covers for the case where | |
3429 | someone does a /DISCARD/ : { *(*) }. */ | |
3430 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
3431 | { | |
3432 | bfd_size_type strsize; | |
3433 | ||
2b0f7ef9 | 3434 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
dc810e39 AM |
3435 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0) |
3436 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0) | |
3437 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0) | |
3438 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize) | |
3439 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT, | |
3440 | (bfd_vma) sizeof (Elf_External_Sym))) | |
b34976b6 | 3441 | return FALSE; |
fc8c40a0 | 3442 | } |
252b5132 RH |
3443 | } |
3444 | ||
3445 | /* The backend must work out the sizes of all the other dynamic | |
3446 | sections. */ | |
252b5132 RH |
3447 | if (bed->elf_backend_size_dynamic_sections |
3448 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
b34976b6 | 3449 | return FALSE; |
252b5132 RH |
3450 | |
3451 | if (elf_hash_table (info)->dynamic_sections_created) | |
3452 | { | |
dc810e39 | 3453 | bfd_size_type dynsymcount; |
252b5132 RH |
3454 | asection *s; |
3455 | size_t bucketcount = 0; | |
c7ac6ff8 | 3456 | size_t hash_entry_size; |
db6751f2 | 3457 | unsigned int dtagcount; |
252b5132 RH |
3458 | |
3459 | /* Set up the version definition section. */ | |
3460 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3461 | BFD_ASSERT (s != NULL); | |
3462 | ||
3463 | /* We may have created additional version definitions if we are | |
c44233aa | 3464 | just linking a regular application. */ |
252b5132 RH |
3465 | verdefs = asvinfo.verdefs; |
3466 | ||
6b9b879a JJ |
3467 | /* Skip anonymous version tag. */ |
3468 | if (verdefs != NULL && verdefs->vernum == 0) | |
3469 | verdefs = verdefs->next; | |
3470 | ||
252b5132 | 3471 | if (verdefs == NULL) |
7f8d5fc9 | 3472 | _bfd_strip_section_from_output (info, s); |
252b5132 RH |
3473 | else |
3474 | { | |
3475 | unsigned int cdefs; | |
3476 | bfd_size_type size; | |
3477 | struct bfd_elf_version_tree *t; | |
3478 | bfd_byte *p; | |
3479 | Elf_Internal_Verdef def; | |
3480 | Elf_Internal_Verdaux defaux; | |
3481 | ||
252b5132 RH |
3482 | cdefs = 0; |
3483 | size = 0; | |
3484 | ||
3485 | /* Make space for the base version. */ | |
3486 | size += sizeof (Elf_External_Verdef); | |
3487 | size += sizeof (Elf_External_Verdaux); | |
3488 | ++cdefs; | |
3489 | ||
3490 | for (t = verdefs; t != NULL; t = t->next) | |
3491 | { | |
3492 | struct bfd_elf_version_deps *n; | |
3493 | ||
3494 | size += sizeof (Elf_External_Verdef); | |
3495 | size += sizeof (Elf_External_Verdaux); | |
3496 | ++cdefs; | |
3497 | ||
3498 | for (n = t->deps; n != NULL; n = n->next) | |
3499 | size += sizeof (Elf_External_Verdaux); | |
3500 | } | |
3501 | ||
3502 | s->_raw_size = size; | |
3503 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
3504 | if (s->contents == NULL && s->_raw_size != 0) | |
b34976b6 | 3505 | return FALSE; |
252b5132 RH |
3506 | |
3507 | /* Fill in the version definition section. */ | |
3508 | ||
3509 | p = s->contents; | |
3510 | ||
3511 | def.vd_version = VER_DEF_CURRENT; | |
3512 | def.vd_flags = VER_FLG_BASE; | |
3513 | def.vd_ndx = 1; | |
3514 | def.vd_cnt = 1; | |
3515 | def.vd_aux = sizeof (Elf_External_Verdef); | |
3516 | def.vd_next = (sizeof (Elf_External_Verdef) | |
3517 | + sizeof (Elf_External_Verdaux)); | |
3518 | ||
3519 | if (soname_indx != (bfd_size_type) -1) | |
3520 | { | |
2b0f7ef9 JJ |
3521 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, |
3522 | soname_indx); | |
3a99b017 | 3523 | def.vd_hash = bfd_elf_hash (soname); |
252b5132 RH |
3524 | defaux.vda_name = soname_indx; |
3525 | } | |
3526 | else | |
3527 | { | |
3528 | const char *name; | |
3529 | bfd_size_type indx; | |
3530 | ||
96fd004e | 3531 | name = basename (output_bfd->filename); |
3a99b017 | 3532 | def.vd_hash = bfd_elf_hash (name); |
2b0f7ef9 | 3533 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, |
b34976b6 | 3534 | name, FALSE); |
252b5132 | 3535 | if (indx == (bfd_size_type) -1) |
b34976b6 | 3536 | return FALSE; |
252b5132 RH |
3537 | defaux.vda_name = indx; |
3538 | } | |
3539 | defaux.vda_next = 0; | |
3540 | ||
3541 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
a7b97311 | 3542 | (Elf_External_Verdef *) p); |
252b5132 RH |
3543 | p += sizeof (Elf_External_Verdef); |
3544 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
3545 | (Elf_External_Verdaux *) p); | |
3546 | p += sizeof (Elf_External_Verdaux); | |
3547 | ||
3548 | for (t = verdefs; t != NULL; t = t->next) | |
3549 | { | |
3550 | unsigned int cdeps; | |
3551 | struct bfd_elf_version_deps *n; | |
3552 | struct elf_link_hash_entry *h; | |
14a793b2 | 3553 | struct bfd_link_hash_entry *bh; |
252b5132 RH |
3554 | |
3555 | cdeps = 0; | |
3556 | for (n = t->deps; n != NULL; n = n->next) | |
3557 | ++cdeps; | |
3558 | ||
3559 | /* Add a symbol representing this version. */ | |
14a793b2 | 3560 | bh = NULL; |
252b5132 RH |
3561 | if (! (_bfd_generic_link_add_one_symbol |
3562 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
b34976b6 | 3563 | (bfd_vma) 0, (const char *) NULL, FALSE, |
14a793b2 | 3564 | get_elf_backend_data (dynobj)->collect, &bh))) |
b34976b6 | 3565 | return FALSE; |
14a793b2 | 3566 | h = (struct elf_link_hash_entry *) bh; |
252b5132 RH |
3567 | h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF; |
3568 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
3569 | h->type = STT_OBJECT; | |
3570 | h->verinfo.vertree = t; | |
3571 | ||
3572 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
b34976b6 | 3573 | return FALSE; |
252b5132 RH |
3574 | |
3575 | def.vd_version = VER_DEF_CURRENT; | |
3576 | def.vd_flags = 0; | |
3577 | if (t->globals == NULL && t->locals == NULL && ! t->used) | |
3578 | def.vd_flags |= VER_FLG_WEAK; | |
3579 | def.vd_ndx = t->vernum + 1; | |
3580 | def.vd_cnt = cdeps + 1; | |
3a99b017 | 3581 | def.vd_hash = bfd_elf_hash (t->name); |
252b5132 RH |
3582 | def.vd_aux = sizeof (Elf_External_Verdef); |
3583 | if (t->next != NULL) | |
3584 | def.vd_next = (sizeof (Elf_External_Verdef) | |
3585 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
3586 | else | |
3587 | def.vd_next = 0; | |
3588 | ||
3589 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
3590 | (Elf_External_Verdef *) p); | |
3591 | p += sizeof (Elf_External_Verdef); | |
3592 | ||
3593 | defaux.vda_name = h->dynstr_index; | |
2b0f7ef9 JJ |
3594 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, |
3595 | h->dynstr_index); | |
252b5132 RH |
3596 | if (t->deps == NULL) |
3597 | defaux.vda_next = 0; | |
3598 | else | |
3599 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
3600 | t->name_indx = defaux.vda_name; | |
3601 | ||
3602 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
3603 | (Elf_External_Verdaux *) p); | |
3604 | p += sizeof (Elf_External_Verdaux); | |
3605 | ||
3606 | for (n = t->deps; n != NULL; n = n->next) | |
3607 | { | |
3608 | if (n->version_needed == NULL) | |
3609 | { | |
3610 | /* This can happen if there was an error in the | |
3611 | version script. */ | |
3612 | defaux.vda_name = 0; | |
3613 | } | |
3614 | else | |
2b0f7ef9 JJ |
3615 | { |
3616 | defaux.vda_name = n->version_needed->name_indx; | |
3617 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
3618 | defaux.vda_name); | |
3619 | } | |
252b5132 RH |
3620 | if (n->next == NULL) |
3621 | defaux.vda_next = 0; | |
3622 | else | |
3623 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
3624 | ||
3625 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
3626 | (Elf_External_Verdaux *) p); | |
3627 | p += sizeof (Elf_External_Verdaux); | |
3628 | } | |
3629 | } | |
3630 | ||
dc810e39 AM |
3631 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0) |
3632 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM, | |
3633 | (bfd_vma) cdefs)) | |
b34976b6 | 3634 | return FALSE; |
252b5132 RH |
3635 | |
3636 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
3637 | } | |
3638 | ||
13ae64f3 | 3639 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) |
d6cf2879 | 3640 | { |
dc810e39 | 3641 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags)) |
b34976b6 | 3642 | return FALSE; |
d6cf2879 L |
3643 | } |
3644 | ||
4d538889 | 3645 | if (info->flags_1) |
d6cf2879 L |
3646 | { |
3647 | if (! info->shared) | |
3648 | info->flags_1 &= ~ (DF_1_INITFIRST | |
3649 | | DF_1_NODELETE | |
3650 | | DF_1_NOOPEN); | |
dc810e39 AM |
3651 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1, |
3652 | info->flags_1)) | |
b34976b6 | 3653 | return FALSE; |
d6cf2879 L |
3654 | } |
3655 | ||
252b5132 RH |
3656 | /* Work out the size of the version reference section. */ |
3657 | ||
3658 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3659 | BFD_ASSERT (s != NULL); | |
3660 | { | |
3661 | struct elf_find_verdep_info sinfo; | |
3662 | ||
3663 | sinfo.output_bfd = output_bfd; | |
3664 | sinfo.info = info; | |
3665 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
3666 | if (sinfo.vers == 0) | |
3667 | sinfo.vers = 1; | |
b34976b6 | 3668 | sinfo.failed = FALSE; |
252b5132 RH |
3669 | |
3670 | elf_link_hash_traverse (elf_hash_table (info), | |
3671 | elf_link_find_version_dependencies, | |
3672 | (PTR) &sinfo); | |
3673 | ||
3674 | if (elf_tdata (output_bfd)->verref == NULL) | |
7f8d5fc9 | 3675 | _bfd_strip_section_from_output (info, s); |
252b5132 RH |
3676 | else |
3677 | { | |
3678 | Elf_Internal_Verneed *t; | |
3679 | unsigned int size; | |
3680 | unsigned int crefs; | |
3681 | bfd_byte *p; | |
3682 | ||
3683 | /* Build the version definition section. */ | |
3684 | size = 0; | |
3685 | crefs = 0; | |
3686 | for (t = elf_tdata (output_bfd)->verref; | |
3687 | t != NULL; | |
3688 | t = t->vn_nextref) | |
3689 | { | |
3690 | Elf_Internal_Vernaux *a; | |
3691 | ||
3692 | size += sizeof (Elf_External_Verneed); | |
3693 | ++crefs; | |
3694 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3695 | size += sizeof (Elf_External_Vernaux); | |
3696 | } | |
3697 | ||
3698 | s->_raw_size = size; | |
dc810e39 | 3699 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); |
252b5132 | 3700 | if (s->contents == NULL) |
b34976b6 | 3701 | return FALSE; |
252b5132 RH |
3702 | |
3703 | p = s->contents; | |
3704 | for (t = elf_tdata (output_bfd)->verref; | |
3705 | t != NULL; | |
3706 | t = t->vn_nextref) | |
3707 | { | |
3708 | unsigned int caux; | |
3709 | Elf_Internal_Vernaux *a; | |
3710 | bfd_size_type indx; | |
3711 | ||
3712 | caux = 0; | |
3713 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3714 | ++caux; | |
3715 | ||
3716 | t->vn_version = VER_NEED_CURRENT; | |
3717 | t->vn_cnt = caux; | |
2b0f7ef9 JJ |
3718 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, |
3719 | elf_dt_name (t->vn_bfd) != NULL | |
3720 | ? elf_dt_name (t->vn_bfd) | |
3721 | : basename (t->vn_bfd->filename), | |
b34976b6 | 3722 | FALSE); |
252b5132 | 3723 | if (indx == (bfd_size_type) -1) |
b34976b6 | 3724 | return FALSE; |
252b5132 RH |
3725 | t->vn_file = indx; |
3726 | t->vn_aux = sizeof (Elf_External_Verneed); | |
3727 | if (t->vn_nextref == NULL) | |
3728 | t->vn_next = 0; | |
3729 | else | |
3730 | t->vn_next = (sizeof (Elf_External_Verneed) | |
3731 | + caux * sizeof (Elf_External_Vernaux)); | |
3732 | ||
3733 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
3734 | (Elf_External_Verneed *) p); | |
3735 | p += sizeof (Elf_External_Verneed); | |
3736 | ||
3737 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3738 | { | |
3a99b017 | 3739 | a->vna_hash = bfd_elf_hash (a->vna_nodename); |
2b0f7ef9 | 3740 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, |
b34976b6 | 3741 | a->vna_nodename, FALSE); |
252b5132 | 3742 | if (indx == (bfd_size_type) -1) |
b34976b6 | 3743 | return FALSE; |
252b5132 RH |
3744 | a->vna_name = indx; |
3745 | if (a->vna_nextptr == NULL) | |
3746 | a->vna_next = 0; | |
3747 | else | |
3748 | a->vna_next = sizeof (Elf_External_Vernaux); | |
3749 | ||
3750 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
3751 | (Elf_External_Vernaux *) p); | |
3752 | p += sizeof (Elf_External_Vernaux); | |
3753 | } | |
3754 | } | |
3755 | ||
dc810e39 AM |
3756 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED, |
3757 | (bfd_vma) 0) | |
3758 | || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM, | |
3759 | (bfd_vma) crefs)) | |
b34976b6 | 3760 | return FALSE; |
252b5132 RH |
3761 | |
3762 | elf_tdata (output_bfd)->cverrefs = crefs; | |
3763 | } | |
3764 | } | |
3765 | ||
3e932841 | 3766 | /* Assign dynsym indicies. In a shared library we generate a |
30b30c21 RH |
3767 | section symbol for each output section, which come first. |
3768 | Next come all of the back-end allocated local dynamic syms, | |
3769 | followed by the rest of the global symbols. */ | |
3770 | ||
3771 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
252b5132 RH |
3772 | |
3773 | /* Work out the size of the symbol version section. */ | |
3774 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
3775 | BFD_ASSERT (s != NULL); | |
3776 | if (dynsymcount == 0 | |
3777 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL)) | |
3778 | { | |
7f8d5fc9 | 3779 | _bfd_strip_section_from_output (info, s); |
42751cf3 MM |
3780 | /* The DYNSYMCOUNT might have changed if we were going to |
3781 | output a dynamic symbol table entry for S. */ | |
30b30c21 | 3782 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); |
252b5132 RH |
3783 | } |
3784 | else | |
3785 | { | |
3786 | s->_raw_size = dynsymcount * sizeof (Elf_External_Versym); | |
3787 | s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size); | |
3788 | if (s->contents == NULL) | |
b34976b6 | 3789 | return FALSE; |
252b5132 | 3790 | |
dc810e39 | 3791 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0)) |
b34976b6 | 3792 | return FALSE; |
252b5132 RH |
3793 | } |
3794 | ||
3795 | /* Set the size of the .dynsym and .hash sections. We counted | |
3796 | the number of dynamic symbols in elf_link_add_object_symbols. | |
3797 | We will build the contents of .dynsym and .hash when we build | |
3798 | the final symbol table, because until then we do not know the | |
3799 | correct value to give the symbols. We built the .dynstr | |
3800 | section as we went along in elf_link_add_object_symbols. */ | |
3801 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
3802 | BFD_ASSERT (s != NULL); | |
3803 | s->_raw_size = dynsymcount * sizeof (Elf_External_Sym); | |
3804 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
3805 | if (s->contents == NULL && s->_raw_size != 0) | |
b34976b6 | 3806 | return FALSE; |
252b5132 | 3807 | |
fc8c40a0 AM |
3808 | if (dynsymcount != 0) |
3809 | { | |
3810 | Elf_Internal_Sym isym; | |
3811 | ||
3812 | /* The first entry in .dynsym is a dummy symbol. */ | |
3813 | isym.st_value = 0; | |
3814 | isym.st_size = 0; | |
3815 | isym.st_name = 0; | |
3816 | isym.st_info = 0; | |
3817 | isym.st_other = 0; | |
3818 | isym.st_shndx = 0; | |
9ad5cbcf | 3819 | elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0); |
fc8c40a0 | 3820 | } |
252b5132 RH |
3821 | |
3822 | /* Compute the size of the hashing table. As a side effect this | |
3823 | computes the hash values for all the names we export. */ | |
3824 | bucketcount = compute_bucket_count (info); | |
3825 | ||
3826 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
3827 | BFD_ASSERT (s != NULL); | |
c7ac6ff8 MM |
3828 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; |
3829 | s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
1126897b | 3830 | s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size); |
252b5132 | 3831 | if (s->contents == NULL) |
b34976b6 | 3832 | return FALSE; |
252b5132 | 3833 | |
dc810e39 AM |
3834 | bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount, |
3835 | s->contents); | |
3836 | bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount, | |
c7ac6ff8 | 3837 | s->contents + hash_entry_size); |
252b5132 RH |
3838 | |
3839 | elf_hash_table (info)->bucketcount = bucketcount; | |
3840 | ||
3841 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
3842 | BFD_ASSERT (s != NULL); | |
2b0f7ef9 JJ |
3843 | |
3844 | elf_finalize_dynstr (output_bfd, info); | |
3845 | ||
3846 | s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
252b5132 | 3847 | |
db6751f2 | 3848 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) |
dc810e39 | 3849 | if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0)) |
b34976b6 | 3850 | return FALSE; |
252b5132 RH |
3851 | } |
3852 | ||
b34976b6 | 3853 | return TRUE; |
252b5132 RH |
3854 | } |
3855 | \f | |
2b0f7ef9 JJ |
3856 | /* This function is used to adjust offsets into .dynstr for |
3857 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
c44233aa | 3858 | |
b34976b6 AM |
3859 | static bfd_boolean elf_adjust_dynstr_offsets |
3860 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
c44233aa | 3861 | |
b34976b6 | 3862 | static bfd_boolean |
2b0f7ef9 JJ |
3863 | elf_adjust_dynstr_offsets (h, data) |
3864 | struct elf_link_hash_entry *h; | |
3865 | PTR data; | |
3866 | { | |
3867 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; | |
3868 | ||
e92d460e AM |
3869 | if (h->root.type == bfd_link_hash_warning) |
3870 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3871 | ||
2b0f7ef9 JJ |
3872 | if (h->dynindx != -1) |
3873 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
b34976b6 | 3874 | return TRUE; |
2b0f7ef9 JJ |
3875 | } |
3876 | ||
3877 | /* Assign string offsets in .dynstr, update all structures referencing | |
3878 | them. */ | |
3879 | ||
b34976b6 | 3880 | static bfd_boolean |
2b0f7ef9 JJ |
3881 | elf_finalize_dynstr (output_bfd, info) |
3882 | bfd *output_bfd; | |
3883 | struct bfd_link_info *info; | |
3884 | { | |
3885 | struct elf_link_local_dynamic_entry *entry; | |
3886 | struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr; | |
3887 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
3888 | asection *sdyn; | |
3889 | bfd_size_type size; | |
3890 | Elf_External_Dyn *dyncon, *dynconend; | |
3891 | ||
3892 | _bfd_elf_strtab_finalize (dynstr); | |
3893 | size = _bfd_elf_strtab_size (dynstr); | |
3894 | ||
3895 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3896 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3897 | BFD_ASSERT (sdyn != NULL); | |
3898 | ||
3899 | dyncon = (Elf_External_Dyn *) sdyn->contents; | |
3900 | dynconend = (Elf_External_Dyn *) (sdyn->contents + | |
3901 | sdyn->_raw_size); | |
3902 | for (; dyncon < dynconend; dyncon++) | |
3903 | { | |
3904 | Elf_Internal_Dyn dyn; | |
3905 | ||
3906 | elf_swap_dyn_in (dynobj, dyncon, & dyn); | |
3907 | switch (dyn.d_tag) | |
3908 | { | |
3909 | case DT_STRSZ: | |
3910 | dyn.d_un.d_val = size; | |
3911 | elf_swap_dyn_out (dynobj, & dyn, dyncon); | |
3912 | break; | |
3913 | case DT_NEEDED: | |
3914 | case DT_SONAME: | |
3915 | case DT_RPATH: | |
3916 | case DT_RUNPATH: | |
3917 | case DT_FILTER: | |
3918 | case DT_AUXILIARY: | |
3919 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3920 | elf_swap_dyn_out (dynobj, & dyn, dyncon); | |
3921 | break; | |
3922 | default: | |
3923 | break; | |
3924 | } | |
3925 | } | |
3926 | ||
3927 | /* Now update local dynamic symbols. */ | |
3928 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
3929 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3930 | entry->isym.st_name); | |
3931 | ||
3932 | /* And the rest of dynamic symbols. */ | |
3933 | elf_link_hash_traverse (elf_hash_table (info), | |
3934 | elf_adjust_dynstr_offsets, dynstr); | |
3935 | ||
3936 | /* Adjust version definitions. */ | |
3937 | if (elf_tdata (output_bfd)->cverdefs) | |
3938 | { | |
3939 | asection *s; | |
3940 | bfd_byte *p; | |
3941 | bfd_size_type i; | |
3942 | Elf_Internal_Verdef def; | |
3943 | Elf_Internal_Verdaux defaux; | |
c44233aa | 3944 | |
2b0f7ef9 JJ |
3945 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); |
3946 | p = (bfd_byte *) s->contents; | |
3947 | do | |
3948 | { | |
3949 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3950 | &def); | |
3951 | p += sizeof (Elf_External_Verdef); | |
3952 | for (i = 0; i < def.vd_cnt; ++i) | |
3953 | { | |
3954 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3955 | (Elf_External_Verdaux *) p, &defaux); | |
3956 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3957 | defaux.vda_name); | |
3958 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3959 | &defaux, (Elf_External_Verdaux *) p); | |
3960 | p += sizeof (Elf_External_Verdaux); | |
3961 | } | |
3962 | } | |
3963 | while (def.vd_next); | |
3964 | } | |
3965 | ||
3966 | /* Adjust version references. */ | |
3967 | if (elf_tdata (output_bfd)->verref) | |
3968 | { | |
3969 | asection *s; | |
3970 | bfd_byte *p; | |
3971 | bfd_size_type i; | |
3972 | Elf_Internal_Verneed need; | |
3973 | Elf_Internal_Vernaux needaux; | |
c44233aa | 3974 | |
2b0f7ef9 JJ |
3975 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); |
3976 | p = (bfd_byte *) s->contents; | |
3977 | do | |
3978 | { | |
3979 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3980 | &need); | |
3981 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3982 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3983 | (Elf_External_Verneed *) p); | |
3984 | p += sizeof (Elf_External_Verneed); | |
3985 | for (i = 0; i < need.vn_cnt; ++i) | |
3986 | { | |
3987 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3988 | (Elf_External_Vernaux *) p, &needaux); | |
3989 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3990 | needaux.vna_name); | |
3991 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3992 | &needaux, | |
3993 | (Elf_External_Vernaux *) p); | |
3994 | p += sizeof (Elf_External_Vernaux); | |
3995 | } | |
3996 | } | |
3997 | while (need.vn_next); | |
3998 | } | |
3999 | ||
b34976b6 | 4000 | return TRUE; |
2b0f7ef9 JJ |
4001 | } |
4002 | ||
252b5132 RH |
4003 | /* Fix up the flags for a symbol. This handles various cases which |
4004 | can only be fixed after all the input files are seen. This is | |
4005 | currently called by both adjust_dynamic_symbol and | |
4006 | assign_sym_version, which is unnecessary but perhaps more robust in | |
4007 | the face of future changes. */ | |
4008 | ||
b34976b6 | 4009 | static bfd_boolean |
252b5132 RH |
4010 | elf_fix_symbol_flags (h, eif) |
4011 | struct elf_link_hash_entry *h; | |
4012 | struct elf_info_failed *eif; | |
4013 | { | |
4014 | /* If this symbol was mentioned in a non-ELF file, try to set | |
4015 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
4016 | permit a non-ELF file to correctly refer to a symbol defined in | |
4017 | an ELF dynamic object. */ | |
4018 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0) | |
4019 | { | |
94b6c40a L |
4020 | while (h->root.type == bfd_link_hash_indirect) |
4021 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4022 | ||
252b5132 RH |
4023 | if (h->root.type != bfd_link_hash_defined |
4024 | && h->root.type != bfd_link_hash_defweak) | |
4025 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR | |
4026 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); | |
4027 | else | |
4028 | { | |
4029 | if (h->root.u.def.section->owner != NULL | |
4030 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
4031 | == bfd_target_elf_flavour)) | |
4032 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR | |
4033 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); | |
4034 | else | |
4035 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
4036 | } | |
4037 | ||
4038 | if (h->dynindx == -1 | |
4039 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
4040 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
4041 | { | |
4042 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
4043 | { | |
b34976b6 AM |
4044 | eif->failed = TRUE; |
4045 | return FALSE; | |
252b5132 RH |
4046 | } |
4047 | } | |
4048 | } | |
4049 | else | |
4050 | { | |
4051 | /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol | |
c44233aa AM |
4052 | was first seen in a non-ELF file. Fortunately, if the symbol |
4053 | was first seen in an ELF file, we're probably OK unless the | |
4054 | symbol was defined in a non-ELF file. Catch that case here. | |
4055 | FIXME: We're still in trouble if the symbol was first seen in | |
4056 | a dynamic object, and then later in a non-ELF regular object. */ | |
252b5132 RH |
4057 | if ((h->root.type == bfd_link_hash_defined |
4058 | || h->root.type == bfd_link_hash_defweak) | |
4059 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
4060 | && (h->root.u.def.section->owner != NULL | |
4061 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
4062 | != bfd_target_elf_flavour) | |
4063 | : (bfd_is_abs_section (h->root.u.def.section) | |
4064 | && (h->elf_link_hash_flags | |
4065 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0))) | |
4066 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
4067 | } | |
4068 | ||
4069 | /* If this is a final link, and the symbol was defined as a common | |
4070 | symbol in a regular object file, and there was no definition in | |
4071 | any dynamic object, then the linker will have allocated space for | |
4072 | the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR | |
4073 | flag will not have been set. */ | |
4074 | if (h->root.type == bfd_link_hash_defined | |
4075 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
4076 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
4077 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
4078 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
4079 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
4080 | ||
4081 | /* If -Bsymbolic was used (which means to bind references to global | |
4082 | symbols to the definition within the shared object), and this | |
4083 | symbol was defined in a regular object, then it actually doesn't | |
d954b040 HPN |
4084 | need a PLT entry, and we can accomplish that by forcing it local. |
4085 | Likewise, if the symbol has hidden or internal visibility. | |
4086 | FIXME: It might be that we also do not need a PLT for other | |
4087 | non-hidden visibilities, but we would have to tell that to the | |
4088 | backend specifically; we can't just clear PLT-related data here. */ | |
252b5132 RH |
4089 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0 |
4090 | && eif->info->shared | |
8ea2e4bd | 4091 | && is_elf_hash_table (eif->info) |
d954b040 HPN |
4092 | && (eif->info->symbolic |
4093 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
4094 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) | |
252b5132 RH |
4095 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) |
4096 | { | |
391a809a | 4097 | struct elf_backend_data *bed; |
b34976b6 | 4098 | bfd_boolean force_local; |
8ea2e4bd | 4099 | |
391a809a | 4100 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
e5094212 AM |
4101 | |
4102 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
4103 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
4104 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
252b5132 RH |
4105 | } |
4106 | ||
fc4cc5bb ILT |
4107 | /* If this is a weak defined symbol in a dynamic object, and we know |
4108 | the real definition in the dynamic object, copy interesting flags | |
4109 | over to the real definition. */ | |
4110 | if (h->weakdef != NULL) | |
4111 | { | |
4112 | struct elf_link_hash_entry *weakdef; | |
4113 | ||
8e67855b NC |
4114 | weakdef = h->weakdef; |
4115 | if (h->root.type == bfd_link_hash_indirect) | |
4116 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4117 | ||
fc4cc5bb ILT |
4118 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4119 | || h->root.type == bfd_link_hash_defweak); | |
fc4cc5bb ILT |
4120 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined |
4121 | || weakdef->root.type == bfd_link_hash_defweak); | |
4122 | BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC); | |
4123 | ||
4124 | /* If the real definition is defined by a regular object file, | |
4125 | don't do anything special. See the longer description in | |
4126 | elf_adjust_dynamic_symbol, below. */ | |
4127 | if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
4128 | h->weakdef = NULL; | |
4129 | else | |
0a991dfe AM |
4130 | { |
4131 | struct elf_backend_data *bed; | |
4132 | ||
4133 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
b48fa14c | 4134 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); |
0a991dfe | 4135 | } |
fc4cc5bb ILT |
4136 | } |
4137 | ||
b34976b6 | 4138 | return TRUE; |
252b5132 RH |
4139 | } |
4140 | ||
4141 | /* Make the backend pick a good value for a dynamic symbol. This is | |
4142 | called via elf_link_hash_traverse, and also calls itself | |
4143 | recursively. */ | |
4144 | ||
b34976b6 | 4145 | static bfd_boolean |
252b5132 RH |
4146 | elf_adjust_dynamic_symbol (h, data) |
4147 | struct elf_link_hash_entry *h; | |
4148 | PTR data; | |
4149 | { | |
4150 | struct elf_info_failed *eif = (struct elf_info_failed *) data; | |
4151 | bfd *dynobj; | |
4152 | struct elf_backend_data *bed; | |
4153 | ||
5cab59f6 AM |
4154 | if (! is_elf_hash_table (eif->info)) |
4155 | return FALSE; | |
4156 | ||
e92d460e AM |
4157 | if (h->root.type == bfd_link_hash_warning) |
4158 | { | |
5cab59f6 AM |
4159 | h->plt = elf_hash_table (eif->info)->init_offset; |
4160 | h->got = elf_hash_table (eif->info)->init_offset; | |
e92d460e AM |
4161 | |
4162 | /* When warning symbols are created, they **replace** the "real" | |
4163 | entry in the hash table, thus we never get to see the real | |
4164 | symbol in a hash traversal. So look at it now. */ | |
4165 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4166 | } | |
4167 | ||
252b5132 RH |
4168 | /* Ignore indirect symbols. These are added by the versioning code. */ |
4169 | if (h->root.type == bfd_link_hash_indirect) | |
b34976b6 | 4170 | return TRUE; |
252b5132 RH |
4171 | |
4172 | /* Fix the symbol flags. */ | |
4173 | if (! elf_fix_symbol_flags (h, eif)) | |
b34976b6 | 4174 | return FALSE; |
252b5132 RH |
4175 | |
4176 | /* If this symbol does not require a PLT entry, and it is not | |
4177 | defined by a dynamic object, or is not referenced by a regular | |
4178 | object, ignore it. We do have to handle a weak defined symbol, | |
4179 | even if no regular object refers to it, if we decided to add it | |
4180 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
4181 | about symbols which are defined by one dynamic object and | |
4182 | referenced by another one? */ | |
4183 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0 | |
4184 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
4185 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
4186 | || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 | |
4187 | && (h->weakdef == NULL || h->weakdef->dynindx == -1)))) | |
4188 | { | |
5cab59f6 | 4189 | h->plt = elf_hash_table (eif->info)->init_offset; |
b34976b6 | 4190 | return TRUE; |
252b5132 RH |
4191 | } |
4192 | ||
4193 | /* If we've already adjusted this symbol, don't do it again. This | |
4194 | can happen via a recursive call. */ | |
4195 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0) | |
b34976b6 | 4196 | return TRUE; |
252b5132 RH |
4197 | |
4198 | /* Don't look at this symbol again. Note that we must set this | |
4199 | after checking the above conditions, because we may look at a | |
4200 | symbol once, decide not to do anything, and then get called | |
4201 | recursively later after REF_REGULAR is set below. */ | |
4202 | h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED; | |
4203 | ||
4204 | /* If this is a weak definition, and we know a real definition, and | |
4205 | the real symbol is not itself defined by a regular object file, | |
4206 | then get a good value for the real definition. We handle the | |
4207 | real symbol first, for the convenience of the backend routine. | |
4208 | ||
4209 | Note that there is a confusing case here. If the real definition | |
4210 | is defined by a regular object file, we don't get the real symbol | |
4211 | from the dynamic object, but we do get the weak symbol. If the | |
4212 | processor backend uses a COPY reloc, then if some routine in the | |
4213 | dynamic object changes the real symbol, we will not see that | |
4214 | change in the corresponding weak symbol. This is the way other | |
4215 | ELF linkers work as well, and seems to be a result of the shared | |
4216 | library model. | |
4217 | ||
4218 | I will clarify this issue. Most SVR4 shared libraries define the | |
4219 | variable _timezone and define timezone as a weak synonym. The | |
4220 | tzset call changes _timezone. If you write | |
4221 | extern int timezone; | |
4222 | int _timezone = 5; | |
4223 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
4224 | you might expect that, since timezone is a synonym for _timezone, | |
4225 | the same number will print both times. However, if the processor | |
4226 | backend uses a COPY reloc, then actually timezone will be copied | |
4227 | into your process image, and, since you define _timezone | |
4228 | yourself, _timezone will not. Thus timezone and _timezone will | |
4229 | wind up at different memory locations. The tzset call will set | |
4230 | _timezone, leaving timezone unchanged. */ | |
4231 | ||
4232 | if (h->weakdef != NULL) | |
4233 | { | |
fc4cc5bb ILT |
4234 | /* If we get to this point, we know there is an implicit |
4235 | reference by a regular object file via the weak symbol H. | |
4236 | FIXME: Is this really true? What if the traversal finds | |
4237 | H->WEAKDEF before it finds H? */ | |
4238 | h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
252b5132 | 4239 | |
fc4cc5bb | 4240 | if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif)) |
b34976b6 | 4241 | return FALSE; |
252b5132 RH |
4242 | } |
4243 | ||
4244 | /* If a symbol has no type and no size and does not require a PLT | |
4245 | entry, then we are probably about to do the wrong thing here: we | |
4246 | are probably going to create a COPY reloc for an empty object. | |
4247 | This case can arise when a shared object is built with assembly | |
4248 | code, and the assembly code fails to set the symbol type. */ | |
4249 | if (h->size == 0 | |
4250 | && h->type == STT_NOTYPE | |
4251 | && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0) | |
4252 | (*_bfd_error_handler) | |
4253 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
58821868 | 4254 | h->root.root.string); |
252b5132 RH |
4255 | |
4256 | dynobj = elf_hash_table (eif->info)->dynobj; | |
4257 | bed = get_elf_backend_data (dynobj); | |
4258 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
4259 | { | |
b34976b6 AM |
4260 | eif->failed = TRUE; |
4261 | return FALSE; | |
252b5132 RH |
4262 | } |
4263 | ||
b34976b6 | 4264 | return TRUE; |
252b5132 RH |
4265 | } |
4266 | \f | |
4267 | /* This routine is used to export all defined symbols into the dynamic | |
4268 | symbol table. It is called via elf_link_hash_traverse. */ | |
4269 | ||
b34976b6 | 4270 | static bfd_boolean |
252b5132 RH |
4271 | elf_export_symbol (h, data) |
4272 | struct elf_link_hash_entry *h; | |
4273 | PTR data; | |
4274 | { | |
4275 | struct elf_info_failed *eif = (struct elf_info_failed *) data; | |
4276 | ||
4277 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4278 | if (h->root.type == bfd_link_hash_indirect) | |
b34976b6 | 4279 | return TRUE; |
252b5132 | 4280 | |
e92d460e AM |
4281 | if (h->root.type == bfd_link_hash_warning) |
4282 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4283 | ||
252b5132 RH |
4284 | if (h->dynindx == -1 |
4285 | && (h->elf_link_hash_flags | |
4286 | & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0) | |
4287 | { | |
bc2b6df7 L |
4288 | struct bfd_elf_version_tree *t; |
4289 | struct bfd_elf_version_expr *d; | |
4290 | ||
4291 | for (t = eif->verdefs; t != NULL; t = t->next) | |
252b5132 | 4292 | { |
bc2b6df7 L |
4293 | if (t->globals != NULL) |
4294 | { | |
4295 | for (d = t->globals; d != NULL; d = d->next) | |
4296 | { | |
4297 | if ((*d->match) (d, h->root.root.string)) | |
4298 | goto doit; | |
4299 | } | |
4300 | } | |
4301 | ||
4302 | if (t->locals != NULL) | |
4303 | { | |
4304 | for (d = t->locals ; d != NULL; d = d->next) | |
4305 | { | |
4306 | if ((*d->match) (d, h->root.root.string)) | |
b34976b6 | 4307 | return TRUE; |
bc2b6df7 L |
4308 | } |
4309 | } | |
252b5132 | 4310 | } |
bc2b6df7 L |
4311 | |
4312 | if (!eif->verdefs) | |
c44233aa | 4313 | { |
58821868 | 4314 | doit: |
bc2b6df7 L |
4315 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
4316 | { | |
b34976b6 AM |
4317 | eif->failed = TRUE; |
4318 | return FALSE; | |
bc2b6df7 | 4319 | } |
c44233aa | 4320 | } |
252b5132 RH |
4321 | } |
4322 | ||
b34976b6 | 4323 | return TRUE; |
252b5132 RH |
4324 | } |
4325 | \f | |
4326 | /* Look through the symbols which are defined in other shared | |
4327 | libraries and referenced here. Update the list of version | |
4328 | dependencies. This will be put into the .gnu.version_r section. | |
4329 | This function is called via elf_link_hash_traverse. */ | |
4330 | ||
b34976b6 | 4331 | static bfd_boolean |
252b5132 RH |
4332 | elf_link_find_version_dependencies (h, data) |
4333 | struct elf_link_hash_entry *h; | |
4334 | PTR data; | |
4335 | { | |
4336 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; | |
4337 | Elf_Internal_Verneed *t; | |
4338 | Elf_Internal_Vernaux *a; | |
dc810e39 | 4339 | bfd_size_type amt; |
252b5132 | 4340 | |
e92d460e AM |
4341 | if (h->root.type == bfd_link_hash_warning) |
4342 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4343 | ||
252b5132 RH |
4344 | /* We only care about symbols defined in shared objects with version |
4345 | information. */ | |
4346 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
4347 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
4348 | || h->dynindx == -1 | |
4349 | || h->verinfo.verdef == NULL) | |
b34976b6 | 4350 | return TRUE; |
252b5132 RH |
4351 | |
4352 | /* See if we already know about this version. */ | |
4353 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
4354 | { | |
4355 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
4356 | continue; | |
4357 | ||
4358 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
4359 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
b34976b6 | 4360 | return TRUE; |
252b5132 RH |
4361 | |
4362 | break; | |
4363 | } | |
4364 | ||
4365 | /* This is a new version. Add it to tree we are building. */ | |
4366 | ||
4367 | if (t == NULL) | |
4368 | { | |
dc810e39 AM |
4369 | amt = sizeof *t; |
4370 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt); | |
252b5132 RH |
4371 | if (t == NULL) |
4372 | { | |
b34976b6 AM |
4373 | rinfo->failed = TRUE; |
4374 | return FALSE; | |
252b5132 RH |
4375 | } |
4376 | ||
4377 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
4378 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
4379 | elf_tdata (rinfo->output_bfd)->verref = t; | |
4380 | } | |
4381 | ||
dc810e39 AM |
4382 | amt = sizeof *a; |
4383 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt); | |
252b5132 RH |
4384 | |
4385 | /* Note that we are copying a string pointer here, and testing it | |
4386 | above. If bfd_elf_string_from_elf_section is ever changed to | |
4387 | discard the string data when low in memory, this will have to be | |
4388 | fixed. */ | |
4389 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
4390 | ||
4391 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
4392 | a->vna_nextptr = t->vn_auxptr; | |
4393 | ||
4394 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
4395 | ++rinfo->vers; | |
4396 | ||
4397 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
4398 | ||
4399 | t->vn_auxptr = a; | |
4400 | ||
b34976b6 | 4401 | return TRUE; |
252b5132 RH |
4402 | } |
4403 | ||
4404 | /* Figure out appropriate versions for all the symbols. We may not | |
4405 | have the version number script until we have read all of the input | |
4406 | files, so until that point we don't know which symbols should be | |
4407 | local. This function is called via elf_link_hash_traverse. */ | |
4408 | ||
b34976b6 | 4409 | static bfd_boolean |
252b5132 RH |
4410 | elf_link_assign_sym_version (h, data) |
4411 | struct elf_link_hash_entry *h; | |
4412 | PTR data; | |
4413 | { | |
dc810e39 AM |
4414 | struct elf_assign_sym_version_info *sinfo; |
4415 | struct bfd_link_info *info; | |
c61b8717 | 4416 | struct elf_backend_data *bed; |
252b5132 RH |
4417 | struct elf_info_failed eif; |
4418 | char *p; | |
dc810e39 AM |
4419 | bfd_size_type amt; |
4420 | ||
4421 | sinfo = (struct elf_assign_sym_version_info *) data; | |
4422 | info = sinfo->info; | |
252b5132 | 4423 | |
e92d460e AM |
4424 | if (h->root.type == bfd_link_hash_warning) |
4425 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4426 | ||
252b5132 | 4427 | /* Fix the symbol flags. */ |
b34976b6 | 4428 | eif.failed = FALSE; |
252b5132 RH |
4429 | eif.info = info; |
4430 | if (! elf_fix_symbol_flags (h, &eif)) | |
4431 | { | |
4432 | if (eif.failed) | |
b34976b6 AM |
4433 | sinfo->failed = TRUE; |
4434 | return FALSE; | |
252b5132 RH |
4435 | } |
4436 | ||
4437 | /* We only need version numbers for symbols defined in regular | |
4438 | objects. */ | |
4439 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
b34976b6 | 4440 | return TRUE; |
252b5132 | 4441 | |
c61b8717 | 4442 | bed = get_elf_backend_data (sinfo->output_bfd); |
252b5132 RH |
4443 | p = strchr (h->root.root.string, ELF_VER_CHR); |
4444 | if (p != NULL && h->verinfo.vertree == NULL) | |
4445 | { | |
4446 | struct bfd_elf_version_tree *t; | |
b34976b6 | 4447 | bfd_boolean hidden; |
252b5132 | 4448 | |
b34976b6 | 4449 | hidden = TRUE; |
252b5132 RH |
4450 | |
4451 | /* There are two consecutive ELF_VER_CHR characters if this is | |
c44233aa | 4452 | not a hidden symbol. */ |
252b5132 RH |
4453 | ++p; |
4454 | if (*p == ELF_VER_CHR) | |
4455 | { | |
b34976b6 | 4456 | hidden = FALSE; |
252b5132 RH |
4457 | ++p; |
4458 | } | |
4459 | ||
4460 | /* If there is no version string, we can just return out. */ | |
4461 | if (*p == '\0') | |
4462 | { | |
4463 | if (hidden) | |
4464 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
b34976b6 | 4465 | return TRUE; |
252b5132 RH |
4466 | } |
4467 | ||
4468 | /* Look for the version. If we find it, it is no longer weak. */ | |
4469 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
4470 | { | |
4471 | if (strcmp (t->name, p) == 0) | |
4472 | { | |
dc810e39 | 4473 | size_t len; |
252b5132 RH |
4474 | char *alc; |
4475 | struct bfd_elf_version_expr *d; | |
4476 | ||
4477 | len = p - h->root.root.string; | |
e5094212 | 4478 | alc = bfd_malloc ((bfd_size_type) len); |
252b5132 | 4479 | if (alc == NULL) |
b34976b6 | 4480 | return FALSE; |
d4c88bbb | 4481 | memcpy (alc, h->root.root.string, len - 1); |
252b5132 RH |
4482 | alc[len - 1] = '\0'; |
4483 | if (alc[len - 2] == ELF_VER_CHR) | |
c44233aa | 4484 | alc[len - 2] = '\0'; |
252b5132 RH |
4485 | |
4486 | h->verinfo.vertree = t; | |
b34976b6 | 4487 | t->used = TRUE; |
252b5132 RH |
4488 | d = NULL; |
4489 | ||
4490 | if (t->globals != NULL) | |
4491 | { | |
4492 | for (d = t->globals; d != NULL; d = d->next) | |
4493 | if ((*d->match) (d, alc)) | |
4494 | break; | |
4495 | } | |
4496 | ||
4497 | /* See if there is anything to force this symbol to | |
c44233aa | 4498 | local scope. */ |
252b5132 RH |
4499 | if (d == NULL && t->locals != NULL) |
4500 | { | |
4501 | for (d = t->locals; d != NULL; d = d->next) | |
4502 | { | |
4503 | if ((*d->match) (d, alc)) | |
4504 | { | |
4505 | if (h->dynindx != -1 | |
4506 | && info->shared | |
99293407 | 4507 | && ! info->export_dynamic) |
252b5132 | 4508 | { |
b34976b6 | 4509 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
252b5132 RH |
4510 | } |
4511 | ||
4512 | break; | |
4513 | } | |
4514 | } | |
4515 | } | |
4516 | ||
e5094212 | 4517 | free (alc); |
252b5132 RH |
4518 | break; |
4519 | } | |
4520 | } | |
4521 | ||
4522 | /* If we are building an application, we need to create a | |
c44233aa | 4523 | version node for this version. */ |
252b5132 RH |
4524 | if (t == NULL && ! info->shared) |
4525 | { | |
4526 | struct bfd_elf_version_tree **pp; | |
4527 | int version_index; | |
4528 | ||
4529 | /* If we aren't going to export this symbol, we don't need | |
c44233aa | 4530 | to worry about it. */ |
252b5132 | 4531 | if (h->dynindx == -1) |
b34976b6 | 4532 | return TRUE; |
252b5132 | 4533 | |
dc810e39 | 4534 | amt = sizeof *t; |
252b5132 | 4535 | t = ((struct bfd_elf_version_tree *) |
dc810e39 | 4536 | bfd_alloc (sinfo->output_bfd, amt)); |
252b5132 RH |
4537 | if (t == NULL) |
4538 | { | |
b34976b6 AM |
4539 | sinfo->failed = TRUE; |
4540 | return FALSE; | |
252b5132 RH |
4541 | } |
4542 | ||
4543 | t->next = NULL; | |
4544 | t->name = p; | |
4545 | t->globals = NULL; | |
4546 | t->locals = NULL; | |
4547 | t->deps = NULL; | |
4548 | t->name_indx = (unsigned int) -1; | |
b34976b6 | 4549 | t->used = TRUE; |
252b5132 RH |
4550 | |
4551 | version_index = 1; | |
6b9b879a JJ |
4552 | /* Don't count anonymous version tag. */ |
4553 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
4554 | version_index = 0; | |
252b5132 RH |
4555 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) |
4556 | ++version_index; | |
4557 | t->vernum = version_index; | |
4558 | ||
4559 | *pp = t; | |
4560 | ||
4561 | h->verinfo.vertree = t; | |
4562 | } | |
4563 | else if (t == NULL) | |
4564 | { | |
4565 | /* We could not find the version for a symbol when | |
c44233aa | 4566 | generating a shared archive. Return an error. */ |
252b5132 RH |
4567 | (*_bfd_error_handler) |
4568 | (_("%s: undefined versioned symbol name %s"), | |
4569 | bfd_get_filename (sinfo->output_bfd), h->root.root.string); | |
4570 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 AM |
4571 | sinfo->failed = TRUE; |
4572 | return FALSE; | |
252b5132 RH |
4573 | } |
4574 | ||
4575 | if (hidden) | |
4576 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
4577 | } | |
4578 | ||
4579 | /* If we don't have a version for this symbol, see if we can find | |
4580 | something. */ | |
4581 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
4582 | { | |
4583 | struct bfd_elf_version_tree *t; | |
58821868 | 4584 | struct bfd_elf_version_tree *local_ver; |
252b5132 RH |
4585 | struct bfd_elf_version_expr *d; |
4586 | ||
4587 | /* See if can find what version this symbol is in. If the | |
c44233aa AM |
4588 | symbol is supposed to be local, then don't actually register |
4589 | it. */ | |
58821868 | 4590 | local_ver = NULL; |
252b5132 RH |
4591 | for (t = sinfo->verdefs; t != NULL; t = t->next) |
4592 | { | |
4593 | if (t->globals != NULL) | |
4594 | { | |
b34976b6 | 4595 | bfd_boolean matched; |
31941635 | 4596 | |
b34976b6 | 4597 | matched = FALSE; |
252b5132 RH |
4598 | for (d = t->globals; d != NULL; d = d->next) |
4599 | { | |
4600 | if ((*d->match) (d, h->root.root.string)) | |
4601 | { | |
31941635 | 4602 | if (d->symver) |
b34976b6 | 4603 | matched = TRUE; |
31941635 L |
4604 | else |
4605 | { | |
4606 | /* There is a version without definition. Make | |
4607 | the symbol the default definition for this | |
4608 | version. */ | |
4609 | h->verinfo.vertree = t; | |
4610 | local_ver = NULL; | |
4611 | d->script = 1; | |
4612 | break; | |
4613 | } | |
252b5132 RH |
4614 | } |
4615 | } | |
4616 | ||
4617 | if (d != NULL) | |
4618 | break; | |
31941635 L |
4619 | else if (matched) |
4620 | /* There is no undefined version for this symbol. Hide the | |
4621 | default one. */ | |
b34976b6 | 4622 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
252b5132 RH |
4623 | } |
4624 | ||
4625 | if (t->locals != NULL) | |
4626 | { | |
4627 | for (d = t->locals; d != NULL; d = d->next) | |
4628 | { | |
58821868 AM |
4629 | /* If the match is "*", keep looking for a more |
4630 | explicit, perhaps even global, match. */ | |
252b5132 | 4631 | if (d->pattern[0] == '*' && d->pattern[1] == '\0') |
58821868 | 4632 | local_ver = t; |
252b5132 RH |
4633 | else if ((*d->match) (d, h->root.root.string)) |
4634 | { | |
58821868 | 4635 | local_ver = t; |
252b5132 RH |
4636 | break; |
4637 | } | |
4638 | } | |
4639 | ||
4640 | if (d != NULL) | |
4641 | break; | |
4642 | } | |
4643 | } | |
4644 | ||
58821868 | 4645 | if (local_ver != NULL) |
252b5132 | 4646 | { |
58821868 | 4647 | h->verinfo.vertree = local_ver; |
252b5132 RH |
4648 | if (h->dynindx != -1 |
4649 | && info->shared | |
99293407 | 4650 | && ! info->export_dynamic) |
252b5132 | 4651 | { |
b34976b6 | 4652 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
252b5132 RH |
4653 | } |
4654 | } | |
4655 | } | |
4656 | ||
b34976b6 | 4657 | return TRUE; |
252b5132 | 4658 | } |
252b5132 RH |
4659 | \f |
4660 | /* Final phase of ELF linker. */ | |
4661 | ||
4662 | /* A structure we use to avoid passing large numbers of arguments. */ | |
4663 | ||
4664 | struct elf_final_link_info | |
4665 | { | |
4666 | /* General link information. */ | |
4667 | struct bfd_link_info *info; | |
4668 | /* Output BFD. */ | |
4669 | bfd *output_bfd; | |
4670 | /* Symbol string table. */ | |
4671 | struct bfd_strtab_hash *symstrtab; | |
4672 | /* .dynsym section. */ | |
4673 | asection *dynsym_sec; | |
4674 | /* .hash section. */ | |
4675 | asection *hash_sec; | |
4676 | /* symbol version section (.gnu.version). */ | |
4677 | asection *symver_sec; | |
13ae64f3 JJ |
4678 | /* first SHF_TLS section (if any). */ |
4679 | asection *first_tls_sec; | |
252b5132 RH |
4680 | /* Buffer large enough to hold contents of any section. */ |
4681 | bfd_byte *contents; | |
4682 | /* Buffer large enough to hold external relocs of any section. */ | |
4683 | PTR external_relocs; | |
4684 | /* Buffer large enough to hold internal relocs of any section. */ | |
4685 | Elf_Internal_Rela *internal_relocs; | |
4686 | /* Buffer large enough to hold external local symbols of any input | |
4687 | BFD. */ | |
4688 | Elf_External_Sym *external_syms; | |
9ad5cbcf AM |
4689 | /* And a buffer for symbol section indices. */ |
4690 | Elf_External_Sym_Shndx *locsym_shndx; | |
252b5132 RH |
4691 | /* Buffer large enough to hold internal local symbols of any input |
4692 | BFD. */ | |
4693 | Elf_Internal_Sym *internal_syms; | |
4694 | /* Array large enough to hold a symbol index for each local symbol | |
4695 | of any input BFD. */ | |
4696 | long *indices; | |
4697 | /* Array large enough to hold a section pointer for each local | |
4698 | symbol of any input BFD. */ | |
4699 | asection **sections; | |
4700 | /* Buffer to hold swapped out symbols. */ | |
4701 | Elf_External_Sym *symbuf; | |
9ad5cbcf AM |
4702 | /* And one for symbol section indices. */ |
4703 | Elf_External_Sym_Shndx *symshndxbuf; | |
252b5132 RH |
4704 | /* Number of swapped out symbols in buffer. */ |
4705 | size_t symbuf_count; | |
4706 | /* Number of symbols which fit in symbuf. */ | |
4707 | size_t symbuf_size; | |
c97e73dd AM |
4708 | /* And same for symshndxbuf. */ |
4709 | size_t shndxbuf_size; | |
252b5132 RH |
4710 | }; |
4711 | ||
b34976b6 | 4712 | static bfd_boolean elf_link_output_sym |
252b5132 RH |
4713 | PARAMS ((struct elf_final_link_info *, const char *, |
4714 | Elf_Internal_Sym *, asection *)); | |
b34976b6 | 4715 | static bfd_boolean elf_link_flush_output_syms |
252b5132 | 4716 | PARAMS ((struct elf_final_link_info *)); |
b34976b6 | 4717 | static bfd_boolean elf_link_output_extsym |
252b5132 | 4718 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 4719 | static bfd_boolean elf_link_sec_merge_syms |
f5fa8ca2 | 4720 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
b34976b6 | 4721 | static bfd_boolean elf_link_check_versioned_symbol |
f5d44ba0 | 4722 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); |
b34976b6 | 4723 | static bfd_boolean elf_link_input_bfd |
252b5132 | 4724 | PARAMS ((struct elf_final_link_info *, bfd *)); |
b34976b6 | 4725 | static bfd_boolean elf_reloc_link_order |
252b5132 RH |
4726 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
4727 | struct bfd_link_order *)); | |
4728 | ||
4729 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
4730 | ||
4731 | struct elf_outext_info | |
4732 | { | |
b34976b6 AM |
4733 | bfd_boolean failed; |
4734 | bfd_boolean localsyms; | |
252b5132 RH |
4735 | struct elf_final_link_info *finfo; |
4736 | }; | |
4737 | ||
23bc299b MM |
4738 | /* Compute the size of, and allocate space for, REL_HDR which is the |
4739 | section header for a section containing relocations for O. */ | |
4740 | ||
b34976b6 | 4741 | static bfd_boolean |
23bc299b MM |
4742 | elf_link_size_reloc_section (abfd, rel_hdr, o) |
4743 | bfd *abfd; | |
4744 | Elf_Internal_Shdr *rel_hdr; | |
4745 | asection *o; | |
4746 | { | |
dc810e39 AM |
4747 | bfd_size_type reloc_count; |
4748 | bfd_size_type num_rel_hashes; | |
23bc299b | 4749 | |
b037af20 MM |
4750 | /* Figure out how many relocations there will be. */ |
4751 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
4752 | reloc_count = elf_section_data (o)->rel_count; | |
4753 | else | |
4754 | reloc_count = elf_section_data (o)->rel_count2; | |
4755 | ||
9317eacc CM |
4756 | num_rel_hashes = o->reloc_count; |
4757 | if (num_rel_hashes < reloc_count) | |
4758 | num_rel_hashes = reloc_count; | |
dc810e39 | 4759 | |
b037af20 MM |
4760 | /* That allows us to calculate the size of the section. */ |
4761 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
23bc299b MM |
4762 | |
4763 | /* The contents field must last into write_object_contents, so we | |
755cfd29 NC |
4764 | allocate it with bfd_alloc rather than malloc. Also since we |
4765 | cannot be sure that the contents will actually be filled in, | |
4766 | we zero the allocated space. */ | |
4767 | rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size); | |
23bc299b | 4768 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
b34976b6 | 4769 | return FALSE; |
3e932841 | 4770 | |
b037af20 MM |
4771 | /* We only allocate one set of hash entries, so we only do it the |
4772 | first time we are called. */ | |
9317eacc CM |
4773 | if (elf_section_data (o)->rel_hashes == NULL |
4774 | && num_rel_hashes) | |
b037af20 | 4775 | { |
209f668e NC |
4776 | struct elf_link_hash_entry **p; |
4777 | ||
b037af20 | 4778 | p = ((struct elf_link_hash_entry **) |
9317eacc | 4779 | bfd_zmalloc (num_rel_hashes |
209f668e | 4780 | * sizeof (struct elf_link_hash_entry *))); |
9317eacc | 4781 | if (p == NULL) |
b34976b6 | 4782 | return FALSE; |
23bc299b | 4783 | |
b037af20 | 4784 | elf_section_data (o)->rel_hashes = p; |
b037af20 | 4785 | } |
23bc299b | 4786 | |
b34976b6 | 4787 | return TRUE; |
23bc299b MM |
4788 | } |
4789 | ||
31367b81 MM |
4790 | /* When performing a relocateable link, the input relocations are |
4791 | preserved. But, if they reference global symbols, the indices | |
4792 | referenced must be updated. Update all the relocations in | |
4793 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
4794 | ||
4795 | static void | |
4796 | elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash) | |
4797 | bfd *abfd; | |
4798 | Elf_Internal_Shdr *rel_hdr; | |
4799 | unsigned int count; | |
4800 | struct elf_link_hash_entry **rel_hash; | |
4801 | { | |
4802 | unsigned int i; | |
32f0787a | 4803 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
947216bf AM |
4804 | bfd_byte *erela; |
4805 | void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *)); | |
4806 | void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *)); | |
209f668e | 4807 | |
947216bf | 4808 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) |
209f668e | 4809 | { |
947216bf AM |
4810 | swap_in = bed->s->swap_reloc_in; |
4811 | swap_out = bed->s->swap_reloc_out; | |
209f668e | 4812 | } |
947216bf | 4813 | else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela)) |
209f668e | 4814 | { |
947216bf AM |
4815 | swap_in = bed->s->swap_reloca_in; |
4816 | swap_out = bed->s->swap_reloca_out; | |
209f668e | 4817 | } |
947216bf AM |
4818 | else |
4819 | abort (); | |
4820 | ||
4821 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
4822 | abort (); | |
31367b81 | 4823 | |
947216bf AM |
4824 | erela = rel_hdr->contents; |
4825 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
31367b81 | 4826 | { |
947216bf AM |
4827 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; |
4828 | unsigned int j; | |
4829 | ||
31367b81 MM |
4830 | if (*rel_hash == NULL) |
4831 | continue; | |
4832 | ||
4833 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
4834 | ||
947216bf AM |
4835 | (*swap_in) (abfd, erela, irela); |
4836 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
4837 | irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx, | |
4838 | ELF_R_TYPE (irela[j].r_info)); | |
4839 | (*swap_out) (abfd, irela, erela); | |
31367b81 MM |
4840 | } |
4841 | } | |
4842 | ||
58821868 AM |
4843 | struct elf_link_sort_rela |
4844 | { | |
db6751f2 JJ |
4845 | bfd_vma offset; |
4846 | enum elf_reloc_type_class type; | |
947216bf AM |
4847 | /* We use this as an array of size int_rels_per_ext_rel. */ |
4848 | Elf_Internal_Rela rela[1]; | |
db6751f2 JJ |
4849 | }; |
4850 | ||
4851 | static int | |
4852 | elf_link_sort_cmp1 (A, B) | |
4853 | const PTR A; | |
4854 | const PTR B; | |
4855 | { | |
f51e552e AM |
4856 | struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A; |
4857 | struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B; | |
db6751f2 JJ |
4858 | int relativea, relativeb; |
4859 | ||
4860 | relativea = a->type == reloc_class_relative; | |
4861 | relativeb = b->type == reloc_class_relative; | |
4862 | ||
4863 | if (relativea < relativeb) | |
db6751f2 | 4864 | return 1; |
fcfbdf31 JJ |
4865 | if (relativea > relativeb) |
4866 | return -1; | |
947216bf | 4867 | if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info)) |
db6751f2 | 4868 | return -1; |
947216bf | 4869 | if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info)) |
db6751f2 | 4870 | return 1; |
947216bf | 4871 | if (a->rela->r_offset < b->rela->r_offset) |
db6751f2 | 4872 | return -1; |
947216bf | 4873 | if (a->rela->r_offset > b->rela->r_offset) |
db6751f2 JJ |
4874 | return 1; |
4875 | return 0; | |
4876 | } | |
4877 | ||
4878 | static int | |
4879 | elf_link_sort_cmp2 (A, B) | |
4880 | const PTR A; | |
4881 | const PTR B; | |
4882 | { | |
f51e552e AM |
4883 | struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A; |
4884 | struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B; | |
db6751f2 JJ |
4885 | int copya, copyb; |
4886 | ||
4887 | if (a->offset < b->offset) | |
4888 | return -1; | |
4889 | if (a->offset > b->offset) | |
4890 | return 1; | |
290394d6 JJ |
4891 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); |
4892 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
db6751f2 JJ |
4893 | if (copya < copyb) |
4894 | return -1; | |
4895 | if (copya > copyb) | |
4896 | return 1; | |
947216bf | 4897 | if (a->rela->r_offset < b->rela->r_offset) |
db6751f2 | 4898 | return -1; |
947216bf | 4899 | if (a->rela->r_offset > b->rela->r_offset) |
db6751f2 JJ |
4900 | return 1; |
4901 | return 0; | |
4902 | } | |
4903 | ||
4904 | static size_t | |
4905 | elf_link_sort_relocs (abfd, info, psec) | |
4906 | bfd *abfd; | |
4907 | struct bfd_link_info *info; | |
4908 | asection **psec; | |
4909 | { | |
4910 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
4911 | asection *reldyn, *o; | |
f51e552e | 4912 | bfd_size_type count, size; |
947216bf AM |
4913 | size_t i, ret, sort_elt, ext_size; |
4914 | bfd_byte *sort, *s_non_relative, *p; | |
4915 | struct elf_link_sort_rela *sq; | |
db6751f2 | 4916 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
033fd5f9 | 4917 | int i2e = bed->s->int_rels_per_ext_rel; |
947216bf AM |
4918 | void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *)); |
4919 | void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *)); | |
db6751f2 JJ |
4920 | |
4921 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
4922 | if (reldyn == NULL || reldyn->_raw_size == 0) | |
4923 | { | |
4924 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
4925 | if (reldyn == NULL || reldyn->_raw_size == 0) | |
4926 | return 0; | |
947216bf AM |
4927 | ext_size = sizeof (Elf_External_Rel); |
4928 | swap_in = bed->s->swap_reloc_in; | |
4929 | swap_out = bed->s->swap_reloc_out; | |
db6751f2 JJ |
4930 | } |
4931 | else | |
947216bf AM |
4932 | { |
4933 | ext_size = sizeof (Elf_External_Rela); | |
4934 | swap_in = bed->s->swap_reloca_in; | |
4935 | swap_out = bed->s->swap_reloca_out; | |
4936 | } | |
4937 | count = reldyn->_raw_size / ext_size; | |
db6751f2 JJ |
4938 | |
4939 | size = 0; | |
4940 | for (o = dynobj->sections; o != NULL; o = o->next) | |
4941 | if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)) | |
4942 | == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED) | |
4943 | && o->output_section == reldyn) | |
4944 | size += o->_raw_size; | |
4945 | ||
4946 | if (size != reldyn->_raw_size) | |
4947 | return 0; | |
4948 | ||
947216bf AM |
4949 | sort_elt = (sizeof (struct elf_link_sort_rela) |
4950 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
4951 | sort = bfd_zmalloc (sort_elt * count); | |
4952 | if (sort == NULL) | |
db6751f2 JJ |
4953 | { |
4954 | (*info->callbacks->warning) | |
dc810e39 AM |
4955 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, |
4956 | (bfd_vma) 0); | |
db6751f2 JJ |
4957 | return 0; |
4958 | } | |
4959 | ||
4960 | for (o = dynobj->sections; o != NULL; o = o->next) | |
4961 | if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)) | |
4962 | == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED) | |
4963 | && o->output_section == reldyn) | |
4964 | { | |
947216bf | 4965 | bfd_byte *erel, *erelend; |
db6751f2 | 4966 | |
947216bf AM |
4967 | erel = o->contents; |
4968 | erelend = o->contents + o->_raw_size; | |
4969 | p = sort + o->output_offset / ext_size * sort_elt; | |
4970 | while (erel < erelend) | |
db6751f2 | 4971 | { |
947216bf AM |
4972 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; |
4973 | (*swap_in) (abfd, erel, s->rela); | |
4974 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
4975 | p += sort_elt; | |
4976 | erel += ext_size; | |
db6751f2 JJ |
4977 | } |
4978 | } | |
4979 | ||
947216bf AM |
4980 | qsort (sort, (size_t) count, sort_elt, elf_link_sort_cmp1); |
4981 | ||
4982 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
db6751f2 | 4983 | { |
947216bf AM |
4984 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; |
4985 | if (s->type != reloc_class_relative) | |
4986 | break; | |
db6751f2 | 4987 | } |
947216bf AM |
4988 | ret = i; |
4989 | s_non_relative = p; | |
4990 | ||
4991 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
4992 | for (; i < count; i++, p += sort_elt) | |
4993 | { | |
4994 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
4995 | if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info)) | |
4996 | sq = sp; | |
4997 | sp->offset = sq->rela->r_offset; | |
4998 | } | |
4999 | ||
5000 | qsort (s_non_relative, (size_t) count - ret, sort_elt, elf_link_sort_cmp2); | |
dc810e39 | 5001 | |
db6751f2 JJ |
5002 | for (o = dynobj->sections; o != NULL; o = o->next) |
5003 | if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)) | |
5004 | == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED) | |
5005 | && o->output_section == reldyn) | |
5006 | { | |
947216bf | 5007 | bfd_byte *erel, *erelend; |
db6751f2 | 5008 | |
947216bf AM |
5009 | erel = o->contents; |
5010 | erelend = o->contents + o->_raw_size; | |
5011 | p = sort + o->output_offset / ext_size * sort_elt; | |
5012 | while (erel < erelend) | |
db6751f2 | 5013 | { |
947216bf AM |
5014 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; |
5015 | (*swap_out) (abfd, s->rela, erel); | |
5016 | p += sort_elt; | |
5017 | erel += ext_size; | |
db6751f2 JJ |
5018 | } |
5019 | } | |
5020 | ||
5ed6aba4 | 5021 | free (sort); |
db6751f2 JJ |
5022 | *psec = reldyn; |
5023 | return ret; | |
5024 | } | |
5025 | ||
252b5132 RH |
5026 | /* Do the final step of an ELF link. */ |
5027 | ||
b34976b6 | 5028 | bfd_boolean |
252b5132 RH |
5029 | elf_bfd_final_link (abfd, info) |
5030 | bfd *abfd; | |
5031 | struct bfd_link_info *info; | |
5032 | { | |
b34976b6 AM |
5033 | bfd_boolean dynamic; |
5034 | bfd_boolean emit_relocs; | |
252b5132 RH |
5035 | bfd *dynobj; |
5036 | struct elf_final_link_info finfo; | |
5037 | register asection *o; | |
5038 | register struct bfd_link_order *p; | |
5039 | register bfd *sub; | |
dc810e39 AM |
5040 | bfd_size_type max_contents_size; |
5041 | bfd_size_type max_external_reloc_size; | |
5042 | bfd_size_type max_internal_reloc_count; | |
5043 | bfd_size_type max_sym_count; | |
9ad5cbcf | 5044 | bfd_size_type max_sym_shndx_count; |
252b5132 RH |
5045 | file_ptr off; |
5046 | Elf_Internal_Sym elfsym; | |
5047 | unsigned int i; | |
5048 | Elf_Internal_Shdr *symtab_hdr; | |
c97e73dd | 5049 | Elf_Internal_Shdr *symtab_shndx_hdr; |
252b5132 RH |
5050 | Elf_Internal_Shdr *symstrtab_hdr; |
5051 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5052 | struct elf_outext_info eoinfo; | |
b34976b6 | 5053 | bfd_boolean merged; |
db6751f2 JJ |
5054 | size_t relativecount = 0; |
5055 | asection *reldyn = 0; | |
dc810e39 | 5056 | bfd_size_type amt; |
252b5132 | 5057 | |
8ea2e4bd | 5058 | if (! is_elf_hash_table (info)) |
b34976b6 | 5059 | return FALSE; |
8ea2e4bd | 5060 | |
252b5132 RH |
5061 | if (info->shared) |
5062 | abfd->flags |= DYNAMIC; | |
5063 | ||
5064 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
5065 | dynobj = elf_hash_table (info)->dynobj; | |
5066 | ||
9317eacc | 5067 | emit_relocs = (info->relocateable |
c44233aa AM |
5068 | || info->emitrelocations |
5069 | || bed->elf_backend_emit_relocs); | |
9317eacc | 5070 | |
252b5132 RH |
5071 | finfo.info = info; |
5072 | finfo.output_bfd = abfd; | |
5073 | finfo.symstrtab = elf_stringtab_init (); | |
5074 | if (finfo.symstrtab == NULL) | |
b34976b6 | 5075 | return FALSE; |
252b5132 RH |
5076 | |
5077 | if (! dynamic) | |
5078 | { | |
5079 | finfo.dynsym_sec = NULL; | |
5080 | finfo.hash_sec = NULL; | |
5081 | finfo.symver_sec = NULL; | |
5082 | } | |
5083 | else | |
5084 | { | |
5085 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5086 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
5087 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
5088 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5089 | /* Note that it is OK if symver_sec is NULL. */ | |
5090 | } | |
5091 | ||
5092 | finfo.contents = NULL; | |
5093 | finfo.external_relocs = NULL; | |
5094 | finfo.internal_relocs = NULL; | |
5095 | finfo.external_syms = NULL; | |
9ad5cbcf | 5096 | finfo.locsym_shndx = NULL; |
252b5132 RH |
5097 | finfo.internal_syms = NULL; |
5098 | finfo.indices = NULL; | |
5099 | finfo.sections = NULL; | |
5100 | finfo.symbuf = NULL; | |
9ad5cbcf | 5101 | finfo.symshndxbuf = NULL; |
252b5132 | 5102 | finfo.symbuf_count = 0; |
c97e73dd | 5103 | finfo.shndxbuf_size = 0; |
13ae64f3 JJ |
5104 | finfo.first_tls_sec = NULL; |
5105 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
5106 | if ((o->flags & SEC_THREAD_LOCAL) != 0 | |
5107 | && (o->flags & SEC_LOAD) != 0) | |
5108 | { | |
5109 | finfo.first_tls_sec = o; | |
5110 | break; | |
5111 | } | |
252b5132 RH |
5112 | |
5113 | /* Count up the number of relocations we will output for each output | |
5114 | section, so that we know the sizes of the reloc sections. We | |
5115 | also figure out some maximum sizes. */ | |
5116 | max_contents_size = 0; | |
5117 | max_external_reloc_size = 0; | |
5118 | max_internal_reloc_count = 0; | |
5119 | max_sym_count = 0; | |
9ad5cbcf | 5120 | max_sym_shndx_count = 0; |
b34976b6 | 5121 | merged = FALSE; |
252b5132 RH |
5122 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) |
5123 | { | |
3f9a32bd | 5124 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
252b5132 RH |
5125 | o->reloc_count = 0; |
5126 | ||
5127 | for (p = o->link_order_head; p != NULL; p = p->next) | |
5128 | { | |
3f9a32bd AM |
5129 | unsigned int reloc_count = 0; |
5130 | struct bfd_elf_section_data *esdi = NULL; | |
5131 | unsigned int *rel_count1; | |
5132 | ||
252b5132 RH |
5133 | if (p->type == bfd_section_reloc_link_order |
5134 | || p->type == bfd_symbol_reloc_link_order) | |
3f9a32bd | 5135 | reloc_count = 1; |
252b5132 RH |
5136 | else if (p->type == bfd_indirect_link_order) |
5137 | { | |
5138 | asection *sec; | |
5139 | ||
5140 | sec = p->u.indirect.section; | |
3f9a32bd | 5141 | esdi = elf_section_data (sec); |
252b5132 RH |
5142 | |
5143 | /* Mark all sections which are to be included in the | |
5144 | link. This will normally be every section. We need | |
5145 | to do this so that we can identify any sections which | |
5146 | the linker has decided to not include. */ | |
b34976b6 | 5147 | sec->linker_mark = TRUE; |
252b5132 | 5148 | |
f5fa8ca2 | 5149 | if (sec->flags & SEC_MERGE) |
b34976b6 | 5150 | merged = TRUE; |
f5fa8ca2 | 5151 | |
a712da20 | 5152 | if (info->relocateable || info->emitrelocations) |
3f9a32bd | 5153 | reloc_count = sec->reloc_count; |
c44233aa | 5154 | else if (bed->elf_backend_count_relocs) |
9317eacc CM |
5155 | { |
5156 | Elf_Internal_Rela * relocs; | |
5157 | ||
5158 | relocs = (NAME(_bfd_elf,link_read_relocs) | |
5159 | (abfd, sec, (PTR) NULL, | |
5160 | (Elf_Internal_Rela *) NULL, info->keep_memory)); | |
5161 | ||
3f9a32bd | 5162 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); |
9317eacc | 5163 | |
6cdc0ccc | 5164 | if (elf_section_data (o)->relocs != relocs) |
9317eacc CM |
5165 | free (relocs); |
5166 | } | |
252b5132 RH |
5167 | |
5168 | if (sec->_raw_size > max_contents_size) | |
5169 | max_contents_size = sec->_raw_size; | |
5170 | if (sec->_cooked_size > max_contents_size) | |
5171 | max_contents_size = sec->_cooked_size; | |
5172 | ||
5173 | /* We are interested in just local symbols, not all | |
5174 | symbols. */ | |
5175 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
5176 | && (sec->owner->flags & DYNAMIC) == 0) | |
5177 | { | |
5178 | size_t sym_count; | |
5179 | ||
5180 | if (elf_bad_symtab (sec->owner)) | |
5181 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
5182 | / sizeof (Elf_External_Sym)); | |
5183 | else | |
5184 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
5185 | ||
5186 | if (sym_count > max_sym_count) | |
5187 | max_sym_count = sym_count; | |
5188 | ||
9ad5cbcf AM |
5189 | if (sym_count > max_sym_shndx_count |
5190 | && elf_symtab_shndx (sec->owner) != 0) | |
5191 | max_sym_shndx_count = sym_count; | |
5192 | ||
252b5132 RH |
5193 | if ((sec->flags & SEC_RELOC) != 0) |
5194 | { | |
5195 | size_t ext_size; | |
5196 | ||
5197 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
5198 | if (ext_size > max_external_reloc_size) | |
5199 | max_external_reloc_size = ext_size; | |
5200 | if (sec->reloc_count > max_internal_reloc_count) | |
5201 | max_internal_reloc_count = sec->reloc_count; | |
5202 | } | |
5203 | } | |
5204 | } | |
3f9a32bd AM |
5205 | |
5206 | if (reloc_count == 0) | |
5207 | continue; | |
5208 | ||
5209 | o->reloc_count += reloc_count; | |
5210 | ||
5211 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
5212 | To support this curious ABI we keep reloc counts in | |
5213 | elf_section_data too. We must be careful to add the | |
5214 | relocations from the input section to the right output | |
5215 | count. FIXME: Get rid of one count. We have | |
5216 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
5217 | rel_count1 = &esdo->rel_count; | |
5218 | if (esdi != NULL) | |
5219 | { | |
5220 | bfd_boolean same_size; | |
5221 | bfd_size_type entsize1; | |
5222 | ||
5223 | entsize1 = esdi->rel_hdr.sh_entsize; | |
5224 | BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel) | |
5225 | || entsize1 == sizeof (Elf_External_Rela)); | |
5226 | same_size = (!o->use_rela_p | |
5227 | == (entsize1 == sizeof (Elf_External_Rel))); | |
5228 | ||
5229 | if (!same_size) | |
5230 | rel_count1 = &esdo->rel_count2; | |
5231 | ||
5232 | if (esdi->rel_hdr2 != NULL) | |
5233 | { | |
5234 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
5235 | unsigned int alt_count; | |
5236 | unsigned int *rel_count2; | |
5237 | ||
5238 | BFD_ASSERT (entsize2 != entsize1 | |
5239 | && (entsize2 == sizeof (Elf_External_Rel) | |
5240 | || entsize2 == sizeof (Elf_External_Rela))); | |
5241 | ||
5242 | rel_count2 = &esdo->rel_count2; | |
5243 | if (!same_size) | |
5244 | rel_count2 = &esdo->rel_count; | |
5245 | ||
5246 | /* The following is probably too simplistic if the | |
5247 | backend counts output relocs unusually. */ | |
5248 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
5249 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
5250 | *rel_count2 += alt_count; | |
5251 | reloc_count -= alt_count; | |
5252 | } | |
5253 | } | |
5254 | *rel_count1 += reloc_count; | |
252b5132 RH |
5255 | } |
5256 | ||
5257 | if (o->reloc_count > 0) | |
5258 | o->flags |= SEC_RELOC; | |
5259 | else | |
5260 | { | |
5261 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
5262 | set it (this is probably a bug) and if it is set | |
5263 | assign_section_numbers will create a reloc section. */ | |
5264 | o->flags &=~ SEC_RELOC; | |
5265 | } | |
5266 | ||
5267 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
5268 | zero. This is done in elf_fake_sections as well, but forcing | |
5269 | the VMA to 0 here will ensure that relocs against these | |
5270 | sections are handled correctly. */ | |
5271 | if ((o->flags & SEC_ALLOC) == 0 | |
5272 | && ! o->user_set_vma) | |
5273 | o->vma = 0; | |
5274 | } | |
5275 | ||
f5fa8ca2 JJ |
5276 | if (! info->relocateable && merged) |
5277 | elf_link_hash_traverse (elf_hash_table (info), | |
5278 | elf_link_sec_merge_syms, (PTR) abfd); | |
5279 | ||
252b5132 RH |
5280 | /* Figure out the file positions for everything but the symbol table |
5281 | and the relocs. We set symcount to force assign_section_numbers | |
5282 | to create a symbol table. */ | |
5283 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
5284 | BFD_ASSERT (! abfd->output_has_begun); | |
5285 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
5286 | goto error_return; | |
5287 | ||
5288 | /* That created the reloc sections. Set their sizes, and assign | |
5289 | them file positions, and allocate some buffers. */ | |
5290 | for (o = abfd->sections; o != NULL; o = o->next) | |
5291 | { | |
5292 | if ((o->flags & SEC_RELOC) != 0) | |
5293 | { | |
23bc299b MM |
5294 | if (!elf_link_size_reloc_section (abfd, |
5295 | &elf_section_data (o)->rel_hdr, | |
5296 | o)) | |
252b5132 RH |
5297 | goto error_return; |
5298 | ||
23bc299b MM |
5299 | if (elf_section_data (o)->rel_hdr2 |
5300 | && !elf_link_size_reloc_section (abfd, | |
5301 | elf_section_data (o)->rel_hdr2, | |
5302 | o)) | |
252b5132 | 5303 | goto error_return; |
252b5132 | 5304 | } |
b037af20 MM |
5305 | |
5306 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
3e932841 | 5307 | to count upwards while actually outputting the relocations. */ |
b037af20 MM |
5308 | elf_section_data (o)->rel_count = 0; |
5309 | elf_section_data (o)->rel_count2 = 0; | |
252b5132 RH |
5310 | } |
5311 | ||
5312 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
5313 | ||
5314 | /* We have now assigned file positions for all the sections except | |
5315 | .symtab and .strtab. We start the .symtab section at the current | |
5316 | file position, and write directly to it. We build the .strtab | |
5317 | section in memory. */ | |
5318 | bfd_get_symcount (abfd) = 0; | |
5319 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
5320 | /* sh_name is set in prep_headers. */ | |
5321 | symtab_hdr->sh_type = SHT_SYMTAB; | |
c97e73dd | 5322 | /* sh_flags, sh_addr and sh_size all start off zero. */ |
252b5132 RH |
5323 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); |
5324 | /* sh_link is set in assign_section_numbers. */ | |
5325 | /* sh_info is set below. */ | |
5326 | /* sh_offset is set just below. */ | |
f0e1d18a | 5327 | symtab_hdr->sh_addralign = bed->s->file_align; |
252b5132 RH |
5328 | |
5329 | off = elf_tdata (abfd)->next_file_pos; | |
b34976b6 | 5330 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); |
252b5132 RH |
5331 | |
5332 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
5333 | incorrect. We do not yet know the size of the .symtab section. | |
5334 | We correct next_file_pos below, after we do know the size. */ | |
5335 | ||
5336 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
5337 | continuously seeking to the right position in the file. */ | |
5338 | if (! info->keep_memory || max_sym_count < 20) | |
5339 | finfo.symbuf_size = 20; | |
5340 | else | |
5341 | finfo.symbuf_size = max_sym_count; | |
dc810e39 AM |
5342 | amt = finfo.symbuf_size; |
5343 | amt *= sizeof (Elf_External_Sym); | |
5344 | finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt); | |
252b5132 RH |
5345 | if (finfo.symbuf == NULL) |
5346 | goto error_return; | |
9ad5cbcf AM |
5347 | if (elf_numsections (abfd) > SHN_LORESERVE) |
5348 | { | |
c97e73dd AM |
5349 | /* Wild guess at number of output symbols. realloc'd as needed. */ |
5350 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
5351 | finfo.shndxbuf_size = amt; | |
9ad5cbcf | 5352 | amt *= sizeof (Elf_External_Sym_Shndx); |
c97e73dd | 5353 | finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9ad5cbcf AM |
5354 | if (finfo.symshndxbuf == NULL) |
5355 | goto error_return; | |
5356 | } | |
252b5132 RH |
5357 | |
5358 | /* Start writing out the symbol table. The first symbol is always a | |
5359 | dummy symbol. */ | |
9317eacc CM |
5360 | if (info->strip != strip_all |
5361 | || emit_relocs) | |
252b5132 RH |
5362 | { |
5363 | elfsym.st_value = 0; | |
5364 | elfsym.st_size = 0; | |
5365 | elfsym.st_info = 0; | |
5366 | elfsym.st_other = 0; | |
5367 | elfsym.st_shndx = SHN_UNDEF; | |
5368 | if (! elf_link_output_sym (&finfo, (const char *) NULL, | |
5369 | &elfsym, bfd_und_section_ptr)) | |
5370 | goto error_return; | |
5371 | } | |
5372 | ||
5373 | #if 0 | |
5374 | /* Some standard ELF linkers do this, but we don't because it causes | |
5375 | bootstrap comparison failures. */ | |
5376 | /* Output a file symbol for the output file as the second symbol. | |
5377 | We output this even if we are discarding local symbols, although | |
5378 | I'm not sure if this is correct. */ | |
5379 | elfsym.st_value = 0; | |
5380 | elfsym.st_size = 0; | |
5381 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
5382 | elfsym.st_other = 0; | |
5383 | elfsym.st_shndx = SHN_ABS; | |
5384 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), | |
5385 | &elfsym, bfd_abs_section_ptr)) | |
5386 | goto error_return; | |
5387 | #endif | |
5388 | ||
5389 | /* Output a symbol for each section. We output these even if we are | |
5390 | discarding local symbols, since they are used for relocs. These | |
5391 | symbols have no names. We store the index of each one in the | |
5392 | index field of the section, so that we can find it again when | |
5393 | outputting relocs. */ | |
9317eacc CM |
5394 | if (info->strip != strip_all |
5395 | || emit_relocs) | |
252b5132 RH |
5396 | { |
5397 | elfsym.st_size = 0; | |
5398 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
5399 | elfsym.st_other = 0; | |
9ad5cbcf | 5400 | for (i = 1; i < elf_numsections (abfd); i++) |
252b5132 RH |
5401 | { |
5402 | o = section_from_elf_index (abfd, i); | |
5403 | if (o != NULL) | |
5404 | o->target_index = bfd_get_symcount (abfd); | |
5405 | elfsym.st_shndx = i; | |
7ad34365 | 5406 | if (info->relocateable || o == NULL) |
252b5132 RH |
5407 | elfsym.st_value = 0; |
5408 | else | |
5409 | elfsym.st_value = o->vma; | |
5410 | if (! elf_link_output_sym (&finfo, (const char *) NULL, | |
5411 | &elfsym, o)) | |
5412 | goto error_return; | |
c97e73dd | 5413 | if (i == SHN_LORESERVE - 1) |
9ad5cbcf | 5414 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; |
252b5132 RH |
5415 | } |
5416 | } | |
5417 | ||
5418 | /* Allocate some memory to hold information read in from the input | |
5419 | files. */ | |
9ad5cbcf AM |
5420 | if (max_contents_size != 0) |
5421 | { | |
5422 | finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); | |
5423 | if (finfo.contents == NULL) | |
5424 | goto error_return; | |
5425 | } | |
5426 | ||
5427 | if (max_external_reloc_size != 0) | |
5428 | { | |
5429 | finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size); | |
5430 | if (finfo.external_relocs == NULL) | |
5431 | goto error_return; | |
5432 | } | |
5433 | ||
5434 | if (max_internal_reloc_count != 0) | |
5435 | { | |
5436 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
5437 | amt *= sizeof (Elf_Internal_Rela); | |
5438 | finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); | |
5439 | if (finfo.internal_relocs == NULL) | |
5440 | goto error_return; | |
5441 | } | |
5442 | ||
5443 | if (max_sym_count != 0) | |
5444 | { | |
5445 | amt = max_sym_count * sizeof (Elf_External_Sym); | |
5446 | finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt); | |
5447 | if (finfo.external_syms == NULL) | |
5448 | goto error_return; | |
5449 | ||
5450 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
5451 | finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); | |
5452 | if (finfo.internal_syms == NULL) | |
5453 | goto error_return; | |
5454 | ||
5455 | amt = max_sym_count * sizeof (long); | |
5456 | finfo.indices = (long *) bfd_malloc (amt); | |
5457 | if (finfo.indices == NULL) | |
5458 | goto error_return; | |
5459 | ||
5460 | amt = max_sym_count * sizeof (asection *); | |
5461 | finfo.sections = (asection **) bfd_malloc (amt); | |
5462 | if (finfo.sections == NULL) | |
5463 | goto error_return; | |
5464 | } | |
5465 | ||
5466 | if (max_sym_shndx_count != 0) | |
5467 | { | |
5468 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
5469 | finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); | |
5470 | if (finfo.locsym_shndx == NULL) | |
5471 | goto error_return; | |
5472 | } | |
252b5132 | 5473 | |
13ae64f3 JJ |
5474 | if (finfo.first_tls_sec) |
5475 | { | |
5476 | unsigned int align = 0; | |
5477 | bfd_vma base = finfo.first_tls_sec->vma, end = 0; | |
5478 | asection *sec; | |
5479 | ||
5480 | for (sec = finfo.first_tls_sec; | |
5481 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
5482 | sec = sec->next) | |
5483 | { | |
5484 | bfd_vma size = sec->_raw_size; | |
5485 | ||
5486 | if (bfd_get_section_alignment (abfd, sec) > align) | |
5487 | align = bfd_get_section_alignment (abfd, sec); | |
5488 | if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
5489 | { | |
5490 | struct bfd_link_order *o; | |
5491 | ||
5492 | size = 0; | |
5493 | for (o = sec->link_order_head; o != NULL; o = o->next) | |
5494 | if (size < o->offset + o->size) | |
58821868 | 5495 | size = o->offset + o->size; |
13ae64f3 JJ |
5496 | } |
5497 | end = sec->vma + size; | |
5498 | } | |
5499 | elf_hash_table (info)->tls_segment | |
5500 | = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment)); | |
5501 | if (elf_hash_table (info)->tls_segment == NULL) | |
5502 | goto error_return; | |
5503 | elf_hash_table (info)->tls_segment->start = base; | |
5504 | elf_hash_table (info)->tls_segment->size = end - base; | |
5505 | elf_hash_table (info)->tls_segment->align = align; | |
5506 | } | |
5507 | ||
252b5132 RH |
5508 | /* Since ELF permits relocations to be against local symbols, we |
5509 | must have the local symbols available when we do the relocations. | |
5510 | Since we would rather only read the local symbols once, and we | |
5511 | would rather not keep them in memory, we handle all the | |
5512 | relocations for a single input file at the same time. | |
5513 | ||
5514 | Unfortunately, there is no way to know the total number of local | |
5515 | symbols until we have seen all of them, and the local symbol | |
5516 | indices precede the global symbol indices. This means that when | |
5517 | we are generating relocateable output, and we see a reloc against | |
5518 | a global symbol, we can not know the symbol index until we have | |
5519 | finished examining all the local symbols to see which ones we are | |
5520 | going to output. To deal with this, we keep the relocations in | |
5521 | memory, and don't output them until the end of the link. This is | |
5522 | an unfortunate waste of memory, but I don't see a good way around | |
5523 | it. Fortunately, it only happens when performing a relocateable | |
5524 | link, which is not the common case. FIXME: If keep_memory is set | |
5525 | we could write the relocs out and then read them again; I don't | |
5526 | know how bad the memory loss will be. */ | |
5527 | ||
5528 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
b34976b6 | 5529 | sub->output_has_begun = FALSE; |
252b5132 RH |
5530 | for (o = abfd->sections; o != NULL; o = o->next) |
5531 | { | |
5532 | for (p = o->link_order_head; p != NULL; p = p->next) | |
5533 | { | |
5534 | if (p->type == bfd_indirect_link_order | |
a50c1845 | 5535 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) |
4ddafab0 | 5536 | == bfd_target_elf_flavour) |
a50c1845 | 5537 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) |
252b5132 | 5538 | { |
252b5132 RH |
5539 | if (! sub->output_has_begun) |
5540 | { | |
5541 | if (! elf_link_input_bfd (&finfo, sub)) | |
5542 | goto error_return; | |
b34976b6 | 5543 | sub->output_has_begun = TRUE; |
252b5132 RH |
5544 | } |
5545 | } | |
5546 | else if (p->type == bfd_section_reloc_link_order | |
5547 | || p->type == bfd_symbol_reloc_link_order) | |
5548 | { | |
5549 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
5550 | goto error_return; | |
5551 | } | |
5552 | else | |
5553 | { | |
5554 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
5555 | goto error_return; | |
5556 | } | |
5557 | } | |
5558 | } | |
5559 | ||
c44233aa AM |
5560 | /* Output any global symbols that got converted to local in a |
5561 | version script or due to symbol visibility. We do this in a | |
5562 | separate step since ELF requires all local symbols to appear | |
5563 | prior to any global symbols. FIXME: We should only do this if | |
5564 | some global symbols were, in fact, converted to become local. | |
5565 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
b34976b6 | 5566 | eoinfo.failed = FALSE; |
c44233aa | 5567 | eoinfo.finfo = &finfo; |
b34976b6 | 5568 | eoinfo.localsyms = TRUE; |
c44233aa AM |
5569 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, |
5570 | (PTR) &eoinfo); | |
5571 | if (eoinfo.failed) | |
b34976b6 | 5572 | return FALSE; |
c44233aa | 5573 | |
252b5132 | 5574 | /* That wrote out all the local symbols. Finish up the symbol table |
5cc7c785 L |
5575 | with the global symbols. Even if we want to strip everything we |
5576 | can, we still need to deal with those global symbols that got | |
3e932841 | 5577 | converted to local in a version script. */ |
252b5132 | 5578 | |
30b30c21 | 5579 | /* The sh_info field records the index of the first non local symbol. */ |
252b5132 | 5580 | symtab_hdr->sh_info = bfd_get_symcount (abfd); |
30b30c21 | 5581 | |
fc8c40a0 AM |
5582 | if (dynamic |
5583 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
30b30c21 RH |
5584 | { |
5585 | Elf_Internal_Sym sym; | |
5586 | Elf_External_Sym *dynsym = | |
a7b97311 | 5587 | (Elf_External_Sym *) finfo.dynsym_sec->contents; |
71a40b32 | 5588 | long last_local = 0; |
30b30c21 RH |
5589 | |
5590 | /* Write out the section symbols for the output sections. */ | |
5591 | if (info->shared) | |
5592 | { | |
5593 | asection *s; | |
5594 | ||
5595 | sym.st_size = 0; | |
5596 | sym.st_name = 0; | |
5597 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
5598 | sym.st_other = 0; | |
5599 | ||
5600 | for (s = abfd->sections; s != NULL; s = s->next) | |
5601 | { | |
5602 | int indx; | |
9ad5cbcf AM |
5603 | Elf_External_Sym *dest; |
5604 | ||
30b30c21 RH |
5605 | indx = elf_section_data (s)->this_idx; |
5606 | BFD_ASSERT (indx > 0); | |
5607 | sym.st_shndx = indx; | |
5608 | sym.st_value = s->vma; | |
9ad5cbcf AM |
5609 | dest = dynsym + elf_section_data (s)->dynindx; |
5610 | elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0); | |
30b30c21 RH |
5611 | } |
5612 | ||
5613 | last_local = bfd_count_sections (abfd); | |
5614 | } | |
5615 | ||
5616 | /* Write out the local dynsyms. */ | |
5617 | if (elf_hash_table (info)->dynlocal) | |
5618 | { | |
5619 | struct elf_link_local_dynamic_entry *e; | |
5620 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
5621 | { | |
318da145 | 5622 | asection *s; |
9ad5cbcf | 5623 | Elf_External_Sym *dest; |
30b30c21 | 5624 | |
b037af20 MM |
5625 | sym.st_size = e->isym.st_size; |
5626 | sym.st_other = e->isym.st_other; | |
5627 | ||
1fa0ddb3 RH |
5628 | /* Copy the internal symbol as is. |
5629 | Note that we saved a word of storage and overwrote | |
c44233aa AM |
5630 | the original st_name with the dynstr_index. */ |
5631 | sym = e->isym; | |
30b30c21 | 5632 | |
c8e5ddc8 | 5633 | if (e->isym.st_shndx != SHN_UNDEF |
58821868 AM |
5634 | && (e->isym.st_shndx < SHN_LORESERVE |
5635 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
587ff49e RH |
5636 | { |
5637 | s = bfd_section_from_elf_index (e->input_bfd, | |
5638 | e->isym.st_shndx); | |
5639 | ||
5640 | sym.st_shndx = | |
5641 | elf_section_data (s->output_section)->this_idx; | |
5642 | sym.st_value = (s->output_section->vma | |
5643 | + s->output_offset | |
5644 | + e->isym.st_value); | |
5645 | } | |
30b30c21 RH |
5646 | |
5647 | if (last_local < e->dynindx) | |
5648 | last_local = e->dynindx; | |
5649 | ||
9ad5cbcf AM |
5650 | dest = dynsym + e->dynindx; |
5651 | elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0); | |
30b30c21 RH |
5652 | } |
5653 | } | |
5654 | ||
71a40b32 ILT |
5655 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = |
5656 | last_local + 1; | |
30b30c21 | 5657 | } |
252b5132 RH |
5658 | |
5659 | /* We get the global symbols from the hash table. */ | |
b34976b6 AM |
5660 | eoinfo.failed = FALSE; |
5661 | eoinfo.localsyms = FALSE; | |
252b5132 RH |
5662 | eoinfo.finfo = &finfo; |
5663 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
5664 | (PTR) &eoinfo); | |
5665 | if (eoinfo.failed) | |
b34976b6 | 5666 | return FALSE; |
252b5132 | 5667 | |
587ff49e RH |
5668 | /* If backend needs to output some symbols not present in the hash |
5669 | table, do it now. */ | |
5670 | if (bed->elf_backend_output_arch_syms) | |
5671 | { | |
b34976b6 AM |
5672 | typedef bfd_boolean (*out_sym_func) |
5673 | PARAMS ((PTR, const char *, Elf_Internal_Sym *, asection *)); | |
dc810e39 AM |
5674 | |
5675 | if (! ((*bed->elf_backend_output_arch_syms) | |
5676 | (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym))) | |
b34976b6 | 5677 | return FALSE; |
3e932841 | 5678 | } |
587ff49e | 5679 | |
252b5132 RH |
5680 | /* Flush all symbols to the file. */ |
5681 | if (! elf_link_flush_output_syms (&finfo)) | |
b34976b6 | 5682 | return FALSE; |
252b5132 RH |
5683 | |
5684 | /* Now we know the size of the symtab section. */ | |
5685 | off += symtab_hdr->sh_size; | |
5686 | ||
c97e73dd AM |
5687 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
5688 | if (symtab_shndx_hdr->sh_name != 0) | |
5689 | { | |
5690 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
5691 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
5692 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
5693 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
5694 | symtab_shndx_hdr->sh_size = amt; | |
5695 | ||
5696 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
b34976b6 | 5697 | off, TRUE); |
c97e73dd AM |
5698 | |
5699 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
5700 | || (bfd_bwrite ((PTR) finfo.symshndxbuf, amt, abfd) != amt)) | |
b34976b6 | 5701 | return FALSE; |
c97e73dd AM |
5702 | } |
5703 | ||
5704 | ||
252b5132 RH |
5705 | /* Finish up and write out the symbol string table (.strtab) |
5706 | section. */ | |
5707 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
5708 | /* sh_name was set in prep_headers. */ | |
5709 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
5710 | symstrtab_hdr->sh_flags = 0; | |
5711 | symstrtab_hdr->sh_addr = 0; | |
5712 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
5713 | symstrtab_hdr->sh_entsize = 0; | |
5714 | symstrtab_hdr->sh_link = 0; | |
5715 | symstrtab_hdr->sh_info = 0; | |
5716 | /* sh_offset is set just below. */ | |
5717 | symstrtab_hdr->sh_addralign = 1; | |
5718 | ||
b34976b6 | 5719 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); |
252b5132 RH |
5720 | elf_tdata (abfd)->next_file_pos = off; |
5721 | ||
5722 | if (bfd_get_symcount (abfd) > 0) | |
5723 | { | |
5724 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
5725 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
b34976b6 | 5726 | return FALSE; |
252b5132 RH |
5727 | } |
5728 | ||
5729 | /* Adjust the relocs to have the correct symbol indices. */ | |
5730 | for (o = abfd->sections; o != NULL; o = o->next) | |
5731 | { | |
252b5132 RH |
5732 | if ((o->flags & SEC_RELOC) == 0) |
5733 | continue; | |
5734 | ||
3e932841 | 5735 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, |
31367b81 MM |
5736 | elf_section_data (o)->rel_count, |
5737 | elf_section_data (o)->rel_hashes); | |
5738 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
5739 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
5740 | elf_section_data (o)->rel_count2, | |
3e932841 | 5741 | (elf_section_data (o)->rel_hashes |
31367b81 | 5742 | + elf_section_data (o)->rel_count)); |
252b5132 RH |
5743 | |
5744 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
5745 | trying to swap the relocs out itself. */ | |
5746 | o->reloc_count = 0; | |
5747 | } | |
5748 | ||
db6751f2 JJ |
5749 | if (dynamic && info->combreloc && dynobj != NULL) |
5750 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
5751 | ||
252b5132 RH |
5752 | /* If we are linking against a dynamic object, or generating a |
5753 | shared library, finish up the dynamic linking information. */ | |
5754 | if (dynamic) | |
5755 | { | |
5756 | Elf_External_Dyn *dyncon, *dynconend; | |
5757 | ||
5758 | /* Fix up .dynamic entries. */ | |
5759 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
5760 | BFD_ASSERT (o != NULL); | |
5761 | ||
5762 | dyncon = (Elf_External_Dyn *) o->contents; | |
5763 | dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size); | |
5764 | for (; dyncon < dynconend; dyncon++) | |
5765 | { | |
5766 | Elf_Internal_Dyn dyn; | |
5767 | const char *name; | |
5768 | unsigned int type; | |
5769 | ||
5770 | elf_swap_dyn_in (dynobj, dyncon, &dyn); | |
5771 | ||
5772 | switch (dyn.d_tag) | |
5773 | { | |
5774 | default: | |
5775 | break; | |
db6751f2 JJ |
5776 | case DT_NULL: |
5777 | if (relativecount > 0 && dyncon + 1 < dynconend) | |
5778 | { | |
5779 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
5780 | { | |
5781 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
5782 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
5783 | default: break; | |
5784 | } | |
5785 | if (dyn.d_tag != DT_NULL) | |
5786 | { | |
5787 | dyn.d_un.d_val = relativecount; | |
5788 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
5789 | relativecount = 0; | |
5790 | } | |
5791 | } | |
5792 | break; | |
252b5132 | 5793 | case DT_INIT: |
f0c2e336 | 5794 | name = info->init_function; |
252b5132 RH |
5795 | goto get_sym; |
5796 | case DT_FINI: | |
f0c2e336 | 5797 | name = info->fini_function; |
252b5132 RH |
5798 | get_sym: |
5799 | { | |
5800 | struct elf_link_hash_entry *h; | |
5801 | ||
5802 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
b34976b6 | 5803 | FALSE, FALSE, TRUE); |
252b5132 RH |
5804 | if (h != NULL |
5805 | && (h->root.type == bfd_link_hash_defined | |
5806 | || h->root.type == bfd_link_hash_defweak)) | |
5807 | { | |
5808 | dyn.d_un.d_val = h->root.u.def.value; | |
5809 | o = h->root.u.def.section; | |
5810 | if (o->output_section != NULL) | |
5811 | dyn.d_un.d_val += (o->output_section->vma | |
5812 | + o->output_offset); | |
5813 | else | |
5814 | { | |
5815 | /* The symbol is imported from another shared | |
5816 | library and does not apply to this one. */ | |
5817 | dyn.d_un.d_val = 0; | |
5818 | } | |
5819 | ||
5820 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
5821 | } | |
5822 | } | |
5823 | break; | |
5824 | ||
30831527 RH |
5825 | case DT_PREINIT_ARRAYSZ: |
5826 | name = ".preinit_array"; | |
5827 | goto get_size; | |
5828 | case DT_INIT_ARRAYSZ: | |
5829 | name = ".init_array"; | |
5830 | goto get_size; | |
5831 | case DT_FINI_ARRAYSZ: | |
5832 | name = ".fini_array"; | |
5833 | get_size: | |
5834 | o = bfd_get_section_by_name (abfd, name); | |
2cb69dd3 JL |
5835 | if (o == NULL) |
5836 | { | |
5837 | (*_bfd_error_handler) | |
5838 | (_("%s: could not find output section %s"), | |
5839 | bfd_get_filename (abfd), name); | |
5840 | goto error_return; | |
5841 | } | |
25e27870 L |
5842 | if (o->_raw_size == 0) |
5843 | (*_bfd_error_handler) | |
5844 | (_("warning: %s section has zero size"), name); | |
30831527 RH |
5845 | dyn.d_un.d_val = o->_raw_size; |
5846 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
5847 | break; | |
5848 | ||
5849 | case DT_PREINIT_ARRAY: | |
5850 | name = ".preinit_array"; | |
5851 | goto get_vma; | |
5852 | case DT_INIT_ARRAY: | |
5853 | name = ".init_array"; | |
5854 | goto get_vma; | |
5855 | case DT_FINI_ARRAY: | |
5856 | name = ".fini_array"; | |
5857 | goto get_vma; | |
5858 | ||
252b5132 RH |
5859 | case DT_HASH: |
5860 | name = ".hash"; | |
5861 | goto get_vma; | |
5862 | case DT_STRTAB: | |
5863 | name = ".dynstr"; | |
5864 | goto get_vma; | |
5865 | case DT_SYMTAB: | |
5866 | name = ".dynsym"; | |
5867 | goto get_vma; | |
5868 | case DT_VERDEF: | |
5869 | name = ".gnu.version_d"; | |
5870 | goto get_vma; | |
5871 | case DT_VERNEED: | |
5872 | name = ".gnu.version_r"; | |
5873 | goto get_vma; | |
5874 | case DT_VERSYM: | |
5875 | name = ".gnu.version"; | |
5876 | get_vma: | |
5877 | o = bfd_get_section_by_name (abfd, name); | |
2cb69dd3 JL |
5878 | if (o == NULL) |
5879 | { | |
5880 | (*_bfd_error_handler) | |
5881 | (_("%s: could not find output section %s"), | |
5882 | bfd_get_filename (abfd), name); | |
5883 | goto error_return; | |
5884 | } | |
252b5132 RH |
5885 | dyn.d_un.d_ptr = o->vma; |
5886 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
5887 | break; | |
5888 | ||
5889 | case DT_REL: | |
5890 | case DT_RELA: | |
5891 | case DT_RELSZ: | |
5892 | case DT_RELASZ: | |
5893 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
5894 | type = SHT_REL; | |
5895 | else | |
5896 | type = SHT_RELA; | |
5897 | dyn.d_un.d_val = 0; | |
9ad5cbcf | 5898 | for (i = 1; i < elf_numsections (abfd); i++) |
252b5132 RH |
5899 | { |
5900 | Elf_Internal_Shdr *hdr; | |
5901 | ||
5902 | hdr = elf_elfsections (abfd)[i]; | |
5903 | if (hdr->sh_type == type | |
5904 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
5905 | { | |
5906 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
5907 | dyn.d_un.d_val += hdr->sh_size; | |
5908 | else | |
5909 | { | |
5910 | if (dyn.d_un.d_val == 0 | |
5911 | || hdr->sh_addr < dyn.d_un.d_val) | |
5912 | dyn.d_un.d_val = hdr->sh_addr; | |
5913 | } | |
5914 | } | |
5915 | } | |
5916 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
5917 | break; | |
5918 | } | |
5919 | } | |
5920 | } | |
5921 | ||
5922 | /* If we have created any dynamic sections, then output them. */ | |
5923 | if (dynobj != NULL) | |
5924 | { | |
5925 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
5926 | goto error_return; | |
5927 | ||
5928 | for (o = dynobj->sections; o != NULL; o = o->next) | |
5929 | { | |
5930 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
fc8c40a0 AM |
5931 | || o->_raw_size == 0 |
5932 | || o->output_section == bfd_abs_section_ptr) | |
252b5132 RH |
5933 | continue; |
5934 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
5935 | { | |
5936 | /* At this point, we are only interested in sections | |
c44233aa | 5937 | created by elf_link_create_dynamic_sections. */ |
252b5132 RH |
5938 | continue; |
5939 | } | |
5940 | if ((elf_section_data (o->output_section)->this_hdr.sh_type | |
5941 | != SHT_STRTAB) | |
5942 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
5943 | { | |
5944 | if (! bfd_set_section_contents (abfd, o->output_section, | |
dc810e39 AM |
5945 | o->contents, |
5946 | (file_ptr) o->output_offset, | |
252b5132 RH |
5947 | o->_raw_size)) |
5948 | goto error_return; | |
5949 | } | |
5950 | else | |
5951 | { | |
252b5132 | 5952 | /* The contents of the .dynstr section are actually in a |
c44233aa | 5953 | stringtab. */ |
252b5132 RH |
5954 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
5955 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
2b0f7ef9 JJ |
5956 | || ! _bfd_elf_strtab_emit (abfd, |
5957 | elf_hash_table (info)->dynstr)) | |
252b5132 RH |
5958 | goto error_return; |
5959 | } | |
5960 | } | |
5961 | } | |
5962 | ||
1126897b AM |
5963 | if (info->relocateable) |
5964 | { | |
b34976b6 | 5965 | bfd_boolean failed = FALSE; |
1126897b AM |
5966 | |
5967 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
5968 | if (failed) | |
5969 | goto error_return; | |
5970 | } | |
5971 | ||
252b5132 RH |
5972 | /* If we have optimized stabs strings, output them. */ |
5973 | if (elf_hash_table (info)->stab_info != NULL) | |
5974 | { | |
5975 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
5976 | goto error_return; | |
5977 | } | |
5978 | ||
126495ed | 5979 | if (info->eh_frame_hdr) |
65765700 | 5980 | { |
126495ed AM |
5981 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
5982 | goto error_return; | |
65765700 JJ |
5983 | } |
5984 | ||
252b5132 RH |
5985 | if (finfo.symstrtab != NULL) |
5986 | _bfd_stringtab_free (finfo.symstrtab); | |
5987 | if (finfo.contents != NULL) | |
5988 | free (finfo.contents); | |
5989 | if (finfo.external_relocs != NULL) | |
5990 | free (finfo.external_relocs); | |
5991 | if (finfo.internal_relocs != NULL) | |
5992 | free (finfo.internal_relocs); | |
5993 | if (finfo.external_syms != NULL) | |
5994 | free (finfo.external_syms); | |
9ad5cbcf AM |
5995 | if (finfo.locsym_shndx != NULL) |
5996 | free (finfo.locsym_shndx); | |
252b5132 RH |
5997 | if (finfo.internal_syms != NULL) |
5998 | free (finfo.internal_syms); | |
5999 | if (finfo.indices != NULL) | |
6000 | free (finfo.indices); | |
6001 | if (finfo.sections != NULL) | |
6002 | free (finfo.sections); | |
6003 | if (finfo.symbuf != NULL) | |
6004 | free (finfo.symbuf); | |
9ad5cbcf | 6005 | if (finfo.symshndxbuf != NULL) |
c97e73dd | 6006 | free (finfo.symshndxbuf); |
252b5132 RH |
6007 | for (o = abfd->sections; o != NULL; o = o->next) |
6008 | { | |
6009 | if ((o->flags & SEC_RELOC) != 0 | |
6010 | && elf_section_data (o)->rel_hashes != NULL) | |
c44233aa | 6011 | free (elf_section_data (o)->rel_hashes); |
252b5132 RH |
6012 | } |
6013 | ||
b34976b6 | 6014 | elf_tdata (abfd)->linker = TRUE; |
252b5132 | 6015 | |
b34976b6 | 6016 | return TRUE; |
252b5132 RH |
6017 | |
6018 | error_return: | |
6019 | if (finfo.symstrtab != NULL) | |
6020 | _bfd_stringtab_free (finfo.symstrtab); | |
6021 | if (finfo.contents != NULL) | |
6022 | free (finfo.contents); | |
6023 | if (finfo.external_relocs != NULL) | |
6024 | free (finfo.external_relocs); | |
6025 | if (finfo.internal_relocs != NULL) | |
6026 | free (finfo.internal_relocs); | |
6027 | if (finfo.external_syms != NULL) | |
6028 | free (finfo.external_syms); | |
9ad5cbcf AM |
6029 | if (finfo.locsym_shndx != NULL) |
6030 | free (finfo.locsym_shndx); | |
252b5132 RH |
6031 | if (finfo.internal_syms != NULL) |
6032 | free (finfo.internal_syms); | |
6033 | if (finfo.indices != NULL) | |
6034 | free (finfo.indices); | |
6035 | if (finfo.sections != NULL) | |
6036 | free (finfo.sections); | |
6037 | if (finfo.symbuf != NULL) | |
6038 | free (finfo.symbuf); | |
9ad5cbcf | 6039 | if (finfo.symshndxbuf != NULL) |
c97e73dd | 6040 | free (finfo.symshndxbuf); |
252b5132 RH |
6041 | for (o = abfd->sections; o != NULL; o = o->next) |
6042 | { | |
6043 | if ((o->flags & SEC_RELOC) != 0 | |
6044 | && elf_section_data (o)->rel_hashes != NULL) | |
6045 | free (elf_section_data (o)->rel_hashes); | |
6046 | } | |
6047 | ||
b34976b6 | 6048 | return FALSE; |
252b5132 RH |
6049 | } |
6050 | ||
6051 | /* Add a symbol to the output symbol table. */ | |
6052 | ||
b34976b6 | 6053 | static bfd_boolean |
252b5132 RH |
6054 | elf_link_output_sym (finfo, name, elfsym, input_sec) |
6055 | struct elf_final_link_info *finfo; | |
6056 | const char *name; | |
6057 | Elf_Internal_Sym *elfsym; | |
6058 | asection *input_sec; | |
6059 | { | |
9ad5cbcf AM |
6060 | Elf_External_Sym *dest; |
6061 | Elf_External_Sym_Shndx *destshndx; | |
b34976b6 AM |
6062 | bfd_boolean (*output_symbol_hook) |
6063 | PARAMS ((bfd *, struct bfd_link_info *info, const char *, | |
6064 | Elf_Internal_Sym *, asection *)); | |
252b5132 RH |
6065 | |
6066 | output_symbol_hook = get_elf_backend_data (finfo->output_bfd)-> | |
6067 | elf_backend_link_output_symbol_hook; | |
6068 | if (output_symbol_hook != NULL) | |
6069 | { | |
6070 | if (! ((*output_symbol_hook) | |
6071 | (finfo->output_bfd, finfo->info, name, elfsym, input_sec))) | |
b34976b6 | 6072 | return FALSE; |
252b5132 RH |
6073 | } |
6074 | ||
6075 | if (name == (const char *) NULL || *name == '\0') | |
6076 | elfsym->st_name = 0; | |
6077 | else if (input_sec->flags & SEC_EXCLUDE) | |
6078 | elfsym->st_name = 0; | |
6079 | else | |
6080 | { | |
6081 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
b34976b6 | 6082 | name, TRUE, FALSE); |
252b5132 | 6083 | if (elfsym->st_name == (unsigned long) -1) |
b34976b6 | 6084 | return FALSE; |
252b5132 RH |
6085 | } |
6086 | ||
6087 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
6088 | { | |
6089 | if (! elf_link_flush_output_syms (finfo)) | |
b34976b6 | 6090 | return FALSE; |
252b5132 RH |
6091 | } |
6092 | ||
9ad5cbcf AM |
6093 | dest = finfo->symbuf + finfo->symbuf_count; |
6094 | destshndx = finfo->symshndxbuf; | |
6095 | if (destshndx != NULL) | |
c97e73dd AM |
6096 | { |
6097 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
6098 | { | |
6099 | bfd_size_type amt; | |
252b5132 | 6100 | |
c97e73dd AM |
6101 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); |
6102 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
6103 | if (destshndx == NULL) | |
b34976b6 | 6104 | return FALSE; |
c97e73dd AM |
6105 | memset ((char *) destshndx + amt, 0, amt); |
6106 | finfo->shndxbuf_size *= 2; | |
6107 | } | |
6108 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
6109 | } | |
6110 | ||
6111 | elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx); | |
6112 | finfo->symbuf_count += 1; | |
6113 | bfd_get_symcount (finfo->output_bfd) += 1; | |
252b5132 | 6114 | |
b34976b6 | 6115 | return TRUE; |
252b5132 RH |
6116 | } |
6117 | ||
6118 | /* Flush the output symbols to the file. */ | |
6119 | ||
b34976b6 | 6120 | static bfd_boolean |
252b5132 RH |
6121 | elf_link_flush_output_syms (finfo) |
6122 | struct elf_final_link_info *finfo; | |
6123 | { | |
6124 | if (finfo->symbuf_count > 0) | |
6125 | { | |
9ad5cbcf | 6126 | Elf_Internal_Shdr *hdr; |
dc810e39 AM |
6127 | file_ptr pos; |
6128 | bfd_size_type amt; | |
252b5132 | 6129 | |
9ad5cbcf AM |
6130 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; |
6131 | pos = hdr->sh_offset + hdr->sh_size; | |
dc810e39 AM |
6132 | amt = finfo->symbuf_count * sizeof (Elf_External_Sym); |
6133 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
6134 | || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt) | |
b34976b6 | 6135 | return FALSE; |
252b5132 | 6136 | |
9ad5cbcf | 6137 | hdr->sh_size += amt; |
252b5132 RH |
6138 | finfo->symbuf_count = 0; |
6139 | } | |
6140 | ||
b34976b6 | 6141 | return TRUE; |
252b5132 RH |
6142 | } |
6143 | ||
f5fa8ca2 JJ |
6144 | /* Adjust all external symbols pointing into SEC_MERGE sections |
6145 | to reflect the object merging within the sections. */ | |
6146 | ||
b34976b6 | 6147 | static bfd_boolean |
f5fa8ca2 JJ |
6148 | elf_link_sec_merge_syms (h, data) |
6149 | struct elf_link_hash_entry *h; | |
6150 | PTR data; | |
6151 | { | |
6152 | asection *sec; | |
6153 | ||
e92d460e AM |
6154 | if (h->root.type == bfd_link_hash_warning) |
6155 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6156 | ||
f5fa8ca2 JJ |
6157 | if ((h->root.type == bfd_link_hash_defined |
6158 | || h->root.type == bfd_link_hash_defweak) | |
6159 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
68bfbfcc | 6160 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) |
f5fa8ca2 JJ |
6161 | { |
6162 | bfd *output_bfd = (bfd *) data; | |
6163 | ||
6164 | h->root.u.def.value = | |
6165 | _bfd_merged_section_offset (output_bfd, | |
6166 | &h->root.u.def.section, | |
65765700 | 6167 | elf_section_data (sec)->sec_info, |
f5fa8ca2 JJ |
6168 | h->root.u.def.value, (bfd_vma) 0); |
6169 | } | |
6170 | ||
b34976b6 | 6171 | return TRUE; |
f5fa8ca2 JJ |
6172 | } |
6173 | ||
f5d44ba0 AM |
6174 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
6175 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
6176 | versioned symbol that would normally require an explicit version. */ | |
6177 | ||
b34976b6 | 6178 | static bfd_boolean |
f5d44ba0 AM |
6179 | elf_link_check_versioned_symbol (info, h) |
6180 | struct bfd_link_info *info; | |
6181 | struct elf_link_hash_entry *h; | |
6182 | { | |
6183 | bfd *undef_bfd = h->root.u.undef.abfd; | |
6184 | struct elf_link_loaded_list *loaded; | |
f5d44ba0 AM |
6185 | |
6186 | if ((undef_bfd->flags & DYNAMIC) == 0 | |
6187 | || info->hash->creator->flavour != bfd_target_elf_flavour | |
1b1fe8fe | 6188 | || elf_dt_soname (undef_bfd) == NULL) |
b34976b6 | 6189 | return FALSE; |
f5d44ba0 AM |
6190 | |
6191 | for (loaded = elf_hash_table (info)->loaded; | |
6192 | loaded != NULL; | |
6193 | loaded = loaded->next) | |
6194 | { | |
6195 | bfd *input; | |
6196 | Elf_Internal_Shdr *hdr; | |
6197 | bfd_size_type symcount; | |
6198 | bfd_size_type extsymcount; | |
6199 | bfd_size_type extsymoff; | |
6200 | Elf_Internal_Shdr *versymhdr; | |
6cdc0ccc AM |
6201 | Elf_Internal_Sym *isym; |
6202 | Elf_Internal_Sym *isymend; | |
6203 | Elf_Internal_Sym *isymbuf; | |
f5d44ba0 | 6204 | Elf_External_Versym *ever; |
6cdc0ccc | 6205 | Elf_External_Versym *extversym; |
f5d44ba0 AM |
6206 | |
6207 | input = loaded->abfd; | |
6208 | ||
6209 | /* We check each DSO for a possible hidden versioned definition. */ | |
6210 | if (input == undef_bfd | |
6211 | || (input->flags & DYNAMIC) == 0 | |
6212 | || elf_dynversym (input) == 0) | |
6213 | continue; | |
6214 | ||
6215 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
6216 | ||
6217 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); | |
6218 | if (elf_bad_symtab (input)) | |
6219 | { | |
6220 | extsymcount = symcount; | |
6221 | extsymoff = 0; | |
6222 | } | |
6223 | else | |
6224 | { | |
6225 | extsymcount = symcount - hdr->sh_info; | |
6226 | extsymoff = hdr->sh_info; | |
6227 | } | |
6228 | ||
6229 | if (extsymcount == 0) | |
6230 | continue; | |
6231 | ||
6cdc0ccc AM |
6232 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, |
6233 | NULL, NULL, NULL); | |
6234 | if (isymbuf == NULL) | |
b34976b6 | 6235 | return FALSE; |
f5d44ba0 | 6236 | |
f5d44ba0 AM |
6237 | /* Read in any version definitions. */ |
6238 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
6239 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); | |
6240 | if (extversym == NULL) | |
6241 | goto error_ret; | |
6242 | ||
6243 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
6244 | || (bfd_bread ((PTR) extversym, versymhdr->sh_size, input) | |
6245 | != versymhdr->sh_size)) | |
6246 | { | |
6247 | free (extversym); | |
6248 | error_ret: | |
6cdc0ccc | 6249 | free (isymbuf); |
b34976b6 | 6250 | return FALSE; |
f5d44ba0 AM |
6251 | } |
6252 | ||
6253 | ever = extversym + extsymoff; | |
6cdc0ccc AM |
6254 | isymend = isymbuf + extsymcount; |
6255 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
f5d44ba0 AM |
6256 | { |
6257 | const char *name; | |
f5d44ba0 AM |
6258 | Elf_Internal_Versym iver; |
6259 | ||
6cdc0ccc AM |
6260 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL |
6261 | || isym->st_shndx == SHN_UNDEF) | |
f5d44ba0 AM |
6262 | continue; |
6263 | ||
6264 | name = bfd_elf_string_from_elf_section (input, | |
6265 | hdr->sh_link, | |
6cdc0ccc | 6266 | isym->st_name); |
f5d44ba0 AM |
6267 | if (strcmp (name, h->root.root.string) != 0) |
6268 | continue; | |
6269 | ||
6270 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
6271 | ||
6272 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
6273 | { | |
6274 | /* If we have a non-hidden versioned sym, then it should | |
6275 | have provided a definition for the undefined sym. */ | |
6276 | abort (); | |
6277 | } | |
6278 | ||
6279 | if ((iver.vs_vers & VERSYM_VERSION) == 2) | |
6280 | { | |
6281 | /* This is the oldest (default) sym. We can use it. */ | |
6282 | free (extversym); | |
6cdc0ccc | 6283 | free (isymbuf); |
b34976b6 | 6284 | return TRUE; |
f5d44ba0 AM |
6285 | } |
6286 | } | |
6287 | ||
6288 | free (extversym); | |
6cdc0ccc | 6289 | free (isymbuf); |
f5d44ba0 AM |
6290 | } |
6291 | ||
b34976b6 | 6292 | return FALSE; |
f5d44ba0 AM |
6293 | } |
6294 | ||
252b5132 RH |
6295 | /* Add an external symbol to the symbol table. This is called from |
6296 | the hash table traversal routine. When generating a shared object, | |
6297 | we go through the symbol table twice. The first time we output | |
6298 | anything that might have been forced to local scope in a version | |
6299 | script. The second time we output the symbols that are still | |
6300 | global symbols. */ | |
6301 | ||
b34976b6 | 6302 | static bfd_boolean |
252b5132 RH |
6303 | elf_link_output_extsym (h, data) |
6304 | struct elf_link_hash_entry *h; | |
6305 | PTR data; | |
6306 | { | |
6307 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; | |
6308 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
b34976b6 | 6309 | bfd_boolean strip; |
252b5132 RH |
6310 | Elf_Internal_Sym sym; |
6311 | asection *input_sec; | |
6312 | ||
e92d460e AM |
6313 | if (h->root.type == bfd_link_hash_warning) |
6314 | { | |
6315 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6316 | if (h->root.type == bfd_link_hash_new) | |
b34976b6 | 6317 | return TRUE; |
e92d460e AM |
6318 | } |
6319 | ||
252b5132 RH |
6320 | /* Decide whether to output this symbol in this pass. */ |
6321 | if (eoinfo->localsyms) | |
6322 | { | |
6323 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
b34976b6 | 6324 | return TRUE; |
252b5132 RH |
6325 | } |
6326 | else | |
6327 | { | |
6328 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
b34976b6 | 6329 | return TRUE; |
252b5132 RH |
6330 | } |
6331 | ||
6332 | /* If we are not creating a shared library, and this symbol is | |
6333 | referenced by a shared library but is not defined anywhere, then | |
6334 | warn that it is undefined. If we do not do this, the runtime | |
6335 | linker will complain that the symbol is undefined when the | |
6336 | program is run. We don't have to worry about symbols that are | |
6337 | referenced by regular files, because we will already have issued | |
6338 | warnings for them. */ | |
6339 | if (! finfo->info->relocateable | |
ae9a127f | 6340 | && (! finfo->info->shared || ! finfo->info->allow_shlib_undefined) |
252b5132 RH |
6341 | && h->root.type == bfd_link_hash_undefined |
6342 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0 | |
f5d44ba0 AM |
6343 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 |
6344 | && ! elf_link_check_versioned_symbol (finfo->info, h)) | |
252b5132 RH |
6345 | { |
6346 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
6347 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
b34976b6 | 6348 | (asection *) NULL, (bfd_vma) 0, TRUE))) |
252b5132 | 6349 | { |
b34976b6 AM |
6350 | eoinfo->failed = TRUE; |
6351 | return FALSE; | |
252b5132 RH |
6352 | } |
6353 | } | |
6354 | ||
1b1fe8fe L |
6355 | /* We should also warn if a forced local symbol is referenced from |
6356 | shared libraries. */ | |
6357 | if (! finfo->info->relocateable | |
6358 | && (! finfo->info->shared || ! finfo->info->allow_shlib_undefined) | |
6359 | && (h->elf_link_hash_flags | |
6360 | & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | |
6361 | | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK)) | |
6362 | == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)) | |
6363 | { | |
6364 | (*_bfd_error_handler) | |
6365 | (_("%s: %s symbol `%s' in %s is referenced by DSO"), | |
6366 | bfd_get_filename (finfo->output_bfd), | |
6367 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
6368 | ? "internal" | |
6369 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
6370 | ? "hidden" : "local", | |
6371 | h->root.root.string, | |
6372 | bfd_archive_filename (h->root.u.def.section->owner)); | |
6373 | eoinfo->failed = TRUE; | |
6374 | return FALSE; | |
6375 | } | |
6376 | ||
252b5132 RH |
6377 | /* We don't want to output symbols that have never been mentioned by |
6378 | a regular file, or that we have been told to strip. However, if | |
6379 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6380 | output it. */ | |
6381 | if (h->indx == -2) | |
b34976b6 | 6382 | strip = FALSE; |
252b5132 RH |
6383 | else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
6384 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) | |
6385 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
6386 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) | |
b34976b6 | 6387 | strip = TRUE; |
75828352 AM |
6388 | else if (finfo->info->strip == strip_all) |
6389 | strip = TRUE; | |
6390 | else if (finfo->info->strip == strip_some | |
6391 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6392 | h->root.root.string, FALSE, FALSE) == NULL) | |
6393 | strip = TRUE; | |
6394 | else if (finfo->info->strip_discarded | |
6395 | && (h->root.type == bfd_link_hash_defined | |
6396 | || h->root.type == bfd_link_hash_defweak) | |
6397 | && elf_discarded_section (h->root.u.def.section)) | |
b34976b6 | 6398 | strip = TRUE; |
252b5132 | 6399 | else |
b34976b6 | 6400 | strip = FALSE; |
252b5132 RH |
6401 | |
6402 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
2bd171e0 ILT |
6403 | nothing else to do unless it is a forced local symbol. */ |
6404 | if (strip | |
6405 | && h->dynindx == -1 | |
6406 | && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
b34976b6 | 6407 | return TRUE; |
252b5132 RH |
6408 | |
6409 | sym.st_value = 0; | |
6410 | sym.st_size = h->size; | |
6411 | sym.st_other = h->other; | |
6412 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
6413 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); | |
6414 | else if (h->root.type == bfd_link_hash_undefweak | |
6415 | || h->root.type == bfd_link_hash_defweak) | |
6416 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6417 | else | |
6418 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6419 | ||
6420 | switch (h->root.type) | |
6421 | { | |
6422 | default: | |
6423 | case bfd_link_hash_new: | |
e92d460e | 6424 | case bfd_link_hash_warning: |
252b5132 | 6425 | abort (); |
b34976b6 | 6426 | return FALSE; |
252b5132 RH |
6427 | |
6428 | case bfd_link_hash_undefined: | |
252b5132 RH |
6429 | case bfd_link_hash_undefweak: |
6430 | input_sec = bfd_und_section_ptr; | |
6431 | sym.st_shndx = SHN_UNDEF; | |
6432 | break; | |
6433 | ||
6434 | case bfd_link_hash_defined: | |
6435 | case bfd_link_hash_defweak: | |
6436 | { | |
6437 | input_sec = h->root.u.def.section; | |
6438 | if (input_sec->output_section != NULL) | |
6439 | { | |
6440 | sym.st_shndx = | |
6441 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
6442 | input_sec->output_section); | |
9ad5cbcf | 6443 | if (sym.st_shndx == SHN_BAD) |
252b5132 RH |
6444 | { |
6445 | (*_bfd_error_handler) | |
6446 | (_("%s: could not find output section %s for input section %s"), | |
6447 | bfd_get_filename (finfo->output_bfd), | |
6448 | input_sec->output_section->name, | |
6449 | input_sec->name); | |
b34976b6 AM |
6450 | eoinfo->failed = TRUE; |
6451 | return FALSE; | |
252b5132 RH |
6452 | } |
6453 | ||
6454 | /* ELF symbols in relocateable files are section relative, | |
6455 | but in nonrelocateable files they are virtual | |
6456 | addresses. */ | |
6457 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
6458 | if (! finfo->info->relocateable) | |
13ae64f3 JJ |
6459 | { |
6460 | sym.st_value += input_sec->output_section->vma; | |
6461 | if (h->type == STT_TLS) | |
6462 | { | |
6463 | /* STT_TLS symbols are relative to PT_TLS segment | |
6464 | base. */ | |
6465 | BFD_ASSERT (finfo->first_tls_sec != NULL); | |
6466 | sym.st_value -= finfo->first_tls_sec->vma; | |
6467 | } | |
6468 | } | |
252b5132 RH |
6469 | } |
6470 | else | |
6471 | { | |
6472 | BFD_ASSERT (input_sec->owner == NULL | |
6473 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
6474 | sym.st_shndx = SHN_UNDEF; | |
6475 | input_sec = bfd_und_section_ptr; | |
6476 | } | |
6477 | } | |
6478 | break; | |
6479 | ||
6480 | case bfd_link_hash_common: | |
6481 | input_sec = h->root.u.c.p->section; | |
6482 | sym.st_shndx = SHN_COMMON; | |
6483 | sym.st_value = 1 << h->root.u.c.p->alignment_power; | |
6484 | break; | |
6485 | ||
6486 | case bfd_link_hash_indirect: | |
6487 | /* These symbols are created by symbol versioning. They point | |
c44233aa AM |
6488 | to the decorated version of the name. For example, if the |
6489 | symbol foo@@GNU_1.2 is the default, which should be used when | |
6490 | foo is used with no version, then we add an indirect symbol | |
6491 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
6492 | since the indirected symbol is already in the hash table. */ | |
b34976b6 | 6493 | return TRUE; |
252b5132 RH |
6494 | } |
6495 | ||
6496 | /* Give the processor backend a chance to tweak the symbol value, | |
6497 | and also to finish up anything that needs to be done for this | |
c44233aa AM |
6498 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for |
6499 | forced local syms when non-shared is due to a historical quirk. */ | |
252b5132 RH |
6500 | if ((h->dynindx != -1 |
6501 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
c44233aa AM |
6502 | && (finfo->info->shared |
6503 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
252b5132 RH |
6504 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
6505 | { | |
6506 | struct elf_backend_data *bed; | |
6507 | ||
6508 | bed = get_elf_backend_data (finfo->output_bfd); | |
6509 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
6510 | (finfo->output_bfd, finfo->info, h, &sym))) | |
6511 | { | |
b34976b6 AM |
6512 | eoinfo->failed = TRUE; |
6513 | return FALSE; | |
252b5132 RH |
6514 | } |
6515 | } | |
6516 | ||
6517 | /* If we are marking the symbol as undefined, and there are no | |
6518 | non-weak references to this symbol from a regular object, then | |
91d3970e ILT |
6519 | mark the symbol as weak undefined; if there are non-weak |
6520 | references, mark the symbol as strong. We can't do this earlier, | |
252b5132 RH |
6521 | because it might not be marked as undefined until the |
6522 | finish_dynamic_symbol routine gets through with it. */ | |
6523 | if (sym.st_shndx == SHN_UNDEF | |
252b5132 | 6524 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 |
a7b97311 AM |
6525 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
6526 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
91d3970e ILT |
6527 | { |
6528 | int bindtype; | |
6529 | ||
6530 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0) | |
6531 | bindtype = STB_GLOBAL; | |
6532 | else | |
6533 | bindtype = STB_WEAK; | |
6534 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
6535 | } | |
252b5132 | 6536 | |
1b1fe8fe L |
6537 | /* If a non-weak symbol with non-default visibility is not defined |
6538 | locally, it is a fatal error. */ | |
2cd533b7 | 6539 | if (! finfo->info->relocateable |
1b1fe8fe L |
6540 | && ELF_ST_VISIBILITY (sym.st_other) |
6541 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
6542 | && h->root.type != bfd_link_hash_undefweak | |
2cd533b7 | 6543 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) |
1b1fe8fe L |
6544 | { |
6545 | (*_bfd_error_handler) | |
6546 | (_("%s: %s symbol `%s' isn't defined"), | |
6547 | bfd_get_filename (finfo->output_bfd), | |
6548 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
6549 | ? "protected" | |
6550 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
6551 | ? "internal" : "hidden", | |
6552 | h->root.root.string); | |
6553 | eoinfo->failed = TRUE; | |
6554 | return FALSE; | |
6555 | } | |
32c092c3 | 6556 | |
252b5132 | 6557 | /* If this symbol should be put in the .dynsym section, then put it |
f5d44ba0 AM |
6558 | there now. We already know the symbol index. We also fill in |
6559 | the entry in the .hash section. */ | |
252b5132 RH |
6560 | if (h->dynindx != -1 |
6561 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6562 | { | |
6563 | size_t bucketcount; | |
6564 | size_t bucket; | |
c7ac6ff8 | 6565 | size_t hash_entry_size; |
252b5132 RH |
6566 | bfd_byte *bucketpos; |
6567 | bfd_vma chain; | |
dc810e39 | 6568 | Elf_External_Sym *esym; |
252b5132 RH |
6569 | |
6570 | sym.st_name = h->dynstr_index; | |
dc810e39 | 6571 | esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx; |
9ad5cbcf | 6572 | elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0); |
252b5132 RH |
6573 | |
6574 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
6575 | bucket = h->elf_hash_value % bucketcount; | |
3e932841 | 6576 | hash_entry_size |
c7ac6ff8 | 6577 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; |
252b5132 | 6578 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents |
c7ac6ff8 MM |
6579 | + (bucket + 2) * hash_entry_size); |
6580 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
dc810e39 AM |
6581 | bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx, |
6582 | bucketpos); | |
c7ac6ff8 MM |
6583 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, |
6584 | ((bfd_byte *) finfo->hash_sec->contents | |
6585 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
252b5132 RH |
6586 | |
6587 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
6588 | { | |
6589 | Elf_Internal_Versym iversym; | |
dc810e39 | 6590 | Elf_External_Versym *eversym; |
252b5132 RH |
6591 | |
6592 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
6593 | { | |
6594 | if (h->verinfo.verdef == NULL) | |
6595 | iversym.vs_vers = 0; | |
6596 | else | |
6597 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
6598 | } | |
6599 | else | |
6600 | { | |
6601 | if (h->verinfo.vertree == NULL) | |
6602 | iversym.vs_vers = 1; | |
6603 | else | |
6604 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
6605 | } | |
6606 | ||
6607 | if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0) | |
6608 | iversym.vs_vers |= VERSYM_HIDDEN; | |
6609 | ||
dc810e39 AM |
6610 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; |
6611 | eversym += h->dynindx; | |
6612 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
252b5132 RH |
6613 | } |
6614 | } | |
6615 | ||
6616 | /* If we're stripping it, then it was just a dynamic symbol, and | |
6617 | there's nothing else to do. */ | |
7330fb86 | 6618 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) |
b34976b6 | 6619 | return TRUE; |
252b5132 RH |
6620 | |
6621 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
6622 | ||
6623 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec)) | |
6624 | { | |
b34976b6 AM |
6625 | eoinfo->failed = TRUE; |
6626 | return FALSE; | |
252b5132 RH |
6627 | } |
6628 | ||
b34976b6 | 6629 | return TRUE; |
252b5132 RH |
6630 | } |
6631 | ||
23bc299b MM |
6632 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which |
6633 | originated from the section given by INPUT_REL_HDR) to the | |
6634 | OUTPUT_BFD. */ | |
6635 | ||
b34976b6 | 6636 | static bfd_boolean |
3e932841 | 6637 | elf_link_output_relocs (output_bfd, input_section, input_rel_hdr, |
23bc299b MM |
6638 | internal_relocs) |
6639 | bfd *output_bfd; | |
6640 | asection *input_section; | |
6641 | Elf_Internal_Shdr *input_rel_hdr; | |
6642 | Elf_Internal_Rela *internal_relocs; | |
6643 | { | |
6644 | Elf_Internal_Rela *irela; | |
6645 | Elf_Internal_Rela *irelaend; | |
947216bf | 6646 | bfd_byte *erel; |
23bc299b MM |
6647 | Elf_Internal_Shdr *output_rel_hdr; |
6648 | asection *output_section; | |
7442e600 | 6649 | unsigned int *rel_countp = NULL; |
32f0787a | 6650 | struct elf_backend_data *bed; |
947216bf | 6651 | void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *)); |
23bc299b MM |
6652 | |
6653 | output_section = input_section->output_section; | |
6654 | output_rel_hdr = NULL; | |
6655 | ||
3e932841 | 6656 | if (elf_section_data (output_section)->rel_hdr.sh_entsize |
23bc299b MM |
6657 | == input_rel_hdr->sh_entsize) |
6658 | { | |
6659 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
6660 | rel_countp = &elf_section_data (output_section)->rel_count; | |
6661 | } | |
6662 | else if (elf_section_data (output_section)->rel_hdr2 | |
6663 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
6664 | == input_rel_hdr->sh_entsize)) | |
6665 | { | |
6666 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
6667 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
6668 | } | |
41241523 TS |
6669 | else |
6670 | { | |
58821868 AM |
6671 | (*_bfd_error_handler) |
6672 | (_("%s: relocation size mismatch in %s section %s"), | |
6673 | bfd_get_filename (output_bfd), | |
6674 | bfd_archive_filename (input_section->owner), | |
6675 | input_section->name); | |
41241523 | 6676 | bfd_set_error (bfd_error_wrong_object_format); |
b34976b6 | 6677 | return FALSE; |
41241523 | 6678 | } |
32f0787a UC |
6679 | |
6680 | bed = get_elf_backend_data (output_bfd); | |
947216bf AM |
6681 | if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) |
6682 | swap_out = bed->s->swap_reloc_out; | |
6683 | else if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela)) | |
6684 | swap_out = bed->s->swap_reloca_out; | |
6685 | else | |
6686 | abort (); | |
6687 | ||
6688 | erel = output_rel_hdr->contents; | |
6689 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
23bc299b | 6690 | irela = internal_relocs; |
58821868 AM |
6691 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) |
6692 | * bed->s->int_rels_per_ext_rel); | |
947216bf | 6693 | while (irela < irelaend) |
23bc299b | 6694 | { |
947216bf AM |
6695 | (*swap_out) (output_bfd, irela, erel); |
6696 | irela += bed->s->int_rels_per_ext_rel; | |
6697 | erel += input_rel_hdr->sh_entsize; | |
23bc299b MM |
6698 | } |
6699 | ||
6700 | /* Bump the counter, so that we know where to add the next set of | |
6701 | relocations. */ | |
d9bc7a44 | 6702 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); |
41241523 | 6703 | |
b34976b6 | 6704 | return TRUE; |
23bc299b MM |
6705 | } |
6706 | ||
252b5132 RH |
6707 | /* Link an input file into the linker output file. This function |
6708 | handles all the sections and relocations of the input file at once. | |
6709 | This is so that we only have to read the local symbols once, and | |
6710 | don't have to keep them in memory. */ | |
6711 | ||
b34976b6 | 6712 | static bfd_boolean |
252b5132 RH |
6713 | elf_link_input_bfd (finfo, input_bfd) |
6714 | struct elf_final_link_info *finfo; | |
6715 | bfd *input_bfd; | |
6716 | { | |
b34976b6 AM |
6717 | bfd_boolean (*relocate_section) |
6718 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
6719 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
252b5132 RH |
6720 | bfd *output_bfd; |
6721 | Elf_Internal_Shdr *symtab_hdr; | |
6722 | size_t locsymcount; | |
6723 | size_t extsymoff; | |
6cdc0ccc | 6724 | Elf_Internal_Sym *isymbuf; |
252b5132 | 6725 | Elf_Internal_Sym *isym; |
6cdc0ccc | 6726 | Elf_Internal_Sym *isymend; |
252b5132 RH |
6727 | long *pindex; |
6728 | asection **ppsection; | |
6729 | asection *o; | |
c7ac6ff8 | 6730 | struct elf_backend_data *bed; |
b34976b6 | 6731 | bfd_boolean emit_relocs; |
f8deed93 | 6732 | struct elf_link_hash_entry **sym_hashes; |
252b5132 RH |
6733 | |
6734 | output_bfd = finfo->output_bfd; | |
c7ac6ff8 MM |
6735 | bed = get_elf_backend_data (output_bfd); |
6736 | relocate_section = bed->elf_backend_relocate_section; | |
252b5132 RH |
6737 | |
6738 | /* If this is a dynamic object, we don't want to do anything here: | |
6739 | we don't want the local symbols, and we don't want the section | |
6740 | contents. */ | |
6741 | if ((input_bfd->flags & DYNAMIC) != 0) | |
b34976b6 | 6742 | return TRUE; |
252b5132 | 6743 | |
9317eacc | 6744 | emit_relocs = (finfo->info->relocateable |
c44233aa AM |
6745 | || finfo->info->emitrelocations |
6746 | || bed->elf_backend_emit_relocs); | |
9317eacc | 6747 | |
252b5132 RH |
6748 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
6749 | if (elf_bad_symtab (input_bfd)) | |
6750 | { | |
6751 | locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym); | |
6752 | extsymoff = 0; | |
6753 | } | |
6754 | else | |
6755 | { | |
6756 | locsymcount = symtab_hdr->sh_info; | |
6757 | extsymoff = symtab_hdr->sh_info; | |
6758 | } | |
6759 | ||
6760 | /* Read the local symbols. */ | |
6cdc0ccc AM |
6761 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; |
6762 | if (isymbuf == NULL && locsymcount != 0) | |
6763 | { | |
6764 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
6765 | finfo->internal_syms, | |
6766 | finfo->external_syms, | |
6767 | finfo->locsym_shndx); | |
6768 | if (isymbuf == NULL) | |
b34976b6 | 6769 | return FALSE; |
252b5132 RH |
6770 | } |
6771 | ||
6cdc0ccc AM |
6772 | /* Find local symbol sections and adjust values of symbols in |
6773 | SEC_MERGE sections. Write out those local symbols we know are | |
6774 | going into the output file. */ | |
6775 | isymend = isymbuf + locsymcount; | |
6776 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
6777 | isym < isymend; | |
6778 | isym++, pindex++, ppsection++) | |
252b5132 RH |
6779 | { |
6780 | asection *isec; | |
6781 | const char *name; | |
6782 | Elf_Internal_Sym osym; | |
6783 | ||
252b5132 RH |
6784 | *pindex = -1; |
6785 | ||
6786 | if (elf_bad_symtab (input_bfd)) | |
6787 | { | |
6788 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6789 | { | |
6790 | *ppsection = NULL; | |
6791 | continue; | |
6792 | } | |
6793 | } | |
6794 | ||
6795 | if (isym->st_shndx == SHN_UNDEF) | |
862517b6 | 6796 | isec = bfd_und_section_ptr; |
9ad5cbcf AM |
6797 | else if (isym->st_shndx < SHN_LORESERVE |
6798 | || isym->st_shndx > SHN_HIRESERVE) | |
f5fa8ca2 JJ |
6799 | { |
6800 | isec = section_from_elf_index (input_bfd, isym->st_shndx); | |
65765700 | 6801 | if (isec |
68bfbfcc | 6802 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE |
f5fa8ca2 JJ |
6803 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
6804 | isym->st_value = | |
6805 | _bfd_merged_section_offset (output_bfd, &isec, | |
65765700 | 6806 | elf_section_data (isec)->sec_info, |
f5fa8ca2 JJ |
6807 | isym->st_value, (bfd_vma) 0); |
6808 | } | |
252b5132 | 6809 | else if (isym->st_shndx == SHN_ABS) |
862517b6 | 6810 | isec = bfd_abs_section_ptr; |
252b5132 | 6811 | else if (isym->st_shndx == SHN_COMMON) |
862517b6 | 6812 | isec = bfd_com_section_ptr; |
252b5132 RH |
6813 | else |
6814 | { | |
6815 | /* Who knows? */ | |
6816 | isec = NULL; | |
6817 | } | |
6818 | ||
6819 | *ppsection = isec; | |
6820 | ||
6821 | /* Don't output the first, undefined, symbol. */ | |
6cdc0ccc | 6822 | if (ppsection == finfo->sections) |
252b5132 RH |
6823 | continue; |
6824 | ||
24376d1b AM |
6825 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) |
6826 | { | |
24376d1b AM |
6827 | /* We never output section symbols. Instead, we use the |
6828 | section symbol of the corresponding section in the output | |
6829 | file. */ | |
6830 | continue; | |
6831 | } | |
6832 | ||
252b5132 RH |
6833 | /* If we are stripping all symbols, we don't want to output this |
6834 | one. */ | |
6835 | if (finfo->info->strip == strip_all) | |
6836 | continue; | |
6837 | ||
252b5132 RH |
6838 | /* If we are discarding all local symbols, we don't want to |
6839 | output this one. If we are generating a relocateable output | |
6840 | file, then some of the local symbols may be required by | |
6841 | relocs; we output them below as we discover that they are | |
6842 | needed. */ | |
6843 | if (finfo->info->discard == discard_all) | |
6844 | continue; | |
6845 | ||
6846 | /* If this symbol is defined in a section which we are | |
c44233aa AM |
6847 | discarding, we don't need to keep it, but note that |
6848 | linker_mark is only reliable for sections that have contents. | |
6849 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
6850 | as well as linker_mark. */ | |
9ad5cbcf | 6851 | if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) |
252b5132 RH |
6852 | && isec != NULL |
6853 | && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) | |
6854 | || (! finfo->info->relocateable | |
6855 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
6856 | continue; | |
6857 | ||
6858 | /* Get the name of the symbol. */ | |
6859 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6860 | isym->st_name); | |
6861 | if (name == NULL) | |
b34976b6 | 6862 | return FALSE; |
252b5132 RH |
6863 | |
6864 | /* See if we are discarding symbols with this name. */ | |
6865 | if ((finfo->info->strip == strip_some | |
b34976b6 | 6866 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) |
252b5132 | 6867 | == NULL)) |
f5fa8ca2 JJ |
6868 | || (((finfo->info->discard == discard_sec_merge |
6869 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable) | |
6870 | || finfo->info->discard == discard_l) | |
252b5132 RH |
6871 | && bfd_is_local_label_name (input_bfd, name))) |
6872 | continue; | |
6873 | ||
6874 | /* If we get here, we are going to output this symbol. */ | |
6875 | ||
6876 | osym = *isym; | |
6877 | ||
6878 | /* Adjust the section index for the output file. */ | |
6879 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
6880 | isec->output_section); | |
9ad5cbcf | 6881 | if (osym.st_shndx == SHN_BAD) |
b34976b6 | 6882 | return FALSE; |
252b5132 RH |
6883 | |
6884 | *pindex = bfd_get_symcount (output_bfd); | |
6885 | ||
6886 | /* ELF symbols in relocateable files are section relative, but | |
6887 | in executable files they are virtual addresses. Note that | |
6888 | this code assumes that all ELF sections have an associated | |
6889 | BFD section with a reasonable value for output_offset; below | |
6890 | we assume that they also have a reasonable value for | |
6891 | output_section. Any special sections must be set up to meet | |
6892 | these requirements. */ | |
6893 | osym.st_value += isec->output_offset; | |
6894 | if (! finfo->info->relocateable) | |
13ae64f3 JJ |
6895 | { |
6896 | osym.st_value += isec->output_section->vma; | |
6897 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
6898 | { | |
6899 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
6900 | BFD_ASSERT (finfo->first_tls_sec != NULL); | |
6901 | osym.st_value -= finfo->first_tls_sec->vma; | |
6902 | } | |
6903 | } | |
252b5132 RH |
6904 | |
6905 | if (! elf_link_output_sym (finfo, name, &osym, isec)) | |
b34976b6 | 6906 | return FALSE; |
252b5132 RH |
6907 | } |
6908 | ||
6909 | /* Relocate the contents of each section. */ | |
f8deed93 | 6910 | sym_hashes = elf_sym_hashes (input_bfd); |
252b5132 RH |
6911 | for (o = input_bfd->sections; o != NULL; o = o->next) |
6912 | { | |
6913 | bfd_byte *contents; | |
6914 | ||
6915 | if (! o->linker_mark) | |
6916 | { | |
6917 | /* This section was omitted from the link. */ | |
6918 | continue; | |
6919 | } | |
6920 | ||
6921 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
6922 | || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0)) | |
6923 | continue; | |
6924 | ||
6925 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
6926 | { | |
6927 | /* Section was created by elf_link_create_dynamic_sections | |
6928 | or somesuch. */ | |
6929 | continue; | |
6930 | } | |
6931 | ||
6932 | /* Get the contents of the section. They have been cached by a | |
c44233aa AM |
6933 | relaxation routine. Note that o is a section in an input |
6934 | file, so the contents field will not have been set by any of | |
6935 | the routines which work on output files. */ | |
252b5132 RH |
6936 | if (elf_section_data (o)->this_hdr.contents != NULL) |
6937 | contents = elf_section_data (o)->this_hdr.contents; | |
6938 | else | |
6939 | { | |
6940 | contents = finfo->contents; | |
6941 | if (! bfd_get_section_contents (input_bfd, o, contents, | |
6942 | (file_ptr) 0, o->_raw_size)) | |
b34976b6 | 6943 | return FALSE; |
252b5132 RH |
6944 | } |
6945 | ||
6946 | if ((o->flags & SEC_RELOC) != 0) | |
6947 | { | |
6948 | Elf_Internal_Rela *internal_relocs; | |
6949 | ||
6950 | /* Get the swapped relocs. */ | |
6951 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) | |
6952 | (input_bfd, o, finfo->external_relocs, | |
b34976b6 | 6953 | finfo->internal_relocs, FALSE)); |
252b5132 RH |
6954 | if (internal_relocs == NULL |
6955 | && o->reloc_count > 0) | |
b34976b6 | 6956 | return FALSE; |
252b5132 | 6957 | |
ec338859 AM |
6958 | /* Run through the relocs looking for any against symbols |
6959 | from discarded sections and section symbols from | |
6960 | removed link-once sections. Complain about relocs | |
6961 | against discarded sections. Zero relocs against removed | |
126495ed | 6962 | link-once sections. */ |
73d074b4 DJ |
6963 | if (!finfo->info->relocateable |
6964 | && !elf_section_ignore_discarded_relocs (o)) | |
ec338859 AM |
6965 | { |
6966 | Elf_Internal_Rela *rel, *relend; | |
50b4d486 | 6967 | |
ec338859 AM |
6968 | rel = internal_relocs; |
6969 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6970 | for ( ; rel < relend; rel++) | |
6971 | { | |
6972 | unsigned long r_symndx = ELF_R_SYM (rel->r_info); | |
6973 | ||
6974 | if (r_symndx >= locsymcount | |
6975 | || (elf_bad_symtab (input_bfd) | |
6976 | && finfo->sections[r_symndx] == NULL)) | |
6977 | { | |
6978 | struct elf_link_hash_entry *h; | |
6979 | ||
6980 | h = sym_hashes[r_symndx - extsymoff]; | |
6981 | while (h->root.type == bfd_link_hash_indirect | |
6982 | || h->root.type == bfd_link_hash_warning) | |
6983 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6984 | ||
6985 | /* Complain if the definition comes from a | |
6986 | discarded section. */ | |
6987 | if ((h->root.type == bfd_link_hash_defined | |
6988 | || h->root.type == bfd_link_hash_defweak) | |
ed4de5e2 | 6989 | && elf_discarded_section (h->root.u.def.section)) |
ec338859 | 6990 | { |
ec338859 AM |
6991 | if ((o->flags & SEC_DEBUGGING) != 0) |
6992 | { | |
45e9217a | 6993 | BFD_ASSERT (r_symndx != 0); |
f8deed93 | 6994 | memset (rel, 0, sizeof (*rel)); |
ec338859 AM |
6995 | } |
6996 | else | |
ec338859 AM |
6997 | { |
6998 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
6999 | (finfo->info, h->root.root.string, | |
7000 | input_bfd, o, rel->r_offset, | |
b34976b6 AM |
7001 | TRUE))) |
7002 | return FALSE; | |
ec338859 AM |
7003 | } |
7004 | } | |
7005 | } | |
7006 | else | |
7007 | { | |
f9f32305 | 7008 | asection *sec = finfo->sections[r_symndx]; |
50b4d486 | 7009 | |
ed4de5e2 | 7010 | if (sec != NULL && elf_discarded_section (sec)) |
f9f32305 | 7011 | { |
ad43ed4c L |
7012 | if ((o->flags & SEC_DEBUGGING) != 0 |
7013 | || (sec->flags & SEC_LINK_ONCE) != 0) | |
f9f32305 | 7014 | { |
45e9217a | 7015 | BFD_ASSERT (r_symndx != 0); |
f9f32305 AM |
7016 | rel->r_info |
7017 | = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info)); | |
7018 | rel->r_addend = 0; | |
7019 | } | |
7020 | else | |
f9f32305 | 7021 | { |
b34976b6 | 7022 | bfd_boolean ok; |
f9f32305 AM |
7023 | const char *msg |
7024 | = _("local symbols in discarded section %s"); | |
7025 | bfd_size_type amt | |
7026 | = strlen (sec->name) + strlen (msg) - 1; | |
7027 | char *buf = (char *) bfd_malloc (amt); | |
7028 | ||
7029 | if (buf != NULL) | |
7030 | sprintf (buf, msg, sec->name); | |
7031 | else | |
7032 | buf = (char *) sec->name; | |
7033 | ok = (*finfo->info->callbacks | |
7034 | ->undefined_symbol) (finfo->info, buf, | |
7035 | input_bfd, o, | |
7036 | rel->r_offset, | |
b34976b6 | 7037 | TRUE); |
f9f32305 AM |
7038 | if (buf != sec->name) |
7039 | free (buf); | |
7040 | if (!ok) | |
b34976b6 | 7041 | return FALSE; |
ec338859 AM |
7042 | } |
7043 | } | |
7044 | } | |
7045 | } | |
7046 | } | |
50b4d486 | 7047 | |
252b5132 RH |
7048 | /* Relocate the section by invoking a back end routine. |
7049 | ||
7050 | The back end routine is responsible for adjusting the | |
7051 | section contents as necessary, and (if using Rela relocs | |
7052 | and generating a relocateable output file) adjusting the | |
7053 | reloc addend as necessary. | |
7054 | ||
7055 | The back end routine does not have to worry about setting | |
7056 | the reloc address or the reloc symbol index. | |
7057 | ||
7058 | The back end routine is given a pointer to the swapped in | |
7059 | internal symbols, and can access the hash table entries | |
7060 | for the external symbols via elf_sym_hashes (input_bfd). | |
7061 | ||
7062 | When generating relocateable output, the back end routine | |
7063 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
7064 | output symbol is going to be a section symbol | |
7065 | corresponding to the output section, which will require | |
7066 | the addend to be adjusted. */ | |
7067 | ||
7068 | if (! (*relocate_section) (output_bfd, finfo->info, | |
7069 | input_bfd, o, contents, | |
7070 | internal_relocs, | |
6cdc0ccc | 7071 | isymbuf, |
252b5132 | 7072 | finfo->sections)) |
b34976b6 | 7073 | return FALSE; |
252b5132 | 7074 | |
9317eacc | 7075 | if (emit_relocs) |
252b5132 RH |
7076 | { |
7077 | Elf_Internal_Rela *irela; | |
7078 | Elf_Internal_Rela *irelaend; | |
73722af0 | 7079 | bfd_vma last_offset; |
252b5132 | 7080 | struct elf_link_hash_entry **rel_hash; |
c89583f8 | 7081 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
4e8a9624 | 7082 | unsigned int next_erel; |
b34976b6 AM |
7083 | bfd_boolean (*reloc_emitter) |
7084 | PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, | |
7085 | Elf_Internal_Rela *)); | |
7086 | bfd_boolean rela_normal; | |
b491616a AM |
7087 | |
7088 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
7089 | rela_normal = (bed->rela_normal | |
7090 | && (input_rel_hdr->sh_entsize | |
7091 | == sizeof (Elf_External_Rela))); | |
252b5132 RH |
7092 | |
7093 | /* Adjust the reloc addresses and symbol indices. */ | |
7094 | ||
7095 | irela = internal_relocs; | |
dc810e39 | 7096 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; |
252b5132 | 7097 | rel_hash = (elf_section_data (o->output_section)->rel_hashes |
31367b81 MM |
7098 | + elf_section_data (o->output_section)->rel_count |
7099 | + elf_section_data (o->output_section)->rel_count2); | |
73722af0 AM |
7100 | last_offset = o->output_offset; |
7101 | if (!finfo->info->relocateable) | |
7102 | last_offset += o->output_section->vma; | |
209f668e | 7103 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) |
252b5132 RH |
7104 | { |
7105 | unsigned long r_symndx; | |
252b5132 | 7106 | asection *sec; |
fad2542d | 7107 | Elf_Internal_Sym sym; |
252b5132 | 7108 | |
209f668e NC |
7109 | if (next_erel == bed->s->int_rels_per_ext_rel) |
7110 | { | |
7111 | rel_hash++; | |
7112 | next_erel = 0; | |
7113 | } | |
7114 | ||
d6fe2dc1 AM |
7115 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
7116 | finfo->info, o, | |
7117 | irela->r_offset); | |
7118 | if (irela->r_offset >= (bfd_vma) -2) | |
7119 | { | |
73722af0 AM |
7120 | /* This is a reloc for a deleted entry or somesuch. |
7121 | Turn it into an R_*_NONE reloc, at the same | |
7122 | offset as the last reloc. elf_eh_frame.c and | |
7123 | elf_bfd_discard_info rely on reloc offsets | |
b34976b6 | 7124 | being ordered. */ |
73722af0 AM |
7125 | irela->r_offset = last_offset; |
7126 | irela->r_info = 0; | |
7127 | irela->r_addend = 0; | |
d6fe2dc1 AM |
7128 | continue; |
7129 | } | |
7130 | ||
252b5132 RH |
7131 | irela->r_offset += o->output_offset; |
7132 | ||
7ad34365 | 7133 | /* Relocs in an executable have to be virtual addresses. */ |
fd984e46 | 7134 | if (!finfo->info->relocateable) |
7ad34365 NC |
7135 | irela->r_offset += o->output_section->vma; |
7136 | ||
73722af0 | 7137 | last_offset = irela->r_offset; |
252b5132 | 7138 | |
73722af0 AM |
7139 | r_symndx = ELF_R_SYM (irela->r_info); |
7140 | if (r_symndx == STN_UNDEF) | |
252b5132 RH |
7141 | continue; |
7142 | ||
7143 | if (r_symndx >= locsymcount | |
7144 | || (elf_bad_symtab (input_bfd) | |
7145 | && finfo->sections[r_symndx] == NULL)) | |
7146 | { | |
7147 | struct elf_link_hash_entry *rh; | |
209f668e | 7148 | unsigned long indx; |
252b5132 RH |
7149 | |
7150 | /* This is a reloc against a global symbol. We | |
7151 | have not yet output all the local symbols, so | |
7152 | we do not know the symbol index of any global | |
7153 | symbol. We set the rel_hash entry for this | |
7154 | reloc to point to the global hash table entry | |
7155 | for this symbol. The symbol index is then | |
7156 | set at the end of elf_bfd_final_link. */ | |
7157 | indx = r_symndx - extsymoff; | |
7158 | rh = elf_sym_hashes (input_bfd)[indx]; | |
7159 | while (rh->root.type == bfd_link_hash_indirect | |
7160 | || rh->root.type == bfd_link_hash_warning) | |
7161 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
7162 | ||
7163 | /* Setting the index to -2 tells | |
7164 | elf_link_output_extsym that this symbol is | |
7165 | used by a reloc. */ | |
7166 | BFD_ASSERT (rh->indx < 0); | |
7167 | rh->indx = -2; | |
7168 | ||
7169 | *rel_hash = rh; | |
7170 | ||
7171 | continue; | |
7172 | } | |
7173 | ||
3e932841 | 7174 | /* This is a reloc against a local symbol. */ |
252b5132 RH |
7175 | |
7176 | *rel_hash = NULL; | |
fad2542d | 7177 | sym = isymbuf[r_symndx]; |
252b5132 | 7178 | sec = finfo->sections[r_symndx]; |
fad2542d | 7179 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
252b5132 RH |
7180 | { |
7181 | /* I suppose the backend ought to fill in the | |
7182 | section of any STT_SECTION symbol against a | |
7183 | processor specific section. If we have | |
7184 | discarded a section, the output_section will | |
7185 | be the absolute section. */ | |
b491616a AM |
7186 | if (bfd_is_abs_section (sec) |
7187 | || (sec != NULL | |
7188 | && bfd_is_abs_section (sec->output_section))) | |
252b5132 RH |
7189 | r_symndx = 0; |
7190 | else if (sec == NULL || sec->owner == NULL) | |
7191 | { | |
7192 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 7193 | return FALSE; |
252b5132 RH |
7194 | } |
7195 | else | |
7196 | { | |
7197 | r_symndx = sec->output_section->target_index; | |
7198 | BFD_ASSERT (r_symndx != 0); | |
7199 | } | |
b491616a AM |
7200 | |
7201 | /* Adjust the addend according to where the | |
f5d44ba0 | 7202 | section winds up in the output section. */ |
b491616a AM |
7203 | if (rela_normal) |
7204 | irela->r_addend += sec->output_offset; | |
252b5132 RH |
7205 | } |
7206 | else | |
7207 | { | |
7208 | if (finfo->indices[r_symndx] == -1) | |
7209 | { | |
dc810e39 | 7210 | unsigned long shlink; |
252b5132 RH |
7211 | const char *name; |
7212 | asection *osec; | |
7213 | ||
7214 | if (finfo->info->strip == strip_all) | |
7215 | { | |
7216 | /* You can't do ld -r -s. */ | |
7217 | bfd_set_error (bfd_error_invalid_operation); | |
b34976b6 | 7218 | return FALSE; |
252b5132 RH |
7219 | } |
7220 | ||
7221 | /* This symbol was skipped earlier, but | |
7222 | since it is needed by a reloc, we | |
7223 | must output it now. */ | |
dc810e39 | 7224 | shlink = symtab_hdr->sh_link; |
a7b97311 | 7225 | name = (bfd_elf_string_from_elf_section |
fad2542d | 7226 | (input_bfd, shlink, sym.st_name)); |
252b5132 | 7227 | if (name == NULL) |
b34976b6 | 7228 | return FALSE; |
252b5132 RH |
7229 | |
7230 | osec = sec->output_section; | |
fad2542d | 7231 | sym.st_shndx = |
252b5132 RH |
7232 | _bfd_elf_section_from_bfd_section (output_bfd, |
7233 | osec); | |
fad2542d | 7234 | if (sym.st_shndx == SHN_BAD) |
b34976b6 | 7235 | return FALSE; |
252b5132 | 7236 | |
fad2542d | 7237 | sym.st_value += sec->output_offset; |
252b5132 | 7238 | if (! finfo->info->relocateable) |
13ae64f3 | 7239 | { |
fad2542d AM |
7240 | sym.st_value += osec->vma; |
7241 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
13ae64f3 JJ |
7242 | { |
7243 | /* STT_TLS symbols are relative to PT_TLS | |
7244 | segment base. */ | |
7245 | BFD_ASSERT (finfo->first_tls_sec != NULL); | |
fad2542d | 7246 | sym.st_value -= finfo->first_tls_sec->vma; |
13ae64f3 JJ |
7247 | } |
7248 | } | |
252b5132 | 7249 | |
a7b97311 AM |
7250 | finfo->indices[r_symndx] |
7251 | = bfd_get_symcount (output_bfd); | |
252b5132 | 7252 | |
fad2542d | 7253 | if (! elf_link_output_sym (finfo, name, &sym, sec)) |
b34976b6 | 7254 | return FALSE; |
252b5132 RH |
7255 | } |
7256 | ||
7257 | r_symndx = finfo->indices[r_symndx]; | |
7258 | } | |
7259 | ||
7260 | irela->r_info = ELF_R_INFO (r_symndx, | |
7261 | ELF_R_TYPE (irela->r_info)); | |
7262 | } | |
7263 | ||
7264 | /* Swap out the relocs. */ | |
c44233aa AM |
7265 | if (bed->elf_backend_emit_relocs |
7266 | && !(finfo->info->relocateable | |
a7b97311 | 7267 | || finfo->info->emitrelocations)) |
c44233aa AM |
7268 | reloc_emitter = bed->elf_backend_emit_relocs; |
7269 | else | |
7270 | reloc_emitter = elf_link_output_relocs; | |
9317eacc | 7271 | |
c89583f8 AM |
7272 | if (input_rel_hdr->sh_size != 0 |
7273 | && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr, | |
7274 | internal_relocs)) | |
b34976b6 | 7275 | return FALSE; |
9317eacc | 7276 | |
c89583f8 AM |
7277 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; |
7278 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
c44233aa AM |
7279 | { |
7280 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
dc810e39 | 7281 | * bed->s->int_rels_per_ext_rel); |
c89583f8 | 7282 | if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2, |
41241523 | 7283 | internal_relocs)) |
b34976b6 | 7284 | return FALSE; |
c44233aa | 7285 | } |
252b5132 RH |
7286 | } |
7287 | } | |
7288 | ||
7289 | /* Write out the modified section contents. */ | |
73d074b4 | 7290 | if (bed->elf_backend_write_section |
f9f32305 | 7291 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) |
73d074b4 DJ |
7292 | { |
7293 | /* Section written out. */ | |
7294 | } | |
68bfbfcc | 7295 | else switch (o->sec_info_type) |
f5fa8ca2 | 7296 | { |
65765700 | 7297 | case ELF_INFO_TYPE_STABS: |
f5fa8ca2 | 7298 | if (! (_bfd_write_section_stabs |
65765700 JJ |
7299 | (output_bfd, |
7300 | &elf_hash_table (finfo->info)->stab_info, | |
7301 | o, &elf_section_data (o)->sec_info, contents))) | |
b34976b6 | 7302 | return FALSE; |
65765700 JJ |
7303 | break; |
7304 | case ELF_INFO_TYPE_MERGE: | |
126495ed AM |
7305 | if (! _bfd_write_merged_section (output_bfd, o, |
7306 | elf_section_data (o)->sec_info)) | |
b34976b6 | 7307 | return FALSE; |
65765700 JJ |
7308 | break; |
7309 | case ELF_INFO_TYPE_EH_FRAME: | |
7310 | { | |
126495ed AM |
7311 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, |
7312 | o, contents)) | |
b34976b6 | 7313 | return FALSE; |
65765700 JJ |
7314 | } |
7315 | break; | |
7316 | default: | |
7317 | { | |
7318 | bfd_size_type sec_size; | |
7319 | ||
7320 | sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size); | |
7321 | if (! (o->flags & SEC_EXCLUDE) | |
7322 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
7323 | contents, | |
7324 | (file_ptr) o->output_offset, | |
7325 | sec_size)) | |
b34976b6 | 7326 | return FALSE; |
65765700 JJ |
7327 | } |
7328 | break; | |
252b5132 RH |
7329 | } |
7330 | } | |
7331 | ||
b34976b6 | 7332 | return TRUE; |
252b5132 RH |
7333 | } |
7334 | ||
7335 | /* Generate a reloc when linking an ELF file. This is a reloc | |
7336 | requested by the linker, and does come from any input file. This | |
7337 | is used to build constructor and destructor tables when linking | |
7338 | with -Ur. */ | |
7339 | ||
b34976b6 | 7340 | static bfd_boolean |
252b5132 RH |
7341 | elf_reloc_link_order (output_bfd, info, output_section, link_order) |
7342 | bfd *output_bfd; | |
7343 | struct bfd_link_info *info; | |
7344 | asection *output_section; | |
7345 | struct bfd_link_order *link_order; | |
7346 | { | |
7347 | reloc_howto_type *howto; | |
7348 | long indx; | |
7349 | bfd_vma offset; | |
7350 | bfd_vma addend; | |
7351 | struct elf_link_hash_entry **rel_hash_ptr; | |
7352 | Elf_Internal_Shdr *rel_hdr; | |
32f0787a | 7353 | struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
947216bf AM |
7354 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; |
7355 | bfd_byte *erel; | |
7356 | unsigned int i; | |
252b5132 RH |
7357 | |
7358 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
7359 | if (howto == NULL) | |
7360 | { | |
7361 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 7362 | return FALSE; |
252b5132 RH |
7363 | } |
7364 | ||
7365 | addend = link_order->u.reloc.p->addend; | |
7366 | ||
7367 | /* Figure out the symbol index. */ | |
7368 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
31367b81 MM |
7369 | + elf_section_data (output_section)->rel_count |
7370 | + elf_section_data (output_section)->rel_count2); | |
252b5132 RH |
7371 | if (link_order->type == bfd_section_reloc_link_order) |
7372 | { | |
7373 | indx = link_order->u.reloc.p->u.section->target_index; | |
7374 | BFD_ASSERT (indx != 0); | |
7375 | *rel_hash_ptr = NULL; | |
7376 | } | |
7377 | else | |
7378 | { | |
7379 | struct elf_link_hash_entry *h; | |
7380 | ||
7381 | /* Treat a reloc against a defined symbol as though it were | |
c44233aa | 7382 | actually against the section. */ |
252b5132 RH |
7383 | h = ((struct elf_link_hash_entry *) |
7384 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
7385 | link_order->u.reloc.p->u.name, | |
b34976b6 | 7386 | FALSE, FALSE, TRUE)); |
252b5132 RH |
7387 | if (h != NULL |
7388 | && (h->root.type == bfd_link_hash_defined | |
7389 | || h->root.type == bfd_link_hash_defweak)) | |
7390 | { | |
7391 | asection *section; | |
7392 | ||
7393 | section = h->root.u.def.section; | |
7394 | indx = section->output_section->target_index; | |
7395 | *rel_hash_ptr = NULL; | |
7396 | /* It seems that we ought to add the symbol value to the | |
c44233aa AM |
7397 | addend here, but in practice it has already been added |
7398 | because it was passed to constructor_callback. */ | |
252b5132 RH |
7399 | addend += section->output_section->vma + section->output_offset; |
7400 | } | |
7401 | else if (h != NULL) | |
7402 | { | |
7403 | /* Setting the index to -2 tells elf_link_output_extsym that | |
7404 | this symbol is used by a reloc. */ | |
7405 | h->indx = -2; | |
7406 | *rel_hash_ptr = h; | |
7407 | indx = 0; | |
7408 | } | |
7409 | else | |
7410 | { | |
7411 | if (! ((*info->callbacks->unattached_reloc) | |
7412 | (info, link_order->u.reloc.p->u.name, (bfd *) NULL, | |
7413 | (asection *) NULL, (bfd_vma) 0))) | |
b34976b6 | 7414 | return FALSE; |
252b5132 RH |
7415 | indx = 0; |
7416 | } | |
7417 | } | |
7418 | ||
7419 | /* If this is an inplace reloc, we must write the addend into the | |
7420 | object file. */ | |
7421 | if (howto->partial_inplace && addend != 0) | |
7422 | { | |
7423 | bfd_size_type size; | |
7424 | bfd_reloc_status_type rstat; | |
7425 | bfd_byte *buf; | |
b34976b6 | 7426 | bfd_boolean ok; |
dc810e39 | 7427 | const char *sym_name; |
252b5132 RH |
7428 | |
7429 | size = bfd_get_reloc_size (howto); | |
7430 | buf = (bfd_byte *) bfd_zmalloc (size); | |
7431 | if (buf == (bfd_byte *) NULL) | |
b34976b6 | 7432 | return FALSE; |
dc810e39 | 7433 | rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf); |
252b5132 RH |
7434 | switch (rstat) |
7435 | { | |
7436 | case bfd_reloc_ok: | |
7437 | break; | |
dc810e39 | 7438 | |
252b5132 RH |
7439 | default: |
7440 | case bfd_reloc_outofrange: | |
7441 | abort (); | |
dc810e39 | 7442 | |
252b5132 | 7443 | case bfd_reloc_overflow: |
dc810e39 AM |
7444 | if (link_order->type == bfd_section_reloc_link_order) |
7445 | sym_name = bfd_section_name (output_bfd, | |
7446 | link_order->u.reloc.p->u.section); | |
7447 | else | |
7448 | sym_name = link_order->u.reloc.p->u.name; | |
252b5132 | 7449 | if (! ((*info->callbacks->reloc_overflow) |
dc810e39 AM |
7450 | (info, sym_name, howto->name, addend, |
7451 | (bfd *) NULL, (asection *) NULL, (bfd_vma) 0))) | |
252b5132 RH |
7452 | { |
7453 | free (buf); | |
b34976b6 | 7454 | return FALSE; |
252b5132 RH |
7455 | } |
7456 | break; | |
7457 | } | |
7458 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, | |
7459 | (file_ptr) link_order->offset, size); | |
7460 | free (buf); | |
7461 | if (! ok) | |
b34976b6 | 7462 | return FALSE; |
252b5132 RH |
7463 | } |
7464 | ||
7465 | /* The address of a reloc is relative to the section in a | |
7466 | relocateable file, and is a virtual address in an executable | |
7467 | file. */ | |
7468 | offset = link_order->offset; | |
7469 | if (! info->relocateable) | |
7470 | offset += output_section->vma; | |
7471 | ||
947216bf AM |
7472 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) |
7473 | { | |
7474 | irel[i].r_offset = offset; | |
7475 | irel[i].r_info = 0; | |
7476 | irel[i].r_addend = 0; | |
7477 | } | |
7478 | irel[0].r_info = ELF_R_INFO (indx, howto->type); | |
252b5132 | 7479 | |
947216bf AM |
7480 | rel_hdr = &elf_section_data (output_section)->rel_hdr; |
7481 | erel = rel_hdr->contents; | |
252b5132 RH |
7482 | if (rel_hdr->sh_type == SHT_REL) |
7483 | { | |
947216bf AM |
7484 | erel += (elf_section_data (output_section)->rel_count |
7485 | * sizeof (Elf_External_Rel)); | |
7486 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
252b5132 RH |
7487 | } |
7488 | else | |
7489 | { | |
947216bf AM |
7490 | irel[0].r_addend = addend; |
7491 | erel += (elf_section_data (output_section)->rel_count | |
7492 | * sizeof (Elf_External_Rela)); | |
7493 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
252b5132 RH |
7494 | } |
7495 | ||
0525d26e | 7496 | ++elf_section_data (output_section)->rel_count; |
252b5132 | 7497 | |
b34976b6 | 7498 | return TRUE; |
252b5132 | 7499 | } |
252b5132 RH |
7500 | \f |
7501 | /* Allocate a pointer to live in a linker created section. */ | |
7502 | ||
b34976b6 | 7503 | bfd_boolean |
252b5132 RH |
7504 | elf_create_pointer_linker_section (abfd, info, lsect, h, rel) |
7505 | bfd *abfd; | |
7506 | struct bfd_link_info *info; | |
7507 | elf_linker_section_t *lsect; | |
7508 | struct elf_link_hash_entry *h; | |
7509 | const Elf_Internal_Rela *rel; | |
7510 | { | |
7511 | elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL; | |
7512 | elf_linker_section_pointers_t *linker_section_ptr; | |
dc810e39 AM |
7513 | unsigned long r_symndx = ELF_R_SYM (rel->r_info); |
7514 | bfd_size_type amt; | |
252b5132 RH |
7515 | |
7516 | BFD_ASSERT (lsect != NULL); | |
7517 | ||
a7b97311 | 7518 | /* Is this a global symbol? */ |
252b5132 RH |
7519 | if (h != NULL) |
7520 | { | |
a7b97311 | 7521 | /* Has this symbol already been allocated? If so, our work is done. */ |
252b5132 RH |
7522 | if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer, |
7523 | rel->r_addend, | |
7524 | lsect->which)) | |
b34976b6 | 7525 | return TRUE; |
252b5132 RH |
7526 | |
7527 | ptr_linker_section_ptr = &h->linker_section_pointer; | |
7528 | /* Make sure this symbol is output as a dynamic symbol. */ | |
7529 | if (h->dynindx == -1) | |
7530 | { | |
7531 | if (! elf_link_record_dynamic_symbol (info, h)) | |
b34976b6 | 7532 | return FALSE; |
252b5132 RH |
7533 | } |
7534 | ||
7535 | if (lsect->rel_section) | |
7536 | lsect->rel_section->_raw_size += sizeof (Elf_External_Rela); | |
7537 | } | |
a7b97311 | 7538 | else |
252b5132 | 7539 | { |
a7b97311 | 7540 | /* Allocation of a pointer to a local symbol. */ |
252b5132 RH |
7541 | elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd); |
7542 | ||
a7b97311 | 7543 | /* Allocate a table to hold the local symbols if first time. */ |
252b5132 RH |
7544 | if (!ptr) |
7545 | { | |
7546 | unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info; | |
7547 | register unsigned int i; | |
7548 | ||
dc810e39 AM |
7549 | amt = num_symbols; |
7550 | amt *= sizeof (elf_linker_section_pointers_t *); | |
7551 | ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt); | |
252b5132 RH |
7552 | |
7553 | if (!ptr) | |
b34976b6 | 7554 | return FALSE; |
252b5132 RH |
7555 | |
7556 | elf_local_ptr_offsets (abfd) = ptr; | |
7557 | for (i = 0; i < num_symbols; i++) | |
a7b97311 | 7558 | ptr[i] = (elf_linker_section_pointers_t *) 0; |
252b5132 RH |
7559 | } |
7560 | ||
a7b97311 | 7561 | /* Has this symbol already been allocated? If so, our work is done. */ |
252b5132 RH |
7562 | if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx], |
7563 | rel->r_addend, | |
7564 | lsect->which)) | |
b34976b6 | 7565 | return TRUE; |
252b5132 RH |
7566 | |
7567 | ptr_linker_section_ptr = &ptr[r_symndx]; | |
7568 | ||
7569 | if (info->shared) | |
7570 | { | |
7571 | /* If we are generating a shared object, we need to | |
7572 | output a R_<xxx>_RELATIVE reloc so that the | |
7573 | dynamic linker can adjust this GOT entry. */ | |
7574 | BFD_ASSERT (lsect->rel_section != NULL); | |
7575 | lsect->rel_section->_raw_size += sizeof (Elf_External_Rela); | |
7576 | } | |
7577 | } | |
7578 | ||
a7b97311 AM |
7579 | /* Allocate space for a pointer in the linker section, and allocate |
7580 | a new pointer record from internal memory. */ | |
252b5132 | 7581 | BFD_ASSERT (ptr_linker_section_ptr != NULL); |
dc810e39 AM |
7582 | amt = sizeof (elf_linker_section_pointers_t); |
7583 | linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt); | |
252b5132 RH |
7584 | |
7585 | if (!linker_section_ptr) | |
b34976b6 | 7586 | return FALSE; |
252b5132 RH |
7587 | |
7588 | linker_section_ptr->next = *ptr_linker_section_ptr; | |
7589 | linker_section_ptr->addend = rel->r_addend; | |
7590 | linker_section_ptr->which = lsect->which; | |
b34976b6 | 7591 | linker_section_ptr->written_address_p = FALSE; |
252b5132 RH |
7592 | *ptr_linker_section_ptr = linker_section_ptr; |
7593 | ||
7594 | #if 0 | |
7595 | if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset) | |
7596 | { | |
a7b97311 AM |
7597 | linker_section_ptr->offset = (lsect->section->_raw_size |
7598 | - lsect->hole_size + (ARCH_SIZE / 8)); | |
252b5132 RH |
7599 | lsect->hole_offset += ARCH_SIZE / 8; |
7600 | lsect->sym_offset += ARCH_SIZE / 8; | |
a7b97311 | 7601 | if (lsect->sym_hash) |
252b5132 | 7602 | { |
a7b97311 | 7603 | /* Bump up symbol value if needed. */ |
252b5132 RH |
7604 | lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8; |
7605 | #ifdef DEBUG | |
7606 | fprintf (stderr, "Bump up %s by %ld, current value = %ld\n", | |
7607 | lsect->sym_hash->root.root.string, | |
a7b97311 AM |
7608 | (long) ARCH_SIZE / 8, |
7609 | (long) lsect->sym_hash->root.u.def.value); | |
252b5132 RH |
7610 | #endif |
7611 | } | |
7612 | } | |
7613 | else | |
7614 | #endif | |
7615 | linker_section_ptr->offset = lsect->section->_raw_size; | |
7616 | ||
7617 | lsect->section->_raw_size += ARCH_SIZE / 8; | |
7618 | ||
7619 | #ifdef DEBUG | |
a7b97311 AM |
7620 | fprintf (stderr, |
7621 | "Create pointer in linker section %s, offset = %ld, section size = %ld\n", | |
7622 | lsect->name, (long) linker_section_ptr->offset, | |
7623 | (long) lsect->section->_raw_size); | |
252b5132 RH |
7624 | #endif |
7625 | ||
b34976b6 | 7626 | return TRUE; |
252b5132 | 7627 | } |
252b5132 RH |
7628 | \f |
7629 | #if ARCH_SIZE==64 | |
7630 | #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR) | |
7631 | #endif | |
7632 | #if ARCH_SIZE==32 | |
7633 | #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR) | |
7634 | #endif | |
7635 | ||
209f668e | 7636 | /* Fill in the address for a pointer generated in a linker section. */ |
252b5132 RH |
7637 | |
7638 | bfd_vma | |
a7b97311 AM |
7639 | elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, |
7640 | relocation, rel, relative_reloc) | |
252b5132 RH |
7641 | bfd *output_bfd; |
7642 | bfd *input_bfd; | |
7643 | struct bfd_link_info *info; | |
7644 | elf_linker_section_t *lsect; | |
7645 | struct elf_link_hash_entry *h; | |
7646 | bfd_vma relocation; | |
7647 | const Elf_Internal_Rela *rel; | |
7648 | int relative_reloc; | |
7649 | { | |
7650 | elf_linker_section_pointers_t *linker_section_ptr; | |
7651 | ||
7652 | BFD_ASSERT (lsect != NULL); | |
7653 | ||
a7b97311 | 7654 | if (h != NULL) |
252b5132 | 7655 | { |
a7b97311 AM |
7656 | /* Handle global symbol. */ |
7657 | linker_section_ptr = (_bfd_elf_find_pointer_linker_section | |
7658 | (h->linker_section_pointer, | |
7659 | rel->r_addend, | |
7660 | lsect->which)); | |
252b5132 RH |
7661 | |
7662 | BFD_ASSERT (linker_section_ptr != NULL); | |
7663 | ||
7664 | if (! elf_hash_table (info)->dynamic_sections_created | |
7665 | || (info->shared | |
7666 | && info->symbolic | |
7667 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
7668 | { | |
7669 | /* This is actually a static link, or it is a | |
7670 | -Bsymbolic link and the symbol is defined | |
7671 | locally. We must initialize this entry in the | |
7672 | global section. | |
7673 | ||
7674 | When doing a dynamic link, we create a .rela.<xxx> | |
7675 | relocation entry to initialize the value. This | |
7676 | is done in the finish_dynamic_symbol routine. */ | |
7677 | if (!linker_section_ptr->written_address_p) | |
7678 | { | |
b34976b6 | 7679 | linker_section_ptr->written_address_p = TRUE; |
a7b97311 AM |
7680 | bfd_put_ptr (output_bfd, |
7681 | relocation + linker_section_ptr->addend, | |
7682 | (lsect->section->contents | |
7683 | + linker_section_ptr->offset)); | |
252b5132 RH |
7684 | } |
7685 | } | |
7686 | } | |
a7b97311 | 7687 | else |
252b5132 | 7688 | { |
a7b97311 | 7689 | /* Handle local symbol. */ |
252b5132 RH |
7690 | unsigned long r_symndx = ELF_R_SYM (rel->r_info); |
7691 | BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL); | |
7692 | BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL); | |
a7b97311 AM |
7693 | linker_section_ptr = (_bfd_elf_find_pointer_linker_section |
7694 | (elf_local_ptr_offsets (input_bfd)[r_symndx], | |
7695 | rel->r_addend, | |
7696 | lsect->which)); | |
252b5132 RH |
7697 | |
7698 | BFD_ASSERT (linker_section_ptr != NULL); | |
7699 | ||
a7b97311 | 7700 | /* Write out pointer if it hasn't been rewritten out before. */ |
252b5132 RH |
7701 | if (!linker_section_ptr->written_address_p) |
7702 | { | |
b34976b6 | 7703 | linker_section_ptr->written_address_p = TRUE; |
252b5132 RH |
7704 | bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend, |
7705 | lsect->section->contents + linker_section_ptr->offset); | |
7706 | ||
7707 | if (info->shared) | |
7708 | { | |
7709 | asection *srel = lsect->rel_section; | |
947216bf AM |
7710 | Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL]; |
7711 | bfd_byte *erel; | |
209f668e NC |
7712 | struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
7713 | unsigned int i; | |
252b5132 | 7714 | |
a7b97311 AM |
7715 | /* We need to generate a relative reloc for the dynamic |
7716 | linker. */ | |
252b5132 | 7717 | if (!srel) |
a7b97311 AM |
7718 | { |
7719 | srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj, | |
7720 | lsect->rel_name); | |
7721 | lsect->rel_section = srel; | |
7722 | } | |
252b5132 RH |
7723 | |
7724 | BFD_ASSERT (srel != NULL); | |
7725 | ||
209f668e | 7726 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) |
947216bf AM |
7727 | { |
7728 | outrel[i].r_offset = (lsect->section->output_section->vma | |
7729 | + lsect->section->output_offset | |
7730 | + linker_section_ptr->offset); | |
7731 | outrel[i].r_info = 0; | |
7732 | outrel[i].r_addend = 0; | |
7733 | } | |
209f668e | 7734 | outrel[0].r_info = ELF_R_INFO (0, relative_reloc); |
947216bf AM |
7735 | erel = lsect->section->contents; |
7736 | erel += (elf_section_data (lsect->section)->rel_count++ | |
7737 | * sizeof (Elf_External_Rela)); | |
dc810e39 | 7738 | elf_swap_reloca_out (output_bfd, outrel, erel); |
252b5132 RH |
7739 | } |
7740 | } | |
7741 | } | |
7742 | ||
7743 | relocation = (lsect->section->output_offset | |
7744 | + linker_section_ptr->offset | |
7745 | - lsect->hole_offset | |
7746 | - lsect->sym_offset); | |
7747 | ||
7748 | #ifdef DEBUG | |
a7b97311 AM |
7749 | fprintf (stderr, |
7750 | "Finish pointer in linker section %s, offset = %ld (0x%lx)\n", | |
7751 | lsect->name, (long) relocation, (long) relocation); | |
252b5132 RH |
7752 | #endif |
7753 | ||
7754 | /* Subtract out the addend, because it will get added back in by the normal | |
7755 | processing. */ | |
7756 | return relocation - linker_section_ptr->addend; | |
7757 | } | |
7758 | \f | |
7759 | /* Garbage collect unused sections. */ | |
7760 | ||
b34976b6 | 7761 | static bfd_boolean elf_gc_mark |
58821868 AM |
7762 | PARAMS ((struct bfd_link_info *, asection *, |
7763 | asection * (*) (asection *, struct bfd_link_info *, | |
7764 | Elf_Internal_Rela *, struct elf_link_hash_entry *, | |
7765 | Elf_Internal_Sym *))); | |
252b5132 | 7766 | |
b34976b6 | 7767 | static bfd_boolean elf_gc_sweep |
58821868 | 7768 | PARAMS ((struct bfd_link_info *, |
b34976b6 AM |
7769 | bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *, |
7770 | const Elf_Internal_Rela *))); | |
252b5132 | 7771 | |
b34976b6 | 7772 | static bfd_boolean elf_gc_sweep_symbol |
58821868 | 7773 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
252b5132 | 7774 | |
b34976b6 | 7775 | static bfd_boolean elf_gc_allocate_got_offsets |
58821868 | 7776 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
252b5132 | 7777 | |
b34976b6 | 7778 | static bfd_boolean elf_gc_propagate_vtable_entries_used |
58821868 | 7779 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
252b5132 | 7780 | |
b34976b6 | 7781 | static bfd_boolean elf_gc_smash_unused_vtentry_relocs |
58821868 | 7782 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
252b5132 RH |
7783 | |
7784 | /* The mark phase of garbage collection. For a given section, mark | |
dbb410c3 AM |
7785 | it and any sections in this section's group, and all the sections |
7786 | which define symbols to which it refers. */ | |
252b5132 | 7787 | |
b34976b6 AM |
7788 | typedef asection * (*gc_mark_hook_fn) |
7789 | PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
7790 | struct elf_link_hash_entry *, Elf_Internal_Sym *)); | |
7791 | ||
7792 | static bfd_boolean | |
252b5132 RH |
7793 | elf_gc_mark (info, sec, gc_mark_hook) |
7794 | struct bfd_link_info *info; | |
7795 | asection *sec; | |
b34976b6 | 7796 | gc_mark_hook_fn gc_mark_hook; |
252b5132 | 7797 | { |
b34976b6 | 7798 | bfd_boolean ret; |
dbb410c3 | 7799 | asection *group_sec; |
252b5132 RH |
7800 | |
7801 | sec->gc_mark = 1; | |
7802 | ||
dbb410c3 AM |
7803 | /* Mark all the sections in the group. */ |
7804 | group_sec = elf_section_data (sec)->next_in_group; | |
7805 | if (group_sec && !group_sec->gc_mark) | |
7806 | if (!elf_gc_mark (info, group_sec, gc_mark_hook)) | |
b34976b6 | 7807 | return FALSE; |
252b5132 | 7808 | |
dbb410c3 | 7809 | /* Look through the section relocs. */ |
b34976b6 | 7810 | ret = TRUE; |
252b5132 RH |
7811 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
7812 | { | |
7813 | Elf_Internal_Rela *relstart, *rel, *relend; | |
7814 | Elf_Internal_Shdr *symtab_hdr; | |
7815 | struct elf_link_hash_entry **sym_hashes; | |
7816 | size_t nlocsyms; | |
7817 | size_t extsymoff; | |
252b5132 | 7818 | bfd *input_bfd = sec->owner; |
c7ac6ff8 | 7819 | struct elf_backend_data *bed = get_elf_backend_data (input_bfd); |
6cdc0ccc | 7820 | Elf_Internal_Sym *isym = NULL; |
252b5132 RH |
7821 | |
7822 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
7823 | sym_hashes = elf_sym_hashes (input_bfd); | |
7824 | ||
7825 | /* Read the local symbols. */ | |
7826 | if (elf_bad_symtab (input_bfd)) | |
7827 | { | |
7828 | nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym); | |
c44233aa | 7829 | extsymoff = 0; |
252b5132 RH |
7830 | } |
7831 | else | |
7832 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
9ad5cbcf | 7833 | |
6cdc0ccc AM |
7834 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; |
7835 | if (isym == NULL && nlocsyms != 0) | |
9ad5cbcf | 7836 | { |
6cdc0ccc AM |
7837 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, |
7838 | NULL, NULL, NULL); | |
7839 | if (isym == NULL) | |
b34976b6 | 7840 | return FALSE; |
9ad5cbcf AM |
7841 | } |
7842 | ||
252b5132 RH |
7843 | /* Read the relocations. */ |
7844 | relstart = (NAME(_bfd_elf,link_read_relocs) | |
6cdc0ccc | 7845 | (input_bfd, sec, NULL, (Elf_Internal_Rela *) NULL, |
252b5132 RH |
7846 | info->keep_memory)); |
7847 | if (relstart == NULL) | |
7848 | { | |
b34976b6 | 7849 | ret = FALSE; |
252b5132 RH |
7850 | goto out1; |
7851 | } | |
c7ac6ff8 | 7852 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; |
252b5132 RH |
7853 | |
7854 | for (rel = relstart; rel < relend; rel++) | |
7855 | { | |
7856 | unsigned long r_symndx; | |
7857 | asection *rsec; | |
7858 | struct elf_link_hash_entry *h; | |
252b5132 RH |
7859 | |
7860 | r_symndx = ELF_R_SYM (rel->r_info); | |
7861 | if (r_symndx == 0) | |
7862 | continue; | |
7863 | ||
6cdc0ccc AM |
7864 | if (r_symndx >= nlocsyms |
7865 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
252b5132 RH |
7866 | { |
7867 | h = sym_hashes[r_symndx - extsymoff]; | |
1e2f5b6e | 7868 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
252b5132 RH |
7869 | } |
7870 | else | |
7871 | { | |
6cdc0ccc | 7872 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); |
252b5132 RH |
7873 | } |
7874 | ||
7875 | if (rsec && !rsec->gc_mark) | |
b91afed7 AM |
7876 | { |
7877 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
7878 | rsec->gc_mark = 1; | |
7879 | else if (!elf_gc_mark (info, rsec, gc_mark_hook)) | |
7880 | { | |
b34976b6 | 7881 | ret = FALSE; |
b91afed7 AM |
7882 | goto out2; |
7883 | } | |
7884 | } | |
252b5132 RH |
7885 | } |
7886 | ||
7887 | out2: | |
6cdc0ccc | 7888 | if (elf_section_data (sec)->relocs != relstart) |
252b5132 RH |
7889 | free (relstart); |
7890 | out1: | |
6cdc0ccc AM |
7891 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) |
7892 | { | |
7893 | if (! info->keep_memory) | |
7894 | free (isym); | |
7895 | else | |
7896 | symtab_hdr->contents = (unsigned char *) isym; | |
7897 | } | |
252b5132 RH |
7898 | } |
7899 | ||
7900 | return ret; | |
7901 | } | |
7902 | ||
7903 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
7904 | ||
b34976b6 AM |
7905 | typedef bfd_boolean (*gc_sweep_hook_fn) |
7906 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
7907 | const Elf_Internal_Rela *)); | |
7908 | ||
7909 | static bfd_boolean | |
252b5132 RH |
7910 | elf_gc_sweep (info, gc_sweep_hook) |
7911 | struct bfd_link_info *info; | |
b34976b6 | 7912 | gc_sweep_hook_fn gc_sweep_hook; |
252b5132 RH |
7913 | { |
7914 | bfd *sub; | |
7915 | ||
7916 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
7917 | { | |
7918 | asection *o; | |
7919 | ||
f6af82bd AM |
7920 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) |
7921 | continue; | |
7922 | ||
252b5132 RH |
7923 | for (o = sub->sections; o != NULL; o = o->next) |
7924 | { | |
7925 | /* Keep special sections. Keep .debug sections. */ | |
7926 | if ((o->flags & SEC_LINKER_CREATED) | |
7927 | || (o->flags & SEC_DEBUGGING)) | |
7928 | o->gc_mark = 1; | |
7929 | ||
7930 | if (o->gc_mark) | |
7931 | continue; | |
7932 | ||
7933 | /* Skip sweeping sections already excluded. */ | |
7934 | if (o->flags & SEC_EXCLUDE) | |
7935 | continue; | |
7936 | ||
7937 | /* Since this is early in the link process, it is simple | |
7938 | to remove a section from the output. */ | |
7939 | o->flags |= SEC_EXCLUDE; | |
7940 | ||
7941 | /* But we also have to update some of the relocation | |
7942 | info we collected before. */ | |
7943 | if (gc_sweep_hook | |
7944 | && (o->flags & SEC_RELOC) && o->reloc_count > 0) | |
7945 | { | |
7946 | Elf_Internal_Rela *internal_relocs; | |
b34976b6 | 7947 | bfd_boolean r; |
252b5132 RH |
7948 | |
7949 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) | |
7950 | (o->owner, o, NULL, NULL, info->keep_memory)); | |
7951 | if (internal_relocs == NULL) | |
b34976b6 | 7952 | return FALSE; |
252b5132 | 7953 | |
3e932841 | 7954 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); |
252b5132 | 7955 | |
6cdc0ccc | 7956 | if (elf_section_data (o)->relocs != internal_relocs) |
252b5132 RH |
7957 | free (internal_relocs); |
7958 | ||
7959 | if (!r) | |
b34976b6 | 7960 | return FALSE; |
252b5132 RH |
7961 | } |
7962 | } | |
7963 | } | |
7964 | ||
7965 | /* Remove the symbols that were in the swept sections from the dynamic | |
7966 | symbol table. GCFIXME: Anyone know how to get them out of the | |
7967 | static symbol table as well? */ | |
7968 | { | |
7969 | int i = 0; | |
7970 | ||
7971 | elf_link_hash_traverse (elf_hash_table (info), | |
7972 | elf_gc_sweep_symbol, | |
7973 | (PTR) &i); | |
7974 | ||
7975 | elf_hash_table (info)->dynsymcount = i; | |
7976 | } | |
7977 | ||
b34976b6 | 7978 | return TRUE; |
252b5132 RH |
7979 | } |
7980 | ||
7981 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
7982 | ||
b34976b6 | 7983 | static bfd_boolean |
252b5132 RH |
7984 | elf_gc_sweep_symbol (h, idxptr) |
7985 | struct elf_link_hash_entry *h; | |
7986 | PTR idxptr; | |
7987 | { | |
7988 | int *idx = (int *) idxptr; | |
7989 | ||
e92d460e AM |
7990 | if (h->root.type == bfd_link_hash_warning) |
7991 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7992 | ||
252b5132 RH |
7993 | if (h->dynindx != -1 |
7994 | && ((h->root.type != bfd_link_hash_defined | |
7995 | && h->root.type != bfd_link_hash_defweak) | |
7996 | || h->root.u.def.section->gc_mark)) | |
7997 | h->dynindx = (*idx)++; | |
7998 | ||
b34976b6 | 7999 | return TRUE; |
252b5132 RH |
8000 | } |
8001 | ||
8002 | /* Propogate collected vtable information. This is called through | |
8003 | elf_link_hash_traverse. */ | |
8004 | ||
b34976b6 | 8005 | static bfd_boolean |
252b5132 RH |
8006 | elf_gc_propagate_vtable_entries_used (h, okp) |
8007 | struct elf_link_hash_entry *h; | |
8008 | PTR okp; | |
8009 | { | |
e92d460e AM |
8010 | if (h->root.type == bfd_link_hash_warning) |
8011 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8012 | ||
3e932841 | 8013 | /* Those that are not vtables. */ |
252b5132 | 8014 | if (h->vtable_parent == NULL) |
b34976b6 | 8015 | return TRUE; |
252b5132 RH |
8016 | |
8017 | /* Those vtables that do not have parents, we cannot merge. */ | |
8018 | if (h->vtable_parent == (struct elf_link_hash_entry *) -1) | |
b34976b6 | 8019 | return TRUE; |
252b5132 RH |
8020 | |
8021 | /* If we've already been done, exit. */ | |
8022 | if (h->vtable_entries_used && h->vtable_entries_used[-1]) | |
b34976b6 | 8023 | return TRUE; |
252b5132 RH |
8024 | |
8025 | /* Make sure the parent's table is up to date. */ | |
8026 | elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp); | |
8027 | ||
8028 | if (h->vtable_entries_used == NULL) | |
8029 | { | |
8030 | /* None of this table's entries were referenced. Re-use the | |
8031 | parent's table. */ | |
8032 | h->vtable_entries_used = h->vtable_parent->vtable_entries_used; | |
8033 | h->vtable_entries_size = h->vtable_parent->vtable_entries_size; | |
8034 | } | |
8035 | else | |
8036 | { | |
8037 | size_t n; | |
b34976b6 | 8038 | bfd_boolean *cu, *pu; |
252b5132 RH |
8039 | |
8040 | /* Or the parent's entries into ours. */ | |
8041 | cu = h->vtable_entries_used; | |
b34976b6 | 8042 | cu[-1] = TRUE; |
252b5132 RH |
8043 | pu = h->vtable_parent->vtable_entries_used; |
8044 | if (pu != NULL) | |
8045 | { | |
c44233aa AM |
8046 | asection *sec = h->root.u.def.section; |
8047 | struct elf_backend_data *bed = get_elf_backend_data (sec->owner); | |
8048 | int file_align = bed->s->file_align; | |
0d1ea5c0 CM |
8049 | |
8050 | n = h->vtable_parent->vtable_entries_size / file_align; | |
374b596d | 8051 | while (n--) |
252b5132 | 8052 | { |
374b596d | 8053 | if (*pu) |
b34976b6 | 8054 | *cu = TRUE; |
374b596d NC |
8055 | pu++; |
8056 | cu++; | |
252b5132 RH |
8057 | } |
8058 | } | |
8059 | } | |
8060 | ||
b34976b6 | 8061 | return TRUE; |
252b5132 RH |
8062 | } |
8063 | ||
b34976b6 | 8064 | static bfd_boolean |
252b5132 RH |
8065 | elf_gc_smash_unused_vtentry_relocs (h, okp) |
8066 | struct elf_link_hash_entry *h; | |
8067 | PTR okp; | |
8068 | { | |
8069 | asection *sec; | |
8070 | bfd_vma hstart, hend; | |
8071 | Elf_Internal_Rela *relstart, *relend, *rel; | |
c7ac6ff8 | 8072 | struct elf_backend_data *bed; |
0d1ea5c0 | 8073 | int file_align; |
252b5132 | 8074 | |
e92d460e AM |
8075 | if (h->root.type == bfd_link_hash_warning) |
8076 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8077 | ||
252b5132 RH |
8078 | /* Take care of both those symbols that do not describe vtables as |
8079 | well as those that are not loaded. */ | |
8080 | if (h->vtable_parent == NULL) | |
b34976b6 | 8081 | return TRUE; |
252b5132 RH |
8082 | |
8083 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
8084 | || h->root.type == bfd_link_hash_defweak); | |
8085 | ||
8086 | sec = h->root.u.def.section; | |
8087 | hstart = h->root.u.def.value; | |
8088 | hend = hstart + h->size; | |
8089 | ||
8090 | relstart = (NAME(_bfd_elf,link_read_relocs) | |
b34976b6 | 8091 | (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, TRUE)); |
252b5132 | 8092 | if (!relstart) |
b34976b6 | 8093 | return *(bfd_boolean *) okp = FALSE; |
c7ac6ff8 | 8094 | bed = get_elf_backend_data (sec->owner); |
0d1ea5c0 CM |
8095 | file_align = bed->s->file_align; |
8096 | ||
c7ac6ff8 | 8097 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; |
252b5132 RH |
8098 | |
8099 | for (rel = relstart; rel < relend; ++rel) | |
8100 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
8101 | { | |
8102 | /* If the entry is in use, do nothing. */ | |
8103 | if (h->vtable_entries_used | |
8104 | && (rel->r_offset - hstart) < h->vtable_entries_size) | |
8105 | { | |
0d1ea5c0 | 8106 | bfd_vma entry = (rel->r_offset - hstart) / file_align; |
252b5132 RH |
8107 | if (h->vtable_entries_used[entry]) |
8108 | continue; | |
8109 | } | |
8110 | /* Otherwise, kill it. */ | |
8111 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
8112 | } | |
8113 | ||
b34976b6 | 8114 | return TRUE; |
252b5132 RH |
8115 | } |
8116 | ||
8117 | /* Do mark and sweep of unused sections. */ | |
8118 | ||
b34976b6 | 8119 | bfd_boolean |
252b5132 RH |
8120 | elf_gc_sections (abfd, info) |
8121 | bfd *abfd; | |
8122 | struct bfd_link_info *info; | |
8123 | { | |
b34976b6 | 8124 | bfd_boolean ok = TRUE; |
252b5132 RH |
8125 | bfd *sub; |
8126 | asection * (*gc_mark_hook) | |
1e2f5b6e | 8127 | PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, |
c44233aa | 8128 | struct elf_link_hash_entry *h, Elf_Internal_Sym *)); |
252b5132 RH |
8129 | |
8130 | if (!get_elf_backend_data (abfd)->can_gc_sections | |
6d3e950b | 8131 | || info->relocateable || info->emitrelocations |
252b5132 | 8132 | || elf_hash_table (info)->dynamic_sections_created) |
b34976b6 | 8133 | return TRUE; |
252b5132 RH |
8134 | |
8135 | /* Apply transitive closure to the vtable entry usage info. */ | |
8136 | elf_link_hash_traverse (elf_hash_table (info), | |
8137 | elf_gc_propagate_vtable_entries_used, | |
8138 | (PTR) &ok); | |
8139 | if (!ok) | |
b34976b6 | 8140 | return FALSE; |
252b5132 RH |
8141 | |
8142 | /* Kill the vtable relocations that were not used. */ | |
8143 | elf_link_hash_traverse (elf_hash_table (info), | |
8144 | elf_gc_smash_unused_vtentry_relocs, | |
8145 | (PTR) &ok); | |
8146 | if (!ok) | |
b34976b6 | 8147 | return FALSE; |
252b5132 RH |
8148 | |
8149 | /* Grovel through relocs to find out who stays ... */ | |
8150 | ||
8151 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; | |
8152 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8153 | { | |
8154 | asection *o; | |
f6af82bd AM |
8155 | |
8156 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8157 | continue; | |
8158 | ||
252b5132 RH |
8159 | for (o = sub->sections; o != NULL; o = o->next) |
8160 | { | |
8161 | if (o->flags & SEC_KEEP) | |
c44233aa | 8162 | if (!elf_gc_mark (info, o, gc_mark_hook)) |
b34976b6 | 8163 | return FALSE; |
252b5132 RH |
8164 | } |
8165 | } | |
8166 | ||
8167 | /* ... and mark SEC_EXCLUDE for those that go. */ | |
a7b97311 | 8168 | if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook)) |
b34976b6 | 8169 | return FALSE; |
252b5132 | 8170 | |
b34976b6 | 8171 | return TRUE; |
252b5132 RH |
8172 | } |
8173 | \f | |
8174 | /* Called from check_relocs to record the existance of a VTINHERIT reloc. */ | |
8175 | ||
b34976b6 | 8176 | bfd_boolean |
252b5132 RH |
8177 | elf_gc_record_vtinherit (abfd, sec, h, offset) |
8178 | bfd *abfd; | |
8179 | asection *sec; | |
8180 | struct elf_link_hash_entry *h; | |
8181 | bfd_vma offset; | |
8182 | { | |
8183 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
8184 | struct elf_link_hash_entry **search, *child; | |
8185 | bfd_size_type extsymcount; | |
8186 | ||
8187 | /* The sh_info field of the symtab header tells us where the | |
8188 | external symbols start. We don't care about the local symbols at | |
8189 | this point. */ | |
8190 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym); | |
8191 | if (!elf_bad_symtab (abfd)) | |
8192 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
8193 | ||
8194 | sym_hashes = elf_sym_hashes (abfd); | |
8195 | sym_hashes_end = sym_hashes + extsymcount; | |
8196 | ||
8197 | /* Hunt down the child symbol, which is in this section at the same | |
8198 | offset as the relocation. */ | |
8199 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
8200 | { | |
8201 | if ((child = *search) != NULL | |
8202 | && (child->root.type == bfd_link_hash_defined | |
8203 | || child->root.type == bfd_link_hash_defweak) | |
8204 | && child->root.u.def.section == sec | |
8205 | && child->root.u.def.value == offset) | |
8206 | goto win; | |
8207 | } | |
8208 | ||
8209 | (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT", | |
8f615d07 | 8210 | bfd_archive_filename (abfd), sec->name, |
a7b97311 | 8211 | (unsigned long) offset); |
252b5132 | 8212 | bfd_set_error (bfd_error_invalid_operation); |
b34976b6 | 8213 | return FALSE; |
252b5132 | 8214 | |
dc810e39 | 8215 | win: |
252b5132 RH |
8216 | if (!h) |
8217 | { | |
8218 | /* This *should* only be the absolute section. It could potentially | |
8219 | be that someone has defined a non-global vtable though, which | |
8220 | would be bad. It isn't worth paging in the local symbols to be | |
8221 | sure though; that case should simply be handled by the assembler. */ | |
8222 | ||
8223 | child->vtable_parent = (struct elf_link_hash_entry *) -1; | |
8224 | } | |
8225 | else | |
8226 | child->vtable_parent = h; | |
8227 | ||
b34976b6 | 8228 | return TRUE; |
252b5132 RH |
8229 | } |
8230 | ||
8231 | /* Called from check_relocs to record the existance of a VTENTRY reloc. */ | |
8232 | ||
b34976b6 | 8233 | bfd_boolean |
252b5132 | 8234 | elf_gc_record_vtentry (abfd, sec, h, addend) |
7442e600 ILT |
8235 | bfd *abfd ATTRIBUTE_UNUSED; |
8236 | asection *sec ATTRIBUTE_UNUSED; | |
252b5132 RH |
8237 | struct elf_link_hash_entry *h; |
8238 | bfd_vma addend; | |
8239 | { | |
0d1ea5c0 CM |
8240 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8241 | int file_align = bed->s->file_align; | |
8242 | ||
252b5132 RH |
8243 | if (addend >= h->vtable_entries_size) |
8244 | { | |
8245 | size_t size, bytes; | |
b34976b6 | 8246 | bfd_boolean *ptr = h->vtable_entries_used; |
252b5132 RH |
8247 | |
8248 | /* While the symbol is undefined, we have to be prepared to handle | |
8249 | a zero size. */ | |
8250 | if (h->root.type == bfd_link_hash_undefined) | |
8251 | size = addend; | |
8252 | else | |
8253 | { | |
8254 | size = h->size; | |
8255 | if (size < addend) | |
8256 | { | |
8257 | /* Oops! We've got a reference past the defined end of | |
8258 | the table. This is probably a bug -- shall we warn? */ | |
8259 | size = addend; | |
8260 | } | |
8261 | } | |
8262 | ||
8263 | /* Allocate one extra entry for use as a "done" flag for the | |
8264 | consolidation pass. */ | |
b34976b6 | 8265 | bytes = (size / file_align + 1) * sizeof (bfd_boolean); |
252b5132 RH |
8266 | |
8267 | if (ptr) | |
8268 | { | |
dc810e39 | 8269 | ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes); |
3e932841 | 8270 | |
fed79cc6 NC |
8271 | if (ptr != NULL) |
8272 | { | |
8273 | size_t oldbytes; | |
252b5132 | 8274 | |
a7b97311 | 8275 | oldbytes = ((h->vtable_entries_size / file_align + 1) |
b34976b6 | 8276 | * sizeof (bfd_boolean)); |
a7b97311 | 8277 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); |
fed79cc6 | 8278 | } |
252b5132 RH |
8279 | } |
8280 | else | |
dc810e39 | 8281 | ptr = bfd_zmalloc ((bfd_size_type) bytes); |
252b5132 | 8282 | |
fed79cc6 | 8283 | if (ptr == NULL) |
b34976b6 | 8284 | return FALSE; |
3e932841 | 8285 | |
252b5132 | 8286 | /* And arrange for that done flag to be at index -1. */ |
fed79cc6 | 8287 | h->vtable_entries_used = ptr + 1; |
252b5132 RH |
8288 | h->vtable_entries_size = size; |
8289 | } | |
3e932841 | 8290 | |
b34976b6 | 8291 | h->vtable_entries_used[addend / file_align] = TRUE; |
252b5132 | 8292 | |
b34976b6 | 8293 | return TRUE; |
252b5132 RH |
8294 | } |
8295 | ||
8296 | /* And an accompanying bit to work out final got entry offsets once | |
8297 | we're done. Should be called from final_link. */ | |
8298 | ||
b34976b6 | 8299 | bfd_boolean |
252b5132 RH |
8300 | elf_gc_common_finalize_got_offsets (abfd, info) |
8301 | bfd *abfd; | |
8302 | struct bfd_link_info *info; | |
8303 | { | |
8304 | bfd *i; | |
8305 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8306 | bfd_vma gotoff; | |
8307 | ||
8308 | /* The GOT offset is relative to the .got section, but the GOT header is | |
8309 | put into the .got.plt section, if the backend uses it. */ | |
8310 | if (bed->want_got_plt) | |
8311 | gotoff = 0; | |
8312 | else | |
8313 | gotoff = bed->got_header_size; | |
8314 | ||
8315 | /* Do the local .got entries first. */ | |
8316 | for (i = info->input_bfds; i; i = i->link_next) | |
8317 | { | |
f6af82bd | 8318 | bfd_signed_vma *local_got; |
252b5132 RH |
8319 | bfd_size_type j, locsymcount; |
8320 | Elf_Internal_Shdr *symtab_hdr; | |
8321 | ||
f6af82bd AM |
8322 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) |
8323 | continue; | |
8324 | ||
8325 | local_got = elf_local_got_refcounts (i); | |
252b5132 RH |
8326 | if (!local_got) |
8327 | continue; | |
8328 | ||
8329 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
8330 | if (elf_bad_symtab (i)) | |
8331 | locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym); | |
8332 | else | |
8333 | locsymcount = symtab_hdr->sh_info; | |
8334 | ||
8335 | for (j = 0; j < locsymcount; ++j) | |
8336 | { | |
8337 | if (local_got[j] > 0) | |
8338 | { | |
8339 | local_got[j] = gotoff; | |
8340 | gotoff += ARCH_SIZE / 8; | |
8341 | } | |
8342 | else | |
8343 | local_got[j] = (bfd_vma) -1; | |
8344 | } | |
8345 | } | |
8346 | ||
dd5724d5 AM |
8347 | /* Then the global .got entries. .plt refcounts are handled by |
8348 | adjust_dynamic_symbol */ | |
252b5132 RH |
8349 | elf_link_hash_traverse (elf_hash_table (info), |
8350 | elf_gc_allocate_got_offsets, | |
8351 | (PTR) &gotoff); | |
b34976b6 | 8352 | return TRUE; |
252b5132 RH |
8353 | } |
8354 | ||
8355 | /* We need a special top-level link routine to convert got reference counts | |
8356 | to real got offsets. */ | |
8357 | ||
b34976b6 | 8358 | static bfd_boolean |
252b5132 RH |
8359 | elf_gc_allocate_got_offsets (h, offarg) |
8360 | struct elf_link_hash_entry *h; | |
8361 | PTR offarg; | |
8362 | { | |
8363 | bfd_vma *off = (bfd_vma *) offarg; | |
8364 | ||
e92d460e AM |
8365 | if (h->root.type == bfd_link_hash_warning) |
8366 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8367 | ||
252b5132 RH |
8368 | if (h->got.refcount > 0) |
8369 | { | |
8370 | h->got.offset = off[0]; | |
8371 | off[0] += ARCH_SIZE / 8; | |
8372 | } | |
8373 | else | |
8374 | h->got.offset = (bfd_vma) -1; | |
8375 | ||
b34976b6 | 8376 | return TRUE; |
252b5132 RH |
8377 | } |
8378 | ||
8379 | /* Many folk need no more in the way of final link than this, once | |
8380 | got entry reference counting is enabled. */ | |
8381 | ||
b34976b6 | 8382 | bfd_boolean |
252b5132 RH |
8383 | elf_gc_common_final_link (abfd, info) |
8384 | bfd *abfd; | |
8385 | struct bfd_link_info *info; | |
8386 | { | |
8387 | if (!elf_gc_common_finalize_got_offsets (abfd, info)) | |
b34976b6 | 8388 | return FALSE; |
252b5132 RH |
8389 | |
8390 | /* Invoke the regular ELF backend linker to do all the work. */ | |
8391 | return elf_bfd_final_link (abfd, info); | |
8392 | } | |
8393 | ||
8394 | /* This function will be called though elf_link_hash_traverse to store | |
8395 | all hash value of the exported symbols in an array. */ | |
8396 | ||
b34976b6 | 8397 | static bfd_boolean |
252b5132 RH |
8398 | elf_collect_hash_codes (h, data) |
8399 | struct elf_link_hash_entry *h; | |
8400 | PTR data; | |
8401 | { | |
8402 | unsigned long **valuep = (unsigned long **) data; | |
8403 | const char *name; | |
8404 | char *p; | |
8405 | unsigned long ha; | |
8406 | char *alc = NULL; | |
8407 | ||
e92d460e AM |
8408 | if (h->root.type == bfd_link_hash_warning) |
8409 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8410 | ||
252b5132 RH |
8411 | /* Ignore indirect symbols. These are added by the versioning code. */ |
8412 | if (h->dynindx == -1) | |
b34976b6 | 8413 | return TRUE; |
252b5132 RH |
8414 | |
8415 | name = h->root.root.string; | |
8416 | p = strchr (name, ELF_VER_CHR); | |
8417 | if (p != NULL) | |
8418 | { | |
dc810e39 AM |
8419 | alc = bfd_malloc ((bfd_size_type) (p - name + 1)); |
8420 | memcpy (alc, name, (size_t) (p - name)); | |
252b5132 RH |
8421 | alc[p - name] = '\0'; |
8422 | name = alc; | |
8423 | } | |
8424 | ||
8425 | /* Compute the hash value. */ | |
8426 | ha = bfd_elf_hash (name); | |
8427 | ||
8428 | /* Store the found hash value in the array given as the argument. */ | |
8429 | *(*valuep)++ = ha; | |
8430 | ||
8431 | /* And store it in the struct so that we can put it in the hash table | |
8432 | later. */ | |
8433 | h->elf_hash_value = ha; | |
8434 | ||
8435 | if (alc != NULL) | |
8436 | free (alc); | |
8437 | ||
b34976b6 | 8438 | return TRUE; |
252b5132 | 8439 | } |
73d074b4 | 8440 | |
b34976b6 | 8441 | bfd_boolean |
73d074b4 DJ |
8442 | elf_reloc_symbol_deleted_p (offset, cookie) |
8443 | bfd_vma offset; | |
8444 | PTR cookie; | |
8445 | { | |
9ad5cbcf | 8446 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
73d074b4 DJ |
8447 | |
8448 | if (rcookie->bad_symtab) | |
8449 | rcookie->rel = rcookie->rels; | |
8450 | ||
8451 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
8452 | { | |
d6fe2dc1 | 8453 | unsigned long r_symndx; |
73d074b4 DJ |
8454 | |
8455 | if (! rcookie->bad_symtab) | |
8456 | if (rcookie->rel->r_offset > offset) | |
b34976b6 | 8457 | return FALSE; |
73d074b4 DJ |
8458 | if (rcookie->rel->r_offset != offset) |
8459 | continue; | |
8460 | ||
d6fe2dc1 AM |
8461 | r_symndx = ELF_R_SYM (rcookie->rel->r_info); |
8462 | if (r_symndx == SHN_UNDEF) | |
b34976b6 | 8463 | return TRUE; |
d6fe2dc1 | 8464 | |
73d074b4 | 8465 | if (r_symndx >= rcookie->locsymcount |
6cdc0ccc | 8466 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) |
73d074b4 DJ |
8467 | { |
8468 | struct elf_link_hash_entry *h; | |
8469 | ||
8470 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
8471 | ||
8472 | while (h->root.type == bfd_link_hash_indirect | |
8473 | || h->root.type == bfd_link_hash_warning) | |
8474 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8475 | ||
8476 | if ((h->root.type == bfd_link_hash_defined | |
8477 | || h->root.type == bfd_link_hash_defweak) | |
ed4de5e2 | 8478 | && elf_discarded_section (h->root.u.def.section)) |
b34976b6 | 8479 | return TRUE; |
73d074b4 | 8480 | else |
b34976b6 | 8481 | return FALSE; |
73d074b4 | 8482 | } |
6cdc0ccc | 8483 | else |
73d074b4 DJ |
8484 | { |
8485 | /* It's not a relocation against a global symbol, | |
44421011 | 8486 | but it could be a relocation against a local |
73d074b4 DJ |
8487 | symbol for a discarded section. */ |
8488 | asection *isec; | |
6cdc0ccc | 8489 | Elf_Internal_Sym *isym; |
73d074b4 DJ |
8490 | |
8491 | /* Need to: get the symbol; get the section. */ | |
6cdc0ccc AM |
8492 | isym = &rcookie->locsyms[r_symndx]; |
8493 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
73d074b4 | 8494 | { |
6cdc0ccc | 8495 | isec = section_from_elf_index (rcookie->abfd, isym->st_shndx); |
ed4de5e2 | 8496 | if (isec != NULL && elf_discarded_section (isec)) |
b34976b6 | 8497 | return TRUE; |
73d074b4 DJ |
8498 | } |
8499 | } | |
b34976b6 | 8500 | return FALSE; |
73d074b4 | 8501 | } |
b34976b6 | 8502 | return FALSE; |
73d074b4 DJ |
8503 | } |
8504 | ||
8505 | /* Discard unneeded references to discarded sections. | |
b34976b6 | 8506 | Returns TRUE if any section's size was changed. */ |
73d074b4 | 8507 | /* This function assumes that the relocations are in sorted order, |
ab3acfbe | 8508 | which is true for all known assemblers. */ |
73d074b4 | 8509 | |
b34976b6 | 8510 | bfd_boolean |
65765700 JJ |
8511 | elf_bfd_discard_info (output_bfd, info) |
8512 | bfd *output_bfd; | |
73d074b4 DJ |
8513 | struct bfd_link_info *info; |
8514 | { | |
8515 | struct elf_reloc_cookie cookie; | |
126495ed | 8516 | asection *stab, *eh; |
73d074b4 | 8517 | Elf_Internal_Shdr *symtab_hdr; |
73d074b4 DJ |
8518 | struct elf_backend_data *bed; |
8519 | bfd *abfd; | |
99eb2ac8 | 8520 | unsigned int count; |
b34976b6 | 8521 | bfd_boolean ret = FALSE; |
73d074b4 | 8522 | |
d6fe2dc1 | 8523 | if (info->traditional_format |
73d074b4 | 8524 | || info->hash->creator->flavour != bfd_target_elf_flavour |
65765700 | 8525 | || ! is_elf_hash_table (info)) |
b34976b6 | 8526 | return FALSE; |
65765700 | 8527 | |
73d074b4 DJ |
8528 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) |
8529 | { | |
163c1c30 L |
8530 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
8531 | continue; | |
8532 | ||
73d074b4 DJ |
8533 | bed = get_elf_backend_data (abfd); |
8534 | ||
8535 | if ((abfd->flags & DYNAMIC) != 0) | |
8536 | continue; | |
8537 | ||
126495ed | 8538 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); |
2d36fe5f AH |
8539 | if (info->relocateable |
8540 | || (eh != NULL | |
8541 | && (eh->_raw_size == 0 | |
8542 | || bfd_is_abs_section (eh->output_section)))) | |
126495ed | 8543 | eh = NULL; |
65765700 | 8544 | |
99eb2ac8 AM |
8545 | stab = bfd_get_section_by_name (abfd, ".stab"); |
8546 | if (stab != NULL | |
8547 | && (stab->_raw_size == 0 | |
8548 | || bfd_is_abs_section (stab->output_section) | |
68bfbfcc | 8549 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) |
99eb2ac8 AM |
8550 | stab = NULL; |
8551 | ||
8552 | if (stab == NULL | |
8553 | && eh == NULL | |
8554 | && bed->elf_backend_discard_info == NULL) | |
73d074b4 DJ |
8555 | continue; |
8556 | ||
8557 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
73d074b4 DJ |
8558 | cookie.abfd = abfd; |
8559 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
8560 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
8561 | if (cookie.bad_symtab) | |
8562 | { | |
99eb2ac8 | 8563 | cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym); |
73d074b4 DJ |
8564 | cookie.extsymoff = 0; |
8565 | } | |
8566 | else | |
8567 | { | |
8568 | cookie.locsymcount = symtab_hdr->sh_info; | |
8569 | cookie.extsymoff = symtab_hdr->sh_info; | |
8570 | } | |
8571 | ||
6cdc0ccc AM |
8572 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; |
8573 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
c44233aa | 8574 | { |
6cdc0ccc AM |
8575 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, |
8576 | cookie.locsymcount, 0, | |
8577 | NULL, NULL, NULL); | |
c44233aa | 8578 | if (cookie.locsyms == NULL) |
b34976b6 | 8579 | return FALSE; |
9ad5cbcf | 8580 | } |
73d074b4 | 8581 | |
99eb2ac8 | 8582 | if (stab != NULL) |
73d074b4 | 8583 | { |
99eb2ac8 AM |
8584 | cookie.rels = NULL; |
8585 | count = stab->reloc_count; | |
8586 | if (count != 0) | |
8587 | cookie.rels = (NAME(_bfd_elf,link_read_relocs) | |
8588 | (abfd, stab, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
8589 | info->keep_memory)); | |
8590 | if (cookie.rels != NULL) | |
73d074b4 DJ |
8591 | { |
8592 | cookie.rel = cookie.rels; | |
99eb2ac8 AM |
8593 | cookie.relend = cookie.rels; |
8594 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
65765700 JJ |
8595 | if (_bfd_discard_section_stabs (abfd, stab, |
8596 | elf_section_data (stab)->sec_info, | |
73d074b4 DJ |
8597 | elf_reloc_symbol_deleted_p, |
8598 | &cookie)) | |
b34976b6 | 8599 | ret = TRUE; |
6cdc0ccc | 8600 | if (elf_section_data (stab)->relocs != cookie.rels) |
73d074b4 DJ |
8601 | free (cookie.rels); |
8602 | } | |
8603 | } | |
8604 | ||
99eb2ac8 | 8605 | if (eh != NULL) |
65765700 JJ |
8606 | { |
8607 | cookie.rels = NULL; | |
99eb2ac8 AM |
8608 | count = eh->reloc_count; |
8609 | if (count != 0) | |
65765700 | 8610 | cookie.rels = (NAME(_bfd_elf,link_read_relocs) |
40b829d4 | 8611 | (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL, |
65765700 | 8612 | info->keep_memory)); |
99eb2ac8 AM |
8613 | cookie.rel = cookie.rels; |
8614 | cookie.relend = cookie.rels; | |
8615 | if (cookie.rels != NULL) | |
8616 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
8617 | ||
126495ed | 8618 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, |
65765700 JJ |
8619 | elf_reloc_symbol_deleted_p, |
8620 | &cookie)) | |
b34976b6 | 8621 | ret = TRUE; |
99eb2ac8 AM |
8622 | |
8623 | if (cookie.rels != NULL | |
8624 | && elf_section_data (eh)->relocs != cookie.rels) | |
65765700 JJ |
8625 | free (cookie.rels); |
8626 | } | |
8627 | ||
99eb2ac8 AM |
8628 | if (bed->elf_backend_discard_info != NULL |
8629 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
b34976b6 | 8630 | ret = TRUE; |
73d074b4 | 8631 | |
6cdc0ccc AM |
8632 | if (cookie.locsyms != NULL |
8633 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
8634 | { | |
8635 | if (! info->keep_memory) | |
8636 | free (cookie.locsyms); | |
8637 | else | |
8638 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
8639 | } | |
73d074b4 | 8640 | } |
65765700 | 8641 | |
126495ed | 8642 | if (info->eh_frame_hdr |
2d36fe5f | 8643 | && !info->relocateable |
126495ed | 8644 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
b34976b6 | 8645 | ret = TRUE; |
99eb2ac8 | 8646 | |
73d074b4 DJ |
8647 | return ret; |
8648 | } | |
8649 | ||
b34976b6 | 8650 | static bfd_boolean |
73d074b4 DJ |
8651 | elf_section_ignore_discarded_relocs (sec) |
8652 | asection *sec; | |
8653 | { | |
40b829d4 AM |
8654 | struct elf_backend_data *bed; |
8655 | ||
68bfbfcc | 8656 | switch (sec->sec_info_type) |
65765700 JJ |
8657 | { |
8658 | case ELF_INFO_TYPE_STABS: | |
8659 | case ELF_INFO_TYPE_EH_FRAME: | |
b34976b6 | 8660 | return TRUE; |
65765700 JJ |
8661 | default: |
8662 | break; | |
8663 | } | |
40b829d4 AM |
8664 | |
8665 | bed = get_elf_backend_data (sec->owner); | |
8666 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
8667 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
b34976b6 | 8668 | return TRUE; |
65765700 | 8669 | |
b34976b6 | 8670 | return FALSE; |
73d074b4 | 8671 | } |