Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
64d03ab5 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
24f58f47 | 3 | 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
9dbe8890 | 4 | Free Software Foundation, Inc. |
252b5132 | 5 | |
8fdd7217 | 6 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 7 | |
8fdd7217 NC |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 10 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 11 | (at your option) any later version. |
252b5132 | 12 | |
8fdd7217 NC |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
252b5132 | 17 | |
8fdd7217 NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
21 | MA 02110-1301, USA. */ | |
252b5132 | 22 | |
252b5132 | 23 | #include "sysdep.h" |
3db64b00 | 24 | #include "bfd.h" |
252b5132 RH |
25 | #include "bfdlink.h" |
26 | #include "libbfd.h" | |
27 | #define ARCH_SIZE 0 | |
28 | #include "elf-bfd.h" | |
4ad4eba5 | 29 | #include "safe-ctype.h" |
ccf2f652 | 30 | #include "libiberty.h" |
66eb6687 | 31 | #include "objalloc.h" |
252b5132 | 32 | |
28caa186 AM |
33 | /* This struct is used to pass information to routines called via |
34 | elf_link_hash_traverse which must return failure. */ | |
35 | ||
36 | struct elf_info_failed | |
37 | { | |
38 | struct bfd_link_info *info; | |
28caa186 AM |
39 | bfd_boolean failed; |
40 | }; | |
41 | ||
42 | /* This structure is used to pass information to | |
43 | _bfd_elf_link_find_version_dependencies. */ | |
44 | ||
45 | struct elf_find_verdep_info | |
46 | { | |
47 | /* General link information. */ | |
48 | struct bfd_link_info *info; | |
49 | /* The number of dependencies. */ | |
50 | unsigned int vers; | |
51 | /* Whether we had a failure. */ | |
52 | bfd_boolean failed; | |
53 | }; | |
54 | ||
55 | static bfd_boolean _bfd_elf_fix_symbol_flags | |
56 | (struct elf_link_hash_entry *, struct elf_info_failed *); | |
57 | ||
d98685ac AM |
58 | /* Define a symbol in a dynamic linkage section. */ |
59 | ||
60 | struct elf_link_hash_entry * | |
61 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
62 | struct bfd_link_info *info, | |
63 | asection *sec, | |
64 | const char *name) | |
65 | { | |
66 | struct elf_link_hash_entry *h; | |
67 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 68 | const struct elf_backend_data *bed; |
d98685ac AM |
69 | |
70 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
71 | if (h != NULL) | |
72 | { | |
73 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
74 | This is a symptom of a larger problem: Absolute symbols | |
75 | defined in shared libraries can't be overridden, because we | |
76 | lose the link to the bfd which is via the symbol section. */ | |
77 | h->root.type = bfd_link_hash_new; | |
78 | } | |
79 | ||
80 | bh = &h->root; | |
81 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, | |
82 | sec, 0, NULL, FALSE, | |
83 | get_elf_backend_data (abfd)->collect, | |
84 | &bh)) | |
85 | return NULL; | |
86 | h = (struct elf_link_hash_entry *) bh; | |
87 | h->def_regular = 1; | |
e28df02b | 88 | h->non_elf = 0; |
d98685ac AM |
89 | h->type = STT_OBJECT; |
90 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
91 | ||
ccabcbe5 AM |
92 | bed = get_elf_backend_data (abfd); |
93 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
d98685ac AM |
94 | return h; |
95 | } | |
96 | ||
b34976b6 | 97 | bfd_boolean |
268b6b39 | 98 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
99 | { |
100 | flagword flags; | |
aad5d350 | 101 | asection *s; |
252b5132 | 102 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 103 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 104 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
105 | |
106 | /* This function may be called more than once. */ | |
aad5d350 AM |
107 | s = bfd_get_section_by_name (abfd, ".got"); |
108 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 109 | return TRUE; |
252b5132 | 110 | |
e5a52504 | 111 | flags = bed->dynamic_sec_flags; |
252b5132 | 112 | |
6de2ae4a L |
113 | s = bfd_make_section_with_flags (abfd, |
114 | (bed->rela_plts_and_copies_p | |
115 | ? ".rela.got" : ".rel.got"), | |
116 | (bed->dynamic_sec_flags | |
117 | | SEC_READONLY)); | |
118 | if (s == NULL | |
119 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
120 | return FALSE; | |
121 | htab->srelgot = s; | |
252b5132 | 122 | |
64e77c6d L |
123 | s = bfd_make_section_with_flags (abfd, ".got", flags); |
124 | if (s == NULL | |
125 | || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
126 | return FALSE; | |
127 | htab->sgot = s; | |
128 | ||
252b5132 RH |
129 | if (bed->want_got_plt) |
130 | { | |
3496cb2a | 131 | s = bfd_make_section_with_flags (abfd, ".got.plt", flags); |
252b5132 | 132 | if (s == NULL |
6de2ae4a L |
133 | || !bfd_set_section_alignment (abfd, s, |
134 | bed->s->log_file_align)) | |
b34976b6 | 135 | return FALSE; |
6de2ae4a | 136 | htab->sgotplt = s; |
252b5132 RH |
137 | } |
138 | ||
64e77c6d L |
139 | /* The first bit of the global offset table is the header. */ |
140 | s->size += bed->got_header_size; | |
141 | ||
2517a57f AM |
142 | if (bed->want_got_sym) |
143 | { | |
144 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
145 | (or .got.plt) section. We don't do this in the linker script | |
146 | because we don't want to define the symbol if we are not creating | |
147 | a global offset table. */ | |
6de2ae4a L |
148 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
149 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 150 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
151 | if (h == NULL) |
152 | return FALSE; | |
2517a57f | 153 | } |
252b5132 | 154 | |
b34976b6 | 155 | return TRUE; |
252b5132 RH |
156 | } |
157 | \f | |
7e9f0867 AM |
158 | /* Create a strtab to hold the dynamic symbol names. */ |
159 | static bfd_boolean | |
160 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
161 | { | |
162 | struct elf_link_hash_table *hash_table; | |
163 | ||
164 | hash_table = elf_hash_table (info); | |
165 | if (hash_table->dynobj == NULL) | |
166 | hash_table->dynobj = abfd; | |
167 | ||
168 | if (hash_table->dynstr == NULL) | |
169 | { | |
170 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
171 | if (hash_table->dynstr == NULL) | |
172 | return FALSE; | |
173 | } | |
174 | return TRUE; | |
175 | } | |
176 | ||
45d6a902 AM |
177 | /* Create some sections which will be filled in with dynamic linking |
178 | information. ABFD is an input file which requires dynamic sections | |
179 | to be created. The dynamic sections take up virtual memory space | |
180 | when the final executable is run, so we need to create them before | |
181 | addresses are assigned to the output sections. We work out the | |
182 | actual contents and size of these sections later. */ | |
252b5132 | 183 | |
b34976b6 | 184 | bfd_boolean |
268b6b39 | 185 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 186 | { |
45d6a902 | 187 | flagword flags; |
91d6fa6a | 188 | asection *s; |
9c5bfbb7 | 189 | const struct elf_backend_data *bed; |
252b5132 | 190 | |
0eddce27 | 191 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
192 | return FALSE; |
193 | ||
194 | if (elf_hash_table (info)->dynamic_sections_created) | |
195 | return TRUE; | |
196 | ||
7e9f0867 AM |
197 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
198 | return FALSE; | |
45d6a902 | 199 | |
7e9f0867 | 200 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
201 | bed = get_elf_backend_data (abfd); |
202 | ||
203 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
204 | |
205 | /* A dynamically linked executable has a .interp section, but a | |
206 | shared library does not. */ | |
36af4a4e | 207 | if (info->executable) |
252b5132 | 208 | { |
3496cb2a L |
209 | s = bfd_make_section_with_flags (abfd, ".interp", |
210 | flags | SEC_READONLY); | |
211 | if (s == NULL) | |
45d6a902 AM |
212 | return FALSE; |
213 | } | |
bb0deeff | 214 | |
45d6a902 AM |
215 | /* Create sections to hold version informations. These are removed |
216 | if they are not needed. */ | |
3496cb2a L |
217 | s = bfd_make_section_with_flags (abfd, ".gnu.version_d", |
218 | flags | SEC_READONLY); | |
45d6a902 | 219 | if (s == NULL |
45d6a902 AM |
220 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
221 | return FALSE; | |
222 | ||
3496cb2a L |
223 | s = bfd_make_section_with_flags (abfd, ".gnu.version", |
224 | flags | SEC_READONLY); | |
45d6a902 | 225 | if (s == NULL |
45d6a902 AM |
226 | || ! bfd_set_section_alignment (abfd, s, 1)) |
227 | return FALSE; | |
228 | ||
3496cb2a L |
229 | s = bfd_make_section_with_flags (abfd, ".gnu.version_r", |
230 | flags | SEC_READONLY); | |
45d6a902 | 231 | if (s == NULL |
45d6a902 AM |
232 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
233 | return FALSE; | |
234 | ||
3496cb2a L |
235 | s = bfd_make_section_with_flags (abfd, ".dynsym", |
236 | flags | SEC_READONLY); | |
45d6a902 | 237 | if (s == NULL |
45d6a902 AM |
238 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
239 | return FALSE; | |
240 | ||
3496cb2a L |
241 | s = bfd_make_section_with_flags (abfd, ".dynstr", |
242 | flags | SEC_READONLY); | |
243 | if (s == NULL) | |
45d6a902 AM |
244 | return FALSE; |
245 | ||
3496cb2a | 246 | s = bfd_make_section_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 247 | if (s == NULL |
45d6a902 AM |
248 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
249 | return FALSE; | |
250 | ||
251 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
252 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
253 | only want to define it if we are, in fact, creating a .dynamic | |
254 | section. We don't want to define it if there is no .dynamic | |
255 | section, since on some ELF platforms the start up code examines it | |
256 | to decide how to initialize the process. */ | |
d98685ac | 257 | if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) |
45d6a902 AM |
258 | return FALSE; |
259 | ||
fdc90cb4 JJ |
260 | if (info->emit_hash) |
261 | { | |
262 | s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY); | |
263 | if (s == NULL | |
264 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
265 | return FALSE; | |
266 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
267 | } | |
268 | ||
269 | if (info->emit_gnu_hash) | |
270 | { | |
271 | s = bfd_make_section_with_flags (abfd, ".gnu.hash", | |
272 | flags | SEC_READONLY); | |
273 | if (s == NULL | |
274 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
275 | return FALSE; | |
276 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
277 | 4 32-bit words followed by variable count of 64-bit words, then | |
278 | variable count of 32-bit words. */ | |
279 | if (bed->s->arch_size == 64) | |
280 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
281 | else | |
282 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
283 | } | |
45d6a902 AM |
284 | |
285 | /* Let the backend create the rest of the sections. This lets the | |
286 | backend set the right flags. The backend will normally create | |
287 | the .got and .plt sections. */ | |
894891db NC |
288 | if (bed->elf_backend_create_dynamic_sections == NULL |
289 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
45d6a902 AM |
290 | return FALSE; |
291 | ||
292 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
293 | ||
294 | return TRUE; | |
295 | } | |
296 | ||
297 | /* Create dynamic sections when linking against a dynamic object. */ | |
298 | ||
299 | bfd_boolean | |
268b6b39 | 300 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
301 | { |
302 | flagword flags, pltflags; | |
7325306f | 303 | struct elf_link_hash_entry *h; |
45d6a902 | 304 | asection *s; |
9c5bfbb7 | 305 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 306 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 307 | |
252b5132 RH |
308 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
309 | .rel[a].bss sections. */ | |
e5a52504 | 310 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
311 | |
312 | pltflags = flags; | |
252b5132 | 313 | if (bed->plt_not_loaded) |
6df4d94c MM |
314 | /* We do not clear SEC_ALLOC here because we still want the OS to |
315 | allocate space for the section; it's just that there's nothing | |
316 | to read in from the object file. */ | |
5d1634d7 | 317 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
318 | else |
319 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
320 | if (bed->plt_readonly) |
321 | pltflags |= SEC_READONLY; | |
322 | ||
3496cb2a | 323 | s = bfd_make_section_with_flags (abfd, ".plt", pltflags); |
252b5132 | 324 | if (s == NULL |
252b5132 | 325 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 326 | return FALSE; |
6de2ae4a | 327 | htab->splt = s; |
252b5132 | 328 | |
d98685ac AM |
329 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
330 | .plt section. */ | |
7325306f RS |
331 | if (bed->want_plt_sym) |
332 | { | |
333 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
334 | "_PROCEDURE_LINKAGE_TABLE_"); | |
335 | elf_hash_table (info)->hplt = h; | |
336 | if (h == NULL) | |
337 | return FALSE; | |
338 | } | |
252b5132 | 339 | |
3496cb2a | 340 | s = bfd_make_section_with_flags (abfd, |
d35fd659 | 341 | (bed->rela_plts_and_copies_p |
3496cb2a L |
342 | ? ".rela.plt" : ".rel.plt"), |
343 | flags | SEC_READONLY); | |
252b5132 | 344 | if (s == NULL |
45d6a902 | 345 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 346 | return FALSE; |
6de2ae4a | 347 | htab->srelplt = s; |
252b5132 RH |
348 | |
349 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 350 | return FALSE; |
252b5132 | 351 | |
3018b441 RH |
352 | if (bed->want_dynbss) |
353 | { | |
354 | /* The .dynbss section is a place to put symbols which are defined | |
355 | by dynamic objects, are referenced by regular objects, and are | |
356 | not functions. We must allocate space for them in the process | |
357 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
358 | initialize them at run time. The linker script puts the .dynbss | |
359 | section into the .bss section of the final image. */ | |
3496cb2a L |
360 | s = bfd_make_section_with_flags (abfd, ".dynbss", |
361 | (SEC_ALLOC | |
362 | | SEC_LINKER_CREATED)); | |
363 | if (s == NULL) | |
b34976b6 | 364 | return FALSE; |
252b5132 | 365 | |
3018b441 | 366 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
367 | normally needed. We need to create it here, though, so that the |
368 | linker will map it to an output section. We can't just create it | |
369 | only if we need it, because we will not know whether we need it | |
370 | until we have seen all the input files, and the first time the | |
371 | main linker code calls BFD after examining all the input files | |
372 | (size_dynamic_sections) the input sections have already been | |
373 | mapped to the output sections. If the section turns out not to | |
374 | be needed, we can discard it later. We will never need this | |
375 | section when generating a shared object, since they do not use | |
376 | copy relocs. */ | |
3018b441 RH |
377 | if (! info->shared) |
378 | { | |
3496cb2a | 379 | s = bfd_make_section_with_flags (abfd, |
d35fd659 | 380 | (bed->rela_plts_and_copies_p |
3496cb2a L |
381 | ? ".rela.bss" : ".rel.bss"), |
382 | flags | SEC_READONLY); | |
3018b441 | 383 | if (s == NULL |
45d6a902 | 384 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 385 | return FALSE; |
3018b441 | 386 | } |
252b5132 RH |
387 | } |
388 | ||
b34976b6 | 389 | return TRUE; |
252b5132 RH |
390 | } |
391 | \f | |
252b5132 RH |
392 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
393 | read the input files, since we need to have a list of all of them | |
394 | before we can determine the final sizes of the output sections. | |
395 | Note that we may actually call this function even though we are not | |
396 | going to output any dynamic symbols; in some cases we know that a | |
397 | symbol should be in the dynamic symbol table, but only if there is | |
398 | one. */ | |
399 | ||
b34976b6 | 400 | bfd_boolean |
c152c796 AM |
401 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
402 | struct elf_link_hash_entry *h) | |
252b5132 RH |
403 | { |
404 | if (h->dynindx == -1) | |
405 | { | |
2b0f7ef9 | 406 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 407 | char *p; |
252b5132 | 408 | const char *name; |
252b5132 RH |
409 | bfd_size_type indx; |
410 | ||
7a13edea NC |
411 | /* XXX: The ABI draft says the linker must turn hidden and |
412 | internal symbols into STB_LOCAL symbols when producing the | |
413 | DSO. However, if ld.so honors st_other in the dynamic table, | |
414 | this would not be necessary. */ | |
415 | switch (ELF_ST_VISIBILITY (h->other)) | |
416 | { | |
417 | case STV_INTERNAL: | |
418 | case STV_HIDDEN: | |
9d6eee78 L |
419 | if (h->root.type != bfd_link_hash_undefined |
420 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 421 | { |
f5385ebf | 422 | h->forced_local = 1; |
67687978 PB |
423 | if (!elf_hash_table (info)->is_relocatable_executable) |
424 | return TRUE; | |
7a13edea | 425 | } |
0444bdd4 | 426 | |
7a13edea NC |
427 | default: |
428 | break; | |
429 | } | |
430 | ||
252b5132 RH |
431 | h->dynindx = elf_hash_table (info)->dynsymcount; |
432 | ++elf_hash_table (info)->dynsymcount; | |
433 | ||
434 | dynstr = elf_hash_table (info)->dynstr; | |
435 | if (dynstr == NULL) | |
436 | { | |
437 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 438 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 439 | if (dynstr == NULL) |
b34976b6 | 440 | return FALSE; |
252b5132 RH |
441 | } |
442 | ||
443 | /* We don't put any version information in the dynamic string | |
aad5d350 | 444 | table. */ |
252b5132 RH |
445 | name = h->root.root.string; |
446 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
447 | if (p != NULL) |
448 | /* We know that the p points into writable memory. In fact, | |
449 | there are only a few symbols that have read-only names, being | |
450 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
451 | by the backends. Most symbols will have names pointing into | |
452 | an ELF string table read from a file, or to objalloc memory. */ | |
453 | *p = 0; | |
454 | ||
455 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
456 | ||
457 | if (p != NULL) | |
458 | *p = ELF_VER_CHR; | |
252b5132 RH |
459 | |
460 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 461 | return FALSE; |
252b5132 RH |
462 | h->dynstr_index = indx; |
463 | } | |
464 | ||
b34976b6 | 465 | return TRUE; |
252b5132 | 466 | } |
45d6a902 | 467 | \f |
55255dae L |
468 | /* Mark a symbol dynamic. */ |
469 | ||
28caa186 | 470 | static void |
55255dae | 471 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
472 | struct elf_link_hash_entry *h, |
473 | Elf_Internal_Sym *sym) | |
55255dae | 474 | { |
40b36307 | 475 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 476 | |
40b36307 L |
477 | /* It may be called more than once on the same H. */ |
478 | if(h->dynamic || info->relocatable) | |
55255dae L |
479 | return; |
480 | ||
40b36307 L |
481 | if ((info->dynamic_data |
482 | && (h->type == STT_OBJECT | |
483 | || (sym != NULL | |
484 | && ELF_ST_TYPE (sym->st_info) == STT_OBJECT))) | |
a0c8462f | 485 | || (d != NULL |
40b36307 L |
486 | && h->root.type == bfd_link_hash_new |
487 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
55255dae L |
488 | h->dynamic = 1; |
489 | } | |
490 | ||
45d6a902 AM |
491 | /* Record an assignment to a symbol made by a linker script. We need |
492 | this in case some dynamic object refers to this symbol. */ | |
493 | ||
494 | bfd_boolean | |
fe21a8fc L |
495 | bfd_elf_record_link_assignment (bfd *output_bfd, |
496 | struct bfd_link_info *info, | |
268b6b39 | 497 | const char *name, |
fe21a8fc L |
498 | bfd_boolean provide, |
499 | bfd_boolean hidden) | |
45d6a902 | 500 | { |
00cbee0a | 501 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 502 | struct elf_link_hash_table *htab; |
00cbee0a | 503 | const struct elf_backend_data *bed; |
45d6a902 | 504 | |
0eddce27 | 505 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
506 | return TRUE; |
507 | ||
4ea42fb7 AM |
508 | htab = elf_hash_table (info); |
509 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 510 | if (h == NULL) |
4ea42fb7 | 511 | return provide; |
45d6a902 | 512 | |
00cbee0a | 513 | switch (h->root.type) |
77cfaee6 | 514 | { |
00cbee0a L |
515 | case bfd_link_hash_defined: |
516 | case bfd_link_hash_defweak: | |
517 | case bfd_link_hash_common: | |
518 | break; | |
519 | case bfd_link_hash_undefweak: | |
520 | case bfd_link_hash_undefined: | |
521 | /* Since we're defining the symbol, don't let it seem to have not | |
522 | been defined. record_dynamic_symbol and size_dynamic_sections | |
523 | may depend on this. */ | |
4ea42fb7 | 524 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
525 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
526 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
527 | break; |
528 | case bfd_link_hash_new: | |
40b36307 | 529 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae | 530 | h->non_elf = 0; |
00cbee0a L |
531 | break; |
532 | case bfd_link_hash_indirect: | |
533 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 534 | the versioned symbol point to this one. */ |
00cbee0a L |
535 | bed = get_elf_backend_data (output_bfd); |
536 | hv = h; | |
537 | while (hv->root.type == bfd_link_hash_indirect | |
538 | || hv->root.type == bfd_link_hash_warning) | |
539 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
540 | /* We don't need to update h->root.u since linker will set them | |
541 | later. */ | |
542 | h->root.type = bfd_link_hash_undefined; | |
543 | hv->root.type = bfd_link_hash_indirect; | |
544 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
545 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
546 | break; | |
547 | case bfd_link_hash_warning: | |
548 | abort (); | |
549 | break; | |
55255dae | 550 | } |
45d6a902 AM |
551 | |
552 | /* If this symbol is being provided by the linker script, and it is | |
553 | currently defined by a dynamic object, but not by a regular | |
554 | object, then mark it as undefined so that the generic linker will | |
555 | force the correct value. */ | |
556 | if (provide | |
f5385ebf AM |
557 | && h->def_dynamic |
558 | && !h->def_regular) | |
45d6a902 AM |
559 | h->root.type = bfd_link_hash_undefined; |
560 | ||
561 | /* If this symbol is not being provided by the linker script, and it is | |
562 | currently defined by a dynamic object, but not by a regular object, | |
563 | then clear out any version information because the symbol will not be | |
564 | associated with the dynamic object any more. */ | |
565 | if (!provide | |
f5385ebf AM |
566 | && h->def_dynamic |
567 | && !h->def_regular) | |
45d6a902 AM |
568 | h->verinfo.verdef = NULL; |
569 | ||
f5385ebf | 570 | h->def_regular = 1; |
45d6a902 | 571 | |
fe21a8fc L |
572 | if (provide && hidden) |
573 | { | |
91d6fa6a | 574 | bed = get_elf_backend_data (output_bfd); |
fe21a8fc L |
575 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; |
576 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
577 | } | |
578 | ||
6fa3860b PB |
579 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
580 | and executables. */ | |
581 | if (!info->relocatable | |
582 | && h->dynindx != -1 | |
583 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
584 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
585 | h->forced_local = 1; | |
586 | ||
f5385ebf AM |
587 | if ((h->def_dynamic |
588 | || h->ref_dynamic | |
67687978 PB |
589 | || info->shared |
590 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
591 | && h->dynindx == -1) |
592 | { | |
c152c796 | 593 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
594 | return FALSE; |
595 | ||
596 | /* If this is a weak defined symbol, and we know a corresponding | |
597 | real symbol from the same dynamic object, make sure the real | |
598 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
599 | if (h->u.weakdef != NULL |
600 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 601 | { |
f6e332e6 | 602 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
603 | return FALSE; |
604 | } | |
605 | } | |
606 | ||
607 | return TRUE; | |
608 | } | |
42751cf3 | 609 | |
8c58d23b AM |
610 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
611 | success, and 2 on a failure caused by attempting to record a symbol | |
612 | in a discarded section, eg. a discarded link-once section symbol. */ | |
613 | ||
614 | int | |
c152c796 AM |
615 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
616 | bfd *input_bfd, | |
617 | long input_indx) | |
8c58d23b AM |
618 | { |
619 | bfd_size_type amt; | |
620 | struct elf_link_local_dynamic_entry *entry; | |
621 | struct elf_link_hash_table *eht; | |
622 | struct elf_strtab_hash *dynstr; | |
623 | unsigned long dynstr_index; | |
624 | char *name; | |
625 | Elf_External_Sym_Shndx eshndx; | |
626 | char esym[sizeof (Elf64_External_Sym)]; | |
627 | ||
0eddce27 | 628 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
629 | return 0; |
630 | ||
631 | /* See if the entry exists already. */ | |
632 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
633 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
634 | return 1; | |
635 | ||
636 | amt = sizeof (*entry); | |
a50b1753 | 637 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
638 | if (entry == NULL) |
639 | return 0; | |
640 | ||
641 | /* Go find the symbol, so that we can find it's name. */ | |
642 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 643 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
644 | { |
645 | bfd_release (input_bfd, entry); | |
646 | return 0; | |
647 | } | |
648 | ||
649 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 650 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
651 | { |
652 | asection *s; | |
653 | ||
654 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
655 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
656 | { | |
657 | /* We can still bfd_release here as nothing has done another | |
658 | bfd_alloc. We can't do this later in this function. */ | |
659 | bfd_release (input_bfd, entry); | |
660 | return 2; | |
661 | } | |
662 | } | |
663 | ||
664 | name = (bfd_elf_string_from_elf_section | |
665 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
666 | entry->isym.st_name)); | |
667 | ||
668 | dynstr = elf_hash_table (info)->dynstr; | |
669 | if (dynstr == NULL) | |
670 | { | |
671 | /* Create a strtab to hold the dynamic symbol names. */ | |
672 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
673 | if (dynstr == NULL) | |
674 | return 0; | |
675 | } | |
676 | ||
b34976b6 | 677 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
678 | if (dynstr_index == (unsigned long) -1) |
679 | return 0; | |
680 | entry->isym.st_name = dynstr_index; | |
681 | ||
682 | eht = elf_hash_table (info); | |
683 | ||
684 | entry->next = eht->dynlocal; | |
685 | eht->dynlocal = entry; | |
686 | entry->input_bfd = input_bfd; | |
687 | entry->input_indx = input_indx; | |
688 | eht->dynsymcount++; | |
689 | ||
690 | /* Whatever binding the symbol had before, it's now local. */ | |
691 | entry->isym.st_info | |
692 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
693 | ||
694 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
695 | ||
696 | return 1; | |
697 | } | |
698 | ||
30b30c21 | 699 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 700 | |
30b30c21 | 701 | long |
268b6b39 AM |
702 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
703 | bfd *input_bfd, | |
704 | long input_indx) | |
30b30c21 RH |
705 | { |
706 | struct elf_link_local_dynamic_entry *e; | |
707 | ||
708 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
709 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
710 | return e->dynindx; | |
711 | return -1; | |
712 | } | |
713 | ||
714 | /* This function is used to renumber the dynamic symbols, if some of | |
715 | them are removed because they are marked as local. This is called | |
716 | via elf_link_hash_traverse. */ | |
717 | ||
b34976b6 | 718 | static bfd_boolean |
268b6b39 AM |
719 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
720 | void *data) | |
42751cf3 | 721 | { |
a50b1753 | 722 | size_t *count = (size_t *) data; |
30b30c21 | 723 | |
6fa3860b PB |
724 | if (h->forced_local) |
725 | return TRUE; | |
726 | ||
727 | if (h->dynindx != -1) | |
728 | h->dynindx = ++(*count); | |
729 | ||
730 | return TRUE; | |
731 | } | |
732 | ||
733 | ||
734 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
735 | STB_LOCAL binding. */ | |
736 | ||
737 | static bfd_boolean | |
738 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
739 | void *data) | |
740 | { | |
a50b1753 | 741 | size_t *count = (size_t *) data; |
6fa3860b | 742 | |
6fa3860b PB |
743 | if (!h->forced_local) |
744 | return TRUE; | |
745 | ||
42751cf3 | 746 | if (h->dynindx != -1) |
30b30c21 RH |
747 | h->dynindx = ++(*count); |
748 | ||
b34976b6 | 749 | return TRUE; |
42751cf3 | 750 | } |
30b30c21 | 751 | |
aee6f5b4 AO |
752 | /* Return true if the dynamic symbol for a given section should be |
753 | omitted when creating a shared library. */ | |
754 | bfd_boolean | |
755 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
756 | struct bfd_link_info *info, | |
757 | asection *p) | |
758 | { | |
74541ad4 AM |
759 | struct elf_link_hash_table *htab; |
760 | ||
aee6f5b4 AO |
761 | switch (elf_section_data (p)->this_hdr.sh_type) |
762 | { | |
763 | case SHT_PROGBITS: | |
764 | case SHT_NOBITS: | |
765 | /* If sh_type is yet undecided, assume it could be | |
766 | SHT_PROGBITS/SHT_NOBITS. */ | |
767 | case SHT_NULL: | |
74541ad4 AM |
768 | htab = elf_hash_table (info); |
769 | if (p == htab->tls_sec) | |
770 | return FALSE; | |
771 | ||
772 | if (htab->text_index_section != NULL) | |
773 | return p != htab->text_index_section && p != htab->data_index_section; | |
774 | ||
aee6f5b4 AO |
775 | if (strcmp (p->name, ".got") == 0 |
776 | || strcmp (p->name, ".got.plt") == 0 | |
777 | || strcmp (p->name, ".plt") == 0) | |
778 | { | |
779 | asection *ip; | |
aee6f5b4 | 780 | |
74541ad4 AM |
781 | if (htab->dynobj != NULL |
782 | && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL | |
aee6f5b4 AO |
783 | && (ip->flags & SEC_LINKER_CREATED) |
784 | && ip->output_section == p) | |
785 | return TRUE; | |
786 | } | |
787 | return FALSE; | |
788 | ||
789 | /* There shouldn't be section relative relocations | |
790 | against any other section. */ | |
791 | default: | |
792 | return TRUE; | |
793 | } | |
794 | } | |
795 | ||
062e2358 | 796 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
797 | symbol for each output section, which come first. Next come symbols |
798 | which have been forced to local binding. Then all of the back-end | |
799 | allocated local dynamic syms, followed by the rest of the global | |
800 | symbols. */ | |
30b30c21 | 801 | |
554220db AM |
802 | static unsigned long |
803 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
804 | struct bfd_link_info *info, | |
805 | unsigned long *section_sym_count) | |
30b30c21 RH |
806 | { |
807 | unsigned long dynsymcount = 0; | |
808 | ||
67687978 | 809 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 810 | { |
aee6f5b4 | 811 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
812 | asection *p; |
813 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 814 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
815 | && (p->flags & SEC_ALLOC) != 0 |
816 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
817 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
818 | else |
819 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 820 | } |
554220db | 821 | *section_sym_count = dynsymcount; |
30b30c21 | 822 | |
6fa3860b PB |
823 | elf_link_hash_traverse (elf_hash_table (info), |
824 | elf_link_renumber_local_hash_table_dynsyms, | |
825 | &dynsymcount); | |
826 | ||
30b30c21 RH |
827 | if (elf_hash_table (info)->dynlocal) |
828 | { | |
829 | struct elf_link_local_dynamic_entry *p; | |
830 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
831 | p->dynindx = ++dynsymcount; | |
832 | } | |
833 | ||
834 | elf_link_hash_traverse (elf_hash_table (info), | |
835 | elf_link_renumber_hash_table_dynsyms, | |
836 | &dynsymcount); | |
837 | ||
838 | /* There is an unused NULL entry at the head of the table which | |
839 | we must account for in our count. Unless there weren't any | |
840 | symbols, which means we'll have no table at all. */ | |
841 | if (dynsymcount != 0) | |
842 | ++dynsymcount; | |
843 | ||
ccabcbe5 AM |
844 | elf_hash_table (info)->dynsymcount = dynsymcount; |
845 | return dynsymcount; | |
30b30c21 | 846 | } |
252b5132 | 847 | |
54ac0771 L |
848 | /* Merge st_other field. */ |
849 | ||
850 | static void | |
851 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
852 | Elf_Internal_Sym *isym, bfd_boolean definition, | |
853 | bfd_boolean dynamic) | |
854 | { | |
855 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
856 | ||
857 | /* If st_other has a processor-specific meaning, specific | |
858 | code might be needed here. We never merge the visibility | |
859 | attribute with the one from a dynamic object. */ | |
860 | if (bed->elf_backend_merge_symbol_attribute) | |
861 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
862 | dynamic); | |
863 | ||
864 | /* If this symbol has default visibility and the user has requested | |
865 | we not re-export it, then mark it as hidden. */ | |
866 | if (definition | |
867 | && !dynamic | |
868 | && (abfd->no_export | |
869 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
870 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
871 | isym->st_other = (STV_HIDDEN | |
872 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
873 | ||
874 | if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0) | |
875 | { | |
876 | unsigned char hvis, symvis, other, nvis; | |
877 | ||
878 | /* Only merge the visibility. Leave the remainder of the | |
879 | st_other field to elf_backend_merge_symbol_attribute. */ | |
880 | other = h->other & ~ELF_ST_VISIBILITY (-1); | |
881 | ||
882 | /* Combine visibilities, using the most constraining one. */ | |
883 | hvis = ELF_ST_VISIBILITY (h->other); | |
884 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
885 | if (! hvis) | |
886 | nvis = symvis; | |
887 | else if (! symvis) | |
888 | nvis = hvis; | |
889 | else | |
890 | nvis = hvis < symvis ? hvis : symvis; | |
891 | ||
892 | h->other = other | nvis; | |
893 | } | |
894 | } | |
895 | ||
45d6a902 AM |
896 | /* This function is called when we want to define a new symbol. It |
897 | handles the various cases which arise when we find a definition in | |
898 | a dynamic object, or when there is already a definition in a | |
899 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
900 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
901 | OVERRIDE if the old symbol is overriding a new definition. We set | |
902 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
903 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
904 | change, we mean that we shouldn't warn if the type or size does | |
af44c138 L |
905 | change. We set POLD_ALIGNMENT if an old common symbol in a dynamic |
906 | object is overridden by a regular object. */ | |
45d6a902 AM |
907 | |
908 | bfd_boolean | |
268b6b39 AM |
909 | _bfd_elf_merge_symbol (bfd *abfd, |
910 | struct bfd_link_info *info, | |
911 | const char *name, | |
912 | Elf_Internal_Sym *sym, | |
913 | asection **psec, | |
914 | bfd_vma *pvalue, | |
af44c138 | 915 | unsigned int *pold_alignment, |
268b6b39 AM |
916 | struct elf_link_hash_entry **sym_hash, |
917 | bfd_boolean *skip, | |
918 | bfd_boolean *override, | |
919 | bfd_boolean *type_change_ok, | |
0f8a2703 | 920 | bfd_boolean *size_change_ok) |
252b5132 | 921 | { |
7479dfd4 | 922 | asection *sec, *oldsec; |
45d6a902 AM |
923 | struct elf_link_hash_entry *h; |
924 | struct elf_link_hash_entry *flip; | |
925 | int bind; | |
926 | bfd *oldbfd; | |
927 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 928 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 929 | const struct elf_backend_data *bed; |
45d6a902 AM |
930 | |
931 | *skip = FALSE; | |
932 | *override = FALSE; | |
933 | ||
934 | sec = *psec; | |
935 | bind = ELF_ST_BIND (sym->st_info); | |
936 | ||
cd7be95b KH |
937 | /* Silently discard TLS symbols from --just-syms. There's no way to |
938 | combine a static TLS block with a new TLS block for this executable. */ | |
939 | if (ELF_ST_TYPE (sym->st_info) == STT_TLS | |
940 | && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
941 | { | |
942 | *skip = TRUE; | |
943 | return TRUE; | |
944 | } | |
945 | ||
45d6a902 AM |
946 | if (! bfd_is_und_section (sec)) |
947 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
948 | else | |
949 | h = ((struct elf_link_hash_entry *) | |
950 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
951 | if (h == NULL) | |
952 | return FALSE; | |
953 | *sym_hash = h; | |
252b5132 | 954 | |
88ba32a0 L |
955 | bed = get_elf_backend_data (abfd); |
956 | ||
45d6a902 AM |
957 | /* This code is for coping with dynamic objects, and is only useful |
958 | if we are doing an ELF link. */ | |
88ba32a0 | 959 | if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) |
45d6a902 | 960 | return TRUE; |
252b5132 | 961 | |
45d6a902 AM |
962 | /* For merging, we only care about real symbols. */ |
963 | ||
964 | while (h->root.type == bfd_link_hash_indirect | |
965 | || h->root.type == bfd_link_hash_warning) | |
966 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
967 | ||
40b36307 L |
968 | /* We have to check it for every instance since the first few may be |
969 | refereences and not all compilers emit symbol type for undefined | |
970 | symbols. */ | |
971 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
972 | ||
45d6a902 AM |
973 | /* If we just created the symbol, mark it as being an ELF symbol. |
974 | Other than that, there is nothing to do--there is no merge issue | |
975 | with a newly defined symbol--so we just return. */ | |
976 | ||
977 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 978 | { |
f5385ebf | 979 | h->non_elf = 0; |
45d6a902 AM |
980 | return TRUE; |
981 | } | |
252b5132 | 982 | |
7479dfd4 L |
983 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
984 | existing symbol. */ | |
252b5132 | 985 | |
45d6a902 AM |
986 | switch (h->root.type) |
987 | { | |
988 | default: | |
989 | oldbfd = NULL; | |
7479dfd4 | 990 | oldsec = NULL; |
45d6a902 | 991 | break; |
252b5132 | 992 | |
45d6a902 AM |
993 | case bfd_link_hash_undefined: |
994 | case bfd_link_hash_undefweak: | |
995 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 996 | oldsec = NULL; |
45d6a902 AM |
997 | break; |
998 | ||
999 | case bfd_link_hash_defined: | |
1000 | case bfd_link_hash_defweak: | |
1001 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 1002 | oldsec = h->root.u.def.section; |
45d6a902 AM |
1003 | break; |
1004 | ||
1005 | case bfd_link_hash_common: | |
1006 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 1007 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
1008 | break; |
1009 | } | |
1010 | ||
895fa45f MGD |
1011 | /* Differentiate strong and weak symbols. */ |
1012 | newweak = bind == STB_WEAK; | |
1013 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1014 | || h->root.type == bfd_link_hash_undefweak); | |
1015 | ||
45d6a902 AM |
1016 | /* In cases involving weak versioned symbols, we may wind up trying |
1017 | to merge a symbol with itself. Catch that here, to avoid the | |
1018 | confusion that results if we try to override a symbol with | |
1019 | itself. The additional tests catch cases like | |
1020 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1021 | dynamic object, which we do want to handle here. */ | |
1022 | if (abfd == oldbfd | |
895fa45f | 1023 | && (newweak || oldweak) |
45d6a902 | 1024 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1025 | || !h->def_regular)) |
45d6a902 AM |
1026 | return TRUE; |
1027 | ||
1028 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
1029 | respectively, is from a dynamic object. */ | |
1030 | ||
707bba77 | 1031 | newdyn = (abfd->flags & DYNAMIC) != 0; |
45d6a902 | 1032 | |
707bba77 | 1033 | olddyn = FALSE; |
45d6a902 AM |
1034 | if (oldbfd != NULL) |
1035 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1036 | else if (oldsec != NULL) |
45d6a902 | 1037 | { |
707bba77 | 1038 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1039 | indices used by MIPS ELF. */ |
707bba77 | 1040 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1041 | } |
252b5132 | 1042 | |
45d6a902 AM |
1043 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1044 | respectively, appear to be a definition rather than reference. */ | |
1045 | ||
707bba77 | 1046 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1047 | |
707bba77 AM |
1048 | olddef = (h->root.type != bfd_link_hash_undefined |
1049 | && h->root.type != bfd_link_hash_undefweak | |
1050 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 1051 | |
0a36a439 L |
1052 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1053 | respectively, appear to be a function. */ | |
1054 | ||
1055 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1056 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1057 | ||
1058 | oldfunc = (h->type != STT_NOTYPE | |
1059 | && bed->is_function_type (h->type)); | |
1060 | ||
580a2b6e L |
1061 | /* When we try to create a default indirect symbol from the dynamic |
1062 | definition with the default version, we skip it if its type and | |
1063 | the type of existing regular definition mismatch. We only do it | |
1064 | if the existing regular definition won't be dynamic. */ | |
1065 | if (pold_alignment == NULL | |
1066 | && !info->shared | |
1067 | && !info->export_dynamic | |
1068 | && !h->ref_dynamic | |
1069 | && newdyn | |
1070 | && newdef | |
1071 | && !olddyn | |
1072 | && (olddef || h->root.type == bfd_link_hash_common) | |
1073 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1074 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
fcb93ecf | 1075 | && h->type != STT_NOTYPE |
0a36a439 | 1076 | && !(newfunc && oldfunc)) |
580a2b6e L |
1077 | { |
1078 | *skip = TRUE; | |
1079 | return TRUE; | |
1080 | } | |
1081 | ||
3a5dbfb2 AM |
1082 | /* Plugin symbol type isn't currently set. Stop bogus errors. */ |
1083 | if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0) | |
1084 | *type_change_ok = TRUE; | |
1085 | ||
68f49ba3 L |
1086 | /* Check TLS symbol. We don't check undefined symbol introduced by |
1087 | "ld -u". */ | |
3a5dbfb2 AM |
1088 | else if (oldbfd != NULL |
1089 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1090 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1091 | { |
1092 | bfd *ntbfd, *tbfd; | |
1093 | bfd_boolean ntdef, tdef; | |
1094 | asection *ntsec, *tsec; | |
1095 | ||
1096 | if (h->type == STT_TLS) | |
1097 | { | |
3b36f7e6 | 1098 | ntbfd = abfd; |
7479dfd4 L |
1099 | ntsec = sec; |
1100 | ntdef = newdef; | |
1101 | tbfd = oldbfd; | |
1102 | tsec = oldsec; | |
1103 | tdef = olddef; | |
1104 | } | |
1105 | else | |
1106 | { | |
1107 | ntbfd = oldbfd; | |
1108 | ntsec = oldsec; | |
1109 | ntdef = olddef; | |
1110 | tbfd = abfd; | |
1111 | tsec = sec; | |
1112 | tdef = newdef; | |
1113 | } | |
1114 | ||
1115 | if (tdef && ntdef) | |
1116 | (*_bfd_error_handler) | |
fc3e1e3c | 1117 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), |
7479dfd4 L |
1118 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); |
1119 | else if (!tdef && !ntdef) | |
1120 | (*_bfd_error_handler) | |
fc3e1e3c | 1121 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), |
7479dfd4 L |
1122 | tbfd, ntbfd, h->root.root.string); |
1123 | else if (tdef) | |
1124 | (*_bfd_error_handler) | |
fc3e1e3c | 1125 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), |
7479dfd4 L |
1126 | tbfd, tsec, ntbfd, h->root.root.string); |
1127 | else | |
1128 | (*_bfd_error_handler) | |
fc3e1e3c | 1129 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), |
7479dfd4 L |
1130 | tbfd, ntbfd, ntsec, h->root.root.string); |
1131 | ||
1132 | bfd_set_error (bfd_error_bad_value); | |
1133 | return FALSE; | |
1134 | } | |
1135 | ||
4cc11e76 | 1136 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
1137 | object or is weak in all dynamic objects. Internal and hidden |
1138 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 1139 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
1140 | { |
1141 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 1142 | h->dynamic_def = 1; |
45d6a902 | 1143 | else |
252b5132 | 1144 | { |
45d6a902 AM |
1145 | /* Check if this symbol is weak in all dynamic objects. If it |
1146 | is the first time we see it in a dynamic object, we mark | |
1147 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 1148 | if (!h->ref_dynamic) |
79349b09 | 1149 | { |
45d6a902 | 1150 | if (bind == STB_WEAK) |
f5385ebf | 1151 | h->dynamic_weak = 1; |
252b5132 | 1152 | } |
45d6a902 | 1153 | else if (bind != STB_WEAK) |
f5385ebf | 1154 | h->dynamic_weak = 0; |
252b5132 | 1155 | } |
45d6a902 | 1156 | } |
252b5132 | 1157 | |
45d6a902 AM |
1158 | /* If the old symbol has non-default visibility, we ignore the new |
1159 | definition from a dynamic object. */ | |
1160 | if (newdyn | |
9c7a29a3 | 1161 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1162 | && !bfd_is_und_section (sec)) |
1163 | { | |
1164 | *skip = TRUE; | |
1165 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1166 | h->ref_dynamic = 1; |
45d6a902 AM |
1167 | /* A protected symbol has external availability. Make sure it is |
1168 | recorded as dynamic. | |
1169 | ||
1170 | FIXME: Should we check type and size for protected symbol? */ | |
1171 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1172 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1173 | else |
1174 | return TRUE; | |
1175 | } | |
1176 | else if (!newdyn | |
9c7a29a3 | 1177 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1178 | && h->def_dynamic) |
45d6a902 AM |
1179 | { |
1180 | /* If the new symbol with non-default visibility comes from a | |
1181 | relocatable file and the old definition comes from a dynamic | |
1182 | object, we remove the old definition. */ | |
1183 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
d2dee3b2 L |
1184 | { |
1185 | /* Handle the case where the old dynamic definition is | |
1186 | default versioned. We need to copy the symbol info from | |
1187 | the symbol with default version to the normal one if it | |
1188 | was referenced before. */ | |
1189 | if (h->ref_regular) | |
1190 | { | |
d2dee3b2 | 1191 | struct elf_link_hash_entry *vh = *sym_hash; |
91d6fa6a | 1192 | |
d2dee3b2 L |
1193 | vh->root.type = h->root.type; |
1194 | h->root.type = bfd_link_hash_indirect; | |
1195 | (*bed->elf_backend_copy_indirect_symbol) (info, vh, h); | |
1196 | /* Protected symbols will override the dynamic definition | |
1197 | with default version. */ | |
1198 | if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED) | |
1199 | { | |
1200 | h->root.u.i.link = (struct bfd_link_hash_entry *) vh; | |
1201 | vh->dynamic_def = 1; | |
1202 | vh->ref_dynamic = 1; | |
1203 | } | |
1204 | else | |
1205 | { | |
1206 | h->root.type = vh->root.type; | |
1207 | vh->ref_dynamic = 0; | |
1208 | /* We have to hide it here since it was made dynamic | |
1209 | global with extra bits when the symbol info was | |
1210 | copied from the old dynamic definition. */ | |
1211 | (*bed->elf_backend_hide_symbol) (info, vh, TRUE); | |
1212 | } | |
1213 | h = vh; | |
1214 | } | |
1215 | else | |
1216 | h = *sym_hash; | |
1217 | } | |
1de1a317 | 1218 | |
f6e332e6 | 1219 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
1220 | && bfd_is_und_section (sec)) |
1221 | { | |
1222 | /* If the new symbol is undefined and the old symbol was | |
1223 | also undefined before, we need to make sure | |
1224 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 1225 | up the linker hash table undefs list. Since the old |
1de1a317 L |
1226 | definition came from a dynamic object, it is still on the |
1227 | undefs list. */ | |
1228 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
1229 | h->root.u.undef.abfd = abfd; |
1230 | } | |
1231 | else | |
1232 | { | |
1233 | h->root.type = bfd_link_hash_new; | |
1234 | h->root.u.undef.abfd = NULL; | |
1235 | } | |
1236 | ||
f5385ebf | 1237 | if (h->def_dynamic) |
252b5132 | 1238 | { |
f5385ebf AM |
1239 | h->def_dynamic = 0; |
1240 | h->ref_dynamic = 1; | |
45d6a902 AM |
1241 | } |
1242 | /* FIXME: Should we check type and size for protected symbol? */ | |
1243 | h->size = 0; | |
1244 | h->type = 0; | |
1245 | return TRUE; | |
1246 | } | |
14a793b2 | 1247 | |
3e7a7d11 NC |
1248 | if (bind == STB_GNU_UNIQUE) |
1249 | h->unique_global = 1; | |
1250 | ||
15b43f48 AM |
1251 | /* If a new weak symbol definition comes from a regular file and the |
1252 | old symbol comes from a dynamic library, we treat the new one as | |
1253 | strong. Similarly, an old weak symbol definition from a regular | |
1254 | file is treated as strong when the new symbol comes from a dynamic | |
1255 | library. Further, an old weak symbol from a dynamic library is | |
1256 | treated as strong if the new symbol is from a dynamic library. | |
1257 | This reflects the way glibc's ld.so works. | |
1258 | ||
1259 | Do this before setting *type_change_ok or *size_change_ok so that | |
1260 | we warn properly when dynamic library symbols are overridden. */ | |
1261 | ||
1262 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1263 | newweak = FALSE; |
15b43f48 | 1264 | if (olddef && newdyn) |
0f8a2703 AM |
1265 | oldweak = FALSE; |
1266 | ||
d334575b | 1267 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1268 | if (newfunc && oldfunc) |
fcb93ecf PB |
1269 | *type_change_ok = TRUE; |
1270 | ||
79349b09 AM |
1271 | /* It's OK to change the type if either the existing symbol or the |
1272 | new symbol is weak. A type change is also OK if the old symbol | |
1273 | is undefined and the new symbol is defined. */ | |
252b5132 | 1274 | |
79349b09 AM |
1275 | if (oldweak |
1276 | || newweak | |
1277 | || (newdef | |
1278 | && h->root.type == bfd_link_hash_undefined)) | |
1279 | *type_change_ok = TRUE; | |
1280 | ||
1281 | /* It's OK to change the size if either the existing symbol or the | |
1282 | new symbol is weak, or if the old symbol is undefined. */ | |
1283 | ||
1284 | if (*type_change_ok | |
1285 | || h->root.type == bfd_link_hash_undefined) | |
1286 | *size_change_ok = TRUE; | |
45d6a902 | 1287 | |
45d6a902 AM |
1288 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1289 | symbol, respectively, appears to be a common symbol in a dynamic | |
1290 | object. If a symbol appears in an uninitialized section, and is | |
1291 | not weak, and is not a function, then it may be a common symbol | |
1292 | which was resolved when the dynamic object was created. We want | |
1293 | to treat such symbols specially, because they raise special | |
1294 | considerations when setting the symbol size: if the symbol | |
1295 | appears as a common symbol in a regular object, and the size in | |
1296 | the regular object is larger, we must make sure that we use the | |
1297 | larger size. This problematic case can always be avoided in C, | |
1298 | but it must be handled correctly when using Fortran shared | |
1299 | libraries. | |
1300 | ||
1301 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1302 | likewise for OLDDYNCOMMON and OLDDEF. | |
1303 | ||
1304 | Note that this test is just a heuristic, and that it is quite | |
1305 | possible to have an uninitialized symbol in a shared object which | |
1306 | is really a definition, rather than a common symbol. This could | |
1307 | lead to some minor confusion when the symbol really is a common | |
1308 | symbol in some regular object. However, I think it will be | |
1309 | harmless. */ | |
1310 | ||
1311 | if (newdyn | |
1312 | && newdef | |
79349b09 | 1313 | && !newweak |
45d6a902 AM |
1314 | && (sec->flags & SEC_ALLOC) != 0 |
1315 | && (sec->flags & SEC_LOAD) == 0 | |
1316 | && sym->st_size > 0 | |
0a36a439 | 1317 | && !newfunc) |
45d6a902 AM |
1318 | newdyncommon = TRUE; |
1319 | else | |
1320 | newdyncommon = FALSE; | |
1321 | ||
1322 | if (olddyn | |
1323 | && olddef | |
1324 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1325 | && h->def_dynamic |
45d6a902 AM |
1326 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1327 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1328 | && h->size > 0 | |
0a36a439 | 1329 | && !oldfunc) |
45d6a902 AM |
1330 | olddyncommon = TRUE; |
1331 | else | |
1332 | olddyncommon = FALSE; | |
1333 | ||
a4d8e49b L |
1334 | /* We now know everything about the old and new symbols. We ask the |
1335 | backend to check if we can merge them. */ | |
a4d8e49b L |
1336 | if (bed->merge_symbol |
1337 | && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, | |
1338 | pold_alignment, skip, override, | |
1339 | type_change_ok, size_change_ok, | |
1340 | &newdyn, &newdef, &newdyncommon, &newweak, | |
1341 | abfd, &sec, | |
1342 | &olddyn, &olddef, &olddyncommon, &oldweak, | |
1343 | oldbfd, &oldsec)) | |
1344 | return FALSE; | |
1345 | ||
45d6a902 AM |
1346 | /* If both the old and the new symbols look like common symbols in a |
1347 | dynamic object, set the size of the symbol to the larger of the | |
1348 | two. */ | |
1349 | ||
1350 | if (olddyncommon | |
1351 | && newdyncommon | |
1352 | && sym->st_size != h->size) | |
1353 | { | |
1354 | /* Since we think we have two common symbols, issue a multiple | |
1355 | common warning if desired. Note that we only warn if the | |
1356 | size is different. If the size is the same, we simply let | |
1357 | the old symbol override the new one as normally happens with | |
1358 | symbols defined in dynamic objects. */ | |
1359 | ||
1360 | if (! ((*info->callbacks->multiple_common) | |
24f58f47 | 1361 | (info, &h->root, abfd, bfd_link_hash_common, sym->st_size))) |
45d6a902 | 1362 | return FALSE; |
252b5132 | 1363 | |
45d6a902 AM |
1364 | if (sym->st_size > h->size) |
1365 | h->size = sym->st_size; | |
252b5132 | 1366 | |
45d6a902 | 1367 | *size_change_ok = TRUE; |
252b5132 RH |
1368 | } |
1369 | ||
45d6a902 AM |
1370 | /* If we are looking at a dynamic object, and we have found a |
1371 | definition, we need to see if the symbol was already defined by | |
1372 | some other object. If so, we want to use the existing | |
1373 | definition, and we do not want to report a multiple symbol | |
1374 | definition error; we do this by clobbering *PSEC to be | |
1375 | bfd_und_section_ptr. | |
1376 | ||
1377 | We treat a common symbol as a definition if the symbol in the | |
1378 | shared library is a function, since common symbols always | |
1379 | represent variables; this can cause confusion in principle, but | |
1380 | any such confusion would seem to indicate an erroneous program or | |
1381 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1382 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1383 | |
1384 | if (newdyn | |
1385 | && newdef | |
77cfaee6 | 1386 | && (olddef |
45d6a902 | 1387 | || (h->root.type == bfd_link_hash_common |
0a36a439 | 1388 | && (newweak || newfunc)))) |
45d6a902 AM |
1389 | { |
1390 | *override = TRUE; | |
1391 | newdef = FALSE; | |
1392 | newdyncommon = FALSE; | |
252b5132 | 1393 | |
45d6a902 AM |
1394 | *psec = sec = bfd_und_section_ptr; |
1395 | *size_change_ok = TRUE; | |
252b5132 | 1396 | |
45d6a902 AM |
1397 | /* If we get here when the old symbol is a common symbol, then |
1398 | we are explicitly letting it override a weak symbol or | |
1399 | function in a dynamic object, and we don't want to warn about | |
1400 | a type change. If the old symbol is a defined symbol, a type | |
1401 | change warning may still be appropriate. */ | |
252b5132 | 1402 | |
45d6a902 AM |
1403 | if (h->root.type == bfd_link_hash_common) |
1404 | *type_change_ok = TRUE; | |
1405 | } | |
1406 | ||
1407 | /* Handle the special case of an old common symbol merging with a | |
1408 | new symbol which looks like a common symbol in a shared object. | |
1409 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1410 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1411 | right thing. */ | |
45d6a902 AM |
1412 | |
1413 | if (newdyncommon | |
1414 | && h->root.type == bfd_link_hash_common) | |
1415 | { | |
1416 | *override = TRUE; | |
1417 | newdef = FALSE; | |
1418 | newdyncommon = FALSE; | |
1419 | *pvalue = sym->st_size; | |
a4d8e49b | 1420 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1421 | *size_change_ok = TRUE; |
1422 | } | |
1423 | ||
c5e2cead | 1424 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1425 | if (newdef && olddef && newweak) |
54ac0771 | 1426 | { |
35ed3f94 | 1427 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1428 | if (!(oldbfd != NULL |
1429 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 AM |
1430 | && (abfd->flags & BFD_PLUGIN) == 0)) |
1431 | *skip = TRUE; | |
54ac0771 L |
1432 | |
1433 | /* Merge st_other. If the symbol already has a dynamic index, | |
1434 | but visibility says it should not be visible, turn it into a | |
1435 | local symbol. */ | |
1436 | elf_merge_st_other (abfd, h, sym, newdef, newdyn); | |
1437 | if (h->dynindx != -1) | |
1438 | switch (ELF_ST_VISIBILITY (h->other)) | |
1439 | { | |
1440 | case STV_INTERNAL: | |
1441 | case STV_HIDDEN: | |
1442 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1443 | break; | |
1444 | } | |
1445 | } | |
c5e2cead | 1446 | |
45d6a902 AM |
1447 | /* If the old symbol is from a dynamic object, and the new symbol is |
1448 | a definition which is not from a dynamic object, then the new | |
1449 | symbol overrides the old symbol. Symbols from regular files | |
1450 | always take precedence over symbols from dynamic objects, even if | |
1451 | they are defined after the dynamic object in the link. | |
1452 | ||
1453 | As above, we again permit a common symbol in a regular object to | |
1454 | override a definition in a shared object if the shared object | |
0f8a2703 | 1455 | symbol is a function or is weak. */ |
45d6a902 AM |
1456 | |
1457 | flip = NULL; | |
77cfaee6 | 1458 | if (!newdyn |
45d6a902 AM |
1459 | && (newdef |
1460 | || (bfd_is_com_section (sec) | |
0a36a439 | 1461 | && (oldweak || oldfunc))) |
45d6a902 AM |
1462 | && olddyn |
1463 | && olddef | |
f5385ebf | 1464 | && h->def_dynamic) |
45d6a902 AM |
1465 | { |
1466 | /* Change the hash table entry to undefined, and let | |
1467 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1468 | new definition. */ | |
1469 | ||
1470 | h->root.type = bfd_link_hash_undefined; | |
1471 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1472 | *size_change_ok = TRUE; | |
1473 | ||
1474 | olddef = FALSE; | |
1475 | olddyncommon = FALSE; | |
1476 | ||
1477 | /* We again permit a type change when a common symbol may be | |
1478 | overriding a function. */ | |
1479 | ||
1480 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1481 | { |
1482 | if (oldfunc) | |
1483 | { | |
1484 | /* If a common symbol overrides a function, make sure | |
1485 | that it isn't defined dynamically nor has type | |
1486 | function. */ | |
1487 | h->def_dynamic = 0; | |
1488 | h->type = STT_NOTYPE; | |
1489 | } | |
1490 | *type_change_ok = TRUE; | |
1491 | } | |
45d6a902 AM |
1492 | |
1493 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1494 | flip = *sym_hash; | |
1495 | else | |
1496 | /* This union may have been set to be non-NULL when this symbol | |
1497 | was seen in a dynamic object. We must force the union to be | |
1498 | NULL, so that it is correct for a regular symbol. */ | |
1499 | h->verinfo.vertree = NULL; | |
1500 | } | |
1501 | ||
1502 | /* Handle the special case of a new common symbol merging with an | |
1503 | old symbol that looks like it might be a common symbol defined in | |
1504 | a shared object. Note that we have already handled the case in | |
1505 | which a new common symbol should simply override the definition | |
1506 | in the shared library. */ | |
1507 | ||
1508 | if (! newdyn | |
1509 | && bfd_is_com_section (sec) | |
1510 | && olddyncommon) | |
1511 | { | |
1512 | /* It would be best if we could set the hash table entry to a | |
1513 | common symbol, but we don't know what to use for the section | |
1514 | or the alignment. */ | |
1515 | if (! ((*info->callbacks->multiple_common) | |
24f58f47 | 1516 | (info, &h->root, abfd, bfd_link_hash_common, sym->st_size))) |
45d6a902 AM |
1517 | return FALSE; |
1518 | ||
4cc11e76 | 1519 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1520 | larger, pretend that the new symbol has its size. */ |
1521 | ||
1522 | if (h->size > *pvalue) | |
1523 | *pvalue = h->size; | |
1524 | ||
af44c138 L |
1525 | /* We need to remember the alignment required by the symbol |
1526 | in the dynamic object. */ | |
1527 | BFD_ASSERT (pold_alignment); | |
1528 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1529 | |
1530 | olddef = FALSE; | |
1531 | olddyncommon = FALSE; | |
1532 | ||
1533 | h->root.type = bfd_link_hash_undefined; | |
1534 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1535 | ||
1536 | *size_change_ok = TRUE; | |
1537 | *type_change_ok = TRUE; | |
1538 | ||
1539 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1540 | flip = *sym_hash; | |
1541 | else | |
1542 | h->verinfo.vertree = NULL; | |
1543 | } | |
1544 | ||
1545 | if (flip != NULL) | |
1546 | { | |
1547 | /* Handle the case where we had a versioned symbol in a dynamic | |
1548 | library and now find a definition in a normal object. In this | |
1549 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1550 | flip->root.type = h->root.type; |
00cbee0a | 1551 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1552 | h->root.type = bfd_link_hash_indirect; |
1553 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1554 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1555 | if (h->def_dynamic) |
45d6a902 | 1556 | { |
f5385ebf AM |
1557 | h->def_dynamic = 0; |
1558 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1559 | } |
1560 | } | |
1561 | ||
45d6a902 AM |
1562 | return TRUE; |
1563 | } | |
1564 | ||
1565 | /* This function is called to create an indirect symbol from the | |
1566 | default for the symbol with the default version if needed. The | |
1567 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1568 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1569 | |
28caa186 | 1570 | static bfd_boolean |
268b6b39 AM |
1571 | _bfd_elf_add_default_symbol (bfd *abfd, |
1572 | struct bfd_link_info *info, | |
1573 | struct elf_link_hash_entry *h, | |
1574 | const char *name, | |
1575 | Elf_Internal_Sym *sym, | |
1576 | asection **psec, | |
1577 | bfd_vma *value, | |
1578 | bfd_boolean *dynsym, | |
0f8a2703 | 1579 | bfd_boolean override) |
45d6a902 AM |
1580 | { |
1581 | bfd_boolean type_change_ok; | |
1582 | bfd_boolean size_change_ok; | |
1583 | bfd_boolean skip; | |
1584 | char *shortname; | |
1585 | struct elf_link_hash_entry *hi; | |
1586 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1587 | const struct elf_backend_data *bed; |
45d6a902 AM |
1588 | bfd_boolean collect; |
1589 | bfd_boolean dynamic; | |
1590 | char *p; | |
1591 | size_t len, shortlen; | |
1592 | asection *sec; | |
1593 | ||
1594 | /* If this symbol has a version, and it is the default version, we | |
1595 | create an indirect symbol from the default name to the fully | |
1596 | decorated name. This will cause external references which do not | |
1597 | specify a version to be bound to this version of the symbol. */ | |
1598 | p = strchr (name, ELF_VER_CHR); | |
1599 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1600 | return TRUE; | |
1601 | ||
1602 | if (override) | |
1603 | { | |
4cc11e76 | 1604 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1605 | need to create the indirect symbol from the default name. */ |
1606 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1607 | FALSE, FALSE); | |
1608 | BFD_ASSERT (hi != NULL); | |
1609 | if (hi == h) | |
1610 | return TRUE; | |
1611 | while (hi->root.type == bfd_link_hash_indirect | |
1612 | || hi->root.type == bfd_link_hash_warning) | |
1613 | { | |
1614 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1615 | if (hi == h) | |
1616 | return TRUE; | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | bed = get_elf_backend_data (abfd); | |
1621 | collect = bed->collect; | |
1622 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1623 | ||
1624 | shortlen = p - name; | |
a50b1753 | 1625 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1626 | if (shortname == NULL) |
1627 | return FALSE; | |
1628 | memcpy (shortname, name, shortlen); | |
1629 | shortname[shortlen] = '\0'; | |
1630 | ||
1631 | /* We are going to create a new symbol. Merge it with any existing | |
1632 | symbol with this name. For the purposes of the merge, act as | |
1633 | though we were defining the symbol we just defined, although we | |
1634 | actually going to define an indirect symbol. */ | |
1635 | type_change_ok = FALSE; | |
1636 | size_change_ok = FALSE; | |
1637 | sec = *psec; | |
1638 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1639 | NULL, &hi, &skip, &override, |
1640 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1641 | return FALSE; |
1642 | ||
1643 | if (skip) | |
1644 | goto nondefault; | |
1645 | ||
1646 | if (! override) | |
1647 | { | |
1648 | bh = &hi->root; | |
1649 | if (! (_bfd_generic_link_add_one_symbol | |
1650 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1651 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1652 | return FALSE; |
1653 | hi = (struct elf_link_hash_entry *) bh; | |
1654 | } | |
1655 | else | |
1656 | { | |
1657 | /* In this case the symbol named SHORTNAME is overriding the | |
1658 | indirect symbol we want to add. We were planning on making | |
1659 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1660 | is the name without a version. NAME is the fully versioned | |
1661 | name, and it is the default version. | |
1662 | ||
1663 | Overriding means that we already saw a definition for the | |
1664 | symbol SHORTNAME in a regular object, and it is overriding | |
1665 | the symbol defined in the dynamic object. | |
1666 | ||
1667 | When this happens, we actually want to change NAME, the | |
1668 | symbol we just added, to refer to SHORTNAME. This will cause | |
1669 | references to NAME in the shared object to become references | |
1670 | to SHORTNAME in the regular object. This is what we expect | |
1671 | when we override a function in a shared object: that the | |
1672 | references in the shared object will be mapped to the | |
1673 | definition in the regular object. */ | |
1674 | ||
1675 | while (hi->root.type == bfd_link_hash_indirect | |
1676 | || hi->root.type == bfd_link_hash_warning) | |
1677 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1678 | ||
1679 | h->root.type = bfd_link_hash_indirect; | |
1680 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1681 | if (h->def_dynamic) |
45d6a902 | 1682 | { |
f5385ebf AM |
1683 | h->def_dynamic = 0; |
1684 | hi->ref_dynamic = 1; | |
1685 | if (hi->ref_regular | |
1686 | || hi->def_regular) | |
45d6a902 | 1687 | { |
c152c796 | 1688 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1689 | return FALSE; |
1690 | } | |
1691 | } | |
1692 | ||
1693 | /* Now set HI to H, so that the following code will set the | |
1694 | other fields correctly. */ | |
1695 | hi = h; | |
1696 | } | |
1697 | ||
fab4a87f L |
1698 | /* Check if HI is a warning symbol. */ |
1699 | if (hi->root.type == bfd_link_hash_warning) | |
1700 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1701 | ||
45d6a902 AM |
1702 | /* If there is a duplicate definition somewhere, then HI may not |
1703 | point to an indirect symbol. We will have reported an error to | |
1704 | the user in that case. */ | |
1705 | ||
1706 | if (hi->root.type == bfd_link_hash_indirect) | |
1707 | { | |
1708 | struct elf_link_hash_entry *ht; | |
1709 | ||
45d6a902 | 1710 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1711 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 AM |
1712 | |
1713 | /* See if the new flags lead us to realize that the symbol must | |
1714 | be dynamic. */ | |
1715 | if (! *dynsym) | |
1716 | { | |
1717 | if (! dynamic) | |
1718 | { | |
ca4a656b | 1719 | if (! info->executable |
f5385ebf | 1720 | || hi->ref_dynamic) |
45d6a902 AM |
1721 | *dynsym = TRUE; |
1722 | } | |
1723 | else | |
1724 | { | |
f5385ebf | 1725 | if (hi->ref_regular) |
45d6a902 AM |
1726 | *dynsym = TRUE; |
1727 | } | |
1728 | } | |
1729 | } | |
1730 | ||
1731 | /* We also need to define an indirection from the nondefault version | |
1732 | of the symbol. */ | |
1733 | ||
1734 | nondefault: | |
1735 | len = strlen (name); | |
a50b1753 | 1736 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
1737 | if (shortname == NULL) |
1738 | return FALSE; | |
1739 | memcpy (shortname, name, shortlen); | |
1740 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1741 | ||
1742 | /* Once again, merge with any existing symbol. */ | |
1743 | type_change_ok = FALSE; | |
1744 | size_change_ok = FALSE; | |
1745 | sec = *psec; | |
1746 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
af44c138 L |
1747 | NULL, &hi, &skip, &override, |
1748 | &type_change_ok, &size_change_ok)) | |
45d6a902 AM |
1749 | return FALSE; |
1750 | ||
1751 | if (skip) | |
1752 | return TRUE; | |
1753 | ||
1754 | if (override) | |
1755 | { | |
1756 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1757 | the type of override we do in the case above unless it is | |
4cc11e76 | 1758 | overridden by a versioned definition. */ |
45d6a902 AM |
1759 | if (hi->root.type != bfd_link_hash_defined |
1760 | && hi->root.type != bfd_link_hash_defweak) | |
1761 | (*_bfd_error_handler) | |
d003868e AM |
1762 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1763 | abfd, shortname); | |
45d6a902 AM |
1764 | } |
1765 | else | |
1766 | { | |
1767 | bh = &hi->root; | |
1768 | if (! (_bfd_generic_link_add_one_symbol | |
1769 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1770 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1771 | return FALSE; |
1772 | hi = (struct elf_link_hash_entry *) bh; | |
1773 | ||
1774 | /* If there is a duplicate definition somewhere, then HI may not | |
1775 | point to an indirect symbol. We will have reported an error | |
1776 | to the user in that case. */ | |
1777 | ||
1778 | if (hi->root.type == bfd_link_hash_indirect) | |
1779 | { | |
fcfa13d2 | 1780 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
45d6a902 AM |
1781 | |
1782 | /* See if the new flags lead us to realize that the symbol | |
1783 | must be dynamic. */ | |
1784 | if (! *dynsym) | |
1785 | { | |
1786 | if (! dynamic) | |
1787 | { | |
ca4a656b | 1788 | if (! info->executable |
f5385ebf | 1789 | || hi->ref_dynamic) |
45d6a902 AM |
1790 | *dynsym = TRUE; |
1791 | } | |
1792 | else | |
1793 | { | |
f5385ebf | 1794 | if (hi->ref_regular) |
45d6a902 AM |
1795 | *dynsym = TRUE; |
1796 | } | |
1797 | } | |
1798 | } | |
1799 | } | |
1800 | ||
1801 | return TRUE; | |
1802 | } | |
1803 | \f | |
1804 | /* This routine is used to export all defined symbols into the dynamic | |
1805 | symbol table. It is called via elf_link_hash_traverse. */ | |
1806 | ||
28caa186 | 1807 | static bfd_boolean |
268b6b39 | 1808 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1809 | { |
a50b1753 | 1810 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
1811 | |
1812 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1813 | if (h->root.type == bfd_link_hash_indirect) | |
1814 | return TRUE; | |
1815 | ||
7686d77d AM |
1816 | /* Ignore this if we won't export it. */ |
1817 | if (!eif->info->export_dynamic && !h->dynamic) | |
1818 | return TRUE; | |
45d6a902 AM |
1819 | |
1820 | if (h->dynindx == -1 | |
fd91d419 L |
1821 | && (h->def_regular || h->ref_regular) |
1822 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
1823 | h->root.root.string)) | |
45d6a902 | 1824 | { |
fd91d419 | 1825 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 1826 | { |
fd91d419 L |
1827 | eif->failed = TRUE; |
1828 | return FALSE; | |
45d6a902 AM |
1829 | } |
1830 | } | |
1831 | ||
1832 | return TRUE; | |
1833 | } | |
1834 | \f | |
1835 | /* Look through the symbols which are defined in other shared | |
1836 | libraries and referenced here. Update the list of version | |
1837 | dependencies. This will be put into the .gnu.version_r section. | |
1838 | This function is called via elf_link_hash_traverse. */ | |
1839 | ||
28caa186 | 1840 | static bfd_boolean |
268b6b39 AM |
1841 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1842 | void *data) | |
45d6a902 | 1843 | { |
a50b1753 | 1844 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
1845 | Elf_Internal_Verneed *t; |
1846 | Elf_Internal_Vernaux *a; | |
1847 | bfd_size_type amt; | |
1848 | ||
45d6a902 AM |
1849 | /* We only care about symbols defined in shared objects with version |
1850 | information. */ | |
f5385ebf AM |
1851 | if (!h->def_dynamic |
1852 | || h->def_regular | |
45d6a902 AM |
1853 | || h->dynindx == -1 |
1854 | || h->verinfo.verdef == NULL) | |
1855 | return TRUE; | |
1856 | ||
1857 | /* See if we already know about this version. */ | |
28caa186 AM |
1858 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
1859 | t != NULL; | |
1860 | t = t->vn_nextref) | |
45d6a902 AM |
1861 | { |
1862 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1863 | continue; | |
1864 | ||
1865 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1866 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1867 | return TRUE; | |
1868 | ||
1869 | break; | |
1870 | } | |
1871 | ||
1872 | /* This is a new version. Add it to tree we are building. */ | |
1873 | ||
1874 | if (t == NULL) | |
1875 | { | |
1876 | amt = sizeof *t; | |
a50b1753 | 1877 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
1878 | if (t == NULL) |
1879 | { | |
1880 | rinfo->failed = TRUE; | |
1881 | return FALSE; | |
1882 | } | |
1883 | ||
1884 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
1885 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
1886 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
1887 | } |
1888 | ||
1889 | amt = sizeof *a; | |
a50b1753 | 1890 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
1891 | if (a == NULL) |
1892 | { | |
1893 | rinfo->failed = TRUE; | |
1894 | return FALSE; | |
1895 | } | |
45d6a902 AM |
1896 | |
1897 | /* Note that we are copying a string pointer here, and testing it | |
1898 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1899 | discard the string data when low in memory, this will have to be | |
1900 | fixed. */ | |
1901 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1902 | ||
1903 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1904 | a->vna_nextptr = t->vn_auxptr; | |
1905 | ||
1906 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1907 | ++rinfo->vers; | |
1908 | ||
1909 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1910 | ||
1911 | t->vn_auxptr = a; | |
1912 | ||
1913 | return TRUE; | |
1914 | } | |
1915 | ||
1916 | /* Figure out appropriate versions for all the symbols. We may not | |
1917 | have the version number script until we have read all of the input | |
1918 | files, so until that point we don't know which symbols should be | |
1919 | local. This function is called via elf_link_hash_traverse. */ | |
1920 | ||
28caa186 | 1921 | static bfd_boolean |
268b6b39 | 1922 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1923 | { |
28caa186 | 1924 | struct elf_info_failed *sinfo; |
45d6a902 | 1925 | struct bfd_link_info *info; |
9c5bfbb7 | 1926 | const struct elf_backend_data *bed; |
45d6a902 AM |
1927 | struct elf_info_failed eif; |
1928 | char *p; | |
1929 | bfd_size_type amt; | |
1930 | ||
a50b1753 | 1931 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
1932 | info = sinfo->info; |
1933 | ||
45d6a902 AM |
1934 | /* Fix the symbol flags. */ |
1935 | eif.failed = FALSE; | |
1936 | eif.info = info; | |
1937 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1938 | { | |
1939 | if (eif.failed) | |
1940 | sinfo->failed = TRUE; | |
1941 | return FALSE; | |
1942 | } | |
1943 | ||
1944 | /* We only need version numbers for symbols defined in regular | |
1945 | objects. */ | |
f5385ebf | 1946 | if (!h->def_regular) |
45d6a902 AM |
1947 | return TRUE; |
1948 | ||
28caa186 | 1949 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
1950 | p = strchr (h->root.root.string, ELF_VER_CHR); |
1951 | if (p != NULL && h->verinfo.vertree == NULL) | |
1952 | { | |
1953 | struct bfd_elf_version_tree *t; | |
1954 | bfd_boolean hidden; | |
1955 | ||
1956 | hidden = TRUE; | |
1957 | ||
1958 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1959 | not a hidden symbol. */ | |
1960 | ++p; | |
1961 | if (*p == ELF_VER_CHR) | |
1962 | { | |
1963 | hidden = FALSE; | |
1964 | ++p; | |
1965 | } | |
1966 | ||
1967 | /* If there is no version string, we can just return out. */ | |
1968 | if (*p == '\0') | |
1969 | { | |
1970 | if (hidden) | |
f5385ebf | 1971 | h->hidden = 1; |
45d6a902 AM |
1972 | return TRUE; |
1973 | } | |
1974 | ||
1975 | /* Look for the version. If we find it, it is no longer weak. */ | |
fd91d419 | 1976 | for (t = sinfo->info->version_info; t != NULL; t = t->next) |
45d6a902 AM |
1977 | { |
1978 | if (strcmp (t->name, p) == 0) | |
1979 | { | |
1980 | size_t len; | |
1981 | char *alc; | |
1982 | struct bfd_elf_version_expr *d; | |
1983 | ||
1984 | len = p - h->root.root.string; | |
a50b1753 | 1985 | alc = (char *) bfd_malloc (len); |
45d6a902 | 1986 | if (alc == NULL) |
14b1c01e AM |
1987 | { |
1988 | sinfo->failed = TRUE; | |
1989 | return FALSE; | |
1990 | } | |
45d6a902 AM |
1991 | memcpy (alc, h->root.root.string, len - 1); |
1992 | alc[len - 1] = '\0'; | |
1993 | if (alc[len - 2] == ELF_VER_CHR) | |
1994 | alc[len - 2] = '\0'; | |
1995 | ||
1996 | h->verinfo.vertree = t; | |
1997 | t->used = TRUE; | |
1998 | d = NULL; | |
1999 | ||
108ba305 JJ |
2000 | if (t->globals.list != NULL) |
2001 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
2002 | |
2003 | /* See if there is anything to force this symbol to | |
2004 | local scope. */ | |
108ba305 | 2005 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 2006 | { |
108ba305 JJ |
2007 | d = (*t->match) (&t->locals, NULL, alc); |
2008 | if (d != NULL | |
2009 | && h->dynindx != -1 | |
108ba305 JJ |
2010 | && ! info->export_dynamic) |
2011 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2012 | } |
2013 | ||
2014 | free (alc); | |
2015 | break; | |
2016 | } | |
2017 | } | |
2018 | ||
2019 | /* If we are building an application, we need to create a | |
2020 | version node for this version. */ | |
36af4a4e | 2021 | if (t == NULL && info->executable) |
45d6a902 AM |
2022 | { |
2023 | struct bfd_elf_version_tree **pp; | |
2024 | int version_index; | |
2025 | ||
2026 | /* If we aren't going to export this symbol, we don't need | |
2027 | to worry about it. */ | |
2028 | if (h->dynindx == -1) | |
2029 | return TRUE; | |
2030 | ||
2031 | amt = sizeof *t; | |
a50b1753 | 2032 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt); |
45d6a902 AM |
2033 | if (t == NULL) |
2034 | { | |
2035 | sinfo->failed = TRUE; | |
2036 | return FALSE; | |
2037 | } | |
2038 | ||
45d6a902 | 2039 | t->name = p; |
45d6a902 AM |
2040 | t->name_indx = (unsigned int) -1; |
2041 | t->used = TRUE; | |
2042 | ||
2043 | version_index = 1; | |
2044 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2045 | if (sinfo->info->version_info != NULL |
2046 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2047 | version_index = 0; |
fd91d419 L |
2048 | for (pp = &sinfo->info->version_info; |
2049 | *pp != NULL; | |
2050 | pp = &(*pp)->next) | |
45d6a902 AM |
2051 | ++version_index; |
2052 | t->vernum = version_index; | |
2053 | ||
2054 | *pp = t; | |
2055 | ||
2056 | h->verinfo.vertree = t; | |
2057 | } | |
2058 | else if (t == NULL) | |
2059 | { | |
2060 | /* We could not find the version for a symbol when | |
2061 | generating a shared archive. Return an error. */ | |
2062 | (*_bfd_error_handler) | |
c55fe096 | 2063 | (_("%B: version node not found for symbol %s"), |
28caa186 | 2064 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2065 | bfd_set_error (bfd_error_bad_value); |
2066 | sinfo->failed = TRUE; | |
2067 | return FALSE; | |
2068 | } | |
2069 | ||
2070 | if (hidden) | |
f5385ebf | 2071 | h->hidden = 1; |
45d6a902 AM |
2072 | } |
2073 | ||
2074 | /* If we don't have a version for this symbol, see if we can find | |
2075 | something. */ | |
fd91d419 | 2076 | if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) |
45d6a902 | 2077 | { |
1e8fa21e | 2078 | bfd_boolean hide; |
ae5a3597 | 2079 | |
fd91d419 L |
2080 | h->verinfo.vertree |
2081 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2082 | h->root.root.string, &hide); | |
1e8fa21e AM |
2083 | if (h->verinfo.vertree != NULL && hide) |
2084 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2085 | } |
2086 | ||
2087 | return TRUE; | |
2088 | } | |
2089 | \f | |
45d6a902 AM |
2090 | /* Read and swap the relocs from the section indicated by SHDR. This |
2091 | may be either a REL or a RELA section. The relocations are | |
2092 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2093 | which should have already been allocated to contain enough space. | |
2094 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2095 | relocations should be stored. | |
2096 | ||
2097 | Returns FALSE if something goes wrong. */ | |
2098 | ||
2099 | static bfd_boolean | |
268b6b39 | 2100 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2101 | asection *sec, |
268b6b39 AM |
2102 | Elf_Internal_Shdr *shdr, |
2103 | void *external_relocs, | |
2104 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2105 | { |
9c5bfbb7 | 2106 | const struct elf_backend_data *bed; |
268b6b39 | 2107 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2108 | const bfd_byte *erela; |
2109 | const bfd_byte *erelaend; | |
2110 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2111 | Elf_Internal_Shdr *symtab_hdr; |
2112 | size_t nsyms; | |
45d6a902 | 2113 | |
45d6a902 AM |
2114 | /* Position ourselves at the start of the section. */ |
2115 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2116 | return FALSE; | |
2117 | ||
2118 | /* Read the relocations. */ | |
2119 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2120 | return FALSE; | |
2121 | ||
243ef1e0 | 2122 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2123 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2124 | |
45d6a902 AM |
2125 | bed = get_elf_backend_data (abfd); |
2126 | ||
2127 | /* Convert the external relocations to the internal format. */ | |
2128 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2129 | swap_in = bed->s->swap_reloc_in; | |
2130 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2131 | swap_in = bed->s->swap_reloca_in; | |
2132 | else | |
2133 | { | |
2134 | bfd_set_error (bfd_error_wrong_format); | |
2135 | return FALSE; | |
2136 | } | |
2137 | ||
a50b1753 | 2138 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2139 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2140 | irela = internal_relocs; |
2141 | while (erela < erelaend) | |
2142 | { | |
243ef1e0 L |
2143 | bfd_vma r_symndx; |
2144 | ||
45d6a902 | 2145 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2146 | r_symndx = ELF32_R_SYM (irela->r_info); |
2147 | if (bed->s->arch_size == 64) | |
2148 | r_symndx >>= 24; | |
ce98a316 NC |
2149 | if (nsyms > 0) |
2150 | { | |
2151 | if ((size_t) r_symndx >= nsyms) | |
2152 | { | |
2153 | (*_bfd_error_handler) | |
2154 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" | |
2155 | " for offset 0x%lx in section `%A'"), | |
2156 | abfd, sec, | |
2157 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
2158 | bfd_set_error (bfd_error_bad_value); | |
2159 | return FALSE; | |
2160 | } | |
2161 | } | |
cf35638d | 2162 | else if (r_symndx != STN_UNDEF) |
243ef1e0 L |
2163 | { |
2164 | (*_bfd_error_handler) | |
ce98a316 NC |
2165 | (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'" |
2166 | " when the object file has no symbol table"), | |
d003868e AM |
2167 | abfd, sec, |
2168 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2169 | bfd_set_error (bfd_error_bad_value); |
2170 | return FALSE; | |
2171 | } | |
45d6a902 AM |
2172 | irela += bed->s->int_rels_per_ext_rel; |
2173 | erela += shdr->sh_entsize; | |
2174 | } | |
2175 | ||
2176 | return TRUE; | |
2177 | } | |
2178 | ||
2179 | /* Read and swap the relocs for a section O. They may have been | |
2180 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2181 | not NULL, they are used as buffers to read into. They are known to | |
2182 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2183 | the return value is allocated using either malloc or bfd_alloc, | |
2184 | according to the KEEP_MEMORY argument. If O has two relocation | |
2185 | sections (both REL and RELA relocations), then the REL_HDR | |
2186 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2187 | RELA_HDR relocations. */ |
45d6a902 AM |
2188 | |
2189 | Elf_Internal_Rela * | |
268b6b39 AM |
2190 | _bfd_elf_link_read_relocs (bfd *abfd, |
2191 | asection *o, | |
2192 | void *external_relocs, | |
2193 | Elf_Internal_Rela *internal_relocs, | |
2194 | bfd_boolean keep_memory) | |
45d6a902 | 2195 | { |
268b6b39 | 2196 | void *alloc1 = NULL; |
45d6a902 | 2197 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2198 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2199 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2200 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2201 | |
d4730f92 BS |
2202 | if (esdo->relocs != NULL) |
2203 | return esdo->relocs; | |
45d6a902 AM |
2204 | |
2205 | if (o->reloc_count == 0) | |
2206 | return NULL; | |
2207 | ||
45d6a902 AM |
2208 | if (internal_relocs == NULL) |
2209 | { | |
2210 | bfd_size_type size; | |
2211 | ||
2212 | size = o->reloc_count; | |
2213 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2214 | if (keep_memory) | |
a50b1753 | 2215 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2216 | else |
a50b1753 | 2217 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2218 | if (internal_relocs == NULL) |
2219 | goto error_return; | |
2220 | } | |
2221 | ||
2222 | if (external_relocs == NULL) | |
2223 | { | |
d4730f92 BS |
2224 | bfd_size_type size = 0; |
2225 | ||
2226 | if (esdo->rel.hdr) | |
2227 | size += esdo->rel.hdr->sh_size; | |
2228 | if (esdo->rela.hdr) | |
2229 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2230 | |
268b6b39 | 2231 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2232 | if (alloc1 == NULL) |
2233 | goto error_return; | |
2234 | external_relocs = alloc1; | |
2235 | } | |
2236 | ||
d4730f92 BS |
2237 | internal_rela_relocs = internal_relocs; |
2238 | if (esdo->rel.hdr) | |
2239 | { | |
2240 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2241 | external_relocs, | |
2242 | internal_relocs)) | |
2243 | goto error_return; | |
2244 | external_relocs = (((bfd_byte *) external_relocs) | |
2245 | + esdo->rel.hdr->sh_size); | |
2246 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2247 | * bed->s->int_rels_per_ext_rel); | |
2248 | } | |
2249 | ||
2250 | if (esdo->rela.hdr | |
2251 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2252 | external_relocs, | |
2253 | internal_rela_relocs))) | |
45d6a902 AM |
2254 | goto error_return; |
2255 | ||
2256 | /* Cache the results for next time, if we can. */ | |
2257 | if (keep_memory) | |
d4730f92 | 2258 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2259 | |
2260 | if (alloc1 != NULL) | |
2261 | free (alloc1); | |
2262 | ||
2263 | /* Don't free alloc2, since if it was allocated we are passing it | |
2264 | back (under the name of internal_relocs). */ | |
2265 | ||
2266 | return internal_relocs; | |
2267 | ||
2268 | error_return: | |
2269 | if (alloc1 != NULL) | |
2270 | free (alloc1); | |
2271 | if (alloc2 != NULL) | |
4dd07732 AM |
2272 | { |
2273 | if (keep_memory) | |
2274 | bfd_release (abfd, alloc2); | |
2275 | else | |
2276 | free (alloc2); | |
2277 | } | |
45d6a902 AM |
2278 | return NULL; |
2279 | } | |
2280 | ||
2281 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2282 | section header for a section containing relocations for O. */ | |
2283 | ||
28caa186 | 2284 | static bfd_boolean |
268b6b39 | 2285 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
d4730f92 | 2286 | struct bfd_elf_section_reloc_data *reldata) |
45d6a902 | 2287 | { |
d4730f92 | 2288 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2289 | |
2290 | /* That allows us to calculate the size of the section. */ | |
d4730f92 | 2291 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2292 | |
2293 | /* The contents field must last into write_object_contents, so we | |
2294 | allocate it with bfd_alloc rather than malloc. Also since we | |
2295 | cannot be sure that the contents will actually be filled in, | |
2296 | we zero the allocated space. */ | |
a50b1753 | 2297 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2298 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2299 | return FALSE; | |
2300 | ||
d4730f92 | 2301 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2302 | { |
2303 | struct elf_link_hash_entry **p; | |
2304 | ||
a50b1753 | 2305 | p = (struct elf_link_hash_entry **) |
d4730f92 | 2306 | bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2307 | if (p == NULL) |
2308 | return FALSE; | |
2309 | ||
d4730f92 | 2310 | reldata->hashes = p; |
45d6a902 AM |
2311 | } |
2312 | ||
2313 | return TRUE; | |
2314 | } | |
2315 | ||
2316 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2317 | originated from the section given by INPUT_REL_HDR) to the | |
2318 | OUTPUT_BFD. */ | |
2319 | ||
2320 | bfd_boolean | |
268b6b39 AM |
2321 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2322 | asection *input_section, | |
2323 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2324 | Elf_Internal_Rela *internal_relocs, |
2325 | struct elf_link_hash_entry **rel_hash | |
2326 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2327 | { |
2328 | Elf_Internal_Rela *irela; | |
2329 | Elf_Internal_Rela *irelaend; | |
2330 | bfd_byte *erel; | |
d4730f92 | 2331 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2332 | asection *output_section; |
9c5bfbb7 | 2333 | const struct elf_backend_data *bed; |
268b6b39 | 2334 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2335 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2336 | |
2337 | output_section = input_section->output_section; | |
45d6a902 | 2338 | |
d4730f92 BS |
2339 | bed = get_elf_backend_data (output_bfd); |
2340 | esdo = elf_section_data (output_section); | |
2341 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2342 | { |
d4730f92 BS |
2343 | output_reldata = &esdo->rel; |
2344 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2345 | } |
d4730f92 BS |
2346 | else if (esdo->rela.hdr |
2347 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2348 | { |
d4730f92 BS |
2349 | output_reldata = &esdo->rela; |
2350 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2351 | } |
2352 | else | |
2353 | { | |
2354 | (*_bfd_error_handler) | |
d003868e AM |
2355 | (_("%B: relocation size mismatch in %B section %A"), |
2356 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2357 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2358 | return FALSE; |
2359 | } | |
2360 | ||
d4730f92 BS |
2361 | erel = output_reldata->hdr->contents; |
2362 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2363 | irela = internal_relocs; |
2364 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2365 | * bed->s->int_rels_per_ext_rel); | |
2366 | while (irela < irelaend) | |
2367 | { | |
2368 | (*swap_out) (output_bfd, irela, erel); | |
2369 | irela += bed->s->int_rels_per_ext_rel; | |
2370 | erel += input_rel_hdr->sh_entsize; | |
2371 | } | |
2372 | ||
2373 | /* Bump the counter, so that we know where to add the next set of | |
2374 | relocations. */ | |
d4730f92 | 2375 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2376 | |
2377 | return TRUE; | |
2378 | } | |
2379 | \f | |
508c3946 L |
2380 | /* Make weak undefined symbols in PIE dynamic. */ |
2381 | ||
2382 | bfd_boolean | |
2383 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2384 | struct elf_link_hash_entry *h) | |
2385 | { | |
2386 | if (info->pie | |
2387 | && h->dynindx == -1 | |
2388 | && h->root.type == bfd_link_hash_undefweak) | |
2389 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2390 | ||
2391 | return TRUE; | |
2392 | } | |
2393 | ||
45d6a902 AM |
2394 | /* Fix up the flags for a symbol. This handles various cases which |
2395 | can only be fixed after all the input files are seen. This is | |
2396 | currently called by both adjust_dynamic_symbol and | |
2397 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2398 | the face of future changes. */ | |
2399 | ||
28caa186 | 2400 | static bfd_boolean |
268b6b39 AM |
2401 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2402 | struct elf_info_failed *eif) | |
45d6a902 | 2403 | { |
33774f08 | 2404 | const struct elf_backend_data *bed; |
508c3946 | 2405 | |
45d6a902 AM |
2406 | /* If this symbol was mentioned in a non-ELF file, try to set |
2407 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2408 | permit a non-ELF file to correctly refer to a symbol defined in | |
2409 | an ELF dynamic object. */ | |
f5385ebf | 2410 | if (h->non_elf) |
45d6a902 AM |
2411 | { |
2412 | while (h->root.type == bfd_link_hash_indirect) | |
2413 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2414 | ||
2415 | if (h->root.type != bfd_link_hash_defined | |
2416 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2417 | { |
2418 | h->ref_regular = 1; | |
2419 | h->ref_regular_nonweak = 1; | |
2420 | } | |
45d6a902 AM |
2421 | else |
2422 | { | |
2423 | if (h->root.u.def.section->owner != NULL | |
2424 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2425 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2426 | { |
2427 | h->ref_regular = 1; | |
2428 | h->ref_regular_nonweak = 1; | |
2429 | } | |
45d6a902 | 2430 | else |
f5385ebf | 2431 | h->def_regular = 1; |
45d6a902 AM |
2432 | } |
2433 | ||
2434 | if (h->dynindx == -1 | |
f5385ebf AM |
2435 | && (h->def_dynamic |
2436 | || h->ref_dynamic)) | |
45d6a902 | 2437 | { |
c152c796 | 2438 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2439 | { |
2440 | eif->failed = TRUE; | |
2441 | return FALSE; | |
2442 | } | |
2443 | } | |
2444 | } | |
2445 | else | |
2446 | { | |
f5385ebf | 2447 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2448 | was first seen in a non-ELF file. Fortunately, if the symbol |
2449 | was first seen in an ELF file, we're probably OK unless the | |
2450 | symbol was defined in a non-ELF file. Catch that case here. | |
2451 | FIXME: We're still in trouble if the symbol was first seen in | |
2452 | a dynamic object, and then later in a non-ELF regular object. */ | |
2453 | if ((h->root.type == bfd_link_hash_defined | |
2454 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2455 | && !h->def_regular |
45d6a902 AM |
2456 | && (h->root.u.def.section->owner != NULL |
2457 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2458 | != bfd_target_elf_flavour) | |
2459 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2460 | && !h->def_dynamic))) |
2461 | h->def_regular = 1; | |
45d6a902 AM |
2462 | } |
2463 | ||
508c3946 | 2464 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2465 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2466 | if (bed->elf_backend_fixup_symbol | |
2467 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2468 | return FALSE; | |
508c3946 | 2469 | |
45d6a902 AM |
2470 | /* If this is a final link, and the symbol was defined as a common |
2471 | symbol in a regular object file, and there was no definition in | |
2472 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2473 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2474 | flag will not have been set. */ |
2475 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2476 | && !h->def_regular |
2477 | && h->ref_regular | |
2478 | && !h->def_dynamic | |
45d6a902 | 2479 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2480 | h->def_regular = 1; |
45d6a902 AM |
2481 | |
2482 | /* If -Bsymbolic was used (which means to bind references to global | |
2483 | symbols to the definition within the shared object), and this | |
2484 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2485 | need a PLT entry. Likewise, if the symbol has non-default |
2486 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2487 | will force it local. */ |
f5385ebf | 2488 | if (h->needs_plt |
45d6a902 | 2489 | && eif->info->shared |
0eddce27 | 2490 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2491 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2492 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2493 | && h->def_regular) |
45d6a902 | 2494 | { |
45d6a902 AM |
2495 | bfd_boolean force_local; |
2496 | ||
45d6a902 AM |
2497 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2498 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2499 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2500 | } | |
2501 | ||
2502 | /* If a weak undefined symbol has non-default visibility, we also | |
2503 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2504 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 | 2505 | && h->root.type == bfd_link_hash_undefweak) |
33774f08 | 2506 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
45d6a902 AM |
2507 | |
2508 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2509 | the real definition in the dynamic object, copy interesting flags | |
2510 | over to the real definition. */ | |
f6e332e6 | 2511 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2512 | { |
2513 | struct elf_link_hash_entry *weakdef; | |
2514 | ||
f6e332e6 | 2515 | weakdef = h->u.weakdef; |
ec24dc88 | 2516 | while (h->root.type == bfd_link_hash_indirect) |
45d6a902 AM |
2517 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2518 | ||
2519 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2520 | || h->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2521 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2522 | |
2523 | /* If the real definition is defined by a regular object file, | |
2524 | don't do anything special. See the longer description in | |
2525 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2526 | if (weakdef->def_regular) |
f6e332e6 | 2527 | h->u.weakdef = NULL; |
45d6a902 | 2528 | else |
a26587ba RS |
2529 | { |
2530 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2531 | || weakdef->root.type == bfd_link_hash_defweak); | |
2532 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); | |
2533 | } | |
45d6a902 AM |
2534 | } |
2535 | ||
2536 | return TRUE; | |
2537 | } | |
2538 | ||
2539 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2540 | called via elf_link_hash_traverse, and also calls itself | |
2541 | recursively. */ | |
2542 | ||
28caa186 | 2543 | static bfd_boolean |
268b6b39 | 2544 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2545 | { |
a50b1753 | 2546 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 | 2547 | bfd *dynobj; |
9c5bfbb7 | 2548 | const struct elf_backend_data *bed; |
45d6a902 | 2549 | |
0eddce27 | 2550 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2551 | return FALSE; |
2552 | ||
45d6a902 AM |
2553 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2554 | if (h->root.type == bfd_link_hash_indirect) | |
2555 | return TRUE; | |
2556 | ||
2557 | /* Fix the symbol flags. */ | |
2558 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2559 | return FALSE; | |
2560 | ||
2561 | /* If this symbol does not require a PLT entry, and it is not | |
2562 | defined by a dynamic object, or is not referenced by a regular | |
2563 | object, ignore it. We do have to handle a weak defined symbol, | |
2564 | even if no regular object refers to it, if we decided to add it | |
2565 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2566 | about symbols which are defined by one dynamic object and | |
2567 | referenced by another one? */ | |
f5385ebf | 2568 | if (!h->needs_plt |
91e21fb7 | 2569 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2570 | && (h->def_regular |
2571 | || !h->def_dynamic | |
2572 | || (!h->ref_regular | |
f6e332e6 | 2573 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2574 | { |
a6aa5195 | 2575 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2576 | return TRUE; |
2577 | } | |
2578 | ||
2579 | /* If we've already adjusted this symbol, don't do it again. This | |
2580 | can happen via a recursive call. */ | |
f5385ebf | 2581 | if (h->dynamic_adjusted) |
45d6a902 AM |
2582 | return TRUE; |
2583 | ||
2584 | /* Don't look at this symbol again. Note that we must set this | |
2585 | after checking the above conditions, because we may look at a | |
2586 | symbol once, decide not to do anything, and then get called | |
2587 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2588 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2589 | |
2590 | /* If this is a weak definition, and we know a real definition, and | |
2591 | the real symbol is not itself defined by a regular object file, | |
2592 | then get a good value for the real definition. We handle the | |
2593 | real symbol first, for the convenience of the backend routine. | |
2594 | ||
2595 | Note that there is a confusing case here. If the real definition | |
2596 | is defined by a regular object file, we don't get the real symbol | |
2597 | from the dynamic object, but we do get the weak symbol. If the | |
2598 | processor backend uses a COPY reloc, then if some routine in the | |
2599 | dynamic object changes the real symbol, we will not see that | |
2600 | change in the corresponding weak symbol. This is the way other | |
2601 | ELF linkers work as well, and seems to be a result of the shared | |
2602 | library model. | |
2603 | ||
2604 | I will clarify this issue. Most SVR4 shared libraries define the | |
2605 | variable _timezone and define timezone as a weak synonym. The | |
2606 | tzset call changes _timezone. If you write | |
2607 | extern int timezone; | |
2608 | int _timezone = 5; | |
2609 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2610 | you might expect that, since timezone is a synonym for _timezone, | |
2611 | the same number will print both times. However, if the processor | |
2612 | backend uses a COPY reloc, then actually timezone will be copied | |
2613 | into your process image, and, since you define _timezone | |
2614 | yourself, _timezone will not. Thus timezone and _timezone will | |
2615 | wind up at different memory locations. The tzset call will set | |
2616 | _timezone, leaving timezone unchanged. */ | |
2617 | ||
f6e332e6 | 2618 | if (h->u.weakdef != NULL) |
45d6a902 | 2619 | { |
ec24dc88 AM |
2620 | /* If we get to this point, there is an implicit reference to |
2621 | H->U.WEAKDEF by a regular object file via the weak symbol H. */ | |
f6e332e6 | 2622 | h->u.weakdef->ref_regular = 1; |
45d6a902 | 2623 | |
ec24dc88 AM |
2624 | /* Ensure that the backend adjust_dynamic_symbol function sees |
2625 | H->U.WEAKDEF before H by recursively calling ourselves. */ | |
f6e332e6 | 2626 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2627 | return FALSE; |
2628 | } | |
2629 | ||
2630 | /* If a symbol has no type and no size and does not require a PLT | |
2631 | entry, then we are probably about to do the wrong thing here: we | |
2632 | are probably going to create a COPY reloc for an empty object. | |
2633 | This case can arise when a shared object is built with assembly | |
2634 | code, and the assembly code fails to set the symbol type. */ | |
2635 | if (h->size == 0 | |
2636 | && h->type == STT_NOTYPE | |
f5385ebf | 2637 | && !h->needs_plt) |
45d6a902 AM |
2638 | (*_bfd_error_handler) |
2639 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2640 | h->root.root.string); | |
2641 | ||
2642 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2643 | bed = get_elf_backend_data (dynobj); | |
e7c33416 | 2644 | |
45d6a902 AM |
2645 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
2646 | { | |
2647 | eif->failed = TRUE; | |
2648 | return FALSE; | |
2649 | } | |
2650 | ||
2651 | return TRUE; | |
2652 | } | |
2653 | ||
027297b7 L |
2654 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2655 | DYNBSS. */ | |
2656 | ||
2657 | bfd_boolean | |
2658 | _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h, | |
2659 | asection *dynbss) | |
2660 | { | |
91ac5911 | 2661 | unsigned int power_of_two; |
027297b7 L |
2662 | bfd_vma mask; |
2663 | asection *sec = h->root.u.def.section; | |
2664 | ||
2665 | /* The section aligment of definition is the maximum alignment | |
91ac5911 L |
2666 | requirement of symbols defined in the section. Since we don't |
2667 | know the symbol alignment requirement, we start with the | |
2668 | maximum alignment and check low bits of the symbol address | |
2669 | for the minimum alignment. */ | |
2670 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2671 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2672 | while ((h->root.u.def.value & mask) != 0) | |
2673 | { | |
2674 | mask >>= 1; | |
2675 | --power_of_two; | |
2676 | } | |
027297b7 | 2677 | |
91ac5911 L |
2678 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2679 | dynbss)) | |
027297b7 L |
2680 | { |
2681 | /* Adjust the section alignment if needed. */ | |
2682 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2683 | power_of_two)) |
027297b7 L |
2684 | return FALSE; |
2685 | } | |
2686 | ||
91ac5911 | 2687 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2688 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2689 | ||
2690 | /* Define the symbol as being at this point in DYNBSS. */ | |
2691 | h->root.u.def.section = dynbss; | |
2692 | h->root.u.def.value = dynbss->size; | |
2693 | ||
2694 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2695 | dynbss->size += h->size; | |
2696 | ||
2697 | return TRUE; | |
2698 | } | |
2699 | ||
45d6a902 AM |
2700 | /* Adjust all external symbols pointing into SEC_MERGE sections |
2701 | to reflect the object merging within the sections. */ | |
2702 | ||
28caa186 | 2703 | static bfd_boolean |
268b6b39 | 2704 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2705 | { |
2706 | asection *sec; | |
2707 | ||
45d6a902 AM |
2708 | if ((h->root.type == bfd_link_hash_defined |
2709 | || h->root.type == bfd_link_hash_defweak) | |
2710 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2711 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2712 | { | |
a50b1753 | 2713 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
2714 | |
2715 | h->root.u.def.value = | |
2716 | _bfd_merged_section_offset (output_bfd, | |
2717 | &h->root.u.def.section, | |
2718 | elf_section_data (sec)->sec_info, | |
753731ee | 2719 | h->root.u.def.value); |
45d6a902 AM |
2720 | } |
2721 | ||
2722 | return TRUE; | |
2723 | } | |
986a241f RH |
2724 | |
2725 | /* Returns false if the symbol referred to by H should be considered | |
2726 | to resolve local to the current module, and true if it should be | |
2727 | considered to bind dynamically. */ | |
2728 | ||
2729 | bfd_boolean | |
268b6b39 AM |
2730 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2731 | struct bfd_link_info *info, | |
89a2ee5a | 2732 | bfd_boolean not_local_protected) |
986a241f RH |
2733 | { |
2734 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2735 | const struct elf_backend_data *bed; |
2736 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2737 | |
2738 | if (h == NULL) | |
2739 | return FALSE; | |
2740 | ||
2741 | while (h->root.type == bfd_link_hash_indirect | |
2742 | || h->root.type == bfd_link_hash_warning) | |
2743 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2744 | ||
2745 | /* If it was forced local, then clearly it's not dynamic. */ | |
2746 | if (h->dynindx == -1) | |
2747 | return FALSE; | |
f5385ebf | 2748 | if (h->forced_local) |
986a241f RH |
2749 | return FALSE; |
2750 | ||
2751 | /* Identify the cases where name binding rules say that a | |
2752 | visible symbol resolves locally. */ | |
55255dae | 2753 | binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); |
986a241f RH |
2754 | |
2755 | switch (ELF_ST_VISIBILITY (h->other)) | |
2756 | { | |
2757 | case STV_INTERNAL: | |
2758 | case STV_HIDDEN: | |
2759 | return FALSE; | |
2760 | ||
2761 | case STV_PROTECTED: | |
fcb93ecf PB |
2762 | hash_table = elf_hash_table (info); |
2763 | if (!is_elf_hash_table (hash_table)) | |
2764 | return FALSE; | |
2765 | ||
2766 | bed = get_elf_backend_data (hash_table->dynobj); | |
2767 | ||
986a241f RH |
2768 | /* Proper resolution for function pointer equality may require |
2769 | that these symbols perhaps be resolved dynamically, even though | |
2770 | we should be resolving them to the current module. */ | |
89a2ee5a | 2771 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2772 | binding_stays_local_p = TRUE; |
2773 | break; | |
2774 | ||
2775 | default: | |
986a241f RH |
2776 | break; |
2777 | } | |
2778 | ||
aa37626c | 2779 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 2780 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
2781 | return TRUE; |
2782 | ||
986a241f RH |
2783 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2784 | us that it remains local. */ | |
2785 | return !binding_stays_local_p; | |
2786 | } | |
f6c52c13 AM |
2787 | |
2788 | /* Return true if the symbol referred to by H should be considered | |
2789 | to resolve local to the current module, and false otherwise. Differs | |
2790 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 2791 | undefined symbols. The two functions are virtually identical except |
89a2ee5a AM |
2792 | for the place where forced_local and dynindx == -1 are tested. If |
2793 | either of those tests are true, _bfd_elf_dynamic_symbol_p will say | |
2794 | the symbol is local, while _bfd_elf_symbol_refs_local_p will say | |
2795 | the symbol is local only for defined symbols. | |
2796 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as | |
2797 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
2798 | treatment of undefined weak symbols. For those that do not make | |
2799 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
2800 | |
2801 | bfd_boolean | |
268b6b39 AM |
2802 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2803 | struct bfd_link_info *info, | |
2804 | bfd_boolean local_protected) | |
f6c52c13 | 2805 | { |
fcb93ecf PB |
2806 | const struct elf_backend_data *bed; |
2807 | struct elf_link_hash_table *hash_table; | |
2808 | ||
f6c52c13 AM |
2809 | /* If it's a local sym, of course we resolve locally. */ |
2810 | if (h == NULL) | |
2811 | return TRUE; | |
2812 | ||
d95edcac L |
2813 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
2814 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
2815 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
2816 | return TRUE; | |
2817 | ||
7e2294f9 AO |
2818 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2819 | flag set, so test it first, and don't bail out. */ | |
2820 | if (ELF_COMMON_DEF_P (h)) | |
2821 | /* Do nothing. */; | |
f6c52c13 | 2822 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2823 | resolve locally. The sym is either undefined or dynamic. */ |
2824 | else if (!h->def_regular) | |
f6c52c13 AM |
2825 | return FALSE; |
2826 | ||
2827 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2828 | if (h->forced_local) |
f6c52c13 AM |
2829 | return TRUE; |
2830 | ||
2831 | /* As do non-dynamic symbols. */ | |
2832 | if (h->dynindx == -1) | |
2833 | return TRUE; | |
2834 | ||
2835 | /* At this point, we know the symbol is defined and dynamic. In an | |
2836 | executable it must resolve locally, likewise when building symbolic | |
2837 | shared libraries. */ | |
55255dae | 2838 | if (info->executable || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
2839 | return TRUE; |
2840 | ||
2841 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2842 | with default visibility might not resolve locally. */ | |
2843 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2844 | return FALSE; | |
2845 | ||
fcb93ecf PB |
2846 | hash_table = elf_hash_table (info); |
2847 | if (!is_elf_hash_table (hash_table)) | |
2848 | return TRUE; | |
2849 | ||
2850 | bed = get_elf_backend_data (hash_table->dynobj); | |
2851 | ||
1c16dfa5 | 2852 | /* STV_PROTECTED non-function symbols are local. */ |
fcb93ecf | 2853 | if (!bed->is_function_type (h->type)) |
1c16dfa5 L |
2854 | return TRUE; |
2855 | ||
f6c52c13 | 2856 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
2857 | symbols be treated as dynamic symbols. If the address of a |
2858 | function not defined in an executable is set to that function's | |
2859 | plt entry in the executable, then the address of the function in | |
2860 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
2861 | return local_protected; |
2862 | } | |
e1918d23 AM |
2863 | |
2864 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2865 | aligned. Returns the first TLS output section. */ | |
2866 | ||
2867 | struct bfd_section * | |
2868 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2869 | { | |
2870 | struct bfd_section *sec, *tls; | |
2871 | unsigned int align = 0; | |
2872 | ||
2873 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2874 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2875 | break; | |
2876 | tls = sec; | |
2877 | ||
2878 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2879 | if (sec->alignment_power > align) | |
2880 | align = sec->alignment_power; | |
2881 | ||
2882 | elf_hash_table (info)->tls_sec = tls; | |
2883 | ||
2884 | /* Ensure the alignment of the first section is the largest alignment, | |
2885 | so that the tls segment starts aligned. */ | |
2886 | if (tls != NULL) | |
2887 | tls->alignment_power = align; | |
2888 | ||
2889 | return tls; | |
2890 | } | |
0ad989f9 L |
2891 | |
2892 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2893 | static bfd_boolean | |
2894 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2895 | Elf_Internal_Sym *sym) | |
2896 | { | |
a4d8e49b L |
2897 | const struct elf_backend_data *bed; |
2898 | ||
0ad989f9 L |
2899 | /* Local symbols do not count, but target specific ones might. */ |
2900 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2901 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2902 | return FALSE; | |
2903 | ||
fcb93ecf | 2904 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 2905 | /* Function symbols do not count. */ |
fcb93ecf | 2906 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
2907 | return FALSE; |
2908 | ||
2909 | /* If the section is undefined, then so is the symbol. */ | |
2910 | if (sym->st_shndx == SHN_UNDEF) | |
2911 | return FALSE; | |
2912 | ||
2913 | /* If the symbol is defined in the common section, then | |
2914 | it is a common definition and so does not count. */ | |
a4d8e49b | 2915 | if (bed->common_definition (sym)) |
0ad989f9 L |
2916 | return FALSE; |
2917 | ||
2918 | /* If the symbol is in a target specific section then we | |
2919 | must rely upon the backend to tell us what it is. */ | |
2920 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2921 | /* FIXME - this function is not coded yet: | |
2922 | ||
2923 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2924 | ||
2925 | Instead for now assume that the definition is not global, | |
2926 | Even if this is wrong, at least the linker will behave | |
2927 | in the same way that it used to do. */ | |
2928 | return FALSE; | |
2929 | ||
2930 | return TRUE; | |
2931 | } | |
2932 | ||
2933 | /* Search the symbol table of the archive element of the archive ABFD | |
2934 | whose archive map contains a mention of SYMDEF, and determine if | |
2935 | the symbol is defined in this element. */ | |
2936 | static bfd_boolean | |
2937 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2938 | { | |
2939 | Elf_Internal_Shdr * hdr; | |
2940 | bfd_size_type symcount; | |
2941 | bfd_size_type extsymcount; | |
2942 | bfd_size_type extsymoff; | |
2943 | Elf_Internal_Sym *isymbuf; | |
2944 | Elf_Internal_Sym *isym; | |
2945 | Elf_Internal_Sym *isymend; | |
2946 | bfd_boolean result; | |
2947 | ||
2948 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2949 | if (abfd == NULL) | |
2950 | return FALSE; | |
2951 | ||
2952 | if (! bfd_check_format (abfd, bfd_object)) | |
2953 | return FALSE; | |
2954 | ||
2955 | /* If we have already included the element containing this symbol in the | |
2956 | link then we do not need to include it again. Just claim that any symbol | |
2957 | it contains is not a definition, so that our caller will not decide to | |
2958 | (re)include this element. */ | |
2959 | if (abfd->archive_pass) | |
2960 | return FALSE; | |
2961 | ||
2962 | /* Select the appropriate symbol table. */ | |
2963 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2964 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2965 | else | |
2966 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2967 | ||
2968 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2969 | ||
2970 | /* The sh_info field of the symtab header tells us where the | |
2971 | external symbols start. We don't care about the local symbols. */ | |
2972 | if (elf_bad_symtab (abfd)) | |
2973 | { | |
2974 | extsymcount = symcount; | |
2975 | extsymoff = 0; | |
2976 | } | |
2977 | else | |
2978 | { | |
2979 | extsymcount = symcount - hdr->sh_info; | |
2980 | extsymoff = hdr->sh_info; | |
2981 | } | |
2982 | ||
2983 | if (extsymcount == 0) | |
2984 | return FALSE; | |
2985 | ||
2986 | /* Read in the symbol table. */ | |
2987 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2988 | NULL, NULL, NULL); | |
2989 | if (isymbuf == NULL) | |
2990 | return FALSE; | |
2991 | ||
2992 | /* Scan the symbol table looking for SYMDEF. */ | |
2993 | result = FALSE; | |
2994 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2995 | { | |
2996 | const char *name; | |
2997 | ||
2998 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2999 | isym->st_name); | |
3000 | if (name == NULL) | |
3001 | break; | |
3002 | ||
3003 | if (strcmp (name, symdef->name) == 0) | |
3004 | { | |
3005 | result = is_global_data_symbol_definition (abfd, isym); | |
3006 | break; | |
3007 | } | |
3008 | } | |
3009 | ||
3010 | free (isymbuf); | |
3011 | ||
3012 | return result; | |
3013 | } | |
3014 | \f | |
5a580b3a AM |
3015 | /* Add an entry to the .dynamic table. */ |
3016 | ||
3017 | bfd_boolean | |
3018 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3019 | bfd_vma tag, | |
3020 | bfd_vma val) | |
3021 | { | |
3022 | struct elf_link_hash_table *hash_table; | |
3023 | const struct elf_backend_data *bed; | |
3024 | asection *s; | |
3025 | bfd_size_type newsize; | |
3026 | bfd_byte *newcontents; | |
3027 | Elf_Internal_Dyn dyn; | |
3028 | ||
3029 | hash_table = elf_hash_table (info); | |
3030 | if (! is_elf_hash_table (hash_table)) | |
3031 | return FALSE; | |
3032 | ||
3033 | bed = get_elf_backend_data (hash_table->dynobj); | |
3034 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
3035 | BFD_ASSERT (s != NULL); | |
3036 | ||
eea6121a | 3037 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3038 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3039 | if (newcontents == NULL) |
3040 | return FALSE; | |
3041 | ||
3042 | dyn.d_tag = tag; | |
3043 | dyn.d_un.d_val = val; | |
eea6121a | 3044 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3045 | |
eea6121a | 3046 | s->size = newsize; |
5a580b3a AM |
3047 | s->contents = newcontents; |
3048 | ||
3049 | return TRUE; | |
3050 | } | |
3051 | ||
3052 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3053 | otherwise just check whether one already exists. Returns -1 on error, | |
3054 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3055 | ||
4ad4eba5 | 3056 | static int |
7e9f0867 AM |
3057 | elf_add_dt_needed_tag (bfd *abfd, |
3058 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3059 | const char *soname, |
3060 | bfd_boolean do_it) | |
5a580b3a AM |
3061 | { |
3062 | struct elf_link_hash_table *hash_table; | |
3063 | bfd_size_type oldsize; | |
3064 | bfd_size_type strindex; | |
3065 | ||
7e9f0867 AM |
3066 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3067 | return -1; | |
3068 | ||
5a580b3a AM |
3069 | hash_table = elf_hash_table (info); |
3070 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
3071 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
3072 | if (strindex == (bfd_size_type) -1) | |
3073 | return -1; | |
3074 | ||
3075 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
3076 | { | |
3077 | asection *sdyn; | |
3078 | const struct elf_backend_data *bed; | |
3079 | bfd_byte *extdyn; | |
3080 | ||
3081 | bed = get_elf_backend_data (hash_table->dynobj); | |
3082 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
7e9f0867 AM |
3083 | if (sdyn != NULL) |
3084 | for (extdyn = sdyn->contents; | |
3085 | extdyn < sdyn->contents + sdyn->size; | |
3086 | extdyn += bed->s->sizeof_dyn) | |
3087 | { | |
3088 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3089 | |
7e9f0867 AM |
3090 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3091 | if (dyn.d_tag == DT_NEEDED | |
3092 | && dyn.d_un.d_val == strindex) | |
3093 | { | |
3094 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3095 | return 1; | |
3096 | } | |
3097 | } | |
5a580b3a AM |
3098 | } |
3099 | ||
3100 | if (do_it) | |
3101 | { | |
7e9f0867 AM |
3102 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3103 | return -1; | |
3104 | ||
5a580b3a AM |
3105 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3106 | return -1; | |
3107 | } | |
3108 | else | |
3109 | /* We were just checking for existence of the tag. */ | |
3110 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3111 | ||
3112 | return 0; | |
3113 | } | |
3114 | ||
010e5ae2 AM |
3115 | static bfd_boolean |
3116 | on_needed_list (const char *soname, struct bfd_link_needed_list *needed) | |
3117 | { | |
3118 | for (; needed != NULL; needed = needed->next) | |
3119 | if (strcmp (soname, needed->name) == 0) | |
3120 | return TRUE; | |
3121 | ||
3122 | return FALSE; | |
3123 | } | |
3124 | ||
5a580b3a | 3125 | /* Sort symbol by value and section. */ |
4ad4eba5 AM |
3126 | static int |
3127 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3128 | { |
3129 | const struct elf_link_hash_entry *h1; | |
3130 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3131 | bfd_signed_vma vdiff; |
5a580b3a AM |
3132 | |
3133 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3134 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3135 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3136 | if (vdiff != 0) | |
3137 | return vdiff > 0 ? 1 : -1; | |
3138 | else | |
3139 | { | |
3140 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3141 | if (sdiff != 0) | |
3142 | return sdiff > 0 ? 1 : -1; | |
3143 | } | |
5a580b3a AM |
3144 | return 0; |
3145 | } | |
4ad4eba5 | 3146 | |
5a580b3a AM |
3147 | /* This function is used to adjust offsets into .dynstr for |
3148 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3149 | ||
3150 | static bfd_boolean | |
3151 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3152 | { | |
a50b1753 | 3153 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3154 | |
5a580b3a AM |
3155 | if (h->dynindx != -1) |
3156 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3157 | return TRUE; | |
3158 | } | |
3159 | ||
3160 | /* Assign string offsets in .dynstr, update all structures referencing | |
3161 | them. */ | |
3162 | ||
4ad4eba5 AM |
3163 | static bfd_boolean |
3164 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3165 | { |
3166 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3167 | struct elf_link_local_dynamic_entry *entry; | |
3168 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3169 | bfd *dynobj = hash_table->dynobj; | |
3170 | asection *sdyn; | |
3171 | bfd_size_type size; | |
3172 | const struct elf_backend_data *bed; | |
3173 | bfd_byte *extdyn; | |
3174 | ||
3175 | _bfd_elf_strtab_finalize (dynstr); | |
3176 | size = _bfd_elf_strtab_size (dynstr); | |
3177 | ||
3178 | bed = get_elf_backend_data (dynobj); | |
3179 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3180 | BFD_ASSERT (sdyn != NULL); | |
3181 | ||
3182 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3183 | for (extdyn = sdyn->contents; | |
eea6121a | 3184 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3185 | extdyn += bed->s->sizeof_dyn) |
3186 | { | |
3187 | Elf_Internal_Dyn dyn; | |
3188 | ||
3189 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3190 | switch (dyn.d_tag) | |
3191 | { | |
3192 | case DT_STRSZ: | |
3193 | dyn.d_un.d_val = size; | |
3194 | break; | |
3195 | case DT_NEEDED: | |
3196 | case DT_SONAME: | |
3197 | case DT_RPATH: | |
3198 | case DT_RUNPATH: | |
3199 | case DT_FILTER: | |
3200 | case DT_AUXILIARY: | |
7ee314fa AM |
3201 | case DT_AUDIT: |
3202 | case DT_DEPAUDIT: | |
5a580b3a AM |
3203 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3204 | break; | |
3205 | default: | |
3206 | continue; | |
3207 | } | |
3208 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3209 | } | |
3210 | ||
3211 | /* Now update local dynamic symbols. */ | |
3212 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3213 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3214 | entry->isym.st_name); | |
3215 | ||
3216 | /* And the rest of dynamic symbols. */ | |
3217 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3218 | ||
3219 | /* Adjust version definitions. */ | |
3220 | if (elf_tdata (output_bfd)->cverdefs) | |
3221 | { | |
3222 | asection *s; | |
3223 | bfd_byte *p; | |
3224 | bfd_size_type i; | |
3225 | Elf_Internal_Verdef def; | |
3226 | Elf_Internal_Verdaux defaux; | |
3227 | ||
3228 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3229 | p = s->contents; | |
3230 | do | |
3231 | { | |
3232 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3233 | &def); | |
3234 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3235 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3236 | continue; | |
5a580b3a AM |
3237 | for (i = 0; i < def.vd_cnt; ++i) |
3238 | { | |
3239 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3240 | (Elf_External_Verdaux *) p, &defaux); | |
3241 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3242 | defaux.vda_name); | |
3243 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3244 | &defaux, (Elf_External_Verdaux *) p); | |
3245 | p += sizeof (Elf_External_Verdaux); | |
3246 | } | |
3247 | } | |
3248 | while (def.vd_next); | |
3249 | } | |
3250 | ||
3251 | /* Adjust version references. */ | |
3252 | if (elf_tdata (output_bfd)->verref) | |
3253 | { | |
3254 | asection *s; | |
3255 | bfd_byte *p; | |
3256 | bfd_size_type i; | |
3257 | Elf_Internal_Verneed need; | |
3258 | Elf_Internal_Vernaux needaux; | |
3259 | ||
3260 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3261 | p = s->contents; | |
3262 | do | |
3263 | { | |
3264 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3265 | &need); | |
3266 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3267 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3268 | (Elf_External_Verneed *) p); | |
3269 | p += sizeof (Elf_External_Verneed); | |
3270 | for (i = 0; i < need.vn_cnt; ++i) | |
3271 | { | |
3272 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3273 | (Elf_External_Vernaux *) p, &needaux); | |
3274 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3275 | needaux.vna_name); | |
3276 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3277 | &needaux, | |
3278 | (Elf_External_Vernaux *) p); | |
3279 | p += sizeof (Elf_External_Vernaux); | |
3280 | } | |
3281 | } | |
3282 | while (need.vn_next); | |
3283 | } | |
3284 | ||
3285 | return TRUE; | |
3286 | } | |
3287 | \f | |
13285a1b AM |
3288 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3289 | The default is to only match when the INPUT and OUTPUT are exactly | |
3290 | the same target. */ | |
3291 | ||
3292 | bfd_boolean | |
3293 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3294 | const bfd_target *output) | |
3295 | { | |
3296 | return input == output; | |
3297 | } | |
3298 | ||
3299 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3300 | This version is used when different targets for the same architecture | |
3301 | are virtually identical. */ | |
3302 | ||
3303 | bfd_boolean | |
3304 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3305 | const bfd_target *output) | |
3306 | { | |
3307 | const struct elf_backend_data *obed, *ibed; | |
3308 | ||
3309 | if (input == output) | |
3310 | return TRUE; | |
3311 | ||
3312 | ibed = xvec_get_elf_backend_data (input); | |
3313 | obed = xvec_get_elf_backend_data (output); | |
3314 | ||
3315 | if (ibed->arch != obed->arch) | |
3316 | return FALSE; | |
3317 | ||
3318 | /* If both backends are using this function, deem them compatible. */ | |
3319 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3320 | } | |
3321 | ||
4ad4eba5 AM |
3322 | /* Add symbols from an ELF object file to the linker hash table. */ |
3323 | ||
3324 | static bfd_boolean | |
3325 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3326 | { | |
a0c402a5 | 3327 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 AM |
3328 | Elf_Internal_Shdr *hdr; |
3329 | bfd_size_type symcount; | |
3330 | bfd_size_type extsymcount; | |
3331 | bfd_size_type extsymoff; | |
3332 | struct elf_link_hash_entry **sym_hash; | |
3333 | bfd_boolean dynamic; | |
3334 | Elf_External_Versym *extversym = NULL; | |
3335 | Elf_External_Versym *ever; | |
3336 | struct elf_link_hash_entry *weaks; | |
3337 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3338 | bfd_size_type nondeflt_vers_cnt = 0; | |
3339 | Elf_Internal_Sym *isymbuf = NULL; | |
3340 | Elf_Internal_Sym *isym; | |
3341 | Elf_Internal_Sym *isymend; | |
3342 | const struct elf_backend_data *bed; | |
3343 | bfd_boolean add_needed; | |
66eb6687 | 3344 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3345 | bfd_size_type amt; |
66eb6687 | 3346 | void *alloc_mark = NULL; |
4f87808c AM |
3347 | struct bfd_hash_entry **old_table = NULL; |
3348 | unsigned int old_size = 0; | |
3349 | unsigned int old_count = 0; | |
66eb6687 AM |
3350 | void *old_tab = NULL; |
3351 | void *old_hash; | |
3352 | void *old_ent; | |
3353 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3354 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
3355 | long old_dynsymcount = 0; | |
3356 | size_t tabsize = 0; | |
3357 | size_t hashsize = 0; | |
4ad4eba5 | 3358 | |
66eb6687 | 3359 | htab = elf_hash_table (info); |
4ad4eba5 | 3360 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3361 | |
3362 | if ((abfd->flags & DYNAMIC) == 0) | |
3363 | dynamic = FALSE; | |
3364 | else | |
3365 | { | |
3366 | dynamic = TRUE; | |
3367 | ||
3368 | /* You can't use -r against a dynamic object. Also, there's no | |
3369 | hope of using a dynamic object which does not exactly match | |
3370 | the format of the output file. */ | |
3371 | if (info->relocatable | |
66eb6687 | 3372 | || !is_elf_hash_table (htab) |
f13a99db | 3373 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3374 | { |
9a0789ec NC |
3375 | if (info->relocatable) |
3376 | bfd_set_error (bfd_error_invalid_operation); | |
3377 | else | |
3378 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3379 | goto error_return; |
3380 | } | |
3381 | } | |
3382 | ||
a0c402a5 L |
3383 | ehdr = elf_elfheader (abfd); |
3384 | if (info->warn_alternate_em | |
3385 | && bed->elf_machine_code != ehdr->e_machine | |
3386 | && ((bed->elf_machine_alt1 != 0 | |
3387 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3388 | || (bed->elf_machine_alt2 != 0 | |
3389 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
3390 | info->callbacks->einfo | |
3391 | (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), | |
3392 | ehdr->e_machine, abfd, bed->elf_machine_code); | |
3393 | ||
4ad4eba5 AM |
3394 | /* As a GNU extension, any input sections which are named |
3395 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3396 | symbol. This differs from .gnu.warning sections, which generate | |
3397 | warnings when they are included in an output file. */ | |
dd98f8d2 NC |
3398 | /* PR 12761: Also generate this warning when building shared libraries. */ |
3399 | if (info->executable || info->shared) | |
4ad4eba5 AM |
3400 | { |
3401 | asection *s; | |
3402 | ||
3403 | for (s = abfd->sections; s != NULL; s = s->next) | |
3404 | { | |
3405 | const char *name; | |
3406 | ||
3407 | name = bfd_get_section_name (abfd, s); | |
0112cd26 | 3408 | if (CONST_STRNEQ (name, ".gnu.warning.")) |
4ad4eba5 AM |
3409 | { |
3410 | char *msg; | |
3411 | bfd_size_type sz; | |
4ad4eba5 AM |
3412 | |
3413 | name += sizeof ".gnu.warning." - 1; | |
3414 | ||
3415 | /* If this is a shared object, then look up the symbol | |
3416 | in the hash table. If it is there, and it is already | |
3417 | been defined, then we will not be using the entry | |
3418 | from this shared object, so we don't need to warn. | |
3419 | FIXME: If we see the definition in a regular object | |
3420 | later on, we will warn, but we shouldn't. The only | |
3421 | fix is to keep track of what warnings we are supposed | |
3422 | to emit, and then handle them all at the end of the | |
3423 | link. */ | |
3424 | if (dynamic) | |
3425 | { | |
3426 | struct elf_link_hash_entry *h; | |
3427 | ||
66eb6687 | 3428 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
4ad4eba5 AM |
3429 | |
3430 | /* FIXME: What about bfd_link_hash_common? */ | |
3431 | if (h != NULL | |
3432 | && (h->root.type == bfd_link_hash_defined | |
3433 | || h->root.type == bfd_link_hash_defweak)) | |
3434 | { | |
3435 | /* We don't want to issue this warning. Clobber | |
3436 | the section size so that the warning does not | |
3437 | get copied into the output file. */ | |
eea6121a | 3438 | s->size = 0; |
4ad4eba5 AM |
3439 | continue; |
3440 | } | |
3441 | } | |
3442 | ||
eea6121a | 3443 | sz = s->size; |
a50b1753 | 3444 | msg = (char *) bfd_alloc (abfd, sz + 1); |
4ad4eba5 AM |
3445 | if (msg == NULL) |
3446 | goto error_return; | |
3447 | ||
370a0e1b | 3448 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4ad4eba5 AM |
3449 | goto error_return; |
3450 | ||
370a0e1b | 3451 | msg[sz] = '\0'; |
4ad4eba5 AM |
3452 | |
3453 | if (! (_bfd_generic_link_add_one_symbol | |
3454 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
66eb6687 | 3455 | FALSE, bed->collect, NULL))) |
4ad4eba5 AM |
3456 | goto error_return; |
3457 | ||
3458 | if (! info->relocatable) | |
3459 | { | |
3460 | /* Clobber the section size so that the warning does | |
3461 | not get copied into the output file. */ | |
eea6121a | 3462 | s->size = 0; |
11d2f718 AM |
3463 | |
3464 | /* Also set SEC_EXCLUDE, so that symbols defined in | |
3465 | the warning section don't get copied to the output. */ | |
3466 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3467 | } |
3468 | } | |
3469 | } | |
3470 | } | |
3471 | ||
3472 | add_needed = TRUE; | |
3473 | if (! dynamic) | |
3474 | { | |
3475 | /* If we are creating a shared library, create all the dynamic | |
3476 | sections immediately. We need to attach them to something, | |
3477 | so we attach them to this BFD, provided it is the right | |
3478 | format. FIXME: If there are no input BFD's of the same | |
3479 | format as the output, we can't make a shared library. */ | |
3480 | if (info->shared | |
66eb6687 | 3481 | && is_elf_hash_table (htab) |
f13a99db | 3482 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3483 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3484 | { |
3485 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3486 | goto error_return; | |
3487 | } | |
3488 | } | |
66eb6687 | 3489 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3490 | goto error_return; |
3491 | else | |
3492 | { | |
3493 | asection *s; | |
3494 | const char *soname = NULL; | |
7ee314fa | 3495 | char *audit = NULL; |
4ad4eba5 AM |
3496 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
3497 | int ret; | |
3498 | ||
3499 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3500 | ld shouldn't allow it. */ |
4ad4eba5 AM |
3501 | if ((s = abfd->sections) != NULL |
3502 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
92fd189d | 3503 | abort (); |
4ad4eba5 AM |
3504 | |
3505 | /* If this dynamic lib was specified on the command line with | |
3506 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3507 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3508 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3509 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3510 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3511 | all. */ | |
3512 | add_needed = (elf_dyn_lib_class (abfd) | |
3513 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3514 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3515 | |
3516 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3517 | if (s != NULL) | |
3518 | { | |
3519 | bfd_byte *dynbuf; | |
3520 | bfd_byte *extdyn; | |
cb33740c | 3521 | unsigned int elfsec; |
4ad4eba5 AM |
3522 | unsigned long shlink; |
3523 | ||
eea6121a | 3524 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
3525 | { |
3526 | error_free_dyn: | |
3527 | free (dynbuf); | |
3528 | goto error_return; | |
3529 | } | |
4ad4eba5 AM |
3530 | |
3531 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3532 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3533 | goto error_free_dyn; |
3534 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3535 | ||
3536 | for (extdyn = dynbuf; | |
eea6121a | 3537 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3538 | extdyn += bed->s->sizeof_dyn) |
3539 | { | |
3540 | Elf_Internal_Dyn dyn; | |
3541 | ||
3542 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3543 | if (dyn.d_tag == DT_SONAME) | |
3544 | { | |
3545 | unsigned int tagv = dyn.d_un.d_val; | |
3546 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3547 | if (soname == NULL) | |
3548 | goto error_free_dyn; | |
3549 | } | |
3550 | if (dyn.d_tag == DT_NEEDED) | |
3551 | { | |
3552 | struct bfd_link_needed_list *n, **pn; | |
3553 | char *fnm, *anm; | |
3554 | unsigned int tagv = dyn.d_un.d_val; | |
3555 | ||
3556 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3557 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3558 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3559 | if (n == NULL || fnm == NULL) | |
3560 | goto error_free_dyn; | |
3561 | amt = strlen (fnm) + 1; | |
a50b1753 | 3562 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3563 | if (anm == NULL) |
3564 | goto error_free_dyn; | |
3565 | memcpy (anm, fnm, amt); | |
3566 | n->name = anm; | |
3567 | n->by = abfd; | |
3568 | n->next = NULL; | |
66eb6687 | 3569 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3570 | ; |
3571 | *pn = n; | |
3572 | } | |
3573 | if (dyn.d_tag == DT_RUNPATH) | |
3574 | { | |
3575 | struct bfd_link_needed_list *n, **pn; | |
3576 | char *fnm, *anm; | |
3577 | unsigned int tagv = dyn.d_un.d_val; | |
3578 | ||
3579 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3580 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3581 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3582 | if (n == NULL || fnm == NULL) | |
3583 | goto error_free_dyn; | |
3584 | amt = strlen (fnm) + 1; | |
a50b1753 | 3585 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3586 | if (anm == NULL) |
3587 | goto error_free_dyn; | |
3588 | memcpy (anm, fnm, amt); | |
3589 | n->name = anm; | |
3590 | n->by = abfd; | |
3591 | n->next = NULL; | |
3592 | for (pn = & runpath; | |
3593 | *pn != NULL; | |
3594 | pn = &(*pn)->next) | |
3595 | ; | |
3596 | *pn = n; | |
3597 | } | |
3598 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3599 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3600 | { | |
3601 | struct bfd_link_needed_list *n, **pn; | |
3602 | char *fnm, *anm; | |
3603 | unsigned int tagv = dyn.d_un.d_val; | |
3604 | ||
3605 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3606 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3607 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3608 | if (n == NULL || fnm == NULL) | |
3609 | goto error_free_dyn; | |
3610 | amt = strlen (fnm) + 1; | |
a50b1753 | 3611 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 3612 | if (anm == NULL) |
f8703194 | 3613 | goto error_free_dyn; |
4ad4eba5 AM |
3614 | memcpy (anm, fnm, amt); |
3615 | n->name = anm; | |
3616 | n->by = abfd; | |
3617 | n->next = NULL; | |
3618 | for (pn = & rpath; | |
3619 | *pn != NULL; | |
3620 | pn = &(*pn)->next) | |
3621 | ; | |
3622 | *pn = n; | |
3623 | } | |
7ee314fa AM |
3624 | if (dyn.d_tag == DT_AUDIT) |
3625 | { | |
3626 | unsigned int tagv = dyn.d_un.d_val; | |
3627 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3628 | } | |
4ad4eba5 AM |
3629 | } |
3630 | ||
3631 | free (dynbuf); | |
3632 | } | |
3633 | ||
3634 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3635 | frees all more recently bfd_alloc'd blocks as well. */ | |
3636 | if (runpath) | |
3637 | rpath = runpath; | |
3638 | ||
3639 | if (rpath) | |
3640 | { | |
3641 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3642 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3643 | ; |
3644 | *pn = rpath; | |
3645 | } | |
3646 | ||
3647 | /* We do not want to include any of the sections in a dynamic | |
3648 | object in the output file. We hack by simply clobbering the | |
3649 | list of sections in the BFD. This could be handled more | |
3650 | cleanly by, say, a new section flag; the existing | |
3651 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3652 | still implies that the section takes up space in the output | |
3653 | file. */ | |
3654 | bfd_section_list_clear (abfd); | |
3655 | ||
4ad4eba5 AM |
3656 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3657 | object. If the object has a DT_SONAME entry, we use it. | |
3658 | Otherwise, if the generic linker stuck something in | |
3659 | elf_dt_name, we use that. Otherwise, we just use the file | |
3660 | name. */ | |
3661 | if (soname == NULL || *soname == '\0') | |
3662 | { | |
3663 | soname = elf_dt_name (abfd); | |
3664 | if (soname == NULL || *soname == '\0') | |
3665 | soname = bfd_get_filename (abfd); | |
3666 | } | |
3667 | ||
3668 | /* Save the SONAME because sometimes the linker emulation code | |
3669 | will need to know it. */ | |
3670 | elf_dt_name (abfd) = soname; | |
3671 | ||
7e9f0867 | 3672 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3673 | if (ret < 0) |
3674 | goto error_return; | |
3675 | ||
3676 | /* If we have already included this dynamic object in the | |
3677 | link, just ignore it. There is no reason to include a | |
3678 | particular dynamic object more than once. */ | |
3679 | if (ret > 0) | |
3680 | return TRUE; | |
7ee314fa AM |
3681 | |
3682 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
3683 | elf_dt_audit (abfd) = audit; | |
4ad4eba5 AM |
3684 | } |
3685 | ||
3686 | /* If this is a dynamic object, we always link against the .dynsym | |
3687 | symbol table, not the .symtab symbol table. The dynamic linker | |
3688 | will only see the .dynsym symbol table, so there is no reason to | |
3689 | look at .symtab for a dynamic object. */ | |
3690 | ||
3691 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3692 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3693 | else | |
3694 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3695 | ||
3696 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3697 | ||
3698 | /* The sh_info field of the symtab header tells us where the | |
3699 | external symbols start. We don't care about the local symbols at | |
3700 | this point. */ | |
3701 | if (elf_bad_symtab (abfd)) | |
3702 | { | |
3703 | extsymcount = symcount; | |
3704 | extsymoff = 0; | |
3705 | } | |
3706 | else | |
3707 | { | |
3708 | extsymcount = symcount - hdr->sh_info; | |
3709 | extsymoff = hdr->sh_info; | |
3710 | } | |
3711 | ||
3712 | sym_hash = NULL; | |
3713 | if (extsymcount != 0) | |
3714 | { | |
3715 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3716 | NULL, NULL, NULL); | |
3717 | if (isymbuf == NULL) | |
3718 | goto error_return; | |
3719 | ||
3720 | /* We store a pointer to the hash table entry for each external | |
3721 | symbol. */ | |
3722 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 3723 | sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3724 | if (sym_hash == NULL) |
3725 | goto error_free_sym; | |
3726 | elf_sym_hashes (abfd) = sym_hash; | |
3727 | } | |
3728 | ||
3729 | if (dynamic) | |
3730 | { | |
3731 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3732 | if (!_bfd_elf_slurp_version_tables (abfd, |
3733 | info->default_imported_symver)) | |
4ad4eba5 AM |
3734 | goto error_free_sym; |
3735 | ||
3736 | /* Read in the symbol versions, but don't bother to convert them | |
3737 | to internal format. */ | |
3738 | if (elf_dynversym (abfd) != 0) | |
3739 | { | |
3740 | Elf_Internal_Shdr *versymhdr; | |
3741 | ||
3742 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 3743 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
3744 | if (extversym == NULL) |
3745 | goto error_free_sym; | |
3746 | amt = versymhdr->sh_size; | |
3747 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3748 | || bfd_bread (extversym, amt, abfd) != amt) | |
3749 | goto error_free_vers; | |
3750 | } | |
3751 | } | |
3752 | ||
66eb6687 AM |
3753 | /* If we are loading an as-needed shared lib, save the symbol table |
3754 | state before we start adding symbols. If the lib turns out | |
3755 | to be unneeded, restore the state. */ | |
3756 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
3757 | { | |
3758 | unsigned int i; | |
3759 | size_t entsize; | |
3760 | ||
3761 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
3762 | { | |
3763 | struct bfd_hash_entry *p; | |
2de92251 | 3764 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3765 | |
3766 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
3767 | { |
3768 | h = (struct elf_link_hash_entry *) p; | |
3769 | entsize += htab->root.table.entsize; | |
3770 | if (h->root.type == bfd_link_hash_warning) | |
3771 | entsize += htab->root.table.entsize; | |
3772 | } | |
66eb6687 AM |
3773 | } |
3774 | ||
3775 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
3776 | hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3777 | old_tab = bfd_malloc (tabsize + entsize + hashsize); | |
3778 | if (old_tab == NULL) | |
3779 | goto error_free_vers; | |
3780 | ||
3781 | /* Remember the current objalloc pointer, so that all mem for | |
3782 | symbols added can later be reclaimed. */ | |
3783 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
3784 | if (alloc_mark == NULL) | |
3785 | goto error_free_vers; | |
3786 | ||
5061a885 AM |
3787 | /* Make a special call to the linker "notice" function to |
3788 | tell it that we are about to handle an as-needed lib. */ | |
3789 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
16d96b5b | 3790 | notice_as_needed, 0, NULL)) |
9af2a943 | 3791 | goto error_free_vers; |
5061a885 | 3792 | |
66eb6687 AM |
3793 | /* Clone the symbol table and sym hashes. Remember some |
3794 | pointers into the symbol table, and dynamic symbol count. */ | |
3795 | old_hash = (char *) old_tab + tabsize; | |
3796 | old_ent = (char *) old_hash + hashsize; | |
3797 | memcpy (old_tab, htab->root.table.table, tabsize); | |
3798 | memcpy (old_hash, sym_hash, hashsize); | |
3799 | old_undefs = htab->root.undefs; | |
3800 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
3801 | old_table = htab->root.table.table; |
3802 | old_size = htab->root.table.size; | |
3803 | old_count = htab->root.table.count; | |
66eb6687 AM |
3804 | old_dynsymcount = htab->dynsymcount; |
3805 | ||
3806 | for (i = 0; i < htab->root.table.size; i++) | |
3807 | { | |
3808 | struct bfd_hash_entry *p; | |
2de92251 | 3809 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3810 | |
3811 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
3812 | { | |
3813 | memcpy (old_ent, p, htab->root.table.entsize); | |
3814 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
3815 | h = (struct elf_link_hash_entry *) p; |
3816 | if (h->root.type == bfd_link_hash_warning) | |
3817 | { | |
3818 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
3819 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
3820 | } | |
66eb6687 AM |
3821 | } |
3822 | } | |
3823 | } | |
4ad4eba5 | 3824 | |
66eb6687 | 3825 | weaks = NULL; |
4ad4eba5 AM |
3826 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
3827 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3828 | isym < isymend; | |
3829 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3830 | { | |
3831 | int bind; | |
3832 | bfd_vma value; | |
af44c138 | 3833 | asection *sec, *new_sec; |
4ad4eba5 AM |
3834 | flagword flags; |
3835 | const char *name; | |
3836 | struct elf_link_hash_entry *h; | |
3837 | bfd_boolean definition; | |
3838 | bfd_boolean size_change_ok; | |
3839 | bfd_boolean type_change_ok; | |
3840 | bfd_boolean new_weakdef; | |
3841 | bfd_boolean override; | |
a4d8e49b | 3842 | bfd_boolean common; |
4ad4eba5 AM |
3843 | unsigned int old_alignment; |
3844 | bfd *old_bfd; | |
3cbc5de0 | 3845 | bfd * undef_bfd = NULL; |
4ad4eba5 AM |
3846 | |
3847 | override = FALSE; | |
3848 | ||
3849 | flags = BSF_NO_FLAGS; | |
3850 | sec = NULL; | |
3851 | value = isym->st_value; | |
3852 | *sym_hash = NULL; | |
a4d8e49b | 3853 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3854 | |
3855 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 3856 | switch (bind) |
4ad4eba5 | 3857 | { |
3e7a7d11 | 3858 | case STB_LOCAL: |
4ad4eba5 AM |
3859 | /* This should be impossible, since ELF requires that all |
3860 | global symbols follow all local symbols, and that sh_info | |
3861 | point to the first global symbol. Unfortunately, Irix 5 | |
3862 | screws this up. */ | |
3863 | continue; | |
3e7a7d11 NC |
3864 | |
3865 | case STB_GLOBAL: | |
a4d8e49b | 3866 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 3867 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
3868 | break; |
3869 | ||
3870 | case STB_WEAK: | |
3871 | flags = BSF_WEAK; | |
3872 | break; | |
3873 | ||
3874 | case STB_GNU_UNIQUE: | |
3875 | flags = BSF_GNU_UNIQUE; | |
3876 | break; | |
3877 | ||
3878 | default: | |
4ad4eba5 | 3879 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 3880 | break; |
4ad4eba5 AM |
3881 | } |
3882 | ||
3883 | if (isym->st_shndx == SHN_UNDEF) | |
3884 | sec = bfd_und_section_ptr; | |
cb33740c AM |
3885 | else if (isym->st_shndx == SHN_ABS) |
3886 | sec = bfd_abs_section_ptr; | |
3887 | else if (isym->st_shndx == SHN_COMMON) | |
3888 | { | |
3889 | sec = bfd_com_section_ptr; | |
3890 | /* What ELF calls the size we call the value. What ELF | |
3891 | calls the value we call the alignment. */ | |
3892 | value = isym->st_size; | |
3893 | } | |
3894 | else | |
4ad4eba5 AM |
3895 | { |
3896 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3897 | if (sec == NULL) | |
3898 | sec = bfd_abs_section_ptr; | |
0c511000 | 3899 | else if (elf_discarded_section (sec)) |
529fcb95 | 3900 | { |
e5d08002 L |
3901 | /* Symbols from discarded section are undefined. We keep |
3902 | its visibility. */ | |
529fcb95 PB |
3903 | sec = bfd_und_section_ptr; |
3904 | isym->st_shndx = SHN_UNDEF; | |
3905 | } | |
4ad4eba5 AM |
3906 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3907 | value -= sec->vma; | |
3908 | } | |
4ad4eba5 AM |
3909 | |
3910 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3911 | isym->st_name); | |
3912 | if (name == NULL) | |
3913 | goto error_free_vers; | |
3914 | ||
3915 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
3916 | && (abfd->flags & BFD_PLUGIN) != 0) |
3917 | { | |
3918 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
3919 | ||
3920 | if (xc == NULL) | |
3921 | { | |
3922 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
3923 | | SEC_EXCLUDE); | |
3924 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
3925 | if (xc == NULL) | |
3926 | goto error_free_vers; | |
3927 | } | |
3928 | sec = xc; | |
3929 | } | |
3930 | else if (isym->st_shndx == SHN_COMMON | |
3931 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
3932 | && !info->relocatable) | |
4ad4eba5 AM |
3933 | { |
3934 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3935 | ||
3936 | if (tcomm == NULL) | |
3937 | { | |
02d00247 AM |
3938 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
3939 | | SEC_LINKER_CREATED); | |
3940 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 3941 | if (tcomm == NULL) |
4ad4eba5 AM |
3942 | goto error_free_vers; |
3943 | } | |
3944 | sec = tcomm; | |
3945 | } | |
66eb6687 | 3946 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 3947 | { |
66eb6687 AM |
3948 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
3949 | &sec, &value)) | |
4ad4eba5 AM |
3950 | goto error_free_vers; |
3951 | ||
3952 | /* The hook function sets the name to NULL if this symbol | |
3953 | should be skipped for some reason. */ | |
3954 | if (name == NULL) | |
3955 | continue; | |
3956 | } | |
3957 | ||
3958 | /* Sanity check that all possibilities were handled. */ | |
3959 | if (sec == NULL) | |
3960 | { | |
3961 | bfd_set_error (bfd_error_bad_value); | |
3962 | goto error_free_vers; | |
3963 | } | |
3964 | ||
3965 | if (bfd_is_und_section (sec) | |
3966 | || bfd_is_com_section (sec)) | |
3967 | definition = FALSE; | |
3968 | else | |
3969 | definition = TRUE; | |
3970 | ||
3971 | size_change_ok = FALSE; | |
66eb6687 | 3972 | type_change_ok = bed->type_change_ok; |
4ad4eba5 AM |
3973 | old_alignment = 0; |
3974 | old_bfd = NULL; | |
af44c138 | 3975 | new_sec = sec; |
4ad4eba5 | 3976 | |
66eb6687 | 3977 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
3978 | { |
3979 | Elf_Internal_Versym iver; | |
3980 | unsigned int vernum = 0; | |
3981 | bfd_boolean skip; | |
3982 | ||
b918acf9 NC |
3983 | /* If this is a definition of a symbol which was previously |
3984 | referenced in a non-weak manner then make a note of the bfd | |
3985 | that contained the reference. This is used if we need to | |
3986 | refer to the source of the reference later on. */ | |
3987 | if (! bfd_is_und_section (sec)) | |
3988 | { | |
3989 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
3990 | ||
3991 | if (h != NULL | |
3992 | && h->root.type == bfd_link_hash_undefined | |
3993 | && h->root.u.undef.abfd) | |
3994 | undef_bfd = h->root.u.undef.abfd; | |
3995 | } | |
3996 | ||
fc0e6df6 | 3997 | if (ever == NULL) |
4ad4eba5 | 3998 | { |
fc0e6df6 PB |
3999 | if (info->default_imported_symver) |
4000 | /* Use the default symbol version created earlier. */ | |
4001 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4002 | else | |
4003 | iver.vs_vers = 0; | |
4004 | } | |
4005 | else | |
4006 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4007 | ||
4008 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4009 | ||
4010 | /* If this is a hidden symbol, or if it is not version | |
4011 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4012 | However, we do not modify a non-hidden absolute symbol |
4013 | if it is not a function, because it might be the version | |
4014 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4015 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4016 | || (vernum > 1 |
4017 | && (!bfd_is_abs_section (sec) | |
4018 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4019 | { |
4020 | const char *verstr; | |
4021 | size_t namelen, verlen, newlen; | |
4022 | char *newname, *p; | |
4023 | ||
4024 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4025 | { |
fc0e6df6 PB |
4026 | if (vernum > elf_tdata (abfd)->cverdefs) |
4027 | verstr = NULL; | |
4028 | else if (vernum > 1) | |
4029 | verstr = | |
4030 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4031 | else | |
4032 | verstr = ""; | |
4ad4eba5 | 4033 | |
fc0e6df6 | 4034 | if (verstr == NULL) |
4ad4eba5 | 4035 | { |
fc0e6df6 PB |
4036 | (*_bfd_error_handler) |
4037 | (_("%B: %s: invalid version %u (max %d)"), | |
4038 | abfd, name, vernum, | |
4039 | elf_tdata (abfd)->cverdefs); | |
4040 | bfd_set_error (bfd_error_bad_value); | |
4041 | goto error_free_vers; | |
4ad4eba5 | 4042 | } |
fc0e6df6 PB |
4043 | } |
4044 | else | |
4045 | { | |
4046 | /* We cannot simply test for the number of | |
4047 | entries in the VERNEED section since the | |
4048 | numbers for the needed versions do not start | |
4049 | at 0. */ | |
4050 | Elf_Internal_Verneed *t; | |
4051 | ||
4052 | verstr = NULL; | |
4053 | for (t = elf_tdata (abfd)->verref; | |
4054 | t != NULL; | |
4055 | t = t->vn_nextref) | |
4ad4eba5 | 4056 | { |
fc0e6df6 | 4057 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4058 | |
fc0e6df6 PB |
4059 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4060 | { | |
4061 | if (a->vna_other == vernum) | |
4ad4eba5 | 4062 | { |
fc0e6df6 PB |
4063 | verstr = a->vna_nodename; |
4064 | break; | |
4ad4eba5 | 4065 | } |
4ad4eba5 | 4066 | } |
fc0e6df6 PB |
4067 | if (a != NULL) |
4068 | break; | |
4069 | } | |
4070 | if (verstr == NULL) | |
4071 | { | |
4072 | (*_bfd_error_handler) | |
4073 | (_("%B: %s: invalid needed version %d"), | |
4074 | abfd, name, vernum); | |
4075 | bfd_set_error (bfd_error_bad_value); | |
4076 | goto error_free_vers; | |
4ad4eba5 | 4077 | } |
4ad4eba5 | 4078 | } |
fc0e6df6 PB |
4079 | |
4080 | namelen = strlen (name); | |
4081 | verlen = strlen (verstr); | |
4082 | newlen = namelen + verlen + 2; | |
4083 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4084 | && isym->st_shndx != SHN_UNDEF) | |
4085 | ++newlen; | |
4086 | ||
a50b1753 | 4087 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4088 | if (newname == NULL) |
4089 | goto error_free_vers; | |
4090 | memcpy (newname, name, namelen); | |
4091 | p = newname + namelen; | |
4092 | *p++ = ELF_VER_CHR; | |
4093 | /* If this is a defined non-hidden version symbol, | |
4094 | we add another @ to the name. This indicates the | |
4095 | default version of the symbol. */ | |
4096 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4097 | && isym->st_shndx != SHN_UNDEF) | |
4098 | *p++ = ELF_VER_CHR; | |
4099 | memcpy (p, verstr, verlen + 1); | |
4100 | ||
4101 | name = newname; | |
4ad4eba5 AM |
4102 | } |
4103 | ||
b918acf9 NC |
4104 | /* If necessary, make a second attempt to locate the bfd |
4105 | containing an unresolved, non-weak reference to the | |
4106 | current symbol. */ | |
4107 | if (! bfd_is_und_section (sec) && undef_bfd == NULL) | |
3cbc5de0 NC |
4108 | { |
4109 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4110 | ||
4111 | if (h != NULL | |
b918acf9 | 4112 | && h->root.type == bfd_link_hash_undefined |
3cbc5de0 NC |
4113 | && h->root.u.undef.abfd) |
4114 | undef_bfd = h->root.u.undef.abfd; | |
4115 | } | |
4116 | ||
af44c138 L |
4117 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, |
4118 | &value, &old_alignment, | |
4ad4eba5 AM |
4119 | sym_hash, &skip, &override, |
4120 | &type_change_ok, &size_change_ok)) | |
4121 | goto error_free_vers; | |
4122 | ||
4123 | if (skip) | |
4124 | continue; | |
4125 | ||
4126 | if (override) | |
4127 | definition = FALSE; | |
4128 | ||
4129 | h = *sym_hash; | |
4130 | while (h->root.type == bfd_link_hash_indirect | |
4131 | || h->root.type == bfd_link_hash_warning) | |
4132 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4133 | ||
4134 | /* Remember the old alignment if this is a common symbol, so | |
4135 | that we don't reduce the alignment later on. We can't | |
4136 | check later, because _bfd_generic_link_add_one_symbol | |
4137 | will set a default for the alignment which we want to | |
4138 | override. We also remember the old bfd where the existing | |
4139 | definition comes from. */ | |
4140 | switch (h->root.type) | |
4141 | { | |
4142 | default: | |
4143 | break; | |
4144 | ||
4145 | case bfd_link_hash_defined: | |
4146 | case bfd_link_hash_defweak: | |
4147 | old_bfd = h->root.u.def.section->owner; | |
4148 | break; | |
4149 | ||
4150 | case bfd_link_hash_common: | |
4151 | old_bfd = h->root.u.c.p->section->owner; | |
4152 | old_alignment = h->root.u.c.p->alignment_power; | |
4153 | break; | |
4154 | } | |
4155 | ||
4156 | if (elf_tdata (abfd)->verdef != NULL | |
4157 | && ! override | |
4158 | && vernum > 1 | |
4159 | && definition) | |
4160 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4161 | } | |
4162 | ||
4163 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4164 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4165 | (struct bfd_link_hash_entry **) sym_hash))) |
4166 | goto error_free_vers; | |
4167 | ||
4168 | h = *sym_hash; | |
4169 | while (h->root.type == bfd_link_hash_indirect | |
4170 | || h->root.type == bfd_link_hash_warning) | |
4171 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4172 | |
4ad4eba5 | 4173 | *sym_hash = h; |
d64284fe L |
4174 | if (is_elf_hash_table (htab)) |
4175 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4ad4eba5 AM |
4176 | |
4177 | new_weakdef = FALSE; | |
4178 | if (dynamic | |
4179 | && definition | |
4180 | && (flags & BSF_WEAK) != 0 | |
fcb93ecf | 4181 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4182 | && is_elf_hash_table (htab) |
f6e332e6 | 4183 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
4184 | { |
4185 | /* Keep a list of all weak defined non function symbols from | |
4186 | a dynamic object, using the weakdef field. Later in this | |
4187 | function we will set the weakdef field to the correct | |
4188 | value. We only put non-function symbols from dynamic | |
4189 | objects on this list, because that happens to be the only | |
4190 | time we need to know the normal symbol corresponding to a | |
4191 | weak symbol, and the information is time consuming to | |
4192 | figure out. If the weakdef field is not already NULL, | |
4193 | then this symbol was already defined by some previous | |
4194 | dynamic object, and we will be using that previous | |
4195 | definition anyhow. */ | |
4196 | ||
f6e332e6 | 4197 | h->u.weakdef = weaks; |
4ad4eba5 AM |
4198 | weaks = h; |
4199 | new_weakdef = TRUE; | |
4200 | } | |
4201 | ||
4202 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4203 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4204 | && h->root.type == bfd_link_hash_common) |
4205 | { | |
4206 | unsigned int align; | |
4207 | ||
a4d8e49b | 4208 | if (common) |
af44c138 L |
4209 | align = bfd_log2 (isym->st_value); |
4210 | else | |
4211 | { | |
4212 | /* The new symbol is a common symbol in a shared object. | |
4213 | We need to get the alignment from the section. */ | |
4214 | align = new_sec->alignment_power; | |
4215 | } | |
595213d4 | 4216 | if (align > old_alignment) |
4ad4eba5 AM |
4217 | h->root.u.c.p->alignment_power = align; |
4218 | else | |
4219 | h->root.u.c.p->alignment_power = old_alignment; | |
4220 | } | |
4221 | ||
66eb6687 | 4222 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4223 | { |
4ad4eba5 | 4224 | bfd_boolean dynsym; |
4ad4eba5 AM |
4225 | |
4226 | /* Check the alignment when a common symbol is involved. This | |
4227 | can change when a common symbol is overridden by a normal | |
4228 | definition or a common symbol is ignored due to the old | |
4229 | normal definition. We need to make sure the maximum | |
4230 | alignment is maintained. */ | |
a4d8e49b | 4231 | if ((old_alignment || common) |
4ad4eba5 AM |
4232 | && h->root.type != bfd_link_hash_common) |
4233 | { | |
4234 | unsigned int common_align; | |
4235 | unsigned int normal_align; | |
4236 | unsigned int symbol_align; | |
4237 | bfd *normal_bfd; | |
4238 | bfd *common_bfd; | |
4239 | ||
4240 | symbol_align = ffs (h->root.u.def.value) - 1; | |
4241 | if (h->root.u.def.section->owner != NULL | |
4242 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
4243 | { | |
4244 | normal_align = h->root.u.def.section->alignment_power; | |
4245 | if (normal_align > symbol_align) | |
4246 | normal_align = symbol_align; | |
4247 | } | |
4248 | else | |
4249 | normal_align = symbol_align; | |
4250 | ||
4251 | if (old_alignment) | |
4252 | { | |
4253 | common_align = old_alignment; | |
4254 | common_bfd = old_bfd; | |
4255 | normal_bfd = abfd; | |
4256 | } | |
4257 | else | |
4258 | { | |
4259 | common_align = bfd_log2 (isym->st_value); | |
4260 | common_bfd = abfd; | |
4261 | normal_bfd = old_bfd; | |
4262 | } | |
4263 | ||
4264 | if (normal_align < common_align) | |
d07676f8 NC |
4265 | { |
4266 | /* PR binutils/2735 */ | |
4267 | if (normal_bfd == NULL) | |
4268 | (*_bfd_error_handler) | |
4269 | (_("Warning: alignment %u of common symbol `%s' in %B" | |
4270 | " is greater than the alignment (%u) of its section %A"), | |
4271 | common_bfd, h->root.u.def.section, | |
4272 | 1 << common_align, name, 1 << normal_align); | |
4273 | else | |
4274 | (*_bfd_error_handler) | |
4275 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4276 | " is smaller than %u in %B"), | |
4277 | normal_bfd, common_bfd, | |
4278 | 1 << normal_align, name, 1 << common_align); | |
4279 | } | |
4ad4eba5 AM |
4280 | } |
4281 | ||
83ad0046 L |
4282 | /* Remember the symbol size if it isn't undefined. */ |
4283 | if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 AM |
4284 | && (definition || h->size == 0)) |
4285 | { | |
83ad0046 L |
4286 | if (h->size != 0 |
4287 | && h->size != isym->st_size | |
4288 | && ! size_change_ok) | |
4ad4eba5 | 4289 | (*_bfd_error_handler) |
d003868e AM |
4290 | (_("Warning: size of symbol `%s' changed" |
4291 | " from %lu in %B to %lu in %B"), | |
4292 | old_bfd, abfd, | |
4ad4eba5 | 4293 | name, (unsigned long) h->size, |
d003868e | 4294 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4295 | |
4296 | h->size = isym->st_size; | |
4297 | } | |
4298 | ||
4299 | /* If this is a common symbol, then we always want H->SIZE | |
4300 | to be the size of the common symbol. The code just above | |
4301 | won't fix the size if a common symbol becomes larger. We | |
4302 | don't warn about a size change here, because that is | |
fcb93ecf PB |
4303 | covered by --warn-common. Allow changed between different |
4304 | function types. */ | |
4ad4eba5 AM |
4305 | if (h->root.type == bfd_link_hash_common) |
4306 | h->size = h->root.u.c.size; | |
4307 | ||
4308 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
4309 | && (definition || h->type == STT_NOTYPE)) | |
4310 | { | |
2955ec4c L |
4311 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4312 | ||
4313 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4314 | symbol. */ | |
4315 | if (type == STT_GNU_IFUNC | |
4316 | && (abfd->flags & DYNAMIC) != 0) | |
4317 | type = STT_FUNC; | |
4ad4eba5 | 4318 | |
2955ec4c L |
4319 | if (h->type != type) |
4320 | { | |
4321 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
4322 | (*_bfd_error_handler) | |
4323 | (_("Warning: type of symbol `%s' changed" | |
4324 | " from %d to %d in %B"), | |
4325 | abfd, name, h->type, type); | |
4326 | ||
4327 | h->type = type; | |
4328 | } | |
4ad4eba5 AM |
4329 | } |
4330 | ||
54ac0771 L |
4331 | /* Merge st_other field. */ |
4332 | elf_merge_st_other (abfd, h, isym, definition, dynamic); | |
4ad4eba5 AM |
4333 | |
4334 | /* Set a flag in the hash table entry indicating the type of | |
4335 | reference or definition we just found. Keep a count of | |
4336 | the number of dynamic symbols we find. A dynamic symbol | |
4337 | is one which is referenced or defined by both a regular | |
4338 | object and a shared object. */ | |
4ad4eba5 AM |
4339 | dynsym = FALSE; |
4340 | if (! dynamic) | |
4341 | { | |
4342 | if (! definition) | |
4343 | { | |
f5385ebf | 4344 | h->ref_regular = 1; |
4ad4eba5 | 4345 | if (bind != STB_WEAK) |
f5385ebf | 4346 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
4347 | } |
4348 | else | |
d8880531 L |
4349 | { |
4350 | h->def_regular = 1; | |
4351 | if (h->def_dynamic) | |
4352 | { | |
4353 | h->def_dynamic = 0; | |
4354 | h->ref_dynamic = 1; | |
d8880531 L |
4355 | } |
4356 | } | |
4ad4eba5 | 4357 | if (! info->executable |
f5385ebf AM |
4358 | || h->def_dynamic |
4359 | || h->ref_dynamic) | |
4ad4eba5 AM |
4360 | dynsym = TRUE; |
4361 | } | |
4362 | else | |
4363 | { | |
4364 | if (! definition) | |
f5385ebf | 4365 | h->ref_dynamic = 1; |
4ad4eba5 | 4366 | else |
54e8959c L |
4367 | { |
4368 | h->def_dynamic = 1; | |
4369 | h->dynamic_def = 1; | |
4370 | } | |
f5385ebf AM |
4371 | if (h->def_regular |
4372 | || h->ref_regular | |
f6e332e6 | 4373 | || (h->u.weakdef != NULL |
4ad4eba5 | 4374 | && ! new_weakdef |
f6e332e6 | 4375 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
4376 | dynsym = TRUE; |
4377 | } | |
4378 | ||
b2064611 | 4379 | if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable) |
92b7c7b6 L |
4380 | { |
4381 | /* We don't want to make debug symbol dynamic. */ | |
92b7c7b6 L |
4382 | dynsym = FALSE; |
4383 | } | |
4384 | ||
35fc36a8 RS |
4385 | if (definition) |
4386 | h->target_internal = isym->st_target_internal; | |
4387 | ||
4ad4eba5 AM |
4388 | /* Check to see if we need to add an indirect symbol for |
4389 | the default name. */ | |
4390 | if (definition || h->root.type == bfd_link_hash_common) | |
4391 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4392 | &sec, &value, &dynsym, | |
4393 | override)) | |
4394 | goto error_free_vers; | |
4395 | ||
4396 | if (definition && !dynamic) | |
4397 | { | |
4398 | char *p = strchr (name, ELF_VER_CHR); | |
4399 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4400 | { | |
4401 | /* Queue non-default versions so that .symver x, x@FOO | |
4402 | aliases can be checked. */ | |
66eb6687 | 4403 | if (!nondeflt_vers) |
4ad4eba5 | 4404 | { |
66eb6687 AM |
4405 | amt = ((isymend - isym + 1) |
4406 | * sizeof (struct elf_link_hash_entry *)); | |
a50b1753 NC |
4407 | nondeflt_vers = |
4408 | (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4409 | if (!nondeflt_vers) |
4410 | goto error_free_vers; | |
4ad4eba5 | 4411 | } |
66eb6687 | 4412 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4413 | } |
4414 | } | |
4415 | ||
4416 | if (dynsym && h->dynindx == -1) | |
4417 | { | |
c152c796 | 4418 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4419 | goto error_free_vers; |
f6e332e6 | 4420 | if (h->u.weakdef != NULL |
4ad4eba5 | 4421 | && ! new_weakdef |
f6e332e6 | 4422 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4423 | { |
66eb6687 | 4424 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4425 | goto error_free_vers; |
4426 | } | |
4427 | } | |
4428 | else if (dynsym && h->dynindx != -1) | |
4429 | /* If the symbol already has a dynamic index, but | |
4430 | visibility says it should not be visible, turn it into | |
4431 | a local symbol. */ | |
4432 | switch (ELF_ST_VISIBILITY (h->other)) | |
4433 | { | |
4434 | case STV_INTERNAL: | |
4435 | case STV_HIDDEN: | |
4436 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4437 | dynsym = FALSE; | |
4438 | break; | |
4439 | } | |
4440 | ||
4441 | if (!add_needed | |
4442 | && definition | |
010e5ae2 AM |
4443 | && ((dynsym |
4444 | && h->ref_regular) | |
4445 | || (h->ref_dynamic | |
4446 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 | |
4447 | && !on_needed_list (elf_dt_name (abfd), htab->needed)))) | |
4ad4eba5 AM |
4448 | { |
4449 | int ret; | |
4450 | const char *soname = elf_dt_name (abfd); | |
4451 | ||
4452 | /* A symbol from a library loaded via DT_NEEDED of some | |
4453 | other library is referenced by a regular object. | |
e56f61be | 4454 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
4455 | --no-add-needed is used and the reference was not |
4456 | a weak one. */ | |
4457 | if (undef_bfd != NULL | |
4458 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
e56f61be L |
4459 | { |
4460 | (*_bfd_error_handler) | |
3cbc5de0 | 4461 | (_("%B: undefined reference to symbol '%s'"), |
b918acf9 | 4462 | undef_bfd, name); |
3cbc5de0 NC |
4463 | (*_bfd_error_handler) |
4464 | (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"), | |
d003868e | 4465 | abfd, name); |
3cbc5de0 | 4466 | bfd_set_error (bfd_error_invalid_operation); |
e56f61be L |
4467 | goto error_free_vers; |
4468 | } | |
4469 | ||
a50b1753 NC |
4470 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
4471 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); | |
a5db907e | 4472 | |
4ad4eba5 | 4473 | add_needed = TRUE; |
7e9f0867 | 4474 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4475 | if (ret < 0) |
4476 | goto error_free_vers; | |
4477 | ||
4478 | BFD_ASSERT (ret == 0); | |
4479 | } | |
4480 | } | |
4481 | } | |
4482 | ||
66eb6687 AM |
4483 | if (extversym != NULL) |
4484 | { | |
4485 | free (extversym); | |
4486 | extversym = NULL; | |
4487 | } | |
4488 | ||
4489 | if (isymbuf != NULL) | |
4490 | { | |
4491 | free (isymbuf); | |
4492 | isymbuf = NULL; | |
4493 | } | |
4494 | ||
4495 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4496 | { | |
4497 | unsigned int i; | |
4498 | ||
4499 | /* Restore the symbol table. */ | |
97fed1c9 JJ |
4500 | if (bed->as_needed_cleanup) |
4501 | (*bed->as_needed_cleanup) (abfd, info); | |
66eb6687 AM |
4502 | old_hash = (char *) old_tab + tabsize; |
4503 | old_ent = (char *) old_hash + hashsize; | |
4504 | sym_hash = elf_sym_hashes (abfd); | |
4f87808c AM |
4505 | htab->root.table.table = old_table; |
4506 | htab->root.table.size = old_size; | |
4507 | htab->root.table.count = old_count; | |
66eb6687 AM |
4508 | memcpy (htab->root.table.table, old_tab, tabsize); |
4509 | memcpy (sym_hash, old_hash, hashsize); | |
4510 | htab->root.undefs = old_undefs; | |
4511 | htab->root.undefs_tail = old_undefs_tail; | |
4512 | for (i = 0; i < htab->root.table.size; i++) | |
4513 | { | |
4514 | struct bfd_hash_entry *p; | |
4515 | struct elf_link_hash_entry *h; | |
4516 | ||
4517 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4518 | { | |
4519 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4520 | if (h->root.type == bfd_link_hash_warning) |
4521 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
66eb6687 AM |
4522 | if (h->dynindx >= old_dynsymcount) |
4523 | _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); | |
2de92251 | 4524 | |
66eb6687 AM |
4525 | memcpy (p, old_ent, htab->root.table.entsize); |
4526 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4527 | h = (struct elf_link_hash_entry *) p; |
4528 | if (h->root.type == bfd_link_hash_warning) | |
4529 | { | |
4530 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4531 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4532 | } | |
66eb6687 AM |
4533 | } |
4534 | } | |
4535 | ||
5061a885 AM |
4536 | /* Make a special call to the linker "notice" function to |
4537 | tell it that symbols added for crefs may need to be removed. */ | |
4538 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, | |
16d96b5b | 4539 | notice_not_needed, 0, NULL)) |
9af2a943 | 4540 | goto error_free_vers; |
5061a885 | 4541 | |
66eb6687 AM |
4542 | free (old_tab); |
4543 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4544 | alloc_mark); | |
4545 | if (nondeflt_vers != NULL) | |
4546 | free (nondeflt_vers); | |
4547 | return TRUE; | |
4548 | } | |
2de92251 | 4549 | |
66eb6687 AM |
4550 | if (old_tab != NULL) |
4551 | { | |
5061a885 | 4552 | if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, |
16d96b5b | 4553 | notice_needed, 0, NULL)) |
9af2a943 | 4554 | goto error_free_vers; |
66eb6687 AM |
4555 | free (old_tab); |
4556 | old_tab = NULL; | |
4557 | } | |
4558 | ||
4ad4eba5 AM |
4559 | /* Now that all the symbols from this input file are created, handle |
4560 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4561 | if (nondeflt_vers != NULL) | |
4562 | { | |
4563 | bfd_size_type cnt, symidx; | |
4564 | ||
4565 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4566 | { | |
4567 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4568 | char *shortname, *p; | |
4569 | ||
4570 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4571 | if (p == NULL | |
4572 | || (h->root.type != bfd_link_hash_defined | |
4573 | && h->root.type != bfd_link_hash_defweak)) | |
4574 | continue; | |
4575 | ||
4576 | amt = p - h->root.root.string; | |
a50b1753 | 4577 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
4578 | if (!shortname) |
4579 | goto error_free_vers; | |
4ad4eba5 AM |
4580 | memcpy (shortname, h->root.root.string, amt); |
4581 | shortname[amt] = '\0'; | |
4582 | ||
4583 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4584 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4585 | FALSE, FALSE, FALSE); |
4586 | if (hi != NULL | |
4587 | && hi->root.type == h->root.type | |
4588 | && hi->root.u.def.value == h->root.u.def.value | |
4589 | && hi->root.u.def.section == h->root.u.def.section) | |
4590 | { | |
4591 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4592 | hi->root.type = bfd_link_hash_indirect; | |
4593 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4594 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4595 | sym_hash = elf_sym_hashes (abfd); |
4596 | if (sym_hash) | |
4597 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4598 | if (sym_hash[symidx] == hi) | |
4599 | { | |
4600 | sym_hash[symidx] = h; | |
4601 | break; | |
4602 | } | |
4603 | } | |
4604 | free (shortname); | |
4605 | } | |
4606 | free (nondeflt_vers); | |
4607 | nondeflt_vers = NULL; | |
4608 | } | |
4609 | ||
4ad4eba5 AM |
4610 | /* Now set the weakdefs field correctly for all the weak defined |
4611 | symbols we found. The only way to do this is to search all the | |
4612 | symbols. Since we only need the information for non functions in | |
4613 | dynamic objects, that's the only time we actually put anything on | |
4614 | the list WEAKS. We need this information so that if a regular | |
4615 | object refers to a symbol defined weakly in a dynamic object, the | |
4616 | real symbol in the dynamic object is also put in the dynamic | |
4617 | symbols; we also must arrange for both symbols to point to the | |
4618 | same memory location. We could handle the general case of symbol | |
4619 | aliasing, but a general symbol alias can only be generated in | |
4620 | assembler code, handling it correctly would be very time | |
4621 | consuming, and other ELF linkers don't handle general aliasing | |
4622 | either. */ | |
4623 | if (weaks != NULL) | |
4624 | { | |
4625 | struct elf_link_hash_entry **hpp; | |
4626 | struct elf_link_hash_entry **hppend; | |
4627 | struct elf_link_hash_entry **sorted_sym_hash; | |
4628 | struct elf_link_hash_entry *h; | |
4629 | size_t sym_count; | |
4630 | ||
4631 | /* Since we have to search the whole symbol list for each weak | |
4632 | defined symbol, search time for N weak defined symbols will be | |
4633 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4634 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 4635 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
4636 | if (sorted_sym_hash == NULL) |
4637 | goto error_return; | |
4638 | sym_hash = sorted_sym_hash; | |
4639 | hpp = elf_sym_hashes (abfd); | |
4640 | hppend = hpp + extsymcount; | |
4641 | sym_count = 0; | |
4642 | for (; hpp < hppend; hpp++) | |
4643 | { | |
4644 | h = *hpp; | |
4645 | if (h != NULL | |
4646 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4647 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4648 | { |
4649 | *sym_hash = h; | |
4650 | sym_hash++; | |
4651 | sym_count++; | |
4652 | } | |
4653 | } | |
4654 | ||
4655 | qsort (sorted_sym_hash, sym_count, | |
4656 | sizeof (struct elf_link_hash_entry *), | |
4657 | elf_sort_symbol); | |
4658 | ||
4659 | while (weaks != NULL) | |
4660 | { | |
4661 | struct elf_link_hash_entry *hlook; | |
4662 | asection *slook; | |
4663 | bfd_vma vlook; | |
4664 | long ilook; | |
4665 | size_t i, j, idx; | |
4666 | ||
4667 | hlook = weaks; | |
f6e332e6 AM |
4668 | weaks = hlook->u.weakdef; |
4669 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4670 | |
4671 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4672 | || hlook->root.type == bfd_link_hash_defweak | |
4673 | || hlook->root.type == bfd_link_hash_common | |
4674 | || hlook->root.type == bfd_link_hash_indirect); | |
4675 | slook = hlook->root.u.def.section; | |
4676 | vlook = hlook->root.u.def.value; | |
4677 | ||
4678 | ilook = -1; | |
4679 | i = 0; | |
4680 | j = sym_count; | |
4681 | while (i < j) | |
4682 | { | |
4683 | bfd_signed_vma vdiff; | |
4684 | idx = (i + j) / 2; | |
4685 | h = sorted_sym_hash [idx]; | |
4686 | vdiff = vlook - h->root.u.def.value; | |
4687 | if (vdiff < 0) | |
4688 | j = idx; | |
4689 | else if (vdiff > 0) | |
4690 | i = idx + 1; | |
4691 | else | |
4692 | { | |
a9b881be | 4693 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4694 | if (sdiff < 0) |
4695 | j = idx; | |
4696 | else if (sdiff > 0) | |
4697 | i = idx + 1; | |
4698 | else | |
4699 | { | |
4700 | ilook = idx; | |
4701 | break; | |
4702 | } | |
4703 | } | |
4704 | } | |
4705 | ||
4706 | /* We didn't find a value/section match. */ | |
4707 | if (ilook == -1) | |
4708 | continue; | |
4709 | ||
4710 | for (i = ilook; i < sym_count; i++) | |
4711 | { | |
4712 | h = sorted_sym_hash [i]; | |
4713 | ||
4714 | /* Stop if value or section doesn't match. */ | |
4715 | if (h->root.u.def.value != vlook | |
4716 | || h->root.u.def.section != slook) | |
4717 | break; | |
4718 | else if (h != hlook) | |
4719 | { | |
f6e332e6 | 4720 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4721 | |
4722 | /* If the weak definition is in the list of dynamic | |
4723 | symbols, make sure the real definition is put | |
4724 | there as well. */ | |
4725 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4726 | { | |
c152c796 | 4727 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
4728 | { |
4729 | err_free_sym_hash: | |
4730 | free (sorted_sym_hash); | |
4731 | goto error_return; | |
4732 | } | |
4ad4eba5 AM |
4733 | } |
4734 | ||
4735 | /* If the real definition is in the list of dynamic | |
4736 | symbols, make sure the weak definition is put | |
4737 | there as well. If we don't do this, then the | |
4738 | dynamic loader might not merge the entries for the | |
4739 | real definition and the weak definition. */ | |
4740 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4741 | { | |
c152c796 | 4742 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 4743 | goto err_free_sym_hash; |
4ad4eba5 AM |
4744 | } |
4745 | break; | |
4746 | } | |
4747 | } | |
4748 | } | |
4749 | ||
4750 | free (sorted_sym_hash); | |
4751 | } | |
4752 | ||
33177bb1 AM |
4753 | if (bed->check_directives |
4754 | && !(*bed->check_directives) (abfd, info)) | |
4755 | return FALSE; | |
85fbca6a | 4756 | |
4ad4eba5 AM |
4757 | /* If this object is the same format as the output object, and it is |
4758 | not a shared library, then let the backend look through the | |
4759 | relocs. | |
4760 | ||
4761 | This is required to build global offset table entries and to | |
4762 | arrange for dynamic relocs. It is not required for the | |
4763 | particular common case of linking non PIC code, even when linking | |
4764 | against shared libraries, but unfortunately there is no way of | |
4765 | knowing whether an object file has been compiled PIC or not. | |
4766 | Looking through the relocs is not particularly time consuming. | |
4767 | The problem is that we must either (1) keep the relocs in memory, | |
4768 | which causes the linker to require additional runtime memory or | |
4769 | (2) read the relocs twice from the input file, which wastes time. | |
4770 | This would be a good case for using mmap. | |
4771 | ||
4772 | I have no idea how to handle linking PIC code into a file of a | |
4773 | different format. It probably can't be done. */ | |
4ad4eba5 | 4774 | if (! dynamic |
66eb6687 | 4775 | && is_elf_hash_table (htab) |
13285a1b | 4776 | && bed->check_relocs != NULL |
39334f3a | 4777 | && elf_object_id (abfd) == elf_hash_table_id (htab) |
f13a99db | 4778 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) |
4ad4eba5 AM |
4779 | { |
4780 | asection *o; | |
4781 | ||
4782 | for (o = abfd->sections; o != NULL; o = o->next) | |
4783 | { | |
4784 | Elf_Internal_Rela *internal_relocs; | |
4785 | bfd_boolean ok; | |
4786 | ||
4787 | if ((o->flags & SEC_RELOC) == 0 | |
4788 | || o->reloc_count == 0 | |
4789 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4790 | && (o->flags & SEC_DEBUGGING) != 0) | |
4791 | || bfd_is_abs_section (o->output_section)) | |
4792 | continue; | |
4793 | ||
4794 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4795 | info->keep_memory); | |
4796 | if (internal_relocs == NULL) | |
4797 | goto error_return; | |
4798 | ||
66eb6687 | 4799 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); |
4ad4eba5 AM |
4800 | |
4801 | if (elf_section_data (o)->relocs != internal_relocs) | |
4802 | free (internal_relocs); | |
4803 | ||
4804 | if (! ok) | |
4805 | goto error_return; | |
4806 | } | |
4807 | } | |
4808 | ||
4809 | /* If this is a non-traditional link, try to optimize the handling | |
4810 | of the .stab/.stabstr sections. */ | |
4811 | if (! dynamic | |
4812 | && ! info->traditional_format | |
66eb6687 | 4813 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
4814 | && (info->strip != strip_all && info->strip != strip_debugger)) |
4815 | { | |
4816 | asection *stabstr; | |
4817 | ||
4818 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4819 | if (stabstr != NULL) | |
4820 | { | |
4821 | bfd_size_type string_offset = 0; | |
4822 | asection *stab; | |
4823 | ||
4824 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 4825 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
4826 | && (!stab->name[5] || |
4827 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4828 | && (stab->flags & SEC_MERGE) == 0 | |
4829 | && !bfd_is_abs_section (stab->output_section)) | |
4830 | { | |
4831 | struct bfd_elf_section_data *secdata; | |
4832 | ||
4833 | secdata = elf_section_data (stab); | |
66eb6687 AM |
4834 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
4835 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
4836 | &string_offset)) |
4837 | goto error_return; | |
4838 | if (secdata->sec_info) | |
4839 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4840 | } | |
4841 | } | |
4842 | } | |
4843 | ||
66eb6687 | 4844 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
4845 | { |
4846 | /* Add this bfd to the loaded list. */ | |
4847 | struct elf_link_loaded_list *n; | |
4848 | ||
a50b1753 NC |
4849 | n = (struct elf_link_loaded_list *) |
4850 | bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4ad4eba5 AM |
4851 | if (n == NULL) |
4852 | goto error_return; | |
4853 | n->abfd = abfd; | |
66eb6687 AM |
4854 | n->next = htab->loaded; |
4855 | htab->loaded = n; | |
4ad4eba5 AM |
4856 | } |
4857 | ||
4858 | return TRUE; | |
4859 | ||
4860 | error_free_vers: | |
66eb6687 AM |
4861 | if (old_tab != NULL) |
4862 | free (old_tab); | |
4ad4eba5 AM |
4863 | if (nondeflt_vers != NULL) |
4864 | free (nondeflt_vers); | |
4865 | if (extversym != NULL) | |
4866 | free (extversym); | |
4867 | error_free_sym: | |
4868 | if (isymbuf != NULL) | |
4869 | free (isymbuf); | |
4870 | error_return: | |
4871 | return FALSE; | |
4872 | } | |
4873 | ||
8387904d AM |
4874 | /* Return the linker hash table entry of a symbol that might be |
4875 | satisfied by an archive symbol. Return -1 on error. */ | |
4876 | ||
4877 | struct elf_link_hash_entry * | |
4878 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4879 | struct bfd_link_info *info, | |
4880 | const char *name) | |
4881 | { | |
4882 | struct elf_link_hash_entry *h; | |
4883 | char *p, *copy; | |
4884 | size_t len, first; | |
4885 | ||
2a41f396 | 4886 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
4887 | if (h != NULL) |
4888 | return h; | |
4889 | ||
4890 | /* If this is a default version (the name contains @@), look up the | |
4891 | symbol again with only one `@' as well as without the version. | |
4892 | The effect is that references to the symbol with and without the | |
4893 | version will be matched by the default symbol in the archive. */ | |
4894 | ||
4895 | p = strchr (name, ELF_VER_CHR); | |
4896 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4897 | return h; | |
4898 | ||
4899 | /* First check with only one `@'. */ | |
4900 | len = strlen (name); | |
a50b1753 | 4901 | copy = (char *) bfd_alloc (abfd, len); |
8387904d AM |
4902 | if (copy == NULL) |
4903 | return (struct elf_link_hash_entry *) 0 - 1; | |
4904 | ||
4905 | first = p - name + 1; | |
4906 | memcpy (copy, name, first); | |
4907 | memcpy (copy + first, name + first + 1, len - first); | |
4908 | ||
2a41f396 | 4909 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
4910 | if (h == NULL) |
4911 | { | |
4912 | /* We also need to check references to the symbol without the | |
4913 | version. */ | |
4914 | copy[first - 1] = '\0'; | |
4915 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 4916 | FALSE, FALSE, TRUE); |
8387904d AM |
4917 | } |
4918 | ||
4919 | bfd_release (abfd, copy); | |
4920 | return h; | |
4921 | } | |
4922 | ||
0ad989f9 L |
4923 | /* Add symbols from an ELF archive file to the linker hash table. We |
4924 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4925 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4926 | archive which includes an entry libc.so.1 which defines a bunch of | |
4927 | symbols. The libc.so archive also includes a number of other | |
4928 | object files, which also define symbols, some of which are the same | |
4929 | as those defined in libc.so.1. Correct linking requires that we | |
4930 | consider each object file in turn, and include it if it defines any | |
4931 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4932 | this; it looks through the list of undefined symbols, and includes | |
4933 | any object file which defines them. When this algorithm is used on | |
4934 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4935 | bunch of symbols. This means that some of the other objects in the | |
4936 | archive are not included in the link, which is incorrect since they | |
4937 | precede libc.so.1 in the archive. | |
4938 | ||
4939 | Fortunately, ELF archive handling is simpler than that done by | |
4940 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4941 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4942 | symbol is currently undefined, we know that we must pull in that | |
4943 | object file. | |
4944 | ||
4945 | Unfortunately, we do have to make multiple passes over the symbol | |
4946 | table until nothing further is resolved. */ | |
4947 | ||
4ad4eba5 AM |
4948 | static bfd_boolean |
4949 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4950 | { |
4951 | symindex c; | |
4952 | bfd_boolean *defined = NULL; | |
4953 | bfd_boolean *included = NULL; | |
4954 | carsym *symdefs; | |
4955 | bfd_boolean loop; | |
4956 | bfd_size_type amt; | |
8387904d AM |
4957 | const struct elf_backend_data *bed; |
4958 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4959 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4960 | |
4961 | if (! bfd_has_map (abfd)) | |
4962 | { | |
4963 | /* An empty archive is a special case. */ | |
4964 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4965 | return TRUE; | |
4966 | bfd_set_error (bfd_error_no_armap); | |
4967 | return FALSE; | |
4968 | } | |
4969 | ||
4970 | /* Keep track of all symbols we know to be already defined, and all | |
4971 | files we know to be already included. This is to speed up the | |
4972 | second and subsequent passes. */ | |
4973 | c = bfd_ardata (abfd)->symdef_count; | |
4974 | if (c == 0) | |
4975 | return TRUE; | |
4976 | amt = c; | |
4977 | amt *= sizeof (bfd_boolean); | |
a50b1753 NC |
4978 | defined = (bfd_boolean *) bfd_zmalloc (amt); |
4979 | included = (bfd_boolean *) bfd_zmalloc (amt); | |
0ad989f9 L |
4980 | if (defined == NULL || included == NULL) |
4981 | goto error_return; | |
4982 | ||
4983 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4984 | bed = get_elf_backend_data (abfd); |
4985 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4986 | |
4987 | do | |
4988 | { | |
4989 | file_ptr last; | |
4990 | symindex i; | |
4991 | carsym *symdef; | |
4992 | carsym *symdefend; | |
4993 | ||
4994 | loop = FALSE; | |
4995 | last = -1; | |
4996 | ||
4997 | symdef = symdefs; | |
4998 | symdefend = symdef + c; | |
4999 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5000 | { | |
5001 | struct elf_link_hash_entry *h; | |
5002 | bfd *element; | |
5003 | struct bfd_link_hash_entry *undefs_tail; | |
5004 | symindex mark; | |
5005 | ||
5006 | if (defined[i] || included[i]) | |
5007 | continue; | |
5008 | if (symdef->file_offset == last) | |
5009 | { | |
5010 | included[i] = TRUE; | |
5011 | continue; | |
5012 | } | |
5013 | ||
8387904d AM |
5014 | h = archive_symbol_lookup (abfd, info, symdef->name); |
5015 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
5016 | goto error_return; | |
0ad989f9 L |
5017 | |
5018 | if (h == NULL) | |
5019 | continue; | |
5020 | ||
5021 | if (h->root.type == bfd_link_hash_common) | |
5022 | { | |
5023 | /* We currently have a common symbol. The archive map contains | |
5024 | a reference to this symbol, so we may want to include it. We | |
5025 | only want to include it however, if this archive element | |
5026 | contains a definition of the symbol, not just another common | |
5027 | declaration of it. | |
5028 | ||
5029 | Unfortunately some archivers (including GNU ar) will put | |
5030 | declarations of common symbols into their archive maps, as | |
5031 | well as real definitions, so we cannot just go by the archive | |
5032 | map alone. Instead we must read in the element's symbol | |
5033 | table and check that to see what kind of symbol definition | |
5034 | this is. */ | |
5035 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5036 | continue; | |
5037 | } | |
5038 | else if (h->root.type != bfd_link_hash_undefined) | |
5039 | { | |
5040 | if (h->root.type != bfd_link_hash_undefweak) | |
5041 | defined[i] = TRUE; | |
5042 | continue; | |
5043 | } | |
5044 | ||
5045 | /* We need to include this archive member. */ | |
5046 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5047 | if (element == NULL) | |
5048 | goto error_return; | |
5049 | ||
5050 | if (! bfd_check_format (element, bfd_object)) | |
5051 | goto error_return; | |
5052 | ||
5053 | /* Doublecheck that we have not included this object | |
5054 | already--it should be impossible, but there may be | |
5055 | something wrong with the archive. */ | |
5056 | if (element->archive_pass != 0) | |
5057 | { | |
5058 | bfd_set_error (bfd_error_bad_value); | |
5059 | goto error_return; | |
5060 | } | |
5061 | element->archive_pass = 1; | |
5062 | ||
5063 | undefs_tail = info->hash->undefs_tail; | |
5064 | ||
0e144ba7 AM |
5065 | if (!(*info->callbacks |
5066 | ->add_archive_element) (info, element, symdef->name, &element)) | |
0ad989f9 | 5067 | goto error_return; |
0e144ba7 | 5068 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5069 | goto error_return; |
5070 | ||
5071 | /* If there are any new undefined symbols, we need to make | |
5072 | another pass through the archive in order to see whether | |
5073 | they can be defined. FIXME: This isn't perfect, because | |
5074 | common symbols wind up on undefs_tail and because an | |
5075 | undefined symbol which is defined later on in this pass | |
5076 | does not require another pass. This isn't a bug, but it | |
5077 | does make the code less efficient than it could be. */ | |
5078 | if (undefs_tail != info->hash->undefs_tail) | |
5079 | loop = TRUE; | |
5080 | ||
5081 | /* Look backward to mark all symbols from this object file | |
5082 | which we have already seen in this pass. */ | |
5083 | mark = i; | |
5084 | do | |
5085 | { | |
5086 | included[mark] = TRUE; | |
5087 | if (mark == 0) | |
5088 | break; | |
5089 | --mark; | |
5090 | } | |
5091 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5092 | ||
5093 | /* We mark subsequent symbols from this object file as we go | |
5094 | on through the loop. */ | |
5095 | last = symdef->file_offset; | |
5096 | } | |
5097 | } | |
5098 | while (loop); | |
5099 | ||
5100 | free (defined); | |
5101 | free (included); | |
5102 | ||
5103 | return TRUE; | |
5104 | ||
5105 | error_return: | |
5106 | if (defined != NULL) | |
5107 | free (defined); | |
5108 | if (included != NULL) | |
5109 | free (included); | |
5110 | return FALSE; | |
5111 | } | |
4ad4eba5 AM |
5112 | |
5113 | /* Given an ELF BFD, add symbols to the global hash table as | |
5114 | appropriate. */ | |
5115 | ||
5116 | bfd_boolean | |
5117 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5118 | { | |
5119 | switch (bfd_get_format (abfd)) | |
5120 | { | |
5121 | case bfd_object: | |
5122 | return elf_link_add_object_symbols (abfd, info); | |
5123 | case bfd_archive: | |
5124 | return elf_link_add_archive_symbols (abfd, info); | |
5125 | default: | |
5126 | bfd_set_error (bfd_error_wrong_format); | |
5127 | return FALSE; | |
5128 | } | |
5129 | } | |
5a580b3a | 5130 | \f |
14b1c01e AM |
5131 | struct hash_codes_info |
5132 | { | |
5133 | unsigned long *hashcodes; | |
5134 | bfd_boolean error; | |
5135 | }; | |
a0c8462f | 5136 | |
5a580b3a AM |
5137 | /* This function will be called though elf_link_hash_traverse to store |
5138 | all hash value of the exported symbols in an array. */ | |
5139 | ||
5140 | static bfd_boolean | |
5141 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5142 | { | |
a50b1753 | 5143 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a AM |
5144 | const char *name; |
5145 | char *p; | |
5146 | unsigned long ha; | |
5147 | char *alc = NULL; | |
5148 | ||
5a580b3a AM |
5149 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5150 | if (h->dynindx == -1) | |
5151 | return TRUE; | |
5152 | ||
5153 | name = h->root.root.string; | |
5154 | p = strchr (name, ELF_VER_CHR); | |
5155 | if (p != NULL) | |
5156 | { | |
a50b1753 | 5157 | alc = (char *) bfd_malloc (p - name + 1); |
14b1c01e AM |
5158 | if (alc == NULL) |
5159 | { | |
5160 | inf->error = TRUE; | |
5161 | return FALSE; | |
5162 | } | |
5a580b3a AM |
5163 | memcpy (alc, name, p - name); |
5164 | alc[p - name] = '\0'; | |
5165 | name = alc; | |
5166 | } | |
5167 | ||
5168 | /* Compute the hash value. */ | |
5169 | ha = bfd_elf_hash (name); | |
5170 | ||
5171 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5172 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5173 | |
5174 | /* And store it in the struct so that we can put it in the hash table | |
5175 | later. */ | |
f6e332e6 | 5176 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5177 | |
5178 | if (alc != NULL) | |
5179 | free (alc); | |
5180 | ||
5181 | return TRUE; | |
5182 | } | |
5183 | ||
fdc90cb4 JJ |
5184 | struct collect_gnu_hash_codes |
5185 | { | |
5186 | bfd *output_bfd; | |
5187 | const struct elf_backend_data *bed; | |
5188 | unsigned long int nsyms; | |
5189 | unsigned long int maskbits; | |
5190 | unsigned long int *hashcodes; | |
5191 | unsigned long int *hashval; | |
5192 | unsigned long int *indx; | |
5193 | unsigned long int *counts; | |
5194 | bfd_vma *bitmask; | |
5195 | bfd_byte *contents; | |
5196 | long int min_dynindx; | |
5197 | unsigned long int bucketcount; | |
5198 | unsigned long int symindx; | |
5199 | long int local_indx; | |
5200 | long int shift1, shift2; | |
5201 | unsigned long int mask; | |
14b1c01e | 5202 | bfd_boolean error; |
fdc90cb4 JJ |
5203 | }; |
5204 | ||
5205 | /* This function will be called though elf_link_hash_traverse to store | |
5206 | all hash value of the exported symbols in an array. */ | |
5207 | ||
5208 | static bfd_boolean | |
5209 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5210 | { | |
a50b1753 | 5211 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5212 | const char *name; |
5213 | char *p; | |
5214 | unsigned long ha; | |
5215 | char *alc = NULL; | |
5216 | ||
fdc90cb4 JJ |
5217 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5218 | if (h->dynindx == -1) | |
5219 | return TRUE; | |
5220 | ||
5221 | /* Ignore also local symbols and undefined symbols. */ | |
5222 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5223 | return TRUE; | |
5224 | ||
5225 | name = h->root.root.string; | |
5226 | p = strchr (name, ELF_VER_CHR); | |
5227 | if (p != NULL) | |
5228 | { | |
a50b1753 | 5229 | alc = (char *) bfd_malloc (p - name + 1); |
14b1c01e AM |
5230 | if (alc == NULL) |
5231 | { | |
5232 | s->error = TRUE; | |
5233 | return FALSE; | |
5234 | } | |
fdc90cb4 JJ |
5235 | memcpy (alc, name, p - name); |
5236 | alc[p - name] = '\0'; | |
5237 | name = alc; | |
5238 | } | |
5239 | ||
5240 | /* Compute the hash value. */ | |
5241 | ha = bfd_elf_gnu_hash (name); | |
5242 | ||
5243 | /* Store the found hash value in the array for compute_bucket_count, | |
5244 | and also for .dynsym reordering purposes. */ | |
5245 | s->hashcodes[s->nsyms] = ha; | |
5246 | s->hashval[h->dynindx] = ha; | |
5247 | ++s->nsyms; | |
5248 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5249 | s->min_dynindx = h->dynindx; | |
5250 | ||
5251 | if (alc != NULL) | |
5252 | free (alc); | |
5253 | ||
5254 | return TRUE; | |
5255 | } | |
5256 | ||
5257 | /* This function will be called though elf_link_hash_traverse to do | |
5258 | final dynaminc symbol renumbering. */ | |
5259 | ||
5260 | static bfd_boolean | |
5261 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5262 | { | |
a50b1753 | 5263 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5264 | unsigned long int bucket; |
5265 | unsigned long int val; | |
5266 | ||
fdc90cb4 JJ |
5267 | /* Ignore indirect symbols. */ |
5268 | if (h->dynindx == -1) | |
5269 | return TRUE; | |
5270 | ||
5271 | /* Ignore also local symbols and undefined symbols. */ | |
5272 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5273 | { | |
5274 | if (h->dynindx >= s->min_dynindx) | |
5275 | h->dynindx = s->local_indx++; | |
5276 | return TRUE; | |
5277 | } | |
5278 | ||
5279 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5280 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5281 | & ((s->maskbits >> s->shift1) - 1); | |
5282 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5283 | s->bitmask[val] | |
5284 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5285 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5286 | if (s->counts[bucket] == 1) | |
5287 | /* Last element terminates the chain. */ | |
5288 | val |= 1; | |
5289 | bfd_put_32 (s->output_bfd, val, | |
5290 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5291 | --s->counts[bucket]; | |
5292 | h->dynindx = s->indx[bucket]++; | |
5293 | return TRUE; | |
5294 | } | |
5295 | ||
5296 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5297 | ||
5298 | bfd_boolean | |
5299 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5300 | { | |
5301 | return !(h->forced_local | |
5302 | || h->root.type == bfd_link_hash_undefined | |
5303 | || h->root.type == bfd_link_hash_undefweak | |
5304 | || ((h->root.type == bfd_link_hash_defined | |
5305 | || h->root.type == bfd_link_hash_defweak) | |
5306 | && h->root.u.def.section->output_section == NULL)); | |
5307 | } | |
5308 | ||
5a580b3a AM |
5309 | /* Array used to determine the number of hash table buckets to use |
5310 | based on the number of symbols there are. If there are fewer than | |
5311 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5312 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5313 | than 32771 buckets. */ | |
5314 | ||
5315 | static const size_t elf_buckets[] = | |
5316 | { | |
5317 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5318 | 16411, 32771, 0 | |
5319 | }; | |
5320 | ||
5321 | /* Compute bucket count for hashing table. We do not use a static set | |
5322 | of possible tables sizes anymore. Instead we determine for all | |
5323 | possible reasonable sizes of the table the outcome (i.e., the | |
5324 | number of collisions etc) and choose the best solution. The | |
5325 | weighting functions are not too simple to allow the table to grow | |
5326 | without bounds. Instead one of the weighting factors is the size. | |
5327 | Therefore the result is always a good payoff between few collisions | |
5328 | (= short chain lengths) and table size. */ | |
5329 | static size_t | |
b20dd2ce | 5330 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5331 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5332 | unsigned long int nsyms, | |
5333 | int gnu_hash) | |
5a580b3a | 5334 | { |
5a580b3a | 5335 | size_t best_size = 0; |
5a580b3a | 5336 | unsigned long int i; |
5a580b3a | 5337 | |
5a580b3a AM |
5338 | /* We have a problem here. The following code to optimize the table |
5339 | size requires an integer type with more the 32 bits. If | |
5340 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5341 | #ifdef BFD_HOST_U_64_BIT | |
5342 | if (info->optimize) | |
5343 | { | |
5a580b3a AM |
5344 | size_t minsize; |
5345 | size_t maxsize; | |
5346 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5347 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5348 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5349 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5350 | unsigned long int *counts; |
d40f3da9 | 5351 | bfd_size_type amt; |
0883b6e0 | 5352 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5353 | |
5354 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5355 | that the hashing table must at least have NSYMS/4 and at most | |
5356 | 2*NSYMS buckets. */ | |
5357 | minsize = nsyms / 4; | |
5358 | if (minsize == 0) | |
5359 | minsize = 1; | |
5360 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5361 | if (gnu_hash) |
5362 | { | |
5363 | if (minsize < 2) | |
5364 | minsize = 2; | |
5365 | if ((best_size & 31) == 0) | |
5366 | ++best_size; | |
5367 | } | |
5a580b3a AM |
5368 | |
5369 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5370 | since the size could be large. */ | |
5371 | amt = maxsize; | |
5372 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5373 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5374 | if (counts == NULL) |
fdc90cb4 | 5375 | return 0; |
5a580b3a AM |
5376 | |
5377 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5378 | minimal chain length. The minor criteria is (of course) the size | |
5379 | of the table. */ | |
5380 | for (i = minsize; i < maxsize; ++i) | |
5381 | { | |
5382 | /* Walk through the array of hashcodes and count the collisions. */ | |
5383 | BFD_HOST_U_64_BIT max; | |
5384 | unsigned long int j; | |
5385 | unsigned long int fact; | |
5386 | ||
fdc90cb4 JJ |
5387 | if (gnu_hash && (i & 31) == 0) |
5388 | continue; | |
5389 | ||
5a580b3a AM |
5390 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5391 | ||
5392 | /* Determine how often each hash bucket is used. */ | |
5393 | for (j = 0; j < nsyms; ++j) | |
5394 | ++counts[hashcodes[j] % i]; | |
5395 | ||
5396 | /* For the weight function we need some information about the | |
5397 | pagesize on the target. This is information need not be 100% | |
5398 | accurate. Since this information is not available (so far) we | |
5399 | define it here to a reasonable default value. If it is crucial | |
5400 | to have a better value some day simply define this value. */ | |
5401 | # ifndef BFD_TARGET_PAGESIZE | |
5402 | # define BFD_TARGET_PAGESIZE (4096) | |
5403 | # endif | |
5404 | ||
fdc90cb4 JJ |
5405 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5406 | and the chains. */ | |
5407 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5408 | |
5409 | # if 1 | |
5410 | /* Variant 1: optimize for short chains. We add the squares | |
5411 | of all the chain lengths (which favors many small chain | |
5412 | over a few long chains). */ | |
5413 | for (j = 0; j < i; ++j) | |
5414 | max += counts[j] * counts[j]; | |
5415 | ||
5416 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5417 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5418 | max *= fact * fact; |
5419 | # else | |
5420 | /* Variant 2: Optimize a lot more for small table. Here we | |
5421 | also add squares of the size but we also add penalties for | |
5422 | empty slots (the +1 term). */ | |
5423 | for (j = 0; j < i; ++j) | |
5424 | max += (1 + counts[j]) * (1 + counts[j]); | |
5425 | ||
5426 | /* The overall size of the table is considered, but not as | |
5427 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5428 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5429 | max *= fact; |
5430 | # endif | |
5431 | ||
5432 | /* Compare with current best results. */ | |
5433 | if (max < best_chlen) | |
5434 | { | |
5435 | best_chlen = max; | |
5436 | best_size = i; | |
0883b6e0 | 5437 | no_improvement_count = 0; |
5a580b3a | 5438 | } |
0883b6e0 NC |
5439 | /* PR 11843: Avoid futile long searches for the best bucket size |
5440 | when there are a large number of symbols. */ | |
5441 | else if (++no_improvement_count == 100) | |
5442 | break; | |
5a580b3a AM |
5443 | } |
5444 | ||
5445 | free (counts); | |
5446 | } | |
5447 | else | |
5448 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5449 | { | |
5450 | /* This is the fallback solution if no 64bit type is available or if we | |
5451 | are not supposed to spend much time on optimizations. We select the | |
5452 | bucket count using a fixed set of numbers. */ | |
5453 | for (i = 0; elf_buckets[i] != 0; i++) | |
5454 | { | |
5455 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5456 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5457 | break; |
5458 | } | |
fdc90cb4 JJ |
5459 | if (gnu_hash && best_size < 2) |
5460 | best_size = 2; | |
5a580b3a AM |
5461 | } |
5462 | ||
5a580b3a AM |
5463 | return best_size; |
5464 | } | |
5465 | ||
d0bf826b AM |
5466 | /* Size any SHT_GROUP section for ld -r. */ |
5467 | ||
5468 | bfd_boolean | |
5469 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
5470 | { | |
5471 | bfd *ibfd; | |
5472 | ||
5473 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
5474 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
5475 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) | |
5476 | return FALSE; | |
5477 | return TRUE; | |
5478 | } | |
5479 | ||
5a580b3a AM |
5480 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
5481 | called by the ELF linker emulation before_allocation routine. We | |
5482 | must set the sizes of the sections before the linker sets the | |
5483 | addresses of the various sections. */ | |
5484 | ||
5485 | bfd_boolean | |
5486 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5487 | const char *soname, | |
5488 | const char *rpath, | |
5489 | const char *filter_shlib, | |
7ee314fa AM |
5490 | const char *audit, |
5491 | const char *depaudit, | |
5a580b3a AM |
5492 | const char * const *auxiliary_filters, |
5493 | struct bfd_link_info *info, | |
fd91d419 | 5494 | asection **sinterpptr) |
5a580b3a AM |
5495 | { |
5496 | bfd_size_type soname_indx; | |
5497 | bfd *dynobj; | |
5498 | const struct elf_backend_data *bed; | |
28caa186 | 5499 | struct elf_info_failed asvinfo; |
5a580b3a AM |
5500 | |
5501 | *sinterpptr = NULL; | |
5502 | ||
5503 | soname_indx = (bfd_size_type) -1; | |
5504 | ||
5505 | if (!is_elf_hash_table (info->hash)) | |
5506 | return TRUE; | |
5507 | ||
6bfdb61b | 5508 | bed = get_elf_backend_data (output_bfd); |
5a580b3a AM |
5509 | if (info->execstack) |
5510 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
5511 | else if (info->noexecstack) | |
5512 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
5513 | else | |
5514 | { | |
5515 | bfd *inputobj; | |
5516 | asection *notesec = NULL; | |
5517 | int exec = 0; | |
5518 | ||
5519 | for (inputobj = info->input_bfds; | |
5520 | inputobj; | |
5521 | inputobj = inputobj->link_next) | |
5522 | { | |
5523 | asection *s; | |
5524 | ||
a92c088a L |
5525 | if (inputobj->flags |
5526 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
5a580b3a AM |
5527 | continue; |
5528 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5529 | if (s) | |
5530 | { | |
5531 | if (s->flags & SEC_CODE) | |
5532 | exec = PF_X; | |
5533 | notesec = s; | |
5534 | } | |
6bfdb61b | 5535 | else if (bed->default_execstack) |
5a580b3a AM |
5536 | exec = PF_X; |
5537 | } | |
5538 | if (notesec) | |
5539 | { | |
5540 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
5541 | if (exec && info->relocatable | |
5542 | && notesec->output_section != bfd_abs_section_ptr) | |
5543 | notesec->output_section->flags |= SEC_CODE; | |
5544 | } | |
5545 | } | |
5546 | ||
5547 | /* Any syms created from now on start with -1 in | |
5548 | got.refcount/offset and plt.refcount/offset. */ | |
a6aa5195 AM |
5549 | elf_hash_table (info)->init_got_refcount |
5550 | = elf_hash_table (info)->init_got_offset; | |
5551 | elf_hash_table (info)->init_plt_refcount | |
5552 | = elf_hash_table (info)->init_plt_offset; | |
5a580b3a | 5553 | |
d0bf826b AM |
5554 | if (info->relocatable |
5555 | && !_bfd_elf_size_group_sections (info)) | |
5556 | return FALSE; | |
5557 | ||
5a580b3a AM |
5558 | /* The backend may have to create some sections regardless of whether |
5559 | we're dynamic or not. */ | |
5a580b3a AM |
5560 | if (bed->elf_backend_always_size_sections |
5561 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5562 | return FALSE; | |
5563 | ||
eb3d5f3b JB |
5564 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
5565 | return FALSE; | |
5566 | ||
5a580b3a AM |
5567 | dynobj = elf_hash_table (info)->dynobj; |
5568 | ||
5569 | /* If there were no dynamic objects in the link, there is nothing to | |
5570 | do here. */ | |
5571 | if (dynobj == NULL) | |
5572 | return TRUE; | |
5573 | ||
5a580b3a AM |
5574 | if (elf_hash_table (info)->dynamic_sections_created) |
5575 | { | |
5576 | struct elf_info_failed eif; | |
5577 | struct elf_link_hash_entry *h; | |
5578 | asection *dynstr; | |
5579 | struct bfd_elf_version_tree *t; | |
5580 | struct bfd_elf_version_expr *d; | |
046183de | 5581 | asection *s; |
5a580b3a AM |
5582 | bfd_boolean all_defined; |
5583 | ||
5584 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
5585 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
5586 | ||
5587 | if (soname != NULL) | |
5588 | { | |
5589 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5590 | soname, TRUE); | |
5591 | if (soname_indx == (bfd_size_type) -1 | |
5592 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5593 | return FALSE; | |
5594 | } | |
5595 | ||
5596 | if (info->symbolic) | |
5597 | { | |
5598 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5599 | return FALSE; | |
5600 | info->flags |= DF_SYMBOLIC; | |
5601 | } | |
5602 | ||
5603 | if (rpath != NULL) | |
5604 | { | |
5605 | bfd_size_type indx; | |
5606 | ||
5607 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5608 | TRUE); | |
5609 | if (indx == (bfd_size_type) -1 | |
5610 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
5611 | return FALSE; | |
5612 | ||
5613 | if (info->new_dtags) | |
5614 | { | |
5615 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
5616 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
5617 | return FALSE; | |
5618 | } | |
5619 | } | |
5620 | ||
5621 | if (filter_shlib != NULL) | |
5622 | { | |
5623 | bfd_size_type indx; | |
5624 | ||
5625 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5626 | filter_shlib, TRUE); | |
5627 | if (indx == (bfd_size_type) -1 | |
5628 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5629 | return FALSE; | |
5630 | } | |
5631 | ||
5632 | if (auxiliary_filters != NULL) | |
5633 | { | |
5634 | const char * const *p; | |
5635 | ||
5636 | for (p = auxiliary_filters; *p != NULL; p++) | |
5637 | { | |
5638 | bfd_size_type indx; | |
5639 | ||
5640 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5641 | *p, TRUE); | |
5642 | if (indx == (bfd_size_type) -1 | |
5643 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5644 | return FALSE; | |
5645 | } | |
5646 | } | |
5647 | ||
7ee314fa AM |
5648 | if (audit != NULL) |
5649 | { | |
5650 | bfd_size_type indx; | |
5651 | ||
5652 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
5653 | TRUE); | |
5654 | if (indx == (bfd_size_type) -1 | |
5655 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
5656 | return FALSE; | |
5657 | } | |
5658 | ||
5659 | if (depaudit != NULL) | |
5660 | { | |
5661 | bfd_size_type indx; | |
5662 | ||
5663 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
5664 | TRUE); | |
5665 | if (indx == (bfd_size_type) -1 | |
5666 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
5667 | return FALSE; | |
5668 | } | |
5669 | ||
5a580b3a | 5670 | eif.info = info; |
5a580b3a AM |
5671 | eif.failed = FALSE; |
5672 | ||
5673 | /* If we are supposed to export all symbols into the dynamic symbol | |
5674 | table (this is not the normal case), then do so. */ | |
55255dae L |
5675 | if (info->export_dynamic |
5676 | || (info->executable && info->dynamic)) | |
5a580b3a AM |
5677 | { |
5678 | elf_link_hash_traverse (elf_hash_table (info), | |
5679 | _bfd_elf_export_symbol, | |
5680 | &eif); | |
5681 | if (eif.failed) | |
5682 | return FALSE; | |
5683 | } | |
5684 | ||
5685 | /* Make all global versions with definition. */ | |
fd91d419 | 5686 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 5687 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 5688 | if (!d->symver && d->literal) |
5a580b3a AM |
5689 | { |
5690 | const char *verstr, *name; | |
5691 | size_t namelen, verlen, newlen; | |
93252b1c | 5692 | char *newname, *p, leading_char; |
5a580b3a AM |
5693 | struct elf_link_hash_entry *newh; |
5694 | ||
93252b1c | 5695 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 5696 | name = d->pattern; |
93252b1c | 5697 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
5698 | verstr = t->name; |
5699 | verlen = strlen (verstr); | |
5700 | newlen = namelen + verlen + 3; | |
5701 | ||
a50b1753 | 5702 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
5703 | if (newname == NULL) |
5704 | return FALSE; | |
93252b1c MF |
5705 | newname[0] = leading_char; |
5706 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
5707 | |
5708 | /* Check the hidden versioned definition. */ | |
5709 | p = newname + namelen; | |
5710 | *p++ = ELF_VER_CHR; | |
5711 | memcpy (p, verstr, verlen + 1); | |
5712 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5713 | newname, FALSE, FALSE, | |
5714 | FALSE); | |
5715 | if (newh == NULL | |
5716 | || (newh->root.type != bfd_link_hash_defined | |
5717 | && newh->root.type != bfd_link_hash_defweak)) | |
5718 | { | |
5719 | /* Check the default versioned definition. */ | |
5720 | *p++ = ELF_VER_CHR; | |
5721 | memcpy (p, verstr, verlen + 1); | |
5722 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5723 | newname, FALSE, FALSE, | |
5724 | FALSE); | |
5725 | } | |
5726 | free (newname); | |
5727 | ||
5728 | /* Mark this version if there is a definition and it is | |
5729 | not defined in a shared object. */ | |
5730 | if (newh != NULL | |
f5385ebf | 5731 | && !newh->def_dynamic |
5a580b3a AM |
5732 | && (newh->root.type == bfd_link_hash_defined |
5733 | || newh->root.type == bfd_link_hash_defweak)) | |
5734 | d->symver = 1; | |
5735 | } | |
5736 | ||
5737 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 5738 | asvinfo.info = info; |
5a580b3a AM |
5739 | asvinfo.failed = FALSE; |
5740 | ||
5741 | elf_link_hash_traverse (elf_hash_table (info), | |
5742 | _bfd_elf_link_assign_sym_version, | |
5743 | &asvinfo); | |
5744 | if (asvinfo.failed) | |
5745 | return FALSE; | |
5746 | ||
5747 | if (!info->allow_undefined_version) | |
5748 | { | |
5749 | /* Check if all global versions have a definition. */ | |
5750 | all_defined = TRUE; | |
fd91d419 | 5751 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 5752 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 5753 | if (d->literal && !d->symver && !d->script) |
5a580b3a AM |
5754 | { |
5755 | (*_bfd_error_handler) | |
5756 | (_("%s: undefined version: %s"), | |
5757 | d->pattern, t->name); | |
5758 | all_defined = FALSE; | |
5759 | } | |
5760 | ||
5761 | if (!all_defined) | |
5762 | { | |
5763 | bfd_set_error (bfd_error_bad_value); | |
5764 | return FALSE; | |
5765 | } | |
5766 | } | |
5767 | ||
5768 | /* Find all symbols which were defined in a dynamic object and make | |
5769 | the backend pick a reasonable value for them. */ | |
5770 | elf_link_hash_traverse (elf_hash_table (info), | |
5771 | _bfd_elf_adjust_dynamic_symbol, | |
5772 | &eif); | |
5773 | if (eif.failed) | |
5774 | return FALSE; | |
5775 | ||
5776 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5777 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5778 | now so that we know the final size of the .dynamic section. */ |
5779 | ||
5780 | /* If there are initialization and/or finalization functions to | |
5781 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5782 | h = (info->init_function | |
5783 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5784 | info->init_function, FALSE, | |
5785 | FALSE, FALSE) | |
5786 | : NULL); | |
5787 | if (h != NULL | |
f5385ebf AM |
5788 | && (h->ref_regular |
5789 | || h->def_regular)) | |
5a580b3a AM |
5790 | { |
5791 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5792 | return FALSE; | |
5793 | } | |
5794 | h = (info->fini_function | |
5795 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5796 | info->fini_function, FALSE, | |
5797 | FALSE, FALSE) | |
5798 | : NULL); | |
5799 | if (h != NULL | |
f5385ebf AM |
5800 | && (h->ref_regular |
5801 | || h->def_regular)) | |
5a580b3a AM |
5802 | { |
5803 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5804 | return FALSE; | |
5805 | } | |
5806 | ||
046183de AM |
5807 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5808 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5809 | { |
5810 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5811 | if (! info->executable) | |
5812 | { | |
5813 | bfd *sub; | |
5814 | asection *o; | |
5815 | ||
5816 | for (sub = info->input_bfds; sub != NULL; | |
5817 | sub = sub->link_next) | |
3fcd97f1 JJ |
5818 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
5819 | for (o = sub->sections; o != NULL; o = o->next) | |
5820 | if (elf_section_data (o)->this_hdr.sh_type | |
5821 | == SHT_PREINIT_ARRAY) | |
5822 | { | |
5823 | (*_bfd_error_handler) | |
5824 | (_("%B: .preinit_array section is not allowed in DSO"), | |
5825 | sub); | |
5826 | break; | |
5827 | } | |
5a580b3a AM |
5828 | |
5829 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5830 | return FALSE; | |
5831 | } | |
5832 | ||
5833 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5834 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5835 | return FALSE; | |
5836 | } | |
046183de AM |
5837 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5838 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5839 | { |
5840 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5841 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5842 | return FALSE; | |
5843 | } | |
046183de AM |
5844 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5845 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5846 | { |
5847 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5848 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5849 | return FALSE; | |
5850 | } | |
5851 | ||
5852 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5853 | /* If .dynstr is excluded from the link, we don't want any of | |
5854 | these tags. Strictly, we should be checking each section | |
5855 | individually; This quick check covers for the case where | |
5856 | someone does a /DISCARD/ : { *(*) }. */ | |
5857 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5858 | { | |
5859 | bfd_size_type strsize; | |
5860 | ||
5861 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
5862 | if ((info->emit_hash |
5863 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
5864 | || (info->emit_gnu_hash | |
5865 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
5866 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
5867 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5868 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5869 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5870 | bed->s->sizeof_sym)) | |
5871 | return FALSE; | |
5872 | } | |
5873 | } | |
5874 | ||
5875 | /* The backend must work out the sizes of all the other dynamic | |
5876 | sections. */ | |
5877 | if (bed->elf_backend_size_dynamic_sections | |
5878 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5879 | return FALSE; | |
5880 | ||
5881 | if (elf_hash_table (info)->dynamic_sections_created) | |
5882 | { | |
554220db | 5883 | unsigned long section_sym_count; |
fd91d419 | 5884 | struct bfd_elf_version_tree *verdefs; |
5a580b3a | 5885 | asection *s; |
5a580b3a AM |
5886 | |
5887 | /* Set up the version definition section. */ | |
5888 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5889 | BFD_ASSERT (s != NULL); | |
5890 | ||
5891 | /* We may have created additional version definitions if we are | |
5892 | just linking a regular application. */ | |
fd91d419 | 5893 | verdefs = info->version_info; |
5a580b3a AM |
5894 | |
5895 | /* Skip anonymous version tag. */ | |
5896 | if (verdefs != NULL && verdefs->vernum == 0) | |
5897 | verdefs = verdefs->next; | |
5898 | ||
3e3b46e5 | 5899 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5900 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5901 | else |
5902 | { | |
5903 | unsigned int cdefs; | |
5904 | bfd_size_type size; | |
5905 | struct bfd_elf_version_tree *t; | |
5906 | bfd_byte *p; | |
5907 | Elf_Internal_Verdef def; | |
5908 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5909 | struct bfd_link_hash_entry *bh; |
5910 | struct elf_link_hash_entry *h; | |
5911 | const char *name; | |
5a580b3a AM |
5912 | |
5913 | cdefs = 0; | |
5914 | size = 0; | |
5915 | ||
5916 | /* Make space for the base version. */ | |
5917 | size += sizeof (Elf_External_Verdef); | |
5918 | size += sizeof (Elf_External_Verdaux); | |
5919 | ++cdefs; | |
5920 | ||
3e3b46e5 PB |
5921 | /* Make space for the default version. */ |
5922 | if (info->create_default_symver) | |
5923 | { | |
5924 | size += sizeof (Elf_External_Verdef); | |
5925 | ++cdefs; | |
5926 | } | |
5927 | ||
5a580b3a AM |
5928 | for (t = verdefs; t != NULL; t = t->next) |
5929 | { | |
5930 | struct bfd_elf_version_deps *n; | |
5931 | ||
a6cc6b3b RO |
5932 | /* Don't emit base version twice. */ |
5933 | if (t->vernum == 0) | |
5934 | continue; | |
5935 | ||
5a580b3a AM |
5936 | size += sizeof (Elf_External_Verdef); |
5937 | size += sizeof (Elf_External_Verdaux); | |
5938 | ++cdefs; | |
5939 | ||
5940 | for (n = t->deps; n != NULL; n = n->next) | |
5941 | size += sizeof (Elf_External_Verdaux); | |
5942 | } | |
5943 | ||
eea6121a | 5944 | s->size = size; |
a50b1753 | 5945 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 5946 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
5947 | return FALSE; |
5948 | ||
5949 | /* Fill in the version definition section. */ | |
5950 | ||
5951 | p = s->contents; | |
5952 | ||
5953 | def.vd_version = VER_DEF_CURRENT; | |
5954 | def.vd_flags = VER_FLG_BASE; | |
5955 | def.vd_ndx = 1; | |
5956 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5957 | if (info->create_default_symver) |
5958 | { | |
5959 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5960 | def.vd_next = sizeof (Elf_External_Verdef); | |
5961 | } | |
5962 | else | |
5963 | { | |
5964 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5965 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5966 | + sizeof (Elf_External_Verdaux)); | |
5967 | } | |
5a580b3a AM |
5968 | |
5969 | if (soname_indx != (bfd_size_type) -1) | |
5970 | { | |
5971 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5972 | soname_indx); | |
5973 | def.vd_hash = bfd_elf_hash (soname); | |
5974 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5975 | name = soname; |
5a580b3a AM |
5976 | } |
5977 | else | |
5978 | { | |
5a580b3a AM |
5979 | bfd_size_type indx; |
5980 | ||
06084812 | 5981 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
5982 | def.vd_hash = bfd_elf_hash (name); |
5983 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5984 | name, FALSE); | |
5985 | if (indx == (bfd_size_type) -1) | |
5986 | return FALSE; | |
5987 | defaux.vda_name = indx; | |
5988 | } | |
5989 | defaux.vda_next = 0; | |
5990 | ||
5991 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5992 | (Elf_External_Verdef *) p); | |
5993 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5994 | if (info->create_default_symver) |
5995 | { | |
5996 | /* Add a symbol representing this version. */ | |
5997 | bh = NULL; | |
5998 | if (! (_bfd_generic_link_add_one_symbol | |
5999 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6000 | 0, NULL, FALSE, | |
6001 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6002 | return FALSE; | |
6003 | h = (struct elf_link_hash_entry *) bh; | |
6004 | h->non_elf = 0; | |
6005 | h->def_regular = 1; | |
6006 | h->type = STT_OBJECT; | |
6007 | h->verinfo.vertree = NULL; | |
6008 | ||
6009 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6010 | return FALSE; | |
6011 | ||
6012 | /* Create a duplicate of the base version with the same | |
6013 | aux block, but different flags. */ | |
6014 | def.vd_flags = 0; | |
6015 | def.vd_ndx = 2; | |
6016 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6017 | if (verdefs) | |
6018 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6019 | + sizeof (Elf_External_Verdaux)); | |
6020 | else | |
6021 | def.vd_next = 0; | |
6022 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6023 | (Elf_External_Verdef *) p); | |
6024 | p += sizeof (Elf_External_Verdef); | |
6025 | } | |
5a580b3a AM |
6026 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6027 | (Elf_External_Verdaux *) p); | |
6028 | p += sizeof (Elf_External_Verdaux); | |
6029 | ||
6030 | for (t = verdefs; t != NULL; t = t->next) | |
6031 | { | |
6032 | unsigned int cdeps; | |
6033 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6034 | |
a6cc6b3b RO |
6035 | /* Don't emit the base version twice. */ |
6036 | if (t->vernum == 0) | |
6037 | continue; | |
6038 | ||
5a580b3a AM |
6039 | cdeps = 0; |
6040 | for (n = t->deps; n != NULL; n = n->next) | |
6041 | ++cdeps; | |
6042 | ||
6043 | /* Add a symbol representing this version. */ | |
6044 | bh = NULL; | |
6045 | if (! (_bfd_generic_link_add_one_symbol | |
6046 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6047 | 0, NULL, FALSE, | |
6048 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6049 | return FALSE; | |
6050 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6051 | h->non_elf = 0; |
6052 | h->def_regular = 1; | |
5a580b3a AM |
6053 | h->type = STT_OBJECT; |
6054 | h->verinfo.vertree = t; | |
6055 | ||
c152c796 | 6056 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6057 | return FALSE; |
6058 | ||
6059 | def.vd_version = VER_DEF_CURRENT; | |
6060 | def.vd_flags = 0; | |
6061 | if (t->globals.list == NULL | |
6062 | && t->locals.list == NULL | |
6063 | && ! t->used) | |
6064 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6065 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6066 | def.vd_cnt = cdeps + 1; |
6067 | def.vd_hash = bfd_elf_hash (t->name); | |
6068 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6069 | def.vd_next = 0; | |
a6cc6b3b RO |
6070 | |
6071 | /* If a basever node is next, it *must* be the last node in | |
6072 | the chain, otherwise Verdef construction breaks. */ | |
6073 | if (t->next != NULL && t->next->vernum == 0) | |
6074 | BFD_ASSERT (t->next->next == NULL); | |
6075 | ||
6076 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6077 | def.vd_next = (sizeof (Elf_External_Verdef) |
6078 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6079 | ||
6080 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6081 | (Elf_External_Verdef *) p); | |
6082 | p += sizeof (Elf_External_Verdef); | |
6083 | ||
6084 | defaux.vda_name = h->dynstr_index; | |
6085 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6086 | h->dynstr_index); | |
6087 | defaux.vda_next = 0; | |
6088 | if (t->deps != NULL) | |
6089 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6090 | t->name_indx = defaux.vda_name; | |
6091 | ||
6092 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6093 | (Elf_External_Verdaux *) p); | |
6094 | p += sizeof (Elf_External_Verdaux); | |
6095 | ||
6096 | for (n = t->deps; n != NULL; n = n->next) | |
6097 | { | |
6098 | if (n->version_needed == NULL) | |
6099 | { | |
6100 | /* This can happen if there was an error in the | |
6101 | version script. */ | |
6102 | defaux.vda_name = 0; | |
6103 | } | |
6104 | else | |
6105 | { | |
6106 | defaux.vda_name = n->version_needed->name_indx; | |
6107 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6108 | defaux.vda_name); | |
6109 | } | |
6110 | if (n->next == NULL) | |
6111 | defaux.vda_next = 0; | |
6112 | else | |
6113 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6114 | ||
6115 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6116 | (Elf_External_Verdaux *) p); | |
6117 | p += sizeof (Elf_External_Verdaux); | |
6118 | } | |
6119 | } | |
6120 | ||
6121 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6122 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
6123 | return FALSE; | |
6124 | ||
6125 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
6126 | } | |
6127 | ||
6128 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6129 | { | |
6130 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6131 | return FALSE; | |
6132 | } | |
6133 | else if (info->flags & DF_BIND_NOW) | |
6134 | { | |
6135 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6136 | return FALSE; | |
6137 | } | |
6138 | ||
6139 | if (info->flags_1) | |
6140 | { | |
6141 | if (info->executable) | |
6142 | info->flags_1 &= ~ (DF_1_INITFIRST | |
6143 | | DF_1_NODELETE | |
6144 | | DF_1_NOOPEN); | |
6145 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6146 | return FALSE; | |
6147 | } | |
6148 | ||
6149 | /* Work out the size of the version reference section. */ | |
6150 | ||
6151 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
6152 | BFD_ASSERT (s != NULL); | |
6153 | { | |
6154 | struct elf_find_verdep_info sinfo; | |
6155 | ||
5a580b3a AM |
6156 | sinfo.info = info; |
6157 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6158 | if (sinfo.vers == 0) | |
6159 | sinfo.vers = 1; | |
6160 | sinfo.failed = FALSE; | |
6161 | ||
6162 | elf_link_hash_traverse (elf_hash_table (info), | |
6163 | _bfd_elf_link_find_version_dependencies, | |
6164 | &sinfo); | |
14b1c01e AM |
6165 | if (sinfo.failed) |
6166 | return FALSE; | |
5a580b3a AM |
6167 | |
6168 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 6169 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6170 | else |
6171 | { | |
6172 | Elf_Internal_Verneed *t; | |
6173 | unsigned int size; | |
6174 | unsigned int crefs; | |
6175 | bfd_byte *p; | |
6176 | ||
a6cc6b3b | 6177 | /* Build the version dependency section. */ |
5a580b3a AM |
6178 | size = 0; |
6179 | crefs = 0; | |
6180 | for (t = elf_tdata (output_bfd)->verref; | |
6181 | t != NULL; | |
6182 | t = t->vn_nextref) | |
6183 | { | |
6184 | Elf_Internal_Vernaux *a; | |
6185 | ||
6186 | size += sizeof (Elf_External_Verneed); | |
6187 | ++crefs; | |
6188 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6189 | size += sizeof (Elf_External_Vernaux); | |
6190 | } | |
6191 | ||
eea6121a | 6192 | s->size = size; |
a50b1753 | 6193 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
5a580b3a AM |
6194 | if (s->contents == NULL) |
6195 | return FALSE; | |
6196 | ||
6197 | p = s->contents; | |
6198 | for (t = elf_tdata (output_bfd)->verref; | |
6199 | t != NULL; | |
6200 | t = t->vn_nextref) | |
6201 | { | |
6202 | unsigned int caux; | |
6203 | Elf_Internal_Vernaux *a; | |
6204 | bfd_size_type indx; | |
6205 | ||
6206 | caux = 0; | |
6207 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6208 | ++caux; | |
6209 | ||
6210 | t->vn_version = VER_NEED_CURRENT; | |
6211 | t->vn_cnt = caux; | |
6212 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6213 | elf_dt_name (t->vn_bfd) != NULL | |
6214 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 6215 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
6216 | FALSE); |
6217 | if (indx == (bfd_size_type) -1) | |
6218 | return FALSE; | |
6219 | t->vn_file = indx; | |
6220 | t->vn_aux = sizeof (Elf_External_Verneed); | |
6221 | if (t->vn_nextref == NULL) | |
6222 | t->vn_next = 0; | |
6223 | else | |
6224 | t->vn_next = (sizeof (Elf_External_Verneed) | |
6225 | + caux * sizeof (Elf_External_Vernaux)); | |
6226 | ||
6227 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
6228 | (Elf_External_Verneed *) p); | |
6229 | p += sizeof (Elf_External_Verneed); | |
6230 | ||
6231 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6232 | { | |
6233 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6234 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6235 | a->vna_nodename, FALSE); | |
6236 | if (indx == (bfd_size_type) -1) | |
6237 | return FALSE; | |
6238 | a->vna_name = indx; | |
6239 | if (a->vna_nextptr == NULL) | |
6240 | a->vna_next = 0; | |
6241 | else | |
6242 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6243 | ||
6244 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6245 | (Elf_External_Vernaux *) p); | |
6246 | p += sizeof (Elf_External_Vernaux); | |
6247 | } | |
6248 | } | |
6249 | ||
6250 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6251 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6252 | return FALSE; | |
6253 | ||
6254 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6255 | } | |
6256 | } | |
6257 | ||
8423293d AM |
6258 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6259 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6260 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6261 | §ion_sym_count) == 0) | |
6262 | { | |
6263 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6264 | s->flags |= SEC_EXCLUDE; | |
6265 | } | |
6266 | } | |
6267 | return TRUE; | |
6268 | } | |
6269 | ||
74541ad4 AM |
6270 | /* Find the first non-excluded output section. We'll use its |
6271 | section symbol for some emitted relocs. */ | |
6272 | void | |
6273 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6274 | { | |
6275 | asection *s; | |
6276 | ||
6277 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6278 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6279 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6280 | { | |
6281 | elf_hash_table (info)->text_index_section = s; | |
6282 | break; | |
6283 | } | |
6284 | } | |
6285 | ||
6286 | /* Find two non-excluded output sections, one for code, one for data. | |
6287 | We'll use their section symbols for some emitted relocs. */ | |
6288 | void | |
6289 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6290 | { | |
6291 | asection *s; | |
6292 | ||
266b05cf DJ |
6293 | /* Data first, since setting text_index_section changes |
6294 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6295 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6296 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
74541ad4 AM |
6297 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6298 | { | |
266b05cf | 6299 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6300 | break; |
6301 | } | |
6302 | ||
6303 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6304 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6305 | == (SEC_ALLOC | SEC_READONLY)) | |
74541ad4 AM |
6306 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6307 | { | |
266b05cf | 6308 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6309 | break; |
6310 | } | |
6311 | ||
6312 | if (elf_hash_table (info)->text_index_section == NULL) | |
6313 | elf_hash_table (info)->text_index_section | |
6314 | = elf_hash_table (info)->data_index_section; | |
6315 | } | |
6316 | ||
8423293d AM |
6317 | bfd_boolean |
6318 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6319 | { | |
74541ad4 AM |
6320 | const struct elf_backend_data *bed; |
6321 | ||
8423293d AM |
6322 | if (!is_elf_hash_table (info->hash)) |
6323 | return TRUE; | |
6324 | ||
74541ad4 AM |
6325 | bed = get_elf_backend_data (output_bfd); |
6326 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6327 | ||
8423293d AM |
6328 | if (elf_hash_table (info)->dynamic_sections_created) |
6329 | { | |
6330 | bfd *dynobj; | |
8423293d AM |
6331 | asection *s; |
6332 | bfd_size_type dynsymcount; | |
6333 | unsigned long section_sym_count; | |
8423293d AM |
6334 | unsigned int dtagcount; |
6335 | ||
6336 | dynobj = elf_hash_table (info)->dynobj; | |
6337 | ||
5a580b3a AM |
6338 | /* Assign dynsym indicies. In a shared library we generate a |
6339 | section symbol for each output section, which come first. | |
6340 | Next come all of the back-end allocated local dynamic syms, | |
6341 | followed by the rest of the global symbols. */ | |
6342 | ||
554220db AM |
6343 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6344 | §ion_sym_count); | |
5a580b3a AM |
6345 | |
6346 | /* Work out the size of the symbol version section. */ | |
6347 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
6348 | BFD_ASSERT (s != NULL); | |
8423293d AM |
6349 | if (dynsymcount != 0 |
6350 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 6351 | { |
eea6121a | 6352 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 6353 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
6354 | if (s->contents == NULL) |
6355 | return FALSE; | |
6356 | ||
6357 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6358 | return FALSE; | |
6359 | } | |
6360 | ||
6361 | /* Set the size of the .dynsym and .hash sections. We counted | |
6362 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6363 | We will build the contents of .dynsym and .hash when we build | |
6364 | the final symbol table, because until then we do not know the | |
6365 | correct value to give the symbols. We built the .dynstr | |
6366 | section as we went along in elf_link_add_object_symbols. */ | |
6367 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
6368 | BFD_ASSERT (s != NULL); | |
eea6121a | 6369 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
6370 | |
6371 | if (dynsymcount != 0) | |
6372 | { | |
a50b1753 | 6373 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
554220db AM |
6374 | if (s->contents == NULL) |
6375 | return FALSE; | |
5a580b3a | 6376 | |
554220db AM |
6377 | /* The first entry in .dynsym is a dummy symbol. |
6378 | Clear all the section syms, in case we don't output them all. */ | |
6379 | ++section_sym_count; | |
6380 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
6381 | } |
6382 | ||
fdc90cb4 JJ |
6383 | elf_hash_table (info)->bucketcount = 0; |
6384 | ||
5a580b3a AM |
6385 | /* Compute the size of the hashing table. As a side effect this |
6386 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6387 | if (info->emit_hash) |
6388 | { | |
6389 | unsigned long int *hashcodes; | |
14b1c01e | 6390 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
6391 | bfd_size_type amt; |
6392 | unsigned long int nsyms; | |
6393 | size_t bucketcount; | |
6394 | size_t hash_entry_size; | |
6395 | ||
6396 | /* Compute the hash values for all exported symbols. At the same | |
6397 | time store the values in an array so that we could use them for | |
6398 | optimizations. */ | |
6399 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 6400 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6401 | if (hashcodes == NULL) |
6402 | return FALSE; | |
14b1c01e AM |
6403 | hashinf.hashcodes = hashcodes; |
6404 | hashinf.error = FALSE; | |
5a580b3a | 6405 | |
fdc90cb4 JJ |
6406 | /* Put all hash values in HASHCODES. */ |
6407 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
6408 | elf_collect_hash_codes, &hashinf); |
6409 | if (hashinf.error) | |
4dd07732 AM |
6410 | { |
6411 | free (hashcodes); | |
6412 | return FALSE; | |
6413 | } | |
5a580b3a | 6414 | |
14b1c01e | 6415 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
6416 | bucketcount |
6417 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6418 | free (hashcodes); | |
6419 | ||
6420 | if (bucketcount == 0) | |
6421 | return FALSE; | |
5a580b3a | 6422 | |
fdc90cb4 JJ |
6423 | elf_hash_table (info)->bucketcount = bucketcount; |
6424 | ||
6425 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
6426 | BFD_ASSERT (s != NULL); | |
6427 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6428 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 6429 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6430 | if (s->contents == NULL) |
6431 | return FALSE; | |
6432 | ||
6433 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6434 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6435 | s->contents + hash_entry_size); | |
6436 | } | |
6437 | ||
6438 | if (info->emit_gnu_hash) | |
6439 | { | |
6440 | size_t i, cnt; | |
6441 | unsigned char *contents; | |
6442 | struct collect_gnu_hash_codes cinfo; | |
6443 | bfd_size_type amt; | |
6444 | size_t bucketcount; | |
6445 | ||
6446 | memset (&cinfo, 0, sizeof (cinfo)); | |
6447 | ||
6448 | /* Compute the hash values for all exported symbols. At the same | |
6449 | time store the values in an array so that we could use them for | |
6450 | optimizations. */ | |
6451 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 6452 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6453 | if (cinfo.hashcodes == NULL) |
6454 | return FALSE; | |
6455 | ||
6456 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6457 | cinfo.min_dynindx = -1; | |
6458 | cinfo.output_bfd = output_bfd; | |
6459 | cinfo.bed = bed; | |
6460 | ||
6461 | /* Put all hash values in HASHCODES. */ | |
6462 | elf_link_hash_traverse (elf_hash_table (info), | |
6463 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 6464 | if (cinfo.error) |
4dd07732 AM |
6465 | { |
6466 | free (cinfo.hashcodes); | |
6467 | return FALSE; | |
6468 | } | |
fdc90cb4 JJ |
6469 | |
6470 | bucketcount | |
6471 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6472 | ||
6473 | if (bucketcount == 0) | |
6474 | { | |
6475 | free (cinfo.hashcodes); | |
6476 | return FALSE; | |
6477 | } | |
6478 | ||
6479 | s = bfd_get_section_by_name (dynobj, ".gnu.hash"); | |
6480 | BFD_ASSERT (s != NULL); | |
6481 | ||
6482 | if (cinfo.nsyms == 0) | |
6483 | { | |
6484 | /* Empty .gnu.hash section is special. */ | |
6485 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6486 | free (cinfo.hashcodes); | |
6487 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 6488 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6489 | if (contents == NULL) |
6490 | return FALSE; | |
6491 | s->contents = contents; | |
6492 | /* 1 empty bucket. */ | |
6493 | bfd_put_32 (output_bfd, 1, contents); | |
6494 | /* SYMIDX above the special symbol 0. */ | |
6495 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6496 | /* Just one word for bitmask. */ | |
6497 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6498 | /* Only hash fn bloom filter. */ | |
6499 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6500 | /* No hashes are valid - empty bitmask. */ | |
6501 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6502 | /* No hashes in the only bucket. */ | |
6503 | bfd_put_32 (output_bfd, 0, | |
6504 | contents + 16 + bed->s->arch_size / 8); | |
6505 | } | |
6506 | else | |
6507 | { | |
9e6619e2 | 6508 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 6509 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 6510 | |
9e6619e2 AM |
6511 | x = cinfo.nsyms; |
6512 | maskbitslog2 = 1; | |
6513 | while ((x >>= 1) != 0) | |
6514 | ++maskbitslog2; | |
fdc90cb4 JJ |
6515 | if (maskbitslog2 < 3) |
6516 | maskbitslog2 = 5; | |
6517 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6518 | maskbitslog2 = maskbitslog2 + 3; | |
6519 | else | |
6520 | maskbitslog2 = maskbitslog2 + 2; | |
6521 | if (bed->s->arch_size == 64) | |
6522 | { | |
6523 | if (maskbitslog2 == 5) | |
6524 | maskbitslog2 = 6; | |
6525 | cinfo.shift1 = 6; | |
6526 | } | |
6527 | else | |
6528 | cinfo.shift1 = 5; | |
6529 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6530 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6531 | cinfo.maskbits = 1 << maskbitslog2; |
6532 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6533 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6534 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 6535 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
6536 | if (cinfo.bitmask == NULL) |
6537 | { | |
6538 | free (cinfo.hashcodes); | |
6539 | return FALSE; | |
6540 | } | |
6541 | ||
a50b1753 | 6542 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
6543 | cinfo.indx = cinfo.counts + bucketcount; |
6544 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6545 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6546 | ||
6547 | /* Determine how often each hash bucket is used. */ | |
6548 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6549 | for (i = 0; i < cinfo.nsyms; ++i) | |
6550 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6551 | ||
6552 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6553 | if (cinfo.counts[i] != 0) | |
6554 | { | |
6555 | cinfo.indx[i] = cnt; | |
6556 | cnt += cinfo.counts[i]; | |
6557 | } | |
6558 | BFD_ASSERT (cnt == dynsymcount); | |
6559 | cinfo.bucketcount = bucketcount; | |
6560 | cinfo.local_indx = cinfo.min_dynindx; | |
6561 | ||
6562 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6563 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 6564 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6565 | if (contents == NULL) |
6566 | { | |
6567 | free (cinfo.bitmask); | |
6568 | free (cinfo.hashcodes); | |
6569 | return FALSE; | |
6570 | } | |
6571 | ||
6572 | s->contents = contents; | |
6573 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6574 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6575 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6576 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6577 | contents += 16 + cinfo.maskbits / 8; | |
6578 | ||
6579 | for (i = 0; i < bucketcount; ++i) | |
6580 | { | |
6581 | if (cinfo.counts[i] == 0) | |
6582 | bfd_put_32 (output_bfd, 0, contents); | |
6583 | else | |
6584 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6585 | contents += 4; | |
6586 | } | |
6587 | ||
6588 | cinfo.contents = contents; | |
6589 | ||
6590 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6591 | elf_link_hash_traverse (elf_hash_table (info), | |
6592 | elf_renumber_gnu_hash_syms, &cinfo); | |
6593 | ||
6594 | contents = s->contents + 16; | |
6595 | for (i = 0; i < maskwords; ++i) | |
6596 | { | |
6597 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6598 | contents); | |
6599 | contents += bed->s->arch_size / 8; | |
6600 | } | |
6601 | ||
6602 | free (cinfo.bitmask); | |
6603 | free (cinfo.hashcodes); | |
6604 | } | |
6605 | } | |
5a580b3a AM |
6606 | |
6607 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
6608 | BFD_ASSERT (s != NULL); | |
6609 | ||
4ad4eba5 | 6610 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6611 | |
eea6121a | 6612 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6613 | |
6614 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6615 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6616 | return FALSE; | |
6617 | } | |
6618 | ||
6619 | return TRUE; | |
6620 | } | |
4d269e42 AM |
6621 | \f |
6622 | /* Indicate that we are only retrieving symbol values from this | |
6623 | section. */ | |
6624 | ||
6625 | void | |
6626 | _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info) | |
6627 | { | |
6628 | if (is_elf_hash_table (info->hash)) | |
6629 | sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS; | |
6630 | _bfd_generic_link_just_syms (sec, info); | |
6631 | } | |
6632 | ||
6633 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ | |
6634 | ||
6635 | static void | |
6636 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
6637 | asection *sec) | |
6638 | { | |
6639 | BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE); | |
6640 | sec->sec_info_type = ELF_INFO_TYPE_NONE; | |
6641 | } | |
6642 | ||
6643 | /* Finish SHF_MERGE section merging. */ | |
6644 | ||
6645 | bfd_boolean | |
6646 | _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info) | |
6647 | { | |
6648 | bfd *ibfd; | |
6649 | asection *sec; | |
6650 | ||
6651 | if (!is_elf_hash_table (info->hash)) | |
6652 | return FALSE; | |
6653 | ||
6654 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
6655 | if ((ibfd->flags & DYNAMIC) == 0) | |
6656 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
6657 | if ((sec->flags & SEC_MERGE) != 0 | |
6658 | && !bfd_is_abs_section (sec->output_section)) | |
6659 | { | |
6660 | struct bfd_elf_section_data *secdata; | |
6661 | ||
6662 | secdata = elf_section_data (sec); | |
6663 | if (! _bfd_add_merge_section (abfd, | |
6664 | &elf_hash_table (info)->merge_info, | |
6665 | sec, &secdata->sec_info)) | |
6666 | return FALSE; | |
6667 | else if (secdata->sec_info) | |
6668 | sec->sec_info_type = ELF_INFO_TYPE_MERGE; | |
6669 | } | |
6670 | ||
6671 | if (elf_hash_table (info)->merge_info != NULL) | |
6672 | _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info, | |
6673 | merge_sections_remove_hook); | |
6674 | return TRUE; | |
6675 | } | |
6676 | ||
6677 | /* Create an entry in an ELF linker hash table. */ | |
6678 | ||
6679 | struct bfd_hash_entry * | |
6680 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
6681 | struct bfd_hash_table *table, | |
6682 | const char *string) | |
6683 | { | |
6684 | /* Allocate the structure if it has not already been allocated by a | |
6685 | subclass. */ | |
6686 | if (entry == NULL) | |
6687 | { | |
a50b1753 NC |
6688 | entry = (struct bfd_hash_entry *) |
6689 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); | |
4d269e42 AM |
6690 | if (entry == NULL) |
6691 | return entry; | |
6692 | } | |
6693 | ||
6694 | /* Call the allocation method of the superclass. */ | |
6695 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
6696 | if (entry != NULL) | |
6697 | { | |
6698 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
6699 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
6700 | ||
6701 | /* Set local fields. */ | |
6702 | ret->indx = -1; | |
6703 | ret->dynindx = -1; | |
6704 | ret->got = htab->init_got_refcount; | |
6705 | ret->plt = htab->init_plt_refcount; | |
6706 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
6707 | - offsetof (struct elf_link_hash_entry, size))); | |
6708 | /* Assume that we have been called by a non-ELF symbol reader. | |
6709 | This flag is then reset by the code which reads an ELF input | |
6710 | file. This ensures that a symbol created by a non-ELF symbol | |
6711 | reader will have the flag set correctly. */ | |
6712 | ret->non_elf = 1; | |
6713 | } | |
6714 | ||
6715 | return entry; | |
6716 | } | |
6717 | ||
6718 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
6719 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
6720 | ||
6721 | void | |
6722 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
6723 | struct elf_link_hash_entry *dir, | |
6724 | struct elf_link_hash_entry *ind) | |
6725 | { | |
6726 | struct elf_link_hash_table *htab; | |
6727 | ||
6728 | /* Copy down any references that we may have already seen to the | |
6729 | symbol which just became indirect. */ | |
6730 | ||
6731 | dir->ref_dynamic |= ind->ref_dynamic; | |
6732 | dir->ref_regular |= ind->ref_regular; | |
6733 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
6734 | dir->non_got_ref |= ind->non_got_ref; | |
6735 | dir->needs_plt |= ind->needs_plt; | |
6736 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
6737 | ||
6738 | if (ind->root.type != bfd_link_hash_indirect) | |
6739 | return; | |
6740 | ||
6741 | /* Copy over the global and procedure linkage table refcount entries. | |
6742 | These may have been already set up by a check_relocs routine. */ | |
6743 | htab = elf_hash_table (info); | |
6744 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
6745 | { | |
6746 | if (dir->got.refcount < 0) | |
6747 | dir->got.refcount = 0; | |
6748 | dir->got.refcount += ind->got.refcount; | |
6749 | ind->got.refcount = htab->init_got_refcount.refcount; | |
6750 | } | |
6751 | ||
6752 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
6753 | { | |
6754 | if (dir->plt.refcount < 0) | |
6755 | dir->plt.refcount = 0; | |
6756 | dir->plt.refcount += ind->plt.refcount; | |
6757 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
6758 | } | |
6759 | ||
6760 | if (ind->dynindx != -1) | |
6761 | { | |
6762 | if (dir->dynindx != -1) | |
6763 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
6764 | dir->dynindx = ind->dynindx; | |
6765 | dir->dynstr_index = ind->dynstr_index; | |
6766 | ind->dynindx = -1; | |
6767 | ind->dynstr_index = 0; | |
6768 | } | |
6769 | } | |
6770 | ||
6771 | void | |
6772 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
6773 | struct elf_link_hash_entry *h, | |
6774 | bfd_boolean force_local) | |
6775 | { | |
3aa14d16 L |
6776 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
6777 | if (h->type != STT_GNU_IFUNC) | |
6778 | { | |
6779 | h->plt = elf_hash_table (info)->init_plt_offset; | |
6780 | h->needs_plt = 0; | |
6781 | } | |
4d269e42 AM |
6782 | if (force_local) |
6783 | { | |
6784 | h->forced_local = 1; | |
6785 | if (h->dynindx != -1) | |
6786 | { | |
6787 | h->dynindx = -1; | |
6788 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
6789 | h->dynstr_index); | |
6790 | } | |
6791 | } | |
6792 | } | |
6793 | ||
6794 | /* Initialize an ELF linker hash table. */ | |
6795 | ||
6796 | bfd_boolean | |
6797 | _bfd_elf_link_hash_table_init | |
6798 | (struct elf_link_hash_table *table, | |
6799 | bfd *abfd, | |
6800 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
6801 | struct bfd_hash_table *, | |
6802 | const char *), | |
4dfe6ac6 NC |
6803 | unsigned int entsize, |
6804 | enum elf_target_id target_id) | |
4d269e42 AM |
6805 | { |
6806 | bfd_boolean ret; | |
6807 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
6808 | ||
6809 | memset (table, 0, sizeof * table); | |
6810 | table->init_got_refcount.refcount = can_refcount - 1; | |
6811 | table->init_plt_refcount.refcount = can_refcount - 1; | |
6812 | table->init_got_offset.offset = -(bfd_vma) 1; | |
6813 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
6814 | /* The first dynamic symbol is a dummy. */ | |
6815 | table->dynsymcount = 1; | |
6816 | ||
6817 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 6818 | |
4d269e42 | 6819 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 6820 | table->hash_table_id = target_id; |
4d269e42 AM |
6821 | |
6822 | return ret; | |
6823 | } | |
6824 | ||
6825 | /* Create an ELF linker hash table. */ | |
6826 | ||
6827 | struct bfd_link_hash_table * | |
6828 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
6829 | { | |
6830 | struct elf_link_hash_table *ret; | |
6831 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
6832 | ||
a50b1753 | 6833 | ret = (struct elf_link_hash_table *) bfd_malloc (amt); |
4d269e42 AM |
6834 | if (ret == NULL) |
6835 | return NULL; | |
6836 | ||
6837 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
6838 | sizeof (struct elf_link_hash_entry), |
6839 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
6840 | { |
6841 | free (ret); | |
6842 | return NULL; | |
6843 | } | |
6844 | ||
6845 | return &ret->root; | |
6846 | } | |
6847 | ||
6848 | /* This is a hook for the ELF emulation code in the generic linker to | |
6849 | tell the backend linker what file name to use for the DT_NEEDED | |
6850 | entry for a dynamic object. */ | |
6851 | ||
6852 | void | |
6853 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
6854 | { | |
6855 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6856 | && bfd_get_format (abfd) == bfd_object) | |
6857 | elf_dt_name (abfd) = name; | |
6858 | } | |
6859 | ||
6860 | int | |
6861 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
6862 | { | |
6863 | int lib_class; | |
6864 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6865 | && bfd_get_format (abfd) == bfd_object) | |
6866 | lib_class = elf_dyn_lib_class (abfd); | |
6867 | else | |
6868 | lib_class = 0; | |
6869 | return lib_class; | |
6870 | } | |
6871 | ||
6872 | void | |
6873 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
6874 | { | |
6875 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6876 | && bfd_get_format (abfd) == bfd_object) | |
6877 | elf_dyn_lib_class (abfd) = lib_class; | |
6878 | } | |
6879 | ||
6880 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
6881 | the linker ELF emulation code. */ | |
6882 | ||
6883 | struct bfd_link_needed_list * | |
6884 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
6885 | struct bfd_link_info *info) | |
6886 | { | |
6887 | if (! is_elf_hash_table (info->hash)) | |
6888 | return NULL; | |
6889 | return elf_hash_table (info)->needed; | |
6890 | } | |
6891 | ||
6892 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
6893 | hook for the linker ELF emulation code. */ | |
6894 | ||
6895 | struct bfd_link_needed_list * | |
6896 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
6897 | struct bfd_link_info *info) | |
6898 | { | |
6899 | if (! is_elf_hash_table (info->hash)) | |
6900 | return NULL; | |
6901 | return elf_hash_table (info)->runpath; | |
6902 | } | |
6903 | ||
6904 | /* Get the name actually used for a dynamic object for a link. This | |
6905 | is the SONAME entry if there is one. Otherwise, it is the string | |
6906 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
6907 | ||
6908 | const char * | |
6909 | bfd_elf_get_dt_soname (bfd *abfd) | |
6910 | { | |
6911 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6912 | && bfd_get_format (abfd) == bfd_object) | |
6913 | return elf_dt_name (abfd); | |
6914 | return NULL; | |
6915 | } | |
6916 | ||
6917 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
6918 | the ELF linker emulation code. */ | |
6919 | ||
6920 | bfd_boolean | |
6921 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
6922 | struct bfd_link_needed_list **pneeded) | |
6923 | { | |
6924 | asection *s; | |
6925 | bfd_byte *dynbuf = NULL; | |
cb33740c | 6926 | unsigned int elfsec; |
4d269e42 AM |
6927 | unsigned long shlink; |
6928 | bfd_byte *extdyn, *extdynend; | |
6929 | size_t extdynsize; | |
6930 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
6931 | ||
6932 | *pneeded = NULL; | |
6933 | ||
6934 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
6935 | || bfd_get_format (abfd) != bfd_object) | |
6936 | return TRUE; | |
6937 | ||
6938 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
6939 | if (s == NULL || s->size == 0) | |
6940 | return TRUE; | |
6941 | ||
6942 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
6943 | goto error_return; | |
6944 | ||
6945 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 6946 | if (elfsec == SHN_BAD) |
4d269e42 AM |
6947 | goto error_return; |
6948 | ||
6949 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 6950 | |
4d269e42 AM |
6951 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
6952 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
6953 | ||
6954 | extdyn = dynbuf; | |
6955 | extdynend = extdyn + s->size; | |
6956 | for (; extdyn < extdynend; extdyn += extdynsize) | |
6957 | { | |
6958 | Elf_Internal_Dyn dyn; | |
6959 | ||
6960 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
6961 | ||
6962 | if (dyn.d_tag == DT_NULL) | |
6963 | break; | |
6964 | ||
6965 | if (dyn.d_tag == DT_NEEDED) | |
6966 | { | |
6967 | const char *string; | |
6968 | struct bfd_link_needed_list *l; | |
6969 | unsigned int tagv = dyn.d_un.d_val; | |
6970 | bfd_size_type amt; | |
6971 | ||
6972 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
6973 | if (string == NULL) | |
6974 | goto error_return; | |
6975 | ||
6976 | amt = sizeof *l; | |
a50b1753 | 6977 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
6978 | if (l == NULL) |
6979 | goto error_return; | |
6980 | ||
6981 | l->by = abfd; | |
6982 | l->name = string; | |
6983 | l->next = *pneeded; | |
6984 | *pneeded = l; | |
6985 | } | |
6986 | } | |
6987 | ||
6988 | free (dynbuf); | |
6989 | ||
6990 | return TRUE; | |
6991 | ||
6992 | error_return: | |
6993 | if (dynbuf != NULL) | |
6994 | free (dynbuf); | |
6995 | return FALSE; | |
6996 | } | |
6997 | ||
6998 | struct elf_symbuf_symbol | |
6999 | { | |
7000 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7001 | unsigned char st_info; /* Type and binding attributes */ | |
7002 | unsigned char st_other; /* Visibilty, and target specific */ | |
7003 | }; | |
7004 | ||
7005 | struct elf_symbuf_head | |
7006 | { | |
7007 | struct elf_symbuf_symbol *ssym; | |
7008 | bfd_size_type count; | |
7009 | unsigned int st_shndx; | |
7010 | }; | |
7011 | ||
7012 | struct elf_symbol | |
7013 | { | |
7014 | union | |
7015 | { | |
7016 | Elf_Internal_Sym *isym; | |
7017 | struct elf_symbuf_symbol *ssym; | |
7018 | } u; | |
7019 | const char *name; | |
7020 | }; | |
7021 | ||
7022 | /* Sort references to symbols by ascending section number. */ | |
7023 | ||
7024 | static int | |
7025 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7026 | { | |
7027 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7028 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7029 | ||
7030 | return s1->st_shndx - s2->st_shndx; | |
7031 | } | |
7032 | ||
7033 | static int | |
7034 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7035 | { | |
7036 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7037 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7038 | return strcmp (s1->name, s2->name); | |
7039 | } | |
7040 | ||
7041 | static struct elf_symbuf_head * | |
7042 | elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf) | |
7043 | { | |
14b1c01e | 7044 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7045 | struct elf_symbuf_symbol *ssym; |
7046 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
3ae181ee | 7047 | bfd_size_type i, shndx_count, total_size; |
4d269e42 | 7048 | |
a50b1753 | 7049 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7050 | if (indbuf == NULL) |
7051 | return NULL; | |
7052 | ||
7053 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7054 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7055 | *ind++ = &isymbuf[i]; | |
7056 | indbufend = ind; | |
7057 | ||
7058 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7059 | elf_sort_elf_symbol); | |
7060 | ||
7061 | shndx_count = 0; | |
7062 | if (indbufend > indbuf) | |
7063 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7064 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7065 | shndx_count++; | |
7066 | ||
3ae181ee L |
7067 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7068 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7069 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7070 | if (ssymbuf == NULL) |
7071 | { | |
7072 | free (indbuf); | |
7073 | return NULL; | |
7074 | } | |
7075 | ||
3ae181ee | 7076 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7077 | ssymbuf->ssym = NULL; |
7078 | ssymbuf->count = shndx_count; | |
7079 | ssymbuf->st_shndx = 0; | |
7080 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7081 | { | |
7082 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7083 | { | |
7084 | ssymhead++; | |
7085 | ssymhead->ssym = ssym; | |
7086 | ssymhead->count = 0; | |
7087 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7088 | } | |
7089 | ssym->st_name = (*ind)->st_name; | |
7090 | ssym->st_info = (*ind)->st_info; | |
7091 | ssym->st_other = (*ind)->st_other; | |
7092 | ssymhead->count++; | |
7093 | } | |
3ae181ee L |
7094 | BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count |
7095 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) | |
7096 | == total_size)); | |
4d269e42 AM |
7097 | |
7098 | free (indbuf); | |
7099 | return ssymbuf; | |
7100 | } | |
7101 | ||
7102 | /* Check if 2 sections define the same set of local and global | |
7103 | symbols. */ | |
7104 | ||
8f317e31 | 7105 | static bfd_boolean |
4d269e42 AM |
7106 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7107 | struct bfd_link_info *info) | |
7108 | { | |
7109 | bfd *bfd1, *bfd2; | |
7110 | const struct elf_backend_data *bed1, *bed2; | |
7111 | Elf_Internal_Shdr *hdr1, *hdr2; | |
7112 | bfd_size_type symcount1, symcount2; | |
7113 | Elf_Internal_Sym *isymbuf1, *isymbuf2; | |
7114 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7115 | Elf_Internal_Sym *isym, *isymend; | |
7116 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
7117 | bfd_size_type count1, count2, i; | |
cb33740c | 7118 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7119 | bfd_boolean result; |
7120 | ||
7121 | bfd1 = sec1->owner; | |
7122 | bfd2 = sec2->owner; | |
7123 | ||
4d269e42 AM |
7124 | /* Both sections have to be in ELF. */ |
7125 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7126 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7127 | return FALSE; | |
7128 | ||
7129 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7130 | return FALSE; | |
7131 | ||
4d269e42 AM |
7132 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7133 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7134 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7135 | return FALSE; |
7136 | ||
7137 | bed1 = get_elf_backend_data (bfd1); | |
7138 | bed2 = get_elf_backend_data (bfd2); | |
7139 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7140 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7141 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7142 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7143 | ||
7144 | if (symcount1 == 0 || symcount2 == 0) | |
7145 | return FALSE; | |
7146 | ||
7147 | result = FALSE; | |
7148 | isymbuf1 = NULL; | |
7149 | isymbuf2 = NULL; | |
a50b1753 NC |
7150 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7151 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7152 | |
7153 | if (ssymbuf1 == NULL) | |
7154 | { | |
7155 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7156 | NULL, NULL, NULL); | |
7157 | if (isymbuf1 == NULL) | |
7158 | goto done; | |
7159 | ||
7160 | if (!info->reduce_memory_overheads) | |
7161 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7162 | = elf_create_symbuf (symcount1, isymbuf1); | |
7163 | } | |
7164 | ||
7165 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7166 | { | |
7167 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7168 | NULL, NULL, NULL); | |
7169 | if (isymbuf2 == NULL) | |
7170 | goto done; | |
7171 | ||
7172 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7173 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7174 | = elf_create_symbuf (symcount2, isymbuf2); | |
7175 | } | |
7176 | ||
7177 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7178 | { | |
7179 | /* Optimized faster version. */ | |
7180 | bfd_size_type lo, hi, mid; | |
7181 | struct elf_symbol *symp; | |
7182 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7183 | ||
7184 | lo = 0; | |
7185 | hi = ssymbuf1->count; | |
7186 | ssymbuf1++; | |
7187 | count1 = 0; | |
7188 | while (lo < hi) | |
7189 | { | |
7190 | mid = (lo + hi) / 2; | |
cb33740c | 7191 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7192 | hi = mid; |
cb33740c | 7193 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7194 | lo = mid + 1; |
7195 | else | |
7196 | { | |
7197 | count1 = ssymbuf1[mid].count; | |
7198 | ssymbuf1 += mid; | |
7199 | break; | |
7200 | } | |
7201 | } | |
7202 | ||
7203 | lo = 0; | |
7204 | hi = ssymbuf2->count; | |
7205 | ssymbuf2++; | |
7206 | count2 = 0; | |
7207 | while (lo < hi) | |
7208 | { | |
7209 | mid = (lo + hi) / 2; | |
cb33740c | 7210 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7211 | hi = mid; |
cb33740c | 7212 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7213 | lo = mid + 1; |
7214 | else | |
7215 | { | |
7216 | count2 = ssymbuf2[mid].count; | |
7217 | ssymbuf2 += mid; | |
7218 | break; | |
7219 | } | |
7220 | } | |
7221 | ||
7222 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7223 | goto done; | |
7224 | ||
a50b1753 NC |
7225 | symtable1 = (struct elf_symbol *) |
7226 | bfd_malloc (count1 * sizeof (struct elf_symbol)); | |
7227 | symtable2 = (struct elf_symbol *) | |
7228 | bfd_malloc (count2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7229 | if (symtable1 == NULL || symtable2 == NULL) |
7230 | goto done; | |
7231 | ||
7232 | symp = symtable1; | |
7233 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7234 | ssym < ssymend; ssym++, symp++) | |
7235 | { | |
7236 | symp->u.ssym = ssym; | |
7237 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7238 | hdr1->sh_link, | |
7239 | ssym->st_name); | |
7240 | } | |
7241 | ||
7242 | symp = symtable2; | |
7243 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7244 | ssym < ssymend; ssym++, symp++) | |
7245 | { | |
7246 | symp->u.ssym = ssym; | |
7247 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7248 | hdr2->sh_link, | |
7249 | ssym->st_name); | |
7250 | } | |
7251 | ||
7252 | /* Sort symbol by name. */ | |
7253 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7254 | elf_sym_name_compare); | |
7255 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7256 | elf_sym_name_compare); | |
7257 | ||
7258 | for (i = 0; i < count1; i++) | |
7259 | /* Two symbols must have the same binding, type and name. */ | |
7260 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7261 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7262 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7263 | goto done; | |
7264 | ||
7265 | result = TRUE; | |
7266 | goto done; | |
7267 | } | |
7268 | ||
a50b1753 NC |
7269 | symtable1 = (struct elf_symbol *) |
7270 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7271 | symtable2 = (struct elf_symbol *) | |
7272 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7273 | if (symtable1 == NULL || symtable2 == NULL) |
7274 | goto done; | |
7275 | ||
7276 | /* Count definitions in the section. */ | |
7277 | count1 = 0; | |
7278 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7279 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7280 | symtable1[count1++].u.isym = isym; |
7281 | ||
7282 | count2 = 0; | |
7283 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7284 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7285 | symtable2[count2++].u.isym = isym; |
7286 | ||
7287 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7288 | goto done; | |
7289 | ||
7290 | for (i = 0; i < count1; i++) | |
7291 | symtable1[i].name | |
7292 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7293 | symtable1[i].u.isym->st_name); | |
7294 | ||
7295 | for (i = 0; i < count2; i++) | |
7296 | symtable2[i].name | |
7297 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7298 | symtable2[i].u.isym->st_name); | |
7299 | ||
7300 | /* Sort symbol by name. */ | |
7301 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7302 | elf_sym_name_compare); | |
7303 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7304 | elf_sym_name_compare); | |
7305 | ||
7306 | for (i = 0; i < count1; i++) | |
7307 | /* Two symbols must have the same binding, type and name. */ | |
7308 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7309 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7310 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7311 | goto done; | |
7312 | ||
7313 | result = TRUE; | |
7314 | ||
7315 | done: | |
7316 | if (symtable1) | |
7317 | free (symtable1); | |
7318 | if (symtable2) | |
7319 | free (symtable2); | |
7320 | if (isymbuf1) | |
7321 | free (isymbuf1); | |
7322 | if (isymbuf2) | |
7323 | free (isymbuf2); | |
7324 | ||
7325 | return result; | |
7326 | } | |
7327 | ||
7328 | /* Return TRUE if 2 section types are compatible. */ | |
7329 | ||
7330 | bfd_boolean | |
7331 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7332 | bfd *bbfd, const asection *bsec) | |
7333 | { | |
7334 | if (asec == NULL | |
7335 | || bsec == NULL | |
7336 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7337 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7338 | return TRUE; | |
7339 | ||
7340 | return elf_section_type (asec) == elf_section_type (bsec); | |
7341 | } | |
7342 | \f | |
c152c796 AM |
7343 | /* Final phase of ELF linker. */ |
7344 | ||
7345 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7346 | ||
7347 | struct elf_final_link_info | |
7348 | { | |
7349 | /* General link information. */ | |
7350 | struct bfd_link_info *info; | |
7351 | /* Output BFD. */ | |
7352 | bfd *output_bfd; | |
7353 | /* Symbol string table. */ | |
7354 | struct bfd_strtab_hash *symstrtab; | |
7355 | /* .dynsym section. */ | |
7356 | asection *dynsym_sec; | |
7357 | /* .hash section. */ | |
7358 | asection *hash_sec; | |
7359 | /* symbol version section (.gnu.version). */ | |
7360 | asection *symver_sec; | |
7361 | /* Buffer large enough to hold contents of any section. */ | |
7362 | bfd_byte *contents; | |
7363 | /* Buffer large enough to hold external relocs of any section. */ | |
7364 | void *external_relocs; | |
7365 | /* Buffer large enough to hold internal relocs of any section. */ | |
7366 | Elf_Internal_Rela *internal_relocs; | |
7367 | /* Buffer large enough to hold external local symbols of any input | |
7368 | BFD. */ | |
7369 | bfd_byte *external_syms; | |
7370 | /* And a buffer for symbol section indices. */ | |
7371 | Elf_External_Sym_Shndx *locsym_shndx; | |
7372 | /* Buffer large enough to hold internal local symbols of any input | |
7373 | BFD. */ | |
7374 | Elf_Internal_Sym *internal_syms; | |
7375 | /* Array large enough to hold a symbol index for each local symbol | |
7376 | of any input BFD. */ | |
7377 | long *indices; | |
7378 | /* Array large enough to hold a section pointer for each local | |
7379 | symbol of any input BFD. */ | |
7380 | asection **sections; | |
7381 | /* Buffer to hold swapped out symbols. */ | |
7382 | bfd_byte *symbuf; | |
7383 | /* And one for symbol section indices. */ | |
7384 | Elf_External_Sym_Shndx *symshndxbuf; | |
7385 | /* Number of swapped out symbols in buffer. */ | |
7386 | size_t symbuf_count; | |
7387 | /* Number of symbols which fit in symbuf. */ | |
7388 | size_t symbuf_size; | |
7389 | /* And same for symshndxbuf. */ | |
7390 | size_t shndxbuf_size; | |
7391 | }; | |
7392 | ||
7393 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
7394 | ||
7395 | struct elf_outext_info | |
7396 | { | |
7397 | bfd_boolean failed; | |
7398 | bfd_boolean localsyms; | |
7399 | struct elf_final_link_info *finfo; | |
7400 | }; | |
7401 | ||
d9352518 DB |
7402 | |
7403 | /* Support for evaluating a complex relocation. | |
7404 | ||
7405 | Complex relocations are generalized, self-describing relocations. The | |
7406 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 7407 | relocations themselves. |
d9352518 DB |
7408 | |
7409 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
7410 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
7411 | information (start bit, end bit, word width, etc) into the addend. This | |
7412 | information is extracted from CGEN-generated operand tables within gas. | |
7413 | ||
7414 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
7415 | internal) representing prefix-notation expressions, including but not | |
7416 | limited to those sorts of expressions normally encoded as addends in the | |
7417 | addend field. The symbol mangling format is: | |
7418 | ||
7419 | <node> := <literal> | |
7420 | | <unary-operator> ':' <node> | |
7421 | | <binary-operator> ':' <node> ':' <node> | |
7422 | ; | |
7423 | ||
7424 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
7425 | | 'S' <digits=N> ':' <N character section name> | |
7426 | | '#' <hexdigits> | |
7427 | ; | |
7428 | ||
7429 | <binary-operator> := as in C | |
7430 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
7431 | ||
7432 | static void | |
a0c8462f AM |
7433 | set_symbol_value (bfd *bfd_with_globals, |
7434 | Elf_Internal_Sym *isymbuf, | |
7435 | size_t locsymcount, | |
7436 | size_t symidx, | |
7437 | bfd_vma val) | |
d9352518 | 7438 | { |
8977835c AM |
7439 | struct elf_link_hash_entry **sym_hashes; |
7440 | struct elf_link_hash_entry *h; | |
7441 | size_t extsymoff = locsymcount; | |
d9352518 | 7442 | |
8977835c | 7443 | if (symidx < locsymcount) |
d9352518 | 7444 | { |
8977835c AM |
7445 | Elf_Internal_Sym *sym; |
7446 | ||
7447 | sym = isymbuf + symidx; | |
7448 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
7449 | { | |
7450 | /* It is a local symbol: move it to the | |
7451 | "absolute" section and give it a value. */ | |
7452 | sym->st_shndx = SHN_ABS; | |
7453 | sym->st_value = val; | |
7454 | return; | |
7455 | } | |
7456 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
7457 | extsymoff = 0; | |
d9352518 | 7458 | } |
8977835c AM |
7459 | |
7460 | /* It is a global symbol: set its link type | |
7461 | to "defined" and give it a value. */ | |
7462 | ||
7463 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
7464 | h = sym_hashes [symidx - extsymoff]; | |
7465 | while (h->root.type == bfd_link_hash_indirect | |
7466 | || h->root.type == bfd_link_hash_warning) | |
7467 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7468 | h->root.type = bfd_link_hash_defined; | |
7469 | h->root.u.def.value = val; | |
7470 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
7471 | } |
7472 | ||
a0c8462f AM |
7473 | static bfd_boolean |
7474 | resolve_symbol (const char *name, | |
7475 | bfd *input_bfd, | |
7476 | struct elf_final_link_info *finfo, | |
7477 | bfd_vma *result, | |
7478 | Elf_Internal_Sym *isymbuf, | |
7479 | size_t locsymcount) | |
d9352518 | 7480 | { |
a0c8462f AM |
7481 | Elf_Internal_Sym *sym; |
7482 | struct bfd_link_hash_entry *global_entry; | |
7483 | const char *candidate = NULL; | |
7484 | Elf_Internal_Shdr *symtab_hdr; | |
7485 | size_t i; | |
7486 | ||
d9352518 DB |
7487 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
7488 | ||
7489 | for (i = 0; i < locsymcount; ++ i) | |
7490 | { | |
8977835c | 7491 | sym = isymbuf + i; |
d9352518 DB |
7492 | |
7493 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
7494 | continue; | |
7495 | ||
7496 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
7497 | symtab_hdr->sh_link, | |
7498 | sym->st_name); | |
7499 | #ifdef DEBUG | |
0f02bbd9 AM |
7500 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
7501 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
7502 | #endif |
7503 | if (candidate && strcmp (candidate, name) == 0) | |
7504 | { | |
0f02bbd9 | 7505 | asection *sec = finfo->sections [i]; |
d9352518 | 7506 | |
0f02bbd9 AM |
7507 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
7508 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 7509 | #ifdef DEBUG |
0f02bbd9 AM |
7510 | printf ("Found symbol with value %8.8lx\n", |
7511 | (unsigned long) *result); | |
d9352518 DB |
7512 | #endif |
7513 | return TRUE; | |
7514 | } | |
7515 | } | |
7516 | ||
7517 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
a0c8462f AM |
7518 | global_entry = bfd_link_hash_lookup (finfo->info->hash, name, |
7519 | FALSE, FALSE, TRUE); | |
d9352518 DB |
7520 | if (!global_entry) |
7521 | return FALSE; | |
a0c8462f | 7522 | |
d9352518 DB |
7523 | if (global_entry->type == bfd_link_hash_defined |
7524 | || global_entry->type == bfd_link_hash_defweak) | |
7525 | { | |
a0c8462f AM |
7526 | *result = (global_entry->u.def.value |
7527 | + global_entry->u.def.section->output_section->vma | |
7528 | + global_entry->u.def.section->output_offset); | |
d9352518 | 7529 | #ifdef DEBUG |
0f02bbd9 AM |
7530 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
7531 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
7532 | #endif |
7533 | return TRUE; | |
a0c8462f | 7534 | } |
d9352518 | 7535 | |
d9352518 DB |
7536 | return FALSE; |
7537 | } | |
7538 | ||
7539 | static bfd_boolean | |
a0c8462f AM |
7540 | resolve_section (const char *name, |
7541 | asection *sections, | |
7542 | bfd_vma *result) | |
d9352518 | 7543 | { |
a0c8462f AM |
7544 | asection *curr; |
7545 | unsigned int len; | |
d9352518 | 7546 | |
a0c8462f | 7547 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7548 | if (strcmp (curr->name, name) == 0) |
7549 | { | |
7550 | *result = curr->vma; | |
7551 | return TRUE; | |
7552 | } | |
7553 | ||
7554 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
a0c8462f | 7555 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7556 | { |
7557 | len = strlen (curr->name); | |
a0c8462f | 7558 | if (len > strlen (name)) |
d9352518 DB |
7559 | continue; |
7560 | ||
7561 | if (strncmp (curr->name, name, len) == 0) | |
7562 | { | |
7563 | if (strncmp (".end", name + len, 4) == 0) | |
7564 | { | |
7565 | *result = curr->vma + curr->size; | |
7566 | return TRUE; | |
7567 | } | |
7568 | ||
7569 | /* Insert more pseudo-section names here, if you like. */ | |
7570 | } | |
7571 | } | |
a0c8462f | 7572 | |
d9352518 DB |
7573 | return FALSE; |
7574 | } | |
7575 | ||
7576 | static void | |
a0c8462f | 7577 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 7578 | { |
a0c8462f AM |
7579 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
7580 | reftype, name); | |
d9352518 DB |
7581 | } |
7582 | ||
7583 | static bfd_boolean | |
a0c8462f AM |
7584 | eval_symbol (bfd_vma *result, |
7585 | const char **symp, | |
7586 | bfd *input_bfd, | |
7587 | struct elf_final_link_info *finfo, | |
7588 | bfd_vma dot, | |
7589 | Elf_Internal_Sym *isymbuf, | |
7590 | size_t locsymcount, | |
7591 | int signed_p) | |
d9352518 | 7592 | { |
4b93929b NC |
7593 | size_t len; |
7594 | size_t symlen; | |
a0c8462f AM |
7595 | bfd_vma a; |
7596 | bfd_vma b; | |
4b93929b | 7597 | char symbuf[4096]; |
0f02bbd9 | 7598 | const char *sym = *symp; |
a0c8462f AM |
7599 | const char *symend; |
7600 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
7601 | |
7602 | len = strlen (sym); | |
7603 | symend = sym + len; | |
7604 | ||
4b93929b | 7605 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
7606 | { |
7607 | bfd_set_error (bfd_error_invalid_operation); | |
7608 | return FALSE; | |
7609 | } | |
a0c8462f | 7610 | |
d9352518 DB |
7611 | switch (* sym) |
7612 | { | |
7613 | case '.': | |
0f02bbd9 AM |
7614 | *result = dot; |
7615 | *symp = sym + 1; | |
d9352518 DB |
7616 | return TRUE; |
7617 | ||
7618 | case '#': | |
0f02bbd9 AM |
7619 | ++sym; |
7620 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
7621 | return TRUE; |
7622 | ||
7623 | case 'S': | |
7624 | symbol_is_section = TRUE; | |
a0c8462f | 7625 | case 's': |
0f02bbd9 AM |
7626 | ++sym; |
7627 | symlen = strtol (sym, (char **) symp, 10); | |
7628 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 7629 | |
4b93929b | 7630 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
7631 | { |
7632 | bfd_set_error (bfd_error_invalid_operation); | |
7633 | return FALSE; | |
7634 | } | |
7635 | ||
7636 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 7637 | symbuf[symlen] = '\0'; |
0f02bbd9 | 7638 | *symp = sym + symlen; |
a0c8462f AM |
7639 | |
7640 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
7641 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
7642 | interpretation here; section means "try section first", not "must be a | |
7643 | section", and likewise with symbol. */ | |
7644 | ||
a0c8462f | 7645 | if (symbol_is_section) |
d9352518 | 7646 | { |
8977835c AM |
7647 | if (!resolve_section (symbuf, finfo->output_bfd->sections, result) |
7648 | && !resolve_symbol (symbuf, input_bfd, finfo, result, | |
7649 | isymbuf, locsymcount)) | |
d9352518 DB |
7650 | { |
7651 | undefined_reference ("section", symbuf); | |
7652 | return FALSE; | |
7653 | } | |
a0c8462f AM |
7654 | } |
7655 | else | |
d9352518 | 7656 | { |
8977835c AM |
7657 | if (!resolve_symbol (symbuf, input_bfd, finfo, result, |
7658 | isymbuf, locsymcount) | |
7659 | && !resolve_section (symbuf, finfo->output_bfd->sections, | |
7660 | result)) | |
d9352518 DB |
7661 | { |
7662 | undefined_reference ("symbol", symbuf); | |
7663 | return FALSE; | |
7664 | } | |
7665 | } | |
7666 | ||
7667 | return TRUE; | |
a0c8462f | 7668 | |
d9352518 DB |
7669 | /* All that remains are operators. */ |
7670 | ||
7671 | #define UNARY_OP(op) \ | |
7672 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
7673 | { \ | |
7674 | sym += strlen (#op); \ | |
a0c8462f AM |
7675 | if (*sym == ':') \ |
7676 | ++sym; \ | |
0f02bbd9 AM |
7677 | *symp = sym; \ |
7678 | if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \ | |
7679 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f AM |
7680 | return FALSE; \ |
7681 | if (signed_p) \ | |
0f02bbd9 | 7682 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
7683 | else \ |
7684 | *result = op a; \ | |
d9352518 DB |
7685 | return TRUE; \ |
7686 | } | |
7687 | ||
7688 | #define BINARY_OP(op) \ | |
7689 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
7690 | { \ | |
7691 | sym += strlen (#op); \ | |
a0c8462f AM |
7692 | if (*sym == ':') \ |
7693 | ++sym; \ | |
0f02bbd9 AM |
7694 | *symp = sym; \ |
7695 | if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \ | |
7696 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f | 7697 | return FALSE; \ |
0f02bbd9 AM |
7698 | ++*symp; \ |
7699 | if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \ | |
7700 | isymbuf, locsymcount, signed_p)) \ | |
a0c8462f AM |
7701 | return FALSE; \ |
7702 | if (signed_p) \ | |
0f02bbd9 | 7703 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
7704 | else \ |
7705 | *result = a op b; \ | |
d9352518 DB |
7706 | return TRUE; \ |
7707 | } | |
7708 | ||
7709 | default: | |
7710 | UNARY_OP (0-); | |
7711 | BINARY_OP (<<); | |
7712 | BINARY_OP (>>); | |
7713 | BINARY_OP (==); | |
7714 | BINARY_OP (!=); | |
7715 | BINARY_OP (<=); | |
7716 | BINARY_OP (>=); | |
7717 | BINARY_OP (&&); | |
7718 | BINARY_OP (||); | |
7719 | UNARY_OP (~); | |
7720 | UNARY_OP (!); | |
7721 | BINARY_OP (*); | |
7722 | BINARY_OP (/); | |
7723 | BINARY_OP (%); | |
7724 | BINARY_OP (^); | |
7725 | BINARY_OP (|); | |
7726 | BINARY_OP (&); | |
7727 | BINARY_OP (+); | |
7728 | BINARY_OP (-); | |
7729 | BINARY_OP (<); | |
7730 | BINARY_OP (>); | |
7731 | #undef UNARY_OP | |
7732 | #undef BINARY_OP | |
7733 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
7734 | bfd_set_error (bfd_error_invalid_operation); | |
7735 | return FALSE; | |
7736 | } | |
7737 | } | |
7738 | ||
d9352518 | 7739 | static void |
a0c8462f AM |
7740 | put_value (bfd_vma size, |
7741 | unsigned long chunksz, | |
7742 | bfd *input_bfd, | |
7743 | bfd_vma x, | |
7744 | bfd_byte *location) | |
d9352518 DB |
7745 | { |
7746 | location += (size - chunksz); | |
7747 | ||
a0c8462f | 7748 | for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8)) |
d9352518 DB |
7749 | { |
7750 | switch (chunksz) | |
7751 | { | |
7752 | default: | |
7753 | case 0: | |
7754 | abort (); | |
7755 | case 1: | |
7756 | bfd_put_8 (input_bfd, x, location); | |
7757 | break; | |
7758 | case 2: | |
7759 | bfd_put_16 (input_bfd, x, location); | |
7760 | break; | |
7761 | case 4: | |
7762 | bfd_put_32 (input_bfd, x, location); | |
7763 | break; | |
7764 | case 8: | |
7765 | #ifdef BFD64 | |
7766 | bfd_put_64 (input_bfd, x, location); | |
7767 | #else | |
7768 | abort (); | |
7769 | #endif | |
7770 | break; | |
7771 | } | |
7772 | } | |
7773 | } | |
7774 | ||
a0c8462f AM |
7775 | static bfd_vma |
7776 | get_value (bfd_vma size, | |
7777 | unsigned long chunksz, | |
7778 | bfd *input_bfd, | |
7779 | bfd_byte *location) | |
d9352518 DB |
7780 | { |
7781 | bfd_vma x = 0; | |
7782 | ||
a0c8462f | 7783 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
7784 | { |
7785 | switch (chunksz) | |
7786 | { | |
7787 | default: | |
7788 | case 0: | |
7789 | abort (); | |
7790 | case 1: | |
7791 | x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location); | |
7792 | break; | |
7793 | case 2: | |
7794 | x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location); | |
7795 | break; | |
7796 | case 4: | |
7797 | x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location); | |
7798 | break; | |
7799 | case 8: | |
7800 | #ifdef BFD64 | |
7801 | x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location); | |
7802 | #else | |
7803 | abort (); | |
7804 | #endif | |
7805 | break; | |
7806 | } | |
7807 | } | |
7808 | return x; | |
7809 | } | |
7810 | ||
a0c8462f AM |
7811 | static void |
7812 | decode_complex_addend (unsigned long *start, /* in bits */ | |
7813 | unsigned long *oplen, /* in bits */ | |
7814 | unsigned long *len, /* in bits */ | |
7815 | unsigned long *wordsz, /* in bytes */ | |
7816 | unsigned long *chunksz, /* in bytes */ | |
7817 | unsigned long *lsb0_p, | |
7818 | unsigned long *signed_p, | |
7819 | unsigned long *trunc_p, | |
7820 | unsigned long encoded) | |
d9352518 DB |
7821 | { |
7822 | * start = encoded & 0x3F; | |
7823 | * len = (encoded >> 6) & 0x3F; | |
7824 | * oplen = (encoded >> 12) & 0x3F; | |
7825 | * wordsz = (encoded >> 18) & 0xF; | |
7826 | * chunksz = (encoded >> 22) & 0xF; | |
7827 | * lsb0_p = (encoded >> 27) & 1; | |
7828 | * signed_p = (encoded >> 28) & 1; | |
7829 | * trunc_p = (encoded >> 29) & 1; | |
7830 | } | |
7831 | ||
cdfeee4f | 7832 | bfd_reloc_status_type |
0f02bbd9 | 7833 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 7834 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
7835 | bfd_byte *contents, |
7836 | Elf_Internal_Rela *rel, | |
7837 | bfd_vma relocation) | |
d9352518 | 7838 | { |
0f02bbd9 AM |
7839 | bfd_vma shift, x, mask; |
7840 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 7841 | bfd_reloc_status_type r; |
d9352518 DB |
7842 | |
7843 | /* Perform this reloc, since it is complex. | |
7844 | (this is not to say that it necessarily refers to a complex | |
7845 | symbol; merely that it is a self-describing CGEN based reloc. | |
7846 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 7847 | word size, etc) encoded within it.). */ |
d9352518 | 7848 | |
a0c8462f AM |
7849 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
7850 | &chunksz, &lsb0_p, &signed_p, | |
7851 | &trunc_p, rel->r_addend); | |
d9352518 DB |
7852 | |
7853 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
7854 | ||
7855 | if (lsb0_p) | |
7856 | shift = (start + 1) - len; | |
7857 | else | |
7858 | shift = (8 * wordsz) - (start + len); | |
7859 | ||
5dabe785 | 7860 | /* FIXME: octets_per_byte. */ |
a0c8462f | 7861 | x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset); |
d9352518 DB |
7862 | |
7863 | #ifdef DEBUG | |
7864 | printf ("Doing complex reloc: " | |
7865 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
7866 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
7867 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
7868 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
7869 | oplen, (unsigned long) x, (unsigned long) mask, |
7870 | (unsigned long) relocation); | |
d9352518 DB |
7871 | #endif |
7872 | ||
cdfeee4f | 7873 | r = bfd_reloc_ok; |
d9352518 | 7874 | if (! trunc_p) |
cdfeee4f AM |
7875 | /* Now do an overflow check. */ |
7876 | r = bfd_check_overflow ((signed_p | |
7877 | ? complain_overflow_signed | |
7878 | : complain_overflow_unsigned), | |
7879 | len, 0, (8 * wordsz), | |
7880 | relocation); | |
a0c8462f | 7881 | |
d9352518 DB |
7882 | /* Do the deed. */ |
7883 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
7884 | ||
7885 | #ifdef DEBUG | |
7886 | printf (" relocation: %8.8lx\n" | |
7887 | " shifted mask: %8.8lx\n" | |
7888 | " shifted/masked reloc: %8.8lx\n" | |
7889 | " result: %8.8lx\n", | |
9ccb8af9 AM |
7890 | (unsigned long) relocation, (unsigned long) (mask << shift), |
7891 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 7892 | #endif |
5dabe785 | 7893 | /* FIXME: octets_per_byte. */ |
d9352518 | 7894 | put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset); |
cdfeee4f | 7895 | return r; |
d9352518 DB |
7896 | } |
7897 | ||
c152c796 AM |
7898 | /* When performing a relocatable link, the input relocations are |
7899 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
7900 | referenced must be updated. Update all the relocations found in |
7901 | RELDATA. */ | |
c152c796 AM |
7902 | |
7903 | static void | |
7904 | elf_link_adjust_relocs (bfd *abfd, | |
d4730f92 | 7905 | struct bfd_elf_section_reloc_data *reldata) |
c152c796 AM |
7906 | { |
7907 | unsigned int i; | |
7908 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7909 | bfd_byte *erela; | |
7910 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
7911 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
7912 | bfd_vma r_type_mask; | |
7913 | int r_sym_shift; | |
d4730f92 BS |
7914 | unsigned int count = reldata->count; |
7915 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 7916 | |
d4730f92 | 7917 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
7918 | { |
7919 | swap_in = bed->s->swap_reloc_in; | |
7920 | swap_out = bed->s->swap_reloc_out; | |
7921 | } | |
d4730f92 | 7922 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
7923 | { |
7924 | swap_in = bed->s->swap_reloca_in; | |
7925 | swap_out = bed->s->swap_reloca_out; | |
7926 | } | |
7927 | else | |
7928 | abort (); | |
7929 | ||
7930 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
7931 | abort (); | |
7932 | ||
7933 | if (bed->s->arch_size == 32) | |
7934 | { | |
7935 | r_type_mask = 0xff; | |
7936 | r_sym_shift = 8; | |
7937 | } | |
7938 | else | |
7939 | { | |
7940 | r_type_mask = 0xffffffff; | |
7941 | r_sym_shift = 32; | |
7942 | } | |
7943 | ||
d4730f92 BS |
7944 | erela = reldata->hdr->contents; |
7945 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
7946 | { |
7947 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
7948 | unsigned int j; | |
7949 | ||
7950 | if (*rel_hash == NULL) | |
7951 | continue; | |
7952 | ||
7953 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
7954 | ||
7955 | (*swap_in) (abfd, erela, irela); | |
7956 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
7957 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
7958 | | (irela[j].r_info & r_type_mask)); | |
7959 | (*swap_out) (abfd, irela, erela); | |
7960 | } | |
7961 | } | |
7962 | ||
7963 | struct elf_link_sort_rela | |
7964 | { | |
7965 | union { | |
7966 | bfd_vma offset; | |
7967 | bfd_vma sym_mask; | |
7968 | } u; | |
7969 | enum elf_reloc_type_class type; | |
7970 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
7971 | Elf_Internal_Rela rela[1]; | |
7972 | }; | |
7973 | ||
7974 | static int | |
7975 | elf_link_sort_cmp1 (const void *A, const void *B) | |
7976 | { | |
a50b1753 NC |
7977 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
7978 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
7979 | int relativea, relativeb; |
7980 | ||
7981 | relativea = a->type == reloc_class_relative; | |
7982 | relativeb = b->type == reloc_class_relative; | |
7983 | ||
7984 | if (relativea < relativeb) | |
7985 | return 1; | |
7986 | if (relativea > relativeb) | |
7987 | return -1; | |
7988 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
7989 | return -1; | |
7990 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
7991 | return 1; | |
7992 | if (a->rela->r_offset < b->rela->r_offset) | |
7993 | return -1; | |
7994 | if (a->rela->r_offset > b->rela->r_offset) | |
7995 | return 1; | |
7996 | return 0; | |
7997 | } | |
7998 | ||
7999 | static int | |
8000 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8001 | { | |
a50b1753 NC |
8002 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8003 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8004 | int copya, copyb; |
8005 | ||
8006 | if (a->u.offset < b->u.offset) | |
8007 | return -1; | |
8008 | if (a->u.offset > b->u.offset) | |
8009 | return 1; | |
8010 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
8011 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
8012 | if (copya < copyb) | |
8013 | return -1; | |
8014 | if (copya > copyb) | |
8015 | return 1; | |
8016 | if (a->rela->r_offset < b->rela->r_offset) | |
8017 | return -1; | |
8018 | if (a->rela->r_offset > b->rela->r_offset) | |
8019 | return 1; | |
8020 | return 0; | |
8021 | } | |
8022 | ||
8023 | static size_t | |
8024 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
8025 | { | |
3410fea8 | 8026 | asection *dynamic_relocs; |
fc66a176 L |
8027 | asection *rela_dyn; |
8028 | asection *rel_dyn; | |
c152c796 AM |
8029 | bfd_size_type count, size; |
8030 | size_t i, ret, sort_elt, ext_size; | |
8031 | bfd_byte *sort, *s_non_relative, *p; | |
8032 | struct elf_link_sort_rela *sq; | |
8033 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8034 | int i2e = bed->s->int_rels_per_ext_rel; | |
8035 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8036 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8037 | struct bfd_link_order *lo; | |
8038 | bfd_vma r_sym_mask; | |
3410fea8 | 8039 | bfd_boolean use_rela; |
c152c796 | 8040 | |
3410fea8 NC |
8041 | /* Find a dynamic reloc section. */ |
8042 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
8043 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
8044 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
8045 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 8046 | { |
3410fea8 NC |
8047 | bfd_boolean use_rela_initialised = FALSE; |
8048 | ||
8049 | /* This is just here to stop gcc from complaining. | |
8050 | It's initialization checking code is not perfect. */ | |
8051 | use_rela = TRUE; | |
8052 | ||
8053 | /* Both sections are present. Examine the sizes | |
8054 | of the indirect sections to help us choose. */ | |
8055 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8056 | if (lo->type == bfd_indirect_link_order) | |
8057 | { | |
8058 | asection *o = lo->u.indirect.section; | |
8059 | ||
8060 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8061 | { | |
8062 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8063 | /* Section size is divisible by both rel and rela sizes. | |
8064 | It is of no help to us. */ | |
8065 | ; | |
8066 | else | |
8067 | { | |
8068 | /* Section size is only divisible by rela. */ | |
8069 | if (use_rela_initialised && (use_rela == FALSE)) | |
8070 | { | |
8071 | _bfd_error_handler | |
8072 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
8073 | bfd_set_error (bfd_error_invalid_operation); | |
8074 | return 0; | |
8075 | } | |
8076 | else | |
8077 | { | |
8078 | use_rela = TRUE; | |
8079 | use_rela_initialised = TRUE; | |
8080 | } | |
8081 | } | |
8082 | } | |
8083 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8084 | { | |
8085 | /* Section size is only divisible by rel. */ | |
8086 | if (use_rela_initialised && (use_rela == TRUE)) | |
8087 | { | |
8088 | _bfd_error_handler | |
8089 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
8090 | bfd_set_error (bfd_error_invalid_operation); | |
8091 | return 0; | |
8092 | } | |
8093 | else | |
8094 | { | |
8095 | use_rela = FALSE; | |
8096 | use_rela_initialised = TRUE; | |
8097 | } | |
8098 | } | |
8099 | else | |
8100 | { | |
8101 | /* The section size is not divisible by either - something is wrong. */ | |
8102 | _bfd_error_handler | |
8103 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
8104 | bfd_set_error (bfd_error_invalid_operation); | |
8105 | return 0; | |
8106 | } | |
8107 | } | |
8108 | ||
8109 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8110 | if (lo->type == bfd_indirect_link_order) | |
8111 | { | |
8112 | asection *o = lo->u.indirect.section; | |
8113 | ||
8114 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8115 | { | |
8116 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8117 | /* Section size is divisible by both rel and rela sizes. | |
8118 | It is of no help to us. */ | |
8119 | ; | |
8120 | else | |
8121 | { | |
8122 | /* Section size is only divisible by rela. */ | |
8123 | if (use_rela_initialised && (use_rela == FALSE)) | |
8124 | { | |
8125 | _bfd_error_handler | |
8126 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
8127 | bfd_set_error (bfd_error_invalid_operation); | |
8128 | return 0; | |
8129 | } | |
8130 | else | |
8131 | { | |
8132 | use_rela = TRUE; | |
8133 | use_rela_initialised = TRUE; | |
8134 | } | |
8135 | } | |
8136 | } | |
8137 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8138 | { | |
8139 | /* Section size is only divisible by rel. */ | |
8140 | if (use_rela_initialised && (use_rela == TRUE)) | |
8141 | { | |
8142 | _bfd_error_handler | |
8143 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
8144 | bfd_set_error (bfd_error_invalid_operation); | |
8145 | return 0; | |
8146 | } | |
8147 | else | |
8148 | { | |
8149 | use_rela = FALSE; | |
8150 | use_rela_initialised = TRUE; | |
8151 | } | |
8152 | } | |
8153 | else | |
8154 | { | |
8155 | /* The section size is not divisible by either - something is wrong. */ | |
8156 | _bfd_error_handler | |
8157 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
8158 | bfd_set_error (bfd_error_invalid_operation); | |
8159 | return 0; | |
8160 | } | |
8161 | } | |
8162 | ||
8163 | if (! use_rela_initialised) | |
8164 | /* Make a guess. */ | |
8165 | use_rela = TRUE; | |
c152c796 | 8166 | } |
fc66a176 L |
8167 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
8168 | use_rela = TRUE; | |
8169 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 8170 | use_rela = FALSE; |
c152c796 | 8171 | else |
fc66a176 | 8172 | return 0; |
3410fea8 NC |
8173 | |
8174 | if (use_rela) | |
c152c796 | 8175 | { |
3410fea8 | 8176 | dynamic_relocs = rela_dyn; |
c152c796 AM |
8177 | ext_size = bed->s->sizeof_rela; |
8178 | swap_in = bed->s->swap_reloca_in; | |
8179 | swap_out = bed->s->swap_reloca_out; | |
8180 | } | |
3410fea8 NC |
8181 | else |
8182 | { | |
8183 | dynamic_relocs = rel_dyn; | |
8184 | ext_size = bed->s->sizeof_rel; | |
8185 | swap_in = bed->s->swap_reloc_in; | |
8186 | swap_out = bed->s->swap_reloc_out; | |
8187 | } | |
c152c796 AM |
8188 | |
8189 | size = 0; | |
3410fea8 | 8190 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 8191 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 8192 | size += lo->u.indirect.section->size; |
c152c796 | 8193 | |
3410fea8 | 8194 | if (size != dynamic_relocs->size) |
c152c796 AM |
8195 | return 0; |
8196 | ||
8197 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
8198 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
8199 | |
8200 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
8201 | if (count == 0) |
8202 | return 0; | |
a50b1753 | 8203 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 8204 | |
c152c796 AM |
8205 | if (sort == NULL) |
8206 | { | |
8207 | (*info->callbacks->warning) | |
8208 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
8209 | return 0; | |
8210 | } | |
8211 | ||
8212 | if (bed->s->arch_size == 32) | |
8213 | r_sym_mask = ~(bfd_vma) 0xff; | |
8214 | else | |
8215 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
8216 | ||
3410fea8 | 8217 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8218 | if (lo->type == bfd_indirect_link_order) |
8219 | { | |
8220 | bfd_byte *erel, *erelend; | |
8221 | asection *o = lo->u.indirect.section; | |
8222 | ||
1da212d6 AM |
8223 | if (o->contents == NULL && o->size != 0) |
8224 | { | |
8225 | /* This is a reloc section that is being handled as a normal | |
8226 | section. See bfd_section_from_shdr. We can't combine | |
8227 | relocs in this case. */ | |
8228 | free (sort); | |
8229 | return 0; | |
8230 | } | |
c152c796 | 8231 | erel = o->contents; |
eea6121a | 8232 | erelend = o->contents + o->size; |
5dabe785 | 8233 | /* FIXME: octets_per_byte. */ |
c152c796 | 8234 | p = sort + o->output_offset / ext_size * sort_elt; |
3410fea8 | 8235 | |
c152c796 AM |
8236 | while (erel < erelend) |
8237 | { | |
8238 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 8239 | |
c152c796 AM |
8240 | (*swap_in) (abfd, erel, s->rela); |
8241 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
8242 | s->u.sym_mask = r_sym_mask; | |
8243 | p += sort_elt; | |
8244 | erel += ext_size; | |
8245 | } | |
8246 | } | |
8247 | ||
8248 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
8249 | ||
8250 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
8251 | { | |
8252 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8253 | if (s->type != reloc_class_relative) | |
8254 | break; | |
8255 | } | |
8256 | ret = i; | |
8257 | s_non_relative = p; | |
8258 | ||
8259 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
8260 | for (; i < count; i++, p += sort_elt) | |
8261 | { | |
8262 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
8263 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
8264 | sq = sp; | |
8265 | sp->u.offset = sq->rela->r_offset; | |
8266 | } | |
8267 | ||
8268 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
8269 | ||
3410fea8 | 8270 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8271 | if (lo->type == bfd_indirect_link_order) |
8272 | { | |
8273 | bfd_byte *erel, *erelend; | |
8274 | asection *o = lo->u.indirect.section; | |
8275 | ||
8276 | erel = o->contents; | |
eea6121a | 8277 | erelend = o->contents + o->size; |
5dabe785 | 8278 | /* FIXME: octets_per_byte. */ |
c152c796 AM |
8279 | p = sort + o->output_offset / ext_size * sort_elt; |
8280 | while (erel < erelend) | |
8281 | { | |
8282 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8283 | (*swap_out) (abfd, s->rela, erel); | |
8284 | p += sort_elt; | |
8285 | erel += ext_size; | |
8286 | } | |
8287 | } | |
8288 | ||
8289 | free (sort); | |
3410fea8 | 8290 | *psec = dynamic_relocs; |
c152c796 AM |
8291 | return ret; |
8292 | } | |
8293 | ||
8294 | /* Flush the output symbols to the file. */ | |
8295 | ||
8296 | static bfd_boolean | |
8297 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
8298 | const struct elf_backend_data *bed) | |
8299 | { | |
8300 | if (finfo->symbuf_count > 0) | |
8301 | { | |
8302 | Elf_Internal_Shdr *hdr; | |
8303 | file_ptr pos; | |
8304 | bfd_size_type amt; | |
8305 | ||
8306 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
8307 | pos = hdr->sh_offset + hdr->sh_size; | |
8308 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
8309 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
8310 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
8311 | return FALSE; | |
8312 | ||
8313 | hdr->sh_size += amt; | |
8314 | finfo->symbuf_count = 0; | |
8315 | } | |
8316 | ||
8317 | return TRUE; | |
8318 | } | |
8319 | ||
8320 | /* Add a symbol to the output symbol table. */ | |
8321 | ||
6e0b88f1 | 8322 | static int |
c152c796 AM |
8323 | elf_link_output_sym (struct elf_final_link_info *finfo, |
8324 | const char *name, | |
8325 | Elf_Internal_Sym *elfsym, | |
8326 | asection *input_sec, | |
8327 | struct elf_link_hash_entry *h) | |
8328 | { | |
8329 | bfd_byte *dest; | |
8330 | Elf_External_Sym_Shndx *destshndx; | |
6e0b88f1 | 8331 | int (*output_symbol_hook) |
c152c796 AM |
8332 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
8333 | struct elf_link_hash_entry *); | |
8334 | const struct elf_backend_data *bed; | |
8335 | ||
8336 | bed = get_elf_backend_data (finfo->output_bfd); | |
8337 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
8338 | if (output_symbol_hook != NULL) | |
8339 | { | |
6e0b88f1 AM |
8340 | int ret = (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h); |
8341 | if (ret != 1) | |
8342 | return ret; | |
c152c796 AM |
8343 | } |
8344 | ||
8345 | if (name == NULL || *name == '\0') | |
8346 | elfsym->st_name = 0; | |
8347 | else if (input_sec->flags & SEC_EXCLUDE) | |
8348 | elfsym->st_name = 0; | |
8349 | else | |
8350 | { | |
8351 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
8352 | name, TRUE, FALSE); | |
8353 | if (elfsym->st_name == (unsigned long) -1) | |
6e0b88f1 | 8354 | return 0; |
c152c796 AM |
8355 | } |
8356 | ||
8357 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
8358 | { | |
8359 | if (! elf_link_flush_output_syms (finfo, bed)) | |
6e0b88f1 | 8360 | return 0; |
c152c796 AM |
8361 | } |
8362 | ||
8363 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
8364 | destshndx = finfo->symshndxbuf; | |
8365 | if (destshndx != NULL) | |
8366 | { | |
8367 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
8368 | { | |
8369 | bfd_size_type amt; | |
8370 | ||
8371 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 NC |
8372 | destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx, |
8373 | amt * 2); | |
c152c796 | 8374 | if (destshndx == NULL) |
6e0b88f1 | 8375 | return 0; |
515ef31d | 8376 | finfo->symshndxbuf = destshndx; |
c152c796 AM |
8377 | memset ((char *) destshndx + amt, 0, amt); |
8378 | finfo->shndxbuf_size *= 2; | |
8379 | } | |
8380 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
8381 | } | |
8382 | ||
8383 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
8384 | finfo->symbuf_count += 1; | |
8385 | bfd_get_symcount (finfo->output_bfd) += 1; | |
8386 | ||
6e0b88f1 | 8387 | return 1; |
c152c796 AM |
8388 | } |
8389 | ||
c0d5a53d L |
8390 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
8391 | ||
8392 | static bfd_boolean | |
8393 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
8394 | { | |
4fbb74a6 AM |
8395 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
8396 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
8397 | { |
8398 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 8399 | beyond 64k. */ |
c0d5a53d L |
8400 | (*_bfd_error_handler) |
8401 | (_("%B: Too many sections: %d (>= %d)"), | |
4fbb74a6 | 8402 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
8403 | bfd_set_error (bfd_error_nonrepresentable_section); |
8404 | return FALSE; | |
8405 | } | |
8406 | return TRUE; | |
8407 | } | |
8408 | ||
c152c796 AM |
8409 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
8410 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
8411 | versioned symbol that would normally require an explicit version. | |
8412 | We also handle the case that a DSO references a hidden symbol | |
8413 | which may be satisfied by a versioned symbol in another DSO. */ | |
8414 | ||
8415 | static bfd_boolean | |
8416 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
8417 | const struct elf_backend_data *bed, | |
8418 | struct elf_link_hash_entry *h) | |
8419 | { | |
8420 | bfd *abfd; | |
8421 | struct elf_link_loaded_list *loaded; | |
8422 | ||
8423 | if (!is_elf_hash_table (info->hash)) | |
8424 | return FALSE; | |
8425 | ||
8426 | switch (h->root.type) | |
8427 | { | |
8428 | default: | |
8429 | abfd = NULL; | |
8430 | break; | |
8431 | ||
8432 | case bfd_link_hash_undefined: | |
8433 | case bfd_link_hash_undefweak: | |
8434 | abfd = h->root.u.undef.abfd; | |
8435 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 8436 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
8437 | return FALSE; |
8438 | break; | |
8439 | ||
8440 | case bfd_link_hash_defined: | |
8441 | case bfd_link_hash_defweak: | |
8442 | abfd = h->root.u.def.section->owner; | |
8443 | break; | |
8444 | ||
8445 | case bfd_link_hash_common: | |
8446 | abfd = h->root.u.c.p->section->owner; | |
8447 | break; | |
8448 | } | |
8449 | BFD_ASSERT (abfd != NULL); | |
8450 | ||
8451 | for (loaded = elf_hash_table (info)->loaded; | |
8452 | loaded != NULL; | |
8453 | loaded = loaded->next) | |
8454 | { | |
8455 | bfd *input; | |
8456 | Elf_Internal_Shdr *hdr; | |
8457 | bfd_size_type symcount; | |
8458 | bfd_size_type extsymcount; | |
8459 | bfd_size_type extsymoff; | |
8460 | Elf_Internal_Shdr *versymhdr; | |
8461 | Elf_Internal_Sym *isym; | |
8462 | Elf_Internal_Sym *isymend; | |
8463 | Elf_Internal_Sym *isymbuf; | |
8464 | Elf_External_Versym *ever; | |
8465 | Elf_External_Versym *extversym; | |
8466 | ||
8467 | input = loaded->abfd; | |
8468 | ||
8469 | /* We check each DSO for a possible hidden versioned definition. */ | |
8470 | if (input == abfd | |
8471 | || (input->flags & DYNAMIC) == 0 | |
8472 | || elf_dynversym (input) == 0) | |
8473 | continue; | |
8474 | ||
8475 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
8476 | ||
8477 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
8478 | if (elf_bad_symtab (input)) | |
8479 | { | |
8480 | extsymcount = symcount; | |
8481 | extsymoff = 0; | |
8482 | } | |
8483 | else | |
8484 | { | |
8485 | extsymcount = symcount - hdr->sh_info; | |
8486 | extsymoff = hdr->sh_info; | |
8487 | } | |
8488 | ||
8489 | if (extsymcount == 0) | |
8490 | continue; | |
8491 | ||
8492 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
8493 | NULL, NULL, NULL); | |
8494 | if (isymbuf == NULL) | |
8495 | return FALSE; | |
8496 | ||
8497 | /* Read in any version definitions. */ | |
8498 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 8499 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
8500 | if (extversym == NULL) |
8501 | goto error_ret; | |
8502 | ||
8503 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
8504 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
8505 | != versymhdr->sh_size)) | |
8506 | { | |
8507 | free (extversym); | |
8508 | error_ret: | |
8509 | free (isymbuf); | |
8510 | return FALSE; | |
8511 | } | |
8512 | ||
8513 | ever = extversym + extsymoff; | |
8514 | isymend = isymbuf + extsymcount; | |
8515 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
8516 | { | |
8517 | const char *name; | |
8518 | Elf_Internal_Versym iver; | |
8519 | unsigned short version_index; | |
8520 | ||
8521 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
8522 | || isym->st_shndx == SHN_UNDEF) | |
8523 | continue; | |
8524 | ||
8525 | name = bfd_elf_string_from_elf_section (input, | |
8526 | hdr->sh_link, | |
8527 | isym->st_name); | |
8528 | if (strcmp (name, h->root.root.string) != 0) | |
8529 | continue; | |
8530 | ||
8531 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
8532 | ||
d023c380 L |
8533 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
8534 | && !(h->def_regular | |
8535 | && h->forced_local)) | |
c152c796 AM |
8536 | { |
8537 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
8538 | have provided a definition for the undefined sym unless |
8539 | it is defined in a non-shared object and forced local. | |
8540 | */ | |
c152c796 AM |
8541 | abort (); |
8542 | } | |
8543 | ||
8544 | version_index = iver.vs_vers & VERSYM_VERSION; | |
8545 | if (version_index == 1 || version_index == 2) | |
8546 | { | |
8547 | /* This is the base or first version. We can use it. */ | |
8548 | free (extversym); | |
8549 | free (isymbuf); | |
8550 | return TRUE; | |
8551 | } | |
8552 | } | |
8553 | ||
8554 | free (extversym); | |
8555 | free (isymbuf); | |
8556 | } | |
8557 | ||
8558 | return FALSE; | |
8559 | } | |
8560 | ||
8561 | /* Add an external symbol to the symbol table. This is called from | |
8562 | the hash table traversal routine. When generating a shared object, | |
8563 | we go through the symbol table twice. The first time we output | |
8564 | anything that might have been forced to local scope in a version | |
8565 | script. The second time we output the symbols that are still | |
8566 | global symbols. */ | |
8567 | ||
8568 | static bfd_boolean | |
7686d77d | 8569 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 8570 | { |
7686d77d | 8571 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 8572 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
c152c796 AM |
8573 | struct elf_final_link_info *finfo = eoinfo->finfo; |
8574 | bfd_boolean strip; | |
8575 | Elf_Internal_Sym sym; | |
8576 | asection *input_sec; | |
8577 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
8578 | long indx; |
8579 | int ret; | |
c152c796 AM |
8580 | |
8581 | if (h->root.type == bfd_link_hash_warning) | |
8582 | { | |
8583 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8584 | if (h->root.type == bfd_link_hash_new) | |
8585 | return TRUE; | |
8586 | } | |
8587 | ||
8588 | /* Decide whether to output this symbol in this pass. */ | |
8589 | if (eoinfo->localsyms) | |
8590 | { | |
f5385ebf | 8591 | if (!h->forced_local) |
c152c796 AM |
8592 | return TRUE; |
8593 | } | |
8594 | else | |
8595 | { | |
f5385ebf | 8596 | if (h->forced_local) |
c152c796 AM |
8597 | return TRUE; |
8598 | } | |
8599 | ||
8600 | bed = get_elf_backend_data (finfo->output_bfd); | |
8601 | ||
12ac1cf5 | 8602 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 8603 | { |
12ac1cf5 NC |
8604 | /* If we have an undefined symbol reference here then it must have |
8605 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
8606 | references in regular files have already been handled unless |
8607 | they are in unreferenced sections which are removed by garbage | |
8608 | collection). */ | |
12ac1cf5 NC |
8609 | bfd_boolean ignore_undef = FALSE; |
8610 | ||
8611 | /* Some symbols may be special in that the fact that they're | |
8612 | undefined can be safely ignored - let backend determine that. */ | |
8613 | if (bed->elf_backend_ignore_undef_symbol) | |
8614 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
8615 | ||
8616 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 8617 | if (!ignore_undef |
12ac1cf5 | 8618 | && h->ref_dynamic |
98da7939 | 8619 | && (!h->ref_regular || finfo->info->gc_sections) |
12ac1cf5 NC |
8620 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) |
8621 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
c152c796 | 8622 | { |
12ac1cf5 | 8623 | if (! (finfo->info->callbacks->undefined_symbol |
98da7939 L |
8624 | (finfo->info, h->root.root.string, |
8625 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
12ac1cf5 NC |
8626 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) |
8627 | { | |
17d078c5 | 8628 | bfd_set_error (bfd_error_bad_value); |
12ac1cf5 NC |
8629 | eoinfo->failed = TRUE; |
8630 | return FALSE; | |
8631 | } | |
c152c796 AM |
8632 | } |
8633 | } | |
8634 | ||
8635 | /* We should also warn if a forced local symbol is referenced from | |
8636 | shared libraries. */ | |
8637 | if (! finfo->info->relocatable | |
8638 | && (! finfo->info->shared) | |
f5385ebf AM |
8639 | && h->forced_local |
8640 | && h->ref_dynamic | |
8641 | && !h->dynamic_def | |
8642 | && !h->dynamic_weak | |
c152c796 AM |
8643 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
8644 | { | |
17d078c5 AM |
8645 | bfd *def_bfd; |
8646 | const char *msg; | |
8647 | ||
8648 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
8649 | msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); | |
8650 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) | |
8651 | msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); | |
8652 | else | |
8653 | msg = _("%B: local symbol `%s' in %B is referenced by DSO"); | |
8654 | def_bfd = finfo->output_bfd; | |
8655 | if (h->root.u.def.section != bfd_abs_section_ptr) | |
8656 | def_bfd = h->root.u.def.section->owner; | |
8657 | (*_bfd_error_handler) (msg, finfo->output_bfd, def_bfd, | |
8658 | h->root.root.string); | |
8659 | bfd_set_error (bfd_error_bad_value); | |
c152c796 AM |
8660 | eoinfo->failed = TRUE; |
8661 | return FALSE; | |
8662 | } | |
8663 | ||
8664 | /* We don't want to output symbols that have never been mentioned by | |
8665 | a regular file, or that we have been told to strip. However, if | |
8666 | h->indx is set to -2, the symbol is used by a reloc and we must | |
8667 | output it. */ | |
8668 | if (h->indx == -2) | |
8669 | strip = FALSE; | |
f5385ebf | 8670 | else if ((h->def_dynamic |
77cfaee6 AM |
8671 | || h->ref_dynamic |
8672 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
8673 | && !h->def_regular |
8674 | && !h->ref_regular) | |
c152c796 AM |
8675 | strip = TRUE; |
8676 | else if (finfo->info->strip == strip_all) | |
8677 | strip = TRUE; | |
8678 | else if (finfo->info->strip == strip_some | |
8679 | && bfd_hash_lookup (finfo->info->keep_hash, | |
8680 | h->root.root.string, FALSE, FALSE) == NULL) | |
8681 | strip = TRUE; | |
8682 | else if (finfo->info->strip_discarded | |
8683 | && (h->root.type == bfd_link_hash_defined | |
8684 | || h->root.type == bfd_link_hash_defweak) | |
8685 | && elf_discarded_section (h->root.u.def.section)) | |
8686 | strip = TRUE; | |
9e2278f5 AM |
8687 | else if ((h->root.type == bfd_link_hash_undefined |
8688 | || h->root.type == bfd_link_hash_undefweak) | |
8689 | && h->root.u.undef.abfd != NULL | |
8690 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
8691 | strip = TRUE; | |
c152c796 AM |
8692 | else |
8693 | strip = FALSE; | |
8694 | ||
8695 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
57ca8ac7 L |
8696 | nothing else to do unless it is a forced local symbol or a |
8697 | STT_GNU_IFUNC symbol. */ | |
c152c796 AM |
8698 | if (strip |
8699 | && h->dynindx == -1 | |
57ca8ac7 | 8700 | && h->type != STT_GNU_IFUNC |
f5385ebf | 8701 | && !h->forced_local) |
c152c796 AM |
8702 | return TRUE; |
8703 | ||
8704 | sym.st_value = 0; | |
8705 | sym.st_size = h->size; | |
8706 | sym.st_other = h->other; | |
f5385ebf | 8707 | if (h->forced_local) |
935bd1e0 L |
8708 | { |
8709 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); | |
8710 | /* Turn off visibility on local symbol. */ | |
8711 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
8712 | } | |
3e7a7d11 NC |
8713 | else if (h->unique_global) |
8714 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type); | |
c152c796 AM |
8715 | else if (h->root.type == bfd_link_hash_undefweak |
8716 | || h->root.type == bfd_link_hash_defweak) | |
8717 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
8718 | else | |
8719 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
35fc36a8 | 8720 | sym.st_target_internal = h->target_internal; |
c152c796 AM |
8721 | |
8722 | switch (h->root.type) | |
8723 | { | |
8724 | default: | |
8725 | case bfd_link_hash_new: | |
8726 | case bfd_link_hash_warning: | |
8727 | abort (); | |
8728 | return FALSE; | |
8729 | ||
8730 | case bfd_link_hash_undefined: | |
8731 | case bfd_link_hash_undefweak: | |
8732 | input_sec = bfd_und_section_ptr; | |
8733 | sym.st_shndx = SHN_UNDEF; | |
8734 | break; | |
8735 | ||
8736 | case bfd_link_hash_defined: | |
8737 | case bfd_link_hash_defweak: | |
8738 | { | |
8739 | input_sec = h->root.u.def.section; | |
8740 | if (input_sec->output_section != NULL) | |
8741 | { | |
8742 | sym.st_shndx = | |
8743 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
8744 | input_sec->output_section); | |
8745 | if (sym.st_shndx == SHN_BAD) | |
8746 | { | |
8747 | (*_bfd_error_handler) | |
d003868e AM |
8748 | (_("%B: could not find output section %A for input section %A"), |
8749 | finfo->output_bfd, input_sec->output_section, input_sec); | |
17d078c5 | 8750 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
8751 | eoinfo->failed = TRUE; |
8752 | return FALSE; | |
8753 | } | |
8754 | ||
8755 | /* ELF symbols in relocatable files are section relative, | |
8756 | but in nonrelocatable files they are virtual | |
8757 | addresses. */ | |
8758 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
8759 | if (! finfo->info->relocatable) | |
8760 | { | |
8761 | sym.st_value += input_sec->output_section->vma; | |
8762 | if (h->type == STT_TLS) | |
8763 | { | |
430a16a5 NC |
8764 | asection *tls_sec = elf_hash_table (finfo->info)->tls_sec; |
8765 | if (tls_sec != NULL) | |
8766 | sym.st_value -= tls_sec->vma; | |
8767 | else | |
8768 | { | |
8769 | /* The TLS section may have been garbage collected. */ | |
8770 | BFD_ASSERT (finfo->info->gc_sections | |
8771 | && !input_sec->gc_mark); | |
8772 | } | |
c152c796 AM |
8773 | } |
8774 | } | |
8775 | } | |
8776 | else | |
8777 | { | |
8778 | BFD_ASSERT (input_sec->owner == NULL | |
8779 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
8780 | sym.st_shndx = SHN_UNDEF; | |
8781 | input_sec = bfd_und_section_ptr; | |
8782 | } | |
8783 | } | |
8784 | break; | |
8785 | ||
8786 | case bfd_link_hash_common: | |
8787 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 8788 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
8789 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
8790 | break; | |
8791 | ||
8792 | case bfd_link_hash_indirect: | |
8793 | /* These symbols are created by symbol versioning. They point | |
8794 | to the decorated version of the name. For example, if the | |
8795 | symbol foo@@GNU_1.2 is the default, which should be used when | |
8796 | foo is used with no version, then we add an indirect symbol | |
8797 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
8798 | since the indirected symbol is already in the hash table. */ | |
8799 | return TRUE; | |
8800 | } | |
8801 | ||
8802 | /* Give the processor backend a chance to tweak the symbol value, | |
8803 | and also to finish up anything that needs to be done for this | |
8804 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 8805 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 8806 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 8807 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 8808 | && h->def_regular |
3aa14d16 L |
8809 | && !finfo->info->relocatable) |
8810 | || ((h->dynindx != -1 | |
8811 | || h->forced_local) | |
8812 | && ((finfo->info->shared | |
8813 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
8814 | || h->root.type != bfd_link_hash_undefweak)) | |
8815 | || !h->forced_local) | |
8816 | && elf_hash_table (finfo->info)->dynamic_sections_created)) | |
c152c796 AM |
8817 | { |
8818 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8819 | (finfo->output_bfd, finfo->info, h, &sym))) | |
8820 | { | |
8821 | eoinfo->failed = TRUE; | |
8822 | return FALSE; | |
8823 | } | |
8824 | } | |
8825 | ||
8826 | /* If we are marking the symbol as undefined, and there are no | |
8827 | non-weak references to this symbol from a regular object, then | |
8828 | mark the symbol as weak undefined; if there are non-weak | |
8829 | references, mark the symbol as strong. We can't do this earlier, | |
8830 | because it might not be marked as undefined until the | |
8831 | finish_dynamic_symbol routine gets through with it. */ | |
8832 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 8833 | && h->ref_regular |
c152c796 AM |
8834 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
8835 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
8836 | { | |
8837 | int bindtype; | |
2955ec4c L |
8838 | unsigned int type = ELF_ST_TYPE (sym.st_info); |
8839 | ||
8840 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
8841 | if (type == STT_GNU_IFUNC) | |
8842 | type = STT_FUNC; | |
c152c796 | 8843 | |
f5385ebf | 8844 | if (h->ref_regular_nonweak) |
c152c796 AM |
8845 | bindtype = STB_GLOBAL; |
8846 | else | |
8847 | bindtype = STB_WEAK; | |
2955ec4c | 8848 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
8849 | } |
8850 | ||
bda987c2 CD |
8851 | /* If this is a symbol defined in a dynamic library, don't use the |
8852 | symbol size from the dynamic library. Relinking an executable | |
8853 | against a new library may introduce gratuitous changes in the | |
8854 | executable's symbols if we keep the size. */ | |
8855 | if (sym.st_shndx == SHN_UNDEF | |
8856 | && !h->def_regular | |
8857 | && h->def_dynamic) | |
8858 | sym.st_size = 0; | |
8859 | ||
c152c796 AM |
8860 | /* If a non-weak symbol with non-default visibility is not defined |
8861 | locally, it is a fatal error. */ | |
8862 | if (! finfo->info->relocatable | |
8863 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
8864 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
8865 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 8866 | && !h->def_regular) |
c152c796 | 8867 | { |
17d078c5 AM |
8868 | const char *msg; |
8869 | ||
8870 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
8871 | msg = _("%B: protected symbol `%s' isn't defined"); | |
8872 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) | |
8873 | msg = _("%B: internal symbol `%s' isn't defined"); | |
8874 | else | |
8875 | msg = _("%B: hidden symbol `%s' isn't defined"); | |
8876 | (*_bfd_error_handler) (msg, finfo->output_bfd, h->root.root.string); | |
8877 | bfd_set_error (bfd_error_bad_value); | |
c152c796 AM |
8878 | eoinfo->failed = TRUE; |
8879 | return FALSE; | |
8880 | } | |
8881 | ||
8882 | /* If this symbol should be put in the .dynsym section, then put it | |
8883 | there now. We already know the symbol index. We also fill in | |
8884 | the entry in the .hash section. */ | |
8885 | if (h->dynindx != -1 | |
8886 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
8887 | { | |
c152c796 AM |
8888 | bfd_byte *esym; |
8889 | ||
8890 | sym.st_name = h->dynstr_index; | |
8891 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
c0d5a53d L |
8892 | if (! check_dynsym (finfo->output_bfd, &sym)) |
8893 | { | |
8894 | eoinfo->failed = TRUE; | |
8895 | return FALSE; | |
8896 | } | |
c152c796 AM |
8897 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); |
8898 | ||
fdc90cb4 JJ |
8899 | if (finfo->hash_sec != NULL) |
8900 | { | |
8901 | size_t hash_entry_size; | |
8902 | bfd_byte *bucketpos; | |
8903 | bfd_vma chain; | |
41198d0c L |
8904 | size_t bucketcount; |
8905 | size_t bucket; | |
8906 | ||
8907 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
8908 | bucket = h->u.elf_hash_value % bucketcount; | |
fdc90cb4 JJ |
8909 | |
8910 | hash_entry_size | |
8911 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
8912 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
8913 | + (bucket + 2) * hash_entry_size); | |
8914 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
8915 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
8916 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
8917 | ((bfd_byte *) finfo->hash_sec->contents | |
8918 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
8919 | } | |
c152c796 AM |
8920 | |
8921 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
8922 | { | |
8923 | Elf_Internal_Versym iversym; | |
8924 | Elf_External_Versym *eversym; | |
8925 | ||
f5385ebf | 8926 | if (!h->def_regular) |
c152c796 AM |
8927 | { |
8928 | if (h->verinfo.verdef == NULL) | |
8929 | iversym.vs_vers = 0; | |
8930 | else | |
8931 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
8932 | } | |
8933 | else | |
8934 | { | |
8935 | if (h->verinfo.vertree == NULL) | |
8936 | iversym.vs_vers = 1; | |
8937 | else | |
8938 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
8939 | if (finfo->info->create_default_symver) |
8940 | iversym.vs_vers++; | |
c152c796 AM |
8941 | } |
8942 | ||
f5385ebf | 8943 | if (h->hidden) |
c152c796 AM |
8944 | iversym.vs_vers |= VERSYM_HIDDEN; |
8945 | ||
8946 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
8947 | eversym += h->dynindx; | |
8948 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
8949 | } | |
8950 | } | |
8951 | ||
8952 | /* If we're stripping it, then it was just a dynamic symbol, and | |
8953 | there's nothing else to do. */ | |
8954 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
8955 | return TRUE; | |
8956 | ||
6e0b88f1 AM |
8957 | indx = bfd_get_symcount (finfo->output_bfd); |
8958 | ret = elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h); | |
8959 | if (ret == 0) | |
c152c796 AM |
8960 | { |
8961 | eoinfo->failed = TRUE; | |
8962 | return FALSE; | |
8963 | } | |
6e0b88f1 AM |
8964 | else if (ret == 1) |
8965 | h->indx = indx; | |
8966 | else if (h->indx == -2) | |
8967 | abort(); | |
c152c796 AM |
8968 | |
8969 | return TRUE; | |
8970 | } | |
8971 | ||
cdd3575c AM |
8972 | /* Return TRUE if special handling is done for relocs in SEC against |
8973 | symbols defined in discarded sections. */ | |
8974 | ||
c152c796 AM |
8975 | static bfd_boolean |
8976 | elf_section_ignore_discarded_relocs (asection *sec) | |
8977 | { | |
8978 | const struct elf_backend_data *bed; | |
8979 | ||
cdd3575c AM |
8980 | switch (sec->sec_info_type) |
8981 | { | |
8982 | case ELF_INFO_TYPE_STABS: | |
8983 | case ELF_INFO_TYPE_EH_FRAME: | |
8984 | return TRUE; | |
8985 | default: | |
8986 | break; | |
8987 | } | |
c152c796 AM |
8988 | |
8989 | bed = get_elf_backend_data (sec->owner); | |
8990 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
8991 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
8992 | return TRUE; | |
8993 | ||
8994 | return FALSE; | |
8995 | } | |
8996 | ||
9e66c942 AM |
8997 | /* Return a mask saying how ld should treat relocations in SEC against |
8998 | symbols defined in discarded sections. If this function returns | |
8999 | COMPLAIN set, ld will issue a warning message. If this function | |
9000 | returns PRETEND set, and the discarded section was link-once and the | |
9001 | same size as the kept link-once section, ld will pretend that the | |
9002 | symbol was actually defined in the kept section. Otherwise ld will | |
9003 | zero the reloc (at least that is the intent, but some cooperation by | |
9004 | the target dependent code is needed, particularly for REL targets). */ | |
9005 | ||
8a696751 AM |
9006 | unsigned int |
9007 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 9008 | { |
9e66c942 | 9009 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 9010 | return PRETEND; |
cdd3575c AM |
9011 | |
9012 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 9013 | return 0; |
cdd3575c AM |
9014 | |
9015 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 9016 | return 0; |
cdd3575c | 9017 | |
9e66c942 | 9018 | return COMPLAIN | PRETEND; |
cdd3575c AM |
9019 | } |
9020 | ||
3d7f7666 L |
9021 | /* Find a match between a section and a member of a section group. */ |
9022 | ||
9023 | static asection * | |
c0f00686 L |
9024 | match_group_member (asection *sec, asection *group, |
9025 | struct bfd_link_info *info) | |
3d7f7666 L |
9026 | { |
9027 | asection *first = elf_next_in_group (group); | |
9028 | asection *s = first; | |
9029 | ||
9030 | while (s != NULL) | |
9031 | { | |
c0f00686 | 9032 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
9033 | return s; |
9034 | ||
83180ade | 9035 | s = elf_next_in_group (s); |
3d7f7666 L |
9036 | if (s == first) |
9037 | break; | |
9038 | } | |
9039 | ||
9040 | return NULL; | |
9041 | } | |
9042 | ||
01b3c8ab | 9043 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
9044 | to replace it. Return the replacement if it is OK. Otherwise return |
9045 | NULL. */ | |
01b3c8ab L |
9046 | |
9047 | asection * | |
c0f00686 | 9048 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
9049 | { |
9050 | asection *kept; | |
9051 | ||
9052 | kept = sec->kept_section; | |
9053 | if (kept != NULL) | |
9054 | { | |
c2370991 | 9055 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 9056 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
9057 | if (kept != NULL |
9058 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
9059 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 9060 | kept = NULL; |
c2370991 | 9061 | sec->kept_section = kept; |
01b3c8ab L |
9062 | } |
9063 | return kept; | |
9064 | } | |
9065 | ||
c152c796 AM |
9066 | /* Link an input file into the linker output file. This function |
9067 | handles all the sections and relocations of the input file at once. | |
9068 | This is so that we only have to read the local symbols once, and | |
9069 | don't have to keep them in memory. */ | |
9070 | ||
9071 | static bfd_boolean | |
9072 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
9073 | { | |
ece5ef60 | 9074 | int (*relocate_section) |
c152c796 AM |
9075 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
9076 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
9077 | bfd *output_bfd; | |
9078 | Elf_Internal_Shdr *symtab_hdr; | |
9079 | size_t locsymcount; | |
9080 | size_t extsymoff; | |
9081 | Elf_Internal_Sym *isymbuf; | |
9082 | Elf_Internal_Sym *isym; | |
9083 | Elf_Internal_Sym *isymend; | |
9084 | long *pindex; | |
9085 | asection **ppsection; | |
9086 | asection *o; | |
9087 | const struct elf_backend_data *bed; | |
c152c796 | 9088 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
9089 | bfd_size_type address_size; |
9090 | bfd_vma r_type_mask; | |
9091 | int r_sym_shift; | |
c152c796 AM |
9092 | |
9093 | output_bfd = finfo->output_bfd; | |
9094 | bed = get_elf_backend_data (output_bfd); | |
9095 | relocate_section = bed->elf_backend_relocate_section; | |
9096 | ||
9097 | /* If this is a dynamic object, we don't want to do anything here: | |
9098 | we don't want the local symbols, and we don't want the section | |
9099 | contents. */ | |
9100 | if ((input_bfd->flags & DYNAMIC) != 0) | |
9101 | return TRUE; | |
9102 | ||
c152c796 AM |
9103 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
9104 | if (elf_bad_symtab (input_bfd)) | |
9105 | { | |
9106 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9107 | extsymoff = 0; | |
9108 | } | |
9109 | else | |
9110 | { | |
9111 | locsymcount = symtab_hdr->sh_info; | |
9112 | extsymoff = symtab_hdr->sh_info; | |
9113 | } | |
9114 | ||
9115 | /* Read the local symbols. */ | |
9116 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9117 | if (isymbuf == NULL && locsymcount != 0) | |
9118 | { | |
9119 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
9120 | finfo->internal_syms, | |
9121 | finfo->external_syms, | |
9122 | finfo->locsym_shndx); | |
9123 | if (isymbuf == NULL) | |
9124 | return FALSE; | |
9125 | } | |
9126 | ||
9127 | /* Find local symbol sections and adjust values of symbols in | |
9128 | SEC_MERGE sections. Write out those local symbols we know are | |
9129 | going into the output file. */ | |
9130 | isymend = isymbuf + locsymcount; | |
9131 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
9132 | isym < isymend; | |
9133 | isym++, pindex++, ppsection++) | |
9134 | { | |
9135 | asection *isec; | |
9136 | const char *name; | |
9137 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
9138 | long indx; |
9139 | int ret; | |
c152c796 AM |
9140 | |
9141 | *pindex = -1; | |
9142 | ||
9143 | if (elf_bad_symtab (input_bfd)) | |
9144 | { | |
9145 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
9146 | { | |
9147 | *ppsection = NULL; | |
9148 | continue; | |
9149 | } | |
9150 | } | |
9151 | ||
9152 | if (isym->st_shndx == SHN_UNDEF) | |
9153 | isec = bfd_und_section_ptr; | |
c152c796 AM |
9154 | else if (isym->st_shndx == SHN_ABS) |
9155 | isec = bfd_abs_section_ptr; | |
9156 | else if (isym->st_shndx == SHN_COMMON) | |
9157 | isec = bfd_com_section_ptr; | |
9158 | else | |
9159 | { | |
cb33740c AM |
9160 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
9161 | if (isec == NULL) | |
9162 | { | |
9163 | /* Don't attempt to output symbols with st_shnx in the | |
9164 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
9165 | *ppsection = NULL; | |
9166 | continue; | |
9167 | } | |
9168 | else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
9169 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
9170 | isym->st_value = | |
9171 | _bfd_merged_section_offset (output_bfd, &isec, | |
9172 | elf_section_data (isec)->sec_info, | |
9173 | isym->st_value); | |
c152c796 AM |
9174 | } |
9175 | ||
9176 | *ppsection = isec; | |
9177 | ||
9178 | /* Don't output the first, undefined, symbol. */ | |
9179 | if (ppsection == finfo->sections) | |
9180 | continue; | |
9181 | ||
9182 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
9183 | { | |
9184 | /* We never output section symbols. Instead, we use the | |
9185 | section symbol of the corresponding section in the output | |
9186 | file. */ | |
9187 | continue; | |
9188 | } | |
9189 | ||
9190 | /* If we are stripping all symbols, we don't want to output this | |
9191 | one. */ | |
9192 | if (finfo->info->strip == strip_all) | |
9193 | continue; | |
9194 | ||
9195 | /* If we are discarding all local symbols, we don't want to | |
9196 | output this one. If we are generating a relocatable output | |
9197 | file, then some of the local symbols may be required by | |
9198 | relocs; we output them below as we discover that they are | |
9199 | needed. */ | |
9200 | if (finfo->info->discard == discard_all) | |
9201 | continue; | |
9202 | ||
9203 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
9204 | discarding, we don't need to keep it. */ |
9205 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
9206 | && isym->st_shndx < SHN_LORESERVE |
9207 | && bfd_section_removed_from_list (output_bfd, | |
9208 | isec->output_section)) | |
e75a280b L |
9209 | continue; |
9210 | ||
c152c796 AM |
9211 | /* Get the name of the symbol. */ |
9212 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
9213 | isym->st_name); | |
9214 | if (name == NULL) | |
9215 | return FALSE; | |
9216 | ||
9217 | /* See if we are discarding symbols with this name. */ | |
9218 | if ((finfo->info->strip == strip_some | |
9219 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
9220 | == NULL)) | |
9221 | || (((finfo->info->discard == discard_sec_merge | |
9222 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
9223 | || finfo->info->discard == discard_l) | |
9224 | && bfd_is_local_label_name (input_bfd, name))) | |
9225 | continue; | |
9226 | ||
c152c796 AM |
9227 | osym = *isym; |
9228 | ||
9229 | /* Adjust the section index for the output file. */ | |
9230 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
9231 | isec->output_section); | |
9232 | if (osym.st_shndx == SHN_BAD) | |
9233 | return FALSE; | |
9234 | ||
c152c796 AM |
9235 | /* ELF symbols in relocatable files are section relative, but |
9236 | in executable files they are virtual addresses. Note that | |
9237 | this code assumes that all ELF sections have an associated | |
9238 | BFD section with a reasonable value for output_offset; below | |
9239 | we assume that they also have a reasonable value for | |
9240 | output_section. Any special sections must be set up to meet | |
9241 | these requirements. */ | |
9242 | osym.st_value += isec->output_offset; | |
9243 | if (! finfo->info->relocatable) | |
9244 | { | |
9245 | osym.st_value += isec->output_section->vma; | |
9246 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
9247 | { | |
9248 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
9249 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
9250 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
9251 | } | |
9252 | } | |
9253 | ||
6e0b88f1 AM |
9254 | indx = bfd_get_symcount (output_bfd); |
9255 | ret = elf_link_output_sym (finfo, name, &osym, isec, NULL); | |
9256 | if (ret == 0) | |
c152c796 | 9257 | return FALSE; |
6e0b88f1 AM |
9258 | else if (ret == 1) |
9259 | *pindex = indx; | |
c152c796 AM |
9260 | } |
9261 | ||
310fd250 L |
9262 | if (bed->s->arch_size == 32) |
9263 | { | |
9264 | r_type_mask = 0xff; | |
9265 | r_sym_shift = 8; | |
9266 | address_size = 4; | |
9267 | } | |
9268 | else | |
9269 | { | |
9270 | r_type_mask = 0xffffffff; | |
9271 | r_sym_shift = 32; | |
9272 | address_size = 8; | |
9273 | } | |
9274 | ||
c152c796 AM |
9275 | /* Relocate the contents of each section. */ |
9276 | sym_hashes = elf_sym_hashes (input_bfd); | |
9277 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
9278 | { | |
9279 | bfd_byte *contents; | |
9280 | ||
9281 | if (! o->linker_mark) | |
9282 | { | |
9283 | /* This section was omitted from the link. */ | |
9284 | continue; | |
9285 | } | |
9286 | ||
bcacc0f5 AM |
9287 | if (finfo->info->relocatable |
9288 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) | |
9289 | { | |
9290 | /* Deal with the group signature symbol. */ | |
9291 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
9292 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
9293 | asection *osec = o->output_section; | |
9294 | ||
9295 | if (symndx >= locsymcount | |
9296 | || (elf_bad_symtab (input_bfd) | |
9297 | && finfo->sections[symndx] == NULL)) | |
9298 | { | |
9299 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
9300 | while (h->root.type == bfd_link_hash_indirect | |
9301 | || h->root.type == bfd_link_hash_warning) | |
9302 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9303 | /* Arrange for symbol to be output. */ | |
9304 | h->indx = -2; | |
9305 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
9306 | } | |
9307 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
9308 | { | |
9309 | /* We'll use the output section target_index. */ | |
9310 | asection *sec = finfo->sections[symndx]->output_section; | |
9311 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; | |
9312 | } | |
9313 | else | |
9314 | { | |
9315 | if (finfo->indices[symndx] == -1) | |
9316 | { | |
9317 | /* Otherwise output the local symbol now. */ | |
9318 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
9319 | asection *sec = finfo->sections[symndx]->output_section; | |
9320 | const char *name; | |
6e0b88f1 AM |
9321 | long indx; |
9322 | int ret; | |
bcacc0f5 AM |
9323 | |
9324 | name = bfd_elf_string_from_elf_section (input_bfd, | |
9325 | symtab_hdr->sh_link, | |
9326 | sym.st_name); | |
9327 | if (name == NULL) | |
9328 | return FALSE; | |
9329 | ||
9330 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
9331 | sec); | |
9332 | if (sym.st_shndx == SHN_BAD) | |
9333 | return FALSE; | |
9334 | ||
9335 | sym.st_value += o->output_offset; | |
9336 | ||
6e0b88f1 AM |
9337 | indx = bfd_get_symcount (output_bfd); |
9338 | ret = elf_link_output_sym (finfo, name, &sym, o, NULL); | |
9339 | if (ret == 0) | |
bcacc0f5 | 9340 | return FALSE; |
6e0b88f1 AM |
9341 | else if (ret == 1) |
9342 | finfo->indices[symndx] = indx; | |
9343 | else | |
9344 | abort (); | |
bcacc0f5 AM |
9345 | } |
9346 | elf_section_data (osec)->this_hdr.sh_info | |
9347 | = finfo->indices[symndx]; | |
9348 | } | |
9349 | } | |
9350 | ||
c152c796 | 9351 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 9352 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
9353 | continue; |
9354 | ||
9355 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
9356 | { | |
9357 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
9358 | or somesuch. */ | |
9359 | continue; | |
9360 | } | |
9361 | ||
9362 | /* Get the contents of the section. They have been cached by a | |
9363 | relaxation routine. Note that o is a section in an input | |
9364 | file, so the contents field will not have been set by any of | |
9365 | the routines which work on output files. */ | |
9366 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
9367 | contents = elf_section_data (o)->this_hdr.contents; | |
9368 | else | |
9369 | { | |
9370 | contents = finfo->contents; | |
4a114e3e | 9371 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
9372 | return FALSE; |
9373 | } | |
9374 | ||
9375 | if ((o->flags & SEC_RELOC) != 0) | |
9376 | { | |
9377 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 9378 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 9379 | int action_discarded; |
ece5ef60 | 9380 | int ret; |
c152c796 AM |
9381 | |
9382 | /* Get the swapped relocs. */ | |
9383 | internal_relocs | |
9384 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
9385 | finfo->internal_relocs, FALSE); | |
9386 | if (internal_relocs == NULL | |
9387 | && o->reloc_count > 0) | |
9388 | return FALSE; | |
9389 | ||
310fd250 L |
9390 | /* We need to reverse-copy input .ctors/.dtors sections if |
9391 | they are placed in .init_array/.finit_array for output. */ | |
9392 | if (o->size > address_size | |
9393 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
9394 | && strcmp (o->output_section->name, | |
9395 | ".init_array") == 0) | |
9396 | || (strncmp (o->name, ".dtors", 6) == 0 | |
9397 | && strcmp (o->output_section->name, | |
9398 | ".fini_array") == 0)) | |
9399 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 9400 | { |
310fd250 L |
9401 | if (o->size != o->reloc_count * address_size) |
9402 | { | |
9403 | (*_bfd_error_handler) | |
9404 | (_("error: %B: size of section %A is not " | |
9405 | "multiple of address size"), | |
9406 | input_bfd, o); | |
9407 | bfd_set_error (bfd_error_on_input); | |
9408 | return FALSE; | |
9409 | } | |
9410 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
9411 | } |
9412 | ||
0f02bbd9 | 9413 | action_discarded = -1; |
c152c796 | 9414 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
9415 | action_discarded = (*bed->action_discarded) (o); |
9416 | ||
9417 | /* Run through the relocs evaluating complex reloc symbols and | |
9418 | looking for relocs against symbols from discarded sections | |
9419 | or section symbols from removed link-once sections. | |
9420 | Complain about relocs against discarded sections. Zero | |
9421 | relocs against removed link-once sections. */ | |
9422 | ||
9423 | rel = internal_relocs; | |
9424 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
9425 | for ( ; rel < relend; rel++) | |
c152c796 | 9426 | { |
0f02bbd9 AM |
9427 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
9428 | unsigned int s_type; | |
9429 | asection **ps, *sec; | |
9430 | struct elf_link_hash_entry *h = NULL; | |
9431 | const char *sym_name; | |
c152c796 | 9432 | |
0f02bbd9 AM |
9433 | if (r_symndx == STN_UNDEF) |
9434 | continue; | |
c152c796 | 9435 | |
0f02bbd9 AM |
9436 | if (r_symndx >= locsymcount |
9437 | || (elf_bad_symtab (input_bfd) | |
9438 | && finfo->sections[r_symndx] == NULL)) | |
9439 | { | |
9440 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 9441 | |
0f02bbd9 AM |
9442 | /* Badly formatted input files can contain relocs that |
9443 | reference non-existant symbols. Check here so that | |
9444 | we do not seg fault. */ | |
9445 | if (h == NULL) | |
c152c796 | 9446 | { |
0f02bbd9 | 9447 | char buffer [32]; |
dce669a1 | 9448 | |
0f02bbd9 AM |
9449 | sprintf_vma (buffer, rel->r_info); |
9450 | (*_bfd_error_handler) | |
9451 | (_("error: %B contains a reloc (0x%s) for section %A " | |
9452 | "that references a non-existent global symbol"), | |
9453 | input_bfd, o, buffer); | |
9454 | bfd_set_error (bfd_error_bad_value); | |
9455 | return FALSE; | |
9456 | } | |
3b36f7e6 | 9457 | |
0f02bbd9 AM |
9458 | while (h->root.type == bfd_link_hash_indirect |
9459 | || h->root.type == bfd_link_hash_warning) | |
9460 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 9461 | |
0f02bbd9 | 9462 | s_type = h->type; |
cdd3575c | 9463 | |
0f02bbd9 AM |
9464 | ps = NULL; |
9465 | if (h->root.type == bfd_link_hash_defined | |
9466 | || h->root.type == bfd_link_hash_defweak) | |
9467 | ps = &h->root.u.def.section; | |
9468 | ||
9469 | sym_name = h->root.root.string; | |
9470 | } | |
9471 | else | |
9472 | { | |
9473 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
9474 | ||
9475 | s_type = ELF_ST_TYPE (sym->st_info); | |
9476 | ps = &finfo->sections[r_symndx]; | |
9477 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, | |
9478 | sym, *ps); | |
9479 | } | |
c152c796 | 9480 | |
c301e700 DD |
9481 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
9482 | && !finfo->info->relocatable) | |
0f02bbd9 AM |
9483 | { |
9484 | bfd_vma val; | |
9485 | bfd_vma dot = (rel->r_offset | |
9486 | + o->output_offset + o->output_section->vma); | |
9487 | #ifdef DEBUG | |
9488 | printf ("Encountered a complex symbol!"); | |
9489 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
9490 | input_bfd->filename, o->name, |
9491 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
9492 | printf (" symbol: idx %8.8lx, name %s\n", |
9493 | r_symndx, sym_name); | |
9494 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
9495 | (unsigned long) rel->r_info, | |
9496 | (unsigned long) rel->r_offset); | |
9497 | #endif | |
9498 | if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot, | |
9499 | isymbuf, locsymcount, s_type == STT_SRELC)) | |
9500 | return FALSE; | |
9501 | ||
9502 | /* Symbol evaluated OK. Update to absolute value. */ | |
9503 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
9504 | r_symndx, val); | |
9505 | continue; | |
9506 | } | |
9507 | ||
9508 | if (action_discarded != -1 && ps != NULL) | |
9509 | { | |
cdd3575c AM |
9510 | /* Complain if the definition comes from a |
9511 | discarded section. */ | |
9512 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
9513 | { | |
cf35638d | 9514 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 9515 | if (action_discarded & COMPLAIN) |
e1fffbe6 AM |
9516 | (*finfo->info->callbacks->einfo) |
9517 | (_("%X`%s' referenced in section `%A' of %B: " | |
58ac56d0 | 9518 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 9519 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 9520 | |
87e5235d | 9521 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 9522 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
9523 | really defined in the kept linkonce section. |
9524 | FIXME: This is quite broken. Modifying the | |
9525 | symbol here means we will be changing all later | |
e0ae6d6f | 9526 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 9527 | if (action_discarded & PRETEND) |
87e5235d | 9528 | { |
01b3c8ab L |
9529 | asection *kept; |
9530 | ||
c0f00686 L |
9531 | kept = _bfd_elf_check_kept_section (sec, |
9532 | finfo->info); | |
01b3c8ab | 9533 | if (kept != NULL) |
87e5235d AM |
9534 | { |
9535 | *ps = kept; | |
9536 | continue; | |
9537 | } | |
9538 | } | |
c152c796 AM |
9539 | } |
9540 | } | |
9541 | } | |
9542 | ||
9543 | /* Relocate the section by invoking a back end routine. | |
9544 | ||
9545 | The back end routine is responsible for adjusting the | |
9546 | section contents as necessary, and (if using Rela relocs | |
9547 | and generating a relocatable output file) adjusting the | |
9548 | reloc addend as necessary. | |
9549 | ||
9550 | The back end routine does not have to worry about setting | |
9551 | the reloc address or the reloc symbol index. | |
9552 | ||
9553 | The back end routine is given a pointer to the swapped in | |
9554 | internal symbols, and can access the hash table entries | |
9555 | for the external symbols via elf_sym_hashes (input_bfd). | |
9556 | ||
9557 | When generating relocatable output, the back end routine | |
9558 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
9559 | output symbol is going to be a section symbol | |
9560 | corresponding to the output section, which will require | |
9561 | the addend to be adjusted. */ | |
9562 | ||
ece5ef60 | 9563 | ret = (*relocate_section) (output_bfd, finfo->info, |
c152c796 AM |
9564 | input_bfd, o, contents, |
9565 | internal_relocs, | |
9566 | isymbuf, | |
ece5ef60 AM |
9567 | finfo->sections); |
9568 | if (!ret) | |
c152c796 AM |
9569 | return FALSE; |
9570 | ||
ece5ef60 AM |
9571 | if (ret == 2 |
9572 | || finfo->info->relocatable | |
9573 | || finfo->info->emitrelocations) | |
c152c796 AM |
9574 | { |
9575 | Elf_Internal_Rela *irela; | |
d4730f92 | 9576 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
9577 | bfd_vma last_offset; |
9578 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
9579 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
9580 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 9581 | unsigned int next_erel; |
c152c796 | 9582 | bfd_boolean rela_normal; |
d4730f92 | 9583 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 9584 | |
d4730f92 BS |
9585 | esdi = elf_section_data (o); |
9586 | esdo = elf_section_data (o->output_section); | |
9587 | rela_normal = FALSE; | |
c152c796 AM |
9588 | |
9589 | /* Adjust the reloc addresses and symbol indices. */ | |
9590 | ||
9591 | irela = internal_relocs; | |
9592 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
d4730f92 BS |
9593 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
9594 | /* We start processing the REL relocs, if any. When we reach | |
9595 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
9596 | irelamid = irela; | |
9597 | if (esdi->rel.hdr != NULL) | |
9598 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
9599 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 9600 | rel_hash_list = rel_hash; |
d4730f92 | 9601 | rela_hash_list = NULL; |
c152c796 AM |
9602 | last_offset = o->output_offset; |
9603 | if (!finfo->info->relocatable) | |
9604 | last_offset += o->output_section->vma; | |
9605 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
9606 | { | |
9607 | unsigned long r_symndx; | |
9608 | asection *sec; | |
9609 | Elf_Internal_Sym sym; | |
9610 | ||
9611 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
9612 | { | |
9613 | rel_hash++; | |
9614 | next_erel = 0; | |
9615 | } | |
9616 | ||
d4730f92 BS |
9617 | if (irela == irelamid) |
9618 | { | |
9619 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
9620 | rela_hash_list = rel_hash; | |
9621 | rela_normal = bed->rela_normal; | |
9622 | } | |
9623 | ||
c152c796 AM |
9624 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
9625 | finfo->info, o, | |
9626 | irela->r_offset); | |
9627 | if (irela->r_offset >= (bfd_vma) -2) | |
9628 | { | |
9629 | /* This is a reloc for a deleted entry or somesuch. | |
9630 | Turn it into an R_*_NONE reloc, at the same | |
9631 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 9632 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
9633 | being ordered. */ |
9634 | irela->r_offset = last_offset; | |
9635 | irela->r_info = 0; | |
9636 | irela->r_addend = 0; | |
9637 | continue; | |
9638 | } | |
9639 | ||
9640 | irela->r_offset += o->output_offset; | |
9641 | ||
9642 | /* Relocs in an executable have to be virtual addresses. */ | |
9643 | if (!finfo->info->relocatable) | |
9644 | irela->r_offset += o->output_section->vma; | |
9645 | ||
9646 | last_offset = irela->r_offset; | |
9647 | ||
9648 | r_symndx = irela->r_info >> r_sym_shift; | |
9649 | if (r_symndx == STN_UNDEF) | |
9650 | continue; | |
9651 | ||
9652 | if (r_symndx >= locsymcount | |
9653 | || (elf_bad_symtab (input_bfd) | |
9654 | && finfo->sections[r_symndx] == NULL)) | |
9655 | { | |
9656 | struct elf_link_hash_entry *rh; | |
9657 | unsigned long indx; | |
9658 | ||
9659 | /* This is a reloc against a global symbol. We | |
9660 | have not yet output all the local symbols, so | |
9661 | we do not know the symbol index of any global | |
9662 | symbol. We set the rel_hash entry for this | |
9663 | reloc to point to the global hash table entry | |
9664 | for this symbol. The symbol index is then | |
ee75fd95 | 9665 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
9666 | indx = r_symndx - extsymoff; |
9667 | rh = elf_sym_hashes (input_bfd)[indx]; | |
9668 | while (rh->root.type == bfd_link_hash_indirect | |
9669 | || rh->root.type == bfd_link_hash_warning) | |
9670 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
9671 | ||
9672 | /* Setting the index to -2 tells | |
9673 | elf_link_output_extsym that this symbol is | |
9674 | used by a reloc. */ | |
9675 | BFD_ASSERT (rh->indx < 0); | |
9676 | rh->indx = -2; | |
9677 | ||
9678 | *rel_hash = rh; | |
9679 | ||
9680 | continue; | |
9681 | } | |
9682 | ||
9683 | /* This is a reloc against a local symbol. */ | |
9684 | ||
9685 | *rel_hash = NULL; | |
9686 | sym = isymbuf[r_symndx]; | |
9687 | sec = finfo->sections[r_symndx]; | |
9688 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
9689 | { | |
9690 | /* I suppose the backend ought to fill in the | |
9691 | section of any STT_SECTION symbol against a | |
6a8d1586 | 9692 | processor specific section. */ |
cf35638d | 9693 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
9694 | if (bfd_is_abs_section (sec)) |
9695 | ; | |
c152c796 AM |
9696 | else if (sec == NULL || sec->owner == NULL) |
9697 | { | |
9698 | bfd_set_error (bfd_error_bad_value); | |
9699 | return FALSE; | |
9700 | } | |
9701 | else | |
9702 | { | |
6a8d1586 AM |
9703 | asection *osec = sec->output_section; |
9704 | ||
9705 | /* If we have discarded a section, the output | |
9706 | section will be the absolute section. In | |
ab96bf03 AM |
9707 | case of discarded SEC_MERGE sections, use |
9708 | the kept section. relocate_section should | |
9709 | have already handled discarded linkonce | |
9710 | sections. */ | |
6a8d1586 AM |
9711 | if (bfd_is_abs_section (osec) |
9712 | && sec->kept_section != NULL | |
9713 | && sec->kept_section->output_section != NULL) | |
9714 | { | |
9715 | osec = sec->kept_section->output_section; | |
9716 | irela->r_addend -= osec->vma; | |
9717 | } | |
9718 | ||
9719 | if (!bfd_is_abs_section (osec)) | |
9720 | { | |
9721 | r_symndx = osec->target_index; | |
cf35638d | 9722 | if (r_symndx == STN_UNDEF) |
74541ad4 AM |
9723 | { |
9724 | struct elf_link_hash_table *htab; | |
9725 | asection *oi; | |
9726 | ||
9727 | htab = elf_hash_table (finfo->info); | |
9728 | oi = htab->text_index_section; | |
9729 | if ((osec->flags & SEC_READONLY) == 0 | |
9730 | && htab->data_index_section != NULL) | |
9731 | oi = htab->data_index_section; | |
9732 | ||
9733 | if (oi != NULL) | |
9734 | { | |
9735 | irela->r_addend += osec->vma - oi->vma; | |
9736 | r_symndx = oi->target_index; | |
9737 | } | |
9738 | } | |
9739 | ||
cf35638d | 9740 | BFD_ASSERT (r_symndx != STN_UNDEF); |
6a8d1586 | 9741 | } |
c152c796 AM |
9742 | } |
9743 | ||
9744 | /* Adjust the addend according to where the | |
9745 | section winds up in the output section. */ | |
9746 | if (rela_normal) | |
9747 | irela->r_addend += sec->output_offset; | |
9748 | } | |
9749 | else | |
9750 | { | |
9751 | if (finfo->indices[r_symndx] == -1) | |
9752 | { | |
9753 | unsigned long shlink; | |
9754 | const char *name; | |
9755 | asection *osec; | |
6e0b88f1 | 9756 | long indx; |
c152c796 AM |
9757 | |
9758 | if (finfo->info->strip == strip_all) | |
9759 | { | |
9760 | /* You can't do ld -r -s. */ | |
9761 | bfd_set_error (bfd_error_invalid_operation); | |
9762 | return FALSE; | |
9763 | } | |
9764 | ||
9765 | /* This symbol was skipped earlier, but | |
9766 | since it is needed by a reloc, we | |
9767 | must output it now. */ | |
9768 | shlink = symtab_hdr->sh_link; | |
9769 | name = (bfd_elf_string_from_elf_section | |
9770 | (input_bfd, shlink, sym.st_name)); | |
9771 | if (name == NULL) | |
9772 | return FALSE; | |
9773 | ||
9774 | osec = sec->output_section; | |
9775 | sym.st_shndx = | |
9776 | _bfd_elf_section_from_bfd_section (output_bfd, | |
9777 | osec); | |
9778 | if (sym.st_shndx == SHN_BAD) | |
9779 | return FALSE; | |
9780 | ||
9781 | sym.st_value += sec->output_offset; | |
9782 | if (! finfo->info->relocatable) | |
9783 | { | |
9784 | sym.st_value += osec->vma; | |
9785 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
9786 | { | |
9787 | /* STT_TLS symbols are relative to PT_TLS | |
9788 | segment base. */ | |
9789 | BFD_ASSERT (elf_hash_table (finfo->info) | |
9790 | ->tls_sec != NULL); | |
9791 | sym.st_value -= (elf_hash_table (finfo->info) | |
9792 | ->tls_sec->vma); | |
9793 | } | |
9794 | } | |
9795 | ||
6e0b88f1 AM |
9796 | indx = bfd_get_symcount (output_bfd); |
9797 | ret = elf_link_output_sym (finfo, name, &sym, sec, | |
9798 | NULL); | |
9799 | if (ret == 0) | |
c152c796 | 9800 | return FALSE; |
6e0b88f1 AM |
9801 | else if (ret == 1) |
9802 | finfo->indices[r_symndx] = indx; | |
9803 | else | |
9804 | abort (); | |
c152c796 AM |
9805 | } |
9806 | ||
9807 | r_symndx = finfo->indices[r_symndx]; | |
9808 | } | |
9809 | ||
9810 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
9811 | | (irela->r_info & r_type_mask)); | |
9812 | } | |
9813 | ||
9814 | /* Swap out the relocs. */ | |
d4730f92 BS |
9815 | input_rel_hdr = esdi->rel.hdr; |
9816 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 9817 | { |
d4730f92 BS |
9818 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
9819 | input_rel_hdr, | |
9820 | internal_relocs, | |
9821 | rel_hash_list)) | |
9822 | return FALSE; | |
c152c796 AM |
9823 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
9824 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 9825 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
9826 | } |
9827 | ||
9828 | input_rela_hdr = esdi->rela.hdr; | |
9829 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
9830 | { | |
eac338cf | 9831 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 9832 | input_rela_hdr, |
eac338cf | 9833 | internal_relocs, |
d4730f92 | 9834 | rela_hash_list)) |
c152c796 AM |
9835 | return FALSE; |
9836 | } | |
9837 | } | |
9838 | } | |
9839 | ||
9840 | /* Write out the modified section contents. */ | |
9841 | if (bed->elf_backend_write_section | |
c7b8f16e JB |
9842 | && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o, |
9843 | contents)) | |
c152c796 AM |
9844 | { |
9845 | /* Section written out. */ | |
9846 | } | |
9847 | else switch (o->sec_info_type) | |
9848 | { | |
9849 | case ELF_INFO_TYPE_STABS: | |
9850 | if (! (_bfd_write_section_stabs | |
9851 | (output_bfd, | |
9852 | &elf_hash_table (finfo->info)->stab_info, | |
9853 | o, &elf_section_data (o)->sec_info, contents))) | |
9854 | return FALSE; | |
9855 | break; | |
9856 | case ELF_INFO_TYPE_MERGE: | |
9857 | if (! _bfd_write_merged_section (output_bfd, o, | |
9858 | elf_section_data (o)->sec_info)) | |
9859 | return FALSE; | |
9860 | break; | |
9861 | case ELF_INFO_TYPE_EH_FRAME: | |
9862 | { | |
9863 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
9864 | o, contents)) | |
9865 | return FALSE; | |
9866 | } | |
9867 | break; | |
9868 | default: | |
9869 | { | |
5dabe785 | 9870 | /* FIXME: octets_per_byte. */ |
310fd250 L |
9871 | if (! (o->flags & SEC_EXCLUDE)) |
9872 | { | |
9873 | file_ptr offset = (file_ptr) o->output_offset; | |
9874 | bfd_size_type todo = o->size; | |
9875 | if ((o->flags & SEC_ELF_REVERSE_COPY)) | |
9876 | { | |
9877 | /* Reverse-copy input section to output. */ | |
9878 | do | |
9879 | { | |
9880 | todo -= address_size; | |
9881 | if (! bfd_set_section_contents (output_bfd, | |
9882 | o->output_section, | |
9883 | contents + todo, | |
9884 | offset, | |
9885 | address_size)) | |
9886 | return FALSE; | |
9887 | if (todo == 0) | |
9888 | break; | |
9889 | offset += address_size; | |
9890 | } | |
9891 | while (1); | |
9892 | } | |
9893 | else if (! bfd_set_section_contents (output_bfd, | |
9894 | o->output_section, | |
9895 | contents, | |
9896 | offset, todo)) | |
9897 | return FALSE; | |
9898 | } | |
c152c796 AM |
9899 | } |
9900 | break; | |
9901 | } | |
9902 | } | |
9903 | ||
9904 | return TRUE; | |
9905 | } | |
9906 | ||
9907 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 9908 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
9909 | is used to build constructor and destructor tables when linking |
9910 | with -Ur. */ | |
9911 | ||
9912 | static bfd_boolean | |
9913 | elf_reloc_link_order (bfd *output_bfd, | |
9914 | struct bfd_link_info *info, | |
9915 | asection *output_section, | |
9916 | struct bfd_link_order *link_order) | |
9917 | { | |
9918 | reloc_howto_type *howto; | |
9919 | long indx; | |
9920 | bfd_vma offset; | |
9921 | bfd_vma addend; | |
d4730f92 | 9922 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
9923 | struct elf_link_hash_entry **rel_hash_ptr; |
9924 | Elf_Internal_Shdr *rel_hdr; | |
9925 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
9926 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
9927 | bfd_byte *erel; | |
9928 | unsigned int i; | |
d4730f92 | 9929 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
9930 | |
9931 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
9932 | if (howto == NULL) | |
9933 | { | |
9934 | bfd_set_error (bfd_error_bad_value); | |
9935 | return FALSE; | |
9936 | } | |
9937 | ||
9938 | addend = link_order->u.reloc.p->addend; | |
9939 | ||
d4730f92 BS |
9940 | if (esdo->rel.hdr) |
9941 | reldata = &esdo->rel; | |
9942 | else if (esdo->rela.hdr) | |
9943 | reldata = &esdo->rela; | |
9944 | else | |
9945 | { | |
9946 | reldata = NULL; | |
9947 | BFD_ASSERT (0); | |
9948 | } | |
9949 | ||
c152c796 | 9950 | /* Figure out the symbol index. */ |
d4730f92 | 9951 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
9952 | if (link_order->type == bfd_section_reloc_link_order) |
9953 | { | |
9954 | indx = link_order->u.reloc.p->u.section->target_index; | |
9955 | BFD_ASSERT (indx != 0); | |
9956 | *rel_hash_ptr = NULL; | |
9957 | } | |
9958 | else | |
9959 | { | |
9960 | struct elf_link_hash_entry *h; | |
9961 | ||
9962 | /* Treat a reloc against a defined symbol as though it were | |
9963 | actually against the section. */ | |
9964 | h = ((struct elf_link_hash_entry *) | |
9965 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
9966 | link_order->u.reloc.p->u.name, | |
9967 | FALSE, FALSE, TRUE)); | |
9968 | if (h != NULL | |
9969 | && (h->root.type == bfd_link_hash_defined | |
9970 | || h->root.type == bfd_link_hash_defweak)) | |
9971 | { | |
9972 | asection *section; | |
9973 | ||
9974 | section = h->root.u.def.section; | |
9975 | indx = section->output_section->target_index; | |
9976 | *rel_hash_ptr = NULL; | |
9977 | /* It seems that we ought to add the symbol value to the | |
9978 | addend here, but in practice it has already been added | |
9979 | because it was passed to constructor_callback. */ | |
9980 | addend += section->output_section->vma + section->output_offset; | |
9981 | } | |
9982 | else if (h != NULL) | |
9983 | { | |
9984 | /* Setting the index to -2 tells elf_link_output_extsym that | |
9985 | this symbol is used by a reloc. */ | |
9986 | h->indx = -2; | |
9987 | *rel_hash_ptr = h; | |
9988 | indx = 0; | |
9989 | } | |
9990 | else | |
9991 | { | |
9992 | if (! ((*info->callbacks->unattached_reloc) | |
9993 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
9994 | return FALSE; | |
9995 | indx = 0; | |
9996 | } | |
9997 | } | |
9998 | ||
9999 | /* If this is an inplace reloc, we must write the addend into the | |
10000 | object file. */ | |
10001 | if (howto->partial_inplace && addend != 0) | |
10002 | { | |
10003 | bfd_size_type size; | |
10004 | bfd_reloc_status_type rstat; | |
10005 | bfd_byte *buf; | |
10006 | bfd_boolean ok; | |
10007 | const char *sym_name; | |
10008 | ||
a50b1753 NC |
10009 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
10010 | buf = (bfd_byte *) bfd_zmalloc (size); | |
c152c796 AM |
10011 | if (buf == NULL) |
10012 | return FALSE; | |
10013 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
10014 | switch (rstat) | |
10015 | { | |
10016 | case bfd_reloc_ok: | |
10017 | break; | |
10018 | ||
10019 | default: | |
10020 | case bfd_reloc_outofrange: | |
10021 | abort (); | |
10022 | ||
10023 | case bfd_reloc_overflow: | |
10024 | if (link_order->type == bfd_section_reloc_link_order) | |
10025 | sym_name = bfd_section_name (output_bfd, | |
10026 | link_order->u.reloc.p->u.section); | |
10027 | else | |
10028 | sym_name = link_order->u.reloc.p->u.name; | |
10029 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
10030 | (info, NULL, sym_name, howto->name, addend, NULL, |
10031 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
10032 | { |
10033 | free (buf); | |
10034 | return FALSE; | |
10035 | } | |
10036 | break; | |
10037 | } | |
10038 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
10039 | link_order->offset, size); | |
10040 | free (buf); | |
10041 | if (! ok) | |
10042 | return FALSE; | |
10043 | } | |
10044 | ||
10045 | /* The address of a reloc is relative to the section in a | |
10046 | relocatable file, and is a virtual address in an executable | |
10047 | file. */ | |
10048 | offset = link_order->offset; | |
10049 | if (! info->relocatable) | |
10050 | offset += output_section->vma; | |
10051 | ||
10052 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
10053 | { | |
10054 | irel[i].r_offset = offset; | |
10055 | irel[i].r_info = 0; | |
10056 | irel[i].r_addend = 0; | |
10057 | } | |
10058 | if (bed->s->arch_size == 32) | |
10059 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
10060 | else | |
10061 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
10062 | ||
d4730f92 | 10063 | rel_hdr = reldata->hdr; |
c152c796 AM |
10064 | erel = rel_hdr->contents; |
10065 | if (rel_hdr->sh_type == SHT_REL) | |
10066 | { | |
d4730f92 | 10067 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
10068 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
10069 | } | |
10070 | else | |
10071 | { | |
10072 | irel[0].r_addend = addend; | |
d4730f92 | 10073 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
10074 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
10075 | } | |
10076 | ||
d4730f92 | 10077 | ++reldata->count; |
c152c796 AM |
10078 | |
10079 | return TRUE; | |
10080 | } | |
10081 | ||
0b52efa6 PB |
10082 | |
10083 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
10084 | ||
10085 | static bfd_vma | |
10086 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
10087 | { | |
10088 | Elf_Internal_Shdr **elf_shdrp; | |
10089 | asection *s; | |
10090 | int elfsec; | |
10091 | ||
10092 | s = p->u.indirect.section; | |
10093 | elf_shdrp = elf_elfsections (s->owner); | |
10094 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
10095 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
10096 | /* PR 290: |
10097 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 10098 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
10099 | sh_info fields. Hence we could get the situation |
10100 | where elfsec is 0. */ | |
10101 | if (elfsec == 0) | |
10102 | { | |
10103 | const struct elf_backend_data *bed | |
10104 | = get_elf_backend_data (s->owner); | |
10105 | if (bed->link_order_error_handler) | |
d003868e AM |
10106 | bed->link_order_error_handler |
10107 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
10108 | return 0; |
10109 | } | |
10110 | else | |
10111 | { | |
10112 | s = elf_shdrp[elfsec]->bfd_section; | |
10113 | return s->output_section->vma + s->output_offset; | |
10114 | } | |
0b52efa6 PB |
10115 | } |
10116 | ||
10117 | ||
10118 | /* Compare two sections based on the locations of the sections they are | |
10119 | linked to. Used by elf_fixup_link_order. */ | |
10120 | ||
10121 | static int | |
10122 | compare_link_order (const void * a, const void * b) | |
10123 | { | |
10124 | bfd_vma apos; | |
10125 | bfd_vma bpos; | |
10126 | ||
10127 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
10128 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
10129 | if (apos < bpos) | |
10130 | return -1; | |
10131 | return apos > bpos; | |
10132 | } | |
10133 | ||
10134 | ||
10135 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
10136 | order as their linked sections. Returns false if this could not be done | |
10137 | because an output section includes both ordered and unordered | |
10138 | sections. Ideally we'd do this in the linker proper. */ | |
10139 | ||
10140 | static bfd_boolean | |
10141 | elf_fixup_link_order (bfd *abfd, asection *o) | |
10142 | { | |
10143 | int seen_linkorder; | |
10144 | int seen_other; | |
10145 | int n; | |
10146 | struct bfd_link_order *p; | |
10147 | bfd *sub; | |
10148 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 10149 | unsigned elfsec; |
0b52efa6 | 10150 | struct bfd_link_order **sections; |
d33cdfe3 | 10151 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 10152 | bfd_vma offset; |
3b36f7e6 | 10153 | |
d33cdfe3 L |
10154 | other_sec = NULL; |
10155 | linkorder_sec = NULL; | |
0b52efa6 PB |
10156 | seen_other = 0; |
10157 | seen_linkorder = 0; | |
8423293d | 10158 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 10159 | { |
d33cdfe3 | 10160 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
10161 | { |
10162 | s = p->u.indirect.section; | |
d33cdfe3 L |
10163 | sub = s->owner; |
10164 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
10165 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
10166 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
10167 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
10168 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
10169 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
10170 | { |
10171 | seen_linkorder++; | |
10172 | linkorder_sec = s; | |
10173 | } | |
0b52efa6 | 10174 | else |
d33cdfe3 L |
10175 | { |
10176 | seen_other++; | |
10177 | other_sec = s; | |
10178 | } | |
0b52efa6 PB |
10179 | } |
10180 | else | |
10181 | seen_other++; | |
d33cdfe3 L |
10182 | |
10183 | if (seen_other && seen_linkorder) | |
10184 | { | |
10185 | if (other_sec && linkorder_sec) | |
10186 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
10187 | o, linkorder_sec, | |
10188 | linkorder_sec->owner, other_sec, | |
10189 | other_sec->owner); | |
10190 | else | |
10191 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
10192 | o); | |
10193 | bfd_set_error (bfd_error_bad_value); | |
10194 | return FALSE; | |
10195 | } | |
0b52efa6 PB |
10196 | } |
10197 | ||
10198 | if (!seen_linkorder) | |
10199 | return TRUE; | |
10200 | ||
0b52efa6 | 10201 | sections = (struct bfd_link_order **) |
14b1c01e AM |
10202 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
10203 | if (sections == NULL) | |
10204 | return FALSE; | |
0b52efa6 | 10205 | seen_linkorder = 0; |
3b36f7e6 | 10206 | |
8423293d | 10207 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
10208 | { |
10209 | sections[seen_linkorder++] = p; | |
10210 | } | |
10211 | /* Sort the input sections in the order of their linked section. */ | |
10212 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
10213 | compare_link_order); | |
10214 | ||
10215 | /* Change the offsets of the sections. */ | |
10216 | offset = 0; | |
10217 | for (n = 0; n < seen_linkorder; n++) | |
10218 | { | |
10219 | s = sections[n]->u.indirect.section; | |
461686a3 | 10220 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
0b52efa6 PB |
10221 | s->output_offset = offset; |
10222 | sections[n]->offset = offset; | |
5dabe785 | 10223 | /* FIXME: octets_per_byte. */ |
0b52efa6 PB |
10224 | offset += sections[n]->size; |
10225 | } | |
10226 | ||
4dd07732 | 10227 | free (sections); |
0b52efa6 PB |
10228 | return TRUE; |
10229 | } | |
10230 | ||
10231 | ||
c152c796 AM |
10232 | /* Do the final step of an ELF link. */ |
10233 | ||
10234 | bfd_boolean | |
10235 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
10236 | { | |
10237 | bfd_boolean dynamic; | |
10238 | bfd_boolean emit_relocs; | |
10239 | bfd *dynobj; | |
10240 | struct elf_final_link_info finfo; | |
91d6fa6a NC |
10241 | asection *o; |
10242 | struct bfd_link_order *p; | |
10243 | bfd *sub; | |
c152c796 AM |
10244 | bfd_size_type max_contents_size; |
10245 | bfd_size_type max_external_reloc_size; | |
10246 | bfd_size_type max_internal_reloc_count; | |
10247 | bfd_size_type max_sym_count; | |
10248 | bfd_size_type max_sym_shndx_count; | |
10249 | file_ptr off; | |
10250 | Elf_Internal_Sym elfsym; | |
10251 | unsigned int i; | |
10252 | Elf_Internal_Shdr *symtab_hdr; | |
10253 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
10254 | Elf_Internal_Shdr *symstrtab_hdr; | |
10255 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10256 | struct elf_outext_info eoinfo; | |
10257 | bfd_boolean merged; | |
10258 | size_t relativecount = 0; | |
10259 | asection *reldyn = 0; | |
10260 | bfd_size_type amt; | |
104d59d1 JM |
10261 | asection *attr_section = NULL; |
10262 | bfd_vma attr_size = 0; | |
10263 | const char *std_attrs_section; | |
c152c796 AM |
10264 | |
10265 | if (! is_elf_hash_table (info->hash)) | |
10266 | return FALSE; | |
10267 | ||
10268 | if (info->shared) | |
10269 | abfd->flags |= DYNAMIC; | |
10270 | ||
10271 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
10272 | dynobj = elf_hash_table (info)->dynobj; | |
10273 | ||
10274 | emit_relocs = (info->relocatable | |
a4676736 | 10275 | || info->emitrelocations); |
c152c796 AM |
10276 | |
10277 | finfo.info = info; | |
10278 | finfo.output_bfd = abfd; | |
10279 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
10280 | if (finfo.symstrtab == NULL) | |
10281 | return FALSE; | |
10282 | ||
10283 | if (! dynamic) | |
10284 | { | |
10285 | finfo.dynsym_sec = NULL; | |
10286 | finfo.hash_sec = NULL; | |
10287 | finfo.symver_sec = NULL; | |
10288 | } | |
10289 | else | |
10290 | { | |
10291 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
10292 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
fdc90cb4 | 10293 | BFD_ASSERT (finfo.dynsym_sec != NULL); |
c152c796 AM |
10294 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
10295 | /* Note that it is OK if symver_sec is NULL. */ | |
10296 | } | |
10297 | ||
10298 | finfo.contents = NULL; | |
10299 | finfo.external_relocs = NULL; | |
10300 | finfo.internal_relocs = NULL; | |
10301 | finfo.external_syms = NULL; | |
10302 | finfo.locsym_shndx = NULL; | |
10303 | finfo.internal_syms = NULL; | |
10304 | finfo.indices = NULL; | |
10305 | finfo.sections = NULL; | |
10306 | finfo.symbuf = NULL; | |
10307 | finfo.symshndxbuf = NULL; | |
10308 | finfo.symbuf_count = 0; | |
10309 | finfo.shndxbuf_size = 0; | |
10310 | ||
104d59d1 JM |
10311 | /* The object attributes have been merged. Remove the input |
10312 | sections from the link, and set the contents of the output | |
10313 | secton. */ | |
10314 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
10315 | for (o = abfd->sections; o != NULL; o = o->next) | |
10316 | { | |
10317 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
10318 | || strcmp (o->name, ".gnu.attributes") == 0) | |
10319 | { | |
10320 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
10321 | { | |
10322 | asection *input_section; | |
10323 | ||
10324 | if (p->type != bfd_indirect_link_order) | |
10325 | continue; | |
10326 | input_section = p->u.indirect.section; | |
10327 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
10328 | elf_link_input_bfd ignores this section. */ | |
10329 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
10330 | } | |
a0c8462f | 10331 | |
104d59d1 JM |
10332 | attr_size = bfd_elf_obj_attr_size (abfd); |
10333 | if (attr_size) | |
10334 | { | |
10335 | bfd_set_section_size (abfd, o, attr_size); | |
10336 | attr_section = o; | |
10337 | /* Skip this section later on. */ | |
10338 | o->map_head.link_order = NULL; | |
10339 | } | |
10340 | else | |
10341 | o->flags |= SEC_EXCLUDE; | |
10342 | } | |
10343 | } | |
10344 | ||
c152c796 AM |
10345 | /* Count up the number of relocations we will output for each output |
10346 | section, so that we know the sizes of the reloc sections. We | |
10347 | also figure out some maximum sizes. */ | |
10348 | max_contents_size = 0; | |
10349 | max_external_reloc_size = 0; | |
10350 | max_internal_reloc_count = 0; | |
10351 | max_sym_count = 0; | |
10352 | max_sym_shndx_count = 0; | |
10353 | merged = FALSE; | |
10354 | for (o = abfd->sections; o != NULL; o = o->next) | |
10355 | { | |
10356 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
10357 | o->reloc_count = 0; | |
10358 | ||
8423293d | 10359 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
10360 | { |
10361 | unsigned int reloc_count = 0; | |
10362 | struct bfd_elf_section_data *esdi = NULL; | |
c152c796 AM |
10363 | |
10364 | if (p->type == bfd_section_reloc_link_order | |
10365 | || p->type == bfd_symbol_reloc_link_order) | |
10366 | reloc_count = 1; | |
10367 | else if (p->type == bfd_indirect_link_order) | |
10368 | { | |
10369 | asection *sec; | |
10370 | ||
10371 | sec = p->u.indirect.section; | |
10372 | esdi = elf_section_data (sec); | |
10373 | ||
10374 | /* Mark all sections which are to be included in the | |
10375 | link. This will normally be every section. We need | |
10376 | to do this so that we can identify any sections which | |
10377 | the linker has decided to not include. */ | |
10378 | sec->linker_mark = TRUE; | |
10379 | ||
10380 | if (sec->flags & SEC_MERGE) | |
10381 | merged = TRUE; | |
10382 | ||
10383 | if (info->relocatable || info->emitrelocations) | |
10384 | reloc_count = sec->reloc_count; | |
10385 | else if (bed->elf_backend_count_relocs) | |
58217f29 | 10386 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); |
c152c796 | 10387 | |
eea6121a AM |
10388 | if (sec->rawsize > max_contents_size) |
10389 | max_contents_size = sec->rawsize; | |
10390 | if (sec->size > max_contents_size) | |
10391 | max_contents_size = sec->size; | |
c152c796 AM |
10392 | |
10393 | /* We are interested in just local symbols, not all | |
10394 | symbols. */ | |
10395 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
10396 | && (sec->owner->flags & DYNAMIC) == 0) | |
10397 | { | |
10398 | size_t sym_count; | |
10399 | ||
10400 | if (elf_bad_symtab (sec->owner)) | |
10401 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
10402 | / bed->s->sizeof_sym); | |
10403 | else | |
10404 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
10405 | ||
10406 | if (sym_count > max_sym_count) | |
10407 | max_sym_count = sym_count; | |
10408 | ||
10409 | if (sym_count > max_sym_shndx_count | |
10410 | && elf_symtab_shndx (sec->owner) != 0) | |
10411 | max_sym_shndx_count = sym_count; | |
10412 | ||
10413 | if ((sec->flags & SEC_RELOC) != 0) | |
10414 | { | |
d4730f92 | 10415 | size_t ext_size = 0; |
c152c796 | 10416 | |
d4730f92 BS |
10417 | if (esdi->rel.hdr != NULL) |
10418 | ext_size = esdi->rel.hdr->sh_size; | |
10419 | if (esdi->rela.hdr != NULL) | |
10420 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 10421 | |
c152c796 AM |
10422 | if (ext_size > max_external_reloc_size) |
10423 | max_external_reloc_size = ext_size; | |
10424 | if (sec->reloc_count > max_internal_reloc_count) | |
10425 | max_internal_reloc_count = sec->reloc_count; | |
10426 | } | |
10427 | } | |
10428 | } | |
10429 | ||
10430 | if (reloc_count == 0) | |
10431 | continue; | |
10432 | ||
10433 | o->reloc_count += reloc_count; | |
10434 | ||
d4730f92 BS |
10435 | if (p->type == bfd_indirect_link_order |
10436 | && (info->relocatable || info->emitrelocations)) | |
c152c796 | 10437 | { |
d4730f92 BS |
10438 | if (esdi->rel.hdr) |
10439 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); | |
10440 | if (esdi->rela.hdr) | |
10441 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); | |
10442 | } | |
10443 | else | |
10444 | { | |
10445 | if (o->use_rela_p) | |
10446 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 10447 | else |
d4730f92 | 10448 | esdo->rel.count += reloc_count; |
c152c796 | 10449 | } |
c152c796 AM |
10450 | } |
10451 | ||
10452 | if (o->reloc_count > 0) | |
10453 | o->flags |= SEC_RELOC; | |
10454 | else | |
10455 | { | |
10456 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
10457 | set it (this is probably a bug) and if it is set | |
10458 | assign_section_numbers will create a reloc section. */ | |
10459 | o->flags &=~ SEC_RELOC; | |
10460 | } | |
10461 | ||
10462 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
10463 | zero. This is done in elf_fake_sections as well, but forcing | |
10464 | the VMA to 0 here will ensure that relocs against these | |
10465 | sections are handled correctly. */ | |
10466 | if ((o->flags & SEC_ALLOC) == 0 | |
10467 | && ! o->user_set_vma) | |
10468 | o->vma = 0; | |
10469 | } | |
10470 | ||
10471 | if (! info->relocatable && merged) | |
10472 | elf_link_hash_traverse (elf_hash_table (info), | |
10473 | _bfd_elf_link_sec_merge_syms, abfd); | |
10474 | ||
10475 | /* Figure out the file positions for everything but the symbol table | |
10476 | and the relocs. We set symcount to force assign_section_numbers | |
10477 | to create a symbol table. */ | |
10478 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
10479 | BFD_ASSERT (! abfd->output_has_begun); | |
10480 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
10481 | goto error_return; | |
10482 | ||
ee75fd95 | 10483 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
10484 | for (o = abfd->sections; o != NULL; o = o->next) |
10485 | { | |
d4730f92 | 10486 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
10487 | if ((o->flags & SEC_RELOC) != 0) |
10488 | { | |
d4730f92 BS |
10489 | if (esdo->rel.hdr |
10490 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) | |
c152c796 AM |
10491 | goto error_return; |
10492 | ||
d4730f92 BS |
10493 | if (esdo->rela.hdr |
10494 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) | |
c152c796 AM |
10495 | goto error_return; |
10496 | } | |
10497 | ||
10498 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
10499 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
10500 | esdo->rel.count = 0; |
10501 | esdo->rela.count = 0; | |
c152c796 AM |
10502 | } |
10503 | ||
10504 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
10505 | ||
10506 | /* We have now assigned file positions for all the sections except | |
10507 | .symtab and .strtab. We start the .symtab section at the current | |
10508 | file position, and write directly to it. We build the .strtab | |
10509 | section in memory. */ | |
10510 | bfd_get_symcount (abfd) = 0; | |
10511 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
10512 | /* sh_name is set in prep_headers. */ | |
10513 | symtab_hdr->sh_type = SHT_SYMTAB; | |
10514 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
10515 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
10516 | /* sh_link is set in assign_section_numbers. */ | |
10517 | /* sh_info is set below. */ | |
10518 | /* sh_offset is set just below. */ | |
72de5009 | 10519 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 AM |
10520 | |
10521 | off = elf_tdata (abfd)->next_file_pos; | |
10522 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
10523 | ||
10524 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
10525 | incorrect. We do not yet know the size of the .symtab section. | |
10526 | We correct next_file_pos below, after we do know the size. */ | |
10527 | ||
10528 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
10529 | continuously seeking to the right position in the file. */ | |
10530 | if (! info->keep_memory || max_sym_count < 20) | |
10531 | finfo.symbuf_size = 20; | |
10532 | else | |
10533 | finfo.symbuf_size = max_sym_count; | |
10534 | amt = finfo.symbuf_size; | |
10535 | amt *= bed->s->sizeof_sym; | |
a50b1753 | 10536 | finfo.symbuf = (bfd_byte *) bfd_malloc (amt); |
c152c796 AM |
10537 | if (finfo.symbuf == NULL) |
10538 | goto error_return; | |
4fbb74a6 | 10539 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
c152c796 AM |
10540 | { |
10541 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
10542 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
10543 | finfo.shndxbuf_size = amt; | |
10544 | amt *= sizeof (Elf_External_Sym_Shndx); | |
a50b1753 | 10545 | finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
c152c796 AM |
10546 | if (finfo.symshndxbuf == NULL) |
10547 | goto error_return; | |
10548 | } | |
10549 | ||
10550 | /* Start writing out the symbol table. The first symbol is always a | |
10551 | dummy symbol. */ | |
10552 | if (info->strip != strip_all | |
10553 | || emit_relocs) | |
10554 | { | |
10555 | elfsym.st_value = 0; | |
10556 | elfsym.st_size = 0; | |
10557 | elfsym.st_info = 0; | |
10558 | elfsym.st_other = 0; | |
10559 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 10560 | elfsym.st_target_internal = 0; |
6e0b88f1 AM |
10561 | if (elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, |
10562 | NULL) != 1) | |
c152c796 AM |
10563 | goto error_return; |
10564 | } | |
10565 | ||
c152c796 AM |
10566 | /* Output a symbol for each section. We output these even if we are |
10567 | discarding local symbols, since they are used for relocs. These | |
10568 | symbols have no names. We store the index of each one in the | |
10569 | index field of the section, so that we can find it again when | |
10570 | outputting relocs. */ | |
10571 | if (info->strip != strip_all | |
10572 | || emit_relocs) | |
10573 | { | |
10574 | elfsym.st_size = 0; | |
10575 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
10576 | elfsym.st_other = 0; | |
f0b5bb34 | 10577 | elfsym.st_value = 0; |
35fc36a8 | 10578 | elfsym.st_target_internal = 0; |
c152c796 AM |
10579 | for (i = 1; i < elf_numsections (abfd); i++) |
10580 | { | |
10581 | o = bfd_section_from_elf_index (abfd, i); | |
10582 | if (o != NULL) | |
f0b5bb34 AM |
10583 | { |
10584 | o->target_index = bfd_get_symcount (abfd); | |
10585 | elfsym.st_shndx = i; | |
10586 | if (!info->relocatable) | |
10587 | elfsym.st_value = o->vma; | |
6e0b88f1 | 10588 | if (elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL) != 1) |
f0b5bb34 AM |
10589 | goto error_return; |
10590 | } | |
c152c796 AM |
10591 | } |
10592 | } | |
10593 | ||
10594 | /* Allocate some memory to hold information read in from the input | |
10595 | files. */ | |
10596 | if (max_contents_size != 0) | |
10597 | { | |
a50b1753 | 10598 | finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
c152c796 AM |
10599 | if (finfo.contents == NULL) |
10600 | goto error_return; | |
10601 | } | |
10602 | ||
10603 | if (max_external_reloc_size != 0) | |
10604 | { | |
10605 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
10606 | if (finfo.external_relocs == NULL) | |
10607 | goto error_return; | |
10608 | } | |
10609 | ||
10610 | if (max_internal_reloc_count != 0) | |
10611 | { | |
10612 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
10613 | amt *= sizeof (Elf_Internal_Rela); | |
a50b1753 | 10614 | finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
c152c796 AM |
10615 | if (finfo.internal_relocs == NULL) |
10616 | goto error_return; | |
10617 | } | |
10618 | ||
10619 | if (max_sym_count != 0) | |
10620 | { | |
10621 | amt = max_sym_count * bed->s->sizeof_sym; | |
a50b1753 | 10622 | finfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
c152c796 AM |
10623 | if (finfo.external_syms == NULL) |
10624 | goto error_return; | |
10625 | ||
10626 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
a50b1753 | 10627 | finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
c152c796 AM |
10628 | if (finfo.internal_syms == NULL) |
10629 | goto error_return; | |
10630 | ||
10631 | amt = max_sym_count * sizeof (long); | |
a50b1753 | 10632 | finfo.indices = (long int *) bfd_malloc (amt); |
c152c796 AM |
10633 | if (finfo.indices == NULL) |
10634 | goto error_return; | |
10635 | ||
10636 | amt = max_sym_count * sizeof (asection *); | |
a50b1753 | 10637 | finfo.sections = (asection **) bfd_malloc (amt); |
c152c796 AM |
10638 | if (finfo.sections == NULL) |
10639 | goto error_return; | |
10640 | } | |
10641 | ||
10642 | if (max_sym_shndx_count != 0) | |
10643 | { | |
10644 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 | 10645 | finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
c152c796 AM |
10646 | if (finfo.locsym_shndx == NULL) |
10647 | goto error_return; | |
10648 | } | |
10649 | ||
10650 | if (elf_hash_table (info)->tls_sec) | |
10651 | { | |
10652 | bfd_vma base, end = 0; | |
10653 | asection *sec; | |
10654 | ||
10655 | for (sec = elf_hash_table (info)->tls_sec; | |
10656 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
10657 | sec = sec->next) | |
10658 | { | |
3a800eb9 | 10659 | bfd_size_type size = sec->size; |
c152c796 | 10660 | |
3a800eb9 AM |
10661 | if (size == 0 |
10662 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 10663 | { |
91d6fa6a NC |
10664 | struct bfd_link_order *ord = sec->map_tail.link_order; |
10665 | ||
10666 | if (ord != NULL) | |
10667 | size = ord->offset + ord->size; | |
c152c796 AM |
10668 | } |
10669 | end = sec->vma + size; | |
10670 | } | |
10671 | base = elf_hash_table (info)->tls_sec->vma; | |
7dc98aea RO |
10672 | /* Only align end of TLS section if static TLS doesn't have special |
10673 | alignment requirements. */ | |
10674 | if (bed->static_tls_alignment == 1) | |
10675 | end = align_power (end, | |
10676 | elf_hash_table (info)->tls_sec->alignment_power); | |
c152c796 AM |
10677 | elf_hash_table (info)->tls_size = end - base; |
10678 | } | |
10679 | ||
0b52efa6 PB |
10680 | /* Reorder SHF_LINK_ORDER sections. */ |
10681 | for (o = abfd->sections; o != NULL; o = o->next) | |
10682 | { | |
10683 | if (!elf_fixup_link_order (abfd, o)) | |
10684 | return FALSE; | |
10685 | } | |
10686 | ||
c152c796 AM |
10687 | /* Since ELF permits relocations to be against local symbols, we |
10688 | must have the local symbols available when we do the relocations. | |
10689 | Since we would rather only read the local symbols once, and we | |
10690 | would rather not keep them in memory, we handle all the | |
10691 | relocations for a single input file at the same time. | |
10692 | ||
10693 | Unfortunately, there is no way to know the total number of local | |
10694 | symbols until we have seen all of them, and the local symbol | |
10695 | indices precede the global symbol indices. This means that when | |
10696 | we are generating relocatable output, and we see a reloc against | |
10697 | a global symbol, we can not know the symbol index until we have | |
10698 | finished examining all the local symbols to see which ones we are | |
10699 | going to output. To deal with this, we keep the relocations in | |
10700 | memory, and don't output them until the end of the link. This is | |
10701 | an unfortunate waste of memory, but I don't see a good way around | |
10702 | it. Fortunately, it only happens when performing a relocatable | |
10703 | link, which is not the common case. FIXME: If keep_memory is set | |
10704 | we could write the relocs out and then read them again; I don't | |
10705 | know how bad the memory loss will be. */ | |
10706 | ||
10707 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
10708 | sub->output_has_begun = FALSE; | |
10709 | for (o = abfd->sections; o != NULL; o = o->next) | |
10710 | { | |
8423293d | 10711 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
10712 | { |
10713 | if (p->type == bfd_indirect_link_order | |
10714 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
10715 | == bfd_target_elf_flavour) | |
10716 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
10717 | { | |
10718 | if (! sub->output_has_begun) | |
10719 | { | |
10720 | if (! elf_link_input_bfd (&finfo, sub)) | |
10721 | goto error_return; | |
10722 | sub->output_has_begun = TRUE; | |
10723 | } | |
10724 | } | |
10725 | else if (p->type == bfd_section_reloc_link_order | |
10726 | || p->type == bfd_symbol_reloc_link_order) | |
10727 | { | |
10728 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
10729 | goto error_return; | |
10730 | } | |
10731 | else | |
10732 | { | |
10733 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
10734 | { |
10735 | if (p->type == bfd_indirect_link_order | |
10736 | && (bfd_get_flavour (sub) | |
10737 | == bfd_target_elf_flavour) | |
10738 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
10739 | != bed->s->elfclass)) | |
10740 | { | |
10741 | const char *iclass, *oclass; | |
10742 | ||
10743 | if (bed->s->elfclass == ELFCLASS64) | |
10744 | { | |
10745 | iclass = "ELFCLASS32"; | |
10746 | oclass = "ELFCLASS64"; | |
10747 | } | |
10748 | else | |
10749 | { | |
10750 | iclass = "ELFCLASS64"; | |
10751 | oclass = "ELFCLASS32"; | |
10752 | } | |
10753 | ||
10754 | bfd_set_error (bfd_error_wrong_format); | |
10755 | (*_bfd_error_handler) | |
10756 | (_("%B: file class %s incompatible with %s"), | |
10757 | sub, iclass, oclass); | |
10758 | } | |
10759 | ||
10760 | goto error_return; | |
10761 | } | |
c152c796 AM |
10762 | } |
10763 | } | |
10764 | } | |
10765 | ||
c0f00686 L |
10766 | /* Free symbol buffer if needed. */ |
10767 | if (!info->reduce_memory_overheads) | |
10768 | { | |
10769 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
3fcd97f1 JJ |
10770 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
10771 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
10772 | { |
10773 | free (elf_tdata (sub)->symbuf); | |
10774 | elf_tdata (sub)->symbuf = NULL; | |
10775 | } | |
10776 | } | |
10777 | ||
c152c796 AM |
10778 | /* Output any global symbols that got converted to local in a |
10779 | version script or due to symbol visibility. We do this in a | |
10780 | separate step since ELF requires all local symbols to appear | |
10781 | prior to any global symbols. FIXME: We should only do this if | |
10782 | some global symbols were, in fact, converted to become local. | |
10783 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
10784 | eoinfo.failed = FALSE; | |
10785 | eoinfo.finfo = &finfo; | |
10786 | eoinfo.localsyms = TRUE; | |
7686d77d | 10787 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
10788 | if (eoinfo.failed) |
10789 | return FALSE; | |
10790 | ||
4e617b1e PB |
10791 | /* If backend needs to output some local symbols not present in the hash |
10792 | table, do it now. */ | |
10793 | if (bed->elf_backend_output_arch_local_syms) | |
10794 | { | |
6e0b88f1 | 10795 | typedef int (*out_sym_func) |
4e617b1e PB |
10796 | (void *, const char *, Elf_Internal_Sym *, asection *, |
10797 | struct elf_link_hash_entry *); | |
10798 | ||
10799 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
10800 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
10801 | return FALSE; | |
10802 | } | |
10803 | ||
c152c796 AM |
10804 | /* That wrote out all the local symbols. Finish up the symbol table |
10805 | with the global symbols. Even if we want to strip everything we | |
10806 | can, we still need to deal with those global symbols that got | |
10807 | converted to local in a version script. */ | |
10808 | ||
10809 | /* The sh_info field records the index of the first non local symbol. */ | |
10810 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
10811 | ||
10812 | if (dynamic | |
10813 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
10814 | { | |
10815 | Elf_Internal_Sym sym; | |
10816 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
10817 | long last_local = 0; | |
10818 | ||
10819 | /* Write out the section symbols for the output sections. */ | |
67687978 | 10820 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
10821 | { |
10822 | asection *s; | |
10823 | ||
10824 | sym.st_size = 0; | |
10825 | sym.st_name = 0; | |
10826 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
10827 | sym.st_other = 0; | |
35fc36a8 | 10828 | sym.st_target_internal = 0; |
c152c796 AM |
10829 | |
10830 | for (s = abfd->sections; s != NULL; s = s->next) | |
10831 | { | |
10832 | int indx; | |
10833 | bfd_byte *dest; | |
10834 | long dynindx; | |
10835 | ||
c152c796 | 10836 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
10837 | if (dynindx <= 0) |
10838 | continue; | |
10839 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
10840 | BFD_ASSERT (indx > 0); |
10841 | sym.st_shndx = indx; | |
c0d5a53d L |
10842 | if (! check_dynsym (abfd, &sym)) |
10843 | return FALSE; | |
c152c796 AM |
10844 | sym.st_value = s->vma; |
10845 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
10846 | if (last_local < dynindx) |
10847 | last_local = dynindx; | |
c152c796 AM |
10848 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
10849 | } | |
c152c796 AM |
10850 | } |
10851 | ||
10852 | /* Write out the local dynsyms. */ | |
10853 | if (elf_hash_table (info)->dynlocal) | |
10854 | { | |
10855 | struct elf_link_local_dynamic_entry *e; | |
10856 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
10857 | { | |
10858 | asection *s; | |
10859 | bfd_byte *dest; | |
10860 | ||
935bd1e0 | 10861 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
10862 | Note that we saved a word of storage and overwrote |
10863 | the original st_name with the dynstr_index. */ | |
10864 | sym = e->isym; | |
935bd1e0 | 10865 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 10866 | |
cb33740c AM |
10867 | s = bfd_section_from_elf_index (e->input_bfd, |
10868 | e->isym.st_shndx); | |
10869 | if (s != NULL) | |
c152c796 | 10870 | { |
c152c796 AM |
10871 | sym.st_shndx = |
10872 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
10873 | if (! check_dynsym (abfd, &sym)) |
10874 | return FALSE; | |
c152c796 AM |
10875 | sym.st_value = (s->output_section->vma |
10876 | + s->output_offset | |
10877 | + e->isym.st_value); | |
10878 | } | |
10879 | ||
10880 | if (last_local < e->dynindx) | |
10881 | last_local = e->dynindx; | |
10882 | ||
10883 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
10884 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
10885 | } | |
10886 | } | |
10887 | ||
10888 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
10889 | last_local + 1; | |
10890 | } | |
10891 | ||
10892 | /* We get the global symbols from the hash table. */ | |
10893 | eoinfo.failed = FALSE; | |
10894 | eoinfo.localsyms = FALSE; | |
10895 | eoinfo.finfo = &finfo; | |
7686d77d | 10896 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
10897 | if (eoinfo.failed) |
10898 | return FALSE; | |
10899 | ||
10900 | /* If backend needs to output some symbols not present in the hash | |
10901 | table, do it now. */ | |
10902 | if (bed->elf_backend_output_arch_syms) | |
10903 | { | |
6e0b88f1 | 10904 | typedef int (*out_sym_func) |
c152c796 AM |
10905 | (void *, const char *, Elf_Internal_Sym *, asection *, |
10906 | struct elf_link_hash_entry *); | |
10907 | ||
10908 | if (! ((*bed->elf_backend_output_arch_syms) | |
10909 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
10910 | return FALSE; | |
10911 | } | |
10912 | ||
10913 | /* Flush all symbols to the file. */ | |
10914 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
10915 | return FALSE; | |
10916 | ||
10917 | /* Now we know the size of the symtab section. */ | |
10918 | off += symtab_hdr->sh_size; | |
10919 | ||
10920 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
10921 | if (symtab_shndx_hdr->sh_name != 0) | |
10922 | { | |
10923 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
10924 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
10925 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
10926 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
10927 | symtab_shndx_hdr->sh_size = amt; | |
10928 | ||
10929 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
10930 | off, TRUE); | |
10931 | ||
10932 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
10933 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
10934 | return FALSE; | |
10935 | } | |
10936 | ||
10937 | ||
10938 | /* Finish up and write out the symbol string table (.strtab) | |
10939 | section. */ | |
10940 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
10941 | /* sh_name was set in prep_headers. */ | |
10942 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
10943 | symstrtab_hdr->sh_flags = 0; | |
10944 | symstrtab_hdr->sh_addr = 0; | |
10945 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
10946 | symstrtab_hdr->sh_entsize = 0; | |
10947 | symstrtab_hdr->sh_link = 0; | |
10948 | symstrtab_hdr->sh_info = 0; | |
10949 | /* sh_offset is set just below. */ | |
10950 | symstrtab_hdr->sh_addralign = 1; | |
10951 | ||
10952 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
10953 | elf_tdata (abfd)->next_file_pos = off; | |
10954 | ||
10955 | if (bfd_get_symcount (abfd) > 0) | |
10956 | { | |
10957 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
10958 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
10959 | return FALSE; | |
10960 | } | |
10961 | ||
10962 | /* Adjust the relocs to have the correct symbol indices. */ | |
10963 | for (o = abfd->sections; o != NULL; o = o->next) | |
10964 | { | |
d4730f92 | 10965 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
10966 | if ((o->flags & SEC_RELOC) == 0) |
10967 | continue; | |
10968 | ||
d4730f92 BS |
10969 | if (esdo->rel.hdr != NULL) |
10970 | elf_link_adjust_relocs (abfd, &esdo->rel); | |
10971 | if (esdo->rela.hdr != NULL) | |
10972 | elf_link_adjust_relocs (abfd, &esdo->rela); | |
c152c796 AM |
10973 | |
10974 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
10975 | trying to swap the relocs out itself. */ | |
10976 | o->reloc_count = 0; | |
10977 | } | |
10978 | ||
10979 | if (dynamic && info->combreloc && dynobj != NULL) | |
10980 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
10981 | ||
10982 | /* If we are linking against a dynamic object, or generating a | |
10983 | shared library, finish up the dynamic linking information. */ | |
10984 | if (dynamic) | |
10985 | { | |
10986 | bfd_byte *dyncon, *dynconend; | |
10987 | ||
10988 | /* Fix up .dynamic entries. */ | |
10989 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
10990 | BFD_ASSERT (o != NULL); | |
10991 | ||
10992 | dyncon = o->contents; | |
eea6121a | 10993 | dynconend = o->contents + o->size; |
c152c796 AM |
10994 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
10995 | { | |
10996 | Elf_Internal_Dyn dyn; | |
10997 | const char *name; | |
10998 | unsigned int type; | |
10999 | ||
11000 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
11001 | ||
11002 | switch (dyn.d_tag) | |
11003 | { | |
11004 | default: | |
11005 | continue; | |
11006 | case DT_NULL: | |
11007 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
11008 | { | |
11009 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
11010 | { | |
11011 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
11012 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
11013 | default: continue; | |
11014 | } | |
11015 | dyn.d_un.d_val = relativecount; | |
11016 | relativecount = 0; | |
11017 | break; | |
11018 | } | |
11019 | continue; | |
11020 | ||
11021 | case DT_INIT: | |
11022 | name = info->init_function; | |
11023 | goto get_sym; | |
11024 | case DT_FINI: | |
11025 | name = info->fini_function; | |
11026 | get_sym: | |
11027 | { | |
11028 | struct elf_link_hash_entry *h; | |
11029 | ||
11030 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
11031 | FALSE, FALSE, TRUE); | |
11032 | if (h != NULL | |
11033 | && (h->root.type == bfd_link_hash_defined | |
11034 | || h->root.type == bfd_link_hash_defweak)) | |
11035 | { | |
bef26483 | 11036 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
11037 | o = h->root.u.def.section; |
11038 | if (o->output_section != NULL) | |
bef26483 | 11039 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
11040 | + o->output_offset); |
11041 | else | |
11042 | { | |
11043 | /* The symbol is imported from another shared | |
11044 | library and does not apply to this one. */ | |
bef26483 | 11045 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
11046 | } |
11047 | break; | |
11048 | } | |
11049 | } | |
11050 | continue; | |
11051 | ||
11052 | case DT_PREINIT_ARRAYSZ: | |
11053 | name = ".preinit_array"; | |
11054 | goto get_size; | |
11055 | case DT_INIT_ARRAYSZ: | |
11056 | name = ".init_array"; | |
11057 | goto get_size; | |
11058 | case DT_FINI_ARRAYSZ: | |
11059 | name = ".fini_array"; | |
11060 | get_size: | |
11061 | o = bfd_get_section_by_name (abfd, name); | |
11062 | if (o == NULL) | |
11063 | { | |
11064 | (*_bfd_error_handler) | |
d003868e | 11065 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
11066 | goto error_return; |
11067 | } | |
eea6121a | 11068 | if (o->size == 0) |
c152c796 AM |
11069 | (*_bfd_error_handler) |
11070 | (_("warning: %s section has zero size"), name); | |
eea6121a | 11071 | dyn.d_un.d_val = o->size; |
c152c796 AM |
11072 | break; |
11073 | ||
11074 | case DT_PREINIT_ARRAY: | |
11075 | name = ".preinit_array"; | |
11076 | goto get_vma; | |
11077 | case DT_INIT_ARRAY: | |
11078 | name = ".init_array"; | |
11079 | goto get_vma; | |
11080 | case DT_FINI_ARRAY: | |
11081 | name = ".fini_array"; | |
11082 | goto get_vma; | |
11083 | ||
11084 | case DT_HASH: | |
11085 | name = ".hash"; | |
11086 | goto get_vma; | |
fdc90cb4 JJ |
11087 | case DT_GNU_HASH: |
11088 | name = ".gnu.hash"; | |
11089 | goto get_vma; | |
c152c796 AM |
11090 | case DT_STRTAB: |
11091 | name = ".dynstr"; | |
11092 | goto get_vma; | |
11093 | case DT_SYMTAB: | |
11094 | name = ".dynsym"; | |
11095 | goto get_vma; | |
11096 | case DT_VERDEF: | |
11097 | name = ".gnu.version_d"; | |
11098 | goto get_vma; | |
11099 | case DT_VERNEED: | |
11100 | name = ".gnu.version_r"; | |
11101 | goto get_vma; | |
11102 | case DT_VERSYM: | |
11103 | name = ".gnu.version"; | |
11104 | get_vma: | |
11105 | o = bfd_get_section_by_name (abfd, name); | |
11106 | if (o == NULL) | |
11107 | { | |
11108 | (*_bfd_error_handler) | |
d003868e | 11109 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
11110 | goto error_return; |
11111 | } | |
894891db NC |
11112 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
11113 | { | |
11114 | (*_bfd_error_handler) | |
11115 | (_("warning: section '%s' is being made into a note"), name); | |
11116 | bfd_set_error (bfd_error_nonrepresentable_section); | |
11117 | goto error_return; | |
11118 | } | |
c152c796 AM |
11119 | dyn.d_un.d_ptr = o->vma; |
11120 | break; | |
11121 | ||
11122 | case DT_REL: | |
11123 | case DT_RELA: | |
11124 | case DT_RELSZ: | |
11125 | case DT_RELASZ: | |
11126 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
11127 | type = SHT_REL; | |
11128 | else | |
11129 | type = SHT_RELA; | |
11130 | dyn.d_un.d_val = 0; | |
bef26483 | 11131 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
11132 | for (i = 1; i < elf_numsections (abfd); i++) |
11133 | { | |
11134 | Elf_Internal_Shdr *hdr; | |
11135 | ||
11136 | hdr = elf_elfsections (abfd)[i]; | |
11137 | if (hdr->sh_type == type | |
11138 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
11139 | { | |
11140 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
11141 | dyn.d_un.d_val += hdr->sh_size; | |
11142 | else | |
11143 | { | |
bef26483 AM |
11144 | if (dyn.d_un.d_ptr == 0 |
11145 | || hdr->sh_addr < dyn.d_un.d_ptr) | |
11146 | dyn.d_un.d_ptr = hdr->sh_addr; | |
c152c796 AM |
11147 | } |
11148 | } | |
11149 | } | |
11150 | break; | |
11151 | } | |
11152 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
11153 | } | |
11154 | } | |
11155 | ||
11156 | /* If we have created any dynamic sections, then output them. */ | |
11157 | if (dynobj != NULL) | |
11158 | { | |
11159 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
11160 | goto error_return; | |
11161 | ||
943284cc DJ |
11162 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
11163 | if (info->warn_shared_textrel && info->shared) | |
11164 | { | |
11165 | bfd_byte *dyncon, *dynconend; | |
11166 | ||
11167 | /* Fix up .dynamic entries. */ | |
11168 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
11169 | BFD_ASSERT (o != NULL); | |
11170 | ||
11171 | dyncon = o->contents; | |
11172 | dynconend = o->contents + o->size; | |
11173 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
11174 | { | |
11175 | Elf_Internal_Dyn dyn; | |
11176 | ||
11177 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
11178 | ||
11179 | if (dyn.d_tag == DT_TEXTREL) | |
11180 | { | |
a0c8462f | 11181 | info->callbacks->einfo |
9267588c | 11182 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); |
943284cc DJ |
11183 | break; |
11184 | } | |
11185 | } | |
11186 | } | |
11187 | ||
c152c796 AM |
11188 | for (o = dynobj->sections; o != NULL; o = o->next) |
11189 | { | |
11190 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 11191 | || o->size == 0 |
c152c796 AM |
11192 | || o->output_section == bfd_abs_section_ptr) |
11193 | continue; | |
11194 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
11195 | { | |
11196 | /* At this point, we are only interested in sections | |
11197 | created by _bfd_elf_link_create_dynamic_sections. */ | |
11198 | continue; | |
11199 | } | |
3722b82f AM |
11200 | if (elf_hash_table (info)->stab_info.stabstr == o) |
11201 | continue; | |
eea6121a AM |
11202 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
11203 | continue; | |
c152c796 AM |
11204 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
11205 | != SHT_STRTAB) | |
894891db | 11206 | && (strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)) |
c152c796 | 11207 | { |
5dabe785 | 11208 | /* FIXME: octets_per_byte. */ |
c152c796 AM |
11209 | if (! bfd_set_section_contents (abfd, o->output_section, |
11210 | o->contents, | |
11211 | (file_ptr) o->output_offset, | |
eea6121a | 11212 | o->size)) |
c152c796 AM |
11213 | goto error_return; |
11214 | } | |
11215 | else | |
11216 | { | |
11217 | /* The contents of the .dynstr section are actually in a | |
11218 | stringtab. */ | |
11219 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
11220 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
11221 | || ! _bfd_elf_strtab_emit (abfd, | |
11222 | elf_hash_table (info)->dynstr)) | |
11223 | goto error_return; | |
11224 | } | |
11225 | } | |
11226 | } | |
11227 | ||
11228 | if (info->relocatable) | |
11229 | { | |
11230 | bfd_boolean failed = FALSE; | |
11231 | ||
11232 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
11233 | if (failed) | |
11234 | goto error_return; | |
11235 | } | |
11236 | ||
11237 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 11238 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
11239 | { |
11240 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
11241 | goto error_return; | |
11242 | } | |
11243 | ||
11244 | if (info->eh_frame_hdr) | |
11245 | { | |
11246 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
11247 | goto error_return; | |
11248 | } | |
11249 | ||
11250 | if (finfo.symstrtab != NULL) | |
11251 | _bfd_stringtab_free (finfo.symstrtab); | |
11252 | if (finfo.contents != NULL) | |
11253 | free (finfo.contents); | |
11254 | if (finfo.external_relocs != NULL) | |
11255 | free (finfo.external_relocs); | |
11256 | if (finfo.internal_relocs != NULL) | |
11257 | free (finfo.internal_relocs); | |
11258 | if (finfo.external_syms != NULL) | |
11259 | free (finfo.external_syms); | |
11260 | if (finfo.locsym_shndx != NULL) | |
11261 | free (finfo.locsym_shndx); | |
11262 | if (finfo.internal_syms != NULL) | |
11263 | free (finfo.internal_syms); | |
11264 | if (finfo.indices != NULL) | |
11265 | free (finfo.indices); | |
11266 | if (finfo.sections != NULL) | |
11267 | free (finfo.sections); | |
11268 | if (finfo.symbuf != NULL) | |
11269 | free (finfo.symbuf); | |
11270 | if (finfo.symshndxbuf != NULL) | |
11271 | free (finfo.symshndxbuf); | |
11272 | for (o = abfd->sections; o != NULL; o = o->next) | |
11273 | { | |
d4730f92 BS |
11274 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
11275 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11276 | free (esdo->rel.hashes); | |
11277 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11278 | free (esdo->rela.hashes); | |
c152c796 AM |
11279 | } |
11280 | ||
11281 | elf_tdata (abfd)->linker = TRUE; | |
11282 | ||
104d59d1 JM |
11283 | if (attr_section) |
11284 | { | |
a50b1753 | 11285 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 11286 | if (contents == NULL) |
d0f16d5e | 11287 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
11288 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
11289 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
11290 | free (contents); | |
11291 | } | |
11292 | ||
c152c796 AM |
11293 | return TRUE; |
11294 | ||
11295 | error_return: | |
11296 | if (finfo.symstrtab != NULL) | |
11297 | _bfd_stringtab_free (finfo.symstrtab); | |
11298 | if (finfo.contents != NULL) | |
11299 | free (finfo.contents); | |
11300 | if (finfo.external_relocs != NULL) | |
11301 | free (finfo.external_relocs); | |
11302 | if (finfo.internal_relocs != NULL) | |
11303 | free (finfo.internal_relocs); | |
11304 | if (finfo.external_syms != NULL) | |
11305 | free (finfo.external_syms); | |
11306 | if (finfo.locsym_shndx != NULL) | |
11307 | free (finfo.locsym_shndx); | |
11308 | if (finfo.internal_syms != NULL) | |
11309 | free (finfo.internal_syms); | |
11310 | if (finfo.indices != NULL) | |
11311 | free (finfo.indices); | |
11312 | if (finfo.sections != NULL) | |
11313 | free (finfo.sections); | |
11314 | if (finfo.symbuf != NULL) | |
11315 | free (finfo.symbuf); | |
11316 | if (finfo.symshndxbuf != NULL) | |
11317 | free (finfo.symshndxbuf); | |
11318 | for (o = abfd->sections; o != NULL; o = o->next) | |
11319 | { | |
d4730f92 BS |
11320 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
11321 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11322 | free (esdo->rel.hashes); | |
11323 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11324 | free (esdo->rela.hashes); | |
c152c796 AM |
11325 | } |
11326 | ||
11327 | return FALSE; | |
11328 | } | |
11329 | \f | |
5241d853 RS |
11330 | /* Initialize COOKIE for input bfd ABFD. */ |
11331 | ||
11332 | static bfd_boolean | |
11333 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
11334 | struct bfd_link_info *info, bfd *abfd) | |
11335 | { | |
11336 | Elf_Internal_Shdr *symtab_hdr; | |
11337 | const struct elf_backend_data *bed; | |
11338 | ||
11339 | bed = get_elf_backend_data (abfd); | |
11340 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11341 | ||
11342 | cookie->abfd = abfd; | |
11343 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
11344 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
11345 | if (cookie->bad_symtab) | |
11346 | { | |
11347 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11348 | cookie->extsymoff = 0; | |
11349 | } | |
11350 | else | |
11351 | { | |
11352 | cookie->locsymcount = symtab_hdr->sh_info; | |
11353 | cookie->extsymoff = symtab_hdr->sh_info; | |
11354 | } | |
11355 | ||
11356 | if (bed->s->arch_size == 32) | |
11357 | cookie->r_sym_shift = 8; | |
11358 | else | |
11359 | cookie->r_sym_shift = 32; | |
11360 | ||
11361 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
11362 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
11363 | { | |
11364 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
11365 | cookie->locsymcount, 0, | |
11366 | NULL, NULL, NULL); | |
11367 | if (cookie->locsyms == NULL) | |
11368 | { | |
11369 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
11370 | return FALSE; | |
11371 | } | |
11372 | if (info->keep_memory) | |
11373 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
11374 | } | |
11375 | return TRUE; | |
11376 | } | |
11377 | ||
11378 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
11379 | ||
11380 | static void | |
11381 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
11382 | { | |
11383 | Elf_Internal_Shdr *symtab_hdr; | |
11384 | ||
11385 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11386 | if (cookie->locsyms != NULL | |
11387 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
11388 | free (cookie->locsyms); | |
11389 | } | |
11390 | ||
11391 | /* Initialize the relocation information in COOKIE for input section SEC | |
11392 | of input bfd ABFD. */ | |
11393 | ||
11394 | static bfd_boolean | |
11395 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
11396 | struct bfd_link_info *info, bfd *abfd, | |
11397 | asection *sec) | |
11398 | { | |
11399 | const struct elf_backend_data *bed; | |
11400 | ||
11401 | if (sec->reloc_count == 0) | |
11402 | { | |
11403 | cookie->rels = NULL; | |
11404 | cookie->relend = NULL; | |
11405 | } | |
11406 | else | |
11407 | { | |
11408 | bed = get_elf_backend_data (abfd); | |
11409 | ||
11410 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
11411 | info->keep_memory); | |
11412 | if (cookie->rels == NULL) | |
11413 | return FALSE; | |
11414 | cookie->rel = cookie->rels; | |
11415 | cookie->relend = (cookie->rels | |
11416 | + sec->reloc_count * bed->s->int_rels_per_ext_rel); | |
11417 | } | |
11418 | cookie->rel = cookie->rels; | |
11419 | return TRUE; | |
11420 | } | |
11421 | ||
11422 | /* Free the memory allocated by init_reloc_cookie_rels, | |
11423 | if appropriate. */ | |
11424 | ||
11425 | static void | |
11426 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
11427 | asection *sec) | |
11428 | { | |
11429 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
11430 | free (cookie->rels); | |
11431 | } | |
11432 | ||
11433 | /* Initialize the whole of COOKIE for input section SEC. */ | |
11434 | ||
11435 | static bfd_boolean | |
11436 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
11437 | struct bfd_link_info *info, | |
11438 | asection *sec) | |
11439 | { | |
11440 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
11441 | goto error1; | |
11442 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
11443 | goto error2; | |
11444 | return TRUE; | |
11445 | ||
11446 | error2: | |
11447 | fini_reloc_cookie (cookie, sec->owner); | |
11448 | error1: | |
11449 | return FALSE; | |
11450 | } | |
11451 | ||
11452 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
11453 | if appropriate. */ | |
11454 | ||
11455 | static void | |
11456 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
11457 | asection *sec) | |
11458 | { | |
11459 | fini_reloc_cookie_rels (cookie, sec); | |
11460 | fini_reloc_cookie (cookie, sec->owner); | |
11461 | } | |
11462 | \f | |
c152c796 AM |
11463 | /* Garbage collect unused sections. */ |
11464 | ||
07adf181 AM |
11465 | /* Default gc_mark_hook. */ |
11466 | ||
11467 | asection * | |
11468 | _bfd_elf_gc_mark_hook (asection *sec, | |
11469 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
11470 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
11471 | struct elf_link_hash_entry *h, | |
11472 | Elf_Internal_Sym *sym) | |
11473 | { | |
bde6f3eb L |
11474 | const char *sec_name; |
11475 | ||
07adf181 AM |
11476 | if (h != NULL) |
11477 | { | |
11478 | switch (h->root.type) | |
11479 | { | |
11480 | case bfd_link_hash_defined: | |
11481 | case bfd_link_hash_defweak: | |
11482 | return h->root.u.def.section; | |
11483 | ||
11484 | case bfd_link_hash_common: | |
11485 | return h->root.u.c.p->section; | |
11486 | ||
bde6f3eb L |
11487 | case bfd_link_hash_undefined: |
11488 | case bfd_link_hash_undefweak: | |
11489 | /* To work around a glibc bug, keep all XXX input sections | |
11490 | when there is an as yet undefined reference to __start_XXX | |
11491 | or __stop_XXX symbols. The linker will later define such | |
11492 | symbols for orphan input sections that have a name | |
11493 | representable as a C identifier. */ | |
11494 | if (strncmp (h->root.root.string, "__start_", 8) == 0) | |
11495 | sec_name = h->root.root.string + 8; | |
11496 | else if (strncmp (h->root.root.string, "__stop_", 7) == 0) | |
11497 | sec_name = h->root.root.string + 7; | |
11498 | else | |
11499 | sec_name = NULL; | |
11500 | ||
11501 | if (sec_name && *sec_name != '\0') | |
11502 | { | |
11503 | bfd *i; | |
11504 | ||
11505 | for (i = info->input_bfds; i; i = i->link_next) | |
11506 | { | |
11507 | sec = bfd_get_section_by_name (i, sec_name); | |
11508 | if (sec) | |
11509 | sec->flags |= SEC_KEEP; | |
11510 | } | |
11511 | } | |
11512 | break; | |
11513 | ||
07adf181 AM |
11514 | default: |
11515 | break; | |
11516 | } | |
11517 | } | |
11518 | else | |
11519 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
11520 | ||
11521 | return NULL; | |
11522 | } | |
11523 | ||
5241d853 RS |
11524 | /* COOKIE->rel describes a relocation against section SEC, which is |
11525 | a section we've decided to keep. Return the section that contains | |
11526 | the relocation symbol, or NULL if no section contains it. */ | |
11527 | ||
11528 | asection * | |
11529 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
11530 | elf_gc_mark_hook_fn gc_mark_hook, | |
11531 | struct elf_reloc_cookie *cookie) | |
11532 | { | |
11533 | unsigned long r_symndx; | |
11534 | struct elf_link_hash_entry *h; | |
11535 | ||
11536 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 11537 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
11538 | return NULL; |
11539 | ||
11540 | if (r_symndx >= cookie->locsymcount | |
11541 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
11542 | { | |
11543 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
11544 | while (h->root.type == bfd_link_hash_indirect | |
11545 | || h->root.type == bfd_link_hash_warning) | |
11546 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11547 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); | |
11548 | } | |
11549 | ||
11550 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
11551 | &cookie->locsyms[r_symndx]); | |
11552 | } | |
11553 | ||
11554 | /* COOKIE->rel describes a relocation against section SEC, which is | |
11555 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 11556 | the relocation symbol. */ |
5241d853 RS |
11557 | |
11558 | bfd_boolean | |
11559 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
11560 | asection *sec, | |
11561 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 11562 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
11563 | { |
11564 | asection *rsec; | |
11565 | ||
11566 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie); | |
11567 | if (rsec && !rsec->gc_mark) | |
11568 | { | |
11569 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
11570 | rsec->gc_mark = 1; | |
5241d853 RS |
11571 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
11572 | return FALSE; | |
11573 | } | |
11574 | return TRUE; | |
11575 | } | |
11576 | ||
07adf181 AM |
11577 | /* The mark phase of garbage collection. For a given section, mark |
11578 | it and any sections in this section's group, and all the sections | |
11579 | which define symbols to which it refers. */ | |
11580 | ||
ccfa59ea AM |
11581 | bfd_boolean |
11582 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
11583 | asection *sec, | |
6a5bb875 | 11584 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
11585 | { |
11586 | bfd_boolean ret; | |
9d0a14d3 | 11587 | asection *group_sec, *eh_frame; |
c152c796 AM |
11588 | |
11589 | sec->gc_mark = 1; | |
11590 | ||
11591 | /* Mark all the sections in the group. */ | |
11592 | group_sec = elf_section_data (sec)->next_in_group; | |
11593 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 11594 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
11595 | return FALSE; |
11596 | ||
11597 | /* Look through the section relocs. */ | |
11598 | ret = TRUE; | |
9d0a14d3 RS |
11599 | eh_frame = elf_eh_frame_section (sec->owner); |
11600 | if ((sec->flags & SEC_RELOC) != 0 | |
11601 | && sec->reloc_count > 0 | |
11602 | && sec != eh_frame) | |
c152c796 | 11603 | { |
5241d853 | 11604 | struct elf_reloc_cookie cookie; |
c152c796 | 11605 | |
5241d853 RS |
11606 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
11607 | ret = FALSE; | |
c152c796 | 11608 | else |
c152c796 | 11609 | { |
5241d853 | 11610 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 11611 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
11612 | { |
11613 | ret = FALSE; | |
11614 | break; | |
11615 | } | |
11616 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
11617 | } |
11618 | } | |
9d0a14d3 RS |
11619 | |
11620 | if (ret && eh_frame && elf_fde_list (sec)) | |
11621 | { | |
11622 | struct elf_reloc_cookie cookie; | |
11623 | ||
11624 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
11625 | ret = FALSE; | |
11626 | else | |
11627 | { | |
11628 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
11629 | gc_mark_hook, &cookie)) | |
11630 | ret = FALSE; | |
11631 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
11632 | } | |
11633 | } | |
11634 | ||
c152c796 AM |
11635 | return ret; |
11636 | } | |
11637 | ||
7f6ab9f8 AM |
11638 | /* Keep debug and special sections. */ |
11639 | ||
11640 | bfd_boolean | |
11641 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
11642 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
11643 | { | |
11644 | bfd *ibfd; | |
11645 | ||
11646 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
11647 | { | |
11648 | asection *isec; | |
11649 | bfd_boolean some_kept; | |
11650 | ||
11651 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
11652 | continue; | |
11653 | ||
11654 | /* Ensure all linker created sections are kept, and see whether | |
11655 | any other section is already marked. */ | |
11656 | some_kept = FALSE; | |
11657 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
11658 | { | |
11659 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
11660 | isec->gc_mark = 1; | |
11661 | else if (isec->gc_mark) | |
11662 | some_kept = TRUE; | |
11663 | } | |
11664 | ||
11665 | /* If no section in this file will be kept, then we can | |
11666 | toss out debug sections. */ | |
11667 | if (!some_kept) | |
11668 | continue; | |
11669 | ||
11670 | /* Keep debug and special sections like .comment when they are | |
11671 | not part of a group. */ | |
11672 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
11673 | if (elf_next_in_group (isec) == NULL | |
11674 | && ((isec->flags & SEC_DEBUGGING) != 0 | |
11675 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)) | |
11676 | isec->gc_mark = 1; | |
11677 | } | |
11678 | return TRUE; | |
11679 | } | |
11680 | ||
c152c796 AM |
11681 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
11682 | ||
c17d87de NC |
11683 | struct elf_gc_sweep_symbol_info |
11684 | { | |
ccabcbe5 AM |
11685 | struct bfd_link_info *info; |
11686 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
11687 | bfd_boolean); | |
11688 | }; | |
11689 | ||
c152c796 | 11690 | static bfd_boolean |
ccabcbe5 | 11691 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 11692 | { |
1a766c68 L |
11693 | if (((h->root.type == bfd_link_hash_defined |
11694 | || h->root.type == bfd_link_hash_defweak) | |
11695 | && !h->root.u.def.section->gc_mark | |
11696 | && (!(h->root.u.def.section->owner->flags & DYNAMIC) | |
11697 | || (h->plt.refcount <= 0 | |
11698 | && h->got.refcount <= 0))) | |
11699 | || (h->root.type == bfd_link_hash_undefined | |
11700 | && h->plt.refcount <= 0 | |
11701 | && h->got.refcount <= 0)) | |
ccabcbe5 | 11702 | { |
a50b1753 | 11703 | struct elf_gc_sweep_symbol_info *inf = |
1a766c68 | 11704 | (struct elf_gc_sweep_symbol_info *) data; |
ccabcbe5 AM |
11705 | (*inf->hide_symbol) (inf->info, h, TRUE); |
11706 | } | |
c152c796 AM |
11707 | |
11708 | return TRUE; | |
11709 | } | |
11710 | ||
11711 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
11712 | ||
11713 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
11714 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
11715 | ||
11716 | static bfd_boolean | |
ccabcbe5 | 11717 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
11718 | { |
11719 | bfd *sub; | |
ccabcbe5 AM |
11720 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11721 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
11722 | unsigned long section_sym_count; | |
11723 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 AM |
11724 | |
11725 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
11726 | { | |
11727 | asection *o; | |
11728 | ||
11729 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
11730 | continue; | |
11731 | ||
11732 | for (o = sub->sections; o != NULL; o = o->next) | |
11733 | { | |
a33dafc3 L |
11734 | /* When any section in a section group is kept, we keep all |
11735 | sections in the section group. If the first member of | |
11736 | the section group is excluded, we will also exclude the | |
11737 | group section. */ | |
11738 | if (o->flags & SEC_GROUP) | |
11739 | { | |
11740 | asection *first = elf_next_in_group (o); | |
11741 | o->gc_mark = first->gc_mark; | |
11742 | } | |
c152c796 AM |
11743 | |
11744 | if (o->gc_mark) | |
11745 | continue; | |
11746 | ||
11747 | /* Skip sweeping sections already excluded. */ | |
11748 | if (o->flags & SEC_EXCLUDE) | |
11749 | continue; | |
11750 | ||
11751 | /* Since this is early in the link process, it is simple | |
11752 | to remove a section from the output. */ | |
11753 | o->flags |= SEC_EXCLUDE; | |
11754 | ||
c55fe096 | 11755 | if (info->print_gc_sections && o->size != 0) |
c17d87de NC |
11756 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); |
11757 | ||
c152c796 AM |
11758 | /* But we also have to update some of the relocation |
11759 | info we collected before. */ | |
11760 | if (gc_sweep_hook | |
e8aaee2a AM |
11761 | && (o->flags & SEC_RELOC) != 0 |
11762 | && o->reloc_count > 0 | |
11763 | && !bfd_is_abs_section (o->output_section)) | |
c152c796 AM |
11764 | { |
11765 | Elf_Internal_Rela *internal_relocs; | |
11766 | bfd_boolean r; | |
11767 | ||
11768 | internal_relocs | |
11769 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
11770 | info->keep_memory); | |
11771 | if (internal_relocs == NULL) | |
11772 | return FALSE; | |
11773 | ||
11774 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
11775 | ||
11776 | if (elf_section_data (o)->relocs != internal_relocs) | |
11777 | free (internal_relocs); | |
11778 | ||
11779 | if (!r) | |
11780 | return FALSE; | |
11781 | } | |
11782 | } | |
11783 | } | |
11784 | ||
11785 | /* Remove the symbols that were in the swept sections from the dynamic | |
11786 | symbol table. GCFIXME: Anyone know how to get them out of the | |
11787 | static symbol table as well? */ | |
ccabcbe5 AM |
11788 | sweep_info.info = info; |
11789 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
11790 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
11791 | &sweep_info); | |
c152c796 | 11792 | |
ccabcbe5 | 11793 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
11794 | return TRUE; |
11795 | } | |
11796 | ||
11797 | /* Propagate collected vtable information. This is called through | |
11798 | elf_link_hash_traverse. */ | |
11799 | ||
11800 | static bfd_boolean | |
11801 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
11802 | { | |
c152c796 | 11803 | /* Those that are not vtables. */ |
f6e332e6 | 11804 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
11805 | return TRUE; |
11806 | ||
11807 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 11808 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
11809 | return TRUE; |
11810 | ||
11811 | /* If we've already been done, exit. */ | |
f6e332e6 | 11812 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
11813 | return TRUE; |
11814 | ||
11815 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 11816 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 11817 | |
f6e332e6 | 11818 | if (h->vtable->used == NULL) |
c152c796 AM |
11819 | { |
11820 | /* None of this table's entries were referenced. Re-use the | |
11821 | parent's table. */ | |
f6e332e6 AM |
11822 | h->vtable->used = h->vtable->parent->vtable->used; |
11823 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
11824 | } |
11825 | else | |
11826 | { | |
11827 | size_t n; | |
11828 | bfd_boolean *cu, *pu; | |
11829 | ||
11830 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 11831 | cu = h->vtable->used; |
c152c796 | 11832 | cu[-1] = TRUE; |
f6e332e6 | 11833 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
11834 | if (pu != NULL) |
11835 | { | |
11836 | const struct elf_backend_data *bed; | |
11837 | unsigned int log_file_align; | |
11838 | ||
11839 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
11840 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 11841 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
11842 | while (n--) |
11843 | { | |
11844 | if (*pu) | |
11845 | *cu = TRUE; | |
11846 | pu++; | |
11847 | cu++; | |
11848 | } | |
11849 | } | |
11850 | } | |
11851 | ||
11852 | return TRUE; | |
11853 | } | |
11854 | ||
11855 | static bfd_boolean | |
11856 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
11857 | { | |
11858 | asection *sec; | |
11859 | bfd_vma hstart, hend; | |
11860 | Elf_Internal_Rela *relstart, *relend, *rel; | |
11861 | const struct elf_backend_data *bed; | |
11862 | unsigned int log_file_align; | |
11863 | ||
c152c796 AM |
11864 | /* Take care of both those symbols that do not describe vtables as |
11865 | well as those that are not loaded. */ | |
f6e332e6 | 11866 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
11867 | return TRUE; |
11868 | ||
11869 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
11870 | || h->root.type == bfd_link_hash_defweak); | |
11871 | ||
11872 | sec = h->root.u.def.section; | |
11873 | hstart = h->root.u.def.value; | |
11874 | hend = hstart + h->size; | |
11875 | ||
11876 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
11877 | if (!relstart) | |
11878 | return *(bfd_boolean *) okp = FALSE; | |
11879 | bed = get_elf_backend_data (sec->owner); | |
11880 | log_file_align = bed->s->log_file_align; | |
11881 | ||
11882 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
11883 | ||
11884 | for (rel = relstart; rel < relend; ++rel) | |
11885 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
11886 | { | |
11887 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
11888 | if (h->vtable->used |
11889 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
11890 | { |
11891 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 11892 | if (h->vtable->used[entry]) |
c152c796 AM |
11893 | continue; |
11894 | } | |
11895 | /* Otherwise, kill it. */ | |
11896 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
11897 | } | |
11898 | ||
11899 | return TRUE; | |
11900 | } | |
11901 | ||
87538722 AM |
11902 | /* Mark sections containing dynamically referenced symbols. When |
11903 | building shared libraries, we must assume that any visible symbol is | |
11904 | referenced. */ | |
715df9b8 | 11905 | |
64d03ab5 AM |
11906 | bfd_boolean |
11907 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 11908 | { |
87538722 AM |
11909 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
11910 | ||
715df9b8 EB |
11911 | if ((h->root.type == bfd_link_hash_defined |
11912 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 11913 | && (h->ref_dynamic |
5adcfd8b | 11914 | || (!info->executable |
87538722 AM |
11915 | && h->def_regular |
11916 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL | |
fd91d419 | 11917 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
54e8959c L |
11918 | && (strchr (h->root.root.string, ELF_VER_CHR) != NULL |
11919 | || !bfd_hide_sym_by_version (info->version_info, | |
11920 | h->root.root.string))))) | |
715df9b8 EB |
11921 | h->root.u.def.section->flags |= SEC_KEEP; |
11922 | ||
11923 | return TRUE; | |
11924 | } | |
3b36f7e6 | 11925 | |
74f0fb50 AM |
11926 | /* Keep all sections containing symbols undefined on the command-line, |
11927 | and the section containing the entry symbol. */ | |
11928 | ||
11929 | void | |
11930 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
11931 | { | |
11932 | struct bfd_sym_chain *sym; | |
11933 | ||
11934 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
11935 | { | |
11936 | struct elf_link_hash_entry *h; | |
11937 | ||
11938 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
11939 | FALSE, FALSE, FALSE); | |
11940 | ||
11941 | if (h != NULL | |
11942 | && (h->root.type == bfd_link_hash_defined | |
11943 | || h->root.type == bfd_link_hash_defweak) | |
11944 | && !bfd_is_abs_section (h->root.u.def.section)) | |
11945 | h->root.u.def.section->flags |= SEC_KEEP; | |
11946 | } | |
11947 | } | |
11948 | ||
c152c796 AM |
11949 | /* Do mark and sweep of unused sections. */ |
11950 | ||
11951 | bfd_boolean | |
11952 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
11953 | { | |
11954 | bfd_boolean ok = TRUE; | |
11955 | bfd *sub; | |
6a5bb875 | 11956 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 11957 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 11958 | |
64d03ab5 | 11959 | if (!bed->can_gc_sections |
715df9b8 | 11960 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
11961 | { |
11962 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
11963 | return TRUE; | |
11964 | } | |
11965 | ||
74f0fb50 AM |
11966 | bed->gc_keep (info); |
11967 | ||
9d0a14d3 RS |
11968 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
11969 | at the .eh_frame section if we can mark the FDEs individually. */ | |
11970 | _bfd_elf_begin_eh_frame_parsing (info); | |
11971 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
11972 | { | |
11973 | asection *sec; | |
11974 | struct elf_reloc_cookie cookie; | |
11975 | ||
11976 | sec = bfd_get_section_by_name (sub, ".eh_frame"); | |
11977 | if (sec && init_reloc_cookie_for_section (&cookie, info, sec)) | |
11978 | { | |
11979 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
11980 | if (elf_section_data (sec)->sec_info) | |
11981 | elf_eh_frame_section (sub) = sec; | |
11982 | fini_reloc_cookie_for_section (&cookie, sec); | |
11983 | } | |
11984 | } | |
11985 | _bfd_elf_end_eh_frame_parsing (info); | |
11986 | ||
c152c796 AM |
11987 | /* Apply transitive closure to the vtable entry usage info. */ |
11988 | elf_link_hash_traverse (elf_hash_table (info), | |
11989 | elf_gc_propagate_vtable_entries_used, | |
11990 | &ok); | |
11991 | if (!ok) | |
11992 | return FALSE; | |
11993 | ||
11994 | /* Kill the vtable relocations that were not used. */ | |
11995 | elf_link_hash_traverse (elf_hash_table (info), | |
11996 | elf_gc_smash_unused_vtentry_relocs, | |
11997 | &ok); | |
11998 | if (!ok) | |
11999 | return FALSE; | |
12000 | ||
715df9b8 EB |
12001 | /* Mark dynamically referenced symbols. */ |
12002 | if (elf_hash_table (info)->dynamic_sections_created) | |
12003 | elf_link_hash_traverse (elf_hash_table (info), | |
64d03ab5 | 12004 | bed->gc_mark_dynamic_ref, |
87538722 | 12005 | info); |
c152c796 | 12006 | |
715df9b8 | 12007 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 12008 | gc_mark_hook = bed->gc_mark_hook; |
c152c796 AM |
12009 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) |
12010 | { | |
12011 | asection *o; | |
12012 | ||
12013 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
12014 | continue; | |
12015 | ||
7f6ab9f8 AM |
12016 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
12017 | Also treat note sections as a root, if the section is not part | |
12018 | of a group. */ | |
c152c796 | 12019 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
12020 | if (!o->gc_mark |
12021 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 12022 | && ((o->flags & SEC_KEEP) != 0 |
7f6ab9f8 AM |
12023 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
12024 | && elf_next_in_group (o) == NULL ))) | |
12025 | { | |
12026 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
12027 | return FALSE; | |
12028 | } | |
c152c796 AM |
12029 | } |
12030 | ||
6a5bb875 | 12031 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 12032 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 12033 | |
c152c796 | 12034 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 12035 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
12036 | } |
12037 | \f | |
12038 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
12039 | ||
12040 | bfd_boolean | |
12041 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
12042 | asection *sec, | |
12043 | struct elf_link_hash_entry *h, | |
12044 | bfd_vma offset) | |
12045 | { | |
12046 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
12047 | struct elf_link_hash_entry **search, *child; | |
12048 | bfd_size_type extsymcount; | |
12049 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12050 | ||
12051 | /* The sh_info field of the symtab header tells us where the | |
12052 | external symbols start. We don't care about the local symbols at | |
12053 | this point. */ | |
12054 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
12055 | if (!elf_bad_symtab (abfd)) | |
12056 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
12057 | ||
12058 | sym_hashes = elf_sym_hashes (abfd); | |
12059 | sym_hashes_end = sym_hashes + extsymcount; | |
12060 | ||
12061 | /* Hunt down the child symbol, which is in this section at the same | |
12062 | offset as the relocation. */ | |
12063 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
12064 | { | |
12065 | if ((child = *search) != NULL | |
12066 | && (child->root.type == bfd_link_hash_defined | |
12067 | || child->root.type == bfd_link_hash_defweak) | |
12068 | && child->root.u.def.section == sec | |
12069 | && child->root.u.def.value == offset) | |
12070 | goto win; | |
12071 | } | |
12072 | ||
d003868e AM |
12073 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
12074 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
12075 | bfd_set_error (bfd_error_invalid_operation); |
12076 | return FALSE; | |
12077 | ||
12078 | win: | |
f6e332e6 AM |
12079 | if (!child->vtable) |
12080 | { | |
a50b1753 NC |
12081 | child->vtable = (struct elf_link_virtual_table_entry *) |
12082 | bfd_zalloc (abfd, sizeof (*child->vtable)); | |
f6e332e6 AM |
12083 | if (!child->vtable) |
12084 | return FALSE; | |
12085 | } | |
c152c796 AM |
12086 | if (!h) |
12087 | { | |
12088 | /* This *should* only be the absolute section. It could potentially | |
12089 | be that someone has defined a non-global vtable though, which | |
12090 | would be bad. It isn't worth paging in the local symbols to be | |
12091 | sure though; that case should simply be handled by the assembler. */ | |
12092 | ||
f6e332e6 | 12093 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
12094 | } |
12095 | else | |
f6e332e6 | 12096 | child->vtable->parent = h; |
c152c796 AM |
12097 | |
12098 | return TRUE; | |
12099 | } | |
12100 | ||
12101 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
12102 | ||
12103 | bfd_boolean | |
12104 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
12105 | asection *sec ATTRIBUTE_UNUSED, | |
12106 | struct elf_link_hash_entry *h, | |
12107 | bfd_vma addend) | |
12108 | { | |
12109 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12110 | unsigned int log_file_align = bed->s->log_file_align; | |
12111 | ||
f6e332e6 AM |
12112 | if (!h->vtable) |
12113 | { | |
a50b1753 NC |
12114 | h->vtable = (struct elf_link_virtual_table_entry *) |
12115 | bfd_zalloc (abfd, sizeof (*h->vtable)); | |
f6e332e6 AM |
12116 | if (!h->vtable) |
12117 | return FALSE; | |
12118 | } | |
12119 | ||
12120 | if (addend >= h->vtable->size) | |
c152c796 AM |
12121 | { |
12122 | size_t size, bytes, file_align; | |
f6e332e6 | 12123 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
12124 | |
12125 | /* While the symbol is undefined, we have to be prepared to handle | |
12126 | a zero size. */ | |
12127 | file_align = 1 << log_file_align; | |
12128 | if (h->root.type == bfd_link_hash_undefined) | |
12129 | size = addend + file_align; | |
12130 | else | |
12131 | { | |
12132 | size = h->size; | |
12133 | if (addend >= size) | |
12134 | { | |
12135 | /* Oops! We've got a reference past the defined end of | |
12136 | the table. This is probably a bug -- shall we warn? */ | |
12137 | size = addend + file_align; | |
12138 | } | |
12139 | } | |
12140 | size = (size + file_align - 1) & -file_align; | |
12141 | ||
12142 | /* Allocate one extra entry for use as a "done" flag for the | |
12143 | consolidation pass. */ | |
12144 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
12145 | ||
12146 | if (ptr) | |
12147 | { | |
a50b1753 | 12148 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
12149 | |
12150 | if (ptr != NULL) | |
12151 | { | |
12152 | size_t oldbytes; | |
12153 | ||
f6e332e6 | 12154 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
12155 | * sizeof (bfd_boolean)); |
12156 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
12157 | } | |
12158 | } | |
12159 | else | |
a50b1753 | 12160 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
12161 | |
12162 | if (ptr == NULL) | |
12163 | return FALSE; | |
12164 | ||
12165 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
12166 | h->vtable->used = ptr + 1; |
12167 | h->vtable->size = size; | |
c152c796 AM |
12168 | } |
12169 | ||
f6e332e6 | 12170 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
12171 | |
12172 | return TRUE; | |
12173 | } | |
12174 | ||
ae17ab41 CM |
12175 | /* Map an ELF section header flag to its corresponding string. */ |
12176 | typedef struct | |
12177 | { | |
12178 | char *flag_name; | |
12179 | flagword flag_value; | |
12180 | } elf_flags_to_name_table; | |
12181 | ||
12182 | static elf_flags_to_name_table elf_flags_to_names [] = | |
12183 | { | |
12184 | { "SHF_WRITE", SHF_WRITE }, | |
12185 | { "SHF_ALLOC", SHF_ALLOC }, | |
12186 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
12187 | { "SHF_MERGE", SHF_MERGE }, | |
12188 | { "SHF_STRINGS", SHF_STRINGS }, | |
12189 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
12190 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
12191 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
12192 | { "SHF_GROUP", SHF_GROUP }, | |
12193 | { "SHF_TLS", SHF_TLS }, | |
12194 | { "SHF_MASKOS", SHF_MASKOS }, | |
12195 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
12196 | }; | |
12197 | ||
12198 | void | |
12199 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, | |
12200 | struct flag_info *finfo) | |
12201 | { | |
12202 | bfd *output_bfd = info->output_bfd; | |
12203 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
12204 | struct flag_info_list *tf = finfo->flag_list; | |
12205 | int with_hex = 0; | |
12206 | int without_hex = 0; | |
12207 | ||
12208 | for (tf = finfo->flag_list; tf != NULL; tf = tf->next) | |
12209 | { | |
12210 | int i; | |
12211 | if (bed->elf_backend_lookup_section_flags_hook) | |
12212 | { | |
12213 | flagword hexval = | |
12214 | (*bed->elf_backend_lookup_section_flags_hook) ((char *) tf->name); | |
12215 | ||
12216 | if (hexval != 0) | |
12217 | { | |
12218 | if (tf->with == with_flags) | |
12219 | with_hex |= hexval; | |
12220 | else if (tf->with == without_flags) | |
12221 | without_hex |= hexval; | |
12222 | tf->valid = TRUE; | |
12223 | continue; | |
12224 | } | |
12225 | } | |
12226 | for (i = 0; i < 12; i++) | |
12227 | { | |
12228 | if (!strcmp (tf->name, elf_flags_to_names[i].flag_name)) | |
12229 | { | |
12230 | if (tf->with == with_flags) | |
12231 | with_hex |= elf_flags_to_names[i].flag_value; | |
12232 | else if (tf->with == without_flags) | |
12233 | without_hex |= elf_flags_to_names[i].flag_value; | |
12234 | tf->valid = TRUE; | |
12235 | continue; | |
12236 | } | |
12237 | } | |
12238 | if (tf->valid == FALSE) | |
12239 | { | |
12240 | info->callbacks->einfo | |
12241 | (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); | |
12242 | return; | |
12243 | } | |
12244 | } | |
12245 | finfo->flags_initialized = TRUE; | |
12246 | finfo->only_with_flags |= with_hex; | |
12247 | finfo->not_with_flags |= without_hex; | |
12248 | ||
12249 | return; | |
12250 | } | |
12251 | ||
c152c796 AM |
12252 | struct alloc_got_off_arg { |
12253 | bfd_vma gotoff; | |
10455f89 | 12254 | struct bfd_link_info *info; |
c152c796 AM |
12255 | }; |
12256 | ||
12257 | /* We need a special top-level link routine to convert got reference counts | |
12258 | to real got offsets. */ | |
12259 | ||
12260 | static bfd_boolean | |
12261 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
12262 | { | |
a50b1753 | 12263 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
12264 | bfd *obfd = gofarg->info->output_bfd; |
12265 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 12266 | |
c152c796 AM |
12267 | if (h->got.refcount > 0) |
12268 | { | |
12269 | h->got.offset = gofarg->gotoff; | |
10455f89 | 12270 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
12271 | } |
12272 | else | |
12273 | h->got.offset = (bfd_vma) -1; | |
12274 | ||
12275 | return TRUE; | |
12276 | } | |
12277 | ||
12278 | /* And an accompanying bit to work out final got entry offsets once | |
12279 | we're done. Should be called from final_link. */ | |
12280 | ||
12281 | bfd_boolean | |
12282 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
12283 | struct bfd_link_info *info) | |
12284 | { | |
12285 | bfd *i; | |
12286 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12287 | bfd_vma gotoff; | |
c152c796 AM |
12288 | struct alloc_got_off_arg gofarg; |
12289 | ||
10455f89 HPN |
12290 | BFD_ASSERT (abfd == info->output_bfd); |
12291 | ||
c152c796 AM |
12292 | if (! is_elf_hash_table (info->hash)) |
12293 | return FALSE; | |
12294 | ||
12295 | /* The GOT offset is relative to the .got section, but the GOT header is | |
12296 | put into the .got.plt section, if the backend uses it. */ | |
12297 | if (bed->want_got_plt) | |
12298 | gotoff = 0; | |
12299 | else | |
12300 | gotoff = bed->got_header_size; | |
12301 | ||
12302 | /* Do the local .got entries first. */ | |
12303 | for (i = info->input_bfds; i; i = i->link_next) | |
12304 | { | |
12305 | bfd_signed_vma *local_got; | |
12306 | bfd_size_type j, locsymcount; | |
12307 | Elf_Internal_Shdr *symtab_hdr; | |
12308 | ||
12309 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
12310 | continue; | |
12311 | ||
12312 | local_got = elf_local_got_refcounts (i); | |
12313 | if (!local_got) | |
12314 | continue; | |
12315 | ||
12316 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
12317 | if (elf_bad_symtab (i)) | |
12318 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12319 | else | |
12320 | locsymcount = symtab_hdr->sh_info; | |
12321 | ||
12322 | for (j = 0; j < locsymcount; ++j) | |
12323 | { | |
12324 | if (local_got[j] > 0) | |
12325 | { | |
12326 | local_got[j] = gotoff; | |
10455f89 | 12327 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
12328 | } |
12329 | else | |
12330 | local_got[j] = (bfd_vma) -1; | |
12331 | } | |
12332 | } | |
12333 | ||
12334 | /* Then the global .got entries. .plt refcounts are handled by | |
12335 | adjust_dynamic_symbol */ | |
12336 | gofarg.gotoff = gotoff; | |
10455f89 | 12337 | gofarg.info = info; |
c152c796 AM |
12338 | elf_link_hash_traverse (elf_hash_table (info), |
12339 | elf_gc_allocate_got_offsets, | |
12340 | &gofarg); | |
12341 | return TRUE; | |
12342 | } | |
12343 | ||
12344 | /* Many folk need no more in the way of final link than this, once | |
12345 | got entry reference counting is enabled. */ | |
12346 | ||
12347 | bfd_boolean | |
12348 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
12349 | { | |
12350 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
12351 | return FALSE; | |
12352 | ||
12353 | /* Invoke the regular ELF backend linker to do all the work. */ | |
12354 | return bfd_elf_final_link (abfd, info); | |
12355 | } | |
12356 | ||
12357 | bfd_boolean | |
12358 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
12359 | { | |
a50b1753 | 12360 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
12361 | |
12362 | if (rcookie->bad_symtab) | |
12363 | rcookie->rel = rcookie->rels; | |
12364 | ||
12365 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
12366 | { | |
12367 | unsigned long r_symndx; | |
12368 | ||
12369 | if (! rcookie->bad_symtab) | |
12370 | if (rcookie->rel->r_offset > offset) | |
12371 | return FALSE; | |
12372 | if (rcookie->rel->r_offset != offset) | |
12373 | continue; | |
12374 | ||
12375 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 12376 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
12377 | return TRUE; |
12378 | ||
12379 | if (r_symndx >= rcookie->locsymcount | |
12380 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12381 | { | |
12382 | struct elf_link_hash_entry *h; | |
12383 | ||
12384 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
12385 | ||
12386 | while (h->root.type == bfd_link_hash_indirect | |
12387 | || h->root.type == bfd_link_hash_warning) | |
12388 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
12389 | ||
12390 | if ((h->root.type == bfd_link_hash_defined | |
12391 | || h->root.type == bfd_link_hash_defweak) | |
12392 | && elf_discarded_section (h->root.u.def.section)) | |
12393 | return TRUE; | |
12394 | else | |
12395 | return FALSE; | |
12396 | } | |
12397 | else | |
12398 | { | |
12399 | /* It's not a relocation against a global symbol, | |
12400 | but it could be a relocation against a local | |
12401 | symbol for a discarded section. */ | |
12402 | asection *isec; | |
12403 | Elf_Internal_Sym *isym; | |
12404 | ||
12405 | /* Need to: get the symbol; get the section. */ | |
12406 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c AM |
12407 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
12408 | if (isec != NULL && elf_discarded_section (isec)) | |
12409 | return TRUE; | |
c152c796 AM |
12410 | } |
12411 | return FALSE; | |
12412 | } | |
12413 | return FALSE; | |
12414 | } | |
12415 | ||
12416 | /* Discard unneeded references to discarded sections. | |
12417 | Returns TRUE if any section's size was changed. */ | |
12418 | /* This function assumes that the relocations are in sorted order, | |
12419 | which is true for all known assemblers. */ | |
12420 | ||
12421 | bfd_boolean | |
12422 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
12423 | { | |
12424 | struct elf_reloc_cookie cookie; | |
12425 | asection *stab, *eh; | |
c152c796 AM |
12426 | const struct elf_backend_data *bed; |
12427 | bfd *abfd; | |
c152c796 AM |
12428 | bfd_boolean ret = FALSE; |
12429 | ||
12430 | if (info->traditional_format | |
12431 | || !is_elf_hash_table (info->hash)) | |
12432 | return FALSE; | |
12433 | ||
ca92cecb | 12434 | _bfd_elf_begin_eh_frame_parsing (info); |
c152c796 AM |
12435 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) |
12436 | { | |
12437 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
12438 | continue; | |
12439 | ||
12440 | bed = get_elf_backend_data (abfd); | |
12441 | ||
12442 | if ((abfd->flags & DYNAMIC) != 0) | |
12443 | continue; | |
12444 | ||
8da3dbc5 AM |
12445 | eh = NULL; |
12446 | if (!info->relocatable) | |
12447 | { | |
12448 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
12449 | if (eh != NULL | |
eea6121a | 12450 | && (eh->size == 0 |
8da3dbc5 AM |
12451 | || bfd_is_abs_section (eh->output_section))) |
12452 | eh = NULL; | |
12453 | } | |
c152c796 AM |
12454 | |
12455 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
12456 | if (stab != NULL | |
eea6121a | 12457 | && (stab->size == 0 |
c152c796 AM |
12458 | || bfd_is_abs_section (stab->output_section) |
12459 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
12460 | stab = NULL; | |
12461 | ||
12462 | if (stab == NULL | |
12463 | && eh == NULL | |
12464 | && bed->elf_backend_discard_info == NULL) | |
12465 | continue; | |
12466 | ||
5241d853 RS |
12467 | if (!init_reloc_cookie (&cookie, info, abfd)) |
12468 | return FALSE; | |
c152c796 | 12469 | |
5241d853 RS |
12470 | if (stab != NULL |
12471 | && stab->reloc_count > 0 | |
12472 | && init_reloc_cookie_rels (&cookie, info, abfd, stab)) | |
c152c796 | 12473 | { |
5241d853 RS |
12474 | if (_bfd_discard_section_stabs (abfd, stab, |
12475 | elf_section_data (stab)->sec_info, | |
12476 | bfd_elf_reloc_symbol_deleted_p, | |
12477 | &cookie)) | |
12478 | ret = TRUE; | |
12479 | fini_reloc_cookie_rels (&cookie, stab); | |
c152c796 AM |
12480 | } |
12481 | ||
5241d853 RS |
12482 | if (eh != NULL |
12483 | && init_reloc_cookie_rels (&cookie, info, abfd, eh)) | |
c152c796 | 12484 | { |
ca92cecb | 12485 | _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie); |
c152c796 AM |
12486 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, |
12487 | bfd_elf_reloc_symbol_deleted_p, | |
12488 | &cookie)) | |
12489 | ret = TRUE; | |
5241d853 | 12490 | fini_reloc_cookie_rels (&cookie, eh); |
c152c796 AM |
12491 | } |
12492 | ||
12493 | if (bed->elf_backend_discard_info != NULL | |
12494 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
12495 | ret = TRUE; | |
12496 | ||
5241d853 | 12497 | fini_reloc_cookie (&cookie, abfd); |
c152c796 | 12498 | } |
ca92cecb | 12499 | _bfd_elf_end_eh_frame_parsing (info); |
c152c796 AM |
12500 | |
12501 | if (info->eh_frame_hdr | |
12502 | && !info->relocatable | |
12503 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
12504 | ret = TRUE; | |
12505 | ||
12506 | return ret; | |
12507 | } | |
082b7297 | 12508 | |
43e1669b | 12509 | bfd_boolean |
0c511000 | 12510 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 12511 | asection *sec, |
c0f00686 | 12512 | struct bfd_link_info *info) |
082b7297 L |
12513 | { |
12514 | flagword flags; | |
c77ec726 | 12515 | const char *name, *key; |
082b7297 L |
12516 | struct bfd_section_already_linked *l; |
12517 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 12518 | |
c77ec726 AM |
12519 | if (sec->output_section == bfd_abs_section_ptr) |
12520 | return FALSE; | |
0c511000 | 12521 | |
c77ec726 | 12522 | flags = sec->flags; |
0c511000 | 12523 | |
c77ec726 AM |
12524 | /* Return if it isn't a linkonce section. A comdat group section |
12525 | also has SEC_LINK_ONCE set. */ | |
12526 | if ((flags & SEC_LINK_ONCE) == 0) | |
12527 | return FALSE; | |
0c511000 | 12528 | |
c77ec726 AM |
12529 | /* Don't put group member sections on our list of already linked |
12530 | sections. They are handled as a group via their group section. */ | |
12531 | if (elf_sec_group (sec) != NULL) | |
12532 | return FALSE; | |
0c511000 | 12533 | |
c77ec726 AM |
12534 | /* For a SHT_GROUP section, use the group signature as the key. */ |
12535 | name = sec->name; | |
12536 | if ((flags & SEC_GROUP) != 0 | |
12537 | && elf_next_in_group (sec) != NULL | |
12538 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
12539 | key = elf_group_name (elf_next_in_group (sec)); | |
12540 | else | |
12541 | { | |
12542 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 12543 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
12544 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
12545 | key++; | |
0c511000 | 12546 | else |
c77ec726 AM |
12547 | /* Must be a user linkonce section that doesn't follow gcc's |
12548 | naming convention. In this case we won't be matching | |
12549 | single member groups. */ | |
12550 | key = name; | |
0c511000 | 12551 | } |
6d2cd210 | 12552 | |
c77ec726 | 12553 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
12554 | |
12555 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
12556 | { | |
c2370991 | 12557 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
12558 | sections with a signature of <key> (<key> is some string), |
12559 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
12560 | Match like sections. LTO plugin sections are an exception. | |
12561 | They are always named .gnu.linkonce.t.<key> and match either | |
12562 | type of section. */ | |
12563 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
12564 | && ((flags & SEC_GROUP) != 0 | |
12565 | || strcmp (name, l->sec->name) == 0)) | |
12566 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
12567 | { |
12568 | /* The section has already been linked. See if we should | |
6d2cd210 | 12569 | issue a warning. */ |
c77ec726 AM |
12570 | if (!_bfd_handle_already_linked (sec, l, info)) |
12571 | return FALSE; | |
082b7297 | 12572 | |
c77ec726 | 12573 | if (flags & SEC_GROUP) |
3d7f7666 | 12574 | { |
c77ec726 AM |
12575 | asection *first = elf_next_in_group (sec); |
12576 | asection *s = first; | |
3d7f7666 | 12577 | |
c77ec726 | 12578 | while (s != NULL) |
3d7f7666 | 12579 | { |
c77ec726 AM |
12580 | s->output_section = bfd_abs_section_ptr; |
12581 | /* Record which group discards it. */ | |
12582 | s->kept_section = l->sec; | |
12583 | s = elf_next_in_group (s); | |
12584 | /* These lists are circular. */ | |
12585 | if (s == first) | |
12586 | break; | |
3d7f7666 L |
12587 | } |
12588 | } | |
082b7297 | 12589 | |
43e1669b | 12590 | return TRUE; |
082b7297 L |
12591 | } |
12592 | } | |
12593 | ||
c77ec726 AM |
12594 | /* A single member comdat group section may be discarded by a |
12595 | linkonce section and vice versa. */ | |
12596 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 12597 | { |
c77ec726 | 12598 | asection *first = elf_next_in_group (sec); |
c2370991 | 12599 | |
c77ec726 AM |
12600 | if (first != NULL && elf_next_in_group (first) == first) |
12601 | /* Check this single member group against linkonce sections. */ | |
12602 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
12603 | if ((l->sec->flags & SEC_GROUP) == 0 | |
12604 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
12605 | { | |
12606 | first->output_section = bfd_abs_section_ptr; | |
12607 | first->kept_section = l->sec; | |
12608 | sec->output_section = bfd_abs_section_ptr; | |
12609 | break; | |
12610 | } | |
12611 | } | |
12612 | else | |
12613 | /* Check this linkonce section against single member groups. */ | |
12614 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
12615 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 12616 | { |
c77ec726 | 12617 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 12618 | |
c77ec726 AM |
12619 | if (first != NULL |
12620 | && elf_next_in_group (first) == first | |
12621 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
12622 | { | |
12623 | sec->output_section = bfd_abs_section_ptr; | |
12624 | sec->kept_section = first; | |
12625 | break; | |
12626 | } | |
6d2cd210 | 12627 | } |
0c511000 | 12628 | |
c77ec726 AM |
12629 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
12630 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
12631 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
12632 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
12633 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
12634 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
12635 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
12636 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
12637 | The reverse order cannot happen as there is never a bfd with only the | |
12638 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
12639 | matter as here were are looking only for cross-bfd sections. */ | |
12640 | ||
12641 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
12642 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
12643 | if ((l->sec->flags & SEC_GROUP) == 0 | |
12644 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
12645 | { | |
12646 | if (abfd != l->sec->owner) | |
12647 | sec->output_section = bfd_abs_section_ptr; | |
12648 | break; | |
12649 | } | |
80c29487 | 12650 | |
082b7297 | 12651 | /* This is the first section with this name. Record it. */ |
c77ec726 | 12652 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 12653 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 12654 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 12655 | } |
81e1b023 | 12656 | |
a4d8e49b L |
12657 | bfd_boolean |
12658 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
12659 | { | |
12660 | return sym->st_shndx == SHN_COMMON; | |
12661 | } | |
12662 | ||
12663 | unsigned int | |
12664 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
12665 | { | |
12666 | return SHN_COMMON; | |
12667 | } | |
12668 | ||
12669 | asection * | |
12670 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
12671 | { | |
12672 | return bfd_com_section_ptr; | |
12673 | } | |
10455f89 HPN |
12674 | |
12675 | bfd_vma | |
12676 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
12677 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12678 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
12679 | bfd *ibfd ATTRIBUTE_UNUSED, | |
12680 | unsigned long symndx ATTRIBUTE_UNUSED) | |
12681 | { | |
12682 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12683 | return bed->s->arch_size / 8; | |
12684 | } | |
83bac4b0 NC |
12685 | |
12686 | /* Routines to support the creation of dynamic relocs. */ | |
12687 | ||
83bac4b0 NC |
12688 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
12689 | ||
12690 | static const char * | |
12691 | get_dynamic_reloc_section_name (bfd * abfd, | |
12692 | asection * sec, | |
12693 | bfd_boolean is_rela) | |
12694 | { | |
ddcf1fcf BS |
12695 | char *name; |
12696 | const char *old_name = bfd_get_section_name (NULL, sec); | |
12697 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 12698 | |
ddcf1fcf | 12699 | if (old_name == NULL) |
83bac4b0 NC |
12700 | return NULL; |
12701 | ||
ddcf1fcf BS |
12702 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
12703 | sprintf (name, "%s%s", prefix, old_name); | |
83bac4b0 NC |
12704 | |
12705 | return name; | |
12706 | } | |
12707 | ||
12708 | /* Returns the dynamic reloc section associated with SEC. | |
12709 | If necessary compute the name of the dynamic reloc section based | |
12710 | on SEC's name (looked up in ABFD's string table) and the setting | |
12711 | of IS_RELA. */ | |
12712 | ||
12713 | asection * | |
12714 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
12715 | asection * sec, | |
12716 | bfd_boolean is_rela) | |
12717 | { | |
12718 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
12719 | ||
12720 | if (reloc_sec == NULL) | |
12721 | { | |
12722 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
12723 | ||
12724 | if (name != NULL) | |
12725 | { | |
12726 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
12727 | ||
12728 | if (reloc_sec != NULL) | |
12729 | elf_section_data (sec)->sreloc = reloc_sec; | |
12730 | } | |
12731 | } | |
12732 | ||
12733 | return reloc_sec; | |
12734 | } | |
12735 | ||
12736 | /* Returns the dynamic reloc section associated with SEC. If the | |
12737 | section does not exist it is created and attached to the DYNOBJ | |
12738 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
12739 | structure. | |
f8076f98 | 12740 | |
83bac4b0 NC |
12741 | ALIGNMENT is the alignment for the newly created section and |
12742 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
12743 | or .rel.<SEC's name>. The section name is looked up in the | |
12744 | string table associated with ABFD. */ | |
12745 | ||
12746 | asection * | |
12747 | _bfd_elf_make_dynamic_reloc_section (asection * sec, | |
12748 | bfd * dynobj, | |
12749 | unsigned int alignment, | |
12750 | bfd * abfd, | |
12751 | bfd_boolean is_rela) | |
12752 | { | |
12753 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
12754 | ||
12755 | if (reloc_sec == NULL) | |
12756 | { | |
12757 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
12758 | ||
12759 | if (name == NULL) | |
12760 | return NULL; | |
12761 | ||
12762 | reloc_sec = bfd_get_section_by_name (dynobj, name); | |
12763 | ||
12764 | if (reloc_sec == NULL) | |
12765 | { | |
12766 | flagword flags; | |
12767 | ||
12768 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
12769 | if ((sec->flags & SEC_ALLOC) != 0) | |
12770 | flags |= SEC_ALLOC | SEC_LOAD; | |
12771 | ||
12772 | reloc_sec = bfd_make_section_with_flags (dynobj, name, flags); | |
12773 | if (reloc_sec != NULL) | |
12774 | { | |
12775 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) | |
12776 | reloc_sec = NULL; | |
12777 | } | |
12778 | } | |
12779 | ||
12780 | elf_section_data (sec)->sreloc = reloc_sec; | |
12781 | } | |
12782 | ||
12783 | return reloc_sec; | |
12784 | } | |
1338dd10 PB |
12785 | |
12786 | /* Copy the ELF symbol type associated with a linker hash entry. */ | |
12787 | void | |
12788 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED, | |
12789 | struct bfd_link_hash_entry * hdest, | |
12790 | struct bfd_link_hash_entry * hsrc) | |
12791 | { | |
12792 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest; | |
12793 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc; | |
12794 | ||
12795 | ehdest->type = ehsrc->type; | |
35fc36a8 | 12796 | ehdest->target_internal = ehsrc->target_internal; |
1338dd10 | 12797 | } |
351f65ca L |
12798 | |
12799 | /* Append a RELA relocation REL to section S in BFD. */ | |
12800 | ||
12801 | void | |
12802 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
12803 | { | |
12804 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12805 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
12806 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
12807 | bed->s->swap_reloca_out (abfd, rel, loc); | |
12808 | } | |
12809 | ||
12810 | /* Append a REL relocation REL to section S in BFD. */ | |
12811 | ||
12812 | void | |
12813 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
12814 | { | |
12815 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12816 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
12817 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
12818 | bed->s->swap_reloca_out (abfd, rel, loc); | |
12819 | } |