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