Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
64d03ab5 AM |
2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
3 | 2005, 2006 Free Software Foundation, Inc. | |
252b5132 | 4 | |
8fdd7217 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
8fdd7217 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
252b5132 | 11 | |
8fdd7217 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
8fdd7217 NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
3e110533 | 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
252b5132 RH |
20 | |
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "bfdlink.h" | |
24 | #include "libbfd.h" | |
25 | #define ARCH_SIZE 0 | |
26 | #include "elf-bfd.h" | |
4ad4eba5 | 27 | #include "safe-ctype.h" |
ccf2f652 | 28 | #include "libiberty.h" |
66eb6687 | 29 | #include "objalloc.h" |
252b5132 | 30 | |
d98685ac AM |
31 | /* Define a symbol in a dynamic linkage section. */ |
32 | ||
33 | struct elf_link_hash_entry * | |
34 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
35 | struct bfd_link_info *info, | |
36 | asection *sec, | |
37 | const char *name) | |
38 | { | |
39 | struct elf_link_hash_entry *h; | |
40 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 41 | const struct elf_backend_data *bed; |
d98685ac AM |
42 | |
43 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
44 | if (h != NULL) | |
45 | { | |
46 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
47 | This is a symptom of a larger problem: Absolute symbols | |
48 | defined in shared libraries can't be overridden, because we | |
49 | lose the link to the bfd which is via the symbol section. */ | |
50 | h->root.type = bfd_link_hash_new; | |
51 | } | |
52 | ||
53 | bh = &h->root; | |
54 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, | |
55 | sec, 0, NULL, FALSE, | |
56 | get_elf_backend_data (abfd)->collect, | |
57 | &bh)) | |
58 | return NULL; | |
59 | h = (struct elf_link_hash_entry *) bh; | |
60 | h->def_regular = 1; | |
61 | h->type = STT_OBJECT; | |
62 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
63 | ||
ccabcbe5 AM |
64 | bed = get_elf_backend_data (abfd); |
65 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
d98685ac AM |
66 | return h; |
67 | } | |
68 | ||
b34976b6 | 69 | bfd_boolean |
268b6b39 | 70 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
71 | { |
72 | flagword flags; | |
aad5d350 | 73 | asection *s; |
252b5132 | 74 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 75 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
76 | int ptralign; |
77 | ||
78 | /* This function may be called more than once. */ | |
aad5d350 AM |
79 | s = bfd_get_section_by_name (abfd, ".got"); |
80 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 81 | return TRUE; |
252b5132 RH |
82 | |
83 | switch (bed->s->arch_size) | |
84 | { | |
bb0deeff AO |
85 | case 32: |
86 | ptralign = 2; | |
87 | break; | |
88 | ||
89 | case 64: | |
90 | ptralign = 3; | |
91 | break; | |
92 | ||
93 | default: | |
94 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 95 | return FALSE; |
252b5132 RH |
96 | } |
97 | ||
e5a52504 | 98 | flags = bed->dynamic_sec_flags; |
252b5132 | 99 | |
3496cb2a | 100 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
252b5132 | 101 | if (s == NULL |
252b5132 | 102 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 103 | return FALSE; |
252b5132 RH |
104 | |
105 | if (bed->want_got_plt) | |
106 | { | |
3496cb2a | 107 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 108 | if (s == NULL |
252b5132 | 109 | || !bfd_set_section_alignment (abfd, s, ptralign)) |
b34976b6 | 110 | return FALSE; |
252b5132 RH |
111 | } |
112 | ||
2517a57f AM |
113 | if (bed->want_got_sym) |
114 | { | |
115 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
116 | (or .got.plt) section. We don't do this in the linker script | |
117 | because we don't want to define the symbol if we are not creating | |
118 | a global offset table. */ | |
d98685ac | 119 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); |
2517a57f | 120 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
121 | if (h == NULL) |
122 | return FALSE; | |
2517a57f | 123 | } |
252b5132 RH |
124 | |
125 | /* The first bit of the global offset table is the header. */ | |
3b36f7e6 | 126 | s->size += bed->got_header_size; |
252b5132 | 127 | |
b34976b6 | 128 | return TRUE; |
252b5132 RH |
129 | } |
130 | \f | |
7e9f0867 AM |
131 | /* Create a strtab to hold the dynamic symbol names. */ |
132 | static bfd_boolean | |
133 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
134 | { | |
135 | struct elf_link_hash_table *hash_table; | |
136 | ||
137 | hash_table = elf_hash_table (info); | |
138 | if (hash_table->dynobj == NULL) | |
139 | hash_table->dynobj = abfd; | |
140 | ||
141 | if (hash_table->dynstr == NULL) | |
142 | { | |
143 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
144 | if (hash_table->dynstr == NULL) | |
145 | return FALSE; | |
146 | } | |
147 | return TRUE; | |
148 | } | |
149 | ||
45d6a902 AM |
150 | /* Create some sections which will be filled in with dynamic linking |
151 | information. ABFD is an input file which requires dynamic sections | |
152 | to be created. The dynamic sections take up virtual memory space | |
153 | when the final executable is run, so we need to create them before | |
154 | addresses are assigned to the output sections. We work out the | |
155 | actual contents and size of these sections later. */ | |
252b5132 | 156 | |
b34976b6 | 157 | bfd_boolean |
268b6b39 | 158 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 159 | { |
45d6a902 AM |
160 | flagword flags; |
161 | register asection *s; | |
9c5bfbb7 | 162 | const struct elf_backend_data *bed; |
252b5132 | 163 | |
0eddce27 | 164 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
165 | return FALSE; |
166 | ||
167 | if (elf_hash_table (info)->dynamic_sections_created) | |
168 | return TRUE; | |
169 | ||
7e9f0867 AM |
170 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
171 | return FALSE; | |
45d6a902 | 172 | |
7e9f0867 | 173 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
174 | bed = get_elf_backend_data (abfd); |
175 | ||
176 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
177 | |
178 | /* A dynamically linked executable has a .interp section, but a | |
179 | shared library does not. */ | |
36af4a4e | 180 | if (info->executable) |
252b5132 | 181 | { |
3496cb2a L |
182 | s = bfd_make_section_with_flags (abfd, ".interp", |
183 | flags | SEC_READONLY); | |
184 | if (s == NULL) | |
45d6a902 AM |
185 | return FALSE; |
186 | } | |
bb0deeff | 187 | |
45d6a902 AM |
188 | /* Create sections to hold version informations. These are removed |
189 | if they are not needed. */ | |
3496cb2a L |
190 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
191 | flags | SEC_READONLY); | |
45d6a902 | 192 | if (s == NULL |
45d6a902 AM |
193 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
194 | return FALSE; | |
195 | ||
3496cb2a L |
196 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
197 | flags | SEC_READONLY); | |
45d6a902 | 198 | if (s == NULL |
45d6a902 AM |
199 | || ! bfd_set_section_alignment (abfd, s, 1)) |
200 | return FALSE; | |
201 | ||
3496cb2a L |
202 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
203 | flags | SEC_READONLY); | |
45d6a902 | 204 | if (s == NULL |
45d6a902 AM |
205 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
206 | return FALSE; | |
207 | ||
3496cb2a L |
208 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
209 | flags | SEC_READONLY); | |
45d6a902 | 210 | if (s == NULL |
45d6a902 AM |
211 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
212 | return FALSE; | |
213 | ||
3496cb2a L |
214 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
215 | flags | SEC_READONLY); | |
216 | if (s == NULL) | |
45d6a902 AM |
217 | return FALSE; |
218 | ||
3496cb2a | 219 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 220 | if (s == NULL |
45d6a902 AM |
221 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
222 | return FALSE; | |
223 | ||
224 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
225 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
226 | only want to define it if we are, in fact, creating a .dynamic | |
227 | section. We don't want to define it if there is no .dynamic | |
228 | section, since on some ELF platforms the start up code examines it | |
229 | to decide how to initialize the process. */ | |
d98685ac | 230 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
231 | return FALSE; |
232 | ||
fdc90cb4 JJ |
233 | if (info->emit_hash) |
234 | { | |
235 | s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY); | |
236 | if (s == NULL | |
237 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
238 | return FALSE; | |
239 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
240 | } | |
241 | ||
242 | if (info->emit_gnu_hash) | |
243 | { | |
244 | s = bfd_make_section_with_flags (abfd, ".gnu.hash", | |
245 | flags | SEC_READONLY); | |
246 | if (s == NULL | |
247 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
248 | return FALSE; | |
249 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
250 | 4 32-bit words followed by variable count of 64-bit words, then | |
251 | variable count of 32-bit words. */ | |
252 | if (bed->s->arch_size == 64) | |
253 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
254 | else | |
255 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
256 | } | |
45d6a902 AM |
257 | |
258 | /* Let the backend create the rest of the sections. This lets the | |
259 | backend set the right flags. The backend will normally create | |
260 | the .got and .plt sections. */ | |
261 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
262 | return FALSE; | |
263 | ||
264 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
265 | ||
266 | return TRUE; | |
267 | } | |
268 | ||
269 | /* Create dynamic sections when linking against a dynamic object. */ | |
270 | ||
271 | bfd_boolean | |
268b6b39 | 272 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
273 | { |
274 | flagword flags, pltflags; | |
7325306f | 275 | struct elf_link_hash_entry *h; |
45d6a902 | 276 | asection *s; |
9c5bfbb7 | 277 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 278 | |
252b5132 RH |
279 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
280 | .rel[a].bss sections. */ | |
e5a52504 | 281 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
282 | |
283 | pltflags = flags; | |
252b5132 | 284 | if (bed->plt_not_loaded) |
6df4d94c MM |
285 | /* We do not clear SEC_ALLOC here because we still want the OS to |
286 | allocate space for the section; it's just that there's nothing | |
287 | to read in from the object file. */ | |
5d1634d7 | 288 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
289 | else |
290 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
291 | if (bed->plt_readonly) |
292 | pltflags |= SEC_READONLY; | |
293 | ||
3496cb2a | 294 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 295 | if (s == NULL |
252b5132 | 296 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 297 | return FALSE; |
252b5132 | 298 | |
d98685ac AM |
299 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
300 | .plt section. */ | |
7325306f RS |
301 | if (bed->want_plt_sym) |
302 | { | |
303 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
304 | "_PROCEDURE_LINKAGE_TABLE_"); | |
305 | elf_hash_table (info)->hplt = h; | |
306 | if (h == NULL) | |
307 | return FALSE; | |
308 | } | |
252b5132 | 309 | |
3496cb2a L |
310 | s = bfd_make_section_with_flags (abfd, |
311 | (bed->default_use_rela_p | |
312 | ? ".rela.plt" : ".rel.plt"), | |
313 | flags | SEC_READONLY); | |
252b5132 | 314 | if (s == NULL |
45d6a902 | 315 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 316 | return FALSE; |
252b5132 RH |
317 | |
318 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 319 | return FALSE; |
252b5132 | 320 | |
3018b441 RH |
321 | if (bed->want_dynbss) |
322 | { | |
323 | /* The .dynbss section is a place to put symbols which are defined | |
324 | by dynamic objects, are referenced by regular objects, and are | |
325 | not functions. We must allocate space for them in the process | |
326 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
327 | initialize them at run time. The linker script puts the .dynbss | |
328 | section into the .bss section of the final image. */ | |
3496cb2a L |
329 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
330 | (SEC_ALLOC | |
331 | | SEC_LINKER_CREATED)); | |
332 | if (s == NULL) | |
b34976b6 | 333 | return FALSE; |
252b5132 | 334 | |
3018b441 | 335 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
336 | normally needed. We need to create it here, though, so that the |
337 | linker will map it to an output section. We can't just create it | |
338 | only if we need it, because we will not know whether we need it | |
339 | until we have seen all the input files, and the first time the | |
340 | main linker code calls BFD after examining all the input files | |
341 | (size_dynamic_sections) the input sections have already been | |
342 | mapped to the output sections. If the section turns out not to | |
343 | be needed, we can discard it later. We will never need this | |
344 | section when generating a shared object, since they do not use | |
345 | copy relocs. */ | |
3018b441 RH |
346 | if (! info->shared) |
347 | { | |
3496cb2a L |
348 | s = bfd_make_section_with_flags (abfd, |
349 | (bed->default_use_rela_p | |
350 | ? ".rela.bss" : ".rel.bss"), | |
351 | flags | SEC_READONLY); | |
3018b441 | 352 | if (s == NULL |
45d6a902 | 353 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 354 | return FALSE; |
3018b441 | 355 | } |
252b5132 RH |
356 | } |
357 | ||
b34976b6 | 358 | return TRUE; |
252b5132 RH |
359 | } |
360 | \f | |
252b5132 RH |
361 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
362 | read the input files, since we need to have a list of all of them | |
363 | before we can determine the final sizes of the output sections. | |
364 | Note that we may actually call this function even though we are not | |
365 | going to output any dynamic symbols; in some cases we know that a | |
366 | symbol should be in the dynamic symbol table, but only if there is | |
367 | one. */ | |
368 | ||
b34976b6 | 369 | bfd_boolean |
c152c796 AM |
370 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
371 | struct elf_link_hash_entry *h) | |
252b5132 RH |
372 | { |
373 | if (h->dynindx == -1) | |
374 | { | |
2b0f7ef9 | 375 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 376 | char *p; |
252b5132 | 377 | const char *name; |
252b5132 RH |
378 | bfd_size_type indx; |
379 | ||
7a13edea NC |
380 | /* XXX: The ABI draft says the linker must turn hidden and |
381 | internal symbols into STB_LOCAL symbols when producing the | |
382 | DSO. However, if ld.so honors st_other in the dynamic table, | |
383 | this would not be necessary. */ | |
384 | switch (ELF_ST_VISIBILITY (h->other)) | |
385 | { | |
386 | case STV_INTERNAL: | |
387 | case STV_HIDDEN: | |
9d6eee78 L |
388 | if (h->root.type != bfd_link_hash_undefined |
389 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 390 | { |
f5385ebf | 391 | h->forced_local = 1; |
67687978 PB |
392 | if (!elf_hash_table (info)->is_relocatable_executable) |
393 | return TRUE; | |
7a13edea | 394 | } |
0444bdd4 | 395 | |
7a13edea NC |
396 | default: |
397 | break; | |
398 | } | |
399 | ||
252b5132 RH |
400 | h->dynindx = elf_hash_table (info)->dynsymcount; |
401 | ++elf_hash_table (info)->dynsymcount; | |
402 | ||
403 | dynstr = elf_hash_table (info)->dynstr; | |
404 | if (dynstr == NULL) | |
405 | { | |
406 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 407 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 408 | if (dynstr == NULL) |
b34976b6 | 409 | return FALSE; |
252b5132 RH |
410 | } |
411 | ||
412 | /* We don't put any version information in the dynamic string | |
aad5d350 | 413 | table. */ |
252b5132 RH |
414 | name = h->root.root.string; |
415 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
416 | if (p != NULL) |
417 | /* We know that the p points into writable memory. In fact, | |
418 | there are only a few symbols that have read-only names, being | |
419 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
420 | by the backends. Most symbols will have names pointing into | |
421 | an ELF string table read from a file, or to objalloc memory. */ | |
422 | *p = 0; | |
423 | ||
424 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
425 | ||
426 | if (p != NULL) | |
427 | *p = ELF_VER_CHR; | |
252b5132 RH |
428 | |
429 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 430 | return FALSE; |
252b5132 RH |
431 | h->dynstr_index = indx; |
432 | } | |
433 | ||
b34976b6 | 434 | return TRUE; |
252b5132 | 435 | } |
45d6a902 | 436 | \f |
55255dae L |
437 | /* Mark a symbol dynamic. */ |
438 | ||
439 | void | |
440 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, | |
441 | struct elf_link_hash_entry *h) | |
442 | { | |
443 | struct bfd_elf_dynamic_list *d = info->dynamic; | |
444 | ||
445 | if (d == NULL || info->relocatable) | |
446 | return; | |
447 | ||
448 | if ((*d->match) (&d->head, NULL, h->root.root.string)) | |
449 | h->dynamic = 1; | |
450 | } | |
451 | ||
45d6a902 AM |
452 | /* Record an assignment to a symbol made by a linker script. We need |
453 | this in case some dynamic object refers to this symbol. */ | |
454 | ||
455 | bfd_boolean | |
fe21a8fc L |
456 | bfd_elf_record_link_assignment (bfd *output_bfd, |
457 | struct bfd_link_info *info, | |
268b6b39 | 458 | const char *name, |
fe21a8fc L |
459 | bfd_boolean provide, |
460 | bfd_boolean hidden) | |
45d6a902 AM |
461 | { |
462 | struct elf_link_hash_entry *h; | |
4ea42fb7 | 463 | struct elf_link_hash_table *htab; |
45d6a902 | 464 | |
0eddce27 | 465 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
466 | return TRUE; |
467 | ||
4ea42fb7 AM |
468 | htab = elf_hash_table (info); |
469 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 470 | if (h == NULL) |
4ea42fb7 | 471 | return provide; |
45d6a902 | 472 | |
02bb6eae AO |
473 | /* Since we're defining the symbol, don't let it seem to have not |
474 | been defined. record_dynamic_symbol and size_dynamic_sections | |
77cfaee6 | 475 | may depend on this. */ |
02bb6eae AO |
476 | if (h->root.type == bfd_link_hash_undefweak |
477 | || h->root.type == bfd_link_hash_undefined) | |
77cfaee6 | 478 | { |
4ea42fb7 | 479 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
480 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
481 | bfd_link_repair_undef_list (&htab->root); | |
77cfaee6 | 482 | } |
02bb6eae | 483 | |
45d6a902 | 484 | if (h->root.type == bfd_link_hash_new) |
55255dae L |
485 | { |
486 | bfd_elf_link_mark_dynamic_symbol (info, h); | |
487 | h->non_elf = 0; | |
488 | } | |
45d6a902 AM |
489 | |
490 | /* If this symbol is being provided by the linker script, and it is | |
491 | currently defined by a dynamic object, but not by a regular | |
492 | object, then mark it as undefined so that the generic linker will | |
493 | force the correct value. */ | |
494 | if (provide | |
f5385ebf AM |
495 | && h->def_dynamic |
496 | && !h->def_regular) | |
45d6a902 AM |
497 | h->root.type = bfd_link_hash_undefined; |
498 | ||
499 | /* If this symbol is not being provided by the linker script, and it is | |
500 | currently defined by a dynamic object, but not by a regular object, | |
501 | then clear out any version information because the symbol will not be | |
502 | associated with the dynamic object any more. */ | |
503 | if (!provide | |
f5385ebf AM |
504 | && h->def_dynamic |
505 | && !h->def_regular) | |
45d6a902 AM |
506 | h->verinfo.verdef = NULL; |
507 | ||
f5385ebf | 508 | h->def_regular = 1; |
45d6a902 | 509 | |
fe21a8fc L |
510 | if (provide && hidden) |
511 | { | |
512 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
513 | ||
514 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
515 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
516 | } | |
517 | ||
6fa3860b PB |
518 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
519 | and executables. */ | |
520 | if (!info->relocatable | |
521 | && h->dynindx != -1 | |
522 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
523 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
524 | h->forced_local = 1; | |
525 | ||
f5385ebf AM |
526 | if ((h->def_dynamic |
527 | || h->ref_dynamic | |
67687978 PB |
528 | || info->shared |
529 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
530 | && h->dynindx == -1) |
531 | { | |
c152c796 | 532 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
533 | return FALSE; |
534 | ||
535 | /* If this is a weak defined symbol, and we know a corresponding | |
536 | real symbol from the same dynamic object, make sure the real | |
537 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
538 | if (h->u.weakdef != NULL |
539 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 540 | { |
f6e332e6 | 541 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
542 | return FALSE; |
543 | } | |
544 | } | |
545 | ||
546 | return TRUE; | |
547 | } | |
42751cf3 | 548 | |
8c58d23b AM |
549 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
550 | success, and 2 on a failure caused by attempting to record a symbol | |
551 | in a discarded section, eg. a discarded link-once section symbol. */ | |
552 | ||
553 | int | |
c152c796 AM |
554 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
555 | bfd *input_bfd, | |
556 | long input_indx) | |
8c58d23b AM |
557 | { |
558 | bfd_size_type amt; | |
559 | struct elf_link_local_dynamic_entry *entry; | |
560 | struct elf_link_hash_table *eht; | |
561 | struct elf_strtab_hash *dynstr; | |
562 | unsigned long dynstr_index; | |
563 | char *name; | |
564 | Elf_External_Sym_Shndx eshndx; | |
565 | char esym[sizeof (Elf64_External_Sym)]; | |
566 | ||
0eddce27 | 567 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
568 | return 0; |
569 | ||
570 | /* See if the entry exists already. */ | |
571 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
572 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
573 | return 1; | |
574 | ||
575 | amt = sizeof (*entry); | |
268b6b39 | 576 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
577 | if (entry == NULL) |
578 | return 0; | |
579 | ||
580 | /* Go find the symbol, so that we can find it's name. */ | |
581 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 582 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
583 | { |
584 | bfd_release (input_bfd, entry); | |
585 | return 0; | |
586 | } | |
587 | ||
588 | if (entry->isym.st_shndx != SHN_UNDEF | |
589 | && (entry->isym.st_shndx < SHN_LORESERVE | |
590 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
591 | { | |
592 | asection *s; | |
593 | ||
594 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
595 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
596 | { | |
597 | /* We can still bfd_release here as nothing has done another | |
598 | bfd_alloc. We can't do this later in this function. */ | |
599 | bfd_release (input_bfd, entry); | |
600 | return 2; | |
601 | } | |
602 | } | |
603 | ||
604 | name = (bfd_elf_string_from_elf_section | |
605 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
606 | entry->isym.st_name)); | |
607 | ||
608 | dynstr = elf_hash_table (info)->dynstr; | |
609 | if (dynstr == NULL) | |
610 | { | |
611 | /* Create a strtab to hold the dynamic symbol names. */ | |
612 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
613 | if (dynstr == NULL) | |
614 | return 0; | |
615 | } | |
616 | ||
b34976b6 | 617 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
618 | if (dynstr_index == (unsigned long) -1) |
619 | return 0; | |
620 | entry->isym.st_name = dynstr_index; | |
621 | ||
622 | eht = elf_hash_table (info); | |
623 | ||
624 | entry->next = eht->dynlocal; | |
625 | eht->dynlocal = entry; | |
626 | entry->input_bfd = input_bfd; | |
627 | entry->input_indx = input_indx; | |
628 | eht->dynsymcount++; | |
629 | ||
630 | /* Whatever binding the symbol had before, it's now local. */ | |
631 | entry->isym.st_info | |
632 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
633 | ||
634 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
635 | ||
636 | return 1; | |
637 | } | |
638 | ||
30b30c21 | 639 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 640 | |
30b30c21 | 641 | long |
268b6b39 AM |
642 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
643 | bfd *input_bfd, | |
644 | long input_indx) | |
30b30c21 RH |
645 | { |
646 | struct elf_link_local_dynamic_entry *e; | |
647 | ||
648 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
649 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
650 | return e->dynindx; | |
651 | return -1; | |
652 | } | |
653 | ||
654 | /* This function is used to renumber the dynamic symbols, if some of | |
655 | them are removed because they are marked as local. This is called | |
656 | via elf_link_hash_traverse. */ | |
657 | ||
b34976b6 | 658 | static bfd_boolean |
268b6b39 AM |
659 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
660 | void *data) | |
42751cf3 | 661 | { |
268b6b39 | 662 | size_t *count = data; |
30b30c21 | 663 | |
e92d460e AM |
664 | if (h->root.type == bfd_link_hash_warning) |
665 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
666 | ||
6fa3860b PB |
667 | if (h->forced_local) |
668 | return TRUE; | |
669 | ||
670 | if (h->dynindx != -1) | |
671 | h->dynindx = ++(*count); | |
672 | ||
673 | return TRUE; | |
674 | } | |
675 | ||
676 | ||
677 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
678 | STB_LOCAL binding. */ | |
679 | ||
680 | static bfd_boolean | |
681 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
682 | void *data) | |
683 | { | |
684 | size_t *count = data; | |
685 | ||
686 | if (h->root.type == bfd_link_hash_warning) | |
687 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
688 | ||
689 | if (!h->forced_local) | |
690 | return TRUE; | |
691 | ||
42751cf3 | 692 | if (h->dynindx != -1) |
30b30c21 RH |
693 | h->dynindx = ++(*count); |
694 | ||
b34976b6 | 695 | return TRUE; |
42751cf3 | 696 | } |
30b30c21 | 697 | |
aee6f5b4 AO |
698 | /* Return true if the dynamic symbol for a given section should be |
699 | omitted when creating a shared library. */ | |
700 | bfd_boolean | |
701 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
702 | struct bfd_link_info *info, | |
703 | asection *p) | |
704 | { | |
74541ad4 AM |
705 | struct elf_link_hash_table *htab; |
706 | ||
aee6f5b4 AO |
707 | switch (elf_section_data (p)->this_hdr.sh_type) |
708 | { | |
709 | case SHT_PROGBITS: | |
710 | case SHT_NOBITS: | |
711 | /* If sh_type is yet undecided, assume it could be | |
712 | SHT_PROGBITS/SHT_NOBITS. */ | |
713 | case SHT_NULL: | |
74541ad4 AM |
714 | htab = elf_hash_table (info); |
715 | if (p == htab->tls_sec) | |
716 | return FALSE; | |
717 | ||
718 | if (htab->text_index_section != NULL) | |
719 | return p != htab->text_index_section && p != htab->data_index_section; | |
720 | ||
aee6f5b4 AO |
721 | if (strcmp (p->name, ".got") == 0 |
722 | || strcmp (p->name, ".got.plt") == 0 | |
723 | || strcmp (p->name, ".plt") == 0) | |
724 | { | |
725 | asection *ip; | |
aee6f5b4 | 726 | |
74541ad4 AM |
727 | if (htab->dynobj != NULL |
728 | && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL | |
aee6f5b4 AO |
729 | && (ip->flags & SEC_LINKER_CREATED) |
730 | && ip->output_section == p) | |
731 | return TRUE; | |
732 | } | |
733 | return FALSE; | |
734 | ||
735 | /* There shouldn't be section relative relocations | |
736 | against any other section. */ | |
737 | default: | |
738 | return TRUE; | |
739 | } | |
740 | } | |
741 | ||
062e2358 | 742 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
743 | symbol for each output section, which come first. Next come symbols |
744 | which have been forced to local binding. Then all of the back-end | |
745 | allocated local dynamic syms, followed by the rest of the global | |
746 | symbols. */ | |
30b30c21 | 747 | |
554220db AM |
748 | static unsigned long |
749 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
750 | struct bfd_link_info *info, | |
751 | unsigned long *section_sym_count) | |
30b30c21 RH |
752 | { |
753 | unsigned long dynsymcount = 0; | |
754 | ||
67687978 | 755 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 756 | { |
aee6f5b4 | 757 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
758 | asection *p; |
759 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 760 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
761 | && (p->flags & SEC_ALLOC) != 0 |
762 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
763 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
764 | else |
765 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 766 | } |
554220db | 767 | *section_sym_count = dynsymcount; |
30b30c21 | 768 | |
6fa3860b PB |
769 | elf_link_hash_traverse (elf_hash_table (info), |
770 | elf_link_renumber_local_hash_table_dynsyms, | |
771 | &dynsymcount); | |
772 | ||
30b30c21 RH |
773 | if (elf_hash_table (info)->dynlocal) |
774 | { | |
775 | struct elf_link_local_dynamic_entry *p; | |
776 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
777 | p->dynindx = ++dynsymcount; | |
778 | } | |
779 | ||
780 | elf_link_hash_traverse (elf_hash_table (info), | |
781 | elf_link_renumber_hash_table_dynsyms, | |
782 | &dynsymcount); | |
783 | ||
784 | /* There is an unused NULL entry at the head of the table which | |
785 | we must account for in our count. Unless there weren't any | |
786 | symbols, which means we'll have no table at all. */ | |
787 | if (dynsymcount != 0) | |
788 | ++dynsymcount; | |
789 | ||
ccabcbe5 AM |
790 | elf_hash_table (info)->dynsymcount = dynsymcount; |
791 | return dynsymcount; | |
30b30c21 | 792 | } |
252b5132 | 793 | |
45d6a902 AM |
794 | /* This function is called when we want to define a new symbol. It |
795 | handles the various cases which arise when we find a definition in | |
796 | a dynamic object, or when there is already a definition in a | |
797 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
798 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
799 | OVERRIDE if the old symbol is overriding a new definition. We set | |
800 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
801 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
802 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
803 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
804 | object is overridden by a regular object. */ | |
45d6a902 AM |
805 | |
806 | bfd_boolean | |
268b6b39 AM |
807 | _bfd_elf_merge_symbol (bfd *abfd, |
808 | struct bfd_link_info *info, | |
809 | const char *name, | |
810 | Elf_Internal_Sym *sym, | |
811 | asection **psec, | |
812 | bfd_vma *pvalue, | |
af44c138 | 813 | unsigned int *pold_alignment, |
268b6b39 AM |
814 | struct elf_link_hash_entry **sym_hash, |
815 | bfd_boolean *skip, | |
816 | bfd_boolean *override, | |
817 | bfd_boolean *type_change_ok, | |
0f8a2703 | 818 | bfd_boolean *size_change_ok) |
252b5132 | 819 | { |
7479dfd4 | 820 | asection *sec, *oldsec; |
45d6a902 AM |
821 | struct elf_link_hash_entry *h; |
822 | struct elf_link_hash_entry *flip; | |
823 | int bind; | |
824 | bfd *oldbfd; | |
825 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
77cfaee6 | 826 | bfd_boolean newweak, oldweak; |
a4d8e49b | 827 | const struct elf_backend_data *bed; |
45d6a902 AM |
828 | |
829 | *skip = FALSE; | |
830 | *override = FALSE; | |
831 | ||
832 | sec = *psec; | |
833 | bind = ELF_ST_BIND (sym->st_info); | |
834 | ||
cd7be95b KH |
835 | /* Silently discard TLS symbols from --just-syms. There's no way to |
836 | combine a static TLS block with a new TLS block for this executable. */ | |
837 | if (ELF_ST_TYPE (sym->st_info) == STT_TLS | |
838 | && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
839 | { | |
840 | *skip = TRUE; | |
841 | return TRUE; | |
842 | } | |
843 | ||
45d6a902 AM |
844 | if (! bfd_is_und_section (sec)) |
845 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
846 | else | |
847 | h = ((struct elf_link_hash_entry *) | |
848 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
849 | if (h == NULL) | |
850 | return FALSE; | |
851 | *sym_hash = h; | |
252b5132 | 852 | |
45d6a902 AM |
853 | /* This code is for coping with dynamic objects, and is only useful |
854 | if we are doing an ELF link. */ | |
855 | if (info->hash->creator != abfd->xvec) | |
856 | return TRUE; | |
252b5132 | 857 | |
45d6a902 AM |
858 | /* For merging, we only care about real symbols. */ |
859 | ||
860 | while (h->root.type == bfd_link_hash_indirect | |
861 | || h->root.type == bfd_link_hash_warning) | |
862 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
863 | ||
864 | /* If we just created the symbol, mark it as being an ELF symbol. | |
865 | Other than that, there is nothing to do--there is no merge issue | |
866 | with a newly defined symbol--so we just return. */ | |
867 | ||
868 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 869 | { |
55255dae | 870 | bfd_elf_link_mark_dynamic_symbol (info, h); |
f5385ebf | 871 | h->non_elf = 0; |
45d6a902 AM |
872 | return TRUE; |
873 | } | |
252b5132 | 874 | |
7479dfd4 L |
875 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
876 | existing symbol. */ | |
252b5132 | 877 | |
45d6a902 AM |
878 | switch (h->root.type) |
879 | { | |
880 | default: | |
881 | oldbfd = NULL; | |
7479dfd4 | 882 | oldsec = NULL; |
45d6a902 | 883 | break; |
252b5132 | 884 | |
45d6a902 AM |
885 | case bfd_link_hash_undefined: |
886 | case bfd_link_hash_undefweak: | |
887 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 888 | oldsec = NULL; |
45d6a902 AM |
889 | break; |
890 | ||
891 | case bfd_link_hash_defined: | |
892 | case bfd_link_hash_defweak: | |
893 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 894 | oldsec = h->root.u.def.section; |
45d6a902 AM |
895 | break; |
896 | ||
897 | case bfd_link_hash_common: | |
898 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 899 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
900 | break; |
901 | } | |
902 | ||
903 | /* In cases involving weak versioned symbols, we may wind up trying | |
904 | to merge a symbol with itself. Catch that here, to avoid the | |
905 | confusion that results if we try to override a symbol with | |
906 | itself. The additional tests catch cases like | |
907 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
908 | dynamic object, which we do want to handle here. */ | |
909 | if (abfd == oldbfd | |
910 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 911 | || !h->def_regular)) |
45d6a902 AM |
912 | return TRUE; |
913 | ||
914 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
915 | respectively, is from a dynamic object. */ | |
916 | ||
707bba77 | 917 | newdyn = (abfd->flags & DYNAMIC) != 0; |
45d6a902 | 918 | |
707bba77 | 919 | olddyn = FALSE; |
45d6a902 AM |
920 | if (oldbfd != NULL) |
921 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 922 | else if (oldsec != NULL) |
45d6a902 | 923 | { |
707bba77 | 924 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 925 | indices used by MIPS ELF. */ |
707bba77 | 926 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 927 | } |
252b5132 | 928 | |
45d6a902 AM |
929 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
930 | respectively, appear to be a definition rather than reference. */ | |
931 | ||
707bba77 | 932 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 933 | |
707bba77 AM |
934 | olddef = (h->root.type != bfd_link_hash_undefined |
935 | && h->root.type != bfd_link_hash_undefweak | |
936 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 937 | |
580a2b6e L |
938 | /* When we try to create a default indirect symbol from the dynamic |
939 | definition with the default version, we skip it if its type and | |
940 | the type of existing regular definition mismatch. We only do it | |
941 | if the existing regular definition won't be dynamic. */ | |
942 | if (pold_alignment == NULL | |
943 | && !info->shared | |
944 | && !info->export_dynamic | |
945 | && !h->ref_dynamic | |
946 | && newdyn | |
947 | && newdef | |
948 | && !olddyn | |
949 | && (olddef || h->root.type == bfd_link_hash_common) | |
950 | && ELF_ST_TYPE (sym->st_info) != h->type | |
951 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
952 | && h->type != STT_NOTYPE) | |
953 | { | |
954 | *skip = TRUE; | |
955 | return TRUE; | |
956 | } | |
957 | ||
68f49ba3 L |
958 | /* Check TLS symbol. We don't check undefined symbol introduced by |
959 | "ld -u". */ | |
7479dfd4 | 960 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) |
68f49ba3 L |
961 | && ELF_ST_TYPE (sym->st_info) != h->type |
962 | && oldbfd != NULL) | |
7479dfd4 L |
963 | { |
964 | bfd *ntbfd, *tbfd; | |
965 | bfd_boolean ntdef, tdef; | |
966 | asection *ntsec, *tsec; | |
967 | ||
968 | if (h->type == STT_TLS) | |
969 | { | |
3b36f7e6 | 970 | ntbfd = abfd; |
7479dfd4 L |
971 | ntsec = sec; |
972 | ntdef = newdef; | |
973 | tbfd = oldbfd; | |
974 | tsec = oldsec; | |
975 | tdef = olddef; | |
976 | } | |
977 | else | |
978 | { | |
979 | ntbfd = oldbfd; | |
980 | ntsec = oldsec; | |
981 | ntdef = olddef; | |
982 | tbfd = abfd; | |
983 | tsec = sec; | |
984 | tdef = newdef; | |
985 | } | |
986 | ||
987 | if (tdef && ntdef) | |
988 | (*_bfd_error_handler) | |
989 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
990 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
991 | else if (!tdef && !ntdef) | |
992 | (*_bfd_error_handler) | |
993 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
994 | tbfd, ntbfd, h->root.root.string); | |
995 | else if (tdef) | |
996 | (*_bfd_error_handler) | |
997 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
998 | tbfd, tsec, ntbfd, h->root.root.string); | |
999 | else | |
1000 | (*_bfd_error_handler) | |
1001 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
1002 | tbfd, ntbfd, ntsec, h->root.root.string); | |
1003 | ||
1004 | bfd_set_error (bfd_error_bad_value); | |
1005 | return FALSE; | |
1006 | } | |
1007 | ||
4cc11e76 | 1008 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
1009 | object or is weak in all dynamic objects. Internal and hidden |
1010 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 1011 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
1012 | { |
1013 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 1014 | h->dynamic_def = 1; |
45d6a902 | 1015 | else |
252b5132 | 1016 | { |
45d6a902 AM |
1017 | /* Check if this symbol is weak in all dynamic objects. If it |
1018 | is the first time we see it in a dynamic object, we mark | |
1019 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 1020 | if (!h->ref_dynamic) |
79349b09 | 1021 | { |
45d6a902 | 1022 | if (bind == STB_WEAK) |
f5385ebf | 1023 | h->dynamic_weak = 1; |
252b5132 | 1024 | } |
45d6a902 | 1025 | else if (bind != STB_WEAK) |
f5385ebf | 1026 | h->dynamic_weak = 0; |
252b5132 | 1027 | } |
45d6a902 | 1028 | } |
252b5132 | 1029 | |
45d6a902 AM |
1030 | /* If the old symbol has non-default visibility, we ignore the new |
1031 | definition from a dynamic object. */ | |
1032 | if (newdyn | |
9c7a29a3 | 1033 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1034 | && !bfd_is_und_section (sec)) |
1035 | { | |
1036 | *skip = TRUE; | |
1037 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1038 | h->ref_dynamic = 1; |
45d6a902 AM |
1039 | /* A protected symbol has external availability. Make sure it is |
1040 | recorded as dynamic. | |
1041 | ||
1042 | FIXME: Should we check type and size for protected symbol? */ | |
1043 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1044 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1045 | else |
1046 | return TRUE; | |
1047 | } | |
1048 | else if (!newdyn | |
9c7a29a3 | 1049 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1050 | && h->def_dynamic) |
45d6a902 AM |
1051 | { |
1052 | /* If the new symbol with non-default visibility comes from a | |
1053 | relocatable file and the old definition comes from a dynamic | |
1054 | object, we remove the old definition. */ | |
1055 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
d2dee3b2 L |
1056 | { |
1057 | /* Handle the case where the old dynamic definition is | |
1058 | default versioned. We need to copy the symbol info from | |
1059 | the symbol with default version to the normal one if it | |
1060 | was referenced before. */ | |
1061 | if (h->ref_regular) | |
1062 | { | |
1063 | const struct elf_backend_data *bed | |
1064 | = get_elf_backend_data (abfd); | |
1065 | struct elf_link_hash_entry *vh = *sym_hash; | |
1066 | vh->root.type = h->root.type; | |
1067 | h->root.type = bfd_link_hash_indirect; | |
1068 | (*bed->elf_backend_copy_indirect_symbol) (info, vh, h); | |
1069 | /* Protected symbols will override the dynamic definition | |
1070 | with default version. */ | |
1071 | if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED) | |
1072 | { | |
1073 | h->root.u.i.link = (struct bfd_link_hash_entry *) vh; | |
1074 | vh->dynamic_def = 1; | |
1075 | vh->ref_dynamic = 1; | |
1076 | } | |
1077 | else | |
1078 | { | |
1079 | h->root.type = vh->root.type; | |
1080 | vh->ref_dynamic = 0; | |
1081 | /* We have to hide it here since it was made dynamic | |
1082 | global with extra bits when the symbol info was | |
1083 | copied from the old dynamic definition. */ | |
1084 | (*bed->elf_backend_hide_symbol) (info, vh, TRUE); | |
1085 | } | |
1086 | h = vh; | |
1087 | } | |
1088 | else | |
1089 | h = *sym_hash; | |
1090 | } | |
1de1a317 | 1091 | |
f6e332e6 | 1092 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1093 | && bfd_is_und_section (sec)) |
1094 | { | |
1095 | /* If the new symbol is undefined and the old symbol was | |
1096 | also undefined before, we need to make sure | |
1097 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1098 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1099 | definition came from a dynamic object, it is still on the |
1100 | undefs list. */ | |
1101 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1102 | h->root.u.undef.abfd = abfd; |
1103 | } | |
1104 | else | |
1105 | { | |
1106 | h->root.type = bfd_link_hash_new; | |
1107 | h->root.u.undef.abfd = NULL; | |
1108 | } | |
1109 | ||
f5385ebf | 1110 | if (h->def_dynamic) |
252b5132 | 1111 | { |
f5385ebf AM |
1112 | h->def_dynamic = 0; |
1113 | h->ref_dynamic = 1; | |
1114 | h->dynamic_def = 1; | |
45d6a902 AM |
1115 | } |
1116 | /* FIXME: Should we check type and size for protected symbol? */ | |
1117 | h->size = 0; | |
1118 | h->type = 0; | |
1119 | return TRUE; | |
1120 | } | |
14a793b2 | 1121 | |
79349b09 AM |
1122 | /* Differentiate strong and weak symbols. */ |
1123 | newweak = bind == STB_WEAK; | |
1124 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1125 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1126 | |
15b43f48 AM |
1127 | /* If a new weak symbol definition comes from a regular file and the |
1128 | old symbol comes from a dynamic library, we treat the new one as | |
1129 | strong. Similarly, an old weak symbol definition from a regular | |
1130 | file is treated as strong when the new symbol comes from a dynamic | |
1131 | library. Further, an old weak symbol from a dynamic library is | |
1132 | treated as strong if the new symbol is from a dynamic library. | |
1133 | This reflects the way glibc's ld.so works. | |
1134 | ||
1135 | Do this before setting *type_change_ok or *size_change_ok so that | |
1136 | we warn properly when dynamic library symbols are overridden. */ | |
1137 | ||
1138 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1139 | newweak = FALSE; |
15b43f48 | 1140 | if (olddef && newdyn) |
0f8a2703 AM |
1141 | oldweak = FALSE; |
1142 | ||
79349b09 AM |
1143 | /* It's OK to change the type if either the existing symbol or the |
1144 | new symbol is weak. A type change is also OK if the old symbol | |
1145 | is undefined and the new symbol is defined. */ | |
252b5132 | 1146 | |
79349b09 AM |
1147 | if (oldweak |
1148 | || newweak | |
1149 | || (newdef | |
1150 | && h->root.type == bfd_link_hash_undefined)) | |
1151 | *type_change_ok = TRUE; | |
1152 | ||
1153 | /* It's OK to change the size if either the existing symbol or the | |
1154 | new symbol is weak, or if the old symbol is undefined. */ | |
1155 | ||
1156 | if (*type_change_ok | |
1157 | || h->root.type == bfd_link_hash_undefined) | |
1158 | *size_change_ok = TRUE; | |
45d6a902 | 1159 | |
45d6a902 AM |
1160 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1161 | symbol, respectively, appears to be a common symbol in a dynamic | |
1162 | object. If a symbol appears in an uninitialized section, and is | |
1163 | not weak, and is not a function, then it may be a common symbol | |
1164 | which was resolved when the dynamic object was created. We want | |
1165 | to treat such symbols specially, because they raise special | |
1166 | considerations when setting the symbol size: if the symbol | |
1167 | appears as a common symbol in a regular object, and the size in | |
1168 | the regular object is larger, we must make sure that we use the | |
1169 | larger size. This problematic case can always be avoided in C, | |
1170 | but it must be handled correctly when using Fortran shared | |
1171 | libraries. | |
1172 | ||
1173 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1174 | likewise for OLDDYNCOMMON and OLDDEF. | |
1175 | ||
1176 | Note that this test is just a heuristic, and that it is quite | |
1177 | possible to have an uninitialized symbol in a shared object which | |
1178 | is really a definition, rather than a common symbol. This could | |
1179 | lead to some minor confusion when the symbol really is a common | |
1180 | symbol in some regular object. However, I think it will be | |
1181 | harmless. */ | |
1182 | ||
1183 | if (newdyn | |
1184 | && newdef | |
79349b09 | 1185 | && !newweak |
45d6a902 AM |
1186 | && (sec->flags & SEC_ALLOC) != 0 |
1187 | && (sec->flags & SEC_LOAD) == 0 | |
1188 | && sym->st_size > 0 | |
45d6a902 AM |
1189 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
1190 | newdyncommon = TRUE; | |
1191 | else | |
1192 | newdyncommon = FALSE; | |
1193 | ||
1194 | if (olddyn | |
1195 | && olddef | |
1196 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1197 | && h->def_dynamic |
45d6a902 AM |
1198 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1199 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1200 | && h->size > 0 | |
1201 | && h->type != STT_FUNC) | |
1202 | olddyncommon = TRUE; | |
1203 | else | |
1204 | olddyncommon = FALSE; | |
1205 | ||
a4d8e49b L |
1206 | /* We now know everything about the old and new symbols. We ask the |
1207 | backend to check if we can merge them. */ | |
1208 | bed = get_elf_backend_data (abfd); | |
1209 | if (bed->merge_symbol | |
1210 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1211 | pold_alignment, skip, override, | |
1212 | type_change_ok, size_change_ok, | |
1213 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1214 | abfd, &sec, | |
1215 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1216 | oldbfd, &oldsec)) | |
1217 | return FALSE; | |
1218 | ||
45d6a902 AM |
1219 | /* If both the old and the new symbols look like common symbols in a |
1220 | dynamic object, set the size of the symbol to the larger of the | |
1221 | two. */ | |
1222 | ||
1223 | if (olddyncommon | |
1224 | && newdyncommon | |
1225 | && sym->st_size != h->size) | |
1226 | { | |
1227 | /* Since we think we have two common symbols, issue a multiple | |
1228 | common warning if desired. Note that we only warn if the | |
1229 | size is different. If the size is the same, we simply let | |
1230 | the old symbol override the new one as normally happens with | |
1231 | symbols defined in dynamic objects. */ | |
1232 | ||
1233 | if (! ((*info->callbacks->multiple_common) | |
1234 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1235 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1236 | return FALSE; | |
252b5132 | 1237 | |
45d6a902 AM |
1238 | if (sym->st_size > h->size) |
1239 | h->size = sym->st_size; | |
252b5132 | 1240 | |
45d6a902 | 1241 | *size_change_ok = TRUE; |
252b5132 RH |
1242 | } |
1243 | ||
45d6a902 AM |
1244 | /* If we are looking at a dynamic object, and we have found a |
1245 | definition, we need to see if the symbol was already defined by | |
1246 | some other object. If so, we want to use the existing | |
1247 | definition, and we do not want to report a multiple symbol | |
1248 | definition error; we do this by clobbering *PSEC to be | |
1249 | bfd_und_section_ptr. | |
1250 | ||
1251 | We treat a common symbol as a definition if the symbol in the | |
1252 | shared library is a function, since common symbols always | |
1253 | represent variables; this can cause confusion in principle, but | |
1254 | any such confusion would seem to indicate an erroneous program or | |
1255 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1256 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1257 | |
1258 | if (newdyn | |
1259 | && newdef | |
77cfaee6 | 1260 | && (olddef |
45d6a902 | 1261 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1262 | && (newweak |
0f8a2703 | 1263 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1264 | { |
1265 | *override = TRUE; | |
1266 | newdef = FALSE; | |
1267 | newdyncommon = FALSE; | |
252b5132 | 1268 | |
45d6a902 AM |
1269 | *psec = sec = bfd_und_section_ptr; |
1270 | *size_change_ok = TRUE; | |
252b5132 | 1271 | |
45d6a902 AM |
1272 | /* If we get here when the old symbol is a common symbol, then |
1273 | we are explicitly letting it override a weak symbol or | |
1274 | function in a dynamic object, and we don't want to warn about | |
1275 | a type change. If the old symbol is a defined symbol, a type | |
1276 | change warning may still be appropriate. */ | |
252b5132 | 1277 | |
45d6a902 AM |
1278 | if (h->root.type == bfd_link_hash_common) |
1279 | *type_change_ok = TRUE; | |
1280 | } | |
1281 | ||
1282 | /* Handle the special case of an old common symbol merging with a | |
1283 | new symbol which looks like a common symbol in a shared object. | |
1284 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1285 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1286 | right thing. */ | |
45d6a902 AM |
1287 | |
1288 | if (newdyncommon | |
1289 | && h->root.type == bfd_link_hash_common) | |
1290 | { | |
1291 | *override = TRUE; | |
1292 | newdef = FALSE; | |
1293 | newdyncommon = FALSE; | |
1294 | *pvalue = sym->st_size; | |
a4d8e49b | 1295 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1296 | *size_change_ok = TRUE; |
1297 | } | |
1298 | ||
c5e2cead | 1299 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1300 | if (newdef && olddef && newweak) |
c5e2cead L |
1301 | *skip = TRUE; |
1302 | ||
45d6a902 AM |
1303 | /* If the old symbol is from a dynamic object, and the new symbol is |
1304 | a definition which is not from a dynamic object, then the new | |
1305 | symbol overrides the old symbol. Symbols from regular files | |
1306 | always take precedence over symbols from dynamic objects, even if | |
1307 | they are defined after the dynamic object in the link. | |
1308 | ||
1309 | As above, we again permit a common symbol in a regular object to | |
1310 | override a definition in a shared object if the shared object | |
0f8a2703 | 1311 | symbol is a function or is weak. */ |
45d6a902 AM |
1312 | |
1313 | flip = NULL; | |
77cfaee6 | 1314 | if (!newdyn |
45d6a902 AM |
1315 | && (newdef |
1316 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1317 | && (oldweak |
1318 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1319 | && olddyn |
1320 | && olddef | |
f5385ebf | 1321 | && h->def_dynamic) |
45d6a902 AM |
1322 | { |
1323 | /* Change the hash table entry to undefined, and let | |
1324 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1325 | new definition. */ | |
1326 | ||
1327 | h->root.type = bfd_link_hash_undefined; | |
1328 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1329 | *size_change_ok = TRUE; | |
1330 | ||
1331 | olddef = FALSE; | |
1332 | olddyncommon = FALSE; | |
1333 | ||
1334 | /* We again permit a type change when a common symbol may be | |
1335 | overriding a function. */ | |
1336 | ||
1337 | if (bfd_is_com_section (sec)) | |
1338 | *type_change_ok = TRUE; | |
1339 | ||
1340 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1341 | flip = *sym_hash; | |
1342 | else | |
1343 | /* This union may have been set to be non-NULL when this symbol | |
1344 | was seen in a dynamic object. We must force the union to be | |
1345 | NULL, so that it is correct for a regular symbol. */ | |
1346 | h->verinfo.vertree = NULL; | |
1347 | } | |
1348 | ||
1349 | /* Handle the special case of a new common symbol merging with an | |
1350 | old symbol that looks like it might be a common symbol defined in | |
1351 | a shared object. Note that we have already handled the case in | |
1352 | which a new common symbol should simply override the definition | |
1353 | in the shared library. */ | |
1354 | ||
1355 | if (! newdyn | |
1356 | && bfd_is_com_section (sec) | |
1357 | && olddyncommon) | |
1358 | { | |
1359 | /* It would be best if we could set the hash table entry to a | |
1360 | common symbol, but we don't know what to use for the section | |
1361 | or the alignment. */ | |
1362 | if (! ((*info->callbacks->multiple_common) | |
1363 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1364 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1365 | return FALSE; | |
1366 | ||
4cc11e76 | 1367 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1368 | larger, pretend that the new symbol has its size. */ |
1369 | ||
1370 | if (h->size > *pvalue) | |
1371 | *pvalue = h->size; | |
1372 | ||
af44c138 L |
1373 | /* We need to remember the alignment required by the symbol |
1374 | in the dynamic object. */ | |
1375 | BFD_ASSERT (pold_alignment); | |
1376 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1377 | |
1378 | olddef = FALSE; | |
1379 | olddyncommon = FALSE; | |
1380 | ||
1381 | h->root.type = bfd_link_hash_undefined; | |
1382 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1383 | ||
1384 | *size_change_ok = TRUE; | |
1385 | *type_change_ok = TRUE; | |
1386 | ||
1387 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1388 | flip = *sym_hash; | |
1389 | else | |
1390 | h->verinfo.vertree = NULL; | |
1391 | } | |
1392 | ||
1393 | if (flip != NULL) | |
1394 | { | |
1395 | /* Handle the case where we had a versioned symbol in a dynamic | |
1396 | library and now find a definition in a normal object. In this | |
1397 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1398 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1399 | flip->root.type = h->root.type; |
1400 | h->root.type = bfd_link_hash_indirect; | |
1401 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1402 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
45d6a902 | 1403 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
f5385ebf | 1404 | if (h->def_dynamic) |
45d6a902 | 1405 | { |
f5385ebf AM |
1406 | h->def_dynamic = 0; |
1407 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1408 | } |
1409 | } | |
1410 | ||
45d6a902 AM |
1411 | return TRUE; |
1412 | } | |
1413 | ||
1414 | /* This function is called to create an indirect symbol from the | |
1415 | default for the symbol with the default version if needed. The | |
1416 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1417 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1418 | |
1419 | bfd_boolean | |
268b6b39 AM |
1420 | _bfd_elf_add_default_symbol (bfd *abfd, |
1421 | struct bfd_link_info *info, | |
1422 | struct elf_link_hash_entry *h, | |
1423 | const char *name, | |
1424 | Elf_Internal_Sym *sym, | |
1425 | asection **psec, | |
1426 | bfd_vma *value, | |
1427 | bfd_boolean *dynsym, | |
0f8a2703 | 1428 | bfd_boolean override) |
45d6a902 AM |
1429 | { |
1430 | bfd_boolean type_change_ok; | |
1431 | bfd_boolean size_change_ok; | |
1432 | bfd_boolean skip; | |
1433 | char *shortname; | |
1434 | struct elf_link_hash_entry *hi; | |
1435 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1436 | const struct elf_backend_data *bed; |
45d6a902 AM |
1437 | bfd_boolean collect; |
1438 | bfd_boolean dynamic; | |
1439 | char *p; | |
1440 | size_t len, shortlen; | |
1441 | asection *sec; | |
1442 | ||
1443 | /* If this symbol has a version, and it is the default version, we | |
1444 | create an indirect symbol from the default name to the fully | |
1445 | decorated name. This will cause external references which do not | |
1446 | specify a version to be bound to this version of the symbol. */ | |
1447 | p = strchr (name, ELF_VER_CHR); | |
1448 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1449 | return TRUE; | |
1450 | ||
1451 | if (override) | |
1452 | { | |
4cc11e76 | 1453 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1454 | need to create the indirect symbol from the default name. */ |
1455 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1456 | FALSE, FALSE); | |
1457 | BFD_ASSERT (hi != NULL); | |
1458 | if (hi == h) | |
1459 | return TRUE; | |
1460 | while (hi->root.type == bfd_link_hash_indirect | |
1461 | || hi->root.type == bfd_link_hash_warning) | |
1462 | { | |
1463 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1464 | if (hi == h) | |
1465 | return TRUE; | |
1466 | } | |
1467 | } | |
1468 | ||
1469 | bed = get_elf_backend_data (abfd); | |
1470 | collect = bed->collect; | |
1471 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1472 | ||
1473 | shortlen = p - name; | |
1474 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1475 | if (shortname == NULL) | |
1476 | return FALSE; | |
1477 | memcpy (shortname, name, shortlen); | |
1478 | shortname[shortlen] = '\0'; | |
1479 | ||
1480 | /* We are going to create a new symbol. Merge it with any existing | |
1481 | symbol with this name. For the purposes of the merge, act as | |
1482 | though we were defining the symbol we just defined, although we | |
1483 | actually going to define an indirect symbol. */ | |
1484 | type_change_ok = FALSE; | |
1485 | size_change_ok = FALSE; | |
1486 | sec = *psec; | |
1487 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1488 | NULL, &hi, &skip, &override, |
1489 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1490 | return FALSE; |
1491 | ||
1492 | if (skip) | |
1493 | goto nondefault; | |
1494 | ||
1495 | if (! override) | |
1496 | { | |
1497 | bh = &hi->root; | |
1498 | if (! (_bfd_generic_link_add_one_symbol | |
1499 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1500 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1501 | return FALSE; |
1502 | hi = (struct elf_link_hash_entry *) bh; | |
1503 | } | |
1504 | else | |
1505 | { | |
1506 | /* In this case the symbol named SHORTNAME is overriding the | |
1507 | indirect symbol we want to add. We were planning on making | |
1508 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1509 | is the name without a version. NAME is the fully versioned | |
1510 | name, and it is the default version. | |
1511 | ||
1512 | Overriding means that we already saw a definition for the | |
1513 | symbol SHORTNAME in a regular object, and it is overriding | |
1514 | the symbol defined in the dynamic object. | |
1515 | ||
1516 | When this happens, we actually want to change NAME, the | |
1517 | symbol we just added, to refer to SHORTNAME. This will cause | |
1518 | references to NAME in the shared object to become references | |
1519 | to SHORTNAME in the regular object. This is what we expect | |
1520 | when we override a function in a shared object: that the | |
1521 | references in the shared object will be mapped to the | |
1522 | definition in the regular object. */ | |
1523 | ||
1524 | while (hi->root.type == bfd_link_hash_indirect | |
1525 | || hi->root.type == bfd_link_hash_warning) | |
1526 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1527 | ||
1528 | h->root.type = bfd_link_hash_indirect; | |
1529 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1530 | if (h->def_dynamic) |
45d6a902 | 1531 | { |
f5385ebf AM |
1532 | h->def_dynamic = 0; |
1533 | hi->ref_dynamic = 1; | |
1534 | if (hi->ref_regular | |
1535 | || hi->def_regular) | |
45d6a902 | 1536 | { |
c152c796 | 1537 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1538 | return FALSE; |
1539 | } | |
1540 | } | |
1541 | ||
1542 | /* Now set HI to H, so that the following code will set the | |
1543 | other fields correctly. */ | |
1544 | hi = h; | |
1545 | } | |
1546 | ||
1547 | /* If there is a duplicate definition somewhere, then HI may not | |
1548 | point to an indirect symbol. We will have reported an error to | |
1549 | the user in that case. */ | |
1550 | ||
1551 | if (hi->root.type == bfd_link_hash_indirect) | |
1552 | { | |
1553 | struct elf_link_hash_entry *ht; | |
1554 | ||
45d6a902 | 1555 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1556 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 AM |
1557 | |
1558 | /* See if the new flags lead us to realize that the symbol must | |
1559 | be dynamic. */ | |
1560 | if (! *dynsym) | |
1561 | { | |
1562 | if (! dynamic) | |
1563 | { | |
1564 | if (info->shared | |
f5385ebf | 1565 | || hi->ref_dynamic) |
45d6a902 AM |
1566 | *dynsym = TRUE; |
1567 | } | |
1568 | else | |
1569 | { | |
f5385ebf | 1570 | if (hi->ref_regular) |
45d6a902 AM |
1571 | *dynsym = TRUE; |
1572 | } | |
1573 | } | |
1574 | } | |
1575 | ||
1576 | /* We also need to define an indirection from the nondefault version | |
1577 | of the symbol. */ | |
1578 | ||
1579 | nondefault: | |
1580 | len = strlen (name); | |
1581 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1582 | if (shortname == NULL) | |
1583 | return FALSE; | |
1584 | memcpy (shortname, name, shortlen); | |
1585 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1586 | ||
1587 | /* Once again, merge with any existing symbol. */ | |
1588 | type_change_ok = FALSE; | |
1589 | size_change_ok = FALSE; | |
1590 | sec = *psec; | |
1591 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1592 | NULL, &hi, &skip, &override, |
1593 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1594 | return FALSE; |
1595 | ||
1596 | if (skip) | |
1597 | return TRUE; | |
1598 | ||
1599 | if (override) | |
1600 | { | |
1601 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1602 | the type of override we do in the case above unless it is | |
4cc11e76 | 1603 | overridden by a versioned definition. */ |
45d6a902 AM |
1604 | if (hi->root.type != bfd_link_hash_defined |
1605 | && hi->root.type != bfd_link_hash_defweak) | |
1606 | (*_bfd_error_handler) | |
d003868e AM |
1607 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1608 | abfd, shortname); | |
45d6a902 AM |
1609 | } |
1610 | else | |
1611 | { | |
1612 | bh = &hi->root; | |
1613 | if (! (_bfd_generic_link_add_one_symbol | |
1614 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1615 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1616 | return FALSE; |
1617 | hi = (struct elf_link_hash_entry *) bh; | |
1618 | ||
1619 | /* If there is a duplicate definition somewhere, then HI may not | |
1620 | point to an indirect symbol. We will have reported an error | |
1621 | to the user in that case. */ | |
1622 | ||
1623 | if (hi->root.type == bfd_link_hash_indirect) | |
1624 | { | |
fcfa13d2 | 1625 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
45d6a902 AM |
1626 | |
1627 | /* See if the new flags lead us to realize that the symbol | |
1628 | must be dynamic. */ | |
1629 | if (! *dynsym) | |
1630 | { | |
1631 | if (! dynamic) | |
1632 | { | |
1633 | if (info->shared | |
f5385ebf | 1634 | || hi->ref_dynamic) |
45d6a902 AM |
1635 | *dynsym = TRUE; |
1636 | } | |
1637 | else | |
1638 | { | |
f5385ebf | 1639 | if (hi->ref_regular) |
45d6a902 AM |
1640 | *dynsym = TRUE; |
1641 | } | |
1642 | } | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | return TRUE; | |
1647 | } | |
1648 | \f | |
1649 | /* This routine is used to export all defined symbols into the dynamic | |
1650 | symbol table. It is called via elf_link_hash_traverse. */ | |
1651 | ||
1652 | bfd_boolean | |
268b6b39 | 1653 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1654 | { |
268b6b39 | 1655 | struct elf_info_failed *eif = data; |
45d6a902 | 1656 | |
55255dae L |
1657 | /* Ignore this if we won't export it. */ |
1658 | if (!eif->info->export_dynamic && !h->dynamic) | |
1659 | return TRUE; | |
1660 | ||
45d6a902 AM |
1661 | /* Ignore indirect symbols. These are added by the versioning code. */ |
1662 | if (h->root.type == bfd_link_hash_indirect) | |
1663 | return TRUE; | |
1664 | ||
1665 | if (h->root.type == bfd_link_hash_warning) | |
1666 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1667 | ||
1668 | if (h->dynindx == -1 | |
f5385ebf AM |
1669 | && (h->def_regular |
1670 | || h->ref_regular)) | |
45d6a902 AM |
1671 | { |
1672 | struct bfd_elf_version_tree *t; | |
1673 | struct bfd_elf_version_expr *d; | |
1674 | ||
1675 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1676 | { | |
108ba305 | 1677 | if (t->globals.list != NULL) |
45d6a902 | 1678 | { |
108ba305 JJ |
1679 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1680 | if (d != NULL) | |
1681 | goto doit; | |
45d6a902 AM |
1682 | } |
1683 | ||
108ba305 | 1684 | if (t->locals.list != NULL) |
45d6a902 | 1685 | { |
108ba305 JJ |
1686 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1687 | if (d != NULL) | |
1688 | return TRUE; | |
45d6a902 AM |
1689 | } |
1690 | } | |
1691 | ||
1692 | if (!eif->verdefs) | |
1693 | { | |
1694 | doit: | |
c152c796 | 1695 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1696 | { |
1697 | eif->failed = TRUE; | |
1698 | return FALSE; | |
1699 | } | |
1700 | } | |
1701 | } | |
1702 | ||
1703 | return TRUE; | |
1704 | } | |
1705 | \f | |
1706 | /* Look through the symbols which are defined in other shared | |
1707 | libraries and referenced here. Update the list of version | |
1708 | dependencies. This will be put into the .gnu.version_r section. | |
1709 | This function is called via elf_link_hash_traverse. */ | |
1710 | ||
1711 | bfd_boolean | |
268b6b39 AM |
1712 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1713 | void *data) | |
45d6a902 | 1714 | { |
268b6b39 | 1715 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1716 | Elf_Internal_Verneed *t; |
1717 | Elf_Internal_Vernaux *a; | |
1718 | bfd_size_type amt; | |
1719 | ||
1720 | if (h->root.type == bfd_link_hash_warning) | |
1721 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1722 | ||
1723 | /* We only care about symbols defined in shared objects with version | |
1724 | information. */ | |
f5385ebf AM |
1725 | if (!h->def_dynamic |
1726 | || h->def_regular | |
45d6a902 AM |
1727 | || h->dynindx == -1 |
1728 | || h->verinfo.verdef == NULL) | |
1729 | return TRUE; | |
1730 | ||
1731 | /* See if we already know about this version. */ | |
1732 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1733 | { | |
1734 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1735 | continue; | |
1736 | ||
1737 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1738 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1739 | return TRUE; | |
1740 | ||
1741 | break; | |
1742 | } | |
1743 | ||
1744 | /* This is a new version. Add it to tree we are building. */ | |
1745 | ||
1746 | if (t == NULL) | |
1747 | { | |
1748 | amt = sizeof *t; | |
268b6b39 | 1749 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1750 | if (t == NULL) |
1751 | { | |
1752 | rinfo->failed = TRUE; | |
1753 | return FALSE; | |
1754 | } | |
1755 | ||
1756 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1757 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1758 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1759 | } | |
1760 | ||
1761 | amt = sizeof *a; | |
268b6b39 | 1762 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1763 | |
1764 | /* Note that we are copying a string pointer here, and testing it | |
1765 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1766 | discard the string data when low in memory, this will have to be | |
1767 | fixed. */ | |
1768 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1769 | ||
1770 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1771 | a->vna_nextptr = t->vn_auxptr; | |
1772 | ||
1773 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1774 | ++rinfo->vers; | |
1775 | ||
1776 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1777 | ||
1778 | t->vn_auxptr = a; | |
1779 | ||
1780 | return TRUE; | |
1781 | } | |
1782 | ||
1783 | /* Figure out appropriate versions for all the symbols. We may not | |
1784 | have the version number script until we have read all of the input | |
1785 | files, so until that point we don't know which symbols should be | |
1786 | local. This function is called via elf_link_hash_traverse. */ | |
1787 | ||
1788 | bfd_boolean | |
268b6b39 | 1789 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1790 | { |
1791 | struct elf_assign_sym_version_info *sinfo; | |
1792 | struct bfd_link_info *info; | |
9c5bfbb7 | 1793 | const struct elf_backend_data *bed; |
45d6a902 AM |
1794 | struct elf_info_failed eif; |
1795 | char *p; | |
1796 | bfd_size_type amt; | |
1797 | ||
268b6b39 | 1798 | sinfo = data; |
45d6a902 AM |
1799 | info = sinfo->info; |
1800 | ||
1801 | if (h->root.type == bfd_link_hash_warning) | |
1802 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1803 | ||
1804 | /* Fix the symbol flags. */ | |
1805 | eif.failed = FALSE; | |
1806 | eif.info = info; | |
1807 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1808 | { | |
1809 | if (eif.failed) | |
1810 | sinfo->failed = TRUE; | |
1811 | return FALSE; | |
1812 | } | |
1813 | ||
1814 | /* We only need version numbers for symbols defined in regular | |
1815 | objects. */ | |
f5385ebf | 1816 | if (!h->def_regular) |
45d6a902 AM |
1817 | return TRUE; |
1818 | ||
1819 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1820 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1821 | if (p != NULL && h->verinfo.vertree == NULL) | |
1822 | { | |
1823 | struct bfd_elf_version_tree *t; | |
1824 | bfd_boolean hidden; | |
1825 | ||
1826 | hidden = TRUE; | |
1827 | ||
1828 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1829 | not a hidden symbol. */ | |
1830 | ++p; | |
1831 | if (*p == ELF_VER_CHR) | |
1832 | { | |
1833 | hidden = FALSE; | |
1834 | ++p; | |
1835 | } | |
1836 | ||
1837 | /* If there is no version string, we can just return out. */ | |
1838 | if (*p == '\0') | |
1839 | { | |
1840 | if (hidden) | |
f5385ebf | 1841 | h->hidden = 1; |
45d6a902 AM |
1842 | return TRUE; |
1843 | } | |
1844 | ||
1845 | /* Look for the version. If we find it, it is no longer weak. */ | |
1846 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1847 | { | |
1848 | if (strcmp (t->name, p) == 0) | |
1849 | { | |
1850 | size_t len; | |
1851 | char *alc; | |
1852 | struct bfd_elf_version_expr *d; | |
1853 | ||
1854 | len = p - h->root.root.string; | |
268b6b39 | 1855 | alc = bfd_malloc (len); |
45d6a902 AM |
1856 | if (alc == NULL) |
1857 | return FALSE; | |
1858 | memcpy (alc, h->root.root.string, len - 1); | |
1859 | alc[len - 1] = '\0'; | |
1860 | if (alc[len - 2] == ELF_VER_CHR) | |
1861 | alc[len - 2] = '\0'; | |
1862 | ||
1863 | h->verinfo.vertree = t; | |
1864 | t->used = TRUE; | |
1865 | d = NULL; | |
1866 | ||
108ba305 JJ |
1867 | if (t->globals.list != NULL) |
1868 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1869 | |
1870 | /* See if there is anything to force this symbol to | |
1871 | local scope. */ | |
108ba305 | 1872 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1873 | { |
108ba305 JJ |
1874 | d = (*t->match) (&t->locals, NULL, alc); |
1875 | if (d != NULL | |
1876 | && h->dynindx != -1 | |
108ba305 JJ |
1877 | && ! info->export_dynamic) |
1878 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1879 | } |
1880 | ||
1881 | free (alc); | |
1882 | break; | |
1883 | } | |
1884 | } | |
1885 | ||
1886 | /* If we are building an application, we need to create a | |
1887 | version node for this version. */ | |
36af4a4e | 1888 | if (t == NULL && info->executable) |
45d6a902 AM |
1889 | { |
1890 | struct bfd_elf_version_tree **pp; | |
1891 | int version_index; | |
1892 | ||
1893 | /* If we aren't going to export this symbol, we don't need | |
1894 | to worry about it. */ | |
1895 | if (h->dynindx == -1) | |
1896 | return TRUE; | |
1897 | ||
1898 | amt = sizeof *t; | |
108ba305 | 1899 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1900 | if (t == NULL) |
1901 | { | |
1902 | sinfo->failed = TRUE; | |
1903 | return FALSE; | |
1904 | } | |
1905 | ||
45d6a902 | 1906 | t->name = p; |
45d6a902 AM |
1907 | t->name_indx = (unsigned int) -1; |
1908 | t->used = TRUE; | |
1909 | ||
1910 | version_index = 1; | |
1911 | /* Don't count anonymous version tag. */ | |
1912 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1913 | version_index = 0; | |
1914 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1915 | ++version_index; | |
1916 | t->vernum = version_index; | |
1917 | ||
1918 | *pp = t; | |
1919 | ||
1920 | h->verinfo.vertree = t; | |
1921 | } | |
1922 | else if (t == NULL) | |
1923 | { | |
1924 | /* We could not find the version for a symbol when | |
1925 | generating a shared archive. Return an error. */ | |
1926 | (*_bfd_error_handler) | |
d003868e AM |
1927 | (_("%B: undefined versioned symbol name %s"), |
1928 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1929 | bfd_set_error (bfd_error_bad_value); |
1930 | sinfo->failed = TRUE; | |
1931 | return FALSE; | |
1932 | } | |
1933 | ||
1934 | if (hidden) | |
f5385ebf | 1935 | h->hidden = 1; |
45d6a902 AM |
1936 | } |
1937 | ||
1938 | /* If we don't have a version for this symbol, see if we can find | |
1939 | something. */ | |
1940 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1941 | { | |
1942 | struct bfd_elf_version_tree *t; | |
1943 | struct bfd_elf_version_tree *local_ver; | |
1944 | struct bfd_elf_version_expr *d; | |
1945 | ||
1946 | /* See if can find what version this symbol is in. If the | |
1947 | symbol is supposed to be local, then don't actually register | |
1948 | it. */ | |
1949 | local_ver = NULL; | |
1950 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1951 | { | |
108ba305 | 1952 | if (t->globals.list != NULL) |
45d6a902 AM |
1953 | { |
1954 | bfd_boolean matched; | |
1955 | ||
1956 | matched = FALSE; | |
108ba305 JJ |
1957 | d = NULL; |
1958 | while ((d = (*t->match) (&t->globals, d, | |
1959 | h->root.root.string)) != NULL) | |
1960 | if (d->symver) | |
1961 | matched = TRUE; | |
1962 | else | |
1963 | { | |
1964 | /* There is a version without definition. Make | |
1965 | the symbol the default definition for this | |
1966 | version. */ | |
1967 | h->verinfo.vertree = t; | |
1968 | local_ver = NULL; | |
1969 | d->script = 1; | |
1970 | break; | |
1971 | } | |
45d6a902 AM |
1972 | if (d != NULL) |
1973 | break; | |
1974 | else if (matched) | |
1975 | /* There is no undefined version for this symbol. Hide the | |
1976 | default one. */ | |
1977 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1978 | } | |
1979 | ||
108ba305 | 1980 | if (t->locals.list != NULL) |
45d6a902 | 1981 | { |
108ba305 JJ |
1982 | d = NULL; |
1983 | while ((d = (*t->match) (&t->locals, d, | |
1984 | h->root.root.string)) != NULL) | |
45d6a902 | 1985 | { |
108ba305 | 1986 | local_ver = t; |
45d6a902 | 1987 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1988 | explicit, perhaps even global, match. |
1989 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1990 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1991 | break; | |
45d6a902 AM |
1992 | } |
1993 | ||
1994 | if (d != NULL) | |
1995 | break; | |
1996 | } | |
1997 | } | |
1998 | ||
1999 | if (local_ver != NULL) | |
2000 | { | |
2001 | h->verinfo.vertree = local_ver; | |
2002 | if (h->dynindx != -1 | |
45d6a902 AM |
2003 | && ! info->export_dynamic) |
2004 | { | |
2005 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2006 | } | |
2007 | } | |
2008 | } | |
2009 | ||
2010 | return TRUE; | |
2011 | } | |
2012 | \f | |
45d6a902 AM |
2013 | /* Read and swap the relocs from the section indicated by SHDR. This |
2014 | may be either a REL or a RELA section. The relocations are | |
2015 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2016 | which should have already been allocated to contain enough space. | |
2017 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2018 | relocations should be stored. | |
2019 | ||
2020 | Returns FALSE if something goes wrong. */ | |
2021 | ||
2022 | static bfd_boolean | |
268b6b39 | 2023 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2024 | asection *sec, |
268b6b39 AM |
2025 | Elf_Internal_Shdr *shdr, |
2026 | void *external_relocs, | |
2027 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2028 | { |
9c5bfbb7 | 2029 | const struct elf_backend_data *bed; |
268b6b39 | 2030 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2031 | const bfd_byte *erela; |
2032 | const bfd_byte *erelaend; | |
2033 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2034 | Elf_Internal_Shdr *symtab_hdr; |
2035 | size_t nsyms; | |
45d6a902 | 2036 | |
45d6a902 AM |
2037 | /* Position ourselves at the start of the section. */ |
2038 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2039 | return FALSE; | |
2040 | ||
2041 | /* Read the relocations. */ | |
2042 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2043 | return FALSE; | |
2044 | ||
243ef1e0 L |
2045 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2046 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
2047 | ||
45d6a902 AM |
2048 | bed = get_elf_backend_data (abfd); |
2049 | ||
2050 | /* Convert the external relocations to the internal format. */ | |
2051 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2052 | swap_in = bed->s->swap_reloc_in; | |
2053 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2054 | swap_in = bed->s->swap_reloca_in; | |
2055 | else | |
2056 | { | |
2057 | bfd_set_error (bfd_error_wrong_format); | |
2058 | return FALSE; | |
2059 | } | |
2060 | ||
2061 | erela = external_relocs; | |
51992aec | 2062 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2063 | irela = internal_relocs; |
2064 | while (erela < erelaend) | |
2065 | { | |
243ef1e0 L |
2066 | bfd_vma r_symndx; |
2067 | ||
45d6a902 | 2068 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2069 | r_symndx = ELF32_R_SYM (irela->r_info); |
2070 | if (bed->s->arch_size == 64) | |
2071 | r_symndx >>= 24; | |
2072 | if ((size_t) r_symndx >= nsyms) | |
2073 | { | |
2074 | (*_bfd_error_handler) | |
d003868e AM |
2075 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
2076 | " for offset 0x%lx in section `%A'"), | |
2077 | abfd, sec, | |
2078 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2079 | bfd_set_error (bfd_error_bad_value); |
2080 | return FALSE; | |
2081 | } | |
45d6a902 AM |
2082 | irela += bed->s->int_rels_per_ext_rel; |
2083 | erela += shdr->sh_entsize; | |
2084 | } | |
2085 | ||
2086 | return TRUE; | |
2087 | } | |
2088 | ||
2089 | /* Read and swap the relocs for a section O. They may have been | |
2090 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2091 | not NULL, they are used as buffers to read into. They are known to | |
2092 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2093 | the return value is allocated using either malloc or bfd_alloc, | |
2094 | according to the KEEP_MEMORY argument. If O has two relocation | |
2095 | sections (both REL and RELA relocations), then the REL_HDR | |
2096 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
2097 | REL_HDR2 relocations. */ | |
2098 | ||
2099 | Elf_Internal_Rela * | |
268b6b39 AM |
2100 | _bfd_elf_link_read_relocs (bfd *abfd, |
2101 | asection *o, | |
2102 | void *external_relocs, | |
2103 | Elf_Internal_Rela *internal_relocs, | |
2104 | bfd_boolean keep_memory) | |
45d6a902 AM |
2105 | { |
2106 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2107 | void *alloc1 = NULL; |
45d6a902 | 2108 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2109 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2110 | |
2111 | if (elf_section_data (o)->relocs != NULL) | |
2112 | return elf_section_data (o)->relocs; | |
2113 | ||
2114 | if (o->reloc_count == 0) | |
2115 | return NULL; | |
2116 | ||
2117 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2118 | ||
2119 | if (internal_relocs == NULL) | |
2120 | { | |
2121 | bfd_size_type size; | |
2122 | ||
2123 | size = o->reloc_count; | |
2124 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2125 | if (keep_memory) | |
268b6b39 | 2126 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 2127 | else |
268b6b39 | 2128 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2129 | if (internal_relocs == NULL) |
2130 | goto error_return; | |
2131 | } | |
2132 | ||
2133 | if (external_relocs == NULL) | |
2134 | { | |
2135 | bfd_size_type size = rel_hdr->sh_size; | |
2136 | ||
2137 | if (elf_section_data (o)->rel_hdr2) | |
2138 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2139 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2140 | if (alloc1 == NULL) |
2141 | goto error_return; | |
2142 | external_relocs = alloc1; | |
2143 | } | |
2144 | ||
243ef1e0 | 2145 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2146 | external_relocs, |
2147 | internal_relocs)) | |
2148 | goto error_return; | |
51992aec AM |
2149 | if (elf_section_data (o)->rel_hdr2 |
2150 | && (!elf_link_read_relocs_from_section | |
2151 | (abfd, o, | |
2152 | elf_section_data (o)->rel_hdr2, | |
2153 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2154 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2155 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2156 | goto error_return; |
2157 | ||
2158 | /* Cache the results for next time, if we can. */ | |
2159 | if (keep_memory) | |
2160 | elf_section_data (o)->relocs = internal_relocs; | |
2161 | ||
2162 | if (alloc1 != NULL) | |
2163 | free (alloc1); | |
2164 | ||
2165 | /* Don't free alloc2, since if it was allocated we are passing it | |
2166 | back (under the name of internal_relocs). */ | |
2167 | ||
2168 | return internal_relocs; | |
2169 | ||
2170 | error_return: | |
2171 | if (alloc1 != NULL) | |
2172 | free (alloc1); | |
2173 | if (alloc2 != NULL) | |
2174 | free (alloc2); | |
2175 | return NULL; | |
2176 | } | |
2177 | ||
2178 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2179 | section header for a section containing relocations for O. */ | |
2180 | ||
2181 | bfd_boolean | |
268b6b39 AM |
2182 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2183 | Elf_Internal_Shdr *rel_hdr, | |
2184 | asection *o) | |
45d6a902 AM |
2185 | { |
2186 | bfd_size_type reloc_count; | |
2187 | bfd_size_type num_rel_hashes; | |
2188 | ||
2189 | /* Figure out how many relocations there will be. */ | |
2190 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2191 | reloc_count = elf_section_data (o)->rel_count; | |
2192 | else | |
2193 | reloc_count = elf_section_data (o)->rel_count2; | |
2194 | ||
2195 | num_rel_hashes = o->reloc_count; | |
2196 | if (num_rel_hashes < reloc_count) | |
2197 | num_rel_hashes = reloc_count; | |
2198 | ||
2199 | /* That allows us to calculate the size of the section. */ | |
2200 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2201 | ||
2202 | /* The contents field must last into write_object_contents, so we | |
2203 | allocate it with bfd_alloc rather than malloc. Also since we | |
2204 | cannot be sure that the contents will actually be filled in, | |
2205 | we zero the allocated space. */ | |
268b6b39 | 2206 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2207 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2208 | return FALSE; | |
2209 | ||
2210 | /* We only allocate one set of hash entries, so we only do it the | |
2211 | first time we are called. */ | |
2212 | if (elf_section_data (o)->rel_hashes == NULL | |
2213 | && num_rel_hashes) | |
2214 | { | |
2215 | struct elf_link_hash_entry **p; | |
2216 | ||
268b6b39 | 2217 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2218 | if (p == NULL) |
2219 | return FALSE; | |
2220 | ||
2221 | elf_section_data (o)->rel_hashes = p; | |
2222 | } | |
2223 | ||
2224 | return TRUE; | |
2225 | } | |
2226 | ||
2227 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2228 | originated from the section given by INPUT_REL_HDR) to the | |
2229 | OUTPUT_BFD. */ | |
2230 | ||
2231 | bfd_boolean | |
268b6b39 AM |
2232 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2233 | asection *input_section, | |
2234 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2235 | Elf_Internal_Rela *internal_relocs, |
2236 | struct elf_link_hash_entry **rel_hash | |
2237 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2238 | { |
2239 | Elf_Internal_Rela *irela; | |
2240 | Elf_Internal_Rela *irelaend; | |
2241 | bfd_byte *erel; | |
2242 | Elf_Internal_Shdr *output_rel_hdr; | |
2243 | asection *output_section; | |
2244 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2245 | const struct elf_backend_data *bed; |
268b6b39 | 2246 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2247 | |
2248 | output_section = input_section->output_section; | |
2249 | output_rel_hdr = NULL; | |
2250 | ||
2251 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2252 | == input_rel_hdr->sh_entsize) | |
2253 | { | |
2254 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2255 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2256 | } | |
2257 | else if (elf_section_data (output_section)->rel_hdr2 | |
2258 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2259 | == input_rel_hdr->sh_entsize)) | |
2260 | { | |
2261 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2262 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2263 | } | |
2264 | else | |
2265 | { | |
2266 | (*_bfd_error_handler) | |
d003868e AM |
2267 | (_("%B: relocation size mismatch in %B section %A"), |
2268 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2269 | bfd_set_error (bfd_error_wrong_object_format); |
2270 | return FALSE; | |
2271 | } | |
2272 | ||
2273 | bed = get_elf_backend_data (output_bfd); | |
2274 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2275 | swap_out = bed->s->swap_reloc_out; | |
2276 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2277 | swap_out = bed->s->swap_reloca_out; | |
2278 | else | |
2279 | abort (); | |
2280 | ||
2281 | erel = output_rel_hdr->contents; | |
2282 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2283 | irela = internal_relocs; | |
2284 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2285 | * bed->s->int_rels_per_ext_rel); | |
2286 | while (irela < irelaend) | |
2287 | { | |
2288 | (*swap_out) (output_bfd, irela, erel); | |
2289 | irela += bed->s->int_rels_per_ext_rel; | |
2290 | erel += input_rel_hdr->sh_entsize; | |
2291 | } | |
2292 | ||
2293 | /* Bump the counter, so that we know where to add the next set of | |
2294 | relocations. */ | |
2295 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2296 | ||
2297 | return TRUE; | |
2298 | } | |
2299 | \f | |
508c3946 L |
2300 | /* Make weak undefined symbols in PIE dynamic. */ |
2301 | ||
2302 | bfd_boolean | |
2303 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2304 | struct elf_link_hash_entry *h) | |
2305 | { | |
2306 | if (info->pie | |
2307 | && h->dynindx == -1 | |
2308 | && h->root.type == bfd_link_hash_undefweak) | |
2309 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2310 | ||
2311 | return TRUE; | |
2312 | } | |
2313 | ||
45d6a902 AM |
2314 | /* Fix up the flags for a symbol. This handles various cases which |
2315 | can only be fixed after all the input files are seen. This is | |
2316 | currently called by both adjust_dynamic_symbol and | |
2317 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2318 | the face of future changes. */ | |
2319 | ||
2320 | bfd_boolean | |
268b6b39 AM |
2321 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2322 | struct elf_info_failed *eif) | |
45d6a902 | 2323 | { |
508c3946 L |
2324 | const struct elf_backend_data *bed = NULL; |
2325 | ||
45d6a902 AM |
2326 | /* If this symbol was mentioned in a non-ELF file, try to set |
2327 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2328 | permit a non-ELF file to correctly refer to a symbol defined in | |
2329 | an ELF dynamic object. */ | |
f5385ebf | 2330 | if (h->non_elf) |
45d6a902 AM |
2331 | { |
2332 | while (h->root.type == bfd_link_hash_indirect) | |
2333 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2334 | ||
2335 | if (h->root.type != bfd_link_hash_defined | |
2336 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2337 | { |
2338 | h->ref_regular = 1; | |
2339 | h->ref_regular_nonweak = 1; | |
2340 | } | |
45d6a902 AM |
2341 | else |
2342 | { | |
2343 | if (h->root.u.def.section->owner != NULL | |
2344 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2345 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2346 | { |
2347 | h->ref_regular = 1; | |
2348 | h->ref_regular_nonweak = 1; | |
2349 | } | |
45d6a902 | 2350 | else |
f5385ebf | 2351 | h->def_regular = 1; |
45d6a902 AM |
2352 | } |
2353 | ||
2354 | if (h->dynindx == -1 | |
f5385ebf AM |
2355 | && (h->def_dynamic |
2356 | || h->ref_dynamic)) | |
45d6a902 | 2357 | { |
c152c796 | 2358 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2359 | { |
2360 | eif->failed = TRUE; | |
2361 | return FALSE; | |
2362 | } | |
2363 | } | |
2364 | } | |
2365 | else | |
2366 | { | |
f5385ebf | 2367 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2368 | was first seen in a non-ELF file. Fortunately, if the symbol |
2369 | was first seen in an ELF file, we're probably OK unless the | |
2370 | symbol was defined in a non-ELF file. Catch that case here. | |
2371 | FIXME: We're still in trouble if the symbol was first seen in | |
2372 | a dynamic object, and then later in a non-ELF regular object. */ | |
2373 | if ((h->root.type == bfd_link_hash_defined | |
2374 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2375 | && !h->def_regular |
45d6a902 AM |
2376 | && (h->root.u.def.section->owner != NULL |
2377 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2378 | != bfd_target_elf_flavour) | |
2379 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2380 | && !h->def_dynamic))) |
2381 | h->def_regular = 1; | |
45d6a902 AM |
2382 | } |
2383 | ||
508c3946 L |
2384 | /* Backend specific symbol fixup. */ |
2385 | if (elf_hash_table (eif->info)->dynobj) | |
2386 | { | |
2387 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2388 | if (bed->elf_backend_fixup_symbol | |
2389 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2390 | return FALSE; | |
2391 | } | |
2392 | ||
45d6a902 AM |
2393 | /* If this is a final link, and the symbol was defined as a common |
2394 | symbol in a regular object file, and there was no definition in | |
2395 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2396 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2397 | flag will not have been set. */ |
2398 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2399 | && !h->def_regular |
2400 | && h->ref_regular | |
2401 | && !h->def_dynamic | |
45d6a902 | 2402 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2403 | h->def_regular = 1; |
45d6a902 AM |
2404 | |
2405 | /* If -Bsymbolic was used (which means to bind references to global | |
2406 | symbols to the definition within the shared object), and this | |
2407 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2408 | need a PLT entry. Likewise, if the symbol has non-default |
2409 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2410 | will force it local. */ |
f5385ebf | 2411 | if (h->needs_plt |
45d6a902 | 2412 | && eif->info->shared |
0eddce27 | 2413 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2414 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2415 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2416 | && h->def_regular) |
45d6a902 | 2417 | { |
45d6a902 AM |
2418 | bfd_boolean force_local; |
2419 | ||
45d6a902 AM |
2420 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2421 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2422 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2423 | } | |
2424 | ||
2425 | /* If a weak undefined symbol has non-default visibility, we also | |
2426 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2427 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2428 | && h->root.type == bfd_link_hash_undefweak) |
2429 | { | |
9c5bfbb7 | 2430 | const struct elf_backend_data *bed; |
45d6a902 AM |
2431 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2432 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2433 | } | |
2434 | ||
2435 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2436 | the real definition in the dynamic object, copy interesting flags | |
2437 | over to the real definition. */ | |
f6e332e6 | 2438 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2439 | { |
2440 | struct elf_link_hash_entry *weakdef; | |
2441 | ||
f6e332e6 | 2442 | weakdef = h->u.weakdef; |
45d6a902 AM |
2443 | if (h->root.type == bfd_link_hash_indirect) |
2444 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2445 | ||
2446 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2447 | || h->root.type == bfd_link_hash_defweak); | |
2448 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2449 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2450 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2451 | |
2452 | /* If the real definition is defined by a regular object file, | |
2453 | don't do anything special. See the longer description in | |
2454 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2455 | if (weakdef->def_regular) |
f6e332e6 | 2456 | h->u.weakdef = NULL; |
45d6a902 | 2457 | else |
508c3946 L |
2458 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, |
2459 | h); | |
45d6a902 AM |
2460 | } |
2461 | ||
2462 | return TRUE; | |
2463 | } | |
2464 | ||
2465 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2466 | called via elf_link_hash_traverse, and also calls itself | |
2467 | recursively. */ | |
2468 | ||
2469 | bfd_boolean | |
268b6b39 | 2470 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2471 | { |
268b6b39 | 2472 | struct elf_info_failed *eif = data; |
45d6a902 | 2473 | bfd *dynobj; |
9c5bfbb7 | 2474 | const struct elf_backend_data *bed; |
45d6a902 | 2475 | |
0eddce27 | 2476 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2477 | return FALSE; |
2478 | ||
2479 | if (h->root.type == bfd_link_hash_warning) | |
2480 | { | |
a6aa5195 AM |
2481 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2482 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2483 | |
2484 | /* When warning symbols are created, they **replace** the "real" | |
2485 | entry in the hash table, thus we never get to see the real | |
2486 | symbol in a hash traversal. So look at it now. */ | |
2487 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2488 | } | |
2489 | ||
2490 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2491 | if (h->root.type == bfd_link_hash_indirect) | |
2492 | return TRUE; | |
2493 | ||
2494 | /* Fix the symbol flags. */ | |
2495 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2496 | return FALSE; | |
2497 | ||
2498 | /* If this symbol does not require a PLT entry, and it is not | |
2499 | defined by a dynamic object, or is not referenced by a regular | |
2500 | object, ignore it. We do have to handle a weak defined symbol, | |
2501 | even if no regular object refers to it, if we decided to add it | |
2502 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2503 | about symbols which are defined by one dynamic object and | |
2504 | referenced by another one? */ | |
f5385ebf AM |
2505 | if (!h->needs_plt |
2506 | && (h->def_regular | |
2507 | || !h->def_dynamic | |
2508 | || (!h->ref_regular | |
f6e332e6 | 2509 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2510 | { |
a6aa5195 | 2511 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2512 | return TRUE; |
2513 | } | |
2514 | ||
2515 | /* If we've already adjusted this symbol, don't do it again. This | |
2516 | can happen via a recursive call. */ | |
f5385ebf | 2517 | if (h->dynamic_adjusted) |
45d6a902 AM |
2518 | return TRUE; |
2519 | ||
2520 | /* Don't look at this symbol again. Note that we must set this | |
2521 | after checking the above conditions, because we may look at a | |
2522 | symbol once, decide not to do anything, and then get called | |
2523 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2524 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2525 | |
2526 | /* If this is a weak definition, and we know a real definition, and | |
2527 | the real symbol is not itself defined by a regular object file, | |
2528 | then get a good value for the real definition. We handle the | |
2529 | real symbol first, for the convenience of the backend routine. | |
2530 | ||
2531 | Note that there is a confusing case here. If the real definition | |
2532 | is defined by a regular object file, we don't get the real symbol | |
2533 | from the dynamic object, but we do get the weak symbol. If the | |
2534 | processor backend uses a COPY reloc, then if some routine in the | |
2535 | dynamic object changes the real symbol, we will not see that | |
2536 | change in the corresponding weak symbol. This is the way other | |
2537 | ELF linkers work as well, and seems to be a result of the shared | |
2538 | library model. | |
2539 | ||
2540 | I will clarify this issue. Most SVR4 shared libraries define the | |
2541 | variable _timezone and define timezone as a weak synonym. The | |
2542 | tzset call changes _timezone. If you write | |
2543 | extern int timezone; | |
2544 | int _timezone = 5; | |
2545 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2546 | you might expect that, since timezone is a synonym for _timezone, | |
2547 | the same number will print both times. However, if the processor | |
2548 | backend uses a COPY reloc, then actually timezone will be copied | |
2549 | into your process image, and, since you define _timezone | |
2550 | yourself, _timezone will not. Thus timezone and _timezone will | |
2551 | wind up at different memory locations. The tzset call will set | |
2552 | _timezone, leaving timezone unchanged. */ | |
2553 | ||
f6e332e6 | 2554 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2555 | { |
2556 | /* If we get to this point, we know there is an implicit | |
2557 | reference by a regular object file via the weak symbol H. | |
2558 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2559 | H->U.WEAKDEF before it finds H? */ |
2560 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2561 | |
f6e332e6 | 2562 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2563 | return FALSE; |
2564 | } | |
2565 | ||
2566 | /* If a symbol has no type and no size and does not require a PLT | |
2567 | entry, then we are probably about to do the wrong thing here: we | |
2568 | are probably going to create a COPY reloc for an empty object. | |
2569 | This case can arise when a shared object is built with assembly | |
2570 | code, and the assembly code fails to set the symbol type. */ | |
2571 | if (h->size == 0 | |
2572 | && h->type == STT_NOTYPE | |
f5385ebf | 2573 | && !h->needs_plt) |
45d6a902 AM |
2574 | (*_bfd_error_handler) |
2575 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2576 | h->root.root.string); | |
2577 | ||
2578 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2579 | bed = get_elf_backend_data (dynobj); | |
2580 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2581 | { | |
2582 | eif->failed = TRUE; | |
2583 | return FALSE; | |
2584 | } | |
2585 | ||
2586 | return TRUE; | |
2587 | } | |
2588 | ||
2589 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2590 | to reflect the object merging within the sections. */ | |
2591 | ||
2592 | bfd_boolean | |
268b6b39 | 2593 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2594 | { |
2595 | asection *sec; | |
2596 | ||
2597 | if (h->root.type == bfd_link_hash_warning) | |
2598 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2599 | ||
2600 | if ((h->root.type == bfd_link_hash_defined | |
2601 | || h->root.type == bfd_link_hash_defweak) | |
2602 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2603 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2604 | { | |
268b6b39 | 2605 | bfd *output_bfd = data; |
45d6a902 AM |
2606 | |
2607 | h->root.u.def.value = | |
2608 | _bfd_merged_section_offset (output_bfd, | |
2609 | &h->root.u.def.section, | |
2610 | elf_section_data (sec)->sec_info, | |
753731ee | 2611 | h->root.u.def.value); |
45d6a902 AM |
2612 | } |
2613 | ||
2614 | return TRUE; | |
2615 | } | |
986a241f RH |
2616 | |
2617 | /* Returns false if the symbol referred to by H should be considered | |
2618 | to resolve local to the current module, and true if it should be | |
2619 | considered to bind dynamically. */ | |
2620 | ||
2621 | bfd_boolean | |
268b6b39 AM |
2622 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2623 | struct bfd_link_info *info, | |
2624 | bfd_boolean ignore_protected) | |
986a241f RH |
2625 | { |
2626 | bfd_boolean binding_stays_local_p; | |
2627 | ||
2628 | if (h == NULL) | |
2629 | return FALSE; | |
2630 | ||
2631 | while (h->root.type == bfd_link_hash_indirect | |
2632 | || h->root.type == bfd_link_hash_warning) | |
2633 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2634 | ||
2635 | /* If it was forced local, then clearly it's not dynamic. */ | |
2636 | if (h->dynindx == -1) | |
2637 | return FALSE; | |
f5385ebf | 2638 | if (h->forced_local) |
986a241f RH |
2639 | return FALSE; |
2640 | ||
2641 | /* Identify the cases where name binding rules say that a | |
2642 | visible symbol resolves locally. */ | |
55255dae | 2643 | binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); |
986a241f RH |
2644 | |
2645 | switch (ELF_ST_VISIBILITY (h->other)) | |
2646 | { | |
2647 | case STV_INTERNAL: | |
2648 | case STV_HIDDEN: | |
2649 | return FALSE; | |
2650 | ||
2651 | case STV_PROTECTED: | |
2652 | /* Proper resolution for function pointer equality may require | |
2653 | that these symbols perhaps be resolved dynamically, even though | |
2654 | we should be resolving them to the current module. */ | |
1c16dfa5 | 2655 | if (!ignore_protected || h->type != STT_FUNC) |
986a241f RH |
2656 | binding_stays_local_p = TRUE; |
2657 | break; | |
2658 | ||
2659 | default: | |
986a241f RH |
2660 | break; |
2661 | } | |
2662 | ||
aa37626c | 2663 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2664 | if (!h->def_regular) |
aa37626c L |
2665 | return TRUE; |
2666 | ||
986a241f RH |
2667 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2668 | us that it remains local. */ | |
2669 | return !binding_stays_local_p; | |
2670 | } | |
f6c52c13 AM |
2671 | |
2672 | /* Return true if the symbol referred to by H should be considered | |
2673 | to resolve local to the current module, and false otherwise. Differs | |
2674 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2675 | undefined symbols and weak symbols. */ | |
2676 | ||
2677 | bfd_boolean | |
268b6b39 AM |
2678 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2679 | struct bfd_link_info *info, | |
2680 | bfd_boolean local_protected) | |
f6c52c13 AM |
2681 | { |
2682 | /* If it's a local sym, of course we resolve locally. */ | |
2683 | if (h == NULL) | |
2684 | return TRUE; | |
2685 | ||
7e2294f9 AO |
2686 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2687 | flag set, so test it first, and don't bail out. */ | |
2688 | if (ELF_COMMON_DEF_P (h)) | |
2689 | /* Do nothing. */; | |
f6c52c13 | 2690 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2691 | resolve locally. The sym is either undefined or dynamic. */ |
2692 | else if (!h->def_regular) | |
f6c52c13 AM |
2693 | return FALSE; |
2694 | ||
2695 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2696 | if (h->forced_local) |
f6c52c13 AM |
2697 | return TRUE; |
2698 | ||
2699 | /* As do non-dynamic symbols. */ | |
2700 | if (h->dynindx == -1) | |
2701 | return TRUE; | |
2702 | ||
2703 | /* At this point, we know the symbol is defined and dynamic. In an | |
2704 | executable it must resolve locally, likewise when building symbolic | |
2705 | shared libraries. */ | |
55255dae | 2706 | if (info->executable || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
2707 | return TRUE; |
2708 | ||
2709 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2710 | with default visibility might not resolve locally. */ | |
2711 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2712 | return FALSE; | |
2713 | ||
2714 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2715 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2716 | return TRUE; | |
2717 | ||
1c16dfa5 L |
2718 | /* STV_PROTECTED non-function symbols are local. */ |
2719 | if (h->type != STT_FUNC) | |
2720 | return TRUE; | |
2721 | ||
f6c52c13 AM |
2722 | /* Function pointer equality tests may require that STV_PROTECTED |
2723 | symbols be treated as dynamic symbols, even when we know that the | |
2724 | dynamic linker will resolve them locally. */ | |
2725 | return local_protected; | |
2726 | } | |
e1918d23 AM |
2727 | |
2728 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2729 | aligned. Returns the first TLS output section. */ | |
2730 | ||
2731 | struct bfd_section * | |
2732 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2733 | { | |
2734 | struct bfd_section *sec, *tls; | |
2735 | unsigned int align = 0; | |
2736 | ||
2737 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2738 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2739 | break; | |
2740 | tls = sec; | |
2741 | ||
2742 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2743 | if (sec->alignment_power > align) | |
2744 | align = sec->alignment_power; | |
2745 | ||
2746 | elf_hash_table (info)->tls_sec = tls; | |
2747 | ||
2748 | /* Ensure the alignment of the first section is the largest alignment, | |
2749 | so that the tls segment starts aligned. */ | |
2750 | if (tls != NULL) | |
2751 | tls->alignment_power = align; | |
2752 | ||
2753 | return tls; | |
2754 | } | |
0ad989f9 L |
2755 | |
2756 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2757 | static bfd_boolean | |
2758 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2759 | Elf_Internal_Sym *sym) | |
2760 | { | |
a4d8e49b L |
2761 | const struct elf_backend_data *bed; |
2762 | ||
0ad989f9 L |
2763 | /* Local symbols do not count, but target specific ones might. */ |
2764 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2765 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2766 | return FALSE; | |
2767 | ||
2768 | /* Function symbols do not count. */ | |
2769 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2770 | return FALSE; | |
2771 | ||
2772 | /* If the section is undefined, then so is the symbol. */ | |
2773 | if (sym->st_shndx == SHN_UNDEF) | |
2774 | return FALSE; | |
2775 | ||
2776 | /* If the symbol is defined in the common section, then | |
2777 | it is a common definition and so does not count. */ | |
a4d8e49b L |
2778 | bed = get_elf_backend_data (abfd); |
2779 | if (bed->common_definition (sym)) | |
0ad989f9 L |
2780 | return FALSE; |
2781 | ||
2782 | /* If the symbol is in a target specific section then we | |
2783 | must rely upon the backend to tell us what it is. */ | |
2784 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2785 | /* FIXME - this function is not coded yet: | |
2786 | ||
2787 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2788 | ||
2789 | Instead for now assume that the definition is not global, | |
2790 | Even if this is wrong, at least the linker will behave | |
2791 | in the same way that it used to do. */ | |
2792 | return FALSE; | |
2793 | ||
2794 | return TRUE; | |
2795 | } | |
2796 | ||
2797 | /* Search the symbol table of the archive element of the archive ABFD | |
2798 | whose archive map contains a mention of SYMDEF, and determine if | |
2799 | the symbol is defined in this element. */ | |
2800 | static bfd_boolean | |
2801 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2802 | { | |
2803 | Elf_Internal_Shdr * hdr; | |
2804 | bfd_size_type symcount; | |
2805 | bfd_size_type extsymcount; | |
2806 | bfd_size_type extsymoff; | |
2807 | Elf_Internal_Sym *isymbuf; | |
2808 | Elf_Internal_Sym *isym; | |
2809 | Elf_Internal_Sym *isymend; | |
2810 | bfd_boolean result; | |
2811 | ||
2812 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2813 | if (abfd == NULL) | |
2814 | return FALSE; | |
2815 | ||
2816 | if (! bfd_check_format (abfd, bfd_object)) | |
2817 | return FALSE; | |
2818 | ||
2819 | /* If we have already included the element containing this symbol in the | |
2820 | link then we do not need to include it again. Just claim that any symbol | |
2821 | it contains is not a definition, so that our caller will not decide to | |
2822 | (re)include this element. */ | |
2823 | if (abfd->archive_pass) | |
2824 | return FALSE; | |
2825 | ||
2826 | /* Select the appropriate symbol table. */ | |
2827 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2828 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2829 | else | |
2830 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2831 | ||
2832 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2833 | ||
2834 | /* The sh_info field of the symtab header tells us where the | |
2835 | external symbols start. We don't care about the local symbols. */ | |
2836 | if (elf_bad_symtab (abfd)) | |
2837 | { | |
2838 | extsymcount = symcount; | |
2839 | extsymoff = 0; | |
2840 | } | |
2841 | else | |
2842 | { | |
2843 | extsymcount = symcount - hdr->sh_info; | |
2844 | extsymoff = hdr->sh_info; | |
2845 | } | |
2846 | ||
2847 | if (extsymcount == 0) | |
2848 | return FALSE; | |
2849 | ||
2850 | /* Read in the symbol table. */ | |
2851 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2852 | NULL, NULL, NULL); | |
2853 | if (isymbuf == NULL) | |
2854 | return FALSE; | |
2855 | ||
2856 | /* Scan the symbol table looking for SYMDEF. */ | |
2857 | result = FALSE; | |
2858 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2859 | { | |
2860 | const char *name; | |
2861 | ||
2862 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2863 | isym->st_name); | |
2864 | if (name == NULL) | |
2865 | break; | |
2866 | ||
2867 | if (strcmp (name, symdef->name) == 0) | |
2868 | { | |
2869 | result = is_global_data_symbol_definition (abfd, isym); | |
2870 | break; | |
2871 | } | |
2872 | } | |
2873 | ||
2874 | free (isymbuf); | |
2875 | ||
2876 | return result; | |
2877 | } | |
2878 | \f | |
5a580b3a AM |
2879 | /* Add an entry to the .dynamic table. */ |
2880 | ||
2881 | bfd_boolean | |
2882 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2883 | bfd_vma tag, | |
2884 | bfd_vma val) | |
2885 | { | |
2886 | struct elf_link_hash_table *hash_table; | |
2887 | const struct elf_backend_data *bed; | |
2888 | asection *s; | |
2889 | bfd_size_type newsize; | |
2890 | bfd_byte *newcontents; | |
2891 | Elf_Internal_Dyn dyn; | |
2892 | ||
2893 | hash_table = elf_hash_table (info); | |
2894 | if (! is_elf_hash_table (hash_table)) | |
2895 | return FALSE; | |
2896 | ||
2897 | bed = get_elf_backend_data (hash_table->dynobj); | |
2898 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2899 | BFD_ASSERT (s != NULL); | |
2900 | ||
eea6121a | 2901 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2902 | newcontents = bfd_realloc (s->contents, newsize); |
2903 | if (newcontents == NULL) | |
2904 | return FALSE; | |
2905 | ||
2906 | dyn.d_tag = tag; | |
2907 | dyn.d_un.d_val = val; | |
eea6121a | 2908 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2909 | |
eea6121a | 2910 | s->size = newsize; |
5a580b3a AM |
2911 | s->contents = newcontents; |
2912 | ||
2913 | return TRUE; | |
2914 | } | |
2915 | ||
2916 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2917 | otherwise just check whether one already exists. Returns -1 on error, | |
2918 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2919 | ||
4ad4eba5 | 2920 | static int |
7e9f0867 AM |
2921 | elf_add_dt_needed_tag (bfd *abfd, |
2922 | struct bfd_link_info *info, | |
4ad4eba5 AM |
2923 | const char *soname, |
2924 | bfd_boolean do_it) | |
5a580b3a AM |
2925 | { |
2926 | struct elf_link_hash_table *hash_table; | |
2927 | bfd_size_type oldsize; | |
2928 | bfd_size_type strindex; | |
2929 | ||
7e9f0867 AM |
2930 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
2931 | return -1; | |
2932 | ||
5a580b3a AM |
2933 | hash_table = elf_hash_table (info); |
2934 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2935 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2936 | if (strindex == (bfd_size_type) -1) | |
2937 | return -1; | |
2938 | ||
2939 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2940 | { | |
2941 | asection *sdyn; | |
2942 | const struct elf_backend_data *bed; | |
2943 | bfd_byte *extdyn; | |
2944 | ||
2945 | bed = get_elf_backend_data (hash_table->dynobj); | |
2946 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
2947 | if (sdyn != NULL) |
2948 | for (extdyn = sdyn->contents; | |
2949 | extdyn < sdyn->contents + sdyn->size; | |
2950 | extdyn += bed->s->sizeof_dyn) | |
2951 | { | |
2952 | Elf_Internal_Dyn dyn; | |
5a580b3a | 2953 | |
7e9f0867 AM |
2954 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
2955 | if (dyn.d_tag == DT_NEEDED | |
2956 | && dyn.d_un.d_val == strindex) | |
2957 | { | |
2958 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2959 | return 1; | |
2960 | } | |
2961 | } | |
5a580b3a AM |
2962 | } |
2963 | ||
2964 | if (do_it) | |
2965 | { | |
7e9f0867 AM |
2966 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
2967 | return -1; | |
2968 | ||
5a580b3a AM |
2969 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
2970 | return -1; | |
2971 | } | |
2972 | else | |
2973 | /* We were just checking for existence of the tag. */ | |
2974 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2975 | ||
2976 | return 0; | |
2977 | } | |
2978 | ||
2979 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
2980 | static int |
2981 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2982 | { |
2983 | const struct elf_link_hash_entry *h1; | |
2984 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 2985 | bfd_signed_vma vdiff; |
5a580b3a AM |
2986 | |
2987 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2988 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
2989 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
2990 | if (vdiff != 0) | |
2991 | return vdiff > 0 ? 1 : -1; | |
2992 | else | |
2993 | { | |
2994 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
2995 | if (sdiff != 0) | |
2996 | return sdiff > 0 ? 1 : -1; | |
2997 | } | |
5a580b3a AM |
2998 | return 0; |
2999 | } | |
4ad4eba5 | 3000 | |
5a580b3a AM |
3001 | /* This function is used to adjust offsets into .dynstr for |
3002 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3003 | ||
3004 | static bfd_boolean | |
3005 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3006 | { | |
3007 | struct elf_strtab_hash *dynstr = data; | |
3008 | ||
3009 | if (h->root.type == bfd_link_hash_warning) | |
3010 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3011 | ||
3012 | if (h->dynindx != -1) | |
3013 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3014 | return TRUE; | |
3015 | } | |
3016 | ||
3017 | /* Assign string offsets in .dynstr, update all structures referencing | |
3018 | them. */ | |
3019 | ||
4ad4eba5 AM |
3020 | static bfd_boolean |
3021 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3022 | { |
3023 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3024 | struct elf_link_local_dynamic_entry *entry; | |
3025 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3026 | bfd *dynobj = hash_table->dynobj; | |
3027 | asection *sdyn; | |
3028 | bfd_size_type size; | |
3029 | const struct elf_backend_data *bed; | |
3030 | bfd_byte *extdyn; | |
3031 | ||
3032 | _bfd_elf_strtab_finalize (dynstr); | |
3033 | size = _bfd_elf_strtab_size (dynstr); | |
3034 | ||
3035 | bed = get_elf_backend_data (dynobj); | |
3036 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3037 | BFD_ASSERT (sdyn != NULL); | |
3038 | ||
3039 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3040 | for (extdyn = sdyn->contents; | |
eea6121a | 3041 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3042 | extdyn += bed->s->sizeof_dyn) |
3043 | { | |
3044 | Elf_Internal_Dyn dyn; | |
3045 | ||
3046 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3047 | switch (dyn.d_tag) | |
3048 | { | |
3049 | case DT_STRSZ: | |
3050 | dyn.d_un.d_val = size; | |
3051 | break; | |
3052 | case DT_NEEDED: | |
3053 | case DT_SONAME: | |
3054 | case DT_RPATH: | |
3055 | case DT_RUNPATH: | |
3056 | case DT_FILTER: | |
3057 | case DT_AUXILIARY: | |
3058 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3059 | break; | |
3060 | default: | |
3061 | continue; | |
3062 | } | |
3063 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3064 | } | |
3065 | ||
3066 | /* Now update local dynamic symbols. */ | |
3067 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3068 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3069 | entry->isym.st_name); | |
3070 | ||
3071 | /* And the rest of dynamic symbols. */ | |
3072 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3073 | ||
3074 | /* Adjust version definitions. */ | |
3075 | if (elf_tdata (output_bfd)->cverdefs) | |
3076 | { | |
3077 | asection *s; | |
3078 | bfd_byte *p; | |
3079 | bfd_size_type i; | |
3080 | Elf_Internal_Verdef def; | |
3081 | Elf_Internal_Verdaux defaux; | |
3082 | ||
3083 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3084 | p = s->contents; | |
3085 | do | |
3086 | { | |
3087 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3088 | &def); | |
3089 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3090 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3091 | continue; | |
5a580b3a AM |
3092 | for (i = 0; i < def.vd_cnt; ++i) |
3093 | { | |
3094 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3095 | (Elf_External_Verdaux *) p, &defaux); | |
3096 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3097 | defaux.vda_name); | |
3098 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3099 | &defaux, (Elf_External_Verdaux *) p); | |
3100 | p += sizeof (Elf_External_Verdaux); | |
3101 | } | |
3102 | } | |
3103 | while (def.vd_next); | |
3104 | } | |
3105 | ||
3106 | /* Adjust version references. */ | |
3107 | if (elf_tdata (output_bfd)->verref) | |
3108 | { | |
3109 | asection *s; | |
3110 | bfd_byte *p; | |
3111 | bfd_size_type i; | |
3112 | Elf_Internal_Verneed need; | |
3113 | Elf_Internal_Vernaux needaux; | |
3114 | ||
3115 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3116 | p = s->contents; | |
3117 | do | |
3118 | { | |
3119 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3120 | &need); | |
3121 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3122 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3123 | (Elf_External_Verneed *) p); | |
3124 | p += sizeof (Elf_External_Verneed); | |
3125 | for (i = 0; i < need.vn_cnt; ++i) | |
3126 | { | |
3127 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3128 | (Elf_External_Vernaux *) p, &needaux); | |
3129 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3130 | needaux.vna_name); | |
3131 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3132 | &needaux, | |
3133 | (Elf_External_Vernaux *) p); | |
3134 | p += sizeof (Elf_External_Vernaux); | |
3135 | } | |
3136 | } | |
3137 | while (need.vn_next); | |
3138 | } | |
3139 | ||
3140 | return TRUE; | |
3141 | } | |
3142 | \f | |
4ad4eba5 AM |
3143 | /* Add symbols from an ELF object file to the linker hash table. */ |
3144 | ||
3145 | static bfd_boolean | |
3146 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3147 | { | |
4ad4eba5 AM |
3148 | Elf_Internal_Shdr *hdr; |
3149 | bfd_size_type symcount; | |
3150 | bfd_size_type extsymcount; | |
3151 | bfd_size_type extsymoff; | |
3152 | struct elf_link_hash_entry **sym_hash; | |
3153 | bfd_boolean dynamic; | |
3154 | Elf_External_Versym *extversym = NULL; | |
3155 | Elf_External_Versym *ever; | |
3156 | struct elf_link_hash_entry *weaks; | |
3157 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3158 | bfd_size_type nondeflt_vers_cnt = 0; | |
3159 | Elf_Internal_Sym *isymbuf = NULL; | |
3160 | Elf_Internal_Sym *isym; | |
3161 | Elf_Internal_Sym *isymend; | |
3162 | const struct elf_backend_data *bed; | |
3163 | bfd_boolean add_needed; | |
66eb6687 | 3164 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3165 | bfd_size_type amt; |
66eb6687 | 3166 | void *alloc_mark = NULL; |
4f87808c AM |
3167 | struct bfd_hash_entry **old_table = NULL; |
3168 | unsigned int old_size = 0; | |
3169 | unsigned int old_count = 0; | |
66eb6687 AM |
3170 | void *old_tab = NULL; |
3171 | void *old_hash; | |
3172 | void *old_ent; | |
3173 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3174 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
3175 | long old_dynsymcount = 0; | |
3176 | size_t tabsize = 0; | |
3177 | size_t hashsize = 0; | |
4ad4eba5 | 3178 | |
66eb6687 | 3179 | htab = elf_hash_table (info); |
4ad4eba5 | 3180 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3181 | |
3182 | if ((abfd->flags & DYNAMIC) == 0) | |
3183 | dynamic = FALSE; | |
3184 | else | |
3185 | { | |
3186 | dynamic = TRUE; | |
3187 | ||
3188 | /* You can't use -r against a dynamic object. Also, there's no | |
3189 | hope of using a dynamic object which does not exactly match | |
3190 | the format of the output file. */ | |
3191 | if (info->relocatable | |
66eb6687 AM |
3192 | || !is_elf_hash_table (htab) |
3193 | || htab->root.creator != abfd->xvec) | |
4ad4eba5 | 3194 | { |
9a0789ec NC |
3195 | if (info->relocatable) |
3196 | bfd_set_error (bfd_error_invalid_operation); | |
3197 | else | |
3198 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3199 | goto error_return; |
3200 | } | |
3201 | } | |
3202 | ||
3203 | /* As a GNU extension, any input sections which are named | |
3204 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3205 | symbol. This differs from .gnu.warning sections, which generate | |
3206 | warnings when they are included in an output file. */ | |
3207 | if (info->executable) | |
3208 | { | |
3209 | asection *s; | |
3210 | ||
3211 | for (s = abfd->sections; s != NULL; s = s->next) | |
3212 | { | |
3213 | const char *name; | |
3214 | ||
3215 | name = bfd_get_section_name (abfd, s); | |
0112cd26 | 3216 | if (CONST_STRNEQ (name, ".gnu.warning.")) |
4ad4eba5 AM |
3217 | { |
3218 | char *msg; | |
3219 | bfd_size_type sz; | |
4ad4eba5 AM |
3220 | |
3221 | name += sizeof ".gnu.warning." - 1; | |
3222 | ||
3223 | /* If this is a shared object, then look up the symbol | |
3224 | in the hash table. If it is there, and it is already | |
3225 | been defined, then we will not be using the entry | |
3226 | from this shared object, so we don't need to warn. | |
3227 | FIXME: If we see the definition in a regular object | |
3228 | later on, we will warn, but we shouldn't. The only | |
3229 | fix is to keep track of what warnings we are supposed | |
3230 | to emit, and then handle them all at the end of the | |
3231 | link. */ | |
3232 | if (dynamic) | |
3233 | { | |
3234 | struct elf_link_hash_entry *h; | |
3235 | ||
66eb6687 | 3236 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
4ad4eba5 AM |
3237 | |
3238 | /* FIXME: What about bfd_link_hash_common? */ | |
3239 | if (h != NULL | |
3240 | && (h->root.type == bfd_link_hash_defined | |
3241 | || h->root.type == bfd_link_hash_defweak)) | |
3242 | { | |
3243 | /* We don't want to issue this warning. Clobber | |
3244 | the section size so that the warning does not | |
3245 | get copied into the output file. */ | |
eea6121a | 3246 | s->size = 0; |
4ad4eba5 AM |
3247 | continue; |
3248 | } | |
3249 | } | |
3250 | ||
eea6121a | 3251 | sz = s->size; |
370a0e1b | 3252 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3253 | if (msg == NULL) |
3254 | goto error_return; | |
3255 | ||
370a0e1b | 3256 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3257 | goto error_return; |
3258 | ||
370a0e1b | 3259 | msg[sz] = '\0'; |
4ad4eba5 AM |
3260 | |
3261 | if (! (_bfd_generic_link_add_one_symbol | |
3262 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
66eb6687 | 3263 | FALSE, bed->collect, NULL))) |
4ad4eba5 AM |
3264 | goto error_return; |
3265 | ||
3266 | if (! info->relocatable) | |
3267 | { | |
3268 | /* Clobber the section size so that the warning does | |
3269 | not get copied into the output file. */ | |
eea6121a | 3270 | s->size = 0; |
11d2f718 AM |
3271 | |
3272 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3273 | the warning section don't get copied to the output. */ | |
3274 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3275 | } |
3276 | } | |
3277 | } | |
3278 | } | |
3279 | ||
3280 | add_needed = TRUE; | |
3281 | if (! dynamic) | |
3282 | { | |
3283 | /* If we are creating a shared library, create all the dynamic | |
3284 | sections immediately. We need to attach them to something, | |
3285 | so we attach them to this BFD, provided it is the right | |
3286 | format. FIXME: If there are no input BFD's of the same | |
3287 | format as the output, we can't make a shared library. */ | |
3288 | if (info->shared | |
66eb6687 AM |
3289 | && is_elf_hash_table (htab) |
3290 | && htab->root.creator == abfd->xvec | |
3291 | && !htab->dynamic_sections_created) | |
4ad4eba5 AM |
3292 | { |
3293 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3294 | goto error_return; | |
3295 | } | |
3296 | } | |
66eb6687 | 3297 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3298 | goto error_return; |
3299 | else | |
3300 | { | |
3301 | asection *s; | |
3302 | const char *soname = NULL; | |
3303 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3304 | int ret; | |
3305 | ||
3306 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3307 | ld shouldn't allow it. */ |
4ad4eba5 AM |
3308 | if ((s = abfd->sections) != NULL |
3309 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
92fd189d | 3310 | abort (); |
4ad4eba5 AM |
3311 | |
3312 | /* If this dynamic lib was specified on the command line with | |
3313 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3314 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3315 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3316 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3317 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3318 | all. */ | |
3319 | add_needed = (elf_dyn_lib_class (abfd) | |
3320 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3321 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3322 | |
3323 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3324 | if (s != NULL) | |
3325 | { | |
3326 | bfd_byte *dynbuf; | |
3327 | bfd_byte *extdyn; | |
3328 | int elfsec; | |
3329 | unsigned long shlink; | |
3330 | ||
eea6121a | 3331 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3332 | goto error_free_dyn; |
3333 | ||
3334 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3335 | if (elfsec == -1) | |
3336 | goto error_free_dyn; | |
3337 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3338 | ||
3339 | for (extdyn = dynbuf; | |
eea6121a | 3340 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3341 | extdyn += bed->s->sizeof_dyn) |
3342 | { | |
3343 | Elf_Internal_Dyn dyn; | |
3344 | ||
3345 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3346 | if (dyn.d_tag == DT_SONAME) | |
3347 | { | |
3348 | unsigned int tagv = dyn.d_un.d_val; | |
3349 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3350 | if (soname == NULL) | |
3351 | goto error_free_dyn; | |
3352 | } | |
3353 | if (dyn.d_tag == DT_NEEDED) | |
3354 | { | |
3355 | struct bfd_link_needed_list *n, **pn; | |
3356 | char *fnm, *anm; | |
3357 | unsigned int tagv = dyn.d_un.d_val; | |
3358 | ||
3359 | amt = sizeof (struct bfd_link_needed_list); | |
3360 | n = bfd_alloc (abfd, amt); | |
3361 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3362 | if (n == NULL || fnm == NULL) | |
3363 | goto error_free_dyn; | |
3364 | amt = strlen (fnm) + 1; | |
3365 | anm = bfd_alloc (abfd, amt); | |
3366 | if (anm == NULL) | |
3367 | goto error_free_dyn; | |
3368 | memcpy (anm, fnm, amt); | |
3369 | n->name = anm; | |
3370 | n->by = abfd; | |
3371 | n->next = NULL; | |
66eb6687 | 3372 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3373 | ; |
3374 | *pn = n; | |
3375 | } | |
3376 | if (dyn.d_tag == DT_RUNPATH) | |
3377 | { | |
3378 | struct bfd_link_needed_list *n, **pn; | |
3379 | char *fnm, *anm; | |
3380 | unsigned int tagv = dyn.d_un.d_val; | |
3381 | ||
3382 | amt = sizeof (struct bfd_link_needed_list); | |
3383 | n = bfd_alloc (abfd, amt); | |
3384 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3385 | if (n == NULL || fnm == NULL) | |
3386 | goto error_free_dyn; | |
3387 | amt = strlen (fnm) + 1; | |
3388 | anm = bfd_alloc (abfd, amt); | |
3389 | if (anm == NULL) | |
3390 | goto error_free_dyn; | |
3391 | memcpy (anm, fnm, amt); | |
3392 | n->name = anm; | |
3393 | n->by = abfd; | |
3394 | n->next = NULL; | |
3395 | for (pn = & runpath; | |
3396 | *pn != NULL; | |
3397 | pn = &(*pn)->next) | |
3398 | ; | |
3399 | *pn = n; | |
3400 | } | |
3401 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3402 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3403 | { | |
3404 | struct bfd_link_needed_list *n, **pn; | |
3405 | char *fnm, *anm; | |
3406 | unsigned int tagv = dyn.d_un.d_val; | |
3407 | ||
3408 | amt = sizeof (struct bfd_link_needed_list); | |
3409 | n = bfd_alloc (abfd, amt); | |
3410 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3411 | if (n == NULL || fnm == NULL) | |
3412 | goto error_free_dyn; | |
3413 | amt = strlen (fnm) + 1; | |
3414 | anm = bfd_alloc (abfd, amt); | |
3415 | if (anm == NULL) | |
3416 | { | |
3417 | error_free_dyn: | |
3418 | free (dynbuf); | |
3419 | goto error_return; | |
3420 | } | |
3421 | memcpy (anm, fnm, amt); | |
3422 | n->name = anm; | |
3423 | n->by = abfd; | |
3424 | n->next = NULL; | |
3425 | for (pn = & rpath; | |
3426 | *pn != NULL; | |
3427 | pn = &(*pn)->next) | |
3428 | ; | |
3429 | *pn = n; | |
3430 | } | |
3431 | } | |
3432 | ||
3433 | free (dynbuf); | |
3434 | } | |
3435 | ||
3436 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3437 | frees all more recently bfd_alloc'd blocks as well. */ | |
3438 | if (runpath) | |
3439 | rpath = runpath; | |
3440 | ||
3441 | if (rpath) | |
3442 | { | |
3443 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3444 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3445 | ; |
3446 | *pn = rpath; | |
3447 | } | |
3448 | ||
3449 | /* We do not want to include any of the sections in a dynamic | |
3450 | object in the output file. We hack by simply clobbering the | |
3451 | list of sections in the BFD. This could be handled more | |
3452 | cleanly by, say, a new section flag; the existing | |
3453 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3454 | still implies that the section takes up space in the output | |
3455 | file. */ | |
3456 | bfd_section_list_clear (abfd); | |
3457 | ||
4ad4eba5 AM |
3458 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3459 | object. If the object has a DT_SONAME entry, we use it. | |
3460 | Otherwise, if the generic linker stuck something in | |
3461 | elf_dt_name, we use that. Otherwise, we just use the file | |
3462 | name. */ | |
3463 | if (soname == NULL || *soname == '\0') | |
3464 | { | |
3465 | soname = elf_dt_name (abfd); | |
3466 | if (soname == NULL || *soname == '\0') | |
3467 | soname = bfd_get_filename (abfd); | |
3468 | } | |
3469 | ||
3470 | /* Save the SONAME because sometimes the linker emulation code | |
3471 | will need to know it. */ | |
3472 | elf_dt_name (abfd) = soname; | |
3473 | ||
7e9f0867 | 3474 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3475 | if (ret < 0) |
3476 | goto error_return; | |
3477 | ||
3478 | /* If we have already included this dynamic object in the | |
3479 | link, just ignore it. There is no reason to include a | |
3480 | particular dynamic object more than once. */ | |
3481 | if (ret > 0) | |
3482 | return TRUE; | |
3483 | } | |
3484 | ||
3485 | /* If this is a dynamic object, we always link against the .dynsym | |
3486 | symbol table, not the .symtab symbol table. The dynamic linker | |
3487 | will only see the .dynsym symbol table, so there is no reason to | |
3488 | look at .symtab for a dynamic object. */ | |
3489 | ||
3490 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3491 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3492 | else | |
3493 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3494 | ||
3495 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3496 | ||
3497 | /* The sh_info field of the symtab header tells us where the | |
3498 | external symbols start. We don't care about the local symbols at | |
3499 | this point. */ | |
3500 | if (elf_bad_symtab (abfd)) | |
3501 | { | |
3502 | extsymcount = symcount; | |
3503 | extsymoff = 0; | |
3504 | } | |
3505 | else | |
3506 | { | |
3507 | extsymcount = symcount - hdr->sh_info; | |
3508 | extsymoff = hdr->sh_info; | |
3509 | } | |
3510 | ||
3511 | sym_hash = NULL; | |
3512 | if (extsymcount != 0) | |
3513 | { | |
3514 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3515 | NULL, NULL, NULL); | |
3516 | if (isymbuf == NULL) | |
3517 | goto error_return; | |
3518 | ||
3519 | /* We store a pointer to the hash table entry for each external | |
3520 | symbol. */ | |
3521 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3522 | sym_hash = bfd_alloc (abfd, amt); | |
3523 | if (sym_hash == NULL) | |
3524 | goto error_free_sym; | |
3525 | elf_sym_hashes (abfd) = sym_hash; | |
3526 | } | |
3527 | ||
3528 | if (dynamic) | |
3529 | { | |
3530 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3531 | if (!_bfd_elf_slurp_version_tables (abfd, |
3532 | info->default_imported_symver)) | |
4ad4eba5 AM |
3533 | goto error_free_sym; |
3534 | ||
3535 | /* Read in the symbol versions, but don't bother to convert them | |
3536 | to internal format. */ | |
3537 | if (elf_dynversym (abfd) != 0) | |
3538 | { | |
3539 | Elf_Internal_Shdr *versymhdr; | |
3540 | ||
3541 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3542 | extversym = bfd_malloc (versymhdr->sh_size); | |
3543 | if (extversym == NULL) | |
3544 | goto error_free_sym; | |
3545 | amt = versymhdr->sh_size; | |
3546 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3547 | || bfd_bread (extversym, amt, abfd) != amt) | |
3548 | goto error_free_vers; | |
3549 | } | |
3550 | } | |
3551 | ||
66eb6687 AM |
3552 | /* If we are loading an as-needed shared lib, save the symbol table |
3553 | state before we start adding symbols. If the lib turns out | |
3554 | to be unneeded, restore the state. */ | |
3555 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
3556 | { | |
3557 | unsigned int i; | |
3558 | size_t entsize; | |
3559 | ||
3560 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
3561 | { | |
3562 | struct bfd_hash_entry *p; | |
2de92251 | 3563 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3564 | |
3565 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
3566 | { |
3567 | h = (struct elf_link_hash_entry *) p; | |
3568 | entsize += htab->root.table.entsize; | |
3569 | if (h->root.type == bfd_link_hash_warning) | |
3570 | entsize += htab->root.table.entsize; | |
3571 | } | |
66eb6687 AM |
3572 | } |
3573 | ||
3574 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
3575 | hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3576 | old_tab = bfd_malloc (tabsize + entsize + hashsize); | |
3577 | if (old_tab == NULL) | |
3578 | goto error_free_vers; | |
3579 | ||
3580 | /* Remember the current objalloc pointer, so that all mem for | |
3581 | symbols added can later be reclaimed. */ | |
3582 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
3583 | if (alloc_mark == NULL) | |
3584 | goto error_free_vers; | |
3585 | ||
5061a885 AM |
3586 | /* Make a special call to the linker "notice" function to |
3587 | tell it that we are about to handle an as-needed lib. */ | |
3588 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
3589 | notice_as_needed)) | |
3590 | return FALSE; | |
3591 | ||
3592 | ||
66eb6687 AM |
3593 | /* Clone the symbol table and sym hashes. Remember some |
3594 | pointers into the symbol table, and dynamic symbol count. */ | |
3595 | old_hash = (char *) old_tab + tabsize; | |
3596 | old_ent = (char *) old_hash + hashsize; | |
3597 | memcpy (old_tab, htab->root.table.table, tabsize); | |
3598 | memcpy (old_hash, sym_hash, hashsize); | |
3599 | old_undefs = htab->root.undefs; | |
3600 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
3601 | old_table = htab->root.table.table; |
3602 | old_size = htab->root.table.size; | |
3603 | old_count = htab->root.table.count; | |
66eb6687 AM |
3604 | old_dynsymcount = htab->dynsymcount; |
3605 | ||
3606 | for (i = 0; i < htab->root.table.size; i++) | |
3607 | { | |
3608 | struct bfd_hash_entry *p; | |
2de92251 | 3609 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3610 | |
3611 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
3612 | { | |
3613 | memcpy (old_ent, p, htab->root.table.entsize); | |
3614 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
3615 | h = (struct elf_link_hash_entry *) p; |
3616 | if (h->root.type == bfd_link_hash_warning) | |
3617 | { | |
3618 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
3619 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
3620 | } | |
66eb6687 AM |
3621 | } |
3622 | } | |
3623 | } | |
4ad4eba5 | 3624 | |
66eb6687 | 3625 | weaks = NULL; |
4ad4eba5 AM |
3626 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
3627 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3628 | isym < isymend; | |
3629 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3630 | { | |
3631 | int bind; | |
3632 | bfd_vma value; | |
af44c138 | 3633 | asection *sec, *new_sec; |
4ad4eba5 AM |
3634 | flagword flags; |
3635 | const char *name; | |
3636 | struct elf_link_hash_entry *h; | |
3637 | bfd_boolean definition; | |
3638 | bfd_boolean size_change_ok; | |
3639 | bfd_boolean type_change_ok; | |
3640 | bfd_boolean new_weakdef; | |
3641 | bfd_boolean override; | |
a4d8e49b | 3642 | bfd_boolean common; |
4ad4eba5 AM |
3643 | unsigned int old_alignment; |
3644 | bfd *old_bfd; | |
3645 | ||
3646 | override = FALSE; | |
3647 | ||
3648 | flags = BSF_NO_FLAGS; | |
3649 | sec = NULL; | |
3650 | value = isym->st_value; | |
3651 | *sym_hash = NULL; | |
a4d8e49b | 3652 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3653 | |
3654 | bind = ELF_ST_BIND (isym->st_info); | |
3655 | if (bind == STB_LOCAL) | |
3656 | { | |
3657 | /* This should be impossible, since ELF requires that all | |
3658 | global symbols follow all local symbols, and that sh_info | |
3659 | point to the first global symbol. Unfortunately, Irix 5 | |
3660 | screws this up. */ | |
3661 | continue; | |
3662 | } | |
3663 | else if (bind == STB_GLOBAL) | |
3664 | { | |
a4d8e49b | 3665 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 AM |
3666 | flags = BSF_GLOBAL; |
3667 | } | |
3668 | else if (bind == STB_WEAK) | |
3669 | flags = BSF_WEAK; | |
3670 | else | |
3671 | { | |
3672 | /* Leave it up to the processor backend. */ | |
3673 | } | |
3674 | ||
3675 | if (isym->st_shndx == SHN_UNDEF) | |
3676 | sec = bfd_und_section_ptr; | |
66eb6687 AM |
3677 | else if (isym->st_shndx < SHN_LORESERVE |
3678 | || isym->st_shndx > SHN_HIRESERVE) | |
4ad4eba5 AM |
3679 | { |
3680 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3681 | if (sec == NULL) | |
3682 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3683 | else if (sec->kept_section) |
3684 | { | |
e5d08002 L |
3685 | /* Symbols from discarded section are undefined. We keep |
3686 | its visibility. */ | |
529fcb95 PB |
3687 | sec = bfd_und_section_ptr; |
3688 | isym->st_shndx = SHN_UNDEF; | |
3689 | } | |
4ad4eba5 AM |
3690 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3691 | value -= sec->vma; | |
3692 | } | |
3693 | else if (isym->st_shndx == SHN_ABS) | |
3694 | sec = bfd_abs_section_ptr; | |
3695 | else if (isym->st_shndx == SHN_COMMON) | |
3696 | { | |
3697 | sec = bfd_com_section_ptr; | |
3698 | /* What ELF calls the size we call the value. What ELF | |
3699 | calls the value we call the alignment. */ | |
3700 | value = isym->st_size; | |
3701 | } | |
3702 | else | |
3703 | { | |
3704 | /* Leave it up to the processor backend. */ | |
3705 | } | |
3706 | ||
3707 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3708 | isym->st_name); | |
3709 | if (name == NULL) | |
3710 | goto error_free_vers; | |
3711 | ||
3712 | if (isym->st_shndx == SHN_COMMON | |
6a4a0940 JJ |
3713 | && ELF_ST_TYPE (isym->st_info) == STT_TLS |
3714 | && !info->relocatable) | |
4ad4eba5 AM |
3715 | { |
3716 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3717 | ||
3718 | if (tcomm == NULL) | |
3719 | { | |
3496cb2a L |
3720 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3721 | (SEC_ALLOC | |
3722 | | SEC_IS_COMMON | |
3723 | | SEC_LINKER_CREATED | |
3724 | | SEC_THREAD_LOCAL)); | |
3725 | if (tcomm == NULL) | |
4ad4eba5 AM |
3726 | goto error_free_vers; |
3727 | } | |
3728 | sec = tcomm; | |
3729 | } | |
66eb6687 | 3730 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 3731 | { |
66eb6687 AM |
3732 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
3733 | &sec, &value)) | |
4ad4eba5 AM |
3734 | goto error_free_vers; |
3735 | ||
3736 | /* The hook function sets the name to NULL if this symbol | |
3737 | should be skipped for some reason. */ | |
3738 | if (name == NULL) | |
3739 | continue; | |
3740 | } | |
3741 | ||
3742 | /* Sanity check that all possibilities were handled. */ | |
3743 | if (sec == NULL) | |
3744 | { | |
3745 | bfd_set_error (bfd_error_bad_value); | |
3746 | goto error_free_vers; | |
3747 | } | |
3748 | ||
3749 | if (bfd_is_und_section (sec) | |
3750 | || bfd_is_com_section (sec)) | |
3751 | definition = FALSE; | |
3752 | else | |
3753 | definition = TRUE; | |
3754 | ||
3755 | size_change_ok = FALSE; | |
66eb6687 | 3756 | type_change_ok = bed->type_change_ok; |
4ad4eba5 AM |
3757 | old_alignment = 0; |
3758 | old_bfd = NULL; | |
af44c138 | 3759 | new_sec = sec; |
4ad4eba5 | 3760 | |
66eb6687 | 3761 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
3762 | { |
3763 | Elf_Internal_Versym iver; | |
3764 | unsigned int vernum = 0; | |
3765 | bfd_boolean skip; | |
3766 | ||
fc0e6df6 | 3767 | if (ever == NULL) |
4ad4eba5 | 3768 | { |
fc0e6df6 PB |
3769 | if (info->default_imported_symver) |
3770 | /* Use the default symbol version created earlier. */ | |
3771 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3772 | else | |
3773 | iver.vs_vers = 0; | |
3774 | } | |
3775 | else | |
3776 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3777 | ||
3778 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3779 | ||
3780 | /* If this is a hidden symbol, or if it is not version | |
3781 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
3782 | However, we do not modify a non-hidden absolute symbol |
3783 | if it is not a function, because it might be the version | |
3784 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 3785 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
cc86ff91 EB |
3786 | || (vernum > 1 && (! bfd_is_abs_section (sec) |
3787 | || ELF_ST_TYPE (isym->st_info) == STT_FUNC))) | |
fc0e6df6 PB |
3788 | { |
3789 | const char *verstr; | |
3790 | size_t namelen, verlen, newlen; | |
3791 | char *newname, *p; | |
3792 | ||
3793 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3794 | { |
fc0e6df6 PB |
3795 | if (vernum > elf_tdata (abfd)->cverdefs) |
3796 | verstr = NULL; | |
3797 | else if (vernum > 1) | |
3798 | verstr = | |
3799 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3800 | else | |
3801 | verstr = ""; | |
4ad4eba5 | 3802 | |
fc0e6df6 | 3803 | if (verstr == NULL) |
4ad4eba5 | 3804 | { |
fc0e6df6 PB |
3805 | (*_bfd_error_handler) |
3806 | (_("%B: %s: invalid version %u (max %d)"), | |
3807 | abfd, name, vernum, | |
3808 | elf_tdata (abfd)->cverdefs); | |
3809 | bfd_set_error (bfd_error_bad_value); | |
3810 | goto error_free_vers; | |
4ad4eba5 | 3811 | } |
fc0e6df6 PB |
3812 | } |
3813 | else | |
3814 | { | |
3815 | /* We cannot simply test for the number of | |
3816 | entries in the VERNEED section since the | |
3817 | numbers for the needed versions do not start | |
3818 | at 0. */ | |
3819 | Elf_Internal_Verneed *t; | |
3820 | ||
3821 | verstr = NULL; | |
3822 | for (t = elf_tdata (abfd)->verref; | |
3823 | t != NULL; | |
3824 | t = t->vn_nextref) | |
4ad4eba5 | 3825 | { |
fc0e6df6 | 3826 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3827 | |
fc0e6df6 PB |
3828 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3829 | { | |
3830 | if (a->vna_other == vernum) | |
4ad4eba5 | 3831 | { |
fc0e6df6 PB |
3832 | verstr = a->vna_nodename; |
3833 | break; | |
4ad4eba5 | 3834 | } |
4ad4eba5 | 3835 | } |
fc0e6df6 PB |
3836 | if (a != NULL) |
3837 | break; | |
3838 | } | |
3839 | if (verstr == NULL) | |
3840 | { | |
3841 | (*_bfd_error_handler) | |
3842 | (_("%B: %s: invalid needed version %d"), | |
3843 | abfd, name, vernum); | |
3844 | bfd_set_error (bfd_error_bad_value); | |
3845 | goto error_free_vers; | |
4ad4eba5 | 3846 | } |
4ad4eba5 | 3847 | } |
fc0e6df6 PB |
3848 | |
3849 | namelen = strlen (name); | |
3850 | verlen = strlen (verstr); | |
3851 | newlen = namelen + verlen + 2; | |
3852 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3853 | && isym->st_shndx != SHN_UNDEF) | |
3854 | ++newlen; | |
3855 | ||
66eb6687 | 3856 | newname = bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
3857 | if (newname == NULL) |
3858 | goto error_free_vers; | |
3859 | memcpy (newname, name, namelen); | |
3860 | p = newname + namelen; | |
3861 | *p++ = ELF_VER_CHR; | |
3862 | /* If this is a defined non-hidden version symbol, | |
3863 | we add another @ to the name. This indicates the | |
3864 | default version of the symbol. */ | |
3865 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3866 | && isym->st_shndx != SHN_UNDEF) | |
3867 | *p++ = ELF_VER_CHR; | |
3868 | memcpy (p, verstr, verlen + 1); | |
3869 | ||
3870 | name = newname; | |
4ad4eba5 AM |
3871 | } |
3872 | ||
af44c138 L |
3873 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
3874 | &value, &old_alignment, | |
4ad4eba5 AM |
3875 | sym_hash, &skip, &override, |
3876 | &type_change_ok, &size_change_ok)) | |
3877 | goto error_free_vers; | |
3878 | ||
3879 | if (skip) | |
3880 | continue; | |
3881 | ||
3882 | if (override) | |
3883 | definition = FALSE; | |
3884 | ||
3885 | h = *sym_hash; | |
3886 | while (h->root.type == bfd_link_hash_indirect | |
3887 | || h->root.type == bfd_link_hash_warning) | |
3888 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3889 | ||
3890 | /* Remember the old alignment if this is a common symbol, so | |
3891 | that we don't reduce the alignment later on. We can't | |
3892 | check later, because _bfd_generic_link_add_one_symbol | |
3893 | will set a default for the alignment which we want to | |
3894 | override. We also remember the old bfd where the existing | |
3895 | definition comes from. */ | |
3896 | switch (h->root.type) | |
3897 | { | |
3898 | default: | |
3899 | break; | |
3900 | ||
3901 | case bfd_link_hash_defined: | |
3902 | case bfd_link_hash_defweak: | |
3903 | old_bfd = h->root.u.def.section->owner; | |
3904 | break; | |
3905 | ||
3906 | case bfd_link_hash_common: | |
3907 | old_bfd = h->root.u.c.p->section->owner; | |
3908 | old_alignment = h->root.u.c.p->alignment_power; | |
3909 | break; | |
3910 | } | |
3911 | ||
3912 | if (elf_tdata (abfd)->verdef != NULL | |
3913 | && ! override | |
3914 | && vernum > 1 | |
3915 | && definition) | |
3916 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3917 | } | |
3918 | ||
3919 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 3920 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
3921 | (struct bfd_link_hash_entry **) sym_hash))) |
3922 | goto error_free_vers; | |
3923 | ||
3924 | h = *sym_hash; | |
3925 | while (h->root.type == bfd_link_hash_indirect | |
3926 | || h->root.type == bfd_link_hash_warning) | |
3927 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3928 | *sym_hash = h; | |
3929 | ||
3930 | new_weakdef = FALSE; | |
3931 | if (dynamic | |
3932 | && definition | |
3933 | && (flags & BSF_WEAK) != 0 | |
3934 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
66eb6687 | 3935 | && is_elf_hash_table (htab) |
f6e332e6 | 3936 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3937 | { |
3938 | /* Keep a list of all weak defined non function symbols from | |
3939 | a dynamic object, using the weakdef field. Later in this | |
3940 | function we will set the weakdef field to the correct | |
3941 | value. We only put non-function symbols from dynamic | |
3942 | objects on this list, because that happens to be the only | |
3943 | time we need to know the normal symbol corresponding to a | |
3944 | weak symbol, and the information is time consuming to | |
3945 | figure out. If the weakdef field is not already NULL, | |
3946 | then this symbol was already defined by some previous | |
3947 | dynamic object, and we will be using that previous | |
3948 | definition anyhow. */ | |
3949 | ||
f6e332e6 | 3950 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3951 | weaks = h; |
3952 | new_weakdef = TRUE; | |
3953 | } | |
3954 | ||
3955 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 3956 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
3957 | && h->root.type == bfd_link_hash_common) |
3958 | { | |
3959 | unsigned int align; | |
3960 | ||
a4d8e49b | 3961 | if (common) |
af44c138 L |
3962 | align = bfd_log2 (isym->st_value); |
3963 | else | |
3964 | { | |
3965 | /* The new symbol is a common symbol in a shared object. | |
3966 | We need to get the alignment from the section. */ | |
3967 | align = new_sec->alignment_power; | |
3968 | } | |
4ad4eba5 AM |
3969 | if (align > old_alignment |
3970 | /* Permit an alignment power of zero if an alignment of one | |
3971 | is specified and no other alignments have been specified. */ | |
3972 | || (isym->st_value == 1 && old_alignment == 0)) | |
3973 | h->root.u.c.p->alignment_power = align; | |
3974 | else | |
3975 | h->root.u.c.p->alignment_power = old_alignment; | |
3976 | } | |
3977 | ||
66eb6687 | 3978 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 3979 | { |
4ad4eba5 | 3980 | bfd_boolean dynsym; |
4ad4eba5 AM |
3981 | |
3982 | /* Check the alignment when a common symbol is involved. This | |
3983 | can change when a common symbol is overridden by a normal | |
3984 | definition or a common symbol is ignored due to the old | |
3985 | normal definition. We need to make sure the maximum | |
3986 | alignment is maintained. */ | |
a4d8e49b | 3987 | if ((old_alignment || common) |
4ad4eba5 AM |
3988 | && h->root.type != bfd_link_hash_common) |
3989 | { | |
3990 | unsigned int common_align; | |
3991 | unsigned int normal_align; | |
3992 | unsigned int symbol_align; | |
3993 | bfd *normal_bfd; | |
3994 | bfd *common_bfd; | |
3995 | ||
3996 | symbol_align = ffs (h->root.u.def.value) - 1; | |
3997 | if (h->root.u.def.section->owner != NULL | |
3998 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
3999 | { | |
4000 | normal_align = h->root.u.def.section->alignment_power; | |
4001 | if (normal_align > symbol_align) | |
4002 | normal_align = symbol_align; | |
4003 | } | |
4004 | else | |
4005 | normal_align = symbol_align; | |
4006 | ||
4007 | if (old_alignment) | |
4008 | { | |
4009 | common_align = old_alignment; | |
4010 | common_bfd = old_bfd; | |
4011 | normal_bfd = abfd; | |
4012 | } | |
4013 | else | |
4014 | { | |
4015 | common_align = bfd_log2 (isym->st_value); | |
4016 | common_bfd = abfd; | |
4017 | normal_bfd = old_bfd; | |
4018 | } | |
4019 | ||
4020 | if (normal_align < common_align) | |
d07676f8 NC |
4021 | { |
4022 | /* PR binutils/2735 */ | |
4023 | if (normal_bfd == NULL) | |
4024 | (*_bfd_error_handler) | |
4025 | (_("Warning: alignment %u of common symbol `%s' in %B" | |
4026 | " is greater than the alignment (%u) of its section %A"), | |
4027 | common_bfd, h->root.u.def.section, | |
4028 | 1 << common_align, name, 1 << normal_align); | |
4029 | else | |
4030 | (*_bfd_error_handler) | |
4031 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4032 | " is smaller than %u in %B"), | |
4033 | normal_bfd, common_bfd, | |
4034 | 1 << normal_align, name, 1 << common_align); | |
4035 | } | |
4ad4eba5 AM |
4036 | } |
4037 | ||
4038 | /* Remember the symbol size and type. */ | |
4039 | if (isym->st_size != 0 | |
4040 | && (definition || h->size == 0)) | |
4041 | { | |
4042 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
4043 | (*_bfd_error_handler) | |
d003868e AM |
4044 | (_("Warning: size of symbol `%s' changed" |
4045 | " from %lu in %B to %lu in %B"), | |
4046 | old_bfd, abfd, | |
4ad4eba5 | 4047 | name, (unsigned long) h->size, |
d003868e | 4048 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4049 | |
4050 | h->size = isym->st_size; | |
4051 | } | |
4052 | ||
4053 | /* If this is a common symbol, then we always want H->SIZE | |
4054 | to be the size of the common symbol. The code just above | |
4055 | won't fix the size if a common symbol becomes larger. We | |
4056 | don't warn about a size change here, because that is | |
4057 | covered by --warn-common. */ | |
4058 | if (h->root.type == bfd_link_hash_common) | |
4059 | h->size = h->root.u.c.size; | |
4060 | ||
4061 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
4062 | && (definition || h->type == STT_NOTYPE)) | |
4063 | { | |
4064 | if (h->type != STT_NOTYPE | |
4065 | && h->type != ELF_ST_TYPE (isym->st_info) | |
4066 | && ! type_change_ok) | |
4067 | (*_bfd_error_handler) | |
d003868e AM |
4068 | (_("Warning: type of symbol `%s' changed" |
4069 | " from %d to %d in %B"), | |
4070 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
4071 | |
4072 | h->type = ELF_ST_TYPE (isym->st_info); | |
4073 | } | |
4074 | ||
4075 | /* If st_other has a processor-specific meaning, specific | |
4076 | code might be needed here. We never merge the visibility | |
4077 | attribute with the one from a dynamic object. */ | |
4078 | if (bed->elf_backend_merge_symbol_attribute) | |
4079 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
4080 | dynamic); | |
4081 | ||
b58f81ae DJ |
4082 | /* If this symbol has default visibility and the user has requested |
4083 | we not re-export it, then mark it as hidden. */ | |
4084 | if (definition && !dynamic | |
4085 | && (abfd->no_export | |
4086 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
4087 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
66eb6687 AM |
4088 | isym->st_other = (STV_HIDDEN |
4089 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
b58f81ae | 4090 | |
8992f0d7 | 4091 | if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic) |
4ad4eba5 AM |
4092 | { |
4093 | unsigned char hvis, symvis, other, nvis; | |
4094 | ||
8992f0d7 TS |
4095 | /* Only merge the visibility. Leave the remainder of the |
4096 | st_other field to elf_backend_merge_symbol_attribute. */ | |
4097 | other = h->other & ~ELF_ST_VISIBILITY (-1); | |
4ad4eba5 AM |
4098 | |
4099 | /* Combine visibilities, using the most constraining one. */ | |
4100 | hvis = ELF_ST_VISIBILITY (h->other); | |
4101 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4102 | if (! hvis) | |
4103 | nvis = symvis; | |
4104 | else if (! symvis) | |
4105 | nvis = hvis; | |
4106 | else | |
4107 | nvis = hvis < symvis ? hvis : symvis; | |
4108 | ||
4109 | h->other = other | nvis; | |
4110 | } | |
4111 | ||
4112 | /* Set a flag in the hash table entry indicating the type of | |
4113 | reference or definition we just found. Keep a count of | |
4114 | the number of dynamic symbols we find. A dynamic symbol | |
4115 | is one which is referenced or defined by both a regular | |
4116 | object and a shared object. */ | |
4ad4eba5 AM |
4117 | dynsym = FALSE; |
4118 | if (! dynamic) | |
4119 | { | |
4120 | if (! definition) | |
4121 | { | |
f5385ebf | 4122 | h->ref_regular = 1; |
4ad4eba5 | 4123 | if (bind != STB_WEAK) |
f5385ebf | 4124 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4125 | } |
4126 | else | |
f5385ebf | 4127 | h->def_regular = 1; |
4ad4eba5 | 4128 | if (! info->executable |
f5385ebf AM |
4129 | || h->def_dynamic |
4130 | || h->ref_dynamic) | |
4ad4eba5 AM |
4131 | dynsym = TRUE; |
4132 | } | |
4133 | else | |
4134 | { | |
4135 | if (! definition) | |
f5385ebf | 4136 | h->ref_dynamic = 1; |
4ad4eba5 | 4137 | else |
f5385ebf AM |
4138 | h->def_dynamic = 1; |
4139 | if (h->def_regular | |
4140 | || h->ref_regular | |
f6e332e6 | 4141 | || (h->u.weakdef != NULL |
4ad4eba5 | 4142 | && ! new_weakdef |
f6e332e6 | 4143 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4144 | dynsym = TRUE; |
4145 | } | |
4146 | ||
92b7c7b6 L |
4147 | if (definition && (sec->flags & SEC_DEBUGGING)) |
4148 | { | |
4149 | /* We don't want to make debug symbol dynamic. */ | |
4150 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4151 | dynsym = FALSE; | |
4152 | } | |
4153 | ||
4ad4eba5 AM |
4154 | /* Check to see if we need to add an indirect symbol for |
4155 | the default name. */ | |
4156 | if (definition || h->root.type == bfd_link_hash_common) | |
4157 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4158 | &sec, &value, &dynsym, | |
4159 | override)) | |
4160 | goto error_free_vers; | |
4161 | ||
4162 | if (definition && !dynamic) | |
4163 | { | |
4164 | char *p = strchr (name, ELF_VER_CHR); | |
4165 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4166 | { | |
4167 | /* Queue non-default versions so that .symver x, x@FOO | |
4168 | aliases can be checked. */ | |
66eb6687 | 4169 | if (!nondeflt_vers) |
4ad4eba5 | 4170 | { |
66eb6687 AM |
4171 | amt = ((isymend - isym + 1) |
4172 | * sizeof (struct elf_link_hash_entry *)); | |
4ad4eba5 AM |
4173 | nondeflt_vers = bfd_malloc (amt); |
4174 | } | |
66eb6687 | 4175 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4176 | } |
4177 | } | |
4178 | ||
4179 | if (dynsym && h->dynindx == -1) | |
4180 | { | |
c152c796 | 4181 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4182 | goto error_free_vers; |
f6e332e6 | 4183 | if (h->u.weakdef != NULL |
4ad4eba5 | 4184 | && ! new_weakdef |
f6e332e6 | 4185 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4186 | { |
66eb6687 | 4187 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4188 | goto error_free_vers; |
4189 | } | |
4190 | } | |
4191 | else if (dynsym && h->dynindx != -1) | |
4192 | /* If the symbol already has a dynamic index, but | |
4193 | visibility says it should not be visible, turn it into | |
4194 | a local symbol. */ | |
4195 | switch (ELF_ST_VISIBILITY (h->other)) | |
4196 | { | |
4197 | case STV_INTERNAL: | |
4198 | case STV_HIDDEN: | |
4199 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4200 | dynsym = FALSE; | |
4201 | break; | |
4202 | } | |
4203 | ||
4204 | if (!add_needed | |
4205 | && definition | |
4206 | && dynsym | |
f5385ebf | 4207 | && h->ref_regular) |
4ad4eba5 AM |
4208 | { |
4209 | int ret; | |
4210 | const char *soname = elf_dt_name (abfd); | |
4211 | ||
4212 | /* A symbol from a library loaded via DT_NEEDED of some | |
4213 | other library is referenced by a regular object. | |
e56f61be L |
4214 | Add a DT_NEEDED entry for it. Issue an error if |
4215 | --no-add-needed is used. */ | |
4216 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4217 | { | |
4218 | (*_bfd_error_handler) | |
4219 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4220 | abfd, name); |
e56f61be L |
4221 | bfd_set_error (bfd_error_bad_value); |
4222 | goto error_free_vers; | |
4223 | } | |
4224 | ||
a5db907e AM |
4225 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4226 | ||
4ad4eba5 | 4227 | add_needed = TRUE; |
7e9f0867 | 4228 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4229 | if (ret < 0) |
4230 | goto error_free_vers; | |
4231 | ||
4232 | BFD_ASSERT (ret == 0); | |
4233 | } | |
4234 | } | |
4235 | } | |
4236 | ||
66eb6687 AM |
4237 | if (extversym != NULL) |
4238 | { | |
4239 | free (extversym); | |
4240 | extversym = NULL; | |
4241 | } | |
4242 | ||
4243 | if (isymbuf != NULL) | |
4244 | { | |
4245 | free (isymbuf); | |
4246 | isymbuf = NULL; | |
4247 | } | |
4248 | ||
4249 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4250 | { | |
4251 | unsigned int i; | |
4252 | ||
4253 | /* Restore the symbol table. */ | |
97fed1c9 JJ |
4254 | if (bed->as_needed_cleanup) |
4255 | (*bed->as_needed_cleanup) (abfd, info); | |
66eb6687 AM |
4256 | old_hash = (char *) old_tab + tabsize; |
4257 | old_ent = (char *) old_hash + hashsize; | |
4258 | sym_hash = elf_sym_hashes (abfd); | |
4f87808c AM |
4259 | htab->root.table.table = old_table; |
4260 | htab->root.table.size = old_size; | |
4261 | htab->root.table.count = old_count; | |
66eb6687 AM |
4262 | memcpy (htab->root.table.table, old_tab, tabsize); |
4263 | memcpy (sym_hash, old_hash, hashsize); | |
4264 | htab->root.undefs = old_undefs; | |
4265 | htab->root.undefs_tail = old_undefs_tail; | |
4266 | for (i = 0; i < htab->root.table.size; i++) | |
4267 | { | |
4268 | struct bfd_hash_entry *p; | |
4269 | struct elf_link_hash_entry *h; | |
4270 | ||
4271 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4272 | { | |
4273 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4274 | if (h->root.type == bfd_link_hash_warning) |
4275 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
66eb6687 AM |
4276 | if (h->dynindx >= old_dynsymcount) |
4277 | _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); | |
2de92251 | 4278 | |
66eb6687 AM |
4279 | memcpy (p, old_ent, htab->root.table.entsize); |
4280 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4281 | h = (struct elf_link_hash_entry *) p; |
4282 | if (h->root.type == bfd_link_hash_warning) | |
4283 | { | |
4284 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4285 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4286 | } | |
66eb6687 AM |
4287 | } |
4288 | } | |
4289 | ||
5061a885 AM |
4290 | /* Make a special call to the linker "notice" function to |
4291 | tell it that symbols added for crefs may need to be removed. */ | |
4292 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
4293 | notice_not_needed)) | |
4294 | return FALSE; | |
4295 | ||
66eb6687 AM |
4296 | free (old_tab); |
4297 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4298 | alloc_mark); | |
4299 | if (nondeflt_vers != NULL) | |
4300 | free (nondeflt_vers); | |
4301 | return TRUE; | |
4302 | } | |
2de92251 | 4303 | |
66eb6687 AM |
4304 | if (old_tab != NULL) |
4305 | { | |
5061a885 AM |
4306 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, |
4307 | notice_needed)) | |
4308 | return FALSE; | |
66eb6687 AM |
4309 | free (old_tab); |
4310 | old_tab = NULL; | |
4311 | } | |
4312 | ||
4ad4eba5 AM |
4313 | /* Now that all the symbols from this input file are created, handle |
4314 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4315 | if (nondeflt_vers != NULL) | |
4316 | { | |
4317 | bfd_size_type cnt, symidx; | |
4318 | ||
4319 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4320 | { | |
4321 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4322 | char *shortname, *p; | |
4323 | ||
4324 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4325 | if (p == NULL | |
4326 | || (h->root.type != bfd_link_hash_defined | |
4327 | && h->root.type != bfd_link_hash_defweak)) | |
4328 | continue; | |
4329 | ||
4330 | amt = p - h->root.root.string; | |
4331 | shortname = bfd_malloc (amt + 1); | |
4332 | memcpy (shortname, h->root.root.string, amt); | |
4333 | shortname[amt] = '\0'; | |
4334 | ||
4335 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4336 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4337 | FALSE, FALSE, FALSE); |
4338 | if (hi != NULL | |
4339 | && hi->root.type == h->root.type | |
4340 | && hi->root.u.def.value == h->root.u.def.value | |
4341 | && hi->root.u.def.section == h->root.u.def.section) | |
4342 | { | |
4343 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4344 | hi->root.type = bfd_link_hash_indirect; | |
4345 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4346 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4347 | sym_hash = elf_sym_hashes (abfd); |
4348 | if (sym_hash) | |
4349 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4350 | if (sym_hash[symidx] == hi) | |
4351 | { | |
4352 | sym_hash[symidx] = h; | |
4353 | break; | |
4354 | } | |
4355 | } | |
4356 | free (shortname); | |
4357 | } | |
4358 | free (nondeflt_vers); | |
4359 | nondeflt_vers = NULL; | |
4360 | } | |
4361 | ||
4ad4eba5 AM |
4362 | /* Now set the weakdefs field correctly for all the weak defined |
4363 | symbols we found. The only way to do this is to search all the | |
4364 | symbols. Since we only need the information for non functions in | |
4365 | dynamic objects, that's the only time we actually put anything on | |
4366 | the list WEAKS. We need this information so that if a regular | |
4367 | object refers to a symbol defined weakly in a dynamic object, the | |
4368 | real symbol in the dynamic object is also put in the dynamic | |
4369 | symbols; we also must arrange for both symbols to point to the | |
4370 | same memory location. We could handle the general case of symbol | |
4371 | aliasing, but a general symbol alias can only be generated in | |
4372 | assembler code, handling it correctly would be very time | |
4373 | consuming, and other ELF linkers don't handle general aliasing | |
4374 | either. */ | |
4375 | if (weaks != NULL) | |
4376 | { | |
4377 | struct elf_link_hash_entry **hpp; | |
4378 | struct elf_link_hash_entry **hppend; | |
4379 | struct elf_link_hash_entry **sorted_sym_hash; | |
4380 | struct elf_link_hash_entry *h; | |
4381 | size_t sym_count; | |
4382 | ||
4383 | /* Since we have to search the whole symbol list for each weak | |
4384 | defined symbol, search time for N weak defined symbols will be | |
4385 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4386 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4387 | sorted_sym_hash = bfd_malloc (amt); | |
4388 | if (sorted_sym_hash == NULL) | |
4389 | goto error_return; | |
4390 | sym_hash = sorted_sym_hash; | |
4391 | hpp = elf_sym_hashes (abfd); | |
4392 | hppend = hpp + extsymcount; | |
4393 | sym_count = 0; | |
4394 | for (; hpp < hppend; hpp++) | |
4395 | { | |
4396 | h = *hpp; | |
4397 | if (h != NULL | |
4398 | && h->root.type == bfd_link_hash_defined | |
4399 | && h->type != STT_FUNC) | |
4400 | { | |
4401 | *sym_hash = h; | |
4402 | sym_hash++; | |
4403 | sym_count++; | |
4404 | } | |
4405 | } | |
4406 | ||
4407 | qsort (sorted_sym_hash, sym_count, | |
4408 | sizeof (struct elf_link_hash_entry *), | |
4409 | elf_sort_symbol); | |
4410 | ||
4411 | while (weaks != NULL) | |
4412 | { | |
4413 | struct elf_link_hash_entry *hlook; | |
4414 | asection *slook; | |
4415 | bfd_vma vlook; | |
4416 | long ilook; | |
4417 | size_t i, j, idx; | |
4418 | ||
4419 | hlook = weaks; | |
f6e332e6 AM |
4420 | weaks = hlook->u.weakdef; |
4421 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4422 | |
4423 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4424 | || hlook->root.type == bfd_link_hash_defweak | |
4425 | || hlook->root.type == bfd_link_hash_common | |
4426 | || hlook->root.type == bfd_link_hash_indirect); | |
4427 | slook = hlook->root.u.def.section; | |
4428 | vlook = hlook->root.u.def.value; | |
4429 | ||
4430 | ilook = -1; | |
4431 | i = 0; | |
4432 | j = sym_count; | |
4433 | while (i < j) | |
4434 | { | |
4435 | bfd_signed_vma vdiff; | |
4436 | idx = (i + j) / 2; | |
4437 | h = sorted_sym_hash [idx]; | |
4438 | vdiff = vlook - h->root.u.def.value; | |
4439 | if (vdiff < 0) | |
4440 | j = idx; | |
4441 | else if (vdiff > 0) | |
4442 | i = idx + 1; | |
4443 | else | |
4444 | { | |
a9b881be | 4445 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4446 | if (sdiff < 0) |
4447 | j = idx; | |
4448 | else if (sdiff > 0) | |
4449 | i = idx + 1; | |
4450 | else | |
4451 | { | |
4452 | ilook = idx; | |
4453 | break; | |
4454 | } | |
4455 | } | |
4456 | } | |
4457 | ||
4458 | /* We didn't find a value/section match. */ | |
4459 | if (ilook == -1) | |
4460 | continue; | |
4461 | ||
4462 | for (i = ilook; i < sym_count; i++) | |
4463 | { | |
4464 | h = sorted_sym_hash [i]; | |
4465 | ||
4466 | /* Stop if value or section doesn't match. */ | |
4467 | if (h->root.u.def.value != vlook | |
4468 | || h->root.u.def.section != slook) | |
4469 | break; | |
4470 | else if (h != hlook) | |
4471 | { | |
f6e332e6 | 4472 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4473 | |
4474 | /* If the weak definition is in the list of dynamic | |
4475 | symbols, make sure the real definition is put | |
4476 | there as well. */ | |
4477 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4478 | { | |
c152c796 | 4479 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4480 | goto error_return; |
4481 | } | |
4482 | ||
4483 | /* If the real definition is in the list of dynamic | |
4484 | symbols, make sure the weak definition is put | |
4485 | there as well. If we don't do this, then the | |
4486 | dynamic loader might not merge the entries for the | |
4487 | real definition and the weak definition. */ | |
4488 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4489 | { | |
c152c796 | 4490 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4491 | goto error_return; |
4492 | } | |
4493 | break; | |
4494 | } | |
4495 | } | |
4496 | } | |
4497 | ||
4498 | free (sorted_sym_hash); | |
4499 | } | |
4500 | ||
66eb6687 AM |
4501 | if (bed->check_directives) |
4502 | (*bed->check_directives) (abfd, info); | |
85fbca6a | 4503 | |
4ad4eba5 AM |
4504 | /* If this object is the same format as the output object, and it is |
4505 | not a shared library, then let the backend look through the | |
4506 | relocs. | |
4507 | ||
4508 | This is required to build global offset table entries and to | |
4509 | arrange for dynamic relocs. It is not required for the | |
4510 | particular common case of linking non PIC code, even when linking | |
4511 | against shared libraries, but unfortunately there is no way of | |
4512 | knowing whether an object file has been compiled PIC or not. | |
4513 | Looking through the relocs is not particularly time consuming. | |
4514 | The problem is that we must either (1) keep the relocs in memory, | |
4515 | which causes the linker to require additional runtime memory or | |
4516 | (2) read the relocs twice from the input file, which wastes time. | |
4517 | This would be a good case for using mmap. | |
4518 | ||
4519 | I have no idea how to handle linking PIC code into a file of a | |
4520 | different format. It probably can't be done. */ | |
4ad4eba5 | 4521 | if (! dynamic |
66eb6687 AM |
4522 | && is_elf_hash_table (htab) |
4523 | && htab->root.creator == abfd->xvec | |
4524 | && bed->check_relocs != NULL) | |
4ad4eba5 AM |
4525 | { |
4526 | asection *o; | |
4527 | ||
4528 | for (o = abfd->sections; o != NULL; o = o->next) | |
4529 | { | |
4530 | Elf_Internal_Rela *internal_relocs; | |
4531 | bfd_boolean ok; | |
4532 | ||
4533 | if ((o->flags & SEC_RELOC) == 0 | |
4534 | || o->reloc_count == 0 | |
4535 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4536 | && (o->flags & SEC_DEBUGGING) != 0) | |
4537 | || bfd_is_abs_section (o->output_section)) | |
4538 | continue; | |
4539 | ||
4540 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4541 | info->keep_memory); | |
4542 | if (internal_relocs == NULL) | |
4543 | goto error_return; | |
4544 | ||
66eb6687 | 4545 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); |
4ad4eba5 AM |
4546 | |
4547 | if (elf_section_data (o)->relocs != internal_relocs) | |
4548 | free (internal_relocs); | |
4549 | ||
4550 | if (! ok) | |
4551 | goto error_return; | |
4552 | } | |
4553 | } | |
4554 | ||
4555 | /* If this is a non-traditional link, try to optimize the handling | |
4556 | of the .stab/.stabstr sections. */ | |
4557 | if (! dynamic | |
4558 | && ! info->traditional_format | |
66eb6687 | 4559 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
4560 | && (info->strip != strip_all && info->strip != strip_debugger)) |
4561 | { | |
4562 | asection *stabstr; | |
4563 | ||
4564 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4565 | if (stabstr != NULL) | |
4566 | { | |
4567 | bfd_size_type string_offset = 0; | |
4568 | asection *stab; | |
4569 | ||
4570 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 4571 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
4572 | && (!stab->name[5] || |
4573 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4574 | && (stab->flags & SEC_MERGE) == 0 | |
4575 | && !bfd_is_abs_section (stab->output_section)) | |
4576 | { | |
4577 | struct bfd_elf_section_data *secdata; | |
4578 | ||
4579 | secdata = elf_section_data (stab); | |
66eb6687 AM |
4580 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
4581 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
4582 | &string_offset)) |
4583 | goto error_return; | |
4584 | if (secdata->sec_info) | |
4585 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4586 | } | |
4587 | } | |
4588 | } | |
4589 | ||
66eb6687 | 4590 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
4591 | { |
4592 | /* Add this bfd to the loaded list. */ | |
4593 | struct elf_link_loaded_list *n; | |
4594 | ||
4595 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4596 | if (n == NULL) | |
4597 | goto error_return; | |
4598 | n->abfd = abfd; | |
66eb6687 AM |
4599 | n->next = htab->loaded; |
4600 | htab->loaded = n; | |
4ad4eba5 AM |
4601 | } |
4602 | ||
4603 | return TRUE; | |
4604 | ||
4605 | error_free_vers: | |
66eb6687 AM |
4606 | if (old_tab != NULL) |
4607 | free (old_tab); | |
4ad4eba5 AM |
4608 | if (nondeflt_vers != NULL) |
4609 | free (nondeflt_vers); | |
4610 | if (extversym != NULL) | |
4611 | free (extversym); | |
4612 | error_free_sym: | |
4613 | if (isymbuf != NULL) | |
4614 | free (isymbuf); | |
4615 | error_return: | |
4616 | return FALSE; | |
4617 | } | |
4618 | ||
8387904d AM |
4619 | /* Return the linker hash table entry of a symbol that might be |
4620 | satisfied by an archive symbol. Return -1 on error. */ | |
4621 | ||
4622 | struct elf_link_hash_entry * | |
4623 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4624 | struct bfd_link_info *info, | |
4625 | const char *name) | |
4626 | { | |
4627 | struct elf_link_hash_entry *h; | |
4628 | char *p, *copy; | |
4629 | size_t len, first; | |
4630 | ||
4631 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4632 | if (h != NULL) | |
4633 | return h; | |
4634 | ||
4635 | /* If this is a default version (the name contains @@), look up the | |
4636 | symbol again with only one `@' as well as without the version. | |
4637 | The effect is that references to the symbol with and without the | |
4638 | version will be matched by the default symbol in the archive. */ | |
4639 | ||
4640 | p = strchr (name, ELF_VER_CHR); | |
4641 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4642 | return h; | |
4643 | ||
4644 | /* First check with only one `@'. */ | |
4645 | len = strlen (name); | |
4646 | copy = bfd_alloc (abfd, len); | |
4647 | if (copy == NULL) | |
4648 | return (struct elf_link_hash_entry *) 0 - 1; | |
4649 | ||
4650 | first = p - name + 1; | |
4651 | memcpy (copy, name, first); | |
4652 | memcpy (copy + first, name + first + 1, len - first); | |
4653 | ||
4654 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4655 | if (h == NULL) | |
4656 | { | |
4657 | /* We also need to check references to the symbol without the | |
4658 | version. */ | |
4659 | copy[first - 1] = '\0'; | |
4660 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4661 | FALSE, FALSE, FALSE); | |
4662 | } | |
4663 | ||
4664 | bfd_release (abfd, copy); | |
4665 | return h; | |
4666 | } | |
4667 | ||
0ad989f9 L |
4668 | /* Add symbols from an ELF archive file to the linker hash table. We |
4669 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4670 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4671 | archive which includes an entry libc.so.1 which defines a bunch of | |
4672 | symbols. The libc.so archive also includes a number of other | |
4673 | object files, which also define symbols, some of which are the same | |
4674 | as those defined in libc.so.1. Correct linking requires that we | |
4675 | consider each object file in turn, and include it if it defines any | |
4676 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4677 | this; it looks through the list of undefined symbols, and includes | |
4678 | any object file which defines them. When this algorithm is used on | |
4679 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4680 | bunch of symbols. This means that some of the other objects in the | |
4681 | archive are not included in the link, which is incorrect since they | |
4682 | precede libc.so.1 in the archive. | |
4683 | ||
4684 | Fortunately, ELF archive handling is simpler than that done by | |
4685 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4686 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4687 | symbol is currently undefined, we know that we must pull in that | |
4688 | object file. | |
4689 | ||
4690 | Unfortunately, we do have to make multiple passes over the symbol | |
4691 | table until nothing further is resolved. */ | |
4692 | ||
4ad4eba5 AM |
4693 | static bfd_boolean |
4694 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4695 | { |
4696 | symindex c; | |
4697 | bfd_boolean *defined = NULL; | |
4698 | bfd_boolean *included = NULL; | |
4699 | carsym *symdefs; | |
4700 | bfd_boolean loop; | |
4701 | bfd_size_type amt; | |
8387904d AM |
4702 | const struct elf_backend_data *bed; |
4703 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4704 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4705 | |
4706 | if (! bfd_has_map (abfd)) | |
4707 | { | |
4708 | /* An empty archive is a special case. */ | |
4709 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4710 | return TRUE; | |
4711 | bfd_set_error (bfd_error_no_armap); | |
4712 | return FALSE; | |
4713 | } | |
4714 | ||
4715 | /* Keep track of all symbols we know to be already defined, and all | |
4716 | files we know to be already included. This is to speed up the | |
4717 | second and subsequent passes. */ | |
4718 | c = bfd_ardata (abfd)->symdef_count; | |
4719 | if (c == 0) | |
4720 | return TRUE; | |
4721 | amt = c; | |
4722 | amt *= sizeof (bfd_boolean); | |
4723 | defined = bfd_zmalloc (amt); | |
4724 | included = bfd_zmalloc (amt); | |
4725 | if (defined == NULL || included == NULL) | |
4726 | goto error_return; | |
4727 | ||
4728 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4729 | bed = get_elf_backend_data (abfd); |
4730 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4731 | |
4732 | do | |
4733 | { | |
4734 | file_ptr last; | |
4735 | symindex i; | |
4736 | carsym *symdef; | |
4737 | carsym *symdefend; | |
4738 | ||
4739 | loop = FALSE; | |
4740 | last = -1; | |
4741 | ||
4742 | symdef = symdefs; | |
4743 | symdefend = symdef + c; | |
4744 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4745 | { | |
4746 | struct elf_link_hash_entry *h; | |
4747 | bfd *element; | |
4748 | struct bfd_link_hash_entry *undefs_tail; | |
4749 | symindex mark; | |
4750 | ||
4751 | if (defined[i] || included[i]) | |
4752 | continue; | |
4753 | if (symdef->file_offset == last) | |
4754 | { | |
4755 | included[i] = TRUE; | |
4756 | continue; | |
4757 | } | |
4758 | ||
8387904d AM |
4759 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4760 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4761 | goto error_return; | |
0ad989f9 L |
4762 | |
4763 | if (h == NULL) | |
4764 | continue; | |
4765 | ||
4766 | if (h->root.type == bfd_link_hash_common) | |
4767 | { | |
4768 | /* We currently have a common symbol. The archive map contains | |
4769 | a reference to this symbol, so we may want to include it. We | |
4770 | only want to include it however, if this archive element | |
4771 | contains a definition of the symbol, not just another common | |
4772 | declaration of it. | |
4773 | ||
4774 | Unfortunately some archivers (including GNU ar) will put | |
4775 | declarations of common symbols into their archive maps, as | |
4776 | well as real definitions, so we cannot just go by the archive | |
4777 | map alone. Instead we must read in the element's symbol | |
4778 | table and check that to see what kind of symbol definition | |
4779 | this is. */ | |
4780 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4781 | continue; | |
4782 | } | |
4783 | else if (h->root.type != bfd_link_hash_undefined) | |
4784 | { | |
4785 | if (h->root.type != bfd_link_hash_undefweak) | |
4786 | defined[i] = TRUE; | |
4787 | continue; | |
4788 | } | |
4789 | ||
4790 | /* We need to include this archive member. */ | |
4791 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4792 | if (element == NULL) | |
4793 | goto error_return; | |
4794 | ||
4795 | if (! bfd_check_format (element, bfd_object)) | |
4796 | goto error_return; | |
4797 | ||
4798 | /* Doublecheck that we have not included this object | |
4799 | already--it should be impossible, but there may be | |
4800 | something wrong with the archive. */ | |
4801 | if (element->archive_pass != 0) | |
4802 | { | |
4803 | bfd_set_error (bfd_error_bad_value); | |
4804 | goto error_return; | |
4805 | } | |
4806 | element->archive_pass = 1; | |
4807 | ||
4808 | undefs_tail = info->hash->undefs_tail; | |
4809 | ||
4810 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4811 | symdef->name)) | |
4812 | goto error_return; | |
4813 | if (! bfd_link_add_symbols (element, info)) | |
4814 | goto error_return; | |
4815 | ||
4816 | /* If there are any new undefined symbols, we need to make | |
4817 | another pass through the archive in order to see whether | |
4818 | they can be defined. FIXME: This isn't perfect, because | |
4819 | common symbols wind up on undefs_tail and because an | |
4820 | undefined symbol which is defined later on in this pass | |
4821 | does not require another pass. This isn't a bug, but it | |
4822 | does make the code less efficient than it could be. */ | |
4823 | if (undefs_tail != info->hash->undefs_tail) | |
4824 | loop = TRUE; | |
4825 | ||
4826 | /* Look backward to mark all symbols from this object file | |
4827 | which we have already seen in this pass. */ | |
4828 | mark = i; | |
4829 | do | |
4830 | { | |
4831 | included[mark] = TRUE; | |
4832 | if (mark == 0) | |
4833 | break; | |
4834 | --mark; | |
4835 | } | |
4836 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4837 | ||
4838 | /* We mark subsequent symbols from this object file as we go | |
4839 | on through the loop. */ | |
4840 | last = symdef->file_offset; | |
4841 | } | |
4842 | } | |
4843 | while (loop); | |
4844 | ||
4845 | free (defined); | |
4846 | free (included); | |
4847 | ||
4848 | return TRUE; | |
4849 | ||
4850 | error_return: | |
4851 | if (defined != NULL) | |
4852 | free (defined); | |
4853 | if (included != NULL) | |
4854 | free (included); | |
4855 | return FALSE; | |
4856 | } | |
4ad4eba5 AM |
4857 | |
4858 | /* Given an ELF BFD, add symbols to the global hash table as | |
4859 | appropriate. */ | |
4860 | ||
4861 | bfd_boolean | |
4862 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4863 | { | |
4864 | switch (bfd_get_format (abfd)) | |
4865 | { | |
4866 | case bfd_object: | |
4867 | return elf_link_add_object_symbols (abfd, info); | |
4868 | case bfd_archive: | |
4869 | return elf_link_add_archive_symbols (abfd, info); | |
4870 | default: | |
4871 | bfd_set_error (bfd_error_wrong_format); | |
4872 | return FALSE; | |
4873 | } | |
4874 | } | |
5a580b3a AM |
4875 | \f |
4876 | /* This function will be called though elf_link_hash_traverse to store | |
4877 | all hash value of the exported symbols in an array. */ | |
4878 | ||
4879 | static bfd_boolean | |
4880 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4881 | { | |
4882 | unsigned long **valuep = data; | |
4883 | const char *name; | |
4884 | char *p; | |
4885 | unsigned long ha; | |
4886 | char *alc = NULL; | |
4887 | ||
4888 | if (h->root.type == bfd_link_hash_warning) | |
4889 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4890 | ||
4891 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4892 | if (h->dynindx == -1) | |
4893 | return TRUE; | |
4894 | ||
4895 | name = h->root.root.string; | |
4896 | p = strchr (name, ELF_VER_CHR); | |
4897 | if (p != NULL) | |
4898 | { | |
4899 | alc = bfd_malloc (p - name + 1); | |
4900 | memcpy (alc, name, p - name); | |
4901 | alc[p - name] = '\0'; | |
4902 | name = alc; | |
4903 | } | |
4904 | ||
4905 | /* Compute the hash value. */ | |
4906 | ha = bfd_elf_hash (name); | |
4907 | ||
4908 | /* Store the found hash value in the array given as the argument. */ | |
4909 | *(*valuep)++ = ha; | |
4910 | ||
4911 | /* And store it in the struct so that we can put it in the hash table | |
4912 | later. */ | |
f6e332e6 | 4913 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4914 | |
4915 | if (alc != NULL) | |
4916 | free (alc); | |
4917 | ||
4918 | return TRUE; | |
4919 | } | |
4920 | ||
fdc90cb4 JJ |
4921 | struct collect_gnu_hash_codes |
4922 | { | |
4923 | bfd *output_bfd; | |
4924 | const struct elf_backend_data *bed; | |
4925 | unsigned long int nsyms; | |
4926 | unsigned long int maskbits; | |
4927 | unsigned long int *hashcodes; | |
4928 | unsigned long int *hashval; | |
4929 | unsigned long int *indx; | |
4930 | unsigned long int *counts; | |
4931 | bfd_vma *bitmask; | |
4932 | bfd_byte *contents; | |
4933 | long int min_dynindx; | |
4934 | unsigned long int bucketcount; | |
4935 | unsigned long int symindx; | |
4936 | long int local_indx; | |
4937 | long int shift1, shift2; | |
4938 | unsigned long int mask; | |
4939 | }; | |
4940 | ||
4941 | /* This function will be called though elf_link_hash_traverse to store | |
4942 | all hash value of the exported symbols in an array. */ | |
4943 | ||
4944 | static bfd_boolean | |
4945 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4946 | { | |
4947 | struct collect_gnu_hash_codes *s = data; | |
4948 | const char *name; | |
4949 | char *p; | |
4950 | unsigned long ha; | |
4951 | char *alc = NULL; | |
4952 | ||
4953 | if (h->root.type == bfd_link_hash_warning) | |
4954 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4955 | ||
4956 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4957 | if (h->dynindx == -1) | |
4958 | return TRUE; | |
4959 | ||
4960 | /* Ignore also local symbols and undefined symbols. */ | |
4961 | if (! (*s->bed->elf_hash_symbol) (h)) | |
4962 | return TRUE; | |
4963 | ||
4964 | name = h->root.root.string; | |
4965 | p = strchr (name, ELF_VER_CHR); | |
4966 | if (p != NULL) | |
4967 | { | |
4968 | alc = bfd_malloc (p - name + 1); | |
4969 | memcpy (alc, name, p - name); | |
4970 | alc[p - name] = '\0'; | |
4971 | name = alc; | |
4972 | } | |
4973 | ||
4974 | /* Compute the hash value. */ | |
4975 | ha = bfd_elf_gnu_hash (name); | |
4976 | ||
4977 | /* Store the found hash value in the array for compute_bucket_count, | |
4978 | and also for .dynsym reordering purposes. */ | |
4979 | s->hashcodes[s->nsyms] = ha; | |
4980 | s->hashval[h->dynindx] = ha; | |
4981 | ++s->nsyms; | |
4982 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
4983 | s->min_dynindx = h->dynindx; | |
4984 | ||
4985 | if (alc != NULL) | |
4986 | free (alc); | |
4987 | ||
4988 | return TRUE; | |
4989 | } | |
4990 | ||
4991 | /* This function will be called though elf_link_hash_traverse to do | |
4992 | final dynaminc symbol renumbering. */ | |
4993 | ||
4994 | static bfd_boolean | |
4995 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
4996 | { | |
4997 | struct collect_gnu_hash_codes *s = data; | |
4998 | unsigned long int bucket; | |
4999 | unsigned long int val; | |
5000 | ||
5001 | if (h->root.type == bfd_link_hash_warning) | |
5002 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5003 | ||
5004 | /* Ignore indirect symbols. */ | |
5005 | if (h->dynindx == -1) | |
5006 | return TRUE; | |
5007 | ||
5008 | /* Ignore also local symbols and undefined symbols. */ | |
5009 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5010 | { | |
5011 | if (h->dynindx >= s->min_dynindx) | |
5012 | h->dynindx = s->local_indx++; | |
5013 | return TRUE; | |
5014 | } | |
5015 | ||
5016 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5017 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5018 | & ((s->maskbits >> s->shift1) - 1); | |
5019 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5020 | s->bitmask[val] | |
5021 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5022 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5023 | if (s->counts[bucket] == 1) | |
5024 | /* Last element terminates the chain. */ | |
5025 | val |= 1; | |
5026 | bfd_put_32 (s->output_bfd, val, | |
5027 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5028 | --s->counts[bucket]; | |
5029 | h->dynindx = s->indx[bucket]++; | |
5030 | return TRUE; | |
5031 | } | |
5032 | ||
5033 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5034 | ||
5035 | bfd_boolean | |
5036 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5037 | { | |
5038 | return !(h->forced_local | |
5039 | || h->root.type == bfd_link_hash_undefined | |
5040 | || h->root.type == bfd_link_hash_undefweak | |
5041 | || ((h->root.type == bfd_link_hash_defined | |
5042 | || h->root.type == bfd_link_hash_defweak) | |
5043 | && h->root.u.def.section->output_section == NULL)); | |
5044 | } | |
5045 | ||
5a580b3a AM |
5046 | /* Array used to determine the number of hash table buckets to use |
5047 | based on the number of symbols there are. If there are fewer than | |
5048 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5049 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5050 | than 32771 buckets. */ | |
5051 | ||
5052 | static const size_t elf_buckets[] = | |
5053 | { | |
5054 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5055 | 16411, 32771, 0 | |
5056 | }; | |
5057 | ||
5058 | /* Compute bucket count for hashing table. We do not use a static set | |
5059 | of possible tables sizes anymore. Instead we determine for all | |
5060 | possible reasonable sizes of the table the outcome (i.e., the | |
5061 | number of collisions etc) and choose the best solution. The | |
5062 | weighting functions are not too simple to allow the table to grow | |
5063 | without bounds. Instead one of the weighting factors is the size. | |
5064 | Therefore the result is always a good payoff between few collisions | |
5065 | (= short chain lengths) and table size. */ | |
5066 | static size_t | |
fdc90cb4 JJ |
5067 | compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes, |
5068 | unsigned long int nsyms, int gnu_hash) | |
5a580b3a AM |
5069 | { |
5070 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
5071 | size_t best_size = 0; | |
5a580b3a AM |
5072 | unsigned long int i; |
5073 | bfd_size_type amt; | |
5074 | ||
5a580b3a AM |
5075 | /* We have a problem here. The following code to optimize the table |
5076 | size requires an integer type with more the 32 bits. If | |
5077 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5078 | #ifdef BFD_HOST_U_64_BIT | |
5079 | if (info->optimize) | |
5080 | { | |
5a580b3a AM |
5081 | size_t minsize; |
5082 | size_t maxsize; | |
5083 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a AM |
5084 | bfd *dynobj = elf_hash_table (info)->dynobj; |
5085 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
fdc90cb4 | 5086 | unsigned long int *counts; |
5a580b3a AM |
5087 | |
5088 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5089 | that the hashing table must at least have NSYMS/4 and at most | |
5090 | 2*NSYMS buckets. */ | |
5091 | minsize = nsyms / 4; | |
5092 | if (minsize == 0) | |
5093 | minsize = 1; | |
5094 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5095 | if (gnu_hash) |
5096 | { | |
5097 | if (minsize < 2) | |
5098 | minsize = 2; | |
5099 | if ((best_size & 31) == 0) | |
5100 | ++best_size; | |
5101 | } | |
5a580b3a AM |
5102 | |
5103 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5104 | since the size could be large. */ | |
5105 | amt = maxsize; | |
5106 | amt *= sizeof (unsigned long int); | |
5107 | counts = bfd_malloc (amt); | |
5108 | if (counts == NULL) | |
fdc90cb4 | 5109 | return 0; |
5a580b3a AM |
5110 | |
5111 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5112 | minimal chain length. The minor criteria is (of course) the size | |
5113 | of the table. */ | |
5114 | for (i = minsize; i < maxsize; ++i) | |
5115 | { | |
5116 | /* Walk through the array of hashcodes and count the collisions. */ | |
5117 | BFD_HOST_U_64_BIT max; | |
5118 | unsigned long int j; | |
5119 | unsigned long int fact; | |
5120 | ||
fdc90cb4 JJ |
5121 | if (gnu_hash && (i & 31) == 0) |
5122 | continue; | |
5123 | ||
5a580b3a AM |
5124 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5125 | ||
5126 | /* Determine how often each hash bucket is used. */ | |
5127 | for (j = 0; j < nsyms; ++j) | |
5128 | ++counts[hashcodes[j] % i]; | |
5129 | ||
5130 | /* For the weight function we need some information about the | |
5131 | pagesize on the target. This is information need not be 100% | |
5132 | accurate. Since this information is not available (so far) we | |
5133 | define it here to a reasonable default value. If it is crucial | |
5134 | to have a better value some day simply define this value. */ | |
5135 | # ifndef BFD_TARGET_PAGESIZE | |
5136 | # define BFD_TARGET_PAGESIZE (4096) | |
5137 | # endif | |
5138 | ||
fdc90cb4 JJ |
5139 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5140 | and the chains. */ | |
5141 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5142 | |
5143 | # if 1 | |
5144 | /* Variant 1: optimize for short chains. We add the squares | |
5145 | of all the chain lengths (which favors many small chain | |
5146 | over a few long chains). */ | |
5147 | for (j = 0; j < i; ++j) | |
5148 | max += counts[j] * counts[j]; | |
5149 | ||
5150 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5151 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5152 | max *= fact * fact; |
5153 | # else | |
5154 | /* Variant 2: Optimize a lot more for small table. Here we | |
5155 | also add squares of the size but we also add penalties for | |
5156 | empty slots (the +1 term). */ | |
5157 | for (j = 0; j < i; ++j) | |
5158 | max += (1 + counts[j]) * (1 + counts[j]); | |
5159 | ||
5160 | /* The overall size of the table is considered, but not as | |
5161 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5162 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5163 | max *= fact; |
5164 | # endif | |
5165 | ||
5166 | /* Compare with current best results. */ | |
5167 | if (max < best_chlen) | |
5168 | { | |
5169 | best_chlen = max; | |
5170 | best_size = i; | |
5171 | } | |
5172 | } | |
5173 | ||
5174 | free (counts); | |
5175 | } | |
5176 | else | |
5177 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5178 | { | |
5179 | /* This is the fallback solution if no 64bit type is available or if we | |
5180 | are not supposed to spend much time on optimizations. We select the | |
5181 | bucket count using a fixed set of numbers. */ | |
5182 | for (i = 0; elf_buckets[i] != 0; i++) | |
5183 | { | |
5184 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5185 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5186 | break; |
5187 | } | |
fdc90cb4 JJ |
5188 | if (gnu_hash && best_size < 2) |
5189 | best_size = 2; | |
5a580b3a AM |
5190 | } |
5191 | ||
5a580b3a AM |
5192 | return best_size; |
5193 | } | |
5194 | ||
5195 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
5196 | called by the ELF linker emulation before_allocation routine. We | |
5197 | must set the sizes of the sections before the linker sets the | |
5198 | addresses of the various sections. */ | |
5199 | ||
5200 | bfd_boolean | |
5201 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5202 | const char *soname, | |
5203 | const char *rpath, | |
5204 | const char *filter_shlib, | |
5205 | const char * const *auxiliary_filters, | |
5206 | struct bfd_link_info *info, | |
5207 | asection **sinterpptr, | |
5208 | struct bfd_elf_version_tree *verdefs) | |
5209 | { | |
5210 | bfd_size_type soname_indx; | |
5211 | bfd *dynobj; | |
5212 | const struct elf_backend_data *bed; | |
5213 | struct elf_assign_sym_version_info asvinfo; | |
5214 | ||
5215 | *sinterpptr = NULL; | |
5216 | ||
5217 | soname_indx = (bfd_size_type) -1; | |
5218 | ||
5219 | if (!is_elf_hash_table (info->hash)) | |
5220 | return TRUE; | |
5221 | ||
8c37241b | 5222 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
5223 | if (info->execstack) |
5224 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
5225 | else if (info->noexecstack) | |
5226 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
5227 | else | |
5228 | { | |
5229 | bfd *inputobj; | |
5230 | asection *notesec = NULL; | |
5231 | int exec = 0; | |
5232 | ||
5233 | for (inputobj = info->input_bfds; | |
5234 | inputobj; | |
5235 | inputobj = inputobj->link_next) | |
5236 | { | |
5237 | asection *s; | |
5238 | ||
d457dcf6 | 5239 | if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) |
5a580b3a AM |
5240 | continue; |
5241 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5242 | if (s) | |
5243 | { | |
5244 | if (s->flags & SEC_CODE) | |
5245 | exec = PF_X; | |
5246 | notesec = s; | |
5247 | } | |
5248 | else | |
5249 | exec = PF_X; | |
5250 | } | |
5251 | if (notesec) | |
5252 | { | |
5253 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
5254 | if (exec && info->relocatable | |
5255 | && notesec->output_section != bfd_abs_section_ptr) | |
5256 | notesec->output_section->flags |= SEC_CODE; | |
5257 | } | |
5258 | } | |
5259 | ||
5260 | /* Any syms created from now on start with -1 in | |
5261 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
5262 | elf_hash_table (info)->init_got_refcount |
5263 | = elf_hash_table (info)->init_got_offset; | |
5264 | elf_hash_table (info)->init_plt_refcount | |
5265 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
5266 | |
5267 | /* The backend may have to create some sections regardless of whether | |
5268 | we're dynamic or not. */ | |
5269 | bed = get_elf_backend_data (output_bfd); | |
5270 | if (bed->elf_backend_always_size_sections | |
5271 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5272 | return FALSE; | |
5273 | ||
eb3d5f3b JB |
5274 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
5275 | return FALSE; | |
5276 | ||
5a580b3a AM |
5277 | dynobj = elf_hash_table (info)->dynobj; |
5278 | ||
5279 | /* If there were no dynamic objects in the link, there is nothing to | |
5280 | do here. */ | |
5281 | if (dynobj == NULL) | |
5282 | return TRUE; | |
5283 | ||
5a580b3a AM |
5284 | if (elf_hash_table (info)->dynamic_sections_created) |
5285 | { | |
5286 | struct elf_info_failed eif; | |
5287 | struct elf_link_hash_entry *h; | |
5288 | asection *dynstr; | |
5289 | struct bfd_elf_version_tree *t; | |
5290 | struct bfd_elf_version_expr *d; | |
046183de | 5291 | asection *s; |
5a580b3a AM |
5292 | bfd_boolean all_defined; |
5293 | ||
5294 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5295 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5296 | ||
5297 | if (soname != NULL) | |
5298 | { | |
5299 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5300 | soname, TRUE); | |
5301 | if (soname_indx == (bfd_size_type) -1 | |
5302 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5303 | return FALSE; | |
5304 | } | |
5305 | ||
5306 | if (info->symbolic) | |
5307 | { | |
5308 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5309 | return FALSE; | |
5310 | info->flags |= DF_SYMBOLIC; | |
5311 | } | |
5312 | ||
5313 | if (rpath != NULL) | |
5314 | { | |
5315 | bfd_size_type indx; | |
5316 | ||
5317 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5318 | TRUE); | |
5319 | if (indx == (bfd_size_type) -1 | |
5320 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5321 | return FALSE; | |
5322 | ||
5323 | if (info->new_dtags) | |
5324 | { | |
5325 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5326 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5327 | return FALSE; | |
5328 | } | |
5329 | } | |
5330 | ||
5331 | if (filter_shlib != NULL) | |
5332 | { | |
5333 | bfd_size_type indx; | |
5334 | ||
5335 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5336 | filter_shlib, TRUE); | |
5337 | if (indx == (bfd_size_type) -1 | |
5338 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5339 | return FALSE; | |
5340 | } | |
5341 | ||
5342 | if (auxiliary_filters != NULL) | |
5343 | { | |
5344 | const char * const *p; | |
5345 | ||
5346 | for (p = auxiliary_filters; *p != NULL; p++) | |
5347 | { | |
5348 | bfd_size_type indx; | |
5349 | ||
5350 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5351 | *p, TRUE); | |
5352 | if (indx == (bfd_size_type) -1 | |
5353 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5354 | return FALSE; | |
5355 | } | |
5356 | } | |
5357 | ||
5358 | eif.info = info; | |
5359 | eif.verdefs = verdefs; | |
5360 | eif.failed = FALSE; | |
5361 | ||
5362 | /* If we are supposed to export all symbols into the dynamic symbol | |
5363 | table (this is not the normal case), then do so. */ | |
55255dae L |
5364 | if (info->export_dynamic |
5365 | || (info->executable && info->dynamic)) | |
5a580b3a AM |
5366 | { |
5367 | elf_link_hash_traverse (elf_hash_table (info), | |
5368 | _bfd_elf_export_symbol, | |
5369 | &eif); | |
5370 | if (eif.failed) | |
5371 | return FALSE; | |
5372 | } | |
5373 | ||
5374 | /* Make all global versions with definition. */ | |
5375 | for (t = verdefs; t != NULL; t = t->next) | |
5376 | for (d = t->globals.list; d != NULL; d = d->next) | |
5377 | if (!d->symver && d->symbol) | |
5378 | { | |
5379 | const char *verstr, *name; | |
5380 | size_t namelen, verlen, newlen; | |
5381 | char *newname, *p; | |
5382 | struct elf_link_hash_entry *newh; | |
5383 | ||
5384 | name = d->symbol; | |
5385 | namelen = strlen (name); | |
5386 | verstr = t->name; | |
5387 | verlen = strlen (verstr); | |
5388 | newlen = namelen + verlen + 3; | |
5389 | ||
5390 | newname = bfd_malloc (newlen); | |
5391 | if (newname == NULL) | |
5392 | return FALSE; | |
5393 | memcpy (newname, name, namelen); | |
5394 | ||
5395 | /* Check the hidden versioned definition. */ | |
5396 | p = newname + namelen; | |
5397 | *p++ = ELF_VER_CHR; | |
5398 | memcpy (p, verstr, verlen + 1); | |
5399 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5400 | newname, FALSE, FALSE, | |
5401 | FALSE); | |
5402 | if (newh == NULL | |
5403 | || (newh->root.type != bfd_link_hash_defined | |
5404 | && newh->root.type != bfd_link_hash_defweak)) | |
5405 | { | |
5406 | /* Check the default versioned definition. */ | |
5407 | *p++ = ELF_VER_CHR; | |
5408 | memcpy (p, verstr, verlen + 1); | |
5409 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5410 | newname, FALSE, FALSE, | |
5411 | FALSE); | |
5412 | } | |
5413 | free (newname); | |
5414 | ||
5415 | /* Mark this version if there is a definition and it is | |
5416 | not defined in a shared object. */ | |
5417 | if (newh != NULL | |
f5385ebf | 5418 | && !newh->def_dynamic |
5a580b3a AM |
5419 | && (newh->root.type == bfd_link_hash_defined |
5420 | || newh->root.type == bfd_link_hash_defweak)) | |
5421 | d->symver = 1; | |
5422 | } | |
5423 | ||
5424 | /* Attach all the symbols to their version information. */ | |
5425 | asvinfo.output_bfd = output_bfd; | |
5426 | asvinfo.info = info; | |
5427 | asvinfo.verdefs = verdefs; | |
5428 | asvinfo.failed = FALSE; | |
5429 | ||
5430 | elf_link_hash_traverse (elf_hash_table (info), | |
5431 | _bfd_elf_link_assign_sym_version, | |
5432 | &asvinfo); | |
5433 | if (asvinfo.failed) | |
5434 | return FALSE; | |
5435 | ||
5436 | if (!info->allow_undefined_version) | |
5437 | { | |
5438 | /* Check if all global versions have a definition. */ | |
5439 | all_defined = TRUE; | |
5440 | for (t = verdefs; t != NULL; t = t->next) | |
5441 | for (d = t->globals.list; d != NULL; d = d->next) | |
5442 | if (!d->symver && !d->script) | |
5443 | { | |
5444 | (*_bfd_error_handler) | |
5445 | (_("%s: undefined version: %s"), | |
5446 | d->pattern, t->name); | |
5447 | all_defined = FALSE; | |
5448 | } | |
5449 | ||
5450 | if (!all_defined) | |
5451 | { | |
5452 | bfd_set_error (bfd_error_bad_value); | |
5453 | return FALSE; | |
5454 | } | |
5455 | } | |
5456 | ||
5457 | /* Find all symbols which were defined in a dynamic object and make | |
5458 | the backend pick a reasonable value for them. */ | |
5459 | elf_link_hash_traverse (elf_hash_table (info), | |
5460 | _bfd_elf_adjust_dynamic_symbol, | |
5461 | &eif); | |
5462 | if (eif.failed) | |
5463 | return FALSE; | |
5464 | ||
5465 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5466 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5467 | now so that we know the final size of the .dynamic section. */ |
5468 | ||
5469 | /* If there are initialization and/or finalization functions to | |
5470 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5471 | h = (info->init_function | |
5472 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5473 | info->init_function, FALSE, | |
5474 | FALSE, FALSE) | |
5475 | : NULL); | |
5476 | if (h != NULL | |
f5385ebf AM |
5477 | && (h->ref_regular |
5478 | || h->def_regular)) | |
5a580b3a AM |
5479 | { |
5480 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5481 | return FALSE; | |
5482 | } | |
5483 | h = (info->fini_function | |
5484 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5485 | info->fini_function, FALSE, | |
5486 | FALSE, FALSE) | |
5487 | : NULL); | |
5488 | if (h != NULL | |
f5385ebf AM |
5489 | && (h->ref_regular |
5490 | || h->def_regular)) | |
5a580b3a AM |
5491 | { |
5492 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5493 | return FALSE; | |
5494 | } | |
5495 | ||
046183de AM |
5496 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5497 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5498 | { |
5499 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5500 | if (! info->executable) | |
5501 | { | |
5502 | bfd *sub; | |
5503 | asection *o; | |
5504 | ||
5505 | for (sub = info->input_bfds; sub != NULL; | |
5506 | sub = sub->link_next) | |
5507 | for (o = sub->sections; o != NULL; o = o->next) | |
5508 | if (elf_section_data (o)->this_hdr.sh_type | |
5509 | == SHT_PREINIT_ARRAY) | |
5510 | { | |
5511 | (*_bfd_error_handler) | |
d003868e AM |
5512 | (_("%B: .preinit_array section is not allowed in DSO"), |
5513 | sub); | |
5a580b3a AM |
5514 | break; |
5515 | } | |
5516 | ||
5517 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5518 | return FALSE; | |
5519 | } | |
5520 | ||
5521 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5522 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5523 | return FALSE; | |
5524 | } | |
046183de AM |
5525 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5526 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5527 | { |
5528 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5529 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5530 | return FALSE; | |
5531 | } | |
046183de AM |
5532 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5533 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5534 | { |
5535 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5536 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5537 | return FALSE; | |
5538 | } | |
5539 | ||
5540 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5541 | /* If .dynstr is excluded from the link, we don't want any of | |
5542 | these tags. Strictly, we should be checking each section | |
5543 | individually; This quick check covers for the case where | |
5544 | someone does a /DISCARD/ : { *(*) }. */ | |
5545 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5546 | { | |
5547 | bfd_size_type strsize; | |
5548 | ||
5549 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
5550 | if ((info->emit_hash |
5551 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
5552 | || (info->emit_gnu_hash | |
5553 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
5554 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
5555 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5556 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5557 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5558 | bed->s->sizeof_sym)) | |
5559 | return FALSE; | |
5560 | } | |
5561 | } | |
5562 | ||
5563 | /* The backend must work out the sizes of all the other dynamic | |
5564 | sections. */ | |
5565 | if (bed->elf_backend_size_dynamic_sections | |
5566 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5567 | return FALSE; | |
5568 | ||
5569 | if (elf_hash_table (info)->dynamic_sections_created) | |
5570 | { | |
554220db | 5571 | unsigned long section_sym_count; |
5a580b3a | 5572 | asection *s; |
5a580b3a AM |
5573 | |
5574 | /* Set up the version definition section. */ | |
5575 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5576 | BFD_ASSERT (s != NULL); | |
5577 | ||
5578 | /* We may have created additional version definitions if we are | |
5579 | just linking a regular application. */ | |
5580 | verdefs = asvinfo.verdefs; | |
5581 | ||
5582 | /* Skip anonymous version tag. */ | |
5583 | if (verdefs != NULL && verdefs->vernum == 0) | |
5584 | verdefs = verdefs->next; | |
5585 | ||
3e3b46e5 | 5586 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5587 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5588 | else |
5589 | { | |
5590 | unsigned int cdefs; | |
5591 | bfd_size_type size; | |
5592 | struct bfd_elf_version_tree *t; | |
5593 | bfd_byte *p; | |
5594 | Elf_Internal_Verdef def; | |
5595 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5596 | struct bfd_link_hash_entry *bh; |
5597 | struct elf_link_hash_entry *h; | |
5598 | const char *name; | |
5a580b3a AM |
5599 | |
5600 | cdefs = 0; | |
5601 | size = 0; | |
5602 | ||
5603 | /* Make space for the base version. */ | |
5604 | size += sizeof (Elf_External_Verdef); | |
5605 | size += sizeof (Elf_External_Verdaux); | |
5606 | ++cdefs; | |
5607 | ||
3e3b46e5 PB |
5608 | /* Make space for the default version. */ |
5609 | if (info->create_default_symver) | |
5610 | { | |
5611 | size += sizeof (Elf_External_Verdef); | |
5612 | ++cdefs; | |
5613 | } | |
5614 | ||
5a580b3a AM |
5615 | for (t = verdefs; t != NULL; t = t->next) |
5616 | { | |
5617 | struct bfd_elf_version_deps *n; | |
5618 | ||
5619 | size += sizeof (Elf_External_Verdef); | |
5620 | size += sizeof (Elf_External_Verdaux); | |
5621 | ++cdefs; | |
5622 | ||
5623 | for (n = t->deps; n != NULL; n = n->next) | |
5624 | size += sizeof (Elf_External_Verdaux); | |
5625 | } | |
5626 | ||
eea6121a AM |
5627 | s->size = size; |
5628 | s->contents = bfd_alloc (output_bfd, s->size); | |
5629 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5630 | return FALSE; |
5631 | ||
5632 | /* Fill in the version definition section. */ | |
5633 | ||
5634 | p = s->contents; | |
5635 | ||
5636 | def.vd_version = VER_DEF_CURRENT; | |
5637 | def.vd_flags = VER_FLG_BASE; | |
5638 | def.vd_ndx = 1; | |
5639 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5640 | if (info->create_default_symver) |
5641 | { | |
5642 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5643 | def.vd_next = sizeof (Elf_External_Verdef); | |
5644 | } | |
5645 | else | |
5646 | { | |
5647 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5648 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5649 | + sizeof (Elf_External_Verdaux)); | |
5650 | } | |
5a580b3a AM |
5651 | |
5652 | if (soname_indx != (bfd_size_type) -1) | |
5653 | { | |
5654 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5655 | soname_indx); | |
5656 | def.vd_hash = bfd_elf_hash (soname); | |
5657 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5658 | name = soname; |
5a580b3a AM |
5659 | } |
5660 | else | |
5661 | { | |
5a580b3a AM |
5662 | bfd_size_type indx; |
5663 | ||
06084812 | 5664 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5665 | def.vd_hash = bfd_elf_hash (name); |
5666 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5667 | name, FALSE); | |
5668 | if (indx == (bfd_size_type) -1) | |
5669 | return FALSE; | |
5670 | defaux.vda_name = indx; | |
5671 | } | |
5672 | defaux.vda_next = 0; | |
5673 | ||
5674 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5675 | (Elf_External_Verdef *) p); | |
5676 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5677 | if (info->create_default_symver) |
5678 | { | |
5679 | /* Add a symbol representing this version. */ | |
5680 | bh = NULL; | |
5681 | if (! (_bfd_generic_link_add_one_symbol | |
5682 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5683 | 0, NULL, FALSE, | |
5684 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5685 | return FALSE; | |
5686 | h = (struct elf_link_hash_entry *) bh; | |
5687 | h->non_elf = 0; | |
5688 | h->def_regular = 1; | |
5689 | h->type = STT_OBJECT; | |
5690 | h->verinfo.vertree = NULL; | |
5691 | ||
5692 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5693 | return FALSE; | |
5694 | ||
5695 | /* Create a duplicate of the base version with the same | |
5696 | aux block, but different flags. */ | |
5697 | def.vd_flags = 0; | |
5698 | def.vd_ndx = 2; | |
5699 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5700 | if (verdefs) | |
5701 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5702 | + sizeof (Elf_External_Verdaux)); | |
5703 | else | |
5704 | def.vd_next = 0; | |
5705 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5706 | (Elf_External_Verdef *) p); | |
5707 | p += sizeof (Elf_External_Verdef); | |
5708 | } | |
5a580b3a AM |
5709 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5710 | (Elf_External_Verdaux *) p); | |
5711 | p += sizeof (Elf_External_Verdaux); | |
5712 | ||
5713 | for (t = verdefs; t != NULL; t = t->next) | |
5714 | { | |
5715 | unsigned int cdeps; | |
5716 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5717 | |
5718 | cdeps = 0; | |
5719 | for (n = t->deps; n != NULL; n = n->next) | |
5720 | ++cdeps; | |
5721 | ||
5722 | /* Add a symbol representing this version. */ | |
5723 | bh = NULL; | |
5724 | if (! (_bfd_generic_link_add_one_symbol | |
5725 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5726 | 0, NULL, FALSE, | |
5727 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5728 | return FALSE; | |
5729 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5730 | h->non_elf = 0; |
5731 | h->def_regular = 1; | |
5a580b3a AM |
5732 | h->type = STT_OBJECT; |
5733 | h->verinfo.vertree = t; | |
5734 | ||
c152c796 | 5735 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5736 | return FALSE; |
5737 | ||
5738 | def.vd_version = VER_DEF_CURRENT; | |
5739 | def.vd_flags = 0; | |
5740 | if (t->globals.list == NULL | |
5741 | && t->locals.list == NULL | |
5742 | && ! t->used) | |
5743 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5744 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5745 | def.vd_cnt = cdeps + 1; |
5746 | def.vd_hash = bfd_elf_hash (t->name); | |
5747 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5748 | def.vd_next = 0; | |
5749 | if (t->next != NULL) | |
5750 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5751 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5752 | ||
5753 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5754 | (Elf_External_Verdef *) p); | |
5755 | p += sizeof (Elf_External_Verdef); | |
5756 | ||
5757 | defaux.vda_name = h->dynstr_index; | |
5758 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5759 | h->dynstr_index); | |
5760 | defaux.vda_next = 0; | |
5761 | if (t->deps != NULL) | |
5762 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5763 | t->name_indx = defaux.vda_name; | |
5764 | ||
5765 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5766 | (Elf_External_Verdaux *) p); | |
5767 | p += sizeof (Elf_External_Verdaux); | |
5768 | ||
5769 | for (n = t->deps; n != NULL; n = n->next) | |
5770 | { | |
5771 | if (n->version_needed == NULL) | |
5772 | { | |
5773 | /* This can happen if there was an error in the | |
5774 | version script. */ | |
5775 | defaux.vda_name = 0; | |
5776 | } | |
5777 | else | |
5778 | { | |
5779 | defaux.vda_name = n->version_needed->name_indx; | |
5780 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5781 | defaux.vda_name); | |
5782 | } | |
5783 | if (n->next == NULL) | |
5784 | defaux.vda_next = 0; | |
5785 | else | |
5786 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5787 | ||
5788 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5789 | (Elf_External_Verdaux *) p); | |
5790 | p += sizeof (Elf_External_Verdaux); | |
5791 | } | |
5792 | } | |
5793 | ||
5794 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5795 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5796 | return FALSE; | |
5797 | ||
5798 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5799 | } | |
5800 | ||
5801 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5802 | { | |
5803 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5804 | return FALSE; | |
5805 | } | |
5806 | else if (info->flags & DF_BIND_NOW) | |
5807 | { | |
5808 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5809 | return FALSE; | |
5810 | } | |
5811 | ||
5812 | if (info->flags_1) | |
5813 | { | |
5814 | if (info->executable) | |
5815 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5816 | | DF_1_NODELETE | |
5817 | | DF_1_NOOPEN); | |
5818 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5819 | return FALSE; | |
5820 | } | |
5821 | ||
5822 | /* Work out the size of the version reference section. */ | |
5823 | ||
5824 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5825 | BFD_ASSERT (s != NULL); | |
5826 | { | |
5827 | struct elf_find_verdep_info sinfo; | |
5828 | ||
5829 | sinfo.output_bfd = output_bfd; | |
5830 | sinfo.info = info; | |
5831 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5832 | if (sinfo.vers == 0) | |
5833 | sinfo.vers = 1; | |
5834 | sinfo.failed = FALSE; | |
5835 | ||
5836 | elf_link_hash_traverse (elf_hash_table (info), | |
5837 | _bfd_elf_link_find_version_dependencies, | |
5838 | &sinfo); | |
5839 | ||
5840 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 5841 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5842 | else |
5843 | { | |
5844 | Elf_Internal_Verneed *t; | |
5845 | unsigned int size; | |
5846 | unsigned int crefs; | |
5847 | bfd_byte *p; | |
5848 | ||
5849 | /* Build the version definition section. */ | |
5850 | size = 0; | |
5851 | crefs = 0; | |
5852 | for (t = elf_tdata (output_bfd)->verref; | |
5853 | t != NULL; | |
5854 | t = t->vn_nextref) | |
5855 | { | |
5856 | Elf_Internal_Vernaux *a; | |
5857 | ||
5858 | size += sizeof (Elf_External_Verneed); | |
5859 | ++crefs; | |
5860 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5861 | size += sizeof (Elf_External_Vernaux); | |
5862 | } | |
5863 | ||
eea6121a AM |
5864 | s->size = size; |
5865 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5866 | if (s->contents == NULL) |
5867 | return FALSE; | |
5868 | ||
5869 | p = s->contents; | |
5870 | for (t = elf_tdata (output_bfd)->verref; | |
5871 | t != NULL; | |
5872 | t = t->vn_nextref) | |
5873 | { | |
5874 | unsigned int caux; | |
5875 | Elf_Internal_Vernaux *a; | |
5876 | bfd_size_type indx; | |
5877 | ||
5878 | caux = 0; | |
5879 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5880 | ++caux; | |
5881 | ||
5882 | t->vn_version = VER_NEED_CURRENT; | |
5883 | t->vn_cnt = caux; | |
5884 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5885 | elf_dt_name (t->vn_bfd) != NULL | |
5886 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 5887 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
5888 | FALSE); |
5889 | if (indx == (bfd_size_type) -1) | |
5890 | return FALSE; | |
5891 | t->vn_file = indx; | |
5892 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5893 | if (t->vn_nextref == NULL) | |
5894 | t->vn_next = 0; | |
5895 | else | |
5896 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5897 | + caux * sizeof (Elf_External_Vernaux)); | |
5898 | ||
5899 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5900 | (Elf_External_Verneed *) p); | |
5901 | p += sizeof (Elf_External_Verneed); | |
5902 | ||
5903 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5904 | { | |
5905 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5906 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5907 | a->vna_nodename, FALSE); | |
5908 | if (indx == (bfd_size_type) -1) | |
5909 | return FALSE; | |
5910 | a->vna_name = indx; | |
5911 | if (a->vna_nextptr == NULL) | |
5912 | a->vna_next = 0; | |
5913 | else | |
5914 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5915 | ||
5916 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5917 | (Elf_External_Vernaux *) p); | |
5918 | p += sizeof (Elf_External_Vernaux); | |
5919 | } | |
5920 | } | |
5921 | ||
5922 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5923 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5924 | return FALSE; | |
5925 | ||
5926 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5927 | } | |
5928 | } | |
5929 | ||
8423293d AM |
5930 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
5931 | && elf_tdata (output_bfd)->cverdefs == 0) | |
5932 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
5933 | §ion_sym_count) == 0) | |
5934 | { | |
5935 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5936 | s->flags |= SEC_EXCLUDE; | |
5937 | } | |
5938 | } | |
5939 | return TRUE; | |
5940 | } | |
5941 | ||
74541ad4 AM |
5942 | /* Find the first non-excluded output section. We'll use its |
5943 | section symbol for some emitted relocs. */ | |
5944 | void | |
5945 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
5946 | { | |
5947 | asection *s; | |
5948 | ||
5949 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
5950 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
5951 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
5952 | { | |
5953 | elf_hash_table (info)->text_index_section = s; | |
5954 | break; | |
5955 | } | |
5956 | } | |
5957 | ||
5958 | /* Find two non-excluded output sections, one for code, one for data. | |
5959 | We'll use their section symbols for some emitted relocs. */ | |
5960 | void | |
5961 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
5962 | { | |
5963 | asection *s; | |
5964 | ||
5965 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
5966 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) | |
5967 | == (SEC_ALLOC | SEC_READONLY)) | |
5968 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
5969 | { | |
5970 | elf_hash_table (info)->text_index_section = s; | |
5971 | break; | |
5972 | } | |
5973 | ||
5974 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
5975 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) | |
5976 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
5977 | { | |
5978 | elf_hash_table (info)->data_index_section = s; | |
5979 | break; | |
5980 | } | |
5981 | ||
5982 | if (elf_hash_table (info)->text_index_section == NULL) | |
5983 | elf_hash_table (info)->text_index_section | |
5984 | = elf_hash_table (info)->data_index_section; | |
5985 | } | |
5986 | ||
8423293d AM |
5987 | bfd_boolean |
5988 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5989 | { | |
74541ad4 AM |
5990 | const struct elf_backend_data *bed; |
5991 | ||
8423293d AM |
5992 | if (!is_elf_hash_table (info->hash)) |
5993 | return TRUE; | |
5994 | ||
74541ad4 AM |
5995 | bed = get_elf_backend_data (output_bfd); |
5996 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
5997 | ||
8423293d AM |
5998 | if (elf_hash_table (info)->dynamic_sections_created) |
5999 | { | |
6000 | bfd *dynobj; | |
8423293d AM |
6001 | asection *s; |
6002 | bfd_size_type dynsymcount; | |
6003 | unsigned long section_sym_count; | |
8423293d AM |
6004 | unsigned int dtagcount; |
6005 | ||
6006 | dynobj = elf_hash_table (info)->dynobj; | |
6007 | ||
5a580b3a AM |
6008 | /* Assign dynsym indicies. In a shared library we generate a |
6009 | section symbol for each output section, which come first. | |
6010 | Next come all of the back-end allocated local dynamic syms, | |
6011 | followed by the rest of the global symbols. */ | |
6012 | ||
554220db AM |
6013 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6014 | §ion_sym_count); | |
5a580b3a AM |
6015 | |
6016 | /* Work out the size of the symbol version section. */ | |
6017 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6018 | BFD_ASSERT (s != NULL); | |
8423293d AM |
6019 | if (dynsymcount != 0 |
6020 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 6021 | { |
eea6121a AM |
6022 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
6023 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
6024 | if (s->contents == NULL) |
6025 | return FALSE; | |
6026 | ||
6027 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6028 | return FALSE; | |
6029 | } | |
6030 | ||
6031 | /* Set the size of the .dynsym and .hash sections. We counted | |
6032 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6033 | We will build the contents of .dynsym and .hash when we build | |
6034 | the final symbol table, because until then we do not know the | |
6035 | correct value to give the symbols. We built the .dynstr | |
6036 | section as we went along in elf_link_add_object_symbols. */ | |
6037 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
6038 | BFD_ASSERT (s != NULL); | |
eea6121a | 6039 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
6040 | |
6041 | if (dynsymcount != 0) | |
6042 | { | |
554220db AM |
6043 | s->contents = bfd_alloc (output_bfd, s->size); |
6044 | if (s->contents == NULL) | |
6045 | return FALSE; | |
5a580b3a | 6046 | |
554220db AM |
6047 | /* The first entry in .dynsym is a dummy symbol. |
6048 | Clear all the section syms, in case we don't output them all. */ | |
6049 | ++section_sym_count; | |
6050 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
6051 | } |
6052 | ||
fdc90cb4 JJ |
6053 | elf_hash_table (info)->bucketcount = 0; |
6054 | ||
5a580b3a AM |
6055 | /* Compute the size of the hashing table. As a side effect this |
6056 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6057 | if (info->emit_hash) |
6058 | { | |
6059 | unsigned long int *hashcodes; | |
6060 | unsigned long int *hashcodesp; | |
6061 | bfd_size_type amt; | |
6062 | unsigned long int nsyms; | |
6063 | size_t bucketcount; | |
6064 | size_t hash_entry_size; | |
6065 | ||
6066 | /* Compute the hash values for all exported symbols. At the same | |
6067 | time store the values in an array so that we could use them for | |
6068 | optimizations. */ | |
6069 | amt = dynsymcount * sizeof (unsigned long int); | |
6070 | hashcodes = bfd_malloc (amt); | |
6071 | if (hashcodes == NULL) | |
6072 | return FALSE; | |
6073 | hashcodesp = hashcodes; | |
5a580b3a | 6074 | |
fdc90cb4 JJ |
6075 | /* Put all hash values in HASHCODES. */ |
6076 | elf_link_hash_traverse (elf_hash_table (info), | |
6077 | elf_collect_hash_codes, &hashcodesp); | |
5a580b3a | 6078 | |
fdc90cb4 JJ |
6079 | nsyms = hashcodesp - hashcodes; |
6080 | bucketcount | |
6081 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6082 | free (hashcodes); | |
6083 | ||
6084 | if (bucketcount == 0) | |
6085 | return FALSE; | |
5a580b3a | 6086 | |
fdc90cb4 JJ |
6087 | elf_hash_table (info)->bucketcount = bucketcount; |
6088 | ||
6089 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
6090 | BFD_ASSERT (s != NULL); | |
6091 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6092 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
6093 | s->contents = bfd_zalloc (output_bfd, s->size); | |
6094 | if (s->contents == NULL) | |
6095 | return FALSE; | |
6096 | ||
6097 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6098 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6099 | s->contents + hash_entry_size); | |
6100 | } | |
6101 | ||
6102 | if (info->emit_gnu_hash) | |
6103 | { | |
6104 | size_t i, cnt; | |
6105 | unsigned char *contents; | |
6106 | struct collect_gnu_hash_codes cinfo; | |
6107 | bfd_size_type amt; | |
6108 | size_t bucketcount; | |
6109 | ||
6110 | memset (&cinfo, 0, sizeof (cinfo)); | |
6111 | ||
6112 | /* Compute the hash values for all exported symbols. At the same | |
6113 | time store the values in an array so that we could use them for | |
6114 | optimizations. */ | |
6115 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
6116 | cinfo.hashcodes = bfd_malloc (amt); | |
6117 | if (cinfo.hashcodes == NULL) | |
6118 | return FALSE; | |
6119 | ||
6120 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6121 | cinfo.min_dynindx = -1; | |
6122 | cinfo.output_bfd = output_bfd; | |
6123 | cinfo.bed = bed; | |
6124 | ||
6125 | /* Put all hash values in HASHCODES. */ | |
6126 | elf_link_hash_traverse (elf_hash_table (info), | |
6127 | elf_collect_gnu_hash_codes, &cinfo); | |
6128 | ||
6129 | bucketcount | |
6130 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6131 | ||
6132 | if (bucketcount == 0) | |
6133 | { | |
6134 | free (cinfo.hashcodes); | |
6135 | return FALSE; | |
6136 | } | |
6137 | ||
6138 | s = bfd_get_section_by_name (dynobj, ".gnu.hash"); | |
6139 | BFD_ASSERT (s != NULL); | |
6140 | ||
6141 | if (cinfo.nsyms == 0) | |
6142 | { | |
6143 | /* Empty .gnu.hash section is special. */ | |
6144 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6145 | free (cinfo.hashcodes); | |
6146 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
6147 | contents = bfd_zalloc (output_bfd, s->size); | |
6148 | if (contents == NULL) | |
6149 | return FALSE; | |
6150 | s->contents = contents; | |
6151 | /* 1 empty bucket. */ | |
6152 | bfd_put_32 (output_bfd, 1, contents); | |
6153 | /* SYMIDX above the special symbol 0. */ | |
6154 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6155 | /* Just one word for bitmask. */ | |
6156 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6157 | /* Only hash fn bloom filter. */ | |
6158 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6159 | /* No hashes are valid - empty bitmask. */ | |
6160 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6161 | /* No hashes in the only bucket. */ | |
6162 | bfd_put_32 (output_bfd, 0, | |
6163 | contents + 16 + bed->s->arch_size / 8); | |
6164 | } | |
6165 | else | |
6166 | { | |
fdc90cb4 | 6167 | unsigned long int maskwords, maskbitslog2; |
0b33793d | 6168 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 JJ |
6169 | |
6170 | maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1; | |
6171 | if (maskbitslog2 < 3) | |
6172 | maskbitslog2 = 5; | |
6173 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6174 | maskbitslog2 = maskbitslog2 + 3; | |
6175 | else | |
6176 | maskbitslog2 = maskbitslog2 + 2; | |
6177 | if (bed->s->arch_size == 64) | |
6178 | { | |
6179 | if (maskbitslog2 == 5) | |
6180 | maskbitslog2 = 6; | |
6181 | cinfo.shift1 = 6; | |
6182 | } | |
6183 | else | |
6184 | cinfo.shift1 = 5; | |
6185 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6186 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6187 | cinfo.maskbits = 1 << maskbitslog2; |
6188 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6189 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6190 | amt += maskwords * sizeof (bfd_vma); | |
6191 | cinfo.bitmask = bfd_malloc (amt); | |
6192 | if (cinfo.bitmask == NULL) | |
6193 | { | |
6194 | free (cinfo.hashcodes); | |
6195 | return FALSE; | |
6196 | } | |
6197 | ||
6198 | cinfo.counts = (void *) (cinfo.bitmask + maskwords); | |
6199 | cinfo.indx = cinfo.counts + bucketcount; | |
6200 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6201 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6202 | ||
6203 | /* Determine how often each hash bucket is used. */ | |
6204 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6205 | for (i = 0; i < cinfo.nsyms; ++i) | |
6206 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6207 | ||
6208 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6209 | if (cinfo.counts[i] != 0) | |
6210 | { | |
6211 | cinfo.indx[i] = cnt; | |
6212 | cnt += cinfo.counts[i]; | |
6213 | } | |
6214 | BFD_ASSERT (cnt == dynsymcount); | |
6215 | cinfo.bucketcount = bucketcount; | |
6216 | cinfo.local_indx = cinfo.min_dynindx; | |
6217 | ||
6218 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6219 | s->size += cinfo.maskbits / 8; | |
6220 | contents = bfd_zalloc (output_bfd, s->size); | |
6221 | if (contents == NULL) | |
6222 | { | |
6223 | free (cinfo.bitmask); | |
6224 | free (cinfo.hashcodes); | |
6225 | return FALSE; | |
6226 | } | |
6227 | ||
6228 | s->contents = contents; | |
6229 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6230 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6231 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6232 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6233 | contents += 16 + cinfo.maskbits / 8; | |
6234 | ||
6235 | for (i = 0; i < bucketcount; ++i) | |
6236 | { | |
6237 | if (cinfo.counts[i] == 0) | |
6238 | bfd_put_32 (output_bfd, 0, contents); | |
6239 | else | |
6240 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6241 | contents += 4; | |
6242 | } | |
6243 | ||
6244 | cinfo.contents = contents; | |
6245 | ||
6246 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6247 | elf_link_hash_traverse (elf_hash_table (info), | |
6248 | elf_renumber_gnu_hash_syms, &cinfo); | |
6249 | ||
6250 | contents = s->contents + 16; | |
6251 | for (i = 0; i < maskwords; ++i) | |
6252 | { | |
6253 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6254 | contents); | |
6255 | contents += bed->s->arch_size / 8; | |
6256 | } | |
6257 | ||
6258 | free (cinfo.bitmask); | |
6259 | free (cinfo.hashcodes); | |
6260 | } | |
6261 | } | |
5a580b3a AM |
6262 | |
6263 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
6264 | BFD_ASSERT (s != NULL); | |
6265 | ||
4ad4eba5 | 6266 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6267 | |
eea6121a | 6268 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6269 | |
6270 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6271 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6272 | return FALSE; | |
6273 | } | |
6274 | ||
6275 | return TRUE; | |
6276 | } | |
c152c796 AM |
6277 | |
6278 | /* Final phase of ELF linker. */ | |
6279 | ||
6280 | /* A structure we use to avoid passing large numbers of arguments. */ | |
6281 | ||
6282 | struct elf_final_link_info | |
6283 | { | |
6284 | /* General link information. */ | |
6285 | struct bfd_link_info *info; | |
6286 | /* Output BFD. */ | |
6287 | bfd *output_bfd; | |
6288 | /* Symbol string table. */ | |
6289 | struct bfd_strtab_hash *symstrtab; | |
6290 | /* .dynsym section. */ | |
6291 | asection *dynsym_sec; | |
6292 | /* .hash section. */ | |
6293 | asection *hash_sec; | |
6294 | /* symbol version section (.gnu.version). */ | |
6295 | asection *symver_sec; | |
6296 | /* Buffer large enough to hold contents of any section. */ | |
6297 | bfd_byte *contents; | |
6298 | /* Buffer large enough to hold external relocs of any section. */ | |
6299 | void *external_relocs; | |
6300 | /* Buffer large enough to hold internal relocs of any section. */ | |
6301 | Elf_Internal_Rela *internal_relocs; | |
6302 | /* Buffer large enough to hold external local symbols of any input | |
6303 | BFD. */ | |
6304 | bfd_byte *external_syms; | |
6305 | /* And a buffer for symbol section indices. */ | |
6306 | Elf_External_Sym_Shndx *locsym_shndx; | |
6307 | /* Buffer large enough to hold internal local symbols of any input | |
6308 | BFD. */ | |
6309 | Elf_Internal_Sym *internal_syms; | |
6310 | /* Array large enough to hold a symbol index for each local symbol | |
6311 | of any input BFD. */ | |
6312 | long *indices; | |
6313 | /* Array large enough to hold a section pointer for each local | |
6314 | symbol of any input BFD. */ | |
6315 | asection **sections; | |
6316 | /* Buffer to hold swapped out symbols. */ | |
6317 | bfd_byte *symbuf; | |
6318 | /* And one for symbol section indices. */ | |
6319 | Elf_External_Sym_Shndx *symshndxbuf; | |
6320 | /* Number of swapped out symbols in buffer. */ | |
6321 | size_t symbuf_count; | |
6322 | /* Number of symbols which fit in symbuf. */ | |
6323 | size_t symbuf_size; | |
6324 | /* And same for symshndxbuf. */ | |
6325 | size_t shndxbuf_size; | |
6326 | }; | |
6327 | ||
6328 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
6329 | ||
6330 | struct elf_outext_info | |
6331 | { | |
6332 | bfd_boolean failed; | |
6333 | bfd_boolean localsyms; | |
6334 | struct elf_final_link_info *finfo; | |
6335 | }; | |
6336 | ||
6337 | /* When performing a relocatable link, the input relocations are | |
6338 | preserved. But, if they reference global symbols, the indices | |
6339 | referenced must be updated. Update all the relocations in | |
6340 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
6341 | ||
6342 | static void | |
6343 | elf_link_adjust_relocs (bfd *abfd, | |
6344 | Elf_Internal_Shdr *rel_hdr, | |
6345 | unsigned int count, | |
6346 | struct elf_link_hash_entry **rel_hash) | |
6347 | { | |
6348 | unsigned int i; | |
6349 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6350 | bfd_byte *erela; | |
6351 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
6352 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
6353 | bfd_vma r_type_mask; | |
6354 | int r_sym_shift; | |
6355 | ||
6356 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
6357 | { | |
6358 | swap_in = bed->s->swap_reloc_in; | |
6359 | swap_out = bed->s->swap_reloc_out; | |
6360 | } | |
6361 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
6362 | { | |
6363 | swap_in = bed->s->swap_reloca_in; | |
6364 | swap_out = bed->s->swap_reloca_out; | |
6365 | } | |
6366 | else | |
6367 | abort (); | |
6368 | ||
6369 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
6370 | abort (); | |
6371 | ||
6372 | if (bed->s->arch_size == 32) | |
6373 | { | |
6374 | r_type_mask = 0xff; | |
6375 | r_sym_shift = 8; | |
6376 | } | |
6377 | else | |
6378 | { | |
6379 | r_type_mask = 0xffffffff; | |
6380 | r_sym_shift = 32; | |
6381 | } | |
6382 | ||
6383 | erela = rel_hdr->contents; | |
6384 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
6385 | { | |
6386 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
6387 | unsigned int j; | |
6388 | ||
6389 | if (*rel_hash == NULL) | |
6390 | continue; | |
6391 | ||
6392 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
6393 | ||
6394 | (*swap_in) (abfd, erela, irela); | |
6395 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
6396 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
6397 | | (irela[j].r_info & r_type_mask)); | |
6398 | (*swap_out) (abfd, irela, erela); | |
6399 | } | |
6400 | } | |
6401 | ||
6402 | struct elf_link_sort_rela | |
6403 | { | |
6404 | union { | |
6405 | bfd_vma offset; | |
6406 | bfd_vma sym_mask; | |
6407 | } u; | |
6408 | enum elf_reloc_type_class type; | |
6409 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
6410 | Elf_Internal_Rela rela[1]; | |
6411 | }; | |
6412 | ||
6413 | static int | |
6414 | elf_link_sort_cmp1 (const void *A, const void *B) | |
6415 | { | |
6416 | const struct elf_link_sort_rela *a = A; | |
6417 | const struct elf_link_sort_rela *b = B; | |
6418 | int relativea, relativeb; | |
6419 | ||
6420 | relativea = a->type == reloc_class_relative; | |
6421 | relativeb = b->type == reloc_class_relative; | |
6422 | ||
6423 | if (relativea < relativeb) | |
6424 | return 1; | |
6425 | if (relativea > relativeb) | |
6426 | return -1; | |
6427 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
6428 | return -1; | |
6429 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
6430 | return 1; | |
6431 | if (a->rela->r_offset < b->rela->r_offset) | |
6432 | return -1; | |
6433 | if (a->rela->r_offset > b->rela->r_offset) | |
6434 | return 1; | |
6435 | return 0; | |
6436 | } | |
6437 | ||
6438 | static int | |
6439 | elf_link_sort_cmp2 (const void *A, const void *B) | |
6440 | { | |
6441 | const struct elf_link_sort_rela *a = A; | |
6442 | const struct elf_link_sort_rela *b = B; | |
6443 | int copya, copyb; | |
6444 | ||
6445 | if (a->u.offset < b->u.offset) | |
6446 | return -1; | |
6447 | if (a->u.offset > b->u.offset) | |
6448 | return 1; | |
6449 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
6450 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
6451 | if (copya < copyb) | |
6452 | return -1; | |
6453 | if (copya > copyb) | |
6454 | return 1; | |
6455 | if (a->rela->r_offset < b->rela->r_offset) | |
6456 | return -1; | |
6457 | if (a->rela->r_offset > b->rela->r_offset) | |
6458 | return 1; | |
6459 | return 0; | |
6460 | } | |
6461 | ||
6462 | static size_t | |
6463 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
6464 | { | |
6465 | asection *reldyn; | |
6466 | bfd_size_type count, size; | |
6467 | size_t i, ret, sort_elt, ext_size; | |
6468 | bfd_byte *sort, *s_non_relative, *p; | |
6469 | struct elf_link_sort_rela *sq; | |
6470 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6471 | int i2e = bed->s->int_rels_per_ext_rel; | |
6472 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
6473 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
6474 | struct bfd_link_order *lo; | |
6475 | bfd_vma r_sym_mask; | |
6476 | ||
6477 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
eea6121a | 6478 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
6479 | { |
6480 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
eea6121a | 6481 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
6482 | return 0; |
6483 | ext_size = bed->s->sizeof_rel; | |
6484 | swap_in = bed->s->swap_reloc_in; | |
6485 | swap_out = bed->s->swap_reloc_out; | |
6486 | } | |
6487 | else | |
6488 | { | |
6489 | ext_size = bed->s->sizeof_rela; | |
6490 | swap_in = bed->s->swap_reloca_in; | |
6491 | swap_out = bed->s->swap_reloca_out; | |
6492 | } | |
eea6121a | 6493 | count = reldyn->size / ext_size; |
c152c796 AM |
6494 | |
6495 | size = 0; | |
8423293d | 6496 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6497 | if (lo->type == bfd_indirect_link_order) |
6498 | { | |
6499 | asection *o = lo->u.indirect.section; | |
eea6121a | 6500 | size += o->size; |
c152c796 AM |
6501 | } |
6502 | ||
eea6121a | 6503 | if (size != reldyn->size) |
c152c796 AM |
6504 | return 0; |
6505 | ||
6506 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
6507 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
6508 | sort = bfd_zmalloc (sort_elt * count); | |
6509 | if (sort == NULL) | |
6510 | { | |
6511 | (*info->callbacks->warning) | |
6512 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
6513 | return 0; | |
6514 | } | |
6515 | ||
6516 | if (bed->s->arch_size == 32) | |
6517 | r_sym_mask = ~(bfd_vma) 0xff; | |
6518 | else | |
6519 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
6520 | ||
8423293d | 6521 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6522 | if (lo->type == bfd_indirect_link_order) |
6523 | { | |
6524 | bfd_byte *erel, *erelend; | |
6525 | asection *o = lo->u.indirect.section; | |
6526 | ||
1da212d6 AM |
6527 | if (o->contents == NULL && o->size != 0) |
6528 | { | |
6529 | /* This is a reloc section that is being handled as a normal | |
6530 | section. See bfd_section_from_shdr. We can't combine | |
6531 | relocs in this case. */ | |
6532 | free (sort); | |
6533 | return 0; | |
6534 | } | |
c152c796 | 6535 | erel = o->contents; |
eea6121a | 6536 | erelend = o->contents + o->size; |
c152c796 AM |
6537 | p = sort + o->output_offset / ext_size * sort_elt; |
6538 | while (erel < erelend) | |
6539 | { | |
6540 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6541 | (*swap_in) (abfd, erel, s->rela); | |
6542 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
6543 | s->u.sym_mask = r_sym_mask; | |
6544 | p += sort_elt; | |
6545 | erel += ext_size; | |
6546 | } | |
6547 | } | |
6548 | ||
6549 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
6550 | ||
6551 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
6552 | { | |
6553 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6554 | if (s->type != reloc_class_relative) | |
6555 | break; | |
6556 | } | |
6557 | ret = i; | |
6558 | s_non_relative = p; | |
6559 | ||
6560 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
6561 | for (; i < count; i++, p += sort_elt) | |
6562 | { | |
6563 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
6564 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
6565 | sq = sp; | |
6566 | sp->u.offset = sq->rela->r_offset; | |
6567 | } | |
6568 | ||
6569 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
6570 | ||
8423293d | 6571 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6572 | if (lo->type == bfd_indirect_link_order) |
6573 | { | |
6574 | bfd_byte *erel, *erelend; | |
6575 | asection *o = lo->u.indirect.section; | |
6576 | ||
6577 | erel = o->contents; | |
eea6121a | 6578 | erelend = o->contents + o->size; |
c152c796 AM |
6579 | p = sort + o->output_offset / ext_size * sort_elt; |
6580 | while (erel < erelend) | |
6581 | { | |
6582 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6583 | (*swap_out) (abfd, s->rela, erel); | |
6584 | p += sort_elt; | |
6585 | erel += ext_size; | |
6586 | } | |
6587 | } | |
6588 | ||
6589 | free (sort); | |
6590 | *psec = reldyn; | |
6591 | return ret; | |
6592 | } | |
6593 | ||
6594 | /* Flush the output symbols to the file. */ | |
6595 | ||
6596 | static bfd_boolean | |
6597 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
6598 | const struct elf_backend_data *bed) | |
6599 | { | |
6600 | if (finfo->symbuf_count > 0) | |
6601 | { | |
6602 | Elf_Internal_Shdr *hdr; | |
6603 | file_ptr pos; | |
6604 | bfd_size_type amt; | |
6605 | ||
6606 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
6607 | pos = hdr->sh_offset + hdr->sh_size; | |
6608 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
6609 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
6610 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
6611 | return FALSE; | |
6612 | ||
6613 | hdr->sh_size += amt; | |
6614 | finfo->symbuf_count = 0; | |
6615 | } | |
6616 | ||
6617 | return TRUE; | |
6618 | } | |
6619 | ||
6620 | /* Add a symbol to the output symbol table. */ | |
6621 | ||
6622 | static bfd_boolean | |
6623 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
6624 | const char *name, | |
6625 | Elf_Internal_Sym *elfsym, | |
6626 | asection *input_sec, | |
6627 | struct elf_link_hash_entry *h) | |
6628 | { | |
6629 | bfd_byte *dest; | |
6630 | Elf_External_Sym_Shndx *destshndx; | |
6631 | bfd_boolean (*output_symbol_hook) | |
6632 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
6633 | struct elf_link_hash_entry *); | |
6634 | const struct elf_backend_data *bed; | |
6635 | ||
6636 | bed = get_elf_backend_data (finfo->output_bfd); | |
6637 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
6638 | if (output_symbol_hook != NULL) | |
6639 | { | |
6640 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
6641 | return FALSE; | |
6642 | } | |
6643 | ||
6644 | if (name == NULL || *name == '\0') | |
6645 | elfsym->st_name = 0; | |
6646 | else if (input_sec->flags & SEC_EXCLUDE) | |
6647 | elfsym->st_name = 0; | |
6648 | else | |
6649 | { | |
6650 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
6651 | name, TRUE, FALSE); | |
6652 | if (elfsym->st_name == (unsigned long) -1) | |
6653 | return FALSE; | |
6654 | } | |
6655 | ||
6656 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
6657 | { | |
6658 | if (! elf_link_flush_output_syms (finfo, bed)) | |
6659 | return FALSE; | |
6660 | } | |
6661 | ||
6662 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
6663 | destshndx = finfo->symshndxbuf; | |
6664 | if (destshndx != NULL) | |
6665 | { | |
6666 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
6667 | { | |
6668 | bfd_size_type amt; | |
6669 | ||
6670 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
6671 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
6672 | if (destshndx == NULL) | |
6673 | return FALSE; | |
6674 | memset ((char *) destshndx + amt, 0, amt); | |
6675 | finfo->shndxbuf_size *= 2; | |
6676 | } | |
6677 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
6678 | } | |
6679 | ||
6680 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
6681 | finfo->symbuf_count += 1; | |
6682 | bfd_get_symcount (finfo->output_bfd) += 1; | |
6683 | ||
6684 | return TRUE; | |
6685 | } | |
6686 | ||
c0d5a53d L |
6687 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
6688 | ||
6689 | static bfd_boolean | |
6690 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
6691 | { | |
6692 | if (sym->st_shndx > SHN_HIRESERVE) | |
6693 | { | |
6694 | /* The gABI doesn't support dynamic symbols in output sections | |
6695 | beyond 64k. */ | |
6696 | (*_bfd_error_handler) | |
6697 | (_("%B: Too many sections: %d (>= %d)"), | |
6698 | abfd, bfd_count_sections (abfd), SHN_LORESERVE); | |
6699 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6700 | return FALSE; | |
6701 | } | |
6702 | return TRUE; | |
6703 | } | |
6704 | ||
c152c796 AM |
6705 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
6706 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
6707 | versioned symbol that would normally require an explicit version. | |
6708 | We also handle the case that a DSO references a hidden symbol | |
6709 | which may be satisfied by a versioned symbol in another DSO. */ | |
6710 | ||
6711 | static bfd_boolean | |
6712 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
6713 | const struct elf_backend_data *bed, | |
6714 | struct elf_link_hash_entry *h) | |
6715 | { | |
6716 | bfd *abfd; | |
6717 | struct elf_link_loaded_list *loaded; | |
6718 | ||
6719 | if (!is_elf_hash_table (info->hash)) | |
6720 | return FALSE; | |
6721 | ||
6722 | switch (h->root.type) | |
6723 | { | |
6724 | default: | |
6725 | abfd = NULL; | |
6726 | break; | |
6727 | ||
6728 | case bfd_link_hash_undefined: | |
6729 | case bfd_link_hash_undefweak: | |
6730 | abfd = h->root.u.undef.abfd; | |
6731 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 6732 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
6733 | return FALSE; |
6734 | break; | |
6735 | ||
6736 | case bfd_link_hash_defined: | |
6737 | case bfd_link_hash_defweak: | |
6738 | abfd = h->root.u.def.section->owner; | |
6739 | break; | |
6740 | ||
6741 | case bfd_link_hash_common: | |
6742 | abfd = h->root.u.c.p->section->owner; | |
6743 | break; | |
6744 | } | |
6745 | BFD_ASSERT (abfd != NULL); | |
6746 | ||
6747 | for (loaded = elf_hash_table (info)->loaded; | |
6748 | loaded != NULL; | |
6749 | loaded = loaded->next) | |
6750 | { | |
6751 | bfd *input; | |
6752 | Elf_Internal_Shdr *hdr; | |
6753 | bfd_size_type symcount; | |
6754 | bfd_size_type extsymcount; | |
6755 | bfd_size_type extsymoff; | |
6756 | Elf_Internal_Shdr *versymhdr; | |
6757 | Elf_Internal_Sym *isym; | |
6758 | Elf_Internal_Sym *isymend; | |
6759 | Elf_Internal_Sym *isymbuf; | |
6760 | Elf_External_Versym *ever; | |
6761 | Elf_External_Versym *extversym; | |
6762 | ||
6763 | input = loaded->abfd; | |
6764 | ||
6765 | /* We check each DSO for a possible hidden versioned definition. */ | |
6766 | if (input == abfd | |
6767 | || (input->flags & DYNAMIC) == 0 | |
6768 | || elf_dynversym (input) == 0) | |
6769 | continue; | |
6770 | ||
6771 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
6772 | ||
6773 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
6774 | if (elf_bad_symtab (input)) | |
6775 | { | |
6776 | extsymcount = symcount; | |
6777 | extsymoff = 0; | |
6778 | } | |
6779 | else | |
6780 | { | |
6781 | extsymcount = symcount - hdr->sh_info; | |
6782 | extsymoff = hdr->sh_info; | |
6783 | } | |
6784 | ||
6785 | if (extsymcount == 0) | |
6786 | continue; | |
6787 | ||
6788 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
6789 | NULL, NULL, NULL); | |
6790 | if (isymbuf == NULL) | |
6791 | return FALSE; | |
6792 | ||
6793 | /* Read in any version definitions. */ | |
6794 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
6795 | extversym = bfd_malloc (versymhdr->sh_size); | |
6796 | if (extversym == NULL) | |
6797 | goto error_ret; | |
6798 | ||
6799 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
6800 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
6801 | != versymhdr->sh_size)) | |
6802 | { | |
6803 | free (extversym); | |
6804 | error_ret: | |
6805 | free (isymbuf); | |
6806 | return FALSE; | |
6807 | } | |
6808 | ||
6809 | ever = extversym + extsymoff; | |
6810 | isymend = isymbuf + extsymcount; | |
6811 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
6812 | { | |
6813 | const char *name; | |
6814 | Elf_Internal_Versym iver; | |
6815 | unsigned short version_index; | |
6816 | ||
6817 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
6818 | || isym->st_shndx == SHN_UNDEF) | |
6819 | continue; | |
6820 | ||
6821 | name = bfd_elf_string_from_elf_section (input, | |
6822 | hdr->sh_link, | |
6823 | isym->st_name); | |
6824 | if (strcmp (name, h->root.root.string) != 0) | |
6825 | continue; | |
6826 | ||
6827 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
6828 | ||
6829 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
6830 | { | |
6831 | /* If we have a non-hidden versioned sym, then it should | |
6832 | have provided a definition for the undefined sym. */ | |
6833 | abort (); | |
6834 | } | |
6835 | ||
6836 | version_index = iver.vs_vers & VERSYM_VERSION; | |
6837 | if (version_index == 1 || version_index == 2) | |
6838 | { | |
6839 | /* This is the base or first version. We can use it. */ | |
6840 | free (extversym); | |
6841 | free (isymbuf); | |
6842 | return TRUE; | |
6843 | } | |
6844 | } | |
6845 | ||
6846 | free (extversym); | |
6847 | free (isymbuf); | |
6848 | } | |
6849 | ||
6850 | return FALSE; | |
6851 | } | |
6852 | ||
6853 | /* Add an external symbol to the symbol table. This is called from | |
6854 | the hash table traversal routine. When generating a shared object, | |
6855 | we go through the symbol table twice. The first time we output | |
6856 | anything that might have been forced to local scope in a version | |
6857 | script. The second time we output the symbols that are still | |
6858 | global symbols. */ | |
6859 | ||
6860 | static bfd_boolean | |
6861 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
6862 | { | |
6863 | struct elf_outext_info *eoinfo = data; | |
6864 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
6865 | bfd_boolean strip; | |
6866 | Elf_Internal_Sym sym; | |
6867 | asection *input_sec; | |
6868 | const struct elf_backend_data *bed; | |
6869 | ||
6870 | if (h->root.type == bfd_link_hash_warning) | |
6871 | { | |
6872 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6873 | if (h->root.type == bfd_link_hash_new) | |
6874 | return TRUE; | |
6875 | } | |
6876 | ||
6877 | /* Decide whether to output this symbol in this pass. */ | |
6878 | if (eoinfo->localsyms) | |
6879 | { | |
f5385ebf | 6880 | if (!h->forced_local) |
c152c796 AM |
6881 | return TRUE; |
6882 | } | |
6883 | else | |
6884 | { | |
f5385ebf | 6885 | if (h->forced_local) |
c152c796 AM |
6886 | return TRUE; |
6887 | } | |
6888 | ||
6889 | bed = get_elf_backend_data (finfo->output_bfd); | |
6890 | ||
12ac1cf5 | 6891 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 6892 | { |
12ac1cf5 NC |
6893 | /* If we have an undefined symbol reference here then it must have |
6894 | come from a shared library that is being linked in. (Undefined | |
6895 | references in regular files have already been handled). */ | |
6896 | bfd_boolean ignore_undef = FALSE; | |
6897 | ||
6898 | /* Some symbols may be special in that the fact that they're | |
6899 | undefined can be safely ignored - let backend determine that. */ | |
6900 | if (bed->elf_backend_ignore_undef_symbol) | |
6901 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
6902 | ||
6903 | /* If we are reporting errors for this situation then do so now. */ | |
6904 | if (ignore_undef == FALSE | |
6905 | && h->ref_dynamic | |
6906 | && ! h->ref_regular | |
6907 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) | |
6908 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
c152c796 | 6909 | { |
12ac1cf5 NC |
6910 | if (! (finfo->info->callbacks->undefined_symbol |
6911 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
6912 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
6913 | { | |
6914 | eoinfo->failed = TRUE; | |
6915 | return FALSE; | |
6916 | } | |
c152c796 AM |
6917 | } |
6918 | } | |
6919 | ||
6920 | /* We should also warn if a forced local symbol is referenced from | |
6921 | shared libraries. */ | |
6922 | if (! finfo->info->relocatable | |
6923 | && (! finfo->info->shared) | |
f5385ebf AM |
6924 | && h->forced_local |
6925 | && h->ref_dynamic | |
6926 | && !h->dynamic_def | |
6927 | && !h->dynamic_weak | |
c152c796 AM |
6928 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
6929 | { | |
6930 | (*_bfd_error_handler) | |
d003868e | 6931 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
6932 | finfo->output_bfd, |
6933 | h->root.u.def.section == bfd_abs_section_ptr | |
6934 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
6935 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
6936 | ? "internal" | |
6937 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
6938 | ? "hidden" : "local", |
6939 | h->root.root.string); | |
c152c796 AM |
6940 | eoinfo->failed = TRUE; |
6941 | return FALSE; | |
6942 | } | |
6943 | ||
6944 | /* We don't want to output symbols that have never been mentioned by | |
6945 | a regular file, or that we have been told to strip. However, if | |
6946 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6947 | output it. */ | |
6948 | if (h->indx == -2) | |
6949 | strip = FALSE; | |
f5385ebf | 6950 | else if ((h->def_dynamic |
77cfaee6 AM |
6951 | || h->ref_dynamic |
6952 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
6953 | && !h->def_regular |
6954 | && !h->ref_regular) | |
c152c796 AM |
6955 | strip = TRUE; |
6956 | else if (finfo->info->strip == strip_all) | |
6957 | strip = TRUE; | |
6958 | else if (finfo->info->strip == strip_some | |
6959 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6960 | h->root.root.string, FALSE, FALSE) == NULL) | |
6961 | strip = TRUE; | |
6962 | else if (finfo->info->strip_discarded | |
6963 | && (h->root.type == bfd_link_hash_defined | |
6964 | || h->root.type == bfd_link_hash_defweak) | |
6965 | && elf_discarded_section (h->root.u.def.section)) | |
6966 | strip = TRUE; | |
6967 | else | |
6968 | strip = FALSE; | |
6969 | ||
6970 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6971 | nothing else to do unless it is a forced local symbol. */ | |
6972 | if (strip | |
6973 | && h->dynindx == -1 | |
f5385ebf | 6974 | && !h->forced_local) |
c152c796 AM |
6975 | return TRUE; |
6976 | ||
6977 | sym.st_value = 0; | |
6978 | sym.st_size = h->size; | |
6979 | sym.st_other = h->other; | |
f5385ebf | 6980 | if (h->forced_local) |
c152c796 AM |
6981 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
6982 | else if (h->root.type == bfd_link_hash_undefweak | |
6983 | || h->root.type == bfd_link_hash_defweak) | |
6984 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6985 | else | |
6986 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6987 | ||
6988 | switch (h->root.type) | |
6989 | { | |
6990 | default: | |
6991 | case bfd_link_hash_new: | |
6992 | case bfd_link_hash_warning: | |
6993 | abort (); | |
6994 | return FALSE; | |
6995 | ||
6996 | case bfd_link_hash_undefined: | |
6997 | case bfd_link_hash_undefweak: | |
6998 | input_sec = bfd_und_section_ptr; | |
6999 | sym.st_shndx = SHN_UNDEF; | |
7000 | break; | |
7001 | ||
7002 | case bfd_link_hash_defined: | |
7003 | case bfd_link_hash_defweak: | |
7004 | { | |
7005 | input_sec = h->root.u.def.section; | |
7006 | if (input_sec->output_section != NULL) | |
7007 | { | |
7008 | sym.st_shndx = | |
7009 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
7010 | input_sec->output_section); | |
7011 | if (sym.st_shndx == SHN_BAD) | |
7012 | { | |
7013 | (*_bfd_error_handler) | |
d003868e AM |
7014 | (_("%B: could not find output section %A for input section %A"), |
7015 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
7016 | eoinfo->failed = TRUE; |
7017 | return FALSE; | |
7018 | } | |
7019 | ||
7020 | /* ELF symbols in relocatable files are section relative, | |
7021 | but in nonrelocatable files they are virtual | |
7022 | addresses. */ | |
7023 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
7024 | if (! finfo->info->relocatable) | |
7025 | { | |
7026 | sym.st_value += input_sec->output_section->vma; | |
7027 | if (h->type == STT_TLS) | |
7028 | { | |
7029 | /* STT_TLS symbols are relative to PT_TLS segment | |
7030 | base. */ | |
7031 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
7032 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
7033 | } | |
7034 | } | |
7035 | } | |
7036 | else | |
7037 | { | |
7038 | BFD_ASSERT (input_sec->owner == NULL | |
7039 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
7040 | sym.st_shndx = SHN_UNDEF; | |
7041 | input_sec = bfd_und_section_ptr; | |
7042 | } | |
7043 | } | |
7044 | break; | |
7045 | ||
7046 | case bfd_link_hash_common: | |
7047 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 7048 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
7049 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
7050 | break; | |
7051 | ||
7052 | case bfd_link_hash_indirect: | |
7053 | /* These symbols are created by symbol versioning. They point | |
7054 | to the decorated version of the name. For example, if the | |
7055 | symbol foo@@GNU_1.2 is the default, which should be used when | |
7056 | foo is used with no version, then we add an indirect symbol | |
7057 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
7058 | since the indirected symbol is already in the hash table. */ | |
7059 | return TRUE; | |
7060 | } | |
7061 | ||
7062 | /* Give the processor backend a chance to tweak the symbol value, | |
7063 | and also to finish up anything that needs to be done for this | |
7064 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
7065 | forced local syms when non-shared is due to a historical quirk. */ | |
7066 | if ((h->dynindx != -1 | |
f5385ebf | 7067 | || h->forced_local) |
c152c796 AM |
7068 | && ((finfo->info->shared |
7069 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
7070 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 7071 | || !h->forced_local) |
c152c796 AM |
7072 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
7073 | { | |
7074 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
7075 | (finfo->output_bfd, finfo->info, h, &sym))) | |
7076 | { | |
7077 | eoinfo->failed = TRUE; | |
7078 | return FALSE; | |
7079 | } | |
7080 | } | |
7081 | ||
7082 | /* If we are marking the symbol as undefined, and there are no | |
7083 | non-weak references to this symbol from a regular object, then | |
7084 | mark the symbol as weak undefined; if there are non-weak | |
7085 | references, mark the symbol as strong. We can't do this earlier, | |
7086 | because it might not be marked as undefined until the | |
7087 | finish_dynamic_symbol routine gets through with it. */ | |
7088 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 7089 | && h->ref_regular |
c152c796 AM |
7090 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
7091 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
7092 | { | |
7093 | int bindtype; | |
7094 | ||
f5385ebf | 7095 | if (h->ref_regular_nonweak) |
c152c796 AM |
7096 | bindtype = STB_GLOBAL; |
7097 | else | |
7098 | bindtype = STB_WEAK; | |
7099 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
7100 | } | |
7101 | ||
7102 | /* If a non-weak symbol with non-default visibility is not defined | |
7103 | locally, it is a fatal error. */ | |
7104 | if (! finfo->info->relocatable | |
7105 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
7106 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
7107 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 7108 | && !h->def_regular) |
c152c796 AM |
7109 | { |
7110 | (*_bfd_error_handler) | |
d003868e AM |
7111 | (_("%B: %s symbol `%s' isn't defined"), |
7112 | finfo->output_bfd, | |
7113 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
7114 | ? "protected" | |
7115 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
7116 | ? "internal" : "hidden", | |
7117 | h->root.root.string); | |
c152c796 AM |
7118 | eoinfo->failed = TRUE; |
7119 | return FALSE; | |
7120 | } | |
7121 | ||
7122 | /* If this symbol should be put in the .dynsym section, then put it | |
7123 | there now. We already know the symbol index. We also fill in | |
7124 | the entry in the .hash section. */ | |
7125 | if (h->dynindx != -1 | |
7126 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
7127 | { | |
c152c796 AM |
7128 | bfd_byte *esym; |
7129 | ||
7130 | sym.st_name = h->dynstr_index; | |
7131 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
c0d5a53d L |
7132 | if (! check_dynsym (finfo->output_bfd, &sym)) |
7133 | { | |
7134 | eoinfo->failed = TRUE; | |
7135 | return FALSE; | |
7136 | } | |
c152c796 AM |
7137 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); |
7138 | ||
fdc90cb4 JJ |
7139 | if (finfo->hash_sec != NULL) |
7140 | { | |
7141 | size_t hash_entry_size; | |
7142 | bfd_byte *bucketpos; | |
7143 | bfd_vma chain; | |
41198d0c L |
7144 | size_t bucketcount; |
7145 | size_t bucket; | |
7146 | ||
7147 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
7148 | bucket = h->u.elf_hash_value % bucketcount; | |
fdc90cb4 JJ |
7149 | |
7150 | hash_entry_size | |
7151 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
7152 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
7153 | + (bucket + 2) * hash_entry_size); | |
7154 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
7155 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
7156 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
7157 | ((bfd_byte *) finfo->hash_sec->contents | |
7158 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
7159 | } | |
c152c796 AM |
7160 | |
7161 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
7162 | { | |
7163 | Elf_Internal_Versym iversym; | |
7164 | Elf_External_Versym *eversym; | |
7165 | ||
f5385ebf | 7166 | if (!h->def_regular) |
c152c796 AM |
7167 | { |
7168 | if (h->verinfo.verdef == NULL) | |
7169 | iversym.vs_vers = 0; | |
7170 | else | |
7171 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
7172 | } | |
7173 | else | |
7174 | { | |
7175 | if (h->verinfo.vertree == NULL) | |
7176 | iversym.vs_vers = 1; | |
7177 | else | |
7178 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
7179 | if (finfo->info->create_default_symver) |
7180 | iversym.vs_vers++; | |
c152c796 AM |
7181 | } |
7182 | ||
f5385ebf | 7183 | if (h->hidden) |
c152c796 AM |
7184 | iversym.vs_vers |= VERSYM_HIDDEN; |
7185 | ||
7186 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
7187 | eversym += h->dynindx; | |
7188 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
7189 | } | |
7190 | } | |
7191 | ||
7192 | /* If we're stripping it, then it was just a dynamic symbol, and | |
7193 | there's nothing else to do. */ | |
7194 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
7195 | return TRUE; | |
7196 | ||
7197 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
7198 | ||
7199 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
7200 | { | |
7201 | eoinfo->failed = TRUE; | |
7202 | return FALSE; | |
7203 | } | |
7204 | ||
7205 | return TRUE; | |
7206 | } | |
7207 | ||
cdd3575c AM |
7208 | /* Return TRUE if special handling is done for relocs in SEC against |
7209 | symbols defined in discarded sections. */ | |
7210 | ||
c152c796 AM |
7211 | static bfd_boolean |
7212 | elf_section_ignore_discarded_relocs (asection *sec) | |
7213 | { | |
7214 | const struct elf_backend_data *bed; | |
7215 | ||
cdd3575c AM |
7216 | switch (sec->sec_info_type) |
7217 | { | |
7218 | case ELF_INFO_TYPE_STABS: | |
7219 | case ELF_INFO_TYPE_EH_FRAME: | |
7220 | return TRUE; | |
7221 | default: | |
7222 | break; | |
7223 | } | |
c152c796 AM |
7224 | |
7225 | bed = get_elf_backend_data (sec->owner); | |
7226 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
7227 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
7228 | return TRUE; | |
7229 | ||
7230 | return FALSE; | |
7231 | } | |
7232 | ||
9e66c942 AM |
7233 | /* Return a mask saying how ld should treat relocations in SEC against |
7234 | symbols defined in discarded sections. If this function returns | |
7235 | COMPLAIN set, ld will issue a warning message. If this function | |
7236 | returns PRETEND set, and the discarded section was link-once and the | |
7237 | same size as the kept link-once section, ld will pretend that the | |
7238 | symbol was actually defined in the kept section. Otherwise ld will | |
7239 | zero the reloc (at least that is the intent, but some cooperation by | |
7240 | the target dependent code is needed, particularly for REL targets). */ | |
7241 | ||
8a696751 AM |
7242 | unsigned int |
7243 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 7244 | { |
9e66c942 | 7245 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 7246 | return PRETEND; |
cdd3575c AM |
7247 | |
7248 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 7249 | return 0; |
cdd3575c AM |
7250 | |
7251 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 7252 | return 0; |
cdd3575c | 7253 | |
9e66c942 | 7254 | return COMPLAIN | PRETEND; |
cdd3575c AM |
7255 | } |
7256 | ||
3d7f7666 L |
7257 | /* Find a match between a section and a member of a section group. */ |
7258 | ||
7259 | static asection * | |
c0f00686 L |
7260 | match_group_member (asection *sec, asection *group, |
7261 | struct bfd_link_info *info) | |
3d7f7666 L |
7262 | { |
7263 | asection *first = elf_next_in_group (group); | |
7264 | asection *s = first; | |
7265 | ||
7266 | while (s != NULL) | |
7267 | { | |
c0f00686 | 7268 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
7269 | return s; |
7270 | ||
83180ade | 7271 | s = elf_next_in_group (s); |
3d7f7666 L |
7272 | if (s == first) |
7273 | break; | |
7274 | } | |
7275 | ||
7276 | return NULL; | |
7277 | } | |
7278 | ||
01b3c8ab | 7279 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
7280 | to replace it. Return the replacement if it is OK. Otherwise return |
7281 | NULL. */ | |
01b3c8ab L |
7282 | |
7283 | asection * | |
c0f00686 | 7284 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
7285 | { |
7286 | asection *kept; | |
7287 | ||
7288 | kept = sec->kept_section; | |
7289 | if (kept != NULL) | |
7290 | { | |
c2370991 | 7291 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 7292 | kept = match_group_member (sec, kept, info); |
01b3c8ab L |
7293 | if (kept != NULL && sec->size != kept->size) |
7294 | kept = NULL; | |
c2370991 | 7295 | sec->kept_section = kept; |
01b3c8ab L |
7296 | } |
7297 | return kept; | |
7298 | } | |
7299 | ||
c152c796 AM |
7300 | /* Link an input file into the linker output file. This function |
7301 | handles all the sections and relocations of the input file at once. | |
7302 | This is so that we only have to read the local symbols once, and | |
7303 | don't have to keep them in memory. */ | |
7304 | ||
7305 | static bfd_boolean | |
7306 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
7307 | { | |
7308 | bfd_boolean (*relocate_section) | |
7309 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
7310 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
7311 | bfd *output_bfd; | |
7312 | Elf_Internal_Shdr *symtab_hdr; | |
7313 | size_t locsymcount; | |
7314 | size_t extsymoff; | |
7315 | Elf_Internal_Sym *isymbuf; | |
7316 | Elf_Internal_Sym *isym; | |
7317 | Elf_Internal_Sym *isymend; | |
7318 | long *pindex; | |
7319 | asection **ppsection; | |
7320 | asection *o; | |
7321 | const struct elf_backend_data *bed; | |
7322 | bfd_boolean emit_relocs; | |
7323 | struct elf_link_hash_entry **sym_hashes; | |
7324 | ||
7325 | output_bfd = finfo->output_bfd; | |
7326 | bed = get_elf_backend_data (output_bfd); | |
7327 | relocate_section = bed->elf_backend_relocate_section; | |
7328 | ||
7329 | /* If this is a dynamic object, we don't want to do anything here: | |
7330 | we don't want the local symbols, and we don't want the section | |
7331 | contents. */ | |
7332 | if ((input_bfd->flags & DYNAMIC) != 0) | |
7333 | return TRUE; | |
7334 | ||
7335 | emit_relocs = (finfo->info->relocatable | |
eac338cf | 7336 | || finfo->info->emitrelocations); |
c152c796 AM |
7337 | |
7338 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
7339 | if (elf_bad_symtab (input_bfd)) | |
7340 | { | |
7341 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
7342 | extsymoff = 0; | |
7343 | } | |
7344 | else | |
7345 | { | |
7346 | locsymcount = symtab_hdr->sh_info; | |
7347 | extsymoff = symtab_hdr->sh_info; | |
7348 | } | |
7349 | ||
7350 | /* Read the local symbols. */ | |
7351 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
7352 | if (isymbuf == NULL && locsymcount != 0) | |
7353 | { | |
7354 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
7355 | finfo->internal_syms, | |
7356 | finfo->external_syms, | |
7357 | finfo->locsym_shndx); | |
7358 | if (isymbuf == NULL) | |
7359 | return FALSE; | |
7360 | } | |
7361 | ||
7362 | /* Find local symbol sections and adjust values of symbols in | |
7363 | SEC_MERGE sections. Write out those local symbols we know are | |
7364 | going into the output file. */ | |
7365 | isymend = isymbuf + locsymcount; | |
7366 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
7367 | isym < isymend; | |
7368 | isym++, pindex++, ppsection++) | |
7369 | { | |
7370 | asection *isec; | |
7371 | const char *name; | |
7372 | Elf_Internal_Sym osym; | |
7373 | ||
7374 | *pindex = -1; | |
7375 | ||
7376 | if (elf_bad_symtab (input_bfd)) | |
7377 | { | |
7378 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
7379 | { | |
7380 | *ppsection = NULL; | |
7381 | continue; | |
7382 | } | |
7383 | } | |
7384 | ||
7385 | if (isym->st_shndx == SHN_UNDEF) | |
7386 | isec = bfd_und_section_ptr; | |
7387 | else if (isym->st_shndx < SHN_LORESERVE | |
7388 | || isym->st_shndx > SHN_HIRESERVE) | |
7389 | { | |
7390 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
7391 | if (isec | |
7392 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
7393 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
7394 | isym->st_value = | |
7395 | _bfd_merged_section_offset (output_bfd, &isec, | |
7396 | elf_section_data (isec)->sec_info, | |
753731ee | 7397 | isym->st_value); |
c152c796 AM |
7398 | } |
7399 | else if (isym->st_shndx == SHN_ABS) | |
7400 | isec = bfd_abs_section_ptr; | |
7401 | else if (isym->st_shndx == SHN_COMMON) | |
7402 | isec = bfd_com_section_ptr; | |
7403 | else | |
7404 | { | |
f02571c5 AM |
7405 | /* Don't attempt to output symbols with st_shnx in the |
7406 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
7407 | *ppsection = NULL; | |
7408 | continue; | |
c152c796 AM |
7409 | } |
7410 | ||
7411 | *ppsection = isec; | |
7412 | ||
7413 | /* Don't output the first, undefined, symbol. */ | |
7414 | if (ppsection == finfo->sections) | |
7415 | continue; | |
7416 | ||
7417 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
7418 | { | |
7419 | /* We never output section symbols. Instead, we use the | |
7420 | section symbol of the corresponding section in the output | |
7421 | file. */ | |
7422 | continue; | |
7423 | } | |
7424 | ||
7425 | /* If we are stripping all symbols, we don't want to output this | |
7426 | one. */ | |
7427 | if (finfo->info->strip == strip_all) | |
7428 | continue; | |
7429 | ||
7430 | /* If we are discarding all local symbols, we don't want to | |
7431 | output this one. If we are generating a relocatable output | |
7432 | file, then some of the local symbols may be required by | |
7433 | relocs; we output them below as we discover that they are | |
7434 | needed. */ | |
7435 | if (finfo->info->discard == discard_all) | |
7436 | continue; | |
7437 | ||
7438 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
7439 | discarding, we don't need to keep it. */ |
7440 | if (isym->st_shndx != SHN_UNDEF | |
7441 | && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
ccf5f610 | 7442 | && (isec == NULL |
f02571c5 AM |
7443 | || bfd_section_removed_from_list (output_bfd, |
7444 | isec->output_section))) | |
e75a280b L |
7445 | continue; |
7446 | ||
c152c796 AM |
7447 | /* Get the name of the symbol. */ |
7448 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
7449 | isym->st_name); | |
7450 | if (name == NULL) | |
7451 | return FALSE; | |
7452 | ||
7453 | /* See if we are discarding symbols with this name. */ | |
7454 | if ((finfo->info->strip == strip_some | |
7455 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
7456 | == NULL)) | |
7457 | || (((finfo->info->discard == discard_sec_merge | |
7458 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
7459 | || finfo->info->discard == discard_l) | |
7460 | && bfd_is_local_label_name (input_bfd, name))) | |
7461 | continue; | |
7462 | ||
7463 | /* If we get here, we are going to output this symbol. */ | |
7464 | ||
7465 | osym = *isym; | |
7466 | ||
7467 | /* Adjust the section index for the output file. */ | |
7468 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
7469 | isec->output_section); | |
7470 | if (osym.st_shndx == SHN_BAD) | |
7471 | return FALSE; | |
7472 | ||
7473 | *pindex = bfd_get_symcount (output_bfd); | |
7474 | ||
7475 | /* ELF symbols in relocatable files are section relative, but | |
7476 | in executable files they are virtual addresses. Note that | |
7477 | this code assumes that all ELF sections have an associated | |
7478 | BFD section with a reasonable value for output_offset; below | |
7479 | we assume that they also have a reasonable value for | |
7480 | output_section. Any special sections must be set up to meet | |
7481 | these requirements. */ | |
7482 | osym.st_value += isec->output_offset; | |
7483 | if (! finfo->info->relocatable) | |
7484 | { | |
7485 | osym.st_value += isec->output_section->vma; | |
7486 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
7487 | { | |
7488 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
7489 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
7490 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
7491 | } | |
7492 | } | |
7493 | ||
7494 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
7495 | return FALSE; | |
7496 | } | |
7497 | ||
7498 | /* Relocate the contents of each section. */ | |
7499 | sym_hashes = elf_sym_hashes (input_bfd); | |
7500 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
7501 | { | |
7502 | bfd_byte *contents; | |
7503 | ||
7504 | if (! o->linker_mark) | |
7505 | { | |
7506 | /* This section was omitted from the link. */ | |
7507 | continue; | |
7508 | } | |
7509 | ||
7510 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 7511 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
7512 | continue; |
7513 | ||
7514 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
7515 | { | |
7516 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
7517 | or somesuch. */ | |
7518 | continue; | |
7519 | } | |
7520 | ||
7521 | /* Get the contents of the section. They have been cached by a | |
7522 | relaxation routine. Note that o is a section in an input | |
7523 | file, so the contents field will not have been set by any of | |
7524 | the routines which work on output files. */ | |
7525 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
7526 | contents = elf_section_data (o)->this_hdr.contents; | |
7527 | else | |
7528 | { | |
eea6121a AM |
7529 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
7530 | ||
c152c796 | 7531 | contents = finfo->contents; |
eea6121a | 7532 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
7533 | return FALSE; |
7534 | } | |
7535 | ||
7536 | if ((o->flags & SEC_RELOC) != 0) | |
7537 | { | |
7538 | Elf_Internal_Rela *internal_relocs; | |
7539 | bfd_vma r_type_mask; | |
7540 | int r_sym_shift; | |
7541 | ||
7542 | /* Get the swapped relocs. */ | |
7543 | internal_relocs | |
7544 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
7545 | finfo->internal_relocs, FALSE); | |
7546 | if (internal_relocs == NULL | |
7547 | && o->reloc_count > 0) | |
7548 | return FALSE; | |
7549 | ||
7550 | if (bed->s->arch_size == 32) | |
7551 | { | |
7552 | r_type_mask = 0xff; | |
7553 | r_sym_shift = 8; | |
7554 | } | |
7555 | else | |
7556 | { | |
7557 | r_type_mask = 0xffffffff; | |
7558 | r_sym_shift = 32; | |
7559 | } | |
7560 | ||
7561 | /* Run through the relocs looking for any against symbols | |
7562 | from discarded sections and section symbols from | |
7563 | removed link-once sections. Complain about relocs | |
7564 | against discarded sections. Zero relocs against removed | |
e0ae6d6f | 7565 | link-once sections. */ |
c152c796 AM |
7566 | if (!elf_section_ignore_discarded_relocs (o)) |
7567 | { | |
7568 | Elf_Internal_Rela *rel, *relend; | |
8a696751 | 7569 | unsigned int action = (*bed->action_discarded) (o); |
c152c796 AM |
7570 | |
7571 | rel = internal_relocs; | |
7572 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7573 | for ( ; rel < relend; rel++) | |
7574 | { | |
7575 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
7576 | asection **ps, *sec; |
7577 | struct elf_link_hash_entry *h = NULL; | |
7578 | const char *sym_name; | |
c152c796 | 7579 | |
ee75fd95 AM |
7580 | if (r_symndx == STN_UNDEF) |
7581 | continue; | |
7582 | ||
c152c796 AM |
7583 | if (r_symndx >= locsymcount |
7584 | || (elf_bad_symtab (input_bfd) | |
7585 | && finfo->sections[r_symndx] == NULL)) | |
7586 | { | |
c152c796 | 7587 | h = sym_hashes[r_symndx - extsymoff]; |
dce669a1 | 7588 | |
8c19749a NC |
7589 | /* Badly formatted input files can contain relocs that |
7590 | reference non-existant symbols. Check here so that | |
7591 | we do not seg fault. */ | |
7592 | if (h == NULL) | |
7593 | { | |
7594 | char buffer [32]; | |
7595 | ||
7596 | sprintf_vma (buffer, rel->r_info); | |
7597 | (*_bfd_error_handler) | |
7598 | (_("error: %B contains a reloc (0x%s) for section %A " | |
7599 | "that references a non-existent global symbol"), | |
7600 | input_bfd, o, buffer); | |
7601 | bfd_set_error (bfd_error_bad_value); | |
7602 | return FALSE; | |
7603 | } | |
3b36f7e6 | 7604 | |
c152c796 AM |
7605 | while (h->root.type == bfd_link_hash_indirect |
7606 | || h->root.type == bfd_link_hash_warning) | |
7607 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7608 | ||
cdd3575c AM |
7609 | if (h->root.type != bfd_link_hash_defined |
7610 | && h->root.type != bfd_link_hash_defweak) | |
7611 | continue; | |
7612 | ||
7613 | ps = &h->root.u.def.section; | |
7614 | sym_name = h->root.root.string; | |
c152c796 AM |
7615 | } |
7616 | else | |
7617 | { | |
cdd3575c AM |
7618 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
7619 | ps = &finfo->sections[r_symndx]; | |
26c61ae5 L |
7620 | sym_name = bfd_elf_sym_name (input_bfd, |
7621 | symtab_hdr, | |
7622 | sym, *ps); | |
cdd3575c | 7623 | } |
c152c796 | 7624 | |
cdd3575c AM |
7625 | /* Complain if the definition comes from a |
7626 | discarded section. */ | |
7627 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
7628 | { | |
87e5235d | 7629 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 7630 | if (action & COMPLAIN) |
e1fffbe6 AM |
7631 | (*finfo->info->callbacks->einfo) |
7632 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 7633 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 7634 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 7635 | |
87e5235d | 7636 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 7637 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
7638 | really defined in the kept linkonce section. |
7639 | FIXME: This is quite broken. Modifying the | |
7640 | symbol here means we will be changing all later | |
e0ae6d6f | 7641 | uses of the symbol, not just in this section. */ |
01b3c8ab | 7642 | if (action & PRETEND) |
87e5235d | 7643 | { |
01b3c8ab L |
7644 | asection *kept; |
7645 | ||
c0f00686 L |
7646 | kept = _bfd_elf_check_kept_section (sec, |
7647 | finfo->info); | |
01b3c8ab | 7648 | if (kept != NULL) |
87e5235d AM |
7649 | { |
7650 | *ps = kept; | |
7651 | continue; | |
7652 | } | |
7653 | } | |
7654 | ||
cdd3575c AM |
7655 | /* Remove the symbol reference from the reloc, but |
7656 | don't kill the reloc completely. This is so that | |
7657 | a zero value will be written into the section, | |
7658 | which may have non-zero contents put there by the | |
7659 | assembler. Zero in things like an eh_frame fde | |
7660 | pc_begin allows stack unwinders to recognize the | |
7661 | fde as bogus. */ | |
7662 | rel->r_info &= r_type_mask; | |
7663 | rel->r_addend = 0; | |
c152c796 AM |
7664 | } |
7665 | } | |
7666 | } | |
7667 | ||
7668 | /* Relocate the section by invoking a back end routine. | |
7669 | ||
7670 | The back end routine is responsible for adjusting the | |
7671 | section contents as necessary, and (if using Rela relocs | |
7672 | and generating a relocatable output file) adjusting the | |
7673 | reloc addend as necessary. | |
7674 | ||
7675 | The back end routine does not have to worry about setting | |
7676 | the reloc address or the reloc symbol index. | |
7677 | ||
7678 | The back end routine is given a pointer to the swapped in | |
7679 | internal symbols, and can access the hash table entries | |
7680 | for the external symbols via elf_sym_hashes (input_bfd). | |
7681 | ||
7682 | When generating relocatable output, the back end routine | |
7683 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
7684 | output symbol is going to be a section symbol | |
7685 | corresponding to the output section, which will require | |
7686 | the addend to be adjusted. */ | |
7687 | ||
7688 | if (! (*relocate_section) (output_bfd, finfo->info, | |
7689 | input_bfd, o, contents, | |
7690 | internal_relocs, | |
7691 | isymbuf, | |
7692 | finfo->sections)) | |
7693 | return FALSE; | |
7694 | ||
7695 | if (emit_relocs) | |
7696 | { | |
7697 | Elf_Internal_Rela *irela; | |
7698 | Elf_Internal_Rela *irelaend; | |
7699 | bfd_vma last_offset; | |
7700 | struct elf_link_hash_entry **rel_hash; | |
eac338cf | 7701 | struct elf_link_hash_entry **rel_hash_list; |
c152c796 AM |
7702 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; |
7703 | unsigned int next_erel; | |
c152c796 AM |
7704 | bfd_boolean rela_normal; |
7705 | ||
7706 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
7707 | rela_normal = (bed->rela_normal | |
7708 | && (input_rel_hdr->sh_entsize | |
7709 | == bed->s->sizeof_rela)); | |
7710 | ||
7711 | /* Adjust the reloc addresses and symbol indices. */ | |
7712 | ||
7713 | irela = internal_relocs; | |
7714 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7715 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
7716 | + elf_section_data (o->output_section)->rel_count | |
7717 | + elf_section_data (o->output_section)->rel_count2); | |
eac338cf | 7718 | rel_hash_list = rel_hash; |
c152c796 AM |
7719 | last_offset = o->output_offset; |
7720 | if (!finfo->info->relocatable) | |
7721 | last_offset += o->output_section->vma; | |
7722 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
7723 | { | |
7724 | unsigned long r_symndx; | |
7725 | asection *sec; | |
7726 | Elf_Internal_Sym sym; | |
7727 | ||
7728 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
7729 | { | |
7730 | rel_hash++; | |
7731 | next_erel = 0; | |
7732 | } | |
7733 | ||
7734 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
7735 | finfo->info, o, | |
7736 | irela->r_offset); | |
7737 | if (irela->r_offset >= (bfd_vma) -2) | |
7738 | { | |
7739 | /* This is a reloc for a deleted entry or somesuch. | |
7740 | Turn it into an R_*_NONE reloc, at the same | |
7741 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 7742 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
7743 | being ordered. */ |
7744 | irela->r_offset = last_offset; | |
7745 | irela->r_info = 0; | |
7746 | irela->r_addend = 0; | |
7747 | continue; | |
7748 | } | |
7749 | ||
7750 | irela->r_offset += o->output_offset; | |
7751 | ||
7752 | /* Relocs in an executable have to be virtual addresses. */ | |
7753 | if (!finfo->info->relocatable) | |
7754 | irela->r_offset += o->output_section->vma; | |
7755 | ||
7756 | last_offset = irela->r_offset; | |
7757 | ||
7758 | r_symndx = irela->r_info >> r_sym_shift; | |
7759 | if (r_symndx == STN_UNDEF) | |
7760 | continue; | |
7761 | ||
7762 | if (r_symndx >= locsymcount | |
7763 | || (elf_bad_symtab (input_bfd) | |
7764 | && finfo->sections[r_symndx] == NULL)) | |
7765 | { | |
7766 | struct elf_link_hash_entry *rh; | |
7767 | unsigned long indx; | |
7768 | ||
7769 | /* This is a reloc against a global symbol. We | |
7770 | have not yet output all the local symbols, so | |
7771 | we do not know the symbol index of any global | |
7772 | symbol. We set the rel_hash entry for this | |
7773 | reloc to point to the global hash table entry | |
7774 | for this symbol. The symbol index is then | |
ee75fd95 | 7775 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
7776 | indx = r_symndx - extsymoff; |
7777 | rh = elf_sym_hashes (input_bfd)[indx]; | |
7778 | while (rh->root.type == bfd_link_hash_indirect | |
7779 | || rh->root.type == bfd_link_hash_warning) | |
7780 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
7781 | ||
7782 | /* Setting the index to -2 tells | |
7783 | elf_link_output_extsym that this symbol is | |
7784 | used by a reloc. */ | |
7785 | BFD_ASSERT (rh->indx < 0); | |
7786 | rh->indx = -2; | |
7787 | ||
7788 | *rel_hash = rh; | |
7789 | ||
7790 | continue; | |
7791 | } | |
7792 | ||
7793 | /* This is a reloc against a local symbol. */ | |
7794 | ||
7795 | *rel_hash = NULL; | |
7796 | sym = isymbuf[r_symndx]; | |
7797 | sec = finfo->sections[r_symndx]; | |
7798 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
7799 | { | |
7800 | /* I suppose the backend ought to fill in the | |
7801 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
7802 | processor specific section. */ |
7803 | r_symndx = 0; | |
7804 | if (bfd_is_abs_section (sec)) | |
7805 | ; | |
c152c796 AM |
7806 | else if (sec == NULL || sec->owner == NULL) |
7807 | { | |
7808 | bfd_set_error (bfd_error_bad_value); | |
7809 | return FALSE; | |
7810 | } | |
7811 | else | |
7812 | { | |
6a8d1586 AM |
7813 | asection *osec = sec->output_section; |
7814 | ||
7815 | /* If we have discarded a section, the output | |
7816 | section will be the absolute section. In | |
7817 | case of discarded link-once and discarded | |
7818 | SEC_MERGE sections, use the kept section. */ | |
7819 | if (bfd_is_abs_section (osec) | |
7820 | && sec->kept_section != NULL | |
7821 | && sec->kept_section->output_section != NULL) | |
7822 | { | |
7823 | osec = sec->kept_section->output_section; | |
7824 | irela->r_addend -= osec->vma; | |
7825 | } | |
7826 | ||
7827 | if (!bfd_is_abs_section (osec)) | |
7828 | { | |
7829 | r_symndx = osec->target_index; | |
74541ad4 AM |
7830 | if (r_symndx == 0) |
7831 | { | |
7832 | struct elf_link_hash_table *htab; | |
7833 | asection *oi; | |
7834 | ||
7835 | htab = elf_hash_table (finfo->info); | |
7836 | oi = htab->text_index_section; | |
7837 | if ((osec->flags & SEC_READONLY) == 0 | |
7838 | && htab->data_index_section != NULL) | |
7839 | oi = htab->data_index_section; | |
7840 | ||
7841 | if (oi != NULL) | |
7842 | { | |
7843 | irela->r_addend += osec->vma - oi->vma; | |
7844 | r_symndx = oi->target_index; | |
7845 | } | |
7846 | } | |
7847 | ||
6a8d1586 AM |
7848 | BFD_ASSERT (r_symndx != 0); |
7849 | } | |
c152c796 AM |
7850 | } |
7851 | ||
7852 | /* Adjust the addend according to where the | |
7853 | section winds up in the output section. */ | |
7854 | if (rela_normal) | |
7855 | irela->r_addend += sec->output_offset; | |
7856 | } | |
7857 | else | |
7858 | { | |
7859 | if (finfo->indices[r_symndx] == -1) | |
7860 | { | |
7861 | unsigned long shlink; | |
7862 | const char *name; | |
7863 | asection *osec; | |
7864 | ||
7865 | if (finfo->info->strip == strip_all) | |
7866 | { | |
7867 | /* You can't do ld -r -s. */ | |
7868 | bfd_set_error (bfd_error_invalid_operation); | |
7869 | return FALSE; | |
7870 | } | |
7871 | ||
7872 | /* This symbol was skipped earlier, but | |
7873 | since it is needed by a reloc, we | |
7874 | must output it now. */ | |
7875 | shlink = symtab_hdr->sh_link; | |
7876 | name = (bfd_elf_string_from_elf_section | |
7877 | (input_bfd, shlink, sym.st_name)); | |
7878 | if (name == NULL) | |
7879 | return FALSE; | |
7880 | ||
7881 | osec = sec->output_section; | |
7882 | sym.st_shndx = | |
7883 | _bfd_elf_section_from_bfd_section (output_bfd, | |
7884 | osec); | |
7885 | if (sym.st_shndx == SHN_BAD) | |
7886 | return FALSE; | |
7887 | ||
7888 | sym.st_value += sec->output_offset; | |
7889 | if (! finfo->info->relocatable) | |
7890 | { | |
7891 | sym.st_value += osec->vma; | |
7892 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
7893 | { | |
7894 | /* STT_TLS symbols are relative to PT_TLS | |
7895 | segment base. */ | |
7896 | BFD_ASSERT (elf_hash_table (finfo->info) | |
7897 | ->tls_sec != NULL); | |
7898 | sym.st_value -= (elf_hash_table (finfo->info) | |
7899 | ->tls_sec->vma); | |
7900 | } | |
7901 | } | |
7902 | ||
7903 | finfo->indices[r_symndx] | |
7904 | = bfd_get_symcount (output_bfd); | |
7905 | ||
7906 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
7907 | NULL)) | |
7908 | return FALSE; | |
7909 | } | |
7910 | ||
7911 | r_symndx = finfo->indices[r_symndx]; | |
7912 | } | |
7913 | ||
7914 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
7915 | | (irela->r_info & r_type_mask)); | |
7916 | } | |
7917 | ||
7918 | /* Swap out the relocs. */ | |
c152c796 | 7919 | if (input_rel_hdr->sh_size != 0 |
eac338cf PB |
7920 | && !bed->elf_backend_emit_relocs (output_bfd, o, |
7921 | input_rel_hdr, | |
7922 | internal_relocs, | |
7923 | rel_hash_list)) | |
c152c796 AM |
7924 | return FALSE; |
7925 | ||
7926 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
7927 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
7928 | { | |
7929 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
7930 | * bed->s->int_rels_per_ext_rel); | |
eac338cf PB |
7931 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
7932 | if (!bed->elf_backend_emit_relocs (output_bfd, o, | |
7933 | input_rel_hdr2, | |
7934 | internal_relocs, | |
7935 | rel_hash_list)) | |
c152c796 AM |
7936 | return FALSE; |
7937 | } | |
7938 | } | |
7939 | } | |
7940 | ||
7941 | /* Write out the modified section contents. */ | |
7942 | if (bed->elf_backend_write_section | |
7943 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) | |
7944 | { | |
7945 | /* Section written out. */ | |
7946 | } | |
7947 | else switch (o->sec_info_type) | |
7948 | { | |
7949 | case ELF_INFO_TYPE_STABS: | |
7950 | if (! (_bfd_write_section_stabs | |
7951 | (output_bfd, | |
7952 | &elf_hash_table (finfo->info)->stab_info, | |
7953 | o, &elf_section_data (o)->sec_info, contents))) | |
7954 | return FALSE; | |
7955 | break; | |
7956 | case ELF_INFO_TYPE_MERGE: | |
7957 | if (! _bfd_write_merged_section (output_bfd, o, | |
7958 | elf_section_data (o)->sec_info)) | |
7959 | return FALSE; | |
7960 | break; | |
7961 | case ELF_INFO_TYPE_EH_FRAME: | |
7962 | { | |
7963 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
7964 | o, contents)) | |
7965 | return FALSE; | |
7966 | } | |
7967 | break; | |
7968 | default: | |
7969 | { | |
c152c796 AM |
7970 | if (! (o->flags & SEC_EXCLUDE) |
7971 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
7972 | contents, | |
7973 | (file_ptr) o->output_offset, | |
eea6121a | 7974 | o->size)) |
c152c796 AM |
7975 | return FALSE; |
7976 | } | |
7977 | break; | |
7978 | } | |
7979 | } | |
7980 | ||
7981 | return TRUE; | |
7982 | } | |
7983 | ||
7984 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 7985 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
7986 | is used to build constructor and destructor tables when linking |
7987 | with -Ur. */ | |
7988 | ||
7989 | static bfd_boolean | |
7990 | elf_reloc_link_order (bfd *output_bfd, | |
7991 | struct bfd_link_info *info, | |
7992 | asection *output_section, | |
7993 | struct bfd_link_order *link_order) | |
7994 | { | |
7995 | reloc_howto_type *howto; | |
7996 | long indx; | |
7997 | bfd_vma offset; | |
7998 | bfd_vma addend; | |
7999 | struct elf_link_hash_entry **rel_hash_ptr; | |
8000 | Elf_Internal_Shdr *rel_hdr; | |
8001 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
8002 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
8003 | bfd_byte *erel; | |
8004 | unsigned int i; | |
8005 | ||
8006 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
8007 | if (howto == NULL) | |
8008 | { | |
8009 | bfd_set_error (bfd_error_bad_value); | |
8010 | return FALSE; | |
8011 | } | |
8012 | ||
8013 | addend = link_order->u.reloc.p->addend; | |
8014 | ||
8015 | /* Figure out the symbol index. */ | |
8016 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
8017 | + elf_section_data (output_section)->rel_count | |
8018 | + elf_section_data (output_section)->rel_count2); | |
8019 | if (link_order->type == bfd_section_reloc_link_order) | |
8020 | { | |
8021 | indx = link_order->u.reloc.p->u.section->target_index; | |
8022 | BFD_ASSERT (indx != 0); | |
8023 | *rel_hash_ptr = NULL; | |
8024 | } | |
8025 | else | |
8026 | { | |
8027 | struct elf_link_hash_entry *h; | |
8028 | ||
8029 | /* Treat a reloc against a defined symbol as though it were | |
8030 | actually against the section. */ | |
8031 | h = ((struct elf_link_hash_entry *) | |
8032 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
8033 | link_order->u.reloc.p->u.name, | |
8034 | FALSE, FALSE, TRUE)); | |
8035 | if (h != NULL | |
8036 | && (h->root.type == bfd_link_hash_defined | |
8037 | || h->root.type == bfd_link_hash_defweak)) | |
8038 | { | |
8039 | asection *section; | |
8040 | ||
8041 | section = h->root.u.def.section; | |
8042 | indx = section->output_section->target_index; | |
8043 | *rel_hash_ptr = NULL; | |
8044 | /* It seems that we ought to add the symbol value to the | |
8045 | addend here, but in practice it has already been added | |
8046 | because it was passed to constructor_callback. */ | |
8047 | addend += section->output_section->vma + section->output_offset; | |
8048 | } | |
8049 | else if (h != NULL) | |
8050 | { | |
8051 | /* Setting the index to -2 tells elf_link_output_extsym that | |
8052 | this symbol is used by a reloc. */ | |
8053 | h->indx = -2; | |
8054 | *rel_hash_ptr = h; | |
8055 | indx = 0; | |
8056 | } | |
8057 | else | |
8058 | { | |
8059 | if (! ((*info->callbacks->unattached_reloc) | |
8060 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
8061 | return FALSE; | |
8062 | indx = 0; | |
8063 | } | |
8064 | } | |
8065 | ||
8066 | /* If this is an inplace reloc, we must write the addend into the | |
8067 | object file. */ | |
8068 | if (howto->partial_inplace && addend != 0) | |
8069 | { | |
8070 | bfd_size_type size; | |
8071 | bfd_reloc_status_type rstat; | |
8072 | bfd_byte *buf; | |
8073 | bfd_boolean ok; | |
8074 | const char *sym_name; | |
8075 | ||
8076 | size = bfd_get_reloc_size (howto); | |
8077 | buf = bfd_zmalloc (size); | |
8078 | if (buf == NULL) | |
8079 | return FALSE; | |
8080 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
8081 | switch (rstat) | |
8082 | { | |
8083 | case bfd_reloc_ok: | |
8084 | break; | |
8085 | ||
8086 | default: | |
8087 | case bfd_reloc_outofrange: | |
8088 | abort (); | |
8089 | ||
8090 | case bfd_reloc_overflow: | |
8091 | if (link_order->type == bfd_section_reloc_link_order) | |
8092 | sym_name = bfd_section_name (output_bfd, | |
8093 | link_order->u.reloc.p->u.section); | |
8094 | else | |
8095 | sym_name = link_order->u.reloc.p->u.name; | |
8096 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
8097 | (info, NULL, sym_name, howto->name, addend, NULL, |
8098 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
8099 | { |
8100 | free (buf); | |
8101 | return FALSE; | |
8102 | } | |
8103 | break; | |
8104 | } | |
8105 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
8106 | link_order->offset, size); | |
8107 | free (buf); | |
8108 | if (! ok) | |
8109 | return FALSE; | |
8110 | } | |
8111 | ||
8112 | /* The address of a reloc is relative to the section in a | |
8113 | relocatable file, and is a virtual address in an executable | |
8114 | file. */ | |
8115 | offset = link_order->offset; | |
8116 | if (! info->relocatable) | |
8117 | offset += output_section->vma; | |
8118 | ||
8119 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
8120 | { | |
8121 | irel[i].r_offset = offset; | |
8122 | irel[i].r_info = 0; | |
8123 | irel[i].r_addend = 0; | |
8124 | } | |
8125 | if (bed->s->arch_size == 32) | |
8126 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
8127 | else | |
8128 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
8129 | ||
8130 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
8131 | erel = rel_hdr->contents; | |
8132 | if (rel_hdr->sh_type == SHT_REL) | |
8133 | { | |
8134 | erel += (elf_section_data (output_section)->rel_count | |
8135 | * bed->s->sizeof_rel); | |
8136 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
8137 | } | |
8138 | else | |
8139 | { | |
8140 | irel[0].r_addend = addend; | |
8141 | erel += (elf_section_data (output_section)->rel_count | |
8142 | * bed->s->sizeof_rela); | |
8143 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
8144 | } | |
8145 | ||
8146 | ++elf_section_data (output_section)->rel_count; | |
8147 | ||
8148 | return TRUE; | |
8149 | } | |
8150 | ||
0b52efa6 PB |
8151 | |
8152 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
8153 | ||
8154 | static bfd_vma | |
8155 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
8156 | { | |
8157 | Elf_Internal_Shdr **elf_shdrp; | |
8158 | asection *s; | |
8159 | int elfsec; | |
8160 | ||
8161 | s = p->u.indirect.section; | |
8162 | elf_shdrp = elf_elfsections (s->owner); | |
8163 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
8164 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
8165 | /* PR 290: |
8166 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 8167 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
8168 | sh_info fields. Hence we could get the situation |
8169 | where elfsec is 0. */ | |
8170 | if (elfsec == 0) | |
8171 | { | |
8172 | const struct elf_backend_data *bed | |
8173 | = get_elf_backend_data (s->owner); | |
8174 | if (bed->link_order_error_handler) | |
d003868e AM |
8175 | bed->link_order_error_handler |
8176 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
8177 | return 0; |
8178 | } | |
8179 | else | |
8180 | { | |
8181 | s = elf_shdrp[elfsec]->bfd_section; | |
8182 | return s->output_section->vma + s->output_offset; | |
8183 | } | |
0b52efa6 PB |
8184 | } |
8185 | ||
8186 | ||
8187 | /* Compare two sections based on the locations of the sections they are | |
8188 | linked to. Used by elf_fixup_link_order. */ | |
8189 | ||
8190 | static int | |
8191 | compare_link_order (const void * a, const void * b) | |
8192 | { | |
8193 | bfd_vma apos; | |
8194 | bfd_vma bpos; | |
8195 | ||
8196 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
8197 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
8198 | if (apos < bpos) | |
8199 | return -1; | |
8200 | return apos > bpos; | |
8201 | } | |
8202 | ||
8203 | ||
8204 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
8205 | order as their linked sections. Returns false if this could not be done | |
8206 | because an output section includes both ordered and unordered | |
8207 | sections. Ideally we'd do this in the linker proper. */ | |
8208 | ||
8209 | static bfd_boolean | |
8210 | elf_fixup_link_order (bfd *abfd, asection *o) | |
8211 | { | |
8212 | int seen_linkorder; | |
8213 | int seen_other; | |
8214 | int n; | |
8215 | struct bfd_link_order *p; | |
8216 | bfd *sub; | |
8217 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 8218 | unsigned elfsec; |
0b52efa6 | 8219 | struct bfd_link_order **sections; |
d33cdfe3 | 8220 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 8221 | bfd_vma offset; |
3b36f7e6 | 8222 | |
d33cdfe3 L |
8223 | other_sec = NULL; |
8224 | linkorder_sec = NULL; | |
0b52efa6 PB |
8225 | seen_other = 0; |
8226 | seen_linkorder = 0; | |
8423293d | 8227 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 8228 | { |
d33cdfe3 | 8229 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
8230 | { |
8231 | s = p->u.indirect.section; | |
d33cdfe3 L |
8232 | sub = s->owner; |
8233 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
8234 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
8235 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
8236 | && elfsec < elf_numsections (sub) | |
0b52efa6 | 8237 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) |
d33cdfe3 L |
8238 | { |
8239 | seen_linkorder++; | |
8240 | linkorder_sec = s; | |
8241 | } | |
0b52efa6 | 8242 | else |
d33cdfe3 L |
8243 | { |
8244 | seen_other++; | |
8245 | other_sec = s; | |
8246 | } | |
0b52efa6 PB |
8247 | } |
8248 | else | |
8249 | seen_other++; | |
d33cdfe3 L |
8250 | |
8251 | if (seen_other && seen_linkorder) | |
8252 | { | |
8253 | if (other_sec && linkorder_sec) | |
8254 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
8255 | o, linkorder_sec, | |
8256 | linkorder_sec->owner, other_sec, | |
8257 | other_sec->owner); | |
8258 | else | |
8259 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
8260 | o); | |
8261 | bfd_set_error (bfd_error_bad_value); | |
8262 | return FALSE; | |
8263 | } | |
0b52efa6 PB |
8264 | } |
8265 | ||
8266 | if (!seen_linkorder) | |
8267 | return TRUE; | |
8268 | ||
0b52efa6 PB |
8269 | sections = (struct bfd_link_order **) |
8270 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
8271 | seen_linkorder = 0; | |
3b36f7e6 | 8272 | |
8423293d | 8273 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
8274 | { |
8275 | sections[seen_linkorder++] = p; | |
8276 | } | |
8277 | /* Sort the input sections in the order of their linked section. */ | |
8278 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
8279 | compare_link_order); | |
8280 | ||
8281 | /* Change the offsets of the sections. */ | |
8282 | offset = 0; | |
8283 | for (n = 0; n < seen_linkorder; n++) | |
8284 | { | |
8285 | s = sections[n]->u.indirect.section; | |
8286 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
8287 | s->output_offset = offset; | |
8288 | sections[n]->offset = offset; | |
8289 | offset += sections[n]->size; | |
8290 | } | |
8291 | ||
8292 | return TRUE; | |
8293 | } | |
8294 | ||
8295 | ||
c152c796 AM |
8296 | /* Do the final step of an ELF link. */ |
8297 | ||
8298 | bfd_boolean | |
8299 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
8300 | { | |
8301 | bfd_boolean dynamic; | |
8302 | bfd_boolean emit_relocs; | |
8303 | bfd *dynobj; | |
8304 | struct elf_final_link_info finfo; | |
8305 | register asection *o; | |
8306 | register struct bfd_link_order *p; | |
8307 | register bfd *sub; | |
8308 | bfd_size_type max_contents_size; | |
8309 | bfd_size_type max_external_reloc_size; | |
8310 | bfd_size_type max_internal_reloc_count; | |
8311 | bfd_size_type max_sym_count; | |
8312 | bfd_size_type max_sym_shndx_count; | |
8313 | file_ptr off; | |
8314 | Elf_Internal_Sym elfsym; | |
8315 | unsigned int i; | |
8316 | Elf_Internal_Shdr *symtab_hdr; | |
8317 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
8318 | Elf_Internal_Shdr *symstrtab_hdr; | |
8319 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8320 | struct elf_outext_info eoinfo; | |
8321 | bfd_boolean merged; | |
8322 | size_t relativecount = 0; | |
8323 | asection *reldyn = 0; | |
8324 | bfd_size_type amt; | |
8325 | ||
8326 | if (! is_elf_hash_table (info->hash)) | |
8327 | return FALSE; | |
8328 | ||
8329 | if (info->shared) | |
8330 | abfd->flags |= DYNAMIC; | |
8331 | ||
8332 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
8333 | dynobj = elf_hash_table (info)->dynobj; | |
8334 | ||
8335 | emit_relocs = (info->relocatable | |
a4676736 | 8336 | || info->emitrelocations); |
c152c796 AM |
8337 | |
8338 | finfo.info = info; | |
8339 | finfo.output_bfd = abfd; | |
8340 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
8341 | if (finfo.symstrtab == NULL) | |
8342 | return FALSE; | |
8343 | ||
8344 | if (! dynamic) | |
8345 | { | |
8346 | finfo.dynsym_sec = NULL; | |
8347 | finfo.hash_sec = NULL; | |
8348 | finfo.symver_sec = NULL; | |
8349 | } | |
8350 | else | |
8351 | { | |
8352 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
8353 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
fdc90cb4 | 8354 | BFD_ASSERT (finfo.dynsym_sec != NULL); |
c152c796 AM |
8355 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
8356 | /* Note that it is OK if symver_sec is NULL. */ | |
8357 | } | |
8358 | ||
8359 | finfo.contents = NULL; | |
8360 | finfo.external_relocs = NULL; | |
8361 | finfo.internal_relocs = NULL; | |
8362 | finfo.external_syms = NULL; | |
8363 | finfo.locsym_shndx = NULL; | |
8364 | finfo.internal_syms = NULL; | |
8365 | finfo.indices = NULL; | |
8366 | finfo.sections = NULL; | |
8367 | finfo.symbuf = NULL; | |
8368 | finfo.symshndxbuf = NULL; | |
8369 | finfo.symbuf_count = 0; | |
8370 | finfo.shndxbuf_size = 0; | |
8371 | ||
8372 | /* Count up the number of relocations we will output for each output | |
8373 | section, so that we know the sizes of the reloc sections. We | |
8374 | also figure out some maximum sizes. */ | |
8375 | max_contents_size = 0; | |
8376 | max_external_reloc_size = 0; | |
8377 | max_internal_reloc_count = 0; | |
8378 | max_sym_count = 0; | |
8379 | max_sym_shndx_count = 0; | |
8380 | merged = FALSE; | |
8381 | for (o = abfd->sections; o != NULL; o = o->next) | |
8382 | { | |
8383 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
8384 | o->reloc_count = 0; | |
8385 | ||
8423293d | 8386 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
8387 | { |
8388 | unsigned int reloc_count = 0; | |
8389 | struct bfd_elf_section_data *esdi = NULL; | |
8390 | unsigned int *rel_count1; | |
8391 | ||
8392 | if (p->type == bfd_section_reloc_link_order | |
8393 | || p->type == bfd_symbol_reloc_link_order) | |
8394 | reloc_count = 1; | |
8395 | else if (p->type == bfd_indirect_link_order) | |
8396 | { | |
8397 | asection *sec; | |
8398 | ||
8399 | sec = p->u.indirect.section; | |
8400 | esdi = elf_section_data (sec); | |
8401 | ||
8402 | /* Mark all sections which are to be included in the | |
8403 | link. This will normally be every section. We need | |
8404 | to do this so that we can identify any sections which | |
8405 | the linker has decided to not include. */ | |
8406 | sec->linker_mark = TRUE; | |
8407 | ||
8408 | if (sec->flags & SEC_MERGE) | |
8409 | merged = TRUE; | |
8410 | ||
8411 | if (info->relocatable || info->emitrelocations) | |
8412 | reloc_count = sec->reloc_count; | |
8413 | else if (bed->elf_backend_count_relocs) | |
8414 | { | |
8415 | Elf_Internal_Rela * relocs; | |
8416 | ||
8417 | relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
8418 | info->keep_memory); | |
8419 | ||
8420 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); | |
8421 | ||
8422 | if (elf_section_data (o)->relocs != relocs) | |
8423 | free (relocs); | |
8424 | } | |
8425 | ||
eea6121a AM |
8426 | if (sec->rawsize > max_contents_size) |
8427 | max_contents_size = sec->rawsize; | |
8428 | if (sec->size > max_contents_size) | |
8429 | max_contents_size = sec->size; | |
c152c796 AM |
8430 | |
8431 | /* We are interested in just local symbols, not all | |
8432 | symbols. */ | |
8433 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
8434 | && (sec->owner->flags & DYNAMIC) == 0) | |
8435 | { | |
8436 | size_t sym_count; | |
8437 | ||
8438 | if (elf_bad_symtab (sec->owner)) | |
8439 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
8440 | / bed->s->sizeof_sym); | |
8441 | else | |
8442 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
8443 | ||
8444 | if (sym_count > max_sym_count) | |
8445 | max_sym_count = sym_count; | |
8446 | ||
8447 | if (sym_count > max_sym_shndx_count | |
8448 | && elf_symtab_shndx (sec->owner) != 0) | |
8449 | max_sym_shndx_count = sym_count; | |
8450 | ||
8451 | if ((sec->flags & SEC_RELOC) != 0) | |
8452 | { | |
8453 | size_t ext_size; | |
8454 | ||
8455 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
8456 | if (ext_size > max_external_reloc_size) | |
8457 | max_external_reloc_size = ext_size; | |
8458 | if (sec->reloc_count > max_internal_reloc_count) | |
8459 | max_internal_reloc_count = sec->reloc_count; | |
8460 | } | |
8461 | } | |
8462 | } | |
8463 | ||
8464 | if (reloc_count == 0) | |
8465 | continue; | |
8466 | ||
8467 | o->reloc_count += reloc_count; | |
8468 | ||
8469 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
8470 | To support this curious ABI we keep reloc counts in | |
8471 | elf_section_data too. We must be careful to add the | |
8472 | relocations from the input section to the right output | |
8473 | count. FIXME: Get rid of one count. We have | |
8474 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
8475 | rel_count1 = &esdo->rel_count; | |
8476 | if (esdi != NULL) | |
8477 | { | |
8478 | bfd_boolean same_size; | |
8479 | bfd_size_type entsize1; | |
8480 | ||
8481 | entsize1 = esdi->rel_hdr.sh_entsize; | |
8482 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
8483 | || entsize1 == bed->s->sizeof_rela); | |
8484 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
8485 | ||
8486 | if (!same_size) | |
8487 | rel_count1 = &esdo->rel_count2; | |
8488 | ||
8489 | if (esdi->rel_hdr2 != NULL) | |
8490 | { | |
8491 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
8492 | unsigned int alt_count; | |
8493 | unsigned int *rel_count2; | |
8494 | ||
8495 | BFD_ASSERT (entsize2 != entsize1 | |
8496 | && (entsize2 == bed->s->sizeof_rel | |
8497 | || entsize2 == bed->s->sizeof_rela)); | |
8498 | ||
8499 | rel_count2 = &esdo->rel_count2; | |
8500 | if (!same_size) | |
8501 | rel_count2 = &esdo->rel_count; | |
8502 | ||
8503 | /* The following is probably too simplistic if the | |
8504 | backend counts output relocs unusually. */ | |
8505 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
8506 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
8507 | *rel_count2 += alt_count; | |
8508 | reloc_count -= alt_count; | |
8509 | } | |
8510 | } | |
8511 | *rel_count1 += reloc_count; | |
8512 | } | |
8513 | ||
8514 | if (o->reloc_count > 0) | |
8515 | o->flags |= SEC_RELOC; | |
8516 | else | |
8517 | { | |
8518 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
8519 | set it (this is probably a bug) and if it is set | |
8520 | assign_section_numbers will create a reloc section. */ | |
8521 | o->flags &=~ SEC_RELOC; | |
8522 | } | |
8523 | ||
8524 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
8525 | zero. This is done in elf_fake_sections as well, but forcing | |
8526 | the VMA to 0 here will ensure that relocs against these | |
8527 | sections are handled correctly. */ | |
8528 | if ((o->flags & SEC_ALLOC) == 0 | |
8529 | && ! o->user_set_vma) | |
8530 | o->vma = 0; | |
8531 | } | |
8532 | ||
8533 | if (! info->relocatable && merged) | |
8534 | elf_link_hash_traverse (elf_hash_table (info), | |
8535 | _bfd_elf_link_sec_merge_syms, abfd); | |
8536 | ||
8537 | /* Figure out the file positions for everything but the symbol table | |
8538 | and the relocs. We set symcount to force assign_section_numbers | |
8539 | to create a symbol table. */ | |
8540 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
8541 | BFD_ASSERT (! abfd->output_has_begun); | |
8542 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
8543 | goto error_return; | |
8544 | ||
ee75fd95 | 8545 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
8546 | for (o = abfd->sections; o != NULL; o = o->next) |
8547 | { | |
8548 | if ((o->flags & SEC_RELOC) != 0) | |
8549 | { | |
8550 | if (!(_bfd_elf_link_size_reloc_section | |
8551 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
8552 | goto error_return; | |
8553 | ||
8554 | if (elf_section_data (o)->rel_hdr2 | |
8555 | && !(_bfd_elf_link_size_reloc_section | |
8556 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
8557 | goto error_return; | |
8558 | } | |
8559 | ||
8560 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
8561 | to count upwards while actually outputting the relocations. */ | |
8562 | elf_section_data (o)->rel_count = 0; | |
8563 | elf_section_data (o)->rel_count2 = 0; | |
8564 | } | |
8565 | ||
8566 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
8567 | ||
8568 | /* We have now assigned file positions for all the sections except | |
8569 | .symtab and .strtab. We start the .symtab section at the current | |
8570 | file position, and write directly to it. We build the .strtab | |
8571 | section in memory. */ | |
8572 | bfd_get_symcount (abfd) = 0; | |
8573 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
8574 | /* sh_name is set in prep_headers. */ | |
8575 | symtab_hdr->sh_type = SHT_SYMTAB; | |
8576 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
8577 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
8578 | /* sh_link is set in assign_section_numbers. */ | |
8579 | /* sh_info is set below. */ | |
8580 | /* sh_offset is set just below. */ | |
8581 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
8582 | ||
8583 | off = elf_tdata (abfd)->next_file_pos; | |
8584 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
8585 | ||
8586 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
8587 | incorrect. We do not yet know the size of the .symtab section. | |
8588 | We correct next_file_pos below, after we do know the size. */ | |
8589 | ||
8590 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
8591 | continuously seeking to the right position in the file. */ | |
8592 | if (! info->keep_memory || max_sym_count < 20) | |
8593 | finfo.symbuf_size = 20; | |
8594 | else | |
8595 | finfo.symbuf_size = max_sym_count; | |
8596 | amt = finfo.symbuf_size; | |
8597 | amt *= bed->s->sizeof_sym; | |
8598 | finfo.symbuf = bfd_malloc (amt); | |
8599 | if (finfo.symbuf == NULL) | |
8600 | goto error_return; | |
8601 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
8602 | { | |
8603 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
8604 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
8605 | finfo.shndxbuf_size = amt; | |
8606 | amt *= sizeof (Elf_External_Sym_Shndx); | |
8607 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
8608 | if (finfo.symshndxbuf == NULL) | |
8609 | goto error_return; | |
8610 | } | |
8611 | ||
8612 | /* Start writing out the symbol table. The first symbol is always a | |
8613 | dummy symbol. */ | |
8614 | if (info->strip != strip_all | |
8615 | || emit_relocs) | |
8616 | { | |
8617 | elfsym.st_value = 0; | |
8618 | elfsym.st_size = 0; | |
8619 | elfsym.st_info = 0; | |
8620 | elfsym.st_other = 0; | |
8621 | elfsym.st_shndx = SHN_UNDEF; | |
8622 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
8623 | NULL)) | |
8624 | goto error_return; | |
8625 | } | |
8626 | ||
c152c796 AM |
8627 | /* Output a symbol for each section. We output these even if we are |
8628 | discarding local symbols, since they are used for relocs. These | |
8629 | symbols have no names. We store the index of each one in the | |
8630 | index field of the section, so that we can find it again when | |
8631 | outputting relocs. */ | |
8632 | if (info->strip != strip_all | |
8633 | || emit_relocs) | |
8634 | { | |
8635 | elfsym.st_size = 0; | |
8636 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8637 | elfsym.st_other = 0; | |
f0b5bb34 | 8638 | elfsym.st_value = 0; |
c152c796 AM |
8639 | for (i = 1; i < elf_numsections (abfd); i++) |
8640 | { | |
8641 | o = bfd_section_from_elf_index (abfd, i); | |
8642 | if (o != NULL) | |
f0b5bb34 AM |
8643 | { |
8644 | o->target_index = bfd_get_symcount (abfd); | |
8645 | elfsym.st_shndx = i; | |
8646 | if (!info->relocatable) | |
8647 | elfsym.st_value = o->vma; | |
8648 | if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
8649 | goto error_return; | |
8650 | } | |
c152c796 AM |
8651 | if (i == SHN_LORESERVE - 1) |
8652 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
8653 | } | |
8654 | } | |
8655 | ||
8656 | /* Allocate some memory to hold information read in from the input | |
8657 | files. */ | |
8658 | if (max_contents_size != 0) | |
8659 | { | |
8660 | finfo.contents = bfd_malloc (max_contents_size); | |
8661 | if (finfo.contents == NULL) | |
8662 | goto error_return; | |
8663 | } | |
8664 | ||
8665 | if (max_external_reloc_size != 0) | |
8666 | { | |
8667 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
8668 | if (finfo.external_relocs == NULL) | |
8669 | goto error_return; | |
8670 | } | |
8671 | ||
8672 | if (max_internal_reloc_count != 0) | |
8673 | { | |
8674 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
8675 | amt *= sizeof (Elf_Internal_Rela); | |
8676 | finfo.internal_relocs = bfd_malloc (amt); | |
8677 | if (finfo.internal_relocs == NULL) | |
8678 | goto error_return; | |
8679 | } | |
8680 | ||
8681 | if (max_sym_count != 0) | |
8682 | { | |
8683 | amt = max_sym_count * bed->s->sizeof_sym; | |
8684 | finfo.external_syms = bfd_malloc (amt); | |
8685 | if (finfo.external_syms == NULL) | |
8686 | goto error_return; | |
8687 | ||
8688 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8689 | finfo.internal_syms = bfd_malloc (amt); | |
8690 | if (finfo.internal_syms == NULL) | |
8691 | goto error_return; | |
8692 | ||
8693 | amt = max_sym_count * sizeof (long); | |
8694 | finfo.indices = bfd_malloc (amt); | |
8695 | if (finfo.indices == NULL) | |
8696 | goto error_return; | |
8697 | ||
8698 | amt = max_sym_count * sizeof (asection *); | |
8699 | finfo.sections = bfd_malloc (amt); | |
8700 | if (finfo.sections == NULL) | |
8701 | goto error_return; | |
8702 | } | |
8703 | ||
8704 | if (max_sym_shndx_count != 0) | |
8705 | { | |
8706 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8707 | finfo.locsym_shndx = bfd_malloc (amt); | |
8708 | if (finfo.locsym_shndx == NULL) | |
8709 | goto error_return; | |
8710 | } | |
8711 | ||
8712 | if (elf_hash_table (info)->tls_sec) | |
8713 | { | |
8714 | bfd_vma base, end = 0; | |
8715 | asection *sec; | |
8716 | ||
8717 | for (sec = elf_hash_table (info)->tls_sec; | |
8718 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
8719 | sec = sec->next) | |
8720 | { | |
3a800eb9 | 8721 | bfd_size_type size = sec->size; |
c152c796 | 8722 | |
3a800eb9 AM |
8723 | if (size == 0 |
8724 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 8725 | { |
3a800eb9 AM |
8726 | struct bfd_link_order *o = sec->map_tail.link_order; |
8727 | if (o != NULL) | |
8728 | size = o->offset + o->size; | |
c152c796 AM |
8729 | } |
8730 | end = sec->vma + size; | |
8731 | } | |
8732 | base = elf_hash_table (info)->tls_sec->vma; | |
8733 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
8734 | elf_hash_table (info)->tls_size = end - base; | |
8735 | } | |
8736 | ||
0b52efa6 PB |
8737 | /* Reorder SHF_LINK_ORDER sections. */ |
8738 | for (o = abfd->sections; o != NULL; o = o->next) | |
8739 | { | |
8740 | if (!elf_fixup_link_order (abfd, o)) | |
8741 | return FALSE; | |
8742 | } | |
8743 | ||
c152c796 AM |
8744 | /* Since ELF permits relocations to be against local symbols, we |
8745 | must have the local symbols available when we do the relocations. | |
8746 | Since we would rather only read the local symbols once, and we | |
8747 | would rather not keep them in memory, we handle all the | |
8748 | relocations for a single input file at the same time. | |
8749 | ||
8750 | Unfortunately, there is no way to know the total number of local | |
8751 | symbols until we have seen all of them, and the local symbol | |
8752 | indices precede the global symbol indices. This means that when | |
8753 | we are generating relocatable output, and we see a reloc against | |
8754 | a global symbol, we can not know the symbol index until we have | |
8755 | finished examining all the local symbols to see which ones we are | |
8756 | going to output. To deal with this, we keep the relocations in | |
8757 | memory, and don't output them until the end of the link. This is | |
8758 | an unfortunate waste of memory, but I don't see a good way around | |
8759 | it. Fortunately, it only happens when performing a relocatable | |
8760 | link, which is not the common case. FIXME: If keep_memory is set | |
8761 | we could write the relocs out and then read them again; I don't | |
8762 | know how bad the memory loss will be. */ | |
8763 | ||
8764 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8765 | sub->output_has_begun = FALSE; | |
8766 | for (o = abfd->sections; o != NULL; o = o->next) | |
8767 | { | |
8423293d | 8768 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
8769 | { |
8770 | if (p->type == bfd_indirect_link_order | |
8771 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
8772 | == bfd_target_elf_flavour) | |
8773 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
8774 | { | |
8775 | if (! sub->output_has_begun) | |
8776 | { | |
8777 | if (! elf_link_input_bfd (&finfo, sub)) | |
8778 | goto error_return; | |
8779 | sub->output_has_begun = TRUE; | |
8780 | } | |
8781 | } | |
8782 | else if (p->type == bfd_section_reloc_link_order | |
8783 | || p->type == bfd_symbol_reloc_link_order) | |
8784 | { | |
8785 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
8786 | goto error_return; | |
8787 | } | |
8788 | else | |
8789 | { | |
8790 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
8791 | goto error_return; | |
8792 | } | |
8793 | } | |
8794 | } | |
8795 | ||
c0f00686 L |
8796 | /* Free symbol buffer if needed. */ |
8797 | if (!info->reduce_memory_overheads) | |
8798 | { | |
8799 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8800 | if (elf_tdata (sub)->symbuf) | |
8801 | { | |
8802 | free (elf_tdata (sub)->symbuf); | |
8803 | elf_tdata (sub)->symbuf = NULL; | |
8804 | } | |
8805 | } | |
8806 | ||
c152c796 AM |
8807 | /* Output any global symbols that got converted to local in a |
8808 | version script or due to symbol visibility. We do this in a | |
8809 | separate step since ELF requires all local symbols to appear | |
8810 | prior to any global symbols. FIXME: We should only do this if | |
8811 | some global symbols were, in fact, converted to become local. | |
8812 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
8813 | eoinfo.failed = FALSE; | |
8814 | eoinfo.finfo = &finfo; | |
8815 | eoinfo.localsyms = TRUE; | |
8816 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8817 | &eoinfo); | |
8818 | if (eoinfo.failed) | |
8819 | return FALSE; | |
8820 | ||
4e617b1e PB |
8821 | /* If backend needs to output some local symbols not present in the hash |
8822 | table, do it now. */ | |
8823 | if (bed->elf_backend_output_arch_local_syms) | |
8824 | { | |
8825 | typedef bfd_boolean (*out_sym_func) | |
8826 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8827 | struct elf_link_hash_entry *); | |
8828 | ||
8829 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
8830 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8831 | return FALSE; | |
8832 | } | |
8833 | ||
c152c796 AM |
8834 | /* That wrote out all the local symbols. Finish up the symbol table |
8835 | with the global symbols. Even if we want to strip everything we | |
8836 | can, we still need to deal with those global symbols that got | |
8837 | converted to local in a version script. */ | |
8838 | ||
8839 | /* The sh_info field records the index of the first non local symbol. */ | |
8840 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
8841 | ||
8842 | if (dynamic | |
8843 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
8844 | { | |
8845 | Elf_Internal_Sym sym; | |
8846 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
8847 | long last_local = 0; | |
8848 | ||
8849 | /* Write out the section symbols for the output sections. */ | |
67687978 | 8850 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
8851 | { |
8852 | asection *s; | |
8853 | ||
8854 | sym.st_size = 0; | |
8855 | sym.st_name = 0; | |
8856 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8857 | sym.st_other = 0; | |
8858 | ||
8859 | for (s = abfd->sections; s != NULL; s = s->next) | |
8860 | { | |
8861 | int indx; | |
8862 | bfd_byte *dest; | |
8863 | long dynindx; | |
8864 | ||
c152c796 | 8865 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
8866 | if (dynindx <= 0) |
8867 | continue; | |
8868 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
8869 | BFD_ASSERT (indx > 0); |
8870 | sym.st_shndx = indx; | |
c0d5a53d L |
8871 | if (! check_dynsym (abfd, &sym)) |
8872 | return FALSE; | |
c152c796 AM |
8873 | sym.st_value = s->vma; |
8874 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
8875 | if (last_local < dynindx) |
8876 | last_local = dynindx; | |
c152c796 AM |
8877 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
8878 | } | |
c152c796 AM |
8879 | } |
8880 | ||
8881 | /* Write out the local dynsyms. */ | |
8882 | if (elf_hash_table (info)->dynlocal) | |
8883 | { | |
8884 | struct elf_link_local_dynamic_entry *e; | |
8885 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
8886 | { | |
8887 | asection *s; | |
8888 | bfd_byte *dest; | |
8889 | ||
8890 | sym.st_size = e->isym.st_size; | |
8891 | sym.st_other = e->isym.st_other; | |
8892 | ||
8893 | /* Copy the internal symbol as is. | |
8894 | Note that we saved a word of storage and overwrote | |
8895 | the original st_name with the dynstr_index. */ | |
8896 | sym = e->isym; | |
8897 | ||
8898 | if (e->isym.st_shndx != SHN_UNDEF | |
8899 | && (e->isym.st_shndx < SHN_LORESERVE | |
8900 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
8901 | { | |
8902 | s = bfd_section_from_elf_index (e->input_bfd, | |
8903 | e->isym.st_shndx); | |
8904 | ||
8905 | sym.st_shndx = | |
8906 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
8907 | if (! check_dynsym (abfd, &sym)) |
8908 | return FALSE; | |
c152c796 AM |
8909 | sym.st_value = (s->output_section->vma |
8910 | + s->output_offset | |
8911 | + e->isym.st_value); | |
8912 | } | |
8913 | ||
8914 | if (last_local < e->dynindx) | |
8915 | last_local = e->dynindx; | |
8916 | ||
8917 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
8918 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
8919 | } | |
8920 | } | |
8921 | ||
8922 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
8923 | last_local + 1; | |
8924 | } | |
8925 | ||
8926 | /* We get the global symbols from the hash table. */ | |
8927 | eoinfo.failed = FALSE; | |
8928 | eoinfo.localsyms = FALSE; | |
8929 | eoinfo.finfo = &finfo; | |
8930 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8931 | &eoinfo); | |
8932 | if (eoinfo.failed) | |
8933 | return FALSE; | |
8934 | ||
8935 | /* If backend needs to output some symbols not present in the hash | |
8936 | table, do it now. */ | |
8937 | if (bed->elf_backend_output_arch_syms) | |
8938 | { | |
8939 | typedef bfd_boolean (*out_sym_func) | |
8940 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8941 | struct elf_link_hash_entry *); | |
8942 | ||
8943 | if (! ((*bed->elf_backend_output_arch_syms) | |
8944 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8945 | return FALSE; | |
8946 | } | |
8947 | ||
8948 | /* Flush all symbols to the file. */ | |
8949 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
8950 | return FALSE; | |
8951 | ||
8952 | /* Now we know the size of the symtab section. */ | |
8953 | off += symtab_hdr->sh_size; | |
8954 | ||
8955 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
8956 | if (symtab_shndx_hdr->sh_name != 0) | |
8957 | { | |
8958 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8959 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8960 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8961 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
8962 | symtab_shndx_hdr->sh_size = amt; | |
8963 | ||
8964 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
8965 | off, TRUE); | |
8966 | ||
8967 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
8968 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
8969 | return FALSE; | |
8970 | } | |
8971 | ||
8972 | ||
8973 | /* Finish up and write out the symbol string table (.strtab) | |
8974 | section. */ | |
8975 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8976 | /* sh_name was set in prep_headers. */ | |
8977 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8978 | symstrtab_hdr->sh_flags = 0; | |
8979 | symstrtab_hdr->sh_addr = 0; | |
8980 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
8981 | symstrtab_hdr->sh_entsize = 0; | |
8982 | symstrtab_hdr->sh_link = 0; | |
8983 | symstrtab_hdr->sh_info = 0; | |
8984 | /* sh_offset is set just below. */ | |
8985 | symstrtab_hdr->sh_addralign = 1; | |
8986 | ||
8987 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
8988 | elf_tdata (abfd)->next_file_pos = off; | |
8989 | ||
8990 | if (bfd_get_symcount (abfd) > 0) | |
8991 | { | |
8992 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
8993 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
8994 | return FALSE; | |
8995 | } | |
8996 | ||
8997 | /* Adjust the relocs to have the correct symbol indices. */ | |
8998 | for (o = abfd->sections; o != NULL; o = o->next) | |
8999 | { | |
9000 | if ((o->flags & SEC_RELOC) == 0) | |
9001 | continue; | |
9002 | ||
9003 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
9004 | elf_section_data (o)->rel_count, | |
9005 | elf_section_data (o)->rel_hashes); | |
9006 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
9007 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
9008 | elf_section_data (o)->rel_count2, | |
9009 | (elf_section_data (o)->rel_hashes | |
9010 | + elf_section_data (o)->rel_count)); | |
9011 | ||
9012 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
9013 | trying to swap the relocs out itself. */ | |
9014 | o->reloc_count = 0; | |
9015 | } | |
9016 | ||
9017 | if (dynamic && info->combreloc && dynobj != NULL) | |
9018 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
9019 | ||
9020 | /* If we are linking against a dynamic object, or generating a | |
9021 | shared library, finish up the dynamic linking information. */ | |
9022 | if (dynamic) | |
9023 | { | |
9024 | bfd_byte *dyncon, *dynconend; | |
9025 | ||
9026 | /* Fix up .dynamic entries. */ | |
9027 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
9028 | BFD_ASSERT (o != NULL); | |
9029 | ||
9030 | dyncon = o->contents; | |
eea6121a | 9031 | dynconend = o->contents + o->size; |
c152c796 AM |
9032 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
9033 | { | |
9034 | Elf_Internal_Dyn dyn; | |
9035 | const char *name; | |
9036 | unsigned int type; | |
9037 | ||
9038 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
9039 | ||
9040 | switch (dyn.d_tag) | |
9041 | { | |
9042 | default: | |
9043 | continue; | |
9044 | case DT_NULL: | |
9045 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
9046 | { | |
9047 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
9048 | { | |
9049 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
9050 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
9051 | default: continue; | |
9052 | } | |
9053 | dyn.d_un.d_val = relativecount; | |
9054 | relativecount = 0; | |
9055 | break; | |
9056 | } | |
9057 | continue; | |
9058 | ||
9059 | case DT_INIT: | |
9060 | name = info->init_function; | |
9061 | goto get_sym; | |
9062 | case DT_FINI: | |
9063 | name = info->fini_function; | |
9064 | get_sym: | |
9065 | { | |
9066 | struct elf_link_hash_entry *h; | |
9067 | ||
9068 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
9069 | FALSE, FALSE, TRUE); | |
9070 | if (h != NULL | |
9071 | && (h->root.type == bfd_link_hash_defined | |
9072 | || h->root.type == bfd_link_hash_defweak)) | |
9073 | { | |
9074 | dyn.d_un.d_val = h->root.u.def.value; | |
9075 | o = h->root.u.def.section; | |
9076 | if (o->output_section != NULL) | |
9077 | dyn.d_un.d_val += (o->output_section->vma | |
9078 | + o->output_offset); | |
9079 | else | |
9080 | { | |
9081 | /* The symbol is imported from another shared | |
9082 | library and does not apply to this one. */ | |
9083 | dyn.d_un.d_val = 0; | |
9084 | } | |
9085 | break; | |
9086 | } | |
9087 | } | |
9088 | continue; | |
9089 | ||
9090 | case DT_PREINIT_ARRAYSZ: | |
9091 | name = ".preinit_array"; | |
9092 | goto get_size; | |
9093 | case DT_INIT_ARRAYSZ: | |
9094 | name = ".init_array"; | |
9095 | goto get_size; | |
9096 | case DT_FINI_ARRAYSZ: | |
9097 | name = ".fini_array"; | |
9098 | get_size: | |
9099 | o = bfd_get_section_by_name (abfd, name); | |
9100 | if (o == NULL) | |
9101 | { | |
9102 | (*_bfd_error_handler) | |
d003868e | 9103 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
9104 | goto error_return; |
9105 | } | |
eea6121a | 9106 | if (o->size == 0) |
c152c796 AM |
9107 | (*_bfd_error_handler) |
9108 | (_("warning: %s section has zero size"), name); | |
eea6121a | 9109 | dyn.d_un.d_val = o->size; |
c152c796 AM |
9110 | break; |
9111 | ||
9112 | case DT_PREINIT_ARRAY: | |
9113 | name = ".preinit_array"; | |
9114 | goto get_vma; | |
9115 | case DT_INIT_ARRAY: | |
9116 | name = ".init_array"; | |
9117 | goto get_vma; | |
9118 | case DT_FINI_ARRAY: | |
9119 | name = ".fini_array"; | |
9120 | goto get_vma; | |
9121 | ||
9122 | case DT_HASH: | |
9123 | name = ".hash"; | |
9124 | goto get_vma; | |
fdc90cb4 JJ |
9125 | case DT_GNU_HASH: |
9126 | name = ".gnu.hash"; | |
9127 | goto get_vma; | |
c152c796 AM |
9128 | case DT_STRTAB: |
9129 | name = ".dynstr"; | |
9130 | goto get_vma; | |
9131 | case DT_SYMTAB: | |
9132 | name = ".dynsym"; | |
9133 | goto get_vma; | |
9134 | case DT_VERDEF: | |
9135 | name = ".gnu.version_d"; | |
9136 | goto get_vma; | |
9137 | case DT_VERNEED: | |
9138 | name = ".gnu.version_r"; | |
9139 | goto get_vma; | |
9140 | case DT_VERSYM: | |
9141 | name = ".gnu.version"; | |
9142 | get_vma: | |
9143 | o = bfd_get_section_by_name (abfd, name); | |
9144 | if (o == NULL) | |
9145 | { | |
9146 | (*_bfd_error_handler) | |
d003868e | 9147 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
9148 | goto error_return; |
9149 | } | |
9150 | dyn.d_un.d_ptr = o->vma; | |
9151 | break; | |
9152 | ||
9153 | case DT_REL: | |
9154 | case DT_RELA: | |
9155 | case DT_RELSZ: | |
9156 | case DT_RELASZ: | |
9157 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
9158 | type = SHT_REL; | |
9159 | else | |
9160 | type = SHT_RELA; | |
9161 | dyn.d_un.d_val = 0; | |
9162 | for (i = 1; i < elf_numsections (abfd); i++) | |
9163 | { | |
9164 | Elf_Internal_Shdr *hdr; | |
9165 | ||
9166 | hdr = elf_elfsections (abfd)[i]; | |
9167 | if (hdr->sh_type == type | |
9168 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
9169 | { | |
9170 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
9171 | dyn.d_un.d_val += hdr->sh_size; | |
9172 | else | |
9173 | { | |
9174 | if (dyn.d_un.d_val == 0 | |
9175 | || hdr->sh_addr < dyn.d_un.d_val) | |
9176 | dyn.d_un.d_val = hdr->sh_addr; | |
9177 | } | |
9178 | } | |
9179 | } | |
9180 | break; | |
9181 | } | |
9182 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
9183 | } | |
9184 | } | |
9185 | ||
9186 | /* If we have created any dynamic sections, then output them. */ | |
9187 | if (dynobj != NULL) | |
9188 | { | |
9189 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
9190 | goto error_return; | |
9191 | ||
943284cc DJ |
9192 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
9193 | if (info->warn_shared_textrel && info->shared) | |
9194 | { | |
9195 | bfd_byte *dyncon, *dynconend; | |
9196 | ||
9197 | /* Fix up .dynamic entries. */ | |
9198 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
9199 | BFD_ASSERT (o != NULL); | |
9200 | ||
9201 | dyncon = o->contents; | |
9202 | dynconend = o->contents + o->size; | |
9203 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
9204 | { | |
9205 | Elf_Internal_Dyn dyn; | |
9206 | ||
9207 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
9208 | ||
9209 | if (dyn.d_tag == DT_TEXTREL) | |
9210 | { | |
9211 | _bfd_error_handler | |
9212 | (_("warning: creating a DT_TEXTREL in a shared object.")); | |
9213 | break; | |
9214 | } | |
9215 | } | |
9216 | } | |
9217 | ||
c152c796 AM |
9218 | for (o = dynobj->sections; o != NULL; o = o->next) |
9219 | { | |
9220 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 9221 | || o->size == 0 |
c152c796 AM |
9222 | || o->output_section == bfd_abs_section_ptr) |
9223 | continue; | |
9224 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
9225 | { | |
9226 | /* At this point, we are only interested in sections | |
9227 | created by _bfd_elf_link_create_dynamic_sections. */ | |
9228 | continue; | |
9229 | } | |
3722b82f AM |
9230 | if (elf_hash_table (info)->stab_info.stabstr == o) |
9231 | continue; | |
eea6121a AM |
9232 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
9233 | continue; | |
c152c796 AM |
9234 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
9235 | != SHT_STRTAB) | |
9236 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
9237 | { | |
9238 | if (! bfd_set_section_contents (abfd, o->output_section, | |
9239 | o->contents, | |
9240 | (file_ptr) o->output_offset, | |
eea6121a | 9241 | o->size)) |
c152c796 AM |
9242 | goto error_return; |
9243 | } | |
9244 | else | |
9245 | { | |
9246 | /* The contents of the .dynstr section are actually in a | |
9247 | stringtab. */ | |
9248 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
9249 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
9250 | || ! _bfd_elf_strtab_emit (abfd, | |
9251 | elf_hash_table (info)->dynstr)) | |
9252 | goto error_return; | |
9253 | } | |
9254 | } | |
9255 | } | |
9256 | ||
9257 | if (info->relocatable) | |
9258 | { | |
9259 | bfd_boolean failed = FALSE; | |
9260 | ||
9261 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
9262 | if (failed) | |
9263 | goto error_return; | |
9264 | } | |
9265 | ||
9266 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 9267 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
9268 | { |
9269 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
9270 | goto error_return; | |
9271 | } | |
9272 | ||
9273 | if (info->eh_frame_hdr) | |
9274 | { | |
9275 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
9276 | goto error_return; | |
9277 | } | |
9278 | ||
9279 | if (finfo.symstrtab != NULL) | |
9280 | _bfd_stringtab_free (finfo.symstrtab); | |
9281 | if (finfo.contents != NULL) | |
9282 | free (finfo.contents); | |
9283 | if (finfo.external_relocs != NULL) | |
9284 | free (finfo.external_relocs); | |
9285 | if (finfo.internal_relocs != NULL) | |
9286 | free (finfo.internal_relocs); | |
9287 | if (finfo.external_syms != NULL) | |
9288 | free (finfo.external_syms); | |
9289 | if (finfo.locsym_shndx != NULL) | |
9290 | free (finfo.locsym_shndx); | |
9291 | if (finfo.internal_syms != NULL) | |
9292 | free (finfo.internal_syms); | |
9293 | if (finfo.indices != NULL) | |
9294 | free (finfo.indices); | |
9295 | if (finfo.sections != NULL) | |
9296 | free (finfo.sections); | |
9297 | if (finfo.symbuf != NULL) | |
9298 | free (finfo.symbuf); | |
9299 | if (finfo.symshndxbuf != NULL) | |
9300 | free (finfo.symshndxbuf); | |
9301 | for (o = abfd->sections; o != NULL; o = o->next) | |
9302 | { | |
9303 | if ((o->flags & SEC_RELOC) != 0 | |
9304 | && elf_section_data (o)->rel_hashes != NULL) | |
9305 | free (elf_section_data (o)->rel_hashes); | |
9306 | } | |
9307 | ||
9308 | elf_tdata (abfd)->linker = TRUE; | |
9309 | ||
9310 | return TRUE; | |
9311 | ||
9312 | error_return: | |
9313 | if (finfo.symstrtab != NULL) | |
9314 | _bfd_stringtab_free (finfo.symstrtab); | |
9315 | if (finfo.contents != NULL) | |
9316 | free (finfo.contents); | |
9317 | if (finfo.external_relocs != NULL) | |
9318 | free (finfo.external_relocs); | |
9319 | if (finfo.internal_relocs != NULL) | |
9320 | free (finfo.internal_relocs); | |
9321 | if (finfo.external_syms != NULL) | |
9322 | free (finfo.external_syms); | |
9323 | if (finfo.locsym_shndx != NULL) | |
9324 | free (finfo.locsym_shndx); | |
9325 | if (finfo.internal_syms != NULL) | |
9326 | free (finfo.internal_syms); | |
9327 | if (finfo.indices != NULL) | |
9328 | free (finfo.indices); | |
9329 | if (finfo.sections != NULL) | |
9330 | free (finfo.sections); | |
9331 | if (finfo.symbuf != NULL) | |
9332 | free (finfo.symbuf); | |
9333 | if (finfo.symshndxbuf != NULL) | |
9334 | free (finfo.symshndxbuf); | |
9335 | for (o = abfd->sections; o != NULL; o = o->next) | |
9336 | { | |
9337 | if ((o->flags & SEC_RELOC) != 0 | |
9338 | && elf_section_data (o)->rel_hashes != NULL) | |
9339 | free (elf_section_data (o)->rel_hashes); | |
9340 | } | |
9341 | ||
9342 | return FALSE; | |
9343 | } | |
9344 | \f | |
9345 | /* Garbage collect unused sections. */ | |
9346 | ||
c152c796 AM |
9347 | typedef asection * (*gc_mark_hook_fn) |
9348 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
9349 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
9350 | ||
07adf181 AM |
9351 | /* Default gc_mark_hook. */ |
9352 | ||
9353 | asection * | |
9354 | _bfd_elf_gc_mark_hook (asection *sec, | |
9355 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
9356 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
9357 | struct elf_link_hash_entry *h, | |
9358 | Elf_Internal_Sym *sym) | |
9359 | { | |
9360 | if (h != NULL) | |
9361 | { | |
9362 | switch (h->root.type) | |
9363 | { | |
9364 | case bfd_link_hash_defined: | |
9365 | case bfd_link_hash_defweak: | |
9366 | return h->root.u.def.section; | |
9367 | ||
9368 | case bfd_link_hash_common: | |
9369 | return h->root.u.c.p->section; | |
9370 | ||
9371 | default: | |
9372 | break; | |
9373 | } | |
9374 | } | |
9375 | else | |
9376 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
9377 | ||
9378 | return NULL; | |
9379 | } | |
9380 | ||
9381 | /* The mark phase of garbage collection. For a given section, mark | |
9382 | it and any sections in this section's group, and all the sections | |
9383 | which define symbols to which it refers. */ | |
9384 | ||
ccfa59ea AM |
9385 | bfd_boolean |
9386 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
9387 | asection *sec, | |
9388 | gc_mark_hook_fn gc_mark_hook) | |
c152c796 AM |
9389 | { |
9390 | bfd_boolean ret; | |
39c2f51b | 9391 | bfd_boolean is_eh; |
c152c796 AM |
9392 | asection *group_sec; |
9393 | ||
9394 | sec->gc_mark = 1; | |
9395 | ||
9396 | /* Mark all the sections in the group. */ | |
9397 | group_sec = elf_section_data (sec)->next_in_group; | |
9398 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 9399 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
9400 | return FALSE; |
9401 | ||
9402 | /* Look through the section relocs. */ | |
9403 | ret = TRUE; | |
39c2f51b | 9404 | is_eh = strcmp (sec->name, ".eh_frame") == 0; |
c152c796 AM |
9405 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
9406 | { | |
9407 | Elf_Internal_Rela *relstart, *rel, *relend; | |
9408 | Elf_Internal_Shdr *symtab_hdr; | |
9409 | struct elf_link_hash_entry **sym_hashes; | |
9410 | size_t nlocsyms; | |
9411 | size_t extsymoff; | |
9412 | bfd *input_bfd = sec->owner; | |
9413 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
9414 | Elf_Internal_Sym *isym = NULL; | |
9415 | int r_sym_shift; | |
9416 | ||
9417 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
9418 | sym_hashes = elf_sym_hashes (input_bfd); | |
9419 | ||
9420 | /* Read the local symbols. */ | |
9421 | if (elf_bad_symtab (input_bfd)) | |
9422 | { | |
9423 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9424 | extsymoff = 0; | |
9425 | } | |
9426 | else | |
9427 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
9428 | ||
9429 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9430 | if (isym == NULL && nlocsyms != 0) | |
9431 | { | |
9432 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
9433 | NULL, NULL, NULL); | |
9434 | if (isym == NULL) | |
9435 | return FALSE; | |
9436 | } | |
9437 | ||
9438 | /* Read the relocations. */ | |
9439 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
9440 | info->keep_memory); | |
9441 | if (relstart == NULL) | |
9442 | { | |
9443 | ret = FALSE; | |
9444 | goto out1; | |
9445 | } | |
9446 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
9447 | ||
9448 | if (bed->s->arch_size == 32) | |
9449 | r_sym_shift = 8; | |
9450 | else | |
9451 | r_sym_shift = 32; | |
9452 | ||
9453 | for (rel = relstart; rel < relend; rel++) | |
9454 | { | |
9455 | unsigned long r_symndx; | |
9456 | asection *rsec; | |
9457 | struct elf_link_hash_entry *h; | |
9458 | ||
9459 | r_symndx = rel->r_info >> r_sym_shift; | |
9460 | if (r_symndx == 0) | |
9461 | continue; | |
9462 | ||
9463 | if (r_symndx >= nlocsyms | |
9464 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
9465 | { | |
9466 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
9467 | while (h->root.type == bfd_link_hash_indirect |
9468 | || h->root.type == bfd_link_hash_warning) | |
9469 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
9470 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
9471 | } | |
9472 | else | |
9473 | { | |
9474 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
9475 | } | |
9476 | ||
9477 | if (rsec && !rsec->gc_mark) | |
9478 | { | |
9479 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
9480 | rsec->gc_mark = 1; | |
39c2f51b AM |
9481 | else if (is_eh) |
9482 | rsec->gc_mark_from_eh = 1; | |
ccfa59ea | 9483 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
9484 | { |
9485 | ret = FALSE; | |
9486 | goto out2; | |
9487 | } | |
9488 | } | |
9489 | } | |
9490 | ||
9491 | out2: | |
9492 | if (elf_section_data (sec)->relocs != relstart) | |
9493 | free (relstart); | |
9494 | out1: | |
9495 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
9496 | { | |
9497 | if (! info->keep_memory) | |
9498 | free (isym); | |
9499 | else | |
9500 | symtab_hdr->contents = (unsigned char *) isym; | |
9501 | } | |
9502 | } | |
9503 | ||
9504 | return ret; | |
9505 | } | |
9506 | ||
9507 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
9508 | ||
c17d87de NC |
9509 | struct elf_gc_sweep_symbol_info |
9510 | { | |
ccabcbe5 AM |
9511 | struct bfd_link_info *info; |
9512 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
9513 | bfd_boolean); | |
9514 | }; | |
9515 | ||
c152c796 | 9516 | static bfd_boolean |
ccabcbe5 | 9517 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 9518 | { |
c152c796 AM |
9519 | if (h->root.type == bfd_link_hash_warning) |
9520 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9521 | ||
ccabcbe5 AM |
9522 | if ((h->root.type == bfd_link_hash_defined |
9523 | || h->root.type == bfd_link_hash_defweak) | |
9524 | && !h->root.u.def.section->gc_mark | |
9525 | && !(h->root.u.def.section->owner->flags & DYNAMIC)) | |
9526 | { | |
9527 | struct elf_gc_sweep_symbol_info *inf = data; | |
9528 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
9529 | } | |
c152c796 AM |
9530 | |
9531 | return TRUE; | |
9532 | } | |
9533 | ||
9534 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
9535 | ||
9536 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
9537 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
9538 | ||
9539 | static bfd_boolean | |
ccabcbe5 | 9540 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
9541 | { |
9542 | bfd *sub; | |
ccabcbe5 AM |
9543 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9544 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
9545 | unsigned long section_sym_count; | |
9546 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 AM |
9547 | |
9548 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9549 | { | |
9550 | asection *o; | |
9551 | ||
9552 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9553 | continue; | |
9554 | ||
9555 | for (o = sub->sections; o != NULL; o = o->next) | |
9556 | { | |
7c2c8505 AM |
9557 | /* Keep debug and special sections. */ |
9558 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
dea5f36a | 9559 | || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) |
c152c796 AM |
9560 | o->gc_mark = 1; |
9561 | ||
9562 | if (o->gc_mark) | |
9563 | continue; | |
9564 | ||
9565 | /* Skip sweeping sections already excluded. */ | |
9566 | if (o->flags & SEC_EXCLUDE) | |
9567 | continue; | |
9568 | ||
9569 | /* Since this is early in the link process, it is simple | |
9570 | to remove a section from the output. */ | |
9571 | o->flags |= SEC_EXCLUDE; | |
9572 | ||
c17d87de NC |
9573 | if (info->print_gc_sections == TRUE) |
9574 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); | |
9575 | ||
c152c796 AM |
9576 | /* But we also have to update some of the relocation |
9577 | info we collected before. */ | |
9578 | if (gc_sweep_hook | |
e8aaee2a AM |
9579 | && (o->flags & SEC_RELOC) != 0 |
9580 | && o->reloc_count > 0 | |
9581 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
9582 | { |
9583 | Elf_Internal_Rela *internal_relocs; | |
9584 | bfd_boolean r; | |
9585 | ||
9586 | internal_relocs | |
9587 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
9588 | info->keep_memory); | |
9589 | if (internal_relocs == NULL) | |
9590 | return FALSE; | |
9591 | ||
9592 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
9593 | ||
9594 | if (elf_section_data (o)->relocs != internal_relocs) | |
9595 | free (internal_relocs); | |
9596 | ||
9597 | if (!r) | |
9598 | return FALSE; | |
9599 | } | |
9600 | } | |
9601 | } | |
9602 | ||
9603 | /* Remove the symbols that were in the swept sections from the dynamic | |
9604 | symbol table. GCFIXME: Anyone know how to get them out of the | |
9605 | static symbol table as well? */ | |
ccabcbe5 AM |
9606 | sweep_info.info = info; |
9607 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
9608 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
9609 | &sweep_info); | |
c152c796 | 9610 | |
ccabcbe5 | 9611 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
9612 | return TRUE; |
9613 | } | |
9614 | ||
9615 | /* Propagate collected vtable information. This is called through | |
9616 | elf_link_hash_traverse. */ | |
9617 | ||
9618 | static bfd_boolean | |
9619 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
9620 | { | |
9621 | if (h->root.type == bfd_link_hash_warning) | |
9622 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9623 | ||
9624 | /* Those that are not vtables. */ | |
f6e332e6 | 9625 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
9626 | return TRUE; |
9627 | ||
9628 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 9629 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
9630 | return TRUE; |
9631 | ||
9632 | /* If we've already been done, exit. */ | |
f6e332e6 | 9633 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
9634 | return TRUE; |
9635 | ||
9636 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 9637 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 9638 | |
f6e332e6 | 9639 | if (h->vtable->used == NULL) |
c152c796 AM |
9640 | { |
9641 | /* None of this table's entries were referenced. Re-use the | |
9642 | parent's table. */ | |
f6e332e6 AM |
9643 | h->vtable->used = h->vtable->parent->vtable->used; |
9644 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
9645 | } |
9646 | else | |
9647 | { | |
9648 | size_t n; | |
9649 | bfd_boolean *cu, *pu; | |
9650 | ||
9651 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 9652 | cu = h->vtable->used; |
c152c796 | 9653 | cu[-1] = TRUE; |
f6e332e6 | 9654 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
9655 | if (pu != NULL) |
9656 | { | |
9657 | const struct elf_backend_data *bed; | |
9658 | unsigned int log_file_align; | |
9659 | ||
9660 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
9661 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 9662 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
9663 | while (n--) |
9664 | { | |
9665 | if (*pu) | |
9666 | *cu = TRUE; | |
9667 | pu++; | |
9668 | cu++; | |
9669 | } | |
9670 | } | |
9671 | } | |
9672 | ||
9673 | return TRUE; | |
9674 | } | |
9675 | ||
9676 | static bfd_boolean | |
9677 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
9678 | { | |
9679 | asection *sec; | |
9680 | bfd_vma hstart, hend; | |
9681 | Elf_Internal_Rela *relstart, *relend, *rel; | |
9682 | const struct elf_backend_data *bed; | |
9683 | unsigned int log_file_align; | |
9684 | ||
9685 | if (h->root.type == bfd_link_hash_warning) | |
9686 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9687 | ||
9688 | /* Take care of both those symbols that do not describe vtables as | |
9689 | well as those that are not loaded. */ | |
f6e332e6 | 9690 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
9691 | return TRUE; |
9692 | ||
9693 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
9694 | || h->root.type == bfd_link_hash_defweak); | |
9695 | ||
9696 | sec = h->root.u.def.section; | |
9697 | hstart = h->root.u.def.value; | |
9698 | hend = hstart + h->size; | |
9699 | ||
9700 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
9701 | if (!relstart) | |
9702 | return *(bfd_boolean *) okp = FALSE; | |
9703 | bed = get_elf_backend_data (sec->owner); | |
9704 | log_file_align = bed->s->log_file_align; | |
9705 | ||
9706 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
9707 | ||
9708 | for (rel = relstart; rel < relend; ++rel) | |
9709 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
9710 | { | |
9711 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
9712 | if (h->vtable->used |
9713 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
9714 | { |
9715 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 9716 | if (h->vtable->used[entry]) |
c152c796 AM |
9717 | continue; |
9718 | } | |
9719 | /* Otherwise, kill it. */ | |
9720 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
9721 | } | |
9722 | ||
9723 | return TRUE; | |
9724 | } | |
9725 | ||
87538722 AM |
9726 | /* Mark sections containing dynamically referenced symbols. When |
9727 | building shared libraries, we must assume that any visible symbol is | |
9728 | referenced. */ | |
715df9b8 | 9729 | |
64d03ab5 AM |
9730 | bfd_boolean |
9731 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 9732 | { |
87538722 AM |
9733 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
9734 | ||
715df9b8 EB |
9735 | if (h->root.type == bfd_link_hash_warning) |
9736 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9737 | ||
9738 | if ((h->root.type == bfd_link_hash_defined | |
9739 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 9740 | && (h->ref_dynamic |
5adcfd8b | 9741 | || (!info->executable |
87538722 AM |
9742 | && h->def_regular |
9743 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
9744 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) | |
715df9b8 EB |
9745 | h->root.u.def.section->flags |= SEC_KEEP; |
9746 | ||
9747 | return TRUE; | |
9748 | } | |
3b36f7e6 | 9749 | |
c152c796 AM |
9750 | /* Do mark and sweep of unused sections. */ |
9751 | ||
9752 | bfd_boolean | |
9753 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
9754 | { | |
9755 | bfd_boolean ok = TRUE; | |
9756 | bfd *sub; | |
9757 | asection * (*gc_mark_hook) | |
9758 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
9759 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
64d03ab5 | 9760 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 9761 | |
64d03ab5 | 9762 | if (!bed->can_gc_sections |
c152c796 AM |
9763 | || info->relocatable |
9764 | || info->emitrelocations | |
715df9b8 | 9765 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
9766 | { |
9767 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
9768 | return TRUE; | |
9769 | } | |
9770 | ||
9771 | /* Apply transitive closure to the vtable entry usage info. */ | |
9772 | elf_link_hash_traverse (elf_hash_table (info), | |
9773 | elf_gc_propagate_vtable_entries_used, | |
9774 | &ok); | |
9775 | if (!ok) | |
9776 | return FALSE; | |
9777 | ||
9778 | /* Kill the vtable relocations that were not used. */ | |
9779 | elf_link_hash_traverse (elf_hash_table (info), | |
9780 | elf_gc_smash_unused_vtentry_relocs, | |
9781 | &ok); | |
9782 | if (!ok) | |
9783 | return FALSE; | |
9784 | ||
715df9b8 EB |
9785 | /* Mark dynamically referenced symbols. */ |
9786 | if (elf_hash_table (info)->dynamic_sections_created) | |
9787 | elf_link_hash_traverse (elf_hash_table (info), | |
64d03ab5 | 9788 | bed->gc_mark_dynamic_ref, |
87538722 | 9789 | info); |
c152c796 | 9790 | |
715df9b8 | 9791 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 9792 | gc_mark_hook = bed->gc_mark_hook; |
c152c796 AM |
9793 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
9794 | { | |
9795 | asection *o; | |
9796 | ||
9797 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9798 | continue; | |
9799 | ||
9800 | for (o = sub->sections; o != NULL; o = o->next) | |
a14a5de3 | 9801 | if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark) |
39c2f51b AM |
9802 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) |
9803 | return FALSE; | |
c152c796 AM |
9804 | } |
9805 | ||
9e8cc8b4 AM |
9806 | /* ... again for sections marked from eh_frame. */ |
9807 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9808 | { | |
9809 | asection *o; | |
9810 | ||
9811 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9812 | continue; | |
9813 | ||
a48710b3 EB |
9814 | /* Keep .gcc_except_table.* if the associated .text.* (or the |
9815 | associated .gnu.linkonce.t.* if .text.* doesn't exist) is | |
9e8cc8b4 | 9816 | marked. This isn't very nice, but the proper solution, |
2de92251 | 9817 | splitting .eh_frame up and using comdat doesn't pan out |
9e8cc8b4 AM |
9818 | easily due to needing special relocs to handle the |
9819 | difference of two symbols in separate sections. | |
9820 | Don't keep code sections referenced by .eh_frame. */ | |
ea9986ff | 9821 | #define TEXT_PREFIX ".text." |
a48710b3 | 9822 | #define TEXT_PREFIX2 ".gnu.linkonce.t." |
ea9986ff | 9823 | #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table." |
9e8cc8b4 AM |
9824 | for (o = sub->sections; o != NULL; o = o->next) |
9825 | if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0) | |
9826 | { | |
ea9986ff | 9827 | if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX)) |
9e8cc8b4 | 9828 | { |
9e8cc8b4 | 9829 | char *fn_name; |
ea9986ff | 9830 | const char *sec_name; |
9e8cc8b4 | 9831 | asection *fn_text; |
a48710b3 | 9832 | unsigned o_name_prefix_len , fn_name_prefix_len, tmp; |
9e8cc8b4 | 9833 | |
a48710b3 | 9834 | o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX); |
ea9986ff | 9835 | sec_name = o->name + o_name_prefix_len; |
a48710b3 EB |
9836 | fn_name_prefix_len = strlen (TEXT_PREFIX); |
9837 | tmp = strlen (TEXT_PREFIX2); | |
9838 | if (tmp > fn_name_prefix_len) | |
9839 | fn_name_prefix_len = tmp; | |
9840 | fn_name | |
9841 | = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1); | |
9e8cc8b4 AM |
9842 | if (fn_name == NULL) |
9843 | return FALSE; | |
a48710b3 EB |
9844 | |
9845 | /* Try the first prefix. */ | |
ea9986ff | 9846 | sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name); |
9e8cc8b4 | 9847 | fn_text = bfd_get_section_by_name (sub, fn_name); |
a48710b3 EB |
9848 | |
9849 | /* Try the second prefix. */ | |
9850 | if (fn_text == NULL) | |
9851 | { | |
9852 | sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name); | |
9853 | fn_text = bfd_get_section_by_name (sub, fn_name); | |
9854 | } | |
9855 | ||
9e8cc8b4 AM |
9856 | free (fn_name); |
9857 | if (fn_text == NULL || !fn_text->gc_mark) | |
9858 | continue; | |
9859 | } | |
9860 | ||
9861 | /* If not using specially named exception table section, | |
9862 | then keep whatever we are using. */ | |
9863 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
9864 | return FALSE; | |
9865 | } | |
9866 | } | |
9867 | ||
c152c796 | 9868 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 9869 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
9870 | } |
9871 | \f | |
9872 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
9873 | ||
9874 | bfd_boolean | |
9875 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
9876 | asection *sec, | |
9877 | struct elf_link_hash_entry *h, | |
9878 | bfd_vma offset) | |
9879 | { | |
9880 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
9881 | struct elf_link_hash_entry **search, *child; | |
9882 | bfd_size_type extsymcount; | |
9883 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9884 | ||
9885 | /* The sh_info field of the symtab header tells us where the | |
9886 | external symbols start. We don't care about the local symbols at | |
9887 | this point. */ | |
9888 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
9889 | if (!elf_bad_symtab (abfd)) | |
9890 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
9891 | ||
9892 | sym_hashes = elf_sym_hashes (abfd); | |
9893 | sym_hashes_end = sym_hashes + extsymcount; | |
9894 | ||
9895 | /* Hunt down the child symbol, which is in this section at the same | |
9896 | offset as the relocation. */ | |
9897 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
9898 | { | |
9899 | if ((child = *search) != NULL | |
9900 | && (child->root.type == bfd_link_hash_defined | |
9901 | || child->root.type == bfd_link_hash_defweak) | |
9902 | && child->root.u.def.section == sec | |
9903 | && child->root.u.def.value == offset) | |
9904 | goto win; | |
9905 | } | |
9906 | ||
d003868e AM |
9907 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
9908 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
9909 | bfd_set_error (bfd_error_invalid_operation); |
9910 | return FALSE; | |
9911 | ||
9912 | win: | |
f6e332e6 AM |
9913 | if (!child->vtable) |
9914 | { | |
9915 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
9916 | if (!child->vtable) | |
9917 | return FALSE; | |
9918 | } | |
c152c796 AM |
9919 | if (!h) |
9920 | { | |
9921 | /* This *should* only be the absolute section. It could potentially | |
9922 | be that someone has defined a non-global vtable though, which | |
9923 | would be bad. It isn't worth paging in the local symbols to be | |
9924 | sure though; that case should simply be handled by the assembler. */ | |
9925 | ||
f6e332e6 | 9926 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
9927 | } |
9928 | else | |
f6e332e6 | 9929 | child->vtable->parent = h; |
c152c796 AM |
9930 | |
9931 | return TRUE; | |
9932 | } | |
9933 | ||
9934 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
9935 | ||
9936 | bfd_boolean | |
9937 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
9938 | asection *sec ATTRIBUTE_UNUSED, | |
9939 | struct elf_link_hash_entry *h, | |
9940 | bfd_vma addend) | |
9941 | { | |
9942 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9943 | unsigned int log_file_align = bed->s->log_file_align; | |
9944 | ||
f6e332e6 AM |
9945 | if (!h->vtable) |
9946 | { | |
9947 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
9948 | if (!h->vtable) | |
9949 | return FALSE; | |
9950 | } | |
9951 | ||
9952 | if (addend >= h->vtable->size) | |
c152c796 AM |
9953 | { |
9954 | size_t size, bytes, file_align; | |
f6e332e6 | 9955 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
9956 | |
9957 | /* While the symbol is undefined, we have to be prepared to handle | |
9958 | a zero size. */ | |
9959 | file_align = 1 << log_file_align; | |
9960 | if (h->root.type == bfd_link_hash_undefined) | |
9961 | size = addend + file_align; | |
9962 | else | |
9963 | { | |
9964 | size = h->size; | |
9965 | if (addend >= size) | |
9966 | { | |
9967 | /* Oops! We've got a reference past the defined end of | |
9968 | the table. This is probably a bug -- shall we warn? */ | |
9969 | size = addend + file_align; | |
9970 | } | |
9971 | } | |
9972 | size = (size + file_align - 1) & -file_align; | |
9973 | ||
9974 | /* Allocate one extra entry for use as a "done" flag for the | |
9975 | consolidation pass. */ | |
9976 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
9977 | ||
9978 | if (ptr) | |
9979 | { | |
9980 | ptr = bfd_realloc (ptr - 1, bytes); | |
9981 | ||
9982 | if (ptr != NULL) | |
9983 | { | |
9984 | size_t oldbytes; | |
9985 | ||
f6e332e6 | 9986 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
9987 | * sizeof (bfd_boolean)); |
9988 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
9989 | } | |
9990 | } | |
9991 | else | |
9992 | ptr = bfd_zmalloc (bytes); | |
9993 | ||
9994 | if (ptr == NULL) | |
9995 | return FALSE; | |
9996 | ||
9997 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
9998 | h->vtable->used = ptr + 1; |
9999 | h->vtable->size = size; | |
c152c796 AM |
10000 | } |
10001 | ||
f6e332e6 | 10002 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
10003 | |
10004 | return TRUE; | |
10005 | } | |
10006 | ||
10007 | struct alloc_got_off_arg { | |
10008 | bfd_vma gotoff; | |
10009 | unsigned int got_elt_size; | |
10010 | }; | |
10011 | ||
10012 | /* We need a special top-level link routine to convert got reference counts | |
10013 | to real got offsets. */ | |
10014 | ||
10015 | static bfd_boolean | |
10016 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
10017 | { | |
10018 | struct alloc_got_off_arg *gofarg = arg; | |
10019 | ||
10020 | if (h->root.type == bfd_link_hash_warning) | |
10021 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10022 | ||
10023 | if (h->got.refcount > 0) | |
10024 | { | |
10025 | h->got.offset = gofarg->gotoff; | |
10026 | gofarg->gotoff += gofarg->got_elt_size; | |
10027 | } | |
10028 | else | |
10029 | h->got.offset = (bfd_vma) -1; | |
10030 | ||
10031 | return TRUE; | |
10032 | } | |
10033 | ||
10034 | /* And an accompanying bit to work out final got entry offsets once | |
10035 | we're done. Should be called from final_link. */ | |
10036 | ||
10037 | bfd_boolean | |
10038 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
10039 | struct bfd_link_info *info) | |
10040 | { | |
10041 | bfd *i; | |
10042 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10043 | bfd_vma gotoff; | |
10044 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
10045 | struct alloc_got_off_arg gofarg; | |
10046 | ||
10047 | if (! is_elf_hash_table (info->hash)) | |
10048 | return FALSE; | |
10049 | ||
10050 | /* The GOT offset is relative to the .got section, but the GOT header is | |
10051 | put into the .got.plt section, if the backend uses it. */ | |
10052 | if (bed->want_got_plt) | |
10053 | gotoff = 0; | |
10054 | else | |
10055 | gotoff = bed->got_header_size; | |
10056 | ||
10057 | /* Do the local .got entries first. */ | |
10058 | for (i = info->input_bfds; i; i = i->link_next) | |
10059 | { | |
10060 | bfd_signed_vma *local_got; | |
10061 | bfd_size_type j, locsymcount; | |
10062 | Elf_Internal_Shdr *symtab_hdr; | |
10063 | ||
10064 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
10065 | continue; | |
10066 | ||
10067 | local_got = elf_local_got_refcounts (i); | |
10068 | if (!local_got) | |
10069 | continue; | |
10070 | ||
10071 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
10072 | if (elf_bad_symtab (i)) | |
10073 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10074 | else | |
10075 | locsymcount = symtab_hdr->sh_info; | |
10076 | ||
10077 | for (j = 0; j < locsymcount; ++j) | |
10078 | { | |
10079 | if (local_got[j] > 0) | |
10080 | { | |
10081 | local_got[j] = gotoff; | |
10082 | gotoff += got_elt_size; | |
10083 | } | |
10084 | else | |
10085 | local_got[j] = (bfd_vma) -1; | |
10086 | } | |
10087 | } | |
10088 | ||
10089 | /* Then the global .got entries. .plt refcounts are handled by | |
10090 | adjust_dynamic_symbol */ | |
10091 | gofarg.gotoff = gotoff; | |
10092 | gofarg.got_elt_size = got_elt_size; | |
10093 | elf_link_hash_traverse (elf_hash_table (info), | |
10094 | elf_gc_allocate_got_offsets, | |
10095 | &gofarg); | |
10096 | return TRUE; | |
10097 | } | |
10098 | ||
10099 | /* Many folk need no more in the way of final link than this, once | |
10100 | got entry reference counting is enabled. */ | |
10101 | ||
10102 | bfd_boolean | |
10103 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
10104 | { | |
10105 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
10106 | return FALSE; | |
10107 | ||
10108 | /* Invoke the regular ELF backend linker to do all the work. */ | |
10109 | return bfd_elf_final_link (abfd, info); | |
10110 | } | |
10111 | ||
10112 | bfd_boolean | |
10113 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
10114 | { | |
10115 | struct elf_reloc_cookie *rcookie = cookie; | |
10116 | ||
10117 | if (rcookie->bad_symtab) | |
10118 | rcookie->rel = rcookie->rels; | |
10119 | ||
10120 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
10121 | { | |
10122 | unsigned long r_symndx; | |
10123 | ||
10124 | if (! rcookie->bad_symtab) | |
10125 | if (rcookie->rel->r_offset > offset) | |
10126 | return FALSE; | |
10127 | if (rcookie->rel->r_offset != offset) | |
10128 | continue; | |
10129 | ||
10130 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
10131 | if (r_symndx == SHN_UNDEF) | |
10132 | return TRUE; | |
10133 | ||
10134 | if (r_symndx >= rcookie->locsymcount | |
10135 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
10136 | { | |
10137 | struct elf_link_hash_entry *h; | |
10138 | ||
10139 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
10140 | ||
10141 | while (h->root.type == bfd_link_hash_indirect | |
10142 | || h->root.type == bfd_link_hash_warning) | |
10143 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10144 | ||
10145 | if ((h->root.type == bfd_link_hash_defined | |
10146 | || h->root.type == bfd_link_hash_defweak) | |
10147 | && elf_discarded_section (h->root.u.def.section)) | |
10148 | return TRUE; | |
10149 | else | |
10150 | return FALSE; | |
10151 | } | |
10152 | else | |
10153 | { | |
10154 | /* It's not a relocation against a global symbol, | |
10155 | but it could be a relocation against a local | |
10156 | symbol for a discarded section. */ | |
10157 | asection *isec; | |
10158 | Elf_Internal_Sym *isym; | |
10159 | ||
10160 | /* Need to: get the symbol; get the section. */ | |
10161 | isym = &rcookie->locsyms[r_symndx]; | |
10162 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
10163 | { | |
10164 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
10165 | if (isec != NULL && elf_discarded_section (isec)) | |
10166 | return TRUE; | |
10167 | } | |
10168 | } | |
10169 | return FALSE; | |
10170 | } | |
10171 | return FALSE; | |
10172 | } | |
10173 | ||
10174 | /* Discard unneeded references to discarded sections. | |
10175 | Returns TRUE if any section's size was changed. */ | |
10176 | /* This function assumes that the relocations are in sorted order, | |
10177 | which is true for all known assemblers. */ | |
10178 | ||
10179 | bfd_boolean | |
10180 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
10181 | { | |
10182 | struct elf_reloc_cookie cookie; | |
10183 | asection *stab, *eh; | |
10184 | Elf_Internal_Shdr *symtab_hdr; | |
10185 | const struct elf_backend_data *bed; | |
10186 | bfd *abfd; | |
10187 | unsigned int count; | |
10188 | bfd_boolean ret = FALSE; | |
10189 | ||
10190 | if (info->traditional_format | |
10191 | || !is_elf_hash_table (info->hash)) | |
10192 | return FALSE; | |
10193 | ||
10194 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
10195 | { | |
10196 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
10197 | continue; | |
10198 | ||
10199 | bed = get_elf_backend_data (abfd); | |
10200 | ||
10201 | if ((abfd->flags & DYNAMIC) != 0) | |
10202 | continue; | |
10203 | ||
10204 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
10205 | if (info->relocatable | |
10206 | || (eh != NULL | |
eea6121a | 10207 | && (eh->size == 0 |
c152c796 AM |
10208 | || bfd_is_abs_section (eh->output_section)))) |
10209 | eh = NULL; | |
10210 | ||
10211 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
10212 | if (stab != NULL | |
eea6121a | 10213 | && (stab->size == 0 |
c152c796 AM |
10214 | || bfd_is_abs_section (stab->output_section) |
10215 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
10216 | stab = NULL; | |
10217 | ||
10218 | if (stab == NULL | |
10219 | && eh == NULL | |
10220 | && bed->elf_backend_discard_info == NULL) | |
10221 | continue; | |
10222 | ||
10223 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
10224 | cookie.abfd = abfd; | |
10225 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
10226 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
10227 | if (cookie.bad_symtab) | |
10228 | { | |
10229 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10230 | cookie.extsymoff = 0; | |
10231 | } | |
10232 | else | |
10233 | { | |
10234 | cookie.locsymcount = symtab_hdr->sh_info; | |
10235 | cookie.extsymoff = symtab_hdr->sh_info; | |
10236 | } | |
10237 | ||
10238 | if (bed->s->arch_size == 32) | |
10239 | cookie.r_sym_shift = 8; | |
10240 | else | |
10241 | cookie.r_sym_shift = 32; | |
10242 | ||
10243 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10244 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
10245 | { | |
10246 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
10247 | cookie.locsymcount, 0, | |
10248 | NULL, NULL, NULL); | |
10249 | if (cookie.locsyms == NULL) | |
10250 | return FALSE; | |
10251 | } | |
10252 | ||
10253 | if (stab != NULL) | |
10254 | { | |
10255 | cookie.rels = NULL; | |
10256 | count = stab->reloc_count; | |
10257 | if (count != 0) | |
10258 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
10259 | info->keep_memory); | |
10260 | if (cookie.rels != NULL) | |
10261 | { | |
10262 | cookie.rel = cookie.rels; | |
10263 | cookie.relend = cookie.rels; | |
10264 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
10265 | if (_bfd_discard_section_stabs (abfd, stab, | |
10266 | elf_section_data (stab)->sec_info, | |
10267 | bfd_elf_reloc_symbol_deleted_p, | |
10268 | &cookie)) | |
10269 | ret = TRUE; | |
10270 | if (elf_section_data (stab)->relocs != cookie.rels) | |
10271 | free (cookie.rels); | |
10272 | } | |
10273 | } | |
10274 | ||
10275 | if (eh != NULL) | |
10276 | { | |
10277 | cookie.rels = NULL; | |
10278 | count = eh->reloc_count; | |
10279 | if (count != 0) | |
10280 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
10281 | info->keep_memory); | |
10282 | cookie.rel = cookie.rels; | |
10283 | cookie.relend = cookie.rels; | |
10284 | if (cookie.rels != NULL) | |
10285 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
10286 | ||
10287 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
10288 | bfd_elf_reloc_symbol_deleted_p, | |
10289 | &cookie)) | |
10290 | ret = TRUE; | |
10291 | ||
10292 | if (cookie.rels != NULL | |
10293 | && elf_section_data (eh)->relocs != cookie.rels) | |
10294 | free (cookie.rels); | |
10295 | } | |
10296 | ||
10297 | if (bed->elf_backend_discard_info != NULL | |
10298 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
10299 | ret = TRUE; | |
10300 | ||
10301 | if (cookie.locsyms != NULL | |
10302 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
10303 | { | |
10304 | if (! info->keep_memory) | |
10305 | free (cookie.locsyms); | |
10306 | else | |
10307 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
10308 | } | |
10309 | } | |
10310 | ||
10311 | if (info->eh_frame_hdr | |
10312 | && !info->relocatable | |
10313 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
10314 | ret = TRUE; | |
10315 | ||
10316 | return ret; | |
10317 | } | |
082b7297 L |
10318 | |
10319 | void | |
c0f00686 L |
10320 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec, |
10321 | struct bfd_link_info *info) | |
082b7297 L |
10322 | { |
10323 | flagword flags; | |
6d2cd210 | 10324 | const char *name, *p; |
082b7297 L |
10325 | struct bfd_section_already_linked *l; |
10326 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 | 10327 | |
3d7f7666 L |
10328 | if (sec->output_section == bfd_abs_section_ptr) |
10329 | return; | |
082b7297 L |
10330 | |
10331 | flags = sec->flags; | |
3d7f7666 | 10332 | |
c2370991 AM |
10333 | /* Return if it isn't a linkonce section. A comdat group section |
10334 | also has SEC_LINK_ONCE set. */ | |
10335 | if ((flags & SEC_LINK_ONCE) == 0) | |
082b7297 L |
10336 | return; |
10337 | ||
c2370991 AM |
10338 | /* Don't put group member sections on our list of already linked |
10339 | sections. They are handled as a group via their group section. */ | |
10340 | if (elf_sec_group (sec) != NULL) | |
10341 | return; | |
3d7f7666 | 10342 | |
082b7297 L |
10343 | /* FIXME: When doing a relocatable link, we may have trouble |
10344 | copying relocations in other sections that refer to local symbols | |
10345 | in the section being discarded. Those relocations will have to | |
10346 | be converted somehow; as of this writing I'm not sure that any of | |
10347 | the backends handle that correctly. | |
10348 | ||
10349 | It is tempting to instead not discard link once sections when | |
10350 | doing a relocatable link (technically, they should be discarded | |
10351 | whenever we are building constructors). However, that fails, | |
10352 | because the linker winds up combining all the link once sections | |
10353 | into a single large link once section, which defeats the purpose | |
10354 | of having link once sections in the first place. | |
10355 | ||
10356 | Also, not merging link once sections in a relocatable link | |
10357 | causes trouble for MIPS ELF, which relies on link once semantics | |
10358 | to handle the .reginfo section correctly. */ | |
10359 | ||
10360 | name = bfd_get_section_name (abfd, sec); | |
10361 | ||
0112cd26 | 10362 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
6d2cd210 JJ |
10363 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
10364 | p++; | |
10365 | else | |
10366 | p = name; | |
10367 | ||
10368 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
10369 | |
10370 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
10371 | { | |
c2370991 AM |
10372 | /* We may have 2 different types of sections on the list: group |
10373 | sections and linkonce sections. Match like sections. */ | |
3d7f7666 | 10374 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 10375 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
10376 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
10377 | { | |
10378 | /* The section has already been linked. See if we should | |
6d2cd210 | 10379 | issue a warning. */ |
082b7297 L |
10380 | switch (flags & SEC_LINK_DUPLICATES) |
10381 | { | |
10382 | default: | |
10383 | abort (); | |
10384 | ||
10385 | case SEC_LINK_DUPLICATES_DISCARD: | |
10386 | break; | |
10387 | ||
10388 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
10389 | (*_bfd_error_handler) | |
c93625e2 | 10390 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 10391 | abfd, sec); |
082b7297 L |
10392 | break; |
10393 | ||
10394 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
10395 | if (sec->size != l->sec->size) | |
10396 | (*_bfd_error_handler) | |
c93625e2 | 10397 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 10398 | abfd, sec); |
082b7297 | 10399 | break; |
ea5158d8 DJ |
10400 | |
10401 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
10402 | if (sec->size != l->sec->size) | |
10403 | (*_bfd_error_handler) | |
c93625e2 | 10404 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
10405 | abfd, sec); |
10406 | else if (sec->size != 0) | |
10407 | { | |
10408 | bfd_byte *sec_contents, *l_sec_contents; | |
10409 | ||
10410 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
10411 | (*_bfd_error_handler) | |
c93625e2 | 10412 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
10413 | abfd, sec); |
10414 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
10415 | &l_sec_contents)) | |
10416 | (*_bfd_error_handler) | |
c93625e2 | 10417 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
10418 | l->sec->owner, l->sec); |
10419 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
10420 | (*_bfd_error_handler) | |
c93625e2 | 10421 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
10422 | abfd, sec); |
10423 | ||
10424 | if (sec_contents) | |
10425 | free (sec_contents); | |
10426 | if (l_sec_contents) | |
10427 | free (l_sec_contents); | |
10428 | } | |
10429 | break; | |
082b7297 L |
10430 | } |
10431 | ||
10432 | /* Set the output_section field so that lang_add_section | |
10433 | does not create a lang_input_section structure for this | |
10434 | section. Since there might be a symbol in the section | |
10435 | being discarded, we must retain a pointer to the section | |
10436 | which we are really going to use. */ | |
10437 | sec->output_section = bfd_abs_section_ptr; | |
10438 | sec->kept_section = l->sec; | |
3b36f7e6 | 10439 | |
082b7297 | 10440 | if (flags & SEC_GROUP) |
3d7f7666 L |
10441 | { |
10442 | asection *first = elf_next_in_group (sec); | |
10443 | asection *s = first; | |
10444 | ||
10445 | while (s != NULL) | |
10446 | { | |
10447 | s->output_section = bfd_abs_section_ptr; | |
10448 | /* Record which group discards it. */ | |
10449 | s->kept_section = l->sec; | |
10450 | s = elf_next_in_group (s); | |
10451 | /* These lists are circular. */ | |
10452 | if (s == first) | |
10453 | break; | |
10454 | } | |
10455 | } | |
082b7297 L |
10456 | |
10457 | return; | |
10458 | } | |
10459 | } | |
10460 | ||
c2370991 AM |
10461 | /* A single member comdat group section may be discarded by a |
10462 | linkonce section and vice versa. */ | |
10463 | ||
10464 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 10465 | { |
c2370991 AM |
10466 | asection *first = elf_next_in_group (sec); |
10467 | ||
10468 | if (first != NULL && elf_next_in_group (first) == first) | |
10469 | /* Check this single member group against linkonce sections. */ | |
10470 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
10471 | if ((l->sec->flags & SEC_GROUP) == 0 | |
10472 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
10473 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
10474 | { | |
10475 | first->output_section = bfd_abs_section_ptr; | |
10476 | first->kept_section = l->sec; | |
10477 | sec->output_section = bfd_abs_section_ptr; | |
10478 | break; | |
10479 | } | |
3d7f7666 L |
10480 | } |
10481 | else | |
c2370991 | 10482 | /* Check this linkonce section against single member groups. */ |
6d2cd210 JJ |
10483 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
10484 | if (l->sec->flags & SEC_GROUP) | |
10485 | { | |
10486 | asection *first = elf_next_in_group (l->sec); | |
10487 | ||
10488 | if (first != NULL | |
10489 | && elf_next_in_group (first) == first | |
c0f00686 | 10490 | && bfd_elf_match_symbols_in_sections (first, sec, info)) |
6d2cd210 JJ |
10491 | { |
10492 | sec->output_section = bfd_abs_section_ptr; | |
c2370991 | 10493 | sec->kept_section = first; |
6d2cd210 JJ |
10494 | break; |
10495 | } | |
10496 | } | |
10497 | ||
082b7297 L |
10498 | /* This is the first section with this name. Record it. */ |
10499 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
10500 | } | |
81e1b023 | 10501 | |
a4d8e49b L |
10502 | bfd_boolean |
10503 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
10504 | { | |
10505 | return sym->st_shndx == SHN_COMMON; | |
10506 | } | |
10507 | ||
10508 | unsigned int | |
10509 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
10510 | { | |
10511 | return SHN_COMMON; | |
10512 | } | |
10513 | ||
10514 | asection * | |
10515 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
10516 | { | |
10517 | return bfd_com_section_ptr; | |
10518 | } |