Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
b90efa5b | 2 | Copyright (C) 1995-2015 Free Software Foundation, Inc. |
252b5132 | 3 | |
8fdd7217 | 4 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 5 | |
8fdd7217 NC |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 8 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 9 | (at your option) any later version. |
252b5132 | 10 | |
8fdd7217 NC |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
252b5132 | 15 | |
8fdd7217 NC |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
252b5132 | 20 | |
252b5132 | 21 | #include "sysdep.h" |
3db64b00 | 22 | #include "bfd.h" |
53df40a4 | 23 | #include "bfd_stdint.h" |
252b5132 RH |
24 | #include "bfdlink.h" |
25 | #include "libbfd.h" | |
26 | #define ARCH_SIZE 0 | |
27 | #include "elf-bfd.h" | |
4ad4eba5 | 28 | #include "safe-ctype.h" |
ccf2f652 | 29 | #include "libiberty.h" |
66eb6687 | 30 | #include "objalloc.h" |
252b5132 | 31 | |
28caa186 AM |
32 | /* This struct is used to pass information to routines called via |
33 | elf_link_hash_traverse which must return failure. */ | |
34 | ||
35 | struct elf_info_failed | |
36 | { | |
37 | struct bfd_link_info *info; | |
28caa186 AM |
38 | bfd_boolean failed; |
39 | }; | |
40 | ||
41 | /* This structure is used to pass information to | |
42 | _bfd_elf_link_find_version_dependencies. */ | |
43 | ||
44 | struct elf_find_verdep_info | |
45 | { | |
46 | /* General link information. */ | |
47 | struct bfd_link_info *info; | |
48 | /* The number of dependencies. */ | |
49 | unsigned int vers; | |
50 | /* Whether we had a failure. */ | |
51 | bfd_boolean failed; | |
52 | }; | |
53 | ||
54 | static bfd_boolean _bfd_elf_fix_symbol_flags | |
55 | (struct elf_link_hash_entry *, struct elf_info_failed *); | |
56 | ||
2f0c68f2 CM |
57 | asection * |
58 | _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, | |
59 | unsigned long r_symndx, | |
60 | bfd_boolean discard) | |
61 | { | |
62 | if (r_symndx >= cookie->locsymcount | |
63 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
64 | { | |
65 | struct elf_link_hash_entry *h; | |
66 | ||
67 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
68 | ||
69 | while (h->root.type == bfd_link_hash_indirect | |
70 | || h->root.type == bfd_link_hash_warning) | |
71 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
72 | ||
73 | if ((h->root.type == bfd_link_hash_defined | |
74 | || h->root.type == bfd_link_hash_defweak) | |
75 | && discarded_section (h->root.u.def.section)) | |
76 | return h->root.u.def.section; | |
77 | else | |
78 | return NULL; | |
79 | } | |
80 | else | |
81 | { | |
82 | /* It's not a relocation against a global symbol, | |
83 | but it could be a relocation against a local | |
84 | symbol for a discarded section. */ | |
85 | asection *isec; | |
86 | Elf_Internal_Sym *isym; | |
87 | ||
88 | /* Need to: get the symbol; get the section. */ | |
89 | isym = &cookie->locsyms[r_symndx]; | |
90 | isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); | |
91 | if (isec != NULL | |
92 | && discard ? discarded_section (isec) : 1) | |
93 | return isec; | |
94 | } | |
95 | return NULL; | |
96 | } | |
97 | ||
d98685ac AM |
98 | /* Define a symbol in a dynamic linkage section. */ |
99 | ||
100 | struct elf_link_hash_entry * | |
101 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
102 | struct bfd_link_info *info, | |
103 | asection *sec, | |
104 | const char *name) | |
105 | { | |
106 | struct elf_link_hash_entry *h; | |
107 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 108 | const struct elf_backend_data *bed; |
d98685ac AM |
109 | |
110 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
111 | if (h != NULL) | |
112 | { | |
113 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
114 | This is a symptom of a larger problem: Absolute symbols | |
115 | defined in shared libraries can't be overridden, because we | |
116 | lose the link to the bfd which is via the symbol section. */ | |
117 | h->root.type = bfd_link_hash_new; | |
118 | } | |
119 | ||
120 | bh = &h->root; | |
cf18fda4 | 121 | bed = get_elf_backend_data (abfd); |
d98685ac | 122 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, |
cf18fda4 | 123 | sec, 0, NULL, FALSE, bed->collect, |
d98685ac AM |
124 | &bh)) |
125 | return NULL; | |
126 | h = (struct elf_link_hash_entry *) bh; | |
127 | h->def_regular = 1; | |
e28df02b | 128 | h->non_elf = 0; |
12b2843a | 129 | h->root.linker_def = 1; |
d98685ac | 130 | h->type = STT_OBJECT; |
00b7642b AM |
131 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
132 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
d98685ac | 133 | |
ccabcbe5 | 134 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
d98685ac AM |
135 | return h; |
136 | } | |
137 | ||
b34976b6 | 138 | bfd_boolean |
268b6b39 | 139 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
140 | { |
141 | flagword flags; | |
aad5d350 | 142 | asection *s; |
252b5132 | 143 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 144 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 145 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
146 | |
147 | /* This function may be called more than once. */ | |
3d4d4302 AM |
148 | s = bfd_get_linker_section (abfd, ".got"); |
149 | if (s != NULL) | |
b34976b6 | 150 | return TRUE; |
252b5132 | 151 | |
e5a52504 | 152 | flags = bed->dynamic_sec_flags; |
252b5132 | 153 | |
14b2f831 AM |
154 | s = bfd_make_section_anyway_with_flags (abfd, |
155 | (bed->rela_plts_and_copies_p | |
156 | ? ".rela.got" : ".rel.got"), | |
157 | (bed->dynamic_sec_flags | |
158 | | SEC_READONLY)); | |
6de2ae4a L |
159 | if (s == NULL |
160 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
161 | return FALSE; | |
162 | htab->srelgot = s; | |
252b5132 | 163 | |
14b2f831 | 164 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d L |
165 | if (s == NULL |
166 | || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
167 | return FALSE; | |
168 | htab->sgot = s; | |
169 | ||
252b5132 RH |
170 | if (bed->want_got_plt) |
171 | { | |
14b2f831 | 172 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 173 | if (s == NULL |
6de2ae4a L |
174 | || !bfd_set_section_alignment (abfd, s, |
175 | bed->s->log_file_align)) | |
b34976b6 | 176 | return FALSE; |
6de2ae4a | 177 | htab->sgotplt = s; |
252b5132 RH |
178 | } |
179 | ||
64e77c6d L |
180 | /* The first bit of the global offset table is the header. */ |
181 | s->size += bed->got_header_size; | |
182 | ||
2517a57f AM |
183 | if (bed->want_got_sym) |
184 | { | |
185 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
186 | (or .got.plt) section. We don't do this in the linker script | |
187 | because we don't want to define the symbol if we are not creating | |
188 | a global offset table. */ | |
6de2ae4a L |
189 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
190 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 191 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
192 | if (h == NULL) |
193 | return FALSE; | |
2517a57f | 194 | } |
252b5132 | 195 | |
b34976b6 | 196 | return TRUE; |
252b5132 RH |
197 | } |
198 | \f | |
7e9f0867 AM |
199 | /* Create a strtab to hold the dynamic symbol names. */ |
200 | static bfd_boolean | |
201 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
202 | { | |
203 | struct elf_link_hash_table *hash_table; | |
204 | ||
205 | hash_table = elf_hash_table (info); | |
206 | if (hash_table->dynobj == NULL) | |
207 | hash_table->dynobj = abfd; | |
208 | ||
209 | if (hash_table->dynstr == NULL) | |
210 | { | |
211 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
212 | if (hash_table->dynstr == NULL) | |
213 | return FALSE; | |
214 | } | |
215 | return TRUE; | |
216 | } | |
217 | ||
45d6a902 AM |
218 | /* Create some sections which will be filled in with dynamic linking |
219 | information. ABFD is an input file which requires dynamic sections | |
220 | to be created. The dynamic sections take up virtual memory space | |
221 | when the final executable is run, so we need to create them before | |
222 | addresses are assigned to the output sections. We work out the | |
223 | actual contents and size of these sections later. */ | |
252b5132 | 224 | |
b34976b6 | 225 | bfd_boolean |
268b6b39 | 226 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 227 | { |
45d6a902 | 228 | flagword flags; |
91d6fa6a | 229 | asection *s; |
9c5bfbb7 | 230 | const struct elf_backend_data *bed; |
9637f6ef | 231 | struct elf_link_hash_entry *h; |
252b5132 | 232 | |
0eddce27 | 233 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
234 | return FALSE; |
235 | ||
236 | if (elf_hash_table (info)->dynamic_sections_created) | |
237 | return TRUE; | |
238 | ||
7e9f0867 AM |
239 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
240 | return FALSE; | |
45d6a902 | 241 | |
7e9f0867 | 242 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
243 | bed = get_elf_backend_data (abfd); |
244 | ||
245 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
246 | |
247 | /* A dynamically linked executable has a .interp section, but a | |
248 | shared library does not. */ | |
36af4a4e | 249 | if (info->executable) |
252b5132 | 250 | { |
14b2f831 AM |
251 | s = bfd_make_section_anyway_with_flags (abfd, ".interp", |
252 | flags | SEC_READONLY); | |
3496cb2a | 253 | if (s == NULL) |
45d6a902 AM |
254 | return FALSE; |
255 | } | |
bb0deeff | 256 | |
45d6a902 AM |
257 | /* Create sections to hold version informations. These are removed |
258 | if they are not needed. */ | |
14b2f831 AM |
259 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", |
260 | flags | SEC_READONLY); | |
45d6a902 | 261 | if (s == NULL |
45d6a902 AM |
262 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
263 | return FALSE; | |
264 | ||
14b2f831 AM |
265 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", |
266 | flags | SEC_READONLY); | |
45d6a902 | 267 | if (s == NULL |
45d6a902 AM |
268 | || ! bfd_set_section_alignment (abfd, s, 1)) |
269 | return FALSE; | |
270 | ||
14b2f831 AM |
271 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", |
272 | flags | SEC_READONLY); | |
45d6a902 | 273 | if (s == NULL |
45d6a902 AM |
274 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
275 | return FALSE; | |
276 | ||
14b2f831 AM |
277 | s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", |
278 | flags | SEC_READONLY); | |
45d6a902 | 279 | if (s == NULL |
45d6a902 AM |
280 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
281 | return FALSE; | |
282 | ||
14b2f831 AM |
283 | s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", |
284 | flags | SEC_READONLY); | |
3496cb2a | 285 | if (s == NULL) |
45d6a902 AM |
286 | return FALSE; |
287 | ||
14b2f831 | 288 | s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 289 | if (s == NULL |
45d6a902 AM |
290 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
291 | return FALSE; | |
292 | ||
293 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
294 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
295 | only want to define it if we are, in fact, creating a .dynamic | |
296 | section. We don't want to define it if there is no .dynamic | |
297 | section, since on some ELF platforms the start up code examines it | |
298 | to decide how to initialize the process. */ | |
9637f6ef L |
299 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); |
300 | elf_hash_table (info)->hdynamic = h; | |
301 | if (h == NULL) | |
45d6a902 AM |
302 | return FALSE; |
303 | ||
fdc90cb4 JJ |
304 | if (info->emit_hash) |
305 | { | |
14b2f831 AM |
306 | s = bfd_make_section_anyway_with_flags (abfd, ".hash", |
307 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
308 | if (s == NULL |
309 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
310 | return FALSE; | |
311 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
312 | } | |
313 | ||
314 | if (info->emit_gnu_hash) | |
315 | { | |
14b2f831 AM |
316 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", |
317 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
318 | if (s == NULL |
319 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
320 | return FALSE; | |
321 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
322 | 4 32-bit words followed by variable count of 64-bit words, then | |
323 | variable count of 32-bit words. */ | |
324 | if (bed->s->arch_size == 64) | |
325 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
326 | else | |
327 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
328 | } | |
45d6a902 AM |
329 | |
330 | /* Let the backend create the rest of the sections. This lets the | |
331 | backend set the right flags. The backend will normally create | |
332 | the .got and .plt sections. */ | |
894891db NC |
333 | if (bed->elf_backend_create_dynamic_sections == NULL |
334 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
45d6a902 AM |
335 | return FALSE; |
336 | ||
337 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
338 | ||
339 | return TRUE; | |
340 | } | |
341 | ||
342 | /* Create dynamic sections when linking against a dynamic object. */ | |
343 | ||
344 | bfd_boolean | |
268b6b39 | 345 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
346 | { |
347 | flagword flags, pltflags; | |
7325306f | 348 | struct elf_link_hash_entry *h; |
45d6a902 | 349 | asection *s; |
9c5bfbb7 | 350 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 351 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 352 | |
252b5132 RH |
353 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
354 | .rel[a].bss sections. */ | |
e5a52504 | 355 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
356 | |
357 | pltflags = flags; | |
252b5132 | 358 | if (bed->plt_not_loaded) |
6df4d94c MM |
359 | /* We do not clear SEC_ALLOC here because we still want the OS to |
360 | allocate space for the section; it's just that there's nothing | |
361 | to read in from the object file. */ | |
5d1634d7 | 362 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
363 | else |
364 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
365 | if (bed->plt_readonly) |
366 | pltflags |= SEC_READONLY; | |
367 | ||
14b2f831 | 368 | s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); |
252b5132 | 369 | if (s == NULL |
252b5132 | 370 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 371 | return FALSE; |
6de2ae4a | 372 | htab->splt = s; |
252b5132 | 373 | |
d98685ac AM |
374 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
375 | .plt section. */ | |
7325306f RS |
376 | if (bed->want_plt_sym) |
377 | { | |
378 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
379 | "_PROCEDURE_LINKAGE_TABLE_"); | |
380 | elf_hash_table (info)->hplt = h; | |
381 | if (h == NULL) | |
382 | return FALSE; | |
383 | } | |
252b5132 | 384 | |
14b2f831 AM |
385 | s = bfd_make_section_anyway_with_flags (abfd, |
386 | (bed->rela_plts_and_copies_p | |
387 | ? ".rela.plt" : ".rel.plt"), | |
388 | flags | SEC_READONLY); | |
252b5132 | 389 | if (s == NULL |
45d6a902 | 390 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 391 | return FALSE; |
6de2ae4a | 392 | htab->srelplt = s; |
252b5132 RH |
393 | |
394 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 395 | return FALSE; |
252b5132 | 396 | |
3018b441 RH |
397 | if (bed->want_dynbss) |
398 | { | |
399 | /* The .dynbss section is a place to put symbols which are defined | |
400 | by dynamic objects, are referenced by regular objects, and are | |
401 | not functions. We must allocate space for them in the process | |
402 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
403 | initialize them at run time. The linker script puts the .dynbss | |
404 | section into the .bss section of the final image. */ | |
14b2f831 AM |
405 | s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", |
406 | (SEC_ALLOC | SEC_LINKER_CREATED)); | |
3496cb2a | 407 | if (s == NULL) |
b34976b6 | 408 | return FALSE; |
252b5132 | 409 | |
3018b441 | 410 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
411 | normally needed. We need to create it here, though, so that the |
412 | linker will map it to an output section. We can't just create it | |
413 | only if we need it, because we will not know whether we need it | |
414 | until we have seen all the input files, and the first time the | |
415 | main linker code calls BFD after examining all the input files | |
416 | (size_dynamic_sections) the input sections have already been | |
417 | mapped to the output sections. If the section turns out not to | |
418 | be needed, we can discard it later. We will never need this | |
419 | section when generating a shared object, since they do not use | |
420 | copy relocs. */ | |
3018b441 RH |
421 | if (! info->shared) |
422 | { | |
14b2f831 AM |
423 | s = bfd_make_section_anyway_with_flags (abfd, |
424 | (bed->rela_plts_and_copies_p | |
425 | ? ".rela.bss" : ".rel.bss"), | |
426 | flags | SEC_READONLY); | |
3018b441 | 427 | if (s == NULL |
45d6a902 | 428 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 429 | return FALSE; |
3018b441 | 430 | } |
252b5132 RH |
431 | } |
432 | ||
b34976b6 | 433 | return TRUE; |
252b5132 RH |
434 | } |
435 | \f | |
252b5132 RH |
436 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
437 | read the input files, since we need to have a list of all of them | |
438 | before we can determine the final sizes of the output sections. | |
439 | Note that we may actually call this function even though we are not | |
440 | going to output any dynamic symbols; in some cases we know that a | |
441 | symbol should be in the dynamic symbol table, but only if there is | |
442 | one. */ | |
443 | ||
b34976b6 | 444 | bfd_boolean |
c152c796 AM |
445 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
446 | struct elf_link_hash_entry *h) | |
252b5132 RH |
447 | { |
448 | if (h->dynindx == -1) | |
449 | { | |
2b0f7ef9 | 450 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 451 | char *p; |
252b5132 | 452 | const char *name; |
252b5132 RH |
453 | bfd_size_type indx; |
454 | ||
7a13edea NC |
455 | /* XXX: The ABI draft says the linker must turn hidden and |
456 | internal symbols into STB_LOCAL symbols when producing the | |
457 | DSO. However, if ld.so honors st_other in the dynamic table, | |
458 | this would not be necessary. */ | |
459 | switch (ELF_ST_VISIBILITY (h->other)) | |
460 | { | |
461 | case STV_INTERNAL: | |
462 | case STV_HIDDEN: | |
9d6eee78 L |
463 | if (h->root.type != bfd_link_hash_undefined |
464 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 465 | { |
f5385ebf | 466 | h->forced_local = 1; |
67687978 PB |
467 | if (!elf_hash_table (info)->is_relocatable_executable) |
468 | return TRUE; | |
7a13edea | 469 | } |
0444bdd4 | 470 | |
7a13edea NC |
471 | default: |
472 | break; | |
473 | } | |
474 | ||
252b5132 RH |
475 | h->dynindx = elf_hash_table (info)->dynsymcount; |
476 | ++elf_hash_table (info)->dynsymcount; | |
477 | ||
478 | dynstr = elf_hash_table (info)->dynstr; | |
479 | if (dynstr == NULL) | |
480 | { | |
481 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 482 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 483 | if (dynstr == NULL) |
b34976b6 | 484 | return FALSE; |
252b5132 RH |
485 | } |
486 | ||
487 | /* We don't put any version information in the dynamic string | |
aad5d350 | 488 | table. */ |
252b5132 RH |
489 | name = h->root.root.string; |
490 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
491 | if (p != NULL) |
492 | /* We know that the p points into writable memory. In fact, | |
493 | there are only a few symbols that have read-only names, being | |
494 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
495 | by the backends. Most symbols will have names pointing into | |
496 | an ELF string table read from a file, or to objalloc memory. */ | |
497 | *p = 0; | |
498 | ||
499 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
500 | ||
501 | if (p != NULL) | |
502 | *p = ELF_VER_CHR; | |
252b5132 RH |
503 | |
504 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 505 | return FALSE; |
252b5132 RH |
506 | h->dynstr_index = indx; |
507 | } | |
508 | ||
b34976b6 | 509 | return TRUE; |
252b5132 | 510 | } |
45d6a902 | 511 | \f |
55255dae L |
512 | /* Mark a symbol dynamic. */ |
513 | ||
28caa186 | 514 | static void |
55255dae | 515 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
516 | struct elf_link_hash_entry *h, |
517 | Elf_Internal_Sym *sym) | |
55255dae | 518 | { |
40b36307 | 519 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 520 | |
40b36307 L |
521 | /* It may be called more than once on the same H. */ |
522 | if(h->dynamic || info->relocatable) | |
55255dae L |
523 | return; |
524 | ||
40b36307 L |
525 | if ((info->dynamic_data |
526 | && (h->type == STT_OBJECT | |
527 | || (sym != NULL | |
528 | && ELF_ST_TYPE (sym->st_info) == STT_OBJECT))) | |
a0c8462f | 529 | || (d != NULL |
40b36307 L |
530 | && h->root.type == bfd_link_hash_new |
531 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
55255dae L |
532 | h->dynamic = 1; |
533 | } | |
534 | ||
45d6a902 AM |
535 | /* Record an assignment to a symbol made by a linker script. We need |
536 | this in case some dynamic object refers to this symbol. */ | |
537 | ||
538 | bfd_boolean | |
fe21a8fc L |
539 | bfd_elf_record_link_assignment (bfd *output_bfd, |
540 | struct bfd_link_info *info, | |
268b6b39 | 541 | const char *name, |
fe21a8fc L |
542 | bfd_boolean provide, |
543 | bfd_boolean hidden) | |
45d6a902 | 544 | { |
00cbee0a | 545 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 546 | struct elf_link_hash_table *htab; |
00cbee0a | 547 | const struct elf_backend_data *bed; |
45d6a902 | 548 | |
0eddce27 | 549 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
550 | return TRUE; |
551 | ||
4ea42fb7 AM |
552 | htab = elf_hash_table (info); |
553 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 554 | if (h == NULL) |
4ea42fb7 | 555 | return provide; |
45d6a902 | 556 | |
00cbee0a | 557 | switch (h->root.type) |
77cfaee6 | 558 | { |
00cbee0a L |
559 | case bfd_link_hash_defined: |
560 | case bfd_link_hash_defweak: | |
561 | case bfd_link_hash_common: | |
562 | break; | |
563 | case bfd_link_hash_undefweak: | |
564 | case bfd_link_hash_undefined: | |
565 | /* Since we're defining the symbol, don't let it seem to have not | |
566 | been defined. record_dynamic_symbol and size_dynamic_sections | |
567 | may depend on this. */ | |
4ea42fb7 | 568 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
569 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
570 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
571 | break; |
572 | case bfd_link_hash_new: | |
40b36307 | 573 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); |
55255dae | 574 | h->non_elf = 0; |
00cbee0a L |
575 | break; |
576 | case bfd_link_hash_indirect: | |
577 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 578 | the versioned symbol point to this one. */ |
00cbee0a L |
579 | bed = get_elf_backend_data (output_bfd); |
580 | hv = h; | |
581 | while (hv->root.type == bfd_link_hash_indirect | |
582 | || hv->root.type == bfd_link_hash_warning) | |
583 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
584 | /* We don't need to update h->root.u since linker will set them | |
585 | later. */ | |
586 | h->root.type = bfd_link_hash_undefined; | |
587 | hv->root.type = bfd_link_hash_indirect; | |
588 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
589 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
590 | break; | |
591 | case bfd_link_hash_warning: | |
592 | abort (); | |
593 | break; | |
55255dae | 594 | } |
45d6a902 AM |
595 | |
596 | /* If this symbol is being provided by the linker script, and it is | |
597 | currently defined by a dynamic object, but not by a regular | |
598 | object, then mark it as undefined so that the generic linker will | |
599 | force the correct value. */ | |
600 | if (provide | |
f5385ebf AM |
601 | && h->def_dynamic |
602 | && !h->def_regular) | |
45d6a902 AM |
603 | h->root.type = bfd_link_hash_undefined; |
604 | ||
605 | /* If this symbol is not being provided by the linker script, and it is | |
606 | currently defined by a dynamic object, but not by a regular object, | |
607 | then clear out any version information because the symbol will not be | |
608 | associated with the dynamic object any more. */ | |
609 | if (!provide | |
f5385ebf AM |
610 | && h->def_dynamic |
611 | && !h->def_regular) | |
45d6a902 AM |
612 | h->verinfo.verdef = NULL; |
613 | ||
f5385ebf | 614 | h->def_regular = 1; |
45d6a902 | 615 | |
eb8476a6 | 616 | if (hidden) |
fe21a8fc | 617 | { |
91d6fa6a | 618 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
619 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
620 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
fe21a8fc L |
621 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
622 | } | |
623 | ||
6fa3860b PB |
624 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
625 | and executables. */ | |
626 | if (!info->relocatable | |
627 | && h->dynindx != -1 | |
628 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
629 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
630 | h->forced_local = 1; | |
631 | ||
f5385ebf AM |
632 | if ((h->def_dynamic |
633 | || h->ref_dynamic | |
67687978 PB |
634 | || info->shared |
635 | || (info->executable && elf_hash_table (info)->is_relocatable_executable)) | |
45d6a902 AM |
636 | && h->dynindx == -1) |
637 | { | |
c152c796 | 638 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
639 | return FALSE; |
640 | ||
641 | /* If this is a weak defined symbol, and we know a corresponding | |
642 | real symbol from the same dynamic object, make sure the real | |
643 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
644 | if (h->u.weakdef != NULL |
645 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 646 | { |
f6e332e6 | 647 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
648 | return FALSE; |
649 | } | |
650 | } | |
651 | ||
652 | return TRUE; | |
653 | } | |
42751cf3 | 654 | |
8c58d23b AM |
655 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
656 | success, and 2 on a failure caused by attempting to record a symbol | |
657 | in a discarded section, eg. a discarded link-once section symbol. */ | |
658 | ||
659 | int | |
c152c796 AM |
660 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
661 | bfd *input_bfd, | |
662 | long input_indx) | |
8c58d23b AM |
663 | { |
664 | bfd_size_type amt; | |
665 | struct elf_link_local_dynamic_entry *entry; | |
666 | struct elf_link_hash_table *eht; | |
667 | struct elf_strtab_hash *dynstr; | |
668 | unsigned long dynstr_index; | |
669 | char *name; | |
670 | Elf_External_Sym_Shndx eshndx; | |
671 | char esym[sizeof (Elf64_External_Sym)]; | |
672 | ||
0eddce27 | 673 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
674 | return 0; |
675 | ||
676 | /* See if the entry exists already. */ | |
677 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
678 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
679 | return 1; | |
680 | ||
681 | amt = sizeof (*entry); | |
a50b1753 | 682 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
683 | if (entry == NULL) |
684 | return 0; | |
685 | ||
686 | /* Go find the symbol, so that we can find it's name. */ | |
687 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 688 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
689 | { |
690 | bfd_release (input_bfd, entry); | |
691 | return 0; | |
692 | } | |
693 | ||
694 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 695 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
696 | { |
697 | asection *s; | |
698 | ||
699 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
700 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
701 | { | |
702 | /* We can still bfd_release here as nothing has done another | |
703 | bfd_alloc. We can't do this later in this function. */ | |
704 | bfd_release (input_bfd, entry); | |
705 | return 2; | |
706 | } | |
707 | } | |
708 | ||
709 | name = (bfd_elf_string_from_elf_section | |
710 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
711 | entry->isym.st_name)); | |
712 | ||
713 | dynstr = elf_hash_table (info)->dynstr; | |
714 | if (dynstr == NULL) | |
715 | { | |
716 | /* Create a strtab to hold the dynamic symbol names. */ | |
717 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
718 | if (dynstr == NULL) | |
719 | return 0; | |
720 | } | |
721 | ||
b34976b6 | 722 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
723 | if (dynstr_index == (unsigned long) -1) |
724 | return 0; | |
725 | entry->isym.st_name = dynstr_index; | |
726 | ||
727 | eht = elf_hash_table (info); | |
728 | ||
729 | entry->next = eht->dynlocal; | |
730 | eht->dynlocal = entry; | |
731 | entry->input_bfd = input_bfd; | |
732 | entry->input_indx = input_indx; | |
733 | eht->dynsymcount++; | |
734 | ||
735 | /* Whatever binding the symbol had before, it's now local. */ | |
736 | entry->isym.st_info | |
737 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
738 | ||
739 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
740 | ||
741 | return 1; | |
742 | } | |
743 | ||
30b30c21 | 744 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 745 | |
30b30c21 | 746 | long |
268b6b39 AM |
747 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
748 | bfd *input_bfd, | |
749 | long input_indx) | |
30b30c21 RH |
750 | { |
751 | struct elf_link_local_dynamic_entry *e; | |
752 | ||
753 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
754 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
755 | return e->dynindx; | |
756 | return -1; | |
757 | } | |
758 | ||
759 | /* This function is used to renumber the dynamic symbols, if some of | |
760 | them are removed because they are marked as local. This is called | |
761 | via elf_link_hash_traverse. */ | |
762 | ||
b34976b6 | 763 | static bfd_boolean |
268b6b39 AM |
764 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
765 | void *data) | |
42751cf3 | 766 | { |
a50b1753 | 767 | size_t *count = (size_t *) data; |
30b30c21 | 768 | |
6fa3860b PB |
769 | if (h->forced_local) |
770 | return TRUE; | |
771 | ||
772 | if (h->dynindx != -1) | |
773 | h->dynindx = ++(*count); | |
774 | ||
775 | return TRUE; | |
776 | } | |
777 | ||
778 | ||
779 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
780 | STB_LOCAL binding. */ | |
781 | ||
782 | static bfd_boolean | |
783 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
784 | void *data) | |
785 | { | |
a50b1753 | 786 | size_t *count = (size_t *) data; |
6fa3860b | 787 | |
6fa3860b PB |
788 | if (!h->forced_local) |
789 | return TRUE; | |
790 | ||
42751cf3 | 791 | if (h->dynindx != -1) |
30b30c21 RH |
792 | h->dynindx = ++(*count); |
793 | ||
b34976b6 | 794 | return TRUE; |
42751cf3 | 795 | } |
30b30c21 | 796 | |
aee6f5b4 AO |
797 | /* Return true if the dynamic symbol for a given section should be |
798 | omitted when creating a shared library. */ | |
799 | bfd_boolean | |
800 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
801 | struct bfd_link_info *info, | |
802 | asection *p) | |
803 | { | |
74541ad4 | 804 | struct elf_link_hash_table *htab; |
ca55926c | 805 | asection *ip; |
74541ad4 | 806 | |
aee6f5b4 AO |
807 | switch (elf_section_data (p)->this_hdr.sh_type) |
808 | { | |
809 | case SHT_PROGBITS: | |
810 | case SHT_NOBITS: | |
811 | /* If sh_type is yet undecided, assume it could be | |
812 | SHT_PROGBITS/SHT_NOBITS. */ | |
813 | case SHT_NULL: | |
74541ad4 AM |
814 | htab = elf_hash_table (info); |
815 | if (p == htab->tls_sec) | |
816 | return FALSE; | |
817 | ||
818 | if (htab->text_index_section != NULL) | |
819 | return p != htab->text_index_section && p != htab->data_index_section; | |
820 | ||
ca55926c | 821 | return (htab->dynobj != NULL |
3d4d4302 | 822 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 823 | && ip->output_section == p); |
aee6f5b4 AO |
824 | |
825 | /* There shouldn't be section relative relocations | |
826 | against any other section. */ | |
827 | default: | |
828 | return TRUE; | |
829 | } | |
830 | } | |
831 | ||
062e2358 | 832 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
833 | symbol for each output section, which come first. Next come symbols |
834 | which have been forced to local binding. Then all of the back-end | |
835 | allocated local dynamic syms, followed by the rest of the global | |
836 | symbols. */ | |
30b30c21 | 837 | |
554220db AM |
838 | static unsigned long |
839 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
840 | struct bfd_link_info *info, | |
841 | unsigned long *section_sym_count) | |
30b30c21 RH |
842 | { |
843 | unsigned long dynsymcount = 0; | |
844 | ||
67687978 | 845 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
30b30c21 | 846 | { |
aee6f5b4 | 847 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
848 | asection *p; |
849 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 850 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
851 | && (p->flags & SEC_ALLOC) != 0 |
852 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
853 | elf_section_data (p)->dynindx = ++dynsymcount; | |
74541ad4 AM |
854 | else |
855 | elf_section_data (p)->dynindx = 0; | |
30b30c21 | 856 | } |
554220db | 857 | *section_sym_count = dynsymcount; |
30b30c21 | 858 | |
6fa3860b PB |
859 | elf_link_hash_traverse (elf_hash_table (info), |
860 | elf_link_renumber_local_hash_table_dynsyms, | |
861 | &dynsymcount); | |
862 | ||
30b30c21 RH |
863 | if (elf_hash_table (info)->dynlocal) |
864 | { | |
865 | struct elf_link_local_dynamic_entry *p; | |
866 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
867 | p->dynindx = ++dynsymcount; | |
868 | } | |
869 | ||
870 | elf_link_hash_traverse (elf_hash_table (info), | |
871 | elf_link_renumber_hash_table_dynsyms, | |
872 | &dynsymcount); | |
873 | ||
874 | /* There is an unused NULL entry at the head of the table which | |
875 | we must account for in our count. Unless there weren't any | |
876 | symbols, which means we'll have no table at all. */ | |
877 | if (dynsymcount != 0) | |
878 | ++dynsymcount; | |
879 | ||
ccabcbe5 AM |
880 | elf_hash_table (info)->dynsymcount = dynsymcount; |
881 | return dynsymcount; | |
30b30c21 | 882 | } |
252b5132 | 883 | |
54ac0771 L |
884 | /* Merge st_other field. */ |
885 | ||
886 | static void | |
887 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 888 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 889 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
890 | { |
891 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
892 | ||
893 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 894 | code might be needed here. */ |
54ac0771 L |
895 | if (bed->elf_backend_merge_symbol_attribute) |
896 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
897 | dynamic); | |
898 | ||
cd3416da | 899 | if (!dynamic) |
54ac0771 | 900 | { |
cd3416da AM |
901 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
902 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 903 | |
cd3416da AM |
904 | /* Keep the most constraining visibility. Leave the remainder |
905 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
906 | if (symvis - 1 < hvis - 1) | |
907 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 908 | } |
b8417128 AM |
909 | else if (definition |
910 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
911 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 912 | h->protected_def = 1; |
54ac0771 L |
913 | } |
914 | ||
4f3fedcf AM |
915 | /* This function is called when we want to merge a new symbol with an |
916 | existing symbol. It handles the various cases which arise when we | |
917 | find a definition in a dynamic object, or when there is already a | |
918 | definition in a dynamic object. The new symbol is described by | |
919 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
920 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
921 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
922 | of an old common symbol. We set OVERRIDE if the old symbol is | |
923 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
924 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
925 | to change. By OK to change, we mean that we shouldn't warn if the | |
926 | type or size does change. */ | |
45d6a902 | 927 | |
8a56bd02 | 928 | static bfd_boolean |
268b6b39 AM |
929 | _bfd_elf_merge_symbol (bfd *abfd, |
930 | struct bfd_link_info *info, | |
931 | const char *name, | |
932 | Elf_Internal_Sym *sym, | |
933 | asection **psec, | |
934 | bfd_vma *pvalue, | |
4f3fedcf AM |
935 | struct elf_link_hash_entry **sym_hash, |
936 | bfd **poldbfd, | |
37a9e49a | 937 | bfd_boolean *pold_weak, |
af44c138 | 938 | unsigned int *pold_alignment, |
268b6b39 AM |
939 | bfd_boolean *skip, |
940 | bfd_boolean *override, | |
941 | bfd_boolean *type_change_ok, | |
0f8a2703 | 942 | bfd_boolean *size_change_ok) |
252b5132 | 943 | { |
7479dfd4 | 944 | asection *sec, *oldsec; |
45d6a902 | 945 | struct elf_link_hash_entry *h; |
90c984fc | 946 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
947 | struct elf_link_hash_entry *flip; |
948 | int bind; | |
949 | bfd *oldbfd; | |
950 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 951 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 952 | const struct elf_backend_data *bed; |
45d6a902 AM |
953 | |
954 | *skip = FALSE; | |
955 | *override = FALSE; | |
956 | ||
957 | sec = *psec; | |
958 | bind = ELF_ST_BIND (sym->st_info); | |
959 | ||
960 | if (! bfd_is_und_section (sec)) | |
961 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
962 | else | |
963 | h = ((struct elf_link_hash_entry *) | |
964 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
965 | if (h == NULL) | |
966 | return FALSE; | |
967 | *sym_hash = h; | |
252b5132 | 968 | |
88ba32a0 L |
969 | bed = get_elf_backend_data (abfd); |
970 | ||
90c984fc L |
971 | /* For merging, we only care about real symbols. But we need to make |
972 | sure that indirect symbol dynamic flags are updated. */ | |
973 | hi = h; | |
45d6a902 AM |
974 | while (h->root.type == bfd_link_hash_indirect |
975 | || h->root.type == bfd_link_hash_warning) | |
976 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
977 | ||
934bce08 AM |
978 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
979 | existing symbol. */ | |
980 | ||
981 | oldbfd = NULL; | |
982 | oldsec = NULL; | |
983 | switch (h->root.type) | |
984 | { | |
985 | default: | |
986 | break; | |
987 | ||
988 | case bfd_link_hash_undefined: | |
989 | case bfd_link_hash_undefweak: | |
990 | oldbfd = h->root.u.undef.abfd; | |
991 | break; | |
992 | ||
993 | case bfd_link_hash_defined: | |
994 | case bfd_link_hash_defweak: | |
995 | oldbfd = h->root.u.def.section->owner; | |
996 | oldsec = h->root.u.def.section; | |
997 | break; | |
998 | ||
999 | case bfd_link_hash_common: | |
1000 | oldbfd = h->root.u.c.p->section->owner; | |
1001 | oldsec = h->root.u.c.p->section; | |
1002 | if (pold_alignment) | |
1003 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1004 | break; | |
1005 | } | |
1006 | if (poldbfd && *poldbfd == NULL) | |
1007 | *poldbfd = oldbfd; | |
1008 | ||
1009 | /* Differentiate strong and weak symbols. */ | |
1010 | newweak = bind == STB_WEAK; | |
1011 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1012 | || h->root.type == bfd_link_hash_undefweak); | |
1013 | if (pold_weak) | |
1014 | *pold_weak = oldweak; | |
1015 | ||
1016 | /* This code is for coping with dynamic objects, and is only useful | |
1017 | if we are doing an ELF link. */ | |
1018 | if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
1019 | return TRUE; | |
1020 | ||
40b36307 | 1021 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1022 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1023 | symbols. */ |
1024 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1025 | ||
ee659f1f AM |
1026 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1027 | respectively, is from a dynamic object. */ | |
1028 | ||
1029 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1030 | ||
1031 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1032 | syms and defined syms in dynamic libraries respectively. | |
1033 | ref_dynamic on the other hand can be set for a symbol defined in | |
1034 | a dynamic library, and def_dynamic may not be set; When the | |
1035 | definition in a dynamic lib is overridden by a definition in the | |
1036 | executable use of the symbol in the dynamic lib becomes a | |
1037 | reference to the executable symbol. */ | |
1038 | if (newdyn) | |
1039 | { | |
1040 | if (bfd_is_und_section (sec)) | |
1041 | { | |
1042 | if (bind != STB_WEAK) | |
1043 | { | |
1044 | h->ref_dynamic_nonweak = 1; | |
1045 | hi->ref_dynamic_nonweak = 1; | |
1046 | } | |
1047 | } | |
1048 | else | |
1049 | { | |
1050 | h->dynamic_def = 1; | |
1051 | hi->dynamic_def = 1; | |
1052 | } | |
1053 | } | |
1054 | ||
45d6a902 AM |
1055 | /* If we just created the symbol, mark it as being an ELF symbol. |
1056 | Other than that, there is nothing to do--there is no merge issue | |
1057 | with a newly defined symbol--so we just return. */ | |
1058 | ||
1059 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1060 | { |
f5385ebf | 1061 | h->non_elf = 0; |
45d6a902 AM |
1062 | return TRUE; |
1063 | } | |
252b5132 | 1064 | |
45d6a902 AM |
1065 | /* In cases involving weak versioned symbols, we may wind up trying |
1066 | to merge a symbol with itself. Catch that here, to avoid the | |
1067 | confusion that results if we try to override a symbol with | |
1068 | itself. The additional tests catch cases like | |
1069 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1070 | dynamic object, which we do want to handle here. */ | |
1071 | if (abfd == oldbfd | |
895fa45f | 1072 | && (newweak || oldweak) |
45d6a902 | 1073 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1074 | || !h->def_regular)) |
45d6a902 AM |
1075 | return TRUE; |
1076 | ||
707bba77 | 1077 | olddyn = FALSE; |
45d6a902 AM |
1078 | if (oldbfd != NULL) |
1079 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1080 | else if (oldsec != NULL) |
45d6a902 | 1081 | { |
707bba77 | 1082 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1083 | indices used by MIPS ELF. */ |
707bba77 | 1084 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1085 | } |
252b5132 | 1086 | |
45d6a902 AM |
1087 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1088 | respectively, appear to be a definition rather than reference. */ | |
1089 | ||
707bba77 | 1090 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1091 | |
707bba77 AM |
1092 | olddef = (h->root.type != bfd_link_hash_undefined |
1093 | && h->root.type != bfd_link_hash_undefweak | |
1094 | && h->root.type != bfd_link_hash_common); | |
45d6a902 | 1095 | |
0a36a439 L |
1096 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1097 | respectively, appear to be a function. */ | |
1098 | ||
1099 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1100 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1101 | ||
1102 | oldfunc = (h->type != STT_NOTYPE | |
1103 | && bed->is_function_type (h->type)); | |
1104 | ||
580a2b6e L |
1105 | /* When we try to create a default indirect symbol from the dynamic |
1106 | definition with the default version, we skip it if its type and | |
40101021 | 1107 | the type of existing regular definition mismatch. */ |
580a2b6e | 1108 | if (pold_alignment == NULL |
580a2b6e L |
1109 | && newdyn |
1110 | && newdef | |
1111 | && !olddyn | |
4584ec12 L |
1112 | && (((olddef || h->root.type == bfd_link_hash_common) |
1113 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1114 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1115 | && h->type != STT_NOTYPE | |
1116 | && !(newfunc && oldfunc)) | |
1117 | || (olddef | |
1118 | && ((h->type == STT_GNU_IFUNC) | |
1119 | != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))))) | |
580a2b6e L |
1120 | { |
1121 | *skip = TRUE; | |
1122 | return TRUE; | |
1123 | } | |
1124 | ||
4c34aff8 AM |
1125 | /* Check TLS symbols. We don't check undefined symbols introduced |
1126 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1127 | check symbols from plugins because they also have no type. */ | |
1128 | if (oldbfd != NULL | |
1129 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1130 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1131 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1132 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1133 | { |
1134 | bfd *ntbfd, *tbfd; | |
1135 | bfd_boolean ntdef, tdef; | |
1136 | asection *ntsec, *tsec; | |
1137 | ||
1138 | if (h->type == STT_TLS) | |
1139 | { | |
3b36f7e6 | 1140 | ntbfd = abfd; |
7479dfd4 L |
1141 | ntsec = sec; |
1142 | ntdef = newdef; | |
1143 | tbfd = oldbfd; | |
1144 | tsec = oldsec; | |
1145 | tdef = olddef; | |
1146 | } | |
1147 | else | |
1148 | { | |
1149 | ntbfd = oldbfd; | |
1150 | ntsec = oldsec; | |
1151 | ntdef = olddef; | |
1152 | tbfd = abfd; | |
1153 | tsec = sec; | |
1154 | tdef = newdef; | |
1155 | } | |
1156 | ||
1157 | if (tdef && ntdef) | |
1158 | (*_bfd_error_handler) | |
191c0c42 AM |
1159 | (_("%s: TLS definition in %B section %A " |
1160 | "mismatches non-TLS definition in %B section %A"), | |
7479dfd4 L |
1161 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); |
1162 | else if (!tdef && !ntdef) | |
1163 | (*_bfd_error_handler) | |
191c0c42 AM |
1164 | (_("%s: TLS reference in %B " |
1165 | "mismatches non-TLS reference in %B"), | |
7479dfd4 L |
1166 | tbfd, ntbfd, h->root.root.string); |
1167 | else if (tdef) | |
1168 | (*_bfd_error_handler) | |
191c0c42 AM |
1169 | (_("%s: TLS definition in %B section %A " |
1170 | "mismatches non-TLS reference in %B"), | |
7479dfd4 L |
1171 | tbfd, tsec, ntbfd, h->root.root.string); |
1172 | else | |
1173 | (*_bfd_error_handler) | |
191c0c42 AM |
1174 | (_("%s: TLS reference in %B " |
1175 | "mismatches non-TLS definition in %B section %A"), | |
7479dfd4 L |
1176 | tbfd, ntbfd, ntsec, h->root.root.string); |
1177 | ||
1178 | bfd_set_error (bfd_error_bad_value); | |
1179 | return FALSE; | |
1180 | } | |
1181 | ||
45d6a902 AM |
1182 | /* If the old symbol has non-default visibility, we ignore the new |
1183 | definition from a dynamic object. */ | |
1184 | if (newdyn | |
9c7a29a3 | 1185 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1186 | && !bfd_is_und_section (sec)) |
1187 | { | |
1188 | *skip = TRUE; | |
1189 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1190 | h->ref_dynamic = 1; |
90c984fc | 1191 | hi->ref_dynamic = 1; |
45d6a902 AM |
1192 | /* A protected symbol has external availability. Make sure it is |
1193 | recorded as dynamic. | |
1194 | ||
1195 | FIXME: Should we check type and size for protected symbol? */ | |
1196 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1197 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1198 | else |
1199 | return TRUE; | |
1200 | } | |
1201 | else if (!newdyn | |
9c7a29a3 | 1202 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1203 | && h->def_dynamic) |
45d6a902 AM |
1204 | { |
1205 | /* If the new symbol with non-default visibility comes from a | |
1206 | relocatable file and the old definition comes from a dynamic | |
1207 | object, we remove the old definition. */ | |
6c9b78e6 | 1208 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1209 | { |
1210 | /* Handle the case where the old dynamic definition is | |
1211 | default versioned. We need to copy the symbol info from | |
1212 | the symbol with default version to the normal one if it | |
1213 | was referenced before. */ | |
1214 | if (h->ref_regular) | |
1215 | { | |
6c9b78e6 | 1216 | hi->root.type = h->root.type; |
d2dee3b2 | 1217 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1218 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1219 | |
6c9b78e6 | 1220 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1221 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1222 | { |
aed81c4e MR |
1223 | /* If the new symbol is hidden or internal, completely undo |
1224 | any dynamic link state. */ | |
1225 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1226 | h->forced_local = 0; | |
1227 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1228 | } |
1229 | else | |
aed81c4e MR |
1230 | h->ref_dynamic = 1; |
1231 | ||
1232 | h->def_dynamic = 0; | |
aed81c4e MR |
1233 | /* FIXME: Should we check type and size for protected symbol? */ |
1234 | h->size = 0; | |
1235 | h->type = 0; | |
1236 | ||
6c9b78e6 | 1237 | h = hi; |
d2dee3b2 L |
1238 | } |
1239 | else | |
6c9b78e6 | 1240 | h = hi; |
d2dee3b2 | 1241 | } |
1de1a317 | 1242 | |
f5eda473 AM |
1243 | /* If the old symbol was undefined before, then it will still be |
1244 | on the undefs list. If the new symbol is undefined or | |
1245 | common, we can't make it bfd_link_hash_new here, because new | |
1246 | undefined or common symbols will be added to the undefs list | |
1247 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1248 | added twice to the undefs list. Also, if the new symbol is | |
1249 | undefweak then we don't want to lose the strong undef. */ | |
1250 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1251 | { |
1de1a317 | 1252 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1253 | h->root.u.undef.abfd = abfd; |
1254 | } | |
1255 | else | |
1256 | { | |
1257 | h->root.type = bfd_link_hash_new; | |
1258 | h->root.u.undef.abfd = NULL; | |
1259 | } | |
1260 | ||
f5eda473 | 1261 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1262 | { |
f5eda473 AM |
1263 | /* If the new symbol is hidden or internal, completely undo |
1264 | any dynamic link state. */ | |
1265 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1266 | h->forced_local = 0; | |
1267 | h->ref_dynamic = 0; | |
45d6a902 | 1268 | } |
f5eda473 AM |
1269 | else |
1270 | h->ref_dynamic = 1; | |
1271 | h->def_dynamic = 0; | |
45d6a902 AM |
1272 | /* FIXME: Should we check type and size for protected symbol? */ |
1273 | h->size = 0; | |
1274 | h->type = 0; | |
1275 | return TRUE; | |
1276 | } | |
14a793b2 | 1277 | |
15b43f48 AM |
1278 | /* If a new weak symbol definition comes from a regular file and the |
1279 | old symbol comes from a dynamic library, we treat the new one as | |
1280 | strong. Similarly, an old weak symbol definition from a regular | |
1281 | file is treated as strong when the new symbol comes from a dynamic | |
1282 | library. Further, an old weak symbol from a dynamic library is | |
1283 | treated as strong if the new symbol is from a dynamic library. | |
1284 | This reflects the way glibc's ld.so works. | |
1285 | ||
1286 | Do this before setting *type_change_ok or *size_change_ok so that | |
1287 | we warn properly when dynamic library symbols are overridden. */ | |
1288 | ||
1289 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1290 | newweak = FALSE; |
15b43f48 | 1291 | if (olddef && newdyn) |
0f8a2703 AM |
1292 | oldweak = FALSE; |
1293 | ||
d334575b | 1294 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1295 | if (newfunc && oldfunc) |
fcb93ecf PB |
1296 | *type_change_ok = TRUE; |
1297 | ||
79349b09 AM |
1298 | /* It's OK to change the type if either the existing symbol or the |
1299 | new symbol is weak. A type change is also OK if the old symbol | |
1300 | is undefined and the new symbol is defined. */ | |
252b5132 | 1301 | |
79349b09 AM |
1302 | if (oldweak |
1303 | || newweak | |
1304 | || (newdef | |
1305 | && h->root.type == bfd_link_hash_undefined)) | |
1306 | *type_change_ok = TRUE; | |
1307 | ||
1308 | /* It's OK to change the size if either the existing symbol or the | |
1309 | new symbol is weak, or if the old symbol is undefined. */ | |
1310 | ||
1311 | if (*type_change_ok | |
1312 | || h->root.type == bfd_link_hash_undefined) | |
1313 | *size_change_ok = TRUE; | |
45d6a902 | 1314 | |
45d6a902 AM |
1315 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1316 | symbol, respectively, appears to be a common symbol in a dynamic | |
1317 | object. If a symbol appears in an uninitialized section, and is | |
1318 | not weak, and is not a function, then it may be a common symbol | |
1319 | which was resolved when the dynamic object was created. We want | |
1320 | to treat such symbols specially, because they raise special | |
1321 | considerations when setting the symbol size: if the symbol | |
1322 | appears as a common symbol in a regular object, and the size in | |
1323 | the regular object is larger, we must make sure that we use the | |
1324 | larger size. This problematic case can always be avoided in C, | |
1325 | but it must be handled correctly when using Fortran shared | |
1326 | libraries. | |
1327 | ||
1328 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1329 | likewise for OLDDYNCOMMON and OLDDEF. | |
1330 | ||
1331 | Note that this test is just a heuristic, and that it is quite | |
1332 | possible to have an uninitialized symbol in a shared object which | |
1333 | is really a definition, rather than a common symbol. This could | |
1334 | lead to some minor confusion when the symbol really is a common | |
1335 | symbol in some regular object. However, I think it will be | |
1336 | harmless. */ | |
1337 | ||
1338 | if (newdyn | |
1339 | && newdef | |
79349b09 | 1340 | && !newweak |
45d6a902 AM |
1341 | && (sec->flags & SEC_ALLOC) != 0 |
1342 | && (sec->flags & SEC_LOAD) == 0 | |
1343 | && sym->st_size > 0 | |
0a36a439 | 1344 | && !newfunc) |
45d6a902 AM |
1345 | newdyncommon = TRUE; |
1346 | else | |
1347 | newdyncommon = FALSE; | |
1348 | ||
1349 | if (olddyn | |
1350 | && olddef | |
1351 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1352 | && h->def_dynamic |
45d6a902 AM |
1353 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1354 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1355 | && h->size > 0 | |
0a36a439 | 1356 | && !oldfunc) |
45d6a902 AM |
1357 | olddyncommon = TRUE; |
1358 | else | |
1359 | olddyncommon = FALSE; | |
1360 | ||
a4d8e49b L |
1361 | /* We now know everything about the old and new symbols. We ask the |
1362 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1363 | if (bed->merge_symbol != NULL) |
1364 | { | |
1365 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1366 | return FALSE; | |
1367 | sec = *psec; | |
1368 | } | |
a4d8e49b | 1369 | |
45d6a902 AM |
1370 | /* If both the old and the new symbols look like common symbols in a |
1371 | dynamic object, set the size of the symbol to the larger of the | |
1372 | two. */ | |
1373 | ||
1374 | if (olddyncommon | |
1375 | && newdyncommon | |
1376 | && sym->st_size != h->size) | |
1377 | { | |
1378 | /* Since we think we have two common symbols, issue a multiple | |
1379 | common warning if desired. Note that we only warn if the | |
1380 | size is different. If the size is the same, we simply let | |
1381 | the old symbol override the new one as normally happens with | |
1382 | symbols defined in dynamic objects. */ | |
1383 | ||
1384 | if (! ((*info->callbacks->multiple_common) | |
24f58f47 | 1385 | (info, &h->root, abfd, bfd_link_hash_common, sym->st_size))) |
45d6a902 | 1386 | return FALSE; |
252b5132 | 1387 | |
45d6a902 AM |
1388 | if (sym->st_size > h->size) |
1389 | h->size = sym->st_size; | |
252b5132 | 1390 | |
45d6a902 | 1391 | *size_change_ok = TRUE; |
252b5132 RH |
1392 | } |
1393 | ||
45d6a902 AM |
1394 | /* If we are looking at a dynamic object, and we have found a |
1395 | definition, we need to see if the symbol was already defined by | |
1396 | some other object. If so, we want to use the existing | |
1397 | definition, and we do not want to report a multiple symbol | |
1398 | definition error; we do this by clobbering *PSEC to be | |
1399 | bfd_und_section_ptr. | |
1400 | ||
1401 | We treat a common symbol as a definition if the symbol in the | |
1402 | shared library is a function, since common symbols always | |
1403 | represent variables; this can cause confusion in principle, but | |
1404 | any such confusion would seem to indicate an erroneous program or | |
1405 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1406 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1407 | |
1408 | if (newdyn | |
1409 | && newdef | |
77cfaee6 | 1410 | && (olddef |
45d6a902 | 1411 | || (h->root.type == bfd_link_hash_common |
0a36a439 | 1412 | && (newweak || newfunc)))) |
45d6a902 AM |
1413 | { |
1414 | *override = TRUE; | |
1415 | newdef = FALSE; | |
1416 | newdyncommon = FALSE; | |
252b5132 | 1417 | |
45d6a902 AM |
1418 | *psec = sec = bfd_und_section_ptr; |
1419 | *size_change_ok = TRUE; | |
252b5132 | 1420 | |
45d6a902 AM |
1421 | /* If we get here when the old symbol is a common symbol, then |
1422 | we are explicitly letting it override a weak symbol or | |
1423 | function in a dynamic object, and we don't want to warn about | |
1424 | a type change. If the old symbol is a defined symbol, a type | |
1425 | change warning may still be appropriate. */ | |
252b5132 | 1426 | |
45d6a902 AM |
1427 | if (h->root.type == bfd_link_hash_common) |
1428 | *type_change_ok = TRUE; | |
1429 | } | |
1430 | ||
1431 | /* Handle the special case of an old common symbol merging with a | |
1432 | new symbol which looks like a common symbol in a shared object. | |
1433 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1434 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1435 | right thing. */ | |
45d6a902 AM |
1436 | |
1437 | if (newdyncommon | |
1438 | && h->root.type == bfd_link_hash_common) | |
1439 | { | |
1440 | *override = TRUE; | |
1441 | newdef = FALSE; | |
1442 | newdyncommon = FALSE; | |
1443 | *pvalue = sym->st_size; | |
a4d8e49b | 1444 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1445 | *size_change_ok = TRUE; |
1446 | } | |
1447 | ||
c5e2cead | 1448 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1449 | if (newdef && olddef && newweak) |
54ac0771 | 1450 | { |
35ed3f94 | 1451 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1452 | if (!(oldbfd != NULL |
1453 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1454 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1455 | { |
1456 | newdef = FALSE; | |
1457 | *skip = TRUE; | |
1458 | } | |
54ac0771 L |
1459 | |
1460 | /* Merge st_other. If the symbol already has a dynamic index, | |
1461 | but visibility says it should not be visible, turn it into a | |
1462 | local symbol. */ | |
b8417128 | 1463 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1464 | if (h->dynindx != -1) |
1465 | switch (ELF_ST_VISIBILITY (h->other)) | |
1466 | { | |
1467 | case STV_INTERNAL: | |
1468 | case STV_HIDDEN: | |
1469 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1470 | break; | |
1471 | } | |
1472 | } | |
c5e2cead | 1473 | |
45d6a902 AM |
1474 | /* If the old symbol is from a dynamic object, and the new symbol is |
1475 | a definition which is not from a dynamic object, then the new | |
1476 | symbol overrides the old symbol. Symbols from regular files | |
1477 | always take precedence over symbols from dynamic objects, even if | |
1478 | they are defined after the dynamic object in the link. | |
1479 | ||
1480 | As above, we again permit a common symbol in a regular object to | |
1481 | override a definition in a shared object if the shared object | |
0f8a2703 | 1482 | symbol is a function or is weak. */ |
45d6a902 AM |
1483 | |
1484 | flip = NULL; | |
77cfaee6 | 1485 | if (!newdyn |
45d6a902 AM |
1486 | && (newdef |
1487 | || (bfd_is_com_section (sec) | |
0a36a439 | 1488 | && (oldweak || oldfunc))) |
45d6a902 AM |
1489 | && olddyn |
1490 | && olddef | |
f5385ebf | 1491 | && h->def_dynamic) |
45d6a902 AM |
1492 | { |
1493 | /* Change the hash table entry to undefined, and let | |
1494 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1495 | new definition. */ | |
1496 | ||
1497 | h->root.type = bfd_link_hash_undefined; | |
1498 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1499 | *size_change_ok = TRUE; | |
1500 | ||
1501 | olddef = FALSE; | |
1502 | olddyncommon = FALSE; | |
1503 | ||
1504 | /* We again permit a type change when a common symbol may be | |
1505 | overriding a function. */ | |
1506 | ||
1507 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1508 | { |
1509 | if (oldfunc) | |
1510 | { | |
1511 | /* If a common symbol overrides a function, make sure | |
1512 | that it isn't defined dynamically nor has type | |
1513 | function. */ | |
1514 | h->def_dynamic = 0; | |
1515 | h->type = STT_NOTYPE; | |
1516 | } | |
1517 | *type_change_ok = TRUE; | |
1518 | } | |
45d6a902 | 1519 | |
6c9b78e6 AM |
1520 | if (hi->root.type == bfd_link_hash_indirect) |
1521 | flip = hi; | |
45d6a902 AM |
1522 | else |
1523 | /* This union may have been set to be non-NULL when this symbol | |
1524 | was seen in a dynamic object. We must force the union to be | |
1525 | NULL, so that it is correct for a regular symbol. */ | |
1526 | h->verinfo.vertree = NULL; | |
1527 | } | |
1528 | ||
1529 | /* Handle the special case of a new common symbol merging with an | |
1530 | old symbol that looks like it might be a common symbol defined in | |
1531 | a shared object. Note that we have already handled the case in | |
1532 | which a new common symbol should simply override the definition | |
1533 | in the shared library. */ | |
1534 | ||
1535 | if (! newdyn | |
1536 | && bfd_is_com_section (sec) | |
1537 | && olddyncommon) | |
1538 | { | |
1539 | /* It would be best if we could set the hash table entry to a | |
1540 | common symbol, but we don't know what to use for the section | |
1541 | or the alignment. */ | |
1542 | if (! ((*info->callbacks->multiple_common) | |
24f58f47 | 1543 | (info, &h->root, abfd, bfd_link_hash_common, sym->st_size))) |
45d6a902 AM |
1544 | return FALSE; |
1545 | ||
4cc11e76 | 1546 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1547 | larger, pretend that the new symbol has its size. */ |
1548 | ||
1549 | if (h->size > *pvalue) | |
1550 | *pvalue = h->size; | |
1551 | ||
af44c138 L |
1552 | /* We need to remember the alignment required by the symbol |
1553 | in the dynamic object. */ | |
1554 | BFD_ASSERT (pold_alignment); | |
1555 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1556 | |
1557 | olddef = FALSE; | |
1558 | olddyncommon = FALSE; | |
1559 | ||
1560 | h->root.type = bfd_link_hash_undefined; | |
1561 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1562 | ||
1563 | *size_change_ok = TRUE; | |
1564 | *type_change_ok = TRUE; | |
1565 | ||
6c9b78e6 AM |
1566 | if (hi->root.type == bfd_link_hash_indirect) |
1567 | flip = hi; | |
45d6a902 AM |
1568 | else |
1569 | h->verinfo.vertree = NULL; | |
1570 | } | |
1571 | ||
1572 | if (flip != NULL) | |
1573 | { | |
1574 | /* Handle the case where we had a versioned symbol in a dynamic | |
1575 | library and now find a definition in a normal object. In this | |
1576 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1577 | flip->root.type = h->root.type; |
00cbee0a | 1578 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1579 | h->root.type = bfd_link_hash_indirect; |
1580 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1581 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1582 | if (h->def_dynamic) |
45d6a902 | 1583 | { |
f5385ebf AM |
1584 | h->def_dynamic = 0; |
1585 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1586 | } |
1587 | } | |
1588 | ||
45d6a902 AM |
1589 | return TRUE; |
1590 | } | |
1591 | ||
1592 | /* This function is called to create an indirect symbol from the | |
1593 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1594 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1595 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1596 | |
28caa186 | 1597 | static bfd_boolean |
268b6b39 AM |
1598 | _bfd_elf_add_default_symbol (bfd *abfd, |
1599 | struct bfd_link_info *info, | |
1600 | struct elf_link_hash_entry *h, | |
1601 | const char *name, | |
1602 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1603 | asection *sec, |
1604 | bfd_vma value, | |
1605 | bfd **poldbfd, | |
e3c9d234 | 1606 | bfd_boolean *dynsym) |
45d6a902 AM |
1607 | { |
1608 | bfd_boolean type_change_ok; | |
1609 | bfd_boolean size_change_ok; | |
1610 | bfd_boolean skip; | |
1611 | char *shortname; | |
1612 | struct elf_link_hash_entry *hi; | |
1613 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1614 | const struct elf_backend_data *bed; |
45d6a902 AM |
1615 | bfd_boolean collect; |
1616 | bfd_boolean dynamic; | |
e3c9d234 | 1617 | bfd_boolean override; |
45d6a902 AM |
1618 | char *p; |
1619 | size_t len, shortlen; | |
ffd65175 | 1620 | asection *tmp_sec; |
45d6a902 AM |
1621 | |
1622 | /* If this symbol has a version, and it is the default version, we | |
1623 | create an indirect symbol from the default name to the fully | |
1624 | decorated name. This will cause external references which do not | |
1625 | specify a version to be bound to this version of the symbol. */ | |
1626 | p = strchr (name, ELF_VER_CHR); | |
1627 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1628 | return TRUE; | |
1629 | ||
45d6a902 AM |
1630 | bed = get_elf_backend_data (abfd); |
1631 | collect = bed->collect; | |
1632 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1633 | ||
1634 | shortlen = p - name; | |
a50b1753 | 1635 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1636 | if (shortname == NULL) |
1637 | return FALSE; | |
1638 | memcpy (shortname, name, shortlen); | |
1639 | shortname[shortlen] = '\0'; | |
1640 | ||
1641 | /* We are going to create a new symbol. Merge it with any existing | |
1642 | symbol with this name. For the purposes of the merge, act as | |
1643 | though we were defining the symbol we just defined, although we | |
1644 | actually going to define an indirect symbol. */ | |
1645 | type_change_ok = FALSE; | |
1646 | size_change_ok = FALSE; | |
ffd65175 AM |
1647 | tmp_sec = sec; |
1648 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1649 | &hi, poldbfd, NULL, NULL, &skip, &override, |
af44c138 | 1650 | &type_change_ok, &size_change_ok)) |
45d6a902 AM |
1651 | return FALSE; |
1652 | ||
1653 | if (skip) | |
1654 | goto nondefault; | |
1655 | ||
1656 | if (! override) | |
1657 | { | |
1658 | bh = &hi->root; | |
1659 | if (! (_bfd_generic_link_add_one_symbol | |
1660 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1661 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1662 | return FALSE; |
1663 | hi = (struct elf_link_hash_entry *) bh; | |
1664 | } | |
1665 | else | |
1666 | { | |
1667 | /* In this case the symbol named SHORTNAME is overriding the | |
1668 | indirect symbol we want to add. We were planning on making | |
1669 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1670 | is the name without a version. NAME is the fully versioned | |
1671 | name, and it is the default version. | |
1672 | ||
1673 | Overriding means that we already saw a definition for the | |
1674 | symbol SHORTNAME in a regular object, and it is overriding | |
1675 | the symbol defined in the dynamic object. | |
1676 | ||
1677 | When this happens, we actually want to change NAME, the | |
1678 | symbol we just added, to refer to SHORTNAME. This will cause | |
1679 | references to NAME in the shared object to become references | |
1680 | to SHORTNAME in the regular object. This is what we expect | |
1681 | when we override a function in a shared object: that the | |
1682 | references in the shared object will be mapped to the | |
1683 | definition in the regular object. */ | |
1684 | ||
1685 | while (hi->root.type == bfd_link_hash_indirect | |
1686 | || hi->root.type == bfd_link_hash_warning) | |
1687 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1688 | ||
1689 | h->root.type = bfd_link_hash_indirect; | |
1690 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1691 | if (h->def_dynamic) |
45d6a902 | 1692 | { |
f5385ebf AM |
1693 | h->def_dynamic = 0; |
1694 | hi->ref_dynamic = 1; | |
1695 | if (hi->ref_regular | |
1696 | || hi->def_regular) | |
45d6a902 | 1697 | { |
c152c796 | 1698 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1699 | return FALSE; |
1700 | } | |
1701 | } | |
1702 | ||
1703 | /* Now set HI to H, so that the following code will set the | |
1704 | other fields correctly. */ | |
1705 | hi = h; | |
1706 | } | |
1707 | ||
fab4a87f L |
1708 | /* Check if HI is a warning symbol. */ |
1709 | if (hi->root.type == bfd_link_hash_warning) | |
1710 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1711 | ||
45d6a902 AM |
1712 | /* If there is a duplicate definition somewhere, then HI may not |
1713 | point to an indirect symbol. We will have reported an error to | |
1714 | the user in that case. */ | |
1715 | ||
1716 | if (hi->root.type == bfd_link_hash_indirect) | |
1717 | { | |
1718 | struct elf_link_hash_entry *ht; | |
1719 | ||
45d6a902 | 1720 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 1721 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 1722 | |
68c88cd4 AM |
1723 | /* A reference to the SHORTNAME symbol from a dynamic library |
1724 | will be satisfied by the versioned symbol at runtime. In | |
1725 | effect, we have a reference to the versioned symbol. */ | |
1726 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
1727 | hi->dynamic_def |= ht->dynamic_def; | |
1728 | ||
45d6a902 AM |
1729 | /* See if the new flags lead us to realize that the symbol must |
1730 | be dynamic. */ | |
1731 | if (! *dynsym) | |
1732 | { | |
1733 | if (! dynamic) | |
1734 | { | |
ca4a656b | 1735 | if (! info->executable |
90c984fc | 1736 | || hi->def_dynamic |
f5385ebf | 1737 | || hi->ref_dynamic) |
45d6a902 AM |
1738 | *dynsym = TRUE; |
1739 | } | |
1740 | else | |
1741 | { | |
f5385ebf | 1742 | if (hi->ref_regular) |
45d6a902 AM |
1743 | *dynsym = TRUE; |
1744 | } | |
1745 | } | |
1746 | } | |
1747 | ||
1748 | /* We also need to define an indirection from the nondefault version | |
1749 | of the symbol. */ | |
1750 | ||
1751 | nondefault: | |
1752 | len = strlen (name); | |
a50b1753 | 1753 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
1754 | if (shortname == NULL) |
1755 | return FALSE; | |
1756 | memcpy (shortname, name, shortlen); | |
1757 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1758 | ||
1759 | /* Once again, merge with any existing symbol. */ | |
1760 | type_change_ok = FALSE; | |
1761 | size_change_ok = FALSE; | |
ffd65175 AM |
1762 | tmp_sec = sec; |
1763 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 1764 | &hi, poldbfd, NULL, NULL, &skip, &override, |
af44c138 | 1765 | &type_change_ok, &size_change_ok)) |
45d6a902 AM |
1766 | return FALSE; |
1767 | ||
1768 | if (skip) | |
1769 | return TRUE; | |
1770 | ||
1771 | if (override) | |
1772 | { | |
1773 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1774 | the type of override we do in the case above unless it is | |
4cc11e76 | 1775 | overridden by a versioned definition. */ |
45d6a902 AM |
1776 | if (hi->root.type != bfd_link_hash_defined |
1777 | && hi->root.type != bfd_link_hash_defweak) | |
1778 | (*_bfd_error_handler) | |
d003868e AM |
1779 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1780 | abfd, shortname); | |
45d6a902 AM |
1781 | } |
1782 | else | |
1783 | { | |
1784 | bh = &hi->root; | |
1785 | if (! (_bfd_generic_link_add_one_symbol | |
1786 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1787 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1788 | return FALSE; |
1789 | hi = (struct elf_link_hash_entry *) bh; | |
1790 | ||
1791 | /* If there is a duplicate definition somewhere, then HI may not | |
1792 | point to an indirect symbol. We will have reported an error | |
1793 | to the user in that case. */ | |
1794 | ||
1795 | if (hi->root.type == bfd_link_hash_indirect) | |
1796 | { | |
fcfa13d2 | 1797 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
1798 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
1799 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
1800 | |
1801 | /* See if the new flags lead us to realize that the symbol | |
1802 | must be dynamic. */ | |
1803 | if (! *dynsym) | |
1804 | { | |
1805 | if (! dynamic) | |
1806 | { | |
ca4a656b | 1807 | if (! info->executable |
f5385ebf | 1808 | || hi->ref_dynamic) |
45d6a902 AM |
1809 | *dynsym = TRUE; |
1810 | } | |
1811 | else | |
1812 | { | |
f5385ebf | 1813 | if (hi->ref_regular) |
45d6a902 AM |
1814 | *dynsym = TRUE; |
1815 | } | |
1816 | } | |
1817 | } | |
1818 | } | |
1819 | ||
1820 | return TRUE; | |
1821 | } | |
1822 | \f | |
1823 | /* This routine is used to export all defined symbols into the dynamic | |
1824 | symbol table. It is called via elf_link_hash_traverse. */ | |
1825 | ||
28caa186 | 1826 | static bfd_boolean |
268b6b39 | 1827 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1828 | { |
a50b1753 | 1829 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
1830 | |
1831 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1832 | if (h->root.type == bfd_link_hash_indirect) | |
1833 | return TRUE; | |
1834 | ||
7686d77d AM |
1835 | /* Ignore this if we won't export it. */ |
1836 | if (!eif->info->export_dynamic && !h->dynamic) | |
1837 | return TRUE; | |
45d6a902 AM |
1838 | |
1839 | if (h->dynindx == -1 | |
fd91d419 L |
1840 | && (h->def_regular || h->ref_regular) |
1841 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
1842 | h->root.root.string)) | |
45d6a902 | 1843 | { |
fd91d419 | 1844 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 1845 | { |
fd91d419 L |
1846 | eif->failed = TRUE; |
1847 | return FALSE; | |
45d6a902 AM |
1848 | } |
1849 | } | |
1850 | ||
1851 | return TRUE; | |
1852 | } | |
1853 | \f | |
1854 | /* Look through the symbols which are defined in other shared | |
1855 | libraries and referenced here. Update the list of version | |
1856 | dependencies. This will be put into the .gnu.version_r section. | |
1857 | This function is called via elf_link_hash_traverse. */ | |
1858 | ||
28caa186 | 1859 | static bfd_boolean |
268b6b39 AM |
1860 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1861 | void *data) | |
45d6a902 | 1862 | { |
a50b1753 | 1863 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
1864 | Elf_Internal_Verneed *t; |
1865 | Elf_Internal_Vernaux *a; | |
1866 | bfd_size_type amt; | |
1867 | ||
45d6a902 AM |
1868 | /* We only care about symbols defined in shared objects with version |
1869 | information. */ | |
f5385ebf AM |
1870 | if (!h->def_dynamic |
1871 | || h->def_regular | |
45d6a902 | 1872 | || h->dynindx == -1 |
7b20f099 AM |
1873 | || h->verinfo.verdef == NULL |
1874 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
1875 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
1876 | return TRUE; |
1877 | ||
1878 | /* See if we already know about this version. */ | |
28caa186 AM |
1879 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
1880 | t != NULL; | |
1881 | t = t->vn_nextref) | |
45d6a902 AM |
1882 | { |
1883 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1884 | continue; | |
1885 | ||
1886 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1887 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1888 | return TRUE; | |
1889 | ||
1890 | break; | |
1891 | } | |
1892 | ||
1893 | /* This is a new version. Add it to tree we are building. */ | |
1894 | ||
1895 | if (t == NULL) | |
1896 | { | |
1897 | amt = sizeof *t; | |
a50b1753 | 1898 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
1899 | if (t == NULL) |
1900 | { | |
1901 | rinfo->failed = TRUE; | |
1902 | return FALSE; | |
1903 | } | |
1904 | ||
1905 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
1906 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
1907 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
1908 | } |
1909 | ||
1910 | amt = sizeof *a; | |
a50b1753 | 1911 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
1912 | if (a == NULL) |
1913 | { | |
1914 | rinfo->failed = TRUE; | |
1915 | return FALSE; | |
1916 | } | |
45d6a902 AM |
1917 | |
1918 | /* Note that we are copying a string pointer here, and testing it | |
1919 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1920 | discard the string data when low in memory, this will have to be | |
1921 | fixed. */ | |
1922 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1923 | ||
1924 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1925 | a->vna_nextptr = t->vn_auxptr; | |
1926 | ||
1927 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1928 | ++rinfo->vers; | |
1929 | ||
1930 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1931 | ||
1932 | t->vn_auxptr = a; | |
1933 | ||
1934 | return TRUE; | |
1935 | } | |
1936 | ||
1937 | /* Figure out appropriate versions for all the symbols. We may not | |
1938 | have the version number script until we have read all of the input | |
1939 | files, so until that point we don't know which symbols should be | |
1940 | local. This function is called via elf_link_hash_traverse. */ | |
1941 | ||
28caa186 | 1942 | static bfd_boolean |
268b6b39 | 1943 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1944 | { |
28caa186 | 1945 | struct elf_info_failed *sinfo; |
45d6a902 | 1946 | struct bfd_link_info *info; |
9c5bfbb7 | 1947 | const struct elf_backend_data *bed; |
45d6a902 AM |
1948 | struct elf_info_failed eif; |
1949 | char *p; | |
1950 | bfd_size_type amt; | |
1951 | ||
a50b1753 | 1952 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
1953 | info = sinfo->info; |
1954 | ||
45d6a902 AM |
1955 | /* Fix the symbol flags. */ |
1956 | eif.failed = FALSE; | |
1957 | eif.info = info; | |
1958 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1959 | { | |
1960 | if (eif.failed) | |
1961 | sinfo->failed = TRUE; | |
1962 | return FALSE; | |
1963 | } | |
1964 | ||
1965 | /* We only need version numbers for symbols defined in regular | |
1966 | objects. */ | |
f5385ebf | 1967 | if (!h->def_regular) |
45d6a902 AM |
1968 | return TRUE; |
1969 | ||
28caa186 | 1970 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
1971 | p = strchr (h->root.root.string, ELF_VER_CHR); |
1972 | if (p != NULL && h->verinfo.vertree == NULL) | |
1973 | { | |
1974 | struct bfd_elf_version_tree *t; | |
1975 | bfd_boolean hidden; | |
1976 | ||
1977 | hidden = TRUE; | |
1978 | ||
1979 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1980 | not a hidden symbol. */ | |
1981 | ++p; | |
1982 | if (*p == ELF_VER_CHR) | |
1983 | { | |
1984 | hidden = FALSE; | |
1985 | ++p; | |
1986 | } | |
1987 | ||
1988 | /* If there is no version string, we can just return out. */ | |
1989 | if (*p == '\0') | |
1990 | { | |
1991 | if (hidden) | |
f5385ebf | 1992 | h->hidden = 1; |
45d6a902 AM |
1993 | return TRUE; |
1994 | } | |
1995 | ||
1996 | /* Look for the version. If we find it, it is no longer weak. */ | |
fd91d419 | 1997 | for (t = sinfo->info->version_info; t != NULL; t = t->next) |
45d6a902 AM |
1998 | { |
1999 | if (strcmp (t->name, p) == 0) | |
2000 | { | |
2001 | size_t len; | |
2002 | char *alc; | |
2003 | struct bfd_elf_version_expr *d; | |
2004 | ||
2005 | len = p - h->root.root.string; | |
a50b1753 | 2006 | alc = (char *) bfd_malloc (len); |
45d6a902 | 2007 | if (alc == NULL) |
14b1c01e AM |
2008 | { |
2009 | sinfo->failed = TRUE; | |
2010 | return FALSE; | |
2011 | } | |
45d6a902 AM |
2012 | memcpy (alc, h->root.root.string, len - 1); |
2013 | alc[len - 1] = '\0'; | |
2014 | if (alc[len - 2] == ELF_VER_CHR) | |
2015 | alc[len - 2] = '\0'; | |
2016 | ||
2017 | h->verinfo.vertree = t; | |
2018 | t->used = TRUE; | |
2019 | d = NULL; | |
2020 | ||
108ba305 JJ |
2021 | if (t->globals.list != NULL) |
2022 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
2023 | |
2024 | /* See if there is anything to force this symbol to | |
2025 | local scope. */ | |
108ba305 | 2026 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 2027 | { |
108ba305 JJ |
2028 | d = (*t->match) (&t->locals, NULL, alc); |
2029 | if (d != NULL | |
2030 | && h->dynindx != -1 | |
108ba305 JJ |
2031 | && ! info->export_dynamic) |
2032 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2033 | } |
2034 | ||
2035 | free (alc); | |
2036 | break; | |
2037 | } | |
2038 | } | |
2039 | ||
2040 | /* If we are building an application, we need to create a | |
2041 | version node for this version. */ | |
36af4a4e | 2042 | if (t == NULL && info->executable) |
45d6a902 AM |
2043 | { |
2044 | struct bfd_elf_version_tree **pp; | |
2045 | int version_index; | |
2046 | ||
2047 | /* If we aren't going to export this symbol, we don't need | |
2048 | to worry about it. */ | |
2049 | if (h->dynindx == -1) | |
2050 | return TRUE; | |
2051 | ||
2052 | amt = sizeof *t; | |
a50b1753 | 2053 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt); |
45d6a902 AM |
2054 | if (t == NULL) |
2055 | { | |
2056 | sinfo->failed = TRUE; | |
2057 | return FALSE; | |
2058 | } | |
2059 | ||
45d6a902 | 2060 | t->name = p; |
45d6a902 AM |
2061 | t->name_indx = (unsigned int) -1; |
2062 | t->used = TRUE; | |
2063 | ||
2064 | version_index = 1; | |
2065 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2066 | if (sinfo->info->version_info != NULL |
2067 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2068 | version_index = 0; |
fd91d419 L |
2069 | for (pp = &sinfo->info->version_info; |
2070 | *pp != NULL; | |
2071 | pp = &(*pp)->next) | |
45d6a902 AM |
2072 | ++version_index; |
2073 | t->vernum = version_index; | |
2074 | ||
2075 | *pp = t; | |
2076 | ||
2077 | h->verinfo.vertree = t; | |
2078 | } | |
2079 | else if (t == NULL) | |
2080 | { | |
2081 | /* We could not find the version for a symbol when | |
2082 | generating a shared archive. Return an error. */ | |
2083 | (*_bfd_error_handler) | |
c55fe096 | 2084 | (_("%B: version node not found for symbol %s"), |
28caa186 | 2085 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2086 | bfd_set_error (bfd_error_bad_value); |
2087 | sinfo->failed = TRUE; | |
2088 | return FALSE; | |
2089 | } | |
2090 | ||
2091 | if (hidden) | |
f5385ebf | 2092 | h->hidden = 1; |
45d6a902 AM |
2093 | } |
2094 | ||
2095 | /* If we don't have a version for this symbol, see if we can find | |
2096 | something. */ | |
fd91d419 | 2097 | if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) |
45d6a902 | 2098 | { |
1e8fa21e | 2099 | bfd_boolean hide; |
ae5a3597 | 2100 | |
fd91d419 L |
2101 | h->verinfo.vertree |
2102 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2103 | h->root.root.string, &hide); | |
1e8fa21e AM |
2104 | if (h->verinfo.vertree != NULL && hide) |
2105 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2106 | } |
2107 | ||
2108 | return TRUE; | |
2109 | } | |
2110 | \f | |
45d6a902 AM |
2111 | /* Read and swap the relocs from the section indicated by SHDR. This |
2112 | may be either a REL or a RELA section. The relocations are | |
2113 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2114 | which should have already been allocated to contain enough space. | |
2115 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2116 | relocations should be stored. | |
2117 | ||
2118 | Returns FALSE if something goes wrong. */ | |
2119 | ||
2120 | static bfd_boolean | |
268b6b39 | 2121 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2122 | asection *sec, |
268b6b39 AM |
2123 | Elf_Internal_Shdr *shdr, |
2124 | void *external_relocs, | |
2125 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2126 | { |
9c5bfbb7 | 2127 | const struct elf_backend_data *bed; |
268b6b39 | 2128 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2129 | const bfd_byte *erela; |
2130 | const bfd_byte *erelaend; | |
2131 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2132 | Elf_Internal_Shdr *symtab_hdr; |
2133 | size_t nsyms; | |
45d6a902 | 2134 | |
45d6a902 AM |
2135 | /* Position ourselves at the start of the section. */ |
2136 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2137 | return FALSE; | |
2138 | ||
2139 | /* Read the relocations. */ | |
2140 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2141 | return FALSE; | |
2142 | ||
243ef1e0 | 2143 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2144 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2145 | |
45d6a902 AM |
2146 | bed = get_elf_backend_data (abfd); |
2147 | ||
2148 | /* Convert the external relocations to the internal format. */ | |
2149 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2150 | swap_in = bed->s->swap_reloc_in; | |
2151 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2152 | swap_in = bed->s->swap_reloca_in; | |
2153 | else | |
2154 | { | |
2155 | bfd_set_error (bfd_error_wrong_format); | |
2156 | return FALSE; | |
2157 | } | |
2158 | ||
a50b1753 | 2159 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2160 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2161 | irela = internal_relocs; |
2162 | while (erela < erelaend) | |
2163 | { | |
243ef1e0 L |
2164 | bfd_vma r_symndx; |
2165 | ||
45d6a902 | 2166 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2167 | r_symndx = ELF32_R_SYM (irela->r_info); |
2168 | if (bed->s->arch_size == 64) | |
2169 | r_symndx >>= 24; | |
ce98a316 NC |
2170 | if (nsyms > 0) |
2171 | { | |
2172 | if ((size_t) r_symndx >= nsyms) | |
2173 | { | |
2174 | (*_bfd_error_handler) | |
2175 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" | |
2176 | " for offset 0x%lx in section `%A'"), | |
2177 | abfd, sec, | |
2178 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
2179 | bfd_set_error (bfd_error_bad_value); | |
2180 | return FALSE; | |
2181 | } | |
2182 | } | |
cf35638d | 2183 | else if (r_symndx != STN_UNDEF) |
243ef1e0 L |
2184 | { |
2185 | (*_bfd_error_handler) | |
ce98a316 NC |
2186 | (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'" |
2187 | " when the object file has no symbol table"), | |
d003868e AM |
2188 | abfd, sec, |
2189 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
2190 | bfd_set_error (bfd_error_bad_value); |
2191 | return FALSE; | |
2192 | } | |
45d6a902 AM |
2193 | irela += bed->s->int_rels_per_ext_rel; |
2194 | erela += shdr->sh_entsize; | |
2195 | } | |
2196 | ||
2197 | return TRUE; | |
2198 | } | |
2199 | ||
2200 | /* Read and swap the relocs for a section O. They may have been | |
2201 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2202 | not NULL, they are used as buffers to read into. They are known to | |
2203 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2204 | the return value is allocated using either malloc or bfd_alloc, | |
2205 | according to the KEEP_MEMORY argument. If O has two relocation | |
2206 | sections (both REL and RELA relocations), then the REL_HDR | |
2207 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2208 | RELA_HDR relocations. */ |
45d6a902 AM |
2209 | |
2210 | Elf_Internal_Rela * | |
268b6b39 AM |
2211 | _bfd_elf_link_read_relocs (bfd *abfd, |
2212 | asection *o, | |
2213 | void *external_relocs, | |
2214 | Elf_Internal_Rela *internal_relocs, | |
2215 | bfd_boolean keep_memory) | |
45d6a902 | 2216 | { |
268b6b39 | 2217 | void *alloc1 = NULL; |
45d6a902 | 2218 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2219 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2220 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2221 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2222 | |
d4730f92 BS |
2223 | if (esdo->relocs != NULL) |
2224 | return esdo->relocs; | |
45d6a902 AM |
2225 | |
2226 | if (o->reloc_count == 0) | |
2227 | return NULL; | |
2228 | ||
45d6a902 AM |
2229 | if (internal_relocs == NULL) |
2230 | { | |
2231 | bfd_size_type size; | |
2232 | ||
2233 | size = o->reloc_count; | |
2234 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
2235 | if (keep_memory) | |
a50b1753 | 2236 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2237 | else |
a50b1753 | 2238 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2239 | if (internal_relocs == NULL) |
2240 | goto error_return; | |
2241 | } | |
2242 | ||
2243 | if (external_relocs == NULL) | |
2244 | { | |
d4730f92 BS |
2245 | bfd_size_type size = 0; |
2246 | ||
2247 | if (esdo->rel.hdr) | |
2248 | size += esdo->rel.hdr->sh_size; | |
2249 | if (esdo->rela.hdr) | |
2250 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2251 | |
268b6b39 | 2252 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2253 | if (alloc1 == NULL) |
2254 | goto error_return; | |
2255 | external_relocs = alloc1; | |
2256 | } | |
2257 | ||
d4730f92 BS |
2258 | internal_rela_relocs = internal_relocs; |
2259 | if (esdo->rel.hdr) | |
2260 | { | |
2261 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2262 | external_relocs, | |
2263 | internal_relocs)) | |
2264 | goto error_return; | |
2265 | external_relocs = (((bfd_byte *) external_relocs) | |
2266 | + esdo->rel.hdr->sh_size); | |
2267 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2268 | * bed->s->int_rels_per_ext_rel); | |
2269 | } | |
2270 | ||
2271 | if (esdo->rela.hdr | |
2272 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2273 | external_relocs, | |
2274 | internal_rela_relocs))) | |
45d6a902 AM |
2275 | goto error_return; |
2276 | ||
2277 | /* Cache the results for next time, if we can. */ | |
2278 | if (keep_memory) | |
d4730f92 | 2279 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2280 | |
2281 | if (alloc1 != NULL) | |
2282 | free (alloc1); | |
2283 | ||
2284 | /* Don't free alloc2, since if it was allocated we are passing it | |
2285 | back (under the name of internal_relocs). */ | |
2286 | ||
2287 | return internal_relocs; | |
2288 | ||
2289 | error_return: | |
2290 | if (alloc1 != NULL) | |
2291 | free (alloc1); | |
2292 | if (alloc2 != NULL) | |
4dd07732 AM |
2293 | { |
2294 | if (keep_memory) | |
2295 | bfd_release (abfd, alloc2); | |
2296 | else | |
2297 | free (alloc2); | |
2298 | } | |
45d6a902 AM |
2299 | return NULL; |
2300 | } | |
2301 | ||
2302 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2303 | section header for a section containing relocations for O. */ | |
2304 | ||
28caa186 | 2305 | static bfd_boolean |
268b6b39 | 2306 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
d4730f92 | 2307 | struct bfd_elf_section_reloc_data *reldata) |
45d6a902 | 2308 | { |
d4730f92 | 2309 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2310 | |
2311 | /* That allows us to calculate the size of the section. */ | |
d4730f92 | 2312 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2313 | |
2314 | /* The contents field must last into write_object_contents, so we | |
2315 | allocate it with bfd_alloc rather than malloc. Also since we | |
2316 | cannot be sure that the contents will actually be filled in, | |
2317 | we zero the allocated space. */ | |
a50b1753 | 2318 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2319 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2320 | return FALSE; | |
2321 | ||
d4730f92 | 2322 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2323 | { |
2324 | struct elf_link_hash_entry **p; | |
2325 | ||
ca4be51c AM |
2326 | p = ((struct elf_link_hash_entry **) |
2327 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2328 | if (p == NULL) |
2329 | return FALSE; | |
2330 | ||
d4730f92 | 2331 | reldata->hashes = p; |
45d6a902 AM |
2332 | } |
2333 | ||
2334 | return TRUE; | |
2335 | } | |
2336 | ||
2337 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2338 | originated from the section given by INPUT_REL_HDR) to the | |
2339 | OUTPUT_BFD. */ | |
2340 | ||
2341 | bfd_boolean | |
268b6b39 AM |
2342 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2343 | asection *input_section, | |
2344 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2345 | Elf_Internal_Rela *internal_relocs, |
2346 | struct elf_link_hash_entry **rel_hash | |
2347 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2348 | { |
2349 | Elf_Internal_Rela *irela; | |
2350 | Elf_Internal_Rela *irelaend; | |
2351 | bfd_byte *erel; | |
d4730f92 | 2352 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2353 | asection *output_section; |
9c5bfbb7 | 2354 | const struct elf_backend_data *bed; |
268b6b39 | 2355 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2356 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2357 | |
2358 | output_section = input_section->output_section; | |
45d6a902 | 2359 | |
d4730f92 BS |
2360 | bed = get_elf_backend_data (output_bfd); |
2361 | esdo = elf_section_data (output_section); | |
2362 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2363 | { |
d4730f92 BS |
2364 | output_reldata = &esdo->rel; |
2365 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2366 | } |
d4730f92 BS |
2367 | else if (esdo->rela.hdr |
2368 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2369 | { |
d4730f92 BS |
2370 | output_reldata = &esdo->rela; |
2371 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2372 | } |
2373 | else | |
2374 | { | |
2375 | (*_bfd_error_handler) | |
d003868e AM |
2376 | (_("%B: relocation size mismatch in %B section %A"), |
2377 | output_bfd, input_section->owner, input_section); | |
297d8443 | 2378 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2379 | return FALSE; |
2380 | } | |
2381 | ||
d4730f92 BS |
2382 | erel = output_reldata->hdr->contents; |
2383 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2384 | irela = internal_relocs; |
2385 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2386 | * bed->s->int_rels_per_ext_rel); | |
2387 | while (irela < irelaend) | |
2388 | { | |
2389 | (*swap_out) (output_bfd, irela, erel); | |
2390 | irela += bed->s->int_rels_per_ext_rel; | |
2391 | erel += input_rel_hdr->sh_entsize; | |
2392 | } | |
2393 | ||
2394 | /* Bump the counter, so that we know where to add the next set of | |
2395 | relocations. */ | |
d4730f92 | 2396 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2397 | |
2398 | return TRUE; | |
2399 | } | |
2400 | \f | |
508c3946 L |
2401 | /* Make weak undefined symbols in PIE dynamic. */ |
2402 | ||
2403 | bfd_boolean | |
2404 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2405 | struct elf_link_hash_entry *h) | |
2406 | { | |
2407 | if (info->pie | |
2408 | && h->dynindx == -1 | |
2409 | && h->root.type == bfd_link_hash_undefweak) | |
2410 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2411 | ||
2412 | return TRUE; | |
2413 | } | |
2414 | ||
45d6a902 AM |
2415 | /* Fix up the flags for a symbol. This handles various cases which |
2416 | can only be fixed after all the input files are seen. This is | |
2417 | currently called by both adjust_dynamic_symbol and | |
2418 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2419 | the face of future changes. */ | |
2420 | ||
28caa186 | 2421 | static bfd_boolean |
268b6b39 AM |
2422 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2423 | struct elf_info_failed *eif) | |
45d6a902 | 2424 | { |
33774f08 | 2425 | const struct elf_backend_data *bed; |
508c3946 | 2426 | |
45d6a902 AM |
2427 | /* If this symbol was mentioned in a non-ELF file, try to set |
2428 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2429 | permit a non-ELF file to correctly refer to a symbol defined in | |
2430 | an ELF dynamic object. */ | |
f5385ebf | 2431 | if (h->non_elf) |
45d6a902 AM |
2432 | { |
2433 | while (h->root.type == bfd_link_hash_indirect) | |
2434 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2435 | ||
2436 | if (h->root.type != bfd_link_hash_defined | |
2437 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2438 | { |
2439 | h->ref_regular = 1; | |
2440 | h->ref_regular_nonweak = 1; | |
2441 | } | |
45d6a902 AM |
2442 | else |
2443 | { | |
2444 | if (h->root.u.def.section->owner != NULL | |
2445 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2446 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2447 | { |
2448 | h->ref_regular = 1; | |
2449 | h->ref_regular_nonweak = 1; | |
2450 | } | |
45d6a902 | 2451 | else |
f5385ebf | 2452 | h->def_regular = 1; |
45d6a902 AM |
2453 | } |
2454 | ||
2455 | if (h->dynindx == -1 | |
f5385ebf AM |
2456 | && (h->def_dynamic |
2457 | || h->ref_dynamic)) | |
45d6a902 | 2458 | { |
c152c796 | 2459 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2460 | { |
2461 | eif->failed = TRUE; | |
2462 | return FALSE; | |
2463 | } | |
2464 | } | |
2465 | } | |
2466 | else | |
2467 | { | |
f5385ebf | 2468 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2469 | was first seen in a non-ELF file. Fortunately, if the symbol |
2470 | was first seen in an ELF file, we're probably OK unless the | |
2471 | symbol was defined in a non-ELF file. Catch that case here. | |
2472 | FIXME: We're still in trouble if the symbol was first seen in | |
2473 | a dynamic object, and then later in a non-ELF regular object. */ | |
2474 | if ((h->root.type == bfd_link_hash_defined | |
2475 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2476 | && !h->def_regular |
45d6a902 AM |
2477 | && (h->root.u.def.section->owner != NULL |
2478 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2479 | != bfd_target_elf_flavour) | |
2480 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2481 | && !h->def_dynamic))) |
2482 | h->def_regular = 1; | |
45d6a902 AM |
2483 | } |
2484 | ||
508c3946 | 2485 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2486 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2487 | if (bed->elf_backend_fixup_symbol | |
2488 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2489 | return FALSE; | |
508c3946 | 2490 | |
45d6a902 AM |
2491 | /* If this is a final link, and the symbol was defined as a common |
2492 | symbol in a regular object file, and there was no definition in | |
2493 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2494 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2495 | flag will not have been set. */ |
2496 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2497 | && !h->def_regular |
2498 | && h->ref_regular | |
2499 | && !h->def_dynamic | |
96f29d96 | 2500 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2501 | h->def_regular = 1; |
45d6a902 AM |
2502 | |
2503 | /* If -Bsymbolic was used (which means to bind references to global | |
2504 | symbols to the definition within the shared object), and this | |
2505 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2506 | need a PLT entry. Likewise, if the symbol has non-default |
2507 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2508 | will force it local. */ |
f5385ebf | 2509 | if (h->needs_plt |
45d6a902 | 2510 | && eif->info->shared |
0eddce27 | 2511 | && is_elf_hash_table (eif->info->hash) |
55255dae | 2512 | && (SYMBOLIC_BIND (eif->info, h) |
c1be741f | 2513 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2514 | && h->def_regular) |
45d6a902 | 2515 | { |
45d6a902 AM |
2516 | bfd_boolean force_local; |
2517 | ||
45d6a902 AM |
2518 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2519 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2520 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2521 | } | |
2522 | ||
2523 | /* If a weak undefined symbol has non-default visibility, we also | |
2524 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2525 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 | 2526 | && h->root.type == bfd_link_hash_undefweak) |
33774f08 | 2527 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
45d6a902 AM |
2528 | |
2529 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2530 | the real definition in the dynamic object, copy interesting flags | |
2531 | over to the real definition. */ | |
f6e332e6 | 2532 | if (h->u.weakdef != NULL) |
45d6a902 | 2533 | { |
45d6a902 AM |
2534 | /* If the real definition is defined by a regular object file, |
2535 | don't do anything special. See the longer description in | |
2536 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
4e6b54a6 | 2537 | if (h->u.weakdef->def_regular) |
f6e332e6 | 2538 | h->u.weakdef = NULL; |
45d6a902 | 2539 | else |
a26587ba | 2540 | { |
4e6b54a6 AM |
2541 | struct elf_link_hash_entry *weakdef = h->u.weakdef; |
2542 | ||
2543 | while (h->root.type == bfd_link_hash_indirect) | |
2544 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2545 | ||
2546 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2547 | || h->root.type == bfd_link_hash_defweak); | |
2548 | BFD_ASSERT (weakdef->def_dynamic); | |
a26587ba RS |
2549 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined |
2550 | || weakdef->root.type == bfd_link_hash_defweak); | |
2551 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); | |
2552 | } | |
45d6a902 AM |
2553 | } |
2554 | ||
2555 | return TRUE; | |
2556 | } | |
2557 | ||
2558 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2559 | called via elf_link_hash_traverse, and also calls itself | |
2560 | recursively. */ | |
2561 | ||
28caa186 | 2562 | static bfd_boolean |
268b6b39 | 2563 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2564 | { |
a50b1753 | 2565 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 | 2566 | bfd *dynobj; |
9c5bfbb7 | 2567 | const struct elf_backend_data *bed; |
45d6a902 | 2568 | |
0eddce27 | 2569 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2570 | return FALSE; |
2571 | ||
45d6a902 AM |
2572 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2573 | if (h->root.type == bfd_link_hash_indirect) | |
2574 | return TRUE; | |
2575 | ||
2576 | /* Fix the symbol flags. */ | |
2577 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2578 | return FALSE; | |
2579 | ||
2580 | /* If this symbol does not require a PLT entry, and it is not | |
2581 | defined by a dynamic object, or is not referenced by a regular | |
2582 | object, ignore it. We do have to handle a weak defined symbol, | |
2583 | even if no regular object refers to it, if we decided to add it | |
2584 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2585 | about symbols which are defined by one dynamic object and | |
2586 | referenced by another one? */ | |
f5385ebf | 2587 | if (!h->needs_plt |
91e21fb7 | 2588 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2589 | && (h->def_regular |
2590 | || !h->def_dynamic | |
2591 | || (!h->ref_regular | |
f6e332e6 | 2592 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 | 2593 | { |
a6aa5195 | 2594 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2595 | return TRUE; |
2596 | } | |
2597 | ||
2598 | /* If we've already adjusted this symbol, don't do it again. This | |
2599 | can happen via a recursive call. */ | |
f5385ebf | 2600 | if (h->dynamic_adjusted) |
45d6a902 AM |
2601 | return TRUE; |
2602 | ||
2603 | /* Don't look at this symbol again. Note that we must set this | |
2604 | after checking the above conditions, because we may look at a | |
2605 | symbol once, decide not to do anything, and then get called | |
2606 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2607 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2608 | |
2609 | /* If this is a weak definition, and we know a real definition, and | |
2610 | the real symbol is not itself defined by a regular object file, | |
2611 | then get a good value for the real definition. We handle the | |
2612 | real symbol first, for the convenience of the backend routine. | |
2613 | ||
2614 | Note that there is a confusing case here. If the real definition | |
2615 | is defined by a regular object file, we don't get the real symbol | |
2616 | from the dynamic object, but we do get the weak symbol. If the | |
2617 | processor backend uses a COPY reloc, then if some routine in the | |
2618 | dynamic object changes the real symbol, we will not see that | |
2619 | change in the corresponding weak symbol. This is the way other | |
2620 | ELF linkers work as well, and seems to be a result of the shared | |
2621 | library model. | |
2622 | ||
2623 | I will clarify this issue. Most SVR4 shared libraries define the | |
2624 | variable _timezone and define timezone as a weak synonym. The | |
2625 | tzset call changes _timezone. If you write | |
2626 | extern int timezone; | |
2627 | int _timezone = 5; | |
2628 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2629 | you might expect that, since timezone is a synonym for _timezone, | |
2630 | the same number will print both times. However, if the processor | |
2631 | backend uses a COPY reloc, then actually timezone will be copied | |
2632 | into your process image, and, since you define _timezone | |
2633 | yourself, _timezone will not. Thus timezone and _timezone will | |
2634 | wind up at different memory locations. The tzset call will set | |
2635 | _timezone, leaving timezone unchanged. */ | |
2636 | ||
f6e332e6 | 2637 | if (h->u.weakdef != NULL) |
45d6a902 | 2638 | { |
ec24dc88 AM |
2639 | /* If we get to this point, there is an implicit reference to |
2640 | H->U.WEAKDEF by a regular object file via the weak symbol H. */ | |
f6e332e6 | 2641 | h->u.weakdef->ref_regular = 1; |
45d6a902 | 2642 | |
ec24dc88 AM |
2643 | /* Ensure that the backend adjust_dynamic_symbol function sees |
2644 | H->U.WEAKDEF before H by recursively calling ourselves. */ | |
f6e332e6 | 2645 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2646 | return FALSE; |
2647 | } | |
2648 | ||
2649 | /* If a symbol has no type and no size and does not require a PLT | |
2650 | entry, then we are probably about to do the wrong thing here: we | |
2651 | are probably going to create a COPY reloc for an empty object. | |
2652 | This case can arise when a shared object is built with assembly | |
2653 | code, and the assembly code fails to set the symbol type. */ | |
2654 | if (h->size == 0 | |
2655 | && h->type == STT_NOTYPE | |
f5385ebf | 2656 | && !h->needs_plt) |
45d6a902 AM |
2657 | (*_bfd_error_handler) |
2658 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2659 | h->root.root.string); | |
2660 | ||
2661 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2662 | bed = get_elf_backend_data (dynobj); | |
e7c33416 | 2663 | |
45d6a902 AM |
2664 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
2665 | { | |
2666 | eif->failed = TRUE; | |
2667 | return FALSE; | |
2668 | } | |
2669 | ||
2670 | return TRUE; | |
2671 | } | |
2672 | ||
027297b7 L |
2673 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2674 | DYNBSS. */ | |
2675 | ||
2676 | bfd_boolean | |
6cabe1ea AM |
2677 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
2678 | struct elf_link_hash_entry *h, | |
027297b7 L |
2679 | asection *dynbss) |
2680 | { | |
91ac5911 | 2681 | unsigned int power_of_two; |
027297b7 L |
2682 | bfd_vma mask; |
2683 | asection *sec = h->root.u.def.section; | |
2684 | ||
2685 | /* The section aligment of definition is the maximum alignment | |
91ac5911 L |
2686 | requirement of symbols defined in the section. Since we don't |
2687 | know the symbol alignment requirement, we start with the | |
2688 | maximum alignment and check low bits of the symbol address | |
2689 | for the minimum alignment. */ | |
2690 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
2691 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
2692 | while ((h->root.u.def.value & mask) != 0) | |
2693 | { | |
2694 | mask >>= 1; | |
2695 | --power_of_two; | |
2696 | } | |
027297b7 | 2697 | |
91ac5911 L |
2698 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
2699 | dynbss)) | |
027297b7 L |
2700 | { |
2701 | /* Adjust the section alignment if needed. */ | |
2702 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 2703 | power_of_two)) |
027297b7 L |
2704 | return FALSE; |
2705 | } | |
2706 | ||
91ac5911 | 2707 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
2708 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
2709 | ||
2710 | /* Define the symbol as being at this point in DYNBSS. */ | |
2711 | h->root.u.def.section = dynbss; | |
2712 | h->root.u.def.value = dynbss->size; | |
2713 | ||
2714 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
2715 | dynbss->size += h->size; | |
2716 | ||
f7483970 L |
2717 | /* No error if extern_protected_data is true. */ |
2718 | if (h->protected_def | |
889c2a67 L |
2719 | && (!info->extern_protected_data |
2720 | || (info->extern_protected_data < 0 | |
2721 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 AM |
2722 | info->callbacks->einfo |
2723 | (_("%P: copy reloc against protected `%T' is dangerous\n"), | |
2724 | h->root.root.string); | |
6cabe1ea | 2725 | |
027297b7 L |
2726 | return TRUE; |
2727 | } | |
2728 | ||
45d6a902 AM |
2729 | /* Adjust all external symbols pointing into SEC_MERGE sections |
2730 | to reflect the object merging within the sections. */ | |
2731 | ||
28caa186 | 2732 | static bfd_boolean |
268b6b39 | 2733 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2734 | { |
2735 | asection *sec; | |
2736 | ||
45d6a902 AM |
2737 | if ((h->root.type == bfd_link_hash_defined |
2738 | || h->root.type == bfd_link_hash_defweak) | |
2739 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 2740 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 2741 | { |
a50b1753 | 2742 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
2743 | |
2744 | h->root.u.def.value = | |
2745 | _bfd_merged_section_offset (output_bfd, | |
2746 | &h->root.u.def.section, | |
2747 | elf_section_data (sec)->sec_info, | |
753731ee | 2748 | h->root.u.def.value); |
45d6a902 AM |
2749 | } |
2750 | ||
2751 | return TRUE; | |
2752 | } | |
986a241f RH |
2753 | |
2754 | /* Returns false if the symbol referred to by H should be considered | |
2755 | to resolve local to the current module, and true if it should be | |
2756 | considered to bind dynamically. */ | |
2757 | ||
2758 | bfd_boolean | |
268b6b39 AM |
2759 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2760 | struct bfd_link_info *info, | |
89a2ee5a | 2761 | bfd_boolean not_local_protected) |
986a241f RH |
2762 | { |
2763 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
2764 | const struct elf_backend_data *bed; |
2765 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
2766 | |
2767 | if (h == NULL) | |
2768 | return FALSE; | |
2769 | ||
2770 | while (h->root.type == bfd_link_hash_indirect | |
2771 | || h->root.type == bfd_link_hash_warning) | |
2772 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2773 | ||
2774 | /* If it was forced local, then clearly it's not dynamic. */ | |
2775 | if (h->dynindx == -1) | |
2776 | return FALSE; | |
f5385ebf | 2777 | if (h->forced_local) |
986a241f RH |
2778 | return FALSE; |
2779 | ||
2780 | /* Identify the cases where name binding rules say that a | |
2781 | visible symbol resolves locally. */ | |
55255dae | 2782 | binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); |
986a241f RH |
2783 | |
2784 | switch (ELF_ST_VISIBILITY (h->other)) | |
2785 | { | |
2786 | case STV_INTERNAL: | |
2787 | case STV_HIDDEN: | |
2788 | return FALSE; | |
2789 | ||
2790 | case STV_PROTECTED: | |
fcb93ecf PB |
2791 | hash_table = elf_hash_table (info); |
2792 | if (!is_elf_hash_table (hash_table)) | |
2793 | return FALSE; | |
2794 | ||
2795 | bed = get_elf_backend_data (hash_table->dynobj); | |
2796 | ||
986a241f RH |
2797 | /* Proper resolution for function pointer equality may require |
2798 | that these symbols perhaps be resolved dynamically, even though | |
2799 | we should be resolving them to the current module. */ | |
89a2ee5a | 2800 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
2801 | binding_stays_local_p = TRUE; |
2802 | break; | |
2803 | ||
2804 | default: | |
986a241f RH |
2805 | break; |
2806 | } | |
2807 | ||
aa37626c | 2808 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 2809 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
2810 | return TRUE; |
2811 | ||
986a241f RH |
2812 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2813 | us that it remains local. */ | |
2814 | return !binding_stays_local_p; | |
2815 | } | |
f6c52c13 AM |
2816 | |
2817 | /* Return true if the symbol referred to by H should be considered | |
2818 | to resolve local to the current module, and false otherwise. Differs | |
2819 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 2820 | undefined symbols. The two functions are virtually identical except |
89a2ee5a AM |
2821 | for the place where forced_local and dynindx == -1 are tested. If |
2822 | either of those tests are true, _bfd_elf_dynamic_symbol_p will say | |
2823 | the symbol is local, while _bfd_elf_symbol_refs_local_p will say | |
2824 | the symbol is local only for defined symbols. | |
2825 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as | |
2826 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
2827 | treatment of undefined weak symbols. For those that do not make | |
2828 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
2829 | |
2830 | bfd_boolean | |
268b6b39 AM |
2831 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2832 | struct bfd_link_info *info, | |
2833 | bfd_boolean local_protected) | |
f6c52c13 | 2834 | { |
fcb93ecf PB |
2835 | const struct elf_backend_data *bed; |
2836 | struct elf_link_hash_table *hash_table; | |
2837 | ||
f6c52c13 AM |
2838 | /* If it's a local sym, of course we resolve locally. */ |
2839 | if (h == NULL) | |
2840 | return TRUE; | |
2841 | ||
d95edcac L |
2842 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
2843 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
2844 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
2845 | return TRUE; | |
2846 | ||
7e2294f9 AO |
2847 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2848 | flag set, so test it first, and don't bail out. */ | |
2849 | if (ELF_COMMON_DEF_P (h)) | |
2850 | /* Do nothing. */; | |
f6c52c13 | 2851 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
2852 | resolve locally. The sym is either undefined or dynamic. */ |
2853 | else if (!h->def_regular) | |
f6c52c13 AM |
2854 | return FALSE; |
2855 | ||
2856 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2857 | if (h->forced_local) |
f6c52c13 AM |
2858 | return TRUE; |
2859 | ||
2860 | /* As do non-dynamic symbols. */ | |
2861 | if (h->dynindx == -1) | |
2862 | return TRUE; | |
2863 | ||
2864 | /* At this point, we know the symbol is defined and dynamic. In an | |
2865 | executable it must resolve locally, likewise when building symbolic | |
2866 | shared libraries. */ | |
55255dae | 2867 | if (info->executable || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
2868 | return TRUE; |
2869 | ||
2870 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2871 | with default visibility might not resolve locally. */ | |
2872 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2873 | return FALSE; | |
2874 | ||
fcb93ecf PB |
2875 | hash_table = elf_hash_table (info); |
2876 | if (!is_elf_hash_table (hash_table)) | |
2877 | return TRUE; | |
2878 | ||
2879 | bed = get_elf_backend_data (hash_table->dynobj); | |
2880 | ||
f7483970 L |
2881 | /* If extern_protected_data is false, STV_PROTECTED non-function |
2882 | symbols are local. */ | |
889c2a67 L |
2883 | if ((!info->extern_protected_data |
2884 | || (info->extern_protected_data < 0 | |
2885 | && !bed->extern_protected_data)) | |
2886 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
2887 | return TRUE; |
2888 | ||
f6c52c13 | 2889 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
2890 | symbols be treated as dynamic symbols. If the address of a |
2891 | function not defined in an executable is set to that function's | |
2892 | plt entry in the executable, then the address of the function in | |
2893 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
2894 | return local_protected; |
2895 | } | |
e1918d23 AM |
2896 | |
2897 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2898 | aligned. Returns the first TLS output section. */ | |
2899 | ||
2900 | struct bfd_section * | |
2901 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2902 | { | |
2903 | struct bfd_section *sec, *tls; | |
2904 | unsigned int align = 0; | |
2905 | ||
2906 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2907 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2908 | break; | |
2909 | tls = sec; | |
2910 | ||
2911 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2912 | if (sec->alignment_power > align) | |
2913 | align = sec->alignment_power; | |
2914 | ||
2915 | elf_hash_table (info)->tls_sec = tls; | |
2916 | ||
2917 | /* Ensure the alignment of the first section is the largest alignment, | |
2918 | so that the tls segment starts aligned. */ | |
2919 | if (tls != NULL) | |
2920 | tls->alignment_power = align; | |
2921 | ||
2922 | return tls; | |
2923 | } | |
0ad989f9 L |
2924 | |
2925 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2926 | static bfd_boolean | |
2927 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2928 | Elf_Internal_Sym *sym) | |
2929 | { | |
a4d8e49b L |
2930 | const struct elf_backend_data *bed; |
2931 | ||
0ad989f9 L |
2932 | /* Local symbols do not count, but target specific ones might. */ |
2933 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2934 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2935 | return FALSE; | |
2936 | ||
fcb93ecf | 2937 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 2938 | /* Function symbols do not count. */ |
fcb93ecf | 2939 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
2940 | return FALSE; |
2941 | ||
2942 | /* If the section is undefined, then so is the symbol. */ | |
2943 | if (sym->st_shndx == SHN_UNDEF) | |
2944 | return FALSE; | |
2945 | ||
2946 | /* If the symbol is defined in the common section, then | |
2947 | it is a common definition and so does not count. */ | |
a4d8e49b | 2948 | if (bed->common_definition (sym)) |
0ad989f9 L |
2949 | return FALSE; |
2950 | ||
2951 | /* If the symbol is in a target specific section then we | |
2952 | must rely upon the backend to tell us what it is. */ | |
2953 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2954 | /* FIXME - this function is not coded yet: | |
2955 | ||
2956 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2957 | ||
2958 | Instead for now assume that the definition is not global, | |
2959 | Even if this is wrong, at least the linker will behave | |
2960 | in the same way that it used to do. */ | |
2961 | return FALSE; | |
2962 | ||
2963 | return TRUE; | |
2964 | } | |
2965 | ||
2966 | /* Search the symbol table of the archive element of the archive ABFD | |
2967 | whose archive map contains a mention of SYMDEF, and determine if | |
2968 | the symbol is defined in this element. */ | |
2969 | static bfd_boolean | |
2970 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2971 | { | |
2972 | Elf_Internal_Shdr * hdr; | |
2973 | bfd_size_type symcount; | |
2974 | bfd_size_type extsymcount; | |
2975 | bfd_size_type extsymoff; | |
2976 | Elf_Internal_Sym *isymbuf; | |
2977 | Elf_Internal_Sym *isym; | |
2978 | Elf_Internal_Sym *isymend; | |
2979 | bfd_boolean result; | |
2980 | ||
2981 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2982 | if (abfd == NULL) | |
2983 | return FALSE; | |
2984 | ||
f0bf6bfd L |
2985 | /* Return FALSE if the object has been claimed by plugin. */ |
2986 | if (abfd->plugin_format == bfd_plugin_yes) | |
2987 | return FALSE; | |
2988 | ||
0ad989f9 L |
2989 | if (! bfd_check_format (abfd, bfd_object)) |
2990 | return FALSE; | |
2991 | ||
0ad989f9 L |
2992 | /* Select the appropriate symbol table. */ |
2993 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2994 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2995 | else | |
2996 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2997 | ||
2998 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2999 | ||
3000 | /* The sh_info field of the symtab header tells us where the | |
3001 | external symbols start. We don't care about the local symbols. */ | |
3002 | if (elf_bad_symtab (abfd)) | |
3003 | { | |
3004 | extsymcount = symcount; | |
3005 | extsymoff = 0; | |
3006 | } | |
3007 | else | |
3008 | { | |
3009 | extsymcount = symcount - hdr->sh_info; | |
3010 | extsymoff = hdr->sh_info; | |
3011 | } | |
3012 | ||
3013 | if (extsymcount == 0) | |
3014 | return FALSE; | |
3015 | ||
3016 | /* Read in the symbol table. */ | |
3017 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3018 | NULL, NULL, NULL); | |
3019 | if (isymbuf == NULL) | |
3020 | return FALSE; | |
3021 | ||
3022 | /* Scan the symbol table looking for SYMDEF. */ | |
3023 | result = FALSE; | |
3024 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3025 | { | |
3026 | const char *name; | |
3027 | ||
3028 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3029 | isym->st_name); | |
3030 | if (name == NULL) | |
3031 | break; | |
3032 | ||
3033 | if (strcmp (name, symdef->name) == 0) | |
3034 | { | |
3035 | result = is_global_data_symbol_definition (abfd, isym); | |
3036 | break; | |
3037 | } | |
3038 | } | |
3039 | ||
3040 | free (isymbuf); | |
3041 | ||
3042 | return result; | |
3043 | } | |
3044 | \f | |
5a580b3a AM |
3045 | /* Add an entry to the .dynamic table. */ |
3046 | ||
3047 | bfd_boolean | |
3048 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3049 | bfd_vma tag, | |
3050 | bfd_vma val) | |
3051 | { | |
3052 | struct elf_link_hash_table *hash_table; | |
3053 | const struct elf_backend_data *bed; | |
3054 | asection *s; | |
3055 | bfd_size_type newsize; | |
3056 | bfd_byte *newcontents; | |
3057 | Elf_Internal_Dyn dyn; | |
3058 | ||
3059 | hash_table = elf_hash_table (info); | |
3060 | if (! is_elf_hash_table (hash_table)) | |
3061 | return FALSE; | |
3062 | ||
3063 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3064 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3065 | BFD_ASSERT (s != NULL); |
3066 | ||
eea6121a | 3067 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3068 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3069 | if (newcontents == NULL) |
3070 | return FALSE; | |
3071 | ||
3072 | dyn.d_tag = tag; | |
3073 | dyn.d_un.d_val = val; | |
eea6121a | 3074 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3075 | |
eea6121a | 3076 | s->size = newsize; |
5a580b3a AM |
3077 | s->contents = newcontents; |
3078 | ||
3079 | return TRUE; | |
3080 | } | |
3081 | ||
3082 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3083 | otherwise just check whether one already exists. Returns -1 on error, | |
3084 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3085 | ||
4ad4eba5 | 3086 | static int |
7e9f0867 AM |
3087 | elf_add_dt_needed_tag (bfd *abfd, |
3088 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3089 | const char *soname, |
3090 | bfd_boolean do_it) | |
5a580b3a AM |
3091 | { |
3092 | struct elf_link_hash_table *hash_table; | |
5a580b3a AM |
3093 | bfd_size_type strindex; |
3094 | ||
7e9f0867 AM |
3095 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3096 | return -1; | |
3097 | ||
5a580b3a | 3098 | hash_table = elf_hash_table (info); |
5a580b3a AM |
3099 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
3100 | if (strindex == (bfd_size_type) -1) | |
3101 | return -1; | |
3102 | ||
02be4619 | 3103 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3104 | { |
3105 | asection *sdyn; | |
3106 | const struct elf_backend_data *bed; | |
3107 | bfd_byte *extdyn; | |
3108 | ||
3109 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3110 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3111 | if (sdyn != NULL) |
3112 | for (extdyn = sdyn->contents; | |
3113 | extdyn < sdyn->contents + sdyn->size; | |
3114 | extdyn += bed->s->sizeof_dyn) | |
3115 | { | |
3116 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3117 | |
7e9f0867 AM |
3118 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3119 | if (dyn.d_tag == DT_NEEDED | |
3120 | && dyn.d_un.d_val == strindex) | |
3121 | { | |
3122 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3123 | return 1; | |
3124 | } | |
3125 | } | |
5a580b3a AM |
3126 | } |
3127 | ||
3128 | if (do_it) | |
3129 | { | |
7e9f0867 AM |
3130 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3131 | return -1; | |
3132 | ||
5a580b3a AM |
3133 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3134 | return -1; | |
3135 | } | |
3136 | else | |
3137 | /* We were just checking for existence of the tag. */ | |
3138 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3139 | ||
3140 | return 0; | |
3141 | } | |
3142 | ||
010e5ae2 AM |
3143 | static bfd_boolean |
3144 | on_needed_list (const char *soname, struct bfd_link_needed_list *needed) | |
3145 | { | |
3146 | for (; needed != NULL; needed = needed->next) | |
1240be6b AM |
3147 | if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0 |
3148 | && strcmp (soname, needed->name) == 0) | |
010e5ae2 AM |
3149 | return TRUE; |
3150 | ||
3151 | return FALSE; | |
3152 | } | |
3153 | ||
14160578 | 3154 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3155 | static int |
3156 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3157 | { |
3158 | const struct elf_link_hash_entry *h1; | |
3159 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3160 | bfd_signed_vma vdiff; |
5a580b3a AM |
3161 | |
3162 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3163 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3164 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3165 | if (vdiff != 0) | |
3166 | return vdiff > 0 ? 1 : -1; | |
3167 | else | |
3168 | { | |
3169 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3170 | if (sdiff != 0) | |
3171 | return sdiff > 0 ? 1 : -1; | |
3172 | } | |
14160578 AM |
3173 | vdiff = h1->size - h2->size; |
3174 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3175 | } |
4ad4eba5 | 3176 | |
5a580b3a AM |
3177 | /* This function is used to adjust offsets into .dynstr for |
3178 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3179 | ||
3180 | static bfd_boolean | |
3181 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3182 | { | |
a50b1753 | 3183 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3184 | |
5a580b3a AM |
3185 | if (h->dynindx != -1) |
3186 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3187 | return TRUE; | |
3188 | } | |
3189 | ||
3190 | /* Assign string offsets in .dynstr, update all structures referencing | |
3191 | them. */ | |
3192 | ||
4ad4eba5 AM |
3193 | static bfd_boolean |
3194 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3195 | { |
3196 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3197 | struct elf_link_local_dynamic_entry *entry; | |
3198 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3199 | bfd *dynobj = hash_table->dynobj; | |
3200 | asection *sdyn; | |
3201 | bfd_size_type size; | |
3202 | const struct elf_backend_data *bed; | |
3203 | bfd_byte *extdyn; | |
3204 | ||
3205 | _bfd_elf_strtab_finalize (dynstr); | |
3206 | size = _bfd_elf_strtab_size (dynstr); | |
3207 | ||
3208 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3209 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3210 | BFD_ASSERT (sdyn != NULL); |
3211 | ||
3212 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3213 | for (extdyn = sdyn->contents; | |
eea6121a | 3214 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3215 | extdyn += bed->s->sizeof_dyn) |
3216 | { | |
3217 | Elf_Internal_Dyn dyn; | |
3218 | ||
3219 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3220 | switch (dyn.d_tag) | |
3221 | { | |
3222 | case DT_STRSZ: | |
3223 | dyn.d_un.d_val = size; | |
3224 | break; | |
3225 | case DT_NEEDED: | |
3226 | case DT_SONAME: | |
3227 | case DT_RPATH: | |
3228 | case DT_RUNPATH: | |
3229 | case DT_FILTER: | |
3230 | case DT_AUXILIARY: | |
7ee314fa AM |
3231 | case DT_AUDIT: |
3232 | case DT_DEPAUDIT: | |
5a580b3a AM |
3233 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3234 | break; | |
3235 | default: | |
3236 | continue; | |
3237 | } | |
3238 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3239 | } | |
3240 | ||
3241 | /* Now update local dynamic symbols. */ | |
3242 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3243 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3244 | entry->isym.st_name); | |
3245 | ||
3246 | /* And the rest of dynamic symbols. */ | |
3247 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3248 | ||
3249 | /* Adjust version definitions. */ | |
3250 | if (elf_tdata (output_bfd)->cverdefs) | |
3251 | { | |
3252 | asection *s; | |
3253 | bfd_byte *p; | |
3254 | bfd_size_type i; | |
3255 | Elf_Internal_Verdef def; | |
3256 | Elf_Internal_Verdaux defaux; | |
3257 | ||
3d4d4302 | 3258 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3259 | p = s->contents; |
3260 | do | |
3261 | { | |
3262 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3263 | &def); | |
3264 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3265 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3266 | continue; | |
5a580b3a AM |
3267 | for (i = 0; i < def.vd_cnt; ++i) |
3268 | { | |
3269 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3270 | (Elf_External_Verdaux *) p, &defaux); | |
3271 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3272 | defaux.vda_name); | |
3273 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3274 | &defaux, (Elf_External_Verdaux *) p); | |
3275 | p += sizeof (Elf_External_Verdaux); | |
3276 | } | |
3277 | } | |
3278 | while (def.vd_next); | |
3279 | } | |
3280 | ||
3281 | /* Adjust version references. */ | |
3282 | if (elf_tdata (output_bfd)->verref) | |
3283 | { | |
3284 | asection *s; | |
3285 | bfd_byte *p; | |
3286 | bfd_size_type i; | |
3287 | Elf_Internal_Verneed need; | |
3288 | Elf_Internal_Vernaux needaux; | |
3289 | ||
3d4d4302 | 3290 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3291 | p = s->contents; |
3292 | do | |
3293 | { | |
3294 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3295 | &need); | |
3296 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3297 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3298 | (Elf_External_Verneed *) p); | |
3299 | p += sizeof (Elf_External_Verneed); | |
3300 | for (i = 0; i < need.vn_cnt; ++i) | |
3301 | { | |
3302 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3303 | (Elf_External_Vernaux *) p, &needaux); | |
3304 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3305 | needaux.vna_name); | |
3306 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3307 | &needaux, | |
3308 | (Elf_External_Vernaux *) p); | |
3309 | p += sizeof (Elf_External_Vernaux); | |
3310 | } | |
3311 | } | |
3312 | while (need.vn_next); | |
3313 | } | |
3314 | ||
3315 | return TRUE; | |
3316 | } | |
3317 | \f | |
13285a1b AM |
3318 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3319 | The default is to only match when the INPUT and OUTPUT are exactly | |
3320 | the same target. */ | |
3321 | ||
3322 | bfd_boolean | |
3323 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3324 | const bfd_target *output) | |
3325 | { | |
3326 | return input == output; | |
3327 | } | |
3328 | ||
3329 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3330 | This version is used when different targets for the same architecture | |
3331 | are virtually identical. */ | |
3332 | ||
3333 | bfd_boolean | |
3334 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3335 | const bfd_target *output) | |
3336 | { | |
3337 | const struct elf_backend_data *obed, *ibed; | |
3338 | ||
3339 | if (input == output) | |
3340 | return TRUE; | |
3341 | ||
3342 | ibed = xvec_get_elf_backend_data (input); | |
3343 | obed = xvec_get_elf_backend_data (output); | |
3344 | ||
3345 | if (ibed->arch != obed->arch) | |
3346 | return FALSE; | |
3347 | ||
3348 | /* If both backends are using this function, deem them compatible. */ | |
3349 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3350 | } | |
3351 | ||
e5034e59 AM |
3352 | /* Make a special call to the linker "notice" function to tell it that |
3353 | we are about to handle an as-needed lib, or have finished | |
3354 | processing the lib. */ | |
3355 | ||
3356 | bfd_boolean | |
3357 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3358 | struct bfd_link_info *info, | |
3359 | enum notice_asneeded_action act) | |
3360 | { | |
46135103 | 3361 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3362 | } |
3363 | ||
4ad4eba5 AM |
3364 | /* Add symbols from an ELF object file to the linker hash table. */ |
3365 | ||
3366 | static bfd_boolean | |
3367 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3368 | { | |
a0c402a5 | 3369 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 AM |
3370 | Elf_Internal_Shdr *hdr; |
3371 | bfd_size_type symcount; | |
3372 | bfd_size_type extsymcount; | |
3373 | bfd_size_type extsymoff; | |
3374 | struct elf_link_hash_entry **sym_hash; | |
3375 | bfd_boolean dynamic; | |
3376 | Elf_External_Versym *extversym = NULL; | |
3377 | Elf_External_Versym *ever; | |
3378 | struct elf_link_hash_entry *weaks; | |
3379 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
3380 | bfd_size_type nondeflt_vers_cnt = 0; | |
3381 | Elf_Internal_Sym *isymbuf = NULL; | |
3382 | Elf_Internal_Sym *isym; | |
3383 | Elf_Internal_Sym *isymend; | |
3384 | const struct elf_backend_data *bed; | |
3385 | bfd_boolean add_needed; | |
66eb6687 | 3386 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3387 | bfd_size_type amt; |
66eb6687 | 3388 | void *alloc_mark = NULL; |
4f87808c AM |
3389 | struct bfd_hash_entry **old_table = NULL; |
3390 | unsigned int old_size = 0; | |
3391 | unsigned int old_count = 0; | |
66eb6687 | 3392 | void *old_tab = NULL; |
66eb6687 AM |
3393 | void *old_ent; |
3394 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3395 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
3396 | long old_dynsymcount = 0; | |
a4542f1b | 3397 | bfd_size_type old_dynstr_size = 0; |
66eb6687 | 3398 | size_t tabsize = 0; |
db6a5d5f | 3399 | asection *s; |
29a9f53e | 3400 | bfd_boolean just_syms; |
4ad4eba5 | 3401 | |
66eb6687 | 3402 | htab = elf_hash_table (info); |
4ad4eba5 | 3403 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3404 | |
3405 | if ((abfd->flags & DYNAMIC) == 0) | |
3406 | dynamic = FALSE; | |
3407 | else | |
3408 | { | |
3409 | dynamic = TRUE; | |
3410 | ||
3411 | /* You can't use -r against a dynamic object. Also, there's no | |
3412 | hope of using a dynamic object which does not exactly match | |
3413 | the format of the output file. */ | |
3414 | if (info->relocatable | |
66eb6687 | 3415 | || !is_elf_hash_table (htab) |
f13a99db | 3416 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3417 | { |
9a0789ec NC |
3418 | if (info->relocatable) |
3419 | bfd_set_error (bfd_error_invalid_operation); | |
3420 | else | |
3421 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3422 | goto error_return; |
3423 | } | |
3424 | } | |
3425 | ||
a0c402a5 L |
3426 | ehdr = elf_elfheader (abfd); |
3427 | if (info->warn_alternate_em | |
3428 | && bed->elf_machine_code != ehdr->e_machine | |
3429 | && ((bed->elf_machine_alt1 != 0 | |
3430 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3431 | || (bed->elf_machine_alt2 != 0 | |
3432 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
3433 | info->callbacks->einfo | |
3434 | (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), | |
3435 | ehdr->e_machine, abfd, bed->elf_machine_code); | |
3436 | ||
4ad4eba5 AM |
3437 | /* As a GNU extension, any input sections which are named |
3438 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3439 | symbol. This differs from .gnu.warning sections, which generate | |
3440 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3441 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3442 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3443 | { |
db6a5d5f | 3444 | const char *name; |
4ad4eba5 | 3445 | |
db6a5d5f AM |
3446 | name = bfd_get_section_name (abfd, s); |
3447 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3448 | { |
db6a5d5f AM |
3449 | char *msg; |
3450 | bfd_size_type sz; | |
3451 | ||
3452 | name += sizeof ".gnu.warning." - 1; | |
3453 | ||
3454 | /* If this is a shared object, then look up the symbol | |
3455 | in the hash table. If it is there, and it is already | |
3456 | been defined, then we will not be using the entry | |
3457 | from this shared object, so we don't need to warn. | |
3458 | FIXME: If we see the definition in a regular object | |
3459 | later on, we will warn, but we shouldn't. The only | |
3460 | fix is to keep track of what warnings we are supposed | |
3461 | to emit, and then handle them all at the end of the | |
3462 | link. */ | |
3463 | if (dynamic) | |
4ad4eba5 | 3464 | { |
db6a5d5f AM |
3465 | struct elf_link_hash_entry *h; |
3466 | ||
3467 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3468 | ||
3469 | /* FIXME: What about bfd_link_hash_common? */ | |
3470 | if (h != NULL | |
3471 | && (h->root.type == bfd_link_hash_defined | |
3472 | || h->root.type == bfd_link_hash_defweak)) | |
3473 | continue; | |
3474 | } | |
4ad4eba5 | 3475 | |
db6a5d5f AM |
3476 | sz = s->size; |
3477 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3478 | if (msg == NULL) | |
3479 | goto error_return; | |
4ad4eba5 | 3480 | |
db6a5d5f AM |
3481 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3482 | goto error_return; | |
4ad4eba5 | 3483 | |
db6a5d5f | 3484 | msg[sz] = '\0'; |
4ad4eba5 | 3485 | |
db6a5d5f AM |
3486 | if (! (_bfd_generic_link_add_one_symbol |
3487 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3488 | FALSE, bed->collect, NULL))) | |
3489 | goto error_return; | |
4ad4eba5 | 3490 | |
db6a5d5f AM |
3491 | if (!info->relocatable && info->executable) |
3492 | { | |
3493 | /* Clobber the section size so that the warning does | |
3494 | not get copied into the output file. */ | |
3495 | s->size = 0; | |
11d2f718 | 3496 | |
db6a5d5f AM |
3497 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3498 | the warning section don't get copied to the output. */ | |
3499 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3500 | } |
3501 | } | |
3502 | } | |
3503 | ||
29a9f53e L |
3504 | just_syms = ((s = abfd->sections) != NULL |
3505 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
3506 | ||
4ad4eba5 AM |
3507 | add_needed = TRUE; |
3508 | if (! dynamic) | |
3509 | { | |
3510 | /* If we are creating a shared library, create all the dynamic | |
3511 | sections immediately. We need to attach them to something, | |
3512 | so we attach them to this BFD, provided it is the right | |
29a9f53e L |
3513 | format and is not from ld --just-symbols. FIXME: If there |
3514 | are no input BFD's of the same format as the output, we can't | |
3515 | make a shared library. */ | |
3516 | if (!just_syms | |
3517 | && info->shared | |
66eb6687 | 3518 | && is_elf_hash_table (htab) |
f13a99db | 3519 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3520 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3521 | { |
3522 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3523 | goto error_return; | |
3524 | } | |
3525 | } | |
66eb6687 | 3526 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3527 | goto error_return; |
3528 | else | |
3529 | { | |
4ad4eba5 | 3530 | const char *soname = NULL; |
7ee314fa | 3531 | char *audit = NULL; |
4ad4eba5 AM |
3532 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
3533 | int ret; | |
3534 | ||
3535 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3536 | ld shouldn't allow it. */ |
29a9f53e | 3537 | if (just_syms) |
92fd189d | 3538 | abort (); |
4ad4eba5 AM |
3539 | |
3540 | /* If this dynamic lib was specified on the command line with | |
3541 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3542 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3543 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3544 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3545 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3546 | all. */ | |
3547 | add_needed = (elf_dyn_lib_class (abfd) | |
3548 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3549 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3550 | |
3551 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3552 | if (s != NULL) | |
3553 | { | |
3554 | bfd_byte *dynbuf; | |
3555 | bfd_byte *extdyn; | |
cb33740c | 3556 | unsigned int elfsec; |
4ad4eba5 AM |
3557 | unsigned long shlink; |
3558 | ||
eea6121a | 3559 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
3560 | { |
3561 | error_free_dyn: | |
3562 | free (dynbuf); | |
3563 | goto error_return; | |
3564 | } | |
4ad4eba5 AM |
3565 | |
3566 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3567 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3568 | goto error_free_dyn; |
3569 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3570 | ||
3571 | for (extdyn = dynbuf; | |
eea6121a | 3572 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3573 | extdyn += bed->s->sizeof_dyn) |
3574 | { | |
3575 | Elf_Internal_Dyn dyn; | |
3576 | ||
3577 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3578 | if (dyn.d_tag == DT_SONAME) | |
3579 | { | |
3580 | unsigned int tagv = dyn.d_un.d_val; | |
3581 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3582 | if (soname == NULL) | |
3583 | goto error_free_dyn; | |
3584 | } | |
3585 | if (dyn.d_tag == DT_NEEDED) | |
3586 | { | |
3587 | struct bfd_link_needed_list *n, **pn; | |
3588 | char *fnm, *anm; | |
3589 | unsigned int tagv = dyn.d_un.d_val; | |
3590 | ||
3591 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3592 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3593 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3594 | if (n == NULL || fnm == NULL) | |
3595 | goto error_free_dyn; | |
3596 | amt = strlen (fnm) + 1; | |
a50b1753 | 3597 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3598 | if (anm == NULL) |
3599 | goto error_free_dyn; | |
3600 | memcpy (anm, fnm, amt); | |
3601 | n->name = anm; | |
3602 | n->by = abfd; | |
3603 | n->next = NULL; | |
66eb6687 | 3604 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3605 | ; |
3606 | *pn = n; | |
3607 | } | |
3608 | if (dyn.d_tag == DT_RUNPATH) | |
3609 | { | |
3610 | struct bfd_link_needed_list *n, **pn; | |
3611 | char *fnm, *anm; | |
3612 | unsigned int tagv = dyn.d_un.d_val; | |
3613 | ||
3614 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3615 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3616 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3617 | if (n == NULL || fnm == NULL) | |
3618 | goto error_free_dyn; | |
3619 | amt = strlen (fnm) + 1; | |
a50b1753 | 3620 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3621 | if (anm == NULL) |
3622 | goto error_free_dyn; | |
3623 | memcpy (anm, fnm, amt); | |
3624 | n->name = anm; | |
3625 | n->by = abfd; | |
3626 | n->next = NULL; | |
3627 | for (pn = & runpath; | |
3628 | *pn != NULL; | |
3629 | pn = &(*pn)->next) | |
3630 | ; | |
3631 | *pn = n; | |
3632 | } | |
3633 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3634 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3635 | { | |
3636 | struct bfd_link_needed_list *n, **pn; | |
3637 | char *fnm, *anm; | |
3638 | unsigned int tagv = dyn.d_un.d_val; | |
3639 | ||
3640 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3641 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3642 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3643 | if (n == NULL || fnm == NULL) | |
3644 | goto error_free_dyn; | |
3645 | amt = strlen (fnm) + 1; | |
a50b1753 | 3646 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 3647 | if (anm == NULL) |
f8703194 | 3648 | goto error_free_dyn; |
4ad4eba5 AM |
3649 | memcpy (anm, fnm, amt); |
3650 | n->name = anm; | |
3651 | n->by = abfd; | |
3652 | n->next = NULL; | |
3653 | for (pn = & rpath; | |
3654 | *pn != NULL; | |
3655 | pn = &(*pn)->next) | |
3656 | ; | |
3657 | *pn = n; | |
3658 | } | |
7ee314fa AM |
3659 | if (dyn.d_tag == DT_AUDIT) |
3660 | { | |
3661 | unsigned int tagv = dyn.d_un.d_val; | |
3662 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3663 | } | |
4ad4eba5 AM |
3664 | } |
3665 | ||
3666 | free (dynbuf); | |
3667 | } | |
3668 | ||
3669 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3670 | frees all more recently bfd_alloc'd blocks as well. */ | |
3671 | if (runpath) | |
3672 | rpath = runpath; | |
3673 | ||
3674 | if (rpath) | |
3675 | { | |
3676 | struct bfd_link_needed_list **pn; | |
66eb6687 | 3677 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
3678 | ; |
3679 | *pn = rpath; | |
3680 | } | |
3681 | ||
3682 | /* We do not want to include any of the sections in a dynamic | |
3683 | object in the output file. We hack by simply clobbering the | |
3684 | list of sections in the BFD. This could be handled more | |
3685 | cleanly by, say, a new section flag; the existing | |
3686 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3687 | still implies that the section takes up space in the output | |
3688 | file. */ | |
3689 | bfd_section_list_clear (abfd); | |
3690 | ||
4ad4eba5 AM |
3691 | /* Find the name to use in a DT_NEEDED entry that refers to this |
3692 | object. If the object has a DT_SONAME entry, we use it. | |
3693 | Otherwise, if the generic linker stuck something in | |
3694 | elf_dt_name, we use that. Otherwise, we just use the file | |
3695 | name. */ | |
3696 | if (soname == NULL || *soname == '\0') | |
3697 | { | |
3698 | soname = elf_dt_name (abfd); | |
3699 | if (soname == NULL || *soname == '\0') | |
3700 | soname = bfd_get_filename (abfd); | |
3701 | } | |
3702 | ||
3703 | /* Save the SONAME because sometimes the linker emulation code | |
3704 | will need to know it. */ | |
3705 | elf_dt_name (abfd) = soname; | |
3706 | ||
7e9f0867 | 3707 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
3708 | if (ret < 0) |
3709 | goto error_return; | |
3710 | ||
3711 | /* If we have already included this dynamic object in the | |
3712 | link, just ignore it. There is no reason to include a | |
3713 | particular dynamic object more than once. */ | |
3714 | if (ret > 0) | |
3715 | return TRUE; | |
7ee314fa AM |
3716 | |
3717 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 3718 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
3719 | } |
3720 | ||
3721 | /* If this is a dynamic object, we always link against the .dynsym | |
3722 | symbol table, not the .symtab symbol table. The dynamic linker | |
3723 | will only see the .dynsym symbol table, so there is no reason to | |
3724 | look at .symtab for a dynamic object. */ | |
3725 | ||
3726 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3727 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3728 | else | |
3729 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3730 | ||
3731 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3732 | ||
3733 | /* The sh_info field of the symtab header tells us where the | |
3734 | external symbols start. We don't care about the local symbols at | |
3735 | this point. */ | |
3736 | if (elf_bad_symtab (abfd)) | |
3737 | { | |
3738 | extsymcount = symcount; | |
3739 | extsymoff = 0; | |
3740 | } | |
3741 | else | |
3742 | { | |
3743 | extsymcount = symcount - hdr->sh_info; | |
3744 | extsymoff = hdr->sh_info; | |
3745 | } | |
3746 | ||
f45794cb | 3747 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 3748 | if (extsymcount != 0) |
4ad4eba5 AM |
3749 | { |
3750 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3751 | NULL, NULL, NULL); | |
3752 | if (isymbuf == NULL) | |
3753 | goto error_return; | |
3754 | ||
4ad4eba5 | 3755 | if (sym_hash == NULL) |
012b2306 AM |
3756 | { |
3757 | /* We store a pointer to the hash table entry for each | |
3758 | external symbol. */ | |
3759 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3760 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); | |
3761 | if (sym_hash == NULL) | |
3762 | goto error_free_sym; | |
3763 | elf_sym_hashes (abfd) = sym_hash; | |
3764 | } | |
4ad4eba5 AM |
3765 | } |
3766 | ||
3767 | if (dynamic) | |
3768 | { | |
3769 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3770 | if (!_bfd_elf_slurp_version_tables (abfd, |
3771 | info->default_imported_symver)) | |
4ad4eba5 AM |
3772 | goto error_free_sym; |
3773 | ||
3774 | /* Read in the symbol versions, but don't bother to convert them | |
3775 | to internal format. */ | |
3776 | if (elf_dynversym (abfd) != 0) | |
3777 | { | |
3778 | Elf_Internal_Shdr *versymhdr; | |
3779 | ||
3780 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 3781 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
3782 | if (extversym == NULL) |
3783 | goto error_free_sym; | |
3784 | amt = versymhdr->sh_size; | |
3785 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3786 | || bfd_bread (extversym, amt, abfd) != amt) | |
3787 | goto error_free_vers; | |
3788 | } | |
3789 | } | |
3790 | ||
66eb6687 AM |
3791 | /* If we are loading an as-needed shared lib, save the symbol table |
3792 | state before we start adding symbols. If the lib turns out | |
3793 | to be unneeded, restore the state. */ | |
3794 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
3795 | { | |
3796 | unsigned int i; | |
3797 | size_t entsize; | |
3798 | ||
3799 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
3800 | { | |
3801 | struct bfd_hash_entry *p; | |
2de92251 | 3802 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3803 | |
3804 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
3805 | { |
3806 | h = (struct elf_link_hash_entry *) p; | |
3807 | entsize += htab->root.table.entsize; | |
3808 | if (h->root.type == bfd_link_hash_warning) | |
3809 | entsize += htab->root.table.entsize; | |
3810 | } | |
66eb6687 AM |
3811 | } |
3812 | ||
3813 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 3814 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
3815 | if (old_tab == NULL) |
3816 | goto error_free_vers; | |
3817 | ||
3818 | /* Remember the current objalloc pointer, so that all mem for | |
3819 | symbols added can later be reclaimed. */ | |
3820 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
3821 | if (alloc_mark == NULL) | |
3822 | goto error_free_vers; | |
3823 | ||
5061a885 AM |
3824 | /* Make a special call to the linker "notice" function to |
3825 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 3826 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 3827 | goto error_free_vers; |
5061a885 | 3828 | |
f45794cb AM |
3829 | /* Clone the symbol table. Remember some pointers into the |
3830 | symbol table, and dynamic symbol count. */ | |
3831 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 3832 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
3833 | old_undefs = htab->root.undefs; |
3834 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
3835 | old_table = htab->root.table.table; |
3836 | old_size = htab->root.table.size; | |
3837 | old_count = htab->root.table.count; | |
66eb6687 | 3838 | old_dynsymcount = htab->dynsymcount; |
a4542f1b | 3839 | old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr); |
66eb6687 AM |
3840 | |
3841 | for (i = 0; i < htab->root.table.size; i++) | |
3842 | { | |
3843 | struct bfd_hash_entry *p; | |
2de92251 | 3844 | struct elf_link_hash_entry *h; |
66eb6687 AM |
3845 | |
3846 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
3847 | { | |
3848 | memcpy (old_ent, p, htab->root.table.entsize); | |
3849 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
3850 | h = (struct elf_link_hash_entry *) p; |
3851 | if (h->root.type == bfd_link_hash_warning) | |
3852 | { | |
3853 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
3854 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
3855 | } | |
66eb6687 AM |
3856 | } |
3857 | } | |
3858 | } | |
4ad4eba5 | 3859 | |
66eb6687 | 3860 | weaks = NULL; |
4ad4eba5 AM |
3861 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
3862 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3863 | isym < isymend; | |
3864 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3865 | { | |
3866 | int bind; | |
3867 | bfd_vma value; | |
af44c138 | 3868 | asection *sec, *new_sec; |
4ad4eba5 AM |
3869 | flagword flags; |
3870 | const char *name; | |
3871 | struct elf_link_hash_entry *h; | |
90c984fc | 3872 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
3873 | bfd_boolean definition; |
3874 | bfd_boolean size_change_ok; | |
3875 | bfd_boolean type_change_ok; | |
3876 | bfd_boolean new_weakdef; | |
37a9e49a L |
3877 | bfd_boolean new_weak; |
3878 | bfd_boolean old_weak; | |
4ad4eba5 | 3879 | bfd_boolean override; |
a4d8e49b | 3880 | bfd_boolean common; |
4ad4eba5 AM |
3881 | unsigned int old_alignment; |
3882 | bfd *old_bfd; | |
3883 | ||
3884 | override = FALSE; | |
3885 | ||
3886 | flags = BSF_NO_FLAGS; | |
3887 | sec = NULL; | |
3888 | value = isym->st_value; | |
a4d8e49b | 3889 | common = bed->common_definition (isym); |
4ad4eba5 AM |
3890 | |
3891 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 3892 | switch (bind) |
4ad4eba5 | 3893 | { |
3e7a7d11 | 3894 | case STB_LOCAL: |
4ad4eba5 AM |
3895 | /* This should be impossible, since ELF requires that all |
3896 | global symbols follow all local symbols, and that sh_info | |
3897 | point to the first global symbol. Unfortunately, Irix 5 | |
3898 | screws this up. */ | |
3899 | continue; | |
3e7a7d11 NC |
3900 | |
3901 | case STB_GLOBAL: | |
a4d8e49b | 3902 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 3903 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
3904 | break; |
3905 | ||
3906 | case STB_WEAK: | |
3907 | flags = BSF_WEAK; | |
3908 | break; | |
3909 | ||
3910 | case STB_GNU_UNIQUE: | |
3911 | flags = BSF_GNU_UNIQUE; | |
3912 | break; | |
3913 | ||
3914 | default: | |
4ad4eba5 | 3915 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 3916 | break; |
4ad4eba5 AM |
3917 | } |
3918 | ||
3919 | if (isym->st_shndx == SHN_UNDEF) | |
3920 | sec = bfd_und_section_ptr; | |
cb33740c AM |
3921 | else if (isym->st_shndx == SHN_ABS) |
3922 | sec = bfd_abs_section_ptr; | |
3923 | else if (isym->st_shndx == SHN_COMMON) | |
3924 | { | |
3925 | sec = bfd_com_section_ptr; | |
3926 | /* What ELF calls the size we call the value. What ELF | |
3927 | calls the value we call the alignment. */ | |
3928 | value = isym->st_size; | |
3929 | } | |
3930 | else | |
4ad4eba5 AM |
3931 | { |
3932 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3933 | if (sec == NULL) | |
3934 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 3935 | else if (discarded_section (sec)) |
529fcb95 | 3936 | { |
e5d08002 L |
3937 | /* Symbols from discarded section are undefined. We keep |
3938 | its visibility. */ | |
529fcb95 PB |
3939 | sec = bfd_und_section_ptr; |
3940 | isym->st_shndx = SHN_UNDEF; | |
3941 | } | |
4ad4eba5 AM |
3942 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3943 | value -= sec->vma; | |
3944 | } | |
4ad4eba5 AM |
3945 | |
3946 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3947 | isym->st_name); | |
3948 | if (name == NULL) | |
3949 | goto error_free_vers; | |
3950 | ||
3951 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
3952 | && (abfd->flags & BFD_PLUGIN) != 0) |
3953 | { | |
3954 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
3955 | ||
3956 | if (xc == NULL) | |
3957 | { | |
3958 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
3959 | | SEC_EXCLUDE); | |
3960 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
3961 | if (xc == NULL) | |
3962 | goto error_free_vers; | |
3963 | } | |
3964 | sec = xc; | |
3965 | } | |
3966 | else if (isym->st_shndx == SHN_COMMON | |
3967 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
3968 | && !info->relocatable) | |
4ad4eba5 AM |
3969 | { |
3970 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3971 | ||
3972 | if (tcomm == NULL) | |
3973 | { | |
02d00247 AM |
3974 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
3975 | | SEC_LINKER_CREATED); | |
3976 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 3977 | if (tcomm == NULL) |
4ad4eba5 AM |
3978 | goto error_free_vers; |
3979 | } | |
3980 | sec = tcomm; | |
3981 | } | |
66eb6687 | 3982 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 3983 | { |
66eb6687 AM |
3984 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
3985 | &sec, &value)) | |
4ad4eba5 AM |
3986 | goto error_free_vers; |
3987 | ||
3988 | /* The hook function sets the name to NULL if this symbol | |
3989 | should be skipped for some reason. */ | |
3990 | if (name == NULL) | |
3991 | continue; | |
3992 | } | |
3993 | ||
3994 | /* Sanity check that all possibilities were handled. */ | |
3995 | if (sec == NULL) | |
3996 | { | |
3997 | bfd_set_error (bfd_error_bad_value); | |
3998 | goto error_free_vers; | |
3999 | } | |
4000 | ||
191c0c42 AM |
4001 | /* Silently discard TLS symbols from --just-syms. There's |
4002 | no way to combine a static TLS block with a new TLS block | |
4003 | for this executable. */ | |
4004 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4005 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4006 | continue; | |
4007 | ||
4ad4eba5 AM |
4008 | if (bfd_is_und_section (sec) |
4009 | || bfd_is_com_section (sec)) | |
4010 | definition = FALSE; | |
4011 | else | |
4012 | definition = TRUE; | |
4013 | ||
4014 | size_change_ok = FALSE; | |
66eb6687 | 4015 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4016 | old_weak = FALSE; |
4ad4eba5 AM |
4017 | old_alignment = 0; |
4018 | old_bfd = NULL; | |
af44c138 | 4019 | new_sec = sec; |
4ad4eba5 | 4020 | |
66eb6687 | 4021 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4022 | { |
4023 | Elf_Internal_Versym iver; | |
4024 | unsigned int vernum = 0; | |
4025 | bfd_boolean skip; | |
4026 | ||
fc0e6df6 | 4027 | if (ever == NULL) |
4ad4eba5 | 4028 | { |
fc0e6df6 PB |
4029 | if (info->default_imported_symver) |
4030 | /* Use the default symbol version created earlier. */ | |
4031 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4032 | else | |
4033 | iver.vs_vers = 0; | |
4034 | } | |
4035 | else | |
4036 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4037 | ||
4038 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4039 | ||
4040 | /* If this is a hidden symbol, or if it is not version | |
4041 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4042 | However, we do not modify a non-hidden absolute symbol |
4043 | if it is not a function, because it might be the version | |
4044 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4045 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4046 | || (vernum > 1 |
4047 | && (!bfd_is_abs_section (sec) | |
4048 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4049 | { |
4050 | const char *verstr; | |
4051 | size_t namelen, verlen, newlen; | |
4052 | char *newname, *p; | |
4053 | ||
4054 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4055 | { |
fc0e6df6 PB |
4056 | if (vernum > elf_tdata (abfd)->cverdefs) |
4057 | verstr = NULL; | |
4058 | else if (vernum > 1) | |
4059 | verstr = | |
4060 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4061 | else | |
4062 | verstr = ""; | |
4ad4eba5 | 4063 | |
fc0e6df6 | 4064 | if (verstr == NULL) |
4ad4eba5 | 4065 | { |
fc0e6df6 PB |
4066 | (*_bfd_error_handler) |
4067 | (_("%B: %s: invalid version %u (max %d)"), | |
4068 | abfd, name, vernum, | |
4069 | elf_tdata (abfd)->cverdefs); | |
4070 | bfd_set_error (bfd_error_bad_value); | |
4071 | goto error_free_vers; | |
4ad4eba5 | 4072 | } |
fc0e6df6 PB |
4073 | } |
4074 | else | |
4075 | { | |
4076 | /* We cannot simply test for the number of | |
4077 | entries in the VERNEED section since the | |
4078 | numbers for the needed versions do not start | |
4079 | at 0. */ | |
4080 | Elf_Internal_Verneed *t; | |
4081 | ||
4082 | verstr = NULL; | |
4083 | for (t = elf_tdata (abfd)->verref; | |
4084 | t != NULL; | |
4085 | t = t->vn_nextref) | |
4ad4eba5 | 4086 | { |
fc0e6df6 | 4087 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4088 | |
fc0e6df6 PB |
4089 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4090 | { | |
4091 | if (a->vna_other == vernum) | |
4ad4eba5 | 4092 | { |
fc0e6df6 PB |
4093 | verstr = a->vna_nodename; |
4094 | break; | |
4ad4eba5 | 4095 | } |
4ad4eba5 | 4096 | } |
fc0e6df6 PB |
4097 | if (a != NULL) |
4098 | break; | |
4099 | } | |
4100 | if (verstr == NULL) | |
4101 | { | |
4102 | (*_bfd_error_handler) | |
4103 | (_("%B: %s: invalid needed version %d"), | |
4104 | abfd, name, vernum); | |
4105 | bfd_set_error (bfd_error_bad_value); | |
4106 | goto error_free_vers; | |
4ad4eba5 | 4107 | } |
4ad4eba5 | 4108 | } |
fc0e6df6 PB |
4109 | |
4110 | namelen = strlen (name); | |
4111 | verlen = strlen (verstr); | |
4112 | newlen = namelen + verlen + 2; | |
4113 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4114 | && isym->st_shndx != SHN_UNDEF) | |
4115 | ++newlen; | |
4116 | ||
a50b1753 | 4117 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4118 | if (newname == NULL) |
4119 | goto error_free_vers; | |
4120 | memcpy (newname, name, namelen); | |
4121 | p = newname + namelen; | |
4122 | *p++ = ELF_VER_CHR; | |
4123 | /* If this is a defined non-hidden version symbol, | |
4124 | we add another @ to the name. This indicates the | |
4125 | default version of the symbol. */ | |
4126 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4127 | && isym->st_shndx != SHN_UNDEF) | |
4128 | *p++ = ELF_VER_CHR; | |
4129 | memcpy (p, verstr, verlen + 1); | |
4130 | ||
4131 | name = newname; | |
4ad4eba5 AM |
4132 | } |
4133 | ||
cd3416da AM |
4134 | /* If this symbol has default visibility and the user has |
4135 | requested we not re-export it, then mark it as hidden. */ | |
4136 | if (definition | |
4137 | && !dynamic | |
ce875075 | 4138 | && abfd->no_export |
cd3416da AM |
4139 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4140 | isym->st_other = (STV_HIDDEN | |
4141 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4142 | ||
4f3fedcf AM |
4143 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4144 | sym_hash, &old_bfd, &old_weak, | |
4145 | &old_alignment, &skip, &override, | |
4ad4eba5 AM |
4146 | &type_change_ok, &size_change_ok)) |
4147 | goto error_free_vers; | |
4148 | ||
4149 | if (skip) | |
4150 | continue; | |
4151 | ||
4152 | if (override) | |
4153 | definition = FALSE; | |
4154 | ||
4155 | h = *sym_hash; | |
4156 | while (h->root.type == bfd_link_hash_indirect | |
4157 | || h->root.type == bfd_link_hash_warning) | |
4158 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4159 | ||
4ad4eba5 | 4160 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4161 | && vernum > 1 |
4162 | && definition) | |
4163 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4164 | } | |
4165 | ||
4166 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4167 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4168 | (struct bfd_link_hash_entry **) sym_hash))) |
4169 | goto error_free_vers; | |
4170 | ||
4171 | h = *sym_hash; | |
90c984fc L |
4172 | /* We need to make sure that indirect symbol dynamic flags are |
4173 | updated. */ | |
4174 | hi = h; | |
4ad4eba5 AM |
4175 | while (h->root.type == bfd_link_hash_indirect |
4176 | || h->root.type == bfd_link_hash_warning) | |
4177 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4178 | |
4ad4eba5 AM |
4179 | *sym_hash = h; |
4180 | ||
37a9e49a | 4181 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4182 | new_weakdef = FALSE; |
4183 | if (dynamic | |
4184 | && definition | |
37a9e49a | 4185 | && new_weak |
fcb93ecf | 4186 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4187 | && is_elf_hash_table (htab) |
f6e332e6 | 4188 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
4189 | { |
4190 | /* Keep a list of all weak defined non function symbols from | |
4191 | a dynamic object, using the weakdef field. Later in this | |
4192 | function we will set the weakdef field to the correct | |
4193 | value. We only put non-function symbols from dynamic | |
4194 | objects on this list, because that happens to be the only | |
4195 | time we need to know the normal symbol corresponding to a | |
4196 | weak symbol, and the information is time consuming to | |
4197 | figure out. If the weakdef field is not already NULL, | |
4198 | then this symbol was already defined by some previous | |
4199 | dynamic object, and we will be using that previous | |
4200 | definition anyhow. */ | |
4201 | ||
f6e332e6 | 4202 | h->u.weakdef = weaks; |
4ad4eba5 AM |
4203 | weaks = h; |
4204 | new_weakdef = TRUE; | |
4205 | } | |
4206 | ||
4207 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4208 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4209 | && h->root.type == bfd_link_hash_common) |
4210 | { | |
4211 | unsigned int align; | |
4212 | ||
a4d8e49b | 4213 | if (common) |
af44c138 L |
4214 | align = bfd_log2 (isym->st_value); |
4215 | else | |
4216 | { | |
4217 | /* The new symbol is a common symbol in a shared object. | |
4218 | We need to get the alignment from the section. */ | |
4219 | align = new_sec->alignment_power; | |
4220 | } | |
595213d4 | 4221 | if (align > old_alignment) |
4ad4eba5 AM |
4222 | h->root.u.c.p->alignment_power = align; |
4223 | else | |
4224 | h->root.u.c.p->alignment_power = old_alignment; | |
4225 | } | |
4226 | ||
66eb6687 | 4227 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4228 | { |
4f3fedcf AM |
4229 | /* Set a flag in the hash table entry indicating the type of |
4230 | reference or definition we just found. A dynamic symbol | |
4231 | is one which is referenced or defined by both a regular | |
4232 | object and a shared object. */ | |
4233 | bfd_boolean dynsym = FALSE; | |
4234 | ||
4235 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4236 | ref_regular for them, or make them dynamic. */ | |
4237 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4238 | ; | |
4239 | else if (! dynamic) | |
4240 | { | |
4241 | if (! definition) | |
4242 | { | |
4243 | h->ref_regular = 1; | |
4244 | if (bind != STB_WEAK) | |
4245 | h->ref_regular_nonweak = 1; | |
4246 | } | |
4247 | else | |
4248 | { | |
4249 | h->def_regular = 1; | |
4250 | if (h->def_dynamic) | |
4251 | { | |
4252 | h->def_dynamic = 0; | |
4253 | h->ref_dynamic = 1; | |
4254 | } | |
4255 | } | |
4256 | ||
4257 | /* If the indirect symbol has been forced local, don't | |
4258 | make the real symbol dynamic. */ | |
4259 | if ((h == hi || !hi->forced_local) | |
4260 | && (! info->executable | |
4261 | || h->def_dynamic | |
4262 | || h->ref_dynamic)) | |
4263 | dynsym = TRUE; | |
4264 | } | |
4265 | else | |
4266 | { | |
4267 | if (! definition) | |
4268 | { | |
4269 | h->ref_dynamic = 1; | |
4270 | hi->ref_dynamic = 1; | |
4271 | } | |
4272 | else | |
4273 | { | |
4274 | h->def_dynamic = 1; | |
4275 | hi->def_dynamic = 1; | |
4276 | } | |
4277 | ||
4278 | /* If the indirect symbol has been forced local, don't | |
4279 | make the real symbol dynamic. */ | |
4280 | if ((h == hi || !hi->forced_local) | |
4281 | && (h->def_regular | |
4282 | || h->ref_regular | |
4283 | || (h->u.weakdef != NULL | |
4284 | && ! new_weakdef | |
4285 | && h->u.weakdef->dynindx != -1))) | |
4286 | dynsym = TRUE; | |
4287 | } | |
4288 | ||
4289 | /* Check to see if we need to add an indirect symbol for | |
4290 | the default name. */ | |
4291 | if (definition | |
4292 | || (!override && h->root.type == bfd_link_hash_common)) | |
4293 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4294 | sec, value, &old_bfd, &dynsym)) | |
4295 | goto error_free_vers; | |
4ad4eba5 AM |
4296 | |
4297 | /* Check the alignment when a common symbol is involved. This | |
4298 | can change when a common symbol is overridden by a normal | |
4299 | definition or a common symbol is ignored due to the old | |
4300 | normal definition. We need to make sure the maximum | |
4301 | alignment is maintained. */ | |
a4d8e49b | 4302 | if ((old_alignment || common) |
4ad4eba5 AM |
4303 | && h->root.type != bfd_link_hash_common) |
4304 | { | |
4305 | unsigned int common_align; | |
4306 | unsigned int normal_align; | |
4307 | unsigned int symbol_align; | |
4308 | bfd *normal_bfd; | |
4309 | bfd *common_bfd; | |
4310 | ||
3a81e825 AM |
4311 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4312 | || h->root.type == bfd_link_hash_defweak); | |
4313 | ||
4ad4eba5 AM |
4314 | symbol_align = ffs (h->root.u.def.value) - 1; |
4315 | if (h->root.u.def.section->owner != NULL | |
4316 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
4317 | { | |
4318 | normal_align = h->root.u.def.section->alignment_power; | |
4319 | if (normal_align > symbol_align) | |
4320 | normal_align = symbol_align; | |
4321 | } | |
4322 | else | |
4323 | normal_align = symbol_align; | |
4324 | ||
4325 | if (old_alignment) | |
4326 | { | |
4327 | common_align = old_alignment; | |
4328 | common_bfd = old_bfd; | |
4329 | normal_bfd = abfd; | |
4330 | } | |
4331 | else | |
4332 | { | |
4333 | common_align = bfd_log2 (isym->st_value); | |
4334 | common_bfd = abfd; | |
4335 | normal_bfd = old_bfd; | |
4336 | } | |
4337 | ||
4338 | if (normal_align < common_align) | |
d07676f8 NC |
4339 | { |
4340 | /* PR binutils/2735 */ | |
4341 | if (normal_bfd == NULL) | |
4342 | (*_bfd_error_handler) | |
4f3fedcf AM |
4343 | (_("Warning: alignment %u of common symbol `%s' in %B is" |
4344 | " greater than the alignment (%u) of its section %A"), | |
d07676f8 NC |
4345 | common_bfd, h->root.u.def.section, |
4346 | 1 << common_align, name, 1 << normal_align); | |
4347 | else | |
4348 | (*_bfd_error_handler) | |
4349 | (_("Warning: alignment %u of symbol `%s' in %B" | |
4350 | " is smaller than %u in %B"), | |
4351 | normal_bfd, common_bfd, | |
4352 | 1 << normal_align, name, 1 << common_align); | |
4353 | } | |
4ad4eba5 AM |
4354 | } |
4355 | ||
83ad0046 | 4356 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4357 | if (isym->st_size != 0 |
4358 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4359 | && (definition || h->size == 0)) |
4360 | { | |
83ad0046 L |
4361 | if (h->size != 0 |
4362 | && h->size != isym->st_size | |
4363 | && ! size_change_ok) | |
4ad4eba5 | 4364 | (*_bfd_error_handler) |
d003868e AM |
4365 | (_("Warning: size of symbol `%s' changed" |
4366 | " from %lu in %B to %lu in %B"), | |
4367 | old_bfd, abfd, | |
4ad4eba5 | 4368 | name, (unsigned long) h->size, |
d003868e | 4369 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
4370 | |
4371 | h->size = isym->st_size; | |
4372 | } | |
4373 | ||
4374 | /* If this is a common symbol, then we always want H->SIZE | |
4375 | to be the size of the common symbol. The code just above | |
4376 | won't fix the size if a common symbol becomes larger. We | |
4377 | don't warn about a size change here, because that is | |
4f3fedcf | 4378 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4379 | function types. */ |
4ad4eba5 AM |
4380 | if (h->root.type == bfd_link_hash_common) |
4381 | h->size = h->root.u.c.size; | |
4382 | ||
4383 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4384 | && ((definition && !new_weak) |
4385 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4386 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4387 | { |
2955ec4c L |
4388 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4389 | ||
4390 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4391 | symbol. */ | |
4392 | if (type == STT_GNU_IFUNC | |
4393 | && (abfd->flags & DYNAMIC) != 0) | |
4394 | type = STT_FUNC; | |
4ad4eba5 | 4395 | |
2955ec4c L |
4396 | if (h->type != type) |
4397 | { | |
4398 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
4399 | (*_bfd_error_handler) | |
4400 | (_("Warning: type of symbol `%s' changed" | |
4401 | " from %d to %d in %B"), | |
4402 | abfd, name, h->type, type); | |
4403 | ||
4404 | h->type = type; | |
4405 | } | |
4ad4eba5 AM |
4406 | } |
4407 | ||
54ac0771 | 4408 | /* Merge st_other field. */ |
b8417128 | 4409 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4410 | |
c3df8c14 | 4411 | /* We don't want to make debug symbol dynamic. */ |
b2064611 | 4412 | if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable) |
c3df8c14 AM |
4413 | dynsym = FALSE; |
4414 | ||
4f3fedcf AM |
4415 | /* Nor should we make plugin symbols dynamic. */ |
4416 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4417 | dynsym = FALSE; | |
4418 | ||
35fc36a8 | 4419 | if (definition) |
35399224 L |
4420 | { |
4421 | h->target_internal = isym->st_target_internal; | |
4422 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4423 | } | |
35fc36a8 | 4424 | |
4ad4eba5 AM |
4425 | if (definition && !dynamic) |
4426 | { | |
4427 | char *p = strchr (name, ELF_VER_CHR); | |
4428 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4429 | { | |
4430 | /* Queue non-default versions so that .symver x, x@FOO | |
4431 | aliases can be checked. */ | |
66eb6687 | 4432 | if (!nondeflt_vers) |
4ad4eba5 | 4433 | { |
66eb6687 AM |
4434 | amt = ((isymend - isym + 1) |
4435 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4436 | nondeflt_vers |
4437 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4438 | if (!nondeflt_vers) |
4439 | goto error_free_vers; | |
4ad4eba5 | 4440 | } |
66eb6687 | 4441 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4442 | } |
4443 | } | |
4444 | ||
4445 | if (dynsym && h->dynindx == -1) | |
4446 | { | |
c152c796 | 4447 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4448 | goto error_free_vers; |
f6e332e6 | 4449 | if (h->u.weakdef != NULL |
4ad4eba5 | 4450 | && ! new_weakdef |
f6e332e6 | 4451 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 4452 | { |
66eb6687 | 4453 | if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
4454 | goto error_free_vers; |
4455 | } | |
4456 | } | |
4457 | else if (dynsym && h->dynindx != -1) | |
4458 | /* If the symbol already has a dynamic index, but | |
4459 | visibility says it should not be visible, turn it into | |
4460 | a local symbol. */ | |
4461 | switch (ELF_ST_VISIBILITY (h->other)) | |
4462 | { | |
4463 | case STV_INTERNAL: | |
4464 | case STV_HIDDEN: | |
4465 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4466 | dynsym = FALSE; | |
4467 | break; | |
4468 | } | |
4469 | ||
3d5bef4c | 4470 | /* Don't add DT_NEEDED for references from the dummy bfd. */ |
4ad4eba5 AM |
4471 | if (!add_needed |
4472 | && definition | |
010e5ae2 | 4473 | && ((dynsym |
ffa9430d | 4474 | && h->ref_regular_nonweak |
4f3fedcf AM |
4475 | && (old_bfd == NULL |
4476 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 4477 | || (h->ref_dynamic_nonweak |
010e5ae2 AM |
4478 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
4479 | && !on_needed_list (elf_dt_name (abfd), htab->needed)))) | |
4ad4eba5 AM |
4480 | { |
4481 | int ret; | |
4482 | const char *soname = elf_dt_name (abfd); | |
4483 | ||
16e4ecc0 AM |
4484 | info->callbacks->minfo ("%!", soname, old_bfd, |
4485 | h->root.root.string); | |
4486 | ||
4ad4eba5 AM |
4487 | /* A symbol from a library loaded via DT_NEEDED of some |
4488 | other library is referenced by a regular object. | |
e56f61be | 4489 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
4490 | --no-add-needed is used and the reference was not |
4491 | a weak one. */ | |
4f3fedcf | 4492 | if (old_bfd != NULL |
b918acf9 | 4493 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be L |
4494 | { |
4495 | (*_bfd_error_handler) | |
3cbc5de0 | 4496 | (_("%B: undefined reference to symbol '%s'"), |
4f3fedcf | 4497 | old_bfd, name); |
ff5ac77b | 4498 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
4499 | goto error_free_vers; |
4500 | } | |
4501 | ||
a50b1753 | 4502 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 4503 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 4504 | |
4ad4eba5 | 4505 | add_needed = TRUE; |
7e9f0867 | 4506 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4507 | if (ret < 0) |
4508 | goto error_free_vers; | |
4509 | ||
4510 | BFD_ASSERT (ret == 0); | |
4511 | } | |
4512 | } | |
4513 | } | |
4514 | ||
66eb6687 AM |
4515 | if (extversym != NULL) |
4516 | { | |
4517 | free (extversym); | |
4518 | extversym = NULL; | |
4519 | } | |
4520 | ||
4521 | if (isymbuf != NULL) | |
4522 | { | |
4523 | free (isymbuf); | |
4524 | isymbuf = NULL; | |
4525 | } | |
4526 | ||
4527 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4528 | { | |
4529 | unsigned int i; | |
4530 | ||
4531 | /* Restore the symbol table. */ | |
f45794cb AM |
4532 | old_ent = (char *) old_tab + tabsize; |
4533 | memset (elf_sym_hashes (abfd), 0, | |
4534 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
4535 | htab->root.table.table = old_table; |
4536 | htab->root.table.size = old_size; | |
4537 | htab->root.table.count = old_count; | |
66eb6687 | 4538 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
4539 | htab->root.undefs = old_undefs; |
4540 | htab->root.undefs_tail = old_undefs_tail; | |
d45f8bda | 4541 | _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size); |
66eb6687 AM |
4542 | for (i = 0; i < htab->root.table.size; i++) |
4543 | { | |
4544 | struct bfd_hash_entry *p; | |
4545 | struct elf_link_hash_entry *h; | |
3e0882af L |
4546 | bfd_size_type size; |
4547 | unsigned int alignment_power; | |
66eb6687 AM |
4548 | |
4549 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4550 | { | |
4551 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
4552 | if (h->root.type == bfd_link_hash_warning) |
4553 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
a4542f1b AM |
4554 | if (h->dynindx >= old_dynsymcount |
4555 | && h->dynstr_index < old_dynstr_size) | |
66eb6687 | 4556 | _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); |
2de92251 | 4557 | |
3e0882af L |
4558 | /* Preserve the maximum alignment and size for common |
4559 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 4560 | since it can still be loaded at run time by another |
3e0882af L |
4561 | dynamic lib. */ |
4562 | if (h->root.type == bfd_link_hash_common) | |
4563 | { | |
4564 | size = h->root.u.c.size; | |
4565 | alignment_power = h->root.u.c.p->alignment_power; | |
4566 | } | |
4567 | else | |
4568 | { | |
4569 | size = 0; | |
4570 | alignment_power = 0; | |
4571 | } | |
66eb6687 AM |
4572 | memcpy (p, old_ent, htab->root.table.entsize); |
4573 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4574 | h = (struct elf_link_hash_entry *) p; |
4575 | if (h->root.type == bfd_link_hash_warning) | |
4576 | { | |
4577 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
4578 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 4579 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 4580 | } |
a4542f1b | 4581 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
4582 | { |
4583 | if (size > h->root.u.c.size) | |
4584 | h->root.u.c.size = size; | |
4585 | if (alignment_power > h->root.u.c.p->alignment_power) | |
4586 | h->root.u.c.p->alignment_power = alignment_power; | |
4587 | } | |
66eb6687 AM |
4588 | } |
4589 | } | |
4590 | ||
5061a885 AM |
4591 | /* Make a special call to the linker "notice" function to |
4592 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 4593 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 4594 | goto error_free_vers; |
5061a885 | 4595 | |
66eb6687 AM |
4596 | free (old_tab); |
4597 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
4598 | alloc_mark); | |
4599 | if (nondeflt_vers != NULL) | |
4600 | free (nondeflt_vers); | |
4601 | return TRUE; | |
4602 | } | |
2de92251 | 4603 | |
66eb6687 AM |
4604 | if (old_tab != NULL) |
4605 | { | |
e5034e59 | 4606 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 4607 | goto error_free_vers; |
66eb6687 AM |
4608 | free (old_tab); |
4609 | old_tab = NULL; | |
4610 | } | |
4611 | ||
4ad4eba5 AM |
4612 | /* Now that all the symbols from this input file are created, handle |
4613 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
4614 | if (nondeflt_vers != NULL) | |
4615 | { | |
4616 | bfd_size_type cnt, symidx; | |
4617 | ||
4618 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
4619 | { | |
4620 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
4621 | char *shortname, *p; | |
4622 | ||
4623 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
4624 | if (p == NULL | |
4625 | || (h->root.type != bfd_link_hash_defined | |
4626 | && h->root.type != bfd_link_hash_defweak)) | |
4627 | continue; | |
4628 | ||
4629 | amt = p - h->root.root.string; | |
a50b1753 | 4630 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
4631 | if (!shortname) |
4632 | goto error_free_vers; | |
4ad4eba5 AM |
4633 | memcpy (shortname, h->root.root.string, amt); |
4634 | shortname[amt] = '\0'; | |
4635 | ||
4636 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 4637 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
4638 | FALSE, FALSE, FALSE); |
4639 | if (hi != NULL | |
4640 | && hi->root.type == h->root.type | |
4641 | && hi->root.u.def.value == h->root.u.def.value | |
4642 | && hi->root.u.def.section == h->root.u.def.section) | |
4643 | { | |
4644 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
4645 | hi->root.type = bfd_link_hash_indirect; | |
4646 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 4647 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
4648 | sym_hash = elf_sym_hashes (abfd); |
4649 | if (sym_hash) | |
4650 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
4651 | if (sym_hash[symidx] == hi) | |
4652 | { | |
4653 | sym_hash[symidx] = h; | |
4654 | break; | |
4655 | } | |
4656 | } | |
4657 | free (shortname); | |
4658 | } | |
4659 | free (nondeflt_vers); | |
4660 | nondeflt_vers = NULL; | |
4661 | } | |
4662 | ||
4ad4eba5 AM |
4663 | /* Now set the weakdefs field correctly for all the weak defined |
4664 | symbols we found. The only way to do this is to search all the | |
4665 | symbols. Since we only need the information for non functions in | |
4666 | dynamic objects, that's the only time we actually put anything on | |
4667 | the list WEAKS. We need this information so that if a regular | |
4668 | object refers to a symbol defined weakly in a dynamic object, the | |
4669 | real symbol in the dynamic object is also put in the dynamic | |
4670 | symbols; we also must arrange for both symbols to point to the | |
4671 | same memory location. We could handle the general case of symbol | |
4672 | aliasing, but a general symbol alias can only be generated in | |
4673 | assembler code, handling it correctly would be very time | |
4674 | consuming, and other ELF linkers don't handle general aliasing | |
4675 | either. */ | |
4676 | if (weaks != NULL) | |
4677 | { | |
4678 | struct elf_link_hash_entry **hpp; | |
4679 | struct elf_link_hash_entry **hppend; | |
4680 | struct elf_link_hash_entry **sorted_sym_hash; | |
4681 | struct elf_link_hash_entry *h; | |
4682 | size_t sym_count; | |
4683 | ||
4684 | /* Since we have to search the whole symbol list for each weak | |
4685 | defined symbol, search time for N weak defined symbols will be | |
4686 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4687 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 4688 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
4689 | if (sorted_sym_hash == NULL) |
4690 | goto error_return; | |
4691 | sym_hash = sorted_sym_hash; | |
4692 | hpp = elf_sym_hashes (abfd); | |
4693 | hppend = hpp + extsymcount; | |
4694 | sym_count = 0; | |
4695 | for (; hpp < hppend; hpp++) | |
4696 | { | |
4697 | h = *hpp; | |
4698 | if (h != NULL | |
4699 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 4700 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
4701 | { |
4702 | *sym_hash = h; | |
4703 | sym_hash++; | |
4704 | sym_count++; | |
4705 | } | |
4706 | } | |
4707 | ||
4708 | qsort (sorted_sym_hash, sym_count, | |
4709 | sizeof (struct elf_link_hash_entry *), | |
4710 | elf_sort_symbol); | |
4711 | ||
4712 | while (weaks != NULL) | |
4713 | { | |
4714 | struct elf_link_hash_entry *hlook; | |
4715 | asection *slook; | |
4716 | bfd_vma vlook; | |
ed54588d | 4717 | size_t i, j, idx = 0; |
4ad4eba5 AM |
4718 | |
4719 | hlook = weaks; | |
f6e332e6 AM |
4720 | weaks = hlook->u.weakdef; |
4721 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4722 | |
4723 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4724 | || hlook->root.type == bfd_link_hash_defweak | |
4725 | || hlook->root.type == bfd_link_hash_common | |
4726 | || hlook->root.type == bfd_link_hash_indirect); | |
4727 | slook = hlook->root.u.def.section; | |
4728 | vlook = hlook->root.u.def.value; | |
4729 | ||
4ad4eba5 AM |
4730 | i = 0; |
4731 | j = sym_count; | |
14160578 | 4732 | while (i != j) |
4ad4eba5 AM |
4733 | { |
4734 | bfd_signed_vma vdiff; | |
4735 | idx = (i + j) / 2; | |
14160578 | 4736 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
4737 | vdiff = vlook - h->root.u.def.value; |
4738 | if (vdiff < 0) | |
4739 | j = idx; | |
4740 | else if (vdiff > 0) | |
4741 | i = idx + 1; | |
4742 | else | |
4743 | { | |
a9b881be | 4744 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4745 | if (sdiff < 0) |
4746 | j = idx; | |
4747 | else if (sdiff > 0) | |
4748 | i = idx + 1; | |
4749 | else | |
14160578 | 4750 | break; |
4ad4eba5 AM |
4751 | } |
4752 | } | |
4753 | ||
4754 | /* We didn't find a value/section match. */ | |
14160578 | 4755 | if (i == j) |
4ad4eba5 AM |
4756 | continue; |
4757 | ||
14160578 AM |
4758 | /* With multiple aliases, or when the weak symbol is already |
4759 | strongly defined, we have multiple matching symbols and | |
4760 | the binary search above may land on any of them. Step | |
4761 | one past the matching symbol(s). */ | |
4762 | while (++idx != j) | |
4763 | { | |
4764 | h = sorted_sym_hash[idx]; | |
4765 | if (h->root.u.def.section != slook | |
4766 | || h->root.u.def.value != vlook) | |
4767 | break; | |
4768 | } | |
4769 | ||
4770 | /* Now look back over the aliases. Since we sorted by size | |
4771 | as well as value and section, we'll choose the one with | |
4772 | the largest size. */ | |
4773 | while (idx-- != i) | |
4ad4eba5 | 4774 | { |
14160578 | 4775 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
4776 | |
4777 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
4778 | if (h->root.u.def.section != slook |
4779 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
4780 | break; |
4781 | else if (h != hlook) | |
4782 | { | |
f6e332e6 | 4783 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4784 | |
4785 | /* If the weak definition is in the list of dynamic | |
4786 | symbols, make sure the real definition is put | |
4787 | there as well. */ | |
4788 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4789 | { | |
c152c796 | 4790 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
4791 | { |
4792 | err_free_sym_hash: | |
4793 | free (sorted_sym_hash); | |
4794 | goto error_return; | |
4795 | } | |
4ad4eba5 AM |
4796 | } |
4797 | ||
4798 | /* If the real definition is in the list of dynamic | |
4799 | symbols, make sure the weak definition is put | |
4800 | there as well. If we don't do this, then the | |
4801 | dynamic loader might not merge the entries for the | |
4802 | real definition and the weak definition. */ | |
4803 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4804 | { | |
c152c796 | 4805 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 4806 | goto err_free_sym_hash; |
4ad4eba5 AM |
4807 | } |
4808 | break; | |
4809 | } | |
4810 | } | |
4811 | } | |
4812 | ||
4813 | free (sorted_sym_hash); | |
4814 | } | |
4815 | ||
33177bb1 AM |
4816 | if (bed->check_directives |
4817 | && !(*bed->check_directives) (abfd, info)) | |
4818 | return FALSE; | |
85fbca6a | 4819 | |
4ad4eba5 AM |
4820 | /* If this object is the same format as the output object, and it is |
4821 | not a shared library, then let the backend look through the | |
4822 | relocs. | |
4823 | ||
4824 | This is required to build global offset table entries and to | |
4825 | arrange for dynamic relocs. It is not required for the | |
4826 | particular common case of linking non PIC code, even when linking | |
4827 | against shared libraries, but unfortunately there is no way of | |
4828 | knowing whether an object file has been compiled PIC or not. | |
4829 | Looking through the relocs is not particularly time consuming. | |
4830 | The problem is that we must either (1) keep the relocs in memory, | |
4831 | which causes the linker to require additional runtime memory or | |
4832 | (2) read the relocs twice from the input file, which wastes time. | |
4833 | This would be a good case for using mmap. | |
4834 | ||
4835 | I have no idea how to handle linking PIC code into a file of a | |
4836 | different format. It probably can't be done. */ | |
4ad4eba5 | 4837 | if (! dynamic |
66eb6687 | 4838 | && is_elf_hash_table (htab) |
13285a1b | 4839 | && bed->check_relocs != NULL |
39334f3a | 4840 | && elf_object_id (abfd) == elf_hash_table_id (htab) |
f13a99db | 4841 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) |
4ad4eba5 AM |
4842 | { |
4843 | asection *o; | |
4844 | ||
4845 | for (o = abfd->sections; o != NULL; o = o->next) | |
4846 | { | |
4847 | Elf_Internal_Rela *internal_relocs; | |
4848 | bfd_boolean ok; | |
4849 | ||
4850 | if ((o->flags & SEC_RELOC) == 0 | |
4851 | || o->reloc_count == 0 | |
4852 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4853 | && (o->flags & SEC_DEBUGGING) != 0) | |
4854 | || bfd_is_abs_section (o->output_section)) | |
4855 | continue; | |
4856 | ||
4857 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4858 | info->keep_memory); | |
4859 | if (internal_relocs == NULL) | |
4860 | goto error_return; | |
4861 | ||
66eb6687 | 4862 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); |
4ad4eba5 AM |
4863 | |
4864 | if (elf_section_data (o)->relocs != internal_relocs) | |
4865 | free (internal_relocs); | |
4866 | ||
4867 | if (! ok) | |
4868 | goto error_return; | |
4869 | } | |
4870 | } | |
4871 | ||
4872 | /* If this is a non-traditional link, try to optimize the handling | |
4873 | of the .stab/.stabstr sections. */ | |
4874 | if (! dynamic | |
4875 | && ! info->traditional_format | |
66eb6687 | 4876 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
4877 | && (info->strip != strip_all && info->strip != strip_debugger)) |
4878 | { | |
4879 | asection *stabstr; | |
4880 | ||
4881 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4882 | if (stabstr != NULL) | |
4883 | { | |
4884 | bfd_size_type string_offset = 0; | |
4885 | asection *stab; | |
4886 | ||
4887 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 4888 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
4889 | && (!stab->name[5] || |
4890 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4891 | && (stab->flags & SEC_MERGE) == 0 | |
4892 | && !bfd_is_abs_section (stab->output_section)) | |
4893 | { | |
4894 | struct bfd_elf_section_data *secdata; | |
4895 | ||
4896 | secdata = elf_section_data (stab); | |
66eb6687 AM |
4897 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
4898 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
4899 | &string_offset)) |
4900 | goto error_return; | |
4901 | if (secdata->sec_info) | |
dbaa2011 | 4902 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
4903 | } |
4904 | } | |
4905 | } | |
4906 | ||
66eb6687 | 4907 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
4908 | { |
4909 | /* Add this bfd to the loaded list. */ | |
4910 | struct elf_link_loaded_list *n; | |
4911 | ||
ca4be51c | 4912 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
4913 | if (n == NULL) |
4914 | goto error_return; | |
4915 | n->abfd = abfd; | |
66eb6687 AM |
4916 | n->next = htab->loaded; |
4917 | htab->loaded = n; | |
4ad4eba5 AM |
4918 | } |
4919 | ||
4920 | return TRUE; | |
4921 | ||
4922 | error_free_vers: | |
66eb6687 AM |
4923 | if (old_tab != NULL) |
4924 | free (old_tab); | |
4ad4eba5 AM |
4925 | if (nondeflt_vers != NULL) |
4926 | free (nondeflt_vers); | |
4927 | if (extversym != NULL) | |
4928 | free (extversym); | |
4929 | error_free_sym: | |
4930 | if (isymbuf != NULL) | |
4931 | free (isymbuf); | |
4932 | error_return: | |
4933 | return FALSE; | |
4934 | } | |
4935 | ||
8387904d AM |
4936 | /* Return the linker hash table entry of a symbol that might be |
4937 | satisfied by an archive symbol. Return -1 on error. */ | |
4938 | ||
4939 | struct elf_link_hash_entry * | |
4940 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4941 | struct bfd_link_info *info, | |
4942 | const char *name) | |
4943 | { | |
4944 | struct elf_link_hash_entry *h; | |
4945 | char *p, *copy; | |
4946 | size_t len, first; | |
4947 | ||
2a41f396 | 4948 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
4949 | if (h != NULL) |
4950 | return h; | |
4951 | ||
4952 | /* If this is a default version (the name contains @@), look up the | |
4953 | symbol again with only one `@' as well as without the version. | |
4954 | The effect is that references to the symbol with and without the | |
4955 | version will be matched by the default symbol in the archive. */ | |
4956 | ||
4957 | p = strchr (name, ELF_VER_CHR); | |
4958 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4959 | return h; | |
4960 | ||
4961 | /* First check with only one `@'. */ | |
4962 | len = strlen (name); | |
a50b1753 | 4963 | copy = (char *) bfd_alloc (abfd, len); |
8387904d AM |
4964 | if (copy == NULL) |
4965 | return (struct elf_link_hash_entry *) 0 - 1; | |
4966 | ||
4967 | first = p - name + 1; | |
4968 | memcpy (copy, name, first); | |
4969 | memcpy (copy + first, name + first + 1, len - first); | |
4970 | ||
2a41f396 | 4971 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
4972 | if (h == NULL) |
4973 | { | |
4974 | /* We also need to check references to the symbol without the | |
4975 | version. */ | |
4976 | copy[first - 1] = '\0'; | |
4977 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 4978 | FALSE, FALSE, TRUE); |
8387904d AM |
4979 | } |
4980 | ||
4981 | bfd_release (abfd, copy); | |
4982 | return h; | |
4983 | } | |
4984 | ||
0ad989f9 | 4985 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
4986 | don't use _bfd_generic_link_add_archive_symbols because we need to |
4987 | handle versioned symbols. | |
0ad989f9 L |
4988 | |
4989 | Fortunately, ELF archive handling is simpler than that done by | |
4990 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4991 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4992 | symbol is currently undefined, we know that we must pull in that | |
4993 | object file. | |
4994 | ||
4995 | Unfortunately, we do have to make multiple passes over the symbol | |
4996 | table until nothing further is resolved. */ | |
4997 | ||
4ad4eba5 AM |
4998 | static bfd_boolean |
4999 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5000 | { |
5001 | symindex c; | |
13e570f8 | 5002 | unsigned char *included = NULL; |
0ad989f9 L |
5003 | carsym *symdefs; |
5004 | bfd_boolean loop; | |
5005 | bfd_size_type amt; | |
8387904d AM |
5006 | const struct elf_backend_data *bed; |
5007 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5008 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5009 | |
5010 | if (! bfd_has_map (abfd)) | |
5011 | { | |
5012 | /* An empty archive is a special case. */ | |
5013 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5014 | return TRUE; | |
5015 | bfd_set_error (bfd_error_no_armap); | |
5016 | return FALSE; | |
5017 | } | |
5018 | ||
5019 | /* Keep track of all symbols we know to be already defined, and all | |
5020 | files we know to be already included. This is to speed up the | |
5021 | second and subsequent passes. */ | |
5022 | c = bfd_ardata (abfd)->symdef_count; | |
5023 | if (c == 0) | |
5024 | return TRUE; | |
5025 | amt = c; | |
13e570f8 AM |
5026 | amt *= sizeof (*included); |
5027 | included = (unsigned char *) bfd_zmalloc (amt); | |
5028 | if (included == NULL) | |
5029 | return FALSE; | |
0ad989f9 L |
5030 | |
5031 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5032 | bed = get_elf_backend_data (abfd); |
5033 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5034 | |
5035 | do | |
5036 | { | |
5037 | file_ptr last; | |
5038 | symindex i; | |
5039 | carsym *symdef; | |
5040 | carsym *symdefend; | |
5041 | ||
5042 | loop = FALSE; | |
5043 | last = -1; | |
5044 | ||
5045 | symdef = symdefs; | |
5046 | symdefend = symdef + c; | |
5047 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5048 | { | |
5049 | struct elf_link_hash_entry *h; | |
5050 | bfd *element; | |
5051 | struct bfd_link_hash_entry *undefs_tail; | |
5052 | symindex mark; | |
5053 | ||
13e570f8 | 5054 | if (included[i]) |
0ad989f9 L |
5055 | continue; |
5056 | if (symdef->file_offset == last) | |
5057 | { | |
5058 | included[i] = TRUE; | |
5059 | continue; | |
5060 | } | |
5061 | ||
8387904d AM |
5062 | h = archive_symbol_lookup (abfd, info, symdef->name); |
5063 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
5064 | goto error_return; | |
0ad989f9 L |
5065 | |
5066 | if (h == NULL) | |
5067 | continue; | |
5068 | ||
5069 | if (h->root.type == bfd_link_hash_common) | |
5070 | { | |
5071 | /* We currently have a common symbol. The archive map contains | |
5072 | a reference to this symbol, so we may want to include it. We | |
5073 | only want to include it however, if this archive element | |
5074 | contains a definition of the symbol, not just another common | |
5075 | declaration of it. | |
5076 | ||
5077 | Unfortunately some archivers (including GNU ar) will put | |
5078 | declarations of common symbols into their archive maps, as | |
5079 | well as real definitions, so we cannot just go by the archive | |
5080 | map alone. Instead we must read in the element's symbol | |
5081 | table and check that to see what kind of symbol definition | |
5082 | this is. */ | |
5083 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5084 | continue; | |
5085 | } | |
5086 | else if (h->root.type != bfd_link_hash_undefined) | |
5087 | { | |
5088 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5089 | /* Symbol must be defined. Don't check it again. */ |
5090 | included[i] = TRUE; | |
0ad989f9 L |
5091 | continue; |
5092 | } | |
5093 | ||
5094 | /* We need to include this archive member. */ | |
5095 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5096 | if (element == NULL) | |
5097 | goto error_return; | |
5098 | ||
5099 | if (! bfd_check_format (element, bfd_object)) | |
5100 | goto error_return; | |
5101 | ||
0ad989f9 L |
5102 | undefs_tail = info->hash->undefs_tail; |
5103 | ||
0e144ba7 AM |
5104 | if (!(*info->callbacks |
5105 | ->add_archive_element) (info, element, symdef->name, &element)) | |
0ad989f9 | 5106 | goto error_return; |
0e144ba7 | 5107 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5108 | goto error_return; |
5109 | ||
5110 | /* If there are any new undefined symbols, we need to make | |
5111 | another pass through the archive in order to see whether | |
5112 | they can be defined. FIXME: This isn't perfect, because | |
5113 | common symbols wind up on undefs_tail and because an | |
5114 | undefined symbol which is defined later on in this pass | |
5115 | does not require another pass. This isn't a bug, but it | |
5116 | does make the code less efficient than it could be. */ | |
5117 | if (undefs_tail != info->hash->undefs_tail) | |
5118 | loop = TRUE; | |
5119 | ||
5120 | /* Look backward to mark all symbols from this object file | |
5121 | which we have already seen in this pass. */ | |
5122 | mark = i; | |
5123 | do | |
5124 | { | |
5125 | included[mark] = TRUE; | |
5126 | if (mark == 0) | |
5127 | break; | |
5128 | --mark; | |
5129 | } | |
5130 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5131 | ||
5132 | /* We mark subsequent symbols from this object file as we go | |
5133 | on through the loop. */ | |
5134 | last = symdef->file_offset; | |
5135 | } | |
5136 | } | |
5137 | while (loop); | |
5138 | ||
0ad989f9 L |
5139 | free (included); |
5140 | ||
5141 | return TRUE; | |
5142 | ||
5143 | error_return: | |
0ad989f9 L |
5144 | if (included != NULL) |
5145 | free (included); | |
5146 | return FALSE; | |
5147 | } | |
4ad4eba5 AM |
5148 | |
5149 | /* Given an ELF BFD, add symbols to the global hash table as | |
5150 | appropriate. */ | |
5151 | ||
5152 | bfd_boolean | |
5153 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5154 | { | |
5155 | switch (bfd_get_format (abfd)) | |
5156 | { | |
5157 | case bfd_object: | |
5158 | return elf_link_add_object_symbols (abfd, info); | |
5159 | case bfd_archive: | |
5160 | return elf_link_add_archive_symbols (abfd, info); | |
5161 | default: | |
5162 | bfd_set_error (bfd_error_wrong_format); | |
5163 | return FALSE; | |
5164 | } | |
5165 | } | |
5a580b3a | 5166 | \f |
14b1c01e AM |
5167 | struct hash_codes_info |
5168 | { | |
5169 | unsigned long *hashcodes; | |
5170 | bfd_boolean error; | |
5171 | }; | |
a0c8462f | 5172 | |
5a580b3a AM |
5173 | /* This function will be called though elf_link_hash_traverse to store |
5174 | all hash value of the exported symbols in an array. */ | |
5175 | ||
5176 | static bfd_boolean | |
5177 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5178 | { | |
a50b1753 | 5179 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a AM |
5180 | const char *name; |
5181 | char *p; | |
5182 | unsigned long ha; | |
5183 | char *alc = NULL; | |
5184 | ||
5a580b3a AM |
5185 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5186 | if (h->dynindx == -1) | |
5187 | return TRUE; | |
5188 | ||
5189 | name = h->root.root.string; | |
5190 | p = strchr (name, ELF_VER_CHR); | |
5191 | if (p != NULL) | |
5192 | { | |
a50b1753 | 5193 | alc = (char *) bfd_malloc (p - name + 1); |
14b1c01e AM |
5194 | if (alc == NULL) |
5195 | { | |
5196 | inf->error = TRUE; | |
5197 | return FALSE; | |
5198 | } | |
5a580b3a AM |
5199 | memcpy (alc, name, p - name); |
5200 | alc[p - name] = '\0'; | |
5201 | name = alc; | |
5202 | } | |
5203 | ||
5204 | /* Compute the hash value. */ | |
5205 | ha = bfd_elf_hash (name); | |
5206 | ||
5207 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5208 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5209 | |
5210 | /* And store it in the struct so that we can put it in the hash table | |
5211 | later. */ | |
f6e332e6 | 5212 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5213 | |
5214 | if (alc != NULL) | |
5215 | free (alc); | |
5216 | ||
5217 | return TRUE; | |
5218 | } | |
5219 | ||
fdc90cb4 JJ |
5220 | struct collect_gnu_hash_codes |
5221 | { | |
5222 | bfd *output_bfd; | |
5223 | const struct elf_backend_data *bed; | |
5224 | unsigned long int nsyms; | |
5225 | unsigned long int maskbits; | |
5226 | unsigned long int *hashcodes; | |
5227 | unsigned long int *hashval; | |
5228 | unsigned long int *indx; | |
5229 | unsigned long int *counts; | |
5230 | bfd_vma *bitmask; | |
5231 | bfd_byte *contents; | |
5232 | long int min_dynindx; | |
5233 | unsigned long int bucketcount; | |
5234 | unsigned long int symindx; | |
5235 | long int local_indx; | |
5236 | long int shift1, shift2; | |
5237 | unsigned long int mask; | |
14b1c01e | 5238 | bfd_boolean error; |
fdc90cb4 JJ |
5239 | }; |
5240 | ||
5241 | /* This function will be called though elf_link_hash_traverse to store | |
5242 | all hash value of the exported symbols in an array. */ | |
5243 | ||
5244 | static bfd_boolean | |
5245 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5246 | { | |
a50b1753 | 5247 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5248 | const char *name; |
5249 | char *p; | |
5250 | unsigned long ha; | |
5251 | char *alc = NULL; | |
5252 | ||
fdc90cb4 JJ |
5253 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5254 | if (h->dynindx == -1) | |
5255 | return TRUE; | |
5256 | ||
5257 | /* Ignore also local symbols and undefined symbols. */ | |
5258 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5259 | return TRUE; | |
5260 | ||
5261 | name = h->root.root.string; | |
5262 | p = strchr (name, ELF_VER_CHR); | |
5263 | if (p != NULL) | |
5264 | { | |
a50b1753 | 5265 | alc = (char *) bfd_malloc (p - name + 1); |
14b1c01e AM |
5266 | if (alc == NULL) |
5267 | { | |
5268 | s->error = TRUE; | |
5269 | return FALSE; | |
5270 | } | |
fdc90cb4 JJ |
5271 | memcpy (alc, name, p - name); |
5272 | alc[p - name] = '\0'; | |
5273 | name = alc; | |
5274 | } | |
5275 | ||
5276 | /* Compute the hash value. */ | |
5277 | ha = bfd_elf_gnu_hash (name); | |
5278 | ||
5279 | /* Store the found hash value in the array for compute_bucket_count, | |
5280 | and also for .dynsym reordering purposes. */ | |
5281 | s->hashcodes[s->nsyms] = ha; | |
5282 | s->hashval[h->dynindx] = ha; | |
5283 | ++s->nsyms; | |
5284 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5285 | s->min_dynindx = h->dynindx; | |
5286 | ||
5287 | if (alc != NULL) | |
5288 | free (alc); | |
5289 | ||
5290 | return TRUE; | |
5291 | } | |
5292 | ||
5293 | /* This function will be called though elf_link_hash_traverse to do | |
5294 | final dynaminc symbol renumbering. */ | |
5295 | ||
5296 | static bfd_boolean | |
5297 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5298 | { | |
a50b1753 | 5299 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5300 | unsigned long int bucket; |
5301 | unsigned long int val; | |
5302 | ||
fdc90cb4 JJ |
5303 | /* Ignore indirect symbols. */ |
5304 | if (h->dynindx == -1) | |
5305 | return TRUE; | |
5306 | ||
5307 | /* Ignore also local symbols and undefined symbols. */ | |
5308 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5309 | { | |
5310 | if (h->dynindx >= s->min_dynindx) | |
5311 | h->dynindx = s->local_indx++; | |
5312 | return TRUE; | |
5313 | } | |
5314 | ||
5315 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5316 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5317 | & ((s->maskbits >> s->shift1) - 1); | |
5318 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5319 | s->bitmask[val] | |
5320 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5321 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5322 | if (s->counts[bucket] == 1) | |
5323 | /* Last element terminates the chain. */ | |
5324 | val |= 1; | |
5325 | bfd_put_32 (s->output_bfd, val, | |
5326 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5327 | --s->counts[bucket]; | |
5328 | h->dynindx = s->indx[bucket]++; | |
5329 | return TRUE; | |
5330 | } | |
5331 | ||
5332 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5333 | ||
5334 | bfd_boolean | |
5335 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5336 | { | |
5337 | return !(h->forced_local | |
5338 | || h->root.type == bfd_link_hash_undefined | |
5339 | || h->root.type == bfd_link_hash_undefweak | |
5340 | || ((h->root.type == bfd_link_hash_defined | |
5341 | || h->root.type == bfd_link_hash_defweak) | |
5342 | && h->root.u.def.section->output_section == NULL)); | |
5343 | } | |
5344 | ||
5a580b3a AM |
5345 | /* Array used to determine the number of hash table buckets to use |
5346 | based on the number of symbols there are. If there are fewer than | |
5347 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5348 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5349 | than 32771 buckets. */ | |
5350 | ||
5351 | static const size_t elf_buckets[] = | |
5352 | { | |
5353 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5354 | 16411, 32771, 0 | |
5355 | }; | |
5356 | ||
5357 | /* Compute bucket count for hashing table. We do not use a static set | |
5358 | of possible tables sizes anymore. Instead we determine for all | |
5359 | possible reasonable sizes of the table the outcome (i.e., the | |
5360 | number of collisions etc) and choose the best solution. The | |
5361 | weighting functions are not too simple to allow the table to grow | |
5362 | without bounds. Instead one of the weighting factors is the size. | |
5363 | Therefore the result is always a good payoff between few collisions | |
5364 | (= short chain lengths) and table size. */ | |
5365 | static size_t | |
b20dd2ce | 5366 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5367 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5368 | unsigned long int nsyms, | |
5369 | int gnu_hash) | |
5a580b3a | 5370 | { |
5a580b3a | 5371 | size_t best_size = 0; |
5a580b3a | 5372 | unsigned long int i; |
5a580b3a | 5373 | |
5a580b3a AM |
5374 | /* We have a problem here. The following code to optimize the table |
5375 | size requires an integer type with more the 32 bits. If | |
5376 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5377 | #ifdef BFD_HOST_U_64_BIT | |
5378 | if (info->optimize) | |
5379 | { | |
5a580b3a AM |
5380 | size_t minsize; |
5381 | size_t maxsize; | |
5382 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5383 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5384 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5385 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5386 | unsigned long int *counts; |
d40f3da9 | 5387 | bfd_size_type amt; |
0883b6e0 | 5388 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5389 | |
5390 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5391 | that the hashing table must at least have NSYMS/4 and at most | |
5392 | 2*NSYMS buckets. */ | |
5393 | minsize = nsyms / 4; | |
5394 | if (minsize == 0) | |
5395 | minsize = 1; | |
5396 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5397 | if (gnu_hash) |
5398 | { | |
5399 | if (minsize < 2) | |
5400 | minsize = 2; | |
5401 | if ((best_size & 31) == 0) | |
5402 | ++best_size; | |
5403 | } | |
5a580b3a AM |
5404 | |
5405 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5406 | since the size could be large. */ | |
5407 | amt = maxsize; | |
5408 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5409 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5410 | if (counts == NULL) |
fdc90cb4 | 5411 | return 0; |
5a580b3a AM |
5412 | |
5413 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5414 | minimal chain length. The minor criteria is (of course) the size | |
5415 | of the table. */ | |
5416 | for (i = minsize; i < maxsize; ++i) | |
5417 | { | |
5418 | /* Walk through the array of hashcodes and count the collisions. */ | |
5419 | BFD_HOST_U_64_BIT max; | |
5420 | unsigned long int j; | |
5421 | unsigned long int fact; | |
5422 | ||
fdc90cb4 JJ |
5423 | if (gnu_hash && (i & 31) == 0) |
5424 | continue; | |
5425 | ||
5a580b3a AM |
5426 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5427 | ||
5428 | /* Determine how often each hash bucket is used. */ | |
5429 | for (j = 0; j < nsyms; ++j) | |
5430 | ++counts[hashcodes[j] % i]; | |
5431 | ||
5432 | /* For the weight function we need some information about the | |
5433 | pagesize on the target. This is information need not be 100% | |
5434 | accurate. Since this information is not available (so far) we | |
5435 | define it here to a reasonable default value. If it is crucial | |
5436 | to have a better value some day simply define this value. */ | |
5437 | # ifndef BFD_TARGET_PAGESIZE | |
5438 | # define BFD_TARGET_PAGESIZE (4096) | |
5439 | # endif | |
5440 | ||
fdc90cb4 JJ |
5441 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5442 | and the chains. */ | |
5443 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5444 | |
5445 | # if 1 | |
5446 | /* Variant 1: optimize for short chains. We add the squares | |
5447 | of all the chain lengths (which favors many small chain | |
5448 | over a few long chains). */ | |
5449 | for (j = 0; j < i; ++j) | |
5450 | max += counts[j] * counts[j]; | |
5451 | ||
5452 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5453 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5454 | max *= fact * fact; |
5455 | # else | |
5456 | /* Variant 2: Optimize a lot more for small table. Here we | |
5457 | also add squares of the size but we also add penalties for | |
5458 | empty slots (the +1 term). */ | |
5459 | for (j = 0; j < i; ++j) | |
5460 | max += (1 + counts[j]) * (1 + counts[j]); | |
5461 | ||
5462 | /* The overall size of the table is considered, but not as | |
5463 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5464 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5465 | max *= fact; |
5466 | # endif | |
5467 | ||
5468 | /* Compare with current best results. */ | |
5469 | if (max < best_chlen) | |
5470 | { | |
5471 | best_chlen = max; | |
5472 | best_size = i; | |
ca4be51c | 5473 | no_improvement_count = 0; |
5a580b3a | 5474 | } |
0883b6e0 NC |
5475 | /* PR 11843: Avoid futile long searches for the best bucket size |
5476 | when there are a large number of symbols. */ | |
5477 | else if (++no_improvement_count == 100) | |
5478 | break; | |
5a580b3a AM |
5479 | } |
5480 | ||
5481 | free (counts); | |
5482 | } | |
5483 | else | |
5484 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5485 | { | |
5486 | /* This is the fallback solution if no 64bit type is available or if we | |
5487 | are not supposed to spend much time on optimizations. We select the | |
5488 | bucket count using a fixed set of numbers. */ | |
5489 | for (i = 0; elf_buckets[i] != 0; i++) | |
5490 | { | |
5491 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5492 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5493 | break; |
5494 | } | |
fdc90cb4 JJ |
5495 | if (gnu_hash && best_size < 2) |
5496 | best_size = 2; | |
5a580b3a AM |
5497 | } |
5498 | ||
5a580b3a AM |
5499 | return best_size; |
5500 | } | |
5501 | ||
d0bf826b AM |
5502 | /* Size any SHT_GROUP section for ld -r. */ |
5503 | ||
5504 | bfd_boolean | |
5505 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
5506 | { | |
5507 | bfd *ibfd; | |
5508 | ||
c72f2fb2 | 5509 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b AM |
5510 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
5511 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) | |
5512 | return FALSE; | |
5513 | return TRUE; | |
5514 | } | |
5515 | ||
04c3a755 NS |
5516 | /* Set a default stack segment size. The value in INFO wins. If it |
5517 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
5518 | undefined it is initialized. */ | |
5519 | ||
5520 | bfd_boolean | |
5521 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
5522 | struct bfd_link_info *info, | |
5523 | const char *legacy_symbol, | |
5524 | bfd_vma default_size) | |
5525 | { | |
5526 | struct elf_link_hash_entry *h = NULL; | |
5527 | ||
5528 | /* Look for legacy symbol. */ | |
5529 | if (legacy_symbol) | |
5530 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
5531 | FALSE, FALSE, FALSE); | |
5532 | if (h && (h->root.type == bfd_link_hash_defined | |
5533 | || h->root.type == bfd_link_hash_defweak) | |
5534 | && h->def_regular | |
5535 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
5536 | { | |
5537 | /* The symbol has no type if specified on the command line. */ | |
5538 | h->type = STT_OBJECT; | |
5539 | if (info->stacksize) | |
5540 | (*_bfd_error_handler) (_("%B: stack size specified and %s set"), | |
5541 | output_bfd, legacy_symbol); | |
5542 | else if (h->root.u.def.section != bfd_abs_section_ptr) | |
5543 | (*_bfd_error_handler) (_("%B: %s not absolute"), | |
5544 | output_bfd, legacy_symbol); | |
5545 | else | |
5546 | info->stacksize = h->root.u.def.value; | |
5547 | } | |
5548 | ||
5549 | if (!info->stacksize) | |
5550 | /* If the user didn't set a size, or explicitly inhibit the | |
5551 | size, set it now. */ | |
5552 | info->stacksize = default_size; | |
5553 | ||
5554 | /* Provide the legacy symbol, if it is referenced. */ | |
5555 | if (h && (h->root.type == bfd_link_hash_undefined | |
5556 | || h->root.type == bfd_link_hash_undefweak)) | |
5557 | { | |
5558 | struct bfd_link_hash_entry *bh = NULL; | |
5559 | ||
5560 | if (!(_bfd_generic_link_add_one_symbol | |
5561 | (info, output_bfd, legacy_symbol, | |
5562 | BSF_GLOBAL, bfd_abs_section_ptr, | |
5563 | info->stacksize >= 0 ? info->stacksize : 0, | |
5564 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
5565 | return FALSE; | |
5566 | ||
5567 | h = (struct elf_link_hash_entry *) bh; | |
5568 | h->def_regular = 1; | |
5569 | h->type = STT_OBJECT; | |
5570 | } | |
5571 | ||
5572 | return TRUE; | |
5573 | } | |
5574 | ||
5a580b3a AM |
5575 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
5576 | called by the ELF linker emulation before_allocation routine. We | |
5577 | must set the sizes of the sections before the linker sets the | |
5578 | addresses of the various sections. */ | |
5579 | ||
5580 | bfd_boolean | |
5581 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
5582 | const char *soname, | |
5583 | const char *rpath, | |
5584 | const char *filter_shlib, | |
7ee314fa AM |
5585 | const char *audit, |
5586 | const char *depaudit, | |
5a580b3a AM |
5587 | const char * const *auxiliary_filters, |
5588 | struct bfd_link_info *info, | |
fd91d419 | 5589 | asection **sinterpptr) |
5a580b3a AM |
5590 | { |
5591 | bfd_size_type soname_indx; | |
5592 | bfd *dynobj; | |
5593 | const struct elf_backend_data *bed; | |
28caa186 | 5594 | struct elf_info_failed asvinfo; |
5a580b3a AM |
5595 | |
5596 | *sinterpptr = NULL; | |
5597 | ||
5598 | soname_indx = (bfd_size_type) -1; | |
5599 | ||
5600 | if (!is_elf_hash_table (info->hash)) | |
5601 | return TRUE; | |
5602 | ||
6bfdb61b | 5603 | bed = get_elf_backend_data (output_bfd); |
04c3a755 NS |
5604 | |
5605 | /* Any syms created from now on start with -1 in | |
5606 | got.refcount/offset and plt.refcount/offset. */ | |
5607 | elf_hash_table (info)->init_got_refcount | |
5608 | = elf_hash_table (info)->init_got_offset; | |
5609 | elf_hash_table (info)->init_plt_refcount | |
5610 | = elf_hash_table (info)->init_plt_offset; | |
5611 | ||
5612 | if (info->relocatable | |
5613 | && !_bfd_elf_size_group_sections (info)) | |
5614 | return FALSE; | |
5615 | ||
5616 | /* The backend may have to create some sections regardless of whether | |
5617 | we're dynamic or not. */ | |
5618 | if (bed->elf_backend_always_size_sections | |
5619 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
5620 | return FALSE; | |
5621 | ||
5622 | /* Determine any GNU_STACK segment requirements, after the backend | |
5623 | has had a chance to set a default segment size. */ | |
5a580b3a | 5624 | if (info->execstack) |
12bd6957 | 5625 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; |
5a580b3a | 5626 | else if (info->noexecstack) |
12bd6957 | 5627 | elf_stack_flags (output_bfd) = PF_R | PF_W; |
5a580b3a AM |
5628 | else |
5629 | { | |
5630 | bfd *inputobj; | |
5631 | asection *notesec = NULL; | |
5632 | int exec = 0; | |
5633 | ||
5634 | for (inputobj = info->input_bfds; | |
5635 | inputobj; | |
c72f2fb2 | 5636 | inputobj = inputobj->link.next) |
5a580b3a AM |
5637 | { |
5638 | asection *s; | |
5639 | ||
a92c088a L |
5640 | if (inputobj->flags |
5641 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
5a580b3a AM |
5642 | continue; |
5643 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
5644 | if (s) | |
5645 | { | |
5646 | if (s->flags & SEC_CODE) | |
5647 | exec = PF_X; | |
5648 | notesec = s; | |
5649 | } | |
6bfdb61b | 5650 | else if (bed->default_execstack) |
5a580b3a AM |
5651 | exec = PF_X; |
5652 | } | |
04c3a755 | 5653 | if (notesec || info->stacksize > 0) |
12bd6957 | 5654 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; |
04c3a755 NS |
5655 | if (notesec && exec && info->relocatable |
5656 | && notesec->output_section != bfd_abs_section_ptr) | |
5657 | notesec->output_section->flags |= SEC_CODE; | |
5a580b3a AM |
5658 | } |
5659 | ||
5a580b3a AM |
5660 | dynobj = elf_hash_table (info)->dynobj; |
5661 | ||
9a2a56cc | 5662 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a AM |
5663 | { |
5664 | struct elf_info_failed eif; | |
5665 | struct elf_link_hash_entry *h; | |
5666 | asection *dynstr; | |
5667 | struct bfd_elf_version_tree *t; | |
5668 | struct bfd_elf_version_expr *d; | |
046183de | 5669 | asection *s; |
5a580b3a AM |
5670 | bfd_boolean all_defined; |
5671 | ||
3d4d4302 | 5672 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); |
5a580b3a AM |
5673 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); |
5674 | ||
5675 | if (soname != NULL) | |
5676 | { | |
5677 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5678 | soname, TRUE); | |
5679 | if (soname_indx == (bfd_size_type) -1 | |
5680 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
5681 | return FALSE; | |
5682 | } | |
5683 | ||
5684 | if (info->symbolic) | |
5685 | { | |
5686 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
5687 | return FALSE; | |
5688 | info->flags |= DF_SYMBOLIC; | |
5689 | } | |
5690 | ||
5691 | if (rpath != NULL) | |
5692 | { | |
5693 | bfd_size_type indx; | |
b1b00fcc | 5694 | bfd_vma tag; |
5a580b3a AM |
5695 | |
5696 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
5697 | TRUE); | |
b1b00fcc | 5698 | if (indx == (bfd_size_type) -1) |
5a580b3a AM |
5699 | return FALSE; |
5700 | ||
b1b00fcc MF |
5701 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; |
5702 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
5703 | return FALSE; | |
5a580b3a AM |
5704 | } |
5705 | ||
5706 | if (filter_shlib != NULL) | |
5707 | { | |
5708 | bfd_size_type indx; | |
5709 | ||
5710 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5711 | filter_shlib, TRUE); | |
5712 | if (indx == (bfd_size_type) -1 | |
5713 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
5714 | return FALSE; | |
5715 | } | |
5716 | ||
5717 | if (auxiliary_filters != NULL) | |
5718 | { | |
5719 | const char * const *p; | |
5720 | ||
5721 | for (p = auxiliary_filters; *p != NULL; p++) | |
5722 | { | |
5723 | bfd_size_type indx; | |
5724 | ||
5725 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5726 | *p, TRUE); | |
5727 | if (indx == (bfd_size_type) -1 | |
5728 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
5729 | return FALSE; | |
5730 | } | |
5731 | } | |
5732 | ||
7ee314fa AM |
5733 | if (audit != NULL) |
5734 | { | |
5735 | bfd_size_type indx; | |
5736 | ||
5737 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
5738 | TRUE); | |
5739 | if (indx == (bfd_size_type) -1 | |
5740 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
5741 | return FALSE; | |
5742 | } | |
5743 | ||
5744 | if (depaudit != NULL) | |
5745 | { | |
5746 | bfd_size_type indx; | |
5747 | ||
5748 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
5749 | TRUE); | |
5750 | if (indx == (bfd_size_type) -1 | |
5751 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
5752 | return FALSE; | |
5753 | } | |
5754 | ||
5a580b3a | 5755 | eif.info = info; |
5a580b3a AM |
5756 | eif.failed = FALSE; |
5757 | ||
5758 | /* If we are supposed to export all symbols into the dynamic symbol | |
5759 | table (this is not the normal case), then do so. */ | |
55255dae L |
5760 | if (info->export_dynamic |
5761 | || (info->executable && info->dynamic)) | |
5a580b3a AM |
5762 | { |
5763 | elf_link_hash_traverse (elf_hash_table (info), | |
5764 | _bfd_elf_export_symbol, | |
5765 | &eif); | |
5766 | if (eif.failed) | |
5767 | return FALSE; | |
5768 | } | |
5769 | ||
5770 | /* Make all global versions with definition. */ | |
fd91d419 | 5771 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 5772 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 5773 | if (!d->symver && d->literal) |
5a580b3a AM |
5774 | { |
5775 | const char *verstr, *name; | |
5776 | size_t namelen, verlen, newlen; | |
93252b1c | 5777 | char *newname, *p, leading_char; |
5a580b3a AM |
5778 | struct elf_link_hash_entry *newh; |
5779 | ||
93252b1c | 5780 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 5781 | name = d->pattern; |
93252b1c | 5782 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
5783 | verstr = t->name; |
5784 | verlen = strlen (verstr); | |
5785 | newlen = namelen + verlen + 3; | |
5786 | ||
a50b1753 | 5787 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
5788 | if (newname == NULL) |
5789 | return FALSE; | |
93252b1c MF |
5790 | newname[0] = leading_char; |
5791 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
5792 | |
5793 | /* Check the hidden versioned definition. */ | |
5794 | p = newname + namelen; | |
5795 | *p++ = ELF_VER_CHR; | |
5796 | memcpy (p, verstr, verlen + 1); | |
5797 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5798 | newname, FALSE, FALSE, | |
5799 | FALSE); | |
5800 | if (newh == NULL | |
5801 | || (newh->root.type != bfd_link_hash_defined | |
5802 | && newh->root.type != bfd_link_hash_defweak)) | |
5803 | { | |
5804 | /* Check the default versioned definition. */ | |
5805 | *p++ = ELF_VER_CHR; | |
5806 | memcpy (p, verstr, verlen + 1); | |
5807 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
5808 | newname, FALSE, FALSE, | |
5809 | FALSE); | |
5810 | } | |
5811 | free (newname); | |
5812 | ||
5813 | /* Mark this version if there is a definition and it is | |
5814 | not defined in a shared object. */ | |
5815 | if (newh != NULL | |
f5385ebf | 5816 | && !newh->def_dynamic |
5a580b3a AM |
5817 | && (newh->root.type == bfd_link_hash_defined |
5818 | || newh->root.type == bfd_link_hash_defweak)) | |
5819 | d->symver = 1; | |
5820 | } | |
5821 | ||
5822 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 5823 | asvinfo.info = info; |
5a580b3a AM |
5824 | asvinfo.failed = FALSE; |
5825 | ||
5826 | elf_link_hash_traverse (elf_hash_table (info), | |
5827 | _bfd_elf_link_assign_sym_version, | |
5828 | &asvinfo); | |
5829 | if (asvinfo.failed) | |
5830 | return FALSE; | |
5831 | ||
5832 | if (!info->allow_undefined_version) | |
5833 | { | |
5834 | /* Check if all global versions have a definition. */ | |
5835 | all_defined = TRUE; | |
fd91d419 | 5836 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 5837 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 5838 | if (d->literal && !d->symver && !d->script) |
5a580b3a AM |
5839 | { |
5840 | (*_bfd_error_handler) | |
5841 | (_("%s: undefined version: %s"), | |
5842 | d->pattern, t->name); | |
5843 | all_defined = FALSE; | |
5844 | } | |
5845 | ||
5846 | if (!all_defined) | |
5847 | { | |
5848 | bfd_set_error (bfd_error_bad_value); | |
5849 | return FALSE; | |
5850 | } | |
5851 | } | |
5852 | ||
5853 | /* Find all symbols which were defined in a dynamic object and make | |
5854 | the backend pick a reasonable value for them. */ | |
5855 | elf_link_hash_traverse (elf_hash_table (info), | |
5856 | _bfd_elf_adjust_dynamic_symbol, | |
5857 | &eif); | |
5858 | if (eif.failed) | |
5859 | return FALSE; | |
5860 | ||
5861 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5862 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5863 | now so that we know the final size of the .dynamic section. */ |
5864 | ||
5865 | /* If there are initialization and/or finalization functions to | |
5866 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5867 | h = (info->init_function | |
5868 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5869 | info->init_function, FALSE, | |
5870 | FALSE, FALSE) | |
5871 | : NULL); | |
5872 | if (h != NULL | |
f5385ebf AM |
5873 | && (h->ref_regular |
5874 | || h->def_regular)) | |
5a580b3a AM |
5875 | { |
5876 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5877 | return FALSE; | |
5878 | } | |
5879 | h = (info->fini_function | |
5880 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5881 | info->fini_function, FALSE, | |
5882 | FALSE, FALSE) | |
5883 | : NULL); | |
5884 | if (h != NULL | |
f5385ebf AM |
5885 | && (h->ref_regular |
5886 | || h->def_regular)) | |
5a580b3a AM |
5887 | { |
5888 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5889 | return FALSE; | |
5890 | } | |
5891 | ||
046183de AM |
5892 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); |
5893 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5894 | { |
5895 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5896 | if (! info->executable) | |
5897 | { | |
5898 | bfd *sub; | |
5899 | asection *o; | |
5900 | ||
5901 | for (sub = info->input_bfds; sub != NULL; | |
c72f2fb2 | 5902 | sub = sub->link.next) |
3fcd97f1 JJ |
5903 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
5904 | for (o = sub->sections; o != NULL; o = o->next) | |
5905 | if (elf_section_data (o)->this_hdr.sh_type | |
5906 | == SHT_PREINIT_ARRAY) | |
5907 | { | |
5908 | (*_bfd_error_handler) | |
5909 | (_("%B: .preinit_array section is not allowed in DSO"), | |
5910 | sub); | |
5911 | break; | |
5912 | } | |
5a580b3a AM |
5913 | |
5914 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5915 | return FALSE; | |
5916 | } | |
5917 | ||
5918 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5919 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5920 | return FALSE; | |
5921 | } | |
046183de AM |
5922 | s = bfd_get_section_by_name (output_bfd, ".init_array"); |
5923 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5924 | { |
5925 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5926 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5927 | return FALSE; | |
5928 | } | |
046183de AM |
5929 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); |
5930 | if (s != NULL && s->linker_has_input) | |
5a580b3a AM |
5931 | { |
5932 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5933 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5934 | return FALSE; | |
5935 | } | |
5936 | ||
3d4d4302 | 5937 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
5938 | /* If .dynstr is excluded from the link, we don't want any of |
5939 | these tags. Strictly, we should be checking each section | |
5940 | individually; This quick check covers for the case where | |
5941 | someone does a /DISCARD/ : { *(*) }. */ | |
5942 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5943 | { | |
5944 | bfd_size_type strsize; | |
5945 | ||
5946 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
fdc90cb4 JJ |
5947 | if ((info->emit_hash |
5948 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
5949 | || (info->emit_gnu_hash | |
5950 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
5a580b3a AM |
5951 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
5952 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5953 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5954 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5955 | bed->s->sizeof_sym)) | |
5956 | return FALSE; | |
5957 | } | |
5958 | } | |
5959 | ||
5960 | /* The backend must work out the sizes of all the other dynamic | |
5961 | sections. */ | |
9a2a56cc AM |
5962 | if (dynobj != NULL |
5963 | && bed->elf_backend_size_dynamic_sections != NULL | |
5a580b3a AM |
5964 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) |
5965 | return FALSE; | |
5966 | ||
9a2a56cc AM |
5967 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) |
5968 | return FALSE; | |
5969 | ||
5970 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
5a580b3a | 5971 | { |
554220db | 5972 | unsigned long section_sym_count; |
fd91d419 | 5973 | struct bfd_elf_version_tree *verdefs; |
5a580b3a | 5974 | asection *s; |
5a580b3a AM |
5975 | |
5976 | /* Set up the version definition section. */ | |
3d4d4302 | 5977 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
5978 | BFD_ASSERT (s != NULL); |
5979 | ||
5980 | /* We may have created additional version definitions if we are | |
5981 | just linking a regular application. */ | |
fd91d419 | 5982 | verdefs = info->version_info; |
5a580b3a AM |
5983 | |
5984 | /* Skip anonymous version tag. */ | |
5985 | if (verdefs != NULL && verdefs->vernum == 0) | |
5986 | verdefs = verdefs->next; | |
5987 | ||
3e3b46e5 | 5988 | if (verdefs == NULL && !info->create_default_symver) |
8423293d | 5989 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
5990 | else |
5991 | { | |
5992 | unsigned int cdefs; | |
5993 | bfd_size_type size; | |
5994 | struct bfd_elf_version_tree *t; | |
5995 | bfd_byte *p; | |
5996 | Elf_Internal_Verdef def; | |
5997 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5998 | struct bfd_link_hash_entry *bh; |
5999 | struct elf_link_hash_entry *h; | |
6000 | const char *name; | |
5a580b3a AM |
6001 | |
6002 | cdefs = 0; | |
6003 | size = 0; | |
6004 | ||
6005 | /* Make space for the base version. */ | |
6006 | size += sizeof (Elf_External_Verdef); | |
6007 | size += sizeof (Elf_External_Verdaux); | |
6008 | ++cdefs; | |
6009 | ||
3e3b46e5 PB |
6010 | /* Make space for the default version. */ |
6011 | if (info->create_default_symver) | |
6012 | { | |
6013 | size += sizeof (Elf_External_Verdef); | |
6014 | ++cdefs; | |
6015 | } | |
6016 | ||
5a580b3a AM |
6017 | for (t = verdefs; t != NULL; t = t->next) |
6018 | { | |
6019 | struct bfd_elf_version_deps *n; | |
6020 | ||
a6cc6b3b RO |
6021 | /* Don't emit base version twice. */ |
6022 | if (t->vernum == 0) | |
6023 | continue; | |
6024 | ||
5a580b3a AM |
6025 | size += sizeof (Elf_External_Verdef); |
6026 | size += sizeof (Elf_External_Verdaux); | |
6027 | ++cdefs; | |
6028 | ||
6029 | for (n = t->deps; n != NULL; n = n->next) | |
6030 | size += sizeof (Elf_External_Verdaux); | |
6031 | } | |
6032 | ||
eea6121a | 6033 | s->size = size; |
a50b1753 | 6034 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6035 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6036 | return FALSE; |
6037 | ||
6038 | /* Fill in the version definition section. */ | |
6039 | ||
6040 | p = s->contents; | |
6041 | ||
6042 | def.vd_version = VER_DEF_CURRENT; | |
6043 | def.vd_flags = VER_FLG_BASE; | |
6044 | def.vd_ndx = 1; | |
6045 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6046 | if (info->create_default_symver) |
6047 | { | |
6048 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6049 | def.vd_next = sizeof (Elf_External_Verdef); | |
6050 | } | |
6051 | else | |
6052 | { | |
6053 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6054 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6055 | + sizeof (Elf_External_Verdaux)); | |
6056 | } | |
5a580b3a AM |
6057 | |
6058 | if (soname_indx != (bfd_size_type) -1) | |
6059 | { | |
6060 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6061 | soname_indx); | |
6062 | def.vd_hash = bfd_elf_hash (soname); | |
6063 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6064 | name = soname; |
5a580b3a AM |
6065 | } |
6066 | else | |
6067 | { | |
5a580b3a AM |
6068 | bfd_size_type indx; |
6069 | ||
06084812 | 6070 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6071 | def.vd_hash = bfd_elf_hash (name); |
6072 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6073 | name, FALSE); | |
6074 | if (indx == (bfd_size_type) -1) | |
6075 | return FALSE; | |
6076 | defaux.vda_name = indx; | |
6077 | } | |
6078 | defaux.vda_next = 0; | |
6079 | ||
6080 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6081 | (Elf_External_Verdef *) p); | |
6082 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6083 | if (info->create_default_symver) |
6084 | { | |
6085 | /* Add a symbol representing this version. */ | |
6086 | bh = NULL; | |
6087 | if (! (_bfd_generic_link_add_one_symbol | |
6088 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6089 | 0, NULL, FALSE, | |
6090 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6091 | return FALSE; | |
6092 | h = (struct elf_link_hash_entry *) bh; | |
6093 | h->non_elf = 0; | |
6094 | h->def_regular = 1; | |
6095 | h->type = STT_OBJECT; | |
6096 | h->verinfo.vertree = NULL; | |
6097 | ||
6098 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6099 | return FALSE; | |
6100 | ||
6101 | /* Create a duplicate of the base version with the same | |
6102 | aux block, but different flags. */ | |
6103 | def.vd_flags = 0; | |
6104 | def.vd_ndx = 2; | |
6105 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6106 | if (verdefs) | |
6107 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6108 | + sizeof (Elf_External_Verdaux)); | |
6109 | else | |
6110 | def.vd_next = 0; | |
6111 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6112 | (Elf_External_Verdef *) p); | |
6113 | p += sizeof (Elf_External_Verdef); | |
6114 | } | |
5a580b3a AM |
6115 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6116 | (Elf_External_Verdaux *) p); | |
6117 | p += sizeof (Elf_External_Verdaux); | |
6118 | ||
6119 | for (t = verdefs; t != NULL; t = t->next) | |
6120 | { | |
6121 | unsigned int cdeps; | |
6122 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6123 | |
a6cc6b3b RO |
6124 | /* Don't emit the base version twice. */ |
6125 | if (t->vernum == 0) | |
6126 | continue; | |
6127 | ||
5a580b3a AM |
6128 | cdeps = 0; |
6129 | for (n = t->deps; n != NULL; n = n->next) | |
6130 | ++cdeps; | |
6131 | ||
6132 | /* Add a symbol representing this version. */ | |
6133 | bh = NULL; | |
6134 | if (! (_bfd_generic_link_add_one_symbol | |
6135 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6136 | 0, NULL, FALSE, | |
6137 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6138 | return FALSE; | |
6139 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6140 | h->non_elf = 0; |
6141 | h->def_regular = 1; | |
5a580b3a AM |
6142 | h->type = STT_OBJECT; |
6143 | h->verinfo.vertree = t; | |
6144 | ||
c152c796 | 6145 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6146 | return FALSE; |
6147 | ||
6148 | def.vd_version = VER_DEF_CURRENT; | |
6149 | def.vd_flags = 0; | |
6150 | if (t->globals.list == NULL | |
6151 | && t->locals.list == NULL | |
6152 | && ! t->used) | |
6153 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6154 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6155 | def.vd_cnt = cdeps + 1; |
6156 | def.vd_hash = bfd_elf_hash (t->name); | |
6157 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6158 | def.vd_next = 0; | |
a6cc6b3b RO |
6159 | |
6160 | /* If a basever node is next, it *must* be the last node in | |
6161 | the chain, otherwise Verdef construction breaks. */ | |
6162 | if (t->next != NULL && t->next->vernum == 0) | |
6163 | BFD_ASSERT (t->next->next == NULL); | |
6164 | ||
6165 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6166 | def.vd_next = (sizeof (Elf_External_Verdef) |
6167 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6168 | ||
6169 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6170 | (Elf_External_Verdef *) p); | |
6171 | p += sizeof (Elf_External_Verdef); | |
6172 | ||
6173 | defaux.vda_name = h->dynstr_index; | |
6174 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6175 | h->dynstr_index); | |
6176 | defaux.vda_next = 0; | |
6177 | if (t->deps != NULL) | |
6178 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6179 | t->name_indx = defaux.vda_name; | |
6180 | ||
6181 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6182 | (Elf_External_Verdaux *) p); | |
6183 | p += sizeof (Elf_External_Verdaux); | |
6184 | ||
6185 | for (n = t->deps; n != NULL; n = n->next) | |
6186 | { | |
6187 | if (n->version_needed == NULL) | |
6188 | { | |
6189 | /* This can happen if there was an error in the | |
6190 | version script. */ | |
6191 | defaux.vda_name = 0; | |
6192 | } | |
6193 | else | |
6194 | { | |
6195 | defaux.vda_name = n->version_needed->name_indx; | |
6196 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6197 | defaux.vda_name); | |
6198 | } | |
6199 | if (n->next == NULL) | |
6200 | defaux.vda_next = 0; | |
6201 | else | |
6202 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6203 | ||
6204 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6205 | (Elf_External_Verdaux *) p); | |
6206 | p += sizeof (Elf_External_Verdaux); | |
6207 | } | |
6208 | } | |
6209 | ||
6210 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6211 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
6212 | return FALSE; | |
6213 | ||
6214 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
6215 | } | |
6216 | ||
6217 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6218 | { | |
6219 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6220 | return FALSE; | |
6221 | } | |
6222 | else if (info->flags & DF_BIND_NOW) | |
6223 | { | |
6224 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6225 | return FALSE; | |
6226 | } | |
6227 | ||
6228 | if (info->flags_1) | |
6229 | { | |
6230 | if (info->executable) | |
6231 | info->flags_1 &= ~ (DF_1_INITFIRST | |
6232 | | DF_1_NODELETE | |
6233 | | DF_1_NOOPEN); | |
6234 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6235 | return FALSE; | |
6236 | } | |
6237 | ||
6238 | /* Work out the size of the version reference section. */ | |
6239 | ||
3d4d4302 | 6240 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
6241 | BFD_ASSERT (s != NULL); |
6242 | { | |
6243 | struct elf_find_verdep_info sinfo; | |
6244 | ||
5a580b3a AM |
6245 | sinfo.info = info; |
6246 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6247 | if (sinfo.vers == 0) | |
6248 | sinfo.vers = 1; | |
6249 | sinfo.failed = FALSE; | |
6250 | ||
6251 | elf_link_hash_traverse (elf_hash_table (info), | |
6252 | _bfd_elf_link_find_version_dependencies, | |
6253 | &sinfo); | |
14b1c01e AM |
6254 | if (sinfo.failed) |
6255 | return FALSE; | |
5a580b3a AM |
6256 | |
6257 | if (elf_tdata (output_bfd)->verref == NULL) | |
8423293d | 6258 | s->flags |= SEC_EXCLUDE; |
5a580b3a AM |
6259 | else |
6260 | { | |
6261 | Elf_Internal_Verneed *t; | |
6262 | unsigned int size; | |
6263 | unsigned int crefs; | |
6264 | bfd_byte *p; | |
6265 | ||
a6cc6b3b | 6266 | /* Build the version dependency section. */ |
5a580b3a AM |
6267 | size = 0; |
6268 | crefs = 0; | |
6269 | for (t = elf_tdata (output_bfd)->verref; | |
6270 | t != NULL; | |
6271 | t = t->vn_nextref) | |
6272 | { | |
6273 | Elf_Internal_Vernaux *a; | |
6274 | ||
6275 | size += sizeof (Elf_External_Verneed); | |
6276 | ++crefs; | |
6277 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6278 | size += sizeof (Elf_External_Vernaux); | |
6279 | } | |
6280 | ||
eea6121a | 6281 | s->size = size; |
a50b1753 | 6282 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
5a580b3a AM |
6283 | if (s->contents == NULL) |
6284 | return FALSE; | |
6285 | ||
6286 | p = s->contents; | |
6287 | for (t = elf_tdata (output_bfd)->verref; | |
6288 | t != NULL; | |
6289 | t = t->vn_nextref) | |
6290 | { | |
6291 | unsigned int caux; | |
6292 | Elf_Internal_Vernaux *a; | |
6293 | bfd_size_type indx; | |
6294 | ||
6295 | caux = 0; | |
6296 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6297 | ++caux; | |
6298 | ||
6299 | t->vn_version = VER_NEED_CURRENT; | |
6300 | t->vn_cnt = caux; | |
6301 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6302 | elf_dt_name (t->vn_bfd) != NULL | |
6303 | ? elf_dt_name (t->vn_bfd) | |
06084812 | 6304 | : lbasename (t->vn_bfd->filename), |
5a580b3a AM |
6305 | FALSE); |
6306 | if (indx == (bfd_size_type) -1) | |
6307 | return FALSE; | |
6308 | t->vn_file = indx; | |
6309 | t->vn_aux = sizeof (Elf_External_Verneed); | |
6310 | if (t->vn_nextref == NULL) | |
6311 | t->vn_next = 0; | |
6312 | else | |
6313 | t->vn_next = (sizeof (Elf_External_Verneed) | |
6314 | + caux * sizeof (Elf_External_Vernaux)); | |
6315 | ||
6316 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
6317 | (Elf_External_Verneed *) p); | |
6318 | p += sizeof (Elf_External_Verneed); | |
6319 | ||
6320 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6321 | { | |
6322 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6323 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6324 | a->vna_nodename, FALSE); | |
6325 | if (indx == (bfd_size_type) -1) | |
6326 | return FALSE; | |
6327 | a->vna_name = indx; | |
6328 | if (a->vna_nextptr == NULL) | |
6329 | a->vna_next = 0; | |
6330 | else | |
6331 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6332 | ||
6333 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6334 | (Elf_External_Vernaux *) p); | |
6335 | p += sizeof (Elf_External_Vernaux); | |
6336 | } | |
6337 | } | |
6338 | ||
6339 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6340 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6341 | return FALSE; | |
6342 | ||
6343 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6344 | } | |
6345 | } | |
6346 | ||
8423293d AM |
6347 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6348 | && elf_tdata (output_bfd)->cverdefs == 0) | |
6349 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6350 | §ion_sym_count) == 0) | |
6351 | { | |
3d4d4302 | 6352 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
6353 | s->flags |= SEC_EXCLUDE; |
6354 | } | |
6355 | } | |
6356 | return TRUE; | |
6357 | } | |
6358 | ||
74541ad4 AM |
6359 | /* Find the first non-excluded output section. We'll use its |
6360 | section symbol for some emitted relocs. */ | |
6361 | void | |
6362 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6363 | { | |
6364 | asection *s; | |
6365 | ||
6366 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6367 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
6368 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) | |
6369 | { | |
6370 | elf_hash_table (info)->text_index_section = s; | |
6371 | break; | |
6372 | } | |
6373 | } | |
6374 | ||
6375 | /* Find two non-excluded output sections, one for code, one for data. | |
6376 | We'll use their section symbols for some emitted relocs. */ | |
6377 | void | |
6378 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6379 | { | |
6380 | asection *s; | |
6381 | ||
266b05cf DJ |
6382 | /* Data first, since setting text_index_section changes |
6383 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6384 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6385 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
74541ad4 AM |
6386 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6387 | { | |
266b05cf | 6388 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6389 | break; |
6390 | } | |
6391 | ||
6392 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6393 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6394 | == (SEC_ALLOC | SEC_READONLY)) | |
74541ad4 AM |
6395 | && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) |
6396 | { | |
266b05cf | 6397 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6398 | break; |
6399 | } | |
6400 | ||
6401 | if (elf_hash_table (info)->text_index_section == NULL) | |
6402 | elf_hash_table (info)->text_index_section | |
6403 | = elf_hash_table (info)->data_index_section; | |
6404 | } | |
6405 | ||
8423293d AM |
6406 | bfd_boolean |
6407 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6408 | { | |
74541ad4 AM |
6409 | const struct elf_backend_data *bed; |
6410 | ||
8423293d AM |
6411 | if (!is_elf_hash_table (info->hash)) |
6412 | return TRUE; | |
6413 | ||
74541ad4 AM |
6414 | bed = get_elf_backend_data (output_bfd); |
6415 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6416 | ||
8423293d AM |
6417 | if (elf_hash_table (info)->dynamic_sections_created) |
6418 | { | |
6419 | bfd *dynobj; | |
8423293d AM |
6420 | asection *s; |
6421 | bfd_size_type dynsymcount; | |
6422 | unsigned long section_sym_count; | |
8423293d AM |
6423 | unsigned int dtagcount; |
6424 | ||
6425 | dynobj = elf_hash_table (info)->dynobj; | |
6426 | ||
5a580b3a AM |
6427 | /* Assign dynsym indicies. In a shared library we generate a |
6428 | section symbol for each output section, which come first. | |
6429 | Next come all of the back-end allocated local dynamic syms, | |
6430 | followed by the rest of the global symbols. */ | |
6431 | ||
554220db AM |
6432 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, |
6433 | §ion_sym_count); | |
5a580b3a AM |
6434 | |
6435 | /* Work out the size of the symbol version section. */ | |
3d4d4302 | 6436 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 6437 | BFD_ASSERT (s != NULL); |
8423293d AM |
6438 | if (dynsymcount != 0 |
6439 | && (s->flags & SEC_EXCLUDE) == 0) | |
5a580b3a | 6440 | { |
eea6121a | 6441 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 6442 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
6443 | if (s->contents == NULL) |
6444 | return FALSE; | |
6445 | ||
6446 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
6447 | return FALSE; | |
6448 | } | |
6449 | ||
6450 | /* Set the size of the .dynsym and .hash sections. We counted | |
6451 | the number of dynamic symbols in elf_link_add_object_symbols. | |
6452 | We will build the contents of .dynsym and .hash when we build | |
6453 | the final symbol table, because until then we do not know the | |
6454 | correct value to give the symbols. We built the .dynstr | |
6455 | section as we went along in elf_link_add_object_symbols. */ | |
3d4d4302 | 6456 | s = bfd_get_linker_section (dynobj, ".dynsym"); |
5a580b3a | 6457 | BFD_ASSERT (s != NULL); |
eea6121a | 6458 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a AM |
6459 | |
6460 | if (dynsymcount != 0) | |
6461 | { | |
a50b1753 | 6462 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
554220db AM |
6463 | if (s->contents == NULL) |
6464 | return FALSE; | |
5a580b3a | 6465 | |
554220db AM |
6466 | /* The first entry in .dynsym is a dummy symbol. |
6467 | Clear all the section syms, in case we don't output them all. */ | |
6468 | ++section_sym_count; | |
6469 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a AM |
6470 | } |
6471 | ||
fdc90cb4 JJ |
6472 | elf_hash_table (info)->bucketcount = 0; |
6473 | ||
5a580b3a AM |
6474 | /* Compute the size of the hashing table. As a side effect this |
6475 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
6476 | if (info->emit_hash) |
6477 | { | |
6478 | unsigned long int *hashcodes; | |
14b1c01e | 6479 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
6480 | bfd_size_type amt; |
6481 | unsigned long int nsyms; | |
6482 | size_t bucketcount; | |
6483 | size_t hash_entry_size; | |
6484 | ||
6485 | /* Compute the hash values for all exported symbols. At the same | |
6486 | time store the values in an array so that we could use them for | |
6487 | optimizations. */ | |
6488 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 6489 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6490 | if (hashcodes == NULL) |
6491 | return FALSE; | |
14b1c01e AM |
6492 | hashinf.hashcodes = hashcodes; |
6493 | hashinf.error = FALSE; | |
5a580b3a | 6494 | |
fdc90cb4 JJ |
6495 | /* Put all hash values in HASHCODES. */ |
6496 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
6497 | elf_collect_hash_codes, &hashinf); |
6498 | if (hashinf.error) | |
4dd07732 AM |
6499 | { |
6500 | free (hashcodes); | |
6501 | return FALSE; | |
6502 | } | |
5a580b3a | 6503 | |
14b1c01e | 6504 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
6505 | bucketcount |
6506 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
6507 | free (hashcodes); | |
6508 | ||
6509 | if (bucketcount == 0) | |
6510 | return FALSE; | |
5a580b3a | 6511 | |
fdc90cb4 JJ |
6512 | elf_hash_table (info)->bucketcount = bucketcount; |
6513 | ||
3d4d4302 | 6514 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
6515 | BFD_ASSERT (s != NULL); |
6516 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
6517 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 6518 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6519 | if (s->contents == NULL) |
6520 | return FALSE; | |
6521 | ||
6522 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
6523 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
6524 | s->contents + hash_entry_size); | |
6525 | } | |
6526 | ||
6527 | if (info->emit_gnu_hash) | |
6528 | { | |
6529 | size_t i, cnt; | |
6530 | unsigned char *contents; | |
6531 | struct collect_gnu_hash_codes cinfo; | |
6532 | bfd_size_type amt; | |
6533 | size_t bucketcount; | |
6534 | ||
6535 | memset (&cinfo, 0, sizeof (cinfo)); | |
6536 | ||
6537 | /* Compute the hash values for all exported symbols. At the same | |
6538 | time store the values in an array so that we could use them for | |
6539 | optimizations. */ | |
6540 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 6541 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
6542 | if (cinfo.hashcodes == NULL) |
6543 | return FALSE; | |
6544 | ||
6545 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
6546 | cinfo.min_dynindx = -1; | |
6547 | cinfo.output_bfd = output_bfd; | |
6548 | cinfo.bed = bed; | |
6549 | ||
6550 | /* Put all hash values in HASHCODES. */ | |
6551 | elf_link_hash_traverse (elf_hash_table (info), | |
6552 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 6553 | if (cinfo.error) |
4dd07732 AM |
6554 | { |
6555 | free (cinfo.hashcodes); | |
6556 | return FALSE; | |
6557 | } | |
fdc90cb4 JJ |
6558 | |
6559 | bucketcount | |
6560 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
6561 | ||
6562 | if (bucketcount == 0) | |
6563 | { | |
6564 | free (cinfo.hashcodes); | |
6565 | return FALSE; | |
6566 | } | |
6567 | ||
3d4d4302 | 6568 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
6569 | BFD_ASSERT (s != NULL); |
6570 | ||
6571 | if (cinfo.nsyms == 0) | |
6572 | { | |
6573 | /* Empty .gnu.hash section is special. */ | |
6574 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
6575 | free (cinfo.hashcodes); | |
6576 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 6577 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6578 | if (contents == NULL) |
6579 | return FALSE; | |
6580 | s->contents = contents; | |
6581 | /* 1 empty bucket. */ | |
6582 | bfd_put_32 (output_bfd, 1, contents); | |
6583 | /* SYMIDX above the special symbol 0. */ | |
6584 | bfd_put_32 (output_bfd, 1, contents + 4); | |
6585 | /* Just one word for bitmask. */ | |
6586 | bfd_put_32 (output_bfd, 1, contents + 8); | |
6587 | /* Only hash fn bloom filter. */ | |
6588 | bfd_put_32 (output_bfd, 0, contents + 12); | |
6589 | /* No hashes are valid - empty bitmask. */ | |
6590 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
6591 | /* No hashes in the only bucket. */ | |
6592 | bfd_put_32 (output_bfd, 0, | |
6593 | contents + 16 + bed->s->arch_size / 8); | |
6594 | } | |
6595 | else | |
6596 | { | |
9e6619e2 | 6597 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 6598 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 6599 | |
9e6619e2 AM |
6600 | x = cinfo.nsyms; |
6601 | maskbitslog2 = 1; | |
6602 | while ((x >>= 1) != 0) | |
6603 | ++maskbitslog2; | |
fdc90cb4 JJ |
6604 | if (maskbitslog2 < 3) |
6605 | maskbitslog2 = 5; | |
6606 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
6607 | maskbitslog2 = maskbitslog2 + 3; | |
6608 | else | |
6609 | maskbitslog2 = maskbitslog2 + 2; | |
6610 | if (bed->s->arch_size == 64) | |
6611 | { | |
6612 | if (maskbitslog2 == 5) | |
6613 | maskbitslog2 = 6; | |
6614 | cinfo.shift1 = 6; | |
6615 | } | |
6616 | else | |
6617 | cinfo.shift1 = 5; | |
6618 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 6619 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
6620 | cinfo.maskbits = 1 << maskbitslog2; |
6621 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
6622 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
6623 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 6624 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
6625 | if (cinfo.bitmask == NULL) |
6626 | { | |
6627 | free (cinfo.hashcodes); | |
6628 | return FALSE; | |
6629 | } | |
6630 | ||
a50b1753 | 6631 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
6632 | cinfo.indx = cinfo.counts + bucketcount; |
6633 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
6634 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
6635 | ||
6636 | /* Determine how often each hash bucket is used. */ | |
6637 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
6638 | for (i = 0; i < cinfo.nsyms; ++i) | |
6639 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
6640 | ||
6641 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
6642 | if (cinfo.counts[i] != 0) | |
6643 | { | |
6644 | cinfo.indx[i] = cnt; | |
6645 | cnt += cinfo.counts[i]; | |
6646 | } | |
6647 | BFD_ASSERT (cnt == dynsymcount); | |
6648 | cinfo.bucketcount = bucketcount; | |
6649 | cinfo.local_indx = cinfo.min_dynindx; | |
6650 | ||
6651 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
6652 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 6653 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
6654 | if (contents == NULL) |
6655 | { | |
6656 | free (cinfo.bitmask); | |
6657 | free (cinfo.hashcodes); | |
6658 | return FALSE; | |
6659 | } | |
6660 | ||
6661 | s->contents = contents; | |
6662 | bfd_put_32 (output_bfd, bucketcount, contents); | |
6663 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
6664 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
6665 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
6666 | contents += 16 + cinfo.maskbits / 8; | |
6667 | ||
6668 | for (i = 0; i < bucketcount; ++i) | |
6669 | { | |
6670 | if (cinfo.counts[i] == 0) | |
6671 | bfd_put_32 (output_bfd, 0, contents); | |
6672 | else | |
6673 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
6674 | contents += 4; | |
6675 | } | |
6676 | ||
6677 | cinfo.contents = contents; | |
6678 | ||
6679 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
6680 | elf_link_hash_traverse (elf_hash_table (info), | |
6681 | elf_renumber_gnu_hash_syms, &cinfo); | |
6682 | ||
6683 | contents = s->contents + 16; | |
6684 | for (i = 0; i < maskwords; ++i) | |
6685 | { | |
6686 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
6687 | contents); | |
6688 | contents += bed->s->arch_size / 8; | |
6689 | } | |
6690 | ||
6691 | free (cinfo.bitmask); | |
6692 | free (cinfo.hashcodes); | |
6693 | } | |
6694 | } | |
5a580b3a | 6695 | |
3d4d4302 | 6696 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
6697 | BFD_ASSERT (s != NULL); |
6698 | ||
4ad4eba5 | 6699 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 6700 | |
eea6121a | 6701 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
6702 | |
6703 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
6704 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
6705 | return FALSE; | |
6706 | } | |
6707 | ||
6708 | return TRUE; | |
6709 | } | |
4d269e42 | 6710 | \f |
4d269e42 AM |
6711 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
6712 | ||
6713 | static void | |
6714 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
6715 | asection *sec) | |
6716 | { | |
dbaa2011 AM |
6717 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
6718 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
6719 | } |
6720 | ||
6721 | /* Finish SHF_MERGE section merging. */ | |
6722 | ||
6723 | bfd_boolean | |
6724 | _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info) | |
6725 | { | |
6726 | bfd *ibfd; | |
6727 | asection *sec; | |
6728 | ||
6729 | if (!is_elf_hash_table (info->hash)) | |
6730 | return FALSE; | |
6731 | ||
c72f2fb2 | 6732 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
4d269e42 AM |
6733 | if ((ibfd->flags & DYNAMIC) == 0) |
6734 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
6735 | if ((sec->flags & SEC_MERGE) != 0 | |
6736 | && !bfd_is_abs_section (sec->output_section)) | |
6737 | { | |
6738 | struct bfd_elf_section_data *secdata; | |
6739 | ||
6740 | secdata = elf_section_data (sec); | |
6741 | if (! _bfd_add_merge_section (abfd, | |
6742 | &elf_hash_table (info)->merge_info, | |
6743 | sec, &secdata->sec_info)) | |
6744 | return FALSE; | |
6745 | else if (secdata->sec_info) | |
dbaa2011 | 6746 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
6747 | } |
6748 | ||
6749 | if (elf_hash_table (info)->merge_info != NULL) | |
6750 | _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info, | |
6751 | merge_sections_remove_hook); | |
6752 | return TRUE; | |
6753 | } | |
6754 | ||
6755 | /* Create an entry in an ELF linker hash table. */ | |
6756 | ||
6757 | struct bfd_hash_entry * | |
6758 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
6759 | struct bfd_hash_table *table, | |
6760 | const char *string) | |
6761 | { | |
6762 | /* Allocate the structure if it has not already been allocated by a | |
6763 | subclass. */ | |
6764 | if (entry == NULL) | |
6765 | { | |
a50b1753 | 6766 | entry = (struct bfd_hash_entry *) |
ca4be51c | 6767 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
6768 | if (entry == NULL) |
6769 | return entry; | |
6770 | } | |
6771 | ||
6772 | /* Call the allocation method of the superclass. */ | |
6773 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
6774 | if (entry != NULL) | |
6775 | { | |
6776 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
6777 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
6778 | ||
6779 | /* Set local fields. */ | |
6780 | ret->indx = -1; | |
6781 | ret->dynindx = -1; | |
6782 | ret->got = htab->init_got_refcount; | |
6783 | ret->plt = htab->init_plt_refcount; | |
6784 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
6785 | - offsetof (struct elf_link_hash_entry, size))); | |
6786 | /* Assume that we have been called by a non-ELF symbol reader. | |
6787 | This flag is then reset by the code which reads an ELF input | |
6788 | file. This ensures that a symbol created by a non-ELF symbol | |
6789 | reader will have the flag set correctly. */ | |
6790 | ret->non_elf = 1; | |
6791 | } | |
6792 | ||
6793 | return entry; | |
6794 | } | |
6795 | ||
6796 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
6797 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
6798 | ||
6799 | void | |
6800 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
6801 | struct elf_link_hash_entry *dir, | |
6802 | struct elf_link_hash_entry *ind) | |
6803 | { | |
6804 | struct elf_link_hash_table *htab; | |
6805 | ||
6806 | /* Copy down any references that we may have already seen to the | |
6807 | symbol which just became indirect. */ | |
6808 | ||
6809 | dir->ref_dynamic |= ind->ref_dynamic; | |
6810 | dir->ref_regular |= ind->ref_regular; | |
6811 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
6812 | dir->non_got_ref |= ind->non_got_ref; | |
6813 | dir->needs_plt |= ind->needs_plt; | |
6814 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
6815 | ||
6816 | if (ind->root.type != bfd_link_hash_indirect) | |
6817 | return; | |
6818 | ||
6819 | /* Copy over the global and procedure linkage table refcount entries. | |
6820 | These may have been already set up by a check_relocs routine. */ | |
6821 | htab = elf_hash_table (info); | |
6822 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
6823 | { | |
6824 | if (dir->got.refcount < 0) | |
6825 | dir->got.refcount = 0; | |
6826 | dir->got.refcount += ind->got.refcount; | |
6827 | ind->got.refcount = htab->init_got_refcount.refcount; | |
6828 | } | |
6829 | ||
6830 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
6831 | { | |
6832 | if (dir->plt.refcount < 0) | |
6833 | dir->plt.refcount = 0; | |
6834 | dir->plt.refcount += ind->plt.refcount; | |
6835 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
6836 | } | |
6837 | ||
6838 | if (ind->dynindx != -1) | |
6839 | { | |
6840 | if (dir->dynindx != -1) | |
6841 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
6842 | dir->dynindx = ind->dynindx; | |
6843 | dir->dynstr_index = ind->dynstr_index; | |
6844 | ind->dynindx = -1; | |
6845 | ind->dynstr_index = 0; | |
6846 | } | |
6847 | } | |
6848 | ||
6849 | void | |
6850 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
6851 | struct elf_link_hash_entry *h, | |
6852 | bfd_boolean force_local) | |
6853 | { | |
3aa14d16 L |
6854 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
6855 | if (h->type != STT_GNU_IFUNC) | |
6856 | { | |
6857 | h->plt = elf_hash_table (info)->init_plt_offset; | |
6858 | h->needs_plt = 0; | |
6859 | } | |
4d269e42 AM |
6860 | if (force_local) |
6861 | { | |
6862 | h->forced_local = 1; | |
6863 | if (h->dynindx != -1) | |
6864 | { | |
6865 | h->dynindx = -1; | |
6866 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
6867 | h->dynstr_index); | |
6868 | } | |
6869 | } | |
6870 | } | |
6871 | ||
7bf52ea2 AM |
6872 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
6873 | caller. */ | |
4d269e42 AM |
6874 | |
6875 | bfd_boolean | |
6876 | _bfd_elf_link_hash_table_init | |
6877 | (struct elf_link_hash_table *table, | |
6878 | bfd *abfd, | |
6879 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
6880 | struct bfd_hash_table *, | |
6881 | const char *), | |
4dfe6ac6 NC |
6882 | unsigned int entsize, |
6883 | enum elf_target_id target_id) | |
4d269e42 AM |
6884 | { |
6885 | bfd_boolean ret; | |
6886 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
6887 | ||
4d269e42 AM |
6888 | table->init_got_refcount.refcount = can_refcount - 1; |
6889 | table->init_plt_refcount.refcount = can_refcount - 1; | |
6890 | table->init_got_offset.offset = -(bfd_vma) 1; | |
6891 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
6892 | /* The first dynamic symbol is a dummy. */ | |
6893 | table->dynsymcount = 1; | |
6894 | ||
6895 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 6896 | |
4d269e42 | 6897 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 6898 | table->hash_table_id = target_id; |
4d269e42 AM |
6899 | |
6900 | return ret; | |
6901 | } | |
6902 | ||
6903 | /* Create an ELF linker hash table. */ | |
6904 | ||
6905 | struct bfd_link_hash_table * | |
6906 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
6907 | { | |
6908 | struct elf_link_hash_table *ret; | |
6909 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
6910 | ||
7bf52ea2 | 6911 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
6912 | if (ret == NULL) |
6913 | return NULL; | |
6914 | ||
6915 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
6916 | sizeof (struct elf_link_hash_entry), |
6917 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
6918 | { |
6919 | free (ret); | |
6920 | return NULL; | |
6921 | } | |
d495ab0d | 6922 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
6923 | |
6924 | return &ret->root; | |
6925 | } | |
6926 | ||
9f7c3e5e AM |
6927 | /* Destroy an ELF linker hash table. */ |
6928 | ||
6929 | void | |
d495ab0d | 6930 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 6931 | { |
d495ab0d AM |
6932 | struct elf_link_hash_table *htab; |
6933 | ||
6934 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
6935 | if (htab->dynstr != NULL) |
6936 | _bfd_elf_strtab_free (htab->dynstr); | |
6937 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 6938 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
6939 | } |
6940 | ||
4d269e42 AM |
6941 | /* This is a hook for the ELF emulation code in the generic linker to |
6942 | tell the backend linker what file name to use for the DT_NEEDED | |
6943 | entry for a dynamic object. */ | |
6944 | ||
6945 | void | |
6946 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
6947 | { | |
6948 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6949 | && bfd_get_format (abfd) == bfd_object) | |
6950 | elf_dt_name (abfd) = name; | |
6951 | } | |
6952 | ||
6953 | int | |
6954 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
6955 | { | |
6956 | int lib_class; | |
6957 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6958 | && bfd_get_format (abfd) == bfd_object) | |
6959 | lib_class = elf_dyn_lib_class (abfd); | |
6960 | else | |
6961 | lib_class = 0; | |
6962 | return lib_class; | |
6963 | } | |
6964 | ||
6965 | void | |
6966 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
6967 | { | |
6968 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6969 | && bfd_get_format (abfd) == bfd_object) | |
6970 | elf_dyn_lib_class (abfd) = lib_class; | |
6971 | } | |
6972 | ||
6973 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
6974 | the linker ELF emulation code. */ | |
6975 | ||
6976 | struct bfd_link_needed_list * | |
6977 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
6978 | struct bfd_link_info *info) | |
6979 | { | |
6980 | if (! is_elf_hash_table (info->hash)) | |
6981 | return NULL; | |
6982 | return elf_hash_table (info)->needed; | |
6983 | } | |
6984 | ||
6985 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
6986 | hook for the linker ELF emulation code. */ | |
6987 | ||
6988 | struct bfd_link_needed_list * | |
6989 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
6990 | struct bfd_link_info *info) | |
6991 | { | |
6992 | if (! is_elf_hash_table (info->hash)) | |
6993 | return NULL; | |
6994 | return elf_hash_table (info)->runpath; | |
6995 | } | |
6996 | ||
6997 | /* Get the name actually used for a dynamic object for a link. This | |
6998 | is the SONAME entry if there is one. Otherwise, it is the string | |
6999 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7000 | ||
7001 | const char * | |
7002 | bfd_elf_get_dt_soname (bfd *abfd) | |
7003 | { | |
7004 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7005 | && bfd_get_format (abfd) == bfd_object) | |
7006 | return elf_dt_name (abfd); | |
7007 | return NULL; | |
7008 | } | |
7009 | ||
7010 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7011 | the ELF linker emulation code. */ | |
7012 | ||
7013 | bfd_boolean | |
7014 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7015 | struct bfd_link_needed_list **pneeded) | |
7016 | { | |
7017 | asection *s; | |
7018 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7019 | unsigned int elfsec; |
4d269e42 AM |
7020 | unsigned long shlink; |
7021 | bfd_byte *extdyn, *extdynend; | |
7022 | size_t extdynsize; | |
7023 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7024 | ||
7025 | *pneeded = NULL; | |
7026 | ||
7027 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7028 | || bfd_get_format (abfd) != bfd_object) | |
7029 | return TRUE; | |
7030 | ||
7031 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7032 | if (s == NULL || s->size == 0) | |
7033 | return TRUE; | |
7034 | ||
7035 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7036 | goto error_return; | |
7037 | ||
7038 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7039 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7040 | goto error_return; |
7041 | ||
7042 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7043 | |
4d269e42 AM |
7044 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7045 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7046 | ||
7047 | extdyn = dynbuf; | |
7048 | extdynend = extdyn + s->size; | |
7049 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7050 | { | |
7051 | Elf_Internal_Dyn dyn; | |
7052 | ||
7053 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7054 | ||
7055 | if (dyn.d_tag == DT_NULL) | |
7056 | break; | |
7057 | ||
7058 | if (dyn.d_tag == DT_NEEDED) | |
7059 | { | |
7060 | const char *string; | |
7061 | struct bfd_link_needed_list *l; | |
7062 | unsigned int tagv = dyn.d_un.d_val; | |
7063 | bfd_size_type amt; | |
7064 | ||
7065 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7066 | if (string == NULL) | |
7067 | goto error_return; | |
7068 | ||
7069 | amt = sizeof *l; | |
a50b1753 | 7070 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7071 | if (l == NULL) |
7072 | goto error_return; | |
7073 | ||
7074 | l->by = abfd; | |
7075 | l->name = string; | |
7076 | l->next = *pneeded; | |
7077 | *pneeded = l; | |
7078 | } | |
7079 | } | |
7080 | ||
7081 | free (dynbuf); | |
7082 | ||
7083 | return TRUE; | |
7084 | ||
7085 | error_return: | |
7086 | if (dynbuf != NULL) | |
7087 | free (dynbuf); | |
7088 | return FALSE; | |
7089 | } | |
7090 | ||
7091 | struct elf_symbuf_symbol | |
7092 | { | |
7093 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7094 | unsigned char st_info; /* Type and binding attributes */ | |
7095 | unsigned char st_other; /* Visibilty, and target specific */ | |
7096 | }; | |
7097 | ||
7098 | struct elf_symbuf_head | |
7099 | { | |
7100 | struct elf_symbuf_symbol *ssym; | |
7101 | bfd_size_type count; | |
7102 | unsigned int st_shndx; | |
7103 | }; | |
7104 | ||
7105 | struct elf_symbol | |
7106 | { | |
7107 | union | |
7108 | { | |
7109 | Elf_Internal_Sym *isym; | |
7110 | struct elf_symbuf_symbol *ssym; | |
7111 | } u; | |
7112 | const char *name; | |
7113 | }; | |
7114 | ||
7115 | /* Sort references to symbols by ascending section number. */ | |
7116 | ||
7117 | static int | |
7118 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7119 | { | |
7120 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7121 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7122 | ||
7123 | return s1->st_shndx - s2->st_shndx; | |
7124 | } | |
7125 | ||
7126 | static int | |
7127 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7128 | { | |
7129 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7130 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7131 | return strcmp (s1->name, s2->name); | |
7132 | } | |
7133 | ||
7134 | static struct elf_symbuf_head * | |
7135 | elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf) | |
7136 | { | |
14b1c01e | 7137 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7138 | struct elf_symbuf_symbol *ssym; |
7139 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
3ae181ee | 7140 | bfd_size_type i, shndx_count, total_size; |
4d269e42 | 7141 | |
a50b1753 | 7142 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7143 | if (indbuf == NULL) |
7144 | return NULL; | |
7145 | ||
7146 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7147 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7148 | *ind++ = &isymbuf[i]; | |
7149 | indbufend = ind; | |
7150 | ||
7151 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7152 | elf_sort_elf_symbol); | |
7153 | ||
7154 | shndx_count = 0; | |
7155 | if (indbufend > indbuf) | |
7156 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7157 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7158 | shndx_count++; | |
7159 | ||
3ae181ee L |
7160 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7161 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7162 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7163 | if (ssymbuf == NULL) |
7164 | { | |
7165 | free (indbuf); | |
7166 | return NULL; | |
7167 | } | |
7168 | ||
3ae181ee | 7169 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7170 | ssymbuf->ssym = NULL; |
7171 | ssymbuf->count = shndx_count; | |
7172 | ssymbuf->st_shndx = 0; | |
7173 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7174 | { | |
7175 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7176 | { | |
7177 | ssymhead++; | |
7178 | ssymhead->ssym = ssym; | |
7179 | ssymhead->count = 0; | |
7180 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7181 | } | |
7182 | ssym->st_name = (*ind)->st_name; | |
7183 | ssym->st_info = (*ind)->st_info; | |
7184 | ssym->st_other = (*ind)->st_other; | |
7185 | ssymhead->count++; | |
7186 | } | |
3ae181ee L |
7187 | BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count |
7188 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) | |
7189 | == total_size)); | |
4d269e42 AM |
7190 | |
7191 | free (indbuf); | |
7192 | return ssymbuf; | |
7193 | } | |
7194 | ||
7195 | /* Check if 2 sections define the same set of local and global | |
7196 | symbols. */ | |
7197 | ||
8f317e31 | 7198 | static bfd_boolean |
4d269e42 AM |
7199 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7200 | struct bfd_link_info *info) | |
7201 | { | |
7202 | bfd *bfd1, *bfd2; | |
7203 | const struct elf_backend_data *bed1, *bed2; | |
7204 | Elf_Internal_Shdr *hdr1, *hdr2; | |
7205 | bfd_size_type symcount1, symcount2; | |
7206 | Elf_Internal_Sym *isymbuf1, *isymbuf2; | |
7207 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7208 | Elf_Internal_Sym *isym, *isymend; | |
7209 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
7210 | bfd_size_type count1, count2, i; | |
cb33740c | 7211 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7212 | bfd_boolean result; |
7213 | ||
7214 | bfd1 = sec1->owner; | |
7215 | bfd2 = sec2->owner; | |
7216 | ||
4d269e42 AM |
7217 | /* Both sections have to be in ELF. */ |
7218 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7219 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7220 | return FALSE; | |
7221 | ||
7222 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7223 | return FALSE; | |
7224 | ||
4d269e42 AM |
7225 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7226 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7227 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7228 | return FALSE; |
7229 | ||
7230 | bed1 = get_elf_backend_data (bfd1); | |
7231 | bed2 = get_elf_backend_data (bfd2); | |
7232 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7233 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7234 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7235 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7236 | ||
7237 | if (symcount1 == 0 || symcount2 == 0) | |
7238 | return FALSE; | |
7239 | ||
7240 | result = FALSE; | |
7241 | isymbuf1 = NULL; | |
7242 | isymbuf2 = NULL; | |
a50b1753 NC |
7243 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7244 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7245 | |
7246 | if (ssymbuf1 == NULL) | |
7247 | { | |
7248 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7249 | NULL, NULL, NULL); | |
7250 | if (isymbuf1 == NULL) | |
7251 | goto done; | |
7252 | ||
7253 | if (!info->reduce_memory_overheads) | |
7254 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7255 | = elf_create_symbuf (symcount1, isymbuf1); | |
7256 | } | |
7257 | ||
7258 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7259 | { | |
7260 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7261 | NULL, NULL, NULL); | |
7262 | if (isymbuf2 == NULL) | |
7263 | goto done; | |
7264 | ||
7265 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7266 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7267 | = elf_create_symbuf (symcount2, isymbuf2); | |
7268 | } | |
7269 | ||
7270 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7271 | { | |
7272 | /* Optimized faster version. */ | |
7273 | bfd_size_type lo, hi, mid; | |
7274 | struct elf_symbol *symp; | |
7275 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7276 | ||
7277 | lo = 0; | |
7278 | hi = ssymbuf1->count; | |
7279 | ssymbuf1++; | |
7280 | count1 = 0; | |
7281 | while (lo < hi) | |
7282 | { | |
7283 | mid = (lo + hi) / 2; | |
cb33740c | 7284 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7285 | hi = mid; |
cb33740c | 7286 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7287 | lo = mid + 1; |
7288 | else | |
7289 | { | |
7290 | count1 = ssymbuf1[mid].count; | |
7291 | ssymbuf1 += mid; | |
7292 | break; | |
7293 | } | |
7294 | } | |
7295 | ||
7296 | lo = 0; | |
7297 | hi = ssymbuf2->count; | |
7298 | ssymbuf2++; | |
7299 | count2 = 0; | |
7300 | while (lo < hi) | |
7301 | { | |
7302 | mid = (lo + hi) / 2; | |
cb33740c | 7303 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7304 | hi = mid; |
cb33740c | 7305 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7306 | lo = mid + 1; |
7307 | else | |
7308 | { | |
7309 | count2 = ssymbuf2[mid].count; | |
7310 | ssymbuf2 += mid; | |
7311 | break; | |
7312 | } | |
7313 | } | |
7314 | ||
7315 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7316 | goto done; | |
7317 | ||
ca4be51c AM |
7318 | symtable1 |
7319 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
7320 | symtable2 | |
7321 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
7322 | if (symtable1 == NULL || symtable2 == NULL) |
7323 | goto done; | |
7324 | ||
7325 | symp = symtable1; | |
7326 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7327 | ssym < ssymend; ssym++, symp++) | |
7328 | { | |
7329 | symp->u.ssym = ssym; | |
7330 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7331 | hdr1->sh_link, | |
7332 | ssym->st_name); | |
7333 | } | |
7334 | ||
7335 | symp = symtable2; | |
7336 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7337 | ssym < ssymend; ssym++, symp++) | |
7338 | { | |
7339 | symp->u.ssym = ssym; | |
7340 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7341 | hdr2->sh_link, | |
7342 | ssym->st_name); | |
7343 | } | |
7344 | ||
7345 | /* Sort symbol by name. */ | |
7346 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7347 | elf_sym_name_compare); | |
7348 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7349 | elf_sym_name_compare); | |
7350 | ||
7351 | for (i = 0; i < count1; i++) | |
7352 | /* Two symbols must have the same binding, type and name. */ | |
7353 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7354 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7355 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7356 | goto done; | |
7357 | ||
7358 | result = TRUE; | |
7359 | goto done; | |
7360 | } | |
7361 | ||
a50b1753 NC |
7362 | symtable1 = (struct elf_symbol *) |
7363 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7364 | symtable2 = (struct elf_symbol *) | |
7365 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7366 | if (symtable1 == NULL || symtable2 == NULL) |
7367 | goto done; | |
7368 | ||
7369 | /* Count definitions in the section. */ | |
7370 | count1 = 0; | |
7371 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7372 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7373 | symtable1[count1++].u.isym = isym; |
7374 | ||
7375 | count2 = 0; | |
7376 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7377 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7378 | symtable2[count2++].u.isym = isym; |
7379 | ||
7380 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7381 | goto done; | |
7382 | ||
7383 | for (i = 0; i < count1; i++) | |
7384 | symtable1[i].name | |
7385 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7386 | symtable1[i].u.isym->st_name); | |
7387 | ||
7388 | for (i = 0; i < count2; i++) | |
7389 | symtable2[i].name | |
7390 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7391 | symtable2[i].u.isym->st_name); | |
7392 | ||
7393 | /* Sort symbol by name. */ | |
7394 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7395 | elf_sym_name_compare); | |
7396 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7397 | elf_sym_name_compare); | |
7398 | ||
7399 | for (i = 0; i < count1; i++) | |
7400 | /* Two symbols must have the same binding, type and name. */ | |
7401 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7402 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7403 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7404 | goto done; | |
7405 | ||
7406 | result = TRUE; | |
7407 | ||
7408 | done: | |
7409 | if (symtable1) | |
7410 | free (symtable1); | |
7411 | if (symtable2) | |
7412 | free (symtable2); | |
7413 | if (isymbuf1) | |
7414 | free (isymbuf1); | |
7415 | if (isymbuf2) | |
7416 | free (isymbuf2); | |
7417 | ||
7418 | return result; | |
7419 | } | |
7420 | ||
7421 | /* Return TRUE if 2 section types are compatible. */ | |
7422 | ||
7423 | bfd_boolean | |
7424 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
7425 | bfd *bbfd, const asection *bsec) | |
7426 | { | |
7427 | if (asec == NULL | |
7428 | || bsec == NULL | |
7429 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
7430 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
7431 | return TRUE; | |
7432 | ||
7433 | return elf_section_type (asec) == elf_section_type (bsec); | |
7434 | } | |
7435 | \f | |
c152c796 AM |
7436 | /* Final phase of ELF linker. */ |
7437 | ||
7438 | /* A structure we use to avoid passing large numbers of arguments. */ | |
7439 | ||
7440 | struct elf_final_link_info | |
7441 | { | |
7442 | /* General link information. */ | |
7443 | struct bfd_link_info *info; | |
7444 | /* Output BFD. */ | |
7445 | bfd *output_bfd; | |
7446 | /* Symbol string table. */ | |
7447 | struct bfd_strtab_hash *symstrtab; | |
7448 | /* .dynsym section. */ | |
7449 | asection *dynsym_sec; | |
7450 | /* .hash section. */ | |
7451 | asection *hash_sec; | |
7452 | /* symbol version section (.gnu.version). */ | |
7453 | asection *symver_sec; | |
7454 | /* Buffer large enough to hold contents of any section. */ | |
7455 | bfd_byte *contents; | |
7456 | /* Buffer large enough to hold external relocs of any section. */ | |
7457 | void *external_relocs; | |
7458 | /* Buffer large enough to hold internal relocs of any section. */ | |
7459 | Elf_Internal_Rela *internal_relocs; | |
7460 | /* Buffer large enough to hold external local symbols of any input | |
7461 | BFD. */ | |
7462 | bfd_byte *external_syms; | |
7463 | /* And a buffer for symbol section indices. */ | |
7464 | Elf_External_Sym_Shndx *locsym_shndx; | |
7465 | /* Buffer large enough to hold internal local symbols of any input | |
7466 | BFD. */ | |
7467 | Elf_Internal_Sym *internal_syms; | |
7468 | /* Array large enough to hold a symbol index for each local symbol | |
7469 | of any input BFD. */ | |
7470 | long *indices; | |
7471 | /* Array large enough to hold a section pointer for each local | |
7472 | symbol of any input BFD. */ | |
7473 | asection **sections; | |
7474 | /* Buffer to hold swapped out symbols. */ | |
7475 | bfd_byte *symbuf; | |
7476 | /* And one for symbol section indices. */ | |
7477 | Elf_External_Sym_Shndx *symshndxbuf; | |
7478 | /* Number of swapped out symbols in buffer. */ | |
7479 | size_t symbuf_count; | |
7480 | /* Number of symbols which fit in symbuf. */ | |
7481 | size_t symbuf_size; | |
7482 | /* And same for symshndxbuf. */ | |
7483 | size_t shndxbuf_size; | |
ffbc01cc AM |
7484 | /* Number of STT_FILE syms seen. */ |
7485 | size_t filesym_count; | |
c152c796 AM |
7486 | }; |
7487 | ||
7488 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
7489 | ||
7490 | struct elf_outext_info | |
7491 | { | |
7492 | bfd_boolean failed; | |
7493 | bfd_boolean localsyms; | |
34a79995 | 7494 | bfd_boolean file_sym_done; |
8b127cbc | 7495 | struct elf_final_link_info *flinfo; |
c152c796 AM |
7496 | }; |
7497 | ||
d9352518 DB |
7498 | |
7499 | /* Support for evaluating a complex relocation. | |
7500 | ||
7501 | Complex relocations are generalized, self-describing relocations. The | |
7502 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 7503 | relocations themselves. |
d9352518 DB |
7504 | |
7505 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
7506 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
7507 | information (start bit, end bit, word width, etc) into the addend. This | |
7508 | information is extracted from CGEN-generated operand tables within gas. | |
7509 | ||
7510 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
7511 | internal) representing prefix-notation expressions, including but not | |
7512 | limited to those sorts of expressions normally encoded as addends in the | |
7513 | addend field. The symbol mangling format is: | |
7514 | ||
7515 | <node> := <literal> | |
7516 | | <unary-operator> ':' <node> | |
7517 | | <binary-operator> ':' <node> ':' <node> | |
7518 | ; | |
7519 | ||
7520 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
7521 | | 'S' <digits=N> ':' <N character section name> | |
7522 | | '#' <hexdigits> | |
7523 | ; | |
7524 | ||
7525 | <binary-operator> := as in C | |
7526 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
7527 | ||
7528 | static void | |
a0c8462f AM |
7529 | set_symbol_value (bfd *bfd_with_globals, |
7530 | Elf_Internal_Sym *isymbuf, | |
7531 | size_t locsymcount, | |
7532 | size_t symidx, | |
7533 | bfd_vma val) | |
d9352518 | 7534 | { |
8977835c AM |
7535 | struct elf_link_hash_entry **sym_hashes; |
7536 | struct elf_link_hash_entry *h; | |
7537 | size_t extsymoff = locsymcount; | |
d9352518 | 7538 | |
8977835c | 7539 | if (symidx < locsymcount) |
d9352518 | 7540 | { |
8977835c AM |
7541 | Elf_Internal_Sym *sym; |
7542 | ||
7543 | sym = isymbuf + symidx; | |
7544 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
7545 | { | |
7546 | /* It is a local symbol: move it to the | |
7547 | "absolute" section and give it a value. */ | |
7548 | sym->st_shndx = SHN_ABS; | |
7549 | sym->st_value = val; | |
7550 | return; | |
7551 | } | |
7552 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
7553 | extsymoff = 0; | |
d9352518 | 7554 | } |
8977835c AM |
7555 | |
7556 | /* It is a global symbol: set its link type | |
7557 | to "defined" and give it a value. */ | |
7558 | ||
7559 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
7560 | h = sym_hashes [symidx - extsymoff]; | |
7561 | while (h->root.type == bfd_link_hash_indirect | |
7562 | || h->root.type == bfd_link_hash_warning) | |
7563 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
7564 | h->root.type = bfd_link_hash_defined; | |
7565 | h->root.u.def.value = val; | |
7566 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
7567 | } |
7568 | ||
a0c8462f AM |
7569 | static bfd_boolean |
7570 | resolve_symbol (const char *name, | |
7571 | bfd *input_bfd, | |
8b127cbc | 7572 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
7573 | bfd_vma *result, |
7574 | Elf_Internal_Sym *isymbuf, | |
7575 | size_t locsymcount) | |
d9352518 | 7576 | { |
a0c8462f AM |
7577 | Elf_Internal_Sym *sym; |
7578 | struct bfd_link_hash_entry *global_entry; | |
7579 | const char *candidate = NULL; | |
7580 | Elf_Internal_Shdr *symtab_hdr; | |
7581 | size_t i; | |
7582 | ||
d9352518 DB |
7583 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
7584 | ||
7585 | for (i = 0; i < locsymcount; ++ i) | |
7586 | { | |
8977835c | 7587 | sym = isymbuf + i; |
d9352518 DB |
7588 | |
7589 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
7590 | continue; | |
7591 | ||
7592 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
7593 | symtab_hdr->sh_link, | |
7594 | sym->st_name); | |
7595 | #ifdef DEBUG | |
0f02bbd9 AM |
7596 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
7597 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
7598 | #endif |
7599 | if (candidate && strcmp (candidate, name) == 0) | |
7600 | { | |
8b127cbc | 7601 | asection *sec = flinfo->sections [i]; |
d9352518 | 7602 | |
0f02bbd9 AM |
7603 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
7604 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 7605 | #ifdef DEBUG |
0f02bbd9 AM |
7606 | printf ("Found symbol with value %8.8lx\n", |
7607 | (unsigned long) *result); | |
d9352518 DB |
7608 | #endif |
7609 | return TRUE; | |
7610 | } | |
7611 | } | |
7612 | ||
7613 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 7614 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 7615 | FALSE, FALSE, TRUE); |
d9352518 DB |
7616 | if (!global_entry) |
7617 | return FALSE; | |
a0c8462f | 7618 | |
d9352518 DB |
7619 | if (global_entry->type == bfd_link_hash_defined |
7620 | || global_entry->type == bfd_link_hash_defweak) | |
7621 | { | |
a0c8462f AM |
7622 | *result = (global_entry->u.def.value |
7623 | + global_entry->u.def.section->output_section->vma | |
7624 | + global_entry->u.def.section->output_offset); | |
d9352518 | 7625 | #ifdef DEBUG |
0f02bbd9 AM |
7626 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
7627 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
7628 | #endif |
7629 | return TRUE; | |
a0c8462f | 7630 | } |
d9352518 | 7631 | |
d9352518 DB |
7632 | return FALSE; |
7633 | } | |
7634 | ||
7635 | static bfd_boolean | |
a0c8462f AM |
7636 | resolve_section (const char *name, |
7637 | asection *sections, | |
7638 | bfd_vma *result) | |
d9352518 | 7639 | { |
a0c8462f AM |
7640 | asection *curr; |
7641 | unsigned int len; | |
d9352518 | 7642 | |
a0c8462f | 7643 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7644 | if (strcmp (curr->name, name) == 0) |
7645 | { | |
7646 | *result = curr->vma; | |
7647 | return TRUE; | |
7648 | } | |
7649 | ||
7650 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
a0c8462f | 7651 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
7652 | { |
7653 | len = strlen (curr->name); | |
a0c8462f | 7654 | if (len > strlen (name)) |
d9352518 DB |
7655 | continue; |
7656 | ||
7657 | if (strncmp (curr->name, name, len) == 0) | |
7658 | { | |
7659 | if (strncmp (".end", name + len, 4) == 0) | |
7660 | { | |
7661 | *result = curr->vma + curr->size; | |
7662 | return TRUE; | |
7663 | } | |
7664 | ||
7665 | /* Insert more pseudo-section names here, if you like. */ | |
7666 | } | |
7667 | } | |
a0c8462f | 7668 | |
d9352518 DB |
7669 | return FALSE; |
7670 | } | |
7671 | ||
7672 | static void | |
a0c8462f | 7673 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 7674 | { |
a0c8462f AM |
7675 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
7676 | reftype, name); | |
d9352518 DB |
7677 | } |
7678 | ||
7679 | static bfd_boolean | |
a0c8462f AM |
7680 | eval_symbol (bfd_vma *result, |
7681 | const char **symp, | |
7682 | bfd *input_bfd, | |
8b127cbc | 7683 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
7684 | bfd_vma dot, |
7685 | Elf_Internal_Sym *isymbuf, | |
7686 | size_t locsymcount, | |
7687 | int signed_p) | |
d9352518 | 7688 | { |
4b93929b NC |
7689 | size_t len; |
7690 | size_t symlen; | |
a0c8462f AM |
7691 | bfd_vma a; |
7692 | bfd_vma b; | |
4b93929b | 7693 | char symbuf[4096]; |
0f02bbd9 | 7694 | const char *sym = *symp; |
a0c8462f AM |
7695 | const char *symend; |
7696 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
7697 | |
7698 | len = strlen (sym); | |
7699 | symend = sym + len; | |
7700 | ||
4b93929b | 7701 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
7702 | { |
7703 | bfd_set_error (bfd_error_invalid_operation); | |
7704 | return FALSE; | |
7705 | } | |
a0c8462f | 7706 | |
d9352518 DB |
7707 | switch (* sym) |
7708 | { | |
7709 | case '.': | |
0f02bbd9 AM |
7710 | *result = dot; |
7711 | *symp = sym + 1; | |
d9352518 DB |
7712 | return TRUE; |
7713 | ||
7714 | case '#': | |
0f02bbd9 AM |
7715 | ++sym; |
7716 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
7717 | return TRUE; |
7718 | ||
7719 | case 'S': | |
7720 | symbol_is_section = TRUE; | |
a0c8462f | 7721 | case 's': |
0f02bbd9 AM |
7722 | ++sym; |
7723 | symlen = strtol (sym, (char **) symp, 10); | |
7724 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 7725 | |
4b93929b | 7726 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
7727 | { |
7728 | bfd_set_error (bfd_error_invalid_operation); | |
7729 | return FALSE; | |
7730 | } | |
7731 | ||
7732 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 7733 | symbuf[symlen] = '\0'; |
0f02bbd9 | 7734 | *symp = sym + symlen; |
a0c8462f AM |
7735 | |
7736 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
7737 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
7738 | interpretation here; section means "try section first", not "must be a | |
7739 | section", and likewise with symbol. */ | |
7740 | ||
a0c8462f | 7741 | if (symbol_is_section) |
d9352518 | 7742 | { |
8b127cbc AM |
7743 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result) |
7744 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, | |
8977835c | 7745 | isymbuf, locsymcount)) |
d9352518 DB |
7746 | { |
7747 | undefined_reference ("section", symbuf); | |
7748 | return FALSE; | |
7749 | } | |
a0c8462f AM |
7750 | } |
7751 | else | |
d9352518 | 7752 | { |
8b127cbc | 7753 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 7754 | isymbuf, locsymcount) |
8b127cbc | 7755 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
8977835c | 7756 | result)) |
d9352518 DB |
7757 | { |
7758 | undefined_reference ("symbol", symbuf); | |
7759 | return FALSE; | |
7760 | } | |
7761 | } | |
7762 | ||
7763 | return TRUE; | |
a0c8462f | 7764 | |
d9352518 DB |
7765 | /* All that remains are operators. */ |
7766 | ||
7767 | #define UNARY_OP(op) \ | |
7768 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
7769 | { \ | |
7770 | sym += strlen (#op); \ | |
a0c8462f AM |
7771 | if (*sym == ':') \ |
7772 | ++sym; \ | |
0f02bbd9 | 7773 | *symp = sym; \ |
8b127cbc | 7774 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 7775 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
7776 | return FALSE; \ |
7777 | if (signed_p) \ | |
0f02bbd9 | 7778 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
7779 | else \ |
7780 | *result = op a; \ | |
d9352518 DB |
7781 | return TRUE; \ |
7782 | } | |
7783 | ||
7784 | #define BINARY_OP(op) \ | |
7785 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
7786 | { \ | |
7787 | sym += strlen (#op); \ | |
a0c8462f AM |
7788 | if (*sym == ':') \ |
7789 | ++sym; \ | |
0f02bbd9 | 7790 | *symp = sym; \ |
8b127cbc | 7791 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 7792 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 7793 | return FALSE; \ |
0f02bbd9 | 7794 | ++*symp; \ |
8b127cbc | 7795 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 7796 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
7797 | return FALSE; \ |
7798 | if (signed_p) \ | |
0f02bbd9 | 7799 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
7800 | else \ |
7801 | *result = a op b; \ | |
d9352518 DB |
7802 | return TRUE; \ |
7803 | } | |
7804 | ||
7805 | default: | |
7806 | UNARY_OP (0-); | |
7807 | BINARY_OP (<<); | |
7808 | BINARY_OP (>>); | |
7809 | BINARY_OP (==); | |
7810 | BINARY_OP (!=); | |
7811 | BINARY_OP (<=); | |
7812 | BINARY_OP (>=); | |
7813 | BINARY_OP (&&); | |
7814 | BINARY_OP (||); | |
7815 | UNARY_OP (~); | |
7816 | UNARY_OP (!); | |
7817 | BINARY_OP (*); | |
7818 | BINARY_OP (/); | |
7819 | BINARY_OP (%); | |
7820 | BINARY_OP (^); | |
7821 | BINARY_OP (|); | |
7822 | BINARY_OP (&); | |
7823 | BINARY_OP (+); | |
7824 | BINARY_OP (-); | |
7825 | BINARY_OP (<); | |
7826 | BINARY_OP (>); | |
7827 | #undef UNARY_OP | |
7828 | #undef BINARY_OP | |
7829 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
7830 | bfd_set_error (bfd_error_invalid_operation); | |
7831 | return FALSE; | |
7832 | } | |
7833 | } | |
7834 | ||
d9352518 | 7835 | static void |
a0c8462f AM |
7836 | put_value (bfd_vma size, |
7837 | unsigned long chunksz, | |
7838 | bfd *input_bfd, | |
7839 | bfd_vma x, | |
7840 | bfd_byte *location) | |
d9352518 DB |
7841 | { |
7842 | location += (size - chunksz); | |
7843 | ||
41cd1ad1 | 7844 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
7845 | { |
7846 | switch (chunksz) | |
7847 | { | |
d9352518 DB |
7848 | case 1: |
7849 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 7850 | x >>= 8; |
d9352518 DB |
7851 | break; |
7852 | case 2: | |
7853 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 7854 | x >>= 16; |
d9352518 DB |
7855 | break; |
7856 | case 4: | |
7857 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
7858 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
7859 | x >>= 16; | |
7860 | x >>= 16; | |
d9352518 | 7861 | break; |
d9352518 | 7862 | #ifdef BFD64 |
41cd1ad1 | 7863 | case 8: |
d9352518 | 7864 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
7865 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
7866 | x >>= 32; | |
7867 | x >>= 32; | |
7868 | break; | |
d9352518 | 7869 | #endif |
41cd1ad1 NC |
7870 | default: |
7871 | abort (); | |
d9352518 DB |
7872 | break; |
7873 | } | |
7874 | } | |
7875 | } | |
7876 | ||
a0c8462f AM |
7877 | static bfd_vma |
7878 | get_value (bfd_vma size, | |
7879 | unsigned long chunksz, | |
7880 | bfd *input_bfd, | |
7881 | bfd_byte *location) | |
d9352518 | 7882 | { |
9b239e0e | 7883 | int shift; |
d9352518 DB |
7884 | bfd_vma x = 0; |
7885 | ||
9b239e0e NC |
7886 | /* Sanity checks. */ |
7887 | BFD_ASSERT (chunksz <= sizeof (x) | |
7888 | && size >= chunksz | |
7889 | && chunksz != 0 | |
7890 | && (size % chunksz) == 0 | |
7891 | && input_bfd != NULL | |
7892 | && location != NULL); | |
7893 | ||
7894 | if (chunksz == sizeof (x)) | |
7895 | { | |
7896 | BFD_ASSERT (size == chunksz); | |
7897 | ||
7898 | /* Make sure that we do not perform an undefined shift operation. | |
7899 | We know that size == chunksz so there will only be one iteration | |
7900 | of the loop below. */ | |
7901 | shift = 0; | |
7902 | } | |
7903 | else | |
7904 | shift = 8 * chunksz; | |
7905 | ||
a0c8462f | 7906 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
7907 | { |
7908 | switch (chunksz) | |
7909 | { | |
d9352518 | 7910 | case 1: |
9b239e0e | 7911 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
7912 | break; |
7913 | case 2: | |
9b239e0e | 7914 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
7915 | break; |
7916 | case 4: | |
9b239e0e | 7917 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 7918 | break; |
d9352518 | 7919 | #ifdef BFD64 |
9b239e0e NC |
7920 | case 8: |
7921 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 7922 | break; |
9b239e0e NC |
7923 | #endif |
7924 | default: | |
7925 | abort (); | |
d9352518 DB |
7926 | } |
7927 | } | |
7928 | return x; | |
7929 | } | |
7930 | ||
a0c8462f AM |
7931 | static void |
7932 | decode_complex_addend (unsigned long *start, /* in bits */ | |
7933 | unsigned long *oplen, /* in bits */ | |
7934 | unsigned long *len, /* in bits */ | |
7935 | unsigned long *wordsz, /* in bytes */ | |
7936 | unsigned long *chunksz, /* in bytes */ | |
7937 | unsigned long *lsb0_p, | |
7938 | unsigned long *signed_p, | |
7939 | unsigned long *trunc_p, | |
7940 | unsigned long encoded) | |
d9352518 DB |
7941 | { |
7942 | * start = encoded & 0x3F; | |
7943 | * len = (encoded >> 6) & 0x3F; | |
7944 | * oplen = (encoded >> 12) & 0x3F; | |
7945 | * wordsz = (encoded >> 18) & 0xF; | |
7946 | * chunksz = (encoded >> 22) & 0xF; | |
7947 | * lsb0_p = (encoded >> 27) & 1; | |
7948 | * signed_p = (encoded >> 28) & 1; | |
7949 | * trunc_p = (encoded >> 29) & 1; | |
7950 | } | |
7951 | ||
cdfeee4f | 7952 | bfd_reloc_status_type |
0f02bbd9 | 7953 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 7954 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
7955 | bfd_byte *contents, |
7956 | Elf_Internal_Rela *rel, | |
7957 | bfd_vma relocation) | |
d9352518 | 7958 | { |
0f02bbd9 AM |
7959 | bfd_vma shift, x, mask; |
7960 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 7961 | bfd_reloc_status_type r; |
d9352518 DB |
7962 | |
7963 | /* Perform this reloc, since it is complex. | |
7964 | (this is not to say that it necessarily refers to a complex | |
7965 | symbol; merely that it is a self-describing CGEN based reloc. | |
7966 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 7967 | word size, etc) encoded within it.). */ |
d9352518 | 7968 | |
a0c8462f AM |
7969 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
7970 | &chunksz, &lsb0_p, &signed_p, | |
7971 | &trunc_p, rel->r_addend); | |
d9352518 DB |
7972 | |
7973 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
7974 | ||
7975 | if (lsb0_p) | |
7976 | shift = (start + 1) - len; | |
7977 | else | |
7978 | shift = (8 * wordsz) - (start + len); | |
7979 | ||
5dabe785 | 7980 | /* FIXME: octets_per_byte. */ |
a0c8462f | 7981 | x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset); |
d9352518 DB |
7982 | |
7983 | #ifdef DEBUG | |
7984 | printf ("Doing complex reloc: " | |
7985 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
7986 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
7987 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
7988 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
7989 | oplen, (unsigned long) x, (unsigned long) mask, |
7990 | (unsigned long) relocation); | |
d9352518 DB |
7991 | #endif |
7992 | ||
cdfeee4f | 7993 | r = bfd_reloc_ok; |
d9352518 | 7994 | if (! trunc_p) |
cdfeee4f AM |
7995 | /* Now do an overflow check. */ |
7996 | r = bfd_check_overflow ((signed_p | |
7997 | ? complain_overflow_signed | |
7998 | : complain_overflow_unsigned), | |
7999 | len, 0, (8 * wordsz), | |
8000 | relocation); | |
a0c8462f | 8001 | |
d9352518 DB |
8002 | /* Do the deed. */ |
8003 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8004 | ||
8005 | #ifdef DEBUG | |
8006 | printf (" relocation: %8.8lx\n" | |
8007 | " shifted mask: %8.8lx\n" | |
8008 | " shifted/masked reloc: %8.8lx\n" | |
8009 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8010 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8011 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8012 | #endif |
5dabe785 | 8013 | /* FIXME: octets_per_byte. */ |
d9352518 | 8014 | put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset); |
cdfeee4f | 8015 | return r; |
d9352518 DB |
8016 | } |
8017 | ||
53df40a4 AM |
8018 | /* qsort comparison functions sorting external relocs by r_offset. */ |
8019 | ||
8020 | static int | |
8021 | cmp_ext32l_r_offset (const void *p, const void *q) | |
8022 | { | |
8023 | union aligned32 | |
8024 | { | |
8025 | uint32_t v; | |
8026 | unsigned char c[4]; | |
8027 | }; | |
8028 | const union aligned32 *a | |
8029 | = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset; | |
8030 | const union aligned32 *b | |
8031 | = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset; | |
8032 | ||
8033 | uint32_t aval = ( (uint32_t) a->c[0] | |
8034 | | (uint32_t) a->c[1] << 8 | |
8035 | | (uint32_t) a->c[2] << 16 | |
8036 | | (uint32_t) a->c[3] << 24); | |
8037 | uint32_t bval = ( (uint32_t) b->c[0] | |
8038 | | (uint32_t) b->c[1] << 8 | |
8039 | | (uint32_t) b->c[2] << 16 | |
8040 | | (uint32_t) b->c[3] << 24); | |
8041 | if (aval < bval) | |
8042 | return -1; | |
8043 | else if (aval > bval) | |
8044 | return 1; | |
8045 | return 0; | |
8046 | } | |
8047 | ||
8048 | static int | |
8049 | cmp_ext32b_r_offset (const void *p, const void *q) | |
8050 | { | |
8051 | union aligned32 | |
8052 | { | |
8053 | uint32_t v; | |
8054 | unsigned char c[4]; | |
8055 | }; | |
8056 | const union aligned32 *a | |
8057 | = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset; | |
8058 | const union aligned32 *b | |
8059 | = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset; | |
8060 | ||
8061 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8062 | | (uint32_t) a->c[1] << 16 | |
8063 | | (uint32_t) a->c[2] << 8 | |
8064 | | (uint32_t) a->c[3]); | |
8065 | uint32_t bval = ( (uint32_t) b->c[0] << 24 | |
8066 | | (uint32_t) b->c[1] << 16 | |
8067 | | (uint32_t) b->c[2] << 8 | |
8068 | | (uint32_t) b->c[3]); | |
8069 | if (aval < bval) | |
8070 | return -1; | |
8071 | else if (aval > bval) | |
8072 | return 1; | |
8073 | return 0; | |
8074 | } | |
8075 | ||
8076 | #ifdef BFD_HOST_64_BIT | |
8077 | static int | |
8078 | cmp_ext64l_r_offset (const void *p, const void *q) | |
8079 | { | |
8080 | union aligned64 | |
8081 | { | |
8082 | uint64_t v; | |
8083 | unsigned char c[8]; | |
8084 | }; | |
8085 | const union aligned64 *a | |
8086 | = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset; | |
8087 | const union aligned64 *b | |
8088 | = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset; | |
8089 | ||
8090 | uint64_t aval = ( (uint64_t) a->c[0] | |
8091 | | (uint64_t) a->c[1] << 8 | |
8092 | | (uint64_t) a->c[2] << 16 | |
8093 | | (uint64_t) a->c[3] << 24 | |
8094 | | (uint64_t) a->c[4] << 32 | |
8095 | | (uint64_t) a->c[5] << 40 | |
8096 | | (uint64_t) a->c[6] << 48 | |
8097 | | (uint64_t) a->c[7] << 56); | |
8098 | uint64_t bval = ( (uint64_t) b->c[0] | |
8099 | | (uint64_t) b->c[1] << 8 | |
8100 | | (uint64_t) b->c[2] << 16 | |
8101 | | (uint64_t) b->c[3] << 24 | |
8102 | | (uint64_t) b->c[4] << 32 | |
8103 | | (uint64_t) b->c[5] << 40 | |
8104 | | (uint64_t) b->c[6] << 48 | |
8105 | | (uint64_t) b->c[7] << 56); | |
8106 | if (aval < bval) | |
8107 | return -1; | |
8108 | else if (aval > bval) | |
8109 | return 1; | |
8110 | return 0; | |
8111 | } | |
8112 | ||
8113 | static int | |
8114 | cmp_ext64b_r_offset (const void *p, const void *q) | |
8115 | { | |
8116 | union aligned64 | |
8117 | { | |
8118 | uint64_t v; | |
8119 | unsigned char c[8]; | |
8120 | }; | |
8121 | const union aligned64 *a | |
8122 | = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset; | |
8123 | const union aligned64 *b | |
8124 | = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset; | |
8125 | ||
8126 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8127 | | (uint64_t) a->c[1] << 48 | |
8128 | | (uint64_t) a->c[2] << 40 | |
8129 | | (uint64_t) a->c[3] << 32 | |
8130 | | (uint64_t) a->c[4] << 24 | |
8131 | | (uint64_t) a->c[5] << 16 | |
8132 | | (uint64_t) a->c[6] << 8 | |
8133 | | (uint64_t) a->c[7]); | |
8134 | uint64_t bval = ( (uint64_t) b->c[0] << 56 | |
8135 | | (uint64_t) b->c[1] << 48 | |
8136 | | (uint64_t) b->c[2] << 40 | |
8137 | | (uint64_t) b->c[3] << 32 | |
8138 | | (uint64_t) b->c[4] << 24 | |
8139 | | (uint64_t) b->c[5] << 16 | |
8140 | | (uint64_t) b->c[6] << 8 | |
8141 | | (uint64_t) b->c[7]); | |
8142 | if (aval < bval) | |
8143 | return -1; | |
8144 | else if (aval > bval) | |
8145 | return 1; | |
8146 | return 0; | |
8147 | } | |
8148 | #endif | |
8149 | ||
c152c796 AM |
8150 | /* When performing a relocatable link, the input relocations are |
8151 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8152 | referenced must be updated. Update all the relocations found in |
8153 | RELDATA. */ | |
c152c796 AM |
8154 | |
8155 | static void | |
8156 | elf_link_adjust_relocs (bfd *abfd, | |
28dbcedc AM |
8157 | struct bfd_elf_section_reloc_data *reldata, |
8158 | bfd_boolean sort) | |
c152c796 AM |
8159 | { |
8160 | unsigned int i; | |
8161 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8162 | bfd_byte *erela; | |
8163 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8164 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8165 | bfd_vma r_type_mask; | |
8166 | int r_sym_shift; | |
d4730f92 BS |
8167 | unsigned int count = reldata->count; |
8168 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8169 | |
d4730f92 | 8170 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8171 | { |
8172 | swap_in = bed->s->swap_reloc_in; | |
8173 | swap_out = bed->s->swap_reloc_out; | |
8174 | } | |
d4730f92 | 8175 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8176 | { |
8177 | swap_in = bed->s->swap_reloca_in; | |
8178 | swap_out = bed->s->swap_reloca_out; | |
8179 | } | |
8180 | else | |
8181 | abort (); | |
8182 | ||
8183 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8184 | abort (); | |
8185 | ||
8186 | if (bed->s->arch_size == 32) | |
8187 | { | |
8188 | r_type_mask = 0xff; | |
8189 | r_sym_shift = 8; | |
8190 | } | |
8191 | else | |
8192 | { | |
8193 | r_type_mask = 0xffffffff; | |
8194 | r_sym_shift = 32; | |
8195 | } | |
8196 | ||
d4730f92 BS |
8197 | erela = reldata->hdr->contents; |
8198 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8199 | { |
8200 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8201 | unsigned int j; | |
8202 | ||
8203 | if (*rel_hash == NULL) | |
8204 | continue; | |
8205 | ||
8206 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
8207 | ||
8208 | (*swap_in) (abfd, erela, irela); | |
8209 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8210 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8211 | | (irela[j].r_info & r_type_mask)); | |
8212 | (*swap_out) (abfd, irela, erela); | |
8213 | } | |
53df40a4 | 8214 | |
28dbcedc | 8215 | if (sort) |
53df40a4 | 8216 | { |
28dbcedc AM |
8217 | int (*compare) (const void *, const void *); |
8218 | ||
8219 | if (bed->s->arch_size == 32) | |
8220 | { | |
8221 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
8222 | compare = cmp_ext32l_r_offset; | |
8223 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) | |
8224 | compare = cmp_ext32b_r_offset; | |
8225 | else | |
8226 | abort (); | |
8227 | } | |
53df40a4 | 8228 | else |
28dbcedc | 8229 | { |
53df40a4 | 8230 | #ifdef BFD_HOST_64_BIT |
28dbcedc AM |
8231 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
8232 | compare = cmp_ext64l_r_offset; | |
8233 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) | |
8234 | compare = cmp_ext64b_r_offset; | |
8235 | else | |
53df40a4 | 8236 | #endif |
28dbcedc AM |
8237 | abort (); |
8238 | } | |
8239 | qsort (reldata->hdr->contents, count, reldata->hdr->sh_entsize, compare); | |
8240 | free (reldata->hashes); | |
8241 | reldata->hashes = NULL; | |
53df40a4 | 8242 | } |
c152c796 AM |
8243 | } |
8244 | ||
8245 | struct elf_link_sort_rela | |
8246 | { | |
8247 | union { | |
8248 | bfd_vma offset; | |
8249 | bfd_vma sym_mask; | |
8250 | } u; | |
8251 | enum elf_reloc_type_class type; | |
8252 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8253 | Elf_Internal_Rela rela[1]; | |
8254 | }; | |
8255 | ||
8256 | static int | |
8257 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8258 | { | |
a50b1753 NC |
8259 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8260 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8261 | int relativea, relativeb; |
8262 | ||
8263 | relativea = a->type == reloc_class_relative; | |
8264 | relativeb = b->type == reloc_class_relative; | |
8265 | ||
8266 | if (relativea < relativeb) | |
8267 | return 1; | |
8268 | if (relativea > relativeb) | |
8269 | return -1; | |
8270 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
8271 | return -1; | |
8272 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
8273 | return 1; | |
8274 | if (a->rela->r_offset < b->rela->r_offset) | |
8275 | return -1; | |
8276 | if (a->rela->r_offset > b->rela->r_offset) | |
8277 | return 1; | |
8278 | return 0; | |
8279 | } | |
8280 | ||
8281 | static int | |
8282 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8283 | { | |
a50b1753 NC |
8284 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8285 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 8286 | |
7e612e98 | 8287 | if (a->type < b->type) |
c152c796 | 8288 | return -1; |
7e612e98 | 8289 | if (a->type > b->type) |
c152c796 | 8290 | return 1; |
7e612e98 | 8291 | if (a->u.offset < b->u.offset) |
c152c796 | 8292 | return -1; |
7e612e98 | 8293 | if (a->u.offset > b->u.offset) |
c152c796 AM |
8294 | return 1; |
8295 | if (a->rela->r_offset < b->rela->r_offset) | |
8296 | return -1; | |
8297 | if (a->rela->r_offset > b->rela->r_offset) | |
8298 | return 1; | |
8299 | return 0; | |
8300 | } | |
8301 | ||
8302 | static size_t | |
8303 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
8304 | { | |
3410fea8 | 8305 | asection *dynamic_relocs; |
fc66a176 L |
8306 | asection *rela_dyn; |
8307 | asection *rel_dyn; | |
c152c796 AM |
8308 | bfd_size_type count, size; |
8309 | size_t i, ret, sort_elt, ext_size; | |
8310 | bfd_byte *sort, *s_non_relative, *p; | |
8311 | struct elf_link_sort_rela *sq; | |
8312 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8313 | int i2e = bed->s->int_rels_per_ext_rel; | |
8314 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8315 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8316 | struct bfd_link_order *lo; | |
8317 | bfd_vma r_sym_mask; | |
3410fea8 | 8318 | bfd_boolean use_rela; |
c152c796 | 8319 | |
3410fea8 NC |
8320 | /* Find a dynamic reloc section. */ |
8321 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
8322 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
8323 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
8324 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 8325 | { |
3410fea8 NC |
8326 | bfd_boolean use_rela_initialised = FALSE; |
8327 | ||
8328 | /* This is just here to stop gcc from complaining. | |
8329 | It's initialization checking code is not perfect. */ | |
8330 | use_rela = TRUE; | |
8331 | ||
8332 | /* Both sections are present. Examine the sizes | |
8333 | of the indirect sections to help us choose. */ | |
8334 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8335 | if (lo->type == bfd_indirect_link_order) | |
8336 | { | |
8337 | asection *o = lo->u.indirect.section; | |
8338 | ||
8339 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8340 | { | |
8341 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8342 | /* Section size is divisible by both rel and rela sizes. | |
8343 | It is of no help to us. */ | |
8344 | ; | |
8345 | else | |
8346 | { | |
8347 | /* Section size is only divisible by rela. */ | |
8348 | if (use_rela_initialised && (use_rela == FALSE)) | |
8349 | { | |
8350 | _bfd_error_handler | |
8351 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
8352 | bfd_set_error (bfd_error_invalid_operation); | |
8353 | return 0; | |
8354 | } | |
8355 | else | |
8356 | { | |
8357 | use_rela = TRUE; | |
8358 | use_rela_initialised = TRUE; | |
8359 | } | |
8360 | } | |
8361 | } | |
8362 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8363 | { | |
8364 | /* Section size is only divisible by rel. */ | |
8365 | if (use_rela_initialised && (use_rela == TRUE)) | |
8366 | { | |
8367 | _bfd_error_handler | |
8368 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
8369 | bfd_set_error (bfd_error_invalid_operation); | |
8370 | return 0; | |
8371 | } | |
8372 | else | |
8373 | { | |
8374 | use_rela = FALSE; | |
8375 | use_rela_initialised = TRUE; | |
8376 | } | |
8377 | } | |
8378 | else | |
8379 | { | |
8380 | /* The section size is not divisible by either - something is wrong. */ | |
8381 | _bfd_error_handler | |
8382 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
8383 | bfd_set_error (bfd_error_invalid_operation); | |
8384 | return 0; | |
8385 | } | |
8386 | } | |
8387 | ||
8388 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8389 | if (lo->type == bfd_indirect_link_order) | |
8390 | { | |
8391 | asection *o = lo->u.indirect.section; | |
8392 | ||
8393 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8394 | { | |
8395 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8396 | /* Section size is divisible by both rel and rela sizes. | |
8397 | It is of no help to us. */ | |
8398 | ; | |
8399 | else | |
8400 | { | |
8401 | /* Section size is only divisible by rela. */ | |
8402 | if (use_rela_initialised && (use_rela == FALSE)) | |
8403 | { | |
8404 | _bfd_error_handler | |
8405 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
8406 | bfd_set_error (bfd_error_invalid_operation); | |
8407 | return 0; | |
8408 | } | |
8409 | else | |
8410 | { | |
8411 | use_rela = TRUE; | |
8412 | use_rela_initialised = TRUE; | |
8413 | } | |
8414 | } | |
8415 | } | |
8416 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8417 | { | |
8418 | /* Section size is only divisible by rel. */ | |
8419 | if (use_rela_initialised && (use_rela == TRUE)) | |
8420 | { | |
8421 | _bfd_error_handler | |
8422 | (_("%B: Unable to sort relocs - they are in more than one size"), abfd); | |
8423 | bfd_set_error (bfd_error_invalid_operation); | |
8424 | return 0; | |
8425 | } | |
8426 | else | |
8427 | { | |
8428 | use_rela = FALSE; | |
8429 | use_rela_initialised = TRUE; | |
8430 | } | |
8431 | } | |
8432 | else | |
8433 | { | |
8434 | /* The section size is not divisible by either - something is wrong. */ | |
8435 | _bfd_error_handler | |
8436 | (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); | |
8437 | bfd_set_error (bfd_error_invalid_operation); | |
8438 | return 0; | |
8439 | } | |
8440 | } | |
8441 | ||
8442 | if (! use_rela_initialised) | |
8443 | /* Make a guess. */ | |
8444 | use_rela = TRUE; | |
c152c796 | 8445 | } |
fc66a176 L |
8446 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
8447 | use_rela = TRUE; | |
8448 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 8449 | use_rela = FALSE; |
c152c796 | 8450 | else |
fc66a176 | 8451 | return 0; |
3410fea8 NC |
8452 | |
8453 | if (use_rela) | |
c152c796 | 8454 | { |
3410fea8 | 8455 | dynamic_relocs = rela_dyn; |
c152c796 AM |
8456 | ext_size = bed->s->sizeof_rela; |
8457 | swap_in = bed->s->swap_reloca_in; | |
8458 | swap_out = bed->s->swap_reloca_out; | |
8459 | } | |
3410fea8 NC |
8460 | else |
8461 | { | |
8462 | dynamic_relocs = rel_dyn; | |
8463 | ext_size = bed->s->sizeof_rel; | |
8464 | swap_in = bed->s->swap_reloc_in; | |
8465 | swap_out = bed->s->swap_reloc_out; | |
8466 | } | |
c152c796 AM |
8467 | |
8468 | size = 0; | |
3410fea8 | 8469 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 8470 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 8471 | size += lo->u.indirect.section->size; |
c152c796 | 8472 | |
3410fea8 | 8473 | if (size != dynamic_relocs->size) |
c152c796 AM |
8474 | return 0; |
8475 | ||
8476 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
8477 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
8478 | |
8479 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
8480 | if (count == 0) |
8481 | return 0; | |
a50b1753 | 8482 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 8483 | |
c152c796 AM |
8484 | if (sort == NULL) |
8485 | { | |
8486 | (*info->callbacks->warning) | |
8487 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
8488 | return 0; | |
8489 | } | |
8490 | ||
8491 | if (bed->s->arch_size == 32) | |
8492 | r_sym_mask = ~(bfd_vma) 0xff; | |
8493 | else | |
8494 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
8495 | ||
3410fea8 | 8496 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8497 | if (lo->type == bfd_indirect_link_order) |
8498 | { | |
8499 | bfd_byte *erel, *erelend; | |
8500 | asection *o = lo->u.indirect.section; | |
8501 | ||
1da212d6 AM |
8502 | if (o->contents == NULL && o->size != 0) |
8503 | { | |
8504 | /* This is a reloc section that is being handled as a normal | |
8505 | section. See bfd_section_from_shdr. We can't combine | |
8506 | relocs in this case. */ | |
8507 | free (sort); | |
8508 | return 0; | |
8509 | } | |
c152c796 | 8510 | erel = o->contents; |
eea6121a | 8511 | erelend = o->contents + o->size; |
5dabe785 | 8512 | /* FIXME: octets_per_byte. */ |
c152c796 | 8513 | p = sort + o->output_offset / ext_size * sort_elt; |
3410fea8 | 8514 | |
c152c796 AM |
8515 | while (erel < erelend) |
8516 | { | |
8517 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 8518 | |
c152c796 | 8519 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 8520 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
8521 | s->u.sym_mask = r_sym_mask; |
8522 | p += sort_elt; | |
8523 | erel += ext_size; | |
8524 | } | |
8525 | } | |
8526 | ||
8527 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
8528 | ||
8529 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
8530 | { | |
8531 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8532 | if (s->type != reloc_class_relative) | |
8533 | break; | |
8534 | } | |
8535 | ret = i; | |
8536 | s_non_relative = p; | |
8537 | ||
8538 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
8539 | for (; i < count; i++, p += sort_elt) | |
8540 | { | |
8541 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
8542 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
8543 | sq = sp; | |
8544 | sp->u.offset = sq->rela->r_offset; | |
8545 | } | |
8546 | ||
8547 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
8548 | ||
3410fea8 | 8549 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
8550 | if (lo->type == bfd_indirect_link_order) |
8551 | { | |
8552 | bfd_byte *erel, *erelend; | |
8553 | asection *o = lo->u.indirect.section; | |
8554 | ||
8555 | erel = o->contents; | |
eea6121a | 8556 | erelend = o->contents + o->size; |
5dabe785 | 8557 | /* FIXME: octets_per_byte. */ |
c152c796 AM |
8558 | p = sort + o->output_offset / ext_size * sort_elt; |
8559 | while (erel < erelend) | |
8560 | { | |
8561 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
8562 | (*swap_out) (abfd, s->rela, erel); | |
8563 | p += sort_elt; | |
8564 | erel += ext_size; | |
8565 | } | |
8566 | } | |
8567 | ||
8568 | free (sort); | |
3410fea8 | 8569 | *psec = dynamic_relocs; |
c152c796 AM |
8570 | return ret; |
8571 | } | |
8572 | ||
8573 | /* Flush the output symbols to the file. */ | |
8574 | ||
8575 | static bfd_boolean | |
8b127cbc | 8576 | elf_link_flush_output_syms (struct elf_final_link_info *flinfo, |
c152c796 AM |
8577 | const struct elf_backend_data *bed) |
8578 | { | |
8b127cbc | 8579 | if (flinfo->symbuf_count > 0) |
c152c796 AM |
8580 | { |
8581 | Elf_Internal_Shdr *hdr; | |
8582 | file_ptr pos; | |
8583 | bfd_size_type amt; | |
8584 | ||
8b127cbc | 8585 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; |
c152c796 | 8586 | pos = hdr->sh_offset + hdr->sh_size; |
8b127cbc AM |
8587 | amt = flinfo->symbuf_count * bed->s->sizeof_sym; |
8588 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0 | |
8589 | || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt) | |
c152c796 AM |
8590 | return FALSE; |
8591 | ||
8592 | hdr->sh_size += amt; | |
8b127cbc | 8593 | flinfo->symbuf_count = 0; |
c152c796 AM |
8594 | } |
8595 | ||
8596 | return TRUE; | |
8597 | } | |
8598 | ||
8599 | /* Add a symbol to the output symbol table. */ | |
8600 | ||
6e0b88f1 | 8601 | static int |
8b127cbc | 8602 | elf_link_output_sym (struct elf_final_link_info *flinfo, |
c152c796 AM |
8603 | const char *name, |
8604 | Elf_Internal_Sym *elfsym, | |
8605 | asection *input_sec, | |
8606 | struct elf_link_hash_entry *h) | |
8607 | { | |
8608 | bfd_byte *dest; | |
8609 | Elf_External_Sym_Shndx *destshndx; | |
6e0b88f1 | 8610 | int (*output_symbol_hook) |
c152c796 AM |
8611 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
8612 | struct elf_link_hash_entry *); | |
8613 | const struct elf_backend_data *bed; | |
8614 | ||
8539e4e8 AM |
8615 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
8616 | ||
8b127cbc | 8617 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
8618 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
8619 | if (output_symbol_hook != NULL) | |
8620 | { | |
8b127cbc | 8621 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
8622 | if (ret != 1) |
8623 | return ret; | |
c152c796 AM |
8624 | } |
8625 | ||
8626 | if (name == NULL || *name == '\0') | |
8627 | elfsym->st_name = 0; | |
8628 | else if (input_sec->flags & SEC_EXCLUDE) | |
8629 | elfsym->st_name = 0; | |
8630 | else | |
8631 | { | |
8b127cbc | 8632 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab, |
c152c796 AM |
8633 | name, TRUE, FALSE); |
8634 | if (elfsym->st_name == (unsigned long) -1) | |
6e0b88f1 | 8635 | return 0; |
c152c796 AM |
8636 | } |
8637 | ||
8b127cbc | 8638 | if (flinfo->symbuf_count >= flinfo->symbuf_size) |
c152c796 | 8639 | { |
8b127cbc | 8640 | if (! elf_link_flush_output_syms (flinfo, bed)) |
6e0b88f1 | 8641 | return 0; |
c152c796 AM |
8642 | } |
8643 | ||
8b127cbc AM |
8644 | dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym; |
8645 | destshndx = flinfo->symshndxbuf; | |
c152c796 AM |
8646 | if (destshndx != NULL) |
8647 | { | |
8b127cbc | 8648 | if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size) |
c152c796 AM |
8649 | { |
8650 | bfd_size_type amt; | |
8651 | ||
8b127cbc | 8652 | amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); |
a50b1753 | 8653 | destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx, |
ca4be51c | 8654 | amt * 2); |
c152c796 | 8655 | if (destshndx == NULL) |
6e0b88f1 | 8656 | return 0; |
8b127cbc | 8657 | flinfo->symshndxbuf = destshndx; |
c152c796 | 8658 | memset ((char *) destshndx + amt, 0, amt); |
8b127cbc | 8659 | flinfo->shndxbuf_size *= 2; |
c152c796 | 8660 | } |
8b127cbc | 8661 | destshndx += bfd_get_symcount (flinfo->output_bfd); |
c152c796 AM |
8662 | } |
8663 | ||
8b127cbc AM |
8664 | bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx); |
8665 | flinfo->symbuf_count += 1; | |
8666 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
c152c796 | 8667 | |
6e0b88f1 | 8668 | return 1; |
c152c796 AM |
8669 | } |
8670 | ||
c0d5a53d L |
8671 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
8672 | ||
8673 | static bfd_boolean | |
8674 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
8675 | { | |
4fbb74a6 AM |
8676 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
8677 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
8678 | { |
8679 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 8680 | beyond 64k. */ |
c0d5a53d L |
8681 | (*_bfd_error_handler) |
8682 | (_("%B: Too many sections: %d (>= %d)"), | |
4fbb74a6 | 8683 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
8684 | bfd_set_error (bfd_error_nonrepresentable_section); |
8685 | return FALSE; | |
8686 | } | |
8687 | return TRUE; | |
8688 | } | |
8689 | ||
c152c796 AM |
8690 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
8691 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
8692 | versioned symbol that would normally require an explicit version. | |
8693 | We also handle the case that a DSO references a hidden symbol | |
8694 | which may be satisfied by a versioned symbol in another DSO. */ | |
8695 | ||
8696 | static bfd_boolean | |
8697 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
8698 | const struct elf_backend_data *bed, | |
8699 | struct elf_link_hash_entry *h) | |
8700 | { | |
8701 | bfd *abfd; | |
8702 | struct elf_link_loaded_list *loaded; | |
8703 | ||
8704 | if (!is_elf_hash_table (info->hash)) | |
8705 | return FALSE; | |
8706 | ||
90c984fc L |
8707 | /* Check indirect symbol. */ |
8708 | while (h->root.type == bfd_link_hash_indirect) | |
8709 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8710 | ||
c152c796 AM |
8711 | switch (h->root.type) |
8712 | { | |
8713 | default: | |
8714 | abfd = NULL; | |
8715 | break; | |
8716 | ||
8717 | case bfd_link_hash_undefined: | |
8718 | case bfd_link_hash_undefweak: | |
8719 | abfd = h->root.u.undef.abfd; | |
8720 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 8721 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
8722 | return FALSE; |
8723 | break; | |
8724 | ||
8725 | case bfd_link_hash_defined: | |
8726 | case bfd_link_hash_defweak: | |
8727 | abfd = h->root.u.def.section->owner; | |
8728 | break; | |
8729 | ||
8730 | case bfd_link_hash_common: | |
8731 | abfd = h->root.u.c.p->section->owner; | |
8732 | break; | |
8733 | } | |
8734 | BFD_ASSERT (abfd != NULL); | |
8735 | ||
8736 | for (loaded = elf_hash_table (info)->loaded; | |
8737 | loaded != NULL; | |
8738 | loaded = loaded->next) | |
8739 | { | |
8740 | bfd *input; | |
8741 | Elf_Internal_Shdr *hdr; | |
8742 | bfd_size_type symcount; | |
8743 | bfd_size_type extsymcount; | |
8744 | bfd_size_type extsymoff; | |
8745 | Elf_Internal_Shdr *versymhdr; | |
8746 | Elf_Internal_Sym *isym; | |
8747 | Elf_Internal_Sym *isymend; | |
8748 | Elf_Internal_Sym *isymbuf; | |
8749 | Elf_External_Versym *ever; | |
8750 | Elf_External_Versym *extversym; | |
8751 | ||
8752 | input = loaded->abfd; | |
8753 | ||
8754 | /* We check each DSO for a possible hidden versioned definition. */ | |
8755 | if (input == abfd | |
8756 | || (input->flags & DYNAMIC) == 0 | |
8757 | || elf_dynversym (input) == 0) | |
8758 | continue; | |
8759 | ||
8760 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
8761 | ||
8762 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
8763 | if (elf_bad_symtab (input)) | |
8764 | { | |
8765 | extsymcount = symcount; | |
8766 | extsymoff = 0; | |
8767 | } | |
8768 | else | |
8769 | { | |
8770 | extsymcount = symcount - hdr->sh_info; | |
8771 | extsymoff = hdr->sh_info; | |
8772 | } | |
8773 | ||
8774 | if (extsymcount == 0) | |
8775 | continue; | |
8776 | ||
8777 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
8778 | NULL, NULL, NULL); | |
8779 | if (isymbuf == NULL) | |
8780 | return FALSE; | |
8781 | ||
8782 | /* Read in any version definitions. */ | |
8783 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 8784 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
8785 | if (extversym == NULL) |
8786 | goto error_ret; | |
8787 | ||
8788 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
8789 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
8790 | != versymhdr->sh_size)) | |
8791 | { | |
8792 | free (extversym); | |
8793 | error_ret: | |
8794 | free (isymbuf); | |
8795 | return FALSE; | |
8796 | } | |
8797 | ||
8798 | ever = extversym + extsymoff; | |
8799 | isymend = isymbuf + extsymcount; | |
8800 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
8801 | { | |
8802 | const char *name; | |
8803 | Elf_Internal_Versym iver; | |
8804 | unsigned short version_index; | |
8805 | ||
8806 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
8807 | || isym->st_shndx == SHN_UNDEF) | |
8808 | continue; | |
8809 | ||
8810 | name = bfd_elf_string_from_elf_section (input, | |
8811 | hdr->sh_link, | |
8812 | isym->st_name); | |
8813 | if (strcmp (name, h->root.root.string) != 0) | |
8814 | continue; | |
8815 | ||
8816 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
8817 | ||
d023c380 L |
8818 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
8819 | && !(h->def_regular | |
8820 | && h->forced_local)) | |
c152c796 AM |
8821 | { |
8822 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
8823 | have provided a definition for the undefined sym unless |
8824 | it is defined in a non-shared object and forced local. | |
8825 | */ | |
c152c796 AM |
8826 | abort (); |
8827 | } | |
8828 | ||
8829 | version_index = iver.vs_vers & VERSYM_VERSION; | |
8830 | if (version_index == 1 || version_index == 2) | |
8831 | { | |
8832 | /* This is the base or first version. We can use it. */ | |
8833 | free (extversym); | |
8834 | free (isymbuf); | |
8835 | return TRUE; | |
8836 | } | |
8837 | } | |
8838 | ||
8839 | free (extversym); | |
8840 | free (isymbuf); | |
8841 | } | |
8842 | ||
8843 | return FALSE; | |
8844 | } | |
8845 | ||
8846 | /* Add an external symbol to the symbol table. This is called from | |
8847 | the hash table traversal routine. When generating a shared object, | |
8848 | we go through the symbol table twice. The first time we output | |
8849 | anything that might have been forced to local scope in a version | |
8850 | script. The second time we output the symbols that are still | |
8851 | global symbols. */ | |
8852 | ||
8853 | static bfd_boolean | |
7686d77d | 8854 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 8855 | { |
7686d77d | 8856 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 8857 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 8858 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
8859 | bfd_boolean strip; |
8860 | Elf_Internal_Sym sym; | |
8861 | asection *input_sec; | |
8862 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
8863 | long indx; |
8864 | int ret; | |
c152c796 AM |
8865 | |
8866 | if (h->root.type == bfd_link_hash_warning) | |
8867 | { | |
8868 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8869 | if (h->root.type == bfd_link_hash_new) | |
8870 | return TRUE; | |
8871 | } | |
8872 | ||
8873 | /* Decide whether to output this symbol in this pass. */ | |
8874 | if (eoinfo->localsyms) | |
8875 | { | |
f5385ebf | 8876 | if (!h->forced_local) |
c152c796 AM |
8877 | return TRUE; |
8878 | } | |
8879 | else | |
8880 | { | |
f5385ebf | 8881 | if (h->forced_local) |
c152c796 AM |
8882 | return TRUE; |
8883 | } | |
8884 | ||
8b127cbc | 8885 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 8886 | |
12ac1cf5 | 8887 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 8888 | { |
12ac1cf5 NC |
8889 | /* If we have an undefined symbol reference here then it must have |
8890 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
8891 | references in regular files have already been handled unless |
8892 | they are in unreferenced sections which are removed by garbage | |
8893 | collection). */ | |
12ac1cf5 NC |
8894 | bfd_boolean ignore_undef = FALSE; |
8895 | ||
8896 | /* Some symbols may be special in that the fact that they're | |
8897 | undefined can be safely ignored - let backend determine that. */ | |
8898 | if (bed->elf_backend_ignore_undef_symbol) | |
8899 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
8900 | ||
8901 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 8902 | if (!ignore_undef |
12ac1cf5 | 8903 | && h->ref_dynamic |
8b127cbc AM |
8904 | && (!h->ref_regular || flinfo->info->gc_sections) |
8905 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
8906 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
8907 | { | |
8908 | if (!(flinfo->info->callbacks->undefined_symbol | |
8909 | (flinfo->info, h->root.root.string, | |
8910 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
8911 | NULL, 0, | |
8912 | (flinfo->info->unresolved_syms_in_shared_libs | |
8913 | == RM_GENERATE_ERROR)))) | |
12ac1cf5 | 8914 | { |
17d078c5 | 8915 | bfd_set_error (bfd_error_bad_value); |
12ac1cf5 NC |
8916 | eoinfo->failed = TRUE; |
8917 | return FALSE; | |
8918 | } | |
c152c796 AM |
8919 | } |
8920 | } | |
8921 | ||
8922 | /* We should also warn if a forced local symbol is referenced from | |
8923 | shared libraries. */ | |
8b127cbc AM |
8924 | if (!flinfo->info->relocatable |
8925 | && flinfo->info->executable | |
f5385ebf AM |
8926 | && h->forced_local |
8927 | && h->ref_dynamic | |
371a5866 | 8928 | && h->def_regular |
f5385ebf | 8929 | && !h->dynamic_def |
ee659f1f | 8930 | && h->ref_dynamic_nonweak |
8b127cbc | 8931 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 8932 | { |
17d078c5 AM |
8933 | bfd *def_bfd; |
8934 | const char *msg; | |
90c984fc L |
8935 | struct elf_link_hash_entry *hi = h; |
8936 | ||
8937 | /* Check indirect symbol. */ | |
8938 | while (hi->root.type == bfd_link_hash_indirect) | |
8939 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
8940 | |
8941 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
8942 | msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); | |
8943 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) | |
8944 | msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); | |
8945 | else | |
8946 | msg = _("%B: local symbol `%s' in %B is referenced by DSO"); | |
8b127cbc | 8947 | def_bfd = flinfo->output_bfd; |
90c984fc L |
8948 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
8949 | def_bfd = hi->root.u.def.section->owner; | |
8b127cbc | 8950 | (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd, |
17d078c5 AM |
8951 | h->root.root.string); |
8952 | bfd_set_error (bfd_error_bad_value); | |
c152c796 AM |
8953 | eoinfo->failed = TRUE; |
8954 | return FALSE; | |
8955 | } | |
8956 | ||
8957 | /* We don't want to output symbols that have never been mentioned by | |
8958 | a regular file, or that we have been told to strip. However, if | |
8959 | h->indx is set to -2, the symbol is used by a reloc and we must | |
8960 | output it. */ | |
d983c8c5 | 8961 | strip = FALSE; |
c152c796 | 8962 | if (h->indx == -2) |
d983c8c5 | 8963 | ; |
f5385ebf | 8964 | else if ((h->def_dynamic |
77cfaee6 AM |
8965 | || h->ref_dynamic |
8966 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
8967 | && !h->def_regular |
8968 | && !h->ref_regular) | |
c152c796 | 8969 | strip = TRUE; |
8b127cbc | 8970 | else if (flinfo->info->strip == strip_all) |
c152c796 | 8971 | strip = TRUE; |
8b127cbc AM |
8972 | else if (flinfo->info->strip == strip_some |
8973 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
8974 | h->root.root.string, FALSE, FALSE) == NULL) |
8975 | strip = TRUE; | |
d56d55e7 AM |
8976 | else if ((h->root.type == bfd_link_hash_defined |
8977 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 8978 | && ((flinfo->info->strip_discarded |
dbaa2011 | 8979 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
8980 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
8981 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 8982 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 8983 | strip = TRUE; |
9e2278f5 AM |
8984 | else if ((h->root.type == bfd_link_hash_undefined |
8985 | || h->root.type == bfd_link_hash_undefweak) | |
8986 | && h->root.u.undef.abfd != NULL | |
8987 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
8988 | strip = TRUE; | |
c152c796 AM |
8989 | |
8990 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
d983c8c5 AM |
8991 | nothing else to do. However, if it is a forced local symbol or |
8992 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
8993 | function a chance to make it dynamic. */ | |
c152c796 AM |
8994 | if (strip |
8995 | && h->dynindx == -1 | |
57ca8ac7 | 8996 | && h->type != STT_GNU_IFUNC |
f5385ebf | 8997 | && !h->forced_local) |
c152c796 AM |
8998 | return TRUE; |
8999 | ||
9000 | sym.st_value = 0; | |
9001 | sym.st_size = h->size; | |
9002 | sym.st_other = h->other; | |
f5385ebf | 9003 | if (h->forced_local) |
935bd1e0 L |
9004 | { |
9005 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); | |
9006 | /* Turn off visibility on local symbol. */ | |
9007 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9008 | } | |
02acbe22 L |
9009 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ |
9010 | else if (h->unique_global && h->def_regular) | |
3e7a7d11 | 9011 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type); |
c152c796 AM |
9012 | else if (h->root.type == bfd_link_hash_undefweak |
9013 | || h->root.type == bfd_link_hash_defweak) | |
9014 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
9015 | else | |
9016 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
35fc36a8 | 9017 | sym.st_target_internal = h->target_internal; |
c152c796 AM |
9018 | |
9019 | switch (h->root.type) | |
9020 | { | |
9021 | default: | |
9022 | case bfd_link_hash_new: | |
9023 | case bfd_link_hash_warning: | |
9024 | abort (); | |
9025 | return FALSE; | |
9026 | ||
9027 | case bfd_link_hash_undefined: | |
9028 | case bfd_link_hash_undefweak: | |
9029 | input_sec = bfd_und_section_ptr; | |
9030 | sym.st_shndx = SHN_UNDEF; | |
9031 | break; | |
9032 | ||
9033 | case bfd_link_hash_defined: | |
9034 | case bfd_link_hash_defweak: | |
9035 | { | |
9036 | input_sec = h->root.u.def.section; | |
9037 | if (input_sec->output_section != NULL) | |
9038 | { | |
9039 | sym.st_shndx = | |
8b127cbc | 9040 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9041 | input_sec->output_section); |
9042 | if (sym.st_shndx == SHN_BAD) | |
9043 | { | |
9044 | (*_bfd_error_handler) | |
d003868e | 9045 | (_("%B: could not find output section %A for input section %A"), |
8b127cbc | 9046 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9047 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9048 | eoinfo->failed = TRUE; |
9049 | return FALSE; | |
9050 | } | |
9051 | ||
9052 | /* ELF symbols in relocatable files are section relative, | |
9053 | but in nonrelocatable files they are virtual | |
9054 | addresses. */ | |
9055 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
8b127cbc | 9056 | if (!flinfo->info->relocatable) |
c152c796 AM |
9057 | { |
9058 | sym.st_value += input_sec->output_section->vma; | |
9059 | if (h->type == STT_TLS) | |
9060 | { | |
8b127cbc | 9061 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9062 | if (tls_sec != NULL) |
9063 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9064 | } |
9065 | } | |
9066 | } | |
9067 | else | |
9068 | { | |
9069 | BFD_ASSERT (input_sec->owner == NULL | |
9070 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9071 | sym.st_shndx = SHN_UNDEF; | |
9072 | input_sec = bfd_und_section_ptr; | |
9073 | } | |
9074 | } | |
9075 | break; | |
9076 | ||
9077 | case bfd_link_hash_common: | |
9078 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9079 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9080 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9081 | break; | |
9082 | ||
9083 | case bfd_link_hash_indirect: | |
9084 | /* These symbols are created by symbol versioning. They point | |
9085 | to the decorated version of the name. For example, if the | |
9086 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9087 | foo is used with no version, then we add an indirect symbol | |
9088 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9089 | since the indirected symbol is already in the hash table. */ | |
9090 | return TRUE; | |
9091 | } | |
9092 | ||
9093 | /* Give the processor backend a chance to tweak the symbol value, | |
9094 | and also to finish up anything that needs to be done for this | |
9095 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9096 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9097 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9098 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9099 | && h->def_regular |
8b127cbc | 9100 | && !flinfo->info->relocatable) |
3aa14d16 L |
9101 | || ((h->dynindx != -1 |
9102 | || h->forced_local) | |
8b127cbc | 9103 | && ((flinfo->info->shared |
3aa14d16 L |
9104 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9105 | || h->root.type != bfd_link_hash_undefweak)) | |
9106 | || !h->forced_local) | |
8b127cbc | 9107 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9108 | { |
9109 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9110 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9111 | { |
9112 | eoinfo->failed = TRUE; | |
9113 | return FALSE; | |
9114 | } | |
9115 | } | |
9116 | ||
9117 | /* If we are marking the symbol as undefined, and there are no | |
9118 | non-weak references to this symbol from a regular object, then | |
9119 | mark the symbol as weak undefined; if there are non-weak | |
9120 | references, mark the symbol as strong. We can't do this earlier, | |
9121 | because it might not be marked as undefined until the | |
9122 | finish_dynamic_symbol routine gets through with it. */ | |
9123 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9124 | && h->ref_regular |
c152c796 AM |
9125 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9126 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9127 | { | |
9128 | int bindtype; | |
2955ec4c L |
9129 | unsigned int type = ELF_ST_TYPE (sym.st_info); |
9130 | ||
9131 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9132 | if (type == STT_GNU_IFUNC) | |
9133 | type = STT_FUNC; | |
c152c796 | 9134 | |
f5385ebf | 9135 | if (h->ref_regular_nonweak) |
c152c796 AM |
9136 | bindtype = STB_GLOBAL; |
9137 | else | |
9138 | bindtype = STB_WEAK; | |
2955ec4c | 9139 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
9140 | } |
9141 | ||
bda987c2 CD |
9142 | /* If this is a symbol defined in a dynamic library, don't use the |
9143 | symbol size from the dynamic library. Relinking an executable | |
9144 | against a new library may introduce gratuitous changes in the | |
9145 | executable's symbols if we keep the size. */ | |
9146 | if (sym.st_shndx == SHN_UNDEF | |
9147 | && !h->def_regular | |
9148 | && h->def_dynamic) | |
9149 | sym.st_size = 0; | |
9150 | ||
c152c796 AM |
9151 | /* If a non-weak symbol with non-default visibility is not defined |
9152 | locally, it is a fatal error. */ | |
8b127cbc | 9153 | if (!flinfo->info->relocatable |
c152c796 AM |
9154 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
9155 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
9156 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 9157 | && !h->def_regular) |
c152c796 | 9158 | { |
17d078c5 AM |
9159 | const char *msg; |
9160 | ||
9161 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
9162 | msg = _("%B: protected symbol `%s' isn't defined"); | |
9163 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) | |
9164 | msg = _("%B: internal symbol `%s' isn't defined"); | |
9165 | else | |
9166 | msg = _("%B: hidden symbol `%s' isn't defined"); | |
8b127cbc | 9167 | (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 9168 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9169 | eoinfo->failed = TRUE; |
9170 | return FALSE; | |
9171 | } | |
9172 | ||
9173 | /* If this symbol should be put in the .dynsym section, then put it | |
9174 | there now. We already know the symbol index. We also fill in | |
9175 | the entry in the .hash section. */ | |
8b127cbc | 9176 | if (flinfo->dynsym_sec != NULL |
202e2356 | 9177 | && h->dynindx != -1 |
8b127cbc | 9178 | && elf_hash_table (flinfo->info)->dynamic_sections_created) |
c152c796 | 9179 | { |
c152c796 AM |
9180 | bfd_byte *esym; |
9181 | ||
90c984fc L |
9182 | /* Since there is no version information in the dynamic string, |
9183 | if there is no version info in symbol version section, we will | |
9184 | have a run-time problem. */ | |
9185 | if (h->verinfo.verdef == NULL) | |
9186 | { | |
9187 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
9188 | ||
9189 | if (p && p [1] != '\0') | |
9190 | { | |
9191 | (*_bfd_error_handler) | |
9192 | (_("%B: No symbol version section for versioned symbol `%s'"), | |
9193 | flinfo->output_bfd, h->root.root.string); | |
9194 | eoinfo->failed = TRUE; | |
9195 | return FALSE; | |
9196 | } | |
9197 | } | |
9198 | ||
c152c796 | 9199 | sym.st_name = h->dynstr_index; |
8b127cbc AM |
9200 | esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; |
9201 | if (!check_dynsym (flinfo->output_bfd, &sym)) | |
c0d5a53d L |
9202 | { |
9203 | eoinfo->failed = TRUE; | |
9204 | return FALSE; | |
9205 | } | |
8b127cbc | 9206 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 9207 | |
8b127cbc | 9208 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
9209 | { |
9210 | size_t hash_entry_size; | |
9211 | bfd_byte *bucketpos; | |
9212 | bfd_vma chain; | |
41198d0c L |
9213 | size_t bucketcount; |
9214 | size_t bucket; | |
9215 | ||
8b127cbc | 9216 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 9217 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
9218 | |
9219 | hash_entry_size | |
8b127cbc AM |
9220 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
9221 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 9222 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
9223 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
9224 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
9225 | bucketpos); | |
9226 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
9227 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
9228 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
9229 | } | |
c152c796 | 9230 | |
8b127cbc | 9231 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
9232 | { |
9233 | Elf_Internal_Versym iversym; | |
9234 | Elf_External_Versym *eversym; | |
9235 | ||
f5385ebf | 9236 | if (!h->def_regular) |
c152c796 | 9237 | { |
7b20f099 AM |
9238 | if (h->verinfo.verdef == NULL |
9239 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
9240 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
9241 | iversym.vs_vers = 0; |
9242 | else | |
9243 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
9244 | } | |
9245 | else | |
9246 | { | |
9247 | if (h->verinfo.vertree == NULL) | |
9248 | iversym.vs_vers = 1; | |
9249 | else | |
9250 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 9251 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 9252 | iversym.vs_vers++; |
c152c796 AM |
9253 | } |
9254 | ||
f5385ebf | 9255 | if (h->hidden) |
c152c796 AM |
9256 | iversym.vs_vers |= VERSYM_HIDDEN; |
9257 | ||
8b127cbc | 9258 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 9259 | eversym += h->dynindx; |
8b127cbc | 9260 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
9261 | } |
9262 | } | |
9263 | ||
d983c8c5 AM |
9264 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
9265 | strip it from .symtab too. Obviously we can't do this for | |
9266 | relocatable output or when needed for --emit-relocs. */ | |
9267 | else if (input_sec == bfd_und_section_ptr | |
9268 | && h->indx != -2 | |
9269 | && !flinfo->info->relocatable) | |
9270 | return TRUE; | |
9271 | /* Also strip others that we couldn't earlier due to dynamic symbol | |
9272 | processing. */ | |
9273 | if (strip) | |
9274 | return TRUE; | |
9275 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
9276 | return TRUE; |
9277 | ||
2ec55de3 AM |
9278 | /* Output a FILE symbol so that following locals are not associated |
9279 | with the wrong input file. We need one for forced local symbols | |
9280 | if we've seen more than one FILE symbol or when we have exactly | |
9281 | one FILE symbol but global symbols are present in a file other | |
9282 | than the one with the FILE symbol. We also need one if linker | |
9283 | defined symbols are present. In practice these conditions are | |
9284 | always met, so just emit the FILE symbol unconditionally. */ | |
9285 | if (eoinfo->localsyms | |
9286 | && !eoinfo->file_sym_done | |
9287 | && eoinfo->flinfo->filesym_count != 0) | |
9288 | { | |
9289 | Elf_Internal_Sym fsym; | |
9290 | ||
9291 | memset (&fsym, 0, sizeof (fsym)); | |
9292 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
9293 | fsym.st_shndx = SHN_ABS; | |
9294 | if (!elf_link_output_sym (eoinfo->flinfo, NULL, &fsym, | |
9295 | bfd_und_section_ptr, NULL)) | |
9296 | return FALSE; | |
9297 | ||
9298 | eoinfo->file_sym_done = TRUE; | |
9299 | } | |
9300 | ||
8b127cbc AM |
9301 | indx = bfd_get_symcount (flinfo->output_bfd); |
9302 | ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h); | |
6e0b88f1 | 9303 | if (ret == 0) |
c152c796 AM |
9304 | { |
9305 | eoinfo->failed = TRUE; | |
9306 | return FALSE; | |
9307 | } | |
6e0b88f1 AM |
9308 | else if (ret == 1) |
9309 | h->indx = indx; | |
9310 | else if (h->indx == -2) | |
9311 | abort(); | |
c152c796 AM |
9312 | |
9313 | return TRUE; | |
9314 | } | |
9315 | ||
cdd3575c AM |
9316 | /* Return TRUE if special handling is done for relocs in SEC against |
9317 | symbols defined in discarded sections. */ | |
9318 | ||
c152c796 AM |
9319 | static bfd_boolean |
9320 | elf_section_ignore_discarded_relocs (asection *sec) | |
9321 | { | |
9322 | const struct elf_backend_data *bed; | |
9323 | ||
cdd3575c AM |
9324 | switch (sec->sec_info_type) |
9325 | { | |
dbaa2011 AM |
9326 | case SEC_INFO_TYPE_STABS: |
9327 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 9328 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
9329 | return TRUE; |
9330 | default: | |
9331 | break; | |
9332 | } | |
c152c796 AM |
9333 | |
9334 | bed = get_elf_backend_data (sec->owner); | |
9335 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
9336 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
9337 | return TRUE; | |
9338 | ||
9339 | return FALSE; | |
9340 | } | |
9341 | ||
9e66c942 AM |
9342 | /* Return a mask saying how ld should treat relocations in SEC against |
9343 | symbols defined in discarded sections. If this function returns | |
9344 | COMPLAIN set, ld will issue a warning message. If this function | |
9345 | returns PRETEND set, and the discarded section was link-once and the | |
9346 | same size as the kept link-once section, ld will pretend that the | |
9347 | symbol was actually defined in the kept section. Otherwise ld will | |
9348 | zero the reloc (at least that is the intent, but some cooperation by | |
9349 | the target dependent code is needed, particularly for REL targets). */ | |
9350 | ||
8a696751 AM |
9351 | unsigned int |
9352 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 9353 | { |
9e66c942 | 9354 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 9355 | return PRETEND; |
cdd3575c AM |
9356 | |
9357 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 9358 | return 0; |
cdd3575c AM |
9359 | |
9360 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 9361 | return 0; |
cdd3575c | 9362 | |
9e66c942 | 9363 | return COMPLAIN | PRETEND; |
cdd3575c AM |
9364 | } |
9365 | ||
3d7f7666 L |
9366 | /* Find a match between a section and a member of a section group. */ |
9367 | ||
9368 | static asection * | |
c0f00686 L |
9369 | match_group_member (asection *sec, asection *group, |
9370 | struct bfd_link_info *info) | |
3d7f7666 L |
9371 | { |
9372 | asection *first = elf_next_in_group (group); | |
9373 | asection *s = first; | |
9374 | ||
9375 | while (s != NULL) | |
9376 | { | |
c0f00686 | 9377 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
9378 | return s; |
9379 | ||
83180ade | 9380 | s = elf_next_in_group (s); |
3d7f7666 L |
9381 | if (s == first) |
9382 | break; | |
9383 | } | |
9384 | ||
9385 | return NULL; | |
9386 | } | |
9387 | ||
01b3c8ab | 9388 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
9389 | to replace it. Return the replacement if it is OK. Otherwise return |
9390 | NULL. */ | |
01b3c8ab L |
9391 | |
9392 | asection * | |
c0f00686 | 9393 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
9394 | { |
9395 | asection *kept; | |
9396 | ||
9397 | kept = sec->kept_section; | |
9398 | if (kept != NULL) | |
9399 | { | |
c2370991 | 9400 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 9401 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
9402 | if (kept != NULL |
9403 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
9404 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 9405 | kept = NULL; |
c2370991 | 9406 | sec->kept_section = kept; |
01b3c8ab L |
9407 | } |
9408 | return kept; | |
9409 | } | |
9410 | ||
c152c796 AM |
9411 | /* Link an input file into the linker output file. This function |
9412 | handles all the sections and relocations of the input file at once. | |
9413 | This is so that we only have to read the local symbols once, and | |
9414 | don't have to keep them in memory. */ | |
9415 | ||
9416 | static bfd_boolean | |
8b127cbc | 9417 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 9418 | { |
ece5ef60 | 9419 | int (*relocate_section) |
c152c796 AM |
9420 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
9421 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
9422 | bfd *output_bfd; | |
9423 | Elf_Internal_Shdr *symtab_hdr; | |
9424 | size_t locsymcount; | |
9425 | size_t extsymoff; | |
9426 | Elf_Internal_Sym *isymbuf; | |
9427 | Elf_Internal_Sym *isym; | |
9428 | Elf_Internal_Sym *isymend; | |
9429 | long *pindex; | |
9430 | asection **ppsection; | |
9431 | asection *o; | |
9432 | const struct elf_backend_data *bed; | |
c152c796 | 9433 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
9434 | bfd_size_type address_size; |
9435 | bfd_vma r_type_mask; | |
9436 | int r_sym_shift; | |
ffbc01cc | 9437 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 9438 | |
8b127cbc | 9439 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
9440 | bed = get_elf_backend_data (output_bfd); |
9441 | relocate_section = bed->elf_backend_relocate_section; | |
9442 | ||
9443 | /* If this is a dynamic object, we don't want to do anything here: | |
9444 | we don't want the local symbols, and we don't want the section | |
9445 | contents. */ | |
9446 | if ((input_bfd->flags & DYNAMIC) != 0) | |
9447 | return TRUE; | |
9448 | ||
c152c796 AM |
9449 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
9450 | if (elf_bad_symtab (input_bfd)) | |
9451 | { | |
9452 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9453 | extsymoff = 0; | |
9454 | } | |
9455 | else | |
9456 | { | |
9457 | locsymcount = symtab_hdr->sh_info; | |
9458 | extsymoff = symtab_hdr->sh_info; | |
9459 | } | |
9460 | ||
9461 | /* Read the local symbols. */ | |
9462 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9463 | if (isymbuf == NULL && locsymcount != 0) | |
9464 | { | |
9465 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
9466 | flinfo->internal_syms, |
9467 | flinfo->external_syms, | |
9468 | flinfo->locsym_shndx); | |
c152c796 AM |
9469 | if (isymbuf == NULL) |
9470 | return FALSE; | |
9471 | } | |
9472 | ||
9473 | /* Find local symbol sections and adjust values of symbols in | |
9474 | SEC_MERGE sections. Write out those local symbols we know are | |
9475 | going into the output file. */ | |
9476 | isymend = isymbuf + locsymcount; | |
8b127cbc | 9477 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
9478 | isym < isymend; |
9479 | isym++, pindex++, ppsection++) | |
9480 | { | |
9481 | asection *isec; | |
9482 | const char *name; | |
9483 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
9484 | long indx; |
9485 | int ret; | |
c152c796 AM |
9486 | |
9487 | *pindex = -1; | |
9488 | ||
9489 | if (elf_bad_symtab (input_bfd)) | |
9490 | { | |
9491 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
9492 | { | |
9493 | *ppsection = NULL; | |
9494 | continue; | |
9495 | } | |
9496 | } | |
9497 | ||
9498 | if (isym->st_shndx == SHN_UNDEF) | |
9499 | isec = bfd_und_section_ptr; | |
c152c796 AM |
9500 | else if (isym->st_shndx == SHN_ABS) |
9501 | isec = bfd_abs_section_ptr; | |
9502 | else if (isym->st_shndx == SHN_COMMON) | |
9503 | isec = bfd_com_section_ptr; | |
9504 | else | |
9505 | { | |
cb33740c AM |
9506 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
9507 | if (isec == NULL) | |
9508 | { | |
9509 | /* Don't attempt to output symbols with st_shnx in the | |
9510 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
9511 | *ppsection = NULL; | |
9512 | continue; | |
9513 | } | |
dbaa2011 | 9514 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
9515 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
9516 | isym->st_value = | |
9517 | _bfd_merged_section_offset (output_bfd, &isec, | |
9518 | elf_section_data (isec)->sec_info, | |
9519 | isym->st_value); | |
c152c796 AM |
9520 | } |
9521 | ||
9522 | *ppsection = isec; | |
9523 | ||
d983c8c5 AM |
9524 | /* Don't output the first, undefined, symbol. In fact, don't |
9525 | output any undefined local symbol. */ | |
9526 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
9527 | continue; |
9528 | ||
9529 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
9530 | { | |
9531 | /* We never output section symbols. Instead, we use the | |
9532 | section symbol of the corresponding section in the output | |
9533 | file. */ | |
9534 | continue; | |
9535 | } | |
9536 | ||
9537 | /* If we are stripping all symbols, we don't want to output this | |
9538 | one. */ | |
8b127cbc | 9539 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
9540 | continue; |
9541 | ||
9542 | /* If we are discarding all local symbols, we don't want to | |
9543 | output this one. If we are generating a relocatable output | |
9544 | file, then some of the local symbols may be required by | |
9545 | relocs; we output them below as we discover that they are | |
9546 | needed. */ | |
8b127cbc | 9547 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
9548 | continue; |
9549 | ||
9550 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
9551 | discarding, we don't need to keep it. */ |
9552 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
9553 | && isym->st_shndx < SHN_LORESERVE |
9554 | && bfd_section_removed_from_list (output_bfd, | |
9555 | isec->output_section)) | |
e75a280b L |
9556 | continue; |
9557 | ||
c152c796 AM |
9558 | /* Get the name of the symbol. */ |
9559 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
9560 | isym->st_name); | |
9561 | if (name == NULL) | |
9562 | return FALSE; | |
9563 | ||
9564 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
9565 | if ((flinfo->info->strip == strip_some |
9566 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 9567 | == NULL)) |
8b127cbc AM |
9568 | || (((flinfo->info->discard == discard_sec_merge |
9569 | && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable) | |
9570 | || flinfo->info->discard == discard_l) | |
c152c796 AM |
9571 | && bfd_is_local_label_name (input_bfd, name))) |
9572 | continue; | |
9573 | ||
ffbc01cc AM |
9574 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
9575 | { | |
ce875075 AM |
9576 | if (input_bfd->lto_output) |
9577 | /* -flto puts a temp file name here. This means builds | |
9578 | are not reproducible. Discard the symbol. */ | |
9579 | continue; | |
ffbc01cc AM |
9580 | have_file_sym = TRUE; |
9581 | flinfo->filesym_count += 1; | |
9582 | } | |
9583 | if (!have_file_sym) | |
9584 | { | |
9585 | /* In the absence of debug info, bfd_find_nearest_line uses | |
9586 | FILE symbols to determine the source file for local | |
9587 | function symbols. Provide a FILE symbol here if input | |
9588 | files lack such, so that their symbols won't be | |
9589 | associated with a previous input file. It's not the | |
9590 | source file, but the best we can do. */ | |
9591 | have_file_sym = TRUE; | |
9592 | flinfo->filesym_count += 1; | |
9593 | memset (&osym, 0, sizeof (osym)); | |
9594 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
9595 | osym.st_shndx = SHN_ABS; | |
ce875075 AM |
9596 | if (!elf_link_output_sym (flinfo, |
9597 | (input_bfd->lto_output ? NULL | |
9598 | : input_bfd->filename), | |
9599 | &osym, bfd_abs_section_ptr, NULL)) | |
ffbc01cc AM |
9600 | return FALSE; |
9601 | } | |
9602 | ||
c152c796 AM |
9603 | osym = *isym; |
9604 | ||
9605 | /* Adjust the section index for the output file. */ | |
9606 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
9607 | isec->output_section); | |
9608 | if (osym.st_shndx == SHN_BAD) | |
9609 | return FALSE; | |
9610 | ||
c152c796 AM |
9611 | /* ELF symbols in relocatable files are section relative, but |
9612 | in executable files they are virtual addresses. Note that | |
9613 | this code assumes that all ELF sections have an associated | |
9614 | BFD section with a reasonable value for output_offset; below | |
9615 | we assume that they also have a reasonable value for | |
9616 | output_section. Any special sections must be set up to meet | |
9617 | these requirements. */ | |
9618 | osym.st_value += isec->output_offset; | |
8b127cbc | 9619 | if (!flinfo->info->relocatable) |
c152c796 AM |
9620 | { |
9621 | osym.st_value += isec->output_section->vma; | |
9622 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
9623 | { | |
9624 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8b127cbc AM |
9625 | BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); |
9626 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
c152c796 AM |
9627 | } |
9628 | } | |
9629 | ||
6e0b88f1 | 9630 | indx = bfd_get_symcount (output_bfd); |
8b127cbc | 9631 | ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 9632 | if (ret == 0) |
c152c796 | 9633 | return FALSE; |
6e0b88f1 AM |
9634 | else if (ret == 1) |
9635 | *pindex = indx; | |
c152c796 AM |
9636 | } |
9637 | ||
310fd250 L |
9638 | if (bed->s->arch_size == 32) |
9639 | { | |
9640 | r_type_mask = 0xff; | |
9641 | r_sym_shift = 8; | |
9642 | address_size = 4; | |
9643 | } | |
9644 | else | |
9645 | { | |
9646 | r_type_mask = 0xffffffff; | |
9647 | r_sym_shift = 32; | |
9648 | address_size = 8; | |
9649 | } | |
9650 | ||
c152c796 AM |
9651 | /* Relocate the contents of each section. */ |
9652 | sym_hashes = elf_sym_hashes (input_bfd); | |
9653 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
9654 | { | |
9655 | bfd_byte *contents; | |
9656 | ||
9657 | if (! o->linker_mark) | |
9658 | { | |
9659 | /* This section was omitted from the link. */ | |
9660 | continue; | |
9661 | } | |
9662 | ||
8b127cbc | 9663 | if (flinfo->info->relocatable |
bcacc0f5 AM |
9664 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
9665 | { | |
9666 | /* Deal with the group signature symbol. */ | |
9667 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
9668 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
9669 | asection *osec = o->output_section; | |
9670 | ||
9671 | if (symndx >= locsymcount | |
9672 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 9673 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
9674 | { |
9675 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
9676 | while (h->root.type == bfd_link_hash_indirect | |
9677 | || h->root.type == bfd_link_hash_warning) | |
9678 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9679 | /* Arrange for symbol to be output. */ | |
9680 | h->indx = -2; | |
9681 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
9682 | } | |
9683 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
9684 | { | |
9685 | /* We'll use the output section target_index. */ | |
8b127cbc | 9686 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
9687 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
9688 | } | |
9689 | else | |
9690 | { | |
8b127cbc | 9691 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
9692 | { |
9693 | /* Otherwise output the local symbol now. */ | |
9694 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 9695 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 9696 | const char *name; |
6e0b88f1 AM |
9697 | long indx; |
9698 | int ret; | |
bcacc0f5 AM |
9699 | |
9700 | name = bfd_elf_string_from_elf_section (input_bfd, | |
9701 | symtab_hdr->sh_link, | |
9702 | sym.st_name); | |
9703 | if (name == NULL) | |
9704 | return FALSE; | |
9705 | ||
9706 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
9707 | sec); | |
9708 | if (sym.st_shndx == SHN_BAD) | |
9709 | return FALSE; | |
9710 | ||
9711 | sym.st_value += o->output_offset; | |
9712 | ||
6e0b88f1 | 9713 | indx = bfd_get_symcount (output_bfd); |
8b127cbc | 9714 | ret = elf_link_output_sym (flinfo, name, &sym, o, NULL); |
6e0b88f1 | 9715 | if (ret == 0) |
bcacc0f5 | 9716 | return FALSE; |
6e0b88f1 | 9717 | else if (ret == 1) |
8b127cbc | 9718 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
9719 | else |
9720 | abort (); | |
bcacc0f5 AM |
9721 | } |
9722 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 9723 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
9724 | } |
9725 | } | |
9726 | ||
c152c796 | 9727 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 9728 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
9729 | continue; |
9730 | ||
9731 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
9732 | { | |
9733 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
9734 | or somesuch. */ | |
9735 | continue; | |
9736 | } | |
9737 | ||
9738 | /* Get the contents of the section. They have been cached by a | |
9739 | relaxation routine. Note that o is a section in an input | |
9740 | file, so the contents field will not have been set by any of | |
9741 | the routines which work on output files. */ | |
9742 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
9743 | { |
9744 | contents = elf_section_data (o)->this_hdr.contents; | |
9745 | if (bed->caches_rawsize | |
9746 | && o->rawsize != 0 | |
9747 | && o->rawsize < o->size) | |
9748 | { | |
9749 | memcpy (flinfo->contents, contents, o->rawsize); | |
9750 | contents = flinfo->contents; | |
9751 | } | |
9752 | } | |
c152c796 AM |
9753 | else |
9754 | { | |
8b127cbc | 9755 | contents = flinfo->contents; |
4a114e3e | 9756 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
9757 | return FALSE; |
9758 | } | |
9759 | ||
9760 | if ((o->flags & SEC_RELOC) != 0) | |
9761 | { | |
9762 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 9763 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 9764 | int action_discarded; |
ece5ef60 | 9765 | int ret; |
c152c796 AM |
9766 | |
9767 | /* Get the swapped relocs. */ | |
9768 | internal_relocs | |
8b127cbc AM |
9769 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
9770 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
9771 | if (internal_relocs == NULL |
9772 | && o->reloc_count > 0) | |
9773 | return FALSE; | |
9774 | ||
310fd250 L |
9775 | /* We need to reverse-copy input .ctors/.dtors sections if |
9776 | they are placed in .init_array/.finit_array for output. */ | |
9777 | if (o->size > address_size | |
9778 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
9779 | && strcmp (o->output_section->name, | |
9780 | ".init_array") == 0) | |
9781 | || (strncmp (o->name, ".dtors", 6) == 0 | |
9782 | && strcmp (o->output_section->name, | |
9783 | ".fini_array") == 0)) | |
9784 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 9785 | { |
310fd250 L |
9786 | if (o->size != o->reloc_count * address_size) |
9787 | { | |
9788 | (*_bfd_error_handler) | |
9789 | (_("error: %B: size of section %A is not " | |
9790 | "multiple of address size"), | |
9791 | input_bfd, o); | |
9792 | bfd_set_error (bfd_error_on_input); | |
9793 | return FALSE; | |
9794 | } | |
9795 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
9796 | } |
9797 | ||
0f02bbd9 | 9798 | action_discarded = -1; |
c152c796 | 9799 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
9800 | action_discarded = (*bed->action_discarded) (o); |
9801 | ||
9802 | /* Run through the relocs evaluating complex reloc symbols and | |
9803 | looking for relocs against symbols from discarded sections | |
9804 | or section symbols from removed link-once sections. | |
9805 | Complain about relocs against discarded sections. Zero | |
9806 | relocs against removed link-once sections. */ | |
9807 | ||
9808 | rel = internal_relocs; | |
9809 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
9810 | for ( ; rel < relend; rel++) | |
c152c796 | 9811 | { |
0f02bbd9 AM |
9812 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
9813 | unsigned int s_type; | |
9814 | asection **ps, *sec; | |
9815 | struct elf_link_hash_entry *h = NULL; | |
9816 | const char *sym_name; | |
c152c796 | 9817 | |
0f02bbd9 AM |
9818 | if (r_symndx == STN_UNDEF) |
9819 | continue; | |
c152c796 | 9820 | |
0f02bbd9 AM |
9821 | if (r_symndx >= locsymcount |
9822 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 9823 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
9824 | { |
9825 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 9826 | |
0f02bbd9 AM |
9827 | /* Badly formatted input files can contain relocs that |
9828 | reference non-existant symbols. Check here so that | |
9829 | we do not seg fault. */ | |
9830 | if (h == NULL) | |
c152c796 | 9831 | { |
0f02bbd9 | 9832 | char buffer [32]; |
dce669a1 | 9833 | |
0f02bbd9 AM |
9834 | sprintf_vma (buffer, rel->r_info); |
9835 | (*_bfd_error_handler) | |
9836 | (_("error: %B contains a reloc (0x%s) for section %A " | |
9837 | "that references a non-existent global symbol"), | |
9838 | input_bfd, o, buffer); | |
9839 | bfd_set_error (bfd_error_bad_value); | |
9840 | return FALSE; | |
9841 | } | |
3b36f7e6 | 9842 | |
0f02bbd9 AM |
9843 | while (h->root.type == bfd_link_hash_indirect |
9844 | || h->root.type == bfd_link_hash_warning) | |
9845 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 9846 | |
0f02bbd9 | 9847 | s_type = h->type; |
cdd3575c | 9848 | |
9e2dec47 | 9849 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
9850 | mark the symbol as undefined. Note that the |
9851 | linker may attach linker created dynamic sections | |
9852 | to the plugin bfd. Symbols defined in linker | |
9853 | created sections are not plugin symbols. */ | |
9e2dec47 L |
9854 | if (h->root.non_ir_ref |
9855 | && (h->root.type == bfd_link_hash_defined | |
9856 | || h->root.type == bfd_link_hash_defweak) | |
9857 | && (h->root.u.def.section->flags | |
9858 | & SEC_LINKER_CREATED) == 0 | |
9859 | && h->root.u.def.section->owner != NULL | |
9860 | && (h->root.u.def.section->owner->flags | |
9861 | & BFD_PLUGIN) != 0) | |
9862 | { | |
9863 | h->root.type = bfd_link_hash_undefined; | |
9864 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
9865 | } | |
9866 | ||
0f02bbd9 AM |
9867 | ps = NULL; |
9868 | if (h->root.type == bfd_link_hash_defined | |
9869 | || h->root.type == bfd_link_hash_defweak) | |
9870 | ps = &h->root.u.def.section; | |
9871 | ||
9872 | sym_name = h->root.root.string; | |
9873 | } | |
9874 | else | |
9875 | { | |
9876 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
9877 | ||
9878 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 9879 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
9880 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
9881 | sym, *ps); | |
9882 | } | |
c152c796 | 9883 | |
c301e700 | 9884 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
8b127cbc | 9885 | && !flinfo->info->relocatable) |
0f02bbd9 AM |
9886 | { |
9887 | bfd_vma val; | |
9888 | bfd_vma dot = (rel->r_offset | |
9889 | + o->output_offset + o->output_section->vma); | |
9890 | #ifdef DEBUG | |
9891 | printf ("Encountered a complex symbol!"); | |
9892 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
9893 | input_bfd->filename, o->name, |
9894 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
9895 | printf (" symbol: idx %8.8lx, name %s\n", |
9896 | r_symndx, sym_name); | |
9897 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
9898 | (unsigned long) rel->r_info, | |
9899 | (unsigned long) rel->r_offset); | |
9900 | #endif | |
8b127cbc | 9901 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
9902 | isymbuf, locsymcount, s_type == STT_SRELC)) |
9903 | return FALSE; | |
9904 | ||
9905 | /* Symbol evaluated OK. Update to absolute value. */ | |
9906 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
9907 | r_symndx, val); | |
9908 | continue; | |
9909 | } | |
9910 | ||
9911 | if (action_discarded != -1 && ps != NULL) | |
9912 | { | |
cdd3575c AM |
9913 | /* Complain if the definition comes from a |
9914 | discarded section. */ | |
dbaa2011 | 9915 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 9916 | { |
cf35638d | 9917 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 9918 | if (action_discarded & COMPLAIN) |
8b127cbc | 9919 | (*flinfo->info->callbacks->einfo) |
e1fffbe6 | 9920 | (_("%X`%s' referenced in section `%A' of %B: " |
58ac56d0 | 9921 | "defined in discarded section `%A' of %B\n"), |
e1fffbe6 | 9922 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 9923 | |
87e5235d | 9924 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 9925 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
9926 | really defined in the kept linkonce section. |
9927 | FIXME: This is quite broken. Modifying the | |
9928 | symbol here means we will be changing all later | |
e0ae6d6f | 9929 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 9930 | if (action_discarded & PRETEND) |
87e5235d | 9931 | { |
01b3c8ab L |
9932 | asection *kept; |
9933 | ||
c0f00686 | 9934 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 9935 | flinfo->info); |
01b3c8ab | 9936 | if (kept != NULL) |
87e5235d AM |
9937 | { |
9938 | *ps = kept; | |
9939 | continue; | |
9940 | } | |
9941 | } | |
c152c796 AM |
9942 | } |
9943 | } | |
9944 | } | |
9945 | ||
9946 | /* Relocate the section by invoking a back end routine. | |
9947 | ||
9948 | The back end routine is responsible for adjusting the | |
9949 | section contents as necessary, and (if using Rela relocs | |
9950 | and generating a relocatable output file) adjusting the | |
9951 | reloc addend as necessary. | |
9952 | ||
9953 | The back end routine does not have to worry about setting | |
9954 | the reloc address or the reloc symbol index. | |
9955 | ||
9956 | The back end routine is given a pointer to the swapped in | |
9957 | internal symbols, and can access the hash table entries | |
9958 | for the external symbols via elf_sym_hashes (input_bfd). | |
9959 | ||
9960 | When generating relocatable output, the back end routine | |
9961 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
9962 | output symbol is going to be a section symbol | |
9963 | corresponding to the output section, which will require | |
9964 | the addend to be adjusted. */ | |
9965 | ||
8b127cbc | 9966 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
9967 | input_bfd, o, contents, |
9968 | internal_relocs, | |
9969 | isymbuf, | |
8b127cbc | 9970 | flinfo->sections); |
ece5ef60 | 9971 | if (!ret) |
c152c796 AM |
9972 | return FALSE; |
9973 | ||
ece5ef60 | 9974 | if (ret == 2 |
8b127cbc AM |
9975 | || flinfo->info->relocatable |
9976 | || flinfo->info->emitrelocations) | |
c152c796 AM |
9977 | { |
9978 | Elf_Internal_Rela *irela; | |
d4730f92 | 9979 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
9980 | bfd_vma last_offset; |
9981 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
9982 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
9983 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 9984 | unsigned int next_erel; |
c152c796 | 9985 | bfd_boolean rela_normal; |
d4730f92 | 9986 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 9987 | |
d4730f92 BS |
9988 | esdi = elf_section_data (o); |
9989 | esdo = elf_section_data (o->output_section); | |
9990 | rela_normal = FALSE; | |
c152c796 AM |
9991 | |
9992 | /* Adjust the reloc addresses and symbol indices. */ | |
9993 | ||
9994 | irela = internal_relocs; | |
9995 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
d4730f92 BS |
9996 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
9997 | /* We start processing the REL relocs, if any. When we reach | |
9998 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
9999 | irelamid = irela; | |
10000 | if (esdi->rel.hdr != NULL) | |
10001 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10002 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10003 | rel_hash_list = rel_hash; |
d4730f92 | 10004 | rela_hash_list = NULL; |
c152c796 | 10005 | last_offset = o->output_offset; |
8b127cbc | 10006 | if (!flinfo->info->relocatable) |
c152c796 AM |
10007 | last_offset += o->output_section->vma; |
10008 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10009 | { | |
10010 | unsigned long r_symndx; | |
10011 | asection *sec; | |
10012 | Elf_Internal_Sym sym; | |
10013 | ||
10014 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10015 | { | |
10016 | rel_hash++; | |
10017 | next_erel = 0; | |
10018 | } | |
10019 | ||
d4730f92 BS |
10020 | if (irela == irelamid) |
10021 | { | |
10022 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10023 | rela_hash_list = rel_hash; | |
10024 | rela_normal = bed->rela_normal; | |
10025 | } | |
10026 | ||
c152c796 | 10027 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10028 | flinfo->info, o, |
c152c796 AM |
10029 | irela->r_offset); |
10030 | if (irela->r_offset >= (bfd_vma) -2) | |
10031 | { | |
10032 | /* This is a reloc for a deleted entry or somesuch. | |
10033 | Turn it into an R_*_NONE reloc, at the same | |
10034 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10035 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10036 | being ordered. */ |
10037 | irela->r_offset = last_offset; | |
10038 | irela->r_info = 0; | |
10039 | irela->r_addend = 0; | |
10040 | continue; | |
10041 | } | |
10042 | ||
10043 | irela->r_offset += o->output_offset; | |
10044 | ||
10045 | /* Relocs in an executable have to be virtual addresses. */ | |
8b127cbc | 10046 | if (!flinfo->info->relocatable) |
c152c796 AM |
10047 | irela->r_offset += o->output_section->vma; |
10048 | ||
10049 | last_offset = irela->r_offset; | |
10050 | ||
10051 | r_symndx = irela->r_info >> r_sym_shift; | |
10052 | if (r_symndx == STN_UNDEF) | |
10053 | continue; | |
10054 | ||
10055 | if (r_symndx >= locsymcount | |
10056 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10057 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10058 | { |
10059 | struct elf_link_hash_entry *rh; | |
10060 | unsigned long indx; | |
10061 | ||
10062 | /* This is a reloc against a global symbol. We | |
10063 | have not yet output all the local symbols, so | |
10064 | we do not know the symbol index of any global | |
10065 | symbol. We set the rel_hash entry for this | |
10066 | reloc to point to the global hash table entry | |
10067 | for this symbol. The symbol index is then | |
ee75fd95 | 10068 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10069 | indx = r_symndx - extsymoff; |
10070 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10071 | while (rh->root.type == bfd_link_hash_indirect | |
10072 | || rh->root.type == bfd_link_hash_warning) | |
10073 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10074 | ||
10075 | /* Setting the index to -2 tells | |
10076 | elf_link_output_extsym that this symbol is | |
10077 | used by a reloc. */ | |
10078 | BFD_ASSERT (rh->indx < 0); | |
10079 | rh->indx = -2; | |
10080 | ||
10081 | *rel_hash = rh; | |
10082 | ||
10083 | continue; | |
10084 | } | |
10085 | ||
10086 | /* This is a reloc against a local symbol. */ | |
10087 | ||
10088 | *rel_hash = NULL; | |
10089 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10090 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10091 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10092 | { | |
10093 | /* I suppose the backend ought to fill in the | |
10094 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10095 | processor specific section. */ |
cf35638d | 10096 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10097 | if (bfd_is_abs_section (sec)) |
10098 | ; | |
c152c796 AM |
10099 | else if (sec == NULL || sec->owner == NULL) |
10100 | { | |
10101 | bfd_set_error (bfd_error_bad_value); | |
10102 | return FALSE; | |
10103 | } | |
10104 | else | |
10105 | { | |
6a8d1586 AM |
10106 | asection *osec = sec->output_section; |
10107 | ||
10108 | /* If we have discarded a section, the output | |
10109 | section will be the absolute section. In | |
ab96bf03 AM |
10110 | case of discarded SEC_MERGE sections, use |
10111 | the kept section. relocate_section should | |
10112 | have already handled discarded linkonce | |
10113 | sections. */ | |
6a8d1586 AM |
10114 | if (bfd_is_abs_section (osec) |
10115 | && sec->kept_section != NULL | |
10116 | && sec->kept_section->output_section != NULL) | |
10117 | { | |
10118 | osec = sec->kept_section->output_section; | |
10119 | irela->r_addend -= osec->vma; | |
10120 | } | |
10121 | ||
10122 | if (!bfd_is_abs_section (osec)) | |
10123 | { | |
10124 | r_symndx = osec->target_index; | |
cf35638d | 10125 | if (r_symndx == STN_UNDEF) |
74541ad4 | 10126 | { |
051d833a AM |
10127 | irela->r_addend += osec->vma; |
10128 | osec = _bfd_nearby_section (output_bfd, osec, | |
10129 | osec->vma); | |
10130 | irela->r_addend -= osec->vma; | |
10131 | r_symndx = osec->target_index; | |
74541ad4 | 10132 | } |
6a8d1586 | 10133 | } |
c152c796 AM |
10134 | } |
10135 | ||
10136 | /* Adjust the addend according to where the | |
10137 | section winds up in the output section. */ | |
10138 | if (rela_normal) | |
10139 | irela->r_addend += sec->output_offset; | |
10140 | } | |
10141 | else | |
10142 | { | |
8b127cbc | 10143 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
10144 | { |
10145 | unsigned long shlink; | |
10146 | const char *name; | |
10147 | asection *osec; | |
6e0b88f1 | 10148 | long indx; |
c152c796 | 10149 | |
8b127cbc | 10150 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10151 | { |
10152 | /* You can't do ld -r -s. */ | |
10153 | bfd_set_error (bfd_error_invalid_operation); | |
10154 | return FALSE; | |
10155 | } | |
10156 | ||
10157 | /* This symbol was skipped earlier, but | |
10158 | since it is needed by a reloc, we | |
10159 | must output it now. */ | |
10160 | shlink = symtab_hdr->sh_link; | |
10161 | name = (bfd_elf_string_from_elf_section | |
10162 | (input_bfd, shlink, sym.st_name)); | |
10163 | if (name == NULL) | |
10164 | return FALSE; | |
10165 | ||
10166 | osec = sec->output_section; | |
10167 | sym.st_shndx = | |
10168 | _bfd_elf_section_from_bfd_section (output_bfd, | |
10169 | osec); | |
10170 | if (sym.st_shndx == SHN_BAD) | |
10171 | return FALSE; | |
10172 | ||
10173 | sym.st_value += sec->output_offset; | |
8b127cbc | 10174 | if (!flinfo->info->relocatable) |
c152c796 AM |
10175 | { |
10176 | sym.st_value += osec->vma; | |
10177 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
10178 | { | |
10179 | /* STT_TLS symbols are relative to PT_TLS | |
10180 | segment base. */ | |
8b127cbc | 10181 | BFD_ASSERT (elf_hash_table (flinfo->info) |
c152c796 | 10182 | ->tls_sec != NULL); |
8b127cbc | 10183 | sym.st_value -= (elf_hash_table (flinfo->info) |
c152c796 AM |
10184 | ->tls_sec->vma); |
10185 | } | |
10186 | } | |
10187 | ||
6e0b88f1 | 10188 | indx = bfd_get_symcount (output_bfd); |
8b127cbc | 10189 | ret = elf_link_output_sym (flinfo, name, &sym, sec, |
6e0b88f1 AM |
10190 | NULL); |
10191 | if (ret == 0) | |
c152c796 | 10192 | return FALSE; |
6e0b88f1 | 10193 | else if (ret == 1) |
8b127cbc | 10194 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
10195 | else |
10196 | abort (); | |
c152c796 AM |
10197 | } |
10198 | ||
8b127cbc | 10199 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
10200 | } |
10201 | ||
10202 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
10203 | | (irela->r_info & r_type_mask)); | |
10204 | } | |
10205 | ||
10206 | /* Swap out the relocs. */ | |
d4730f92 BS |
10207 | input_rel_hdr = esdi->rel.hdr; |
10208 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 10209 | { |
d4730f92 BS |
10210 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
10211 | input_rel_hdr, | |
10212 | internal_relocs, | |
10213 | rel_hash_list)) | |
10214 | return FALSE; | |
c152c796 AM |
10215 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
10216 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10217 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
10218 | } |
10219 | ||
10220 | input_rela_hdr = esdi->rela.hdr; | |
10221 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
10222 | { | |
eac338cf | 10223 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 10224 | input_rela_hdr, |
eac338cf | 10225 | internal_relocs, |
d4730f92 | 10226 | rela_hash_list)) |
c152c796 AM |
10227 | return FALSE; |
10228 | } | |
10229 | } | |
10230 | } | |
10231 | ||
10232 | /* Write out the modified section contents. */ | |
10233 | if (bed->elf_backend_write_section | |
8b127cbc | 10234 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 10235 | contents)) |
c152c796 AM |
10236 | { |
10237 | /* Section written out. */ | |
10238 | } | |
10239 | else switch (o->sec_info_type) | |
10240 | { | |
dbaa2011 | 10241 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
10242 | if (! (_bfd_write_section_stabs |
10243 | (output_bfd, | |
8b127cbc | 10244 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
10245 | o, &elf_section_data (o)->sec_info, contents))) |
10246 | return FALSE; | |
10247 | break; | |
dbaa2011 | 10248 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
10249 | if (! _bfd_write_merged_section (output_bfd, o, |
10250 | elf_section_data (o)->sec_info)) | |
10251 | return FALSE; | |
10252 | break; | |
dbaa2011 | 10253 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 10254 | { |
8b127cbc | 10255 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
10256 | o, contents)) |
10257 | return FALSE; | |
10258 | } | |
10259 | break; | |
2f0c68f2 CM |
10260 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
10261 | { | |
10262 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
10263 | flinfo->info, | |
10264 | o, contents)) | |
10265 | return FALSE; | |
10266 | } | |
10267 | break; | |
c152c796 AM |
10268 | default: |
10269 | { | |
5dabe785 | 10270 | /* FIXME: octets_per_byte. */ |
310fd250 L |
10271 | if (! (o->flags & SEC_EXCLUDE)) |
10272 | { | |
10273 | file_ptr offset = (file_ptr) o->output_offset; | |
10274 | bfd_size_type todo = o->size; | |
10275 | if ((o->flags & SEC_ELF_REVERSE_COPY)) | |
10276 | { | |
10277 | /* Reverse-copy input section to output. */ | |
10278 | do | |
10279 | { | |
10280 | todo -= address_size; | |
10281 | if (! bfd_set_section_contents (output_bfd, | |
10282 | o->output_section, | |
10283 | contents + todo, | |
10284 | offset, | |
10285 | address_size)) | |
10286 | return FALSE; | |
10287 | if (todo == 0) | |
10288 | break; | |
10289 | offset += address_size; | |
10290 | } | |
10291 | while (1); | |
10292 | } | |
10293 | else if (! bfd_set_section_contents (output_bfd, | |
10294 | o->output_section, | |
10295 | contents, | |
10296 | offset, todo)) | |
10297 | return FALSE; | |
10298 | } | |
c152c796 AM |
10299 | } |
10300 | break; | |
10301 | } | |
10302 | } | |
10303 | ||
10304 | return TRUE; | |
10305 | } | |
10306 | ||
10307 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 10308 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
10309 | is used to build constructor and destructor tables when linking |
10310 | with -Ur. */ | |
10311 | ||
10312 | static bfd_boolean | |
10313 | elf_reloc_link_order (bfd *output_bfd, | |
10314 | struct bfd_link_info *info, | |
10315 | asection *output_section, | |
10316 | struct bfd_link_order *link_order) | |
10317 | { | |
10318 | reloc_howto_type *howto; | |
10319 | long indx; | |
10320 | bfd_vma offset; | |
10321 | bfd_vma addend; | |
d4730f92 | 10322 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
10323 | struct elf_link_hash_entry **rel_hash_ptr; |
10324 | Elf_Internal_Shdr *rel_hdr; | |
10325 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
10326 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
10327 | bfd_byte *erel; | |
10328 | unsigned int i; | |
d4730f92 | 10329 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
10330 | |
10331 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
10332 | if (howto == NULL) | |
10333 | { | |
10334 | bfd_set_error (bfd_error_bad_value); | |
10335 | return FALSE; | |
10336 | } | |
10337 | ||
10338 | addend = link_order->u.reloc.p->addend; | |
10339 | ||
d4730f92 BS |
10340 | if (esdo->rel.hdr) |
10341 | reldata = &esdo->rel; | |
10342 | else if (esdo->rela.hdr) | |
10343 | reldata = &esdo->rela; | |
10344 | else | |
10345 | { | |
10346 | reldata = NULL; | |
10347 | BFD_ASSERT (0); | |
10348 | } | |
10349 | ||
c152c796 | 10350 | /* Figure out the symbol index. */ |
d4730f92 | 10351 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
10352 | if (link_order->type == bfd_section_reloc_link_order) |
10353 | { | |
10354 | indx = link_order->u.reloc.p->u.section->target_index; | |
10355 | BFD_ASSERT (indx != 0); | |
10356 | *rel_hash_ptr = NULL; | |
10357 | } | |
10358 | else | |
10359 | { | |
10360 | struct elf_link_hash_entry *h; | |
10361 | ||
10362 | /* Treat a reloc against a defined symbol as though it were | |
10363 | actually against the section. */ | |
10364 | h = ((struct elf_link_hash_entry *) | |
10365 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
10366 | link_order->u.reloc.p->u.name, | |
10367 | FALSE, FALSE, TRUE)); | |
10368 | if (h != NULL | |
10369 | && (h->root.type == bfd_link_hash_defined | |
10370 | || h->root.type == bfd_link_hash_defweak)) | |
10371 | { | |
10372 | asection *section; | |
10373 | ||
10374 | section = h->root.u.def.section; | |
10375 | indx = section->output_section->target_index; | |
10376 | *rel_hash_ptr = NULL; | |
10377 | /* It seems that we ought to add the symbol value to the | |
10378 | addend here, but in practice it has already been added | |
10379 | because it was passed to constructor_callback. */ | |
10380 | addend += section->output_section->vma + section->output_offset; | |
10381 | } | |
10382 | else if (h != NULL) | |
10383 | { | |
10384 | /* Setting the index to -2 tells elf_link_output_extsym that | |
10385 | this symbol is used by a reloc. */ | |
10386 | h->indx = -2; | |
10387 | *rel_hash_ptr = h; | |
10388 | indx = 0; | |
10389 | } | |
10390 | else | |
10391 | { | |
10392 | if (! ((*info->callbacks->unattached_reloc) | |
10393 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
10394 | return FALSE; | |
10395 | indx = 0; | |
10396 | } | |
10397 | } | |
10398 | ||
10399 | /* If this is an inplace reloc, we must write the addend into the | |
10400 | object file. */ | |
10401 | if (howto->partial_inplace && addend != 0) | |
10402 | { | |
10403 | bfd_size_type size; | |
10404 | bfd_reloc_status_type rstat; | |
10405 | bfd_byte *buf; | |
10406 | bfd_boolean ok; | |
10407 | const char *sym_name; | |
10408 | ||
a50b1753 NC |
10409 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
10410 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 10411 | if (buf == NULL && size != 0) |
c152c796 AM |
10412 | return FALSE; |
10413 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
10414 | switch (rstat) | |
10415 | { | |
10416 | case bfd_reloc_ok: | |
10417 | break; | |
10418 | ||
10419 | default: | |
10420 | case bfd_reloc_outofrange: | |
10421 | abort (); | |
10422 | ||
10423 | case bfd_reloc_overflow: | |
10424 | if (link_order->type == bfd_section_reloc_link_order) | |
10425 | sym_name = bfd_section_name (output_bfd, | |
10426 | link_order->u.reloc.p->u.section); | |
10427 | else | |
10428 | sym_name = link_order->u.reloc.p->u.name; | |
10429 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
10430 | (info, NULL, sym_name, howto->name, addend, NULL, |
10431 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
10432 | { |
10433 | free (buf); | |
10434 | return FALSE; | |
10435 | } | |
10436 | break; | |
10437 | } | |
10438 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
10439 | link_order->offset, size); | |
10440 | free (buf); | |
10441 | if (! ok) | |
10442 | return FALSE; | |
10443 | } | |
10444 | ||
10445 | /* The address of a reloc is relative to the section in a | |
10446 | relocatable file, and is a virtual address in an executable | |
10447 | file. */ | |
10448 | offset = link_order->offset; | |
10449 | if (! info->relocatable) | |
10450 | offset += output_section->vma; | |
10451 | ||
10452 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
10453 | { | |
10454 | irel[i].r_offset = offset; | |
10455 | irel[i].r_info = 0; | |
10456 | irel[i].r_addend = 0; | |
10457 | } | |
10458 | if (bed->s->arch_size == 32) | |
10459 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
10460 | else | |
10461 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
10462 | ||
d4730f92 | 10463 | rel_hdr = reldata->hdr; |
c152c796 AM |
10464 | erel = rel_hdr->contents; |
10465 | if (rel_hdr->sh_type == SHT_REL) | |
10466 | { | |
d4730f92 | 10467 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
10468 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
10469 | } | |
10470 | else | |
10471 | { | |
10472 | irel[0].r_addend = addend; | |
d4730f92 | 10473 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
10474 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
10475 | } | |
10476 | ||
d4730f92 | 10477 | ++reldata->count; |
c152c796 AM |
10478 | |
10479 | return TRUE; | |
10480 | } | |
10481 | ||
0b52efa6 PB |
10482 | |
10483 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
10484 | ||
10485 | static bfd_vma | |
10486 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
10487 | { | |
10488 | Elf_Internal_Shdr **elf_shdrp; | |
10489 | asection *s; | |
10490 | int elfsec; | |
10491 | ||
10492 | s = p->u.indirect.section; | |
10493 | elf_shdrp = elf_elfsections (s->owner); | |
10494 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
10495 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
10496 | /* PR 290: |
10497 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 10498 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
10499 | sh_info fields. Hence we could get the situation |
10500 | where elfsec is 0. */ | |
10501 | if (elfsec == 0) | |
10502 | { | |
10503 | const struct elf_backend_data *bed | |
10504 | = get_elf_backend_data (s->owner); | |
10505 | if (bed->link_order_error_handler) | |
d003868e AM |
10506 | bed->link_order_error_handler |
10507 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
10508 | return 0; |
10509 | } | |
10510 | else | |
10511 | { | |
10512 | s = elf_shdrp[elfsec]->bfd_section; | |
10513 | return s->output_section->vma + s->output_offset; | |
10514 | } | |
0b52efa6 PB |
10515 | } |
10516 | ||
10517 | ||
10518 | /* Compare two sections based on the locations of the sections they are | |
10519 | linked to. Used by elf_fixup_link_order. */ | |
10520 | ||
10521 | static int | |
10522 | compare_link_order (const void * a, const void * b) | |
10523 | { | |
10524 | bfd_vma apos; | |
10525 | bfd_vma bpos; | |
10526 | ||
10527 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
10528 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
10529 | if (apos < bpos) | |
10530 | return -1; | |
10531 | return apos > bpos; | |
10532 | } | |
10533 | ||
10534 | ||
10535 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
10536 | order as their linked sections. Returns false if this could not be done | |
10537 | because an output section includes both ordered and unordered | |
10538 | sections. Ideally we'd do this in the linker proper. */ | |
10539 | ||
10540 | static bfd_boolean | |
10541 | elf_fixup_link_order (bfd *abfd, asection *o) | |
10542 | { | |
10543 | int seen_linkorder; | |
10544 | int seen_other; | |
10545 | int n; | |
10546 | struct bfd_link_order *p; | |
10547 | bfd *sub; | |
10548 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 10549 | unsigned elfsec; |
0b52efa6 | 10550 | struct bfd_link_order **sections; |
d33cdfe3 | 10551 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 10552 | bfd_vma offset; |
3b36f7e6 | 10553 | |
d33cdfe3 L |
10554 | other_sec = NULL; |
10555 | linkorder_sec = NULL; | |
0b52efa6 PB |
10556 | seen_other = 0; |
10557 | seen_linkorder = 0; | |
8423293d | 10558 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 10559 | { |
d33cdfe3 | 10560 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
10561 | { |
10562 | s = p->u.indirect.section; | |
d33cdfe3 L |
10563 | sub = s->owner; |
10564 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
10565 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
10566 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
10567 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
10568 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
10569 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
10570 | { |
10571 | seen_linkorder++; | |
10572 | linkorder_sec = s; | |
10573 | } | |
0b52efa6 | 10574 | else |
d33cdfe3 L |
10575 | { |
10576 | seen_other++; | |
10577 | other_sec = s; | |
10578 | } | |
0b52efa6 PB |
10579 | } |
10580 | else | |
10581 | seen_other++; | |
d33cdfe3 L |
10582 | |
10583 | if (seen_other && seen_linkorder) | |
10584 | { | |
10585 | if (other_sec && linkorder_sec) | |
10586 | (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), | |
10587 | o, linkorder_sec, | |
10588 | linkorder_sec->owner, other_sec, | |
10589 | other_sec->owner); | |
10590 | else | |
10591 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), | |
10592 | o); | |
10593 | bfd_set_error (bfd_error_bad_value); | |
10594 | return FALSE; | |
10595 | } | |
0b52efa6 PB |
10596 | } |
10597 | ||
10598 | if (!seen_linkorder) | |
10599 | return TRUE; | |
10600 | ||
0b52efa6 | 10601 | sections = (struct bfd_link_order **) |
14b1c01e AM |
10602 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
10603 | if (sections == NULL) | |
10604 | return FALSE; | |
0b52efa6 | 10605 | seen_linkorder = 0; |
3b36f7e6 | 10606 | |
8423293d | 10607 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
10608 | { |
10609 | sections[seen_linkorder++] = p; | |
10610 | } | |
10611 | /* Sort the input sections in the order of their linked section. */ | |
10612 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
10613 | compare_link_order); | |
10614 | ||
10615 | /* Change the offsets of the sections. */ | |
10616 | offset = 0; | |
10617 | for (n = 0; n < seen_linkorder; n++) | |
10618 | { | |
10619 | s = sections[n]->u.indirect.section; | |
461686a3 | 10620 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
0b52efa6 PB |
10621 | s->output_offset = offset; |
10622 | sections[n]->offset = offset; | |
5dabe785 | 10623 | /* FIXME: octets_per_byte. */ |
0b52efa6 PB |
10624 | offset += sections[n]->size; |
10625 | } | |
10626 | ||
4dd07732 | 10627 | free (sections); |
0b52efa6 PB |
10628 | return TRUE; |
10629 | } | |
10630 | ||
9f7c3e5e AM |
10631 | static void |
10632 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
10633 | { | |
10634 | asection *o; | |
10635 | ||
10636 | if (flinfo->symstrtab != NULL) | |
10637 | _bfd_stringtab_free (flinfo->symstrtab); | |
10638 | if (flinfo->contents != NULL) | |
10639 | free (flinfo->contents); | |
10640 | if (flinfo->external_relocs != NULL) | |
10641 | free (flinfo->external_relocs); | |
10642 | if (flinfo->internal_relocs != NULL) | |
10643 | free (flinfo->internal_relocs); | |
10644 | if (flinfo->external_syms != NULL) | |
10645 | free (flinfo->external_syms); | |
10646 | if (flinfo->locsym_shndx != NULL) | |
10647 | free (flinfo->locsym_shndx); | |
10648 | if (flinfo->internal_syms != NULL) | |
10649 | free (flinfo->internal_syms); | |
10650 | if (flinfo->indices != NULL) | |
10651 | free (flinfo->indices); | |
10652 | if (flinfo->sections != NULL) | |
10653 | free (flinfo->sections); | |
10654 | if (flinfo->symbuf != NULL) | |
10655 | free (flinfo->symbuf); | |
10656 | if (flinfo->symshndxbuf != NULL) | |
10657 | free (flinfo->symshndxbuf); | |
10658 | for (o = obfd->sections; o != NULL; o = o->next) | |
10659 | { | |
10660 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
10661 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
10662 | free (esdo->rel.hashes); | |
10663 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
10664 | free (esdo->rela.hashes); | |
10665 | } | |
10666 | } | |
0b52efa6 | 10667 | |
c152c796 AM |
10668 | /* Do the final step of an ELF link. */ |
10669 | ||
10670 | bfd_boolean | |
10671 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
10672 | { | |
10673 | bfd_boolean dynamic; | |
10674 | bfd_boolean emit_relocs; | |
10675 | bfd *dynobj; | |
8b127cbc | 10676 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
10677 | asection *o; |
10678 | struct bfd_link_order *p; | |
10679 | bfd *sub; | |
c152c796 AM |
10680 | bfd_size_type max_contents_size; |
10681 | bfd_size_type max_external_reloc_size; | |
10682 | bfd_size_type max_internal_reloc_count; | |
10683 | bfd_size_type max_sym_count; | |
10684 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
10685 | Elf_Internal_Sym elfsym; |
10686 | unsigned int i; | |
10687 | Elf_Internal_Shdr *symtab_hdr; | |
10688 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
10689 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10690 | struct elf_outext_info eoinfo; | |
10691 | bfd_boolean merged; | |
10692 | size_t relativecount = 0; | |
10693 | asection *reldyn = 0; | |
10694 | bfd_size_type amt; | |
104d59d1 JM |
10695 | asection *attr_section = NULL; |
10696 | bfd_vma attr_size = 0; | |
10697 | const char *std_attrs_section; | |
c152c796 AM |
10698 | |
10699 | if (! is_elf_hash_table (info->hash)) | |
10700 | return FALSE; | |
10701 | ||
10702 | if (info->shared) | |
10703 | abfd->flags |= DYNAMIC; | |
10704 | ||
10705 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
10706 | dynobj = elf_hash_table (info)->dynobj; | |
10707 | ||
10708 | emit_relocs = (info->relocatable | |
a4676736 | 10709 | || info->emitrelocations); |
c152c796 | 10710 | |
8b127cbc AM |
10711 | flinfo.info = info; |
10712 | flinfo.output_bfd = abfd; | |
10713 | flinfo.symstrtab = _bfd_elf_stringtab_init (); | |
10714 | if (flinfo.symstrtab == NULL) | |
c152c796 AM |
10715 | return FALSE; |
10716 | ||
10717 | if (! dynamic) | |
10718 | { | |
8b127cbc AM |
10719 | flinfo.dynsym_sec = NULL; |
10720 | flinfo.hash_sec = NULL; | |
10721 | flinfo.symver_sec = NULL; | |
c152c796 AM |
10722 | } |
10723 | else | |
10724 | { | |
3d4d4302 AM |
10725 | flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym"); |
10726 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); | |
202e2356 | 10727 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 10728 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
10729 | /* Note that it is OK if symver_sec is NULL. */ |
10730 | } | |
10731 | ||
8b127cbc AM |
10732 | flinfo.contents = NULL; |
10733 | flinfo.external_relocs = NULL; | |
10734 | flinfo.internal_relocs = NULL; | |
10735 | flinfo.external_syms = NULL; | |
10736 | flinfo.locsym_shndx = NULL; | |
10737 | flinfo.internal_syms = NULL; | |
10738 | flinfo.indices = NULL; | |
10739 | flinfo.sections = NULL; | |
10740 | flinfo.symbuf = NULL; | |
10741 | flinfo.symshndxbuf = NULL; | |
10742 | flinfo.symbuf_count = 0; | |
10743 | flinfo.shndxbuf_size = 0; | |
ffbc01cc | 10744 | flinfo.filesym_count = 0; |
c152c796 | 10745 | |
104d59d1 JM |
10746 | /* The object attributes have been merged. Remove the input |
10747 | sections from the link, and set the contents of the output | |
10748 | secton. */ | |
10749 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
10750 | for (o = abfd->sections; o != NULL; o = o->next) | |
10751 | { | |
10752 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
10753 | || strcmp (o->name, ".gnu.attributes") == 0) | |
10754 | { | |
10755 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
10756 | { | |
10757 | asection *input_section; | |
10758 | ||
10759 | if (p->type != bfd_indirect_link_order) | |
10760 | continue; | |
10761 | input_section = p->u.indirect.section; | |
10762 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
10763 | elf_link_input_bfd ignores this section. */ | |
10764 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
10765 | } | |
a0c8462f | 10766 | |
104d59d1 JM |
10767 | attr_size = bfd_elf_obj_attr_size (abfd); |
10768 | if (attr_size) | |
10769 | { | |
10770 | bfd_set_section_size (abfd, o, attr_size); | |
10771 | attr_section = o; | |
10772 | /* Skip this section later on. */ | |
10773 | o->map_head.link_order = NULL; | |
10774 | } | |
10775 | else | |
10776 | o->flags |= SEC_EXCLUDE; | |
10777 | } | |
10778 | } | |
10779 | ||
c152c796 AM |
10780 | /* Count up the number of relocations we will output for each output |
10781 | section, so that we know the sizes of the reloc sections. We | |
10782 | also figure out some maximum sizes. */ | |
10783 | max_contents_size = 0; | |
10784 | max_external_reloc_size = 0; | |
10785 | max_internal_reloc_count = 0; | |
10786 | max_sym_count = 0; | |
10787 | max_sym_shndx_count = 0; | |
10788 | merged = FALSE; | |
10789 | for (o = abfd->sections; o != NULL; o = o->next) | |
10790 | { | |
10791 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
10792 | o->reloc_count = 0; | |
10793 | ||
8423293d | 10794 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
10795 | { |
10796 | unsigned int reloc_count = 0; | |
10797 | struct bfd_elf_section_data *esdi = NULL; | |
c152c796 AM |
10798 | |
10799 | if (p->type == bfd_section_reloc_link_order | |
10800 | || p->type == bfd_symbol_reloc_link_order) | |
10801 | reloc_count = 1; | |
10802 | else if (p->type == bfd_indirect_link_order) | |
10803 | { | |
10804 | asection *sec; | |
10805 | ||
10806 | sec = p->u.indirect.section; | |
10807 | esdi = elf_section_data (sec); | |
10808 | ||
10809 | /* Mark all sections which are to be included in the | |
10810 | link. This will normally be every section. We need | |
10811 | to do this so that we can identify any sections which | |
10812 | the linker has decided to not include. */ | |
10813 | sec->linker_mark = TRUE; | |
10814 | ||
10815 | if (sec->flags & SEC_MERGE) | |
10816 | merged = TRUE; | |
10817 | ||
aed64b35 L |
10818 | if (esdo->this_hdr.sh_type == SHT_REL |
10819 | || esdo->this_hdr.sh_type == SHT_RELA) | |
10820 | /* Some backends use reloc_count in relocation sections | |
10821 | to count particular types of relocs. Of course, | |
10822 | reloc sections themselves can't have relocations. */ | |
10823 | reloc_count = 0; | |
10824 | else if (info->relocatable || info->emitrelocations) | |
c152c796 AM |
10825 | reloc_count = sec->reloc_count; |
10826 | else if (bed->elf_backend_count_relocs) | |
58217f29 | 10827 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); |
c152c796 | 10828 | |
eea6121a AM |
10829 | if (sec->rawsize > max_contents_size) |
10830 | max_contents_size = sec->rawsize; | |
10831 | if (sec->size > max_contents_size) | |
10832 | max_contents_size = sec->size; | |
c152c796 AM |
10833 | |
10834 | /* We are interested in just local symbols, not all | |
10835 | symbols. */ | |
10836 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
10837 | && (sec->owner->flags & DYNAMIC) == 0) | |
10838 | { | |
10839 | size_t sym_count; | |
10840 | ||
10841 | if (elf_bad_symtab (sec->owner)) | |
10842 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
10843 | / bed->s->sizeof_sym); | |
10844 | else | |
10845 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
10846 | ||
10847 | if (sym_count > max_sym_count) | |
10848 | max_sym_count = sym_count; | |
10849 | ||
10850 | if (sym_count > max_sym_shndx_count | |
10851 | && elf_symtab_shndx (sec->owner) != 0) | |
10852 | max_sym_shndx_count = sym_count; | |
10853 | ||
10854 | if ((sec->flags & SEC_RELOC) != 0) | |
10855 | { | |
d4730f92 | 10856 | size_t ext_size = 0; |
c152c796 | 10857 | |
d4730f92 BS |
10858 | if (esdi->rel.hdr != NULL) |
10859 | ext_size = esdi->rel.hdr->sh_size; | |
10860 | if (esdi->rela.hdr != NULL) | |
10861 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 10862 | |
c152c796 AM |
10863 | if (ext_size > max_external_reloc_size) |
10864 | max_external_reloc_size = ext_size; | |
10865 | if (sec->reloc_count > max_internal_reloc_count) | |
10866 | max_internal_reloc_count = sec->reloc_count; | |
10867 | } | |
10868 | } | |
10869 | } | |
10870 | ||
10871 | if (reloc_count == 0) | |
10872 | continue; | |
10873 | ||
10874 | o->reloc_count += reloc_count; | |
10875 | ||
d4730f92 BS |
10876 | if (p->type == bfd_indirect_link_order |
10877 | && (info->relocatable || info->emitrelocations)) | |
c152c796 | 10878 | { |
d4730f92 BS |
10879 | if (esdi->rel.hdr) |
10880 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); | |
10881 | if (esdi->rela.hdr) | |
10882 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); | |
10883 | } | |
10884 | else | |
10885 | { | |
10886 | if (o->use_rela_p) | |
10887 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 10888 | else |
d4730f92 | 10889 | esdo->rel.count += reloc_count; |
c152c796 | 10890 | } |
c152c796 AM |
10891 | } |
10892 | ||
10893 | if (o->reloc_count > 0) | |
10894 | o->flags |= SEC_RELOC; | |
10895 | else | |
10896 | { | |
10897 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
10898 | set it (this is probably a bug) and if it is set | |
10899 | assign_section_numbers will create a reloc section. */ | |
10900 | o->flags &=~ SEC_RELOC; | |
10901 | } | |
10902 | ||
10903 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
10904 | zero. This is done in elf_fake_sections as well, but forcing | |
10905 | the VMA to 0 here will ensure that relocs against these | |
10906 | sections are handled correctly. */ | |
10907 | if ((o->flags & SEC_ALLOC) == 0 | |
10908 | && ! o->user_set_vma) | |
10909 | o->vma = 0; | |
10910 | } | |
10911 | ||
10912 | if (! info->relocatable && merged) | |
10913 | elf_link_hash_traverse (elf_hash_table (info), | |
10914 | _bfd_elf_link_sec_merge_syms, abfd); | |
10915 | ||
10916 | /* Figure out the file positions for everything but the symbol table | |
10917 | and the relocs. We set symcount to force assign_section_numbers | |
10918 | to create a symbol table. */ | |
8539e4e8 | 10919 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
10920 | BFD_ASSERT (! abfd->output_has_begun); |
10921 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
10922 | goto error_return; | |
10923 | ||
ee75fd95 | 10924 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
10925 | for (o = abfd->sections; o != NULL; o = o->next) |
10926 | { | |
d4730f92 | 10927 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
10928 | if ((o->flags & SEC_RELOC) != 0) |
10929 | { | |
d4730f92 BS |
10930 | if (esdo->rel.hdr |
10931 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) | |
c152c796 AM |
10932 | goto error_return; |
10933 | ||
d4730f92 BS |
10934 | if (esdo->rela.hdr |
10935 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) | |
c152c796 AM |
10936 | goto error_return; |
10937 | } | |
10938 | ||
10939 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
10940 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
10941 | esdo->rel.count = 0; |
10942 | esdo->rela.count = 0; | |
0ce398f1 L |
10943 | |
10944 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
10945 | { | |
10946 | /* Cache the section contents so that they can be compressed | |
10947 | later. Use bfd_malloc since it will be freed by | |
10948 | bfd_compress_section_contents. */ | |
10949 | unsigned char *contents = esdo->this_hdr.contents; | |
10950 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
10951 | abort (); | |
10952 | contents | |
10953 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
10954 | if (contents == NULL) | |
10955 | goto error_return; | |
10956 | esdo->this_hdr.contents = contents; | |
10957 | } | |
c152c796 AM |
10958 | } |
10959 | ||
c152c796 | 10960 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
10961 | .symtab, .strtab, and non-loaded reloc sections. We start the |
10962 | .symtab section at the current file position, and write directly | |
10963 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
10964 | bfd_get_symcount (abfd) = 0; |
10965 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
10966 | /* sh_name is set in prep_headers. */ | |
10967 | symtab_hdr->sh_type = SHT_SYMTAB; | |
10968 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
10969 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
10970 | /* sh_link is set in assign_section_numbers. */ | |
10971 | /* sh_info is set below. */ | |
10972 | /* sh_offset is set just below. */ | |
72de5009 | 10973 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 10974 | |
c152c796 AM |
10975 | /* Allocate a buffer to hold swapped out symbols. This is to avoid |
10976 | continuously seeking to the right position in the file. */ | |
10977 | if (! info->keep_memory || max_sym_count < 20) | |
8b127cbc | 10978 | flinfo.symbuf_size = 20; |
c152c796 | 10979 | else |
8b127cbc AM |
10980 | flinfo.symbuf_size = max_sym_count; |
10981 | amt = flinfo.symbuf_size; | |
c152c796 | 10982 | amt *= bed->s->sizeof_sym; |
8b127cbc AM |
10983 | flinfo.symbuf = (bfd_byte *) bfd_malloc (amt); |
10984 | if (flinfo.symbuf == NULL) | |
c152c796 | 10985 | goto error_return; |
4fbb74a6 | 10986 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
c152c796 AM |
10987 | { |
10988 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
10989 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
8b127cbc | 10990 | flinfo.shndxbuf_size = amt; |
c152c796 | 10991 | amt *= sizeof (Elf_External_Sym_Shndx); |
8b127cbc AM |
10992 | flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
10993 | if (flinfo.symshndxbuf == NULL) | |
c152c796 AM |
10994 | goto error_return; |
10995 | } | |
10996 | ||
8539e4e8 | 10997 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 10998 | { |
8539e4e8 AM |
10999 | file_ptr off = elf_next_file_pos (abfd); |
11000 | ||
11001 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
11002 | ||
11003 | /* Note that at this point elf_next_file_pos (abfd) is | |
11004 | incorrect. We do not yet know the size of the .symtab section. | |
11005 | We correct next_file_pos below, after we do know the size. */ | |
11006 | ||
11007 | /* Start writing out the symbol table. The first symbol is always a | |
11008 | dummy symbol. */ | |
c152c796 AM |
11009 | elfsym.st_value = 0; |
11010 | elfsym.st_size = 0; | |
11011 | elfsym.st_info = 0; | |
11012 | elfsym.st_other = 0; | |
11013 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 11014 | elfsym.st_target_internal = 0; |
8b127cbc | 11015 | if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr, |
6e0b88f1 | 11016 | NULL) != 1) |
c152c796 | 11017 | goto error_return; |
c152c796 | 11018 | |
8539e4e8 AM |
11019 | /* Output a symbol for each section. We output these even if we are |
11020 | discarding local symbols, since they are used for relocs. These | |
11021 | symbols have no names. We store the index of each one in the | |
11022 | index field of the section, so that we can find it again when | |
11023 | outputting relocs. */ | |
11024 | ||
c152c796 AM |
11025 | elfsym.st_size = 0; |
11026 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11027 | elfsym.st_other = 0; | |
f0b5bb34 | 11028 | elfsym.st_value = 0; |
35fc36a8 | 11029 | elfsym.st_target_internal = 0; |
c152c796 AM |
11030 | for (i = 1; i < elf_numsections (abfd); i++) |
11031 | { | |
11032 | o = bfd_section_from_elf_index (abfd, i); | |
11033 | if (o != NULL) | |
f0b5bb34 AM |
11034 | { |
11035 | o->target_index = bfd_get_symcount (abfd); | |
11036 | elfsym.st_shndx = i; | |
11037 | if (!info->relocatable) | |
11038 | elfsym.st_value = o->vma; | |
8b127cbc | 11039 | if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1) |
f0b5bb34 AM |
11040 | goto error_return; |
11041 | } | |
c152c796 AM |
11042 | } |
11043 | } | |
11044 | ||
11045 | /* Allocate some memory to hold information read in from the input | |
11046 | files. */ | |
11047 | if (max_contents_size != 0) | |
11048 | { | |
8b127cbc AM |
11049 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
11050 | if (flinfo.contents == NULL) | |
c152c796 AM |
11051 | goto error_return; |
11052 | } | |
11053 | ||
11054 | if (max_external_reloc_size != 0) | |
11055 | { | |
8b127cbc AM |
11056 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
11057 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
11058 | goto error_return; |
11059 | } | |
11060 | ||
11061 | if (max_internal_reloc_count != 0) | |
11062 | { | |
11063 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
11064 | amt *= sizeof (Elf_Internal_Rela); | |
8b127cbc AM |
11065 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
11066 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
11067 | goto error_return; |
11068 | } | |
11069 | ||
11070 | if (max_sym_count != 0) | |
11071 | { | |
11072 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
11073 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
11074 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
11075 | goto error_return; |
11076 | ||
11077 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
11078 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
11079 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
11080 | goto error_return; |
11081 | ||
11082 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
11083 | flinfo.indices = (long int *) bfd_malloc (amt); |
11084 | if (flinfo.indices == NULL) | |
c152c796 AM |
11085 | goto error_return; |
11086 | ||
11087 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
11088 | flinfo.sections = (asection **) bfd_malloc (amt); |
11089 | if (flinfo.sections == NULL) | |
c152c796 AM |
11090 | goto error_return; |
11091 | } | |
11092 | ||
11093 | if (max_sym_shndx_count != 0) | |
11094 | { | |
11095 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
11096 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
11097 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
11098 | goto error_return; |
11099 | } | |
11100 | ||
11101 | if (elf_hash_table (info)->tls_sec) | |
11102 | { | |
11103 | bfd_vma base, end = 0; | |
11104 | asection *sec; | |
11105 | ||
11106 | for (sec = elf_hash_table (info)->tls_sec; | |
11107 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
11108 | sec = sec->next) | |
11109 | { | |
3a800eb9 | 11110 | bfd_size_type size = sec->size; |
c152c796 | 11111 | |
3a800eb9 AM |
11112 | if (size == 0 |
11113 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 11114 | { |
91d6fa6a NC |
11115 | struct bfd_link_order *ord = sec->map_tail.link_order; |
11116 | ||
11117 | if (ord != NULL) | |
11118 | size = ord->offset + ord->size; | |
c152c796 AM |
11119 | } |
11120 | end = sec->vma + size; | |
11121 | } | |
11122 | base = elf_hash_table (info)->tls_sec->vma; | |
7dc98aea RO |
11123 | /* Only align end of TLS section if static TLS doesn't have special |
11124 | alignment requirements. */ | |
11125 | if (bed->static_tls_alignment == 1) | |
11126 | end = align_power (end, | |
11127 | elf_hash_table (info)->tls_sec->alignment_power); | |
c152c796 AM |
11128 | elf_hash_table (info)->tls_size = end - base; |
11129 | } | |
11130 | ||
0b52efa6 PB |
11131 | /* Reorder SHF_LINK_ORDER sections. */ |
11132 | for (o = abfd->sections; o != NULL; o = o->next) | |
11133 | { | |
11134 | if (!elf_fixup_link_order (abfd, o)) | |
11135 | return FALSE; | |
11136 | } | |
11137 | ||
2f0c68f2 CM |
11138 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
11139 | return FALSE; | |
11140 | ||
c152c796 AM |
11141 | /* Since ELF permits relocations to be against local symbols, we |
11142 | must have the local symbols available when we do the relocations. | |
11143 | Since we would rather only read the local symbols once, and we | |
11144 | would rather not keep them in memory, we handle all the | |
11145 | relocations for a single input file at the same time. | |
11146 | ||
11147 | Unfortunately, there is no way to know the total number of local | |
11148 | symbols until we have seen all of them, and the local symbol | |
11149 | indices precede the global symbol indices. This means that when | |
11150 | we are generating relocatable output, and we see a reloc against | |
11151 | a global symbol, we can not know the symbol index until we have | |
11152 | finished examining all the local symbols to see which ones we are | |
11153 | going to output. To deal with this, we keep the relocations in | |
11154 | memory, and don't output them until the end of the link. This is | |
11155 | an unfortunate waste of memory, but I don't see a good way around | |
11156 | it. Fortunately, it only happens when performing a relocatable | |
11157 | link, which is not the common case. FIXME: If keep_memory is set | |
11158 | we could write the relocs out and then read them again; I don't | |
11159 | know how bad the memory loss will be. */ | |
11160 | ||
c72f2fb2 | 11161 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
11162 | sub->output_has_begun = FALSE; |
11163 | for (o = abfd->sections; o != NULL; o = o->next) | |
11164 | { | |
8423293d | 11165 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11166 | { |
11167 | if (p->type == bfd_indirect_link_order | |
11168 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
11169 | == bfd_target_elf_flavour) | |
11170 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
11171 | { | |
11172 | if (! sub->output_has_begun) | |
11173 | { | |
8b127cbc | 11174 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
11175 | goto error_return; |
11176 | sub->output_has_begun = TRUE; | |
11177 | } | |
11178 | } | |
11179 | else if (p->type == bfd_section_reloc_link_order | |
11180 | || p->type == bfd_symbol_reloc_link_order) | |
11181 | { | |
11182 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
11183 | goto error_return; | |
11184 | } | |
11185 | else | |
11186 | { | |
11187 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
11188 | { |
11189 | if (p->type == bfd_indirect_link_order | |
11190 | && (bfd_get_flavour (sub) | |
11191 | == bfd_target_elf_flavour) | |
11192 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
11193 | != bed->s->elfclass)) | |
11194 | { | |
11195 | const char *iclass, *oclass; | |
11196 | ||
11197 | if (bed->s->elfclass == ELFCLASS64) | |
11198 | { | |
11199 | iclass = "ELFCLASS32"; | |
11200 | oclass = "ELFCLASS64"; | |
11201 | } | |
11202 | else | |
11203 | { | |
11204 | iclass = "ELFCLASS64"; | |
11205 | oclass = "ELFCLASS32"; | |
11206 | } | |
11207 | ||
11208 | bfd_set_error (bfd_error_wrong_format); | |
11209 | (*_bfd_error_handler) | |
11210 | (_("%B: file class %s incompatible with %s"), | |
11211 | sub, iclass, oclass); | |
11212 | } | |
11213 | ||
11214 | goto error_return; | |
11215 | } | |
c152c796 AM |
11216 | } |
11217 | } | |
11218 | } | |
11219 | ||
c0f00686 L |
11220 | /* Free symbol buffer if needed. */ |
11221 | if (!info->reduce_memory_overheads) | |
11222 | { | |
c72f2fb2 | 11223 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
11224 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
11225 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
11226 | { |
11227 | free (elf_tdata (sub)->symbuf); | |
11228 | elf_tdata (sub)->symbuf = NULL; | |
11229 | } | |
11230 | } | |
11231 | ||
c152c796 AM |
11232 | /* Output any global symbols that got converted to local in a |
11233 | version script or due to symbol visibility. We do this in a | |
11234 | separate step since ELF requires all local symbols to appear | |
11235 | prior to any global symbols. FIXME: We should only do this if | |
11236 | some global symbols were, in fact, converted to become local. | |
11237 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
11238 | eoinfo.failed = FALSE; | |
8b127cbc | 11239 | eoinfo.flinfo = &flinfo; |
c152c796 | 11240 | eoinfo.localsyms = TRUE; |
34a79995 | 11241 | eoinfo.file_sym_done = FALSE; |
7686d77d | 11242 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
11243 | if (eoinfo.failed) |
11244 | return FALSE; | |
11245 | ||
4e617b1e PB |
11246 | /* If backend needs to output some local symbols not present in the hash |
11247 | table, do it now. */ | |
8539e4e8 AM |
11248 | if (bed->elf_backend_output_arch_local_syms |
11249 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 11250 | { |
6e0b88f1 | 11251 | typedef int (*out_sym_func) |
4e617b1e PB |
11252 | (void *, const char *, Elf_Internal_Sym *, asection *, |
11253 | struct elf_link_hash_entry *); | |
11254 | ||
11255 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
8b127cbc | 11256 | (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym))) |
4e617b1e PB |
11257 | return FALSE; |
11258 | } | |
11259 | ||
c152c796 AM |
11260 | /* That wrote out all the local symbols. Finish up the symbol table |
11261 | with the global symbols. Even if we want to strip everything we | |
11262 | can, we still need to deal with those global symbols that got | |
11263 | converted to local in a version script. */ | |
11264 | ||
11265 | /* The sh_info field records the index of the first non local symbol. */ | |
11266 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
11267 | ||
11268 | if (dynamic | |
8b127cbc AM |
11269 | && flinfo.dynsym_sec != NULL |
11270 | && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
11271 | { |
11272 | Elf_Internal_Sym sym; | |
8b127cbc | 11273 | bfd_byte *dynsym = flinfo.dynsym_sec->contents; |
c152c796 AM |
11274 | long last_local = 0; |
11275 | ||
11276 | /* Write out the section symbols for the output sections. */ | |
67687978 | 11277 | if (info->shared || elf_hash_table (info)->is_relocatable_executable) |
c152c796 AM |
11278 | { |
11279 | asection *s; | |
11280 | ||
11281 | sym.st_size = 0; | |
11282 | sym.st_name = 0; | |
11283 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11284 | sym.st_other = 0; | |
35fc36a8 | 11285 | sym.st_target_internal = 0; |
c152c796 AM |
11286 | |
11287 | for (s = abfd->sections; s != NULL; s = s->next) | |
11288 | { | |
11289 | int indx; | |
11290 | bfd_byte *dest; | |
11291 | long dynindx; | |
11292 | ||
c152c796 | 11293 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
11294 | if (dynindx <= 0) |
11295 | continue; | |
11296 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
11297 | BFD_ASSERT (indx > 0); |
11298 | sym.st_shndx = indx; | |
c0d5a53d L |
11299 | if (! check_dynsym (abfd, &sym)) |
11300 | return FALSE; | |
c152c796 AM |
11301 | sym.st_value = s->vma; |
11302 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
11303 | if (last_local < dynindx) |
11304 | last_local = dynindx; | |
c152c796 AM |
11305 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
11306 | } | |
c152c796 AM |
11307 | } |
11308 | ||
11309 | /* Write out the local dynsyms. */ | |
11310 | if (elf_hash_table (info)->dynlocal) | |
11311 | { | |
11312 | struct elf_link_local_dynamic_entry *e; | |
11313 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
11314 | { | |
11315 | asection *s; | |
11316 | bfd_byte *dest; | |
11317 | ||
935bd1e0 | 11318 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
11319 | Note that we saved a word of storage and overwrote |
11320 | the original st_name with the dynstr_index. */ | |
11321 | sym = e->isym; | |
935bd1e0 | 11322 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 11323 | |
cb33740c AM |
11324 | s = bfd_section_from_elf_index (e->input_bfd, |
11325 | e->isym.st_shndx); | |
11326 | if (s != NULL) | |
c152c796 | 11327 | { |
c152c796 AM |
11328 | sym.st_shndx = |
11329 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
11330 | if (! check_dynsym (abfd, &sym)) |
11331 | return FALSE; | |
c152c796 AM |
11332 | sym.st_value = (s->output_section->vma |
11333 | + s->output_offset | |
11334 | + e->isym.st_value); | |
11335 | } | |
11336 | ||
11337 | if (last_local < e->dynindx) | |
11338 | last_local = e->dynindx; | |
11339 | ||
11340 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
11341 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
11342 | } | |
11343 | } | |
11344 | ||
8b127cbc | 11345 | elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info = |
c152c796 AM |
11346 | last_local + 1; |
11347 | } | |
11348 | ||
11349 | /* We get the global symbols from the hash table. */ | |
11350 | eoinfo.failed = FALSE; | |
11351 | eoinfo.localsyms = FALSE; | |
8b127cbc | 11352 | eoinfo.flinfo = &flinfo; |
7686d77d | 11353 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
11354 | if (eoinfo.failed) |
11355 | return FALSE; | |
11356 | ||
11357 | /* If backend needs to output some symbols not present in the hash | |
11358 | table, do it now. */ | |
8539e4e8 AM |
11359 | if (bed->elf_backend_output_arch_syms |
11360 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 11361 | { |
6e0b88f1 | 11362 | typedef int (*out_sym_func) |
c152c796 AM |
11363 | (void *, const char *, Elf_Internal_Sym *, asection *, |
11364 | struct elf_link_hash_entry *); | |
11365 | ||
11366 | if (! ((*bed->elf_backend_output_arch_syms) | |
8b127cbc | 11367 | (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym))) |
c152c796 AM |
11368 | return FALSE; |
11369 | } | |
11370 | ||
11371 | /* Flush all symbols to the file. */ | |
8b127cbc | 11372 | if (! elf_link_flush_output_syms (&flinfo, bed)) |
c152c796 AM |
11373 | return FALSE; |
11374 | ||
11375 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
11376 | if (bfd_get_symcount (abfd) > 0) |
11377 | { | |
ee3b52e9 L |
11378 | /* Finish up and write out the symbol string table (.strtab) |
11379 | section. */ | |
11380 | Elf_Internal_Shdr *symstrtab_hdr; | |
8539e4e8 AM |
11381 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
11382 | ||
11383 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
11384 | if (symtab_shndx_hdr->sh_name != 0) | |
11385 | { | |
11386 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
11387 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
11388 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
11389 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
11390 | symtab_shndx_hdr->sh_size = amt; | |
11391 | ||
11392 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
11393 | off, TRUE); | |
11394 | ||
11395 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
11396 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
11397 | return FALSE; | |
11398 | } | |
ee3b52e9 L |
11399 | |
11400 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
11401 | /* sh_name was set in prep_headers. */ | |
11402 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
11403 | symstrtab_hdr->sh_flags = 0; | |
11404 | symstrtab_hdr->sh_addr = 0; | |
11405 | symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab); | |
11406 | symstrtab_hdr->sh_entsize = 0; | |
11407 | symstrtab_hdr->sh_link = 0; | |
11408 | symstrtab_hdr->sh_info = 0; | |
11409 | /* sh_offset is set just below. */ | |
11410 | symstrtab_hdr->sh_addralign = 1; | |
11411 | ||
11412 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
11413 | off, TRUE); | |
11414 | elf_next_file_pos (abfd) = off; | |
11415 | ||
c152c796 | 11416 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
8b127cbc | 11417 | || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
11418 | return FALSE; |
11419 | } | |
11420 | ||
11421 | /* Adjust the relocs to have the correct symbol indices. */ | |
11422 | for (o = abfd->sections; o != NULL; o = o->next) | |
11423 | { | |
d4730f92 | 11424 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 11425 | bfd_boolean sort; |
c152c796 AM |
11426 | if ((o->flags & SEC_RELOC) == 0) |
11427 | continue; | |
11428 | ||
28dbcedc | 11429 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
d4730f92 | 11430 | if (esdo->rel.hdr != NULL) |
28dbcedc | 11431 | elf_link_adjust_relocs (abfd, &esdo->rel, sort); |
d4730f92 | 11432 | if (esdo->rela.hdr != NULL) |
28dbcedc | 11433 | elf_link_adjust_relocs (abfd, &esdo->rela, sort); |
c152c796 AM |
11434 | |
11435 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
11436 | trying to swap the relocs out itself. */ | |
11437 | o->reloc_count = 0; | |
11438 | } | |
11439 | ||
11440 | if (dynamic && info->combreloc && dynobj != NULL) | |
11441 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
11442 | ||
11443 | /* If we are linking against a dynamic object, or generating a | |
11444 | shared library, finish up the dynamic linking information. */ | |
11445 | if (dynamic) | |
11446 | { | |
11447 | bfd_byte *dyncon, *dynconend; | |
11448 | ||
11449 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 11450 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
11451 | BFD_ASSERT (o != NULL); |
11452 | ||
11453 | dyncon = o->contents; | |
eea6121a | 11454 | dynconend = o->contents + o->size; |
c152c796 AM |
11455 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
11456 | { | |
11457 | Elf_Internal_Dyn dyn; | |
11458 | const char *name; | |
11459 | unsigned int type; | |
11460 | ||
11461 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
11462 | ||
11463 | switch (dyn.d_tag) | |
11464 | { | |
11465 | default: | |
11466 | continue; | |
11467 | case DT_NULL: | |
11468 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
11469 | { | |
11470 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
11471 | { | |
11472 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
11473 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
11474 | default: continue; | |
11475 | } | |
11476 | dyn.d_un.d_val = relativecount; | |
11477 | relativecount = 0; | |
11478 | break; | |
11479 | } | |
11480 | continue; | |
11481 | ||
11482 | case DT_INIT: | |
11483 | name = info->init_function; | |
11484 | goto get_sym; | |
11485 | case DT_FINI: | |
11486 | name = info->fini_function; | |
11487 | get_sym: | |
11488 | { | |
11489 | struct elf_link_hash_entry *h; | |
11490 | ||
11491 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
11492 | FALSE, FALSE, TRUE); | |
11493 | if (h != NULL | |
11494 | && (h->root.type == bfd_link_hash_defined | |
11495 | || h->root.type == bfd_link_hash_defweak)) | |
11496 | { | |
bef26483 | 11497 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
11498 | o = h->root.u.def.section; |
11499 | if (o->output_section != NULL) | |
bef26483 | 11500 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
11501 | + o->output_offset); |
11502 | else | |
11503 | { | |
11504 | /* The symbol is imported from another shared | |
11505 | library and does not apply to this one. */ | |
bef26483 | 11506 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
11507 | } |
11508 | break; | |
11509 | } | |
11510 | } | |
11511 | continue; | |
11512 | ||
11513 | case DT_PREINIT_ARRAYSZ: | |
11514 | name = ".preinit_array"; | |
11515 | goto get_size; | |
11516 | case DT_INIT_ARRAYSZ: | |
11517 | name = ".init_array"; | |
11518 | goto get_size; | |
11519 | case DT_FINI_ARRAYSZ: | |
11520 | name = ".fini_array"; | |
11521 | get_size: | |
11522 | o = bfd_get_section_by_name (abfd, name); | |
11523 | if (o == NULL) | |
11524 | { | |
11525 | (*_bfd_error_handler) | |
d003868e | 11526 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
11527 | goto error_return; |
11528 | } | |
eea6121a | 11529 | if (o->size == 0) |
c152c796 AM |
11530 | (*_bfd_error_handler) |
11531 | (_("warning: %s section has zero size"), name); | |
eea6121a | 11532 | dyn.d_un.d_val = o->size; |
c152c796 AM |
11533 | break; |
11534 | ||
11535 | case DT_PREINIT_ARRAY: | |
11536 | name = ".preinit_array"; | |
11537 | goto get_vma; | |
11538 | case DT_INIT_ARRAY: | |
11539 | name = ".init_array"; | |
11540 | goto get_vma; | |
11541 | case DT_FINI_ARRAY: | |
11542 | name = ".fini_array"; | |
11543 | goto get_vma; | |
11544 | ||
11545 | case DT_HASH: | |
11546 | name = ".hash"; | |
11547 | goto get_vma; | |
fdc90cb4 JJ |
11548 | case DT_GNU_HASH: |
11549 | name = ".gnu.hash"; | |
11550 | goto get_vma; | |
c152c796 AM |
11551 | case DT_STRTAB: |
11552 | name = ".dynstr"; | |
11553 | goto get_vma; | |
11554 | case DT_SYMTAB: | |
11555 | name = ".dynsym"; | |
11556 | goto get_vma; | |
11557 | case DT_VERDEF: | |
11558 | name = ".gnu.version_d"; | |
11559 | goto get_vma; | |
11560 | case DT_VERNEED: | |
11561 | name = ".gnu.version_r"; | |
11562 | goto get_vma; | |
11563 | case DT_VERSYM: | |
11564 | name = ".gnu.version"; | |
11565 | get_vma: | |
11566 | o = bfd_get_section_by_name (abfd, name); | |
11567 | if (o == NULL) | |
11568 | { | |
11569 | (*_bfd_error_handler) | |
d003868e | 11570 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
11571 | goto error_return; |
11572 | } | |
894891db NC |
11573 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
11574 | { | |
11575 | (*_bfd_error_handler) | |
11576 | (_("warning: section '%s' is being made into a note"), name); | |
11577 | bfd_set_error (bfd_error_nonrepresentable_section); | |
11578 | goto error_return; | |
11579 | } | |
c152c796 AM |
11580 | dyn.d_un.d_ptr = o->vma; |
11581 | break; | |
11582 | ||
11583 | case DT_REL: | |
11584 | case DT_RELA: | |
11585 | case DT_RELSZ: | |
11586 | case DT_RELASZ: | |
11587 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
11588 | type = SHT_REL; | |
11589 | else | |
11590 | type = SHT_RELA; | |
11591 | dyn.d_un.d_val = 0; | |
bef26483 | 11592 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
11593 | for (i = 1; i < elf_numsections (abfd); i++) |
11594 | { | |
11595 | Elf_Internal_Shdr *hdr; | |
11596 | ||
11597 | hdr = elf_elfsections (abfd)[i]; | |
11598 | if (hdr->sh_type == type | |
11599 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
11600 | { | |
11601 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
11602 | dyn.d_un.d_val += hdr->sh_size; | |
11603 | else | |
11604 | { | |
bef26483 AM |
11605 | if (dyn.d_un.d_ptr == 0 |
11606 | || hdr->sh_addr < dyn.d_un.d_ptr) | |
11607 | dyn.d_un.d_ptr = hdr->sh_addr; | |
c152c796 AM |
11608 | } |
11609 | } | |
11610 | } | |
11611 | break; | |
11612 | } | |
11613 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
11614 | } | |
11615 | } | |
11616 | ||
11617 | /* If we have created any dynamic sections, then output them. */ | |
11618 | if (dynobj != NULL) | |
11619 | { | |
11620 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
11621 | goto error_return; | |
11622 | ||
943284cc | 11623 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
be7b303d AM |
11624 | if (((info->warn_shared_textrel && info->shared) |
11625 | || info->error_textrel) | |
3d4d4302 | 11626 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
11627 | { |
11628 | bfd_byte *dyncon, *dynconend; | |
11629 | ||
943284cc DJ |
11630 | dyncon = o->contents; |
11631 | dynconend = o->contents + o->size; | |
11632 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
11633 | { | |
11634 | Elf_Internal_Dyn dyn; | |
11635 | ||
11636 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
11637 | ||
11638 | if (dyn.d_tag == DT_TEXTREL) | |
11639 | { | |
c192a133 AM |
11640 | if (info->error_textrel) |
11641 | info->callbacks->einfo | |
11642 | (_("%P%X: read-only segment has dynamic relocations.\n")); | |
11643 | else | |
11644 | info->callbacks->einfo | |
11645 | (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); | |
943284cc DJ |
11646 | break; |
11647 | } | |
11648 | } | |
11649 | } | |
11650 | ||
c152c796 AM |
11651 | for (o = dynobj->sections; o != NULL; o = o->next) |
11652 | { | |
11653 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 11654 | || o->size == 0 |
c152c796 AM |
11655 | || o->output_section == bfd_abs_section_ptr) |
11656 | continue; | |
11657 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
11658 | { | |
11659 | /* At this point, we are only interested in sections | |
11660 | created by _bfd_elf_link_create_dynamic_sections. */ | |
11661 | continue; | |
11662 | } | |
3722b82f AM |
11663 | if (elf_hash_table (info)->stab_info.stabstr == o) |
11664 | continue; | |
eea6121a AM |
11665 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
11666 | continue; | |
3d4d4302 | 11667 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 | 11668 | { |
5dabe785 | 11669 | /* FIXME: octets_per_byte. */ |
c152c796 AM |
11670 | if (! bfd_set_section_contents (abfd, o->output_section, |
11671 | o->contents, | |
11672 | (file_ptr) o->output_offset, | |
eea6121a | 11673 | o->size)) |
c152c796 AM |
11674 | goto error_return; |
11675 | } | |
11676 | else | |
11677 | { | |
11678 | /* The contents of the .dynstr section are actually in a | |
11679 | stringtab. */ | |
8539e4e8 AM |
11680 | file_ptr off; |
11681 | ||
c152c796 AM |
11682 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
11683 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
11684 | || ! _bfd_elf_strtab_emit (abfd, | |
11685 | elf_hash_table (info)->dynstr)) | |
11686 | goto error_return; | |
11687 | } | |
11688 | } | |
11689 | } | |
11690 | ||
11691 | if (info->relocatable) | |
11692 | { | |
11693 | bfd_boolean failed = FALSE; | |
11694 | ||
11695 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
11696 | if (failed) | |
11697 | goto error_return; | |
11698 | } | |
11699 | ||
11700 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 11701 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
11702 | { |
11703 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
11704 | goto error_return; | |
11705 | } | |
11706 | ||
9f7c3e5e AM |
11707 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
11708 | goto error_return; | |
c152c796 | 11709 | |
9f7c3e5e | 11710 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 11711 | |
12bd6957 | 11712 | elf_linker (abfd) = TRUE; |
c152c796 | 11713 | |
104d59d1 JM |
11714 | if (attr_section) |
11715 | { | |
a50b1753 | 11716 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 11717 | if (contents == NULL) |
d0f16d5e | 11718 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
11719 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
11720 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
11721 | free (contents); | |
11722 | } | |
11723 | ||
c152c796 AM |
11724 | return TRUE; |
11725 | ||
11726 | error_return: | |
9f7c3e5e | 11727 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
11728 | return FALSE; |
11729 | } | |
11730 | \f | |
5241d853 RS |
11731 | /* Initialize COOKIE for input bfd ABFD. */ |
11732 | ||
11733 | static bfd_boolean | |
11734 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
11735 | struct bfd_link_info *info, bfd *abfd) | |
11736 | { | |
11737 | Elf_Internal_Shdr *symtab_hdr; | |
11738 | const struct elf_backend_data *bed; | |
11739 | ||
11740 | bed = get_elf_backend_data (abfd); | |
11741 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11742 | ||
11743 | cookie->abfd = abfd; | |
11744 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
11745 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
11746 | if (cookie->bad_symtab) | |
11747 | { | |
11748 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11749 | cookie->extsymoff = 0; | |
11750 | } | |
11751 | else | |
11752 | { | |
11753 | cookie->locsymcount = symtab_hdr->sh_info; | |
11754 | cookie->extsymoff = symtab_hdr->sh_info; | |
11755 | } | |
11756 | ||
11757 | if (bed->s->arch_size == 32) | |
11758 | cookie->r_sym_shift = 8; | |
11759 | else | |
11760 | cookie->r_sym_shift = 32; | |
11761 | ||
11762 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
11763 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
11764 | { | |
11765 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
11766 | cookie->locsymcount, 0, | |
11767 | NULL, NULL, NULL); | |
11768 | if (cookie->locsyms == NULL) | |
11769 | { | |
11770 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
11771 | return FALSE; | |
11772 | } | |
11773 | if (info->keep_memory) | |
11774 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
11775 | } | |
11776 | return TRUE; | |
11777 | } | |
11778 | ||
11779 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
11780 | ||
11781 | static void | |
11782 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
11783 | { | |
11784 | Elf_Internal_Shdr *symtab_hdr; | |
11785 | ||
11786 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11787 | if (cookie->locsyms != NULL | |
11788 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
11789 | free (cookie->locsyms); | |
11790 | } | |
11791 | ||
11792 | /* Initialize the relocation information in COOKIE for input section SEC | |
11793 | of input bfd ABFD. */ | |
11794 | ||
11795 | static bfd_boolean | |
11796 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
11797 | struct bfd_link_info *info, bfd *abfd, | |
11798 | asection *sec) | |
11799 | { | |
11800 | const struct elf_backend_data *bed; | |
11801 | ||
11802 | if (sec->reloc_count == 0) | |
11803 | { | |
11804 | cookie->rels = NULL; | |
11805 | cookie->relend = NULL; | |
11806 | } | |
11807 | else | |
11808 | { | |
11809 | bed = get_elf_backend_data (abfd); | |
11810 | ||
11811 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
11812 | info->keep_memory); | |
11813 | if (cookie->rels == NULL) | |
11814 | return FALSE; | |
11815 | cookie->rel = cookie->rels; | |
11816 | cookie->relend = (cookie->rels | |
11817 | + sec->reloc_count * bed->s->int_rels_per_ext_rel); | |
11818 | } | |
11819 | cookie->rel = cookie->rels; | |
11820 | return TRUE; | |
11821 | } | |
11822 | ||
11823 | /* Free the memory allocated by init_reloc_cookie_rels, | |
11824 | if appropriate. */ | |
11825 | ||
11826 | static void | |
11827 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
11828 | asection *sec) | |
11829 | { | |
11830 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
11831 | free (cookie->rels); | |
11832 | } | |
11833 | ||
11834 | /* Initialize the whole of COOKIE for input section SEC. */ | |
11835 | ||
11836 | static bfd_boolean | |
11837 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
11838 | struct bfd_link_info *info, | |
11839 | asection *sec) | |
11840 | { | |
11841 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
11842 | goto error1; | |
11843 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
11844 | goto error2; | |
11845 | return TRUE; | |
11846 | ||
11847 | error2: | |
11848 | fini_reloc_cookie (cookie, sec->owner); | |
11849 | error1: | |
11850 | return FALSE; | |
11851 | } | |
11852 | ||
11853 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
11854 | if appropriate. */ | |
11855 | ||
11856 | static void | |
11857 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
11858 | asection *sec) | |
11859 | { | |
11860 | fini_reloc_cookie_rels (cookie, sec); | |
11861 | fini_reloc_cookie (cookie, sec->owner); | |
11862 | } | |
11863 | \f | |
c152c796 AM |
11864 | /* Garbage collect unused sections. */ |
11865 | ||
07adf181 AM |
11866 | /* Default gc_mark_hook. */ |
11867 | ||
11868 | asection * | |
11869 | _bfd_elf_gc_mark_hook (asection *sec, | |
11870 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
11871 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
11872 | struct elf_link_hash_entry *h, | |
11873 | Elf_Internal_Sym *sym) | |
11874 | { | |
bde6f3eb L |
11875 | const char *sec_name; |
11876 | ||
07adf181 AM |
11877 | if (h != NULL) |
11878 | { | |
11879 | switch (h->root.type) | |
11880 | { | |
11881 | case bfd_link_hash_defined: | |
11882 | case bfd_link_hash_defweak: | |
11883 | return h->root.u.def.section; | |
11884 | ||
11885 | case bfd_link_hash_common: | |
11886 | return h->root.u.c.p->section; | |
11887 | ||
bde6f3eb L |
11888 | case bfd_link_hash_undefined: |
11889 | case bfd_link_hash_undefweak: | |
11890 | /* To work around a glibc bug, keep all XXX input sections | |
11891 | when there is an as yet undefined reference to __start_XXX | |
11892 | or __stop_XXX symbols. The linker will later define such | |
11893 | symbols for orphan input sections that have a name | |
11894 | representable as a C identifier. */ | |
11895 | if (strncmp (h->root.root.string, "__start_", 8) == 0) | |
11896 | sec_name = h->root.root.string + 8; | |
11897 | else if (strncmp (h->root.root.string, "__stop_", 7) == 0) | |
11898 | sec_name = h->root.root.string + 7; | |
11899 | else | |
11900 | sec_name = NULL; | |
11901 | ||
11902 | if (sec_name && *sec_name != '\0') | |
11903 | { | |
11904 | bfd *i; | |
68ffbac6 | 11905 | |
c72f2fb2 | 11906 | for (i = info->input_bfds; i; i = i->link.next) |
bde6f3eb L |
11907 | { |
11908 | sec = bfd_get_section_by_name (i, sec_name); | |
11909 | if (sec) | |
11910 | sec->flags |= SEC_KEEP; | |
11911 | } | |
11912 | } | |
11913 | break; | |
11914 | ||
07adf181 AM |
11915 | default: |
11916 | break; | |
11917 | } | |
11918 | } | |
11919 | else | |
11920 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
11921 | ||
11922 | return NULL; | |
11923 | } | |
11924 | ||
5241d853 RS |
11925 | /* COOKIE->rel describes a relocation against section SEC, which is |
11926 | a section we've decided to keep. Return the section that contains | |
11927 | the relocation symbol, or NULL if no section contains it. */ | |
11928 | ||
11929 | asection * | |
11930 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
11931 | elf_gc_mark_hook_fn gc_mark_hook, | |
11932 | struct elf_reloc_cookie *cookie) | |
11933 | { | |
11934 | unsigned long r_symndx; | |
11935 | struct elf_link_hash_entry *h; | |
11936 | ||
11937 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 11938 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
11939 | return NULL; |
11940 | ||
11941 | if (r_symndx >= cookie->locsymcount | |
11942 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
11943 | { | |
11944 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
11945 | if (h == NULL) |
11946 | { | |
11947 | info->callbacks->einfo (_("%F%P: corrupt input: %B\n"), | |
11948 | sec->owner); | |
11949 | return NULL; | |
11950 | } | |
5241d853 RS |
11951 | while (h->root.type == bfd_link_hash_indirect |
11952 | || h->root.type == bfd_link_hash_warning) | |
11953 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 11954 | h->mark = 1; |
4e6b54a6 AM |
11955 | /* If this symbol is weak and there is a non-weak definition, we |
11956 | keep the non-weak definition because many backends put | |
11957 | dynamic reloc info on the non-weak definition for code | |
11958 | handling copy relocs. */ | |
11959 | if (h->u.weakdef != NULL) | |
11960 | h->u.weakdef->mark = 1; | |
5241d853 RS |
11961 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
11962 | } | |
11963 | ||
11964 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
11965 | &cookie->locsyms[r_symndx]); | |
11966 | } | |
11967 | ||
11968 | /* COOKIE->rel describes a relocation against section SEC, which is | |
11969 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 11970 | the relocation symbol. */ |
5241d853 RS |
11971 | |
11972 | bfd_boolean | |
11973 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
11974 | asection *sec, | |
11975 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 11976 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
11977 | { |
11978 | asection *rsec; | |
11979 | ||
11980 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie); | |
11981 | if (rsec && !rsec->gc_mark) | |
11982 | { | |
a66eed7a AM |
11983 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour |
11984 | || (rsec->owner->flags & DYNAMIC) != 0) | |
5241d853 | 11985 | rsec->gc_mark = 1; |
5241d853 RS |
11986 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
11987 | return FALSE; | |
11988 | } | |
11989 | return TRUE; | |
11990 | } | |
11991 | ||
07adf181 AM |
11992 | /* The mark phase of garbage collection. For a given section, mark |
11993 | it and any sections in this section's group, and all the sections | |
11994 | which define symbols to which it refers. */ | |
11995 | ||
ccfa59ea AM |
11996 | bfd_boolean |
11997 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
11998 | asection *sec, | |
6a5bb875 | 11999 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
12000 | { |
12001 | bfd_boolean ret; | |
9d0a14d3 | 12002 | asection *group_sec, *eh_frame; |
c152c796 AM |
12003 | |
12004 | sec->gc_mark = 1; | |
12005 | ||
12006 | /* Mark all the sections in the group. */ | |
12007 | group_sec = elf_section_data (sec)->next_in_group; | |
12008 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 12009 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
12010 | return FALSE; |
12011 | ||
12012 | /* Look through the section relocs. */ | |
12013 | ret = TRUE; | |
9d0a14d3 RS |
12014 | eh_frame = elf_eh_frame_section (sec->owner); |
12015 | if ((sec->flags & SEC_RELOC) != 0 | |
12016 | && sec->reloc_count > 0 | |
12017 | && sec != eh_frame) | |
c152c796 | 12018 | { |
5241d853 | 12019 | struct elf_reloc_cookie cookie; |
c152c796 | 12020 | |
5241d853 RS |
12021 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
12022 | ret = FALSE; | |
c152c796 | 12023 | else |
c152c796 | 12024 | { |
5241d853 | 12025 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 12026 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
12027 | { |
12028 | ret = FALSE; | |
12029 | break; | |
12030 | } | |
12031 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
12032 | } |
12033 | } | |
9d0a14d3 RS |
12034 | |
12035 | if (ret && eh_frame && elf_fde_list (sec)) | |
12036 | { | |
12037 | struct elf_reloc_cookie cookie; | |
12038 | ||
12039 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
12040 | ret = FALSE; | |
12041 | else | |
12042 | { | |
12043 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
12044 | gc_mark_hook, &cookie)) | |
12045 | ret = FALSE; | |
12046 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
12047 | } | |
12048 | } | |
12049 | ||
2f0c68f2 CM |
12050 | eh_frame = elf_section_eh_frame_entry (sec); |
12051 | if (ret && eh_frame && !eh_frame->gc_mark) | |
12052 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
12053 | ret = FALSE; | |
12054 | ||
c152c796 AM |
12055 | return ret; |
12056 | } | |
12057 | ||
3c758495 TG |
12058 | /* Scan and mark sections in a special or debug section group. */ |
12059 | ||
12060 | static void | |
12061 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
12062 | { | |
12063 | /* Point to first section of section group. */ | |
12064 | asection *ssec; | |
12065 | /* Used to iterate the section group. */ | |
12066 | asection *msec; | |
12067 | ||
12068 | bfd_boolean is_special_grp = TRUE; | |
12069 | bfd_boolean is_debug_grp = TRUE; | |
12070 | ||
12071 | /* First scan to see if group contains any section other than debug | |
12072 | and special section. */ | |
12073 | ssec = msec = elf_next_in_group (grp); | |
12074 | do | |
12075 | { | |
12076 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
12077 | is_debug_grp = FALSE; | |
12078 | ||
12079 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
12080 | is_special_grp = FALSE; | |
12081 | ||
12082 | msec = elf_next_in_group (msec); | |
12083 | } | |
12084 | while (msec != ssec); | |
12085 | ||
12086 | /* If this is a pure debug section group or pure special section group, | |
12087 | keep all sections in this group. */ | |
12088 | if (is_debug_grp || is_special_grp) | |
12089 | { | |
12090 | do | |
12091 | { | |
12092 | msec->gc_mark = 1; | |
12093 | msec = elf_next_in_group (msec); | |
12094 | } | |
12095 | while (msec != ssec); | |
12096 | } | |
12097 | } | |
12098 | ||
7f6ab9f8 AM |
12099 | /* Keep debug and special sections. */ |
12100 | ||
12101 | bfd_boolean | |
12102 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
12103 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
12104 | { | |
12105 | bfd *ibfd; | |
12106 | ||
c72f2fb2 | 12107 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
12108 | { |
12109 | asection *isec; | |
12110 | bfd_boolean some_kept; | |
b40bf0a2 | 12111 | bfd_boolean debug_frag_seen; |
7f6ab9f8 AM |
12112 | |
12113 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
12114 | continue; | |
12115 | ||
b40bf0a2 NC |
12116 | /* Ensure all linker created sections are kept, |
12117 | see if any other section is already marked, | |
12118 | and note if we have any fragmented debug sections. */ | |
12119 | debug_frag_seen = some_kept = FALSE; | |
7f6ab9f8 AM |
12120 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
12121 | { | |
12122 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
12123 | isec->gc_mark = 1; | |
12124 | else if (isec->gc_mark) | |
12125 | some_kept = TRUE; | |
b40bf0a2 NC |
12126 | |
12127 | if (debug_frag_seen == FALSE | |
12128 | && (isec->flags & SEC_DEBUGGING) | |
12129 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
12130 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
12131 | } |
12132 | ||
12133 | /* If no section in this file will be kept, then we can | |
b40bf0a2 | 12134 | toss out the debug and special sections. */ |
7f6ab9f8 AM |
12135 | if (!some_kept) |
12136 | continue; | |
12137 | ||
12138 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
12139 | not part of a group. Also keep section groups that contain |
12140 | just debug sections or special sections. */ | |
7f6ab9f8 | 12141 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
12142 | { |
12143 | if ((isec->flags & SEC_GROUP) != 0) | |
12144 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
12145 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
12146 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
12147 | && elf_next_in_group (isec) == NULL) | |
12148 | isec->gc_mark = 1; | |
12149 | } | |
b40bf0a2 NC |
12150 | |
12151 | if (! debug_frag_seen) | |
12152 | continue; | |
12153 | ||
12154 | /* Look for CODE sections which are going to be discarded, | |
12155 | and find and discard any fragmented debug sections which | |
12156 | are associated with that code section. */ | |
12157 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
12158 | if ((isec->flags & SEC_CODE) != 0 | |
12159 | && isec->gc_mark == 0) | |
12160 | { | |
12161 | unsigned int ilen; | |
12162 | asection *dsec; | |
12163 | ||
12164 | ilen = strlen (isec->name); | |
12165 | ||
12166 | /* Association is determined by the name of the debug section | |
12167 | containing the name of the code section as a suffix. For | |
12168 | example .debug_line.text.foo is a debug section associated | |
12169 | with .text.foo. */ | |
12170 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
12171 | { | |
12172 | unsigned int dlen; | |
12173 | ||
12174 | if (dsec->gc_mark == 0 | |
12175 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
12176 | continue; | |
12177 | ||
12178 | dlen = strlen (dsec->name); | |
12179 | ||
12180 | if (dlen > ilen | |
12181 | && strncmp (dsec->name + (dlen - ilen), | |
12182 | isec->name, ilen) == 0) | |
12183 | { | |
12184 | dsec->gc_mark = 0; | |
b40bf0a2 NC |
12185 | } |
12186 | } | |
12187 | } | |
7f6ab9f8 AM |
12188 | } |
12189 | return TRUE; | |
12190 | } | |
12191 | ||
c152c796 AM |
12192 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
12193 | ||
c17d87de NC |
12194 | struct elf_gc_sweep_symbol_info |
12195 | { | |
ccabcbe5 AM |
12196 | struct bfd_link_info *info; |
12197 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
12198 | bfd_boolean); | |
12199 | }; | |
12200 | ||
c152c796 | 12201 | static bfd_boolean |
ccabcbe5 | 12202 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
c152c796 | 12203 | { |
1d5316ab AM |
12204 | if (!h->mark |
12205 | && (((h->root.type == bfd_link_hash_defined | |
12206 | || h->root.type == bfd_link_hash_defweak) | |
c4621b33 | 12207 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) |
6673f753 | 12208 | && h->root.u.def.section->gc_mark)) |
1d5316ab AM |
12209 | || h->root.type == bfd_link_hash_undefined |
12210 | || h->root.type == bfd_link_hash_undefweak)) | |
12211 | { | |
12212 | struct elf_gc_sweep_symbol_info *inf; | |
12213 | ||
12214 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
ccabcbe5 | 12215 | (*inf->hide_symbol) (inf->info, h, TRUE); |
1d5316ab AM |
12216 | h->def_regular = 0; |
12217 | h->ref_regular = 0; | |
12218 | h->ref_regular_nonweak = 0; | |
ccabcbe5 | 12219 | } |
c152c796 AM |
12220 | |
12221 | return TRUE; | |
12222 | } | |
12223 | ||
12224 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
12225 | ||
12226 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
12227 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
12228 | ||
12229 | static bfd_boolean | |
ccabcbe5 | 12230 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
12231 | { |
12232 | bfd *sub; | |
ccabcbe5 AM |
12233 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
12234 | gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; | |
12235 | unsigned long section_sym_count; | |
12236 | struct elf_gc_sweep_symbol_info sweep_info; | |
c152c796 | 12237 | |
c72f2fb2 | 12238 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12239 | { |
12240 | asection *o; | |
12241 | ||
b19a8f85 L |
12242 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
12243 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) | |
c152c796 AM |
12244 | continue; |
12245 | ||
12246 | for (o = sub->sections; o != NULL; o = o->next) | |
12247 | { | |
a33dafc3 L |
12248 | /* When any section in a section group is kept, we keep all |
12249 | sections in the section group. If the first member of | |
12250 | the section group is excluded, we will also exclude the | |
12251 | group section. */ | |
12252 | if (o->flags & SEC_GROUP) | |
12253 | { | |
12254 | asection *first = elf_next_in_group (o); | |
12255 | o->gc_mark = first->gc_mark; | |
12256 | } | |
c152c796 AM |
12257 | |
12258 | if (o->gc_mark) | |
12259 | continue; | |
12260 | ||
12261 | /* Skip sweeping sections already excluded. */ | |
12262 | if (o->flags & SEC_EXCLUDE) | |
12263 | continue; | |
12264 | ||
12265 | /* Since this is early in the link process, it is simple | |
12266 | to remove a section from the output. */ | |
12267 | o->flags |= SEC_EXCLUDE; | |
12268 | ||
c55fe096 | 12269 | if (info->print_gc_sections && o->size != 0) |
c17d87de NC |
12270 | _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); |
12271 | ||
c152c796 AM |
12272 | /* But we also have to update some of the relocation |
12273 | info we collected before. */ | |
12274 | if (gc_sweep_hook | |
e8aaee2a | 12275 | && (o->flags & SEC_RELOC) != 0 |
9850436d AM |
12276 | && o->reloc_count != 0 |
12277 | && !((info->strip == strip_all || info->strip == strip_debugger) | |
12278 | && (o->flags & SEC_DEBUGGING) != 0) | |
e8aaee2a | 12279 | && !bfd_is_abs_section (o->output_section)) |
c152c796 AM |
12280 | { |
12281 | Elf_Internal_Rela *internal_relocs; | |
12282 | bfd_boolean r; | |
12283 | ||
12284 | internal_relocs | |
12285 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
12286 | info->keep_memory); | |
12287 | if (internal_relocs == NULL) | |
12288 | return FALSE; | |
12289 | ||
12290 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
12291 | ||
12292 | if (elf_section_data (o)->relocs != internal_relocs) | |
12293 | free (internal_relocs); | |
12294 | ||
12295 | if (!r) | |
12296 | return FALSE; | |
12297 | } | |
12298 | } | |
12299 | } | |
12300 | ||
12301 | /* Remove the symbols that were in the swept sections from the dynamic | |
12302 | symbol table. GCFIXME: Anyone know how to get them out of the | |
12303 | static symbol table as well? */ | |
ccabcbe5 AM |
12304 | sweep_info.info = info; |
12305 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
12306 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
12307 | &sweep_info); | |
c152c796 | 12308 | |
ccabcbe5 | 12309 | _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); |
c152c796 AM |
12310 | return TRUE; |
12311 | } | |
12312 | ||
12313 | /* Propagate collected vtable information. This is called through | |
12314 | elf_link_hash_traverse. */ | |
12315 | ||
12316 | static bfd_boolean | |
12317 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
12318 | { | |
c152c796 | 12319 | /* Those that are not vtables. */ |
f6e332e6 | 12320 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
12321 | return TRUE; |
12322 | ||
12323 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 12324 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
12325 | return TRUE; |
12326 | ||
12327 | /* If we've already been done, exit. */ | |
f6e332e6 | 12328 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
12329 | return TRUE; |
12330 | ||
12331 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 12332 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 12333 | |
f6e332e6 | 12334 | if (h->vtable->used == NULL) |
c152c796 AM |
12335 | { |
12336 | /* None of this table's entries were referenced. Re-use the | |
12337 | parent's table. */ | |
f6e332e6 AM |
12338 | h->vtable->used = h->vtable->parent->vtable->used; |
12339 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
12340 | } |
12341 | else | |
12342 | { | |
12343 | size_t n; | |
12344 | bfd_boolean *cu, *pu; | |
12345 | ||
12346 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 12347 | cu = h->vtable->used; |
c152c796 | 12348 | cu[-1] = TRUE; |
f6e332e6 | 12349 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
12350 | if (pu != NULL) |
12351 | { | |
12352 | const struct elf_backend_data *bed; | |
12353 | unsigned int log_file_align; | |
12354 | ||
12355 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
12356 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 12357 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
12358 | while (n--) |
12359 | { | |
12360 | if (*pu) | |
12361 | *cu = TRUE; | |
12362 | pu++; | |
12363 | cu++; | |
12364 | } | |
12365 | } | |
12366 | } | |
12367 | ||
12368 | return TRUE; | |
12369 | } | |
12370 | ||
12371 | static bfd_boolean | |
12372 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
12373 | { | |
12374 | asection *sec; | |
12375 | bfd_vma hstart, hend; | |
12376 | Elf_Internal_Rela *relstart, *relend, *rel; | |
12377 | const struct elf_backend_data *bed; | |
12378 | unsigned int log_file_align; | |
12379 | ||
c152c796 AM |
12380 | /* Take care of both those symbols that do not describe vtables as |
12381 | well as those that are not loaded. */ | |
f6e332e6 | 12382 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
12383 | return TRUE; |
12384 | ||
12385 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
12386 | || h->root.type == bfd_link_hash_defweak); | |
12387 | ||
12388 | sec = h->root.u.def.section; | |
12389 | hstart = h->root.u.def.value; | |
12390 | hend = hstart + h->size; | |
12391 | ||
12392 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
12393 | if (!relstart) | |
12394 | return *(bfd_boolean *) okp = FALSE; | |
12395 | bed = get_elf_backend_data (sec->owner); | |
12396 | log_file_align = bed->s->log_file_align; | |
12397 | ||
12398 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
12399 | ||
12400 | for (rel = relstart; rel < relend; ++rel) | |
12401 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
12402 | { | |
12403 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
12404 | if (h->vtable->used |
12405 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
12406 | { |
12407 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 12408 | if (h->vtable->used[entry]) |
c152c796 AM |
12409 | continue; |
12410 | } | |
12411 | /* Otherwise, kill it. */ | |
12412 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
12413 | } | |
12414 | ||
12415 | return TRUE; | |
12416 | } | |
12417 | ||
87538722 AM |
12418 | /* Mark sections containing dynamically referenced symbols. When |
12419 | building shared libraries, we must assume that any visible symbol is | |
12420 | referenced. */ | |
715df9b8 | 12421 | |
64d03ab5 AM |
12422 | bfd_boolean |
12423 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 12424 | { |
87538722 | 12425 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 12426 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 12427 | |
715df9b8 EB |
12428 | if ((h->root.type == bfd_link_hash_defined |
12429 | || h->root.type == bfd_link_hash_defweak) | |
87538722 | 12430 | && (h->ref_dynamic |
c4621b33 | 12431 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 12432 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 12433 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
b407645f AM |
12434 | && (!info->executable |
12435 | || info->export_dynamic | |
12436 | || (h->dynamic | |
12437 | && d != NULL | |
12438 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
54e8959c L |
12439 | && (strchr (h->root.root.string, ELF_VER_CHR) != NULL |
12440 | || !bfd_hide_sym_by_version (info->version_info, | |
12441 | h->root.root.string))))) | |
715df9b8 EB |
12442 | h->root.u.def.section->flags |= SEC_KEEP; |
12443 | ||
12444 | return TRUE; | |
12445 | } | |
3b36f7e6 | 12446 | |
74f0fb50 AM |
12447 | /* Keep all sections containing symbols undefined on the command-line, |
12448 | and the section containing the entry symbol. */ | |
12449 | ||
12450 | void | |
12451 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
12452 | { | |
12453 | struct bfd_sym_chain *sym; | |
12454 | ||
12455 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
12456 | { | |
12457 | struct elf_link_hash_entry *h; | |
12458 | ||
12459 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
12460 | FALSE, FALSE, FALSE); | |
12461 | ||
12462 | if (h != NULL | |
12463 | && (h->root.type == bfd_link_hash_defined | |
12464 | || h->root.type == bfd_link_hash_defweak) | |
12465 | && !bfd_is_abs_section (h->root.u.def.section)) | |
12466 | h->root.u.def.section->flags |= SEC_KEEP; | |
12467 | } | |
12468 | } | |
12469 | ||
2f0c68f2 CM |
12470 | bfd_boolean |
12471 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
12472 | struct bfd_link_info *info) | |
12473 | { | |
12474 | bfd *ibfd = info->input_bfds; | |
12475 | ||
12476 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
12477 | { | |
12478 | asection *sec; | |
12479 | struct elf_reloc_cookie cookie; | |
12480 | ||
12481 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
12482 | continue; | |
12483 | ||
12484 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
12485 | return FALSE; | |
12486 | ||
12487 | for (sec = ibfd->sections; sec; sec = sec->next) | |
12488 | { | |
12489 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
12490 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
12491 | { | |
12492 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
12493 | fini_reloc_cookie_rels (&cookie, sec); | |
12494 | } | |
12495 | } | |
12496 | } | |
12497 | return TRUE; | |
12498 | } | |
12499 | ||
c152c796 AM |
12500 | /* Do mark and sweep of unused sections. */ |
12501 | ||
12502 | bfd_boolean | |
12503 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
12504 | { | |
12505 | bfd_boolean ok = TRUE; | |
12506 | bfd *sub; | |
6a5bb875 | 12507 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 12508 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 12509 | struct elf_link_hash_table *htab; |
c152c796 | 12510 | |
64d03ab5 | 12511 | if (!bed->can_gc_sections |
715df9b8 | 12512 | || !is_elf_hash_table (info->hash)) |
c152c796 AM |
12513 | { |
12514 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
12515 | return TRUE; | |
12516 | } | |
12517 | ||
74f0fb50 | 12518 | bed->gc_keep (info); |
da44f4e5 | 12519 | htab = elf_hash_table (info); |
74f0fb50 | 12520 | |
9d0a14d3 RS |
12521 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
12522 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
12523 | for (sub = info->input_bfds; |
12524 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
12525 | sub = sub->link.next) | |
9d0a14d3 RS |
12526 | { |
12527 | asection *sec; | |
12528 | struct elf_reloc_cookie cookie; | |
12529 | ||
12530 | sec = bfd_get_section_by_name (sub, ".eh_frame"); | |
9a2a56cc | 12531 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
12532 | { |
12533 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
12534 | if (elf_section_data (sec)->sec_info |
12535 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
12536 | elf_eh_frame_section (sub) = sec; |
12537 | fini_reloc_cookie_for_section (&cookie, sec); | |
9a2a56cc | 12538 | sec = bfd_get_next_section_by_name (sec); |
9d0a14d3 RS |
12539 | } |
12540 | } | |
9d0a14d3 | 12541 | |
c152c796 | 12542 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 12543 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
12544 | if (!ok) |
12545 | return FALSE; | |
12546 | ||
12547 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 12548 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
12549 | if (!ok) |
12550 | return FALSE; | |
12551 | ||
715df9b8 | 12552 | /* Mark dynamically referenced symbols. */ |
da44f4e5 AM |
12553 | if (htab->dynamic_sections_created) |
12554 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); | |
c152c796 | 12555 | |
715df9b8 | 12556 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 12557 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 12558 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12559 | { |
12560 | asection *o; | |
12561 | ||
b19a8f85 L |
12562 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
12563 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) | |
c152c796 AM |
12564 | continue; |
12565 | ||
7f6ab9f8 AM |
12566 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
12567 | Also treat note sections as a root, if the section is not part | |
12568 | of a group. */ | |
c152c796 | 12569 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
12570 | if (!o->gc_mark |
12571 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 12572 | && ((o->flags & SEC_KEEP) != 0 |
7f6ab9f8 AM |
12573 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
12574 | && elf_next_in_group (o) == NULL ))) | |
12575 | { | |
12576 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
12577 | return FALSE; | |
12578 | } | |
c152c796 AM |
12579 | } |
12580 | ||
6a5bb875 | 12581 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 12582 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 12583 | |
c152c796 | 12584 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 12585 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
12586 | } |
12587 | \f | |
12588 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
12589 | ||
12590 | bfd_boolean | |
12591 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
12592 | asection *sec, | |
12593 | struct elf_link_hash_entry *h, | |
12594 | bfd_vma offset) | |
12595 | { | |
12596 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
12597 | struct elf_link_hash_entry **search, *child; | |
12598 | bfd_size_type extsymcount; | |
12599 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12600 | ||
12601 | /* The sh_info field of the symtab header tells us where the | |
12602 | external symbols start. We don't care about the local symbols at | |
12603 | this point. */ | |
12604 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
12605 | if (!elf_bad_symtab (abfd)) | |
12606 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
12607 | ||
12608 | sym_hashes = elf_sym_hashes (abfd); | |
12609 | sym_hashes_end = sym_hashes + extsymcount; | |
12610 | ||
12611 | /* Hunt down the child symbol, which is in this section at the same | |
12612 | offset as the relocation. */ | |
12613 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
12614 | { | |
12615 | if ((child = *search) != NULL | |
12616 | && (child->root.type == bfd_link_hash_defined | |
12617 | || child->root.type == bfd_link_hash_defweak) | |
12618 | && child->root.u.def.section == sec | |
12619 | && child->root.u.def.value == offset) | |
12620 | goto win; | |
12621 | } | |
12622 | ||
d003868e AM |
12623 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
12624 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
12625 | bfd_set_error (bfd_error_invalid_operation); |
12626 | return FALSE; | |
12627 | ||
12628 | win: | |
f6e332e6 AM |
12629 | if (!child->vtable) |
12630 | { | |
ca4be51c AM |
12631 | child->vtable = ((struct elf_link_virtual_table_entry *) |
12632 | bfd_zalloc (abfd, sizeof (*child->vtable))); | |
f6e332e6 AM |
12633 | if (!child->vtable) |
12634 | return FALSE; | |
12635 | } | |
c152c796 AM |
12636 | if (!h) |
12637 | { | |
12638 | /* This *should* only be the absolute section. It could potentially | |
12639 | be that someone has defined a non-global vtable though, which | |
12640 | would be bad. It isn't worth paging in the local symbols to be | |
12641 | sure though; that case should simply be handled by the assembler. */ | |
12642 | ||
f6e332e6 | 12643 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
12644 | } |
12645 | else | |
f6e332e6 | 12646 | child->vtable->parent = h; |
c152c796 AM |
12647 | |
12648 | return TRUE; | |
12649 | } | |
12650 | ||
12651 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
12652 | ||
12653 | bfd_boolean | |
12654 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
12655 | asection *sec ATTRIBUTE_UNUSED, | |
12656 | struct elf_link_hash_entry *h, | |
12657 | bfd_vma addend) | |
12658 | { | |
12659 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12660 | unsigned int log_file_align = bed->s->log_file_align; | |
12661 | ||
f6e332e6 AM |
12662 | if (!h->vtable) |
12663 | { | |
ca4be51c AM |
12664 | h->vtable = ((struct elf_link_virtual_table_entry *) |
12665 | bfd_zalloc (abfd, sizeof (*h->vtable))); | |
f6e332e6 AM |
12666 | if (!h->vtable) |
12667 | return FALSE; | |
12668 | } | |
12669 | ||
12670 | if (addend >= h->vtable->size) | |
c152c796 AM |
12671 | { |
12672 | size_t size, bytes, file_align; | |
f6e332e6 | 12673 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
12674 | |
12675 | /* While the symbol is undefined, we have to be prepared to handle | |
12676 | a zero size. */ | |
12677 | file_align = 1 << log_file_align; | |
12678 | if (h->root.type == bfd_link_hash_undefined) | |
12679 | size = addend + file_align; | |
12680 | else | |
12681 | { | |
12682 | size = h->size; | |
12683 | if (addend >= size) | |
12684 | { | |
12685 | /* Oops! We've got a reference past the defined end of | |
12686 | the table. This is probably a bug -- shall we warn? */ | |
12687 | size = addend + file_align; | |
12688 | } | |
12689 | } | |
12690 | size = (size + file_align - 1) & -file_align; | |
12691 | ||
12692 | /* Allocate one extra entry for use as a "done" flag for the | |
12693 | consolidation pass. */ | |
12694 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
12695 | ||
12696 | if (ptr) | |
12697 | { | |
a50b1753 | 12698 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
12699 | |
12700 | if (ptr != NULL) | |
12701 | { | |
12702 | size_t oldbytes; | |
12703 | ||
f6e332e6 | 12704 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
12705 | * sizeof (bfd_boolean)); |
12706 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
12707 | } | |
12708 | } | |
12709 | else | |
a50b1753 | 12710 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
12711 | |
12712 | if (ptr == NULL) | |
12713 | return FALSE; | |
12714 | ||
12715 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
12716 | h->vtable->used = ptr + 1; |
12717 | h->vtable->size = size; | |
c152c796 AM |
12718 | } |
12719 | ||
f6e332e6 | 12720 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
12721 | |
12722 | return TRUE; | |
12723 | } | |
12724 | ||
ae17ab41 CM |
12725 | /* Map an ELF section header flag to its corresponding string. */ |
12726 | typedef struct | |
12727 | { | |
12728 | char *flag_name; | |
12729 | flagword flag_value; | |
12730 | } elf_flags_to_name_table; | |
12731 | ||
12732 | static elf_flags_to_name_table elf_flags_to_names [] = | |
12733 | { | |
12734 | { "SHF_WRITE", SHF_WRITE }, | |
12735 | { "SHF_ALLOC", SHF_ALLOC }, | |
12736 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
12737 | { "SHF_MERGE", SHF_MERGE }, | |
12738 | { "SHF_STRINGS", SHF_STRINGS }, | |
12739 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
12740 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
12741 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
12742 | { "SHF_GROUP", SHF_GROUP }, | |
12743 | { "SHF_TLS", SHF_TLS }, | |
12744 | { "SHF_MASKOS", SHF_MASKOS }, | |
12745 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
12746 | }; | |
12747 | ||
b9c361e0 JL |
12748 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
12749 | bfd_boolean | |
ae17ab41 | 12750 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 12751 | struct flag_info *flaginfo, |
b9c361e0 | 12752 | asection *section) |
ae17ab41 | 12753 | { |
8b127cbc | 12754 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 12755 | |
8b127cbc | 12756 | if (!flaginfo->flags_initialized) |
ae17ab41 | 12757 | { |
8b127cbc AM |
12758 | bfd *obfd = info->output_bfd; |
12759 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
12760 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
12761 | int with_hex = 0; |
12762 | int without_hex = 0; | |
12763 | ||
8b127cbc | 12764 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 12765 | { |
b9c361e0 | 12766 | unsigned i; |
8b127cbc | 12767 | flagword (*lookup) (char *); |
ae17ab41 | 12768 | |
8b127cbc AM |
12769 | lookup = bed->elf_backend_lookup_section_flags_hook; |
12770 | if (lookup != NULL) | |
ae17ab41 | 12771 | { |
8b127cbc | 12772 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
12773 | |
12774 | if (hexval != 0) | |
12775 | { | |
12776 | if (tf->with == with_flags) | |
12777 | with_hex |= hexval; | |
12778 | else if (tf->with == without_flags) | |
12779 | without_hex |= hexval; | |
12780 | tf->valid = TRUE; | |
12781 | continue; | |
12782 | } | |
ae17ab41 | 12783 | } |
8b127cbc | 12784 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 12785 | { |
8b127cbc | 12786 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
12787 | { |
12788 | if (tf->with == with_flags) | |
12789 | with_hex |= elf_flags_to_names[i].flag_value; | |
12790 | else if (tf->with == without_flags) | |
12791 | without_hex |= elf_flags_to_names[i].flag_value; | |
12792 | tf->valid = TRUE; | |
12793 | break; | |
12794 | } | |
12795 | } | |
8b127cbc | 12796 | if (!tf->valid) |
b9c361e0 | 12797 | { |
68ffbac6 | 12798 | info->callbacks->einfo |
8b127cbc | 12799 | (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 12800 | return FALSE; |
ae17ab41 CM |
12801 | } |
12802 | } | |
8b127cbc AM |
12803 | flaginfo->flags_initialized = TRUE; |
12804 | flaginfo->only_with_flags |= with_hex; | |
12805 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 12806 | } |
ae17ab41 | 12807 | |
8b127cbc | 12808 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
12809 | return FALSE; |
12810 | ||
8b127cbc | 12811 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
12812 | return FALSE; |
12813 | ||
12814 | return TRUE; | |
ae17ab41 CM |
12815 | } |
12816 | ||
c152c796 AM |
12817 | struct alloc_got_off_arg { |
12818 | bfd_vma gotoff; | |
10455f89 | 12819 | struct bfd_link_info *info; |
c152c796 AM |
12820 | }; |
12821 | ||
12822 | /* We need a special top-level link routine to convert got reference counts | |
12823 | to real got offsets. */ | |
12824 | ||
12825 | static bfd_boolean | |
12826 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
12827 | { | |
a50b1753 | 12828 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
12829 | bfd *obfd = gofarg->info->output_bfd; |
12830 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 12831 | |
c152c796 AM |
12832 | if (h->got.refcount > 0) |
12833 | { | |
12834 | h->got.offset = gofarg->gotoff; | |
10455f89 | 12835 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
12836 | } |
12837 | else | |
12838 | h->got.offset = (bfd_vma) -1; | |
12839 | ||
12840 | return TRUE; | |
12841 | } | |
12842 | ||
12843 | /* And an accompanying bit to work out final got entry offsets once | |
12844 | we're done. Should be called from final_link. */ | |
12845 | ||
12846 | bfd_boolean | |
12847 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
12848 | struct bfd_link_info *info) | |
12849 | { | |
12850 | bfd *i; | |
12851 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12852 | bfd_vma gotoff; | |
c152c796 AM |
12853 | struct alloc_got_off_arg gofarg; |
12854 | ||
10455f89 HPN |
12855 | BFD_ASSERT (abfd == info->output_bfd); |
12856 | ||
c152c796 AM |
12857 | if (! is_elf_hash_table (info->hash)) |
12858 | return FALSE; | |
12859 | ||
12860 | /* The GOT offset is relative to the .got section, but the GOT header is | |
12861 | put into the .got.plt section, if the backend uses it. */ | |
12862 | if (bed->want_got_plt) | |
12863 | gotoff = 0; | |
12864 | else | |
12865 | gotoff = bed->got_header_size; | |
12866 | ||
12867 | /* Do the local .got entries first. */ | |
c72f2fb2 | 12868 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
12869 | { |
12870 | bfd_signed_vma *local_got; | |
12871 | bfd_size_type j, locsymcount; | |
12872 | Elf_Internal_Shdr *symtab_hdr; | |
12873 | ||
12874 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
12875 | continue; | |
12876 | ||
12877 | local_got = elf_local_got_refcounts (i); | |
12878 | if (!local_got) | |
12879 | continue; | |
12880 | ||
12881 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
12882 | if (elf_bad_symtab (i)) | |
12883 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12884 | else | |
12885 | locsymcount = symtab_hdr->sh_info; | |
12886 | ||
12887 | for (j = 0; j < locsymcount; ++j) | |
12888 | { | |
12889 | if (local_got[j] > 0) | |
12890 | { | |
12891 | local_got[j] = gotoff; | |
10455f89 | 12892 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
12893 | } |
12894 | else | |
12895 | local_got[j] = (bfd_vma) -1; | |
12896 | } | |
12897 | } | |
12898 | ||
12899 | /* Then the global .got entries. .plt refcounts are handled by | |
12900 | adjust_dynamic_symbol */ | |
12901 | gofarg.gotoff = gotoff; | |
10455f89 | 12902 | gofarg.info = info; |
c152c796 AM |
12903 | elf_link_hash_traverse (elf_hash_table (info), |
12904 | elf_gc_allocate_got_offsets, | |
12905 | &gofarg); | |
12906 | return TRUE; | |
12907 | } | |
12908 | ||
12909 | /* Many folk need no more in the way of final link than this, once | |
12910 | got entry reference counting is enabled. */ | |
12911 | ||
12912 | bfd_boolean | |
12913 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
12914 | { | |
12915 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
12916 | return FALSE; | |
12917 | ||
12918 | /* Invoke the regular ELF backend linker to do all the work. */ | |
12919 | return bfd_elf_final_link (abfd, info); | |
12920 | } | |
12921 | ||
12922 | bfd_boolean | |
12923 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
12924 | { | |
a50b1753 | 12925 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
12926 | |
12927 | if (rcookie->bad_symtab) | |
12928 | rcookie->rel = rcookie->rels; | |
12929 | ||
12930 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
12931 | { | |
12932 | unsigned long r_symndx; | |
12933 | ||
12934 | if (! rcookie->bad_symtab) | |
12935 | if (rcookie->rel->r_offset > offset) | |
12936 | return FALSE; | |
12937 | if (rcookie->rel->r_offset != offset) | |
12938 | continue; | |
12939 | ||
12940 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 12941 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
12942 | return TRUE; |
12943 | ||
12944 | if (r_symndx >= rcookie->locsymcount | |
12945 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12946 | { | |
12947 | struct elf_link_hash_entry *h; | |
12948 | ||
12949 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
12950 | ||
12951 | while (h->root.type == bfd_link_hash_indirect | |
12952 | || h->root.type == bfd_link_hash_warning) | |
12953 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
12954 | ||
12955 | if ((h->root.type == bfd_link_hash_defined | |
12956 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
12957 | && (h->root.u.def.section->owner != rcookie->abfd |
12958 | || h->root.u.def.section->kept_section != NULL | |
12959 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 12960 | return TRUE; |
c152c796 AM |
12961 | } |
12962 | else | |
12963 | { | |
12964 | /* It's not a relocation against a global symbol, | |
12965 | but it could be a relocation against a local | |
12966 | symbol for a discarded section. */ | |
12967 | asection *isec; | |
12968 | Elf_Internal_Sym *isym; | |
12969 | ||
12970 | /* Need to: get the symbol; get the section. */ | |
12971 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 12972 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
12973 | if (isec != NULL |
12974 | && (isec->kept_section != NULL | |
12975 | || discarded_section (isec))) | |
cb33740c | 12976 | return TRUE; |
c152c796 AM |
12977 | } |
12978 | return FALSE; | |
12979 | } | |
12980 | return FALSE; | |
12981 | } | |
12982 | ||
12983 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
12984 | Returns -1 on error, 1 if any section's size was changed, 0 if |
12985 | nothing changed. This function assumes that the relocations are in | |
12986 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 12987 | |
75938853 | 12988 | int |
c152c796 AM |
12989 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
12990 | { | |
12991 | struct elf_reloc_cookie cookie; | |
18cd5bce | 12992 | asection *o; |
c152c796 | 12993 | bfd *abfd; |
75938853 | 12994 | int changed = 0; |
c152c796 AM |
12995 | |
12996 | if (info->traditional_format | |
12997 | || !is_elf_hash_table (info->hash)) | |
75938853 | 12998 | return 0; |
c152c796 | 12999 | |
18cd5bce AM |
13000 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
13001 | if (o != NULL) | |
c152c796 | 13002 | { |
18cd5bce | 13003 | asection *i; |
c152c796 | 13004 | |
18cd5bce | 13005 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 13006 | { |
18cd5bce AM |
13007 | if (i->size == 0 |
13008 | || i->reloc_count == 0 | |
13009 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
13010 | continue; | |
c152c796 | 13011 | |
18cd5bce AM |
13012 | abfd = i->owner; |
13013 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13014 | continue; | |
c152c796 | 13015 | |
18cd5bce | 13016 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 13017 | return -1; |
c152c796 | 13018 | |
18cd5bce AM |
13019 | if (_bfd_discard_section_stabs (abfd, i, |
13020 | elf_section_data (i)->sec_info, | |
5241d853 RS |
13021 | bfd_elf_reloc_symbol_deleted_p, |
13022 | &cookie)) | |
75938853 | 13023 | changed = 1; |
18cd5bce AM |
13024 | |
13025 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13026 | } |
18cd5bce AM |
13027 | } |
13028 | ||
2f0c68f2 CM |
13029 | o = NULL; |
13030 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
13031 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
13032 | if (o != NULL) |
13033 | { | |
13034 | asection *i; | |
c152c796 | 13035 | |
18cd5bce | 13036 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 13037 | { |
18cd5bce AM |
13038 | if (i->size == 0) |
13039 | continue; | |
13040 | ||
13041 | abfd = i->owner; | |
13042 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13043 | continue; | |
13044 | ||
13045 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 13046 | return -1; |
18cd5bce AM |
13047 | |
13048 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
13049 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
13050 | bfd_elf_reloc_symbol_deleted_p, |
13051 | &cookie)) | |
75938853 | 13052 | changed = 1; |
18cd5bce AM |
13053 | |
13054 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13055 | } |
18cd5bce | 13056 | } |
c152c796 | 13057 | |
18cd5bce AM |
13058 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
13059 | { | |
13060 | const struct elf_backend_data *bed; | |
c152c796 | 13061 | |
18cd5bce AM |
13062 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
13063 | continue; | |
13064 | ||
13065 | bed = get_elf_backend_data (abfd); | |
13066 | ||
13067 | if (bed->elf_backend_discard_info != NULL) | |
13068 | { | |
13069 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 13070 | return -1; |
18cd5bce AM |
13071 | |
13072 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 13073 | changed = 1; |
18cd5bce AM |
13074 | |
13075 | fini_reloc_cookie (&cookie, abfd); | |
13076 | } | |
c152c796 AM |
13077 | } |
13078 | ||
2f0c68f2 CM |
13079 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
13080 | _bfd_elf_end_eh_frame_parsing (info); | |
13081 | ||
13082 | if (info->eh_frame_hdr_type | |
c152c796 AM |
13083 | && !info->relocatable |
13084 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
75938853 | 13085 | changed = 1; |
c152c796 | 13086 | |
75938853 | 13087 | return changed; |
c152c796 | 13088 | } |
082b7297 | 13089 | |
43e1669b | 13090 | bfd_boolean |
0c511000 | 13091 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 13092 | asection *sec, |
c0f00686 | 13093 | struct bfd_link_info *info) |
082b7297 L |
13094 | { |
13095 | flagword flags; | |
c77ec726 | 13096 | const char *name, *key; |
082b7297 L |
13097 | struct bfd_section_already_linked *l; |
13098 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 13099 | |
c77ec726 AM |
13100 | if (sec->output_section == bfd_abs_section_ptr) |
13101 | return FALSE; | |
0c511000 | 13102 | |
c77ec726 | 13103 | flags = sec->flags; |
0c511000 | 13104 | |
c77ec726 AM |
13105 | /* Return if it isn't a linkonce section. A comdat group section |
13106 | also has SEC_LINK_ONCE set. */ | |
13107 | if ((flags & SEC_LINK_ONCE) == 0) | |
13108 | return FALSE; | |
0c511000 | 13109 | |
c77ec726 AM |
13110 | /* Don't put group member sections on our list of already linked |
13111 | sections. They are handled as a group via their group section. */ | |
13112 | if (elf_sec_group (sec) != NULL) | |
13113 | return FALSE; | |
0c511000 | 13114 | |
c77ec726 AM |
13115 | /* For a SHT_GROUP section, use the group signature as the key. */ |
13116 | name = sec->name; | |
13117 | if ((flags & SEC_GROUP) != 0 | |
13118 | && elf_next_in_group (sec) != NULL | |
13119 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
13120 | key = elf_group_name (elf_next_in_group (sec)); | |
13121 | else | |
13122 | { | |
13123 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 13124 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
13125 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
13126 | key++; | |
0c511000 | 13127 | else |
c77ec726 AM |
13128 | /* Must be a user linkonce section that doesn't follow gcc's |
13129 | naming convention. In this case we won't be matching | |
13130 | single member groups. */ | |
13131 | key = name; | |
0c511000 | 13132 | } |
6d2cd210 | 13133 | |
c77ec726 | 13134 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
13135 | |
13136 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13137 | { | |
c2370991 | 13138 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
13139 | sections with a signature of <key> (<key> is some string), |
13140 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
13141 | Match like sections. LTO plugin sections are an exception. | |
13142 | They are always named .gnu.linkonce.t.<key> and match either | |
13143 | type of section. */ | |
13144 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
13145 | && ((flags & SEC_GROUP) != 0 | |
13146 | || strcmp (name, l->sec->name) == 0)) | |
13147 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
13148 | { |
13149 | /* The section has already been linked. See if we should | |
6d2cd210 | 13150 | issue a warning. */ |
c77ec726 AM |
13151 | if (!_bfd_handle_already_linked (sec, l, info)) |
13152 | return FALSE; | |
082b7297 | 13153 | |
c77ec726 | 13154 | if (flags & SEC_GROUP) |
3d7f7666 | 13155 | { |
c77ec726 AM |
13156 | asection *first = elf_next_in_group (sec); |
13157 | asection *s = first; | |
3d7f7666 | 13158 | |
c77ec726 | 13159 | while (s != NULL) |
3d7f7666 | 13160 | { |
c77ec726 AM |
13161 | s->output_section = bfd_abs_section_ptr; |
13162 | /* Record which group discards it. */ | |
13163 | s->kept_section = l->sec; | |
13164 | s = elf_next_in_group (s); | |
13165 | /* These lists are circular. */ | |
13166 | if (s == first) | |
13167 | break; | |
3d7f7666 L |
13168 | } |
13169 | } | |
082b7297 | 13170 | |
43e1669b | 13171 | return TRUE; |
082b7297 L |
13172 | } |
13173 | } | |
13174 | ||
c77ec726 AM |
13175 | /* A single member comdat group section may be discarded by a |
13176 | linkonce section and vice versa. */ | |
13177 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 13178 | { |
c77ec726 | 13179 | asection *first = elf_next_in_group (sec); |
c2370991 | 13180 | |
c77ec726 AM |
13181 | if (first != NULL && elf_next_in_group (first) == first) |
13182 | /* Check this single member group against linkonce sections. */ | |
13183 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13184 | if ((l->sec->flags & SEC_GROUP) == 0 | |
13185 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
13186 | { | |
13187 | first->output_section = bfd_abs_section_ptr; | |
13188 | first->kept_section = l->sec; | |
13189 | sec->output_section = bfd_abs_section_ptr; | |
13190 | break; | |
13191 | } | |
13192 | } | |
13193 | else | |
13194 | /* Check this linkonce section against single member groups. */ | |
13195 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13196 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 13197 | { |
c77ec726 | 13198 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 13199 | |
c77ec726 AM |
13200 | if (first != NULL |
13201 | && elf_next_in_group (first) == first | |
13202 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
13203 | { | |
13204 | sec->output_section = bfd_abs_section_ptr; | |
13205 | sec->kept_section = first; | |
13206 | break; | |
13207 | } | |
6d2cd210 | 13208 | } |
0c511000 | 13209 | |
c77ec726 AM |
13210 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
13211 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
13212 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
13213 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
13214 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
13215 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
13216 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
13217 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
13218 | The reverse order cannot happen as there is never a bfd with only the | |
13219 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
13220 | matter as here were are looking only for cross-bfd sections. */ | |
13221 | ||
13222 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
13223 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
13224 | if ((l->sec->flags & SEC_GROUP) == 0 | |
13225 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
13226 | { | |
13227 | if (abfd != l->sec->owner) | |
13228 | sec->output_section = bfd_abs_section_ptr; | |
13229 | break; | |
13230 | } | |
80c29487 | 13231 | |
082b7297 | 13232 | /* This is the first section with this name. Record it. */ |
c77ec726 | 13233 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 13234 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 13235 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 13236 | } |
81e1b023 | 13237 | |
a4d8e49b L |
13238 | bfd_boolean |
13239 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
13240 | { | |
13241 | return sym->st_shndx == SHN_COMMON; | |
13242 | } | |
13243 | ||
13244 | unsigned int | |
13245 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
13246 | { | |
13247 | return SHN_COMMON; | |
13248 | } | |
13249 | ||
13250 | asection * | |
13251 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
13252 | { | |
13253 | return bfd_com_section_ptr; | |
13254 | } | |
10455f89 HPN |
13255 | |
13256 | bfd_vma | |
13257 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
13258 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13259 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
13260 | bfd *ibfd ATTRIBUTE_UNUSED, | |
13261 | unsigned long symndx ATTRIBUTE_UNUSED) | |
13262 | { | |
13263 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13264 | return bed->s->arch_size / 8; | |
13265 | } | |
83bac4b0 NC |
13266 | |
13267 | /* Routines to support the creation of dynamic relocs. */ | |
13268 | ||
83bac4b0 NC |
13269 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
13270 | ||
13271 | static const char * | |
13272 | get_dynamic_reloc_section_name (bfd * abfd, | |
13273 | asection * sec, | |
13274 | bfd_boolean is_rela) | |
13275 | { | |
ddcf1fcf BS |
13276 | char *name; |
13277 | const char *old_name = bfd_get_section_name (NULL, sec); | |
13278 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 13279 | |
ddcf1fcf | 13280 | if (old_name == NULL) |
83bac4b0 NC |
13281 | return NULL; |
13282 | ||
ddcf1fcf | 13283 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 13284 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
13285 | |
13286 | return name; | |
13287 | } | |
13288 | ||
13289 | /* Returns the dynamic reloc section associated with SEC. | |
13290 | If necessary compute the name of the dynamic reloc section based | |
13291 | on SEC's name (looked up in ABFD's string table) and the setting | |
13292 | of IS_RELA. */ | |
13293 | ||
13294 | asection * | |
13295 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
13296 | asection * sec, | |
13297 | bfd_boolean is_rela) | |
13298 | { | |
13299 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
13300 | ||
13301 | if (reloc_sec == NULL) | |
13302 | { | |
13303 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
13304 | ||
13305 | if (name != NULL) | |
13306 | { | |
3d4d4302 | 13307 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
13308 | |
13309 | if (reloc_sec != NULL) | |
13310 | elf_section_data (sec)->sreloc = reloc_sec; | |
13311 | } | |
13312 | } | |
13313 | ||
13314 | return reloc_sec; | |
13315 | } | |
13316 | ||
13317 | /* Returns the dynamic reloc section associated with SEC. If the | |
13318 | section does not exist it is created and attached to the DYNOBJ | |
13319 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
13320 | structure. | |
f8076f98 | 13321 | |
83bac4b0 NC |
13322 | ALIGNMENT is the alignment for the newly created section and |
13323 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
13324 | or .rel.<SEC's name>. The section name is looked up in the | |
13325 | string table associated with ABFD. */ | |
13326 | ||
13327 | asection * | |
ca4be51c AM |
13328 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
13329 | bfd *dynobj, | |
13330 | unsigned int alignment, | |
13331 | bfd *abfd, | |
13332 | bfd_boolean is_rela) | |
83bac4b0 NC |
13333 | { |
13334 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
13335 | ||
13336 | if (reloc_sec == NULL) | |
13337 | { | |
13338 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
13339 | ||
13340 | if (name == NULL) | |
13341 | return NULL; | |
13342 | ||
3d4d4302 | 13343 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
13344 | |
13345 | if (reloc_sec == NULL) | |
13346 | { | |
3d4d4302 AM |
13347 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
13348 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
13349 | if ((sec->flags & SEC_ALLOC) != 0) |
13350 | flags |= SEC_ALLOC | SEC_LOAD; | |
13351 | ||
3d4d4302 | 13352 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
13353 | if (reloc_sec != NULL) |
13354 | { | |
8877b5e5 AM |
13355 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
13356 | name. Override as it may be wrong, eg. for a user | |
13357 | section named "auto" we'll get ".relauto" which is | |
13358 | seen to be a .rela section. */ | |
13359 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
13360 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
13361 | reloc_sec = NULL; | |
13362 | } | |
13363 | } | |
13364 | ||
13365 | elf_section_data (sec)->sreloc = reloc_sec; | |
13366 | } | |
13367 | ||
13368 | return reloc_sec; | |
13369 | } | |
1338dd10 | 13370 | |
bffebb6b AM |
13371 | /* Copy the ELF symbol type and other attributes for a linker script |
13372 | assignment from HSRC to HDEST. Generally this should be treated as | |
13373 | if we found a strong non-dynamic definition for HDEST (except that | |
13374 | ld ignores multiple definition errors). */ | |
1338dd10 | 13375 | void |
bffebb6b AM |
13376 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
13377 | struct bfd_link_hash_entry *hdest, | |
13378 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 13379 | { |
bffebb6b AM |
13380 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
13381 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
13382 | Elf_Internal_Sym isym; | |
1338dd10 PB |
13383 | |
13384 | ehdest->type = ehsrc->type; | |
35fc36a8 | 13385 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
13386 | |
13387 | isym.st_other = ehsrc->other; | |
b8417128 | 13388 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 13389 | } |
351f65ca L |
13390 | |
13391 | /* Append a RELA relocation REL to section S in BFD. */ | |
13392 | ||
13393 | void | |
13394 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
13395 | { | |
13396 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13397 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
13398 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
13399 | bed->s->swap_reloca_out (abfd, rel, loc); | |
13400 | } | |
13401 | ||
13402 | /* Append a REL relocation REL to section S in BFD. */ | |
13403 | ||
13404 | void | |
13405 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
13406 | { | |
13407 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13408 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
13409 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 13410 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 13411 | } |