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 | |
0eddce27 | 188 | if (! info->traditional_format) |
45d6a902 | 189 | { |
3496cb2a L |
190 | s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr", |
191 | flags | SEC_READONLY); | |
45d6a902 | 192 | if (s == NULL |
45d6a902 AM |
193 | || ! bfd_set_section_alignment (abfd, s, 2)) |
194 | return FALSE; | |
195 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
196 | } | |
bb0deeff | 197 | |
45d6a902 AM |
198 | /* Create sections to hold version informations. These are removed |
199 | if they are not needed. */ | |
3496cb2a L |
200 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
201 | flags | SEC_READONLY); | |
45d6a902 | 202 | if (s == NULL |
45d6a902 AM |
203 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
204 | return FALSE; | |
205 | ||
3496cb2a L |
206 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
207 | flags | SEC_READONLY); | |
45d6a902 | 208 | if (s == NULL |
45d6a902 AM |
209 | || ! bfd_set_section_alignment (abfd, s, 1)) |
210 | return FALSE; | |
211 | ||
3496cb2a L |
212 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
213 | flags | SEC_READONLY); | |
45d6a902 | 214 | if (s == NULL |
45d6a902 AM |
215 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
216 | return FALSE; | |
217 | ||
3496cb2a L |
218 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
219 | flags | SEC_READONLY); | |
45d6a902 | 220 | if (s == NULL |
45d6a902 AM |
221 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
222 | return FALSE; | |
223 | ||
3496cb2a L |
224 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
225 | flags | SEC_READONLY); | |
226 | if (s == NULL) | |
45d6a902 AM |
227 | return FALSE; |
228 | ||
3496cb2a | 229 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 230 | if (s == NULL |
45d6a902 AM |
231 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
232 | return FALSE; | |
233 | ||
234 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
235 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
236 | only want to define it if we are, in fact, creating a .dynamic | |
237 | section. We don't want to define it if there is no .dynamic | |
238 | section, since on some ELF platforms the start up code examines it | |
239 | to decide how to initialize the process. */ | |
d98685ac | 240 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
241 | return FALSE; |
242 | ||
fdc90cb4 JJ |
243 | if (info->emit_hash) |
244 | { | |
245 | s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY); | |
246 | if (s == NULL | |
247 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
248 | return FALSE; | |
249 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
250 | } | |
251 | ||
252 | if (info->emit_gnu_hash) | |
253 | { | |
254 | s = bfd_make_section_with_flags (abfd, ".gnu.hash", | |
255 | flags | SEC_READONLY); | |
256 | if (s == NULL | |
257 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
258 | return FALSE; | |
259 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
260 | 4 32-bit words followed by variable count of 64-bit words, then | |
261 | variable count of 32-bit words. */ | |
262 | if (bed->s->arch_size == 64) | |
263 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
264 | else | |
265 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
266 | } | |
45d6a902 AM |
267 | |
268 | /* Let the backend create the rest of the sections. This lets the | |
269 | backend set the right flags. The backend will normally create | |
270 | the .got and .plt sections. */ | |
271 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
272 | return FALSE; | |
273 | ||
274 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
275 | ||
276 | return TRUE; | |
277 | } | |
278 | ||
279 | /* Create dynamic sections when linking against a dynamic object. */ | |
280 | ||
281 | bfd_boolean | |
268b6b39 | 282 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
283 | { |
284 | flagword flags, pltflags; | |
7325306f | 285 | struct elf_link_hash_entry *h; |
45d6a902 | 286 | asection *s; |
9c5bfbb7 | 287 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 288 | |
252b5132 RH |
289 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
290 | .rel[a].bss sections. */ | |
e5a52504 | 291 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
292 | |
293 | pltflags = flags; | |
252b5132 | 294 | if (bed->plt_not_loaded) |
6df4d94c MM |
295 | /* We do not clear SEC_ALLOC here because we still want the OS to |
296 | allocate space for the section; it's just that there's nothing | |
297 | to read in from the object file. */ | |
5d1634d7 | 298 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
299 | else |
300 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
301 | if (bed->plt_readonly) |
302 | pltflags |= SEC_READONLY; | |
303 | ||
3496cb2a | 304 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 305 | if (s == NULL |
252b5132 | 306 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 307 | return FALSE; |
252b5132 | 308 | |
d98685ac AM |
309 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
310 | .plt section. */ | |
7325306f RS |
311 | if (bed->want_plt_sym) |
312 | { | |
313 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
314 | "_PROCEDURE_LINKAGE_TABLE_"); | |
315 | elf_hash_table (info)->hplt = h; | |
316 | if (h == NULL) | |
317 | return FALSE; | |
318 | } | |
252b5132 | 319 | |
3496cb2a L |
320 | s = bfd_make_section_with_flags (abfd, |
321 | (bed->default_use_rela_p | |
322 | ? ".rela.plt" : ".rel.plt"), | |
323 | flags | SEC_READONLY); | |
252b5132 | 324 | if (s == NULL |
45d6a902 | 325 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 326 | return FALSE; |
252b5132 RH |
327 | |
328 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 329 | return FALSE; |
252b5132 | 330 | |
3018b441 RH |
331 | if (bed->want_dynbss) |
332 | { | |
333 | /* The .dynbss section is a place to put symbols which are defined | |
334 | by dynamic objects, are referenced by regular objects, and are | |
335 | not functions. We must allocate space for them in the process | |
336 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
337 | initialize them at run time. The linker script puts the .dynbss | |
338 | section into the .bss section of the final image. */ | |
3496cb2a L |
339 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
340 | (SEC_ALLOC | |
341 | | SEC_LINKER_CREATED)); | |
342 | if (s == NULL) | |
b34976b6 | 343 | return FALSE; |
252b5132 | 344 | |
3018b441 | 345 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
346 | normally needed. We need to create it here, though, so that the |
347 | linker will map it to an output section. We can't just create it | |
348 | only if we need it, because we will not know whether we need it | |
349 | until we have seen all the input files, and the first time the | |
350 | main linker code calls BFD after examining all the input files | |
351 | (size_dynamic_sections) the input sections have already been | |
352 | mapped to the output sections. If the section turns out not to | |
353 | be needed, we can discard it later. We will never need this | |
354 | section when generating a shared object, since they do not use | |
355 | copy relocs. */ | |
3018b441 RH |
356 | if (! info->shared) |
357 | { | |
3496cb2a L |
358 | s = bfd_make_section_with_flags (abfd, |
359 | (bed->default_use_rela_p | |
360 | ? ".rela.bss" : ".rel.bss"), | |
361 | flags | SEC_READONLY); | |
3018b441 | 362 | if (s == NULL |
45d6a902 | 363 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 364 | return FALSE; |
3018b441 | 365 | } |
252b5132 RH |
366 | } |
367 | ||
b34976b6 | 368 | return TRUE; |
252b5132 RH |
369 | } |
370 | \f | |
252b5132 RH |
371 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
372 | read the input files, since we need to have a list of all of them | |
373 | before we can determine the final sizes of the output sections. | |
374 | Note that we may actually call this function even though we are not | |
375 | going to output any dynamic symbols; in some cases we know that a | |
376 | symbol should be in the dynamic symbol table, but only if there is | |
377 | one. */ | |
378 | ||
b34976b6 | 379 | bfd_boolean |
c152c796 AM |
380 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
381 | struct elf_link_hash_entry *h) | |
252b5132 RH |
382 | { |
383 | if (h->dynindx == -1) | |
384 | { | |
2b0f7ef9 | 385 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 386 | char *p; |
252b5132 | 387 | const char *name; |
252b5132 RH |
388 | bfd_size_type indx; |
389 | ||
7a13edea NC |
390 | /* XXX: The ABI draft says the linker must turn hidden and |
391 | internal symbols into STB_LOCAL symbols when producing the | |
392 | DSO. However, if ld.so honors st_other in the dynamic table, | |
393 | this would not be necessary. */ | |
394 | switch (ELF_ST_VISIBILITY (h->other)) | |
395 | { | |
396 | case STV_INTERNAL: | |
397 | case STV_HIDDEN: | |
9d6eee78 L |
398 | if (h->root.type != bfd_link_hash_undefined |
399 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 400 | { |
f5385ebf | 401 | h->forced_local = 1; |
67687978 PB |
402 | if (!elf_hash_table (info)->is_relocatable_executable) |
403 | return TRUE; | |
7a13edea | 404 | } |
0444bdd4 | 405 | |
7a13edea NC |
406 | default: |
407 | break; | |
408 | } | |
409 | ||
252b5132 RH |
410 | h->dynindx = elf_hash_table (info)->dynsymcount; |
411 | ++elf_hash_table (info)->dynsymcount; | |
412 | ||
413 | dynstr = elf_hash_table (info)->dynstr; | |
414 | if (dynstr == NULL) | |
415 | { | |
416 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 417 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 418 | if (dynstr == NULL) |
b34976b6 | 419 | return FALSE; |
252b5132 RH |
420 | } |
421 | ||
422 | /* We don't put any version information in the dynamic string | |
aad5d350 | 423 | table. */ |
252b5132 RH |
424 | name = h->root.root.string; |
425 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
426 | if (p != NULL) |
427 | /* We know that the p points into writable memory. In fact, | |
428 | there are only a few symbols that have read-only names, being | |
429 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
430 | by the backends. Most symbols will have names pointing into | |
431 | an ELF string table read from a file, or to objalloc memory. */ | |
432 | *p = 0; | |
433 | ||
434 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
435 | ||
436 | if (p != NULL) | |
437 | *p = ELF_VER_CHR; | |
252b5132 RH |
438 | |
439 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 440 | return FALSE; |
252b5132 RH |
441 | h->dynstr_index = indx; |
442 | } | |
443 | ||
b34976b6 | 444 | return TRUE; |
252b5132 | 445 | } |
45d6a902 | 446 | \f |
55255dae L |
447 | /* Mark a symbol dynamic. */ |
448 | ||
449 | void | |
450 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, | |
451 | struct elf_link_hash_entry *h) | |
452 | { | |
453 | struct bfd_elf_dynamic_list *d = info->dynamic; | |
454 | ||
455 | if (d == NULL || info->relocatable) | |
456 | return; | |
457 | ||
458 | if ((*d->match) (&d->head, NULL, h->root.root.string)) | |
459 | h->dynamic = 1; | |
460 | } | |
461 | ||
45d6a902 AM |
462 | /* Record an assignment to a symbol made by a linker script. We need |
463 | this in case some dynamic object refers to this symbol. */ | |
464 | ||
465 | bfd_boolean | |
fe21a8fc L |
466 | bfd_elf_record_link_assignment (bfd *output_bfd, |
467 | struct bfd_link_info *info, | |
268b6b39 | 468 | const char *name, |
fe21a8fc L |
469 | bfd_boolean provide, |
470 | bfd_boolean hidden) | |
45d6a902 AM |
471 | { |
472 | struct elf_link_hash_entry *h; | |
4ea42fb7 | 473 | struct elf_link_hash_table *htab; |
45d6a902 | 474 | |
0eddce27 | 475 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
476 | return TRUE; |
477 | ||
4ea42fb7 AM |
478 | htab = elf_hash_table (info); |
479 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 480 | if (h == NULL) |
4ea42fb7 | 481 | return provide; |
45d6a902 | 482 | |
02bb6eae AO |
483 | /* Since we're defining the symbol, don't let it seem to have not |
484 | been defined. record_dynamic_symbol and size_dynamic_sections | |
77cfaee6 | 485 | may depend on this. */ |
02bb6eae AO |
486 | if (h->root.type == bfd_link_hash_undefweak |
487 | || h->root.type == bfd_link_hash_undefined) | |
77cfaee6 | 488 | { |
4ea42fb7 | 489 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
490 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
491 | bfd_link_repair_undef_list (&htab->root); | |
77cfaee6 | 492 | } |
02bb6eae | 493 | |
45d6a902 | 494 | if (h->root.type == bfd_link_hash_new) |
55255dae L |
495 | { |
496 | bfd_elf_link_mark_dynamic_symbol (info, h); | |
497 | h->non_elf = 0; | |
498 | } | |
45d6a902 AM |
499 | |
500 | /* If this symbol is being provided by the linker script, and it is | |
501 | currently defined by a dynamic object, but not by a regular | |
502 | object, then mark it as undefined so that the generic linker will | |
503 | force the correct value. */ | |
504 | if (provide | |
f5385ebf AM |
505 | && h->def_dynamic |
506 | && !h->def_regular) | |
45d6a902 AM |
507 | h->root.type = bfd_link_hash_undefined; |
508 | ||
509 | /* If this symbol is not being provided by the linker script, and it is | |
510 | currently defined by a dynamic object, but not by a regular object, | |
511 | then clear out any version information because the symbol will not be | |
512 | associated with the dynamic object any more. */ | |
513 | if (!provide | |
f5385ebf AM |
514 | && h->def_dynamic |
515 | && !h->def_regular) | |
45d6a902 AM |
516 | h->verinfo.verdef = NULL; |
517 | ||
f5385ebf | 518 | h->def_regular = 1; |
45d6a902 | 519 | |
fe21a8fc L |
520 | if (provide && hidden) |
521 | { | |
522 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
523 | ||
524 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
525 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
526 | } | |
527 | ||
6fa3860b PB |
528 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
529 | and executables. */ | |
530 | if (!info->relocatable | |
531 | && h->dynindx != -1 | |
532 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
533 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
534 | h->forced_local = 1; | |
535 | ||
f5385ebf AM |
536 | if ((h->def_dynamic |
537 | || h->ref_dynamic | |
67687978 PB |
538 | || info->shared |
539 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
540 | && h->dynindx == -1) |
541 | { | |
c152c796 | 542 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
543 | return FALSE; |
544 | ||
545 | /* If this is a weak defined symbol, and we know a corresponding | |
546 | real symbol from the same dynamic object, make sure the real | |
547 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
548 | if (h->u.weakdef != NULL |
549 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 550 | { |
f6e332e6 | 551 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
552 | return FALSE; |
553 | } | |
554 | } | |
555 | ||
556 | return TRUE; | |
557 | } | |
42751cf3 | 558 | |
8c58d23b AM |
559 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
560 | success, and 2 on a failure caused by attempting to record a symbol | |
561 | in a discarded section, eg. a discarded link-once section symbol. */ | |
562 | ||
563 | int | |
c152c796 AM |
564 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
565 | bfd *input_bfd, | |
566 | long input_indx) | |
8c58d23b AM |
567 | { |
568 | bfd_size_type amt; | |
569 | struct elf_link_local_dynamic_entry *entry; | |
570 | struct elf_link_hash_table *eht; | |
571 | struct elf_strtab_hash *dynstr; | |
572 | unsigned long dynstr_index; | |
573 | char *name; | |
574 | Elf_External_Sym_Shndx eshndx; | |
575 | char esym[sizeof (Elf64_External_Sym)]; | |
576 | ||
0eddce27 | 577 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
578 | return 0; |
579 | ||
580 | /* See if the entry exists already. */ | |
581 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
582 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
583 | return 1; | |
584 | ||
585 | amt = sizeof (*entry); | |
268b6b39 | 586 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
587 | if (entry == NULL) |
588 | return 0; | |
589 | ||
590 | /* Go find the symbol, so that we can find it's name. */ | |
591 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 592 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
593 | { |
594 | bfd_release (input_bfd, entry); | |
595 | return 0; | |
596 | } | |
597 | ||
598 | if (entry->isym.st_shndx != SHN_UNDEF | |
599 | && (entry->isym.st_shndx < SHN_LORESERVE | |
600 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
601 | { | |
602 | asection *s; | |
603 | ||
604 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
605 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
606 | { | |
607 | /* We can still bfd_release here as nothing has done another | |
608 | bfd_alloc. We can't do this later in this function. */ | |
609 | bfd_release (input_bfd, entry); | |
610 | return 2; | |
611 | } | |
612 | } | |
613 | ||
614 | name = (bfd_elf_string_from_elf_section | |
615 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
616 | entry->isym.st_name)); | |
617 | ||
618 | dynstr = elf_hash_table (info)->dynstr; | |
619 | if (dynstr == NULL) | |
620 | { | |
621 | /* Create a strtab to hold the dynamic symbol names. */ | |
622 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
623 | if (dynstr == NULL) | |
624 | return 0; | |
625 | } | |
626 | ||
b34976b6 | 627 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
628 | if (dynstr_index == (unsigned long) -1) |
629 | return 0; | |
630 | entry->isym.st_name = dynstr_index; | |
631 | ||
632 | eht = elf_hash_table (info); | |
633 | ||
634 | entry->next = eht->dynlocal; | |
635 | eht->dynlocal = entry; | |
636 | entry->input_bfd = input_bfd; | |
637 | entry->input_indx = input_indx; | |
638 | eht->dynsymcount++; | |
639 | ||
640 | /* Whatever binding the symbol had before, it's now local. */ | |
641 | entry->isym.st_info | |
642 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
643 | ||
644 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
645 | ||
646 | return 1; | |
647 | } | |
648 | ||
30b30c21 | 649 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 650 | |
30b30c21 | 651 | long |
268b6b39 AM |
652 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
653 | bfd *input_bfd, | |
654 | long input_indx) | |
30b30c21 RH |
655 | { |
656 | struct elf_link_local_dynamic_entry *e; | |
657 | ||
658 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
659 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
660 | return e->dynindx; | |
661 | return -1; | |
662 | } | |
663 | ||
664 | /* This function is used to renumber the dynamic symbols, if some of | |
665 | them are removed because they are marked as local. This is called | |
666 | via elf_link_hash_traverse. */ | |
667 | ||
b34976b6 | 668 | static bfd_boolean |
268b6b39 AM |
669 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
670 | void *data) | |
42751cf3 | 671 | { |
268b6b39 | 672 | size_t *count = data; |
30b30c21 | 673 | |
e92d460e AM |
674 | if (h->root.type == bfd_link_hash_warning) |
675 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
676 | ||
6fa3860b PB |
677 | if (h->forced_local) |
678 | return TRUE; | |
679 | ||
680 | if (h->dynindx != -1) | |
681 | h->dynindx = ++(*count); | |
682 | ||
683 | return TRUE; | |
684 | } | |
685 | ||
686 | ||
687 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
688 | STB_LOCAL binding. */ | |
689 | ||
690 | static bfd_boolean | |
691 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
692 | void *data) | |
693 | { | |
694 | size_t *count = data; | |
695 | ||
696 | if (h->root.type == bfd_link_hash_warning) | |
697 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
698 | ||
699 | if (!h->forced_local) | |
700 | return TRUE; | |
701 | ||
42751cf3 | 702 | if (h->dynindx != -1) |
30b30c21 RH |
703 | h->dynindx = ++(*count); |
704 | ||
b34976b6 | 705 | return TRUE; |
42751cf3 | 706 | } |
30b30c21 | 707 | |
aee6f5b4 AO |
708 | /* Return true if the dynamic symbol for a given section should be |
709 | omitted when creating a shared library. */ | |
710 | bfd_boolean | |
711 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
712 | struct bfd_link_info *info, | |
713 | asection *p) | |
714 | { | |
74541ad4 AM |
715 | struct elf_link_hash_table *htab; |
716 | ||
aee6f5b4 AO |
717 | switch (elf_section_data (p)->this_hdr.sh_type) |
718 | { | |
719 | case SHT_PROGBITS: | |
720 | case SHT_NOBITS: | |
721 | /* If sh_type is yet undecided, assume it could be | |
722 | SHT_PROGBITS/SHT_NOBITS. */ | |
723 | case SHT_NULL: | |
74541ad4 AM |
724 | htab = elf_hash_table (info); |
725 | if (p == htab->tls_sec) | |
726 | return FALSE; | |
727 | ||
728 | if (htab->text_index_section != NULL) | |
729 | return p != htab->text_index_section && p != htab->data_index_section; | |
730 | ||
aee6f5b4 AO |
731 | if (strcmp (p->name, ".got") == 0 |
732 | || strcmp (p->name, ".got.plt") == 0 | |
733 | || strcmp (p->name, ".plt") == 0) | |
734 | { | |
735 | asection *ip; | |
aee6f5b4 | 736 | |
74541ad4 AM |
737 | if (htab->dynobj != NULL |
738 | && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL | |
aee6f5b4 AO |
739 | && (ip->flags & SEC_LINKER_CREATED) |
740 | && ip->output_section == p) | |
741 | return TRUE; | |
742 | } | |
743 | return FALSE; | |
744 | ||
745 | /* There shouldn't be section relative relocations | |
746 | against any other section. */ | |
747 | default: | |
748 | return TRUE; | |
749 | } | |
750 | } | |
751 | ||
062e2358 | 752 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
753 | symbol for each output section, which come first. Next come symbols |
754 | which have been forced to local binding. Then all of the back-end | |
755 | allocated local dynamic syms, followed by the rest of the global | |
756 | symbols. */ | |
30b30c21 | 757 | |
554220db AM |
758 | static unsigned long |
759 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
760 | struct bfd_link_info *info, | |
761 | unsigned long *section_sym_count) | |
30b30c21 RH |
762 | { |
763 | unsigned long dynsymcount = 0; | |
764 | ||
67687978 | 765 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 766 | { |
aee6f5b4 | 767 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
768 | asection *p; |
769 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 770 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
771 | && (p->flags & SEC_ALLOC) != 0 |
772 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
773 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
774 | else |
775 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 776 | } |
554220db | 777 | *section_sym_count = dynsymcount; |
30b30c21 | 778 | |
6fa3860b PB |
779 | elf_link_hash_traverse (elf_hash_table (info), |
780 | elf_link_renumber_local_hash_table_dynsyms, | |
781 | &dynsymcount); | |
782 | ||
30b30c21 RH |
783 | if (elf_hash_table (info)->dynlocal) |
784 | { | |
785 | struct elf_link_local_dynamic_entry *p; | |
786 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
787 | p->dynindx = ++dynsymcount; | |
788 | } | |
789 | ||
790 | elf_link_hash_traverse (elf_hash_table (info), | |
791 | elf_link_renumber_hash_table_dynsyms, | |
792 | &dynsymcount); | |
793 | ||
794 | /* There is an unused NULL entry at the head of the table which | |
795 | we must account for in our count. Unless there weren't any | |
796 | symbols, which means we'll have no table at all. */ | |
797 | if (dynsymcount != 0) | |
798 | ++dynsymcount; | |
799 | ||
ccabcbe5 AM |
800 | elf_hash_table (info)->dynsymcount = dynsymcount; |
801 | return dynsymcount; | |
30b30c21 | 802 | } |
252b5132 | 803 | |
45d6a902 AM |
804 | /* This function is called when we want to define a new symbol. It |
805 | handles the various cases which arise when we find a definition in | |
806 | a dynamic object, or when there is already a definition in a | |
807 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
808 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
809 | OVERRIDE if the old symbol is overriding a new definition. We set | |
810 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
811 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
812 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
813 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
814 | object is overridden by a regular object. */ | |
45d6a902 AM |
815 | |
816 | bfd_boolean | |
268b6b39 AM |
817 | _bfd_elf_merge_symbol (bfd *abfd, |
818 | struct bfd_link_info *info, | |
819 | const char *name, | |
820 | Elf_Internal_Sym *sym, | |
821 | asection **psec, | |
822 | bfd_vma *pvalue, | |
af44c138 | 823 | unsigned int *pold_alignment, |
268b6b39 AM |
824 | struct elf_link_hash_entry **sym_hash, |
825 | bfd_boolean *skip, | |
826 | bfd_boolean *override, | |
827 | bfd_boolean *type_change_ok, | |
0f8a2703 | 828 | bfd_boolean *size_change_ok) |
252b5132 | 829 | { |
7479dfd4 | 830 | asection *sec, *oldsec; |
45d6a902 AM |
831 | struct elf_link_hash_entry *h; |
832 | struct elf_link_hash_entry *flip; | |
833 | int bind; | |
834 | bfd *oldbfd; | |
835 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
77cfaee6 | 836 | bfd_boolean newweak, oldweak; |
a4d8e49b | 837 | const struct elf_backend_data *bed; |
45d6a902 AM |
838 | |
839 | *skip = FALSE; | |
840 | *override = FALSE; | |
841 | ||
842 | sec = *psec; | |
843 | bind = ELF_ST_BIND (sym->st_info); | |
844 | ||
cd7be95b KH |
845 | /* Silently discard TLS symbols from --just-syms. There's no way to |
846 | combine a static TLS block with a new TLS block for this executable. */ | |
847 | if (ELF_ST_TYPE (sym->st_info) == STT_TLS | |
848 | && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
849 | { | |
850 | *skip = TRUE; | |
851 | return TRUE; | |
852 | } | |
853 | ||
45d6a902 AM |
854 | if (! bfd_is_und_section (sec)) |
855 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
856 | else | |
857 | h = ((struct elf_link_hash_entry *) | |
858 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
859 | if (h == NULL) | |
860 | return FALSE; | |
861 | *sym_hash = h; | |
252b5132 | 862 | |
45d6a902 AM |
863 | /* This code is for coping with dynamic objects, and is only useful |
864 | if we are doing an ELF link. */ | |
865 | if (info->hash->creator != abfd->xvec) | |
866 | return TRUE; | |
252b5132 | 867 | |
45d6a902 AM |
868 | /* For merging, we only care about real symbols. */ |
869 | ||
870 | while (h->root.type == bfd_link_hash_indirect | |
871 | || h->root.type == bfd_link_hash_warning) | |
872 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
873 | ||
874 | /* If we just created the symbol, mark it as being an ELF symbol. | |
875 | Other than that, there is nothing to do--there is no merge issue | |
876 | with a newly defined symbol--so we just return. */ | |
877 | ||
878 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 879 | { |
55255dae | 880 | bfd_elf_link_mark_dynamic_symbol (info, h); |
f5385ebf | 881 | h->non_elf = 0; |
45d6a902 AM |
882 | return TRUE; |
883 | } | |
252b5132 | 884 | |
7479dfd4 L |
885 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
886 | existing symbol. */ | |
252b5132 | 887 | |
45d6a902 AM |
888 | switch (h->root.type) |
889 | { | |
890 | default: | |
891 | oldbfd = NULL; | |
7479dfd4 | 892 | oldsec = NULL; |
45d6a902 | 893 | break; |
252b5132 | 894 | |
45d6a902 AM |
895 | case bfd_link_hash_undefined: |
896 | case bfd_link_hash_undefweak: | |
897 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 898 | oldsec = NULL; |
45d6a902 AM |
899 | break; |
900 | ||
901 | case bfd_link_hash_defined: | |
902 | case bfd_link_hash_defweak: | |
903 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 904 | oldsec = h->root.u.def.section; |
45d6a902 AM |
905 | break; |
906 | ||
907 | case bfd_link_hash_common: | |
908 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 909 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
910 | break; |
911 | } | |
912 | ||
913 | /* In cases involving weak versioned symbols, we may wind up trying | |
914 | to merge a symbol with itself. Catch that here, to avoid the | |
915 | confusion that results if we try to override a symbol with | |
916 | itself. The additional tests catch cases like | |
917 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
918 | dynamic object, which we do want to handle here. */ | |
919 | if (abfd == oldbfd | |
920 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 921 | || !h->def_regular)) |
45d6a902 AM |
922 | return TRUE; |
923 | ||
924 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
925 | respectively, is from a dynamic object. */ | |
926 | ||
707bba77 | 927 | newdyn = (abfd->flags & DYNAMIC) != 0; |
45d6a902 | 928 | |
707bba77 | 929 | olddyn = FALSE; |
45d6a902 AM |
930 | if (oldbfd != NULL) |
931 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 932 | else if (oldsec != NULL) |
45d6a902 | 933 | { |
707bba77 | 934 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 935 | indices used by MIPS ELF. */ |
707bba77 | 936 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 937 | } |
252b5132 | 938 | |
45d6a902 AM |
939 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
940 | respectively, appear to be a definition rather than reference. */ | |
941 | ||
707bba77 | 942 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 943 | |
707bba77 AM |
944 | olddef = (h->root.type != bfd_link_hash_undefined |
945 | && h->root.type != bfd_link_hash_undefweak | |
946 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 947 | |
580a2b6e L |
948 | /* When we try to create a default indirect symbol from the dynamic |
949 | definition with the default version, we skip it if its type and | |
950 | the type of existing regular definition mismatch. We only do it | |
951 | if the existing regular definition won't be dynamic. */ | |
952 | if (pold_alignment == NULL | |
953 | && !info->shared | |
954 | && !info->export_dynamic | |
955 | && !h->ref_dynamic | |
956 | && newdyn | |
957 | && newdef | |
958 | && !olddyn | |
959 | && (olddef || h->root.type == bfd_link_hash_common) | |
960 | && ELF_ST_TYPE (sym->st_info) != h->type | |
961 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
962 | && h->type != STT_NOTYPE) | |
963 | { | |
964 | *skip = TRUE; | |
965 | return TRUE; | |
966 | } | |
967 | ||
68f49ba3 L |
968 | /* Check TLS symbol. We don't check undefined symbol introduced by |
969 | "ld -u". */ | |
7479dfd4 | 970 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) |
68f49ba3 L |
971 | && ELF_ST_TYPE (sym->st_info) != h->type |
972 | && oldbfd != NULL) | |
7479dfd4 L |
973 | { |
974 | bfd *ntbfd, *tbfd; | |
975 | bfd_boolean ntdef, tdef; | |
976 | asection *ntsec, *tsec; | |
977 | ||
978 | if (h->type == STT_TLS) | |
979 | { | |
3b36f7e6 | 980 | ntbfd = abfd; |
7479dfd4 L |
981 | ntsec = sec; |
982 | ntdef = newdef; | |
983 | tbfd = oldbfd; | |
984 | tsec = oldsec; | |
985 | tdef = olddef; | |
986 | } | |
987 | else | |
988 | { | |
989 | ntbfd = oldbfd; | |
990 | ntsec = oldsec; | |
991 | ntdef = olddef; | |
992 | tbfd = abfd; | |
993 | tsec = sec; | |
994 | tdef = newdef; | |
995 | } | |
996 | ||
997 | if (tdef && ntdef) | |
998 | (*_bfd_error_handler) | |
999 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
1000 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
1001 | else if (!tdef && !ntdef) | |
1002 | (*_bfd_error_handler) | |
1003 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
1004 | tbfd, ntbfd, h->root.root.string); | |
1005 | else if (tdef) | |
1006 | (*_bfd_error_handler) | |
1007 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
1008 | tbfd, tsec, ntbfd, h->root.root.string); | |
1009 | else | |
1010 | (*_bfd_error_handler) | |
1011 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
1012 | tbfd, ntbfd, ntsec, h->root.root.string); | |
1013 | ||
1014 | bfd_set_error (bfd_error_bad_value); | |
1015 | return FALSE; | |
1016 | } | |
1017 | ||
4cc11e76 | 1018 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
1019 | object or is weak in all dynamic objects. Internal and hidden |
1020 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 1021 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
1022 | { |
1023 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 1024 | h->dynamic_def = 1; |
45d6a902 | 1025 | else |
252b5132 | 1026 | { |
45d6a902 AM |
1027 | /* Check if this symbol is weak in all dynamic objects. If it |
1028 | is the first time we see it in a dynamic object, we mark | |
1029 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 1030 | if (!h->ref_dynamic) |
79349b09 | 1031 | { |
45d6a902 | 1032 | if (bind == STB_WEAK) |
f5385ebf | 1033 | h->dynamic_weak = 1; |
252b5132 | 1034 | } |
45d6a902 | 1035 | else if (bind != STB_WEAK) |
f5385ebf | 1036 | h->dynamic_weak = 0; |
252b5132 | 1037 | } |
45d6a902 | 1038 | } |
252b5132 | 1039 | |
45d6a902 AM |
1040 | /* If the old symbol has non-default visibility, we ignore the new |
1041 | definition from a dynamic object. */ | |
1042 | if (newdyn | |
9c7a29a3 | 1043 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1044 | && !bfd_is_und_section (sec)) |
1045 | { | |
1046 | *skip = TRUE; | |
1047 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1048 | h->ref_dynamic = 1; |
45d6a902 AM |
1049 | /* A protected symbol has external availability. Make sure it is |
1050 | recorded as dynamic. | |
1051 | ||
1052 | FIXME: Should we check type and size for protected symbol? */ | |
1053 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1054 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1055 | else |
1056 | return TRUE; | |
1057 | } | |
1058 | else if (!newdyn | |
9c7a29a3 | 1059 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1060 | && h->def_dynamic) |
45d6a902 AM |
1061 | { |
1062 | /* If the new symbol with non-default visibility comes from a | |
1063 | relocatable file and the old definition comes from a dynamic | |
1064 | object, we remove the old definition. */ | |
1065 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
d2dee3b2 L |
1066 | { |
1067 | /* Handle the case where the old dynamic definition is | |
1068 | default versioned. We need to copy the symbol info from | |
1069 | the symbol with default version to the normal one if it | |
1070 | was referenced before. */ | |
1071 | if (h->ref_regular) | |
1072 | { | |
1073 | const struct elf_backend_data *bed | |
1074 | = get_elf_backend_data (abfd); | |
1075 | struct elf_link_hash_entry *vh = *sym_hash; | |
1076 | vh->root.type = h->root.type; | |
1077 | h->root.type = bfd_link_hash_indirect; | |
1078 | (*bed->elf_backend_copy_indirect_symbol) (info, vh, h); | |
1079 | /* Protected symbols will override the dynamic definition | |
1080 | with default version. */ | |
1081 | if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED) | |
1082 | { | |
1083 | h->root.u.i.link = (struct bfd_link_hash_entry *) vh; | |
1084 | vh->dynamic_def = 1; | |
1085 | vh->ref_dynamic = 1; | |
1086 | } | |
1087 | else | |
1088 | { | |
1089 | h->root.type = vh->root.type; | |
1090 | vh->ref_dynamic = 0; | |
1091 | /* We have to hide it here since it was made dynamic | |
1092 | global with extra bits when the symbol info was | |
1093 | copied from the old dynamic definition. */ | |
1094 | (*bed->elf_backend_hide_symbol) (info, vh, TRUE); | |
1095 | } | |
1096 | h = vh; | |
1097 | } | |
1098 | else | |
1099 | h = *sym_hash; | |
1100 | } | |
1de1a317 | 1101 | |
f6e332e6 | 1102 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1103 | && bfd_is_und_section (sec)) |
1104 | { | |
1105 | /* If the new symbol is undefined and the old symbol was | |
1106 | also undefined before, we need to make sure | |
1107 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1108 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1109 | definition came from a dynamic object, it is still on the |
1110 | undefs list. */ | |
1111 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1112 | h->root.u.undef.abfd = abfd; |
1113 | } | |
1114 | else | |
1115 | { | |
1116 | h->root.type = bfd_link_hash_new; | |
1117 | h->root.u.undef.abfd = NULL; | |
1118 | } | |
1119 | ||
f5385ebf | 1120 | if (h->def_dynamic) |
252b5132 | 1121 | { |
f5385ebf AM |
1122 | h->def_dynamic = 0; |
1123 | h->ref_dynamic = 1; | |
1124 | h->dynamic_def = 1; | |
45d6a902 AM |
1125 | } |
1126 | /* FIXME: Should we check type and size for protected symbol? */ | |
1127 | h->size = 0; | |
1128 | h->type = 0; | |
1129 | return TRUE; | |
1130 | } | |
14a793b2 | 1131 | |
79349b09 AM |
1132 | /* Differentiate strong and weak symbols. */ |
1133 | newweak = bind == STB_WEAK; | |
1134 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1135 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 1136 | |
15b43f48 AM |
1137 | /* If a new weak symbol definition comes from a regular file and the |
1138 | old symbol comes from a dynamic library, we treat the new one as | |
1139 | strong. Similarly, an old weak symbol definition from a regular | |
1140 | file is treated as strong when the new symbol comes from a dynamic | |
1141 | library. Further, an old weak symbol from a dynamic library is | |
1142 | treated as strong if the new symbol is from a dynamic library. | |
1143 | This reflects the way glibc's ld.so works. | |
1144 | ||
1145 | Do this before setting *type_change_ok or *size_change_ok so that | |
1146 | we warn properly when dynamic library symbols are overridden. */ | |
1147 | ||
1148 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1149 | newweak = FALSE; |
15b43f48 | 1150 | if (olddef && newdyn) |
0f8a2703 AM |
1151 | oldweak = FALSE; |
1152 | ||
79349b09 AM |
1153 | /* It's OK to change the type if either the existing symbol or the |
1154 | new symbol is weak. A type change is also OK if the old symbol | |
1155 | is undefined and the new symbol is defined. */ | |
252b5132 | 1156 | |
79349b09 AM |
1157 | if (oldweak |
1158 | || newweak | |
1159 | || (newdef | |
1160 | && h->root.type == bfd_link_hash_undefined)) | |
1161 | *type_change_ok = TRUE; | |
1162 | ||
1163 | /* It's OK to change the size if either the existing symbol or the | |
1164 | new symbol is weak, or if the old symbol is undefined. */ | |
1165 | ||
1166 | if (*type_change_ok | |
1167 | || h->root.type == bfd_link_hash_undefined) | |
1168 | *size_change_ok = TRUE; | |
45d6a902 | 1169 | |
45d6a902 AM |
1170 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1171 | symbol, respectively, appears to be a common symbol in a dynamic | |
1172 | object. If a symbol appears in an uninitialized section, and is | |
1173 | not weak, and is not a function, then it may be a common symbol | |
1174 | which was resolved when the dynamic object was created. We want | |
1175 | to treat such symbols specially, because they raise special | |
1176 | considerations when setting the symbol size: if the symbol | |
1177 | appears as a common symbol in a regular object, and the size in | |
1178 | the regular object is larger, we must make sure that we use the | |
1179 | larger size. This problematic case can always be avoided in C, | |
1180 | but it must be handled correctly when using Fortran shared | |
1181 | libraries. | |
1182 | ||
1183 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1184 | likewise for OLDDYNCOMMON and OLDDEF. | |
1185 | ||
1186 | Note that this test is just a heuristic, and that it is quite | |
1187 | possible to have an uninitialized symbol in a shared object which | |
1188 | is really a definition, rather than a common symbol. This could | |
1189 | lead to some minor confusion when the symbol really is a common | |
1190 | symbol in some regular object. However, I think it will be | |
1191 | harmless. */ | |
1192 | ||
1193 | if (newdyn | |
1194 | && newdef | |
79349b09 | 1195 | && !newweak |
45d6a902 AM |
1196 | && (sec->flags & SEC_ALLOC) != 0 |
1197 | && (sec->flags & SEC_LOAD) == 0 | |
1198 | && sym->st_size > 0 | |
45d6a902 AM |
1199 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
1200 | newdyncommon = TRUE; | |
1201 | else | |
1202 | newdyncommon = FALSE; | |
1203 | ||
1204 | if (olddyn | |
1205 | && olddef | |
1206 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1207 | && h->def_dynamic |
45d6a902 AM |
1208 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1209 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1210 | && h->size > 0 | |
1211 | && h->type != STT_FUNC) | |
1212 | olddyncommon = TRUE; | |
1213 | else | |
1214 | olddyncommon = FALSE; | |
1215 | ||
a4d8e49b L |
1216 | /* We now know everything about the old and new symbols. We ask the |
1217 | backend to check if we can merge them. */ | |
1218 | bed = get_elf_backend_data (abfd); | |
1219 | if (bed->merge_symbol | |
1220 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1221 | pold_alignment, skip, override, | |
1222 | type_change_ok, size_change_ok, | |
1223 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1224 | abfd, &sec, | |
1225 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1226 | oldbfd, &oldsec)) | |
1227 | return FALSE; | |
1228 | ||
45d6a902 AM |
1229 | /* If both the old and the new symbols look like common symbols in a |
1230 | dynamic object, set the size of the symbol to the larger of the | |
1231 | two. */ | |
1232 | ||
1233 | if (olddyncommon | |
1234 | && newdyncommon | |
1235 | && sym->st_size != h->size) | |
1236 | { | |
1237 | /* Since we think we have two common symbols, issue a multiple | |
1238 | common warning if desired. Note that we only warn if the | |
1239 | size is different. If the size is the same, we simply let | |
1240 | the old symbol override the new one as normally happens with | |
1241 | symbols defined in dynamic objects. */ | |
1242 | ||
1243 | if (! ((*info->callbacks->multiple_common) | |
1244 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1245 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1246 | return FALSE; | |
252b5132 | 1247 | |
45d6a902 AM |
1248 | if (sym->st_size > h->size) |
1249 | h->size = sym->st_size; | |
252b5132 | 1250 | |
45d6a902 | 1251 | *size_change_ok = TRUE; |
252b5132 RH |
1252 | } |
1253 | ||
45d6a902 AM |
1254 | /* If we are looking at a dynamic object, and we have found a |
1255 | definition, we need to see if the symbol was already defined by | |
1256 | some other object. If so, we want to use the existing | |
1257 | definition, and we do not want to report a multiple symbol | |
1258 | definition error; we do this by clobbering *PSEC to be | |
1259 | bfd_und_section_ptr. | |
1260 | ||
1261 | We treat a common symbol as a definition if the symbol in the | |
1262 | shared library is a function, since common symbols always | |
1263 | represent variables; this can cause confusion in principle, but | |
1264 | any such confusion would seem to indicate an erroneous program or | |
1265 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1266 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1267 | |
1268 | if (newdyn | |
1269 | && newdef | |
77cfaee6 | 1270 | && (olddef |
45d6a902 | 1271 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1272 | && (newweak |
0f8a2703 | 1273 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1274 | { |
1275 | *override = TRUE; | |
1276 | newdef = FALSE; | |
1277 | newdyncommon = FALSE; | |
252b5132 | 1278 | |
45d6a902 AM |
1279 | *psec = sec = bfd_und_section_ptr; |
1280 | *size_change_ok = TRUE; | |
252b5132 | 1281 | |
45d6a902 AM |
1282 | /* If we get here when the old symbol is a common symbol, then |
1283 | we are explicitly letting it override a weak symbol or | |
1284 | function in a dynamic object, and we don't want to warn about | |
1285 | a type change. If the old symbol is a defined symbol, a type | |
1286 | change warning may still be appropriate. */ | |
252b5132 | 1287 | |
45d6a902 AM |
1288 | if (h->root.type == bfd_link_hash_common) |
1289 | *type_change_ok = TRUE; | |
1290 | } | |
1291 | ||
1292 | /* Handle the special case of an old common symbol merging with a | |
1293 | new symbol which looks like a common symbol in a shared object. | |
1294 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1295 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1296 | right thing. */ | |
45d6a902 AM |
1297 | |
1298 | if (newdyncommon | |
1299 | && h->root.type == bfd_link_hash_common) | |
1300 | { | |
1301 | *override = TRUE; | |
1302 | newdef = FALSE; | |
1303 | newdyncommon = FALSE; | |
1304 | *pvalue = sym->st_size; | |
a4d8e49b | 1305 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1306 | *size_change_ok = TRUE; |
1307 | } | |
1308 | ||
c5e2cead | 1309 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1310 | if (newdef && olddef && newweak) |
c5e2cead L |
1311 | *skip = TRUE; |
1312 | ||
45d6a902 AM |
1313 | /* If the old symbol is from a dynamic object, and the new symbol is |
1314 | a definition which is not from a dynamic object, then the new | |
1315 | symbol overrides the old symbol. Symbols from regular files | |
1316 | always take precedence over symbols from dynamic objects, even if | |
1317 | they are defined after the dynamic object in the link. | |
1318 | ||
1319 | As above, we again permit a common symbol in a regular object to | |
1320 | override a definition in a shared object if the shared object | |
0f8a2703 | 1321 | symbol is a function or is weak. */ |
45d6a902 AM |
1322 | |
1323 | flip = NULL; | |
77cfaee6 | 1324 | if (!newdyn |
45d6a902 AM |
1325 | && (newdef |
1326 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1327 | && (oldweak |
1328 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1329 | && olddyn |
1330 | && olddef | |
f5385ebf | 1331 | && h->def_dynamic) |
45d6a902 AM |
1332 | { |
1333 | /* Change the hash table entry to undefined, and let | |
1334 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1335 | new definition. */ | |
1336 | ||
1337 | h->root.type = bfd_link_hash_undefined; | |
1338 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1339 | *size_change_ok = TRUE; | |
1340 | ||
1341 | olddef = FALSE; | |
1342 | olddyncommon = FALSE; | |
1343 | ||
1344 | /* We again permit a type change when a common symbol may be | |
1345 | overriding a function. */ | |
1346 | ||
1347 | if (bfd_is_com_section (sec)) | |
1348 | *type_change_ok = TRUE; | |
1349 | ||
1350 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1351 | flip = *sym_hash; | |
1352 | else | |
1353 | /* This union may have been set to be non-NULL when this symbol | |
1354 | was seen in a dynamic object. We must force the union to be | |
1355 | NULL, so that it is correct for a regular symbol. */ | |
1356 | h->verinfo.vertree = NULL; | |
1357 | } | |
1358 | ||
1359 | /* Handle the special case of a new common symbol merging with an | |
1360 | old symbol that looks like it might be a common symbol defined in | |
1361 | a shared object. Note that we have already handled the case in | |
1362 | which a new common symbol should simply override the definition | |
1363 | in the shared library. */ | |
1364 | ||
1365 | if (! newdyn | |
1366 | && bfd_is_com_section (sec) | |
1367 | && olddyncommon) | |
1368 | { | |
1369 | /* It would be best if we could set the hash table entry to a | |
1370 | common symbol, but we don't know what to use for the section | |
1371 | or the alignment. */ | |
1372 | if (! ((*info->callbacks->multiple_common) | |
1373 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1374 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1375 | return FALSE; | |
1376 | ||
4cc11e76 | 1377 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1378 | larger, pretend that the new symbol has its size. */ |
1379 | ||
1380 | if (h->size > *pvalue) | |
1381 | *pvalue = h->size; | |
1382 | ||
af44c138 L |
1383 | /* We need to remember the alignment required by the symbol |
1384 | in the dynamic object. */ | |
1385 | BFD_ASSERT (pold_alignment); | |
1386 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1387 | |
1388 | olddef = FALSE; | |
1389 | olddyncommon = FALSE; | |
1390 | ||
1391 | h->root.type = bfd_link_hash_undefined; | |
1392 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1393 | ||
1394 | *size_change_ok = TRUE; | |
1395 | *type_change_ok = TRUE; | |
1396 | ||
1397 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1398 | flip = *sym_hash; | |
1399 | else | |
1400 | h->verinfo.vertree = NULL; | |
1401 | } | |
1402 | ||
1403 | if (flip != NULL) | |
1404 | { | |
1405 | /* Handle the case where we had a versioned symbol in a dynamic | |
1406 | library and now find a definition in a normal object. In this | |
1407 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1408 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1409 | flip->root.type = h->root.type; |
1410 | h->root.type = bfd_link_hash_indirect; | |
1411 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1412 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
45d6a902 | 1413 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
f5385ebf | 1414 | if (h->def_dynamic) |
45d6a902 | 1415 | { |
f5385ebf AM |
1416 | h->def_dynamic = 0; |
1417 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1418 | } |
1419 | } | |
1420 | ||
45d6a902 AM |
1421 | return TRUE; |
1422 | } | |
1423 | ||
1424 | /* This function is called to create an indirect symbol from the | |
1425 | default for the symbol with the default version if needed. The | |
1426 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1427 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1428 | |
1429 | bfd_boolean | |
268b6b39 AM |
1430 | _bfd_elf_add_default_symbol (bfd *abfd, |
1431 | struct bfd_link_info *info, | |
1432 | struct elf_link_hash_entry *h, | |
1433 | const char *name, | |
1434 | Elf_Internal_Sym *sym, | |
1435 | asection **psec, | |
1436 | bfd_vma *value, | |
1437 | bfd_boolean *dynsym, | |
0f8a2703 | 1438 | bfd_boolean override) |
45d6a902 AM |
1439 | { |
1440 | bfd_boolean type_change_ok; | |
1441 | bfd_boolean size_change_ok; | |
1442 | bfd_boolean skip; | |
1443 | char *shortname; | |
1444 | struct elf_link_hash_entry *hi; | |
1445 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1446 | const struct elf_backend_data *bed; |
45d6a902 AM |
1447 | bfd_boolean collect; |
1448 | bfd_boolean dynamic; | |
1449 | char *p; | |
1450 | size_t len, shortlen; | |
1451 | asection *sec; | |
1452 | ||
1453 | /* If this symbol has a version, and it is the default version, we | |
1454 | create an indirect symbol from the default name to the fully | |
1455 | decorated name. This will cause external references which do not | |
1456 | specify a version to be bound to this version of the symbol. */ | |
1457 | p = strchr (name, ELF_VER_CHR); | |
1458 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1459 | return TRUE; | |
1460 | ||
1461 | if (override) | |
1462 | { | |
4cc11e76 | 1463 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1464 | need to create the indirect symbol from the default name. */ |
1465 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1466 | FALSE, FALSE); | |
1467 | BFD_ASSERT (hi != NULL); | |
1468 | if (hi == h) | |
1469 | return TRUE; | |
1470 | while (hi->root.type == bfd_link_hash_indirect | |
1471 | || hi->root.type == bfd_link_hash_warning) | |
1472 | { | |
1473 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1474 | if (hi == h) | |
1475 | return TRUE; | |
1476 | } | |
1477 | } | |
1478 | ||
1479 | bed = get_elf_backend_data (abfd); | |
1480 | collect = bed->collect; | |
1481 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1482 | ||
1483 | shortlen = p - name; | |
1484 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1485 | if (shortname == NULL) | |
1486 | return FALSE; | |
1487 | memcpy (shortname, name, shortlen); | |
1488 | shortname[shortlen] = '\0'; | |
1489 | ||
1490 | /* We are going to create a new symbol. Merge it with any existing | |
1491 | symbol with this name. For the purposes of the merge, act as | |
1492 | though we were defining the symbol we just defined, although we | |
1493 | actually going to define an indirect symbol. */ | |
1494 | type_change_ok = FALSE; | |
1495 | size_change_ok = FALSE; | |
1496 | sec = *psec; | |
1497 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1498 | NULL, &hi, &skip, &override, |
1499 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1500 | return FALSE; |
1501 | ||
1502 | if (skip) | |
1503 | goto nondefault; | |
1504 | ||
1505 | if (! override) | |
1506 | { | |
1507 | bh = &hi->root; | |
1508 | if (! (_bfd_generic_link_add_one_symbol | |
1509 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1510 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1511 | return FALSE; |
1512 | hi = (struct elf_link_hash_entry *) bh; | |
1513 | } | |
1514 | else | |
1515 | { | |
1516 | /* In this case the symbol named SHORTNAME is overriding the | |
1517 | indirect symbol we want to add. We were planning on making | |
1518 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1519 | is the name without a version. NAME is the fully versioned | |
1520 | name, and it is the default version. | |
1521 | ||
1522 | Overriding means that we already saw a definition for the | |
1523 | symbol SHORTNAME in a regular object, and it is overriding | |
1524 | the symbol defined in the dynamic object. | |
1525 | ||
1526 | When this happens, we actually want to change NAME, the | |
1527 | symbol we just added, to refer to SHORTNAME. This will cause | |
1528 | references to NAME in the shared object to become references | |
1529 | to SHORTNAME in the regular object. This is what we expect | |
1530 | when we override a function in a shared object: that the | |
1531 | references in the shared object will be mapped to the | |
1532 | definition in the regular object. */ | |
1533 | ||
1534 | while (hi->root.type == bfd_link_hash_indirect | |
1535 | || hi->root.type == bfd_link_hash_warning) | |
1536 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1537 | ||
1538 | h->root.type = bfd_link_hash_indirect; | |
1539 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1540 | if (h->def_dynamic) |
45d6a902 | 1541 | { |
f5385ebf AM |
1542 | h->def_dynamic = 0; |
1543 | hi->ref_dynamic = 1; | |
1544 | if (hi->ref_regular | |
1545 | || hi->def_regular) | |
45d6a902 | 1546 | { |
c152c796 | 1547 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1548 | return FALSE; |
1549 | } | |
1550 | } | |
1551 | ||
1552 | /* Now set HI to H, so that the following code will set the | |
1553 | other fields correctly. */ | |
1554 | hi = h; | |
1555 | } | |
1556 | ||
1557 | /* If there is a duplicate definition somewhere, then HI may not | |
1558 | point to an indirect symbol. We will have reported an error to | |
1559 | the user in that case. */ | |
1560 | ||
1561 | if (hi->root.type == bfd_link_hash_indirect) | |
1562 | { | |
1563 | struct elf_link_hash_entry *ht; | |
1564 | ||
45d6a902 | 1565 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1566 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 AM |
1567 | |
1568 | /* See if the new flags lead us to realize that the symbol must | |
1569 | be dynamic. */ | |
1570 | if (! *dynsym) | |
1571 | { | |
1572 | if (! dynamic) | |
1573 | { | |
1574 | if (info->shared | |
f5385ebf | 1575 | || hi->ref_dynamic) |
45d6a902 AM |
1576 | *dynsym = TRUE; |
1577 | } | |
1578 | else | |
1579 | { | |
f5385ebf | 1580 | if (hi->ref_regular) |
45d6a902 AM |
1581 | *dynsym = TRUE; |
1582 | } | |
1583 | } | |
1584 | } | |
1585 | ||
1586 | /* We also need to define an indirection from the nondefault version | |
1587 | of the symbol. */ | |
1588 | ||
1589 | nondefault: | |
1590 | len = strlen (name); | |
1591 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1592 | if (shortname == NULL) | |
1593 | return FALSE; | |
1594 | memcpy (shortname, name, shortlen); | |
1595 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1596 | ||
1597 | /* Once again, merge with any existing symbol. */ | |
1598 | type_change_ok = FALSE; | |
1599 | size_change_ok = FALSE; | |
1600 | sec = *psec; | |
1601 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1602 | NULL, &hi, &skip, &override, |
1603 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1604 | return FALSE; |
1605 | ||
1606 | if (skip) | |
1607 | return TRUE; | |
1608 | ||
1609 | if (override) | |
1610 | { | |
1611 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1612 | the type of override we do in the case above unless it is | |
4cc11e76 | 1613 | overridden by a versioned definition. */ |
45d6a902 AM |
1614 | if (hi->root.type != bfd_link_hash_defined |
1615 | && hi->root.type != bfd_link_hash_defweak) | |
1616 | (*_bfd_error_handler) | |
d003868e AM |
1617 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1618 | abfd, shortname); | |
45d6a902 AM |
1619 | } |
1620 | else | |
1621 | { | |
1622 | bh = &hi->root; | |
1623 | if (! (_bfd_generic_link_add_one_symbol | |
1624 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1625 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1626 | return FALSE; |
1627 | hi = (struct elf_link_hash_entry *) bh; | |
1628 | ||
1629 | /* If there is a duplicate definition somewhere, then HI may not | |
1630 | point to an indirect symbol. We will have reported an error | |
1631 | to the user in that case. */ | |
1632 | ||
1633 | if (hi->root.type == bfd_link_hash_indirect) | |
1634 | { | |
fcfa13d2 | 1635 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
45d6a902 AM |
1636 | |
1637 | /* See if the new flags lead us to realize that the symbol | |
1638 | must be dynamic. */ | |
1639 | if (! *dynsym) | |
1640 | { | |
1641 | if (! dynamic) | |
1642 | { | |
1643 | if (info->shared | |
f5385ebf | 1644 | || hi->ref_dynamic) |
45d6a902 AM |
1645 | *dynsym = TRUE; |
1646 | } | |
1647 | else | |
1648 | { | |
f5385ebf | 1649 | if (hi->ref_regular) |
45d6a902 AM |
1650 | *dynsym = TRUE; |
1651 | } | |
1652 | } | |
1653 | } | |
1654 | } | |
1655 | ||
1656 | return TRUE; | |
1657 | } | |
1658 | \f | |
1659 | /* This routine is used to export all defined symbols into the dynamic | |
1660 | symbol table. It is called via elf_link_hash_traverse. */ | |
1661 | ||
1662 | bfd_boolean | |
268b6b39 | 1663 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1664 | { |
268b6b39 | 1665 | struct elf_info_failed *eif = data; |
45d6a902 | 1666 | |
55255dae L |
1667 | /* Ignore this if we won't export it. */ |
1668 | if (!eif->info->export_dynamic && !h->dynamic) | |
1669 | return TRUE; | |
1670 | ||
45d6a902 AM |
1671 | /* Ignore indirect symbols. These are added by the versioning code. */ |
1672 | if (h->root.type == bfd_link_hash_indirect) | |
1673 | return TRUE; | |
1674 | ||
1675 | if (h->root.type == bfd_link_hash_warning) | |
1676 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1677 | ||
1678 | if (h->dynindx == -1 | |
f5385ebf AM |
1679 | && (h->def_regular |
1680 | || h->ref_regular)) | |
45d6a902 AM |
1681 | { |
1682 | struct bfd_elf_version_tree *t; | |
1683 | struct bfd_elf_version_expr *d; | |
1684 | ||
1685 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1686 | { | |
108ba305 | 1687 | if (t->globals.list != NULL) |
45d6a902 | 1688 | { |
108ba305 JJ |
1689 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1690 | if (d != NULL) | |
1691 | goto doit; | |
45d6a902 AM |
1692 | } |
1693 | ||
108ba305 | 1694 | if (t->locals.list != NULL) |
45d6a902 | 1695 | { |
108ba305 JJ |
1696 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1697 | if (d != NULL) | |
1698 | return TRUE; | |
45d6a902 AM |
1699 | } |
1700 | } | |
1701 | ||
1702 | if (!eif->verdefs) | |
1703 | { | |
1704 | doit: | |
c152c796 | 1705 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1706 | { |
1707 | eif->failed = TRUE; | |
1708 | return FALSE; | |
1709 | } | |
1710 | } | |
1711 | } | |
1712 | ||
1713 | return TRUE; | |
1714 | } | |
1715 | \f | |
1716 | /* Look through the symbols which are defined in other shared | |
1717 | libraries and referenced here. Update the list of version | |
1718 | dependencies. This will be put into the .gnu.version_r section. | |
1719 | This function is called via elf_link_hash_traverse. */ | |
1720 | ||
1721 | bfd_boolean | |
268b6b39 AM |
1722 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1723 | void *data) | |
45d6a902 | 1724 | { |
268b6b39 | 1725 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1726 | Elf_Internal_Verneed *t; |
1727 | Elf_Internal_Vernaux *a; | |
1728 | bfd_size_type amt; | |
1729 | ||
1730 | if (h->root.type == bfd_link_hash_warning) | |
1731 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1732 | ||
1733 | /* We only care about symbols defined in shared objects with version | |
1734 | information. */ | |
f5385ebf AM |
1735 | if (!h->def_dynamic |
1736 | || h->def_regular | |
45d6a902 AM |
1737 | || h->dynindx == -1 |
1738 | || h->verinfo.verdef == NULL) | |
1739 | return TRUE; | |
1740 | ||
1741 | /* See if we already know about this version. */ | |
1742 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1743 | { | |
1744 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1745 | continue; | |
1746 | ||
1747 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1748 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1749 | return TRUE; | |
1750 | ||
1751 | break; | |
1752 | } | |
1753 | ||
1754 | /* This is a new version. Add it to tree we are building. */ | |
1755 | ||
1756 | if (t == NULL) | |
1757 | { | |
1758 | amt = sizeof *t; | |
268b6b39 | 1759 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1760 | if (t == NULL) |
1761 | { | |
1762 | rinfo->failed = TRUE; | |
1763 | return FALSE; | |
1764 | } | |
1765 | ||
1766 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1767 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1768 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1769 | } | |
1770 | ||
1771 | amt = sizeof *a; | |
268b6b39 | 1772 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1773 | |
1774 | /* Note that we are copying a string pointer here, and testing it | |
1775 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1776 | discard the string data when low in memory, this will have to be | |
1777 | fixed. */ | |
1778 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1779 | ||
1780 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1781 | a->vna_nextptr = t->vn_auxptr; | |
1782 | ||
1783 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1784 | ++rinfo->vers; | |
1785 | ||
1786 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1787 | ||
1788 | t->vn_auxptr = a; | |
1789 | ||
1790 | return TRUE; | |
1791 | } | |
1792 | ||
1793 | /* Figure out appropriate versions for all the symbols. We may not | |
1794 | have the version number script until we have read all of the input | |
1795 | files, so until that point we don't know which symbols should be | |
1796 | local. This function is called via elf_link_hash_traverse. */ | |
1797 | ||
1798 | bfd_boolean | |
268b6b39 | 1799 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1800 | { |
1801 | struct elf_assign_sym_version_info *sinfo; | |
1802 | struct bfd_link_info *info; | |
9c5bfbb7 | 1803 | const struct elf_backend_data *bed; |
45d6a902 AM |
1804 | struct elf_info_failed eif; |
1805 | char *p; | |
1806 | bfd_size_type amt; | |
1807 | ||
268b6b39 | 1808 | sinfo = data; |
45d6a902 AM |
1809 | info = sinfo->info; |
1810 | ||
1811 | if (h->root.type == bfd_link_hash_warning) | |
1812 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1813 | ||
1814 | /* Fix the symbol flags. */ | |
1815 | eif.failed = FALSE; | |
1816 | eif.info = info; | |
1817 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1818 | { | |
1819 | if (eif.failed) | |
1820 | sinfo->failed = TRUE; | |
1821 | return FALSE; | |
1822 | } | |
1823 | ||
1824 | /* We only need version numbers for symbols defined in regular | |
1825 | objects. */ | |
f5385ebf | 1826 | if (!h->def_regular) |
45d6a902 AM |
1827 | return TRUE; |
1828 | ||
1829 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1830 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1831 | if (p != NULL && h->verinfo.vertree == NULL) | |
1832 | { | |
1833 | struct bfd_elf_version_tree *t; | |
1834 | bfd_boolean hidden; | |
1835 | ||
1836 | hidden = TRUE; | |
1837 | ||
1838 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1839 | not a hidden symbol. */ | |
1840 | ++p; | |
1841 | if (*p == ELF_VER_CHR) | |
1842 | { | |
1843 | hidden = FALSE; | |
1844 | ++p; | |
1845 | } | |
1846 | ||
1847 | /* If there is no version string, we can just return out. */ | |
1848 | if (*p == '\0') | |
1849 | { | |
1850 | if (hidden) | |
f5385ebf | 1851 | h->hidden = 1; |
45d6a902 AM |
1852 | return TRUE; |
1853 | } | |
1854 | ||
1855 | /* Look for the version. If we find it, it is no longer weak. */ | |
1856 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1857 | { | |
1858 | if (strcmp (t->name, p) == 0) | |
1859 | { | |
1860 | size_t len; | |
1861 | char *alc; | |
1862 | struct bfd_elf_version_expr *d; | |
1863 | ||
1864 | len = p - h->root.root.string; | |
268b6b39 | 1865 | alc = bfd_malloc (len); |
45d6a902 AM |
1866 | if (alc == NULL) |
1867 | return FALSE; | |
1868 | memcpy (alc, h->root.root.string, len - 1); | |
1869 | alc[len - 1] = '\0'; | |
1870 | if (alc[len - 2] == ELF_VER_CHR) | |
1871 | alc[len - 2] = '\0'; | |
1872 | ||
1873 | h->verinfo.vertree = t; | |
1874 | t->used = TRUE; | |
1875 | d = NULL; | |
1876 | ||
108ba305 JJ |
1877 | if (t->globals.list != NULL) |
1878 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1879 | |
1880 | /* See if there is anything to force this symbol to | |
1881 | local scope. */ | |
108ba305 | 1882 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1883 | { |
108ba305 JJ |
1884 | d = (*t->match) (&t->locals, NULL, alc); |
1885 | if (d != NULL | |
1886 | && h->dynindx != -1 | |
108ba305 JJ |
1887 | && ! info->export_dynamic) |
1888 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1889 | } |
1890 | ||
1891 | free (alc); | |
1892 | break; | |
1893 | } | |
1894 | } | |
1895 | ||
1896 | /* If we are building an application, we need to create a | |
1897 | version node for this version. */ | |
36af4a4e | 1898 | if (t == NULL && info->executable) |
45d6a902 AM |
1899 | { |
1900 | struct bfd_elf_version_tree **pp; | |
1901 | int version_index; | |
1902 | ||
1903 | /* If we aren't going to export this symbol, we don't need | |
1904 | to worry about it. */ | |
1905 | if (h->dynindx == -1) | |
1906 | return TRUE; | |
1907 | ||
1908 | amt = sizeof *t; | |
108ba305 | 1909 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1910 | if (t == NULL) |
1911 | { | |
1912 | sinfo->failed = TRUE; | |
1913 | return FALSE; | |
1914 | } | |
1915 | ||
45d6a902 | 1916 | t->name = p; |
45d6a902 AM |
1917 | t->name_indx = (unsigned int) -1; |
1918 | t->used = TRUE; | |
1919 | ||
1920 | version_index = 1; | |
1921 | /* Don't count anonymous version tag. */ | |
1922 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1923 | version_index = 0; | |
1924 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1925 | ++version_index; | |
1926 | t->vernum = version_index; | |
1927 | ||
1928 | *pp = t; | |
1929 | ||
1930 | h->verinfo.vertree = t; | |
1931 | } | |
1932 | else if (t == NULL) | |
1933 | { | |
1934 | /* We could not find the version for a symbol when | |
1935 | generating a shared archive. Return an error. */ | |
1936 | (*_bfd_error_handler) | |
d003868e AM |
1937 | (_("%B: undefined versioned symbol name %s"), |
1938 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1939 | bfd_set_error (bfd_error_bad_value); |
1940 | sinfo->failed = TRUE; | |
1941 | return FALSE; | |
1942 | } | |
1943 | ||
1944 | if (hidden) | |
f5385ebf | 1945 | h->hidden = 1; |
45d6a902 AM |
1946 | } |
1947 | ||
1948 | /* If we don't have a version for this symbol, see if we can find | |
1949 | something. */ | |
1950 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1951 | { | |
1952 | struct bfd_elf_version_tree *t; | |
1953 | struct bfd_elf_version_tree *local_ver; | |
1954 | struct bfd_elf_version_expr *d; | |
1955 | ||
1956 | /* See if can find what version this symbol is in. If the | |
1957 | symbol is supposed to be local, then don't actually register | |
1958 | it. */ | |
1959 | local_ver = NULL; | |
1960 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1961 | { | |
108ba305 | 1962 | if (t->globals.list != NULL) |
45d6a902 AM |
1963 | { |
1964 | bfd_boolean matched; | |
1965 | ||
1966 | matched = FALSE; | |
108ba305 JJ |
1967 | d = NULL; |
1968 | while ((d = (*t->match) (&t->globals, d, | |
1969 | h->root.root.string)) != NULL) | |
1970 | if (d->symver) | |
1971 | matched = TRUE; | |
1972 | else | |
1973 | { | |
1974 | /* There is a version without definition. Make | |
1975 | the symbol the default definition for this | |
1976 | version. */ | |
1977 | h->verinfo.vertree = t; | |
1978 | local_ver = NULL; | |
1979 | d->script = 1; | |
1980 | break; | |
1981 | } | |
45d6a902 AM |
1982 | if (d != NULL) |
1983 | break; | |
1984 | else if (matched) | |
1985 | /* There is no undefined version for this symbol. Hide the | |
1986 | default one. */ | |
1987 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1988 | } | |
1989 | ||
108ba305 | 1990 | if (t->locals.list != NULL) |
45d6a902 | 1991 | { |
108ba305 JJ |
1992 | d = NULL; |
1993 | while ((d = (*t->match) (&t->locals, d, | |
1994 | h->root.root.string)) != NULL) | |
45d6a902 | 1995 | { |
108ba305 | 1996 | local_ver = t; |
45d6a902 | 1997 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1998 | explicit, perhaps even global, match. |
1999 | XXX: Shouldn't this be !d->wildcard instead? */ | |
2000 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
2001 | break; | |
45d6a902 AM |
2002 | } |
2003 | ||
2004 | if (d != NULL) | |
2005 | break; | |
2006 | } | |
2007 | } | |
2008 | ||
2009 | if (local_ver != NULL) | |
2010 | { | |
2011 | h->verinfo.vertree = local_ver; | |
2012 | if (h->dynindx != -1 | |
45d6a902 AM |
2013 | && ! info->export_dynamic) |
2014 | { | |
2015 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2016 | } | |
2017 | } | |
2018 | } | |
2019 | ||
2020 | return TRUE; | |
2021 | } | |
2022 | \f | |
45d6a902 AM |
2023 | /* Read and swap the relocs from the section indicated by SHDR. This |
2024 | may be either a REL or a RELA section. The relocations are | |
2025 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2026 | which should have already been allocated to contain enough space. | |
2027 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2028 | relocations should be stored. | |
2029 | ||
2030 | Returns FALSE if something goes wrong. */ | |
2031 | ||
2032 | static bfd_boolean | |
268b6b39 | 2033 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2034 | asection *sec, |
268b6b39 AM |
2035 | Elf_Internal_Shdr *shdr, |
2036 | void *external_relocs, | |
2037 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2038 | { |
9c5bfbb7 | 2039 | const struct elf_backend_data *bed; |
268b6b39 | 2040 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2041 | const bfd_byte *erela; |
2042 | const bfd_byte *erelaend; | |
2043 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2044 | Elf_Internal_Shdr *symtab_hdr; |
2045 | size_t nsyms; | |
45d6a902 | 2046 | |
45d6a902 AM |
2047 | /* Position ourselves at the start of the section. */ |
2048 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2049 | return FALSE; | |
2050 | ||
2051 | /* Read the relocations. */ | |
2052 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2053 | return FALSE; | |
2054 | ||
243ef1e0 L |
2055 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2056 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
2057 | ||
45d6a902 AM |
2058 | bed = get_elf_backend_data (abfd); |
2059 | ||
2060 | /* Convert the external relocations to the internal format. */ | |
2061 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2062 | swap_in = bed->s->swap_reloc_in; | |
2063 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2064 | swap_in = bed->s->swap_reloca_in; | |
2065 | else | |
2066 | { | |
2067 | bfd_set_error (bfd_error_wrong_format); | |
2068 | return FALSE; | |
2069 | } | |
2070 | ||
2071 | erela = external_relocs; | |
51992aec | 2072 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2073 | irela = internal_relocs; |
2074 | while (erela < erelaend) | |
2075 | { | |
243ef1e0 L |
2076 | bfd_vma r_symndx; |
2077 | ||
45d6a902 | 2078 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2079 | r_symndx = ELF32_R_SYM (irela->r_info); |
2080 | if (bed->s->arch_size == 64) | |
2081 | r_symndx >>= 24; | |
2082 | if ((size_t) r_symndx >= nsyms) | |
2083 | { | |
2084 | (*_bfd_error_handler) | |
d003868e AM |
2085 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
2086 | " for offset 0x%lx in section `%A'"), | |
2087 | abfd, sec, | |
2088 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2089 | bfd_set_error (bfd_error_bad_value); |
2090 | return FALSE; | |
2091 | } | |
45d6a902 AM |
2092 | irela += bed->s->int_rels_per_ext_rel; |
2093 | erela += shdr->sh_entsize; | |
2094 | } | |
2095 | ||
2096 | return TRUE; | |
2097 | } | |
2098 | ||
2099 | /* Read and swap the relocs for a section O. They may have been | |
2100 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2101 | not NULL, they are used as buffers to read into. They are known to | |
2102 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2103 | the return value is allocated using either malloc or bfd_alloc, | |
2104 | according to the KEEP_MEMORY argument. If O has two relocation | |
2105 | sections (both REL and RELA relocations), then the REL_HDR | |
2106 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
2107 | REL_HDR2 relocations. */ | |
2108 | ||
2109 | Elf_Internal_Rela * | |
268b6b39 AM |
2110 | _bfd_elf_link_read_relocs (bfd *abfd, |
2111 | asection *o, | |
2112 | void *external_relocs, | |
2113 | Elf_Internal_Rela *internal_relocs, | |
2114 | bfd_boolean keep_memory) | |
45d6a902 AM |
2115 | { |
2116 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 2117 | void *alloc1 = NULL; |
45d6a902 | 2118 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2119 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
2120 | |
2121 | if (elf_section_data (o)->relocs != NULL) | |
2122 | return elf_section_data (o)->relocs; | |
2123 | ||
2124 | if (o->reloc_count == 0) | |
2125 | return NULL; | |
2126 | ||
2127 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
2128 | ||
2129 | if (internal_relocs == NULL) | |
2130 | { | |
2131 | bfd_size_type size; | |
2132 | ||
2133 | size = o->reloc_count; | |
2134 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2135 | if (keep_memory) | |
268b6b39 | 2136 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 2137 | else |
268b6b39 | 2138 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
2139 | if (internal_relocs == NULL) |
2140 | goto error_return; | |
2141 | } | |
2142 | ||
2143 | if (external_relocs == NULL) | |
2144 | { | |
2145 | bfd_size_type size = rel_hdr->sh_size; | |
2146 | ||
2147 | if (elf_section_data (o)->rel_hdr2) | |
2148 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 2149 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2150 | if (alloc1 == NULL) |
2151 | goto error_return; | |
2152 | external_relocs = alloc1; | |
2153 | } | |
2154 | ||
243ef1e0 | 2155 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
2156 | external_relocs, |
2157 | internal_relocs)) | |
2158 | goto error_return; | |
51992aec AM |
2159 | if (elf_section_data (o)->rel_hdr2 |
2160 | && (!elf_link_read_relocs_from_section | |
2161 | (abfd, o, | |
2162 | elf_section_data (o)->rel_hdr2, | |
2163 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2164 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2165 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2166 | goto error_return; |
2167 | ||
2168 | /* Cache the results for next time, if we can. */ | |
2169 | if (keep_memory) | |
2170 | elf_section_data (o)->relocs = internal_relocs; | |
2171 | ||
2172 | if (alloc1 != NULL) | |
2173 | free (alloc1); | |
2174 | ||
2175 | /* Don't free alloc2, since if it was allocated we are passing it | |
2176 | back (under the name of internal_relocs). */ | |
2177 | ||
2178 | return internal_relocs; | |
2179 | ||
2180 | error_return: | |
2181 | if (alloc1 != NULL) | |
2182 | free (alloc1); | |
2183 | if (alloc2 != NULL) | |
2184 | free (alloc2); | |
2185 | return NULL; | |
2186 | } | |
2187 | ||
2188 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2189 | section header for a section containing relocations for O. */ | |
2190 | ||
2191 | bfd_boolean | |
268b6b39 AM |
2192 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2193 | Elf_Internal_Shdr *rel_hdr, | |
2194 | asection *o) | |
45d6a902 AM |
2195 | { |
2196 | bfd_size_type reloc_count; | |
2197 | bfd_size_type num_rel_hashes; | |
2198 | ||
2199 | /* Figure out how many relocations there will be. */ | |
2200 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2201 | reloc_count = elf_section_data (o)->rel_count; | |
2202 | else | |
2203 | reloc_count = elf_section_data (o)->rel_count2; | |
2204 | ||
2205 | num_rel_hashes = o->reloc_count; | |
2206 | if (num_rel_hashes < reloc_count) | |
2207 | num_rel_hashes = reloc_count; | |
2208 | ||
2209 | /* That allows us to calculate the size of the section. */ | |
2210 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2211 | ||
2212 | /* The contents field must last into write_object_contents, so we | |
2213 | allocate it with bfd_alloc rather than malloc. Also since we | |
2214 | cannot be sure that the contents will actually be filled in, | |
2215 | we zero the allocated space. */ | |
268b6b39 | 2216 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2217 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2218 | return FALSE; | |
2219 | ||
2220 | /* We only allocate one set of hash entries, so we only do it the | |
2221 | first time we are called. */ | |
2222 | if (elf_section_data (o)->rel_hashes == NULL | |
2223 | && num_rel_hashes) | |
2224 | { | |
2225 | struct elf_link_hash_entry **p; | |
2226 | ||
268b6b39 | 2227 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2228 | if (p == NULL) |
2229 | return FALSE; | |
2230 | ||
2231 | elf_section_data (o)->rel_hashes = p; | |
2232 | } | |
2233 | ||
2234 | return TRUE; | |
2235 | } | |
2236 | ||
2237 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2238 | originated from the section given by INPUT_REL_HDR) to the | |
2239 | OUTPUT_BFD. */ | |
2240 | ||
2241 | bfd_boolean | |
268b6b39 AM |
2242 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2243 | asection *input_section, | |
2244 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2245 | Elf_Internal_Rela *internal_relocs, |
2246 | struct elf_link_hash_entry **rel_hash | |
2247 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2248 | { |
2249 | Elf_Internal_Rela *irela; | |
2250 | Elf_Internal_Rela *irelaend; | |
2251 | bfd_byte *erel; | |
2252 | Elf_Internal_Shdr *output_rel_hdr; | |
2253 | asection *output_section; | |
2254 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2255 | const struct elf_backend_data *bed; |
268b6b39 | 2256 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2257 | |
2258 | output_section = input_section->output_section; | |
2259 | output_rel_hdr = NULL; | |
2260 | ||
2261 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2262 | == input_rel_hdr->sh_entsize) | |
2263 | { | |
2264 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2265 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2266 | } | |
2267 | else if (elf_section_data (output_section)->rel_hdr2 | |
2268 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2269 | == input_rel_hdr->sh_entsize)) | |
2270 | { | |
2271 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2272 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2273 | } | |
2274 | else | |
2275 | { | |
2276 | (*_bfd_error_handler) | |
d003868e AM |
2277 | (_("%B: relocation size mismatch in %B section %A"), |
2278 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2279 | bfd_set_error (bfd_error_wrong_object_format); |
2280 | return FALSE; | |
2281 | } | |
2282 | ||
2283 | bed = get_elf_backend_data (output_bfd); | |
2284 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2285 | swap_out = bed->s->swap_reloc_out; | |
2286 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2287 | swap_out = bed->s->swap_reloca_out; | |
2288 | else | |
2289 | abort (); | |
2290 | ||
2291 | erel = output_rel_hdr->contents; | |
2292 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2293 | irela = internal_relocs; | |
2294 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2295 | * bed->s->int_rels_per_ext_rel); | |
2296 | while (irela < irelaend) | |
2297 | { | |
2298 | (*swap_out) (output_bfd, irela, erel); | |
2299 | irela += bed->s->int_rels_per_ext_rel; | |
2300 | erel += input_rel_hdr->sh_entsize; | |
2301 | } | |
2302 | ||
2303 | /* Bump the counter, so that we know where to add the next set of | |
2304 | relocations. */ | |
2305 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2306 | ||
2307 | return TRUE; | |
2308 | } | |
2309 | \f | |
508c3946 L |
2310 | /* Make weak undefined symbols in PIE dynamic. */ |
2311 | ||
2312 | bfd_boolean | |
2313 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2314 | struct elf_link_hash_entry *h) | |
2315 | { | |
2316 | if (info->pie | |
2317 | && h->dynindx == -1 | |
2318 | && h->root.type == bfd_link_hash_undefweak) | |
2319 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2320 | ||
2321 | return TRUE; | |
2322 | } | |
2323 | ||
45d6a902 AM |
2324 | /* Fix up the flags for a symbol. This handles various cases which |
2325 | can only be fixed after all the input files are seen. This is | |
2326 | currently called by both adjust_dynamic_symbol and | |
2327 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2328 | the face of future changes. */ | |
2329 | ||
2330 | bfd_boolean | |
268b6b39 AM |
2331 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2332 | struct elf_info_failed *eif) | |
45d6a902 | 2333 | { |
508c3946 L |
2334 | const struct elf_backend_data *bed = NULL; |
2335 | ||
45d6a902 AM |
2336 | /* If this symbol was mentioned in a non-ELF file, try to set |
2337 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2338 | permit a non-ELF file to correctly refer to a symbol defined in | |
2339 | an ELF dynamic object. */ | |
f5385ebf | 2340 | if (h->non_elf) |
45d6a902 AM |
2341 | { |
2342 | while (h->root.type == bfd_link_hash_indirect) | |
2343 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2344 | ||
2345 | if (h->root.type != bfd_link_hash_defined | |
2346 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2347 | { |
2348 | h->ref_regular = 1; | |
2349 | h->ref_regular_nonweak = 1; | |
2350 | } | |
45d6a902 AM |
2351 | else |
2352 | { | |
2353 | if (h->root.u.def.section->owner != NULL | |
2354 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2355 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2356 | { |
2357 | h->ref_regular = 1; | |
2358 | h->ref_regular_nonweak = 1; | |
2359 | } | |
45d6a902 | 2360 | else |
f5385ebf | 2361 | h->def_regular = 1; |
45d6a902 AM |
2362 | } |
2363 | ||
2364 | if (h->dynindx == -1 | |
f5385ebf AM |
2365 | && (h->def_dynamic |
2366 | || h->ref_dynamic)) | |
45d6a902 | 2367 | { |
c152c796 | 2368 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2369 | { |
2370 | eif->failed = TRUE; | |
2371 | return FALSE; | |
2372 | } | |
2373 | } | |
2374 | } | |
2375 | else | |
2376 | { | |
f5385ebf | 2377 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2378 | was first seen in a non-ELF file. Fortunately, if the symbol |
2379 | was first seen in an ELF file, we're probably OK unless the | |
2380 | symbol was defined in a non-ELF file. Catch that case here. | |
2381 | FIXME: We're still in trouble if the symbol was first seen in | |
2382 | a dynamic object, and then later in a non-ELF regular object. */ | |
2383 | if ((h->root.type == bfd_link_hash_defined | |
2384 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2385 | && !h->def_regular |
45d6a902 AM |
2386 | && (h->root.u.def.section->owner != NULL |
2387 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2388 | != bfd_target_elf_flavour) | |
2389 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2390 | && !h->def_dynamic))) |
2391 | h->def_regular = 1; | |
45d6a902 AM |
2392 | } |
2393 | ||
508c3946 L |
2394 | /* Backend specific symbol fixup. */ |
2395 | if (elf_hash_table (eif->info)->dynobj) | |
2396 | { | |
2397 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2398 | if (bed->elf_backend_fixup_symbol | |
2399 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2400 | return FALSE; | |
2401 | } | |
2402 | ||
45d6a902 AM |
2403 | /* If this is a final link, and the symbol was defined as a common |
2404 | symbol in a regular object file, and there was no definition in | |
2405 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2406 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2407 | flag will not have been set. */ |
2408 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2409 | && !h->def_regular |
2410 | && h->ref_regular | |
2411 | && !h->def_dynamic | |
45d6a902 | 2412 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2413 | h->def_regular = 1; |
45d6a902 AM |
2414 | |
2415 | /* If -Bsymbolic was used (which means to bind references to global | |
2416 | symbols to the definition within the shared object), and this | |
2417 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2418 | need a PLT entry. Likewise, if the symbol has non-default |
2419 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2420 | will force it local. */ |
f5385ebf | 2421 | if (h->needs_plt |
45d6a902 | 2422 | && eif->info->shared |
0eddce27 | 2423 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2424 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2425 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2426 | && h->def_regular) |
45d6a902 | 2427 | { |
45d6a902 AM |
2428 | bfd_boolean force_local; |
2429 | ||
45d6a902 AM |
2430 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2431 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2432 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2433 | } | |
2434 | ||
2435 | /* If a weak undefined symbol has non-default visibility, we also | |
2436 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2437 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2438 | && h->root.type == bfd_link_hash_undefweak) |
2439 | { | |
9c5bfbb7 | 2440 | const struct elf_backend_data *bed; |
45d6a902 AM |
2441 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2442 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2443 | } | |
2444 | ||
2445 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2446 | the real definition in the dynamic object, copy interesting flags | |
2447 | over to the real definition. */ | |
f6e332e6 | 2448 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2449 | { |
2450 | struct elf_link_hash_entry *weakdef; | |
2451 | ||
f6e332e6 | 2452 | weakdef = h->u.weakdef; |
45d6a902 AM |
2453 | if (h->root.type == bfd_link_hash_indirect) |
2454 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2455 | ||
2456 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2457 | || h->root.type == bfd_link_hash_defweak); | |
2458 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2459 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2460 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2461 | |
2462 | /* If the real definition is defined by a regular object file, | |
2463 | don't do anything special. See the longer description in | |
2464 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2465 | if (weakdef->def_regular) |
f6e332e6 | 2466 | h->u.weakdef = NULL; |
45d6a902 | 2467 | else |
508c3946 L |
2468 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, |
2469 | h); | |
45d6a902 AM |
2470 | } |
2471 | ||
2472 | return TRUE; | |
2473 | } | |
2474 | ||
2475 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2476 | called via elf_link_hash_traverse, and also calls itself | |
2477 | recursively. */ | |
2478 | ||
2479 | bfd_boolean | |
268b6b39 | 2480 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2481 | { |
268b6b39 | 2482 | struct elf_info_failed *eif = data; |
45d6a902 | 2483 | bfd *dynobj; |
9c5bfbb7 | 2484 | const struct elf_backend_data *bed; |
45d6a902 | 2485 | |
0eddce27 | 2486 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2487 | return FALSE; |
2488 | ||
2489 | if (h->root.type == bfd_link_hash_warning) | |
2490 | { | |
a6aa5195 AM |
2491 | h->got = elf_hash_table (eif->info)->init_got_offset; |
2492 | h->plt = elf_hash_table (eif->info)->init_plt_offset; | |
45d6a902 AM |
2493 | |
2494 | /* When warning symbols are created, they **replace** the "real" | |
2495 | entry in the hash table, thus we never get to see the real | |
2496 | symbol in a hash traversal. So look at it now. */ | |
2497 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2498 | } | |
2499 | ||
2500 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2501 | if (h->root.type == bfd_link_hash_indirect) | |
2502 | return TRUE; | |
2503 | ||
2504 | /* Fix the symbol flags. */ | |
2505 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2506 | return FALSE; | |
2507 | ||
2508 | /* If this symbol does not require a PLT entry, and it is not | |
2509 | defined by a dynamic object, or is not referenced by a regular | |
2510 | object, ignore it. We do have to handle a weak defined symbol, | |
2511 | even if no regular object refers to it, if we decided to add it | |
2512 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2513 | about symbols which are defined by one dynamic object and | |
2514 | referenced by another one? */ | |
f5385ebf AM |
2515 | if (!h->needs_plt |
2516 | && (h->def_regular | |
2517 | || !h->def_dynamic | |
2518 | || (!h->ref_regular | |
f6e332e6 | 2519 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2520 | { |
a6aa5195 | 2521 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2522 | return TRUE; |
2523 | } | |
2524 | ||
2525 | /* If we've already adjusted this symbol, don't do it again. This | |
2526 | can happen via a recursive call. */ | |
f5385ebf | 2527 | if (h->dynamic_adjusted) |
45d6a902 AM |
2528 | return TRUE; |
2529 | ||
2530 | /* Don't look at this symbol again. Note that we must set this | |
2531 | after checking the above conditions, because we may look at a | |
2532 | symbol once, decide not to do anything, and then get called | |
2533 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2534 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2535 | |
2536 | /* If this is a weak definition, and we know a real definition, and | |
2537 | the real symbol is not itself defined by a regular object file, | |
2538 | then get a good value for the real definition. We handle the | |
2539 | real symbol first, for the convenience of the backend routine. | |
2540 | ||
2541 | Note that there is a confusing case here. If the real definition | |
2542 | is defined by a regular object file, we don't get the real symbol | |
2543 | from the dynamic object, but we do get the weak symbol. If the | |
2544 | processor backend uses a COPY reloc, then if some routine in the | |
2545 | dynamic object changes the real symbol, we will not see that | |
2546 | change in the corresponding weak symbol. This is the way other | |
2547 | ELF linkers work as well, and seems to be a result of the shared | |
2548 | library model. | |
2549 | ||
2550 | I will clarify this issue. Most SVR4 shared libraries define the | |
2551 | variable _timezone and define timezone as a weak synonym. The | |
2552 | tzset call changes _timezone. If you write | |
2553 | extern int timezone; | |
2554 | int _timezone = 5; | |
2555 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2556 | you might expect that, since timezone is a synonym for _timezone, | |
2557 | the same number will print both times. However, if the processor | |
2558 | backend uses a COPY reloc, then actually timezone will be copied | |
2559 | into your process image, and, since you define _timezone | |
2560 | yourself, _timezone will not. Thus timezone and _timezone will | |
2561 | wind up at different memory locations. The tzset call will set | |
2562 | _timezone, leaving timezone unchanged. */ | |
2563 | ||
f6e332e6 | 2564 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2565 | { |
2566 | /* If we get to this point, we know there is an implicit | |
2567 | reference by a regular object file via the weak symbol H. | |
2568 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2569 | H->U.WEAKDEF before it finds H? */ |
2570 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2571 | |
f6e332e6 | 2572 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2573 | return FALSE; |
2574 | } | |
2575 | ||
2576 | /* If a symbol has no type and no size and does not require a PLT | |
2577 | entry, then we are probably about to do the wrong thing here: we | |
2578 | are probably going to create a COPY reloc for an empty object. | |
2579 | This case can arise when a shared object is built with assembly | |
2580 | code, and the assembly code fails to set the symbol type. */ | |
2581 | if (h->size == 0 | |
2582 | && h->type == STT_NOTYPE | |
f5385ebf | 2583 | && !h->needs_plt) |
45d6a902 AM |
2584 | (*_bfd_error_handler) |
2585 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2586 | h->root.root.string); | |
2587 | ||
2588 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2589 | bed = get_elf_backend_data (dynobj); | |
2590 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2591 | { | |
2592 | eif->failed = TRUE; | |
2593 | return FALSE; | |
2594 | } | |
2595 | ||
2596 | return TRUE; | |
2597 | } | |
2598 | ||
2599 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2600 | to reflect the object merging within the sections. */ | |
2601 | ||
2602 | bfd_boolean | |
268b6b39 | 2603 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2604 | { |
2605 | asection *sec; | |
2606 | ||
2607 | if (h->root.type == bfd_link_hash_warning) | |
2608 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2609 | ||
2610 | if ((h->root.type == bfd_link_hash_defined | |
2611 | || h->root.type == bfd_link_hash_defweak) | |
2612 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2613 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2614 | { | |
268b6b39 | 2615 | bfd *output_bfd = data; |
45d6a902 AM |
2616 | |
2617 | h->root.u.def.value = | |
2618 | _bfd_merged_section_offset (output_bfd, | |
2619 | &h->root.u.def.section, | |
2620 | elf_section_data (sec)->sec_info, | |
753731ee | 2621 | h->root.u.def.value); |
45d6a902 AM |
2622 | } |
2623 | ||
2624 | return TRUE; | |
2625 | } | |
986a241f RH |
2626 | |
2627 | /* Returns false if the symbol referred to by H should be considered | |
2628 | to resolve local to the current module, and true if it should be | |
2629 | considered to bind dynamically. */ | |
2630 | ||
2631 | bfd_boolean | |
268b6b39 AM |
2632 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2633 | struct bfd_link_info *info, | |
2634 | bfd_boolean ignore_protected) | |
986a241f RH |
2635 | { |
2636 | bfd_boolean binding_stays_local_p; | |
2637 | ||
2638 | if (h == NULL) | |
2639 | return FALSE; | |
2640 | ||
2641 | while (h->root.type == bfd_link_hash_indirect | |
2642 | || h->root.type == bfd_link_hash_warning) | |
2643 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2644 | ||
2645 | /* If it was forced local, then clearly it's not dynamic. */ | |
2646 | if (h->dynindx == -1) | |
2647 | return FALSE; | |
f5385ebf | 2648 | if (h->forced_local) |
986a241f RH |
2649 | return FALSE; |
2650 | ||
2651 | /* Identify the cases where name binding rules say that a | |
2652 | visible symbol resolves locally. */ | |
55255dae | 2653 | binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); |
986a241f RH |
2654 | |
2655 | switch (ELF_ST_VISIBILITY (h->other)) | |
2656 | { | |
2657 | case STV_INTERNAL: | |
2658 | case STV_HIDDEN: | |
2659 | return FALSE; | |
2660 | ||
2661 | case STV_PROTECTED: | |
2662 | /* Proper resolution for function pointer equality may require | |
2663 | that these symbols perhaps be resolved dynamically, even though | |
2664 | we should be resolving them to the current module. */ | |
1c16dfa5 | 2665 | if (!ignore_protected || h->type != STT_FUNC) |
986a241f RH |
2666 | binding_stays_local_p = TRUE; |
2667 | break; | |
2668 | ||
2669 | default: | |
986a241f RH |
2670 | break; |
2671 | } | |
2672 | ||
aa37626c | 2673 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2674 | if (!h->def_regular) |
aa37626c L |
2675 | return TRUE; |
2676 | ||
986a241f RH |
2677 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2678 | us that it remains local. */ | |
2679 | return !binding_stays_local_p; | |
2680 | } | |
f6c52c13 AM |
2681 | |
2682 | /* Return true if the symbol referred to by H should be considered | |
2683 | to resolve local to the current module, and false otherwise. Differs | |
2684 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2685 | undefined symbols and weak symbols. */ | |
2686 | ||
2687 | bfd_boolean | |
268b6b39 AM |
2688 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2689 | struct bfd_link_info *info, | |
2690 | bfd_boolean local_protected) | |
f6c52c13 AM |
2691 | { |
2692 | /* If it's a local sym, of course we resolve locally. */ | |
2693 | if (h == NULL) | |
2694 | return TRUE; | |
2695 | ||
7e2294f9 AO |
2696 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2697 | flag set, so test it first, and don't bail out. */ | |
2698 | if (ELF_COMMON_DEF_P (h)) | |
2699 | /* Do nothing. */; | |
f6c52c13 | 2700 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2701 | resolve locally. The sym is either undefined or dynamic. */ |
2702 | else if (!h->def_regular) | |
f6c52c13 AM |
2703 | return FALSE; |
2704 | ||
2705 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2706 | if (h->forced_local) |
f6c52c13 AM |
2707 | return TRUE; |
2708 | ||
2709 | /* As do non-dynamic symbols. */ | |
2710 | if (h->dynindx == -1) | |
2711 | return TRUE; | |
2712 | ||
2713 | /* At this point, we know the symbol is defined and dynamic. In an | |
2714 | executable it must resolve locally, likewise when building symbolic | |
2715 | shared libraries. */ | |
55255dae | 2716 | if (info->executable || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
2717 | return TRUE; |
2718 | ||
2719 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2720 | with default visibility might not resolve locally. */ | |
2721 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2722 | return FALSE; | |
2723 | ||
2724 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2725 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2726 | return TRUE; | |
2727 | ||
1c16dfa5 L |
2728 | /* STV_PROTECTED non-function symbols are local. */ |
2729 | if (h->type != STT_FUNC) | |
2730 | return TRUE; | |
2731 | ||
f6c52c13 AM |
2732 | /* Function pointer equality tests may require that STV_PROTECTED |
2733 | symbols be treated as dynamic symbols, even when we know that the | |
2734 | dynamic linker will resolve them locally. */ | |
2735 | return local_protected; | |
2736 | } | |
e1918d23 AM |
2737 | |
2738 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2739 | aligned. Returns the first TLS output section. */ | |
2740 | ||
2741 | struct bfd_section * | |
2742 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2743 | { | |
2744 | struct bfd_section *sec, *tls; | |
2745 | unsigned int align = 0; | |
2746 | ||
2747 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2748 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2749 | break; | |
2750 | tls = sec; | |
2751 | ||
2752 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2753 | if (sec->alignment_power > align) | |
2754 | align = sec->alignment_power; | |
2755 | ||
2756 | elf_hash_table (info)->tls_sec = tls; | |
2757 | ||
2758 | /* Ensure the alignment of the first section is the largest alignment, | |
2759 | so that the tls segment starts aligned. */ | |
2760 | if (tls != NULL) | |
2761 | tls->alignment_power = align; | |
2762 | ||
2763 | return tls; | |
2764 | } | |
0ad989f9 L |
2765 | |
2766 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2767 | static bfd_boolean | |
2768 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2769 | Elf_Internal_Sym *sym) | |
2770 | { | |
a4d8e49b L |
2771 | const struct elf_backend_data *bed; |
2772 | ||
0ad989f9 L |
2773 | /* Local symbols do not count, but target specific ones might. */ |
2774 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2775 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2776 | return FALSE; | |
2777 | ||
2778 | /* Function symbols do not count. */ | |
2779 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2780 | return FALSE; | |
2781 | ||
2782 | /* If the section is undefined, then so is the symbol. */ | |
2783 | if (sym->st_shndx == SHN_UNDEF) | |
2784 | return FALSE; | |
2785 | ||
2786 | /* If the symbol is defined in the common section, then | |
2787 | it is a common definition and so does not count. */ | |
a4d8e49b L |
2788 | bed = get_elf_backend_data (abfd); |
2789 | if (bed->common_definition (sym)) | |
0ad989f9 L |
2790 | return FALSE; |
2791 | ||
2792 | /* If the symbol is in a target specific section then we | |
2793 | must rely upon the backend to tell us what it is. */ | |
2794 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2795 | /* FIXME - this function is not coded yet: | |
2796 | ||
2797 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2798 | ||
2799 | Instead for now assume that the definition is not global, | |
2800 | Even if this is wrong, at least the linker will behave | |
2801 | in the same way that it used to do. */ | |
2802 | return FALSE; | |
2803 | ||
2804 | return TRUE; | |
2805 | } | |
2806 | ||
2807 | /* Search the symbol table of the archive element of the archive ABFD | |
2808 | whose archive map contains a mention of SYMDEF, and determine if | |
2809 | the symbol is defined in this element. */ | |
2810 | static bfd_boolean | |
2811 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2812 | { | |
2813 | Elf_Internal_Shdr * hdr; | |
2814 | bfd_size_type symcount; | |
2815 | bfd_size_type extsymcount; | |
2816 | bfd_size_type extsymoff; | |
2817 | Elf_Internal_Sym *isymbuf; | |
2818 | Elf_Internal_Sym *isym; | |
2819 | Elf_Internal_Sym *isymend; | |
2820 | bfd_boolean result; | |
2821 | ||
2822 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2823 | if (abfd == NULL) | |
2824 | return FALSE; | |
2825 | ||
2826 | if (! bfd_check_format (abfd, bfd_object)) | |
2827 | return FALSE; | |
2828 | ||
2829 | /* If we have already included the element containing this symbol in the | |
2830 | link then we do not need to include it again. Just claim that any symbol | |
2831 | it contains is not a definition, so that our caller will not decide to | |
2832 | (re)include this element. */ | |
2833 | if (abfd->archive_pass) | |
2834 | return FALSE; | |
2835 | ||
2836 | /* Select the appropriate symbol table. */ | |
2837 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2838 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2839 | else | |
2840 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2841 | ||
2842 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2843 | ||
2844 | /* The sh_info field of the symtab header tells us where the | |
2845 | external symbols start. We don't care about the local symbols. */ | |
2846 | if (elf_bad_symtab (abfd)) | |
2847 | { | |
2848 | extsymcount = symcount; | |
2849 | extsymoff = 0; | |
2850 | } | |
2851 | else | |
2852 | { | |
2853 | extsymcount = symcount - hdr->sh_info; | |
2854 | extsymoff = hdr->sh_info; | |
2855 | } | |
2856 | ||
2857 | if (extsymcount == 0) | |
2858 | return FALSE; | |
2859 | ||
2860 | /* Read in the symbol table. */ | |
2861 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2862 | NULL, NULL, NULL); | |
2863 | if (isymbuf == NULL) | |
2864 | return FALSE; | |
2865 | ||
2866 | /* Scan the symbol table looking for SYMDEF. */ | |
2867 | result = FALSE; | |
2868 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2869 | { | |
2870 | const char *name; | |
2871 | ||
2872 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2873 | isym->st_name); | |
2874 | if (name == NULL) | |
2875 | break; | |
2876 | ||
2877 | if (strcmp (name, symdef->name) == 0) | |
2878 | { | |
2879 | result = is_global_data_symbol_definition (abfd, isym); | |
2880 | break; | |
2881 | } | |
2882 | } | |
2883 | ||
2884 | free (isymbuf); | |
2885 | ||
2886 | return result; | |
2887 | } | |
2888 | \f | |
5a580b3a AM |
2889 | /* Add an entry to the .dynamic table. */ |
2890 | ||
2891 | bfd_boolean | |
2892 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2893 | bfd_vma tag, | |
2894 | bfd_vma val) | |
2895 | { | |
2896 | struct elf_link_hash_table *hash_table; | |
2897 | const struct elf_backend_data *bed; | |
2898 | asection *s; | |
2899 | bfd_size_type newsize; | |
2900 | bfd_byte *newcontents; | |
2901 | Elf_Internal_Dyn dyn; | |
2902 | ||
2903 | hash_table = elf_hash_table (info); | |
2904 | if (! is_elf_hash_table (hash_table)) | |
2905 | return FALSE; | |
2906 | ||
2907 | bed = get_elf_backend_data (hash_table->dynobj); | |
2908 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2909 | BFD_ASSERT (s != NULL); | |
2910 | ||
eea6121a | 2911 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2912 | newcontents = bfd_realloc (s->contents, newsize); |
2913 | if (newcontents == NULL) | |
2914 | return FALSE; | |
2915 | ||
2916 | dyn.d_tag = tag; | |
2917 | dyn.d_un.d_val = val; | |
eea6121a | 2918 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2919 | |
eea6121a | 2920 | s->size = newsize; |
5a580b3a AM |
2921 | s->contents = newcontents; |
2922 | ||
2923 | return TRUE; | |
2924 | } | |
2925 | ||
2926 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2927 | otherwise just check whether one already exists. Returns -1 on error, | |
2928 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2929 | ||
4ad4eba5 | 2930 | static int |
7e9f0867 AM |
2931 | elf_add_dt_needed_tag (bfd *abfd, |
2932 | struct bfd_link_info *info, | |
4ad4eba5 AM |
2933 | const char *soname, |
2934 | bfd_boolean do_it) | |
5a580b3a AM |
2935 | { |
2936 | struct elf_link_hash_table *hash_table; | |
2937 | bfd_size_type oldsize; | |
2938 | bfd_size_type strindex; | |
2939 | ||
7e9f0867 AM |
2940 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
2941 | return -1; | |
2942 | ||
5a580b3a AM |
2943 | hash_table = elf_hash_table (info); |
2944 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2945 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2946 | if (strindex == (bfd_size_type) -1) | |
2947 | return -1; | |
2948 | ||
2949 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2950 | { | |
2951 | asection *sdyn; | |
2952 | const struct elf_backend_data *bed; | |
2953 | bfd_byte *extdyn; | |
2954 | ||
2955 | bed = get_elf_backend_data (hash_table->dynobj); | |
2956 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
2957 | if (sdyn != NULL) |
2958 | for (extdyn = sdyn->contents; | |
2959 | extdyn < sdyn->contents + sdyn->size; | |
2960 | extdyn += bed->s->sizeof_dyn) | |
2961 | { | |
2962 | Elf_Internal_Dyn dyn; | |
5a580b3a | 2963 | |
7e9f0867 AM |
2964 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
2965 | if (dyn.d_tag == DT_NEEDED | |
2966 | && dyn.d_un.d_val == strindex) | |
2967 | { | |
2968 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2969 | return 1; | |
2970 | } | |
2971 | } | |
5a580b3a AM |
2972 | } |
2973 | ||
2974 | if (do_it) | |
2975 | { | |
7e9f0867 AM |
2976 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
2977 | return -1; | |
2978 | ||
5a580b3a AM |
2979 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
2980 | return -1; | |
2981 | } | |
2982 | else | |
2983 | /* We were just checking for existence of the tag. */ | |
2984 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2985 | ||
2986 | return 0; | |
2987 | } | |
2988 | ||
2989 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
2990 | static int |
2991 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2992 | { |
2993 | const struct elf_link_hash_entry *h1; | |
2994 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 2995 | bfd_signed_vma vdiff; |
5a580b3a AM |
2996 | |
2997 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2998 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
2999 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3000 | if (vdiff != 0) | |
3001 | return vdiff > 0 ? 1 : -1; | |
3002 | else | |
3003 | { | |
3004 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3005 | if (sdiff != 0) | |
3006 | return sdiff > 0 ? 1 : -1; | |
3007 | } | |
5a580b3a AM |
3008 | return 0; |
3009 | } | |
4ad4eba5 | 3010 | |
5a580b3a AM |
3011 | /* This function is used to adjust offsets into .dynstr for |
3012 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3013 | ||
3014 | static bfd_boolean | |
3015 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3016 | { | |
3017 | struct elf_strtab_hash *dynstr = data; | |
3018 | ||
3019 | if (h->root.type == bfd_link_hash_warning) | |
3020 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3021 | ||
3022 | if (h->dynindx != -1) | |
3023 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3024 | return TRUE; | |
3025 | } | |
3026 | ||
3027 | /* Assign string offsets in .dynstr, update all structures referencing | |
3028 | them. */ | |
3029 | ||
4ad4eba5 AM |
3030 | static bfd_boolean |
3031 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3032 | { |
3033 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3034 | struct elf_link_local_dynamic_entry *entry; | |
3035 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3036 | bfd *dynobj = hash_table->dynobj; | |
3037 | asection *sdyn; | |
3038 | bfd_size_type size; | |
3039 | const struct elf_backend_data *bed; | |
3040 | bfd_byte *extdyn; | |
3041 | ||
3042 | _bfd_elf_strtab_finalize (dynstr); | |
3043 | size = _bfd_elf_strtab_size (dynstr); | |
3044 | ||
3045 | bed = get_elf_backend_data (dynobj); | |
3046 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3047 | BFD_ASSERT (sdyn != NULL); | |
3048 | ||
3049 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3050 | for (extdyn = sdyn->contents; | |
eea6121a | 3051 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3052 | extdyn += bed->s->sizeof_dyn) |
3053 | { | |
3054 | Elf_Internal_Dyn dyn; | |
3055 | ||
3056 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3057 | switch (dyn.d_tag) | |
3058 | { | |
3059 | case DT_STRSZ: | |
3060 | dyn.d_un.d_val = size; | |
3061 | break; | |
3062 | case DT_NEEDED: | |
3063 | case DT_SONAME: | |
3064 | case DT_RPATH: | |
3065 | case DT_RUNPATH: | |
3066 | case DT_FILTER: | |
3067 | case DT_AUXILIARY: | |
3068 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
3069 | break; | |
3070 | default: | |
3071 | continue; | |
3072 | } | |
3073 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3074 | } | |
3075 | ||
3076 | /* Now update local dynamic symbols. */ | |
3077 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3078 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3079 | entry->isym.st_name); | |
3080 | ||
3081 | /* And the rest of dynamic symbols. */ | |
3082 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3083 | ||
3084 | /* Adjust version definitions. */ | |
3085 | if (elf_tdata (output_bfd)->cverdefs) | |
3086 | { | |
3087 | asection *s; | |
3088 | bfd_byte *p; | |
3089 | bfd_size_type i; | |
3090 | Elf_Internal_Verdef def; | |
3091 | Elf_Internal_Verdaux defaux; | |
3092 | ||
3093 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3094 | p = s->contents; | |
3095 | do | |
3096 | { | |
3097 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3098 | &def); | |
3099 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3100 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3101 | continue; | |
5a580b3a AM |
3102 | for (i = 0; i < def.vd_cnt; ++i) |
3103 | { | |
3104 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3105 | (Elf_External_Verdaux *) p, &defaux); | |
3106 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3107 | defaux.vda_name); | |
3108 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3109 | &defaux, (Elf_External_Verdaux *) p); | |
3110 | p += sizeof (Elf_External_Verdaux); | |
3111 | } | |
3112 | } | |
3113 | while (def.vd_next); | |
3114 | } | |
3115 | ||
3116 | /* Adjust version references. */ | |
3117 | if (elf_tdata (output_bfd)->verref) | |
3118 | { | |
3119 | asection *s; | |
3120 | bfd_byte *p; | |
3121 | bfd_size_type i; | |
3122 | Elf_Internal_Verneed need; | |
3123 | Elf_Internal_Vernaux needaux; | |
3124 | ||
3125 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3126 | p = s->contents; | |
3127 | do | |
3128 | { | |
3129 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3130 | &need); | |
3131 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3132 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3133 | (Elf_External_Verneed *) p); | |
3134 | p += sizeof (Elf_External_Verneed); | |
3135 | for (i = 0; i < need.vn_cnt; ++i) | |
3136 | { | |
3137 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3138 | (Elf_External_Vernaux *) p, &needaux); | |
3139 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3140 | needaux.vna_name); | |
3141 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3142 | &needaux, | |
3143 | (Elf_External_Vernaux *) p); | |
3144 | p += sizeof (Elf_External_Vernaux); | |
3145 | } | |
3146 | } | |
3147 | while (need.vn_next); | |
3148 | } | |
3149 | ||
3150 | return TRUE; | |
3151 | } | |
3152 | \f | |
4ad4eba5 AM |
3153 | /* Add symbols from an ELF object file to the linker hash table. */ |
3154 | ||
3155 | static bfd_boolean | |
3156 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3157 | { | |
4ad4eba5 AM |
3158 | Elf_Internal_Shdr *hdr; |
3159 | bfd_size_type symcount; | |
3160 | bfd_size_type extsymcount; | |
3161 | bfd_size_type extsymoff; | |
3162 | struct elf_link_hash_entry **sym_hash; | |
3163 | bfd_boolean dynamic; | |
3164 | Elf_External_Versym *extversym = NULL; | |
3165 | Elf_External_Versym *ever; | |
3166 | struct elf_link_hash_entry *weaks; | |
3167 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3168 | bfd_size_type nondeflt_vers_cnt = 0; | |
3169 | Elf_Internal_Sym *isymbuf = NULL; | |
3170 | Elf_Internal_Sym *isym; | |
3171 | Elf_Internal_Sym *isymend; | |
3172 | const struct elf_backend_data *bed; | |
3173 | bfd_boolean add_needed; | |
66eb6687 | 3174 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3175 | bfd_size_type amt; |
66eb6687 | 3176 | void *alloc_mark = NULL; |
4f87808c AM |
3177 | struct bfd_hash_entry **old_table = NULL; |
3178 | unsigned int old_size = 0; | |
3179 | unsigned int old_count = 0; | |
66eb6687 AM |
3180 | void *old_tab = NULL; |
3181 | void *old_hash; | |
3182 | void *old_ent; | |
3183 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3184 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
3185 | long old_dynsymcount = 0; | |
3186 | size_t tabsize = 0; | |
3187 | size_t hashsize = 0; | |
4ad4eba5 | 3188 | |
66eb6687 | 3189 | htab = elf_hash_table (info); |
4ad4eba5 | 3190 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3191 | |
3192 | if ((abfd->flags & DYNAMIC) == 0) | |
3193 | dynamic = FALSE; | |
3194 | else | |
3195 | { | |
3196 | dynamic = TRUE; | |
3197 | ||
3198 | /* You can't use -r against a dynamic object. Also, there's no | |
3199 | hope of using a dynamic object which does not exactly match | |
3200 | the format of the output file. */ | |
3201 | if (info->relocatable | |
66eb6687 AM |
3202 | || !is_elf_hash_table (htab) |
3203 | || htab->root.creator != abfd->xvec) | |
4ad4eba5 | 3204 | { |
9a0789ec NC |
3205 | if (info->relocatable) |
3206 | bfd_set_error (bfd_error_invalid_operation); | |
3207 | else | |
3208 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3209 | goto error_return; |
3210 | } | |
3211 | } | |
3212 | ||
3213 | /* As a GNU extension, any input sections which are named | |
3214 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3215 | symbol. This differs from .gnu.warning sections, which generate | |
3216 | warnings when they are included in an output file. */ | |
3217 | if (info->executable) | |
3218 | { | |
3219 | asection *s; | |
3220 | ||
3221 | for (s = abfd->sections; s != NULL; s = s->next) | |
3222 | { | |
3223 | const char *name; | |
3224 | ||
3225 | name = bfd_get_section_name (abfd, s); | |
0112cd26 | 3226 | if (CONST_STRNEQ (name, ".gnu.warning.")) |
4ad4eba5 AM |
3227 | { |
3228 | char *msg; | |
3229 | bfd_size_type sz; | |
4ad4eba5 AM |
3230 | |
3231 | name += sizeof ".gnu.warning." - 1; | |
3232 | ||
3233 | /* If this is a shared object, then look up the symbol | |
3234 | in the hash table. If it is there, and it is already | |
3235 | been defined, then we will not be using the entry | |
3236 | from this shared object, so we don't need to warn. | |
3237 | FIXME: If we see the definition in a regular object | |
3238 | later on, we will warn, but we shouldn't. The only | |
3239 | fix is to keep track of what warnings we are supposed | |
3240 | to emit, and then handle them all at the end of the | |
3241 | link. */ | |
3242 | if (dynamic) | |
3243 | { | |
3244 | struct elf_link_hash_entry *h; | |
3245 | ||
66eb6687 | 3246 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
4ad4eba5 AM |
3247 | |
3248 | /* FIXME: What about bfd_link_hash_common? */ | |
3249 | if (h != NULL | |
3250 | && (h->root.type == bfd_link_hash_defined | |
3251 | || h->root.type == bfd_link_hash_defweak)) | |
3252 | { | |
3253 | /* We don't want to issue this warning. Clobber | |
3254 | the section size so that the warning does not | |
3255 | get copied into the output file. */ | |
eea6121a | 3256 | s->size = 0; |
4ad4eba5 AM |
3257 | continue; |
3258 | } | |
3259 | } | |
3260 | ||
eea6121a | 3261 | sz = s->size; |
370a0e1b | 3262 | msg = bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3263 | if (msg == NULL) |
3264 | goto error_return; | |
3265 | ||
370a0e1b | 3266 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3267 | goto error_return; |
3268 | ||
370a0e1b | 3269 | msg[sz] = '\0'; |
4ad4eba5 AM |
3270 | |
3271 | if (! (_bfd_generic_link_add_one_symbol | |
3272 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
66eb6687 | 3273 | FALSE, bed->collect, NULL))) |
4ad4eba5 AM |
3274 | goto error_return; |
3275 | ||
3276 | if (! info->relocatable) | |
3277 | { | |
3278 | /* Clobber the section size so that the warning does | |
3279 | not get copied into the output file. */ | |
eea6121a | 3280 | s->size = 0; |
11d2f718 AM |
3281 | |
3282 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3283 | the warning section don't get copied to the output. */ | |
3284 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3285 | } |
3286 | } | |
3287 | } | |
3288 | } | |
3289 | ||
3290 | add_needed = TRUE; | |
3291 | if (! dynamic) | |
3292 | { | |
3293 | /* If we are creating a shared library, create all the dynamic | |
3294 | sections immediately. We need to attach them to something, | |
3295 | so we attach them to this BFD, provided it is the right | |
3296 | format. FIXME: If there are no input BFD's of the same | |
3297 | format as the output, we can't make a shared library. */ | |
3298 | if (info->shared | |
66eb6687 AM |
3299 | && is_elf_hash_table (htab) |
3300 | && htab->root.creator == abfd->xvec | |
3301 | && !htab->dynamic_sections_created) | |
4ad4eba5 AM |
3302 | { |
3303 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3304 | goto error_return; | |
3305 | } | |
3306 | } | |
66eb6687 | 3307 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3308 | goto error_return; |
3309 | else | |
3310 | { | |
3311 | asection *s; | |
3312 | const char *soname = NULL; | |
3313 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3314 | int ret; | |
3315 | ||
3316 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3317 | ld shouldn't allow it. */ |
4ad4eba5 AM |
3318 | if ((s = abfd->sections) != NULL |
3319 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
92fd189d | 3320 | abort (); |
4ad4eba5 AM |
3321 | |
3322 | /* If this dynamic lib was specified on the command line with | |
3323 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3324 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3325 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3326 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3327 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3328 | all. */ | |
3329 | add_needed = (elf_dyn_lib_class (abfd) | |
3330 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3331 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3332 | |
3333 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3334 | if (s != NULL) | |
3335 | { | |
3336 | bfd_byte *dynbuf; | |
3337 | bfd_byte *extdyn; | |
3338 | int elfsec; | |
3339 | unsigned long shlink; | |
3340 | ||
eea6121a | 3341 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3342 | goto error_free_dyn; |
3343 | ||
3344 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3345 | if (elfsec == -1) | |
3346 | goto error_free_dyn; | |
3347 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3348 | ||
3349 | for (extdyn = dynbuf; | |
eea6121a | 3350 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3351 | extdyn += bed->s->sizeof_dyn) |
3352 | { | |
3353 | Elf_Internal_Dyn dyn; | |
3354 | ||
3355 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3356 | if (dyn.d_tag == DT_SONAME) | |
3357 | { | |
3358 | unsigned int tagv = dyn.d_un.d_val; | |
3359 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3360 | if (soname == NULL) | |
3361 | goto error_free_dyn; | |
3362 | } | |
3363 | if (dyn.d_tag == DT_NEEDED) | |
3364 | { | |
3365 | struct bfd_link_needed_list *n, **pn; | |
3366 | char *fnm, *anm; | |
3367 | unsigned int tagv = dyn.d_un.d_val; | |
3368 | ||
3369 | amt = sizeof (struct bfd_link_needed_list); | |
3370 | n = bfd_alloc (abfd, amt); | |
3371 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3372 | if (n == NULL || fnm == NULL) | |
3373 | goto error_free_dyn; | |
3374 | amt = strlen (fnm) + 1; | |
3375 | anm = bfd_alloc (abfd, amt); | |
3376 | if (anm == NULL) | |
3377 | goto error_free_dyn; | |
3378 | memcpy (anm, fnm, amt); | |
3379 | n->name = anm; | |
3380 | n->by = abfd; | |
3381 | n->next = NULL; | |
66eb6687 | 3382 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3383 | ; |
3384 | *pn = n; | |
3385 | } | |
3386 | if (dyn.d_tag == DT_RUNPATH) | |
3387 | { | |
3388 | struct bfd_link_needed_list *n, **pn; | |
3389 | char *fnm, *anm; | |
3390 | unsigned int tagv = dyn.d_un.d_val; | |
3391 | ||
3392 | amt = sizeof (struct bfd_link_needed_list); | |
3393 | n = bfd_alloc (abfd, amt); | |
3394 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3395 | if (n == NULL || fnm == NULL) | |
3396 | goto error_free_dyn; | |
3397 | amt = strlen (fnm) + 1; | |
3398 | anm = bfd_alloc (abfd, amt); | |
3399 | if (anm == NULL) | |
3400 | goto error_free_dyn; | |
3401 | memcpy (anm, fnm, amt); | |
3402 | n->name = anm; | |
3403 | n->by = abfd; | |
3404 | n->next = NULL; | |
3405 | for (pn = & runpath; | |
3406 | *pn != NULL; | |
3407 | pn = &(*pn)->next) | |
3408 | ; | |
3409 | *pn = n; | |
3410 | } | |
3411 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3412 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3413 | { | |
3414 | struct bfd_link_needed_list *n, **pn; | |
3415 | char *fnm, *anm; | |
3416 | unsigned int tagv = dyn.d_un.d_val; | |
3417 | ||
3418 | amt = sizeof (struct bfd_link_needed_list); | |
3419 | n = bfd_alloc (abfd, amt); | |
3420 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3421 | if (n == NULL || fnm == NULL) | |
3422 | goto error_free_dyn; | |
3423 | amt = strlen (fnm) + 1; | |
3424 | anm = bfd_alloc (abfd, amt); | |
3425 | if (anm == NULL) | |
3426 | { | |
3427 | error_free_dyn: | |
3428 | free (dynbuf); | |
3429 | goto error_return; | |
3430 | } | |
3431 | memcpy (anm, fnm, amt); | |
3432 | n->name = anm; | |
3433 | n->by = abfd; | |
3434 | n->next = NULL; | |
3435 | for (pn = & rpath; | |
3436 | *pn != NULL; | |
3437 | pn = &(*pn)->next) | |
3438 | ; | |
3439 | *pn = n; | |
3440 | } | |
3441 | } | |
3442 | ||
3443 | free (dynbuf); | |
3444 | } | |
3445 | ||
3446 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3447 | frees all more recently bfd_alloc'd blocks as well. */ | |
3448 | if (runpath) | |
3449 | rpath = runpath; | |
3450 | ||
3451 | if (rpath) | |
3452 | { | |
3453 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3454 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3455 | ; |
3456 | *pn = rpath; | |
3457 | } | |
3458 | ||
3459 | /* We do not want to include any of the sections in a dynamic | |
3460 | object in the output file. We hack by simply clobbering the | |
3461 | list of sections in the BFD. This could be handled more | |
3462 | cleanly by, say, a new section flag; the existing | |
3463 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3464 | still implies that the section takes up space in the output | |
3465 | file. */ | |
3466 | bfd_section_list_clear (abfd); | |
3467 | ||
4ad4eba5 AM |
3468 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3469 | object. If the object has a DT_SONAME entry, we use it. | |
3470 | Otherwise, if the generic linker stuck something in | |
3471 | elf_dt_name, we use that. Otherwise, we just use the file | |
3472 | name. */ | |
3473 | if (soname == NULL || *soname == '\0') | |
3474 | { | |
3475 | soname = elf_dt_name (abfd); | |
3476 | if (soname == NULL || *soname == '\0') | |
3477 | soname = bfd_get_filename (abfd); | |
3478 | } | |
3479 | ||
3480 | /* Save the SONAME because sometimes the linker emulation code | |
3481 | will need to know it. */ | |
3482 | elf_dt_name (abfd) = soname; | |
3483 | ||
7e9f0867 | 3484 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3485 | if (ret < 0) |
3486 | goto error_return; | |
3487 | ||
3488 | /* If we have already included this dynamic object in the | |
3489 | link, just ignore it. There is no reason to include a | |
3490 | particular dynamic object more than once. */ | |
3491 | if (ret > 0) | |
3492 | return TRUE; | |
3493 | } | |
3494 | ||
3495 | /* If this is a dynamic object, we always link against the .dynsym | |
3496 | symbol table, not the .symtab symbol table. The dynamic linker | |
3497 | will only see the .dynsym symbol table, so there is no reason to | |
3498 | look at .symtab for a dynamic object. */ | |
3499 | ||
3500 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3501 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3502 | else | |
3503 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3504 | ||
3505 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3506 | ||
3507 | /* The sh_info field of the symtab header tells us where the | |
3508 | external symbols start. We don't care about the local symbols at | |
3509 | this point. */ | |
3510 | if (elf_bad_symtab (abfd)) | |
3511 | { | |
3512 | extsymcount = symcount; | |
3513 | extsymoff = 0; | |
3514 | } | |
3515 | else | |
3516 | { | |
3517 | extsymcount = symcount - hdr->sh_info; | |
3518 | extsymoff = hdr->sh_info; | |
3519 | } | |
3520 | ||
3521 | sym_hash = NULL; | |
3522 | if (extsymcount != 0) | |
3523 | { | |
3524 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3525 | NULL, NULL, NULL); | |
3526 | if (isymbuf == NULL) | |
3527 | goto error_return; | |
3528 | ||
3529 | /* We store a pointer to the hash table entry for each external | |
3530 | symbol. */ | |
3531 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3532 | sym_hash = bfd_alloc (abfd, amt); | |
3533 | if (sym_hash == NULL) | |
3534 | goto error_free_sym; | |
3535 | elf_sym_hashes (abfd) = sym_hash; | |
3536 | } | |
3537 | ||
3538 | if (dynamic) | |
3539 | { | |
3540 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3541 | if (!_bfd_elf_slurp_version_tables (abfd, |
3542 | info->default_imported_symver)) | |
4ad4eba5 AM |
3543 | goto error_free_sym; |
3544 | ||
3545 | /* Read in the symbol versions, but don't bother to convert them | |
3546 | to internal format. */ | |
3547 | if (elf_dynversym (abfd) != 0) | |
3548 | { | |
3549 | Elf_Internal_Shdr *versymhdr; | |
3550 | ||
3551 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3552 | extversym = bfd_malloc (versymhdr->sh_size); | |
3553 | if (extversym == NULL) | |
3554 | goto error_free_sym; | |
3555 | amt = versymhdr->sh_size; | |
3556 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3557 | || bfd_bread (extversym, amt, abfd) != amt) | |
3558 | goto error_free_vers; | |
3559 | } | |
3560 | } | |
3561 | ||
66eb6687 AM |
3562 | /* If we are loading an as-needed shared lib, save the symbol table |
3563 | state before we start adding symbols. If the lib turns out | |
3564 | to be unneeded, restore the state. */ | |
3565 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
3566 | { | |
3567 | unsigned int i; | |
3568 | size_t entsize; | |
3569 | ||
3570 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
3571 | { | |
3572 | struct bfd_hash_entry *p; | |
2de92251 | 3573 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3574 | |
3575 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
3576 | { |
3577 | h = (struct elf_link_hash_entry *) p; | |
3578 | entsize += htab->root.table.entsize; | |
3579 | if (h->root.type == bfd_link_hash_warning) | |
3580 | entsize += htab->root.table.entsize; | |
3581 | } | |
66eb6687 AM |
3582 | } |
3583 | ||
3584 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
3585 | hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3586 | old_tab = bfd_malloc (tabsize + entsize + hashsize); | |
3587 | if (old_tab == NULL) | |
3588 | goto error_free_vers; | |
3589 | ||
3590 | /* Remember the current objalloc pointer, so that all mem for | |
3591 | symbols added can later be reclaimed. */ | |
3592 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
3593 | if (alloc_mark == NULL) | |
3594 | goto error_free_vers; | |
3595 | ||
5061a885 AM |
3596 | /* Make a special call to the linker "notice" function to |
3597 | tell it that we are about to handle an as-needed lib. */ | |
3598 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
3599 | notice_as_needed)) | |
3600 | return FALSE; | |
3601 | ||
3602 | ||
66eb6687 AM |
3603 | /* Clone the symbol table and sym hashes. Remember some |
3604 | pointers into the symbol table, and dynamic symbol count. */ | |
3605 | old_hash = (char *) old_tab + tabsize; | |
3606 | old_ent = (char *) old_hash + hashsize; | |
3607 | memcpy (old_tab, htab->root.table.table, tabsize); | |
3608 | memcpy (old_hash, sym_hash, hashsize); | |
3609 | old_undefs = htab->root.undefs; | |
3610 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
3611 | old_table = htab->root.table.table; |
3612 | old_size = htab->root.table.size; | |
3613 | old_count = htab->root.table.count; | |
66eb6687 AM |
3614 | old_dynsymcount = htab->dynsymcount; |
3615 | ||
3616 | for (i = 0; i < htab->root.table.size; i++) | |
3617 | { | |
3618 | struct bfd_hash_entry *p; | |
2de92251 | 3619 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3620 | |
3621 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
3622 | { | |
3623 | memcpy (old_ent, p, htab->root.table.entsize); | |
3624 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
3625 | h = (struct elf_link_hash_entry *) p; |
3626 | if (h->root.type == bfd_link_hash_warning) | |
3627 | { | |
3628 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
3629 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
3630 | } | |
66eb6687 AM |
3631 | } |
3632 | } | |
3633 | } | |
4ad4eba5 | 3634 | |
66eb6687 | 3635 | weaks = NULL; |
4ad4eba5 AM |
3636 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
3637 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3638 | isym < isymend; | |
3639 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3640 | { | |
3641 | int bind; | |
3642 | bfd_vma value; | |
af44c138 | 3643 | asection *sec, *new_sec; |
4ad4eba5 AM |
3644 | flagword flags; |
3645 | const char *name; | |
3646 | struct elf_link_hash_entry *h; | |
3647 | bfd_boolean definition; | |
3648 | bfd_boolean size_change_ok; | |
3649 | bfd_boolean type_change_ok; | |
3650 | bfd_boolean new_weakdef; | |
3651 | bfd_boolean override; | |
a4d8e49b | 3652 | bfd_boolean common; |
4ad4eba5 AM |
3653 | unsigned int old_alignment; |
3654 | bfd *old_bfd; | |
3655 | ||
3656 | override = FALSE; | |
3657 | ||
3658 | flags = BSF_NO_FLAGS; | |
3659 | sec = NULL; | |
3660 | value = isym->st_value; | |
3661 | *sym_hash = NULL; | |
a4d8e49b | 3662 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3663 | |
3664 | bind = ELF_ST_BIND (isym->st_info); | |
3665 | if (bind == STB_LOCAL) | |
3666 | { | |
3667 | /* This should be impossible, since ELF requires that all | |
3668 | global symbols follow all local symbols, and that sh_info | |
3669 | point to the first global symbol. Unfortunately, Irix 5 | |
3670 | screws this up. */ | |
3671 | continue; | |
3672 | } | |
3673 | else if (bind == STB_GLOBAL) | |
3674 | { | |
a4d8e49b | 3675 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 AM |
3676 | flags = BSF_GLOBAL; |
3677 | } | |
3678 | else if (bind == STB_WEAK) | |
3679 | flags = BSF_WEAK; | |
3680 | else | |
3681 | { | |
3682 | /* Leave it up to the processor backend. */ | |
3683 | } | |
3684 | ||
3685 | if (isym->st_shndx == SHN_UNDEF) | |
3686 | sec = bfd_und_section_ptr; | |
66eb6687 AM |
3687 | else if (isym->st_shndx < SHN_LORESERVE |
3688 | || isym->st_shndx > SHN_HIRESERVE) | |
4ad4eba5 AM |
3689 | { |
3690 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3691 | if (sec == NULL) | |
3692 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3693 | else if (sec->kept_section) |
3694 | { | |
1f02cbd9 | 3695 | /* Symbols from discarded section are undefined, and have |
3b36f7e6 | 3696 | default visibility. */ |
529fcb95 PB |
3697 | sec = bfd_und_section_ptr; |
3698 | isym->st_shndx = SHN_UNDEF; | |
66eb6687 AM |
3699 | isym->st_other = (STV_DEFAULT |
3700 | | (isym->st_other & ~ ELF_ST_VISIBILITY (-1))); | |
529fcb95 | 3701 | } |
4ad4eba5 AM |
3702 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3703 | value -= sec->vma; | |
3704 | } | |
3705 | else if (isym->st_shndx == SHN_ABS) | |
3706 | sec = bfd_abs_section_ptr; | |
3707 | else if (isym->st_shndx == SHN_COMMON) | |
3708 | { | |
3709 | sec = bfd_com_section_ptr; | |
3710 | /* What ELF calls the size we call the value. What ELF | |
3711 | calls the value we call the alignment. */ | |
3712 | value = isym->st_size; | |
3713 | } | |
3714 | else | |
3715 | { | |
3716 | /* Leave it up to the processor backend. */ | |
3717 | } | |
3718 | ||
3719 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3720 | isym->st_name); | |
3721 | if (name == NULL) | |
3722 | goto error_free_vers; | |
3723 | ||
3724 | if (isym->st_shndx == SHN_COMMON | |
6a4a0940 JJ |
3725 | && ELF_ST_TYPE (isym->st_info) == STT_TLS |
3726 | && !info->relocatable) | |
4ad4eba5 AM |
3727 | { |
3728 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3729 | ||
3730 | if (tcomm == NULL) | |
3731 | { | |
3496cb2a L |
3732 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", |
3733 | (SEC_ALLOC | |
3734 | | SEC_IS_COMMON | |
3735 | | SEC_LINKER_CREATED | |
3736 | | SEC_THREAD_LOCAL)); | |
3737 | if (tcomm == NULL) | |
4ad4eba5 AM |
3738 | goto error_free_vers; |
3739 | } | |
3740 | sec = tcomm; | |
3741 | } | |
66eb6687 | 3742 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 3743 | { |
66eb6687 AM |
3744 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
3745 | &sec, &value)) | |
4ad4eba5 AM |
3746 | goto error_free_vers; |
3747 | ||
3748 | /* The hook function sets the name to NULL if this symbol | |
3749 | should be skipped for some reason. */ | |
3750 | if (name == NULL) | |
3751 | continue; | |
3752 | } | |
3753 | ||
3754 | /* Sanity check that all possibilities were handled. */ | |
3755 | if (sec == NULL) | |
3756 | { | |
3757 | bfd_set_error (bfd_error_bad_value); | |
3758 | goto error_free_vers; | |
3759 | } | |
3760 | ||
3761 | if (bfd_is_und_section (sec) | |
3762 | || bfd_is_com_section (sec)) | |
3763 | definition = FALSE; | |
3764 | else | |
3765 | definition = TRUE; | |
3766 | ||
3767 | size_change_ok = FALSE; | |
66eb6687 | 3768 | type_change_ok = bed->type_change_ok; |
4ad4eba5 AM |
3769 | old_alignment = 0; |
3770 | old_bfd = NULL; | |
af44c138 | 3771 | new_sec = sec; |
4ad4eba5 | 3772 | |
66eb6687 | 3773 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
3774 | { |
3775 | Elf_Internal_Versym iver; | |
3776 | unsigned int vernum = 0; | |
3777 | bfd_boolean skip; | |
3778 | ||
fc0e6df6 | 3779 | if (ever == NULL) |
4ad4eba5 | 3780 | { |
fc0e6df6 PB |
3781 | if (info->default_imported_symver) |
3782 | /* Use the default symbol version created earlier. */ | |
3783 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3784 | else | |
3785 | iver.vs_vers = 0; | |
3786 | } | |
3787 | else | |
3788 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3789 | ||
3790 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3791 | ||
3792 | /* If this is a hidden symbol, or if it is not version | |
3793 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
3794 | However, we do not modify a non-hidden absolute symbol |
3795 | if it is not a function, because it might be the version | |
3796 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 3797 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
cc86ff91 EB |
3798 | || (vernum > 1 && (! bfd_is_abs_section (sec) |
3799 | || ELF_ST_TYPE (isym->st_info) == STT_FUNC))) | |
fc0e6df6 PB |
3800 | { |
3801 | const char *verstr; | |
3802 | size_t namelen, verlen, newlen; | |
3803 | char *newname, *p; | |
3804 | ||
3805 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3806 | { |
fc0e6df6 PB |
3807 | if (vernum > elf_tdata (abfd)->cverdefs) |
3808 | verstr = NULL; | |
3809 | else if (vernum > 1) | |
3810 | verstr = | |
3811 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3812 | else | |
3813 | verstr = ""; | |
4ad4eba5 | 3814 | |
fc0e6df6 | 3815 | if (verstr == NULL) |
4ad4eba5 | 3816 | { |
fc0e6df6 PB |
3817 | (*_bfd_error_handler) |
3818 | (_("%B: %s: invalid version %u (max %d)"), | |
3819 | abfd, name, vernum, | |
3820 | elf_tdata (abfd)->cverdefs); | |
3821 | bfd_set_error (bfd_error_bad_value); | |
3822 | goto error_free_vers; | |
4ad4eba5 | 3823 | } |
fc0e6df6 PB |
3824 | } |
3825 | else | |
3826 | { | |
3827 | /* We cannot simply test for the number of | |
3828 | entries in the VERNEED section since the | |
3829 | numbers for the needed versions do not start | |
3830 | at 0. */ | |
3831 | Elf_Internal_Verneed *t; | |
3832 | ||
3833 | verstr = NULL; | |
3834 | for (t = elf_tdata (abfd)->verref; | |
3835 | t != NULL; | |
3836 | t = t->vn_nextref) | |
4ad4eba5 | 3837 | { |
fc0e6df6 | 3838 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3839 | |
fc0e6df6 PB |
3840 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3841 | { | |
3842 | if (a->vna_other == vernum) | |
4ad4eba5 | 3843 | { |
fc0e6df6 PB |
3844 | verstr = a->vna_nodename; |
3845 | break; | |
4ad4eba5 | 3846 | } |
4ad4eba5 | 3847 | } |
fc0e6df6 PB |
3848 | if (a != NULL) |
3849 | break; | |
3850 | } | |
3851 | if (verstr == NULL) | |
3852 | { | |
3853 | (*_bfd_error_handler) | |
3854 | (_("%B: %s: invalid needed version %d"), | |
3855 | abfd, name, vernum); | |
3856 | bfd_set_error (bfd_error_bad_value); | |
3857 | goto error_free_vers; | |
4ad4eba5 | 3858 | } |
4ad4eba5 | 3859 | } |
fc0e6df6 PB |
3860 | |
3861 | namelen = strlen (name); | |
3862 | verlen = strlen (verstr); | |
3863 | newlen = namelen + verlen + 2; | |
3864 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3865 | && isym->st_shndx != SHN_UNDEF) | |
3866 | ++newlen; | |
3867 | ||
66eb6687 | 3868 | newname = bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
3869 | if (newname == NULL) |
3870 | goto error_free_vers; | |
3871 | memcpy (newname, name, namelen); | |
3872 | p = newname + namelen; | |
3873 | *p++ = ELF_VER_CHR; | |
3874 | /* If this is a defined non-hidden version symbol, | |
3875 | we add another @ to the name. This indicates the | |
3876 | default version of the symbol. */ | |
3877 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3878 | && isym->st_shndx != SHN_UNDEF) | |
3879 | *p++ = ELF_VER_CHR; | |
3880 | memcpy (p, verstr, verlen + 1); | |
3881 | ||
3882 | name = newname; | |
4ad4eba5 AM |
3883 | } |
3884 | ||
af44c138 L |
3885 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
3886 | &value, &old_alignment, | |
4ad4eba5 AM |
3887 | sym_hash, &skip, &override, |
3888 | &type_change_ok, &size_change_ok)) | |
3889 | goto error_free_vers; | |
3890 | ||
3891 | if (skip) | |
3892 | continue; | |
3893 | ||
3894 | if (override) | |
3895 | definition = FALSE; | |
3896 | ||
3897 | h = *sym_hash; | |
3898 | while (h->root.type == bfd_link_hash_indirect | |
3899 | || h->root.type == bfd_link_hash_warning) | |
3900 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3901 | ||
3902 | /* Remember the old alignment if this is a common symbol, so | |
3903 | that we don't reduce the alignment later on. We can't | |
3904 | check later, because _bfd_generic_link_add_one_symbol | |
3905 | will set a default for the alignment which we want to | |
3906 | override. We also remember the old bfd where the existing | |
3907 | definition comes from. */ | |
3908 | switch (h->root.type) | |
3909 | { | |
3910 | default: | |
3911 | break; | |
3912 | ||
3913 | case bfd_link_hash_defined: | |
3914 | case bfd_link_hash_defweak: | |
3915 | old_bfd = h->root.u.def.section->owner; | |
3916 | break; | |
3917 | ||
3918 | case bfd_link_hash_common: | |
3919 | old_bfd = h->root.u.c.p->section->owner; | |
3920 | old_alignment = h->root.u.c.p->alignment_power; | |
3921 | break; | |
3922 | } | |
3923 | ||
3924 | if (elf_tdata (abfd)->verdef != NULL | |
3925 | && ! override | |
3926 | && vernum > 1 | |
3927 | && definition) | |
3928 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3929 | } | |
3930 | ||
3931 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 3932 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
3933 | (struct bfd_link_hash_entry **) sym_hash))) |
3934 | goto error_free_vers; | |
3935 | ||
3936 | h = *sym_hash; | |
3937 | while (h->root.type == bfd_link_hash_indirect | |
3938 | || h->root.type == bfd_link_hash_warning) | |
3939 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3940 | *sym_hash = h; | |
3941 | ||
3942 | new_weakdef = FALSE; | |
3943 | if (dynamic | |
3944 | && definition | |
3945 | && (flags & BSF_WEAK) != 0 | |
3946 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
66eb6687 | 3947 | && is_elf_hash_table (htab) |
f6e332e6 | 3948 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3949 | { |
3950 | /* Keep a list of all weak defined non function symbols from | |
3951 | a dynamic object, using the weakdef field. Later in this | |
3952 | function we will set the weakdef field to the correct | |
3953 | value. We only put non-function symbols from dynamic | |
3954 | objects on this list, because that happens to be the only | |
3955 | time we need to know the normal symbol corresponding to a | |
3956 | weak symbol, and the information is time consuming to | |
3957 | figure out. If the weakdef field is not already NULL, | |
3958 | then this symbol was already defined by some previous | |
3959 | dynamic object, and we will be using that previous | |
3960 | definition anyhow. */ | |
3961 | ||
f6e332e6 | 3962 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3963 | weaks = h; |
3964 | new_weakdef = TRUE; | |
3965 | } | |
3966 | ||
3967 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 3968 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
3969 | && h->root.type == bfd_link_hash_common) |
3970 | { | |
3971 | unsigned int align; | |
3972 | ||
a4d8e49b | 3973 | if (common) |
af44c138 L |
3974 | align = bfd_log2 (isym->st_value); |
3975 | else | |
3976 | { | |
3977 | /* The new symbol is a common symbol in a shared object. | |
3978 | We need to get the alignment from the section. */ | |
3979 | align = new_sec->alignment_power; | |
3980 | } | |
4ad4eba5 AM |
3981 | if (align > old_alignment |
3982 | /* Permit an alignment power of zero if an alignment of one | |
3983 | is specified and no other alignments have been specified. */ | |
3984 | || (isym->st_value == 1 && old_alignment == 0)) | |
3985 | h->root.u.c.p->alignment_power = align; | |
3986 | else | |
3987 | h->root.u.c.p->alignment_power = old_alignment; | |
3988 | } | |
3989 | ||
66eb6687 | 3990 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 3991 | { |
4ad4eba5 | 3992 | bfd_boolean dynsym; |
4ad4eba5 AM |
3993 | |
3994 | /* Check the alignment when a common symbol is involved. This | |
3995 | can change when a common symbol is overridden by a normal | |
3996 | definition or a common symbol is ignored due to the old | |
3997 | normal definition. We need to make sure the maximum | |
3998 | alignment is maintained. */ | |
a4d8e49b | 3999 | if ((old_alignment || common) |
4ad4eba5 AM |
4000 | && h->root.type != bfd_link_hash_common) |
4001 | { | |
4002 | unsigned int common_align; | |
4003 | unsigned int normal_align; | |
4004 | unsigned int symbol_align; | |
4005 | bfd *normal_bfd; | |
4006 | bfd *common_bfd; | |
4007 | ||
4008 | symbol_align = ffs (h->root.u.def.value) - 1; | |
4009 | if (h->root.u.def.section->owner != NULL | |
4010 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
4011 | { | |
4012 | normal_align = h->root.u.def.section->alignment_power; | |
4013 | if (normal_align > symbol_align) | |
4014 | normal_align = symbol_align; | |
4015 | } | |
4016 | else | |
4017 | normal_align = symbol_align; | |
4018 | ||
4019 | if (old_alignment) | |
4020 | { | |
4021 | common_align = old_alignment; | |
4022 | common_bfd = old_bfd; | |
4023 | normal_bfd = abfd; | |
4024 | } | |
4025 | else | |
4026 | { | |
4027 | common_align = bfd_log2 (isym->st_value); | |
4028 | common_bfd = abfd; | |
4029 | normal_bfd = old_bfd; | |
4030 | } | |
4031 | ||
4032 | if (normal_align < common_align) | |
d07676f8 NC |
4033 | { |
4034 | /* PR binutils/2735 */ | |
4035 | if (normal_bfd == NULL) | |
4036 | (*_bfd_error_handler) | |
4037 | (_("Warning: alignment %u of common symbol `%s' in %B" | |
4038 | " is greater than the alignment (%u) of its section %A"), | |
4039 | common_bfd, h->root.u.def.section, | |
4040 | 1 << common_align, name, 1 << normal_align); | |
4041 | else | |
4042 | (*_bfd_error_handler) | |
4043 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4044 | " is smaller than %u in %B"), | |
4045 | normal_bfd, common_bfd, | |
4046 | 1 << normal_align, name, 1 << common_align); | |
4047 | } | |
4ad4eba5 AM |
4048 | } |
4049 | ||
4050 | /* Remember the symbol size and type. */ | |
4051 | if (isym->st_size != 0 | |
4052 | && (definition || h->size == 0)) | |
4053 | { | |
4054 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
4055 | (*_bfd_error_handler) | |
d003868e AM |
4056 | (_("Warning: size of symbol `%s' changed" |
4057 | " from %lu in %B to %lu in %B"), | |
4058 | old_bfd, abfd, | |
4ad4eba5 | 4059 | name, (unsigned long) h->size, |
d003868e | 4060 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4061 | |
4062 | h->size = isym->st_size; | |
4063 | } | |
4064 | ||
4065 | /* If this is a common symbol, then we always want H->SIZE | |
4066 | to be the size of the common symbol. The code just above | |
4067 | won't fix the size if a common symbol becomes larger. We | |
4068 | don't warn about a size change here, because that is | |
4069 | covered by --warn-common. */ | |
4070 | if (h->root.type == bfd_link_hash_common) | |
4071 | h->size = h->root.u.c.size; | |
4072 | ||
4073 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
4074 | && (definition || h->type == STT_NOTYPE)) | |
4075 | { | |
4076 | if (h->type != STT_NOTYPE | |
4077 | && h->type != ELF_ST_TYPE (isym->st_info) | |
4078 | && ! type_change_ok) | |
4079 | (*_bfd_error_handler) | |
d003868e AM |
4080 | (_("Warning: type of symbol `%s' changed" |
4081 | " from %d to %d in %B"), | |
4082 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
4083 | |
4084 | h->type = ELF_ST_TYPE (isym->st_info); | |
4085 | } | |
4086 | ||
4087 | /* If st_other has a processor-specific meaning, specific | |
4088 | code might be needed here. We never merge the visibility | |
4089 | attribute with the one from a dynamic object. */ | |
4090 | if (bed->elf_backend_merge_symbol_attribute) | |
4091 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
4092 | dynamic); | |
4093 | ||
b58f81ae DJ |
4094 | /* If this symbol has default visibility and the user has requested |
4095 | we not re-export it, then mark it as hidden. */ | |
4096 | if (definition && !dynamic | |
4097 | && (abfd->no_export | |
4098 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
4099 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
66eb6687 AM |
4100 | isym->st_other = (STV_HIDDEN |
4101 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
b58f81ae | 4102 | |
8992f0d7 | 4103 | if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic) |
4ad4eba5 AM |
4104 | { |
4105 | unsigned char hvis, symvis, other, nvis; | |
4106 | ||
8992f0d7 TS |
4107 | /* Only merge the visibility. Leave the remainder of the |
4108 | st_other field to elf_backend_merge_symbol_attribute. */ | |
4109 | other = h->other & ~ELF_ST_VISIBILITY (-1); | |
4ad4eba5 AM |
4110 | |
4111 | /* Combine visibilities, using the most constraining one. */ | |
4112 | hvis = ELF_ST_VISIBILITY (h->other); | |
4113 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
4114 | if (! hvis) | |
4115 | nvis = symvis; | |
4116 | else if (! symvis) | |
4117 | nvis = hvis; | |
4118 | else | |
4119 | nvis = hvis < symvis ? hvis : symvis; | |
4120 | ||
4121 | h->other = other | nvis; | |
4122 | } | |
4123 | ||
4124 | /* Set a flag in the hash table entry indicating the type of | |
4125 | reference or definition we just found. Keep a count of | |
4126 | the number of dynamic symbols we find. A dynamic symbol | |
4127 | is one which is referenced or defined by both a regular | |
4128 | object and a shared object. */ | |
4ad4eba5 AM |
4129 | dynsym = FALSE; |
4130 | if (! dynamic) | |
4131 | { | |
4132 | if (! definition) | |
4133 | { | |
f5385ebf | 4134 | h->ref_regular = 1; |
4ad4eba5 | 4135 | if (bind != STB_WEAK) |
f5385ebf | 4136 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4137 | } |
4138 | else | |
f5385ebf | 4139 | h->def_regular = 1; |
4ad4eba5 | 4140 | if (! info->executable |
f5385ebf AM |
4141 | || h->def_dynamic |
4142 | || h->ref_dynamic) | |
4ad4eba5 AM |
4143 | dynsym = TRUE; |
4144 | } | |
4145 | else | |
4146 | { | |
4147 | if (! definition) | |
f5385ebf | 4148 | h->ref_dynamic = 1; |
4ad4eba5 | 4149 | else |
f5385ebf AM |
4150 | h->def_dynamic = 1; |
4151 | if (h->def_regular | |
4152 | || h->ref_regular | |
f6e332e6 | 4153 | || (h->u.weakdef != NULL |
4ad4eba5 | 4154 | && ! new_weakdef |
f6e332e6 | 4155 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4156 | dynsym = TRUE; |
4157 | } | |
4158 | ||
4ad4eba5 AM |
4159 | /* Check to see if we need to add an indirect symbol for |
4160 | the default name. */ | |
4161 | if (definition || h->root.type == bfd_link_hash_common) | |
4162 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4163 | &sec, &value, &dynsym, | |
4164 | override)) | |
4165 | goto error_free_vers; | |
4166 | ||
4167 | if (definition && !dynamic) | |
4168 | { | |
4169 | char *p = strchr (name, ELF_VER_CHR); | |
4170 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4171 | { | |
4172 | /* Queue non-default versions so that .symver x, x@FOO | |
4173 | aliases can be checked. */ | |
66eb6687 | 4174 | if (!nondeflt_vers) |
4ad4eba5 | 4175 | { |
66eb6687 AM |
4176 | amt = ((isymend - isym + 1) |
4177 | * sizeof (struct elf_link_hash_entry *)); | |
4ad4eba5 AM |
4178 | nondeflt_vers = bfd_malloc (amt); |
4179 | } | |
66eb6687 | 4180 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4181 | } |
4182 | } | |
4183 | ||
4184 | if (dynsym && h->dynindx == -1) | |
4185 | { | |
c152c796 | 4186 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4187 | goto error_free_vers; |
f6e332e6 | 4188 | if (h->u.weakdef != NULL |
4ad4eba5 | 4189 | && ! new_weakdef |
f6e332e6 | 4190 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4191 | { |
66eb6687 | 4192 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4193 | goto error_free_vers; |
4194 | } | |
4195 | } | |
4196 | else if (dynsym && h->dynindx != -1) | |
4197 | /* If the symbol already has a dynamic index, but | |
4198 | visibility says it should not be visible, turn it into | |
4199 | a local symbol. */ | |
4200 | switch (ELF_ST_VISIBILITY (h->other)) | |
4201 | { | |
4202 | case STV_INTERNAL: | |
4203 | case STV_HIDDEN: | |
4204 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4205 | dynsym = FALSE; | |
4206 | break; | |
4207 | } | |
4208 | ||
4209 | if (!add_needed | |
4210 | && definition | |
4211 | && dynsym | |
f5385ebf | 4212 | && h->ref_regular) |
4ad4eba5 AM |
4213 | { |
4214 | int ret; | |
4215 | const char *soname = elf_dt_name (abfd); | |
4216 | ||
4217 | /* A symbol from a library loaded via DT_NEEDED of some | |
4218 | other library is referenced by a regular object. | |
e56f61be L |
4219 | Add a DT_NEEDED entry for it. Issue an error if |
4220 | --no-add-needed is used. */ | |
4221 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
4222 | { | |
4223 | (*_bfd_error_handler) | |
4224 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 4225 | abfd, name); |
e56f61be L |
4226 | bfd_set_error (bfd_error_bad_value); |
4227 | goto error_free_vers; | |
4228 | } | |
4229 | ||
a5db907e AM |
4230 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
4231 | ||
4ad4eba5 | 4232 | add_needed = TRUE; |
7e9f0867 | 4233 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4234 | if (ret < 0) |
4235 | goto error_free_vers; | |
4236 | ||
4237 | BFD_ASSERT (ret == 0); | |
4238 | } | |
4239 | } | |
4240 | } | |
4241 | ||
66eb6687 AM |
4242 | if (extversym != NULL) |
4243 | { | |
4244 | free (extversym); | |
4245 | extversym = NULL; | |
4246 | } | |
4247 | ||
4248 | if (isymbuf != NULL) | |
4249 | { | |
4250 | free (isymbuf); | |
4251 | isymbuf = NULL; | |
4252 | } | |
4253 | ||
4254 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4255 | { | |
4256 | unsigned int i; | |
4257 | ||
4258 | /* Restore the symbol table. */ | |
4259 | old_hash = (char *) old_tab + tabsize; | |
4260 | old_ent = (char *) old_hash + hashsize; | |
4261 | sym_hash = elf_sym_hashes (abfd); | |
4f87808c AM |
4262 | htab->root.table.table = old_table; |
4263 | htab->root.table.size = old_size; | |
4264 | htab->root.table.count = old_count; | |
66eb6687 AM |
4265 | memcpy (htab->root.table.table, old_tab, tabsize); |
4266 | memcpy (sym_hash, old_hash, hashsize); | |
4267 | htab->root.undefs = old_undefs; | |
4268 | htab->root.undefs_tail = old_undefs_tail; | |
4269 | for (i = 0; i < htab->root.table.size; i++) | |
4270 | { | |
4271 | struct bfd_hash_entry *p; | |
4272 | struct elf_link_hash_entry *h; | |
4273 | ||
4274 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4275 | { | |
4276 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4277 | if (h->root.type == bfd_link_hash_warning) |
4278 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
66eb6687 AM |
4279 | if (h->dynindx >= old_dynsymcount) |
4280 | _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); | |
2de92251 | 4281 | |
66eb6687 AM |
4282 | memcpy (p, old_ent, htab->root.table.entsize); |
4283 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4284 | h = (struct elf_link_hash_entry *) p; |
4285 | if (h->root.type == bfd_link_hash_warning) | |
4286 | { | |
4287 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4288 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4289 | } | |
66eb6687 AM |
4290 | } |
4291 | } | |
4292 | ||
5061a885 AM |
4293 | /* Make a special call to the linker "notice" function to |
4294 | tell it that symbols added for crefs may need to be removed. */ | |
4295 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
4296 | notice_not_needed)) | |
4297 | return FALSE; | |
4298 | ||
66eb6687 AM |
4299 | free (old_tab); |
4300 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4301 | alloc_mark); | |
4302 | if (nondeflt_vers != NULL) | |
4303 | free (nondeflt_vers); | |
4304 | return TRUE; | |
4305 | } | |
2de92251 | 4306 | |
66eb6687 AM |
4307 | if (old_tab != NULL) |
4308 | { | |
5061a885 AM |
4309 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, |
4310 | notice_needed)) | |
4311 | return FALSE; | |
66eb6687 AM |
4312 | free (old_tab); |
4313 | old_tab = NULL; | |
4314 | } | |
4315 | ||
4ad4eba5 AM |
4316 | /* Now that all the symbols from this input file are created, handle |
4317 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4318 | if (nondeflt_vers != NULL) | |
4319 | { | |
4320 | bfd_size_type cnt, symidx; | |
4321 | ||
4322 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4323 | { | |
4324 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4325 | char *shortname, *p; | |
4326 | ||
4327 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4328 | if (p == NULL | |
4329 | || (h->root.type != bfd_link_hash_defined | |
4330 | && h->root.type != bfd_link_hash_defweak)) | |
4331 | continue; | |
4332 | ||
4333 | amt = p - h->root.root.string; | |
4334 | shortname = bfd_malloc (amt + 1); | |
4335 | memcpy (shortname, h->root.root.string, amt); | |
4336 | shortname[amt] = '\0'; | |
4337 | ||
4338 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4339 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4340 | FALSE, FALSE, FALSE); |
4341 | if (hi != NULL | |
4342 | && hi->root.type == h->root.type | |
4343 | && hi->root.u.def.value == h->root.u.def.value | |
4344 | && hi->root.u.def.section == h->root.u.def.section) | |
4345 | { | |
4346 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4347 | hi->root.type = bfd_link_hash_indirect; | |
4348 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4349 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4350 | sym_hash = elf_sym_hashes (abfd); |
4351 | if (sym_hash) | |
4352 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4353 | if (sym_hash[symidx] == hi) | |
4354 | { | |
4355 | sym_hash[symidx] = h; | |
4356 | break; | |
4357 | } | |
4358 | } | |
4359 | free (shortname); | |
4360 | } | |
4361 | free (nondeflt_vers); | |
4362 | nondeflt_vers = NULL; | |
4363 | } | |
4364 | ||
4ad4eba5 AM |
4365 | /* Now set the weakdefs field correctly for all the weak defined |
4366 | symbols we found. The only way to do this is to search all the | |
4367 | symbols. Since we only need the information for non functions in | |
4368 | dynamic objects, that's the only time we actually put anything on | |
4369 | the list WEAKS. We need this information so that if a regular | |
4370 | object refers to a symbol defined weakly in a dynamic object, the | |
4371 | real symbol in the dynamic object is also put in the dynamic | |
4372 | symbols; we also must arrange for both symbols to point to the | |
4373 | same memory location. We could handle the general case of symbol | |
4374 | aliasing, but a general symbol alias can only be generated in | |
4375 | assembler code, handling it correctly would be very time | |
4376 | consuming, and other ELF linkers don't handle general aliasing | |
4377 | either. */ | |
4378 | if (weaks != NULL) | |
4379 | { | |
4380 | struct elf_link_hash_entry **hpp; | |
4381 | struct elf_link_hash_entry **hppend; | |
4382 | struct elf_link_hash_entry **sorted_sym_hash; | |
4383 | struct elf_link_hash_entry *h; | |
4384 | size_t sym_count; | |
4385 | ||
4386 | /* Since we have to search the whole symbol list for each weak | |
4387 | defined symbol, search time for N weak defined symbols will be | |
4388 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4389 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4390 | sorted_sym_hash = bfd_malloc (amt); | |
4391 | if (sorted_sym_hash == NULL) | |
4392 | goto error_return; | |
4393 | sym_hash = sorted_sym_hash; | |
4394 | hpp = elf_sym_hashes (abfd); | |
4395 | hppend = hpp + extsymcount; | |
4396 | sym_count = 0; | |
4397 | for (; hpp < hppend; hpp++) | |
4398 | { | |
4399 | h = *hpp; | |
4400 | if (h != NULL | |
4401 | && h->root.type == bfd_link_hash_defined | |
4402 | && h->type != STT_FUNC) | |
4403 | { | |
4404 | *sym_hash = h; | |
4405 | sym_hash++; | |
4406 | sym_count++; | |
4407 | } | |
4408 | } | |
4409 | ||
4410 | qsort (sorted_sym_hash, sym_count, | |
4411 | sizeof (struct elf_link_hash_entry *), | |
4412 | elf_sort_symbol); | |
4413 | ||
4414 | while (weaks != NULL) | |
4415 | { | |
4416 | struct elf_link_hash_entry *hlook; | |
4417 | asection *slook; | |
4418 | bfd_vma vlook; | |
4419 | long ilook; | |
4420 | size_t i, j, idx; | |
4421 | ||
4422 | hlook = weaks; | |
f6e332e6 AM |
4423 | weaks = hlook->u.weakdef; |
4424 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4425 | |
4426 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4427 | || hlook->root.type == bfd_link_hash_defweak | |
4428 | || hlook->root.type == bfd_link_hash_common | |
4429 | || hlook->root.type == bfd_link_hash_indirect); | |
4430 | slook = hlook->root.u.def.section; | |
4431 | vlook = hlook->root.u.def.value; | |
4432 | ||
4433 | ilook = -1; | |
4434 | i = 0; | |
4435 | j = sym_count; | |
4436 | while (i < j) | |
4437 | { | |
4438 | bfd_signed_vma vdiff; | |
4439 | idx = (i + j) / 2; | |
4440 | h = sorted_sym_hash [idx]; | |
4441 | vdiff = vlook - h->root.u.def.value; | |
4442 | if (vdiff < 0) | |
4443 | j = idx; | |
4444 | else if (vdiff > 0) | |
4445 | i = idx + 1; | |
4446 | else | |
4447 | { | |
a9b881be | 4448 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4449 | if (sdiff < 0) |
4450 | j = idx; | |
4451 | else if (sdiff > 0) | |
4452 | i = idx + 1; | |
4453 | else | |
4454 | { | |
4455 | ilook = idx; | |
4456 | break; | |
4457 | } | |
4458 | } | |
4459 | } | |
4460 | ||
4461 | /* We didn't find a value/section match. */ | |
4462 | if (ilook == -1) | |
4463 | continue; | |
4464 | ||
4465 | for (i = ilook; i < sym_count; i++) | |
4466 | { | |
4467 | h = sorted_sym_hash [i]; | |
4468 | ||
4469 | /* Stop if value or section doesn't match. */ | |
4470 | if (h->root.u.def.value != vlook | |
4471 | || h->root.u.def.section != slook) | |
4472 | break; | |
4473 | else if (h != hlook) | |
4474 | { | |
f6e332e6 | 4475 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4476 | |
4477 | /* If the weak definition is in the list of dynamic | |
4478 | symbols, make sure the real definition is put | |
4479 | there as well. */ | |
4480 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4481 | { | |
c152c796 | 4482 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4483 | goto error_return; |
4484 | } | |
4485 | ||
4486 | /* If the real definition is in the list of dynamic | |
4487 | symbols, make sure the weak definition is put | |
4488 | there as well. If we don't do this, then the | |
4489 | dynamic loader might not merge the entries for the | |
4490 | real definition and the weak definition. */ | |
4491 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4492 | { | |
c152c796 | 4493 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4494 | goto error_return; |
4495 | } | |
4496 | break; | |
4497 | } | |
4498 | } | |
4499 | } | |
4500 | ||
4501 | free (sorted_sym_hash); | |
4502 | } | |
4503 | ||
66eb6687 AM |
4504 | if (bed->check_directives) |
4505 | (*bed->check_directives) (abfd, info); | |
85fbca6a | 4506 | |
4ad4eba5 AM |
4507 | /* If this object is the same format as the output object, and it is |
4508 | not a shared library, then let the backend look through the | |
4509 | relocs. | |
4510 | ||
4511 | This is required to build global offset table entries and to | |
4512 | arrange for dynamic relocs. It is not required for the | |
4513 | particular common case of linking non PIC code, even when linking | |
4514 | against shared libraries, but unfortunately there is no way of | |
4515 | knowing whether an object file has been compiled PIC or not. | |
4516 | Looking through the relocs is not particularly time consuming. | |
4517 | The problem is that we must either (1) keep the relocs in memory, | |
4518 | which causes the linker to require additional runtime memory or | |
4519 | (2) read the relocs twice from the input file, which wastes time. | |
4520 | This would be a good case for using mmap. | |
4521 | ||
4522 | I have no idea how to handle linking PIC code into a file of a | |
4523 | different format. It probably can't be done. */ | |
4ad4eba5 | 4524 | if (! dynamic |
66eb6687 AM |
4525 | && is_elf_hash_table (htab) |
4526 | && htab->root.creator == abfd->xvec | |
4527 | && bed->check_relocs != NULL) | |
4ad4eba5 AM |
4528 | { |
4529 | asection *o; | |
4530 | ||
4531 | for (o = abfd->sections; o != NULL; o = o->next) | |
4532 | { | |
4533 | Elf_Internal_Rela *internal_relocs; | |
4534 | bfd_boolean ok; | |
4535 | ||
4536 | if ((o->flags & SEC_RELOC) == 0 | |
4537 | || o->reloc_count == 0 | |
4538 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4539 | && (o->flags & SEC_DEBUGGING) != 0) | |
4540 | || bfd_is_abs_section (o->output_section)) | |
4541 | continue; | |
4542 | ||
4543 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4544 | info->keep_memory); | |
4545 | if (internal_relocs == NULL) | |
4546 | goto error_return; | |
4547 | ||
66eb6687 | 4548 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); |
4ad4eba5 AM |
4549 | |
4550 | if (elf_section_data (o)->relocs != internal_relocs) | |
4551 | free (internal_relocs); | |
4552 | ||
4553 | if (! ok) | |
4554 | goto error_return; | |
4555 | } | |
4556 | } | |
4557 | ||
4558 | /* If this is a non-traditional link, try to optimize the handling | |
4559 | of the .stab/.stabstr sections. */ | |
4560 | if (! dynamic | |
4561 | && ! info->traditional_format | |
66eb6687 | 4562 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
4563 | && (info->strip != strip_all && info->strip != strip_debugger)) |
4564 | { | |
4565 | asection *stabstr; | |
4566 | ||
4567 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4568 | if (stabstr != NULL) | |
4569 | { | |
4570 | bfd_size_type string_offset = 0; | |
4571 | asection *stab; | |
4572 | ||
4573 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 4574 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
4575 | && (!stab->name[5] || |
4576 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4577 | && (stab->flags & SEC_MERGE) == 0 | |
4578 | && !bfd_is_abs_section (stab->output_section)) | |
4579 | { | |
4580 | struct bfd_elf_section_data *secdata; | |
4581 | ||
4582 | secdata = elf_section_data (stab); | |
66eb6687 AM |
4583 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
4584 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
4585 | &string_offset)) |
4586 | goto error_return; | |
4587 | if (secdata->sec_info) | |
4588 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4589 | } | |
4590 | } | |
4591 | } | |
4592 | ||
66eb6687 | 4593 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
4594 | { |
4595 | /* Add this bfd to the loaded list. */ | |
4596 | struct elf_link_loaded_list *n; | |
4597 | ||
4598 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4599 | if (n == NULL) | |
4600 | goto error_return; | |
4601 | n->abfd = abfd; | |
66eb6687 AM |
4602 | n->next = htab->loaded; |
4603 | htab->loaded = n; | |
4ad4eba5 AM |
4604 | } |
4605 | ||
4606 | return TRUE; | |
4607 | ||
4608 | error_free_vers: | |
66eb6687 AM |
4609 | if (old_tab != NULL) |
4610 | free (old_tab); | |
4ad4eba5 AM |
4611 | if (nondeflt_vers != NULL) |
4612 | free (nondeflt_vers); | |
4613 | if (extversym != NULL) | |
4614 | free (extversym); | |
4615 | error_free_sym: | |
4616 | if (isymbuf != NULL) | |
4617 | free (isymbuf); | |
4618 | error_return: | |
4619 | return FALSE; | |
4620 | } | |
4621 | ||
8387904d AM |
4622 | /* Return the linker hash table entry of a symbol that might be |
4623 | satisfied by an archive symbol. Return -1 on error. */ | |
4624 | ||
4625 | struct elf_link_hash_entry * | |
4626 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4627 | struct bfd_link_info *info, | |
4628 | const char *name) | |
4629 | { | |
4630 | struct elf_link_hash_entry *h; | |
4631 | char *p, *copy; | |
4632 | size_t len, first; | |
4633 | ||
4634 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4635 | if (h != NULL) | |
4636 | return h; | |
4637 | ||
4638 | /* If this is a default version (the name contains @@), look up the | |
4639 | symbol again with only one `@' as well as without the version. | |
4640 | The effect is that references to the symbol with and without the | |
4641 | version will be matched by the default symbol in the archive. */ | |
4642 | ||
4643 | p = strchr (name, ELF_VER_CHR); | |
4644 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4645 | return h; | |
4646 | ||
4647 | /* First check with only one `@'. */ | |
4648 | len = strlen (name); | |
4649 | copy = bfd_alloc (abfd, len); | |
4650 | if (copy == NULL) | |
4651 | return (struct elf_link_hash_entry *) 0 - 1; | |
4652 | ||
4653 | first = p - name + 1; | |
4654 | memcpy (copy, name, first); | |
4655 | memcpy (copy + first, name + first + 1, len - first); | |
4656 | ||
4657 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4658 | if (h == NULL) | |
4659 | { | |
4660 | /* We also need to check references to the symbol without the | |
4661 | version. */ | |
4662 | copy[first - 1] = '\0'; | |
4663 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4664 | FALSE, FALSE, FALSE); | |
4665 | } | |
4666 | ||
4667 | bfd_release (abfd, copy); | |
4668 | return h; | |
4669 | } | |
4670 | ||
0ad989f9 L |
4671 | /* Add symbols from an ELF archive file to the linker hash table. We |
4672 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4673 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4674 | archive which includes an entry libc.so.1 which defines a bunch of | |
4675 | symbols. The libc.so archive also includes a number of other | |
4676 | object files, which also define symbols, some of which are the same | |
4677 | as those defined in libc.so.1. Correct linking requires that we | |
4678 | consider each object file in turn, and include it if it defines any | |
4679 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4680 | this; it looks through the list of undefined symbols, and includes | |
4681 | any object file which defines them. When this algorithm is used on | |
4682 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4683 | bunch of symbols. This means that some of the other objects in the | |
4684 | archive are not included in the link, which is incorrect since they | |
4685 | precede libc.so.1 in the archive. | |
4686 | ||
4687 | Fortunately, ELF archive handling is simpler than that done by | |
4688 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4689 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4690 | symbol is currently undefined, we know that we must pull in that | |
4691 | object file. | |
4692 | ||
4693 | Unfortunately, we do have to make multiple passes over the symbol | |
4694 | table until nothing further is resolved. */ | |
4695 | ||
4ad4eba5 AM |
4696 | static bfd_boolean |
4697 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4698 | { |
4699 | symindex c; | |
4700 | bfd_boolean *defined = NULL; | |
4701 | bfd_boolean *included = NULL; | |
4702 | carsym *symdefs; | |
4703 | bfd_boolean loop; | |
4704 | bfd_size_type amt; | |
8387904d AM |
4705 | const struct elf_backend_data *bed; |
4706 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4707 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4708 | |
4709 | if (! bfd_has_map (abfd)) | |
4710 | { | |
4711 | /* An empty archive is a special case. */ | |
4712 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4713 | return TRUE; | |
4714 | bfd_set_error (bfd_error_no_armap); | |
4715 | return FALSE; | |
4716 | } | |
4717 | ||
4718 | /* Keep track of all symbols we know to be already defined, and all | |
4719 | files we know to be already included. This is to speed up the | |
4720 | second and subsequent passes. */ | |
4721 | c = bfd_ardata (abfd)->symdef_count; | |
4722 | if (c == 0) | |
4723 | return TRUE; | |
4724 | amt = c; | |
4725 | amt *= sizeof (bfd_boolean); | |
4726 | defined = bfd_zmalloc (amt); | |
4727 | included = bfd_zmalloc (amt); | |
4728 | if (defined == NULL || included == NULL) | |
4729 | goto error_return; | |
4730 | ||
4731 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4732 | bed = get_elf_backend_data (abfd); |
4733 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4734 | |
4735 | do | |
4736 | { | |
4737 | file_ptr last; | |
4738 | symindex i; | |
4739 | carsym *symdef; | |
4740 | carsym *symdefend; | |
4741 | ||
4742 | loop = FALSE; | |
4743 | last = -1; | |
4744 | ||
4745 | symdef = symdefs; | |
4746 | symdefend = symdef + c; | |
4747 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4748 | { | |
4749 | struct elf_link_hash_entry *h; | |
4750 | bfd *element; | |
4751 | struct bfd_link_hash_entry *undefs_tail; | |
4752 | symindex mark; | |
4753 | ||
4754 | if (defined[i] || included[i]) | |
4755 | continue; | |
4756 | if (symdef->file_offset == last) | |
4757 | { | |
4758 | included[i] = TRUE; | |
4759 | continue; | |
4760 | } | |
4761 | ||
8387904d AM |
4762 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4763 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4764 | goto error_return; | |
0ad989f9 L |
4765 | |
4766 | if (h == NULL) | |
4767 | continue; | |
4768 | ||
4769 | if (h->root.type == bfd_link_hash_common) | |
4770 | { | |
4771 | /* We currently have a common symbol. The archive map contains | |
4772 | a reference to this symbol, so we may want to include it. We | |
4773 | only want to include it however, if this archive element | |
4774 | contains a definition of the symbol, not just another common | |
4775 | declaration of it. | |
4776 | ||
4777 | Unfortunately some archivers (including GNU ar) will put | |
4778 | declarations of common symbols into their archive maps, as | |
4779 | well as real definitions, so we cannot just go by the archive | |
4780 | map alone. Instead we must read in the element's symbol | |
4781 | table and check that to see what kind of symbol definition | |
4782 | this is. */ | |
4783 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4784 | continue; | |
4785 | } | |
4786 | else if (h->root.type != bfd_link_hash_undefined) | |
4787 | { | |
4788 | if (h->root.type != bfd_link_hash_undefweak) | |
4789 | defined[i] = TRUE; | |
4790 | continue; | |
4791 | } | |
4792 | ||
4793 | /* We need to include this archive member. */ | |
4794 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4795 | if (element == NULL) | |
4796 | goto error_return; | |
4797 | ||
4798 | if (! bfd_check_format (element, bfd_object)) | |
4799 | goto error_return; | |
4800 | ||
4801 | /* Doublecheck that we have not included this object | |
4802 | already--it should be impossible, but there may be | |
4803 | something wrong with the archive. */ | |
4804 | if (element->archive_pass != 0) | |
4805 | { | |
4806 | bfd_set_error (bfd_error_bad_value); | |
4807 | goto error_return; | |
4808 | } | |
4809 | element->archive_pass = 1; | |
4810 | ||
4811 | undefs_tail = info->hash->undefs_tail; | |
4812 | ||
4813 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4814 | symdef->name)) | |
4815 | goto error_return; | |
4816 | if (! bfd_link_add_symbols (element, info)) | |
4817 | goto error_return; | |
4818 | ||
4819 | /* If there are any new undefined symbols, we need to make | |
4820 | another pass through the archive in order to see whether | |
4821 | they can be defined. FIXME: This isn't perfect, because | |
4822 | common symbols wind up on undefs_tail and because an | |
4823 | undefined symbol which is defined later on in this pass | |
4824 | does not require another pass. This isn't a bug, but it | |
4825 | does make the code less efficient than it could be. */ | |
4826 | if (undefs_tail != info->hash->undefs_tail) | |
4827 | loop = TRUE; | |
4828 | ||
4829 | /* Look backward to mark all symbols from this object file | |
4830 | which we have already seen in this pass. */ | |
4831 | mark = i; | |
4832 | do | |
4833 | { | |
4834 | included[mark] = TRUE; | |
4835 | if (mark == 0) | |
4836 | break; | |
4837 | --mark; | |
4838 | } | |
4839 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4840 | ||
4841 | /* We mark subsequent symbols from this object file as we go | |
4842 | on through the loop. */ | |
4843 | last = symdef->file_offset; | |
4844 | } | |
4845 | } | |
4846 | while (loop); | |
4847 | ||
4848 | free (defined); | |
4849 | free (included); | |
4850 | ||
4851 | return TRUE; | |
4852 | ||
4853 | error_return: | |
4854 | if (defined != NULL) | |
4855 | free (defined); | |
4856 | if (included != NULL) | |
4857 | free (included); | |
4858 | return FALSE; | |
4859 | } | |
4ad4eba5 AM |
4860 | |
4861 | /* Given an ELF BFD, add symbols to the global hash table as | |
4862 | appropriate. */ | |
4863 | ||
4864 | bfd_boolean | |
4865 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4866 | { | |
4867 | switch (bfd_get_format (abfd)) | |
4868 | { | |
4869 | case bfd_object: | |
4870 | return elf_link_add_object_symbols (abfd, info); | |
4871 | case bfd_archive: | |
4872 | return elf_link_add_archive_symbols (abfd, info); | |
4873 | default: | |
4874 | bfd_set_error (bfd_error_wrong_format); | |
4875 | return FALSE; | |
4876 | } | |
4877 | } | |
5a580b3a AM |
4878 | \f |
4879 | /* This function will be called though elf_link_hash_traverse to store | |
4880 | all hash value of the exported symbols in an array. */ | |
4881 | ||
4882 | static bfd_boolean | |
4883 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4884 | { | |
4885 | unsigned long **valuep = data; | |
4886 | const char *name; | |
4887 | char *p; | |
4888 | unsigned long ha; | |
4889 | char *alc = NULL; | |
4890 | ||
4891 | if (h->root.type == bfd_link_hash_warning) | |
4892 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4893 | ||
4894 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4895 | if (h->dynindx == -1) | |
4896 | return TRUE; | |
4897 | ||
4898 | name = h->root.root.string; | |
4899 | p = strchr (name, ELF_VER_CHR); | |
4900 | if (p != NULL) | |
4901 | { | |
4902 | alc = bfd_malloc (p - name + 1); | |
4903 | memcpy (alc, name, p - name); | |
4904 | alc[p - name] = '\0'; | |
4905 | name = alc; | |
4906 | } | |
4907 | ||
4908 | /* Compute the hash value. */ | |
4909 | ha = bfd_elf_hash (name); | |
4910 | ||
4911 | /* Store the found hash value in the array given as the argument. */ | |
4912 | *(*valuep)++ = ha; | |
4913 | ||
4914 | /* And store it in the struct so that we can put it in the hash table | |
4915 | later. */ | |
f6e332e6 | 4916 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4917 | |
4918 | if (alc != NULL) | |
4919 | free (alc); | |
4920 | ||
4921 | return TRUE; | |
4922 | } | |
4923 | ||
fdc90cb4 JJ |
4924 | struct collect_gnu_hash_codes |
4925 | { | |
4926 | bfd *output_bfd; | |
4927 | const struct elf_backend_data *bed; | |
4928 | unsigned long int nsyms; | |
4929 | unsigned long int maskbits; | |
4930 | unsigned long int *hashcodes; | |
4931 | unsigned long int *hashval; | |
4932 | unsigned long int *indx; | |
4933 | unsigned long int *counts; | |
4934 | bfd_vma *bitmask; | |
4935 | bfd_byte *contents; | |
4936 | long int min_dynindx; | |
4937 | unsigned long int bucketcount; | |
4938 | unsigned long int symindx; | |
4939 | long int local_indx; | |
4940 | long int shift1, shift2; | |
4941 | unsigned long int mask; | |
4942 | }; | |
4943 | ||
4944 | /* This function will be called though elf_link_hash_traverse to store | |
4945 | all hash value of the exported symbols in an array. */ | |
4946 | ||
4947 | static bfd_boolean | |
4948 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4949 | { | |
4950 | struct collect_gnu_hash_codes *s = data; | |
4951 | const char *name; | |
4952 | char *p; | |
4953 | unsigned long ha; | |
4954 | char *alc = NULL; | |
4955 | ||
4956 | if (h->root.type == bfd_link_hash_warning) | |
4957 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4958 | ||
4959 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4960 | if (h->dynindx == -1) | |
4961 | return TRUE; | |
4962 | ||
4963 | /* Ignore also local symbols and undefined symbols. */ | |
4964 | if (! (*s->bed->elf_hash_symbol) (h)) | |
4965 | return TRUE; | |
4966 | ||
4967 | name = h->root.root.string; | |
4968 | p = strchr (name, ELF_VER_CHR); | |
4969 | if (p != NULL) | |
4970 | { | |
4971 | alc = bfd_malloc (p - name + 1); | |
4972 | memcpy (alc, name, p - name); | |
4973 | alc[p - name] = '\0'; | |
4974 | name = alc; | |
4975 | } | |
4976 | ||
4977 | /* Compute the hash value. */ | |
4978 | ha = bfd_elf_gnu_hash (name); | |
4979 | ||
4980 | /* Store the found hash value in the array for compute_bucket_count, | |
4981 | and also for .dynsym reordering purposes. */ | |
4982 | s->hashcodes[s->nsyms] = ha; | |
4983 | s->hashval[h->dynindx] = ha; | |
4984 | ++s->nsyms; | |
4985 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
4986 | s->min_dynindx = h->dynindx; | |
4987 | ||
4988 | if (alc != NULL) | |
4989 | free (alc); | |
4990 | ||
4991 | return TRUE; | |
4992 | } | |
4993 | ||
4994 | /* This function will be called though elf_link_hash_traverse to do | |
4995 | final dynaminc symbol renumbering. */ | |
4996 | ||
4997 | static bfd_boolean | |
4998 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
4999 | { | |
5000 | struct collect_gnu_hash_codes *s = data; | |
5001 | unsigned long int bucket; | |
5002 | unsigned long int val; | |
5003 | ||
5004 | if (h->root.type == bfd_link_hash_warning) | |
5005 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5006 | ||
5007 | /* Ignore indirect symbols. */ | |
5008 | if (h->dynindx == -1) | |
5009 | return TRUE; | |
5010 | ||
5011 | /* Ignore also local symbols and undefined symbols. */ | |
5012 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5013 | { | |
5014 | if (h->dynindx >= s->min_dynindx) | |
5015 | h->dynindx = s->local_indx++; | |
5016 | return TRUE; | |
5017 | } | |
5018 | ||
5019 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5020 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5021 | & ((s->maskbits >> s->shift1) - 1); | |
5022 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5023 | s->bitmask[val] | |
5024 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5025 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5026 | if (s->counts[bucket] == 1) | |
5027 | /* Last element terminates the chain. */ | |
5028 | val |= 1; | |
5029 | bfd_put_32 (s->output_bfd, val, | |
5030 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5031 | --s->counts[bucket]; | |
5032 | h->dynindx = s->indx[bucket]++; | |
5033 | return TRUE; | |
5034 | } | |
5035 | ||
5036 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5037 | ||
5038 | bfd_boolean | |
5039 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5040 | { | |
5041 | return !(h->forced_local | |
5042 | || h->root.type == bfd_link_hash_undefined | |
5043 | || h->root.type == bfd_link_hash_undefweak | |
5044 | || ((h->root.type == bfd_link_hash_defined | |
5045 | || h->root.type == bfd_link_hash_defweak) | |
5046 | && h->root.u.def.section->output_section == NULL)); | |
5047 | } | |
5048 | ||
5a580b3a AM |
5049 | /* Array used to determine the number of hash table buckets to use |
5050 | based on the number of symbols there are. If there are fewer than | |
5051 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5052 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5053 | than 32771 buckets. */ | |
5054 | ||
5055 | static const size_t elf_buckets[] = | |
5056 | { | |
5057 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5058 | 16411, 32771, 0 | |
5059 | }; | |
5060 | ||
5061 | /* Compute bucket count for hashing table. We do not use a static set | |
5062 | of possible tables sizes anymore. Instead we determine for all | |
5063 | possible reasonable sizes of the table the outcome (i.e., the | |
5064 | number of collisions etc) and choose the best solution. The | |
5065 | weighting functions are not too simple to allow the table to grow | |
5066 | without bounds. Instead one of the weighting factors is the size. | |
5067 | Therefore the result is always a good payoff between few collisions | |
5068 | (= short chain lengths) and table size. */ | |
5069 | static size_t | |
fdc90cb4 JJ |
5070 | compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes, |
5071 | unsigned long int nsyms, int gnu_hash) | |
5a580b3a AM |
5072 | { |
5073 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
5074 | size_t best_size = 0; | |
5a580b3a AM |
5075 | unsigned long int i; |
5076 | bfd_size_type amt; | |
5077 | ||
5a580b3a AM |
5078 | /* We have a problem here. The following code to optimize the table |
5079 | size requires an integer type with more the 32 bits. If | |
5080 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5081 | #ifdef BFD_HOST_U_64_BIT | |
5082 | if (info->optimize) | |
5083 | { | |
5a580b3a AM |
5084 | size_t minsize; |
5085 | size_t maxsize; | |
5086 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a AM |
5087 | bfd *dynobj = elf_hash_table (info)->dynobj; |
5088 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
fdc90cb4 | 5089 | unsigned long int *counts; |
5a580b3a AM |
5090 | |
5091 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5092 | that the hashing table must at least have NSYMS/4 and at most | |
5093 | 2*NSYMS buckets. */ | |
5094 | minsize = nsyms / 4; | |
5095 | if (minsize == 0) | |
5096 | minsize = 1; | |
5097 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5098 | if (gnu_hash) |
5099 | { | |
5100 | if (minsize < 2) | |
5101 | minsize = 2; | |
5102 | if ((best_size & 31) == 0) | |
5103 | ++best_size; | |
5104 | } | |
5a580b3a AM |
5105 | |
5106 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5107 | since the size could be large. */ | |
5108 | amt = maxsize; | |
5109 | amt *= sizeof (unsigned long int); | |
5110 | counts = bfd_malloc (amt); | |
5111 | if (counts == NULL) | |
fdc90cb4 | 5112 | return 0; |
5a580b3a AM |
5113 | |
5114 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5115 | minimal chain length. The minor criteria is (of course) the size | |
5116 | of the table. */ | |
5117 | for (i = minsize; i < maxsize; ++i) | |
5118 | { | |
5119 | /* Walk through the array of hashcodes and count the collisions. */ | |
5120 | BFD_HOST_U_64_BIT max; | |
5121 | unsigned long int j; | |
5122 | unsigned long int fact; | |
5123 | ||
fdc90cb4 JJ |
5124 | if (gnu_hash && (i & 31) == 0) |
5125 | continue; | |
5126 | ||
5a580b3a AM |
5127 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5128 | ||
5129 | /* Determine how often each hash bucket is used. */ | |
5130 | for (j = 0; j < nsyms; ++j) | |
5131 | ++counts[hashcodes[j] % i]; | |
5132 | ||
5133 | /* For the weight function we need some information about the | |
5134 | pagesize on the target. This is information need not be 100% | |
5135 | accurate. Since this information is not available (so far) we | |
5136 | define it here to a reasonable default value. If it is crucial | |
5137 | to have a better value some day simply define this value. */ | |
5138 | # ifndef BFD_TARGET_PAGESIZE | |
5139 | # define BFD_TARGET_PAGESIZE (4096) | |
5140 | # endif | |
5141 | ||
fdc90cb4 JJ |
5142 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5143 | and the chains. */ | |
5144 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5145 | |
5146 | # if 1 | |
5147 | /* Variant 1: optimize for short chains. We add the squares | |
5148 | of all the chain lengths (which favors many small chain | |
5149 | over a few long chains). */ | |
5150 | for (j = 0; j < i; ++j) | |
5151 | max += counts[j] * counts[j]; | |
5152 | ||
5153 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5154 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5155 | max *= fact * fact; |
5156 | # else | |
5157 | /* Variant 2: Optimize a lot more for small table. Here we | |
5158 | also add squares of the size but we also add penalties for | |
5159 | empty slots (the +1 term). */ | |
5160 | for (j = 0; j < i; ++j) | |
5161 | max += (1 + counts[j]) * (1 + counts[j]); | |
5162 | ||
5163 | /* The overall size of the table is considered, but not as | |
5164 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5165 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5166 | max *= fact; |
5167 | # endif | |
5168 | ||
5169 | /* Compare with current best results. */ | |
5170 | if (max < best_chlen) | |
5171 | { | |
5172 | best_chlen = max; | |
5173 | best_size = i; | |
5174 | } | |
5175 | } | |
5176 | ||
5177 | free (counts); | |
5178 | } | |
5179 | else | |
5180 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5181 | { | |
5182 | /* This is the fallback solution if no 64bit type is available or if we | |
5183 | are not supposed to spend much time on optimizations. We select the | |
5184 | bucket count using a fixed set of numbers. */ | |
5185 | for (i = 0; elf_buckets[i] != 0; i++) | |
5186 | { | |
5187 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5188 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5189 | break; |
5190 | } | |
fdc90cb4 JJ |
5191 | if (gnu_hash && best_size < 2) |
5192 | best_size = 2; | |
5a580b3a AM |
5193 | } |
5194 | ||
5a580b3a AM |
5195 | return best_size; |
5196 | } | |
5197 | ||
5198 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
5199 | called by the ELF linker emulation before_allocation routine. We | |
5200 | must set the sizes of the sections before the linker sets the | |
5201 | addresses of the various sections. */ | |
5202 | ||
5203 | bfd_boolean | |
5204 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5205 | const char *soname, | |
5206 | const char *rpath, | |
5207 | const char *filter_shlib, | |
5208 | const char * const *auxiliary_filters, | |
5209 | struct bfd_link_info *info, | |
5210 | asection **sinterpptr, | |
5211 | struct bfd_elf_version_tree *verdefs) | |
5212 | { | |
5213 | bfd_size_type soname_indx; | |
5214 | bfd *dynobj; | |
5215 | const struct elf_backend_data *bed; | |
5216 | struct elf_assign_sym_version_info asvinfo; | |
5217 | ||
5218 | *sinterpptr = NULL; | |
5219 | ||
5220 | soname_indx = (bfd_size_type) -1; | |
5221 | ||
5222 | if (!is_elf_hash_table (info->hash)) | |
5223 | return TRUE; | |
5224 | ||
8c37241b | 5225 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
5226 | if (info->execstack) |
5227 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
5228 | else if (info->noexecstack) | |
5229 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
5230 | else | |
5231 | { | |
5232 | bfd *inputobj; | |
5233 | asection *notesec = NULL; | |
5234 | int exec = 0; | |
5235 | ||
5236 | for (inputobj = info->input_bfds; | |
5237 | inputobj; | |
5238 | inputobj = inputobj->link_next) | |
5239 | { | |
5240 | asection *s; | |
5241 | ||
d457dcf6 | 5242 | if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) |
5a580b3a AM |
5243 | continue; |
5244 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5245 | if (s) | |
5246 | { | |
5247 | if (s->flags & SEC_CODE) | |
5248 | exec = PF_X; | |
5249 | notesec = s; | |
5250 | } | |
5251 | else | |
5252 | exec = PF_X; | |
5253 | } | |
5254 | if (notesec) | |
5255 | { | |
5256 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
5257 | if (exec && info->relocatable | |
5258 | && notesec->output_section != bfd_abs_section_ptr) | |
5259 | notesec->output_section->flags |= SEC_CODE; | |
5260 | } | |
5261 | } | |
5262 | ||
5263 | /* Any syms created from now on start with -1 in | |
5264 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
5265 | elf_hash_table (info)->init_got_refcount |
5266 | = elf_hash_table (info)->init_got_offset; | |
5267 | elf_hash_table (info)->init_plt_refcount | |
5268 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a AM |
5269 | |
5270 | /* The backend may have to create some sections regardless of whether | |
5271 | we're dynamic or not. */ | |
5272 | bed = get_elf_backend_data (output_bfd); | |
5273 | if (bed->elf_backend_always_size_sections | |
5274 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5275 | return FALSE; | |
5276 | ||
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 | ||
5284 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
5285 | return FALSE; | |
5286 | ||
5287 | if (elf_hash_table (info)->dynamic_sections_created) | |
5288 | { | |
5289 | struct elf_info_failed eif; | |
5290 | struct elf_link_hash_entry *h; | |
5291 | asection *dynstr; | |
5292 | struct bfd_elf_version_tree *t; | |
5293 | struct bfd_elf_version_expr *d; | |
046183de | 5294 | asection *s; |
5a580b3a AM |
5295 | bfd_boolean all_defined; |
5296 | ||
5297 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5298 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5299 | ||
5300 | if (soname != NULL) | |
5301 | { | |
5302 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5303 | soname, TRUE); | |
5304 | if (soname_indx == (bfd_size_type) -1 | |
5305 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5306 | return FALSE; | |
5307 | } | |
5308 | ||
5309 | if (info->symbolic) | |
5310 | { | |
5311 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5312 | return FALSE; | |
5313 | info->flags |= DF_SYMBOLIC; | |
5314 | } | |
5315 | ||
5316 | if (rpath != NULL) | |
5317 | { | |
5318 | bfd_size_type indx; | |
5319 | ||
5320 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5321 | TRUE); | |
5322 | if (indx == (bfd_size_type) -1 | |
5323 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5324 | return FALSE; | |
5325 | ||
5326 | if (info->new_dtags) | |
5327 | { | |
5328 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5329 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5330 | return FALSE; | |
5331 | } | |
5332 | } | |
5333 | ||
5334 | if (filter_shlib != NULL) | |
5335 | { | |
5336 | bfd_size_type indx; | |
5337 | ||
5338 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5339 | filter_shlib, TRUE); | |
5340 | if (indx == (bfd_size_type) -1 | |
5341 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5342 | return FALSE; | |
5343 | } | |
5344 | ||
5345 | if (auxiliary_filters != NULL) | |
5346 | { | |
5347 | const char * const *p; | |
5348 | ||
5349 | for (p = auxiliary_filters; *p != NULL; p++) | |
5350 | { | |
5351 | bfd_size_type indx; | |
5352 | ||
5353 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5354 | *p, TRUE); | |
5355 | if (indx == (bfd_size_type) -1 | |
5356 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5357 | return FALSE; | |
5358 | } | |
5359 | } | |
5360 | ||
5361 | eif.info = info; | |
5362 | eif.verdefs = verdefs; | |
5363 | eif.failed = FALSE; | |
5364 | ||
5365 | /* If we are supposed to export all symbols into the dynamic symbol | |
5366 | table (this is not the normal case), then do so. */ | |
55255dae L |
5367 | if (info->export_dynamic |
5368 | || (info->executable && info->dynamic)) | |
5a580b3a AM |
5369 | { |
5370 | elf_link_hash_traverse (elf_hash_table (info), | |
5371 | _bfd_elf_export_symbol, | |
5372 | &eif); | |
5373 | if (eif.failed) | |
5374 | return FALSE; | |
5375 | } | |
5376 | ||
5377 | /* Make all global versions with definition. */ | |
5378 | for (t = verdefs; t != NULL; t = t->next) | |
5379 | for (d = t->globals.list; d != NULL; d = d->next) | |
5380 | if (!d->symver && d->symbol) | |
5381 | { | |
5382 | const char *verstr, *name; | |
5383 | size_t namelen, verlen, newlen; | |
5384 | char *newname, *p; | |
5385 | struct elf_link_hash_entry *newh; | |
5386 | ||
5387 | name = d->symbol; | |
5388 | namelen = strlen (name); | |
5389 | verstr = t->name; | |
5390 | verlen = strlen (verstr); | |
5391 | newlen = namelen + verlen + 3; | |
5392 | ||
5393 | newname = bfd_malloc (newlen); | |
5394 | if (newname == NULL) | |
5395 | return FALSE; | |
5396 | memcpy (newname, name, namelen); | |
5397 | ||
5398 | /* Check the hidden versioned definition. */ | |
5399 | p = newname + namelen; | |
5400 | *p++ = ELF_VER_CHR; | |
5401 | memcpy (p, verstr, verlen + 1); | |
5402 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5403 | newname, FALSE, FALSE, | |
5404 | FALSE); | |
5405 | if (newh == NULL | |
5406 | || (newh->root.type != bfd_link_hash_defined | |
5407 | && newh->root.type != bfd_link_hash_defweak)) | |
5408 | { | |
5409 | /* Check the default versioned definition. */ | |
5410 | *p++ = ELF_VER_CHR; | |
5411 | memcpy (p, verstr, verlen + 1); | |
5412 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5413 | newname, FALSE, FALSE, | |
5414 | FALSE); | |
5415 | } | |
5416 | free (newname); | |
5417 | ||
5418 | /* Mark this version if there is a definition and it is | |
5419 | not defined in a shared object. */ | |
5420 | if (newh != NULL | |
f5385ebf | 5421 | && !newh->def_dynamic |
5a580b3a AM |
5422 | && (newh->root.type == bfd_link_hash_defined |
5423 | || newh->root.type == bfd_link_hash_defweak)) | |
5424 | d->symver = 1; | |
5425 | } | |
5426 | ||
5427 | /* Attach all the symbols to their version information. */ | |
5428 | asvinfo.output_bfd = output_bfd; | |
5429 | asvinfo.info = info; | |
5430 | asvinfo.verdefs = verdefs; | |
5431 | asvinfo.failed = FALSE; | |
5432 | ||
5433 | elf_link_hash_traverse (elf_hash_table (info), | |
5434 | _bfd_elf_link_assign_sym_version, | |
5435 | &asvinfo); | |
5436 | if (asvinfo.failed) | |
5437 | return FALSE; | |
5438 | ||
5439 | if (!info->allow_undefined_version) | |
5440 | { | |
5441 | /* Check if all global versions have a definition. */ | |
5442 | all_defined = TRUE; | |
5443 | for (t = verdefs; t != NULL; t = t->next) | |
5444 | for (d = t->globals.list; d != NULL; d = d->next) | |
5445 | if (!d->symver && !d->script) | |
5446 | { | |
5447 | (*_bfd_error_handler) | |
5448 | (_("%s: undefined version: %s"), | |
5449 | d->pattern, t->name); | |
5450 | all_defined = FALSE; | |
5451 | } | |
5452 | ||
5453 | if (!all_defined) | |
5454 | { | |
5455 | bfd_set_error (bfd_error_bad_value); | |
5456 | return FALSE; | |
5457 | } | |
5458 | } | |
5459 | ||
5460 | /* Find all symbols which were defined in a dynamic object and make | |
5461 | the backend pick a reasonable value for them. */ | |
5462 | elf_link_hash_traverse (elf_hash_table (info), | |
5463 | _bfd_elf_adjust_dynamic_symbol, | |
5464 | &eif); | |
5465 | if (eif.failed) | |
5466 | return FALSE; | |
5467 | ||
5468 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5469 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5470 | now so that we know the final size of the .dynamic section. */ |
5471 | ||
5472 | /* If there are initialization and/or finalization functions to | |
5473 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5474 | h = (info->init_function | |
5475 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5476 | info->init_function, FALSE, | |
5477 | FALSE, FALSE) | |
5478 | : NULL); | |
5479 | if (h != NULL | |
f5385ebf AM |
5480 | && (h->ref_regular |
5481 | || h->def_regular)) | |
5a580b3a AM |
5482 | { |
5483 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5484 | return FALSE; | |
5485 | } | |
5486 | h = (info->fini_function | |
5487 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5488 | info->fini_function, FALSE, | |
5489 | FALSE, FALSE) | |
5490 | : NULL); | |
5491 | if (h != NULL | |
f5385ebf AM |
5492 | && (h->ref_regular |
5493 | || h->def_regular)) | |
5a580b3a AM |
5494 | { |
5495 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5496 | return FALSE; | |
5497 | } | |
5498 | ||
046183de AM |
5499 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5500 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5501 | { |
5502 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5503 | if (! info->executable) | |
5504 | { | |
5505 | bfd *sub; | |
5506 | asection *o; | |
5507 | ||
5508 | for (sub = info->input_bfds; sub != NULL; | |
5509 | sub = sub->link_next) | |
5510 | for (o = sub->sections; o != NULL; o = o->next) | |
5511 | if (elf_section_data (o)->this_hdr.sh_type | |
5512 | == SHT_PREINIT_ARRAY) | |
5513 | { | |
5514 | (*_bfd_error_handler) | |
d003868e AM |
5515 | (_("%B: .preinit_array section is not allowed in DSO"), |
5516 | sub); | |
5a580b3a AM |
5517 | break; |
5518 | } | |
5519 | ||
5520 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5521 | return FALSE; | |
5522 | } | |
5523 | ||
5524 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5525 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5526 | return FALSE; | |
5527 | } | |
046183de AM |
5528 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5529 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5530 | { |
5531 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5532 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5533 | return FALSE; | |
5534 | } | |
046183de AM |
5535 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5536 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5537 | { |
5538 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5539 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5540 | return FALSE; | |
5541 | } | |
5542 | ||
5543 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5544 | /* If .dynstr is excluded from the link, we don't want any of | |
5545 | these tags. Strictly, we should be checking each section | |
5546 | individually; This quick check covers for the case where | |
5547 | someone does a /DISCARD/ : { *(*) }. */ | |
5548 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5549 | { | |
5550 | bfd_size_type strsize; | |
5551 | ||
5552 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
5553 | if ((info->emit_hash |
5554 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
5555 | || (info->emit_gnu_hash | |
5556 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
5557 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
5558 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5559 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5560 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5561 | bed->s->sizeof_sym)) | |
5562 | return FALSE; | |
5563 | } | |
5564 | } | |
5565 | ||
5566 | /* The backend must work out the sizes of all the other dynamic | |
5567 | sections. */ | |
5568 | if (bed->elf_backend_size_dynamic_sections | |
5569 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5570 | return FALSE; | |
5571 | ||
5572 | if (elf_hash_table (info)->dynamic_sections_created) | |
5573 | { | |
554220db | 5574 | unsigned long section_sym_count; |
5a580b3a | 5575 | asection *s; |
5a580b3a AM |
5576 | |
5577 | /* Set up the version definition section. */ | |
5578 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5579 | BFD_ASSERT (s != NULL); | |
5580 | ||
5581 | /* We may have created additional version definitions if we are | |
5582 | just linking a regular application. */ | |
5583 | verdefs = asvinfo.verdefs; | |
5584 | ||
5585 | /* Skip anonymous version tag. */ | |
5586 | if (verdefs != NULL && verdefs->vernum == 0) | |
5587 | verdefs = verdefs->next; | |
5588 | ||
3e3b46e5 | 5589 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5590 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5591 | else |
5592 | { | |
5593 | unsigned int cdefs; | |
5594 | bfd_size_type size; | |
5595 | struct bfd_elf_version_tree *t; | |
5596 | bfd_byte *p; | |
5597 | Elf_Internal_Verdef def; | |
5598 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5599 | struct bfd_link_hash_entry *bh; |
5600 | struct elf_link_hash_entry *h; | |
5601 | const char *name; | |
5a580b3a AM |
5602 | |
5603 | cdefs = 0; | |
5604 | size = 0; | |
5605 | ||
5606 | /* Make space for the base version. */ | |
5607 | size += sizeof (Elf_External_Verdef); | |
5608 | size += sizeof (Elf_External_Verdaux); | |
5609 | ++cdefs; | |
5610 | ||
3e3b46e5 PB |
5611 | /* Make space for the default version. */ |
5612 | if (info->create_default_symver) | |
5613 | { | |
5614 | size += sizeof (Elf_External_Verdef); | |
5615 | ++cdefs; | |
5616 | } | |
5617 | ||
5a580b3a AM |
5618 | for (t = verdefs; t != NULL; t = t->next) |
5619 | { | |
5620 | struct bfd_elf_version_deps *n; | |
5621 | ||
5622 | size += sizeof (Elf_External_Verdef); | |
5623 | size += sizeof (Elf_External_Verdaux); | |
5624 | ++cdefs; | |
5625 | ||
5626 | for (n = t->deps; n != NULL; n = n->next) | |
5627 | size += sizeof (Elf_External_Verdaux); | |
5628 | } | |
5629 | ||
eea6121a AM |
5630 | s->size = size; |
5631 | s->contents = bfd_alloc (output_bfd, s->size); | |
5632 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5633 | return FALSE; |
5634 | ||
5635 | /* Fill in the version definition section. */ | |
5636 | ||
5637 | p = s->contents; | |
5638 | ||
5639 | def.vd_version = VER_DEF_CURRENT; | |
5640 | def.vd_flags = VER_FLG_BASE; | |
5641 | def.vd_ndx = 1; | |
5642 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5643 | if (info->create_default_symver) |
5644 | { | |
5645 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5646 | def.vd_next = sizeof (Elf_External_Verdef); | |
5647 | } | |
5648 | else | |
5649 | { | |
5650 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5651 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5652 | + sizeof (Elf_External_Verdaux)); | |
5653 | } | |
5a580b3a AM |
5654 | |
5655 | if (soname_indx != (bfd_size_type) -1) | |
5656 | { | |
5657 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5658 | soname_indx); | |
5659 | def.vd_hash = bfd_elf_hash (soname); | |
5660 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5661 | name = soname; |
5a580b3a AM |
5662 | } |
5663 | else | |
5664 | { | |
5a580b3a AM |
5665 | bfd_size_type indx; |
5666 | ||
06084812 | 5667 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5668 | def.vd_hash = bfd_elf_hash (name); |
5669 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5670 | name, FALSE); | |
5671 | if (indx == (bfd_size_type) -1) | |
5672 | return FALSE; | |
5673 | defaux.vda_name = indx; | |
5674 | } | |
5675 | defaux.vda_next = 0; | |
5676 | ||
5677 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5678 | (Elf_External_Verdef *) p); | |
5679 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5680 | if (info->create_default_symver) |
5681 | { | |
5682 | /* Add a symbol representing this version. */ | |
5683 | bh = NULL; | |
5684 | if (! (_bfd_generic_link_add_one_symbol | |
5685 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5686 | 0, NULL, FALSE, | |
5687 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5688 | return FALSE; | |
5689 | h = (struct elf_link_hash_entry *) bh; | |
5690 | h->non_elf = 0; | |
5691 | h->def_regular = 1; | |
5692 | h->type = STT_OBJECT; | |
5693 | h->verinfo.vertree = NULL; | |
5694 | ||
5695 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5696 | return FALSE; | |
5697 | ||
5698 | /* Create a duplicate of the base version with the same | |
5699 | aux block, but different flags. */ | |
5700 | def.vd_flags = 0; | |
5701 | def.vd_ndx = 2; | |
5702 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5703 | if (verdefs) | |
5704 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5705 | + sizeof (Elf_External_Verdaux)); | |
5706 | else | |
5707 | def.vd_next = 0; | |
5708 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5709 | (Elf_External_Verdef *) p); | |
5710 | p += sizeof (Elf_External_Verdef); | |
5711 | } | |
5a580b3a AM |
5712 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5713 | (Elf_External_Verdaux *) p); | |
5714 | p += sizeof (Elf_External_Verdaux); | |
5715 | ||
5716 | for (t = verdefs; t != NULL; t = t->next) | |
5717 | { | |
5718 | unsigned int cdeps; | |
5719 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5720 | |
5721 | cdeps = 0; | |
5722 | for (n = t->deps; n != NULL; n = n->next) | |
5723 | ++cdeps; | |
5724 | ||
5725 | /* Add a symbol representing this version. */ | |
5726 | bh = NULL; | |
5727 | if (! (_bfd_generic_link_add_one_symbol | |
5728 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5729 | 0, NULL, FALSE, | |
5730 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5731 | return FALSE; | |
5732 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5733 | h->non_elf = 0; |
5734 | h->def_regular = 1; | |
5a580b3a AM |
5735 | h->type = STT_OBJECT; |
5736 | h->verinfo.vertree = t; | |
5737 | ||
c152c796 | 5738 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5739 | return FALSE; |
5740 | ||
5741 | def.vd_version = VER_DEF_CURRENT; | |
5742 | def.vd_flags = 0; | |
5743 | if (t->globals.list == NULL | |
5744 | && t->locals.list == NULL | |
5745 | && ! t->used) | |
5746 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5747 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5748 | def.vd_cnt = cdeps + 1; |
5749 | def.vd_hash = bfd_elf_hash (t->name); | |
5750 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5751 | def.vd_next = 0; | |
5752 | if (t->next != NULL) | |
5753 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5754 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5755 | ||
5756 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5757 | (Elf_External_Verdef *) p); | |
5758 | p += sizeof (Elf_External_Verdef); | |
5759 | ||
5760 | defaux.vda_name = h->dynstr_index; | |
5761 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5762 | h->dynstr_index); | |
5763 | defaux.vda_next = 0; | |
5764 | if (t->deps != NULL) | |
5765 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5766 | t->name_indx = defaux.vda_name; | |
5767 | ||
5768 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5769 | (Elf_External_Verdaux *) p); | |
5770 | p += sizeof (Elf_External_Verdaux); | |
5771 | ||
5772 | for (n = t->deps; n != NULL; n = n->next) | |
5773 | { | |
5774 | if (n->version_needed == NULL) | |
5775 | { | |
5776 | /* This can happen if there was an error in the | |
5777 | version script. */ | |
5778 | defaux.vda_name = 0; | |
5779 | } | |
5780 | else | |
5781 | { | |
5782 | defaux.vda_name = n->version_needed->name_indx; | |
5783 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5784 | defaux.vda_name); | |
5785 | } | |
5786 | if (n->next == NULL) | |
5787 | defaux.vda_next = 0; | |
5788 | else | |
5789 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5790 | ||
5791 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5792 | (Elf_External_Verdaux *) p); | |
5793 | p += sizeof (Elf_External_Verdaux); | |
5794 | } | |
5795 | } | |
5796 | ||
5797 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5798 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5799 | return FALSE; | |
5800 | ||
5801 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5802 | } | |
5803 | ||
5804 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5805 | { | |
5806 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5807 | return FALSE; | |
5808 | } | |
5809 | else if (info->flags & DF_BIND_NOW) | |
5810 | { | |
5811 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5812 | return FALSE; | |
5813 | } | |
5814 | ||
5815 | if (info->flags_1) | |
5816 | { | |
5817 | if (info->executable) | |
5818 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5819 | | DF_1_NODELETE | |
5820 | | DF_1_NOOPEN); | |
5821 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5822 | return FALSE; | |
5823 | } | |
5824 | ||
5825 | /* Work out the size of the version reference section. */ | |
5826 | ||
5827 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5828 | BFD_ASSERT (s != NULL); | |
5829 | { | |
5830 | struct elf_find_verdep_info sinfo; | |
5831 | ||
5832 | sinfo.output_bfd = output_bfd; | |
5833 | sinfo.info = info; | |
5834 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5835 | if (sinfo.vers == 0) | |
5836 | sinfo.vers = 1; | |
5837 | sinfo.failed = FALSE; | |
5838 | ||
5839 | elf_link_hash_traverse (elf_hash_table (info), | |
5840 | _bfd_elf_link_find_version_dependencies, | |
5841 | &sinfo); | |
5842 | ||
5843 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 5844 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5845 | else |
5846 | { | |
5847 | Elf_Internal_Verneed *t; | |
5848 | unsigned int size; | |
5849 | unsigned int crefs; | |
5850 | bfd_byte *p; | |
5851 | ||
5852 | /* Build the version definition section. */ | |
5853 | size = 0; | |
5854 | crefs = 0; | |
5855 | for (t = elf_tdata (output_bfd)->verref; | |
5856 | t != NULL; | |
5857 | t = t->vn_nextref) | |
5858 | { | |
5859 | Elf_Internal_Vernaux *a; | |
5860 | ||
5861 | size += sizeof (Elf_External_Verneed); | |
5862 | ++crefs; | |
5863 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5864 | size += sizeof (Elf_External_Vernaux); | |
5865 | } | |
5866 | ||
eea6121a AM |
5867 | s->size = size; |
5868 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5869 | if (s->contents == NULL) |
5870 | return FALSE; | |
5871 | ||
5872 | p = s->contents; | |
5873 | for (t = elf_tdata (output_bfd)->verref; | |
5874 | t != NULL; | |
5875 | t = t->vn_nextref) | |
5876 | { | |
5877 | unsigned int caux; | |
5878 | Elf_Internal_Vernaux *a; | |
5879 | bfd_size_type indx; | |
5880 | ||
5881 | caux = 0; | |
5882 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5883 | ++caux; | |
5884 | ||
5885 | t->vn_version = VER_NEED_CURRENT; | |
5886 | t->vn_cnt = caux; | |
5887 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5888 | elf_dt_name (t->vn_bfd) != NULL | |
5889 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 5890 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
5891 | FALSE); |
5892 | if (indx == (bfd_size_type) -1) | |
5893 | return FALSE; | |
5894 | t->vn_file = indx; | |
5895 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5896 | if (t->vn_nextref == NULL) | |
5897 | t->vn_next = 0; | |
5898 | else | |
5899 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5900 | + caux * sizeof (Elf_External_Vernaux)); | |
5901 | ||
5902 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5903 | (Elf_External_Verneed *) p); | |
5904 | p += sizeof (Elf_External_Verneed); | |
5905 | ||
5906 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5907 | { | |
5908 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5909 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5910 | a->vna_nodename, FALSE); | |
5911 | if (indx == (bfd_size_type) -1) | |
5912 | return FALSE; | |
5913 | a->vna_name = indx; | |
5914 | if (a->vna_nextptr == NULL) | |
5915 | a->vna_next = 0; | |
5916 | else | |
5917 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5918 | ||
5919 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5920 | (Elf_External_Vernaux *) p); | |
5921 | p += sizeof (Elf_External_Vernaux); | |
5922 | } | |
5923 | } | |
5924 | ||
5925 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5926 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5927 | return FALSE; | |
5928 | ||
5929 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5930 | } | |
5931 | } | |
5932 | ||
8423293d AM |
5933 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
5934 | && elf_tdata (output_bfd)->cverdefs == 0) | |
5935 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
5936 | §ion_sym_count) == 0) | |
5937 | { | |
5938 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5939 | s->flags |= SEC_EXCLUDE; | |
5940 | } | |
5941 | } | |
5942 | return TRUE; | |
5943 | } | |
5944 | ||
74541ad4 AM |
5945 | /* Find the first non-excluded output section. We'll use its |
5946 | section symbol for some emitted relocs. */ | |
5947 | void | |
5948 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
5949 | { | |
5950 | asection *s; | |
5951 | ||
5952 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
5953 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
5954 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
5955 | { | |
5956 | elf_hash_table (info)->text_index_section = s; | |
5957 | break; | |
5958 | } | |
5959 | } | |
5960 | ||
5961 | /* Find two non-excluded output sections, one for code, one for data. | |
5962 | We'll use their section symbols for some emitted relocs. */ | |
5963 | void | |
5964 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
5965 | { | |
5966 | asection *s; | |
5967 | ||
5968 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
5969 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) | |
5970 | == (SEC_ALLOC | SEC_READONLY)) | |
5971 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
5972 | { | |
5973 | elf_hash_table (info)->text_index_section = s; | |
5974 | break; | |
5975 | } | |
5976 | ||
5977 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
5978 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) | |
5979 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
5980 | { | |
5981 | elf_hash_table (info)->data_index_section = s; | |
5982 | break; | |
5983 | } | |
5984 | ||
5985 | if (elf_hash_table (info)->text_index_section == NULL) | |
5986 | elf_hash_table (info)->text_index_section | |
5987 | = elf_hash_table (info)->data_index_section; | |
5988 | } | |
5989 | ||
8423293d AM |
5990 | bfd_boolean |
5991 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5992 | { | |
74541ad4 AM |
5993 | const struct elf_backend_data *bed; |
5994 | ||
8423293d AM |
5995 | if (!is_elf_hash_table (info->hash)) |
5996 | return TRUE; | |
5997 | ||
74541ad4 AM |
5998 | bed = get_elf_backend_data (output_bfd); |
5999 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6000 | ||
8423293d AM |
6001 | if (elf_hash_table (info)->dynamic_sections_created) |
6002 | { | |
6003 | bfd *dynobj; | |
8423293d AM |
6004 | asection *s; |
6005 | bfd_size_type dynsymcount; | |
6006 | unsigned long section_sym_count; | |
8423293d AM |
6007 | unsigned int dtagcount; |
6008 | ||
6009 | dynobj = elf_hash_table (info)->dynobj; | |
6010 | ||
5a580b3a AM |
6011 | /* Assign dynsym indicies. In a shared library we generate a |
6012 | section symbol for each output section, which come first. | |
6013 | Next come all of the back-end allocated local dynamic syms, | |
6014 | followed by the rest of the global symbols. */ | |
6015 | ||
554220db AM |
6016 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6017 | §ion_sym_count); | |
5a580b3a AM |
6018 | |
6019 | /* Work out the size of the symbol version section. */ | |
6020 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6021 | BFD_ASSERT (s != NULL); | |
8423293d AM |
6022 | if (dynsymcount != 0 |
6023 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 6024 | { |
eea6121a AM |
6025 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
6026 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
6027 | if (s->contents == NULL) |
6028 | return FALSE; | |
6029 | ||
6030 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6031 | return FALSE; | |
6032 | } | |
6033 | ||
6034 | /* Set the size of the .dynsym and .hash sections. We counted | |
6035 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6036 | We will build the contents of .dynsym and .hash when we build | |
6037 | the final symbol table, because until then we do not know the | |
6038 | correct value to give the symbols. We built the .dynstr | |
6039 | section as we went along in elf_link_add_object_symbols. */ | |
6040 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
6041 | BFD_ASSERT (s != NULL); | |
eea6121a | 6042 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
6043 | |
6044 | if (dynsymcount != 0) | |
6045 | { | |
554220db AM |
6046 | s->contents = bfd_alloc (output_bfd, s->size); |
6047 | if (s->contents == NULL) | |
6048 | return FALSE; | |
5a580b3a | 6049 | |
554220db AM |
6050 | /* The first entry in .dynsym is a dummy symbol. |
6051 | Clear all the section syms, in case we don't output them all. */ | |
6052 | ++section_sym_count; | |
6053 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
6054 | } |
6055 | ||
fdc90cb4 JJ |
6056 | elf_hash_table (info)->bucketcount = 0; |
6057 | ||
5a580b3a AM |
6058 | /* Compute the size of the hashing table. As a side effect this |
6059 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6060 | if (info->emit_hash) |
6061 | { | |
6062 | unsigned long int *hashcodes; | |
6063 | unsigned long int *hashcodesp; | |
6064 | bfd_size_type amt; | |
6065 | unsigned long int nsyms; | |
6066 | size_t bucketcount; | |
6067 | size_t hash_entry_size; | |
6068 | ||
6069 | /* Compute the hash values for all exported symbols. At the same | |
6070 | time store the values in an array so that we could use them for | |
6071 | optimizations. */ | |
6072 | amt = dynsymcount * sizeof (unsigned long int); | |
6073 | hashcodes = bfd_malloc (amt); | |
6074 | if (hashcodes == NULL) | |
6075 | return FALSE; | |
6076 | hashcodesp = hashcodes; | |
5a580b3a | 6077 | |
fdc90cb4 JJ |
6078 | /* Put all hash values in HASHCODES. */ |
6079 | elf_link_hash_traverse (elf_hash_table (info), | |
6080 | elf_collect_hash_codes, &hashcodesp); | |
5a580b3a | 6081 | |
fdc90cb4 JJ |
6082 | nsyms = hashcodesp - hashcodes; |
6083 | bucketcount | |
6084 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6085 | free (hashcodes); | |
6086 | ||
6087 | if (bucketcount == 0) | |
6088 | return FALSE; | |
5a580b3a | 6089 | |
fdc90cb4 JJ |
6090 | elf_hash_table (info)->bucketcount = bucketcount; |
6091 | ||
6092 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
6093 | BFD_ASSERT (s != NULL); | |
6094 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6095 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
6096 | s->contents = bfd_zalloc (output_bfd, s->size); | |
6097 | if (s->contents == NULL) | |
6098 | return FALSE; | |
6099 | ||
6100 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6101 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6102 | s->contents + hash_entry_size); | |
6103 | } | |
6104 | ||
6105 | if (info->emit_gnu_hash) | |
6106 | { | |
6107 | size_t i, cnt; | |
6108 | unsigned char *contents; | |
6109 | struct collect_gnu_hash_codes cinfo; | |
6110 | bfd_size_type amt; | |
6111 | size_t bucketcount; | |
6112 | ||
6113 | memset (&cinfo, 0, sizeof (cinfo)); | |
6114 | ||
6115 | /* Compute the hash values for all exported symbols. At the same | |
6116 | time store the values in an array so that we could use them for | |
6117 | optimizations. */ | |
6118 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
6119 | cinfo.hashcodes = bfd_malloc (amt); | |
6120 | if (cinfo.hashcodes == NULL) | |
6121 | return FALSE; | |
6122 | ||
6123 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6124 | cinfo.min_dynindx = -1; | |
6125 | cinfo.output_bfd = output_bfd; | |
6126 | cinfo.bed = bed; | |
6127 | ||
6128 | /* Put all hash values in HASHCODES. */ | |
6129 | elf_link_hash_traverse (elf_hash_table (info), | |
6130 | elf_collect_gnu_hash_codes, &cinfo); | |
6131 | ||
6132 | bucketcount | |
6133 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6134 | ||
6135 | if (bucketcount == 0) | |
6136 | { | |
6137 | free (cinfo.hashcodes); | |
6138 | return FALSE; | |
6139 | } | |
6140 | ||
6141 | s = bfd_get_section_by_name (dynobj, ".gnu.hash"); | |
6142 | BFD_ASSERT (s != NULL); | |
6143 | ||
6144 | if (cinfo.nsyms == 0) | |
6145 | { | |
6146 | /* Empty .gnu.hash section is special. */ | |
6147 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6148 | free (cinfo.hashcodes); | |
6149 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
6150 | contents = bfd_zalloc (output_bfd, s->size); | |
6151 | if (contents == NULL) | |
6152 | return FALSE; | |
6153 | s->contents = contents; | |
6154 | /* 1 empty bucket. */ | |
6155 | bfd_put_32 (output_bfd, 1, contents); | |
6156 | /* SYMIDX above the special symbol 0. */ | |
6157 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6158 | /* Just one word for bitmask. */ | |
6159 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6160 | /* Only hash fn bloom filter. */ | |
6161 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6162 | /* No hashes are valid - empty bitmask. */ | |
6163 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6164 | /* No hashes in the only bucket. */ | |
6165 | bfd_put_32 (output_bfd, 0, | |
6166 | contents + 16 + bed->s->arch_size / 8); | |
6167 | } | |
6168 | else | |
6169 | { | |
fdc90cb4 | 6170 | unsigned long int maskwords, maskbitslog2; |
0b33793d | 6171 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 JJ |
6172 | |
6173 | maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1; | |
6174 | if (maskbitslog2 < 3) | |
6175 | maskbitslog2 = 5; | |
6176 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6177 | maskbitslog2 = maskbitslog2 + 3; | |
6178 | else | |
6179 | maskbitslog2 = maskbitslog2 + 2; | |
6180 | if (bed->s->arch_size == 64) | |
6181 | { | |
6182 | if (maskbitslog2 == 5) | |
6183 | maskbitslog2 = 6; | |
6184 | cinfo.shift1 = 6; | |
6185 | } | |
6186 | else | |
6187 | cinfo.shift1 = 5; | |
6188 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6189 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6190 | cinfo.maskbits = 1 << maskbitslog2; |
6191 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6192 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6193 | amt += maskwords * sizeof (bfd_vma); | |
6194 | cinfo.bitmask = bfd_malloc (amt); | |
6195 | if (cinfo.bitmask == NULL) | |
6196 | { | |
6197 | free (cinfo.hashcodes); | |
6198 | return FALSE; | |
6199 | } | |
6200 | ||
6201 | cinfo.counts = (void *) (cinfo.bitmask + maskwords); | |
6202 | cinfo.indx = cinfo.counts + bucketcount; | |
6203 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6204 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6205 | ||
6206 | /* Determine how often each hash bucket is used. */ | |
6207 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6208 | for (i = 0; i < cinfo.nsyms; ++i) | |
6209 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6210 | ||
6211 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6212 | if (cinfo.counts[i] != 0) | |
6213 | { | |
6214 | cinfo.indx[i] = cnt; | |
6215 | cnt += cinfo.counts[i]; | |
6216 | } | |
6217 | BFD_ASSERT (cnt == dynsymcount); | |
6218 | cinfo.bucketcount = bucketcount; | |
6219 | cinfo.local_indx = cinfo.min_dynindx; | |
6220 | ||
6221 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6222 | s->size += cinfo.maskbits / 8; | |
6223 | contents = bfd_zalloc (output_bfd, s->size); | |
6224 | if (contents == NULL) | |
6225 | { | |
6226 | free (cinfo.bitmask); | |
6227 | free (cinfo.hashcodes); | |
6228 | return FALSE; | |
6229 | } | |
6230 | ||
6231 | s->contents = contents; | |
6232 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6233 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6234 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6235 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6236 | contents += 16 + cinfo.maskbits / 8; | |
6237 | ||
6238 | for (i = 0; i < bucketcount; ++i) | |
6239 | { | |
6240 | if (cinfo.counts[i] == 0) | |
6241 | bfd_put_32 (output_bfd, 0, contents); | |
6242 | else | |
6243 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6244 | contents += 4; | |
6245 | } | |
6246 | ||
6247 | cinfo.contents = contents; | |
6248 | ||
6249 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6250 | elf_link_hash_traverse (elf_hash_table (info), | |
6251 | elf_renumber_gnu_hash_syms, &cinfo); | |
6252 | ||
6253 | contents = s->contents + 16; | |
6254 | for (i = 0; i < maskwords; ++i) | |
6255 | { | |
6256 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6257 | contents); | |
6258 | contents += bed->s->arch_size / 8; | |
6259 | } | |
6260 | ||
6261 | free (cinfo.bitmask); | |
6262 | free (cinfo.hashcodes); | |
6263 | } | |
6264 | } | |
5a580b3a AM |
6265 | |
6266 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
6267 | BFD_ASSERT (s != NULL); | |
6268 | ||
4ad4eba5 | 6269 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6270 | |
eea6121a | 6271 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6272 | |
6273 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6274 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6275 | return FALSE; | |
6276 | } | |
6277 | ||
6278 | return TRUE; | |
6279 | } | |
c152c796 AM |
6280 | |
6281 | /* Final phase of ELF linker. */ | |
6282 | ||
6283 | /* A structure we use to avoid passing large numbers of arguments. */ | |
6284 | ||
6285 | struct elf_final_link_info | |
6286 | { | |
6287 | /* General link information. */ | |
6288 | struct bfd_link_info *info; | |
6289 | /* Output BFD. */ | |
6290 | bfd *output_bfd; | |
6291 | /* Symbol string table. */ | |
6292 | struct bfd_strtab_hash *symstrtab; | |
6293 | /* .dynsym section. */ | |
6294 | asection *dynsym_sec; | |
6295 | /* .hash section. */ | |
6296 | asection *hash_sec; | |
6297 | /* symbol version section (.gnu.version). */ | |
6298 | asection *symver_sec; | |
6299 | /* Buffer large enough to hold contents of any section. */ | |
6300 | bfd_byte *contents; | |
6301 | /* Buffer large enough to hold external relocs of any section. */ | |
6302 | void *external_relocs; | |
6303 | /* Buffer large enough to hold internal relocs of any section. */ | |
6304 | Elf_Internal_Rela *internal_relocs; | |
6305 | /* Buffer large enough to hold external local symbols of any input | |
6306 | BFD. */ | |
6307 | bfd_byte *external_syms; | |
6308 | /* And a buffer for symbol section indices. */ | |
6309 | Elf_External_Sym_Shndx *locsym_shndx; | |
6310 | /* Buffer large enough to hold internal local symbols of any input | |
6311 | BFD. */ | |
6312 | Elf_Internal_Sym *internal_syms; | |
6313 | /* Array large enough to hold a symbol index for each local symbol | |
6314 | of any input BFD. */ | |
6315 | long *indices; | |
6316 | /* Array large enough to hold a section pointer for each local | |
6317 | symbol of any input BFD. */ | |
6318 | asection **sections; | |
6319 | /* Buffer to hold swapped out symbols. */ | |
6320 | bfd_byte *symbuf; | |
6321 | /* And one for symbol section indices. */ | |
6322 | Elf_External_Sym_Shndx *symshndxbuf; | |
6323 | /* Number of swapped out symbols in buffer. */ | |
6324 | size_t symbuf_count; | |
6325 | /* Number of symbols which fit in symbuf. */ | |
6326 | size_t symbuf_size; | |
6327 | /* And same for symshndxbuf. */ | |
6328 | size_t shndxbuf_size; | |
6329 | }; | |
6330 | ||
6331 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
6332 | ||
6333 | struct elf_outext_info | |
6334 | { | |
6335 | bfd_boolean failed; | |
6336 | bfd_boolean localsyms; | |
6337 | struct elf_final_link_info *finfo; | |
6338 | }; | |
6339 | ||
6340 | /* When performing a relocatable link, the input relocations are | |
6341 | preserved. But, if they reference global symbols, the indices | |
6342 | referenced must be updated. Update all the relocations in | |
6343 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
6344 | ||
6345 | static void | |
6346 | elf_link_adjust_relocs (bfd *abfd, | |
6347 | Elf_Internal_Shdr *rel_hdr, | |
6348 | unsigned int count, | |
6349 | struct elf_link_hash_entry **rel_hash) | |
6350 | { | |
6351 | unsigned int i; | |
6352 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6353 | bfd_byte *erela; | |
6354 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
6355 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
6356 | bfd_vma r_type_mask; | |
6357 | int r_sym_shift; | |
6358 | ||
6359 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
6360 | { | |
6361 | swap_in = bed->s->swap_reloc_in; | |
6362 | swap_out = bed->s->swap_reloc_out; | |
6363 | } | |
6364 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
6365 | { | |
6366 | swap_in = bed->s->swap_reloca_in; | |
6367 | swap_out = bed->s->swap_reloca_out; | |
6368 | } | |
6369 | else | |
6370 | abort (); | |
6371 | ||
6372 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
6373 | abort (); | |
6374 | ||
6375 | if (bed->s->arch_size == 32) | |
6376 | { | |
6377 | r_type_mask = 0xff; | |
6378 | r_sym_shift = 8; | |
6379 | } | |
6380 | else | |
6381 | { | |
6382 | r_type_mask = 0xffffffff; | |
6383 | r_sym_shift = 32; | |
6384 | } | |
6385 | ||
6386 | erela = rel_hdr->contents; | |
6387 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
6388 | { | |
6389 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
6390 | unsigned int j; | |
6391 | ||
6392 | if (*rel_hash == NULL) | |
6393 | continue; | |
6394 | ||
6395 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
6396 | ||
6397 | (*swap_in) (abfd, erela, irela); | |
6398 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
6399 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
6400 | | (irela[j].r_info & r_type_mask)); | |
6401 | (*swap_out) (abfd, irela, erela); | |
6402 | } | |
6403 | } | |
6404 | ||
6405 | struct elf_link_sort_rela | |
6406 | { | |
6407 | union { | |
6408 | bfd_vma offset; | |
6409 | bfd_vma sym_mask; | |
6410 | } u; | |
6411 | enum elf_reloc_type_class type; | |
6412 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
6413 | Elf_Internal_Rela rela[1]; | |
6414 | }; | |
6415 | ||
6416 | static int | |
6417 | elf_link_sort_cmp1 (const void *A, const void *B) | |
6418 | { | |
6419 | const struct elf_link_sort_rela *a = A; | |
6420 | const struct elf_link_sort_rela *b = B; | |
6421 | int relativea, relativeb; | |
6422 | ||
6423 | relativea = a->type == reloc_class_relative; | |
6424 | relativeb = b->type == reloc_class_relative; | |
6425 | ||
6426 | if (relativea < relativeb) | |
6427 | return 1; | |
6428 | if (relativea > relativeb) | |
6429 | return -1; | |
6430 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
6431 | return -1; | |
6432 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
6433 | return 1; | |
6434 | if (a->rela->r_offset < b->rela->r_offset) | |
6435 | return -1; | |
6436 | if (a->rela->r_offset > b->rela->r_offset) | |
6437 | return 1; | |
6438 | return 0; | |
6439 | } | |
6440 | ||
6441 | static int | |
6442 | elf_link_sort_cmp2 (const void *A, const void *B) | |
6443 | { | |
6444 | const struct elf_link_sort_rela *a = A; | |
6445 | const struct elf_link_sort_rela *b = B; | |
6446 | int copya, copyb; | |
6447 | ||
6448 | if (a->u.offset < b->u.offset) | |
6449 | return -1; | |
6450 | if (a->u.offset > b->u.offset) | |
6451 | return 1; | |
6452 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
6453 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
6454 | if (copya < copyb) | |
6455 | return -1; | |
6456 | if (copya > copyb) | |
6457 | return 1; | |
6458 | if (a->rela->r_offset < b->rela->r_offset) | |
6459 | return -1; | |
6460 | if (a->rela->r_offset > b->rela->r_offset) | |
6461 | return 1; | |
6462 | return 0; | |
6463 | } | |
6464 | ||
6465 | static size_t | |
6466 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
6467 | { | |
6468 | asection *reldyn; | |
6469 | bfd_size_type count, size; | |
6470 | size_t i, ret, sort_elt, ext_size; | |
6471 | bfd_byte *sort, *s_non_relative, *p; | |
6472 | struct elf_link_sort_rela *sq; | |
6473 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6474 | int i2e = bed->s->int_rels_per_ext_rel; | |
6475 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
6476 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
6477 | struct bfd_link_order *lo; | |
6478 | bfd_vma r_sym_mask; | |
6479 | ||
6480 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
eea6121a | 6481 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
6482 | { |
6483 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
eea6121a | 6484 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
6485 | return 0; |
6486 | ext_size = bed->s->sizeof_rel; | |
6487 | swap_in = bed->s->swap_reloc_in; | |
6488 | swap_out = bed->s->swap_reloc_out; | |
6489 | } | |
6490 | else | |
6491 | { | |
6492 | ext_size = bed->s->sizeof_rela; | |
6493 | swap_in = bed->s->swap_reloca_in; | |
6494 | swap_out = bed->s->swap_reloca_out; | |
6495 | } | |
eea6121a | 6496 | count = reldyn->size / ext_size; |
c152c796 AM |
6497 | |
6498 | size = 0; | |
8423293d | 6499 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6500 | if (lo->type == bfd_indirect_link_order) |
6501 | { | |
6502 | asection *o = lo->u.indirect.section; | |
eea6121a | 6503 | size += o->size; |
c152c796 AM |
6504 | } |
6505 | ||
eea6121a | 6506 | if (size != reldyn->size) |
c152c796 AM |
6507 | return 0; |
6508 | ||
6509 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
6510 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
6511 | sort = bfd_zmalloc (sort_elt * count); | |
6512 | if (sort == NULL) | |
6513 | { | |
6514 | (*info->callbacks->warning) | |
6515 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
6516 | return 0; | |
6517 | } | |
6518 | ||
6519 | if (bed->s->arch_size == 32) | |
6520 | r_sym_mask = ~(bfd_vma) 0xff; | |
6521 | else | |
6522 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
6523 | ||
8423293d | 6524 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6525 | if (lo->type == bfd_indirect_link_order) |
6526 | { | |
6527 | bfd_byte *erel, *erelend; | |
6528 | asection *o = lo->u.indirect.section; | |
6529 | ||
1da212d6 AM |
6530 | if (o->contents == NULL && o->size != 0) |
6531 | { | |
6532 | /* This is a reloc section that is being handled as a normal | |
6533 | section. See bfd_section_from_shdr. We can't combine | |
6534 | relocs in this case. */ | |
6535 | free (sort); | |
6536 | return 0; | |
6537 | } | |
c152c796 | 6538 | erel = o->contents; |
eea6121a | 6539 | erelend = o->contents + o->size; |
c152c796 AM |
6540 | p = sort + o->output_offset / ext_size * sort_elt; |
6541 | while (erel < erelend) | |
6542 | { | |
6543 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6544 | (*swap_in) (abfd, erel, s->rela); | |
6545 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
6546 | s->u.sym_mask = r_sym_mask; | |
6547 | p += sort_elt; | |
6548 | erel += ext_size; | |
6549 | } | |
6550 | } | |
6551 | ||
6552 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
6553 | ||
6554 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
6555 | { | |
6556 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6557 | if (s->type != reloc_class_relative) | |
6558 | break; | |
6559 | } | |
6560 | ret = i; | |
6561 | s_non_relative = p; | |
6562 | ||
6563 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
6564 | for (; i < count; i++, p += sort_elt) | |
6565 | { | |
6566 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
6567 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
6568 | sq = sp; | |
6569 | sp->u.offset = sq->rela->r_offset; | |
6570 | } | |
6571 | ||
6572 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
6573 | ||
8423293d | 6574 | for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
6575 | if (lo->type == bfd_indirect_link_order) |
6576 | { | |
6577 | bfd_byte *erel, *erelend; | |
6578 | asection *o = lo->u.indirect.section; | |
6579 | ||
6580 | erel = o->contents; | |
eea6121a | 6581 | erelend = o->contents + o->size; |
c152c796 AM |
6582 | p = sort + o->output_offset / ext_size * sort_elt; |
6583 | while (erel < erelend) | |
6584 | { | |
6585 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
6586 | (*swap_out) (abfd, s->rela, erel); | |
6587 | p += sort_elt; | |
6588 | erel += ext_size; | |
6589 | } | |
6590 | } | |
6591 | ||
6592 | free (sort); | |
6593 | *psec = reldyn; | |
6594 | return ret; | |
6595 | } | |
6596 | ||
6597 | /* Flush the output symbols to the file. */ | |
6598 | ||
6599 | static bfd_boolean | |
6600 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
6601 | const struct elf_backend_data *bed) | |
6602 | { | |
6603 | if (finfo->symbuf_count > 0) | |
6604 | { | |
6605 | Elf_Internal_Shdr *hdr; | |
6606 | file_ptr pos; | |
6607 | bfd_size_type amt; | |
6608 | ||
6609 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
6610 | pos = hdr->sh_offset + hdr->sh_size; | |
6611 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
6612 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
6613 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
6614 | return FALSE; | |
6615 | ||
6616 | hdr->sh_size += amt; | |
6617 | finfo->symbuf_count = 0; | |
6618 | } | |
6619 | ||
6620 | return TRUE; | |
6621 | } | |
6622 | ||
6623 | /* Add a symbol to the output symbol table. */ | |
6624 | ||
6625 | static bfd_boolean | |
6626 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
6627 | const char *name, | |
6628 | Elf_Internal_Sym *elfsym, | |
6629 | asection *input_sec, | |
6630 | struct elf_link_hash_entry *h) | |
6631 | { | |
6632 | bfd_byte *dest; | |
6633 | Elf_External_Sym_Shndx *destshndx; | |
6634 | bfd_boolean (*output_symbol_hook) | |
6635 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
6636 | struct elf_link_hash_entry *); | |
6637 | const struct elf_backend_data *bed; | |
6638 | ||
6639 | bed = get_elf_backend_data (finfo->output_bfd); | |
6640 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
6641 | if (output_symbol_hook != NULL) | |
6642 | { | |
6643 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
6644 | return FALSE; | |
6645 | } | |
6646 | ||
6647 | if (name == NULL || *name == '\0') | |
6648 | elfsym->st_name = 0; | |
6649 | else if (input_sec->flags & SEC_EXCLUDE) | |
6650 | elfsym->st_name = 0; | |
6651 | else | |
6652 | { | |
6653 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
6654 | name, TRUE, FALSE); | |
6655 | if (elfsym->st_name == (unsigned long) -1) | |
6656 | return FALSE; | |
6657 | } | |
6658 | ||
6659 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
6660 | { | |
6661 | if (! elf_link_flush_output_syms (finfo, bed)) | |
6662 | return FALSE; | |
6663 | } | |
6664 | ||
6665 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
6666 | destshndx = finfo->symshndxbuf; | |
6667 | if (destshndx != NULL) | |
6668 | { | |
6669 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
6670 | { | |
6671 | bfd_size_type amt; | |
6672 | ||
6673 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
6674 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
6675 | if (destshndx == NULL) | |
6676 | return FALSE; | |
6677 | memset ((char *) destshndx + amt, 0, amt); | |
6678 | finfo->shndxbuf_size *= 2; | |
6679 | } | |
6680 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
6681 | } | |
6682 | ||
6683 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
6684 | finfo->symbuf_count += 1; | |
6685 | bfd_get_symcount (finfo->output_bfd) += 1; | |
6686 | ||
6687 | return TRUE; | |
6688 | } | |
6689 | ||
c0d5a53d L |
6690 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
6691 | ||
6692 | static bfd_boolean | |
6693 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
6694 | { | |
6695 | if (sym->st_shndx > SHN_HIRESERVE) | |
6696 | { | |
6697 | /* The gABI doesn't support dynamic symbols in output sections | |
6698 | beyond 64k. */ | |
6699 | (*_bfd_error_handler) | |
6700 | (_("%B: Too many sections: %d (>= %d)"), | |
6701 | abfd, bfd_count_sections (abfd), SHN_LORESERVE); | |
6702 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6703 | return FALSE; | |
6704 | } | |
6705 | return TRUE; | |
6706 | } | |
6707 | ||
c152c796 AM |
6708 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
6709 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
6710 | versioned symbol that would normally require an explicit version. | |
6711 | We also handle the case that a DSO references a hidden symbol | |
6712 | which may be satisfied by a versioned symbol in another DSO. */ | |
6713 | ||
6714 | static bfd_boolean | |
6715 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
6716 | const struct elf_backend_data *bed, | |
6717 | struct elf_link_hash_entry *h) | |
6718 | { | |
6719 | bfd *abfd; | |
6720 | struct elf_link_loaded_list *loaded; | |
6721 | ||
6722 | if (!is_elf_hash_table (info->hash)) | |
6723 | return FALSE; | |
6724 | ||
6725 | switch (h->root.type) | |
6726 | { | |
6727 | default: | |
6728 | abfd = NULL; | |
6729 | break; | |
6730 | ||
6731 | case bfd_link_hash_undefined: | |
6732 | case bfd_link_hash_undefweak: | |
6733 | abfd = h->root.u.undef.abfd; | |
6734 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 6735 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
6736 | return FALSE; |
6737 | break; | |
6738 | ||
6739 | case bfd_link_hash_defined: | |
6740 | case bfd_link_hash_defweak: | |
6741 | abfd = h->root.u.def.section->owner; | |
6742 | break; | |
6743 | ||
6744 | case bfd_link_hash_common: | |
6745 | abfd = h->root.u.c.p->section->owner; | |
6746 | break; | |
6747 | } | |
6748 | BFD_ASSERT (abfd != NULL); | |
6749 | ||
6750 | for (loaded = elf_hash_table (info)->loaded; | |
6751 | loaded != NULL; | |
6752 | loaded = loaded->next) | |
6753 | { | |
6754 | bfd *input; | |
6755 | Elf_Internal_Shdr *hdr; | |
6756 | bfd_size_type symcount; | |
6757 | bfd_size_type extsymcount; | |
6758 | bfd_size_type extsymoff; | |
6759 | Elf_Internal_Shdr *versymhdr; | |
6760 | Elf_Internal_Sym *isym; | |
6761 | Elf_Internal_Sym *isymend; | |
6762 | Elf_Internal_Sym *isymbuf; | |
6763 | Elf_External_Versym *ever; | |
6764 | Elf_External_Versym *extversym; | |
6765 | ||
6766 | input = loaded->abfd; | |
6767 | ||
6768 | /* We check each DSO for a possible hidden versioned definition. */ | |
6769 | if (input == abfd | |
6770 | || (input->flags & DYNAMIC) == 0 | |
6771 | || elf_dynversym (input) == 0) | |
6772 | continue; | |
6773 | ||
6774 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
6775 | ||
6776 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
6777 | if (elf_bad_symtab (input)) | |
6778 | { | |
6779 | extsymcount = symcount; | |
6780 | extsymoff = 0; | |
6781 | } | |
6782 | else | |
6783 | { | |
6784 | extsymcount = symcount - hdr->sh_info; | |
6785 | extsymoff = hdr->sh_info; | |
6786 | } | |
6787 | ||
6788 | if (extsymcount == 0) | |
6789 | continue; | |
6790 | ||
6791 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
6792 | NULL, NULL, NULL); | |
6793 | if (isymbuf == NULL) | |
6794 | return FALSE; | |
6795 | ||
6796 | /* Read in any version definitions. */ | |
6797 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
6798 | extversym = bfd_malloc (versymhdr->sh_size); | |
6799 | if (extversym == NULL) | |
6800 | goto error_ret; | |
6801 | ||
6802 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
6803 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
6804 | != versymhdr->sh_size)) | |
6805 | { | |
6806 | free (extversym); | |
6807 | error_ret: | |
6808 | free (isymbuf); | |
6809 | return FALSE; | |
6810 | } | |
6811 | ||
6812 | ever = extversym + extsymoff; | |
6813 | isymend = isymbuf + extsymcount; | |
6814 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
6815 | { | |
6816 | const char *name; | |
6817 | Elf_Internal_Versym iver; | |
6818 | unsigned short version_index; | |
6819 | ||
6820 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
6821 | || isym->st_shndx == SHN_UNDEF) | |
6822 | continue; | |
6823 | ||
6824 | name = bfd_elf_string_from_elf_section (input, | |
6825 | hdr->sh_link, | |
6826 | isym->st_name); | |
6827 | if (strcmp (name, h->root.root.string) != 0) | |
6828 | continue; | |
6829 | ||
6830 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
6831 | ||
6832 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
6833 | { | |
6834 | /* If we have a non-hidden versioned sym, then it should | |
6835 | have provided a definition for the undefined sym. */ | |
6836 | abort (); | |
6837 | } | |
6838 | ||
6839 | version_index = iver.vs_vers & VERSYM_VERSION; | |
6840 | if (version_index == 1 || version_index == 2) | |
6841 | { | |
6842 | /* This is the base or first version. We can use it. */ | |
6843 | free (extversym); | |
6844 | free (isymbuf); | |
6845 | return TRUE; | |
6846 | } | |
6847 | } | |
6848 | ||
6849 | free (extversym); | |
6850 | free (isymbuf); | |
6851 | } | |
6852 | ||
6853 | return FALSE; | |
6854 | } | |
6855 | ||
6856 | /* Add an external symbol to the symbol table. This is called from | |
6857 | the hash table traversal routine. When generating a shared object, | |
6858 | we go through the symbol table twice. The first time we output | |
6859 | anything that might have been forced to local scope in a version | |
6860 | script. The second time we output the symbols that are still | |
6861 | global symbols. */ | |
6862 | ||
6863 | static bfd_boolean | |
6864 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
6865 | { | |
6866 | struct elf_outext_info *eoinfo = data; | |
6867 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
6868 | bfd_boolean strip; | |
6869 | Elf_Internal_Sym sym; | |
6870 | asection *input_sec; | |
6871 | const struct elf_backend_data *bed; | |
6872 | ||
6873 | if (h->root.type == bfd_link_hash_warning) | |
6874 | { | |
6875 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6876 | if (h->root.type == bfd_link_hash_new) | |
6877 | return TRUE; | |
6878 | } | |
6879 | ||
6880 | /* Decide whether to output this symbol in this pass. */ | |
6881 | if (eoinfo->localsyms) | |
6882 | { | |
f5385ebf | 6883 | if (!h->forced_local) |
c152c796 AM |
6884 | return TRUE; |
6885 | } | |
6886 | else | |
6887 | { | |
f5385ebf | 6888 | if (h->forced_local) |
c152c796 AM |
6889 | return TRUE; |
6890 | } | |
6891 | ||
6892 | bed = get_elf_backend_data (finfo->output_bfd); | |
6893 | ||
12ac1cf5 | 6894 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 6895 | { |
12ac1cf5 NC |
6896 | /* If we have an undefined symbol reference here then it must have |
6897 | come from a shared library that is being linked in. (Undefined | |
6898 | references in regular files have already been handled). */ | |
6899 | bfd_boolean ignore_undef = FALSE; | |
6900 | ||
6901 | /* Some symbols may be special in that the fact that they're | |
6902 | undefined can be safely ignored - let backend determine that. */ | |
6903 | if (bed->elf_backend_ignore_undef_symbol) | |
6904 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
6905 | ||
6906 | /* If we are reporting errors for this situation then do so now. */ | |
6907 | if (ignore_undef == FALSE | |
6908 | && h->ref_dynamic | |
6909 | && ! h->ref_regular | |
6910 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) | |
6911 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
c152c796 | 6912 | { |
12ac1cf5 NC |
6913 | if (! (finfo->info->callbacks->undefined_symbol |
6914 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
6915 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
6916 | { | |
6917 | eoinfo->failed = TRUE; | |
6918 | return FALSE; | |
6919 | } | |
c152c796 AM |
6920 | } |
6921 | } | |
6922 | ||
6923 | /* We should also warn if a forced local symbol is referenced from | |
6924 | shared libraries. */ | |
6925 | if (! finfo->info->relocatable | |
6926 | && (! finfo->info->shared) | |
f5385ebf AM |
6927 | && h->forced_local |
6928 | && h->ref_dynamic | |
6929 | && !h->dynamic_def | |
6930 | && !h->dynamic_weak | |
c152c796 AM |
6931 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
6932 | { | |
6933 | (*_bfd_error_handler) | |
d003868e | 6934 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
cfca085c L |
6935 | finfo->output_bfd, |
6936 | h->root.u.def.section == bfd_abs_section_ptr | |
6937 | ? finfo->output_bfd : h->root.u.def.section->owner, | |
c152c796 AM |
6938 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
6939 | ? "internal" | |
6940 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
6941 | ? "hidden" : "local", |
6942 | h->root.root.string); | |
c152c796 AM |
6943 | eoinfo->failed = TRUE; |
6944 | return FALSE; | |
6945 | } | |
6946 | ||
6947 | /* We don't want to output symbols that have never been mentioned by | |
6948 | a regular file, or that we have been told to strip. However, if | |
6949 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6950 | output it. */ | |
6951 | if (h->indx == -2) | |
6952 | strip = FALSE; | |
f5385ebf | 6953 | else if ((h->def_dynamic |
77cfaee6 AM |
6954 | || h->ref_dynamic |
6955 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
6956 | && !h->def_regular |
6957 | && !h->ref_regular) | |
c152c796 AM |
6958 | strip = TRUE; |
6959 | else if (finfo->info->strip == strip_all) | |
6960 | strip = TRUE; | |
6961 | else if (finfo->info->strip == strip_some | |
6962 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6963 | h->root.root.string, FALSE, FALSE) == NULL) | |
6964 | strip = TRUE; | |
6965 | else if (finfo->info->strip_discarded | |
6966 | && (h->root.type == bfd_link_hash_defined | |
6967 | || h->root.type == bfd_link_hash_defweak) | |
6968 | && elf_discarded_section (h->root.u.def.section)) | |
6969 | strip = TRUE; | |
6970 | else | |
6971 | strip = FALSE; | |
6972 | ||
6973 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6974 | nothing else to do unless it is a forced local symbol. */ | |
6975 | if (strip | |
6976 | && h->dynindx == -1 | |
f5385ebf | 6977 | && !h->forced_local) |
c152c796 AM |
6978 | return TRUE; |
6979 | ||
6980 | sym.st_value = 0; | |
6981 | sym.st_size = h->size; | |
6982 | sym.st_other = h->other; | |
f5385ebf | 6983 | if (h->forced_local) |
c152c796 AM |
6984 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
6985 | else if (h->root.type == bfd_link_hash_undefweak | |
6986 | || h->root.type == bfd_link_hash_defweak) | |
6987 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6988 | else | |
6989 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6990 | ||
6991 | switch (h->root.type) | |
6992 | { | |
6993 | default: | |
6994 | case bfd_link_hash_new: | |
6995 | case bfd_link_hash_warning: | |
6996 | abort (); | |
6997 | return FALSE; | |
6998 | ||
6999 | case bfd_link_hash_undefined: | |
7000 | case bfd_link_hash_undefweak: | |
7001 | input_sec = bfd_und_section_ptr; | |
7002 | sym.st_shndx = SHN_UNDEF; | |
7003 | break; | |
7004 | ||
7005 | case bfd_link_hash_defined: | |
7006 | case bfd_link_hash_defweak: | |
7007 | { | |
7008 | input_sec = h->root.u.def.section; | |
7009 | if (input_sec->output_section != NULL) | |
7010 | { | |
7011 | sym.st_shndx = | |
7012 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
7013 | input_sec->output_section); | |
7014 | if (sym.st_shndx == SHN_BAD) | |
7015 | { | |
7016 | (*_bfd_error_handler) | |
d003868e AM |
7017 | (_("%B: could not find output section %A for input section %A"), |
7018 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
7019 | eoinfo->failed = TRUE; |
7020 | return FALSE; | |
7021 | } | |
7022 | ||
7023 | /* ELF symbols in relocatable files are section relative, | |
7024 | but in nonrelocatable files they are virtual | |
7025 | addresses. */ | |
7026 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
7027 | if (! finfo->info->relocatable) | |
7028 | { | |
7029 | sym.st_value += input_sec->output_section->vma; | |
7030 | if (h->type == STT_TLS) | |
7031 | { | |
7032 | /* STT_TLS symbols are relative to PT_TLS segment | |
7033 | base. */ | |
7034 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
7035 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
7036 | } | |
7037 | } | |
7038 | } | |
7039 | else | |
7040 | { | |
7041 | BFD_ASSERT (input_sec->owner == NULL | |
7042 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
7043 | sym.st_shndx = SHN_UNDEF; | |
7044 | input_sec = bfd_und_section_ptr; | |
7045 | } | |
7046 | } | |
7047 | break; | |
7048 | ||
7049 | case bfd_link_hash_common: | |
7050 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 7051 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
7052 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
7053 | break; | |
7054 | ||
7055 | case bfd_link_hash_indirect: | |
7056 | /* These symbols are created by symbol versioning. They point | |
7057 | to the decorated version of the name. For example, if the | |
7058 | symbol foo@@GNU_1.2 is the default, which should be used when | |
7059 | foo is used with no version, then we add an indirect symbol | |
7060 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
7061 | since the indirected symbol is already in the hash table. */ | |
7062 | return TRUE; | |
7063 | } | |
7064 | ||
7065 | /* Give the processor backend a chance to tweak the symbol value, | |
7066 | and also to finish up anything that needs to be done for this | |
7067 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
7068 | forced local syms when non-shared is due to a historical quirk. */ | |
7069 | if ((h->dynindx != -1 | |
f5385ebf | 7070 | || h->forced_local) |
c152c796 AM |
7071 | && ((finfo->info->shared |
7072 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
7073 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 7074 | || !h->forced_local) |
c152c796 AM |
7075 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
7076 | { | |
7077 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
7078 | (finfo->output_bfd, finfo->info, h, &sym))) | |
7079 | { | |
7080 | eoinfo->failed = TRUE; | |
7081 | return FALSE; | |
7082 | } | |
7083 | } | |
7084 | ||
7085 | /* If we are marking the symbol as undefined, and there are no | |
7086 | non-weak references to this symbol from a regular object, then | |
7087 | mark the symbol as weak undefined; if there are non-weak | |
7088 | references, mark the symbol as strong. We can't do this earlier, | |
7089 | because it might not be marked as undefined until the | |
7090 | finish_dynamic_symbol routine gets through with it. */ | |
7091 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 7092 | && h->ref_regular |
c152c796 AM |
7093 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
7094 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
7095 | { | |
7096 | int bindtype; | |
7097 | ||
f5385ebf | 7098 | if (h->ref_regular_nonweak) |
c152c796 AM |
7099 | bindtype = STB_GLOBAL; |
7100 | else | |
7101 | bindtype = STB_WEAK; | |
7102 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
7103 | } | |
7104 | ||
7105 | /* If a non-weak symbol with non-default visibility is not defined | |
7106 | locally, it is a fatal error. */ | |
7107 | if (! finfo->info->relocatable | |
7108 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
7109 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
7110 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 7111 | && !h->def_regular) |
c152c796 AM |
7112 | { |
7113 | (*_bfd_error_handler) | |
d003868e AM |
7114 | (_("%B: %s symbol `%s' isn't defined"), |
7115 | finfo->output_bfd, | |
7116 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
7117 | ? "protected" | |
7118 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
7119 | ? "internal" : "hidden", | |
7120 | h->root.root.string); | |
c152c796 AM |
7121 | eoinfo->failed = TRUE; |
7122 | return FALSE; | |
7123 | } | |
7124 | ||
7125 | /* If this symbol should be put in the .dynsym section, then put it | |
7126 | there now. We already know the symbol index. We also fill in | |
7127 | the entry in the .hash section. */ | |
7128 | if (h->dynindx != -1 | |
7129 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
7130 | { | |
c152c796 AM |
7131 | bfd_byte *esym; |
7132 | ||
7133 | sym.st_name = h->dynstr_index; | |
7134 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
c0d5a53d L |
7135 | if (! check_dynsym (finfo->output_bfd, &sym)) |
7136 | { | |
7137 | eoinfo->failed = TRUE; | |
7138 | return FALSE; | |
7139 | } | |
c152c796 AM |
7140 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); |
7141 | ||
fdc90cb4 JJ |
7142 | if (finfo->hash_sec != NULL) |
7143 | { | |
7144 | size_t hash_entry_size; | |
7145 | bfd_byte *bucketpos; | |
7146 | bfd_vma chain; | |
41198d0c L |
7147 | size_t bucketcount; |
7148 | size_t bucket; | |
7149 | ||
7150 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
7151 | bucket = h->u.elf_hash_value % bucketcount; | |
fdc90cb4 JJ |
7152 | |
7153 | hash_entry_size | |
7154 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
7155 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
7156 | + (bucket + 2) * hash_entry_size); | |
7157 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
7158 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
7159 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
7160 | ((bfd_byte *) finfo->hash_sec->contents | |
7161 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
7162 | } | |
c152c796 AM |
7163 | |
7164 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
7165 | { | |
7166 | Elf_Internal_Versym iversym; | |
7167 | Elf_External_Versym *eversym; | |
7168 | ||
f5385ebf | 7169 | if (!h->def_regular) |
c152c796 AM |
7170 | { |
7171 | if (h->verinfo.verdef == NULL) | |
7172 | iversym.vs_vers = 0; | |
7173 | else | |
7174 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
7175 | } | |
7176 | else | |
7177 | { | |
7178 | if (h->verinfo.vertree == NULL) | |
7179 | iversym.vs_vers = 1; | |
7180 | else | |
7181 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
7182 | if (finfo->info->create_default_symver) |
7183 | iversym.vs_vers++; | |
c152c796 AM |
7184 | } |
7185 | ||
f5385ebf | 7186 | if (h->hidden) |
c152c796 AM |
7187 | iversym.vs_vers |= VERSYM_HIDDEN; |
7188 | ||
7189 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
7190 | eversym += h->dynindx; | |
7191 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
7192 | } | |
7193 | } | |
7194 | ||
7195 | /* If we're stripping it, then it was just a dynamic symbol, and | |
7196 | there's nothing else to do. */ | |
7197 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
7198 | return TRUE; | |
7199 | ||
7200 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
7201 | ||
7202 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
7203 | { | |
7204 | eoinfo->failed = TRUE; | |
7205 | return FALSE; | |
7206 | } | |
7207 | ||
7208 | return TRUE; | |
7209 | } | |
7210 | ||
cdd3575c AM |
7211 | /* Return TRUE if special handling is done for relocs in SEC against |
7212 | symbols defined in discarded sections. */ | |
7213 | ||
c152c796 AM |
7214 | static bfd_boolean |
7215 | elf_section_ignore_discarded_relocs (asection *sec) | |
7216 | { | |
7217 | const struct elf_backend_data *bed; | |
7218 | ||
cdd3575c AM |
7219 | switch (sec->sec_info_type) |
7220 | { | |
7221 | case ELF_INFO_TYPE_STABS: | |
7222 | case ELF_INFO_TYPE_EH_FRAME: | |
7223 | return TRUE; | |
7224 | default: | |
7225 | break; | |
7226 | } | |
c152c796 AM |
7227 | |
7228 | bed = get_elf_backend_data (sec->owner); | |
7229 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
7230 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
7231 | return TRUE; | |
7232 | ||
7233 | return FALSE; | |
7234 | } | |
7235 | ||
9e66c942 AM |
7236 | /* Return a mask saying how ld should treat relocations in SEC against |
7237 | symbols defined in discarded sections. If this function returns | |
7238 | COMPLAIN set, ld will issue a warning message. If this function | |
7239 | returns PRETEND set, and the discarded section was link-once and the | |
7240 | same size as the kept link-once section, ld will pretend that the | |
7241 | symbol was actually defined in the kept section. Otherwise ld will | |
7242 | zero the reloc (at least that is the intent, but some cooperation by | |
7243 | the target dependent code is needed, particularly for REL targets). */ | |
7244 | ||
8a696751 AM |
7245 | unsigned int |
7246 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 7247 | { |
9e66c942 | 7248 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 7249 | return PRETEND; |
cdd3575c AM |
7250 | |
7251 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 7252 | return 0; |
cdd3575c AM |
7253 | |
7254 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 7255 | return 0; |
cdd3575c | 7256 | |
9e66c942 | 7257 | return COMPLAIN | PRETEND; |
cdd3575c AM |
7258 | } |
7259 | ||
3d7f7666 L |
7260 | /* Find a match between a section and a member of a section group. */ |
7261 | ||
7262 | static asection * | |
7263 | match_group_member (asection *sec, asection *group) | |
7264 | { | |
7265 | asection *first = elf_next_in_group (group); | |
7266 | asection *s = first; | |
7267 | ||
7268 | while (s != NULL) | |
7269 | { | |
7270 | if (bfd_elf_match_symbols_in_sections (s, sec)) | |
7271 | return s; | |
7272 | ||
83180ade | 7273 | s = elf_next_in_group (s); |
3d7f7666 L |
7274 | if (s == first) |
7275 | break; | |
7276 | } | |
7277 | ||
7278 | return NULL; | |
7279 | } | |
7280 | ||
01b3c8ab L |
7281 | /* Check if the kept section of a discarded section SEC can be used |
7282 | to replace it. Return the replacement if it is OK. Otherwise return | |
7283 | NULL. */ | |
7284 | ||
7285 | asection * | |
7286 | _bfd_elf_check_kept_section (asection *sec) | |
7287 | { | |
7288 | asection *kept; | |
7289 | ||
7290 | kept = sec->kept_section; | |
7291 | if (kept != NULL) | |
7292 | { | |
7293 | if (elf_sec_group (sec) != NULL) | |
7294 | kept = match_group_member (sec, kept); | |
7295 | if (kept != NULL && sec->size != kept->size) | |
7296 | kept = NULL; | |
7297 | } | |
7298 | return kept; | |
7299 | } | |
7300 | ||
c152c796 AM |
7301 | /* Link an input file into the linker output file. This function |
7302 | handles all the sections and relocations of the input file at once. | |
7303 | This is so that we only have to read the local symbols once, and | |
7304 | don't have to keep them in memory. */ | |
7305 | ||
7306 | static bfd_boolean | |
7307 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
7308 | { | |
7309 | bfd_boolean (*relocate_section) | |
7310 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
7311 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
7312 | bfd *output_bfd; | |
7313 | Elf_Internal_Shdr *symtab_hdr; | |
7314 | size_t locsymcount; | |
7315 | size_t extsymoff; | |
7316 | Elf_Internal_Sym *isymbuf; | |
7317 | Elf_Internal_Sym *isym; | |
7318 | Elf_Internal_Sym *isymend; | |
7319 | long *pindex; | |
7320 | asection **ppsection; | |
7321 | asection *o; | |
7322 | const struct elf_backend_data *bed; | |
7323 | bfd_boolean emit_relocs; | |
7324 | struct elf_link_hash_entry **sym_hashes; | |
7325 | ||
7326 | output_bfd = finfo->output_bfd; | |
7327 | bed = get_elf_backend_data (output_bfd); | |
7328 | relocate_section = bed->elf_backend_relocate_section; | |
7329 | ||
7330 | /* If this is a dynamic object, we don't want to do anything here: | |
7331 | we don't want the local symbols, and we don't want the section | |
7332 | contents. */ | |
7333 | if ((input_bfd->flags & DYNAMIC) != 0) | |
7334 | return TRUE; | |
7335 | ||
7336 | emit_relocs = (finfo->info->relocatable | |
eac338cf | 7337 | || finfo->info->emitrelocations); |
c152c796 AM |
7338 | |
7339 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
7340 | if (elf_bad_symtab (input_bfd)) | |
7341 | { | |
7342 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
7343 | extsymoff = 0; | |
7344 | } | |
7345 | else | |
7346 | { | |
7347 | locsymcount = symtab_hdr->sh_info; | |
7348 | extsymoff = symtab_hdr->sh_info; | |
7349 | } | |
7350 | ||
7351 | /* Read the local symbols. */ | |
7352 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
7353 | if (isymbuf == NULL && locsymcount != 0) | |
7354 | { | |
7355 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
7356 | finfo->internal_syms, | |
7357 | finfo->external_syms, | |
7358 | finfo->locsym_shndx); | |
7359 | if (isymbuf == NULL) | |
7360 | return FALSE; | |
7361 | } | |
7362 | ||
7363 | /* Find local symbol sections and adjust values of symbols in | |
7364 | SEC_MERGE sections. Write out those local symbols we know are | |
7365 | going into the output file. */ | |
7366 | isymend = isymbuf + locsymcount; | |
7367 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
7368 | isym < isymend; | |
7369 | isym++, pindex++, ppsection++) | |
7370 | { | |
7371 | asection *isec; | |
7372 | const char *name; | |
7373 | Elf_Internal_Sym osym; | |
7374 | ||
7375 | *pindex = -1; | |
7376 | ||
7377 | if (elf_bad_symtab (input_bfd)) | |
7378 | { | |
7379 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
7380 | { | |
7381 | *ppsection = NULL; | |
7382 | continue; | |
7383 | } | |
7384 | } | |
7385 | ||
7386 | if (isym->st_shndx == SHN_UNDEF) | |
7387 | isec = bfd_und_section_ptr; | |
7388 | else if (isym->st_shndx < SHN_LORESERVE | |
7389 | || isym->st_shndx > SHN_HIRESERVE) | |
7390 | { | |
7391 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
7392 | if (isec | |
7393 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
7394 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
7395 | isym->st_value = | |
7396 | _bfd_merged_section_offset (output_bfd, &isec, | |
7397 | elf_section_data (isec)->sec_info, | |
753731ee | 7398 | isym->st_value); |
c152c796 AM |
7399 | } |
7400 | else if (isym->st_shndx == SHN_ABS) | |
7401 | isec = bfd_abs_section_ptr; | |
7402 | else if (isym->st_shndx == SHN_COMMON) | |
7403 | isec = bfd_com_section_ptr; | |
7404 | else | |
7405 | { | |
f02571c5 AM |
7406 | /* Don't attempt to output symbols with st_shnx in the |
7407 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
7408 | *ppsection = NULL; | |
7409 | continue; | |
c152c796 AM |
7410 | } |
7411 | ||
7412 | *ppsection = isec; | |
7413 | ||
7414 | /* Don't output the first, undefined, symbol. */ | |
7415 | if (ppsection == finfo->sections) | |
7416 | continue; | |
7417 | ||
7418 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
7419 | { | |
7420 | /* We never output section symbols. Instead, we use the | |
7421 | section symbol of the corresponding section in the output | |
7422 | file. */ | |
7423 | continue; | |
7424 | } | |
7425 | ||
7426 | /* If we are stripping all symbols, we don't want to output this | |
7427 | one. */ | |
7428 | if (finfo->info->strip == strip_all) | |
7429 | continue; | |
7430 | ||
7431 | /* If we are discarding all local symbols, we don't want to | |
7432 | output this one. If we are generating a relocatable output | |
7433 | file, then some of the local symbols may be required by | |
7434 | relocs; we output them below as we discover that they are | |
7435 | needed. */ | |
7436 | if (finfo->info->discard == discard_all) | |
7437 | continue; | |
7438 | ||
7439 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
7440 | discarding, we don't need to keep it. */ |
7441 | if (isym->st_shndx != SHN_UNDEF | |
7442 | && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
ccf5f610 | 7443 | && (isec == NULL |
f02571c5 AM |
7444 | || bfd_section_removed_from_list (output_bfd, |
7445 | isec->output_section))) | |
e75a280b L |
7446 | continue; |
7447 | ||
c152c796 AM |
7448 | /* Get the name of the symbol. */ |
7449 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
7450 | isym->st_name); | |
7451 | if (name == NULL) | |
7452 | return FALSE; | |
7453 | ||
7454 | /* See if we are discarding symbols with this name. */ | |
7455 | if ((finfo->info->strip == strip_some | |
7456 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
7457 | == NULL)) | |
7458 | || (((finfo->info->discard == discard_sec_merge | |
7459 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
7460 | || finfo->info->discard == discard_l) | |
7461 | && bfd_is_local_label_name (input_bfd, name))) | |
7462 | continue; | |
7463 | ||
7464 | /* If we get here, we are going to output this symbol. */ | |
7465 | ||
7466 | osym = *isym; | |
7467 | ||
7468 | /* Adjust the section index for the output file. */ | |
7469 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
7470 | isec->output_section); | |
7471 | if (osym.st_shndx == SHN_BAD) | |
7472 | return FALSE; | |
7473 | ||
7474 | *pindex = bfd_get_symcount (output_bfd); | |
7475 | ||
7476 | /* ELF symbols in relocatable files are section relative, but | |
7477 | in executable files they are virtual addresses. Note that | |
7478 | this code assumes that all ELF sections have an associated | |
7479 | BFD section with a reasonable value for output_offset; below | |
7480 | we assume that they also have a reasonable value for | |
7481 | output_section. Any special sections must be set up to meet | |
7482 | these requirements. */ | |
7483 | osym.st_value += isec->output_offset; | |
7484 | if (! finfo->info->relocatable) | |
7485 | { | |
7486 | osym.st_value += isec->output_section->vma; | |
7487 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
7488 | { | |
7489 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
7490 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
7491 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
7492 | } | |
7493 | } | |
7494 | ||
7495 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
7496 | return FALSE; | |
7497 | } | |
7498 | ||
7499 | /* Relocate the contents of each section. */ | |
7500 | sym_hashes = elf_sym_hashes (input_bfd); | |
7501 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
7502 | { | |
7503 | bfd_byte *contents; | |
7504 | ||
7505 | if (! o->linker_mark) | |
7506 | { | |
7507 | /* This section was omitted from the link. */ | |
7508 | continue; | |
7509 | } | |
7510 | ||
7511 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 7512 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
7513 | continue; |
7514 | ||
7515 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
7516 | { | |
7517 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
7518 | or somesuch. */ | |
7519 | continue; | |
7520 | } | |
7521 | ||
7522 | /* Get the contents of the section. They have been cached by a | |
7523 | relaxation routine. Note that o is a section in an input | |
7524 | file, so the contents field will not have been set by any of | |
7525 | the routines which work on output files. */ | |
7526 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
7527 | contents = elf_section_data (o)->this_hdr.contents; | |
7528 | else | |
7529 | { | |
eea6121a AM |
7530 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
7531 | ||
c152c796 | 7532 | contents = finfo->contents; |
eea6121a | 7533 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
7534 | return FALSE; |
7535 | } | |
7536 | ||
7537 | if ((o->flags & SEC_RELOC) != 0) | |
7538 | { | |
7539 | Elf_Internal_Rela *internal_relocs; | |
7540 | bfd_vma r_type_mask; | |
7541 | int r_sym_shift; | |
7542 | ||
7543 | /* Get the swapped relocs. */ | |
7544 | internal_relocs | |
7545 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
7546 | finfo->internal_relocs, FALSE); | |
7547 | if (internal_relocs == NULL | |
7548 | && o->reloc_count > 0) | |
7549 | return FALSE; | |
7550 | ||
7551 | if (bed->s->arch_size == 32) | |
7552 | { | |
7553 | r_type_mask = 0xff; | |
7554 | r_sym_shift = 8; | |
7555 | } | |
7556 | else | |
7557 | { | |
7558 | r_type_mask = 0xffffffff; | |
7559 | r_sym_shift = 32; | |
7560 | } | |
7561 | ||
7562 | /* Run through the relocs looking for any against symbols | |
7563 | from discarded sections and section symbols from | |
7564 | removed link-once sections. Complain about relocs | |
7565 | against discarded sections. Zero relocs against removed | |
e0ae6d6f | 7566 | link-once sections. */ |
c152c796 AM |
7567 | if (!elf_section_ignore_discarded_relocs (o)) |
7568 | { | |
7569 | Elf_Internal_Rela *rel, *relend; | |
8a696751 | 7570 | unsigned int action = (*bed->action_discarded) (o); |
c152c796 AM |
7571 | |
7572 | rel = internal_relocs; | |
7573 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
7574 | for ( ; rel < relend; rel++) | |
7575 | { | |
7576 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
7577 | asection **ps, *sec; |
7578 | struct elf_link_hash_entry *h = NULL; | |
7579 | const char *sym_name; | |
c152c796 | 7580 | |
ee75fd95 AM |
7581 | if (r_symndx == STN_UNDEF) |
7582 | continue; | |
7583 | ||
c152c796 AM |
7584 | if (r_symndx >= locsymcount |
7585 | || (elf_bad_symtab (input_bfd) | |
7586 | && finfo->sections[r_symndx] == NULL)) | |
7587 | { | |
c152c796 | 7588 | h = sym_hashes[r_symndx - extsymoff]; |
dce669a1 | 7589 | |
8c19749a NC |
7590 | /* Badly formatted input files can contain relocs that |
7591 | reference non-existant symbols. Check here so that | |
7592 | we do not seg fault. */ | |
7593 | if (h == NULL) | |
7594 | { | |
7595 | char buffer [32]; | |
7596 | ||
7597 | sprintf_vma (buffer, rel->r_info); | |
7598 | (*_bfd_error_handler) | |
7599 | (_("error: %B contains a reloc (0x%s) for section %A " | |
7600 | "that references a non-existent global symbol"), | |
7601 | input_bfd, o, buffer); | |
7602 | bfd_set_error (bfd_error_bad_value); | |
7603 | return FALSE; | |
7604 | } | |
3b36f7e6 | 7605 | |
c152c796 AM |
7606 | while (h->root.type == bfd_link_hash_indirect |
7607 | || h->root.type == bfd_link_hash_warning) | |
7608 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7609 | ||
cdd3575c AM |
7610 | if (h->root.type != bfd_link_hash_defined |
7611 | && h->root.type != bfd_link_hash_defweak) | |
7612 | continue; | |
7613 | ||
7614 | ps = &h->root.u.def.section; | |
7615 | sym_name = h->root.root.string; | |
c152c796 AM |
7616 | } |
7617 | else | |
7618 | { | |
cdd3575c AM |
7619 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
7620 | ps = &finfo->sections[r_symndx]; | |
26c61ae5 L |
7621 | sym_name = bfd_elf_sym_name (input_bfd, |
7622 | symtab_hdr, | |
7623 | sym, *ps); | |
cdd3575c | 7624 | } |
c152c796 | 7625 | |
cdd3575c AM |
7626 | /* Complain if the definition comes from a |
7627 | discarded section. */ | |
7628 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
7629 | { | |
87e5235d | 7630 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 7631 | if (action & COMPLAIN) |
e1fffbe6 AM |
7632 | (*finfo->info->callbacks->einfo) |
7633 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 7634 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 7635 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 7636 | |
87e5235d | 7637 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 7638 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
7639 | really defined in the kept linkonce section. |
7640 | FIXME: This is quite broken. Modifying the | |
7641 | symbol here means we will be changing all later | |
e0ae6d6f | 7642 | uses of the symbol, not just in this section. */ |
01b3c8ab | 7643 | if (action & PRETEND) |
87e5235d | 7644 | { |
01b3c8ab L |
7645 | asection *kept; |
7646 | ||
7647 | kept = _bfd_elf_check_kept_section (sec); | |
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 | ||
8796 | /* Output any global symbols that got converted to local in a | |
8797 | version script or due to symbol visibility. We do this in a | |
8798 | separate step since ELF requires all local symbols to appear | |
8799 | prior to any global symbols. FIXME: We should only do this if | |
8800 | some global symbols were, in fact, converted to become local. | |
8801 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
8802 | eoinfo.failed = FALSE; | |
8803 | eoinfo.finfo = &finfo; | |
8804 | eoinfo.localsyms = TRUE; | |
8805 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8806 | &eoinfo); | |
8807 | if (eoinfo.failed) | |
8808 | return FALSE; | |
8809 | ||
4e617b1e PB |
8810 | /* If backend needs to output some local symbols not present in the hash |
8811 | table, do it now. */ | |
8812 | if (bed->elf_backend_output_arch_local_syms) | |
8813 | { | |
8814 | typedef bfd_boolean (*out_sym_func) | |
8815 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8816 | struct elf_link_hash_entry *); | |
8817 | ||
8818 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
8819 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8820 | return FALSE; | |
8821 | } | |
8822 | ||
c152c796 AM |
8823 | /* That wrote out all the local symbols. Finish up the symbol table |
8824 | with the global symbols. Even if we want to strip everything we | |
8825 | can, we still need to deal with those global symbols that got | |
8826 | converted to local in a version script. */ | |
8827 | ||
8828 | /* The sh_info field records the index of the first non local symbol. */ | |
8829 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
8830 | ||
8831 | if (dynamic | |
8832 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
8833 | { | |
8834 | Elf_Internal_Sym sym; | |
8835 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
8836 | long last_local = 0; | |
8837 | ||
8838 | /* Write out the section symbols for the output sections. */ | |
67687978 | 8839 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
8840 | { |
8841 | asection *s; | |
8842 | ||
8843 | sym.st_size = 0; | |
8844 | sym.st_name = 0; | |
8845 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8846 | sym.st_other = 0; | |
8847 | ||
8848 | for (s = abfd->sections; s != NULL; s = s->next) | |
8849 | { | |
8850 | int indx; | |
8851 | bfd_byte *dest; | |
8852 | long dynindx; | |
8853 | ||
c152c796 | 8854 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
8855 | if (dynindx <= 0) |
8856 | continue; | |
8857 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
8858 | BFD_ASSERT (indx > 0); |
8859 | sym.st_shndx = indx; | |
c0d5a53d L |
8860 | if (! check_dynsym (abfd, &sym)) |
8861 | return FALSE; | |
c152c796 AM |
8862 | sym.st_value = s->vma; |
8863 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
8864 | if (last_local < dynindx) |
8865 | last_local = dynindx; | |
c152c796 AM |
8866 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
8867 | } | |
c152c796 AM |
8868 | } |
8869 | ||
8870 | /* Write out the local dynsyms. */ | |
8871 | if (elf_hash_table (info)->dynlocal) | |
8872 | { | |
8873 | struct elf_link_local_dynamic_entry *e; | |
8874 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
8875 | { | |
8876 | asection *s; | |
8877 | bfd_byte *dest; | |
8878 | ||
8879 | sym.st_size = e->isym.st_size; | |
8880 | sym.st_other = e->isym.st_other; | |
8881 | ||
8882 | /* Copy the internal symbol as is. | |
8883 | Note that we saved a word of storage and overwrote | |
8884 | the original st_name with the dynstr_index. */ | |
8885 | sym = e->isym; | |
8886 | ||
8887 | if (e->isym.st_shndx != SHN_UNDEF | |
8888 | && (e->isym.st_shndx < SHN_LORESERVE | |
8889 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
8890 | { | |
8891 | s = bfd_section_from_elf_index (e->input_bfd, | |
8892 | e->isym.st_shndx); | |
8893 | ||
8894 | sym.st_shndx = | |
8895 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
8896 | if (! check_dynsym (abfd, &sym)) |
8897 | return FALSE; | |
c152c796 AM |
8898 | sym.st_value = (s->output_section->vma |
8899 | + s->output_offset | |
8900 | + e->isym.st_value); | |
8901 | } | |
8902 | ||
8903 | if (last_local < e->dynindx) | |
8904 | last_local = e->dynindx; | |
8905 | ||
8906 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
8907 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
8908 | } | |
8909 | } | |
8910 | ||
8911 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
8912 | last_local + 1; | |
8913 | } | |
8914 | ||
8915 | /* We get the global symbols from the hash table. */ | |
8916 | eoinfo.failed = FALSE; | |
8917 | eoinfo.localsyms = FALSE; | |
8918 | eoinfo.finfo = &finfo; | |
8919 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8920 | &eoinfo); | |
8921 | if (eoinfo.failed) | |
8922 | return FALSE; | |
8923 | ||
8924 | /* If backend needs to output some symbols not present in the hash | |
8925 | table, do it now. */ | |
8926 | if (bed->elf_backend_output_arch_syms) | |
8927 | { | |
8928 | typedef bfd_boolean (*out_sym_func) | |
8929 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8930 | struct elf_link_hash_entry *); | |
8931 | ||
8932 | if (! ((*bed->elf_backend_output_arch_syms) | |
8933 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8934 | return FALSE; | |
8935 | } | |
8936 | ||
8937 | /* Flush all symbols to the file. */ | |
8938 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
8939 | return FALSE; | |
8940 | ||
8941 | /* Now we know the size of the symtab section. */ | |
8942 | off += symtab_hdr->sh_size; | |
8943 | ||
8944 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
8945 | if (symtab_shndx_hdr->sh_name != 0) | |
8946 | { | |
8947 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8948 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8949 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8950 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
8951 | symtab_shndx_hdr->sh_size = amt; | |
8952 | ||
8953 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
8954 | off, TRUE); | |
8955 | ||
8956 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
8957 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
8958 | return FALSE; | |
8959 | } | |
8960 | ||
8961 | ||
8962 | /* Finish up and write out the symbol string table (.strtab) | |
8963 | section. */ | |
8964 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8965 | /* sh_name was set in prep_headers. */ | |
8966 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8967 | symstrtab_hdr->sh_flags = 0; | |
8968 | symstrtab_hdr->sh_addr = 0; | |
8969 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
8970 | symstrtab_hdr->sh_entsize = 0; | |
8971 | symstrtab_hdr->sh_link = 0; | |
8972 | symstrtab_hdr->sh_info = 0; | |
8973 | /* sh_offset is set just below. */ | |
8974 | symstrtab_hdr->sh_addralign = 1; | |
8975 | ||
8976 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
8977 | elf_tdata (abfd)->next_file_pos = off; | |
8978 | ||
8979 | if (bfd_get_symcount (abfd) > 0) | |
8980 | { | |
8981 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
8982 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
8983 | return FALSE; | |
8984 | } | |
8985 | ||
8986 | /* Adjust the relocs to have the correct symbol indices. */ | |
8987 | for (o = abfd->sections; o != NULL; o = o->next) | |
8988 | { | |
8989 | if ((o->flags & SEC_RELOC) == 0) | |
8990 | continue; | |
8991 | ||
8992 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
8993 | elf_section_data (o)->rel_count, | |
8994 | elf_section_data (o)->rel_hashes); | |
8995 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
8996 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
8997 | elf_section_data (o)->rel_count2, | |
8998 | (elf_section_data (o)->rel_hashes | |
8999 | + elf_section_data (o)->rel_count)); | |
9000 | ||
9001 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
9002 | trying to swap the relocs out itself. */ | |
9003 | o->reloc_count = 0; | |
9004 | } | |
9005 | ||
9006 | if (dynamic && info->combreloc && dynobj != NULL) | |
9007 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
9008 | ||
9009 | /* If we are linking against a dynamic object, or generating a | |
9010 | shared library, finish up the dynamic linking information. */ | |
9011 | if (dynamic) | |
9012 | { | |
9013 | bfd_byte *dyncon, *dynconend; | |
9014 | ||
9015 | /* Fix up .dynamic entries. */ | |
9016 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
9017 | BFD_ASSERT (o != NULL); | |
9018 | ||
9019 | dyncon = o->contents; | |
eea6121a | 9020 | dynconend = o->contents + o->size; |
c152c796 AM |
9021 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
9022 | { | |
9023 | Elf_Internal_Dyn dyn; | |
9024 | const char *name; | |
9025 | unsigned int type; | |
9026 | ||
9027 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
9028 | ||
9029 | switch (dyn.d_tag) | |
9030 | { | |
9031 | default: | |
9032 | continue; | |
9033 | case DT_NULL: | |
9034 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
9035 | { | |
9036 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
9037 | { | |
9038 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
9039 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
9040 | default: continue; | |
9041 | } | |
9042 | dyn.d_un.d_val = relativecount; | |
9043 | relativecount = 0; | |
9044 | break; | |
9045 | } | |
9046 | continue; | |
9047 | ||
9048 | case DT_INIT: | |
9049 | name = info->init_function; | |
9050 | goto get_sym; | |
9051 | case DT_FINI: | |
9052 | name = info->fini_function; | |
9053 | get_sym: | |
9054 | { | |
9055 | struct elf_link_hash_entry *h; | |
9056 | ||
9057 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
9058 | FALSE, FALSE, TRUE); | |
9059 | if (h != NULL | |
9060 | && (h->root.type == bfd_link_hash_defined | |
9061 | || h->root.type == bfd_link_hash_defweak)) | |
9062 | { | |
9063 | dyn.d_un.d_val = h->root.u.def.value; | |
9064 | o = h->root.u.def.section; | |
9065 | if (o->output_section != NULL) | |
9066 | dyn.d_un.d_val += (o->output_section->vma | |
9067 | + o->output_offset); | |
9068 | else | |
9069 | { | |
9070 | /* The symbol is imported from another shared | |
9071 | library and does not apply to this one. */ | |
9072 | dyn.d_un.d_val = 0; | |
9073 | } | |
9074 | break; | |
9075 | } | |
9076 | } | |
9077 | continue; | |
9078 | ||
9079 | case DT_PREINIT_ARRAYSZ: | |
9080 | name = ".preinit_array"; | |
9081 | goto get_size; | |
9082 | case DT_INIT_ARRAYSZ: | |
9083 | name = ".init_array"; | |
9084 | goto get_size; | |
9085 | case DT_FINI_ARRAYSZ: | |
9086 | name = ".fini_array"; | |
9087 | get_size: | |
9088 | o = bfd_get_section_by_name (abfd, name); | |
9089 | if (o == NULL) | |
9090 | { | |
9091 | (*_bfd_error_handler) | |
d003868e | 9092 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
9093 | goto error_return; |
9094 | } | |
eea6121a | 9095 | if (o->size == 0) |
c152c796 AM |
9096 | (*_bfd_error_handler) |
9097 | (_("warning: %s section has zero size"), name); | |
eea6121a | 9098 | dyn.d_un.d_val = o->size; |
c152c796 AM |
9099 | break; |
9100 | ||
9101 | case DT_PREINIT_ARRAY: | |
9102 | name = ".preinit_array"; | |
9103 | goto get_vma; | |
9104 | case DT_INIT_ARRAY: | |
9105 | name = ".init_array"; | |
9106 | goto get_vma; | |
9107 | case DT_FINI_ARRAY: | |
9108 | name = ".fini_array"; | |
9109 | goto get_vma; | |
9110 | ||
9111 | case DT_HASH: | |
9112 | name = ".hash"; | |
9113 | goto get_vma; | |
fdc90cb4 JJ |
9114 | case DT_GNU_HASH: |
9115 | name = ".gnu.hash"; | |
9116 | goto get_vma; | |
c152c796 AM |
9117 | case DT_STRTAB: |
9118 | name = ".dynstr"; | |
9119 | goto get_vma; | |
9120 | case DT_SYMTAB: | |
9121 | name = ".dynsym"; | |
9122 | goto get_vma; | |
9123 | case DT_VERDEF: | |
9124 | name = ".gnu.version_d"; | |
9125 | goto get_vma; | |
9126 | case DT_VERNEED: | |
9127 | name = ".gnu.version_r"; | |
9128 | goto get_vma; | |
9129 | case DT_VERSYM: | |
9130 | name = ".gnu.version"; | |
9131 | get_vma: | |
9132 | o = bfd_get_section_by_name (abfd, name); | |
9133 | if (o == NULL) | |
9134 | { | |
9135 | (*_bfd_error_handler) | |
d003868e | 9136 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
9137 | goto error_return; |
9138 | } | |
9139 | dyn.d_un.d_ptr = o->vma; | |
9140 | break; | |
9141 | ||
9142 | case DT_REL: | |
9143 | case DT_RELA: | |
9144 | case DT_RELSZ: | |
9145 | case DT_RELASZ: | |
9146 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
9147 | type = SHT_REL; | |
9148 | else | |
9149 | type = SHT_RELA; | |
9150 | dyn.d_un.d_val = 0; | |
9151 | for (i = 1; i < elf_numsections (abfd); i++) | |
9152 | { | |
9153 | Elf_Internal_Shdr *hdr; | |
9154 | ||
9155 | hdr = elf_elfsections (abfd)[i]; | |
9156 | if (hdr->sh_type == type | |
9157 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
9158 | { | |
9159 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
9160 | dyn.d_un.d_val += hdr->sh_size; | |
9161 | else | |
9162 | { | |
9163 | if (dyn.d_un.d_val == 0 | |
9164 | || hdr->sh_addr < dyn.d_un.d_val) | |
9165 | dyn.d_un.d_val = hdr->sh_addr; | |
9166 | } | |
9167 | } | |
9168 | } | |
9169 | break; | |
9170 | } | |
9171 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
9172 | } | |
9173 | } | |
9174 | ||
9175 | /* If we have created any dynamic sections, then output them. */ | |
9176 | if (dynobj != NULL) | |
9177 | { | |
9178 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
9179 | goto error_return; | |
9180 | ||
943284cc DJ |
9181 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
9182 | if (info->warn_shared_textrel && info->shared) | |
9183 | { | |
9184 | bfd_byte *dyncon, *dynconend; | |
9185 | ||
9186 | /* Fix up .dynamic entries. */ | |
9187 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
9188 | BFD_ASSERT (o != NULL); | |
9189 | ||
9190 | dyncon = o->contents; | |
9191 | dynconend = o->contents + o->size; | |
9192 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
9193 | { | |
9194 | Elf_Internal_Dyn dyn; | |
9195 | ||
9196 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
9197 | ||
9198 | if (dyn.d_tag == DT_TEXTREL) | |
9199 | { | |
9200 | _bfd_error_handler | |
9201 | (_("warning: creating a DT_TEXTREL in a shared object.")); | |
9202 | break; | |
9203 | } | |
9204 | } | |
9205 | } | |
9206 | ||
c152c796 AM |
9207 | for (o = dynobj->sections; o != NULL; o = o->next) |
9208 | { | |
9209 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 9210 | || o->size == 0 |
c152c796 AM |
9211 | || o->output_section == bfd_abs_section_ptr) |
9212 | continue; | |
9213 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
9214 | { | |
9215 | /* At this point, we are only interested in sections | |
9216 | created by _bfd_elf_link_create_dynamic_sections. */ | |
9217 | continue; | |
9218 | } | |
3722b82f AM |
9219 | if (elf_hash_table (info)->stab_info.stabstr == o) |
9220 | continue; | |
eea6121a AM |
9221 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
9222 | continue; | |
c152c796 AM |
9223 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
9224 | != SHT_STRTAB) | |
9225 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
9226 | { | |
9227 | if (! bfd_set_section_contents (abfd, o->output_section, | |
9228 | o->contents, | |
9229 | (file_ptr) o->output_offset, | |
eea6121a | 9230 | o->size)) |
c152c796 AM |
9231 | goto error_return; |
9232 | } | |
9233 | else | |
9234 | { | |
9235 | /* The contents of the .dynstr section are actually in a | |
9236 | stringtab. */ | |
9237 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
9238 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
9239 | || ! _bfd_elf_strtab_emit (abfd, | |
9240 | elf_hash_table (info)->dynstr)) | |
9241 | goto error_return; | |
9242 | } | |
9243 | } | |
9244 | } | |
9245 | ||
9246 | if (info->relocatable) | |
9247 | { | |
9248 | bfd_boolean failed = FALSE; | |
9249 | ||
9250 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
9251 | if (failed) | |
9252 | goto error_return; | |
9253 | } | |
9254 | ||
9255 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 9256 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
9257 | { |
9258 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
9259 | goto error_return; | |
9260 | } | |
9261 | ||
9262 | if (info->eh_frame_hdr) | |
9263 | { | |
9264 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
9265 | goto error_return; | |
9266 | } | |
9267 | ||
9268 | if (finfo.symstrtab != NULL) | |
9269 | _bfd_stringtab_free (finfo.symstrtab); | |
9270 | if (finfo.contents != NULL) | |
9271 | free (finfo.contents); | |
9272 | if (finfo.external_relocs != NULL) | |
9273 | free (finfo.external_relocs); | |
9274 | if (finfo.internal_relocs != NULL) | |
9275 | free (finfo.internal_relocs); | |
9276 | if (finfo.external_syms != NULL) | |
9277 | free (finfo.external_syms); | |
9278 | if (finfo.locsym_shndx != NULL) | |
9279 | free (finfo.locsym_shndx); | |
9280 | if (finfo.internal_syms != NULL) | |
9281 | free (finfo.internal_syms); | |
9282 | if (finfo.indices != NULL) | |
9283 | free (finfo.indices); | |
9284 | if (finfo.sections != NULL) | |
9285 | free (finfo.sections); | |
9286 | if (finfo.symbuf != NULL) | |
9287 | free (finfo.symbuf); | |
9288 | if (finfo.symshndxbuf != NULL) | |
9289 | free (finfo.symshndxbuf); | |
9290 | for (o = abfd->sections; o != NULL; o = o->next) | |
9291 | { | |
9292 | if ((o->flags & SEC_RELOC) != 0 | |
9293 | && elf_section_data (o)->rel_hashes != NULL) | |
9294 | free (elf_section_data (o)->rel_hashes); | |
9295 | } | |
9296 | ||
9297 | elf_tdata (abfd)->linker = TRUE; | |
9298 | ||
9299 | return TRUE; | |
9300 | ||
9301 | error_return: | |
9302 | if (finfo.symstrtab != NULL) | |
9303 | _bfd_stringtab_free (finfo.symstrtab); | |
9304 | if (finfo.contents != NULL) | |
9305 | free (finfo.contents); | |
9306 | if (finfo.external_relocs != NULL) | |
9307 | free (finfo.external_relocs); | |
9308 | if (finfo.internal_relocs != NULL) | |
9309 | free (finfo.internal_relocs); | |
9310 | if (finfo.external_syms != NULL) | |
9311 | free (finfo.external_syms); | |
9312 | if (finfo.locsym_shndx != NULL) | |
9313 | free (finfo.locsym_shndx); | |
9314 | if (finfo.internal_syms != NULL) | |
9315 | free (finfo.internal_syms); | |
9316 | if (finfo.indices != NULL) | |
9317 | free (finfo.indices); | |
9318 | if (finfo.sections != NULL) | |
9319 | free (finfo.sections); | |
9320 | if (finfo.symbuf != NULL) | |
9321 | free (finfo.symbuf); | |
9322 | if (finfo.symshndxbuf != NULL) | |
9323 | free (finfo.symshndxbuf); | |
9324 | for (o = abfd->sections; o != NULL; o = o->next) | |
9325 | { | |
9326 | if ((o->flags & SEC_RELOC) != 0 | |
9327 | && elf_section_data (o)->rel_hashes != NULL) | |
9328 | free (elf_section_data (o)->rel_hashes); | |
9329 | } | |
9330 | ||
9331 | return FALSE; | |
9332 | } | |
9333 | \f | |
9334 | /* Garbage collect unused sections. */ | |
9335 | ||
c152c796 AM |
9336 | typedef asection * (*gc_mark_hook_fn) |
9337 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
9338 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
9339 | ||
07adf181 AM |
9340 | /* Default gc_mark_hook. */ |
9341 | ||
9342 | asection * | |
9343 | _bfd_elf_gc_mark_hook (asection *sec, | |
9344 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
9345 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
9346 | struct elf_link_hash_entry *h, | |
9347 | Elf_Internal_Sym *sym) | |
9348 | { | |
9349 | if (h != NULL) | |
9350 | { | |
9351 | switch (h->root.type) | |
9352 | { | |
9353 | case bfd_link_hash_defined: | |
9354 | case bfd_link_hash_defweak: | |
9355 | return h->root.u.def.section; | |
9356 | ||
9357 | case bfd_link_hash_common: | |
9358 | return h->root.u.c.p->section; | |
9359 | ||
9360 | default: | |
9361 | break; | |
9362 | } | |
9363 | } | |
9364 | else | |
9365 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
9366 | ||
9367 | return NULL; | |
9368 | } | |
9369 | ||
9370 | /* The mark phase of garbage collection. For a given section, mark | |
9371 | it and any sections in this section's group, and all the sections | |
9372 | which define symbols to which it refers. */ | |
9373 | ||
ccfa59ea AM |
9374 | bfd_boolean |
9375 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
9376 | asection *sec, | |
9377 | gc_mark_hook_fn gc_mark_hook) | |
c152c796 AM |
9378 | { |
9379 | bfd_boolean ret; | |
39c2f51b | 9380 | bfd_boolean is_eh; |
c152c796 AM |
9381 | asection *group_sec; |
9382 | ||
9383 | sec->gc_mark = 1; | |
9384 | ||
9385 | /* Mark all the sections in the group. */ | |
9386 | group_sec = elf_section_data (sec)->next_in_group; | |
9387 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 9388 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
9389 | return FALSE; |
9390 | ||
9391 | /* Look through the section relocs. */ | |
9392 | ret = TRUE; | |
39c2f51b | 9393 | is_eh = strcmp (sec->name, ".eh_frame") == 0; |
c152c796 AM |
9394 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) |
9395 | { | |
9396 | Elf_Internal_Rela *relstart, *rel, *relend; | |
9397 | Elf_Internal_Shdr *symtab_hdr; | |
9398 | struct elf_link_hash_entry **sym_hashes; | |
9399 | size_t nlocsyms; | |
9400 | size_t extsymoff; | |
9401 | bfd *input_bfd = sec->owner; | |
9402 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
9403 | Elf_Internal_Sym *isym = NULL; | |
9404 | int r_sym_shift; | |
9405 | ||
9406 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
9407 | sym_hashes = elf_sym_hashes (input_bfd); | |
9408 | ||
9409 | /* Read the local symbols. */ | |
9410 | if (elf_bad_symtab (input_bfd)) | |
9411 | { | |
9412 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9413 | extsymoff = 0; | |
9414 | } | |
9415 | else | |
9416 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
9417 | ||
9418 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9419 | if (isym == NULL && nlocsyms != 0) | |
9420 | { | |
9421 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
9422 | NULL, NULL, NULL); | |
9423 | if (isym == NULL) | |
9424 | return FALSE; | |
9425 | } | |
9426 | ||
9427 | /* Read the relocations. */ | |
9428 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
9429 | info->keep_memory); | |
9430 | if (relstart == NULL) | |
9431 | { | |
9432 | ret = FALSE; | |
9433 | goto out1; | |
9434 | } | |
9435 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
9436 | ||
9437 | if (bed->s->arch_size == 32) | |
9438 | r_sym_shift = 8; | |
9439 | else | |
9440 | r_sym_shift = 32; | |
9441 | ||
9442 | for (rel = relstart; rel < relend; rel++) | |
9443 | { | |
9444 | unsigned long r_symndx; | |
9445 | asection *rsec; | |
9446 | struct elf_link_hash_entry *h; | |
9447 | ||
9448 | r_symndx = rel->r_info >> r_sym_shift; | |
9449 | if (r_symndx == 0) | |
9450 | continue; | |
9451 | ||
9452 | if (r_symndx >= nlocsyms | |
9453 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
9454 | { | |
9455 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
9456 | while (h->root.type == bfd_link_hash_indirect |
9457 | || h->root.type == bfd_link_hash_warning) | |
9458 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
9459 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
9460 | } | |
9461 | else | |
9462 | { | |
9463 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
9464 | } | |
9465 | ||
9466 | if (rsec && !rsec->gc_mark) | |
9467 | { | |
9468 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
9469 | rsec->gc_mark = 1; | |
39c2f51b AM |
9470 | else if (is_eh) |
9471 | rsec->gc_mark_from_eh = 1; | |
ccfa59ea | 9472 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
9473 | { |
9474 | ret = FALSE; | |
9475 | goto out2; | |
9476 | } | |
9477 | } | |
9478 | } | |
9479 | ||
9480 | out2: | |
9481 | if (elf_section_data (sec)->relocs != relstart) | |
9482 | free (relstart); | |
9483 | out1: | |
9484 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
9485 | { | |
9486 | if (! info->keep_memory) | |
9487 | free (isym); | |
9488 | else | |
9489 | symtab_hdr->contents = (unsigned char *) isym; | |
9490 | } | |
9491 | } | |
9492 | ||
9493 | return ret; | |
9494 | } | |
9495 | ||
9496 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
9497 | ||
c17d87de NC |
9498 | struct elf_gc_sweep_symbol_info |
9499 | { | |
ccabcbe5 AM |
9500 | struct bfd_link_info *info; |
9501 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
9502 | bfd_boolean); | |
9503 | }; | |
9504 | ||
c152c796 | 9505 | static bfd_boolean |
ccabcbe5 | 9506 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 9507 | { |
c152c796 AM |
9508 | if (h->root.type == bfd_link_hash_warning) |
9509 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9510 | ||
ccabcbe5 AM |
9511 | if ((h->root.type == bfd_link_hash_defined |
9512 | || h->root.type == bfd_link_hash_defweak) | |
9513 | && !h->root.u.def.section->gc_mark | |
9514 | && !(h->root.u.def.section->owner->flags & DYNAMIC)) | |
9515 | { | |
9516 | struct elf_gc_sweep_symbol_info *inf = data; | |
9517 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
9518 | } | |
c152c796 AM |
9519 | |
9520 | return TRUE; | |
9521 | } | |
9522 | ||
9523 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
9524 | ||
9525 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
9526 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
9527 | ||
9528 | static bfd_boolean | |
ccabcbe5 | 9529 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
9530 | { |
9531 | bfd *sub; | |
ccabcbe5 AM |
9532 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9533 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
9534 | unsigned long section_sym_count; | |
9535 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 AM |
9536 | |
9537 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9538 | { | |
9539 | asection *o; | |
9540 | ||
9541 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9542 | continue; | |
9543 | ||
9544 | for (o = sub->sections; o != NULL; o = o->next) | |
9545 | { | |
7c2c8505 AM |
9546 | /* Keep debug and special sections. */ |
9547 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
dea5f36a | 9548 | || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) |
c152c796 AM |
9549 | o->gc_mark = 1; |
9550 | ||
9551 | if (o->gc_mark) | |
9552 | continue; | |
9553 | ||
9554 | /* Skip sweeping sections already excluded. */ | |
9555 | if (o->flags & SEC_EXCLUDE) | |
9556 | continue; | |
9557 | ||
9558 | /* Since this is early in the link process, it is simple | |
9559 | to remove a section from the output. */ | |
9560 | o->flags |= SEC_EXCLUDE; | |
9561 | ||
c17d87de NC |
9562 | if (info->print_gc_sections == TRUE) |
9563 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); | |
9564 | ||
c152c796 AM |
9565 | /* But we also have to update some of the relocation |
9566 | info we collected before. */ | |
9567 | if (gc_sweep_hook | |
e8aaee2a AM |
9568 | && (o->flags & SEC_RELOC) != 0 |
9569 | && o->reloc_count > 0 | |
9570 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
9571 | { |
9572 | Elf_Internal_Rela *internal_relocs; | |
9573 | bfd_boolean r; | |
9574 | ||
9575 | internal_relocs | |
9576 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
9577 | info->keep_memory); | |
9578 | if (internal_relocs == NULL) | |
9579 | return FALSE; | |
9580 | ||
9581 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
9582 | ||
9583 | if (elf_section_data (o)->relocs != internal_relocs) | |
9584 | free (internal_relocs); | |
9585 | ||
9586 | if (!r) | |
9587 | return FALSE; | |
9588 | } | |
9589 | } | |
9590 | } | |
9591 | ||
9592 | /* Remove the symbols that were in the swept sections from the dynamic | |
9593 | symbol table. GCFIXME: Anyone know how to get them out of the | |
9594 | static symbol table as well? */ | |
ccabcbe5 AM |
9595 | sweep_info.info = info; |
9596 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
9597 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
9598 | &sweep_info); | |
c152c796 | 9599 | |
ccabcbe5 | 9600 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
9601 | return TRUE; |
9602 | } | |
9603 | ||
9604 | /* Propagate collected vtable information. This is called through | |
9605 | elf_link_hash_traverse. */ | |
9606 | ||
9607 | static bfd_boolean | |
9608 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
9609 | { | |
9610 | if (h->root.type == bfd_link_hash_warning) | |
9611 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9612 | ||
9613 | /* Those that are not vtables. */ | |
f6e332e6 | 9614 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
9615 | return TRUE; |
9616 | ||
9617 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 9618 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
9619 | return TRUE; |
9620 | ||
9621 | /* If we've already been done, exit. */ | |
f6e332e6 | 9622 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
9623 | return TRUE; |
9624 | ||
9625 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 9626 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 9627 | |
f6e332e6 | 9628 | if (h->vtable->used == NULL) |
c152c796 AM |
9629 | { |
9630 | /* None of this table's entries were referenced. Re-use the | |
9631 | parent's table. */ | |
f6e332e6 AM |
9632 | h->vtable->used = h->vtable->parent->vtable->used; |
9633 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
9634 | } |
9635 | else | |
9636 | { | |
9637 | size_t n; | |
9638 | bfd_boolean *cu, *pu; | |
9639 | ||
9640 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 9641 | cu = h->vtable->used; |
c152c796 | 9642 | cu[-1] = TRUE; |
f6e332e6 | 9643 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
9644 | if (pu != NULL) |
9645 | { | |
9646 | const struct elf_backend_data *bed; | |
9647 | unsigned int log_file_align; | |
9648 | ||
9649 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
9650 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 9651 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
9652 | while (n--) |
9653 | { | |
9654 | if (*pu) | |
9655 | *cu = TRUE; | |
9656 | pu++; | |
9657 | cu++; | |
9658 | } | |
9659 | } | |
9660 | } | |
9661 | ||
9662 | return TRUE; | |
9663 | } | |
9664 | ||
9665 | static bfd_boolean | |
9666 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
9667 | { | |
9668 | asection *sec; | |
9669 | bfd_vma hstart, hend; | |
9670 | Elf_Internal_Rela *relstart, *relend, *rel; | |
9671 | const struct elf_backend_data *bed; | |
9672 | unsigned int log_file_align; | |
9673 | ||
9674 | if (h->root.type == bfd_link_hash_warning) | |
9675 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9676 | ||
9677 | /* Take care of both those symbols that do not describe vtables as | |
9678 | well as those that are not loaded. */ | |
f6e332e6 | 9679 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
9680 | return TRUE; |
9681 | ||
9682 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
9683 | || h->root.type == bfd_link_hash_defweak); | |
9684 | ||
9685 | sec = h->root.u.def.section; | |
9686 | hstart = h->root.u.def.value; | |
9687 | hend = hstart + h->size; | |
9688 | ||
9689 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
9690 | if (!relstart) | |
9691 | return *(bfd_boolean *) okp = FALSE; | |
9692 | bed = get_elf_backend_data (sec->owner); | |
9693 | log_file_align = bed->s->log_file_align; | |
9694 | ||
9695 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
9696 | ||
9697 | for (rel = relstart; rel < relend; ++rel) | |
9698 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
9699 | { | |
9700 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
9701 | if (h->vtable->used |
9702 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
9703 | { |
9704 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 9705 | if (h->vtable->used[entry]) |
c152c796 AM |
9706 | continue; |
9707 | } | |
9708 | /* Otherwise, kill it. */ | |
9709 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
9710 | } | |
9711 | ||
9712 | return TRUE; | |
9713 | } | |
9714 | ||
87538722 AM |
9715 | /* Mark sections containing dynamically referenced symbols. When |
9716 | building shared libraries, we must assume that any visible symbol is | |
9717 | referenced. */ | |
715df9b8 | 9718 | |
64d03ab5 AM |
9719 | bfd_boolean |
9720 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 9721 | { |
87538722 AM |
9722 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
9723 | ||
715df9b8 EB |
9724 | if (h->root.type == bfd_link_hash_warning) |
9725 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9726 | ||
9727 | if ((h->root.type == bfd_link_hash_defined | |
9728 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 9729 | && (h->ref_dynamic |
5adcfd8b | 9730 | || (!info->executable |
87538722 AM |
9731 | && h->def_regular |
9732 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
9733 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) | |
715df9b8 EB |
9734 | h->root.u.def.section->flags |= SEC_KEEP; |
9735 | ||
9736 | return TRUE; | |
9737 | } | |
3b36f7e6 | 9738 | |
c152c796 AM |
9739 | /* Do mark and sweep of unused sections. */ |
9740 | ||
9741 | bfd_boolean | |
9742 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
9743 | { | |
9744 | bfd_boolean ok = TRUE; | |
9745 | bfd *sub; | |
9746 | asection * (*gc_mark_hook) | |
9747 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
9748 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
64d03ab5 | 9749 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 9750 | |
64d03ab5 | 9751 | if (!bed->can_gc_sections |
c152c796 AM |
9752 | || info->relocatable |
9753 | || info->emitrelocations | |
715df9b8 | 9754 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
9755 | { |
9756 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
9757 | return TRUE; | |
9758 | } | |
9759 | ||
9760 | /* Apply transitive closure to the vtable entry usage info. */ | |
9761 | elf_link_hash_traverse (elf_hash_table (info), | |
9762 | elf_gc_propagate_vtable_entries_used, | |
9763 | &ok); | |
9764 | if (!ok) | |
9765 | return FALSE; | |
9766 | ||
9767 | /* Kill the vtable relocations that were not used. */ | |
9768 | elf_link_hash_traverse (elf_hash_table (info), | |
9769 | elf_gc_smash_unused_vtentry_relocs, | |
9770 | &ok); | |
9771 | if (!ok) | |
9772 | return FALSE; | |
9773 | ||
715df9b8 EB |
9774 | /* Mark dynamically referenced symbols. */ |
9775 | if (elf_hash_table (info)->dynamic_sections_created) | |
9776 | elf_link_hash_traverse (elf_hash_table (info), | |
64d03ab5 | 9777 | bed->gc_mark_dynamic_ref, |
87538722 | 9778 | info); |
c152c796 | 9779 | |
715df9b8 | 9780 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 9781 | gc_mark_hook = bed->gc_mark_hook; |
c152c796 AM |
9782 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
9783 | { | |
9784 | asection *o; | |
9785 | ||
9786 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9787 | continue; | |
9788 | ||
9789 | for (o = sub->sections; o != NULL; o = o->next) | |
39c2f51b AM |
9790 | if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark) |
9791 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
9792 | return FALSE; | |
c152c796 AM |
9793 | } |
9794 | ||
9e8cc8b4 AM |
9795 | /* ... again for sections marked from eh_frame. */ |
9796 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
9797 | { | |
9798 | asection *o; | |
9799 | ||
9800 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
9801 | continue; | |
9802 | ||
9803 | /* Keep .gcc_except_table.* if the associated .text.* is | |
9804 | marked. This isn't very nice, but the proper solution, | |
2de92251 | 9805 | splitting .eh_frame up and using comdat doesn't pan out |
9e8cc8b4 AM |
9806 | easily due to needing special relocs to handle the |
9807 | difference of two symbols in separate sections. | |
9808 | Don't keep code sections referenced by .eh_frame. */ | |
ea9986ff NC |
9809 | #define TEXT_PREFIX ".text." |
9810 | #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table." | |
9e8cc8b4 AM |
9811 | for (o = sub->sections; o != NULL; o = o->next) |
9812 | if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0) | |
9813 | { | |
ea9986ff | 9814 | if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX)) |
9e8cc8b4 | 9815 | { |
9e8cc8b4 | 9816 | char *fn_name; |
ea9986ff | 9817 | const char *sec_name; |
9e8cc8b4 | 9818 | asection *fn_text; |
ea9986ff NC |
9819 | unsigned o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX); |
9820 | unsigned fn_name_prefix_len = strlen (TEXT_PREFIX); | |
9e8cc8b4 | 9821 | |
ea9986ff NC |
9822 | sec_name = o->name + o_name_prefix_len; |
9823 | fn_name = bfd_malloc (strlen (sec_name) + fn_name_prefix_len + 1); | |
9e8cc8b4 AM |
9824 | if (fn_name == NULL) |
9825 | return FALSE; | |
ea9986ff | 9826 | sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name); |
9e8cc8b4 AM |
9827 | fn_text = bfd_get_section_by_name (sub, fn_name); |
9828 | free (fn_name); | |
9829 | if (fn_text == NULL || !fn_text->gc_mark) | |
9830 | continue; | |
9831 | } | |
9832 | ||
9833 | /* If not using specially named exception table section, | |
9834 | then keep whatever we are using. */ | |
9835 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
9836 | return FALSE; | |
9837 | } | |
9838 | } | |
9839 | ||
c152c796 | 9840 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 9841 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
9842 | } |
9843 | \f | |
9844 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
9845 | ||
9846 | bfd_boolean | |
9847 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
9848 | asection *sec, | |
9849 | struct elf_link_hash_entry *h, | |
9850 | bfd_vma offset) | |
9851 | { | |
9852 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
9853 | struct elf_link_hash_entry **search, *child; | |
9854 | bfd_size_type extsymcount; | |
9855 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9856 | ||
9857 | /* The sh_info field of the symtab header tells us where the | |
9858 | external symbols start. We don't care about the local symbols at | |
9859 | this point. */ | |
9860 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
9861 | if (!elf_bad_symtab (abfd)) | |
9862 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
9863 | ||
9864 | sym_hashes = elf_sym_hashes (abfd); | |
9865 | sym_hashes_end = sym_hashes + extsymcount; | |
9866 | ||
9867 | /* Hunt down the child symbol, which is in this section at the same | |
9868 | offset as the relocation. */ | |
9869 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
9870 | { | |
9871 | if ((child = *search) != NULL | |
9872 | && (child->root.type == bfd_link_hash_defined | |
9873 | || child->root.type == bfd_link_hash_defweak) | |
9874 | && child->root.u.def.section == sec | |
9875 | && child->root.u.def.value == offset) | |
9876 | goto win; | |
9877 | } | |
9878 | ||
d003868e AM |
9879 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
9880 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
9881 | bfd_set_error (bfd_error_invalid_operation); |
9882 | return FALSE; | |
9883 | ||
9884 | win: | |
f6e332e6 AM |
9885 | if (!child->vtable) |
9886 | { | |
9887 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
9888 | if (!child->vtable) | |
9889 | return FALSE; | |
9890 | } | |
c152c796 AM |
9891 | if (!h) |
9892 | { | |
9893 | /* This *should* only be the absolute section. It could potentially | |
9894 | be that someone has defined a non-global vtable though, which | |
9895 | would be bad. It isn't worth paging in the local symbols to be | |
9896 | sure though; that case should simply be handled by the assembler. */ | |
9897 | ||
f6e332e6 | 9898 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
9899 | } |
9900 | else | |
f6e332e6 | 9901 | child->vtable->parent = h; |
c152c796 AM |
9902 | |
9903 | return TRUE; | |
9904 | } | |
9905 | ||
9906 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
9907 | ||
9908 | bfd_boolean | |
9909 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
9910 | asection *sec ATTRIBUTE_UNUSED, | |
9911 | struct elf_link_hash_entry *h, | |
9912 | bfd_vma addend) | |
9913 | { | |
9914 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9915 | unsigned int log_file_align = bed->s->log_file_align; | |
9916 | ||
f6e332e6 AM |
9917 | if (!h->vtable) |
9918 | { | |
9919 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
9920 | if (!h->vtable) | |
9921 | return FALSE; | |
9922 | } | |
9923 | ||
9924 | if (addend >= h->vtable->size) | |
c152c796 AM |
9925 | { |
9926 | size_t size, bytes, file_align; | |
f6e332e6 | 9927 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
9928 | |
9929 | /* While the symbol is undefined, we have to be prepared to handle | |
9930 | a zero size. */ | |
9931 | file_align = 1 << log_file_align; | |
9932 | if (h->root.type == bfd_link_hash_undefined) | |
9933 | size = addend + file_align; | |
9934 | else | |
9935 | { | |
9936 | size = h->size; | |
9937 | if (addend >= size) | |
9938 | { | |
9939 | /* Oops! We've got a reference past the defined end of | |
9940 | the table. This is probably a bug -- shall we warn? */ | |
9941 | size = addend + file_align; | |
9942 | } | |
9943 | } | |
9944 | size = (size + file_align - 1) & -file_align; | |
9945 | ||
9946 | /* Allocate one extra entry for use as a "done" flag for the | |
9947 | consolidation pass. */ | |
9948 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
9949 | ||
9950 | if (ptr) | |
9951 | { | |
9952 | ptr = bfd_realloc (ptr - 1, bytes); | |
9953 | ||
9954 | if (ptr != NULL) | |
9955 | { | |
9956 | size_t oldbytes; | |
9957 | ||
f6e332e6 | 9958 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
9959 | * sizeof (bfd_boolean)); |
9960 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
9961 | } | |
9962 | } | |
9963 | else | |
9964 | ptr = bfd_zmalloc (bytes); | |
9965 | ||
9966 | if (ptr == NULL) | |
9967 | return FALSE; | |
9968 | ||
9969 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
9970 | h->vtable->used = ptr + 1; |
9971 | h->vtable->size = size; | |
c152c796 AM |
9972 | } |
9973 | ||
f6e332e6 | 9974 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
9975 | |
9976 | return TRUE; | |
9977 | } | |
9978 | ||
9979 | struct alloc_got_off_arg { | |
9980 | bfd_vma gotoff; | |
9981 | unsigned int got_elt_size; | |
9982 | }; | |
9983 | ||
9984 | /* We need a special top-level link routine to convert got reference counts | |
9985 | to real got offsets. */ | |
9986 | ||
9987 | static bfd_boolean | |
9988 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
9989 | { | |
9990 | struct alloc_got_off_arg *gofarg = arg; | |
9991 | ||
9992 | if (h->root.type == bfd_link_hash_warning) | |
9993 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9994 | ||
9995 | if (h->got.refcount > 0) | |
9996 | { | |
9997 | h->got.offset = gofarg->gotoff; | |
9998 | gofarg->gotoff += gofarg->got_elt_size; | |
9999 | } | |
10000 | else | |
10001 | h->got.offset = (bfd_vma) -1; | |
10002 | ||
10003 | return TRUE; | |
10004 | } | |
10005 | ||
10006 | /* And an accompanying bit to work out final got entry offsets once | |
10007 | we're done. Should be called from final_link. */ | |
10008 | ||
10009 | bfd_boolean | |
10010 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
10011 | struct bfd_link_info *info) | |
10012 | { | |
10013 | bfd *i; | |
10014 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10015 | bfd_vma gotoff; | |
10016 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
10017 | struct alloc_got_off_arg gofarg; | |
10018 | ||
10019 | if (! is_elf_hash_table (info->hash)) | |
10020 | return FALSE; | |
10021 | ||
10022 | /* The GOT offset is relative to the .got section, but the GOT header is | |
10023 | put into the .got.plt section, if the backend uses it. */ | |
10024 | if (bed->want_got_plt) | |
10025 | gotoff = 0; | |
10026 | else | |
10027 | gotoff = bed->got_header_size; | |
10028 | ||
10029 | /* Do the local .got entries first. */ | |
10030 | for (i = info->input_bfds; i; i = i->link_next) | |
10031 | { | |
10032 | bfd_signed_vma *local_got; | |
10033 | bfd_size_type j, locsymcount; | |
10034 | Elf_Internal_Shdr *symtab_hdr; | |
10035 | ||
10036 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
10037 | continue; | |
10038 | ||
10039 | local_got = elf_local_got_refcounts (i); | |
10040 | if (!local_got) | |
10041 | continue; | |
10042 | ||
10043 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
10044 | if (elf_bad_symtab (i)) | |
10045 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10046 | else | |
10047 | locsymcount = symtab_hdr->sh_info; | |
10048 | ||
10049 | for (j = 0; j < locsymcount; ++j) | |
10050 | { | |
10051 | if (local_got[j] > 0) | |
10052 | { | |
10053 | local_got[j] = gotoff; | |
10054 | gotoff += got_elt_size; | |
10055 | } | |
10056 | else | |
10057 | local_got[j] = (bfd_vma) -1; | |
10058 | } | |
10059 | } | |
10060 | ||
10061 | /* Then the global .got entries. .plt refcounts are handled by | |
10062 | adjust_dynamic_symbol */ | |
10063 | gofarg.gotoff = gotoff; | |
10064 | gofarg.got_elt_size = got_elt_size; | |
10065 | elf_link_hash_traverse (elf_hash_table (info), | |
10066 | elf_gc_allocate_got_offsets, | |
10067 | &gofarg); | |
10068 | return TRUE; | |
10069 | } | |
10070 | ||
10071 | /* Many folk need no more in the way of final link than this, once | |
10072 | got entry reference counting is enabled. */ | |
10073 | ||
10074 | bfd_boolean | |
10075 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
10076 | { | |
10077 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
10078 | return FALSE; | |
10079 | ||
10080 | /* Invoke the regular ELF backend linker to do all the work. */ | |
10081 | return bfd_elf_final_link (abfd, info); | |
10082 | } | |
10083 | ||
10084 | bfd_boolean | |
10085 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
10086 | { | |
10087 | struct elf_reloc_cookie *rcookie = cookie; | |
10088 | ||
10089 | if (rcookie->bad_symtab) | |
10090 | rcookie->rel = rcookie->rels; | |
10091 | ||
10092 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
10093 | { | |
10094 | unsigned long r_symndx; | |
10095 | ||
10096 | if (! rcookie->bad_symtab) | |
10097 | if (rcookie->rel->r_offset > offset) | |
10098 | return FALSE; | |
10099 | if (rcookie->rel->r_offset != offset) | |
10100 | continue; | |
10101 | ||
10102 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
10103 | if (r_symndx == SHN_UNDEF) | |
10104 | return TRUE; | |
10105 | ||
10106 | if (r_symndx >= rcookie->locsymcount | |
10107 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
10108 | { | |
10109 | struct elf_link_hash_entry *h; | |
10110 | ||
10111 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
10112 | ||
10113 | while (h->root.type == bfd_link_hash_indirect | |
10114 | || h->root.type == bfd_link_hash_warning) | |
10115 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10116 | ||
10117 | if ((h->root.type == bfd_link_hash_defined | |
10118 | || h->root.type == bfd_link_hash_defweak) | |
10119 | && elf_discarded_section (h->root.u.def.section)) | |
10120 | return TRUE; | |
10121 | else | |
10122 | return FALSE; | |
10123 | } | |
10124 | else | |
10125 | { | |
10126 | /* It's not a relocation against a global symbol, | |
10127 | but it could be a relocation against a local | |
10128 | symbol for a discarded section. */ | |
10129 | asection *isec; | |
10130 | Elf_Internal_Sym *isym; | |
10131 | ||
10132 | /* Need to: get the symbol; get the section. */ | |
10133 | isym = &rcookie->locsyms[r_symndx]; | |
10134 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
10135 | { | |
10136 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
10137 | if (isec != NULL && elf_discarded_section (isec)) | |
10138 | return TRUE; | |
10139 | } | |
10140 | } | |
10141 | return FALSE; | |
10142 | } | |
10143 | return FALSE; | |
10144 | } | |
10145 | ||
10146 | /* Discard unneeded references to discarded sections. | |
10147 | Returns TRUE if any section's size was changed. */ | |
10148 | /* This function assumes that the relocations are in sorted order, | |
10149 | which is true for all known assemblers. */ | |
10150 | ||
10151 | bfd_boolean | |
10152 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
10153 | { | |
10154 | struct elf_reloc_cookie cookie; | |
10155 | asection *stab, *eh; | |
10156 | Elf_Internal_Shdr *symtab_hdr; | |
10157 | const struct elf_backend_data *bed; | |
10158 | bfd *abfd; | |
10159 | unsigned int count; | |
10160 | bfd_boolean ret = FALSE; | |
10161 | ||
10162 | if (info->traditional_format | |
10163 | || !is_elf_hash_table (info->hash)) | |
10164 | return FALSE; | |
10165 | ||
10166 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
10167 | { | |
10168 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
10169 | continue; | |
10170 | ||
10171 | bed = get_elf_backend_data (abfd); | |
10172 | ||
10173 | if ((abfd->flags & DYNAMIC) != 0) | |
10174 | continue; | |
10175 | ||
10176 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
10177 | if (info->relocatable | |
10178 | || (eh != NULL | |
eea6121a | 10179 | && (eh->size == 0 |
c152c796 AM |
10180 | || bfd_is_abs_section (eh->output_section)))) |
10181 | eh = NULL; | |
10182 | ||
10183 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
10184 | if (stab != NULL | |
eea6121a | 10185 | && (stab->size == 0 |
c152c796 AM |
10186 | || bfd_is_abs_section (stab->output_section) |
10187 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
10188 | stab = NULL; | |
10189 | ||
10190 | if (stab == NULL | |
10191 | && eh == NULL | |
10192 | && bed->elf_backend_discard_info == NULL) | |
10193 | continue; | |
10194 | ||
10195 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
10196 | cookie.abfd = abfd; | |
10197 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
10198 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
10199 | if (cookie.bad_symtab) | |
10200 | { | |
10201 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10202 | cookie.extsymoff = 0; | |
10203 | } | |
10204 | else | |
10205 | { | |
10206 | cookie.locsymcount = symtab_hdr->sh_info; | |
10207 | cookie.extsymoff = symtab_hdr->sh_info; | |
10208 | } | |
10209 | ||
10210 | if (bed->s->arch_size == 32) | |
10211 | cookie.r_sym_shift = 8; | |
10212 | else | |
10213 | cookie.r_sym_shift = 32; | |
10214 | ||
10215 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10216 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
10217 | { | |
10218 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
10219 | cookie.locsymcount, 0, | |
10220 | NULL, NULL, NULL); | |
10221 | if (cookie.locsyms == NULL) | |
10222 | return FALSE; | |
10223 | } | |
10224 | ||
10225 | if (stab != NULL) | |
10226 | { | |
10227 | cookie.rels = NULL; | |
10228 | count = stab->reloc_count; | |
10229 | if (count != 0) | |
10230 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
10231 | info->keep_memory); | |
10232 | if (cookie.rels != NULL) | |
10233 | { | |
10234 | cookie.rel = cookie.rels; | |
10235 | cookie.relend = cookie.rels; | |
10236 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
10237 | if (_bfd_discard_section_stabs (abfd, stab, | |
10238 | elf_section_data (stab)->sec_info, | |
10239 | bfd_elf_reloc_symbol_deleted_p, | |
10240 | &cookie)) | |
10241 | ret = TRUE; | |
10242 | if (elf_section_data (stab)->relocs != cookie.rels) | |
10243 | free (cookie.rels); | |
10244 | } | |
10245 | } | |
10246 | ||
10247 | if (eh != NULL) | |
10248 | { | |
10249 | cookie.rels = NULL; | |
10250 | count = eh->reloc_count; | |
10251 | if (count != 0) | |
10252 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
10253 | info->keep_memory); | |
10254 | cookie.rel = cookie.rels; | |
10255 | cookie.relend = cookie.rels; | |
10256 | if (cookie.rels != NULL) | |
10257 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
10258 | ||
10259 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
10260 | bfd_elf_reloc_symbol_deleted_p, | |
10261 | &cookie)) | |
10262 | ret = TRUE; | |
10263 | ||
10264 | if (cookie.rels != NULL | |
10265 | && elf_section_data (eh)->relocs != cookie.rels) | |
10266 | free (cookie.rels); | |
10267 | } | |
10268 | ||
10269 | if (bed->elf_backend_discard_info != NULL | |
10270 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
10271 | ret = TRUE; | |
10272 | ||
10273 | if (cookie.locsyms != NULL | |
10274 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
10275 | { | |
10276 | if (! info->keep_memory) | |
10277 | free (cookie.locsyms); | |
10278 | else | |
10279 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
10280 | } | |
10281 | } | |
10282 | ||
10283 | if (info->eh_frame_hdr | |
10284 | && !info->relocatable | |
10285 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
10286 | ret = TRUE; | |
10287 | ||
10288 | return ret; | |
10289 | } | |
082b7297 L |
10290 | |
10291 | void | |
10292 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec) | |
10293 | { | |
10294 | flagword flags; | |
6d2cd210 | 10295 | const char *name, *p; |
082b7297 L |
10296 | struct bfd_section_already_linked *l; |
10297 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 L |
10298 | asection *group; |
10299 | ||
10300 | /* A single member comdat group section may be discarded by a | |
10301 | linkonce section. See below. */ | |
10302 | if (sec->output_section == bfd_abs_section_ptr) | |
10303 | return; | |
082b7297 L |
10304 | |
10305 | flags = sec->flags; | |
3d7f7666 L |
10306 | |
10307 | /* Check if it belongs to a section group. */ | |
10308 | group = elf_sec_group (sec); | |
10309 | ||
10310 | /* Return if it isn't a linkonce section nor a member of a group. A | |
10311 | comdat group section also has SEC_LINK_ONCE set. */ | |
10312 | if ((flags & SEC_LINK_ONCE) == 0 && group == NULL) | |
082b7297 L |
10313 | return; |
10314 | ||
3d7f7666 L |
10315 | if (group) |
10316 | { | |
10317 | /* If this is the member of a single member comdat group, check if | |
10318 | the group should be discarded. */ | |
10319 | if (elf_next_in_group (sec) == sec | |
10320 | && (group->flags & SEC_LINK_ONCE) != 0) | |
10321 | sec = group; | |
10322 | else | |
10323 | return; | |
10324 | } | |
10325 | ||
082b7297 L |
10326 | /* FIXME: When doing a relocatable link, we may have trouble |
10327 | copying relocations in other sections that refer to local symbols | |
10328 | in the section being discarded. Those relocations will have to | |
10329 | be converted somehow; as of this writing I'm not sure that any of | |
10330 | the backends handle that correctly. | |
10331 | ||
10332 | It is tempting to instead not discard link once sections when | |
10333 | doing a relocatable link (technically, they should be discarded | |
10334 | whenever we are building constructors). However, that fails, | |
10335 | because the linker winds up combining all the link once sections | |
10336 | into a single large link once section, which defeats the purpose | |
10337 | of having link once sections in the first place. | |
10338 | ||
10339 | Also, not merging link once sections in a relocatable link | |
10340 | causes trouble for MIPS ELF, which relies on link once semantics | |
10341 | to handle the .reginfo section correctly. */ | |
10342 | ||
10343 | name = bfd_get_section_name (abfd, sec); | |
10344 | ||
0112cd26 | 10345 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
6d2cd210 JJ |
10346 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
10347 | p++; | |
10348 | else | |
10349 | p = name; | |
10350 | ||
10351 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
10352 | |
10353 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
10354 | { | |
10355 | /* We may have 3 different sections on the list: group section, | |
10356 | comdat section and linkonce section. SEC may be a linkonce or | |
10357 | group section. We match a group section with a group section, | |
10358 | a linkonce section with a linkonce section, and ignore comdat | |
10359 | section. */ | |
3d7f7666 | 10360 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 10361 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
10362 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
10363 | { | |
10364 | /* The section has already been linked. See if we should | |
6d2cd210 | 10365 | issue a warning. */ |
082b7297 L |
10366 | switch (flags & SEC_LINK_DUPLICATES) |
10367 | { | |
10368 | default: | |
10369 | abort (); | |
10370 | ||
10371 | case SEC_LINK_DUPLICATES_DISCARD: | |
10372 | break; | |
10373 | ||
10374 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
10375 | (*_bfd_error_handler) | |
c93625e2 | 10376 | (_("%B: ignoring duplicate section `%A'"), |
d003868e | 10377 | abfd, sec); |
082b7297 L |
10378 | break; |
10379 | ||
10380 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
10381 | if (sec->size != l->sec->size) | |
10382 | (*_bfd_error_handler) | |
c93625e2 | 10383 | (_("%B: duplicate section `%A' has different size"), |
d003868e | 10384 | abfd, sec); |
082b7297 | 10385 | break; |
ea5158d8 DJ |
10386 | |
10387 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
10388 | if (sec->size != l->sec->size) | |
10389 | (*_bfd_error_handler) | |
c93625e2 | 10390 | (_("%B: duplicate section `%A' has different size"), |
ea5158d8 DJ |
10391 | abfd, sec); |
10392 | else if (sec->size != 0) | |
10393 | { | |
10394 | bfd_byte *sec_contents, *l_sec_contents; | |
10395 | ||
10396 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
10397 | (*_bfd_error_handler) | |
c93625e2 | 10398 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
10399 | abfd, sec); |
10400 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
10401 | &l_sec_contents)) | |
10402 | (*_bfd_error_handler) | |
c93625e2 | 10403 | (_("%B: warning: could not read contents of section `%A'"), |
ea5158d8 DJ |
10404 | l->sec->owner, l->sec); |
10405 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
10406 | (*_bfd_error_handler) | |
c93625e2 | 10407 | (_("%B: warning: duplicate section `%A' has different contents"), |
ea5158d8 DJ |
10408 | abfd, sec); |
10409 | ||
10410 | if (sec_contents) | |
10411 | free (sec_contents); | |
10412 | if (l_sec_contents) | |
10413 | free (l_sec_contents); | |
10414 | } | |
10415 | break; | |
082b7297 L |
10416 | } |
10417 | ||
10418 | /* Set the output_section field so that lang_add_section | |
10419 | does not create a lang_input_section structure for this | |
10420 | section. Since there might be a symbol in the section | |
10421 | being discarded, we must retain a pointer to the section | |
10422 | which we are really going to use. */ | |
10423 | sec->output_section = bfd_abs_section_ptr; | |
10424 | sec->kept_section = l->sec; | |
3b36f7e6 | 10425 | |
082b7297 | 10426 | if (flags & SEC_GROUP) |
3d7f7666 L |
10427 | { |
10428 | asection *first = elf_next_in_group (sec); | |
10429 | asection *s = first; | |
10430 | ||
10431 | while (s != NULL) | |
10432 | { | |
10433 | s->output_section = bfd_abs_section_ptr; | |
10434 | /* Record which group discards it. */ | |
10435 | s->kept_section = l->sec; | |
10436 | s = elf_next_in_group (s); | |
10437 | /* These lists are circular. */ | |
10438 | if (s == first) | |
10439 | break; | |
10440 | } | |
10441 | } | |
082b7297 L |
10442 | |
10443 | return; | |
10444 | } | |
10445 | } | |
10446 | ||
3d7f7666 L |
10447 | if (group) |
10448 | { | |
10449 | /* If this is the member of a single member comdat group and the | |
10450 | group hasn't be discarded, we check if it matches a linkonce | |
10451 | section. We only record the discarded comdat group. Otherwise | |
10452 | the undiscarded group will be discarded incorrectly later since | |
10453 | itself has been recorded. */ | |
6d2cd210 JJ |
10454 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
10455 | if ((l->sec->flags & SEC_GROUP) == 0 | |
10456 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
10457 | && bfd_elf_match_symbols_in_sections (l->sec, | |
10458 | elf_next_in_group (sec))) | |
10459 | { | |
10460 | elf_next_in_group (sec)->output_section = bfd_abs_section_ptr; | |
10461 | elf_next_in_group (sec)->kept_section = l->sec; | |
10462 | group->output_section = bfd_abs_section_ptr; | |
10463 | break; | |
10464 | } | |
10465 | if (l == NULL) | |
3d7f7666 L |
10466 | return; |
10467 | } | |
10468 | else | |
10469 | /* There is no direct match. But for linkonce section, we should | |
10470 | check if there is a match with comdat group member. We always | |
10471 | record the linkonce section, discarded or not. */ | |
6d2cd210 JJ |
10472 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
10473 | if (l->sec->flags & SEC_GROUP) | |
10474 | { | |
10475 | asection *first = elf_next_in_group (l->sec); | |
10476 | ||
10477 | if (first != NULL | |
10478 | && elf_next_in_group (first) == first | |
10479 | && bfd_elf_match_symbols_in_sections (first, sec)) | |
10480 | { | |
10481 | sec->output_section = bfd_abs_section_ptr; | |
10482 | sec->kept_section = l->sec; | |
10483 | break; | |
10484 | } | |
10485 | } | |
10486 | ||
082b7297 L |
10487 | /* This is the first section with this name. Record it. */ |
10488 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
10489 | } | |
81e1b023 | 10490 | |
a4d8e49b L |
10491 | bfd_boolean |
10492 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
10493 | { | |
10494 | return sym->st_shndx == SHN_COMMON; | |
10495 | } | |
10496 | ||
10497 | unsigned int | |
10498 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
10499 | { | |
10500 | return SHN_COMMON; | |
10501 | } | |
10502 | ||
10503 | asection * | |
10504 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
10505 | { | |
10506 | return bfd_com_section_ptr; | |
10507 | } |