Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
b3adc24a | 2 | Copyright (C) 1995-2020 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" |
252b5132 RH |
23 | #include "bfdlink.h" |
24 | #include "libbfd.h" | |
25 | #define ARCH_SIZE 0 | |
26 | #include "elf-bfd.h" | |
4ad4eba5 | 27 | #include "safe-ctype.h" |
ccf2f652 | 28 | #include "libiberty.h" |
66eb6687 | 29 | #include "objalloc.h" |
08ce1d72 | 30 | #if BFD_SUPPORTS_PLUGINS |
7d0b9ebc | 31 | #include "plugin-api.h" |
7dc3990e L |
32 | #include "plugin.h" |
33 | #endif | |
252b5132 | 34 | |
28caa186 AM |
35 | /* This struct is used to pass information to routines called via |
36 | elf_link_hash_traverse which must return failure. */ | |
37 | ||
38 | struct elf_info_failed | |
39 | { | |
40 | struct bfd_link_info *info; | |
28caa186 AM |
41 | bfd_boolean failed; |
42 | }; | |
43 | ||
44 | /* This structure is used to pass information to | |
45 | _bfd_elf_link_find_version_dependencies. */ | |
46 | ||
47 | struct elf_find_verdep_info | |
48 | { | |
49 | /* General link information. */ | |
50 | struct bfd_link_info *info; | |
51 | /* The number of dependencies. */ | |
52 | unsigned int vers; | |
53 | /* Whether we had a failure. */ | |
54 | bfd_boolean failed; | |
55 | }; | |
56 | ||
57 | static bfd_boolean _bfd_elf_fix_symbol_flags | |
58 | (struct elf_link_hash_entry *, struct elf_info_failed *); | |
59 | ||
2f0c68f2 CM |
60 | asection * |
61 | _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, | |
62 | unsigned long r_symndx, | |
63 | bfd_boolean discard) | |
64 | { | |
65 | if (r_symndx >= cookie->locsymcount | |
66 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
67 | { | |
68 | struct elf_link_hash_entry *h; | |
69 | ||
70 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
71 | ||
72 | while (h->root.type == bfd_link_hash_indirect | |
73 | || h->root.type == bfd_link_hash_warning) | |
74 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
75 | ||
76 | if ((h->root.type == bfd_link_hash_defined | |
77 | || h->root.type == bfd_link_hash_defweak) | |
78 | && discarded_section (h->root.u.def.section)) | |
07d6d2b8 | 79 | return h->root.u.def.section; |
2f0c68f2 CM |
80 | else |
81 | return NULL; | |
82 | } | |
83 | else | |
84 | { | |
85 | /* It's not a relocation against a global symbol, | |
86 | but it could be a relocation against a local | |
87 | symbol for a discarded section. */ | |
88 | asection *isec; | |
89 | Elf_Internal_Sym *isym; | |
90 | ||
91 | /* Need to: get the symbol; get the section. */ | |
92 | isym = &cookie->locsyms[r_symndx]; | |
93 | isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); | |
94 | if (isec != NULL | |
95 | && discard ? discarded_section (isec) : 1) | |
96 | return isec; | |
97 | } | |
98 | return NULL; | |
99 | } | |
100 | ||
d98685ac AM |
101 | /* Define a symbol in a dynamic linkage section. */ |
102 | ||
103 | struct elf_link_hash_entry * | |
104 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
105 | struct bfd_link_info *info, | |
106 | asection *sec, | |
107 | const char *name) | |
108 | { | |
109 | struct elf_link_hash_entry *h; | |
110 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 111 | const struct elf_backend_data *bed; |
d98685ac AM |
112 | |
113 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
114 | if (h != NULL) | |
115 | { | |
116 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
117 | This is a symptom of a larger problem: Absolute symbols | |
118 | defined in shared libraries can't be overridden, because we | |
119 | lose the link to the bfd which is via the symbol section. */ | |
120 | h->root.type = bfd_link_hash_new; | |
ad32986f | 121 | bh = &h->root; |
d98685ac | 122 | } |
ad32986f NC |
123 | else |
124 | bh = NULL; | |
d98685ac | 125 | |
cf18fda4 | 126 | bed = get_elf_backend_data (abfd); |
d98685ac | 127 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, |
cf18fda4 | 128 | sec, 0, NULL, FALSE, bed->collect, |
d98685ac AM |
129 | &bh)) |
130 | return NULL; | |
131 | h = (struct elf_link_hash_entry *) bh; | |
ad32986f | 132 | BFD_ASSERT (h != NULL); |
d98685ac | 133 | h->def_regular = 1; |
e28df02b | 134 | h->non_elf = 0; |
12b2843a | 135 | h->root.linker_def = 1; |
d98685ac | 136 | h->type = STT_OBJECT; |
00b7642b AM |
137 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
138 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
d98685ac | 139 | |
ccabcbe5 | 140 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
d98685ac AM |
141 | return h; |
142 | } | |
143 | ||
b34976b6 | 144 | bfd_boolean |
268b6b39 | 145 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
146 | { |
147 | flagword flags; | |
aad5d350 | 148 | asection *s; |
252b5132 | 149 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 150 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 151 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
152 | |
153 | /* This function may be called more than once. */ | |
ce558b89 | 154 | if (htab->sgot != NULL) |
b34976b6 | 155 | return TRUE; |
252b5132 | 156 | |
e5a52504 | 157 | flags = bed->dynamic_sec_flags; |
252b5132 | 158 | |
14b2f831 AM |
159 | s = bfd_make_section_anyway_with_flags (abfd, |
160 | (bed->rela_plts_and_copies_p | |
161 | ? ".rela.got" : ".rel.got"), | |
162 | (bed->dynamic_sec_flags | |
163 | | SEC_READONLY)); | |
6de2ae4a | 164 | if (s == NULL |
fd361982 | 165 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
6de2ae4a L |
166 | return FALSE; |
167 | htab->srelgot = s; | |
252b5132 | 168 | |
14b2f831 | 169 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d | 170 | if (s == NULL |
fd361982 | 171 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
64e77c6d L |
172 | return FALSE; |
173 | htab->sgot = s; | |
174 | ||
252b5132 RH |
175 | if (bed->want_got_plt) |
176 | { | |
14b2f831 | 177 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 178 | if (s == NULL |
fd361982 | 179 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
b34976b6 | 180 | return FALSE; |
6de2ae4a | 181 | htab->sgotplt = s; |
252b5132 RH |
182 | } |
183 | ||
64e77c6d L |
184 | /* The first bit of the global offset table is the header. */ |
185 | s->size += bed->got_header_size; | |
186 | ||
2517a57f AM |
187 | if (bed->want_got_sym) |
188 | { | |
189 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
190 | (or .got.plt) section. We don't do this in the linker script | |
191 | because we don't want to define the symbol if we are not creating | |
192 | a global offset table. */ | |
6de2ae4a L |
193 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
194 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 195 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
196 | if (h == NULL) |
197 | return FALSE; | |
2517a57f | 198 | } |
252b5132 | 199 | |
b34976b6 | 200 | return TRUE; |
252b5132 RH |
201 | } |
202 | \f | |
7e9f0867 AM |
203 | /* Create a strtab to hold the dynamic symbol names. */ |
204 | static bfd_boolean | |
205 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
206 | { | |
207 | struct elf_link_hash_table *hash_table; | |
208 | ||
209 | hash_table = elf_hash_table (info); | |
210 | if (hash_table->dynobj == NULL) | |
6cd255ca L |
211 | { |
212 | /* We may not set dynobj, an input file holding linker created | |
213 | dynamic sections to abfd, which may be a dynamic object with | |
214 | its own dynamic sections. We need to find a normal input file | |
215 | to hold linker created sections if possible. */ | |
216 | if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) | |
217 | { | |
218 | bfd *ibfd; | |
57963c05 | 219 | asection *s; |
6cd255ca | 220 | for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) |
6645479e | 221 | if ((ibfd->flags |
57963c05 AM |
222 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0 |
223 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
4de5434b | 224 | && elf_object_id (ibfd) == elf_hash_table_id (hash_table) |
57963c05 AM |
225 | && !((s = ibfd->sections) != NULL |
226 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)) | |
6cd255ca L |
227 | { |
228 | abfd = ibfd; | |
229 | break; | |
230 | } | |
231 | } | |
232 | hash_table->dynobj = abfd; | |
233 | } | |
7e9f0867 AM |
234 | |
235 | if (hash_table->dynstr == NULL) | |
236 | { | |
237 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
238 | if (hash_table->dynstr == NULL) | |
239 | return FALSE; | |
240 | } | |
241 | return TRUE; | |
242 | } | |
243 | ||
45d6a902 AM |
244 | /* Create some sections which will be filled in with dynamic linking |
245 | information. ABFD is an input file which requires dynamic sections | |
246 | to be created. The dynamic sections take up virtual memory space | |
247 | when the final executable is run, so we need to create them before | |
248 | addresses are assigned to the output sections. We work out the | |
249 | actual contents and size of these sections later. */ | |
252b5132 | 250 | |
b34976b6 | 251 | bfd_boolean |
268b6b39 | 252 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 253 | { |
45d6a902 | 254 | flagword flags; |
91d6fa6a | 255 | asection *s; |
9c5bfbb7 | 256 | const struct elf_backend_data *bed; |
9637f6ef | 257 | struct elf_link_hash_entry *h; |
252b5132 | 258 | |
0eddce27 | 259 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
260 | return FALSE; |
261 | ||
262 | if (elf_hash_table (info)->dynamic_sections_created) | |
263 | return TRUE; | |
264 | ||
7e9f0867 AM |
265 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
266 | return FALSE; | |
45d6a902 | 267 | |
7e9f0867 | 268 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
269 | bed = get_elf_backend_data (abfd); |
270 | ||
271 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
272 | |
273 | /* A dynamically linked executable has a .interp section, but a | |
274 | shared library does not. */ | |
9b8b325a | 275 | if (bfd_link_executable (info) && !info->nointerp) |
252b5132 | 276 | { |
14b2f831 AM |
277 | s = bfd_make_section_anyway_with_flags (abfd, ".interp", |
278 | flags | SEC_READONLY); | |
3496cb2a | 279 | if (s == NULL) |
45d6a902 AM |
280 | return FALSE; |
281 | } | |
bb0deeff | 282 | |
45d6a902 AM |
283 | /* Create sections to hold version informations. These are removed |
284 | if they are not needed. */ | |
14b2f831 AM |
285 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", |
286 | flags | SEC_READONLY); | |
45d6a902 | 287 | if (s == NULL |
fd361982 | 288 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
45d6a902 AM |
289 | return FALSE; |
290 | ||
14b2f831 AM |
291 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", |
292 | flags | SEC_READONLY); | |
45d6a902 | 293 | if (s == NULL |
fd361982 | 294 | || !bfd_set_section_alignment (s, 1)) |
45d6a902 AM |
295 | return FALSE; |
296 | ||
14b2f831 AM |
297 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", |
298 | flags | SEC_READONLY); | |
45d6a902 | 299 | if (s == NULL |
fd361982 | 300 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
45d6a902 AM |
301 | return FALSE; |
302 | ||
14b2f831 AM |
303 | s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", |
304 | flags | SEC_READONLY); | |
45d6a902 | 305 | if (s == NULL |
fd361982 | 306 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
45d6a902 | 307 | return FALSE; |
cae1fbbb | 308 | elf_hash_table (info)->dynsym = s; |
45d6a902 | 309 | |
14b2f831 AM |
310 | s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", |
311 | flags | SEC_READONLY); | |
3496cb2a | 312 | if (s == NULL) |
45d6a902 AM |
313 | return FALSE; |
314 | ||
14b2f831 | 315 | s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 316 | if (s == NULL |
fd361982 | 317 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
45d6a902 AM |
318 | return FALSE; |
319 | ||
320 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
321 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
322 | only want to define it if we are, in fact, creating a .dynamic | |
323 | section. We don't want to define it if there is no .dynamic | |
324 | section, since on some ELF platforms the start up code examines it | |
325 | to decide how to initialize the process. */ | |
9637f6ef L |
326 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); |
327 | elf_hash_table (info)->hdynamic = h; | |
328 | if (h == NULL) | |
45d6a902 AM |
329 | return FALSE; |
330 | ||
fdc90cb4 JJ |
331 | if (info->emit_hash) |
332 | { | |
14b2f831 AM |
333 | s = bfd_make_section_anyway_with_flags (abfd, ".hash", |
334 | flags | SEC_READONLY); | |
fdc90cb4 | 335 | if (s == NULL |
fd361982 | 336 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
fdc90cb4 JJ |
337 | return FALSE; |
338 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
339 | } | |
340 | ||
f16a9783 | 341 | if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL) |
fdc90cb4 | 342 | { |
14b2f831 AM |
343 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", |
344 | flags | SEC_READONLY); | |
fdc90cb4 | 345 | if (s == NULL |
fd361982 | 346 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
fdc90cb4 JJ |
347 | return FALSE; |
348 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
349 | 4 32-bit words followed by variable count of 64-bit words, then | |
350 | variable count of 32-bit words. */ | |
351 | if (bed->s->arch_size == 64) | |
352 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
353 | else | |
354 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
355 | } | |
45d6a902 AM |
356 | |
357 | /* Let the backend create the rest of the sections. This lets the | |
358 | backend set the right flags. The backend will normally create | |
359 | the .got and .plt sections. */ | |
894891db NC |
360 | if (bed->elf_backend_create_dynamic_sections == NULL |
361 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
45d6a902 AM |
362 | return FALSE; |
363 | ||
364 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
365 | ||
366 | return TRUE; | |
367 | } | |
368 | ||
369 | /* Create dynamic sections when linking against a dynamic object. */ | |
370 | ||
371 | bfd_boolean | |
268b6b39 | 372 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
373 | { |
374 | flagword flags, pltflags; | |
7325306f | 375 | struct elf_link_hash_entry *h; |
45d6a902 | 376 | asection *s; |
9c5bfbb7 | 377 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 378 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 379 | |
252b5132 RH |
380 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
381 | .rel[a].bss sections. */ | |
e5a52504 | 382 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
383 | |
384 | pltflags = flags; | |
252b5132 | 385 | if (bed->plt_not_loaded) |
6df4d94c MM |
386 | /* We do not clear SEC_ALLOC here because we still want the OS to |
387 | allocate space for the section; it's just that there's nothing | |
388 | to read in from the object file. */ | |
5d1634d7 | 389 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
390 | else |
391 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
392 | if (bed->plt_readonly) |
393 | pltflags |= SEC_READONLY; | |
394 | ||
14b2f831 | 395 | s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); |
252b5132 | 396 | if (s == NULL |
fd361982 | 397 | || !bfd_set_section_alignment (s, bed->plt_alignment)) |
b34976b6 | 398 | return FALSE; |
6de2ae4a | 399 | htab->splt = s; |
252b5132 | 400 | |
d98685ac AM |
401 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
402 | .plt section. */ | |
7325306f RS |
403 | if (bed->want_plt_sym) |
404 | { | |
405 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
406 | "_PROCEDURE_LINKAGE_TABLE_"); | |
407 | elf_hash_table (info)->hplt = h; | |
408 | if (h == NULL) | |
409 | return FALSE; | |
410 | } | |
252b5132 | 411 | |
14b2f831 AM |
412 | s = bfd_make_section_anyway_with_flags (abfd, |
413 | (bed->rela_plts_and_copies_p | |
414 | ? ".rela.plt" : ".rel.plt"), | |
415 | flags | SEC_READONLY); | |
252b5132 | 416 | if (s == NULL |
fd361982 | 417 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
b34976b6 | 418 | return FALSE; |
6de2ae4a | 419 | htab->srelplt = s; |
252b5132 RH |
420 | |
421 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 422 | return FALSE; |
252b5132 | 423 | |
3018b441 RH |
424 | if (bed->want_dynbss) |
425 | { | |
426 | /* The .dynbss section is a place to put symbols which are defined | |
427 | by dynamic objects, are referenced by regular objects, and are | |
428 | not functions. We must allocate space for them in the process | |
429 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
430 | initialize them at run time. The linker script puts the .dynbss | |
431 | section into the .bss section of the final image. */ | |
14b2f831 | 432 | s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", |
afbf7e8e | 433 | SEC_ALLOC | SEC_LINKER_CREATED); |
3496cb2a | 434 | if (s == NULL) |
b34976b6 | 435 | return FALSE; |
9d19e4fd | 436 | htab->sdynbss = s; |
252b5132 | 437 | |
5474d94f AM |
438 | if (bed->want_dynrelro) |
439 | { | |
440 | /* Similarly, but for symbols that were originally in read-only | |
afbf7e8e AM |
441 | sections. This section doesn't really need to have contents, |
442 | but make it like other .data.rel.ro sections. */ | |
5474d94f | 443 | s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro", |
afbf7e8e | 444 | flags); |
5474d94f AM |
445 | if (s == NULL) |
446 | return FALSE; | |
447 | htab->sdynrelro = s; | |
448 | } | |
449 | ||
3018b441 | 450 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
451 | normally needed. We need to create it here, though, so that the |
452 | linker will map it to an output section. We can't just create it | |
453 | only if we need it, because we will not know whether we need it | |
454 | until we have seen all the input files, and the first time the | |
455 | main linker code calls BFD after examining all the input files | |
456 | (size_dynamic_sections) the input sections have already been | |
457 | mapped to the output sections. If the section turns out not to | |
458 | be needed, we can discard it later. We will never need this | |
459 | section when generating a shared object, since they do not use | |
460 | copy relocs. */ | |
9d19e4fd | 461 | if (bfd_link_executable (info)) |
3018b441 | 462 | { |
14b2f831 AM |
463 | s = bfd_make_section_anyway_with_flags (abfd, |
464 | (bed->rela_plts_and_copies_p | |
465 | ? ".rela.bss" : ".rel.bss"), | |
466 | flags | SEC_READONLY); | |
3018b441 | 467 | if (s == NULL |
fd361982 | 468 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
b34976b6 | 469 | return FALSE; |
9d19e4fd | 470 | htab->srelbss = s; |
5474d94f AM |
471 | |
472 | if (bed->want_dynrelro) | |
473 | { | |
474 | s = (bfd_make_section_anyway_with_flags | |
475 | (abfd, (bed->rela_plts_and_copies_p | |
476 | ? ".rela.data.rel.ro" : ".rel.data.rel.ro"), | |
477 | flags | SEC_READONLY)); | |
478 | if (s == NULL | |
fd361982 | 479 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
5474d94f AM |
480 | return FALSE; |
481 | htab->sreldynrelro = s; | |
482 | } | |
3018b441 | 483 | } |
252b5132 RH |
484 | } |
485 | ||
b34976b6 | 486 | return TRUE; |
252b5132 RH |
487 | } |
488 | \f | |
252b5132 RH |
489 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
490 | read the input files, since we need to have a list of all of them | |
491 | before we can determine the final sizes of the output sections. | |
492 | Note that we may actually call this function even though we are not | |
493 | going to output any dynamic symbols; in some cases we know that a | |
494 | symbol should be in the dynamic symbol table, but only if there is | |
495 | one. */ | |
496 | ||
b34976b6 | 497 | bfd_boolean |
c152c796 AM |
498 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
499 | struct elf_link_hash_entry *h) | |
252b5132 RH |
500 | { |
501 | if (h->dynindx == -1) | |
502 | { | |
2b0f7ef9 | 503 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 504 | char *p; |
252b5132 | 505 | const char *name; |
ef53be89 | 506 | size_t indx; |
252b5132 | 507 | |
7a13edea NC |
508 | /* XXX: The ABI draft says the linker must turn hidden and |
509 | internal symbols into STB_LOCAL symbols when producing the | |
510 | DSO. However, if ld.so honors st_other in the dynamic table, | |
511 | this would not be necessary. */ | |
512 | switch (ELF_ST_VISIBILITY (h->other)) | |
513 | { | |
514 | case STV_INTERNAL: | |
515 | case STV_HIDDEN: | |
9d6eee78 L |
516 | if (h->root.type != bfd_link_hash_undefined |
517 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 518 | { |
f5385ebf | 519 | h->forced_local = 1; |
67687978 PB |
520 | if (!elf_hash_table (info)->is_relocatable_executable) |
521 | return TRUE; | |
7a13edea | 522 | } |
0444bdd4 | 523 | |
7a13edea NC |
524 | default: |
525 | break; | |
526 | } | |
527 | ||
252b5132 RH |
528 | h->dynindx = elf_hash_table (info)->dynsymcount; |
529 | ++elf_hash_table (info)->dynsymcount; | |
530 | ||
531 | dynstr = elf_hash_table (info)->dynstr; | |
532 | if (dynstr == NULL) | |
533 | { | |
534 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 535 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 536 | if (dynstr == NULL) |
b34976b6 | 537 | return FALSE; |
252b5132 RH |
538 | } |
539 | ||
540 | /* We don't put any version information in the dynamic string | |
aad5d350 | 541 | table. */ |
252b5132 RH |
542 | name = h->root.root.string; |
543 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
544 | if (p != NULL) |
545 | /* We know that the p points into writable memory. In fact, | |
546 | there are only a few symbols that have read-only names, being | |
547 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
548 | by the backends. Most symbols will have names pointing into | |
549 | an ELF string table read from a file, or to objalloc memory. */ | |
550 | *p = 0; | |
551 | ||
552 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
553 | ||
554 | if (p != NULL) | |
555 | *p = ELF_VER_CHR; | |
252b5132 | 556 | |
ef53be89 | 557 | if (indx == (size_t) -1) |
b34976b6 | 558 | return FALSE; |
252b5132 RH |
559 | h->dynstr_index = indx; |
560 | } | |
561 | ||
b34976b6 | 562 | return TRUE; |
252b5132 | 563 | } |
45d6a902 | 564 | \f |
55255dae L |
565 | /* Mark a symbol dynamic. */ |
566 | ||
28caa186 | 567 | static void |
55255dae | 568 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
569 | struct elf_link_hash_entry *h, |
570 | Elf_Internal_Sym *sym) | |
55255dae | 571 | { |
40b36307 | 572 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 573 | |
40b36307 | 574 | /* It may be called more than once on the same H. */ |
0e1862bb | 575 | if(h->dynamic || bfd_link_relocatable (info)) |
55255dae L |
576 | return; |
577 | ||
40b36307 L |
578 | if ((info->dynamic_data |
579 | && (h->type == STT_OBJECT | |
b8871f35 | 580 | || h->type == STT_COMMON |
40b36307 | 581 | || (sym != NULL |
b8871f35 L |
582 | && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT |
583 | || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) | |
a0c8462f | 584 | || (d != NULL |
73ec947d | 585 | && h->non_elf |
40b36307 | 586 | && (*d->match) (&d->head, NULL, h->root.root.string))) |
416c34d6 L |
587 | { |
588 | h->dynamic = 1; | |
589 | /* NB: If a symbol is made dynamic by --dynamic-list, it has | |
590 | non-IR reference. */ | |
591 | h->root.non_ir_ref_dynamic = 1; | |
592 | } | |
55255dae L |
593 | } |
594 | ||
45d6a902 AM |
595 | /* Record an assignment to a symbol made by a linker script. We need |
596 | this in case some dynamic object refers to this symbol. */ | |
597 | ||
598 | bfd_boolean | |
fe21a8fc L |
599 | bfd_elf_record_link_assignment (bfd *output_bfd, |
600 | struct bfd_link_info *info, | |
268b6b39 | 601 | const char *name, |
fe21a8fc L |
602 | bfd_boolean provide, |
603 | bfd_boolean hidden) | |
45d6a902 | 604 | { |
00cbee0a | 605 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 606 | struct elf_link_hash_table *htab; |
00cbee0a | 607 | const struct elf_backend_data *bed; |
45d6a902 | 608 | |
0eddce27 | 609 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
610 | return TRUE; |
611 | ||
4ea42fb7 AM |
612 | htab = elf_hash_table (info); |
613 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 614 | if (h == NULL) |
4ea42fb7 | 615 | return provide; |
45d6a902 | 616 | |
8e2a4f11 AM |
617 | if (h->root.type == bfd_link_hash_warning) |
618 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
619 | ||
0f550b3d L |
620 | if (h->versioned == unknown) |
621 | { | |
622 | /* Set versioned if symbol version is unknown. */ | |
623 | char *version = strrchr (name, ELF_VER_CHR); | |
624 | if (version) | |
625 | { | |
626 | if (version > name && version[-1] != ELF_VER_CHR) | |
627 | h->versioned = versioned_hidden; | |
628 | else | |
629 | h->versioned = versioned; | |
630 | } | |
631 | } | |
632 | ||
73ec947d AM |
633 | /* Symbols defined in a linker script but not referenced anywhere |
634 | else will have non_elf set. */ | |
635 | if (h->non_elf) | |
636 | { | |
637 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); | |
638 | h->non_elf = 0; | |
639 | } | |
640 | ||
00cbee0a | 641 | switch (h->root.type) |
77cfaee6 | 642 | { |
00cbee0a L |
643 | case bfd_link_hash_defined: |
644 | case bfd_link_hash_defweak: | |
645 | case bfd_link_hash_common: | |
646 | break; | |
647 | case bfd_link_hash_undefweak: | |
648 | case bfd_link_hash_undefined: | |
649 | /* Since we're defining the symbol, don't let it seem to have not | |
650 | been defined. record_dynamic_symbol and size_dynamic_sections | |
651 | may depend on this. */ | |
4ea42fb7 | 652 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
653 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
654 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
655 | break; |
656 | case bfd_link_hash_new: | |
00cbee0a L |
657 | break; |
658 | case bfd_link_hash_indirect: | |
659 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 660 | the versioned symbol point to this one. */ |
00cbee0a L |
661 | bed = get_elf_backend_data (output_bfd); |
662 | hv = h; | |
663 | while (hv->root.type == bfd_link_hash_indirect | |
664 | || hv->root.type == bfd_link_hash_warning) | |
665 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
666 | /* We don't need to update h->root.u since linker will set them | |
667 | later. */ | |
668 | h->root.type = bfd_link_hash_undefined; | |
669 | hv->root.type = bfd_link_hash_indirect; | |
670 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
671 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
672 | break; | |
8e2a4f11 AM |
673 | default: |
674 | BFD_FAIL (); | |
c2596ca5 | 675 | return FALSE; |
55255dae | 676 | } |
45d6a902 AM |
677 | |
678 | /* If this symbol is being provided by the linker script, and it is | |
679 | currently defined by a dynamic object, but not by a regular | |
680 | object, then mark it as undefined so that the generic linker will | |
681 | force the correct value. */ | |
682 | if (provide | |
f5385ebf AM |
683 | && h->def_dynamic |
684 | && !h->def_regular) | |
45d6a902 AM |
685 | h->root.type = bfd_link_hash_undefined; |
686 | ||
48e30f52 L |
687 | /* If this symbol is currently defined by a dynamic object, but not |
688 | by a regular object, then clear out any version information because | |
689 | the symbol will not be associated with the dynamic object any | |
690 | more. */ | |
691 | if (h->def_dynamic && !h->def_regular) | |
b531344c MR |
692 | h->verinfo.verdef = NULL; |
693 | ||
694 | /* Make sure this symbol is not garbage collected. */ | |
695 | h->mark = 1; | |
45d6a902 | 696 | |
f5385ebf | 697 | h->def_regular = 1; |
45d6a902 | 698 | |
eb8476a6 | 699 | if (hidden) |
fe21a8fc | 700 | { |
91d6fa6a | 701 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
702 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
703 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
fe21a8fc L |
704 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
705 | } | |
706 | ||
6fa3860b PB |
707 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
708 | and executables. */ | |
0e1862bb | 709 | if (!bfd_link_relocatable (info) |
6fa3860b PB |
710 | && h->dynindx != -1 |
711 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
712 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
713 | h->forced_local = 1; | |
714 | ||
f5385ebf AM |
715 | if ((h->def_dynamic |
716 | || h->ref_dynamic | |
6b3b0ab8 L |
717 | || bfd_link_dll (info) |
718 | || elf_hash_table (info)->is_relocatable_executable) | |
34a87bb0 | 719 | && !h->forced_local |
45d6a902 AM |
720 | && h->dynindx == -1) |
721 | { | |
c152c796 | 722 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
723 | return FALSE; |
724 | ||
725 | /* If this is a weak defined symbol, and we know a corresponding | |
726 | real symbol from the same dynamic object, make sure the real | |
727 | symbol is also made into a dynamic symbol. */ | |
60d67dc8 | 728 | if (h->is_weakalias) |
45d6a902 | 729 | { |
60d67dc8 AM |
730 | struct elf_link_hash_entry *def = weakdef (h); |
731 | ||
732 | if (def->dynindx == -1 | |
733 | && !bfd_elf_link_record_dynamic_symbol (info, def)) | |
45d6a902 AM |
734 | return FALSE; |
735 | } | |
736 | } | |
737 | ||
738 | return TRUE; | |
739 | } | |
42751cf3 | 740 | |
8c58d23b AM |
741 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
742 | success, and 2 on a failure caused by attempting to record a symbol | |
743 | in a discarded section, eg. a discarded link-once section symbol. */ | |
744 | ||
745 | int | |
c152c796 AM |
746 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
747 | bfd *input_bfd, | |
748 | long input_indx) | |
8c58d23b | 749 | { |
986f0783 | 750 | size_t amt; |
8c58d23b AM |
751 | struct elf_link_local_dynamic_entry *entry; |
752 | struct elf_link_hash_table *eht; | |
753 | struct elf_strtab_hash *dynstr; | |
ef53be89 | 754 | size_t dynstr_index; |
8c58d23b AM |
755 | char *name; |
756 | Elf_External_Sym_Shndx eshndx; | |
757 | char esym[sizeof (Elf64_External_Sym)]; | |
758 | ||
0eddce27 | 759 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
760 | return 0; |
761 | ||
762 | /* See if the entry exists already. */ | |
763 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
764 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
765 | return 1; | |
766 | ||
767 | amt = sizeof (*entry); | |
a50b1753 | 768 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
769 | if (entry == NULL) |
770 | return 0; | |
771 | ||
772 | /* Go find the symbol, so that we can find it's name. */ | |
773 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 774 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
775 | { |
776 | bfd_release (input_bfd, entry); | |
777 | return 0; | |
778 | } | |
779 | ||
780 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 781 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
782 | { |
783 | asection *s; | |
784 | ||
785 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
786 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
787 | { | |
788 | /* We can still bfd_release here as nothing has done another | |
789 | bfd_alloc. We can't do this later in this function. */ | |
790 | bfd_release (input_bfd, entry); | |
791 | return 2; | |
792 | } | |
793 | } | |
794 | ||
795 | name = (bfd_elf_string_from_elf_section | |
796 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
797 | entry->isym.st_name)); | |
798 | ||
799 | dynstr = elf_hash_table (info)->dynstr; | |
800 | if (dynstr == NULL) | |
801 | { | |
802 | /* Create a strtab to hold the dynamic symbol names. */ | |
803 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
804 | if (dynstr == NULL) | |
805 | return 0; | |
806 | } | |
807 | ||
b34976b6 | 808 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
ef53be89 | 809 | if (dynstr_index == (size_t) -1) |
8c58d23b AM |
810 | return 0; |
811 | entry->isym.st_name = dynstr_index; | |
812 | ||
813 | eht = elf_hash_table (info); | |
814 | ||
815 | entry->next = eht->dynlocal; | |
816 | eht->dynlocal = entry; | |
817 | entry->input_bfd = input_bfd; | |
818 | entry->input_indx = input_indx; | |
819 | eht->dynsymcount++; | |
820 | ||
821 | /* Whatever binding the symbol had before, it's now local. */ | |
822 | entry->isym.st_info | |
823 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
824 | ||
825 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
826 | ||
827 | return 1; | |
828 | } | |
829 | ||
30b30c21 | 830 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 831 | |
30b30c21 | 832 | long |
268b6b39 AM |
833 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
834 | bfd *input_bfd, | |
835 | long input_indx) | |
30b30c21 RH |
836 | { |
837 | struct elf_link_local_dynamic_entry *e; | |
838 | ||
839 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
840 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
841 | return e->dynindx; | |
842 | return -1; | |
843 | } | |
844 | ||
845 | /* This function is used to renumber the dynamic symbols, if some of | |
846 | them are removed because they are marked as local. This is called | |
847 | via elf_link_hash_traverse. */ | |
848 | ||
b34976b6 | 849 | static bfd_boolean |
268b6b39 AM |
850 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
851 | void *data) | |
42751cf3 | 852 | { |
a50b1753 | 853 | size_t *count = (size_t *) data; |
30b30c21 | 854 | |
6fa3860b PB |
855 | if (h->forced_local) |
856 | return TRUE; | |
857 | ||
858 | if (h->dynindx != -1) | |
859 | h->dynindx = ++(*count); | |
860 | ||
861 | return TRUE; | |
862 | } | |
863 | ||
864 | ||
865 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
866 | STB_LOCAL binding. */ | |
867 | ||
868 | static bfd_boolean | |
869 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
870 | void *data) | |
871 | { | |
a50b1753 | 872 | size_t *count = (size_t *) data; |
6fa3860b | 873 | |
6fa3860b PB |
874 | if (!h->forced_local) |
875 | return TRUE; | |
876 | ||
42751cf3 | 877 | if (h->dynindx != -1) |
30b30c21 RH |
878 | h->dynindx = ++(*count); |
879 | ||
b34976b6 | 880 | return TRUE; |
42751cf3 | 881 | } |
30b30c21 | 882 | |
aee6f5b4 AO |
883 | /* Return true if the dynamic symbol for a given section should be |
884 | omitted when creating a shared library. */ | |
885 | bfd_boolean | |
d00dd7dc AM |
886 | _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED, |
887 | struct bfd_link_info *info, | |
888 | asection *p) | |
aee6f5b4 | 889 | { |
74541ad4 | 890 | struct elf_link_hash_table *htab; |
ca55926c | 891 | asection *ip; |
74541ad4 | 892 | |
aee6f5b4 AO |
893 | switch (elf_section_data (p)->this_hdr.sh_type) |
894 | { | |
895 | case SHT_PROGBITS: | |
896 | case SHT_NOBITS: | |
897 | /* If sh_type is yet undecided, assume it could be | |
898 | SHT_PROGBITS/SHT_NOBITS. */ | |
899 | case SHT_NULL: | |
74541ad4 | 900 | htab = elf_hash_table (info); |
74541ad4 AM |
901 | if (htab->text_index_section != NULL) |
902 | return p != htab->text_index_section && p != htab->data_index_section; | |
903 | ||
ca55926c | 904 | return (htab->dynobj != NULL |
3d4d4302 | 905 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 906 | && ip->output_section == p); |
aee6f5b4 AO |
907 | |
908 | /* There shouldn't be section relative relocations | |
909 | against any other section. */ | |
910 | default: | |
911 | return TRUE; | |
912 | } | |
913 | } | |
914 | ||
d00dd7dc AM |
915 | bfd_boolean |
916 | _bfd_elf_omit_section_dynsym_all | |
917 | (bfd *output_bfd ATTRIBUTE_UNUSED, | |
918 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
919 | asection *p ATTRIBUTE_UNUSED) | |
920 | { | |
921 | return TRUE; | |
922 | } | |
923 | ||
062e2358 | 924 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
925 | symbol for each output section, which come first. Next come symbols |
926 | which have been forced to local binding. Then all of the back-end | |
927 | allocated local dynamic syms, followed by the rest of the global | |
63f452a8 AM |
928 | symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. |
929 | (This prevents the early call before elf_backend_init_index_section | |
930 | and strip_excluded_output_sections setting dynindx for sections | |
931 | that are stripped.) */ | |
30b30c21 | 932 | |
554220db AM |
933 | static unsigned long |
934 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
935 | struct bfd_link_info *info, | |
936 | unsigned long *section_sym_count) | |
30b30c21 RH |
937 | { |
938 | unsigned long dynsymcount = 0; | |
63f452a8 | 939 | bfd_boolean do_sec = section_sym_count != NULL; |
30b30c21 | 940 | |
0e1862bb L |
941 | if (bfd_link_pic (info) |
942 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 943 | { |
aee6f5b4 | 944 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
945 | asection *p; |
946 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 947 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 | 948 | && (p->flags & SEC_ALLOC) != 0 |
7f923b7f | 949 | && elf_hash_table (info)->dynamic_relocs |
aee6f5b4 | 950 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) |
63f452a8 AM |
951 | { |
952 | ++dynsymcount; | |
953 | if (do_sec) | |
954 | elf_section_data (p)->dynindx = dynsymcount; | |
955 | } | |
956 | else if (do_sec) | |
74541ad4 | 957 | elf_section_data (p)->dynindx = 0; |
30b30c21 | 958 | } |
63f452a8 AM |
959 | if (do_sec) |
960 | *section_sym_count = dynsymcount; | |
30b30c21 | 961 | |
6fa3860b PB |
962 | elf_link_hash_traverse (elf_hash_table (info), |
963 | elf_link_renumber_local_hash_table_dynsyms, | |
964 | &dynsymcount); | |
965 | ||
30b30c21 RH |
966 | if (elf_hash_table (info)->dynlocal) |
967 | { | |
968 | struct elf_link_local_dynamic_entry *p; | |
969 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
970 | p->dynindx = ++dynsymcount; | |
971 | } | |
90ac2420 | 972 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
973 | |
974 | elf_link_hash_traverse (elf_hash_table (info), | |
975 | elf_link_renumber_hash_table_dynsyms, | |
976 | &dynsymcount); | |
977 | ||
d5486c43 L |
978 | /* There is an unused NULL entry at the head of the table which we |
979 | must account for in our count even if the table is empty since it | |
980 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
981 | .dynamic section. */ | |
982 | dynsymcount++; | |
30b30c21 | 983 | |
ccabcbe5 AM |
984 | elf_hash_table (info)->dynsymcount = dynsymcount; |
985 | return dynsymcount; | |
30b30c21 | 986 | } |
252b5132 | 987 | |
54ac0771 L |
988 | /* Merge st_other field. */ |
989 | ||
990 | static void | |
991 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 992 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 993 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
994 | { |
995 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
996 | ||
997 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 998 | code might be needed here. */ |
54ac0771 L |
999 | if (bed->elf_backend_merge_symbol_attribute) |
1000 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
1001 | dynamic); | |
1002 | ||
cd3416da | 1003 | if (!dynamic) |
54ac0771 | 1004 | { |
cd3416da AM |
1005 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
1006 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 1007 | |
cd3416da AM |
1008 | /* Keep the most constraining visibility. Leave the remainder |
1009 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
1010 | if (symvis - 1 < hvis - 1) | |
1011 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 1012 | } |
b8417128 AM |
1013 | else if (definition |
1014 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
1015 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 1016 | h->protected_def = 1; |
54ac0771 L |
1017 | } |
1018 | ||
4f3fedcf AM |
1019 | /* This function is called when we want to merge a new symbol with an |
1020 | existing symbol. It handles the various cases which arise when we | |
1021 | find a definition in a dynamic object, or when there is already a | |
1022 | definition in a dynamic object. The new symbol is described by | |
1023 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
1024 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
1025 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
1026 | of an old common symbol. We set OVERRIDE if the old symbol is | |
1027 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
1028 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
1029 | to change. By OK to change, we mean that we shouldn't warn if the | |
1030 | type or size does change. */ | |
45d6a902 | 1031 | |
8a56bd02 | 1032 | static bfd_boolean |
268b6b39 AM |
1033 | _bfd_elf_merge_symbol (bfd *abfd, |
1034 | struct bfd_link_info *info, | |
1035 | const char *name, | |
1036 | Elf_Internal_Sym *sym, | |
1037 | asection **psec, | |
1038 | bfd_vma *pvalue, | |
4f3fedcf AM |
1039 | struct elf_link_hash_entry **sym_hash, |
1040 | bfd **poldbfd, | |
37a9e49a | 1041 | bfd_boolean *pold_weak, |
af44c138 | 1042 | unsigned int *pold_alignment, |
268b6b39 AM |
1043 | bfd_boolean *skip, |
1044 | bfd_boolean *override, | |
1045 | bfd_boolean *type_change_ok, | |
6e33951e L |
1046 | bfd_boolean *size_change_ok, |
1047 | bfd_boolean *matched) | |
252b5132 | 1048 | { |
7479dfd4 | 1049 | asection *sec, *oldsec; |
45d6a902 | 1050 | struct elf_link_hash_entry *h; |
90c984fc | 1051 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
1052 | struct elf_link_hash_entry *flip; |
1053 | int bind; | |
1054 | bfd *oldbfd; | |
1055 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 1056 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 1057 | const struct elf_backend_data *bed; |
6e33951e | 1058 | char *new_version; |
93f4de39 | 1059 | bfd_boolean default_sym = *matched; |
45d6a902 AM |
1060 | |
1061 | *skip = FALSE; | |
1062 | *override = FALSE; | |
1063 | ||
1064 | sec = *psec; | |
1065 | bind = ELF_ST_BIND (sym->st_info); | |
1066 | ||
1067 | if (! bfd_is_und_section (sec)) | |
1068 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1069 | else | |
1070 | h = ((struct elf_link_hash_entry *) | |
1071 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1072 | if (h == NULL) | |
1073 | return FALSE; | |
1074 | *sym_hash = h; | |
252b5132 | 1075 | |
88ba32a0 L |
1076 | bed = get_elf_backend_data (abfd); |
1077 | ||
6e33951e | 1078 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1079 | if (h->versioned != unversioned) |
6e33951e | 1080 | { |
422f1182 L |
1081 | /* Symbol version is unknown or versioned. */ |
1082 | new_version = strrchr (name, ELF_VER_CHR); | |
1083 | if (new_version) | |
1084 | { | |
1085 | if (h->versioned == unknown) | |
1086 | { | |
1087 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1088 | h->versioned = versioned_hidden; | |
1089 | else | |
1090 | h->versioned = versioned; | |
1091 | } | |
1092 | new_version += 1; | |
1093 | if (new_version[0] == '\0') | |
1094 | new_version = NULL; | |
1095 | } | |
1096 | else | |
1097 | h->versioned = unversioned; | |
6e33951e | 1098 | } |
422f1182 L |
1099 | else |
1100 | new_version = NULL; | |
6e33951e | 1101 | |
90c984fc L |
1102 | /* For merging, we only care about real symbols. But we need to make |
1103 | sure that indirect symbol dynamic flags are updated. */ | |
1104 | hi = h; | |
45d6a902 AM |
1105 | while (h->root.type == bfd_link_hash_indirect |
1106 | || h->root.type == bfd_link_hash_warning) | |
1107 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1108 | ||
6e33951e L |
1109 | if (!*matched) |
1110 | { | |
1111 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1112 | *matched = TRUE; | |
1113 | else | |
1114 | { | |
ae7683d2 | 1115 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1116 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1117 | true if the new symbol is only visible to the symbol with |
6e33951e | 1118 | the same symbol version. */ |
422f1182 L |
1119 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1120 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1121 | if (!old_hidden && !new_hidden) |
1122 | /* The new symbol matches the existing symbol if both | |
1123 | aren't hidden. */ | |
1124 | *matched = TRUE; | |
1125 | else | |
1126 | { | |
1127 | /* OLD_VERSION is the symbol version of the existing | |
1128 | symbol. */ | |
422f1182 L |
1129 | char *old_version; |
1130 | ||
1131 | if (h->versioned >= versioned) | |
1132 | old_version = strrchr (h->root.root.string, | |
1133 | ELF_VER_CHR) + 1; | |
1134 | else | |
1135 | old_version = NULL; | |
6e33951e L |
1136 | |
1137 | /* The new symbol matches the existing symbol if they | |
1138 | have the same symbol version. */ | |
1139 | *matched = (old_version == new_version | |
1140 | || (old_version != NULL | |
1141 | && new_version != NULL | |
1142 | && strcmp (old_version, new_version) == 0)); | |
1143 | } | |
1144 | } | |
1145 | } | |
1146 | ||
934bce08 AM |
1147 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1148 | existing symbol. */ | |
1149 | ||
1150 | oldbfd = NULL; | |
1151 | oldsec = NULL; | |
1152 | switch (h->root.type) | |
1153 | { | |
1154 | default: | |
1155 | break; | |
1156 | ||
1157 | case bfd_link_hash_undefined: | |
1158 | case bfd_link_hash_undefweak: | |
1159 | oldbfd = h->root.u.undef.abfd; | |
1160 | break; | |
1161 | ||
1162 | case bfd_link_hash_defined: | |
1163 | case bfd_link_hash_defweak: | |
1164 | oldbfd = h->root.u.def.section->owner; | |
1165 | oldsec = h->root.u.def.section; | |
1166 | break; | |
1167 | ||
1168 | case bfd_link_hash_common: | |
1169 | oldbfd = h->root.u.c.p->section->owner; | |
1170 | oldsec = h->root.u.c.p->section; | |
1171 | if (pold_alignment) | |
1172 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1173 | break; | |
1174 | } | |
1175 | if (poldbfd && *poldbfd == NULL) | |
1176 | *poldbfd = oldbfd; | |
1177 | ||
1178 | /* Differentiate strong and weak symbols. */ | |
1179 | newweak = bind == STB_WEAK; | |
1180 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1181 | || h->root.type == bfd_link_hash_undefweak); | |
1182 | if (pold_weak) | |
1183 | *pold_weak = oldweak; | |
1184 | ||
40b36307 | 1185 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1186 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1187 | symbols. */ |
1188 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1189 | ||
ee659f1f AM |
1190 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1191 | respectively, is from a dynamic object. */ | |
1192 | ||
1193 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1194 | ||
1195 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1196 | syms and defined syms in dynamic libraries respectively. | |
1197 | ref_dynamic on the other hand can be set for a symbol defined in | |
1198 | a dynamic library, and def_dynamic may not be set; When the | |
1199 | definition in a dynamic lib is overridden by a definition in the | |
1200 | executable use of the symbol in the dynamic lib becomes a | |
1201 | reference to the executable symbol. */ | |
1202 | if (newdyn) | |
1203 | { | |
1204 | if (bfd_is_und_section (sec)) | |
1205 | { | |
1206 | if (bind != STB_WEAK) | |
1207 | { | |
1208 | h->ref_dynamic_nonweak = 1; | |
1209 | hi->ref_dynamic_nonweak = 1; | |
1210 | } | |
1211 | } | |
1212 | else | |
1213 | { | |
6e33951e L |
1214 | /* Update the existing symbol only if they match. */ |
1215 | if (*matched) | |
1216 | h->dynamic_def = 1; | |
ee659f1f AM |
1217 | hi->dynamic_def = 1; |
1218 | } | |
1219 | } | |
1220 | ||
45d6a902 AM |
1221 | /* If we just created the symbol, mark it as being an ELF symbol. |
1222 | Other than that, there is nothing to do--there is no merge issue | |
1223 | with a newly defined symbol--so we just return. */ | |
1224 | ||
1225 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1226 | { |
f5385ebf | 1227 | h->non_elf = 0; |
45d6a902 AM |
1228 | return TRUE; |
1229 | } | |
252b5132 | 1230 | |
45d6a902 AM |
1231 | /* In cases involving weak versioned symbols, we may wind up trying |
1232 | to merge a symbol with itself. Catch that here, to avoid the | |
1233 | confusion that results if we try to override a symbol with | |
1234 | itself. The additional tests catch cases like | |
1235 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1236 | dynamic object, which we do want to handle here. */ | |
1237 | if (abfd == oldbfd | |
895fa45f | 1238 | && (newweak || oldweak) |
45d6a902 | 1239 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1240 | || !h->def_regular)) |
45d6a902 AM |
1241 | return TRUE; |
1242 | ||
707bba77 | 1243 | olddyn = FALSE; |
45d6a902 AM |
1244 | if (oldbfd != NULL) |
1245 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1246 | else if (oldsec != NULL) |
45d6a902 | 1247 | { |
707bba77 | 1248 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1249 | indices used by MIPS ELF. */ |
707bba77 | 1250 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1251 | } |
252b5132 | 1252 | |
1a3b5c34 AM |
1253 | /* Handle a case where plugin_notice won't be called and thus won't |
1254 | set the non_ir_ref flags on the first pass over symbols. */ | |
1255 | if (oldbfd != NULL | |
1256 | && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN) | |
1257 | && newdyn != olddyn) | |
1258 | { | |
1259 | h->root.non_ir_ref_dynamic = TRUE; | |
1260 | hi->root.non_ir_ref_dynamic = TRUE; | |
1261 | } | |
1262 | ||
45d6a902 AM |
1263 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1264 | respectively, appear to be a definition rather than reference. */ | |
1265 | ||
707bba77 | 1266 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1267 | |
707bba77 AM |
1268 | olddef = (h->root.type != bfd_link_hash_undefined |
1269 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1270 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1271 | |
0a36a439 L |
1272 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1273 | respectively, appear to be a function. */ | |
1274 | ||
1275 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1276 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1277 | ||
1278 | oldfunc = (h->type != STT_NOTYPE | |
1279 | && bed->is_function_type (h->type)); | |
1280 | ||
c5d37467 | 1281 | if (!(newfunc && oldfunc) |
5b677558 AM |
1282 | && ELF_ST_TYPE (sym->st_info) != h->type |
1283 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1284 | && h->type != STT_NOTYPE | |
c5d37467 AM |
1285 | && (newdef || bfd_is_com_section (sec)) |
1286 | && (olddef || h->root.type == bfd_link_hash_common)) | |
580a2b6e | 1287 | { |
c5d37467 AM |
1288 | /* If creating a default indirect symbol ("foo" or "foo@") from |
1289 | a dynamic versioned definition ("foo@@") skip doing so if | |
1290 | there is an existing regular definition with a different | |
1291 | type. We don't want, for example, a "time" variable in the | |
1292 | executable overriding a "time" function in a shared library. */ | |
1293 | if (newdyn | |
1294 | && !olddyn) | |
1295 | { | |
1296 | *skip = TRUE; | |
1297 | return TRUE; | |
1298 | } | |
1299 | ||
1300 | /* When adding a symbol from a regular object file after we have | |
1301 | created indirect symbols, undo the indirection and any | |
1302 | dynamic state. */ | |
1303 | if (hi != h | |
1304 | && !newdyn | |
1305 | && olddyn) | |
1306 | { | |
1307 | h = hi; | |
1308 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1309 | h->forced_local = 0; | |
1310 | h->ref_dynamic = 0; | |
1311 | h->def_dynamic = 0; | |
1312 | h->dynamic_def = 0; | |
1313 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1314 | { | |
1315 | h->root.type = bfd_link_hash_undefined; | |
1316 | h->root.u.undef.abfd = abfd; | |
1317 | } | |
1318 | else | |
1319 | { | |
1320 | h->root.type = bfd_link_hash_new; | |
1321 | h->root.u.undef.abfd = NULL; | |
1322 | } | |
1323 | return TRUE; | |
1324 | } | |
580a2b6e L |
1325 | } |
1326 | ||
4c34aff8 AM |
1327 | /* Check TLS symbols. We don't check undefined symbols introduced |
1328 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1329 | check symbols from plugins because they also have no type. */ | |
1330 | if (oldbfd != NULL | |
1331 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1332 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1333 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1334 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1335 | { |
1336 | bfd *ntbfd, *tbfd; | |
1337 | bfd_boolean ntdef, tdef; | |
1338 | asection *ntsec, *tsec; | |
1339 | ||
1340 | if (h->type == STT_TLS) | |
1341 | { | |
3b36f7e6 | 1342 | ntbfd = abfd; |
7479dfd4 L |
1343 | ntsec = sec; |
1344 | ntdef = newdef; | |
1345 | tbfd = oldbfd; | |
1346 | tsec = oldsec; | |
1347 | tdef = olddef; | |
1348 | } | |
1349 | else | |
1350 | { | |
1351 | ntbfd = oldbfd; | |
1352 | ntsec = oldsec; | |
1353 | ntdef = olddef; | |
1354 | tbfd = abfd; | |
1355 | tsec = sec; | |
1356 | tdef = newdef; | |
1357 | } | |
1358 | ||
1359 | if (tdef && ntdef) | |
4eca0228 | 1360 | _bfd_error_handler |
695344c0 | 1361 | /* xgettext:c-format */ |
871b3ab2 AM |
1362 | (_("%s: TLS definition in %pB section %pA " |
1363 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1364 | h->root.root.string, tbfd, tsec, ntbfd, ntsec); |
7479dfd4 | 1365 | else if (!tdef && !ntdef) |
4eca0228 | 1366 | _bfd_error_handler |
695344c0 | 1367 | /* xgettext:c-format */ |
871b3ab2 AM |
1368 | (_("%s: TLS reference in %pB " |
1369 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1370 | h->root.root.string, tbfd, ntbfd); |
7479dfd4 | 1371 | else if (tdef) |
4eca0228 | 1372 | _bfd_error_handler |
695344c0 | 1373 | /* xgettext:c-format */ |
871b3ab2 AM |
1374 | (_("%s: TLS definition in %pB section %pA " |
1375 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1376 | h->root.root.string, tbfd, tsec, ntbfd); |
7479dfd4 | 1377 | else |
4eca0228 | 1378 | _bfd_error_handler |
695344c0 | 1379 | /* xgettext:c-format */ |
871b3ab2 AM |
1380 | (_("%s: TLS reference in %pB " |
1381 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1382 | h->root.root.string, tbfd, ntbfd, ntsec); |
7479dfd4 L |
1383 | |
1384 | bfd_set_error (bfd_error_bad_value); | |
1385 | return FALSE; | |
1386 | } | |
1387 | ||
45d6a902 AM |
1388 | /* If the old symbol has non-default visibility, we ignore the new |
1389 | definition from a dynamic object. */ | |
1390 | if (newdyn | |
9c7a29a3 | 1391 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1392 | && !bfd_is_und_section (sec)) |
1393 | { | |
1394 | *skip = TRUE; | |
1395 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1396 | h->ref_dynamic = 1; |
90c984fc | 1397 | hi->ref_dynamic = 1; |
45d6a902 AM |
1398 | /* A protected symbol has external availability. Make sure it is |
1399 | recorded as dynamic. | |
1400 | ||
1401 | FIXME: Should we check type and size for protected symbol? */ | |
1402 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1403 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1404 | else |
1405 | return TRUE; | |
1406 | } | |
1407 | else if (!newdyn | |
9c7a29a3 | 1408 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1409 | && h->def_dynamic) |
45d6a902 AM |
1410 | { |
1411 | /* If the new symbol with non-default visibility comes from a | |
1412 | relocatable file and the old definition comes from a dynamic | |
1413 | object, we remove the old definition. */ | |
6c9b78e6 | 1414 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1415 | { |
1416 | /* Handle the case where the old dynamic definition is | |
1417 | default versioned. We need to copy the symbol info from | |
1418 | the symbol with default version to the normal one if it | |
1419 | was referenced before. */ | |
1420 | if (h->ref_regular) | |
1421 | { | |
6c9b78e6 | 1422 | hi->root.type = h->root.type; |
d2dee3b2 | 1423 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1424 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1425 | |
6c9b78e6 | 1426 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1427 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1428 | { |
aed81c4e MR |
1429 | /* If the new symbol is hidden or internal, completely undo |
1430 | any dynamic link state. */ | |
1431 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1432 | h->forced_local = 0; | |
1433 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1434 | } |
1435 | else | |
aed81c4e MR |
1436 | h->ref_dynamic = 1; |
1437 | ||
1438 | h->def_dynamic = 0; | |
aed81c4e MR |
1439 | /* FIXME: Should we check type and size for protected symbol? */ |
1440 | h->size = 0; | |
1441 | h->type = 0; | |
1442 | ||
6c9b78e6 | 1443 | h = hi; |
d2dee3b2 L |
1444 | } |
1445 | else | |
6c9b78e6 | 1446 | h = hi; |
d2dee3b2 | 1447 | } |
1de1a317 | 1448 | |
f5eda473 AM |
1449 | /* If the old symbol was undefined before, then it will still be |
1450 | on the undefs list. If the new symbol is undefined or | |
1451 | common, we can't make it bfd_link_hash_new here, because new | |
1452 | undefined or common symbols will be added to the undefs list | |
1453 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1454 | added twice to the undefs list. Also, if the new symbol is | |
1455 | undefweak then we don't want to lose the strong undef. */ | |
1456 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1457 | { |
1de1a317 | 1458 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1459 | h->root.u.undef.abfd = abfd; |
1460 | } | |
1461 | else | |
1462 | { | |
1463 | h->root.type = bfd_link_hash_new; | |
1464 | h->root.u.undef.abfd = NULL; | |
1465 | } | |
1466 | ||
f5eda473 | 1467 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1468 | { |
f5eda473 AM |
1469 | /* If the new symbol is hidden or internal, completely undo |
1470 | any dynamic link state. */ | |
1471 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1472 | h->forced_local = 0; | |
1473 | h->ref_dynamic = 0; | |
45d6a902 | 1474 | } |
f5eda473 AM |
1475 | else |
1476 | h->ref_dynamic = 1; | |
1477 | h->def_dynamic = 0; | |
45d6a902 AM |
1478 | /* FIXME: Should we check type and size for protected symbol? */ |
1479 | h->size = 0; | |
1480 | h->type = 0; | |
1481 | return TRUE; | |
1482 | } | |
14a793b2 | 1483 | |
15b43f48 AM |
1484 | /* If a new weak symbol definition comes from a regular file and the |
1485 | old symbol comes from a dynamic library, we treat the new one as | |
1486 | strong. Similarly, an old weak symbol definition from a regular | |
1487 | file is treated as strong when the new symbol comes from a dynamic | |
1488 | library. Further, an old weak symbol from a dynamic library is | |
1489 | treated as strong if the new symbol is from a dynamic library. | |
1490 | This reflects the way glibc's ld.so works. | |
1491 | ||
165f707a AM |
1492 | Also allow a weak symbol to override a linker script symbol |
1493 | defined by an early pass over the script. This is done so the | |
1494 | linker knows the symbol is defined in an object file, for the | |
1495 | DEFINED script function. | |
1496 | ||
15b43f48 AM |
1497 | Do this before setting *type_change_ok or *size_change_ok so that |
1498 | we warn properly when dynamic library symbols are overridden. */ | |
1499 | ||
165f707a | 1500 | if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) |
0f8a2703 | 1501 | newweak = FALSE; |
15b43f48 | 1502 | if (olddef && newdyn) |
0f8a2703 AM |
1503 | oldweak = FALSE; |
1504 | ||
d334575b | 1505 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1506 | if (newfunc && oldfunc) |
fcb93ecf PB |
1507 | *type_change_ok = TRUE; |
1508 | ||
79349b09 AM |
1509 | /* It's OK to change the type if either the existing symbol or the |
1510 | new symbol is weak. A type change is also OK if the old symbol | |
1511 | is undefined and the new symbol is defined. */ | |
252b5132 | 1512 | |
79349b09 AM |
1513 | if (oldweak |
1514 | || newweak | |
1515 | || (newdef | |
1516 | && h->root.type == bfd_link_hash_undefined)) | |
1517 | *type_change_ok = TRUE; | |
1518 | ||
1519 | /* It's OK to change the size if either the existing symbol or the | |
1520 | new symbol is weak, or if the old symbol is undefined. */ | |
1521 | ||
1522 | if (*type_change_ok | |
1523 | || h->root.type == bfd_link_hash_undefined) | |
1524 | *size_change_ok = TRUE; | |
45d6a902 | 1525 | |
45d6a902 AM |
1526 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1527 | symbol, respectively, appears to be a common symbol in a dynamic | |
1528 | object. If a symbol appears in an uninitialized section, and is | |
1529 | not weak, and is not a function, then it may be a common symbol | |
1530 | which was resolved when the dynamic object was created. We want | |
1531 | to treat such symbols specially, because they raise special | |
1532 | considerations when setting the symbol size: if the symbol | |
1533 | appears as a common symbol in a regular object, and the size in | |
1534 | the regular object is larger, we must make sure that we use the | |
1535 | larger size. This problematic case can always be avoided in C, | |
1536 | but it must be handled correctly when using Fortran shared | |
1537 | libraries. | |
1538 | ||
1539 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1540 | likewise for OLDDYNCOMMON and OLDDEF. | |
1541 | ||
1542 | Note that this test is just a heuristic, and that it is quite | |
1543 | possible to have an uninitialized symbol in a shared object which | |
1544 | is really a definition, rather than a common symbol. This could | |
1545 | lead to some minor confusion when the symbol really is a common | |
1546 | symbol in some regular object. However, I think it will be | |
1547 | harmless. */ | |
1548 | ||
1549 | if (newdyn | |
1550 | && newdef | |
79349b09 | 1551 | && !newweak |
45d6a902 AM |
1552 | && (sec->flags & SEC_ALLOC) != 0 |
1553 | && (sec->flags & SEC_LOAD) == 0 | |
1554 | && sym->st_size > 0 | |
0a36a439 | 1555 | && !newfunc) |
45d6a902 AM |
1556 | newdyncommon = TRUE; |
1557 | else | |
1558 | newdyncommon = FALSE; | |
1559 | ||
1560 | if (olddyn | |
1561 | && olddef | |
1562 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1563 | && h->def_dynamic |
45d6a902 AM |
1564 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1565 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1566 | && h->size > 0 | |
0a36a439 | 1567 | && !oldfunc) |
45d6a902 AM |
1568 | olddyncommon = TRUE; |
1569 | else | |
1570 | olddyncommon = FALSE; | |
1571 | ||
a4d8e49b L |
1572 | /* We now know everything about the old and new symbols. We ask the |
1573 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1574 | if (bed->merge_symbol != NULL) |
1575 | { | |
1576 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1577 | return FALSE; | |
1578 | sec = *psec; | |
1579 | } | |
a4d8e49b | 1580 | |
a83ef4d1 L |
1581 | /* There are multiple definitions of a normal symbol. Skip the |
1582 | default symbol as well as definition from an IR object. */ | |
93f4de39 | 1583 | if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak |
a83ef4d1 L |
1584 | && !default_sym && h->def_regular |
1585 | && !(oldbfd != NULL | |
1586 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
1587 | && (abfd->flags & BFD_PLUGIN) == 0)) | |
93f4de39 RL |
1588 | { |
1589 | /* Handle a multiple definition. */ | |
1590 | (*info->callbacks->multiple_definition) (info, &h->root, | |
1591 | abfd, sec, *pvalue); | |
1592 | *skip = TRUE; | |
1593 | return TRUE; | |
1594 | } | |
1595 | ||
45d6a902 AM |
1596 | /* If both the old and the new symbols look like common symbols in a |
1597 | dynamic object, set the size of the symbol to the larger of the | |
1598 | two. */ | |
1599 | ||
1600 | if (olddyncommon | |
1601 | && newdyncommon | |
1602 | && sym->st_size != h->size) | |
1603 | { | |
1604 | /* Since we think we have two common symbols, issue a multiple | |
1605 | common warning if desired. Note that we only warn if the | |
1606 | size is different. If the size is the same, we simply let | |
1607 | the old symbol override the new one as normally happens with | |
1608 | symbols defined in dynamic objects. */ | |
1609 | ||
1a72702b AM |
1610 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1611 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1612 | if (sym->st_size > h->size) |
1613 | h->size = sym->st_size; | |
252b5132 | 1614 | |
45d6a902 | 1615 | *size_change_ok = TRUE; |
252b5132 RH |
1616 | } |
1617 | ||
45d6a902 AM |
1618 | /* If we are looking at a dynamic object, and we have found a |
1619 | definition, we need to see if the symbol was already defined by | |
1620 | some other object. If so, we want to use the existing | |
1621 | definition, and we do not want to report a multiple symbol | |
1622 | definition error; we do this by clobbering *PSEC to be | |
1623 | bfd_und_section_ptr. | |
1624 | ||
1625 | We treat a common symbol as a definition if the symbol in the | |
1626 | shared library is a function, since common symbols always | |
1627 | represent variables; this can cause confusion in principle, but | |
1628 | any such confusion would seem to indicate an erroneous program or | |
1629 | shared library. We also permit a common symbol in a regular | |
8170f769 | 1630 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1631 | |
1632 | if (newdyn | |
1633 | && newdef | |
77cfaee6 | 1634 | && (olddef |
45d6a902 | 1635 | || (h->root.type == bfd_link_hash_common |
8170f769 | 1636 | && (newweak || newfunc)))) |
45d6a902 AM |
1637 | { |
1638 | *override = TRUE; | |
1639 | newdef = FALSE; | |
1640 | newdyncommon = FALSE; | |
252b5132 | 1641 | |
45d6a902 AM |
1642 | *psec = sec = bfd_und_section_ptr; |
1643 | *size_change_ok = TRUE; | |
252b5132 | 1644 | |
45d6a902 AM |
1645 | /* If we get here when the old symbol is a common symbol, then |
1646 | we are explicitly letting it override a weak symbol or | |
1647 | function in a dynamic object, and we don't want to warn about | |
1648 | a type change. If the old symbol is a defined symbol, a type | |
1649 | change warning may still be appropriate. */ | |
252b5132 | 1650 | |
45d6a902 AM |
1651 | if (h->root.type == bfd_link_hash_common) |
1652 | *type_change_ok = TRUE; | |
1653 | } | |
1654 | ||
1655 | /* Handle the special case of an old common symbol merging with a | |
1656 | new symbol which looks like a common symbol in a shared object. | |
1657 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1658 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1659 | right thing. */ | |
45d6a902 AM |
1660 | |
1661 | if (newdyncommon | |
1662 | && h->root.type == bfd_link_hash_common) | |
1663 | { | |
1664 | *override = TRUE; | |
1665 | newdef = FALSE; | |
1666 | newdyncommon = FALSE; | |
1667 | *pvalue = sym->st_size; | |
a4d8e49b | 1668 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1669 | *size_change_ok = TRUE; |
1670 | } | |
1671 | ||
c5e2cead | 1672 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1673 | if (newdef && olddef && newweak) |
54ac0771 | 1674 | { |
35ed3f94 | 1675 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1676 | if (!(oldbfd != NULL |
1677 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1678 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1679 | { |
1680 | newdef = FALSE; | |
1681 | *skip = TRUE; | |
1682 | } | |
54ac0771 L |
1683 | |
1684 | /* Merge st_other. If the symbol already has a dynamic index, | |
1685 | but visibility says it should not be visible, turn it into a | |
1686 | local symbol. */ | |
b8417128 | 1687 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1688 | if (h->dynindx != -1) |
1689 | switch (ELF_ST_VISIBILITY (h->other)) | |
1690 | { | |
1691 | case STV_INTERNAL: | |
1692 | case STV_HIDDEN: | |
1693 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1694 | break; | |
1695 | } | |
1696 | } | |
c5e2cead | 1697 | |
45d6a902 AM |
1698 | /* If the old symbol is from a dynamic object, and the new symbol is |
1699 | a definition which is not from a dynamic object, then the new | |
1700 | symbol overrides the old symbol. Symbols from regular files | |
1701 | always take precedence over symbols from dynamic objects, even if | |
1702 | they are defined after the dynamic object in the link. | |
1703 | ||
1704 | As above, we again permit a common symbol in a regular object to | |
1705 | override a definition in a shared object if the shared object | |
0f8a2703 | 1706 | symbol is a function or is weak. */ |
45d6a902 AM |
1707 | |
1708 | flip = NULL; | |
77cfaee6 | 1709 | if (!newdyn |
45d6a902 AM |
1710 | && (newdef |
1711 | || (bfd_is_com_section (sec) | |
0a36a439 | 1712 | && (oldweak || oldfunc))) |
45d6a902 AM |
1713 | && olddyn |
1714 | && olddef | |
f5385ebf | 1715 | && h->def_dynamic) |
45d6a902 AM |
1716 | { |
1717 | /* Change the hash table entry to undefined, and let | |
1718 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1719 | new definition. */ | |
1720 | ||
1721 | h->root.type = bfd_link_hash_undefined; | |
1722 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1723 | *size_change_ok = TRUE; | |
1724 | ||
1725 | olddef = FALSE; | |
1726 | olddyncommon = FALSE; | |
1727 | ||
1728 | /* We again permit a type change when a common symbol may be | |
1729 | overriding a function. */ | |
1730 | ||
1731 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1732 | { |
1733 | if (oldfunc) | |
1734 | { | |
1735 | /* If a common symbol overrides a function, make sure | |
1736 | that it isn't defined dynamically nor has type | |
1737 | function. */ | |
1738 | h->def_dynamic = 0; | |
1739 | h->type = STT_NOTYPE; | |
1740 | } | |
1741 | *type_change_ok = TRUE; | |
1742 | } | |
45d6a902 | 1743 | |
6c9b78e6 AM |
1744 | if (hi->root.type == bfd_link_hash_indirect) |
1745 | flip = hi; | |
45d6a902 AM |
1746 | else |
1747 | /* This union may have been set to be non-NULL when this symbol | |
1748 | was seen in a dynamic object. We must force the union to be | |
1749 | NULL, so that it is correct for a regular symbol. */ | |
1750 | h->verinfo.vertree = NULL; | |
1751 | } | |
1752 | ||
1753 | /* Handle the special case of a new common symbol merging with an | |
1754 | old symbol that looks like it might be a common symbol defined in | |
1755 | a shared object. Note that we have already handled the case in | |
1756 | which a new common symbol should simply override the definition | |
1757 | in the shared library. */ | |
1758 | ||
1759 | if (! newdyn | |
1760 | && bfd_is_com_section (sec) | |
1761 | && olddyncommon) | |
1762 | { | |
1763 | /* It would be best if we could set the hash table entry to a | |
1764 | common symbol, but we don't know what to use for the section | |
1765 | or the alignment. */ | |
1a72702b AM |
1766 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1767 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1768 | |
4cc11e76 | 1769 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1770 | larger, pretend that the new symbol has its size. */ |
1771 | ||
1772 | if (h->size > *pvalue) | |
1773 | *pvalue = h->size; | |
1774 | ||
af44c138 L |
1775 | /* We need to remember the alignment required by the symbol |
1776 | in the dynamic object. */ | |
1777 | BFD_ASSERT (pold_alignment); | |
1778 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1779 | |
1780 | olddef = FALSE; | |
1781 | olddyncommon = FALSE; | |
1782 | ||
1783 | h->root.type = bfd_link_hash_undefined; | |
1784 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1785 | ||
1786 | *size_change_ok = TRUE; | |
1787 | *type_change_ok = TRUE; | |
1788 | ||
6c9b78e6 AM |
1789 | if (hi->root.type == bfd_link_hash_indirect) |
1790 | flip = hi; | |
45d6a902 AM |
1791 | else |
1792 | h->verinfo.vertree = NULL; | |
1793 | } | |
1794 | ||
1795 | if (flip != NULL) | |
1796 | { | |
1797 | /* Handle the case where we had a versioned symbol in a dynamic | |
1798 | library and now find a definition in a normal object. In this | |
1799 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1800 | flip->root.type = h->root.type; |
00cbee0a | 1801 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1802 | h->root.type = bfd_link_hash_indirect; |
1803 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1804 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1805 | if (h->def_dynamic) |
45d6a902 | 1806 | { |
f5385ebf AM |
1807 | h->def_dynamic = 0; |
1808 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1809 | } |
1810 | } | |
1811 | ||
45d6a902 AM |
1812 | return TRUE; |
1813 | } | |
1814 | ||
1815 | /* This function is called to create an indirect symbol from the | |
1816 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1817 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1818 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1819 | |
28caa186 | 1820 | static bfd_boolean |
268b6b39 AM |
1821 | _bfd_elf_add_default_symbol (bfd *abfd, |
1822 | struct bfd_link_info *info, | |
1823 | struct elf_link_hash_entry *h, | |
1824 | const char *name, | |
1825 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1826 | asection *sec, |
1827 | bfd_vma value, | |
1828 | bfd **poldbfd, | |
e3c9d234 | 1829 | bfd_boolean *dynsym) |
45d6a902 AM |
1830 | { |
1831 | bfd_boolean type_change_ok; | |
1832 | bfd_boolean size_change_ok; | |
1833 | bfd_boolean skip; | |
1834 | char *shortname; | |
1835 | struct elf_link_hash_entry *hi; | |
1836 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1837 | const struct elf_backend_data *bed; |
45d6a902 AM |
1838 | bfd_boolean collect; |
1839 | bfd_boolean dynamic; | |
e3c9d234 | 1840 | bfd_boolean override; |
45d6a902 AM |
1841 | char *p; |
1842 | size_t len, shortlen; | |
ffd65175 | 1843 | asection *tmp_sec; |
6e33951e | 1844 | bfd_boolean matched; |
45d6a902 | 1845 | |
422f1182 L |
1846 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1847 | return TRUE; | |
1848 | ||
45d6a902 AM |
1849 | /* If this symbol has a version, and it is the default version, we |
1850 | create an indirect symbol from the default name to the fully | |
1851 | decorated name. This will cause external references which do not | |
1852 | specify a version to be bound to this version of the symbol. */ | |
1853 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1854 | if (h->versioned == unknown) |
1855 | { | |
1856 | if (p == NULL) | |
1857 | { | |
1858 | h->versioned = unversioned; | |
1859 | return TRUE; | |
1860 | } | |
1861 | else | |
1862 | { | |
1863 | if (p[1] != ELF_VER_CHR) | |
1864 | { | |
1865 | h->versioned = versioned_hidden; | |
1866 | return TRUE; | |
1867 | } | |
1868 | else | |
1869 | h->versioned = versioned; | |
1870 | } | |
1871 | } | |
4373f8af L |
1872 | else |
1873 | { | |
1874 | /* PR ld/19073: We may see an unversioned definition after the | |
1875 | default version. */ | |
1876 | if (p == NULL) | |
1877 | return TRUE; | |
1878 | } | |
45d6a902 | 1879 | |
45d6a902 AM |
1880 | bed = get_elf_backend_data (abfd); |
1881 | collect = bed->collect; | |
1882 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1883 | ||
1884 | shortlen = p - name; | |
a50b1753 | 1885 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1886 | if (shortname == NULL) |
1887 | return FALSE; | |
1888 | memcpy (shortname, name, shortlen); | |
1889 | shortname[shortlen] = '\0'; | |
1890 | ||
1891 | /* We are going to create a new symbol. Merge it with any existing | |
1892 | symbol with this name. For the purposes of the merge, act as | |
1893 | though we were defining the symbol we just defined, although we | |
1894 | actually going to define an indirect symbol. */ | |
1895 | type_change_ok = FALSE; | |
1896 | size_change_ok = FALSE; | |
6e33951e | 1897 | matched = TRUE; |
ffd65175 AM |
1898 | tmp_sec = sec; |
1899 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1900 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1901 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1902 | return FALSE; |
1903 | ||
1904 | if (skip) | |
1905 | goto nondefault; | |
1906 | ||
5fa370e4 | 1907 | if (hi->def_regular || ELF_COMMON_DEF_P (hi)) |
5b677558 AM |
1908 | { |
1909 | /* If the undecorated symbol will have a version added by a | |
1910 | script different to H, then don't indirect to/from the | |
1911 | undecorated symbol. This isn't ideal because we may not yet | |
1912 | have seen symbol versions, if given by a script on the | |
1913 | command line rather than via --version-script. */ | |
1914 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1915 | { | |
1916 | bfd_boolean hide; | |
1917 | ||
1918 | hi->verinfo.vertree | |
1919 | = bfd_find_version_for_sym (info->version_info, | |
1920 | hi->root.root.string, &hide); | |
1921 | if (hi->verinfo.vertree != NULL && hide) | |
1922 | { | |
1923 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1924 | goto nondefault; | |
1925 | } | |
1926 | } | |
1927 | if (hi->verinfo.vertree != NULL | |
1928 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1929 | goto nondefault; | |
1930 | } | |
1931 | ||
45d6a902 AM |
1932 | if (! override) |
1933 | { | |
c6e8a9a8 | 1934 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1935 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1936 | { |
1937 | bh = &hi->root; | |
fbcc8baf | 1938 | if (bh->type == bfd_link_hash_defined |
6cc71b82 | 1939 | && bh->u.def.section->owner != NULL |
fbcc8baf L |
1940 | && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0) |
1941 | { | |
1942 | /* Mark the previous definition from IR object as | |
1943 | undefined so that the generic linker will override | |
1944 | it. */ | |
1945 | bh->type = bfd_link_hash_undefined; | |
1946 | bh->u.undef.abfd = bh->u.def.section->owner; | |
1947 | } | |
c6e8a9a8 L |
1948 | if (! (_bfd_generic_link_add_one_symbol |
1949 | (info, abfd, shortname, BSF_INDIRECT, | |
1950 | bfd_ind_section_ptr, | |
1951 | 0, name, FALSE, collect, &bh))) | |
1952 | return FALSE; | |
1953 | hi = (struct elf_link_hash_entry *) bh; | |
1954 | } | |
45d6a902 AM |
1955 | } |
1956 | else | |
1957 | { | |
1958 | /* In this case the symbol named SHORTNAME is overriding the | |
1959 | indirect symbol we want to add. We were planning on making | |
1960 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1961 | is the name without a version. NAME is the fully versioned | |
1962 | name, and it is the default version. | |
1963 | ||
1964 | Overriding means that we already saw a definition for the | |
1965 | symbol SHORTNAME in a regular object, and it is overriding | |
1966 | the symbol defined in the dynamic object. | |
1967 | ||
1968 | When this happens, we actually want to change NAME, the | |
1969 | symbol we just added, to refer to SHORTNAME. This will cause | |
1970 | references to NAME in the shared object to become references | |
1971 | to SHORTNAME in the regular object. This is what we expect | |
1972 | when we override a function in a shared object: that the | |
1973 | references in the shared object will be mapped to the | |
1974 | definition in the regular object. */ | |
1975 | ||
1976 | while (hi->root.type == bfd_link_hash_indirect | |
1977 | || hi->root.type == bfd_link_hash_warning) | |
1978 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1979 | ||
1980 | h->root.type = bfd_link_hash_indirect; | |
1981 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1982 | if (h->def_dynamic) |
45d6a902 | 1983 | { |
f5385ebf AM |
1984 | h->def_dynamic = 0; |
1985 | hi->ref_dynamic = 1; | |
1986 | if (hi->ref_regular | |
1987 | || hi->def_regular) | |
45d6a902 | 1988 | { |
c152c796 | 1989 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1990 | return FALSE; |
1991 | } | |
1992 | } | |
1993 | ||
1994 | /* Now set HI to H, so that the following code will set the | |
1995 | other fields correctly. */ | |
1996 | hi = h; | |
1997 | } | |
1998 | ||
fab4a87f L |
1999 | /* Check if HI is a warning symbol. */ |
2000 | if (hi->root.type == bfd_link_hash_warning) | |
2001 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
2002 | ||
45d6a902 AM |
2003 | /* If there is a duplicate definition somewhere, then HI may not |
2004 | point to an indirect symbol. We will have reported an error to | |
2005 | the user in that case. */ | |
2006 | ||
2007 | if (hi->root.type == bfd_link_hash_indirect) | |
2008 | { | |
2009 | struct elf_link_hash_entry *ht; | |
2010 | ||
45d6a902 | 2011 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 2012 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 2013 | |
68c88cd4 AM |
2014 | /* A reference to the SHORTNAME symbol from a dynamic library |
2015 | will be satisfied by the versioned symbol at runtime. In | |
2016 | effect, we have a reference to the versioned symbol. */ | |
2017 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
2018 | hi->dynamic_def |= ht->dynamic_def; | |
2019 | ||
45d6a902 AM |
2020 | /* See if the new flags lead us to realize that the symbol must |
2021 | be dynamic. */ | |
2022 | if (! *dynsym) | |
2023 | { | |
2024 | if (! dynamic) | |
2025 | { | |
0e1862bb | 2026 | if (! bfd_link_executable (info) |
90c984fc | 2027 | || hi->def_dynamic |
f5385ebf | 2028 | || hi->ref_dynamic) |
45d6a902 AM |
2029 | *dynsym = TRUE; |
2030 | } | |
2031 | else | |
2032 | { | |
f5385ebf | 2033 | if (hi->ref_regular) |
45d6a902 AM |
2034 | *dynsym = TRUE; |
2035 | } | |
2036 | } | |
2037 | } | |
2038 | ||
2039 | /* We also need to define an indirection from the nondefault version | |
2040 | of the symbol. */ | |
2041 | ||
dc1e8a47 | 2042 | nondefault: |
45d6a902 | 2043 | len = strlen (name); |
a50b1753 | 2044 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
2045 | if (shortname == NULL) |
2046 | return FALSE; | |
2047 | memcpy (shortname, name, shortlen); | |
2048 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
2049 | ||
2050 | /* Once again, merge with any existing symbol. */ | |
2051 | type_change_ok = FALSE; | |
2052 | size_change_ok = FALSE; | |
ffd65175 AM |
2053 | tmp_sec = sec; |
2054 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 2055 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 2056 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
2057 | return FALSE; |
2058 | ||
2059 | if (skip) | |
2060 | return TRUE; | |
2061 | ||
2062 | if (override) | |
2063 | { | |
2064 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
2065 | the type of override we do in the case above unless it is | |
4cc11e76 | 2066 | overridden by a versioned definition. */ |
45d6a902 AM |
2067 | if (hi->root.type != bfd_link_hash_defined |
2068 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 2069 | _bfd_error_handler |
695344c0 | 2070 | /* xgettext:c-format */ |
871b3ab2 | 2071 | (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"), |
d003868e | 2072 | abfd, shortname); |
45d6a902 AM |
2073 | } |
2074 | else | |
2075 | { | |
2076 | bh = &hi->root; | |
2077 | if (! (_bfd_generic_link_add_one_symbol | |
2078 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 2079 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
2080 | return FALSE; |
2081 | hi = (struct elf_link_hash_entry *) bh; | |
2082 | ||
2083 | /* If there is a duplicate definition somewhere, then HI may not | |
2084 | point to an indirect symbol. We will have reported an error | |
2085 | to the user in that case. */ | |
2086 | ||
2087 | if (hi->root.type == bfd_link_hash_indirect) | |
2088 | { | |
fcfa13d2 | 2089 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
2090 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
2091 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
2092 | |
2093 | /* See if the new flags lead us to realize that the symbol | |
2094 | must be dynamic. */ | |
2095 | if (! *dynsym) | |
2096 | { | |
2097 | if (! dynamic) | |
2098 | { | |
0e1862bb | 2099 | if (! bfd_link_executable (info) |
f5385ebf | 2100 | || hi->ref_dynamic) |
45d6a902 AM |
2101 | *dynsym = TRUE; |
2102 | } | |
2103 | else | |
2104 | { | |
f5385ebf | 2105 | if (hi->ref_regular) |
45d6a902 AM |
2106 | *dynsym = TRUE; |
2107 | } | |
2108 | } | |
2109 | } | |
2110 | } | |
2111 | ||
2112 | return TRUE; | |
2113 | } | |
2114 | \f | |
2115 | /* This routine is used to export all defined symbols into the dynamic | |
2116 | symbol table. It is called via elf_link_hash_traverse. */ | |
2117 | ||
28caa186 | 2118 | static bfd_boolean |
268b6b39 | 2119 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2120 | { |
a50b1753 | 2121 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
2122 | |
2123 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2124 | if (h->root.type == bfd_link_hash_indirect) | |
2125 | return TRUE; | |
2126 | ||
7686d77d AM |
2127 | /* Ignore this if we won't export it. */ |
2128 | if (!eif->info->export_dynamic && !h->dynamic) | |
2129 | return TRUE; | |
45d6a902 AM |
2130 | |
2131 | if (h->dynindx == -1 | |
fd91d419 L |
2132 | && (h->def_regular || h->ref_regular) |
2133 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2134 | h->root.root.string)) | |
45d6a902 | 2135 | { |
fd91d419 | 2136 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2137 | { |
fd91d419 L |
2138 | eif->failed = TRUE; |
2139 | return FALSE; | |
45d6a902 AM |
2140 | } |
2141 | } | |
2142 | ||
2143 | return TRUE; | |
2144 | } | |
2145 | \f | |
2146 | /* Look through the symbols which are defined in other shared | |
2147 | libraries and referenced here. Update the list of version | |
2148 | dependencies. This will be put into the .gnu.version_r section. | |
2149 | This function is called via elf_link_hash_traverse. */ | |
2150 | ||
28caa186 | 2151 | static bfd_boolean |
268b6b39 AM |
2152 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2153 | void *data) | |
45d6a902 | 2154 | { |
a50b1753 | 2155 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2156 | Elf_Internal_Verneed *t; |
2157 | Elf_Internal_Vernaux *a; | |
986f0783 | 2158 | size_t amt; |
45d6a902 | 2159 | |
45d6a902 AM |
2160 | /* We only care about symbols defined in shared objects with version |
2161 | information. */ | |
f5385ebf AM |
2162 | if (!h->def_dynamic |
2163 | || h->def_regular | |
45d6a902 | 2164 | || h->dynindx == -1 |
7b20f099 AM |
2165 | || h->verinfo.verdef == NULL |
2166 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2167 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2168 | return TRUE; |
2169 | ||
2170 | /* See if we already know about this version. */ | |
28caa186 AM |
2171 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2172 | t != NULL; | |
2173 | t = t->vn_nextref) | |
45d6a902 AM |
2174 | { |
2175 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2176 | continue; | |
2177 | ||
2178 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2179 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2180 | return TRUE; | |
2181 | ||
2182 | break; | |
2183 | } | |
2184 | ||
2185 | /* This is a new version. Add it to tree we are building. */ | |
2186 | ||
2187 | if (t == NULL) | |
2188 | { | |
2189 | amt = sizeof *t; | |
a50b1753 | 2190 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2191 | if (t == NULL) |
2192 | { | |
2193 | rinfo->failed = TRUE; | |
2194 | return FALSE; | |
2195 | } | |
2196 | ||
2197 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2198 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2199 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2200 | } |
2201 | ||
2202 | amt = sizeof *a; | |
a50b1753 | 2203 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2204 | if (a == NULL) |
2205 | { | |
2206 | rinfo->failed = TRUE; | |
2207 | return FALSE; | |
2208 | } | |
45d6a902 AM |
2209 | |
2210 | /* Note that we are copying a string pointer here, and testing it | |
2211 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2212 | discard the string data when low in memory, this will have to be | |
2213 | fixed. */ | |
2214 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2215 | ||
2216 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2217 | a->vna_nextptr = t->vn_auxptr; | |
2218 | ||
2219 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2220 | ++rinfo->vers; | |
2221 | ||
2222 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2223 | ||
2224 | t->vn_auxptr = a; | |
2225 | ||
2226 | return TRUE; | |
2227 | } | |
2228 | ||
099bb8fb L |
2229 | /* Return TRUE and set *HIDE to TRUE if the versioned symbol is |
2230 | hidden. Set *T_P to NULL if there is no match. */ | |
2231 | ||
2232 | static bfd_boolean | |
2233 | _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info, | |
2234 | struct elf_link_hash_entry *h, | |
2235 | const char *version_p, | |
2236 | struct bfd_elf_version_tree **t_p, | |
2237 | bfd_boolean *hide) | |
2238 | { | |
2239 | struct bfd_elf_version_tree *t; | |
2240 | ||
2241 | /* Look for the version. If we find it, it is no longer weak. */ | |
2242 | for (t = info->version_info; t != NULL; t = t->next) | |
2243 | { | |
2244 | if (strcmp (t->name, version_p) == 0) | |
2245 | { | |
2246 | size_t len; | |
2247 | char *alc; | |
2248 | struct bfd_elf_version_expr *d; | |
2249 | ||
2250 | len = version_p - h->root.root.string; | |
2251 | alc = (char *) bfd_malloc (len); | |
2252 | if (alc == NULL) | |
2253 | return FALSE; | |
2254 | memcpy (alc, h->root.root.string, len - 1); | |
2255 | alc[len - 1] = '\0'; | |
2256 | if (alc[len - 2] == ELF_VER_CHR) | |
2257 | alc[len - 2] = '\0'; | |
2258 | ||
2259 | h->verinfo.vertree = t; | |
2260 | t->used = TRUE; | |
2261 | d = NULL; | |
2262 | ||
2263 | if (t->globals.list != NULL) | |
2264 | d = (*t->match) (&t->globals, NULL, alc); | |
2265 | ||
2266 | /* See if there is anything to force this symbol to | |
2267 | local scope. */ | |
2268 | if (d == NULL && t->locals.list != NULL) | |
2269 | { | |
2270 | d = (*t->match) (&t->locals, NULL, alc); | |
2271 | if (d != NULL | |
2272 | && h->dynindx != -1 | |
2273 | && ! info->export_dynamic) | |
2274 | *hide = TRUE; | |
2275 | } | |
2276 | ||
2277 | free (alc); | |
2278 | break; | |
2279 | } | |
2280 | } | |
2281 | ||
2282 | *t_p = t; | |
2283 | ||
2284 | return TRUE; | |
2285 | } | |
2286 | ||
2287 | /* Return TRUE if the symbol H is hidden by version script. */ | |
2288 | ||
2289 | bfd_boolean | |
2290 | _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info, | |
2291 | struct elf_link_hash_entry *h) | |
2292 | { | |
2293 | const char *p; | |
2294 | bfd_boolean hide = FALSE; | |
2295 | const struct elf_backend_data *bed | |
2296 | = get_elf_backend_data (info->output_bfd); | |
2297 | ||
2298 | /* Version script only hides symbols defined in regular objects. */ | |
2299 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) | |
2300 | return TRUE; | |
2301 | ||
2302 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
2303 | if (p != NULL && h->verinfo.vertree == NULL) | |
2304 | { | |
2305 | struct bfd_elf_version_tree *t; | |
2306 | ||
2307 | ++p; | |
2308 | if (*p == ELF_VER_CHR) | |
2309 | ++p; | |
2310 | ||
2311 | if (*p != '\0' | |
2312 | && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide) | |
2313 | && hide) | |
2314 | { | |
2315 | if (hide) | |
2316 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2317 | return TRUE; | |
2318 | } | |
2319 | } | |
2320 | ||
2321 | /* If we don't have a version for this symbol, see if we can find | |
2322 | something. */ | |
2323 | if (h->verinfo.vertree == NULL && info->version_info != NULL) | |
2324 | { | |
2325 | h->verinfo.vertree | |
2326 | = bfd_find_version_for_sym (info->version_info, | |
2327 | h->root.root.string, &hide); | |
2328 | if (h->verinfo.vertree != NULL && hide) | |
2329 | { | |
2330 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2331 | return TRUE; | |
2332 | } | |
2333 | } | |
2334 | ||
2335 | return FALSE; | |
2336 | } | |
2337 | ||
45d6a902 AM |
2338 | /* Figure out appropriate versions for all the symbols. We may not |
2339 | have the version number script until we have read all of the input | |
2340 | files, so until that point we don't know which symbols should be | |
2341 | local. This function is called via elf_link_hash_traverse. */ | |
2342 | ||
28caa186 | 2343 | static bfd_boolean |
268b6b39 | 2344 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2345 | { |
28caa186 | 2346 | struct elf_info_failed *sinfo; |
45d6a902 | 2347 | struct bfd_link_info *info; |
9c5bfbb7 | 2348 | const struct elf_backend_data *bed; |
45d6a902 AM |
2349 | struct elf_info_failed eif; |
2350 | char *p; | |
099bb8fb | 2351 | bfd_boolean hide; |
45d6a902 | 2352 | |
a50b1753 | 2353 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2354 | info = sinfo->info; |
2355 | ||
45d6a902 AM |
2356 | /* Fix the symbol flags. */ |
2357 | eif.failed = FALSE; | |
2358 | eif.info = info; | |
2359 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2360 | { | |
2361 | if (eif.failed) | |
2362 | sinfo->failed = TRUE; | |
2363 | return FALSE; | |
2364 | } | |
2365 | ||
0a640d71 L |
2366 | bed = get_elf_backend_data (info->output_bfd); |
2367 | ||
45d6a902 AM |
2368 | /* We only need version numbers for symbols defined in regular |
2369 | objects. */ | |
5fa370e4 | 2370 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
0a640d71 L |
2371 | { |
2372 | /* Hide symbols defined in discarded input sections. */ | |
2373 | if ((h->root.type == bfd_link_hash_defined | |
2374 | || h->root.type == bfd_link_hash_defweak) | |
2375 | && discarded_section (h->root.u.def.section)) | |
2376 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2377 | return TRUE; | |
2378 | } | |
45d6a902 | 2379 | |
099bb8fb | 2380 | hide = FALSE; |
45d6a902 AM |
2381 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2382 | if (p != NULL && h->verinfo.vertree == NULL) | |
2383 | { | |
2384 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2385 | |
45d6a902 AM |
2386 | ++p; |
2387 | if (*p == ELF_VER_CHR) | |
6e33951e | 2388 | ++p; |
45d6a902 AM |
2389 | |
2390 | /* If there is no version string, we can just return out. */ | |
2391 | if (*p == '\0') | |
6e33951e | 2392 | return TRUE; |
45d6a902 | 2393 | |
099bb8fb | 2394 | if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)) |
45d6a902 | 2395 | { |
099bb8fb L |
2396 | sinfo->failed = TRUE; |
2397 | return FALSE; | |
45d6a902 AM |
2398 | } |
2399 | ||
099bb8fb L |
2400 | if (hide) |
2401 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2402 | ||
45d6a902 AM |
2403 | /* If we are building an application, we need to create a |
2404 | version node for this version. */ | |
0e1862bb | 2405 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2406 | { |
2407 | struct bfd_elf_version_tree **pp; | |
2408 | int version_index; | |
2409 | ||
2410 | /* If we aren't going to export this symbol, we don't need | |
2411 | to worry about it. */ | |
2412 | if (h->dynindx == -1) | |
2413 | return TRUE; | |
2414 | ||
ef53be89 AM |
2415 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2416 | sizeof *t); | |
45d6a902 AM |
2417 | if (t == NULL) |
2418 | { | |
2419 | sinfo->failed = TRUE; | |
2420 | return FALSE; | |
2421 | } | |
2422 | ||
45d6a902 | 2423 | t->name = p; |
45d6a902 AM |
2424 | t->name_indx = (unsigned int) -1; |
2425 | t->used = TRUE; | |
2426 | ||
2427 | version_index = 1; | |
2428 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2429 | if (sinfo->info->version_info != NULL |
2430 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2431 | version_index = 0; |
fd91d419 L |
2432 | for (pp = &sinfo->info->version_info; |
2433 | *pp != NULL; | |
2434 | pp = &(*pp)->next) | |
45d6a902 AM |
2435 | ++version_index; |
2436 | t->vernum = version_index; | |
2437 | ||
2438 | *pp = t; | |
2439 | ||
2440 | h->verinfo.vertree = t; | |
2441 | } | |
2442 | else if (t == NULL) | |
2443 | { | |
2444 | /* We could not find the version for a symbol when | |
2445 | generating a shared archive. Return an error. */ | |
4eca0228 | 2446 | _bfd_error_handler |
695344c0 | 2447 | /* xgettext:c-format */ |
871b3ab2 | 2448 | (_("%pB: version node not found for symbol %s"), |
28caa186 | 2449 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2450 | bfd_set_error (bfd_error_bad_value); |
2451 | sinfo->failed = TRUE; | |
2452 | return FALSE; | |
2453 | } | |
45d6a902 AM |
2454 | } |
2455 | ||
2456 | /* If we don't have a version for this symbol, see if we can find | |
2457 | something. */ | |
099bb8fb L |
2458 | if (!hide |
2459 | && h->verinfo.vertree == NULL | |
2460 | && sinfo->info->version_info != NULL) | |
45d6a902 | 2461 | { |
fd91d419 L |
2462 | h->verinfo.vertree |
2463 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2464 | h->root.root.string, &hide); | |
1e8fa21e AM |
2465 | if (h->verinfo.vertree != NULL && hide) |
2466 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2467 | } |
2468 | ||
2469 | return TRUE; | |
2470 | } | |
2471 | \f | |
45d6a902 AM |
2472 | /* Read and swap the relocs from the section indicated by SHDR. This |
2473 | may be either a REL or a RELA section. The relocations are | |
2474 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2475 | which should have already been allocated to contain enough space. | |
2476 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2477 | relocations should be stored. | |
2478 | ||
2479 | Returns FALSE if something goes wrong. */ | |
2480 | ||
2481 | static bfd_boolean | |
268b6b39 | 2482 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2483 | asection *sec, |
268b6b39 AM |
2484 | Elf_Internal_Shdr *shdr, |
2485 | void *external_relocs, | |
2486 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2487 | { |
9c5bfbb7 | 2488 | const struct elf_backend_data *bed; |
268b6b39 | 2489 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2490 | const bfd_byte *erela; |
2491 | const bfd_byte *erelaend; | |
2492 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2493 | Elf_Internal_Shdr *symtab_hdr; |
2494 | size_t nsyms; | |
45d6a902 | 2495 | |
45d6a902 AM |
2496 | /* Position ourselves at the start of the section. */ |
2497 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2498 | return FALSE; | |
2499 | ||
2500 | /* Read the relocations. */ | |
2501 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2502 | return FALSE; | |
2503 | ||
243ef1e0 | 2504 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2505 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2506 | |
45d6a902 AM |
2507 | bed = get_elf_backend_data (abfd); |
2508 | ||
2509 | /* Convert the external relocations to the internal format. */ | |
2510 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2511 | swap_in = bed->s->swap_reloc_in; | |
2512 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2513 | swap_in = bed->s->swap_reloca_in; | |
2514 | else | |
2515 | { | |
2516 | bfd_set_error (bfd_error_wrong_format); | |
2517 | return FALSE; | |
2518 | } | |
2519 | ||
a50b1753 | 2520 | erela = (const bfd_byte *) external_relocs; |
f55b1e32 AM |
2521 | /* Setting erelaend like this and comparing with <= handles case of |
2522 | a fuzzed object with sh_size not a multiple of sh_entsize. */ | |
2523 | erelaend = erela + shdr->sh_size - shdr->sh_entsize; | |
45d6a902 | 2524 | irela = internal_relocs; |
f55b1e32 | 2525 | while (erela <= erelaend) |
45d6a902 | 2526 | { |
243ef1e0 L |
2527 | bfd_vma r_symndx; |
2528 | ||
45d6a902 | 2529 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2530 | r_symndx = ELF32_R_SYM (irela->r_info); |
2531 | if (bed->s->arch_size == 64) | |
2532 | r_symndx >>= 24; | |
ce98a316 NC |
2533 | if (nsyms > 0) |
2534 | { | |
2535 | if ((size_t) r_symndx >= nsyms) | |
2536 | { | |
4eca0228 | 2537 | _bfd_error_handler |
695344c0 | 2538 | /* xgettext:c-format */ |
2dcf00ce AM |
2539 | (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)" |
2540 | " for offset %#" PRIx64 " in section `%pA'"), | |
2541 | abfd, (uint64_t) r_symndx, (unsigned long) nsyms, | |
2542 | (uint64_t) irela->r_offset, sec); | |
ce98a316 NC |
2543 | bfd_set_error (bfd_error_bad_value); |
2544 | return FALSE; | |
2545 | } | |
2546 | } | |
cf35638d | 2547 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2548 | { |
4eca0228 | 2549 | _bfd_error_handler |
695344c0 | 2550 | /* xgettext:c-format */ |
2dcf00ce AM |
2551 | (_("%pB: non-zero symbol index (%#" PRIx64 ")" |
2552 | " for offset %#" PRIx64 " in section `%pA'" | |
ce98a316 | 2553 | " when the object file has no symbol table"), |
2dcf00ce AM |
2554 | abfd, (uint64_t) r_symndx, |
2555 | (uint64_t) irela->r_offset, sec); | |
243ef1e0 L |
2556 | bfd_set_error (bfd_error_bad_value); |
2557 | return FALSE; | |
2558 | } | |
45d6a902 AM |
2559 | irela += bed->s->int_rels_per_ext_rel; |
2560 | erela += shdr->sh_entsize; | |
2561 | } | |
2562 | ||
2563 | return TRUE; | |
2564 | } | |
2565 | ||
2566 | /* Read and swap the relocs for a section O. They may have been | |
2567 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2568 | not NULL, they are used as buffers to read into. They are known to | |
2569 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2570 | the return value is allocated using either malloc or bfd_alloc, | |
2571 | according to the KEEP_MEMORY argument. If O has two relocation | |
2572 | sections (both REL and RELA relocations), then the REL_HDR | |
2573 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2574 | RELA_HDR relocations. */ |
45d6a902 AM |
2575 | |
2576 | Elf_Internal_Rela * | |
268b6b39 AM |
2577 | _bfd_elf_link_read_relocs (bfd *abfd, |
2578 | asection *o, | |
2579 | void *external_relocs, | |
2580 | Elf_Internal_Rela *internal_relocs, | |
2581 | bfd_boolean keep_memory) | |
45d6a902 | 2582 | { |
268b6b39 | 2583 | void *alloc1 = NULL; |
45d6a902 | 2584 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2585 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2586 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2587 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2588 | |
d4730f92 BS |
2589 | if (esdo->relocs != NULL) |
2590 | return esdo->relocs; | |
45d6a902 AM |
2591 | |
2592 | if (o->reloc_count == 0) | |
2593 | return NULL; | |
2594 | ||
45d6a902 AM |
2595 | if (internal_relocs == NULL) |
2596 | { | |
2597 | bfd_size_type size; | |
2598 | ||
056bafd4 | 2599 | size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); |
45d6a902 | 2600 | if (keep_memory) |
a50b1753 | 2601 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2602 | else |
a50b1753 | 2603 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2604 | if (internal_relocs == NULL) |
2605 | goto error_return; | |
2606 | } | |
2607 | ||
2608 | if (external_relocs == NULL) | |
2609 | { | |
d4730f92 BS |
2610 | bfd_size_type size = 0; |
2611 | ||
2612 | if (esdo->rel.hdr) | |
2613 | size += esdo->rel.hdr->sh_size; | |
2614 | if (esdo->rela.hdr) | |
2615 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2616 | |
268b6b39 | 2617 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2618 | if (alloc1 == NULL) |
2619 | goto error_return; | |
2620 | external_relocs = alloc1; | |
2621 | } | |
2622 | ||
d4730f92 BS |
2623 | internal_rela_relocs = internal_relocs; |
2624 | if (esdo->rel.hdr) | |
2625 | { | |
2626 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2627 | external_relocs, | |
2628 | internal_relocs)) | |
2629 | goto error_return; | |
2630 | external_relocs = (((bfd_byte *) external_relocs) | |
2631 | + esdo->rel.hdr->sh_size); | |
2632 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2633 | * bed->s->int_rels_per_ext_rel); | |
2634 | } | |
2635 | ||
2636 | if (esdo->rela.hdr | |
2637 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2638 | external_relocs, | |
2639 | internal_rela_relocs))) | |
45d6a902 AM |
2640 | goto error_return; |
2641 | ||
2642 | /* Cache the results for next time, if we can. */ | |
2643 | if (keep_memory) | |
d4730f92 | 2644 | esdo->relocs = internal_relocs; |
45d6a902 | 2645 | |
c9594989 | 2646 | free (alloc1); |
45d6a902 AM |
2647 | |
2648 | /* Don't free alloc2, since if it was allocated we are passing it | |
2649 | back (under the name of internal_relocs). */ | |
2650 | ||
2651 | return internal_relocs; | |
2652 | ||
2653 | error_return: | |
c9594989 | 2654 | free (alloc1); |
45d6a902 | 2655 | if (alloc2 != NULL) |
4dd07732 AM |
2656 | { |
2657 | if (keep_memory) | |
2658 | bfd_release (abfd, alloc2); | |
2659 | else | |
2660 | free (alloc2); | |
2661 | } | |
45d6a902 AM |
2662 | return NULL; |
2663 | } | |
2664 | ||
2665 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2666 | section header for a section containing relocations for O. */ | |
2667 | ||
28caa186 | 2668 | static bfd_boolean |
9eaff861 AO |
2669 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2670 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2671 | { |
9eaff861 | 2672 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2673 | |
2674 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2675 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2676 | |
2677 | /* The contents field must last into write_object_contents, so we | |
2678 | allocate it with bfd_alloc rather than malloc. Also since we | |
2679 | cannot be sure that the contents will actually be filled in, | |
2680 | we zero the allocated space. */ | |
a50b1753 | 2681 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2682 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2683 | return FALSE; | |
2684 | ||
d4730f92 | 2685 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2686 | { |
2687 | struct elf_link_hash_entry **p; | |
2688 | ||
ca4be51c AM |
2689 | p = ((struct elf_link_hash_entry **) |
2690 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2691 | if (p == NULL) |
2692 | return FALSE; | |
2693 | ||
d4730f92 | 2694 | reldata->hashes = p; |
45d6a902 AM |
2695 | } |
2696 | ||
2697 | return TRUE; | |
2698 | } | |
2699 | ||
2700 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2701 | originated from the section given by INPUT_REL_HDR) to the | |
2702 | OUTPUT_BFD. */ | |
2703 | ||
2704 | bfd_boolean | |
268b6b39 AM |
2705 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2706 | asection *input_section, | |
2707 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2708 | Elf_Internal_Rela *internal_relocs, |
2709 | struct elf_link_hash_entry **rel_hash | |
2710 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2711 | { |
2712 | Elf_Internal_Rela *irela; | |
2713 | Elf_Internal_Rela *irelaend; | |
2714 | bfd_byte *erel; | |
d4730f92 | 2715 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2716 | asection *output_section; |
9c5bfbb7 | 2717 | const struct elf_backend_data *bed; |
268b6b39 | 2718 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2719 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2720 | |
2721 | output_section = input_section->output_section; | |
45d6a902 | 2722 | |
d4730f92 BS |
2723 | bed = get_elf_backend_data (output_bfd); |
2724 | esdo = elf_section_data (output_section); | |
2725 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2726 | { |
d4730f92 BS |
2727 | output_reldata = &esdo->rel; |
2728 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2729 | } |
d4730f92 BS |
2730 | else if (esdo->rela.hdr |
2731 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2732 | { |
d4730f92 BS |
2733 | output_reldata = &esdo->rela; |
2734 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2735 | } |
2736 | else | |
2737 | { | |
4eca0228 | 2738 | _bfd_error_handler |
695344c0 | 2739 | /* xgettext:c-format */ |
871b3ab2 | 2740 | (_("%pB: relocation size mismatch in %pB section %pA"), |
d003868e | 2741 | output_bfd, input_section->owner, input_section); |
297d8443 | 2742 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2743 | return FALSE; |
2744 | } | |
2745 | ||
d4730f92 BS |
2746 | erel = output_reldata->hdr->contents; |
2747 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2748 | irela = internal_relocs; |
2749 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2750 | * bed->s->int_rels_per_ext_rel); | |
2751 | while (irela < irelaend) | |
2752 | { | |
2753 | (*swap_out) (output_bfd, irela, erel); | |
2754 | irela += bed->s->int_rels_per_ext_rel; | |
2755 | erel += input_rel_hdr->sh_entsize; | |
2756 | } | |
2757 | ||
2758 | /* Bump the counter, so that we know where to add the next set of | |
2759 | relocations. */ | |
d4730f92 | 2760 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2761 | |
2762 | return TRUE; | |
2763 | } | |
2764 | \f | |
508c3946 L |
2765 | /* Make weak undefined symbols in PIE dynamic. */ |
2766 | ||
2767 | bfd_boolean | |
2768 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2769 | struct elf_link_hash_entry *h) | |
2770 | { | |
0e1862bb | 2771 | if (bfd_link_pie (info) |
508c3946 L |
2772 | && h->dynindx == -1 |
2773 | && h->root.type == bfd_link_hash_undefweak) | |
2774 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2775 | ||
2776 | return TRUE; | |
2777 | } | |
2778 | ||
45d6a902 AM |
2779 | /* Fix up the flags for a symbol. This handles various cases which |
2780 | can only be fixed after all the input files are seen. This is | |
2781 | currently called by both adjust_dynamic_symbol and | |
2782 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2783 | the face of future changes. */ | |
2784 | ||
28caa186 | 2785 | static bfd_boolean |
268b6b39 AM |
2786 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2787 | struct elf_info_failed *eif) | |
45d6a902 | 2788 | { |
33774f08 | 2789 | const struct elf_backend_data *bed; |
508c3946 | 2790 | |
45d6a902 AM |
2791 | /* If this symbol was mentioned in a non-ELF file, try to set |
2792 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2793 | permit a non-ELF file to correctly refer to a symbol defined in | |
2794 | an ELF dynamic object. */ | |
f5385ebf | 2795 | if (h->non_elf) |
45d6a902 AM |
2796 | { |
2797 | while (h->root.type == bfd_link_hash_indirect) | |
2798 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2799 | ||
2800 | if (h->root.type != bfd_link_hash_defined | |
2801 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2802 | { |
2803 | h->ref_regular = 1; | |
2804 | h->ref_regular_nonweak = 1; | |
2805 | } | |
45d6a902 AM |
2806 | else |
2807 | { | |
2808 | if (h->root.u.def.section->owner != NULL | |
2809 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2810 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2811 | { |
2812 | h->ref_regular = 1; | |
2813 | h->ref_regular_nonweak = 1; | |
2814 | } | |
45d6a902 | 2815 | else |
f5385ebf | 2816 | h->def_regular = 1; |
45d6a902 AM |
2817 | } |
2818 | ||
2819 | if (h->dynindx == -1 | |
f5385ebf AM |
2820 | && (h->def_dynamic |
2821 | || h->ref_dynamic)) | |
45d6a902 | 2822 | { |
c152c796 | 2823 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2824 | { |
2825 | eif->failed = TRUE; | |
2826 | return FALSE; | |
2827 | } | |
2828 | } | |
2829 | } | |
2830 | else | |
2831 | { | |
f5385ebf | 2832 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2833 | was first seen in a non-ELF file. Fortunately, if the symbol |
2834 | was first seen in an ELF file, we're probably OK unless the | |
2835 | symbol was defined in a non-ELF file. Catch that case here. | |
2836 | FIXME: We're still in trouble if the symbol was first seen in | |
2837 | a dynamic object, and then later in a non-ELF regular object. */ | |
2838 | if ((h->root.type == bfd_link_hash_defined | |
2839 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2840 | && !h->def_regular |
45d6a902 AM |
2841 | && (h->root.u.def.section->owner != NULL |
2842 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2843 | != bfd_target_elf_flavour) | |
2844 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2845 | && !h->def_dynamic))) |
2846 | h->def_regular = 1; | |
45d6a902 AM |
2847 | } |
2848 | ||
508c3946 | 2849 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2850 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2851 | if (bed->elf_backend_fixup_symbol | |
2852 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2853 | return FALSE; | |
508c3946 | 2854 | |
45d6a902 AM |
2855 | /* If this is a final link, and the symbol was defined as a common |
2856 | symbol in a regular object file, and there was no definition in | |
2857 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2858 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2859 | flag will not have been set. */ |
2860 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2861 | && !h->def_regular |
2862 | && h->ref_regular | |
2863 | && !h->def_dynamic | |
96f29d96 | 2864 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2865 | h->def_regular = 1; |
45d6a902 | 2866 | |
af0bfb9c AM |
2867 | /* Symbols defined in discarded sections shouldn't be dynamic. */ |
2868 | if (h->root.type == bfd_link_hash_undefined && h->indx == -3) | |
2869 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2870 | ||
4deb8f71 L |
2871 | /* If a weak undefined symbol has non-default visibility, we also |
2872 | hide it from the dynamic linker. */ | |
af0bfb9c AM |
2873 | else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
2874 | && h->root.type == bfd_link_hash_undefweak) | |
4deb8f71 L |
2875 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
2876 | ||
2877 | /* A hidden versioned symbol in executable should be forced local if | |
2878 | it is is locally defined, not referenced by shared library and not | |
2879 | exported. */ | |
2880 | else if (bfd_link_executable (eif->info) | |
2881 | && h->versioned == versioned_hidden | |
2882 | && !eif->info->export_dynamic | |
2883 | && !h->dynamic | |
2884 | && !h->ref_dynamic | |
2885 | && h->def_regular) | |
2886 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2887 | ||
45d6a902 AM |
2888 | /* If -Bsymbolic was used (which means to bind references to global |
2889 | symbols to the definition within the shared object), and this | |
2890 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2891 | need a PLT entry. Likewise, if the symbol has non-default |
2892 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2893 | will force it local. */ |
4deb8f71 L |
2894 | else if (h->needs_plt |
2895 | && bfd_link_pic (eif->info) | |
2896 | && is_elf_hash_table (eif->info->hash) | |
2897 | && (SYMBOLIC_BIND (eif->info, h) | |
2898 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
2899 | && h->def_regular) | |
45d6a902 | 2900 | { |
45d6a902 AM |
2901 | bfd_boolean force_local; |
2902 | ||
45d6a902 AM |
2903 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2904 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2905 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2906 | } | |
2907 | ||
45d6a902 AM |
2908 | /* If this is a weak defined symbol in a dynamic object, and we know |
2909 | the real definition in the dynamic object, copy interesting flags | |
2910 | over to the real definition. */ | |
60d67dc8 | 2911 | if (h->is_weakalias) |
45d6a902 | 2912 | { |
60d67dc8 AM |
2913 | struct elf_link_hash_entry *def = weakdef (h); |
2914 | ||
45d6a902 AM |
2915 | /* If the real definition is defined by a regular object file, |
2916 | don't do anything special. See the longer description in | |
5b9d7a9a AM |
2917 | _bfd_elf_adjust_dynamic_symbol, below. If the def is not |
2918 | bfd_link_hash_defined as it was when put on the alias list | |
2919 | then it must have originally been a versioned symbol (for | |
2920 | which a non-versioned indirect symbol is created) and later | |
2921 | a definition for the non-versioned symbol is found. In that | |
2922 | case the indirection is flipped with the versioned symbol | |
2923 | becoming an indirect pointing at the non-versioned symbol. | |
2924 | Thus, not an alias any more. */ | |
2925 | if (def->def_regular | |
2926 | || def->root.type != bfd_link_hash_defined) | |
60d67dc8 AM |
2927 | { |
2928 | h = def; | |
2929 | while ((h = h->u.alias) != def) | |
2930 | h->is_weakalias = 0; | |
2931 | } | |
45d6a902 | 2932 | else |
a26587ba | 2933 | { |
4e6b54a6 AM |
2934 | while (h->root.type == bfd_link_hash_indirect) |
2935 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4e6b54a6 AM |
2936 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
2937 | || h->root.type == bfd_link_hash_defweak); | |
60d67dc8 | 2938 | BFD_ASSERT (def->def_dynamic); |
60d67dc8 | 2939 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); |
a26587ba | 2940 | } |
45d6a902 AM |
2941 | } |
2942 | ||
2943 | return TRUE; | |
2944 | } | |
2945 | ||
2946 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2947 | called via elf_link_hash_traverse, and also calls itself | |
2948 | recursively. */ | |
2949 | ||
28caa186 | 2950 | static bfd_boolean |
268b6b39 | 2951 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2952 | { |
a50b1753 | 2953 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
559192d8 | 2954 | struct elf_link_hash_table *htab; |
9c5bfbb7 | 2955 | const struct elf_backend_data *bed; |
45d6a902 | 2956 | |
0eddce27 | 2957 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2958 | return FALSE; |
2959 | ||
45d6a902 AM |
2960 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2961 | if (h->root.type == bfd_link_hash_indirect) | |
2962 | return TRUE; | |
2963 | ||
2964 | /* Fix the symbol flags. */ | |
2965 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2966 | return FALSE; | |
2967 | ||
559192d8 AM |
2968 | htab = elf_hash_table (eif->info); |
2969 | bed = get_elf_backend_data (htab->dynobj); | |
2970 | ||
954b63d4 AM |
2971 | if (h->root.type == bfd_link_hash_undefweak) |
2972 | { | |
2973 | if (eif->info->dynamic_undefined_weak == 0) | |
559192d8 | 2974 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
954b63d4 AM |
2975 | else if (eif->info->dynamic_undefined_weak > 0 |
2976 | && h->ref_regular | |
2977 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2978 | && !bfd_hide_sym_by_version (eif->info->version_info, | |
2979 | h->root.root.string)) | |
2980 | { | |
2981 | if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2982 | { | |
2983 | eif->failed = TRUE; | |
2984 | return FALSE; | |
2985 | } | |
2986 | } | |
2987 | } | |
2988 | ||
45d6a902 AM |
2989 | /* If this symbol does not require a PLT entry, and it is not |
2990 | defined by a dynamic object, or is not referenced by a regular | |
2991 | object, ignore it. We do have to handle a weak defined symbol, | |
2992 | even if no regular object refers to it, if we decided to add it | |
2993 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2994 | about symbols which are defined by one dynamic object and | |
2995 | referenced by another one? */ | |
f5385ebf | 2996 | if (!h->needs_plt |
91e21fb7 | 2997 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2998 | && (h->def_regular |
2999 | || !h->def_dynamic | |
3000 | || (!h->ref_regular | |
60d67dc8 | 3001 | && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) |
45d6a902 | 3002 | { |
a6aa5195 | 3003 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
3004 | return TRUE; |
3005 | } | |
3006 | ||
3007 | /* If we've already adjusted this symbol, don't do it again. This | |
3008 | can happen via a recursive call. */ | |
f5385ebf | 3009 | if (h->dynamic_adjusted) |
45d6a902 AM |
3010 | return TRUE; |
3011 | ||
3012 | /* Don't look at this symbol again. Note that we must set this | |
3013 | after checking the above conditions, because we may look at a | |
3014 | symbol once, decide not to do anything, and then get called | |
3015 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 3016 | h->dynamic_adjusted = 1; |
45d6a902 AM |
3017 | |
3018 | /* If this is a weak definition, and we know a real definition, and | |
3019 | the real symbol is not itself defined by a regular object file, | |
3020 | then get a good value for the real definition. We handle the | |
3021 | real symbol first, for the convenience of the backend routine. | |
3022 | ||
3023 | Note that there is a confusing case here. If the real definition | |
3024 | is defined by a regular object file, we don't get the real symbol | |
3025 | from the dynamic object, but we do get the weak symbol. If the | |
3026 | processor backend uses a COPY reloc, then if some routine in the | |
3027 | dynamic object changes the real symbol, we will not see that | |
3028 | change in the corresponding weak symbol. This is the way other | |
3029 | ELF linkers work as well, and seems to be a result of the shared | |
3030 | library model. | |
3031 | ||
3032 | I will clarify this issue. Most SVR4 shared libraries define the | |
3033 | variable _timezone and define timezone as a weak synonym. The | |
3034 | tzset call changes _timezone. If you write | |
3035 | extern int timezone; | |
3036 | int _timezone = 5; | |
3037 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
3038 | you might expect that, since timezone is a synonym for _timezone, | |
3039 | the same number will print both times. However, if the processor | |
3040 | backend uses a COPY reloc, then actually timezone will be copied | |
3041 | into your process image, and, since you define _timezone | |
3042 | yourself, _timezone will not. Thus timezone and _timezone will | |
3043 | wind up at different memory locations. The tzset call will set | |
3044 | _timezone, leaving timezone unchanged. */ | |
3045 | ||
60d67dc8 | 3046 | if (h->is_weakalias) |
45d6a902 | 3047 | { |
60d67dc8 AM |
3048 | struct elf_link_hash_entry *def = weakdef (h); |
3049 | ||
ec24dc88 | 3050 | /* If we get to this point, there is an implicit reference to |
60d67dc8 AM |
3051 | the alias by a regular object file via the weak symbol H. */ |
3052 | def->ref_regular = 1; | |
45d6a902 | 3053 | |
ec24dc88 | 3054 | /* Ensure that the backend adjust_dynamic_symbol function sees |
60d67dc8 AM |
3055 | the strong alias before H by recursively calling ourselves. */ |
3056 | if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) | |
45d6a902 AM |
3057 | return FALSE; |
3058 | } | |
3059 | ||
3060 | /* If a symbol has no type and no size and does not require a PLT | |
3061 | entry, then we are probably about to do the wrong thing here: we | |
3062 | are probably going to create a COPY reloc for an empty object. | |
3063 | This case can arise when a shared object is built with assembly | |
3064 | code, and the assembly code fails to set the symbol type. */ | |
3065 | if (h->size == 0 | |
3066 | && h->type == STT_NOTYPE | |
f5385ebf | 3067 | && !h->needs_plt) |
4eca0228 | 3068 | _bfd_error_handler |
45d6a902 AM |
3069 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
3070 | h->root.root.string); | |
3071 | ||
45d6a902 AM |
3072 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
3073 | { | |
3074 | eif->failed = TRUE; | |
3075 | return FALSE; | |
3076 | } | |
3077 | ||
3078 | return TRUE; | |
3079 | } | |
3080 | ||
027297b7 L |
3081 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
3082 | DYNBSS. */ | |
3083 | ||
3084 | bfd_boolean | |
6cabe1ea AM |
3085 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
3086 | struct elf_link_hash_entry *h, | |
027297b7 L |
3087 | asection *dynbss) |
3088 | { | |
91ac5911 | 3089 | unsigned int power_of_two; |
027297b7 L |
3090 | bfd_vma mask; |
3091 | asection *sec = h->root.u.def.section; | |
3092 | ||
de194d85 | 3093 | /* The section alignment of the definition is the maximum alignment |
91ac5911 L |
3094 | requirement of symbols defined in the section. Since we don't |
3095 | know the symbol alignment requirement, we start with the | |
3096 | maximum alignment and check low bits of the symbol address | |
3097 | for the minimum alignment. */ | |
fd361982 | 3098 | power_of_two = bfd_section_alignment (sec); |
91ac5911 L |
3099 | mask = ((bfd_vma) 1 << power_of_two) - 1; |
3100 | while ((h->root.u.def.value & mask) != 0) | |
3101 | { | |
3102 | mask >>= 1; | |
3103 | --power_of_two; | |
3104 | } | |
027297b7 | 3105 | |
fd361982 | 3106 | if (power_of_two > bfd_section_alignment (dynbss)) |
027297b7 L |
3107 | { |
3108 | /* Adjust the section alignment if needed. */ | |
fd361982 | 3109 | if (!bfd_set_section_alignment (dynbss, power_of_two)) |
027297b7 L |
3110 | return FALSE; |
3111 | } | |
3112 | ||
91ac5911 | 3113 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
3114 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
3115 | ||
3116 | /* Define the symbol as being at this point in DYNBSS. */ | |
3117 | h->root.u.def.section = dynbss; | |
3118 | h->root.u.def.value = dynbss->size; | |
3119 | ||
3120 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
3121 | dynbss->size += h->size; | |
3122 | ||
f7483970 L |
3123 | /* No error if extern_protected_data is true. */ |
3124 | if (h->protected_def | |
889c2a67 L |
3125 | && (!info->extern_protected_data |
3126 | || (info->extern_protected_data < 0 | |
3127 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 | 3128 | info->callbacks->einfo |
c1c8c1ef | 3129 | (_("%P: copy reloc against protected `%pT' is dangerous\n"), |
d07a1b05 | 3130 | h->root.root.string); |
6cabe1ea | 3131 | |
027297b7 L |
3132 | return TRUE; |
3133 | } | |
3134 | ||
45d6a902 AM |
3135 | /* Adjust all external symbols pointing into SEC_MERGE sections |
3136 | to reflect the object merging within the sections. */ | |
3137 | ||
28caa186 | 3138 | static bfd_boolean |
268b6b39 | 3139 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
3140 | { |
3141 | asection *sec; | |
3142 | ||
45d6a902 AM |
3143 | if ((h->root.type == bfd_link_hash_defined |
3144 | || h->root.type == bfd_link_hash_defweak) | |
3145 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 3146 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 3147 | { |
a50b1753 | 3148 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
3149 | |
3150 | h->root.u.def.value = | |
3151 | _bfd_merged_section_offset (output_bfd, | |
3152 | &h->root.u.def.section, | |
3153 | elf_section_data (sec)->sec_info, | |
753731ee | 3154 | h->root.u.def.value); |
45d6a902 AM |
3155 | } |
3156 | ||
3157 | return TRUE; | |
3158 | } | |
986a241f RH |
3159 | |
3160 | /* Returns false if the symbol referred to by H should be considered | |
3161 | to resolve local to the current module, and true if it should be | |
3162 | considered to bind dynamically. */ | |
3163 | ||
3164 | bfd_boolean | |
268b6b39 AM |
3165 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
3166 | struct bfd_link_info *info, | |
89a2ee5a | 3167 | bfd_boolean not_local_protected) |
986a241f RH |
3168 | { |
3169 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
3170 | const struct elf_backend_data *bed; |
3171 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
3172 | |
3173 | if (h == NULL) | |
3174 | return FALSE; | |
3175 | ||
3176 | while (h->root.type == bfd_link_hash_indirect | |
3177 | || h->root.type == bfd_link_hash_warning) | |
3178 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3179 | ||
3180 | /* If it was forced local, then clearly it's not dynamic. */ | |
3181 | if (h->dynindx == -1) | |
3182 | return FALSE; | |
f5385ebf | 3183 | if (h->forced_local) |
986a241f RH |
3184 | return FALSE; |
3185 | ||
3186 | /* Identify the cases where name binding rules say that a | |
3187 | visible symbol resolves locally. */ | |
0e1862bb L |
3188 | binding_stays_local_p = (bfd_link_executable (info) |
3189 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
3190 | |
3191 | switch (ELF_ST_VISIBILITY (h->other)) | |
3192 | { | |
3193 | case STV_INTERNAL: | |
3194 | case STV_HIDDEN: | |
3195 | return FALSE; | |
3196 | ||
3197 | case STV_PROTECTED: | |
fcb93ecf PB |
3198 | hash_table = elf_hash_table (info); |
3199 | if (!is_elf_hash_table (hash_table)) | |
3200 | return FALSE; | |
3201 | ||
3202 | bed = get_elf_backend_data (hash_table->dynobj); | |
3203 | ||
986a241f RH |
3204 | /* Proper resolution for function pointer equality may require |
3205 | that these symbols perhaps be resolved dynamically, even though | |
3206 | we should be resolving them to the current module. */ | |
89a2ee5a | 3207 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
3208 | binding_stays_local_p = TRUE; |
3209 | break; | |
3210 | ||
3211 | default: | |
986a241f RH |
3212 | break; |
3213 | } | |
3214 | ||
aa37626c | 3215 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 3216 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
3217 | return TRUE; |
3218 | ||
986a241f RH |
3219 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
3220 | us that it remains local. */ | |
3221 | return !binding_stays_local_p; | |
3222 | } | |
f6c52c13 AM |
3223 | |
3224 | /* Return true if the symbol referred to by H should be considered | |
3225 | to resolve local to the current module, and false otherwise. Differs | |
3226 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 3227 | undefined symbols. The two functions are virtually identical except |
0fad2956 MR |
3228 | for the place where dynindx == -1 is tested. If that test is true, |
3229 | _bfd_elf_dynamic_symbol_p will say the symbol is local, while | |
3230 | _bfd_elf_symbol_refs_local_p will say the symbol is local only for | |
3231 | defined symbols. | |
89a2ee5a AM |
3232 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as |
3233 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
3234 | treatment of undefined weak symbols. For those that do not make | |
3235 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
3236 | |
3237 | bfd_boolean | |
268b6b39 AM |
3238 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3239 | struct bfd_link_info *info, | |
3240 | bfd_boolean local_protected) | |
f6c52c13 | 3241 | { |
fcb93ecf PB |
3242 | const struct elf_backend_data *bed; |
3243 | struct elf_link_hash_table *hash_table; | |
3244 | ||
f6c52c13 AM |
3245 | /* If it's a local sym, of course we resolve locally. */ |
3246 | if (h == NULL) | |
3247 | return TRUE; | |
3248 | ||
d95edcac L |
3249 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3250 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3251 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3252 | return TRUE; | |
3253 | ||
0fad2956 MR |
3254 | /* Forced local symbols resolve locally. */ |
3255 | if (h->forced_local) | |
3256 | return TRUE; | |
3257 | ||
7e2294f9 AO |
3258 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3259 | flag set, so test it first, and don't bail out. */ | |
3260 | if (ELF_COMMON_DEF_P (h)) | |
3261 | /* Do nothing. */; | |
f6c52c13 | 3262 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3263 | resolve locally. The sym is either undefined or dynamic. */ |
3264 | else if (!h->def_regular) | |
f6c52c13 AM |
3265 | return FALSE; |
3266 | ||
0fad2956 | 3267 | /* Non-dynamic symbols resolve locally. */ |
f6c52c13 AM |
3268 | if (h->dynindx == -1) |
3269 | return TRUE; | |
3270 | ||
3271 | /* At this point, we know the symbol is defined and dynamic. In an | |
3272 | executable it must resolve locally, likewise when building symbolic | |
3273 | shared libraries. */ | |
0e1862bb | 3274 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3275 | return TRUE; |
3276 | ||
3277 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3278 | with default visibility might not resolve locally. */ | |
3279 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3280 | return FALSE; | |
3281 | ||
fcb93ecf PB |
3282 | hash_table = elf_hash_table (info); |
3283 | if (!is_elf_hash_table (hash_table)) | |
3284 | return TRUE; | |
3285 | ||
3286 | bed = get_elf_backend_data (hash_table->dynobj); | |
3287 | ||
f7483970 L |
3288 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3289 | symbols are local. */ | |
889c2a67 L |
3290 | if ((!info->extern_protected_data |
3291 | || (info->extern_protected_data < 0 | |
3292 | && !bed->extern_protected_data)) | |
3293 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3294 | return TRUE; |
3295 | ||
f6c52c13 | 3296 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3297 | symbols be treated as dynamic symbols. If the address of a |
3298 | function not defined in an executable is set to that function's | |
3299 | plt entry in the executable, then the address of the function in | |
3300 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3301 | return local_protected; |
3302 | } | |
e1918d23 AM |
3303 | |
3304 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3305 | aligned. Returns the first TLS output section. */ | |
3306 | ||
3307 | struct bfd_section * | |
3308 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3309 | { | |
3310 | struct bfd_section *sec, *tls; | |
3311 | unsigned int align = 0; | |
3312 | ||
3313 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3314 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3315 | break; | |
3316 | tls = sec; | |
3317 | ||
3318 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3319 | if (sec->alignment_power > align) | |
3320 | align = sec->alignment_power; | |
3321 | ||
3322 | elf_hash_table (info)->tls_sec = tls; | |
3323 | ||
fdde2fb6 SH |
3324 | /* Ensure the alignment of the first section (usually .tdata) is the largest |
3325 | alignment, so that the tls segment starts aligned. */ | |
e1918d23 AM |
3326 | if (tls != NULL) |
3327 | tls->alignment_power = align; | |
3328 | ||
3329 | return tls; | |
3330 | } | |
0ad989f9 L |
3331 | |
3332 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3333 | static bfd_boolean | |
3334 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3335 | Elf_Internal_Sym *sym) | |
3336 | { | |
a4d8e49b L |
3337 | const struct elf_backend_data *bed; |
3338 | ||
0ad989f9 L |
3339 | /* Local symbols do not count, but target specific ones might. */ |
3340 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3341 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3342 | return FALSE; | |
3343 | ||
fcb93ecf | 3344 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3345 | /* Function symbols do not count. */ |
fcb93ecf | 3346 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3347 | return FALSE; |
3348 | ||
3349 | /* If the section is undefined, then so is the symbol. */ | |
3350 | if (sym->st_shndx == SHN_UNDEF) | |
3351 | return FALSE; | |
3352 | ||
3353 | /* If the symbol is defined in the common section, then | |
3354 | it is a common definition and so does not count. */ | |
a4d8e49b | 3355 | if (bed->common_definition (sym)) |
0ad989f9 L |
3356 | return FALSE; |
3357 | ||
3358 | /* If the symbol is in a target specific section then we | |
3359 | must rely upon the backend to tell us what it is. */ | |
3360 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3361 | /* FIXME - this function is not coded yet: | |
3362 | ||
3363 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3364 | ||
3365 | Instead for now assume that the definition is not global, | |
3366 | Even if this is wrong, at least the linker will behave | |
3367 | in the same way that it used to do. */ | |
3368 | return FALSE; | |
3369 | ||
3370 | return TRUE; | |
3371 | } | |
3372 | ||
3373 | /* Search the symbol table of the archive element of the archive ABFD | |
3374 | whose archive map contains a mention of SYMDEF, and determine if | |
3375 | the symbol is defined in this element. */ | |
3376 | static bfd_boolean | |
3377 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3378 | { | |
3379 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3380 | size_t symcount; |
3381 | size_t extsymcount; | |
3382 | size_t extsymoff; | |
0ad989f9 L |
3383 | Elf_Internal_Sym *isymbuf; |
3384 | Elf_Internal_Sym *isym; | |
3385 | Elf_Internal_Sym *isymend; | |
3386 | bfd_boolean result; | |
3387 | ||
3388 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3389 | if (abfd == NULL) | |
3390 | return FALSE; | |
3391 | ||
3392 | if (! bfd_check_format (abfd, bfd_object)) | |
3393 | return FALSE; | |
3394 | ||
7dc3990e L |
3395 | /* Select the appropriate symbol table. If we don't know if the |
3396 | object file is an IR object, give linker LTO plugin a chance to | |
3397 | get the correct symbol table. */ | |
3398 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3399 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3400 | || (abfd->plugin_format == bfd_plugin_unknown |
3401 | && bfd_link_plugin_object_p (abfd)) | |
3402 | #endif | |
3403 | ) | |
3404 | { | |
3405 | /* Use the IR symbol table if the object has been claimed by | |
3406 | plugin. */ | |
3407 | abfd = abfd->plugin_dummy_bfd; | |
3408 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3409 | } | |
3410 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3411 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3412 | else | |
3413 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3414 | ||
3415 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3416 | ||
3417 | /* The sh_info field of the symtab header tells us where the | |
3418 | external symbols start. We don't care about the local symbols. */ | |
3419 | if (elf_bad_symtab (abfd)) | |
3420 | { | |
3421 | extsymcount = symcount; | |
3422 | extsymoff = 0; | |
3423 | } | |
3424 | else | |
3425 | { | |
3426 | extsymcount = symcount - hdr->sh_info; | |
3427 | extsymoff = hdr->sh_info; | |
3428 | } | |
3429 | ||
3430 | if (extsymcount == 0) | |
3431 | return FALSE; | |
3432 | ||
3433 | /* Read in the symbol table. */ | |
3434 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3435 | NULL, NULL, NULL); | |
3436 | if (isymbuf == NULL) | |
3437 | return FALSE; | |
3438 | ||
3439 | /* Scan the symbol table looking for SYMDEF. */ | |
3440 | result = FALSE; | |
3441 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3442 | { | |
3443 | const char *name; | |
3444 | ||
3445 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3446 | isym->st_name); | |
3447 | if (name == NULL) | |
3448 | break; | |
3449 | ||
3450 | if (strcmp (name, symdef->name) == 0) | |
3451 | { | |
3452 | result = is_global_data_symbol_definition (abfd, isym); | |
3453 | break; | |
3454 | } | |
3455 | } | |
3456 | ||
3457 | free (isymbuf); | |
3458 | ||
3459 | return result; | |
3460 | } | |
3461 | \f | |
5a580b3a AM |
3462 | /* Add an entry to the .dynamic table. */ |
3463 | ||
3464 | bfd_boolean | |
3465 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3466 | bfd_vma tag, | |
3467 | bfd_vma val) | |
3468 | { | |
3469 | struct elf_link_hash_table *hash_table; | |
3470 | const struct elf_backend_data *bed; | |
3471 | asection *s; | |
3472 | bfd_size_type newsize; | |
3473 | bfd_byte *newcontents; | |
3474 | Elf_Internal_Dyn dyn; | |
3475 | ||
3476 | hash_table = elf_hash_table (info); | |
3477 | if (! is_elf_hash_table (hash_table)) | |
3478 | return FALSE; | |
3479 | ||
7f923b7f AM |
3480 | if (tag == DT_RELA || tag == DT_REL) |
3481 | hash_table->dynamic_relocs = TRUE; | |
3482 | ||
5a580b3a | 3483 | bed = get_elf_backend_data (hash_table->dynobj); |
3d4d4302 | 3484 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3485 | BFD_ASSERT (s != NULL); |
3486 | ||
eea6121a | 3487 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3488 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3489 | if (newcontents == NULL) |
3490 | return FALSE; | |
3491 | ||
3492 | dyn.d_tag = tag; | |
3493 | dyn.d_un.d_val = val; | |
eea6121a | 3494 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3495 | |
eea6121a | 3496 | s->size = newsize; |
5a580b3a AM |
3497 | s->contents = newcontents; |
3498 | ||
3499 | return TRUE; | |
3500 | } | |
3501 | ||
6f6fd151 L |
3502 | /* Strip zero-sized dynamic sections. */ |
3503 | ||
3504 | bfd_boolean | |
3505 | _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info) | |
3506 | { | |
3507 | struct elf_link_hash_table *hash_table; | |
3508 | const struct elf_backend_data *bed; | |
3509 | asection *s, *sdynamic, **pp; | |
3510 | asection *rela_dyn, *rel_dyn; | |
3511 | Elf_Internal_Dyn dyn; | |
3512 | bfd_byte *extdyn, *next; | |
3513 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
3514 | bfd_boolean strip_zero_sized; | |
3515 | bfd_boolean strip_zero_sized_plt; | |
3516 | ||
3517 | if (bfd_link_relocatable (info)) | |
3518 | return TRUE; | |
3519 | ||
3520 | hash_table = elf_hash_table (info); | |
3521 | if (!is_elf_hash_table (hash_table)) | |
3522 | return FALSE; | |
3523 | ||
3524 | if (!hash_table->dynobj) | |
3525 | return TRUE; | |
3526 | ||
3527 | sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic"); | |
3528 | if (!sdynamic) | |
3529 | return TRUE; | |
3530 | ||
3531 | bed = get_elf_backend_data (hash_table->dynobj); | |
3532 | swap_dyn_in = bed->s->swap_dyn_in; | |
3533 | ||
3534 | strip_zero_sized = FALSE; | |
3535 | strip_zero_sized_plt = FALSE; | |
3536 | ||
3537 | /* Strip zero-sized dynamic sections. */ | |
3538 | rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn"); | |
3539 | rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn"); | |
3540 | for (pp = &info->output_bfd->sections; (s = *pp) != NULL;) | |
3541 | if (s->size == 0 | |
3542 | && (s == rela_dyn | |
3543 | || s == rel_dyn | |
3544 | || s == hash_table->srelplt->output_section | |
3545 | || s == hash_table->splt->output_section)) | |
3546 | { | |
3547 | *pp = s->next; | |
3548 | info->output_bfd->section_count--; | |
3549 | strip_zero_sized = TRUE; | |
3550 | if (s == rela_dyn) | |
3551 | s = rela_dyn; | |
3552 | if (s == rel_dyn) | |
3553 | s = rel_dyn; | |
3554 | else if (s == hash_table->splt->output_section) | |
3555 | { | |
3556 | s = hash_table->splt; | |
3557 | strip_zero_sized_plt = TRUE; | |
3558 | } | |
3559 | else | |
3560 | s = hash_table->srelplt; | |
3561 | s->flags |= SEC_EXCLUDE; | |
3562 | s->output_section = bfd_abs_section_ptr; | |
3563 | } | |
3564 | else | |
3565 | pp = &s->next; | |
3566 | ||
3567 | if (strip_zero_sized_plt) | |
3568 | for (extdyn = sdynamic->contents; | |
3569 | extdyn < sdynamic->contents + sdynamic->size; | |
3570 | extdyn = next) | |
3571 | { | |
3572 | next = extdyn + bed->s->sizeof_dyn; | |
3573 | swap_dyn_in (hash_table->dynobj, extdyn, &dyn); | |
3574 | switch (dyn.d_tag) | |
3575 | { | |
3576 | default: | |
3577 | break; | |
3578 | case DT_JMPREL: | |
3579 | case DT_PLTRELSZ: | |
3580 | case DT_PLTREL: | |
3581 | /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if | |
3582 | the procedure linkage table (the .plt section) has been | |
3583 | removed. */ | |
3584 | memmove (extdyn, next, | |
3585 | sdynamic->size - (next - sdynamic->contents)); | |
3586 | next = extdyn; | |
3587 | } | |
3588 | } | |
3589 | ||
3590 | if (strip_zero_sized) | |
3591 | { | |
3592 | /* Regenerate program headers. */ | |
3593 | elf_seg_map (info->output_bfd) = NULL; | |
3594 | return _bfd_elf_map_sections_to_segments (info->output_bfd, info); | |
3595 | } | |
3596 | ||
3597 | return TRUE; | |
3598 | } | |
3599 | ||
e310298c | 3600 | /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error, |
5a580b3a AM |
3601 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ |
3602 | ||
e310298c AM |
3603 | int |
3604 | bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info) | |
5a580b3a AM |
3605 | { |
3606 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3607 | size_t strindex; |
e310298c | 3608 | const char *soname; |
5a580b3a | 3609 | |
7e9f0867 AM |
3610 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3611 | return -1; | |
3612 | ||
5a580b3a | 3613 | hash_table = elf_hash_table (info); |
e310298c | 3614 | soname = elf_dt_name (abfd); |
5a580b3a | 3615 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3616 | if (strindex == (size_t) -1) |
5a580b3a AM |
3617 | return -1; |
3618 | ||
02be4619 | 3619 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3620 | { |
3621 | asection *sdyn; | |
3622 | const struct elf_backend_data *bed; | |
3623 | bfd_byte *extdyn; | |
3624 | ||
3625 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3626 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3627 | if (sdyn != NULL) |
3628 | for (extdyn = sdyn->contents; | |
3629 | extdyn < sdyn->contents + sdyn->size; | |
3630 | extdyn += bed->s->sizeof_dyn) | |
3631 | { | |
3632 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3633 | |
7e9f0867 AM |
3634 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3635 | if (dyn.d_tag == DT_NEEDED | |
3636 | && dyn.d_un.d_val == strindex) | |
3637 | { | |
3638 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3639 | return 1; | |
3640 | } | |
3641 | } | |
5a580b3a AM |
3642 | } |
3643 | ||
e310298c AM |
3644 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3645 | return -1; | |
7e9f0867 | 3646 | |
e310298c AM |
3647 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3648 | return -1; | |
5a580b3a AM |
3649 | |
3650 | return 0; | |
3651 | } | |
3652 | ||
7b15fa7a AM |
3653 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3654 | (or the end of list if STOP is NULL), and needed by a library that | |
3655 | will be loaded. */ | |
3656 | ||
010e5ae2 | 3657 | static bfd_boolean |
7b15fa7a AM |
3658 | on_needed_list (const char *soname, |
3659 | struct bfd_link_needed_list *needed, | |
3660 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3661 | { |
7b15fa7a AM |
3662 | struct bfd_link_needed_list *look; |
3663 | for (look = needed; look != stop; look = look->next) | |
3664 | if (strcmp (soname, look->name) == 0 | |
3665 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3666 | /* If needed by a library that itself is not directly | |
3667 | needed, recursively check whether that library is | |
3668 | indirectly needed. Since we add DT_NEEDED entries to | |
3669 | the end of the list, library dependencies appear after | |
3670 | the library. Therefore search prior to the current | |
3671 | LOOK, preventing possible infinite recursion. */ | |
3672 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3673 | return TRUE; |
3674 | ||
3675 | return FALSE; | |
3676 | } | |
3677 | ||
3a3f4bf7 | 3678 | /* Sort symbol by value, section, size, and type. */ |
4ad4eba5 AM |
3679 | static int |
3680 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3681 | { |
3682 | const struct elf_link_hash_entry *h1; | |
3683 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3684 | bfd_signed_vma vdiff; |
3a3f4bf7 AM |
3685 | int sdiff; |
3686 | const char *n1; | |
3687 | const char *n2; | |
5a580b3a AM |
3688 | |
3689 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3690 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3691 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3692 | if (vdiff != 0) | |
3693 | return vdiff > 0 ? 1 : -1; | |
3a3f4bf7 AM |
3694 | |
3695 | sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3696 | if (sdiff != 0) | |
3697 | return sdiff; | |
3698 | ||
3699 | /* Sort so that sized symbols are selected over zero size symbols. */ | |
3700 | vdiff = h1->size - h2->size; | |
3701 | if (vdiff != 0) | |
3702 | return vdiff > 0 ? 1 : -1; | |
3703 | ||
3704 | /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */ | |
3705 | if (h1->type != h2->type) | |
3706 | return h1->type - h2->type; | |
3707 | ||
3708 | /* If symbols are properly sized and typed, and multiple strong | |
3709 | aliases are not defined in a shared library by the user we | |
3710 | shouldn't get here. Unfortunately linker script symbols like | |
3711 | __bss_start sometimes match a user symbol defined at the start of | |
3712 | .bss without proper size and type. We'd like to preference the | |
3713 | user symbol over reserved system symbols. Sort on leading | |
3714 | underscores. */ | |
3715 | n1 = h1->root.root.string; | |
3716 | n2 = h2->root.root.string; | |
3717 | while (*n1 == *n2) | |
10b7e05b | 3718 | { |
3a3f4bf7 AM |
3719 | if (*n1 == 0) |
3720 | break; | |
3721 | ++n1; | |
3722 | ++n2; | |
10b7e05b | 3723 | } |
3a3f4bf7 AM |
3724 | if (*n1 == '_') |
3725 | return -1; | |
3726 | if (*n2 == '_') | |
3727 | return 1; | |
3728 | ||
3729 | /* Final sort on name selects user symbols like '_u' over reserved | |
3730 | system symbols like '_Z' and also will avoid qsort instability. */ | |
3731 | return *n1 - *n2; | |
5a580b3a | 3732 | } |
4ad4eba5 | 3733 | |
5a580b3a AM |
3734 | /* This function is used to adjust offsets into .dynstr for |
3735 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3736 | ||
3737 | static bfd_boolean | |
3738 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3739 | { | |
a50b1753 | 3740 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3741 | |
5a580b3a AM |
3742 | if (h->dynindx != -1) |
3743 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3744 | return TRUE; | |
3745 | } | |
3746 | ||
3747 | /* Assign string offsets in .dynstr, update all structures referencing | |
3748 | them. */ | |
3749 | ||
4ad4eba5 AM |
3750 | static bfd_boolean |
3751 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3752 | { |
3753 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3754 | struct elf_link_local_dynamic_entry *entry; | |
3755 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3756 | bfd *dynobj = hash_table->dynobj; | |
3757 | asection *sdyn; | |
3758 | bfd_size_type size; | |
3759 | const struct elf_backend_data *bed; | |
3760 | bfd_byte *extdyn; | |
3761 | ||
3762 | _bfd_elf_strtab_finalize (dynstr); | |
3763 | size = _bfd_elf_strtab_size (dynstr); | |
3764 | ||
3765 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3766 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3767 | BFD_ASSERT (sdyn != NULL); |
3768 | ||
3769 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3770 | for (extdyn = sdyn->contents; | |
eea6121a | 3771 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3772 | extdyn += bed->s->sizeof_dyn) |
3773 | { | |
3774 | Elf_Internal_Dyn dyn; | |
3775 | ||
3776 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3777 | switch (dyn.d_tag) | |
3778 | { | |
3779 | case DT_STRSZ: | |
3780 | dyn.d_un.d_val = size; | |
3781 | break; | |
3782 | case DT_NEEDED: | |
3783 | case DT_SONAME: | |
3784 | case DT_RPATH: | |
3785 | case DT_RUNPATH: | |
3786 | case DT_FILTER: | |
3787 | case DT_AUXILIARY: | |
7ee314fa AM |
3788 | case DT_AUDIT: |
3789 | case DT_DEPAUDIT: | |
5a580b3a AM |
3790 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3791 | break; | |
3792 | default: | |
3793 | continue; | |
3794 | } | |
3795 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3796 | } | |
3797 | ||
3798 | /* Now update local dynamic symbols. */ | |
3799 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3800 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3801 | entry->isym.st_name); | |
3802 | ||
3803 | /* And the rest of dynamic symbols. */ | |
3804 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3805 | ||
3806 | /* Adjust version definitions. */ | |
3807 | if (elf_tdata (output_bfd)->cverdefs) | |
3808 | { | |
3809 | asection *s; | |
3810 | bfd_byte *p; | |
ef53be89 | 3811 | size_t i; |
5a580b3a AM |
3812 | Elf_Internal_Verdef def; |
3813 | Elf_Internal_Verdaux defaux; | |
3814 | ||
3d4d4302 | 3815 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3816 | p = s->contents; |
3817 | do | |
3818 | { | |
3819 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3820 | &def); | |
3821 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3822 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3823 | continue; | |
5a580b3a AM |
3824 | for (i = 0; i < def.vd_cnt; ++i) |
3825 | { | |
3826 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3827 | (Elf_External_Verdaux *) p, &defaux); | |
3828 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3829 | defaux.vda_name); | |
3830 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3831 | &defaux, (Elf_External_Verdaux *) p); | |
3832 | p += sizeof (Elf_External_Verdaux); | |
3833 | } | |
3834 | } | |
3835 | while (def.vd_next); | |
3836 | } | |
3837 | ||
3838 | /* Adjust version references. */ | |
3839 | if (elf_tdata (output_bfd)->verref) | |
3840 | { | |
3841 | asection *s; | |
3842 | bfd_byte *p; | |
ef53be89 | 3843 | size_t i; |
5a580b3a AM |
3844 | Elf_Internal_Verneed need; |
3845 | Elf_Internal_Vernaux needaux; | |
3846 | ||
3d4d4302 | 3847 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3848 | p = s->contents; |
3849 | do | |
3850 | { | |
3851 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3852 | &need); | |
3853 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3854 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3855 | (Elf_External_Verneed *) p); | |
3856 | p += sizeof (Elf_External_Verneed); | |
3857 | for (i = 0; i < need.vn_cnt; ++i) | |
3858 | { | |
3859 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3860 | (Elf_External_Vernaux *) p, &needaux); | |
3861 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3862 | needaux.vna_name); | |
3863 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3864 | &needaux, | |
3865 | (Elf_External_Vernaux *) p); | |
3866 | p += sizeof (Elf_External_Vernaux); | |
3867 | } | |
3868 | } | |
3869 | while (need.vn_next); | |
3870 | } | |
3871 | ||
3872 | return TRUE; | |
3873 | } | |
3874 | \f | |
13285a1b AM |
3875 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3876 | The default is to only match when the INPUT and OUTPUT are exactly | |
3877 | the same target. */ | |
3878 | ||
3879 | bfd_boolean | |
3880 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3881 | const bfd_target *output) | |
3882 | { | |
3883 | return input == output; | |
3884 | } | |
3885 | ||
3886 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3887 | This version is used when different targets for the same architecture | |
3888 | are virtually identical. */ | |
3889 | ||
3890 | bfd_boolean | |
3891 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3892 | const bfd_target *output) | |
3893 | { | |
3894 | const struct elf_backend_data *obed, *ibed; | |
3895 | ||
3896 | if (input == output) | |
3897 | return TRUE; | |
3898 | ||
3899 | ibed = xvec_get_elf_backend_data (input); | |
3900 | obed = xvec_get_elf_backend_data (output); | |
3901 | ||
3902 | if (ibed->arch != obed->arch) | |
3903 | return FALSE; | |
3904 | ||
3905 | /* If both backends are using this function, deem them compatible. */ | |
3906 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3907 | } | |
3908 | ||
e5034e59 AM |
3909 | /* Make a special call to the linker "notice" function to tell it that |
3910 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3911 | processing the lib. */ |
e5034e59 AM |
3912 | |
3913 | bfd_boolean | |
3914 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3915 | struct bfd_link_info *info, | |
3916 | enum notice_asneeded_action act) | |
3917 | { | |
46135103 | 3918 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3919 | } |
3920 | ||
d9689752 L |
3921 | /* Check relocations an ELF object file. */ |
3922 | ||
3923 | bfd_boolean | |
3924 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3925 | { | |
3926 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3927 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3928 | ||
3929 | /* If this object is the same format as the output object, and it is | |
3930 | not a shared library, then let the backend look through the | |
3931 | relocs. | |
3932 | ||
3933 | This is required to build global offset table entries and to | |
3934 | arrange for dynamic relocs. It is not required for the | |
3935 | particular common case of linking non PIC code, even when linking | |
3936 | against shared libraries, but unfortunately there is no way of | |
3937 | knowing whether an object file has been compiled PIC or not. | |
3938 | Looking through the relocs is not particularly time consuming. | |
3939 | The problem is that we must either (1) keep the relocs in memory, | |
3940 | which causes the linker to require additional runtime memory or | |
3941 | (2) read the relocs twice from the input file, which wastes time. | |
3942 | This would be a good case for using mmap. | |
3943 | ||
3944 | I have no idea how to handle linking PIC code into a file of a | |
3945 | different format. It probably can't be done. */ | |
3946 | if ((abfd->flags & DYNAMIC) == 0 | |
3947 | && is_elf_hash_table (htab) | |
3948 | && bed->check_relocs != NULL | |
3949 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3950 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3951 | { | |
3952 | asection *o; | |
3953 | ||
3954 | for (o = abfd->sections; o != NULL; o = o->next) | |
3955 | { | |
3956 | Elf_Internal_Rela *internal_relocs; | |
3957 | bfd_boolean ok; | |
3958 | ||
5ce03cea | 3959 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3960 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3961 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3962 | || o->reloc_count == 0 |
3963 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3964 | && (o->flags & SEC_DEBUGGING) != 0) | |
3965 | || bfd_is_abs_section (o->output_section)) | |
3966 | continue; | |
3967 | ||
3968 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3969 | info->keep_memory); | |
3970 | if (internal_relocs == NULL) | |
3971 | return FALSE; | |
3972 | ||
3973 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3974 | ||
3975 | if (elf_section_data (o)->relocs != internal_relocs) | |
3976 | free (internal_relocs); | |
3977 | ||
3978 | if (! ok) | |
3979 | return FALSE; | |
3980 | } | |
3981 | } | |
3982 | ||
3983 | return TRUE; | |
3984 | } | |
3985 | ||
4ad4eba5 AM |
3986 | /* Add symbols from an ELF object file to the linker hash table. */ |
3987 | ||
3988 | static bfd_boolean | |
3989 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3990 | { | |
a0c402a5 | 3991 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3992 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3993 | size_t symcount; |
3994 | size_t extsymcount; | |
3995 | size_t extsymoff; | |
4ad4eba5 AM |
3996 | struct elf_link_hash_entry **sym_hash; |
3997 | bfd_boolean dynamic; | |
3998 | Elf_External_Versym *extversym = NULL; | |
be22c732 | 3999 | Elf_External_Versym *extversym_end = NULL; |
4ad4eba5 AM |
4000 | Elf_External_Versym *ever; |
4001 | struct elf_link_hash_entry *weaks; | |
4002 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 4003 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
4004 | Elf_Internal_Sym *isymbuf = NULL; |
4005 | Elf_Internal_Sym *isym; | |
4006 | Elf_Internal_Sym *isymend; | |
4007 | const struct elf_backend_data *bed; | |
4008 | bfd_boolean add_needed; | |
66eb6687 | 4009 | struct elf_link_hash_table *htab; |
66eb6687 | 4010 | void *alloc_mark = NULL; |
4f87808c AM |
4011 | struct bfd_hash_entry **old_table = NULL; |
4012 | unsigned int old_size = 0; | |
4013 | unsigned int old_count = 0; | |
66eb6687 | 4014 | void *old_tab = NULL; |
66eb6687 AM |
4015 | void *old_ent; |
4016 | struct bfd_link_hash_entry *old_undefs = NULL; | |
4017 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 4018 | void *old_strtab = NULL; |
66eb6687 | 4019 | size_t tabsize = 0; |
db6a5d5f | 4020 | asection *s; |
29a9f53e | 4021 | bfd_boolean just_syms; |
4ad4eba5 | 4022 | |
66eb6687 | 4023 | htab = elf_hash_table (info); |
4ad4eba5 | 4024 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
4025 | |
4026 | if ((abfd->flags & DYNAMIC) == 0) | |
4027 | dynamic = FALSE; | |
4028 | else | |
4029 | { | |
4030 | dynamic = TRUE; | |
4031 | ||
4032 | /* You can't use -r against a dynamic object. Also, there's no | |
4033 | hope of using a dynamic object which does not exactly match | |
4034 | the format of the output file. */ | |
0e1862bb | 4035 | if (bfd_link_relocatable (info) |
66eb6687 | 4036 | || !is_elf_hash_table (htab) |
f13a99db | 4037 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 4038 | { |
0e1862bb | 4039 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
4040 | bfd_set_error (bfd_error_invalid_operation); |
4041 | else | |
4042 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
4043 | goto error_return; |
4044 | } | |
4045 | } | |
4046 | ||
a0c402a5 L |
4047 | ehdr = elf_elfheader (abfd); |
4048 | if (info->warn_alternate_em | |
4049 | && bed->elf_machine_code != ehdr->e_machine | |
4050 | && ((bed->elf_machine_alt1 != 0 | |
4051 | && ehdr->e_machine == bed->elf_machine_alt1) | |
4052 | || (bed->elf_machine_alt2 != 0 | |
4053 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
9793eb77 | 4054 | _bfd_error_handler |
695344c0 | 4055 | /* xgettext:c-format */ |
9793eb77 | 4056 | (_("alternate ELF machine code found (%d) in %pB, expecting %d"), |
a0c402a5 L |
4057 | ehdr->e_machine, abfd, bed->elf_machine_code); |
4058 | ||
4ad4eba5 AM |
4059 | /* As a GNU extension, any input sections which are named |
4060 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
4061 | symbol. This differs from .gnu.warning sections, which generate | |
4062 | warnings when they are included in an output file. */ | |
dd98f8d2 | 4063 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 4064 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 4065 | { |
db6a5d5f | 4066 | const char *name; |
4ad4eba5 | 4067 | |
fd361982 | 4068 | name = bfd_section_name (s); |
db6a5d5f | 4069 | if (CONST_STRNEQ (name, ".gnu.warning.")) |
4ad4eba5 | 4070 | { |
db6a5d5f AM |
4071 | char *msg; |
4072 | bfd_size_type sz; | |
4073 | ||
4074 | name += sizeof ".gnu.warning." - 1; | |
4075 | ||
4076 | /* If this is a shared object, then look up the symbol | |
4077 | in the hash table. If it is there, and it is already | |
4078 | been defined, then we will not be using the entry | |
4079 | from this shared object, so we don't need to warn. | |
4080 | FIXME: If we see the definition in a regular object | |
4081 | later on, we will warn, but we shouldn't. The only | |
4082 | fix is to keep track of what warnings we are supposed | |
4083 | to emit, and then handle them all at the end of the | |
4084 | link. */ | |
4085 | if (dynamic) | |
4ad4eba5 | 4086 | { |
db6a5d5f AM |
4087 | struct elf_link_hash_entry *h; |
4088 | ||
4089 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
4090 | ||
4091 | /* FIXME: What about bfd_link_hash_common? */ | |
4092 | if (h != NULL | |
4093 | && (h->root.type == bfd_link_hash_defined | |
4094 | || h->root.type == bfd_link_hash_defweak)) | |
4095 | continue; | |
4096 | } | |
4ad4eba5 | 4097 | |
db6a5d5f AM |
4098 | sz = s->size; |
4099 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
4100 | if (msg == NULL) | |
4101 | goto error_return; | |
4ad4eba5 | 4102 | |
db6a5d5f AM |
4103 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4104 | goto error_return; | |
4ad4eba5 | 4105 | |
db6a5d5f | 4106 | msg[sz] = '\0'; |
4ad4eba5 | 4107 | |
db6a5d5f AM |
4108 | if (! (_bfd_generic_link_add_one_symbol |
4109 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
4110 | FALSE, bed->collect, NULL))) | |
4111 | goto error_return; | |
4ad4eba5 | 4112 | |
0e1862bb | 4113 | if (bfd_link_executable (info)) |
db6a5d5f AM |
4114 | { |
4115 | /* Clobber the section size so that the warning does | |
4116 | not get copied into the output file. */ | |
4117 | s->size = 0; | |
11d2f718 | 4118 | |
db6a5d5f AM |
4119 | /* Also set SEC_EXCLUDE, so that symbols defined in |
4120 | the warning section don't get copied to the output. */ | |
4121 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
4122 | } |
4123 | } | |
4124 | } | |
4125 | ||
29a9f53e L |
4126 | just_syms = ((s = abfd->sections) != NULL |
4127 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
4128 | ||
4ad4eba5 AM |
4129 | add_needed = TRUE; |
4130 | if (! dynamic) | |
4131 | { | |
4132 | /* If we are creating a shared library, create all the dynamic | |
4133 | sections immediately. We need to attach them to something, | |
4134 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
4135 | format and is not from ld --just-symbols. Always create the |
4136 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
4137 | are no input BFD's of the same format as the output, we can't |
4138 | make a shared library. */ | |
4139 | if (!just_syms | |
bf89386a | 4140 | && (bfd_link_pic (info) |
9c1d7a08 | 4141 | || (!bfd_link_relocatable (info) |
3c5fce9b | 4142 | && info->nointerp |
9c1d7a08 | 4143 | && (info->export_dynamic || info->dynamic))) |
66eb6687 | 4144 | && is_elf_hash_table (htab) |
f13a99db | 4145 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 4146 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
4147 | { |
4148 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
4149 | goto error_return; | |
4150 | } | |
4151 | } | |
66eb6687 | 4152 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
4153 | goto error_return; |
4154 | else | |
4155 | { | |
4ad4eba5 | 4156 | const char *soname = NULL; |
7ee314fa | 4157 | char *audit = NULL; |
4ad4eba5 | 4158 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
9acc85a6 | 4159 | const Elf_Internal_Phdr *phdr; |
e310298c | 4160 | struct elf_link_loaded_list *loaded_lib; |
4ad4eba5 AM |
4161 | |
4162 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 4163 | ld shouldn't allow it. */ |
29a9f53e | 4164 | if (just_syms) |
92fd189d | 4165 | abort (); |
4ad4eba5 AM |
4166 | |
4167 | /* If this dynamic lib was specified on the command line with | |
4168 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
4169 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
4170 | in by another lib's DT_NEEDED. When --no-add-needed is used |
4171 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
4172 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
4173 | all. */ | |
4174 | add_needed = (elf_dyn_lib_class (abfd) | |
4175 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
4176 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
4177 | |
4178 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
4179 | if (s != NULL) | |
4180 | { | |
4181 | bfd_byte *dynbuf; | |
4182 | bfd_byte *extdyn; | |
cb33740c | 4183 | unsigned int elfsec; |
4ad4eba5 AM |
4184 | unsigned long shlink; |
4185 | ||
eea6121a | 4186 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 | 4187 | { |
dc1e8a47 | 4188 | error_free_dyn: |
f8703194 L |
4189 | free (dynbuf); |
4190 | goto error_return; | |
4191 | } | |
4ad4eba5 AM |
4192 | |
4193 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 4194 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
4195 | goto error_free_dyn; |
4196 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
4197 | ||
4198 | for (extdyn = dynbuf; | |
9bff840e | 4199 | extdyn <= dynbuf + s->size - bed->s->sizeof_dyn; |
4ad4eba5 AM |
4200 | extdyn += bed->s->sizeof_dyn) |
4201 | { | |
4202 | Elf_Internal_Dyn dyn; | |
4203 | ||
4204 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
4205 | if (dyn.d_tag == DT_SONAME) | |
4206 | { | |
4207 | unsigned int tagv = dyn.d_un.d_val; | |
4208 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4209 | if (soname == NULL) | |
4210 | goto error_free_dyn; | |
4211 | } | |
4212 | if (dyn.d_tag == DT_NEEDED) | |
4213 | { | |
4214 | struct bfd_link_needed_list *n, **pn; | |
4215 | char *fnm, *anm; | |
4216 | unsigned int tagv = dyn.d_un.d_val; | |
986f0783 | 4217 | size_t amt = sizeof (struct bfd_link_needed_list); |
4ad4eba5 | 4218 | |
a50b1753 | 4219 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4220 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4221 | if (n == NULL || fnm == NULL) | |
4222 | goto error_free_dyn; | |
4223 | amt = strlen (fnm) + 1; | |
a50b1753 | 4224 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4225 | if (anm == NULL) |
4226 | goto error_free_dyn; | |
4227 | memcpy (anm, fnm, amt); | |
4228 | n->name = anm; | |
4229 | n->by = abfd; | |
4230 | n->next = NULL; | |
66eb6687 | 4231 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4232 | ; |
4233 | *pn = n; | |
4234 | } | |
4235 | if (dyn.d_tag == DT_RUNPATH) | |
4236 | { | |
4237 | struct bfd_link_needed_list *n, **pn; | |
4238 | char *fnm, *anm; | |
4239 | unsigned int tagv = dyn.d_un.d_val; | |
986f0783 | 4240 | size_t amt = sizeof (struct bfd_link_needed_list); |
4ad4eba5 | 4241 | |
a50b1753 | 4242 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4243 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4244 | if (n == NULL || fnm == NULL) | |
4245 | goto error_free_dyn; | |
4246 | amt = strlen (fnm) + 1; | |
a50b1753 | 4247 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4248 | if (anm == NULL) |
4249 | goto error_free_dyn; | |
4250 | memcpy (anm, fnm, amt); | |
4251 | n->name = anm; | |
4252 | n->by = abfd; | |
4253 | n->next = NULL; | |
4254 | for (pn = & runpath; | |
4255 | *pn != NULL; | |
4256 | pn = &(*pn)->next) | |
4257 | ; | |
4258 | *pn = n; | |
4259 | } | |
4260 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
4261 | if (!runpath && dyn.d_tag == DT_RPATH) | |
4262 | { | |
4263 | struct bfd_link_needed_list *n, **pn; | |
4264 | char *fnm, *anm; | |
4265 | unsigned int tagv = dyn.d_un.d_val; | |
986f0783 | 4266 | size_t amt = sizeof (struct bfd_link_needed_list); |
4ad4eba5 | 4267 | |
a50b1753 | 4268 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4269 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4270 | if (n == NULL || fnm == NULL) | |
4271 | goto error_free_dyn; | |
4272 | amt = strlen (fnm) + 1; | |
a50b1753 | 4273 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 4274 | if (anm == NULL) |
f8703194 | 4275 | goto error_free_dyn; |
4ad4eba5 AM |
4276 | memcpy (anm, fnm, amt); |
4277 | n->name = anm; | |
4278 | n->by = abfd; | |
4279 | n->next = NULL; | |
4280 | for (pn = & rpath; | |
4281 | *pn != NULL; | |
4282 | pn = &(*pn)->next) | |
4283 | ; | |
4284 | *pn = n; | |
4285 | } | |
7ee314fa AM |
4286 | if (dyn.d_tag == DT_AUDIT) |
4287 | { | |
4288 | unsigned int tagv = dyn.d_un.d_val; | |
4289 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4290 | } | |
4ad4eba5 AM |
4291 | } |
4292 | ||
4293 | free (dynbuf); | |
4294 | } | |
4295 | ||
4296 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
4297 | frees all more recently bfd_alloc'd blocks as well. */ | |
4298 | if (runpath) | |
4299 | rpath = runpath; | |
4300 | ||
4301 | if (rpath) | |
4302 | { | |
4303 | struct bfd_link_needed_list **pn; | |
66eb6687 | 4304 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4305 | ; |
4306 | *pn = rpath; | |
4307 | } | |
4308 | ||
9acc85a6 AM |
4309 | /* If we have a PT_GNU_RELRO program header, mark as read-only |
4310 | all sections contained fully therein. This makes relro | |
4311 | shared library sections appear as they will at run-time. */ | |
4312 | phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; | |
54025d58 | 4313 | while (phdr-- > elf_tdata (abfd)->phdr) |
9acc85a6 AM |
4314 | if (phdr->p_type == PT_GNU_RELRO) |
4315 | { | |
4316 | for (s = abfd->sections; s != NULL; s = s->next) | |
502794d4 CE |
4317 | { |
4318 | unsigned int opb = bfd_octets_per_byte (abfd, s); | |
4319 | ||
4320 | if ((s->flags & SEC_ALLOC) != 0 | |
4321 | && s->vma * opb >= phdr->p_vaddr | |
4322 | && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz) | |
4323 | s->flags |= SEC_READONLY; | |
4324 | } | |
9acc85a6 AM |
4325 | break; |
4326 | } | |
4327 | ||
4ad4eba5 AM |
4328 | /* We do not want to include any of the sections in a dynamic |
4329 | object in the output file. We hack by simply clobbering the | |
4330 | list of sections in the BFD. This could be handled more | |
4331 | cleanly by, say, a new section flag; the existing | |
4332 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4333 | still implies that the section takes up space in the output | |
4334 | file. */ | |
4335 | bfd_section_list_clear (abfd); | |
4336 | ||
4ad4eba5 AM |
4337 | /* Find the name to use in a DT_NEEDED entry that refers to this |
4338 | object. If the object has a DT_SONAME entry, we use it. | |
4339 | Otherwise, if the generic linker stuck something in | |
4340 | elf_dt_name, we use that. Otherwise, we just use the file | |
4341 | name. */ | |
4342 | if (soname == NULL || *soname == '\0') | |
4343 | { | |
4344 | soname = elf_dt_name (abfd); | |
4345 | if (soname == NULL || *soname == '\0') | |
4346 | soname = bfd_get_filename (abfd); | |
4347 | } | |
4348 | ||
4349 | /* Save the SONAME because sometimes the linker emulation code | |
4350 | will need to know it. */ | |
4351 | elf_dt_name (abfd) = soname; | |
4352 | ||
4ad4eba5 AM |
4353 | /* If we have already included this dynamic object in the |
4354 | link, just ignore it. There is no reason to include a | |
4355 | particular dynamic object more than once. */ | |
e310298c AM |
4356 | for (loaded_lib = htab->dyn_loaded; |
4357 | loaded_lib != NULL; | |
4358 | loaded_lib = loaded_lib->next) | |
4359 | { | |
4360 | if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0) | |
4361 | return TRUE; | |
4362 | } | |
4363 | ||
4364 | /* Create dynamic sections for backends that require that be done | |
4365 | before setup_gnu_properties. */ | |
4366 | if (add_needed | |
4367 | && !_bfd_elf_link_create_dynamic_sections (abfd, info)) | |
4368 | return FALSE; | |
7ee314fa AM |
4369 | |
4370 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 4371 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
4372 | } |
4373 | ||
4374 | /* If this is a dynamic object, we always link against the .dynsym | |
4375 | symbol table, not the .symtab symbol table. The dynamic linker | |
4376 | will only see the .dynsym symbol table, so there is no reason to | |
4377 | look at .symtab for a dynamic object. */ | |
4378 | ||
4379 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
4380 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4381 | else | |
4382 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4383 | ||
4384 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
4385 | ||
4386 | /* The sh_info field of the symtab header tells us where the | |
4387 | external symbols start. We don't care about the local symbols at | |
4388 | this point. */ | |
4389 | if (elf_bad_symtab (abfd)) | |
4390 | { | |
4391 | extsymcount = symcount; | |
4392 | extsymoff = 0; | |
4393 | } | |
4394 | else | |
4395 | { | |
4396 | extsymcount = symcount - hdr->sh_info; | |
4397 | extsymoff = hdr->sh_info; | |
4398 | } | |
4399 | ||
f45794cb | 4400 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4401 | if (extsymcount != 0) |
4ad4eba5 AM |
4402 | { |
4403 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4404 | NULL, NULL, NULL); | |
4405 | if (isymbuf == NULL) | |
4406 | goto error_return; | |
4407 | ||
4ad4eba5 | 4408 | if (sym_hash == NULL) |
012b2306 AM |
4409 | { |
4410 | /* We store a pointer to the hash table entry for each | |
4411 | external symbol. */ | |
986f0783 | 4412 | size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *); |
012b2306 AM |
4413 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4414 | if (sym_hash == NULL) | |
4415 | goto error_free_sym; | |
4416 | elf_sym_hashes (abfd) = sym_hash; | |
4417 | } | |
4ad4eba5 AM |
4418 | } |
4419 | ||
4420 | if (dynamic) | |
4421 | { | |
4422 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4423 | if (!_bfd_elf_slurp_version_tables (abfd, |
4424 | info->default_imported_symver)) | |
4ad4eba5 AM |
4425 | goto error_free_sym; |
4426 | ||
4427 | /* Read in the symbol versions, but don't bother to convert them | |
4428 | to internal format. */ | |
4429 | if (elf_dynversym (abfd) != 0) | |
4430 | { | |
986f0783 AM |
4431 | Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr; |
4432 | bfd_size_type amt = versymhdr->sh_size; | |
4ad4eba5 | 4433 | |
2bb3687b AM |
4434 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0) |
4435 | goto error_free_sym; | |
4436 | extversym = (Elf_External_Versym *) | |
4437 | _bfd_malloc_and_read (abfd, amt, amt); | |
4ad4eba5 AM |
4438 | if (extversym == NULL) |
4439 | goto error_free_sym; | |
986f0783 | 4440 | extversym_end = extversym + amt / sizeof (*extversym); |
4ad4eba5 AM |
4441 | } |
4442 | } | |
4443 | ||
66eb6687 AM |
4444 | /* If we are loading an as-needed shared lib, save the symbol table |
4445 | state before we start adding symbols. If the lib turns out | |
4446 | to be unneeded, restore the state. */ | |
4447 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4448 | { | |
4449 | unsigned int i; | |
4450 | size_t entsize; | |
4451 | ||
4452 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4453 | { | |
4454 | struct bfd_hash_entry *p; | |
2de92251 | 4455 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4456 | |
4457 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4458 | { |
4459 | h = (struct elf_link_hash_entry *) p; | |
4460 | entsize += htab->root.table.entsize; | |
4461 | if (h->root.type == bfd_link_hash_warning) | |
4462 | entsize += htab->root.table.entsize; | |
4463 | } | |
66eb6687 AM |
4464 | } |
4465 | ||
4466 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4467 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4468 | if (old_tab == NULL) |
4469 | goto error_free_vers; | |
4470 | ||
4471 | /* Remember the current objalloc pointer, so that all mem for | |
4472 | symbols added can later be reclaimed. */ | |
4473 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4474 | if (alloc_mark == NULL) | |
4475 | goto error_free_vers; | |
4476 | ||
5061a885 AM |
4477 | /* Make a special call to the linker "notice" function to |
4478 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4479 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4480 | goto error_free_vers; |
5061a885 | 4481 | |
f45794cb AM |
4482 | /* Clone the symbol table. Remember some pointers into the |
4483 | symbol table, and dynamic symbol count. */ | |
4484 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4485 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4486 | old_undefs = htab->root.undefs; |
4487 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4488 | old_table = htab->root.table.table; |
4489 | old_size = htab->root.table.size; | |
4490 | old_count = htab->root.table.count; | |
e310298c AM |
4491 | old_strtab = NULL; |
4492 | if (htab->dynstr != NULL) | |
4493 | { | |
4494 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); | |
4495 | if (old_strtab == NULL) | |
4496 | goto error_free_vers; | |
4497 | } | |
66eb6687 AM |
4498 | |
4499 | for (i = 0; i < htab->root.table.size; i++) | |
4500 | { | |
4501 | struct bfd_hash_entry *p; | |
2de92251 | 4502 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4503 | |
4504 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4505 | { | |
4506 | memcpy (old_ent, p, htab->root.table.entsize); | |
4507 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4508 | h = (struct elf_link_hash_entry *) p; |
4509 | if (h->root.type == bfd_link_hash_warning) | |
4510 | { | |
4511 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4512 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4513 | } | |
66eb6687 AM |
4514 | } |
4515 | } | |
4516 | } | |
4ad4eba5 | 4517 | |
66eb6687 | 4518 | weaks = NULL; |
be22c732 NC |
4519 | if (extversym == NULL) |
4520 | ever = NULL; | |
4521 | else if (extversym + extsymoff < extversym_end) | |
4522 | ever = extversym + extsymoff; | |
4523 | else | |
4524 | { | |
4525 | /* xgettext:c-format */ | |
4526 | _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"), | |
4527 | abfd, (long) extsymoff, | |
4528 | (long) (extversym_end - extversym) / sizeof (* extversym)); | |
4529 | bfd_set_error (bfd_error_bad_value); | |
4530 | goto error_free_vers; | |
4531 | } | |
4532 | ||
b4c555cf ML |
4533 | if (!bfd_link_relocatable (info) |
4534 | && abfd->lto_slim_object) | |
cc5277b1 ML |
4535 | { |
4536 | _bfd_error_handler | |
4537 | (_("%pB: plugin needed to handle lto object"), abfd); | |
4538 | } | |
4539 | ||
4ad4eba5 AM |
4540 | for (isym = isymbuf, isymend = isymbuf + extsymcount; |
4541 | isym < isymend; | |
4542 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4543 | { | |
4544 | int bind; | |
4545 | bfd_vma value; | |
af44c138 | 4546 | asection *sec, *new_sec; |
4ad4eba5 AM |
4547 | flagword flags; |
4548 | const char *name; | |
4549 | struct elf_link_hash_entry *h; | |
90c984fc | 4550 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4551 | bfd_boolean definition; |
4552 | bfd_boolean size_change_ok; | |
4553 | bfd_boolean type_change_ok; | |
37a9e49a L |
4554 | bfd_boolean new_weak; |
4555 | bfd_boolean old_weak; | |
4ad4eba5 | 4556 | bfd_boolean override; |
a4d8e49b | 4557 | bfd_boolean common; |
97196564 | 4558 | bfd_boolean discarded; |
4ad4eba5 | 4559 | unsigned int old_alignment; |
4538d1c7 | 4560 | unsigned int shindex; |
4ad4eba5 | 4561 | bfd *old_bfd; |
6e33951e | 4562 | bfd_boolean matched; |
4ad4eba5 AM |
4563 | |
4564 | override = FALSE; | |
4565 | ||
4566 | flags = BSF_NO_FLAGS; | |
4567 | sec = NULL; | |
4568 | value = isym->st_value; | |
a4d8e49b | 4569 | common = bed->common_definition (isym); |
2980ccad L |
4570 | if (common && info->inhibit_common_definition) |
4571 | { | |
4572 | /* Treat common symbol as undefined for --no-define-common. */ | |
4573 | isym->st_shndx = SHN_UNDEF; | |
4574 | common = FALSE; | |
4575 | } | |
97196564 | 4576 | discarded = FALSE; |
4ad4eba5 AM |
4577 | |
4578 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4579 | switch (bind) |
4ad4eba5 | 4580 | { |
3e7a7d11 | 4581 | case STB_LOCAL: |
4ad4eba5 AM |
4582 | /* This should be impossible, since ELF requires that all |
4583 | global symbols follow all local symbols, and that sh_info | |
4584 | point to the first global symbol. Unfortunately, Irix 5 | |
4585 | screws this up. */ | |
fe3fef62 AM |
4586 | if (elf_bad_symtab (abfd)) |
4587 | continue; | |
4588 | ||
4589 | /* If we aren't prepared to handle locals within the globals | |
4538d1c7 AM |
4590 | then we'll likely segfault on a NULL symbol hash if the |
4591 | symbol is ever referenced in relocations. */ | |
4592 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
4593 | name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name); | |
4594 | _bfd_error_handler (_("%pB: %s local symbol at index %lu" | |
4595 | " (>= sh_info of %lu)"), | |
4596 | abfd, name, (long) (isym - isymbuf + extsymoff), | |
4597 | (long) extsymoff); | |
4598 | ||
4599 | /* Dynamic object relocations are not processed by ld, so | |
4600 | ld won't run into the problem mentioned above. */ | |
4601 | if (dynamic) | |
4602 | continue; | |
fe3fef62 AM |
4603 | bfd_set_error (bfd_error_bad_value); |
4604 | goto error_free_vers; | |
3e7a7d11 NC |
4605 | |
4606 | case STB_GLOBAL: | |
a4d8e49b | 4607 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4608 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4609 | break; |
4610 | ||
4611 | case STB_WEAK: | |
4612 | flags = BSF_WEAK; | |
4613 | break; | |
4614 | ||
4615 | case STB_GNU_UNIQUE: | |
4616 | flags = BSF_GNU_UNIQUE; | |
4617 | break; | |
4618 | ||
4619 | default: | |
4ad4eba5 | 4620 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4621 | break; |
4ad4eba5 AM |
4622 | } |
4623 | ||
4624 | if (isym->st_shndx == SHN_UNDEF) | |
4625 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4626 | else if (isym->st_shndx == SHN_ABS) |
4627 | sec = bfd_abs_section_ptr; | |
4628 | else if (isym->st_shndx == SHN_COMMON) | |
4629 | { | |
4630 | sec = bfd_com_section_ptr; | |
4631 | /* What ELF calls the size we call the value. What ELF | |
4632 | calls the value we call the alignment. */ | |
4633 | value = isym->st_size; | |
4634 | } | |
4635 | else | |
4ad4eba5 AM |
4636 | { |
4637 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4638 | if (sec == NULL) | |
4639 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4640 | else if (discarded_section (sec)) |
529fcb95 | 4641 | { |
e5d08002 L |
4642 | /* Symbols from discarded section are undefined. We keep |
4643 | its visibility. */ | |
529fcb95 | 4644 | sec = bfd_und_section_ptr; |
97196564 | 4645 | discarded = TRUE; |
529fcb95 PB |
4646 | isym->st_shndx = SHN_UNDEF; |
4647 | } | |
4ad4eba5 AM |
4648 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4649 | value -= sec->vma; | |
4650 | } | |
4ad4eba5 AM |
4651 | |
4652 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4653 | isym->st_name); | |
4654 | if (name == NULL) | |
4655 | goto error_free_vers; | |
4656 | ||
4657 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4658 | && (abfd->flags & BFD_PLUGIN) != 0) |
4659 | { | |
4660 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4661 | ||
4662 | if (xc == NULL) | |
4663 | { | |
4664 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4665 | | SEC_EXCLUDE); | |
4666 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4667 | if (xc == NULL) | |
4668 | goto error_free_vers; | |
4669 | } | |
4670 | sec = xc; | |
4671 | } | |
4672 | else if (isym->st_shndx == SHN_COMMON | |
4673 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4674 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4675 | { |
4676 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4677 | ||
4678 | if (tcomm == NULL) | |
4679 | { | |
02d00247 AM |
4680 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4681 | | SEC_LINKER_CREATED); | |
4682 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4683 | if (tcomm == NULL) |
4ad4eba5 AM |
4684 | goto error_free_vers; |
4685 | } | |
4686 | sec = tcomm; | |
4687 | } | |
66eb6687 | 4688 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4689 | { |
66eb6687 AM |
4690 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4691 | &sec, &value)) | |
4ad4eba5 AM |
4692 | goto error_free_vers; |
4693 | ||
4694 | /* The hook function sets the name to NULL if this symbol | |
4695 | should be skipped for some reason. */ | |
4696 | if (name == NULL) | |
4697 | continue; | |
4698 | } | |
4699 | ||
4700 | /* Sanity check that all possibilities were handled. */ | |
4701 | if (sec == NULL) | |
4538d1c7 | 4702 | abort (); |
4ad4eba5 | 4703 | |
191c0c42 AM |
4704 | /* Silently discard TLS symbols from --just-syms. There's |
4705 | no way to combine a static TLS block with a new TLS block | |
4706 | for this executable. */ | |
4707 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4708 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4709 | continue; | |
4710 | ||
4ad4eba5 AM |
4711 | if (bfd_is_und_section (sec) |
4712 | || bfd_is_com_section (sec)) | |
4713 | definition = FALSE; | |
4714 | else | |
4715 | definition = TRUE; | |
4716 | ||
4717 | size_change_ok = FALSE; | |
66eb6687 | 4718 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4719 | old_weak = FALSE; |
6e33951e | 4720 | matched = FALSE; |
4ad4eba5 AM |
4721 | old_alignment = 0; |
4722 | old_bfd = NULL; | |
af44c138 | 4723 | new_sec = sec; |
4ad4eba5 | 4724 | |
66eb6687 | 4725 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4726 | { |
4727 | Elf_Internal_Versym iver; | |
4728 | unsigned int vernum = 0; | |
4729 | bfd_boolean skip; | |
4730 | ||
fc0e6df6 | 4731 | if (ever == NULL) |
4ad4eba5 | 4732 | { |
fc0e6df6 PB |
4733 | if (info->default_imported_symver) |
4734 | /* Use the default symbol version created earlier. */ | |
4735 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4736 | else | |
4737 | iver.vs_vers = 0; | |
4738 | } | |
be22c732 NC |
4739 | else if (ever >= extversym_end) |
4740 | { | |
4741 | /* xgettext:c-format */ | |
4742 | _bfd_error_handler (_("%pB: not enough version information"), | |
4743 | abfd); | |
4744 | bfd_set_error (bfd_error_bad_value); | |
4745 | goto error_free_vers; | |
4746 | } | |
fc0e6df6 PB |
4747 | else |
4748 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4749 | ||
4750 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4751 | ||
4752 | /* If this is a hidden symbol, or if it is not version | |
4753 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4754 | However, we do not modify a non-hidden absolute symbol |
4755 | if it is not a function, because it might be the version | |
4756 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4757 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4758 | || (vernum > 1 |
4759 | && (!bfd_is_abs_section (sec) | |
4760 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4761 | { |
4762 | const char *verstr; | |
4763 | size_t namelen, verlen, newlen; | |
4764 | char *newname, *p; | |
4765 | ||
4766 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4767 | { |
fc0e6df6 PB |
4768 | if (vernum > elf_tdata (abfd)->cverdefs) |
4769 | verstr = NULL; | |
4770 | else if (vernum > 1) | |
4771 | verstr = | |
4772 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4773 | else | |
4774 | verstr = ""; | |
4ad4eba5 | 4775 | |
fc0e6df6 | 4776 | if (verstr == NULL) |
4ad4eba5 | 4777 | { |
4eca0228 | 4778 | _bfd_error_handler |
695344c0 | 4779 | /* xgettext:c-format */ |
871b3ab2 | 4780 | (_("%pB: %s: invalid version %u (max %d)"), |
fc0e6df6 PB |
4781 | abfd, name, vernum, |
4782 | elf_tdata (abfd)->cverdefs); | |
4783 | bfd_set_error (bfd_error_bad_value); | |
4784 | goto error_free_vers; | |
4ad4eba5 | 4785 | } |
fc0e6df6 PB |
4786 | } |
4787 | else | |
4788 | { | |
4789 | /* We cannot simply test for the number of | |
4790 | entries in the VERNEED section since the | |
4791 | numbers for the needed versions do not start | |
4792 | at 0. */ | |
4793 | Elf_Internal_Verneed *t; | |
4794 | ||
4795 | verstr = NULL; | |
4796 | for (t = elf_tdata (abfd)->verref; | |
4797 | t != NULL; | |
4798 | t = t->vn_nextref) | |
4ad4eba5 | 4799 | { |
fc0e6df6 | 4800 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4801 | |
fc0e6df6 PB |
4802 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4803 | { | |
4804 | if (a->vna_other == vernum) | |
4ad4eba5 | 4805 | { |
fc0e6df6 PB |
4806 | verstr = a->vna_nodename; |
4807 | break; | |
4ad4eba5 | 4808 | } |
4ad4eba5 | 4809 | } |
fc0e6df6 PB |
4810 | if (a != NULL) |
4811 | break; | |
4812 | } | |
4813 | if (verstr == NULL) | |
4814 | { | |
4eca0228 | 4815 | _bfd_error_handler |
695344c0 | 4816 | /* xgettext:c-format */ |
871b3ab2 | 4817 | (_("%pB: %s: invalid needed version %d"), |
fc0e6df6 PB |
4818 | abfd, name, vernum); |
4819 | bfd_set_error (bfd_error_bad_value); | |
4820 | goto error_free_vers; | |
4ad4eba5 | 4821 | } |
4ad4eba5 | 4822 | } |
fc0e6df6 PB |
4823 | |
4824 | namelen = strlen (name); | |
4825 | verlen = strlen (verstr); | |
4826 | newlen = namelen + verlen + 2; | |
4827 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4828 | && isym->st_shndx != SHN_UNDEF) | |
4829 | ++newlen; | |
4830 | ||
a50b1753 | 4831 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4832 | if (newname == NULL) |
4833 | goto error_free_vers; | |
4834 | memcpy (newname, name, namelen); | |
4835 | p = newname + namelen; | |
4836 | *p++ = ELF_VER_CHR; | |
4837 | /* If this is a defined non-hidden version symbol, | |
4838 | we add another @ to the name. This indicates the | |
4839 | default version of the symbol. */ | |
4840 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4841 | && isym->st_shndx != SHN_UNDEF) | |
4842 | *p++ = ELF_VER_CHR; | |
4843 | memcpy (p, verstr, verlen + 1); | |
4844 | ||
4845 | name = newname; | |
4ad4eba5 AM |
4846 | } |
4847 | ||
cd3416da AM |
4848 | /* If this symbol has default visibility and the user has |
4849 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4850 | if (!bfd_is_und_section (sec) |
cd3416da | 4851 | && !dynamic |
ce875075 | 4852 | && abfd->no_export |
cd3416da AM |
4853 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4854 | isym->st_other = (STV_HIDDEN | |
4855 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4856 | ||
4f3fedcf AM |
4857 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4858 | sym_hash, &old_bfd, &old_weak, | |
4859 | &old_alignment, &skip, &override, | |
6e33951e L |
4860 | &type_change_ok, &size_change_ok, |
4861 | &matched)) | |
4ad4eba5 AM |
4862 | goto error_free_vers; |
4863 | ||
4864 | if (skip) | |
4865 | continue; | |
4866 | ||
6e33951e L |
4867 | /* Override a definition only if the new symbol matches the |
4868 | existing one. */ | |
4869 | if (override && matched) | |
4ad4eba5 AM |
4870 | definition = FALSE; |
4871 | ||
4872 | h = *sym_hash; | |
4873 | while (h->root.type == bfd_link_hash_indirect | |
4874 | || h->root.type == bfd_link_hash_warning) | |
4875 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4876 | ||
4ad4eba5 | 4877 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4878 | && vernum > 1 |
4879 | && definition) | |
4880 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4881 | } | |
4882 | ||
4883 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4884 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4885 | (struct bfd_link_hash_entry **) sym_hash))) |
4886 | goto error_free_vers; | |
4887 | ||
4888 | h = *sym_hash; | |
90c984fc L |
4889 | /* We need to make sure that indirect symbol dynamic flags are |
4890 | updated. */ | |
4891 | hi = h; | |
4ad4eba5 AM |
4892 | while (h->root.type == bfd_link_hash_indirect |
4893 | || h->root.type == bfd_link_hash_warning) | |
4894 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4895 | |
97196564 L |
4896 | /* Setting the index to -3 tells elf_link_output_extsym that |
4897 | this symbol is defined in a discarded section. */ | |
4898 | if (discarded) | |
4899 | h->indx = -3; | |
4900 | ||
4ad4eba5 AM |
4901 | *sym_hash = h; |
4902 | ||
37a9e49a | 4903 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4904 | if (dynamic |
4905 | && definition | |
37a9e49a | 4906 | && new_weak |
fcb93ecf | 4907 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4908 | && is_elf_hash_table (htab) |
60d67dc8 | 4909 | && h->u.alias == NULL) |
4ad4eba5 AM |
4910 | { |
4911 | /* Keep a list of all weak defined non function symbols from | |
60d67dc8 AM |
4912 | a dynamic object, using the alias field. Later in this |
4913 | function we will set the alias field to the correct | |
4ad4eba5 AM |
4914 | value. We only put non-function symbols from dynamic |
4915 | objects on this list, because that happens to be the only | |
4916 | time we need to know the normal symbol corresponding to a | |
4917 | weak symbol, and the information is time consuming to | |
60d67dc8 | 4918 | figure out. If the alias field is not already NULL, |
4ad4eba5 AM |
4919 | then this symbol was already defined by some previous |
4920 | dynamic object, and we will be using that previous | |
4921 | definition anyhow. */ | |
4922 | ||
60d67dc8 | 4923 | h->u.alias = weaks; |
4ad4eba5 | 4924 | weaks = h; |
4ad4eba5 AM |
4925 | } |
4926 | ||
4927 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4928 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4929 | && h->root.type == bfd_link_hash_common) |
4930 | { | |
4931 | unsigned int align; | |
4932 | ||
a4d8e49b | 4933 | if (common) |
af44c138 L |
4934 | align = bfd_log2 (isym->st_value); |
4935 | else | |
4936 | { | |
4937 | /* The new symbol is a common symbol in a shared object. | |
4938 | We need to get the alignment from the section. */ | |
4939 | align = new_sec->alignment_power; | |
4940 | } | |
595213d4 | 4941 | if (align > old_alignment) |
4ad4eba5 AM |
4942 | h->root.u.c.p->alignment_power = align; |
4943 | else | |
4944 | h->root.u.c.p->alignment_power = old_alignment; | |
4945 | } | |
4946 | ||
66eb6687 | 4947 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4948 | { |
4f3fedcf AM |
4949 | /* Set a flag in the hash table entry indicating the type of |
4950 | reference or definition we just found. A dynamic symbol | |
4951 | is one which is referenced or defined by both a regular | |
4952 | object and a shared object. */ | |
4953 | bfd_boolean dynsym = FALSE; | |
4954 | ||
4955 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4956 | ref_regular for them, or make them dynamic. */ | |
4957 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4958 | ; | |
4959 | else if (! dynamic) | |
4960 | { | |
4961 | if (! definition) | |
4962 | { | |
4963 | h->ref_regular = 1; | |
4964 | if (bind != STB_WEAK) | |
4965 | h->ref_regular_nonweak = 1; | |
4966 | } | |
4967 | else | |
4968 | { | |
4969 | h->def_regular = 1; | |
4970 | if (h->def_dynamic) | |
4971 | { | |
4972 | h->def_dynamic = 0; | |
4973 | h->ref_dynamic = 1; | |
4974 | } | |
4975 | } | |
4976 | ||
4977 | /* If the indirect symbol has been forced local, don't | |
4978 | make the real symbol dynamic. */ | |
4979 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4980 | && (bfd_link_dll (info) |
4f3fedcf AM |
4981 | || h->def_dynamic |
4982 | || h->ref_dynamic)) | |
4983 | dynsym = TRUE; | |
4984 | } | |
4985 | else | |
4986 | { | |
4987 | if (! definition) | |
4988 | { | |
4989 | h->ref_dynamic = 1; | |
4990 | hi->ref_dynamic = 1; | |
4991 | } | |
4992 | else | |
4993 | { | |
4994 | h->def_dynamic = 1; | |
4995 | hi->def_dynamic = 1; | |
4996 | } | |
4997 | ||
4998 | /* If the indirect symbol has been forced local, don't | |
4999 | make the real symbol dynamic. */ | |
5000 | if ((h == hi || !hi->forced_local) | |
5001 | && (h->def_regular | |
5002 | || h->ref_regular | |
60d67dc8 AM |
5003 | || (h->is_weakalias |
5004 | && weakdef (h)->dynindx != -1))) | |
4f3fedcf AM |
5005 | dynsym = TRUE; |
5006 | } | |
5007 | ||
5008 | /* Check to see if we need to add an indirect symbol for | |
5009 | the default name. */ | |
5010 | if (definition | |
5011 | || (!override && h->root.type == bfd_link_hash_common)) | |
5012 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
5013 | sec, value, &old_bfd, &dynsym)) | |
5014 | goto error_free_vers; | |
4ad4eba5 AM |
5015 | |
5016 | /* Check the alignment when a common symbol is involved. This | |
5017 | can change when a common symbol is overridden by a normal | |
5018 | definition or a common symbol is ignored due to the old | |
5019 | normal definition. We need to make sure the maximum | |
5020 | alignment is maintained. */ | |
a4d8e49b | 5021 | if ((old_alignment || common) |
4ad4eba5 AM |
5022 | && h->root.type != bfd_link_hash_common) |
5023 | { | |
5024 | unsigned int common_align; | |
5025 | unsigned int normal_align; | |
5026 | unsigned int symbol_align; | |
5027 | bfd *normal_bfd; | |
5028 | bfd *common_bfd; | |
5029 | ||
3a81e825 AM |
5030 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
5031 | || h->root.type == bfd_link_hash_defweak); | |
5032 | ||
4ad4eba5 AM |
5033 | symbol_align = ffs (h->root.u.def.value) - 1; |
5034 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
5035 | && (h->root.u.def.section->owner->flags |
5036 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
5037 | { |
5038 | normal_align = h->root.u.def.section->alignment_power; | |
5039 | if (normal_align > symbol_align) | |
5040 | normal_align = symbol_align; | |
5041 | } | |
5042 | else | |
5043 | normal_align = symbol_align; | |
5044 | ||
5045 | if (old_alignment) | |
5046 | { | |
5047 | common_align = old_alignment; | |
5048 | common_bfd = old_bfd; | |
5049 | normal_bfd = abfd; | |
5050 | } | |
5051 | else | |
5052 | { | |
5053 | common_align = bfd_log2 (isym->st_value); | |
5054 | common_bfd = abfd; | |
5055 | normal_bfd = old_bfd; | |
5056 | } | |
5057 | ||
5058 | if (normal_align < common_align) | |
d07676f8 NC |
5059 | { |
5060 | /* PR binutils/2735 */ | |
5061 | if (normal_bfd == NULL) | |
4eca0228 | 5062 | _bfd_error_handler |
695344c0 | 5063 | /* xgettext:c-format */ |
9793eb77 | 5064 | (_("warning: alignment %u of common symbol `%s' in %pB is" |
871b3ab2 | 5065 | " greater than the alignment (%u) of its section %pA"), |
c08bb8dd AM |
5066 | 1 << common_align, name, common_bfd, |
5067 | 1 << normal_align, h->root.u.def.section); | |
d07676f8 | 5068 | else |
4eca0228 | 5069 | _bfd_error_handler |
695344c0 | 5070 | /* xgettext:c-format */ |
9793eb77 | 5071 | (_("warning: alignment %u of symbol `%s' in %pB" |
871b3ab2 | 5072 | " is smaller than %u in %pB"), |
c08bb8dd AM |
5073 | 1 << normal_align, name, normal_bfd, |
5074 | 1 << common_align, common_bfd); | |
d07676f8 | 5075 | } |
4ad4eba5 AM |
5076 | } |
5077 | ||
83ad0046 | 5078 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
5079 | if (isym->st_size != 0 |
5080 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
5081 | && (definition || h->size == 0)) |
5082 | { | |
83ad0046 L |
5083 | if (h->size != 0 |
5084 | && h->size != isym->st_size | |
5085 | && ! size_change_ok) | |
4eca0228 | 5086 | _bfd_error_handler |
695344c0 | 5087 | /* xgettext:c-format */ |
9793eb77 | 5088 | (_("warning: size of symbol `%s' changed" |
2dcf00ce AM |
5089 | " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"), |
5090 | name, (uint64_t) h->size, old_bfd, | |
5091 | (uint64_t) isym->st_size, abfd); | |
4ad4eba5 AM |
5092 | |
5093 | h->size = isym->st_size; | |
5094 | } | |
5095 | ||
5096 | /* If this is a common symbol, then we always want H->SIZE | |
5097 | to be the size of the common symbol. The code just above | |
5098 | won't fix the size if a common symbol becomes larger. We | |
5099 | don't warn about a size change here, because that is | |
4f3fedcf | 5100 | covered by --warn-common. Allow changes between different |
fcb93ecf | 5101 | function types. */ |
4ad4eba5 AM |
5102 | if (h->root.type == bfd_link_hash_common) |
5103 | h->size = h->root.u.c.size; | |
5104 | ||
5105 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
5106 | && ((definition && !new_weak) |
5107 | || (old_weak && h->root.type == bfd_link_hash_common) | |
5108 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 5109 | { |
2955ec4c L |
5110 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
5111 | ||
5112 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
5113 | symbol. */ | |
5114 | if (type == STT_GNU_IFUNC | |
5115 | && (abfd->flags & DYNAMIC) != 0) | |
5116 | type = STT_FUNC; | |
4ad4eba5 | 5117 | |
2955ec4c L |
5118 | if (h->type != type) |
5119 | { | |
5120 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 5121 | /* xgettext:c-format */ |
4eca0228 | 5122 | _bfd_error_handler |
9793eb77 | 5123 | (_("warning: type of symbol `%s' changed" |
871b3ab2 | 5124 | " from %d to %d in %pB"), |
c08bb8dd | 5125 | name, h->type, type, abfd); |
2955ec4c L |
5126 | |
5127 | h->type = type; | |
5128 | } | |
4ad4eba5 AM |
5129 | } |
5130 | ||
54ac0771 | 5131 | /* Merge st_other field. */ |
b8417128 | 5132 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 5133 | |
c3df8c14 | 5134 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
5135 | if (definition |
5136 | && (sec->flags & SEC_DEBUGGING) | |
5137 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
5138 | dynsym = FALSE; |
5139 | ||
4f3fedcf AM |
5140 | /* Nor should we make plugin symbols dynamic. */ |
5141 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
5142 | dynsym = FALSE; | |
5143 | ||
35fc36a8 | 5144 | if (definition) |
35399224 L |
5145 | { |
5146 | h->target_internal = isym->st_target_internal; | |
5147 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
5148 | } | |
35fc36a8 | 5149 | |
4ad4eba5 AM |
5150 | if (definition && !dynamic) |
5151 | { | |
5152 | char *p = strchr (name, ELF_VER_CHR); | |
5153 | if (p != NULL && p[1] != ELF_VER_CHR) | |
5154 | { | |
5155 | /* Queue non-default versions so that .symver x, x@FOO | |
5156 | aliases can be checked. */ | |
66eb6687 | 5157 | if (!nondeflt_vers) |
4ad4eba5 | 5158 | { |
986f0783 AM |
5159 | size_t amt = ((isymend - isym + 1) |
5160 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
5161 | nondeflt_vers |
5162 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
5163 | if (!nondeflt_vers) |
5164 | goto error_free_vers; | |
4ad4eba5 | 5165 | } |
66eb6687 | 5166 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
5167 | } |
5168 | } | |
5169 | ||
5170 | if (dynsym && h->dynindx == -1) | |
5171 | { | |
c152c796 | 5172 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 5173 | goto error_free_vers; |
60d67dc8 AM |
5174 | if (h->is_weakalias |
5175 | && weakdef (h)->dynindx == -1) | |
4ad4eba5 | 5176 | { |
60d67dc8 | 5177 | if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) |
4ad4eba5 AM |
5178 | goto error_free_vers; |
5179 | } | |
5180 | } | |
1f599d0e | 5181 | else if (h->dynindx != -1) |
4ad4eba5 AM |
5182 | /* If the symbol already has a dynamic index, but |
5183 | visibility says it should not be visible, turn it into | |
5184 | a local symbol. */ | |
5185 | switch (ELF_ST_VISIBILITY (h->other)) | |
5186 | { | |
5187 | case STV_INTERNAL: | |
5188 | case STV_HIDDEN: | |
5189 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
5190 | dynsym = FALSE; | |
5191 | break; | |
5192 | } | |
5193 | ||
aef28989 L |
5194 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
5195 | for unmatched symbol. */ | |
4ad4eba5 | 5196 | if (!add_needed |
aef28989 | 5197 | && matched |
4ad4eba5 | 5198 | && definition |
010e5ae2 | 5199 | && ((dynsym |
ffa9430d | 5200 | && h->ref_regular_nonweak |
4f3fedcf AM |
5201 | && (old_bfd == NULL |
5202 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 5203 | || (h->ref_dynamic_nonweak |
010e5ae2 | 5204 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
5205 | && !on_needed_list (elf_dt_name (abfd), |
5206 | htab->needed, NULL)))) | |
4ad4eba5 | 5207 | { |
4ad4eba5 AM |
5208 | const char *soname = elf_dt_name (abfd); |
5209 | ||
16e4ecc0 AM |
5210 | info->callbacks->minfo ("%!", soname, old_bfd, |
5211 | h->root.root.string); | |
5212 | ||
4ad4eba5 AM |
5213 | /* A symbol from a library loaded via DT_NEEDED of some |
5214 | other library is referenced by a regular object. | |
e56f61be | 5215 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
5216 | --no-add-needed is used and the reference was not |
5217 | a weak one. */ | |
4f3fedcf | 5218 | if (old_bfd != NULL |
b918acf9 | 5219 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 5220 | { |
4eca0228 | 5221 | _bfd_error_handler |
695344c0 | 5222 | /* xgettext:c-format */ |
871b3ab2 | 5223 | (_("%pB: undefined reference to symbol '%s'"), |
4f3fedcf | 5224 | old_bfd, name); |
ff5ac77b | 5225 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
5226 | goto error_free_vers; |
5227 | } | |
5228 | ||
a50b1753 | 5229 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 5230 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 5231 | |
e310298c AM |
5232 | /* Create dynamic sections for backends that require |
5233 | that be done before setup_gnu_properties. */ | |
5234 | if (!_bfd_elf_link_create_dynamic_sections (abfd, info)) | |
5235 | return FALSE; | |
4ad4eba5 | 5236 | add_needed = TRUE; |
4ad4eba5 AM |
5237 | } |
5238 | } | |
5239 | } | |
5240 | ||
a83ef4d1 L |
5241 | if (info->lto_plugin_active |
5242 | && !bfd_link_relocatable (info) | |
5243 | && (abfd->flags & BFD_PLUGIN) == 0 | |
5244 | && !just_syms | |
5245 | && extsymcount) | |
5246 | { | |
5247 | int r_sym_shift; | |
5248 | ||
5249 | if (bed->s->arch_size == 32) | |
5250 | r_sym_shift = 8; | |
5251 | else | |
5252 | r_sym_shift = 32; | |
5253 | ||
5254 | /* If linker plugin is enabled, set non_ir_ref_regular on symbols | |
5255 | referenced in regular objects so that linker plugin will get | |
5256 | the correct symbol resolution. */ | |
5257 | ||
5258 | sym_hash = elf_sym_hashes (abfd); | |
5259 | for (s = abfd->sections; s != NULL; s = s->next) | |
5260 | { | |
5261 | Elf_Internal_Rela *internal_relocs; | |
5262 | Elf_Internal_Rela *rel, *relend; | |
5263 | ||
5264 | /* Don't check relocations in excluded sections. */ | |
5265 | if ((s->flags & SEC_RELOC) == 0 | |
5266 | || s->reloc_count == 0 | |
5267 | || (s->flags & SEC_EXCLUDE) != 0 | |
5268 | || ((info->strip == strip_all | |
5269 | || info->strip == strip_debugger) | |
5270 | && (s->flags & SEC_DEBUGGING) != 0)) | |
5271 | continue; | |
5272 | ||
5273 | internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL, | |
5274 | NULL, | |
5275 | info->keep_memory); | |
5276 | if (internal_relocs == NULL) | |
5277 | goto error_free_vers; | |
5278 | ||
5279 | rel = internal_relocs; | |
5280 | relend = rel + s->reloc_count; | |
5281 | for ( ; rel < relend; rel++) | |
5282 | { | |
5283 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
5284 | struct elf_link_hash_entry *h; | |
5285 | ||
5286 | /* Skip local symbols. */ | |
5287 | if (r_symndx < extsymoff) | |
5288 | continue; | |
5289 | ||
5290 | h = sym_hash[r_symndx - extsymoff]; | |
5291 | if (h != NULL) | |
5292 | h->root.non_ir_ref_regular = 1; | |
5293 | } | |
5294 | ||
5295 | if (elf_section_data (s)->relocs != internal_relocs) | |
5296 | free (internal_relocs); | |
5297 | } | |
5298 | } | |
5299 | ||
c9594989 AM |
5300 | free (extversym); |
5301 | extversym = NULL; | |
5302 | free (isymbuf); | |
5303 | isymbuf = NULL; | |
66eb6687 AM |
5304 | |
5305 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
5306 | { | |
5307 | unsigned int i; | |
5308 | ||
5309 | /* Restore the symbol table. */ | |
f45794cb AM |
5310 | old_ent = (char *) old_tab + tabsize; |
5311 | memset (elf_sym_hashes (abfd), 0, | |
5312 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
5313 | htab->root.table.table = old_table; |
5314 | htab->root.table.size = old_size; | |
5315 | htab->root.table.count = old_count; | |
66eb6687 | 5316 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
5317 | htab->root.undefs = old_undefs; |
5318 | htab->root.undefs_tail = old_undefs_tail; | |
e310298c AM |
5319 | if (htab->dynstr != NULL) |
5320 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); | |
5b677558 AM |
5321 | free (old_strtab); |
5322 | old_strtab = NULL; | |
66eb6687 AM |
5323 | for (i = 0; i < htab->root.table.size; i++) |
5324 | { | |
5325 | struct bfd_hash_entry *p; | |
5326 | struct elf_link_hash_entry *h; | |
3e0882af L |
5327 | bfd_size_type size; |
5328 | unsigned int alignment_power; | |
4070765b | 5329 | unsigned int non_ir_ref_dynamic; |
66eb6687 AM |
5330 | |
5331 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
5332 | { | |
5333 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
5334 | if (h->root.type == bfd_link_hash_warning) |
5335 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 5336 | |
3e0882af L |
5337 | /* Preserve the maximum alignment and size for common |
5338 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 5339 | since it can still be loaded at run time by another |
3e0882af L |
5340 | dynamic lib. */ |
5341 | if (h->root.type == bfd_link_hash_common) | |
5342 | { | |
5343 | size = h->root.u.c.size; | |
5344 | alignment_power = h->root.u.c.p->alignment_power; | |
5345 | } | |
5346 | else | |
5347 | { | |
5348 | size = 0; | |
5349 | alignment_power = 0; | |
5350 | } | |
4070765b | 5351 | /* Preserve non_ir_ref_dynamic so that this symbol |
59fa66c5 L |
5352 | will be exported when the dynamic lib becomes needed |
5353 | in the second pass. */ | |
4070765b | 5354 | non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; |
66eb6687 AM |
5355 | memcpy (p, old_ent, htab->root.table.entsize); |
5356 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
5357 | h = (struct elf_link_hash_entry *) p; |
5358 | if (h->root.type == bfd_link_hash_warning) | |
5359 | { | |
5360 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
5361 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 5362 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 5363 | } |
a4542f1b | 5364 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
5365 | { |
5366 | if (size > h->root.u.c.size) | |
5367 | h->root.u.c.size = size; | |
5368 | if (alignment_power > h->root.u.c.p->alignment_power) | |
5369 | h->root.u.c.p->alignment_power = alignment_power; | |
5370 | } | |
4070765b | 5371 | h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; |
66eb6687 AM |
5372 | } |
5373 | } | |
5374 | ||
5061a885 AM |
5375 | /* Make a special call to the linker "notice" function to |
5376 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 5377 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 5378 | goto error_free_vers; |
5061a885 | 5379 | |
66eb6687 AM |
5380 | free (old_tab); |
5381 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
5382 | alloc_mark); | |
c9594989 | 5383 | free (nondeflt_vers); |
66eb6687 AM |
5384 | return TRUE; |
5385 | } | |
2de92251 | 5386 | |
66eb6687 AM |
5387 | if (old_tab != NULL) |
5388 | { | |
e5034e59 | 5389 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 5390 | goto error_free_vers; |
66eb6687 AM |
5391 | free (old_tab); |
5392 | old_tab = NULL; | |
5393 | } | |
5394 | ||
c6e8a9a8 L |
5395 | /* Now that all the symbols from this input file are created, if |
5396 | not performing a relocatable link, handle .symver foo, foo@BAR | |
5397 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 5398 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 5399 | { |
ef53be89 | 5400 | size_t cnt, symidx; |
4ad4eba5 AM |
5401 | |
5402 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
5403 | { | |
5404 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
5405 | char *shortname, *p; | |
986f0783 | 5406 | size_t amt; |
4ad4eba5 AM |
5407 | |
5408 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
5409 | if (p == NULL | |
5410 | || (h->root.type != bfd_link_hash_defined | |
5411 | && h->root.type != bfd_link_hash_defweak)) | |
5412 | continue; | |
5413 | ||
5414 | amt = p - h->root.root.string; | |
a50b1753 | 5415 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
5416 | if (!shortname) |
5417 | goto error_free_vers; | |
4ad4eba5 AM |
5418 | memcpy (shortname, h->root.root.string, amt); |
5419 | shortname[amt] = '\0'; | |
5420 | ||
5421 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 5422 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
5423 | FALSE, FALSE, FALSE); |
5424 | if (hi != NULL | |
5425 | && hi->root.type == h->root.type | |
5426 | && hi->root.u.def.value == h->root.u.def.value | |
5427 | && hi->root.u.def.section == h->root.u.def.section) | |
5428 | { | |
5429 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
5430 | hi->root.type = bfd_link_hash_indirect; | |
5431 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 5432 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
5433 | sym_hash = elf_sym_hashes (abfd); |
5434 | if (sym_hash) | |
5435 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
5436 | if (sym_hash[symidx] == hi) | |
5437 | { | |
5438 | sym_hash[symidx] = h; | |
5439 | break; | |
5440 | } | |
5441 | } | |
5442 | free (shortname); | |
5443 | } | |
5444 | free (nondeflt_vers); | |
5445 | nondeflt_vers = NULL; | |
5446 | } | |
5447 | ||
60d67dc8 | 5448 | /* Now set the alias field correctly for all the weak defined |
4ad4eba5 AM |
5449 | symbols we found. The only way to do this is to search all the |
5450 | symbols. Since we only need the information for non functions in | |
5451 | dynamic objects, that's the only time we actually put anything on | |
5452 | the list WEAKS. We need this information so that if a regular | |
5453 | object refers to a symbol defined weakly in a dynamic object, the | |
5454 | real symbol in the dynamic object is also put in the dynamic | |
5455 | symbols; we also must arrange for both symbols to point to the | |
5456 | same memory location. We could handle the general case of symbol | |
5457 | aliasing, but a general symbol alias can only be generated in | |
5458 | assembler code, handling it correctly would be very time | |
5459 | consuming, and other ELF linkers don't handle general aliasing | |
5460 | either. */ | |
5461 | if (weaks != NULL) | |
5462 | { | |
5463 | struct elf_link_hash_entry **hpp; | |
5464 | struct elf_link_hash_entry **hppend; | |
5465 | struct elf_link_hash_entry **sorted_sym_hash; | |
5466 | struct elf_link_hash_entry *h; | |
986f0783 | 5467 | size_t sym_count, amt; |
4ad4eba5 AM |
5468 | |
5469 | /* Since we have to search the whole symbol list for each weak | |
5470 | defined symbol, search time for N weak defined symbols will be | |
5471 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
986f0783 | 5472 | amt = extsymcount * sizeof (*sorted_sym_hash); |
3a3f4bf7 | 5473 | sorted_sym_hash = bfd_malloc (amt); |
4ad4eba5 AM |
5474 | if (sorted_sym_hash == NULL) |
5475 | goto error_return; | |
5476 | sym_hash = sorted_sym_hash; | |
5477 | hpp = elf_sym_hashes (abfd); | |
5478 | hppend = hpp + extsymcount; | |
5479 | sym_count = 0; | |
5480 | for (; hpp < hppend; hpp++) | |
5481 | { | |
5482 | h = *hpp; | |
5483 | if (h != NULL | |
5484 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 5485 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
5486 | { |
5487 | *sym_hash = h; | |
5488 | sym_hash++; | |
5489 | sym_count++; | |
5490 | } | |
5491 | } | |
5492 | ||
3a3f4bf7 | 5493 | qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash), |
4ad4eba5 AM |
5494 | elf_sort_symbol); |
5495 | ||
5496 | while (weaks != NULL) | |
5497 | { | |
5498 | struct elf_link_hash_entry *hlook; | |
5499 | asection *slook; | |
5500 | bfd_vma vlook; | |
ed54588d | 5501 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5502 | |
5503 | hlook = weaks; | |
60d67dc8 AM |
5504 | weaks = hlook->u.alias; |
5505 | hlook->u.alias = NULL; | |
4ad4eba5 | 5506 | |
e3e53eed AM |
5507 | if (hlook->root.type != bfd_link_hash_defined |
5508 | && hlook->root.type != bfd_link_hash_defweak) | |
5509 | continue; | |
5510 | ||
4ad4eba5 AM |
5511 | slook = hlook->root.u.def.section; |
5512 | vlook = hlook->root.u.def.value; | |
5513 | ||
4ad4eba5 AM |
5514 | i = 0; |
5515 | j = sym_count; | |
14160578 | 5516 | while (i != j) |
4ad4eba5 AM |
5517 | { |
5518 | bfd_signed_vma vdiff; | |
5519 | idx = (i + j) / 2; | |
14160578 | 5520 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5521 | vdiff = vlook - h->root.u.def.value; |
5522 | if (vdiff < 0) | |
5523 | j = idx; | |
5524 | else if (vdiff > 0) | |
5525 | i = idx + 1; | |
5526 | else | |
5527 | { | |
d3435ae8 | 5528 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5529 | if (sdiff < 0) |
5530 | j = idx; | |
5531 | else if (sdiff > 0) | |
5532 | i = idx + 1; | |
5533 | else | |
14160578 | 5534 | break; |
4ad4eba5 AM |
5535 | } |
5536 | } | |
5537 | ||
5538 | /* We didn't find a value/section match. */ | |
14160578 | 5539 | if (i == j) |
4ad4eba5 AM |
5540 | continue; |
5541 | ||
14160578 AM |
5542 | /* With multiple aliases, or when the weak symbol is already |
5543 | strongly defined, we have multiple matching symbols and | |
5544 | the binary search above may land on any of them. Step | |
5545 | one past the matching symbol(s). */ | |
5546 | while (++idx != j) | |
5547 | { | |
5548 | h = sorted_sym_hash[idx]; | |
5549 | if (h->root.u.def.section != slook | |
5550 | || h->root.u.def.value != vlook) | |
5551 | break; | |
5552 | } | |
5553 | ||
5554 | /* Now look back over the aliases. Since we sorted by size | |
5555 | as well as value and section, we'll choose the one with | |
5556 | the largest size. */ | |
5557 | while (idx-- != i) | |
4ad4eba5 | 5558 | { |
14160578 | 5559 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5560 | |
5561 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5562 | if (h->root.u.def.section != slook |
5563 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5564 | break; |
5565 | else if (h != hlook) | |
5566 | { | |
60d67dc8 AM |
5567 | struct elf_link_hash_entry *t; |
5568 | ||
5569 | hlook->u.alias = h; | |
5570 | hlook->is_weakalias = 1; | |
5571 | t = h; | |
5572 | if (t->u.alias != NULL) | |
5573 | while (t->u.alias != h) | |
5574 | t = t->u.alias; | |
5575 | t->u.alias = hlook; | |
4ad4eba5 AM |
5576 | |
5577 | /* If the weak definition is in the list of dynamic | |
5578 | symbols, make sure the real definition is put | |
5579 | there as well. */ | |
5580 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5581 | { | |
c152c796 | 5582 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5583 | { |
5584 | err_free_sym_hash: | |
5585 | free (sorted_sym_hash); | |
5586 | goto error_return; | |
5587 | } | |
4ad4eba5 AM |
5588 | } |
5589 | ||
5590 | /* If the real definition is in the list of dynamic | |
5591 | symbols, make sure the weak definition is put | |
5592 | there as well. If we don't do this, then the | |
5593 | dynamic loader might not merge the entries for the | |
5594 | real definition and the weak definition. */ | |
5595 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5596 | { | |
c152c796 | 5597 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5598 | goto err_free_sym_hash; |
4ad4eba5 AM |
5599 | } |
5600 | break; | |
5601 | } | |
5602 | } | |
5603 | } | |
5604 | ||
5605 | free (sorted_sym_hash); | |
5606 | } | |
5607 | ||
33177bb1 AM |
5608 | if (bed->check_directives |
5609 | && !(*bed->check_directives) (abfd, info)) | |
5610 | return FALSE; | |
85fbca6a | 5611 | |
4ad4eba5 AM |
5612 | /* If this is a non-traditional link, try to optimize the handling |
5613 | of the .stab/.stabstr sections. */ | |
5614 | if (! dynamic | |
5615 | && ! info->traditional_format | |
66eb6687 | 5616 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5617 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5618 | { | |
5619 | asection *stabstr; | |
5620 | ||
5621 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5622 | if (stabstr != NULL) | |
5623 | { | |
5624 | bfd_size_type string_offset = 0; | |
5625 | asection *stab; | |
5626 | ||
5627 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5628 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5629 | && (!stab->name[5] || |
5630 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5631 | && (stab->flags & SEC_MERGE) == 0 | |
5632 | && !bfd_is_abs_section (stab->output_section)) | |
5633 | { | |
5634 | struct bfd_elf_section_data *secdata; | |
5635 | ||
5636 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5637 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5638 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5639 | &string_offset)) |
5640 | goto error_return; | |
5641 | if (secdata->sec_info) | |
dbaa2011 | 5642 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5643 | } |
5644 | } | |
5645 | } | |
5646 | ||
e310298c | 5647 | if (dynamic && add_needed) |
4ad4eba5 AM |
5648 | { |
5649 | /* Add this bfd to the loaded list. */ | |
5650 | struct elf_link_loaded_list *n; | |
5651 | ||
ca4be51c | 5652 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5653 | if (n == NULL) |
5654 | goto error_return; | |
5655 | n->abfd = abfd; | |
e310298c AM |
5656 | n->next = htab->dyn_loaded; |
5657 | htab->dyn_loaded = n; | |
4ad4eba5 | 5658 | } |
e310298c AM |
5659 | if (dynamic && !add_needed |
5660 | && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0) | |
5661 | elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED; | |
4ad4eba5 AM |
5662 | |
5663 | return TRUE; | |
5664 | ||
5665 | error_free_vers: | |
c9594989 AM |
5666 | free (old_tab); |
5667 | free (old_strtab); | |
5668 | free (nondeflt_vers); | |
5669 | free (extversym); | |
4ad4eba5 | 5670 | error_free_sym: |
c9594989 | 5671 | free (isymbuf); |
4ad4eba5 AM |
5672 | error_return: |
5673 | return FALSE; | |
5674 | } | |
5675 | ||
8387904d AM |
5676 | /* Return the linker hash table entry of a symbol that might be |
5677 | satisfied by an archive symbol. Return -1 on error. */ | |
5678 | ||
5679 | struct elf_link_hash_entry * | |
5680 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5681 | struct bfd_link_info *info, | |
5682 | const char *name) | |
5683 | { | |
5684 | struct elf_link_hash_entry *h; | |
5685 | char *p, *copy; | |
5686 | size_t len, first; | |
5687 | ||
2a41f396 | 5688 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5689 | if (h != NULL) |
5690 | return h; | |
5691 | ||
5692 | /* If this is a default version (the name contains @@), look up the | |
5693 | symbol again with only one `@' as well as without the version. | |
5694 | The effect is that references to the symbol with and without the | |
5695 | version will be matched by the default symbol in the archive. */ | |
5696 | ||
5697 | p = strchr (name, ELF_VER_CHR); | |
5698 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5699 | return h; | |
5700 | ||
5701 | /* First check with only one `@'. */ | |
5702 | len = strlen (name); | |
a50b1753 | 5703 | copy = (char *) bfd_alloc (abfd, len); |
8387904d | 5704 | if (copy == NULL) |
e99955cd | 5705 | return (struct elf_link_hash_entry *) -1; |
8387904d AM |
5706 | |
5707 | first = p - name + 1; | |
5708 | memcpy (copy, name, first); | |
5709 | memcpy (copy + first, name + first + 1, len - first); | |
5710 | ||
2a41f396 | 5711 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5712 | if (h == NULL) |
5713 | { | |
5714 | /* We also need to check references to the symbol without the | |
5715 | version. */ | |
5716 | copy[first - 1] = '\0'; | |
5717 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5718 | FALSE, FALSE, TRUE); |
8387904d AM |
5719 | } |
5720 | ||
5721 | bfd_release (abfd, copy); | |
5722 | return h; | |
5723 | } | |
5724 | ||
0ad989f9 | 5725 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5726 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5727 | handle versioned symbols. | |
0ad989f9 L |
5728 | |
5729 | Fortunately, ELF archive handling is simpler than that done by | |
5730 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5731 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5732 | symbol is currently undefined, we know that we must pull in that | |
5733 | object file. | |
5734 | ||
5735 | Unfortunately, we do have to make multiple passes over the symbol | |
5736 | table until nothing further is resolved. */ | |
5737 | ||
4ad4eba5 AM |
5738 | static bfd_boolean |
5739 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5740 | { |
5741 | symindex c; | |
13e570f8 | 5742 | unsigned char *included = NULL; |
0ad989f9 L |
5743 | carsym *symdefs; |
5744 | bfd_boolean loop; | |
986f0783 | 5745 | size_t amt; |
8387904d AM |
5746 | const struct elf_backend_data *bed; |
5747 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5748 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5749 | |
5750 | if (! bfd_has_map (abfd)) | |
5751 | { | |
5752 | /* An empty archive is a special case. */ | |
5753 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5754 | return TRUE; | |
5755 | bfd_set_error (bfd_error_no_armap); | |
5756 | return FALSE; | |
5757 | } | |
5758 | ||
5759 | /* Keep track of all symbols we know to be already defined, and all | |
5760 | files we know to be already included. This is to speed up the | |
5761 | second and subsequent passes. */ | |
5762 | c = bfd_ardata (abfd)->symdef_count; | |
5763 | if (c == 0) | |
5764 | return TRUE; | |
986f0783 | 5765 | amt = c * sizeof (*included); |
13e570f8 AM |
5766 | included = (unsigned char *) bfd_zmalloc (amt); |
5767 | if (included == NULL) | |
5768 | return FALSE; | |
0ad989f9 L |
5769 | |
5770 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5771 | bed = get_elf_backend_data (abfd); |
5772 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5773 | |
5774 | do | |
5775 | { | |
5776 | file_ptr last; | |
5777 | symindex i; | |
5778 | carsym *symdef; | |
5779 | carsym *symdefend; | |
5780 | ||
5781 | loop = FALSE; | |
5782 | last = -1; | |
5783 | ||
5784 | symdef = symdefs; | |
5785 | symdefend = symdef + c; | |
5786 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5787 | { | |
5788 | struct elf_link_hash_entry *h; | |
5789 | bfd *element; | |
5790 | struct bfd_link_hash_entry *undefs_tail; | |
5791 | symindex mark; | |
5792 | ||
13e570f8 | 5793 | if (included[i]) |
0ad989f9 L |
5794 | continue; |
5795 | if (symdef->file_offset == last) | |
5796 | { | |
5797 | included[i] = TRUE; | |
5798 | continue; | |
5799 | } | |
5800 | ||
8387904d | 5801 | h = archive_symbol_lookup (abfd, info, symdef->name); |
e99955cd | 5802 | if (h == (struct elf_link_hash_entry *) -1) |
8387904d | 5803 | goto error_return; |
0ad989f9 L |
5804 | |
5805 | if (h == NULL) | |
5806 | continue; | |
5807 | ||
5808 | if (h->root.type == bfd_link_hash_common) | |
5809 | { | |
5810 | /* We currently have a common symbol. The archive map contains | |
5811 | a reference to this symbol, so we may want to include it. We | |
5812 | only want to include it however, if this archive element | |
5813 | contains a definition of the symbol, not just another common | |
5814 | declaration of it. | |
5815 | ||
5816 | Unfortunately some archivers (including GNU ar) will put | |
5817 | declarations of common symbols into their archive maps, as | |
5818 | well as real definitions, so we cannot just go by the archive | |
5819 | map alone. Instead we must read in the element's symbol | |
5820 | table and check that to see what kind of symbol definition | |
5821 | this is. */ | |
5822 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5823 | continue; | |
5824 | } | |
5825 | else if (h->root.type != bfd_link_hash_undefined) | |
5826 | { | |
5827 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5828 | /* Symbol must be defined. Don't check it again. */ |
5829 | included[i] = TRUE; | |
0ad989f9 L |
5830 | continue; |
5831 | } | |
5832 | ||
5833 | /* We need to include this archive member. */ | |
5834 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5835 | if (element == NULL) | |
5836 | goto error_return; | |
5837 | ||
5838 | if (! bfd_check_format (element, bfd_object)) | |
5839 | goto error_return; | |
5840 | ||
0ad989f9 L |
5841 | undefs_tail = info->hash->undefs_tail; |
5842 | ||
0e144ba7 AM |
5843 | if (!(*info->callbacks |
5844 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5845 | continue; |
0e144ba7 | 5846 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5847 | goto error_return; |
5848 | ||
5849 | /* If there are any new undefined symbols, we need to make | |
5850 | another pass through the archive in order to see whether | |
5851 | they can be defined. FIXME: This isn't perfect, because | |
5852 | common symbols wind up on undefs_tail and because an | |
5853 | undefined symbol which is defined later on in this pass | |
5854 | does not require another pass. This isn't a bug, but it | |
5855 | does make the code less efficient than it could be. */ | |
5856 | if (undefs_tail != info->hash->undefs_tail) | |
5857 | loop = TRUE; | |
5858 | ||
5859 | /* Look backward to mark all symbols from this object file | |
5860 | which we have already seen in this pass. */ | |
5861 | mark = i; | |
5862 | do | |
5863 | { | |
5864 | included[mark] = TRUE; | |
5865 | if (mark == 0) | |
5866 | break; | |
5867 | --mark; | |
5868 | } | |
5869 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5870 | ||
5871 | /* We mark subsequent symbols from this object file as we go | |
5872 | on through the loop. */ | |
5873 | last = symdef->file_offset; | |
5874 | } | |
5875 | } | |
5876 | while (loop); | |
5877 | ||
0ad989f9 | 5878 | free (included); |
0ad989f9 L |
5879 | return TRUE; |
5880 | ||
5881 | error_return: | |
c9594989 | 5882 | free (included); |
0ad989f9 L |
5883 | return FALSE; |
5884 | } | |
4ad4eba5 AM |
5885 | |
5886 | /* Given an ELF BFD, add symbols to the global hash table as | |
5887 | appropriate. */ | |
5888 | ||
5889 | bfd_boolean | |
5890 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5891 | { | |
5892 | switch (bfd_get_format (abfd)) | |
5893 | { | |
5894 | case bfd_object: | |
5895 | return elf_link_add_object_symbols (abfd, info); | |
5896 | case bfd_archive: | |
5897 | return elf_link_add_archive_symbols (abfd, info); | |
5898 | default: | |
5899 | bfd_set_error (bfd_error_wrong_format); | |
5900 | return FALSE; | |
5901 | } | |
5902 | } | |
5a580b3a | 5903 | \f |
14b1c01e AM |
5904 | struct hash_codes_info |
5905 | { | |
5906 | unsigned long *hashcodes; | |
5907 | bfd_boolean error; | |
5908 | }; | |
a0c8462f | 5909 | |
5a580b3a AM |
5910 | /* This function will be called though elf_link_hash_traverse to store |
5911 | all hash value of the exported symbols in an array. */ | |
5912 | ||
5913 | static bfd_boolean | |
5914 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5915 | { | |
a50b1753 | 5916 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5917 | const char *name; |
5a580b3a AM |
5918 | unsigned long ha; |
5919 | char *alc = NULL; | |
5920 | ||
5a580b3a AM |
5921 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5922 | if (h->dynindx == -1) | |
5923 | return TRUE; | |
5924 | ||
5925 | name = h->root.root.string; | |
422f1182 | 5926 | if (h->versioned >= versioned) |
5a580b3a | 5927 | { |
422f1182 L |
5928 | char *p = strchr (name, ELF_VER_CHR); |
5929 | if (p != NULL) | |
14b1c01e | 5930 | { |
422f1182 L |
5931 | alc = (char *) bfd_malloc (p - name + 1); |
5932 | if (alc == NULL) | |
5933 | { | |
5934 | inf->error = TRUE; | |
5935 | return FALSE; | |
5936 | } | |
5937 | memcpy (alc, name, p - name); | |
5938 | alc[p - name] = '\0'; | |
5939 | name = alc; | |
14b1c01e | 5940 | } |
5a580b3a AM |
5941 | } |
5942 | ||
5943 | /* Compute the hash value. */ | |
5944 | ha = bfd_elf_hash (name); | |
5945 | ||
5946 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5947 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5948 | |
5949 | /* And store it in the struct so that we can put it in the hash table | |
5950 | later. */ | |
f6e332e6 | 5951 | h->u.elf_hash_value = ha; |
5a580b3a | 5952 | |
c9594989 | 5953 | free (alc); |
5a580b3a AM |
5954 | return TRUE; |
5955 | } | |
5956 | ||
fdc90cb4 JJ |
5957 | struct collect_gnu_hash_codes |
5958 | { | |
5959 | bfd *output_bfd; | |
5960 | const struct elf_backend_data *bed; | |
5961 | unsigned long int nsyms; | |
5962 | unsigned long int maskbits; | |
5963 | unsigned long int *hashcodes; | |
5964 | unsigned long int *hashval; | |
5965 | unsigned long int *indx; | |
5966 | unsigned long int *counts; | |
5967 | bfd_vma *bitmask; | |
5968 | bfd_byte *contents; | |
f16a9783 | 5969 | bfd_size_type xlat; |
fdc90cb4 JJ |
5970 | long int min_dynindx; |
5971 | unsigned long int bucketcount; | |
5972 | unsigned long int symindx; | |
5973 | long int local_indx; | |
5974 | long int shift1, shift2; | |
5975 | unsigned long int mask; | |
14b1c01e | 5976 | bfd_boolean error; |
fdc90cb4 JJ |
5977 | }; |
5978 | ||
5979 | /* This function will be called though elf_link_hash_traverse to store | |
5980 | all hash value of the exported symbols in an array. */ | |
5981 | ||
5982 | static bfd_boolean | |
5983 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5984 | { | |
a50b1753 | 5985 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5986 | const char *name; |
fdc90cb4 JJ |
5987 | unsigned long ha; |
5988 | char *alc = NULL; | |
5989 | ||
fdc90cb4 JJ |
5990 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5991 | if (h->dynindx == -1) | |
5992 | return TRUE; | |
5993 | ||
5994 | /* Ignore also local symbols and undefined symbols. */ | |
5995 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5996 | return TRUE; | |
5997 | ||
5998 | name = h->root.root.string; | |
422f1182 | 5999 | if (h->versioned >= versioned) |
fdc90cb4 | 6000 | { |
422f1182 L |
6001 | char *p = strchr (name, ELF_VER_CHR); |
6002 | if (p != NULL) | |
14b1c01e | 6003 | { |
422f1182 L |
6004 | alc = (char *) bfd_malloc (p - name + 1); |
6005 | if (alc == NULL) | |
6006 | { | |
6007 | s->error = TRUE; | |
6008 | return FALSE; | |
6009 | } | |
6010 | memcpy (alc, name, p - name); | |
6011 | alc[p - name] = '\0'; | |
6012 | name = alc; | |
14b1c01e | 6013 | } |
fdc90cb4 JJ |
6014 | } |
6015 | ||
6016 | /* Compute the hash value. */ | |
6017 | ha = bfd_elf_gnu_hash (name); | |
6018 | ||
6019 | /* Store the found hash value in the array for compute_bucket_count, | |
6020 | and also for .dynsym reordering purposes. */ | |
6021 | s->hashcodes[s->nsyms] = ha; | |
6022 | s->hashval[h->dynindx] = ha; | |
6023 | ++s->nsyms; | |
6024 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
6025 | s->min_dynindx = h->dynindx; | |
6026 | ||
c9594989 | 6027 | free (alc); |
fdc90cb4 JJ |
6028 | return TRUE; |
6029 | } | |
6030 | ||
6031 | /* This function will be called though elf_link_hash_traverse to do | |
f16a9783 MS |
6032 | final dynamic symbol renumbering in case of .gnu.hash. |
6033 | If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index | |
6034 | to the translation table. */ | |
fdc90cb4 JJ |
6035 | |
6036 | static bfd_boolean | |
f16a9783 | 6037 | elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data) |
fdc90cb4 | 6038 | { |
a50b1753 | 6039 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
6040 | unsigned long int bucket; |
6041 | unsigned long int val; | |
6042 | ||
fdc90cb4 JJ |
6043 | /* Ignore indirect symbols. */ |
6044 | if (h->dynindx == -1) | |
6045 | return TRUE; | |
6046 | ||
6047 | /* Ignore also local symbols and undefined symbols. */ | |
6048 | if (! (*s->bed->elf_hash_symbol) (h)) | |
6049 | { | |
6050 | if (h->dynindx >= s->min_dynindx) | |
f16a9783 MS |
6051 | { |
6052 | if (s->bed->record_xhash_symbol != NULL) | |
6053 | { | |
6054 | (*s->bed->record_xhash_symbol) (h, 0); | |
6055 | s->local_indx++; | |
6056 | } | |
6057 | else | |
6058 | h->dynindx = s->local_indx++; | |
6059 | } | |
fdc90cb4 JJ |
6060 | return TRUE; |
6061 | } | |
6062 | ||
6063 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
6064 | val = (s->hashval[h->dynindx] >> s->shift1) | |
6065 | & ((s->maskbits >> s->shift1) - 1); | |
6066 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
6067 | s->bitmask[val] | |
6068 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
6069 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
6070 | if (s->counts[bucket] == 1) | |
6071 | /* Last element terminates the chain. */ | |
6072 | val |= 1; | |
6073 | bfd_put_32 (s->output_bfd, val, | |
6074 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
6075 | --s->counts[bucket]; | |
f16a9783 MS |
6076 | if (s->bed->record_xhash_symbol != NULL) |
6077 | { | |
6078 | bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4; | |
6079 | ||
6080 | (*s->bed->record_xhash_symbol) (h, xlat_loc); | |
6081 | } | |
6082 | else | |
6083 | h->dynindx = s->indx[bucket]++; | |
fdc90cb4 JJ |
6084 | return TRUE; |
6085 | } | |
6086 | ||
6087 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
6088 | ||
6089 | bfd_boolean | |
6090 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
6091 | { | |
6092 | return !(h->forced_local | |
6093 | || h->root.type == bfd_link_hash_undefined | |
6094 | || h->root.type == bfd_link_hash_undefweak | |
6095 | || ((h->root.type == bfd_link_hash_defined | |
6096 | || h->root.type == bfd_link_hash_defweak) | |
6097 | && h->root.u.def.section->output_section == NULL)); | |
6098 | } | |
6099 | ||
5a580b3a AM |
6100 | /* Array used to determine the number of hash table buckets to use |
6101 | based on the number of symbols there are. If there are fewer than | |
6102 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
6103 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
6104 | than 32771 buckets. */ | |
6105 | ||
6106 | static const size_t elf_buckets[] = | |
6107 | { | |
6108 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
6109 | 16411, 32771, 0 | |
6110 | }; | |
6111 | ||
6112 | /* Compute bucket count for hashing table. We do not use a static set | |
6113 | of possible tables sizes anymore. Instead we determine for all | |
6114 | possible reasonable sizes of the table the outcome (i.e., the | |
6115 | number of collisions etc) and choose the best solution. The | |
6116 | weighting functions are not too simple to allow the table to grow | |
6117 | without bounds. Instead one of the weighting factors is the size. | |
6118 | Therefore the result is always a good payoff between few collisions | |
6119 | (= short chain lengths) and table size. */ | |
6120 | static size_t | |
b20dd2ce | 6121 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
6122 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
6123 | unsigned long int nsyms, | |
6124 | int gnu_hash) | |
5a580b3a | 6125 | { |
5a580b3a | 6126 | size_t best_size = 0; |
5a580b3a | 6127 | unsigned long int i; |
5a580b3a | 6128 | |
5a580b3a AM |
6129 | /* We have a problem here. The following code to optimize the table |
6130 | size requires an integer type with more the 32 bits. If | |
6131 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
6132 | #ifdef BFD_HOST_U_64_BIT | |
6133 | if (info->optimize) | |
6134 | { | |
5a580b3a AM |
6135 | size_t minsize; |
6136 | size_t maxsize; | |
6137 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 6138 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 6139 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 6140 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 6141 | unsigned long int *counts; |
d40f3da9 | 6142 | bfd_size_type amt; |
0883b6e0 | 6143 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
6144 | |
6145 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
6146 | that the hashing table must at least have NSYMS/4 and at most | |
6147 | 2*NSYMS buckets. */ | |
6148 | minsize = nsyms / 4; | |
6149 | if (minsize == 0) | |
6150 | minsize = 1; | |
6151 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
6152 | if (gnu_hash) |
6153 | { | |
6154 | if (minsize < 2) | |
6155 | minsize = 2; | |
6156 | if ((best_size & 31) == 0) | |
6157 | ++best_size; | |
6158 | } | |
5a580b3a AM |
6159 | |
6160 | /* Create array where we count the collisions in. We must use bfd_malloc | |
6161 | since the size could be large. */ | |
6162 | amt = maxsize; | |
6163 | amt *= sizeof (unsigned long int); | |
a50b1753 | 6164 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 6165 | if (counts == NULL) |
fdc90cb4 | 6166 | return 0; |
5a580b3a AM |
6167 | |
6168 | /* Compute the "optimal" size for the hash table. The criteria is a | |
6169 | minimal chain length. The minor criteria is (of course) the size | |
6170 | of the table. */ | |
6171 | for (i = minsize; i < maxsize; ++i) | |
6172 | { | |
6173 | /* Walk through the array of hashcodes and count the collisions. */ | |
6174 | BFD_HOST_U_64_BIT max; | |
6175 | unsigned long int j; | |
6176 | unsigned long int fact; | |
6177 | ||
fdc90cb4 JJ |
6178 | if (gnu_hash && (i & 31) == 0) |
6179 | continue; | |
6180 | ||
5a580b3a AM |
6181 | memset (counts, '\0', i * sizeof (unsigned long int)); |
6182 | ||
6183 | /* Determine how often each hash bucket is used. */ | |
6184 | for (j = 0; j < nsyms; ++j) | |
6185 | ++counts[hashcodes[j] % i]; | |
6186 | ||
6187 | /* For the weight function we need some information about the | |
6188 | pagesize on the target. This is information need not be 100% | |
6189 | accurate. Since this information is not available (so far) we | |
6190 | define it here to a reasonable default value. If it is crucial | |
6191 | to have a better value some day simply define this value. */ | |
6192 | # ifndef BFD_TARGET_PAGESIZE | |
6193 | # define BFD_TARGET_PAGESIZE (4096) | |
6194 | # endif | |
6195 | ||
fdc90cb4 JJ |
6196 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
6197 | and the chains. */ | |
6198 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
6199 | |
6200 | # if 1 | |
6201 | /* Variant 1: optimize for short chains. We add the squares | |
6202 | of all the chain lengths (which favors many small chain | |
6203 | over a few long chains). */ | |
6204 | for (j = 0; j < i; ++j) | |
6205 | max += counts[j] * counts[j]; | |
6206 | ||
6207 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 6208 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6209 | max *= fact * fact; |
6210 | # else | |
6211 | /* Variant 2: Optimize a lot more for small table. Here we | |
6212 | also add squares of the size but we also add penalties for | |
6213 | empty slots (the +1 term). */ | |
6214 | for (j = 0; j < i; ++j) | |
6215 | max += (1 + counts[j]) * (1 + counts[j]); | |
6216 | ||
6217 | /* The overall size of the table is considered, but not as | |
6218 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 6219 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6220 | max *= fact; |
6221 | # endif | |
6222 | ||
6223 | /* Compare with current best results. */ | |
6224 | if (max < best_chlen) | |
6225 | { | |
6226 | best_chlen = max; | |
6227 | best_size = i; | |
ca4be51c | 6228 | no_improvement_count = 0; |
5a580b3a | 6229 | } |
0883b6e0 NC |
6230 | /* PR 11843: Avoid futile long searches for the best bucket size |
6231 | when there are a large number of symbols. */ | |
6232 | else if (++no_improvement_count == 100) | |
6233 | break; | |
5a580b3a AM |
6234 | } |
6235 | ||
6236 | free (counts); | |
6237 | } | |
6238 | else | |
6239 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
6240 | { | |
6241 | /* This is the fallback solution if no 64bit type is available or if we | |
6242 | are not supposed to spend much time on optimizations. We select the | |
6243 | bucket count using a fixed set of numbers. */ | |
6244 | for (i = 0; elf_buckets[i] != 0; i++) | |
6245 | { | |
6246 | best_size = elf_buckets[i]; | |
fdc90cb4 | 6247 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
6248 | break; |
6249 | } | |
fdc90cb4 JJ |
6250 | if (gnu_hash && best_size < 2) |
6251 | best_size = 2; | |
5a580b3a AM |
6252 | } |
6253 | ||
5a580b3a AM |
6254 | return best_size; |
6255 | } | |
6256 | ||
d0bf826b AM |
6257 | /* Size any SHT_GROUP section for ld -r. */ |
6258 | ||
6259 | bfd_boolean | |
6260 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
6261 | { | |
6262 | bfd *ibfd; | |
57963c05 | 6263 | asection *s; |
d0bf826b | 6264 | |
c72f2fb2 | 6265 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b | 6266 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
57963c05 AM |
6267 | && (s = ibfd->sections) != NULL |
6268 | && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
d0bf826b AM |
6269 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) |
6270 | return FALSE; | |
6271 | return TRUE; | |
6272 | } | |
6273 | ||
04c3a755 NS |
6274 | /* Set a default stack segment size. The value in INFO wins. If it |
6275 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
6276 | undefined it is initialized. */ | |
6277 | ||
6278 | bfd_boolean | |
6279 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
6280 | struct bfd_link_info *info, | |
6281 | const char *legacy_symbol, | |
6282 | bfd_vma default_size) | |
6283 | { | |
6284 | struct elf_link_hash_entry *h = NULL; | |
6285 | ||
6286 | /* Look for legacy symbol. */ | |
6287 | if (legacy_symbol) | |
6288 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
6289 | FALSE, FALSE, FALSE); | |
6290 | if (h && (h->root.type == bfd_link_hash_defined | |
6291 | || h->root.type == bfd_link_hash_defweak) | |
6292 | && h->def_regular | |
6293 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
6294 | { | |
6295 | /* The symbol has no type if specified on the command line. */ | |
6296 | h->type = STT_OBJECT; | |
6297 | if (info->stacksize) | |
695344c0 | 6298 | /* xgettext:c-format */ |
871b3ab2 | 6299 | _bfd_error_handler (_("%pB: stack size specified and %s set"), |
4eca0228 | 6300 | output_bfd, legacy_symbol); |
04c3a755 | 6301 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 6302 | /* xgettext:c-format */ |
871b3ab2 | 6303 | _bfd_error_handler (_("%pB: %s not absolute"), |
4eca0228 | 6304 | output_bfd, legacy_symbol); |
04c3a755 NS |
6305 | else |
6306 | info->stacksize = h->root.u.def.value; | |
6307 | } | |
6308 | ||
6309 | if (!info->stacksize) | |
6310 | /* If the user didn't set a size, or explicitly inhibit the | |
6311 | size, set it now. */ | |
6312 | info->stacksize = default_size; | |
6313 | ||
6314 | /* Provide the legacy symbol, if it is referenced. */ | |
6315 | if (h && (h->root.type == bfd_link_hash_undefined | |
6316 | || h->root.type == bfd_link_hash_undefweak)) | |
6317 | { | |
6318 | struct bfd_link_hash_entry *bh = NULL; | |
6319 | ||
6320 | if (!(_bfd_generic_link_add_one_symbol | |
6321 | (info, output_bfd, legacy_symbol, | |
6322 | BSF_GLOBAL, bfd_abs_section_ptr, | |
6323 | info->stacksize >= 0 ? info->stacksize : 0, | |
6324 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
6325 | return FALSE; | |
6326 | ||
6327 | h = (struct elf_link_hash_entry *) bh; | |
6328 | h->def_regular = 1; | |
6329 | h->type = STT_OBJECT; | |
6330 | } | |
6331 | ||
6332 | return TRUE; | |
6333 | } | |
6334 | ||
b531344c MR |
6335 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
6336 | ||
6337 | struct elf_gc_sweep_symbol_info | |
6338 | { | |
6339 | struct bfd_link_info *info; | |
6340 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
6341 | bfd_boolean); | |
6342 | }; | |
6343 | ||
6344 | static bfd_boolean | |
6345 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) | |
6346 | { | |
6347 | if (!h->mark | |
6348 | && (((h->root.type == bfd_link_hash_defined | |
6349 | || h->root.type == bfd_link_hash_defweak) | |
6350 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) | |
6351 | && h->root.u.def.section->gc_mark)) | |
6352 | || h->root.type == bfd_link_hash_undefined | |
6353 | || h->root.type == bfd_link_hash_undefweak)) | |
6354 | { | |
6355 | struct elf_gc_sweep_symbol_info *inf; | |
6356 | ||
6357 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
6358 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
6359 | h->def_regular = 0; | |
6360 | h->ref_regular = 0; | |
6361 | h->ref_regular_nonweak = 0; | |
6362 | } | |
6363 | ||
6364 | return TRUE; | |
6365 | } | |
6366 | ||
5a580b3a AM |
6367 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
6368 | called by the ELF linker emulation before_allocation routine. We | |
6369 | must set the sizes of the sections before the linker sets the | |
6370 | addresses of the various sections. */ | |
6371 | ||
6372 | bfd_boolean | |
6373 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
6374 | const char *soname, | |
6375 | const char *rpath, | |
6376 | const char *filter_shlib, | |
7ee314fa AM |
6377 | const char *audit, |
6378 | const char *depaudit, | |
5a580b3a AM |
6379 | const char * const *auxiliary_filters, |
6380 | struct bfd_link_info *info, | |
fd91d419 | 6381 | asection **sinterpptr) |
5a580b3a | 6382 | { |
5a580b3a AM |
6383 | bfd *dynobj; |
6384 | const struct elf_backend_data *bed; | |
5a580b3a AM |
6385 | |
6386 | *sinterpptr = NULL; | |
6387 | ||
5a580b3a AM |
6388 | if (!is_elf_hash_table (info->hash)) |
6389 | return TRUE; | |
6390 | ||
5a580b3a AM |
6391 | dynobj = elf_hash_table (info)->dynobj; |
6392 | ||
9a2a56cc | 6393 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6394 | { |
902e9fc7 MR |
6395 | struct bfd_elf_version_tree *verdefs; |
6396 | struct elf_info_failed asvinfo; | |
5a580b3a AM |
6397 | struct bfd_elf_version_tree *t; |
6398 | struct bfd_elf_version_expr *d; | |
902e9fc7 | 6399 | asection *s; |
e6699019 | 6400 | size_t soname_indx; |
7ee314fa | 6401 | |
5a580b3a AM |
6402 | /* If we are supposed to export all symbols into the dynamic symbol |
6403 | table (this is not the normal case), then do so. */ | |
55255dae | 6404 | if (info->export_dynamic |
0e1862bb | 6405 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a | 6406 | { |
3d13f3e9 AM |
6407 | struct elf_info_failed eif; |
6408 | ||
6409 | eif.info = info; | |
6410 | eif.failed = FALSE; | |
5a580b3a AM |
6411 | elf_link_hash_traverse (elf_hash_table (info), |
6412 | _bfd_elf_export_symbol, | |
6413 | &eif); | |
6414 | if (eif.failed) | |
6415 | return FALSE; | |
6416 | } | |
6417 | ||
e6699019 L |
6418 | if (soname != NULL) |
6419 | { | |
6420 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6421 | soname, TRUE); | |
6422 | if (soname_indx == (size_t) -1 | |
6423 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
6424 | return FALSE; | |
6425 | } | |
6426 | else | |
6427 | soname_indx = (size_t) -1; | |
6428 | ||
5a580b3a | 6429 | /* Make all global versions with definition. */ |
fd91d419 | 6430 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6431 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6432 | if (!d->symver && d->literal) |
5a580b3a AM |
6433 | { |
6434 | const char *verstr, *name; | |
6435 | size_t namelen, verlen, newlen; | |
93252b1c | 6436 | char *newname, *p, leading_char; |
5a580b3a AM |
6437 | struct elf_link_hash_entry *newh; |
6438 | ||
93252b1c | 6439 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6440 | name = d->pattern; |
93252b1c | 6441 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6442 | verstr = t->name; |
6443 | verlen = strlen (verstr); | |
6444 | newlen = namelen + verlen + 3; | |
6445 | ||
a50b1753 | 6446 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6447 | if (newname == NULL) |
6448 | return FALSE; | |
93252b1c MF |
6449 | newname[0] = leading_char; |
6450 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6451 | |
6452 | /* Check the hidden versioned definition. */ | |
6453 | p = newname + namelen; | |
6454 | *p++ = ELF_VER_CHR; | |
6455 | memcpy (p, verstr, verlen + 1); | |
6456 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6457 | newname, FALSE, FALSE, | |
6458 | FALSE); | |
6459 | if (newh == NULL | |
6460 | || (newh->root.type != bfd_link_hash_defined | |
6461 | && newh->root.type != bfd_link_hash_defweak)) | |
6462 | { | |
6463 | /* Check the default versioned definition. */ | |
6464 | *p++ = ELF_VER_CHR; | |
6465 | memcpy (p, verstr, verlen + 1); | |
6466 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6467 | newname, FALSE, FALSE, | |
6468 | FALSE); | |
6469 | } | |
6470 | free (newname); | |
6471 | ||
6472 | /* Mark this version if there is a definition and it is | |
6473 | not defined in a shared object. */ | |
6474 | if (newh != NULL | |
f5385ebf | 6475 | && !newh->def_dynamic |
5a580b3a AM |
6476 | && (newh->root.type == bfd_link_hash_defined |
6477 | || newh->root.type == bfd_link_hash_defweak)) | |
6478 | d->symver = 1; | |
6479 | } | |
6480 | ||
6481 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6482 | asvinfo.info = info; |
5a580b3a AM |
6483 | asvinfo.failed = FALSE; |
6484 | ||
6485 | elf_link_hash_traverse (elf_hash_table (info), | |
6486 | _bfd_elf_link_assign_sym_version, | |
6487 | &asvinfo); | |
6488 | if (asvinfo.failed) | |
6489 | return FALSE; | |
6490 | ||
6491 | if (!info->allow_undefined_version) | |
6492 | { | |
6493 | /* Check if all global versions have a definition. */ | |
3d13f3e9 | 6494 | bfd_boolean all_defined = TRUE; |
fd91d419 | 6495 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6496 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6497 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6498 | { |
4eca0228 | 6499 | _bfd_error_handler |
5a580b3a AM |
6500 | (_("%s: undefined version: %s"), |
6501 | d->pattern, t->name); | |
6502 | all_defined = FALSE; | |
6503 | } | |
6504 | ||
6505 | if (!all_defined) | |
6506 | { | |
6507 | bfd_set_error (bfd_error_bad_value); | |
6508 | return FALSE; | |
6509 | } | |
6510 | } | |
6511 | ||
902e9fc7 MR |
6512 | /* Set up the version definition section. */ |
6513 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); | |
6514 | BFD_ASSERT (s != NULL); | |
5a580b3a | 6515 | |
902e9fc7 MR |
6516 | /* We may have created additional version definitions if we are |
6517 | just linking a regular application. */ | |
6518 | verdefs = info->version_info; | |
5a580b3a | 6519 | |
902e9fc7 MR |
6520 | /* Skip anonymous version tag. */ |
6521 | if (verdefs != NULL && verdefs->vernum == 0) | |
6522 | verdefs = verdefs->next; | |
5a580b3a | 6523 | |
902e9fc7 MR |
6524 | if (verdefs == NULL && !info->create_default_symver) |
6525 | s->flags |= SEC_EXCLUDE; | |
6526 | else | |
5a580b3a | 6527 | { |
902e9fc7 MR |
6528 | unsigned int cdefs; |
6529 | bfd_size_type size; | |
6530 | bfd_byte *p; | |
6531 | Elf_Internal_Verdef def; | |
6532 | Elf_Internal_Verdaux defaux; | |
6533 | struct bfd_link_hash_entry *bh; | |
6534 | struct elf_link_hash_entry *h; | |
6535 | const char *name; | |
5a580b3a | 6536 | |
902e9fc7 MR |
6537 | cdefs = 0; |
6538 | size = 0; | |
5a580b3a | 6539 | |
902e9fc7 MR |
6540 | /* Make space for the base version. */ |
6541 | size += sizeof (Elf_External_Verdef); | |
6542 | size += sizeof (Elf_External_Verdaux); | |
6543 | ++cdefs; | |
6544 | ||
6545 | /* Make space for the default version. */ | |
6546 | if (info->create_default_symver) | |
6547 | { | |
6548 | size += sizeof (Elf_External_Verdef); | |
6549 | ++cdefs; | |
3e3b46e5 PB |
6550 | } |
6551 | ||
5a580b3a AM |
6552 | for (t = verdefs; t != NULL; t = t->next) |
6553 | { | |
6554 | struct bfd_elf_version_deps *n; | |
6555 | ||
a6cc6b3b RO |
6556 | /* Don't emit base version twice. */ |
6557 | if (t->vernum == 0) | |
6558 | continue; | |
6559 | ||
5a580b3a AM |
6560 | size += sizeof (Elf_External_Verdef); |
6561 | size += sizeof (Elf_External_Verdaux); | |
6562 | ++cdefs; | |
6563 | ||
6564 | for (n = t->deps; n != NULL; n = n->next) | |
6565 | size += sizeof (Elf_External_Verdaux); | |
6566 | } | |
6567 | ||
eea6121a | 6568 | s->size = size; |
a50b1753 | 6569 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6570 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6571 | return FALSE; |
6572 | ||
6573 | /* Fill in the version definition section. */ | |
6574 | ||
6575 | p = s->contents; | |
6576 | ||
6577 | def.vd_version = VER_DEF_CURRENT; | |
6578 | def.vd_flags = VER_FLG_BASE; | |
6579 | def.vd_ndx = 1; | |
6580 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6581 | if (info->create_default_symver) |
6582 | { | |
6583 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6584 | def.vd_next = sizeof (Elf_External_Verdef); | |
6585 | } | |
6586 | else | |
6587 | { | |
6588 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6589 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6590 | + sizeof (Elf_External_Verdaux)); | |
6591 | } | |
5a580b3a | 6592 | |
ef53be89 | 6593 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6594 | { |
6595 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6596 | soname_indx); | |
6597 | def.vd_hash = bfd_elf_hash (soname); | |
6598 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6599 | name = soname; |
5a580b3a AM |
6600 | } |
6601 | else | |
6602 | { | |
ef53be89 | 6603 | size_t indx; |
5a580b3a | 6604 | |
765cf5f6 | 6605 | name = lbasename (bfd_get_filename (output_bfd)); |
5a580b3a AM |
6606 | def.vd_hash = bfd_elf_hash (name); |
6607 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6608 | name, FALSE); | |
ef53be89 | 6609 | if (indx == (size_t) -1) |
5a580b3a AM |
6610 | return FALSE; |
6611 | defaux.vda_name = indx; | |
6612 | } | |
6613 | defaux.vda_next = 0; | |
6614 | ||
6615 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6616 | (Elf_External_Verdef *) p); | |
6617 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6618 | if (info->create_default_symver) |
6619 | { | |
6620 | /* Add a symbol representing this version. */ | |
6621 | bh = NULL; | |
6622 | if (! (_bfd_generic_link_add_one_symbol | |
6623 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6624 | 0, NULL, FALSE, | |
6625 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6626 | return FALSE; | |
6627 | h = (struct elf_link_hash_entry *) bh; | |
6628 | h->non_elf = 0; | |
6629 | h->def_regular = 1; | |
6630 | h->type = STT_OBJECT; | |
6631 | h->verinfo.vertree = NULL; | |
6632 | ||
6633 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6634 | return FALSE; | |
6635 | ||
6636 | /* Create a duplicate of the base version with the same | |
6637 | aux block, but different flags. */ | |
6638 | def.vd_flags = 0; | |
6639 | def.vd_ndx = 2; | |
6640 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6641 | if (verdefs) | |
6642 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6643 | + sizeof (Elf_External_Verdaux)); | |
6644 | else | |
6645 | def.vd_next = 0; | |
6646 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6647 | (Elf_External_Verdef *) p); | |
6648 | p += sizeof (Elf_External_Verdef); | |
6649 | } | |
5a580b3a AM |
6650 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6651 | (Elf_External_Verdaux *) p); | |
6652 | p += sizeof (Elf_External_Verdaux); | |
6653 | ||
6654 | for (t = verdefs; t != NULL; t = t->next) | |
6655 | { | |
6656 | unsigned int cdeps; | |
6657 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6658 | |
a6cc6b3b RO |
6659 | /* Don't emit the base version twice. */ |
6660 | if (t->vernum == 0) | |
6661 | continue; | |
6662 | ||
5a580b3a AM |
6663 | cdeps = 0; |
6664 | for (n = t->deps; n != NULL; n = n->next) | |
6665 | ++cdeps; | |
6666 | ||
6667 | /* Add a symbol representing this version. */ | |
6668 | bh = NULL; | |
6669 | if (! (_bfd_generic_link_add_one_symbol | |
6670 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6671 | 0, NULL, FALSE, | |
6672 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6673 | return FALSE; | |
6674 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6675 | h->non_elf = 0; |
6676 | h->def_regular = 1; | |
5a580b3a AM |
6677 | h->type = STT_OBJECT; |
6678 | h->verinfo.vertree = t; | |
6679 | ||
c152c796 | 6680 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6681 | return FALSE; |
6682 | ||
6683 | def.vd_version = VER_DEF_CURRENT; | |
6684 | def.vd_flags = 0; | |
6685 | if (t->globals.list == NULL | |
6686 | && t->locals.list == NULL | |
6687 | && ! t->used) | |
6688 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6689 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6690 | def.vd_cnt = cdeps + 1; |
6691 | def.vd_hash = bfd_elf_hash (t->name); | |
6692 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6693 | def.vd_next = 0; | |
a6cc6b3b RO |
6694 | |
6695 | /* If a basever node is next, it *must* be the last node in | |
6696 | the chain, otherwise Verdef construction breaks. */ | |
6697 | if (t->next != NULL && t->next->vernum == 0) | |
6698 | BFD_ASSERT (t->next->next == NULL); | |
6699 | ||
6700 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6701 | def.vd_next = (sizeof (Elf_External_Verdef) |
6702 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6703 | ||
6704 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6705 | (Elf_External_Verdef *) p); | |
6706 | p += sizeof (Elf_External_Verdef); | |
6707 | ||
6708 | defaux.vda_name = h->dynstr_index; | |
6709 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6710 | h->dynstr_index); | |
6711 | defaux.vda_next = 0; | |
6712 | if (t->deps != NULL) | |
6713 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6714 | t->name_indx = defaux.vda_name; | |
6715 | ||
6716 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6717 | (Elf_External_Verdaux *) p); | |
6718 | p += sizeof (Elf_External_Verdaux); | |
6719 | ||
6720 | for (n = t->deps; n != NULL; n = n->next) | |
6721 | { | |
6722 | if (n->version_needed == NULL) | |
6723 | { | |
6724 | /* This can happen if there was an error in the | |
6725 | version script. */ | |
6726 | defaux.vda_name = 0; | |
6727 | } | |
6728 | else | |
6729 | { | |
6730 | defaux.vda_name = n->version_needed->name_indx; | |
6731 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6732 | defaux.vda_name); | |
6733 | } | |
6734 | if (n->next == NULL) | |
6735 | defaux.vda_next = 0; | |
6736 | else | |
6737 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6738 | ||
6739 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6740 | (Elf_External_Verdaux *) p); | |
6741 | p += sizeof (Elf_External_Verdaux); | |
6742 | } | |
6743 | } | |
6744 | ||
5a580b3a AM |
6745 | elf_tdata (output_bfd)->cverdefs = cdefs; |
6746 | } | |
902e9fc7 MR |
6747 | } |
6748 | ||
6749 | bed = get_elf_backend_data (output_bfd); | |
6750 | ||
6751 | if (info->gc_sections && bed->can_gc_sections) | |
6752 | { | |
6753 | struct elf_gc_sweep_symbol_info sweep_info; | |
902e9fc7 MR |
6754 | |
6755 | /* Remove the symbols that were in the swept sections from the | |
3d13f3e9 | 6756 | dynamic symbol table. */ |
902e9fc7 MR |
6757 | sweep_info.info = info; |
6758 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
6759 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
6760 | &sweep_info); | |
3d13f3e9 AM |
6761 | } |
6762 | ||
6763 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6764 | { | |
6765 | asection *s; | |
6766 | struct elf_find_verdep_info sinfo; | |
6767 | ||
6768 | /* Work out the size of the version reference section. */ | |
6769 | ||
6770 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); | |
6771 | BFD_ASSERT (s != NULL); | |
902e9fc7 | 6772 | |
3d13f3e9 AM |
6773 | sinfo.info = info; |
6774 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6775 | if (sinfo.vers == 0) | |
6776 | sinfo.vers = 1; | |
6777 | sinfo.failed = FALSE; | |
6778 | ||
6779 | elf_link_hash_traverse (elf_hash_table (info), | |
6780 | _bfd_elf_link_find_version_dependencies, | |
6781 | &sinfo); | |
6782 | if (sinfo.failed) | |
6783 | return FALSE; | |
6784 | ||
6785 | if (elf_tdata (output_bfd)->verref == NULL) | |
6786 | s->flags |= SEC_EXCLUDE; | |
6787 | else | |
6788 | { | |
6789 | Elf_Internal_Verneed *vn; | |
6790 | unsigned int size; | |
6791 | unsigned int crefs; | |
6792 | bfd_byte *p; | |
6793 | ||
6794 | /* Build the version dependency section. */ | |
6795 | size = 0; | |
6796 | crefs = 0; | |
6797 | for (vn = elf_tdata (output_bfd)->verref; | |
6798 | vn != NULL; | |
6799 | vn = vn->vn_nextref) | |
6800 | { | |
6801 | Elf_Internal_Vernaux *a; | |
6802 | ||
6803 | size += sizeof (Elf_External_Verneed); | |
6804 | ++crefs; | |
6805 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6806 | size += sizeof (Elf_External_Vernaux); | |
6807 | } | |
6808 | ||
6809 | s->size = size; | |
6810 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); | |
6811 | if (s->contents == NULL) | |
6812 | return FALSE; | |
6813 | ||
6814 | p = s->contents; | |
6815 | for (vn = elf_tdata (output_bfd)->verref; | |
6816 | vn != NULL; | |
6817 | vn = vn->vn_nextref) | |
6818 | { | |
6819 | unsigned int caux; | |
6820 | Elf_Internal_Vernaux *a; | |
6821 | size_t indx; | |
6822 | ||
6823 | caux = 0; | |
6824 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6825 | ++caux; | |
6826 | ||
6827 | vn->vn_version = VER_NEED_CURRENT; | |
6828 | vn->vn_cnt = caux; | |
6829 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6830 | elf_dt_name (vn->vn_bfd) != NULL | |
6831 | ? elf_dt_name (vn->vn_bfd) | |
765cf5f6 AM |
6832 | : lbasename (bfd_get_filename |
6833 | (vn->vn_bfd)), | |
3d13f3e9 AM |
6834 | FALSE); |
6835 | if (indx == (size_t) -1) | |
6836 | return FALSE; | |
6837 | vn->vn_file = indx; | |
6838 | vn->vn_aux = sizeof (Elf_External_Verneed); | |
6839 | if (vn->vn_nextref == NULL) | |
6840 | vn->vn_next = 0; | |
6841 | else | |
6842 | vn->vn_next = (sizeof (Elf_External_Verneed) | |
6843 | + caux * sizeof (Elf_External_Vernaux)); | |
6844 | ||
6845 | _bfd_elf_swap_verneed_out (output_bfd, vn, | |
6846 | (Elf_External_Verneed *) p); | |
6847 | p += sizeof (Elf_External_Verneed); | |
6848 | ||
6849 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6850 | { | |
6851 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6852 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6853 | a->vna_nodename, FALSE); | |
6854 | if (indx == (size_t) -1) | |
6855 | return FALSE; | |
6856 | a->vna_name = indx; | |
6857 | if (a->vna_nextptr == NULL) | |
6858 | a->vna_next = 0; | |
6859 | else | |
6860 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6861 | ||
6862 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6863 | (Elf_External_Vernaux *) p); | |
6864 | p += sizeof (Elf_External_Vernaux); | |
6865 | } | |
6866 | } | |
6867 | ||
6868 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6869 | } | |
902e9fc7 MR |
6870 | } |
6871 | ||
6872 | /* Any syms created from now on start with -1 in | |
6873 | got.refcount/offset and plt.refcount/offset. */ | |
6874 | elf_hash_table (info)->init_got_refcount | |
6875 | = elf_hash_table (info)->init_got_offset; | |
6876 | elf_hash_table (info)->init_plt_refcount | |
6877 | = elf_hash_table (info)->init_plt_offset; | |
6878 | ||
6879 | if (bfd_link_relocatable (info) | |
6880 | && !_bfd_elf_size_group_sections (info)) | |
6881 | return FALSE; | |
6882 | ||
6883 | /* The backend may have to create some sections regardless of whether | |
6884 | we're dynamic or not. */ | |
6885 | if (bed->elf_backend_always_size_sections | |
6886 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
6887 | return FALSE; | |
6888 | ||
6889 | /* Determine any GNU_STACK segment requirements, after the backend | |
6890 | has had a chance to set a default segment size. */ | |
6891 | if (info->execstack) | |
6892 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; | |
6893 | else if (info->noexecstack) | |
6894 | elf_stack_flags (output_bfd) = PF_R | PF_W; | |
6895 | else | |
6896 | { | |
6897 | bfd *inputobj; | |
6898 | asection *notesec = NULL; | |
6899 | int exec = 0; | |
6900 | ||
6901 | for (inputobj = info->input_bfds; | |
6902 | inputobj; | |
6903 | inputobj = inputobj->link.next) | |
6904 | { | |
6905 | asection *s; | |
6906 | ||
6907 | if (inputobj->flags | |
6908 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
6909 | continue; | |
57963c05 AM |
6910 | s = inputobj->sections; |
6911 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
6912 | continue; | |
6913 | ||
902e9fc7 MR |
6914 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); |
6915 | if (s) | |
6916 | { | |
6917 | if (s->flags & SEC_CODE) | |
6918 | exec = PF_X; | |
6919 | notesec = s; | |
6920 | } | |
6921 | else if (bed->default_execstack) | |
6922 | exec = PF_X; | |
6923 | } | |
6924 | if (notesec || info->stacksize > 0) | |
6925 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; | |
6926 | if (notesec && exec && bfd_link_relocatable (info) | |
6927 | && notesec->output_section != bfd_abs_section_ptr) | |
6928 | notesec->output_section->flags |= SEC_CODE; | |
6929 | } | |
6930 | ||
6931 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6932 | { | |
6933 | struct elf_info_failed eif; | |
6934 | struct elf_link_hash_entry *h; | |
6935 | asection *dynstr; | |
6936 | asection *s; | |
6937 | ||
6938 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); | |
6939 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); | |
6940 | ||
902e9fc7 MR |
6941 | if (info->symbolic) |
6942 | { | |
6943 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
6944 | return FALSE; | |
6945 | info->flags |= DF_SYMBOLIC; | |
6946 | } | |
6947 | ||
6948 | if (rpath != NULL) | |
6949 | { | |
6950 | size_t indx; | |
6951 | bfd_vma tag; | |
6952 | ||
6953 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
6954 | TRUE); | |
6955 | if (indx == (size_t) -1) | |
6956 | return FALSE; | |
6957 | ||
6958 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; | |
6959 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
6960 | return FALSE; | |
6961 | } | |
6962 | ||
6963 | if (filter_shlib != NULL) | |
6964 | { | |
6965 | size_t indx; | |
6966 | ||
6967 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6968 | filter_shlib, TRUE); | |
6969 | if (indx == (size_t) -1 | |
6970 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
6971 | return FALSE; | |
6972 | } | |
6973 | ||
6974 | if (auxiliary_filters != NULL) | |
6975 | { | |
6976 | const char * const *p; | |
6977 | ||
6978 | for (p = auxiliary_filters; *p != NULL; p++) | |
6979 | { | |
6980 | size_t indx; | |
6981 | ||
6982 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6983 | *p, TRUE); | |
6984 | if (indx == (size_t) -1 | |
6985 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
6986 | return FALSE; | |
6987 | } | |
6988 | } | |
6989 | ||
6990 | if (audit != NULL) | |
6991 | { | |
6992 | size_t indx; | |
6993 | ||
6994 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
6995 | TRUE); | |
6996 | if (indx == (size_t) -1 | |
6997 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
6998 | return FALSE; | |
6999 | } | |
7000 | ||
7001 | if (depaudit != NULL) | |
7002 | { | |
7003 | size_t indx; | |
7004 | ||
7005 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
7006 | TRUE); | |
7007 | if (indx == (size_t) -1 | |
7008 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
7009 | return FALSE; | |
7010 | } | |
7011 | ||
7012 | eif.info = info; | |
7013 | eif.failed = FALSE; | |
7014 | ||
7015 | /* Find all symbols which were defined in a dynamic object and make | |
7016 | the backend pick a reasonable value for them. */ | |
7017 | elf_link_hash_traverse (elf_hash_table (info), | |
7018 | _bfd_elf_adjust_dynamic_symbol, | |
7019 | &eif); | |
7020 | if (eif.failed) | |
7021 | return FALSE; | |
7022 | ||
7023 | /* Add some entries to the .dynamic section. We fill in some of the | |
7024 | values later, in bfd_elf_final_link, but we must add the entries | |
7025 | now so that we know the final size of the .dynamic section. */ | |
7026 | ||
7027 | /* If there are initialization and/or finalization functions to | |
7028 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
7029 | h = (info->init_function | |
7030 | ? elf_link_hash_lookup (elf_hash_table (info), | |
7031 | info->init_function, FALSE, | |
7032 | FALSE, FALSE) | |
7033 | : NULL); | |
7034 | if (h != NULL | |
7035 | && (h->ref_regular | |
7036 | || h->def_regular)) | |
7037 | { | |
7038 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
7039 | return FALSE; | |
7040 | } | |
7041 | h = (info->fini_function | |
7042 | ? elf_link_hash_lookup (elf_hash_table (info), | |
7043 | info->fini_function, FALSE, | |
7044 | FALSE, FALSE) | |
7045 | : NULL); | |
7046 | if (h != NULL | |
7047 | && (h->ref_regular | |
7048 | || h->def_regular)) | |
7049 | { | |
7050 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
7051 | return FALSE; | |
7052 | } | |
7053 | ||
7054 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); | |
7055 | if (s != NULL && s->linker_has_input) | |
7056 | { | |
7057 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
7058 | if (! bfd_link_executable (info)) | |
7059 | { | |
7060 | bfd *sub; | |
7061 | asection *o; | |
7062 | ||
57963c05 AM |
7063 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
7064 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
7065 | && (o = sub->sections) != NULL | |
7066 | && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) | |
902e9fc7 MR |
7067 | for (o = sub->sections; o != NULL; o = o->next) |
7068 | if (elf_section_data (o)->this_hdr.sh_type | |
7069 | == SHT_PREINIT_ARRAY) | |
7070 | { | |
7071 | _bfd_error_handler | |
871b3ab2 | 7072 | (_("%pB: .preinit_array section is not allowed in DSO"), |
902e9fc7 MR |
7073 | sub); |
7074 | break; | |
7075 | } | |
7076 | ||
7077 | bfd_set_error (bfd_error_nonrepresentable_section); | |
7078 | return FALSE; | |
7079 | } | |
7080 | ||
7081 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
7082 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
7083 | return FALSE; | |
7084 | } | |
7085 | s = bfd_get_section_by_name (output_bfd, ".init_array"); | |
7086 | if (s != NULL && s->linker_has_input) | |
7087 | { | |
7088 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
7089 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
7090 | return FALSE; | |
7091 | } | |
7092 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); | |
7093 | if (s != NULL && s->linker_has_input) | |
7094 | { | |
7095 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
7096 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
7097 | return FALSE; | |
7098 | } | |
7099 | ||
7100 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); | |
7101 | /* If .dynstr is excluded from the link, we don't want any of | |
7102 | these tags. Strictly, we should be checking each section | |
7103 | individually; This quick check covers for the case where | |
7104 | someone does a /DISCARD/ : { *(*) }. */ | |
7105 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
7106 | { | |
7107 | bfd_size_type strsize; | |
7108 | ||
7109 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
7110 | if ((info->emit_hash | |
7111 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
7112 | || (info->emit_gnu_hash | |
f16a9783 MS |
7113 | && (bed->record_xhash_symbol == NULL |
7114 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))) | |
902e9fc7 MR |
7115 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
7116 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
7117 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
7118 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
7119 | bed->s->sizeof_sym)) | |
7120 | return FALSE; | |
7121 | } | |
7122 | } | |
7123 | ||
7124 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
7125 | return FALSE; | |
7126 | ||
7127 | /* The backend must work out the sizes of all the other dynamic | |
7128 | sections. */ | |
7129 | if (dynobj != NULL | |
7130 | && bed->elf_backend_size_dynamic_sections != NULL | |
7131 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
7132 | return FALSE; | |
7133 | ||
7134 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
7135 | { | |
902e9fc7 MR |
7136 | if (elf_tdata (output_bfd)->cverdefs) |
7137 | { | |
7138 | unsigned int crefs = elf_tdata (output_bfd)->cverdefs; | |
7139 | ||
7140 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
7141 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) | |
7142 | return FALSE; | |
7143 | } | |
7144 | ||
7145 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
7146 | { | |
7147 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
7148 | return FALSE; | |
7149 | } | |
7150 | else if (info->flags & DF_BIND_NOW) | |
7151 | { | |
7152 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
7153 | return FALSE; | |
7154 | } | |
7155 | ||
7156 | if (info->flags_1) | |
7157 | { | |
7158 | if (bfd_link_executable (info)) | |
7159 | info->flags_1 &= ~ (DF_1_INITFIRST | |
7160 | | DF_1_NODELETE | |
7161 | | DF_1_NOOPEN); | |
7162 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
7163 | return FALSE; | |
7164 | } | |
7165 | ||
7166 | if (elf_tdata (output_bfd)->cverrefs) | |
7167 | { | |
7168 | unsigned int crefs = elf_tdata (output_bfd)->cverrefs; | |
7169 | ||
7170 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
7171 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
7172 | return FALSE; | |
7173 | } | |
5a580b3a | 7174 | |
8423293d AM |
7175 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
7176 | && elf_tdata (output_bfd)->cverdefs == 0) | |
63f452a8 | 7177 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) |
8423293d | 7178 | { |
902e9fc7 MR |
7179 | asection *s; |
7180 | ||
3d4d4302 | 7181 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
7182 | s->flags |= SEC_EXCLUDE; |
7183 | } | |
7184 | } | |
7185 | return TRUE; | |
7186 | } | |
7187 | ||
74541ad4 AM |
7188 | /* Find the first non-excluded output section. We'll use its |
7189 | section symbol for some emitted relocs. */ | |
7190 | void | |
7191 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
7192 | { | |
7193 | asection *s; | |
f26a3287 | 7194 | asection *found = NULL; |
74541ad4 AM |
7195 | |
7196 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
7197 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
d00dd7dc | 7198 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7199 | { |
f26a3287 AM |
7200 | found = s; |
7201 | if ((s->flags & SEC_THREAD_LOCAL) == 0) | |
7202 | break; | |
74541ad4 | 7203 | } |
f26a3287 | 7204 | elf_hash_table (info)->text_index_section = found; |
74541ad4 AM |
7205 | } |
7206 | ||
7207 | /* Find two non-excluded output sections, one for code, one for data. | |
7208 | We'll use their section symbols for some emitted relocs. */ | |
7209 | void | |
7210 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
7211 | { | |
7212 | asection *s; | |
f26a3287 | 7213 | asection *found = NULL; |
74541ad4 | 7214 | |
266b05cf | 7215 | /* Data first, since setting text_index_section changes |
7f923b7f | 7216 | _bfd_elf_omit_section_dynsym_default. */ |
74541ad4 | 7217 | for (s = output_bfd->sections; s != NULL; s = s->next) |
f26a3287 AM |
7218 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC |
7219 | && !(s->flags & SEC_READONLY) | |
d00dd7dc | 7220 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7221 | { |
f26a3287 AM |
7222 | found = s; |
7223 | if ((s->flags & SEC_THREAD_LOCAL) == 0) | |
7224 | break; | |
74541ad4 | 7225 | } |
f26a3287 | 7226 | elf_hash_table (info)->data_index_section = found; |
74541ad4 AM |
7227 | |
7228 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
f26a3287 AM |
7229 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC |
7230 | && (s->flags & SEC_READONLY) | |
d00dd7dc | 7231 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7232 | { |
f26a3287 | 7233 | found = s; |
74541ad4 AM |
7234 | break; |
7235 | } | |
f26a3287 | 7236 | elf_hash_table (info)->text_index_section = found; |
74541ad4 AM |
7237 | } |
7238 | ||
f16a9783 MS |
7239 | #define GNU_HASH_SECTION_NAME(bed) \ |
7240 | (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash" | |
7241 | ||
8423293d AM |
7242 | bfd_boolean |
7243 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
7244 | { | |
74541ad4 | 7245 | const struct elf_backend_data *bed; |
23ec1e32 | 7246 | unsigned long section_sym_count; |
96d01d93 | 7247 | bfd_size_type dynsymcount = 0; |
74541ad4 | 7248 | |
8423293d AM |
7249 | if (!is_elf_hash_table (info->hash)) |
7250 | return TRUE; | |
7251 | ||
74541ad4 AM |
7252 | bed = get_elf_backend_data (output_bfd); |
7253 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
7254 | ||
23ec1e32 MR |
7255 | /* Assign dynsym indices. In a shared library we generate a section |
7256 | symbol for each output section, which come first. Next come all | |
7257 | of the back-end allocated local dynamic syms, followed by the rest | |
7258 | of the global symbols. | |
7259 | ||
7260 | This is usually not needed for static binaries, however backends | |
7261 | can request to always do it, e.g. the MIPS backend uses dynamic | |
7262 | symbol counts to lay out GOT, which will be produced in the | |
7263 | presence of GOT relocations even in static binaries (holding fixed | |
7264 | data in that case, to satisfy those relocations). */ | |
7265 | ||
7266 | if (elf_hash_table (info)->dynamic_sections_created | |
7267 | || bed->always_renumber_dynsyms) | |
7268 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
7269 | §ion_sym_count); | |
7270 | ||
8423293d AM |
7271 | if (elf_hash_table (info)->dynamic_sections_created) |
7272 | { | |
7273 | bfd *dynobj; | |
8423293d | 7274 | asection *s; |
8423293d AM |
7275 | unsigned int dtagcount; |
7276 | ||
7277 | dynobj = elf_hash_table (info)->dynobj; | |
7278 | ||
5a580b3a | 7279 | /* Work out the size of the symbol version section. */ |
3d4d4302 | 7280 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 7281 | BFD_ASSERT (s != NULL); |
d5486c43 | 7282 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 7283 | { |
eea6121a | 7284 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 7285 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
7286 | if (s->contents == NULL) |
7287 | return FALSE; | |
7288 | ||
7289 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
7290 | return FALSE; | |
7291 | } | |
7292 | ||
7293 | /* Set the size of the .dynsym and .hash sections. We counted | |
7294 | the number of dynamic symbols in elf_link_add_object_symbols. | |
7295 | We will build the contents of .dynsym and .hash when we build | |
7296 | the final symbol table, because until then we do not know the | |
7297 | correct value to give the symbols. We built the .dynstr | |
7298 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 7299 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 7300 | BFD_ASSERT (s != NULL); |
eea6121a | 7301 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 7302 | |
d5486c43 L |
7303 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
7304 | if (s->contents == NULL) | |
7305 | return FALSE; | |
5a580b3a | 7306 | |
d5486c43 L |
7307 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
7308 | section syms, in case we don't output them all. */ | |
7309 | ++section_sym_count; | |
7310 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 7311 | |
fdc90cb4 JJ |
7312 | elf_hash_table (info)->bucketcount = 0; |
7313 | ||
5a580b3a AM |
7314 | /* Compute the size of the hashing table. As a side effect this |
7315 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
7316 | if (info->emit_hash) |
7317 | { | |
7318 | unsigned long int *hashcodes; | |
14b1c01e | 7319 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
7320 | bfd_size_type amt; |
7321 | unsigned long int nsyms; | |
7322 | size_t bucketcount; | |
7323 | size_t hash_entry_size; | |
7324 | ||
7325 | /* Compute the hash values for all exported symbols. At the same | |
7326 | time store the values in an array so that we could use them for | |
7327 | optimizations. */ | |
7328 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 7329 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7330 | if (hashcodes == NULL) |
7331 | return FALSE; | |
14b1c01e AM |
7332 | hashinf.hashcodes = hashcodes; |
7333 | hashinf.error = FALSE; | |
5a580b3a | 7334 | |
fdc90cb4 JJ |
7335 | /* Put all hash values in HASHCODES. */ |
7336 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
7337 | elf_collect_hash_codes, &hashinf); |
7338 | if (hashinf.error) | |
4dd07732 AM |
7339 | { |
7340 | free (hashcodes); | |
7341 | return FALSE; | |
7342 | } | |
5a580b3a | 7343 | |
14b1c01e | 7344 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
7345 | bucketcount |
7346 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
7347 | free (hashcodes); | |
7348 | ||
4b48e2f6 | 7349 | if (bucketcount == 0 && nsyms > 0) |
fdc90cb4 | 7350 | return FALSE; |
5a580b3a | 7351 | |
fdc90cb4 JJ |
7352 | elf_hash_table (info)->bucketcount = bucketcount; |
7353 | ||
3d4d4302 | 7354 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
7355 | BFD_ASSERT (s != NULL); |
7356 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
7357 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 7358 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7359 | if (s->contents == NULL) |
7360 | return FALSE; | |
7361 | ||
7362 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
7363 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
7364 | s->contents + hash_entry_size); | |
7365 | } | |
7366 | ||
7367 | if (info->emit_gnu_hash) | |
7368 | { | |
7369 | size_t i, cnt; | |
7370 | unsigned char *contents; | |
7371 | struct collect_gnu_hash_codes cinfo; | |
7372 | bfd_size_type amt; | |
7373 | size_t bucketcount; | |
7374 | ||
7375 | memset (&cinfo, 0, sizeof (cinfo)); | |
7376 | ||
7377 | /* Compute the hash values for all exported symbols. At the same | |
7378 | time store the values in an array so that we could use them for | |
7379 | optimizations. */ | |
7380 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 7381 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7382 | if (cinfo.hashcodes == NULL) |
7383 | return FALSE; | |
7384 | ||
7385 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
7386 | cinfo.min_dynindx = -1; | |
7387 | cinfo.output_bfd = output_bfd; | |
7388 | cinfo.bed = bed; | |
7389 | ||
7390 | /* Put all hash values in HASHCODES. */ | |
7391 | elf_link_hash_traverse (elf_hash_table (info), | |
7392 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 7393 | if (cinfo.error) |
4dd07732 AM |
7394 | { |
7395 | free (cinfo.hashcodes); | |
7396 | return FALSE; | |
7397 | } | |
fdc90cb4 JJ |
7398 | |
7399 | bucketcount | |
7400 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
7401 | ||
7402 | if (bucketcount == 0) | |
7403 | { | |
7404 | free (cinfo.hashcodes); | |
7405 | return FALSE; | |
7406 | } | |
7407 | ||
f16a9783 | 7408 | s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed)); |
fdc90cb4 JJ |
7409 | BFD_ASSERT (s != NULL); |
7410 | ||
7411 | if (cinfo.nsyms == 0) | |
7412 | { | |
f16a9783 | 7413 | /* Empty .gnu.hash or .MIPS.xhash section is special. */ |
fdc90cb4 JJ |
7414 | BFD_ASSERT (cinfo.min_dynindx == -1); |
7415 | free (cinfo.hashcodes); | |
7416 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 7417 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7418 | if (contents == NULL) |
7419 | return FALSE; | |
7420 | s->contents = contents; | |
7421 | /* 1 empty bucket. */ | |
7422 | bfd_put_32 (output_bfd, 1, contents); | |
7423 | /* SYMIDX above the special symbol 0. */ | |
7424 | bfd_put_32 (output_bfd, 1, contents + 4); | |
7425 | /* Just one word for bitmask. */ | |
7426 | bfd_put_32 (output_bfd, 1, contents + 8); | |
7427 | /* Only hash fn bloom filter. */ | |
7428 | bfd_put_32 (output_bfd, 0, contents + 12); | |
7429 | /* No hashes are valid - empty bitmask. */ | |
7430 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
7431 | /* No hashes in the only bucket. */ | |
7432 | bfd_put_32 (output_bfd, 0, | |
7433 | contents + 16 + bed->s->arch_size / 8); | |
7434 | } | |
7435 | else | |
7436 | { | |
9e6619e2 | 7437 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 7438 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 7439 | |
9e6619e2 AM |
7440 | x = cinfo.nsyms; |
7441 | maskbitslog2 = 1; | |
7442 | while ((x >>= 1) != 0) | |
7443 | ++maskbitslog2; | |
fdc90cb4 JJ |
7444 | if (maskbitslog2 < 3) |
7445 | maskbitslog2 = 5; | |
7446 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
7447 | maskbitslog2 = maskbitslog2 + 3; | |
7448 | else | |
7449 | maskbitslog2 = maskbitslog2 + 2; | |
7450 | if (bed->s->arch_size == 64) | |
7451 | { | |
7452 | if (maskbitslog2 == 5) | |
7453 | maskbitslog2 = 6; | |
7454 | cinfo.shift1 = 6; | |
7455 | } | |
7456 | else | |
7457 | cinfo.shift1 = 5; | |
7458 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 7459 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
7460 | cinfo.maskbits = 1 << maskbitslog2; |
7461 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
7462 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
7463 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 7464 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
7465 | if (cinfo.bitmask == NULL) |
7466 | { | |
7467 | free (cinfo.hashcodes); | |
7468 | return FALSE; | |
7469 | } | |
7470 | ||
a50b1753 | 7471 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
7472 | cinfo.indx = cinfo.counts + bucketcount; |
7473 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
7474 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
7475 | ||
7476 | /* Determine how often each hash bucket is used. */ | |
7477 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
7478 | for (i = 0; i < cinfo.nsyms; ++i) | |
7479 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
7480 | ||
7481 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
7482 | if (cinfo.counts[i] != 0) | |
7483 | { | |
7484 | cinfo.indx[i] = cnt; | |
7485 | cnt += cinfo.counts[i]; | |
7486 | } | |
7487 | BFD_ASSERT (cnt == dynsymcount); | |
7488 | cinfo.bucketcount = bucketcount; | |
7489 | cinfo.local_indx = cinfo.min_dynindx; | |
7490 | ||
7491 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
7492 | s->size += cinfo.maskbits / 8; | |
f16a9783 MS |
7493 | if (bed->record_xhash_symbol != NULL) |
7494 | s->size += cinfo.nsyms * 4; | |
a50b1753 | 7495 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7496 | if (contents == NULL) |
7497 | { | |
7498 | free (cinfo.bitmask); | |
7499 | free (cinfo.hashcodes); | |
7500 | return FALSE; | |
7501 | } | |
7502 | ||
7503 | s->contents = contents; | |
7504 | bfd_put_32 (output_bfd, bucketcount, contents); | |
7505 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
7506 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
7507 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
7508 | contents += 16 + cinfo.maskbits / 8; | |
7509 | ||
7510 | for (i = 0; i < bucketcount; ++i) | |
7511 | { | |
7512 | if (cinfo.counts[i] == 0) | |
7513 | bfd_put_32 (output_bfd, 0, contents); | |
7514 | else | |
7515 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
7516 | contents += 4; | |
7517 | } | |
7518 | ||
7519 | cinfo.contents = contents; | |
7520 | ||
f16a9783 MS |
7521 | cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents; |
7522 | /* Renumber dynamic symbols, if populating .gnu.hash section. | |
7523 | If using .MIPS.xhash, populate the translation table. */ | |
fdc90cb4 | 7524 | elf_link_hash_traverse (elf_hash_table (info), |
f16a9783 | 7525 | elf_gnu_hash_process_symidx, &cinfo); |
fdc90cb4 JJ |
7526 | |
7527 | contents = s->contents + 16; | |
7528 | for (i = 0; i < maskwords; ++i) | |
7529 | { | |
7530 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
7531 | contents); | |
7532 | contents += bed->s->arch_size / 8; | |
7533 | } | |
7534 | ||
7535 | free (cinfo.bitmask); | |
7536 | free (cinfo.hashcodes); | |
7537 | } | |
7538 | } | |
5a580b3a | 7539 | |
3d4d4302 | 7540 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
7541 | BFD_ASSERT (s != NULL); |
7542 | ||
4ad4eba5 | 7543 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 7544 | |
eea6121a | 7545 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
7546 | |
7547 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
7548 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
7549 | return FALSE; | |
7550 | } | |
7551 | ||
7552 | return TRUE; | |
7553 | } | |
4d269e42 | 7554 | \f |
4d269e42 AM |
7555 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
7556 | ||
7557 | static void | |
7558 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
7559 | asection *sec) | |
7560 | { | |
dbaa2011 AM |
7561 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
7562 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
7563 | } |
7564 | ||
7565 | /* Finish SHF_MERGE section merging. */ | |
7566 | ||
7567 | bfd_boolean | |
630993ec | 7568 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
7569 | { |
7570 | bfd *ibfd; | |
7571 | asection *sec; | |
7572 | ||
7573 | if (!is_elf_hash_table (info->hash)) | |
7574 | return FALSE; | |
7575 | ||
c72f2fb2 | 7576 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
7577 | if ((ibfd->flags & DYNAMIC) == 0 |
7578 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
7579 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
7580 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
7581 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
7582 | if ((sec->flags & SEC_MERGE) != 0 | |
7583 | && !bfd_is_abs_section (sec->output_section)) | |
7584 | { | |
7585 | struct bfd_elf_section_data *secdata; | |
7586 | ||
7587 | secdata = elf_section_data (sec); | |
630993ec | 7588 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
7589 | &elf_hash_table (info)->merge_info, |
7590 | sec, &secdata->sec_info)) | |
7591 | return FALSE; | |
7592 | else if (secdata->sec_info) | |
dbaa2011 | 7593 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7594 | } |
7595 | ||
7596 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7597 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
7598 | merge_sections_remove_hook); |
7599 | return TRUE; | |
7600 | } | |
7601 | ||
7602 | /* Create an entry in an ELF linker hash table. */ | |
7603 | ||
7604 | struct bfd_hash_entry * | |
7605 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7606 | struct bfd_hash_table *table, | |
7607 | const char *string) | |
7608 | { | |
7609 | /* Allocate the structure if it has not already been allocated by a | |
7610 | subclass. */ | |
7611 | if (entry == NULL) | |
7612 | { | |
a50b1753 | 7613 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7614 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7615 | if (entry == NULL) |
7616 | return entry; | |
7617 | } | |
7618 | ||
7619 | /* Call the allocation method of the superclass. */ | |
7620 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7621 | if (entry != NULL) | |
7622 | { | |
7623 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7624 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7625 | ||
7626 | /* Set local fields. */ | |
7627 | ret->indx = -1; | |
7628 | ret->dynindx = -1; | |
7629 | ret->got = htab->init_got_refcount; | |
7630 | ret->plt = htab->init_plt_refcount; | |
7631 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7632 | - offsetof (struct elf_link_hash_entry, size))); | |
7633 | /* Assume that we have been called by a non-ELF symbol reader. | |
7634 | This flag is then reset by the code which reads an ELF input | |
7635 | file. This ensures that a symbol created by a non-ELF symbol | |
7636 | reader will have the flag set correctly. */ | |
7637 | ret->non_elf = 1; | |
7638 | } | |
7639 | ||
7640 | return entry; | |
7641 | } | |
7642 | ||
7643 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7644 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7645 | ||
7646 | void | |
7647 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7648 | struct elf_link_hash_entry *dir, | |
7649 | struct elf_link_hash_entry *ind) | |
7650 | { | |
7651 | struct elf_link_hash_table *htab; | |
7652 | ||
ad172eaa L |
7653 | if (ind->dyn_relocs != NULL) |
7654 | { | |
7655 | if (dir->dyn_relocs != NULL) | |
7656 | { | |
7657 | struct elf_dyn_relocs **pp; | |
7658 | struct elf_dyn_relocs *p; | |
7659 | ||
7660 | /* Add reloc counts against the indirect sym to the direct sym | |
7661 | list. Merge any entries against the same section. */ | |
7662 | for (pp = &ind->dyn_relocs; (p = *pp) != NULL; ) | |
7663 | { | |
7664 | struct elf_dyn_relocs *q; | |
7665 | ||
7666 | for (q = dir->dyn_relocs; q != NULL; q = q->next) | |
7667 | if (q->sec == p->sec) | |
7668 | { | |
7669 | q->pc_count += p->pc_count; | |
7670 | q->count += p->count; | |
7671 | *pp = p->next; | |
7672 | break; | |
7673 | } | |
7674 | if (q == NULL) | |
7675 | pp = &p->next; | |
7676 | } | |
7677 | *pp = dir->dyn_relocs; | |
7678 | } | |
7679 | ||
7680 | dir->dyn_relocs = ind->dyn_relocs; | |
7681 | ind->dyn_relocs = NULL; | |
7682 | } | |
7683 | ||
4d269e42 | 7684 | /* Copy down any references that we may have already seen to the |
e81830c5 | 7685 | symbol which just became indirect. */ |
4d269e42 | 7686 | |
422f1182 | 7687 | if (dir->versioned != versioned_hidden) |
e81830c5 AM |
7688 | dir->ref_dynamic |= ind->ref_dynamic; |
7689 | dir->ref_regular |= ind->ref_regular; | |
7690 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7691 | dir->non_got_ref |= ind->non_got_ref; | |
7692 | dir->needs_plt |= ind->needs_plt; | |
7693 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
4d269e42 AM |
7694 | |
7695 | if (ind->root.type != bfd_link_hash_indirect) | |
7696 | return; | |
7697 | ||
7698 | /* Copy over the global and procedure linkage table refcount entries. | |
7699 | These may have been already set up by a check_relocs routine. */ | |
7700 | htab = elf_hash_table (info); | |
7701 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7702 | { | |
7703 | if (dir->got.refcount < 0) | |
7704 | dir->got.refcount = 0; | |
7705 | dir->got.refcount += ind->got.refcount; | |
7706 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7707 | } | |
7708 | ||
7709 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7710 | { | |
7711 | if (dir->plt.refcount < 0) | |
7712 | dir->plt.refcount = 0; | |
7713 | dir->plt.refcount += ind->plt.refcount; | |
7714 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7715 | } | |
7716 | ||
7717 | if (ind->dynindx != -1) | |
7718 | { | |
7719 | if (dir->dynindx != -1) | |
7720 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7721 | dir->dynindx = ind->dynindx; | |
7722 | dir->dynstr_index = ind->dynstr_index; | |
7723 | ind->dynindx = -1; | |
7724 | ind->dynstr_index = 0; | |
7725 | } | |
7726 | } | |
7727 | ||
7728 | void | |
7729 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7730 | struct elf_link_hash_entry *h, | |
7731 | bfd_boolean force_local) | |
7732 | { | |
3aa14d16 L |
7733 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7734 | if (h->type != STT_GNU_IFUNC) | |
7735 | { | |
7736 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7737 | h->needs_plt = 0; | |
7738 | } | |
4d269e42 AM |
7739 | if (force_local) |
7740 | { | |
7741 | h->forced_local = 1; | |
7742 | if (h->dynindx != -1) | |
7743 | { | |
4d269e42 AM |
7744 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, |
7745 | h->dynstr_index); | |
641338d8 AM |
7746 | h->dynindx = -1; |
7747 | h->dynstr_index = 0; | |
4d269e42 AM |
7748 | } |
7749 | } | |
7750 | } | |
7751 | ||
34a87bb0 L |
7752 | /* Hide a symbol. */ |
7753 | ||
7754 | void | |
7755 | _bfd_elf_link_hide_symbol (bfd *output_bfd, | |
7756 | struct bfd_link_info *info, | |
7757 | struct bfd_link_hash_entry *h) | |
7758 | { | |
7759 | if (is_elf_hash_table (info->hash)) | |
7760 | { | |
7761 | const struct elf_backend_data *bed | |
7762 | = get_elf_backend_data (output_bfd); | |
7763 | struct elf_link_hash_entry *eh | |
7764 | = (struct elf_link_hash_entry *) h; | |
7765 | bed->elf_backend_hide_symbol (info, eh, TRUE); | |
7766 | eh->def_dynamic = 0; | |
7767 | eh->ref_dynamic = 0; | |
7768 | eh->dynamic_def = 0; | |
7769 | } | |
7770 | } | |
7771 | ||
7bf52ea2 AM |
7772 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7773 | caller. */ | |
4d269e42 AM |
7774 | |
7775 | bfd_boolean | |
7776 | _bfd_elf_link_hash_table_init | |
7777 | (struct elf_link_hash_table *table, | |
7778 | bfd *abfd, | |
7779 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7780 | struct bfd_hash_table *, | |
7781 | const char *), | |
4dfe6ac6 NC |
7782 | unsigned int entsize, |
7783 | enum elf_target_id target_id) | |
4d269e42 AM |
7784 | { |
7785 | bfd_boolean ret; | |
7786 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7787 | ||
4d269e42 AM |
7788 | table->init_got_refcount.refcount = can_refcount - 1; |
7789 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7790 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7791 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7792 | /* The first dynamic symbol is a dummy. */ | |
7793 | table->dynsymcount = 1; | |
7794 | ||
7795 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7796 | |
4d269e42 | 7797 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7798 | table->hash_table_id = target_id; |
4d269e42 AM |
7799 | |
7800 | return ret; | |
7801 | } | |
7802 | ||
7803 | /* Create an ELF linker hash table. */ | |
7804 | ||
7805 | struct bfd_link_hash_table * | |
7806 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7807 | { | |
7808 | struct elf_link_hash_table *ret; | |
986f0783 | 7809 | size_t amt = sizeof (struct elf_link_hash_table); |
4d269e42 | 7810 | |
7bf52ea2 | 7811 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7812 | if (ret == NULL) |
7813 | return NULL; | |
7814 | ||
7815 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7816 | sizeof (struct elf_link_hash_entry), |
7817 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7818 | { |
7819 | free (ret); | |
7820 | return NULL; | |
7821 | } | |
d495ab0d | 7822 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7823 | |
7824 | return &ret->root; | |
7825 | } | |
7826 | ||
9f7c3e5e AM |
7827 | /* Destroy an ELF linker hash table. */ |
7828 | ||
7829 | void | |
d495ab0d | 7830 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7831 | { |
d495ab0d AM |
7832 | struct elf_link_hash_table *htab; |
7833 | ||
7834 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7835 | if (htab->dynstr != NULL) |
7836 | _bfd_elf_strtab_free (htab->dynstr); | |
7837 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7838 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7839 | } |
7840 | ||
4d269e42 AM |
7841 | /* This is a hook for the ELF emulation code in the generic linker to |
7842 | tell the backend linker what file name to use for the DT_NEEDED | |
7843 | entry for a dynamic object. */ | |
7844 | ||
7845 | void | |
7846 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7847 | { | |
7848 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7849 | && bfd_get_format (abfd) == bfd_object) | |
7850 | elf_dt_name (abfd) = name; | |
7851 | } | |
7852 | ||
7853 | int | |
7854 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7855 | { | |
7856 | int lib_class; | |
7857 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7858 | && bfd_get_format (abfd) == bfd_object) | |
7859 | lib_class = elf_dyn_lib_class (abfd); | |
7860 | else | |
7861 | lib_class = 0; | |
7862 | return lib_class; | |
7863 | } | |
7864 | ||
7865 | void | |
7866 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7867 | { | |
7868 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7869 | && bfd_get_format (abfd) == bfd_object) | |
7870 | elf_dyn_lib_class (abfd) = lib_class; | |
7871 | } | |
7872 | ||
7873 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7874 | the linker ELF emulation code. */ | |
7875 | ||
7876 | struct bfd_link_needed_list * | |
7877 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7878 | struct bfd_link_info *info) | |
7879 | { | |
7880 | if (! is_elf_hash_table (info->hash)) | |
7881 | return NULL; | |
7882 | return elf_hash_table (info)->needed; | |
7883 | } | |
7884 | ||
7885 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7886 | hook for the linker ELF emulation code. */ | |
7887 | ||
7888 | struct bfd_link_needed_list * | |
7889 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7890 | struct bfd_link_info *info) | |
7891 | { | |
7892 | if (! is_elf_hash_table (info->hash)) | |
7893 | return NULL; | |
7894 | return elf_hash_table (info)->runpath; | |
7895 | } | |
7896 | ||
7897 | /* Get the name actually used for a dynamic object for a link. This | |
7898 | is the SONAME entry if there is one. Otherwise, it is the string | |
7899 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7900 | ||
7901 | const char * | |
7902 | bfd_elf_get_dt_soname (bfd *abfd) | |
7903 | { | |
7904 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7905 | && bfd_get_format (abfd) == bfd_object) | |
7906 | return elf_dt_name (abfd); | |
7907 | return NULL; | |
7908 | } | |
7909 | ||
7910 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7911 | the ELF linker emulation code. */ | |
7912 | ||
7913 | bfd_boolean | |
7914 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7915 | struct bfd_link_needed_list **pneeded) | |
7916 | { | |
7917 | asection *s; | |
7918 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7919 | unsigned int elfsec; |
4d269e42 AM |
7920 | unsigned long shlink; |
7921 | bfd_byte *extdyn, *extdynend; | |
7922 | size_t extdynsize; | |
7923 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7924 | ||
7925 | *pneeded = NULL; | |
7926 | ||
7927 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7928 | || bfd_get_format (abfd) != bfd_object) | |
7929 | return TRUE; | |
7930 | ||
7931 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7932 | if (s == NULL || s->size == 0) | |
7933 | return TRUE; | |
7934 | ||
7935 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7936 | goto error_return; | |
7937 | ||
7938 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7939 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7940 | goto error_return; |
7941 | ||
7942 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7943 | |
4d269e42 AM |
7944 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7945 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7946 | ||
7947 | extdyn = dynbuf; | |
7948 | extdynend = extdyn + s->size; | |
7949 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7950 | { | |
7951 | Elf_Internal_Dyn dyn; | |
7952 | ||
7953 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7954 | ||
7955 | if (dyn.d_tag == DT_NULL) | |
7956 | break; | |
7957 | ||
7958 | if (dyn.d_tag == DT_NEEDED) | |
7959 | { | |
7960 | const char *string; | |
7961 | struct bfd_link_needed_list *l; | |
7962 | unsigned int tagv = dyn.d_un.d_val; | |
986f0783 | 7963 | size_t amt; |
4d269e42 AM |
7964 | |
7965 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7966 | if (string == NULL) | |
7967 | goto error_return; | |
7968 | ||
7969 | amt = sizeof *l; | |
a50b1753 | 7970 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7971 | if (l == NULL) |
7972 | goto error_return; | |
7973 | ||
7974 | l->by = abfd; | |
7975 | l->name = string; | |
7976 | l->next = *pneeded; | |
7977 | *pneeded = l; | |
7978 | } | |
7979 | } | |
7980 | ||
7981 | free (dynbuf); | |
7982 | ||
7983 | return TRUE; | |
7984 | ||
7985 | error_return: | |
c9594989 | 7986 | free (dynbuf); |
4d269e42 AM |
7987 | return FALSE; |
7988 | } | |
7989 | ||
7990 | struct elf_symbuf_symbol | |
7991 | { | |
7992 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7993 | unsigned char st_info; /* Type and binding attributes */ | |
7994 | unsigned char st_other; /* Visibilty, and target specific */ | |
7995 | }; | |
7996 | ||
7997 | struct elf_symbuf_head | |
7998 | { | |
7999 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 8000 | size_t count; |
4d269e42 AM |
8001 | unsigned int st_shndx; |
8002 | }; | |
8003 | ||
8004 | struct elf_symbol | |
8005 | { | |
8006 | union | |
8007 | { | |
8008 | Elf_Internal_Sym *isym; | |
8009 | struct elf_symbuf_symbol *ssym; | |
dcea6a95 | 8010 | void *p; |
4d269e42 AM |
8011 | } u; |
8012 | const char *name; | |
8013 | }; | |
8014 | ||
8015 | /* Sort references to symbols by ascending section number. */ | |
8016 | ||
8017 | static int | |
8018 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
8019 | { | |
8020 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
8021 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
8022 | ||
dcea6a95 AM |
8023 | if (s1->st_shndx != s2->st_shndx) |
8024 | return s1->st_shndx > s2->st_shndx ? 1 : -1; | |
8025 | /* Final sort by the address of the sym in the symbuf ensures | |
8026 | a stable sort. */ | |
8027 | if (s1 != s2) | |
8028 | return s1 > s2 ? 1 : -1; | |
8029 | return 0; | |
4d269e42 AM |
8030 | } |
8031 | ||
8032 | static int | |
8033 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
8034 | { | |
8035 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
8036 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
dcea6a95 AM |
8037 | int ret = strcmp (s1->name, s2->name); |
8038 | if (ret != 0) | |
8039 | return ret; | |
8040 | if (s1->u.p != s2->u.p) | |
8041 | return s1->u.p > s2->u.p ? 1 : -1; | |
8042 | return 0; | |
4d269e42 AM |
8043 | } |
8044 | ||
8045 | static struct elf_symbuf_head * | |
ef53be89 | 8046 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 8047 | { |
14b1c01e | 8048 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
8049 | struct elf_symbuf_symbol *ssym; |
8050 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
446f7ed5 | 8051 | size_t i, shndx_count, total_size, amt; |
4d269e42 | 8052 | |
446f7ed5 AM |
8053 | amt = symcount * sizeof (*indbuf); |
8054 | indbuf = (Elf_Internal_Sym **) bfd_malloc (amt); | |
4d269e42 AM |
8055 | if (indbuf == NULL) |
8056 | return NULL; | |
8057 | ||
8058 | for (ind = indbuf, i = 0; i < symcount; i++) | |
8059 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
8060 | *ind++ = &isymbuf[i]; | |
8061 | indbufend = ind; | |
8062 | ||
8063 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
8064 | elf_sort_elf_symbol); | |
8065 | ||
8066 | shndx_count = 0; | |
8067 | if (indbufend > indbuf) | |
8068 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
8069 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
8070 | shndx_count++; | |
8071 | ||
3ae181ee L |
8072 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
8073 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 8074 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
8075 | if (ssymbuf == NULL) |
8076 | { | |
8077 | free (indbuf); | |
8078 | return NULL; | |
8079 | } | |
8080 | ||
3ae181ee | 8081 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
8082 | ssymbuf->ssym = NULL; |
8083 | ssymbuf->count = shndx_count; | |
8084 | ssymbuf->st_shndx = 0; | |
8085 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
8086 | { | |
8087 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
8088 | { | |
8089 | ssymhead++; | |
8090 | ssymhead->ssym = ssym; | |
8091 | ssymhead->count = 0; | |
8092 | ssymhead->st_shndx = (*ind)->st_shndx; | |
8093 | } | |
8094 | ssym->st_name = (*ind)->st_name; | |
8095 | ssym->st_info = (*ind)->st_info; | |
8096 | ssym->st_other = (*ind)->st_other; | |
8097 | ssymhead->count++; | |
8098 | } | |
ef53be89 | 8099 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
8100 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
8101 | == total_size)); | |
4d269e42 AM |
8102 | |
8103 | free (indbuf); | |
8104 | return ssymbuf; | |
8105 | } | |
8106 | ||
8107 | /* Check if 2 sections define the same set of local and global | |
8108 | symbols. */ | |
8109 | ||
8f317e31 | 8110 | static bfd_boolean |
4d269e42 AM |
8111 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
8112 | struct bfd_link_info *info) | |
8113 | { | |
8114 | bfd *bfd1, *bfd2; | |
8115 | const struct elf_backend_data *bed1, *bed2; | |
8116 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 8117 | size_t symcount1, symcount2; |
4d269e42 AM |
8118 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
8119 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
8120 | Elf_Internal_Sym *isym, *isymend; | |
8121 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 8122 | size_t count1, count2, i; |
cb33740c | 8123 | unsigned int shndx1, shndx2; |
4d269e42 AM |
8124 | bfd_boolean result; |
8125 | ||
8126 | bfd1 = sec1->owner; | |
8127 | bfd2 = sec2->owner; | |
8128 | ||
4d269e42 AM |
8129 | /* Both sections have to be in ELF. */ |
8130 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
8131 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
8132 | return FALSE; | |
8133 | ||
8134 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
8135 | return FALSE; | |
8136 | ||
4d269e42 AM |
8137 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
8138 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 8139 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
8140 | return FALSE; |
8141 | ||
8142 | bed1 = get_elf_backend_data (bfd1); | |
8143 | bed2 = get_elf_backend_data (bfd2); | |
8144 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
8145 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
8146 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
8147 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
8148 | ||
8149 | if (symcount1 == 0 || symcount2 == 0) | |
8150 | return FALSE; | |
8151 | ||
8152 | result = FALSE; | |
8153 | isymbuf1 = NULL; | |
8154 | isymbuf2 = NULL; | |
a50b1753 NC |
8155 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
8156 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
8157 | |
8158 | if (ssymbuf1 == NULL) | |
8159 | { | |
8160 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
8161 | NULL, NULL, NULL); | |
8162 | if (isymbuf1 == NULL) | |
8163 | goto done; | |
8164 | ||
8165 | if (!info->reduce_memory_overheads) | |
dcea6a95 AM |
8166 | { |
8167 | ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1); | |
8168 | elf_tdata (bfd1)->symbuf = ssymbuf1; | |
8169 | } | |
4d269e42 AM |
8170 | } |
8171 | ||
8172 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
8173 | { | |
8174 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
8175 | NULL, NULL, NULL); | |
8176 | if (isymbuf2 == NULL) | |
8177 | goto done; | |
8178 | ||
8179 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
dcea6a95 AM |
8180 | { |
8181 | ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2); | |
8182 | elf_tdata (bfd2)->symbuf = ssymbuf2; | |
8183 | } | |
4d269e42 AM |
8184 | } |
8185 | ||
8186 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
8187 | { | |
8188 | /* Optimized faster version. */ | |
ef53be89 | 8189 | size_t lo, hi, mid; |
4d269e42 AM |
8190 | struct elf_symbol *symp; |
8191 | struct elf_symbuf_symbol *ssym, *ssymend; | |
8192 | ||
8193 | lo = 0; | |
8194 | hi = ssymbuf1->count; | |
8195 | ssymbuf1++; | |
8196 | count1 = 0; | |
8197 | while (lo < hi) | |
8198 | { | |
8199 | mid = (lo + hi) / 2; | |
cb33740c | 8200 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 8201 | hi = mid; |
cb33740c | 8202 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
8203 | lo = mid + 1; |
8204 | else | |
8205 | { | |
8206 | count1 = ssymbuf1[mid].count; | |
8207 | ssymbuf1 += mid; | |
8208 | break; | |
8209 | } | |
8210 | } | |
8211 | ||
8212 | lo = 0; | |
8213 | hi = ssymbuf2->count; | |
8214 | ssymbuf2++; | |
8215 | count2 = 0; | |
8216 | while (lo < hi) | |
8217 | { | |
8218 | mid = (lo + hi) / 2; | |
cb33740c | 8219 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 8220 | hi = mid; |
cb33740c | 8221 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
8222 | lo = mid + 1; |
8223 | else | |
8224 | { | |
8225 | count2 = ssymbuf2[mid].count; | |
8226 | ssymbuf2 += mid; | |
8227 | break; | |
8228 | } | |
8229 | } | |
8230 | ||
8231 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8232 | goto done; | |
8233 | ||
ca4be51c AM |
8234 | symtable1 |
8235 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
8236 | symtable2 | |
8237 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
8238 | if (symtable1 == NULL || symtable2 == NULL) |
8239 | goto done; | |
8240 | ||
8241 | symp = symtable1; | |
8242 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
8243 | ssym < ssymend; ssym++, symp++) | |
8244 | { | |
8245 | symp->u.ssym = ssym; | |
8246 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
8247 | hdr1->sh_link, | |
8248 | ssym->st_name); | |
8249 | } | |
8250 | ||
8251 | symp = symtable2; | |
8252 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
8253 | ssym < ssymend; ssym++, symp++) | |
8254 | { | |
8255 | symp->u.ssym = ssym; | |
8256 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
8257 | hdr2->sh_link, | |
8258 | ssym->st_name); | |
8259 | } | |
8260 | ||
8261 | /* Sort symbol by name. */ | |
8262 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8263 | elf_sym_name_compare); | |
8264 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8265 | elf_sym_name_compare); | |
8266 | ||
8267 | for (i = 0; i < count1; i++) | |
8268 | /* Two symbols must have the same binding, type and name. */ | |
8269 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
8270 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
8271 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8272 | goto done; | |
8273 | ||
8274 | result = TRUE; | |
8275 | goto done; | |
8276 | } | |
8277 | ||
a50b1753 NC |
8278 | symtable1 = (struct elf_symbol *) |
8279 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
8280 | symtable2 = (struct elf_symbol *) | |
8281 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
8282 | if (symtable1 == NULL || symtable2 == NULL) |
8283 | goto done; | |
8284 | ||
8285 | /* Count definitions in the section. */ | |
8286 | count1 = 0; | |
8287 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 8288 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
8289 | symtable1[count1++].u.isym = isym; |
8290 | ||
8291 | count2 = 0; | |
8292 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 8293 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
8294 | symtable2[count2++].u.isym = isym; |
8295 | ||
8296 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8297 | goto done; | |
8298 | ||
8299 | for (i = 0; i < count1; i++) | |
8300 | symtable1[i].name | |
8301 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
8302 | symtable1[i].u.isym->st_name); | |
8303 | ||
8304 | for (i = 0; i < count2; i++) | |
8305 | symtable2[i].name | |
8306 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
8307 | symtable2[i].u.isym->st_name); | |
8308 | ||
8309 | /* Sort symbol by name. */ | |
8310 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8311 | elf_sym_name_compare); | |
8312 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8313 | elf_sym_name_compare); | |
8314 | ||
8315 | for (i = 0; i < count1; i++) | |
8316 | /* Two symbols must have the same binding, type and name. */ | |
8317 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
8318 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
8319 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8320 | goto done; | |
8321 | ||
8322 | result = TRUE; | |
8323 | ||
dc1e8a47 | 8324 | done: |
c9594989 AM |
8325 | free (symtable1); |
8326 | free (symtable2); | |
8327 | free (isymbuf1); | |
8328 | free (isymbuf2); | |
4d269e42 AM |
8329 | |
8330 | return result; | |
8331 | } | |
8332 | ||
8333 | /* Return TRUE if 2 section types are compatible. */ | |
8334 | ||
8335 | bfd_boolean | |
8336 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
8337 | bfd *bbfd, const asection *bsec) | |
8338 | { | |
8339 | if (asec == NULL | |
8340 | || bsec == NULL | |
8341 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
8342 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
8343 | return TRUE; | |
8344 | ||
8345 | return elf_section_type (asec) == elf_section_type (bsec); | |
8346 | } | |
8347 | \f | |
c152c796 AM |
8348 | /* Final phase of ELF linker. */ |
8349 | ||
8350 | /* A structure we use to avoid passing large numbers of arguments. */ | |
8351 | ||
8352 | struct elf_final_link_info | |
8353 | { | |
8354 | /* General link information. */ | |
8355 | struct bfd_link_info *info; | |
8356 | /* Output BFD. */ | |
8357 | bfd *output_bfd; | |
8358 | /* Symbol string table. */ | |
ef10c3ac | 8359 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
8360 | /* .hash section. */ |
8361 | asection *hash_sec; | |
8362 | /* symbol version section (.gnu.version). */ | |
8363 | asection *symver_sec; | |
8364 | /* Buffer large enough to hold contents of any section. */ | |
8365 | bfd_byte *contents; | |
8366 | /* Buffer large enough to hold external relocs of any section. */ | |
8367 | void *external_relocs; | |
8368 | /* Buffer large enough to hold internal relocs of any section. */ | |
8369 | Elf_Internal_Rela *internal_relocs; | |
8370 | /* Buffer large enough to hold external local symbols of any input | |
8371 | BFD. */ | |
8372 | bfd_byte *external_syms; | |
8373 | /* And a buffer for symbol section indices. */ | |
8374 | Elf_External_Sym_Shndx *locsym_shndx; | |
8375 | /* Buffer large enough to hold internal local symbols of any input | |
8376 | BFD. */ | |
8377 | Elf_Internal_Sym *internal_syms; | |
8378 | /* Array large enough to hold a symbol index for each local symbol | |
8379 | of any input BFD. */ | |
8380 | long *indices; | |
8381 | /* Array large enough to hold a section pointer for each local | |
8382 | symbol of any input BFD. */ | |
8383 | asection **sections; | |
ef10c3ac | 8384 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 8385 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
8386 | /* Number of STT_FILE syms seen. */ |
8387 | size_t filesym_count; | |
c152c796 AM |
8388 | }; |
8389 | ||
8390 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
8391 | ||
8392 | struct elf_outext_info | |
8393 | { | |
8394 | bfd_boolean failed; | |
8395 | bfd_boolean localsyms; | |
34a79995 | 8396 | bfd_boolean file_sym_done; |
8b127cbc | 8397 | struct elf_final_link_info *flinfo; |
c152c796 AM |
8398 | }; |
8399 | ||
d9352518 DB |
8400 | |
8401 | /* Support for evaluating a complex relocation. | |
8402 | ||
8403 | Complex relocations are generalized, self-describing relocations. The | |
8404 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 8405 | relocations themselves. |
d9352518 DB |
8406 | |
8407 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
8408 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
8409 | information (start bit, end bit, word width, etc) into the addend. This | |
8410 | information is extracted from CGEN-generated operand tables within gas. | |
8411 | ||
8412 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
8413 | internal) representing prefix-notation expressions, including but not | |
8414 | limited to those sorts of expressions normally encoded as addends in the | |
8415 | addend field. The symbol mangling format is: | |
8416 | ||
8417 | <node> := <literal> | |
07d6d2b8 AM |
8418 | | <unary-operator> ':' <node> |
8419 | | <binary-operator> ':' <node> ':' <node> | |
d9352518 DB |
8420 | ; |
8421 | ||
8422 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
07d6d2b8 | 8423 | | 'S' <digits=N> ':' <N character section name> |
d9352518 DB |
8424 | | '#' <hexdigits> |
8425 | ; | |
8426 | ||
8427 | <binary-operator> := as in C | |
8428 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
8429 | ||
8430 | static void | |
a0c8462f AM |
8431 | set_symbol_value (bfd *bfd_with_globals, |
8432 | Elf_Internal_Sym *isymbuf, | |
8433 | size_t locsymcount, | |
8434 | size_t symidx, | |
8435 | bfd_vma val) | |
d9352518 | 8436 | { |
8977835c AM |
8437 | struct elf_link_hash_entry **sym_hashes; |
8438 | struct elf_link_hash_entry *h; | |
8439 | size_t extsymoff = locsymcount; | |
d9352518 | 8440 | |
8977835c | 8441 | if (symidx < locsymcount) |
d9352518 | 8442 | { |
8977835c AM |
8443 | Elf_Internal_Sym *sym; |
8444 | ||
8445 | sym = isymbuf + symidx; | |
8446 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
8447 | { | |
8448 | /* It is a local symbol: move it to the | |
8449 | "absolute" section and give it a value. */ | |
8450 | sym->st_shndx = SHN_ABS; | |
8451 | sym->st_value = val; | |
8452 | return; | |
8453 | } | |
8454 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
8455 | extsymoff = 0; | |
d9352518 | 8456 | } |
8977835c AM |
8457 | |
8458 | /* It is a global symbol: set its link type | |
8459 | to "defined" and give it a value. */ | |
8460 | ||
8461 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
8462 | h = sym_hashes [symidx - extsymoff]; | |
8463 | while (h->root.type == bfd_link_hash_indirect | |
8464 | || h->root.type == bfd_link_hash_warning) | |
8465 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8466 | h->root.type = bfd_link_hash_defined; | |
8467 | h->root.u.def.value = val; | |
8468 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
8469 | } |
8470 | ||
a0c8462f AM |
8471 | static bfd_boolean |
8472 | resolve_symbol (const char *name, | |
8473 | bfd *input_bfd, | |
8b127cbc | 8474 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8475 | bfd_vma *result, |
8476 | Elf_Internal_Sym *isymbuf, | |
8477 | size_t locsymcount) | |
d9352518 | 8478 | { |
a0c8462f AM |
8479 | Elf_Internal_Sym *sym; |
8480 | struct bfd_link_hash_entry *global_entry; | |
8481 | const char *candidate = NULL; | |
8482 | Elf_Internal_Shdr *symtab_hdr; | |
8483 | size_t i; | |
8484 | ||
d9352518 DB |
8485 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
8486 | ||
8487 | for (i = 0; i < locsymcount; ++ i) | |
8488 | { | |
8977835c | 8489 | sym = isymbuf + i; |
d9352518 DB |
8490 | |
8491 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
8492 | continue; | |
8493 | ||
8494 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
8495 | symtab_hdr->sh_link, | |
8496 | sym->st_name); | |
8497 | #ifdef DEBUG | |
0f02bbd9 AM |
8498 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
8499 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
8500 | #endif |
8501 | if (candidate && strcmp (candidate, name) == 0) | |
8502 | { | |
8b127cbc | 8503 | asection *sec = flinfo->sections [i]; |
d9352518 | 8504 | |
0f02bbd9 AM |
8505 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
8506 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 8507 | #ifdef DEBUG |
0f02bbd9 AM |
8508 | printf ("Found symbol with value %8.8lx\n", |
8509 | (unsigned long) *result); | |
d9352518 DB |
8510 | #endif |
8511 | return TRUE; | |
8512 | } | |
8513 | } | |
8514 | ||
8515 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 8516 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 8517 | FALSE, FALSE, TRUE); |
d9352518 DB |
8518 | if (!global_entry) |
8519 | return FALSE; | |
a0c8462f | 8520 | |
d9352518 DB |
8521 | if (global_entry->type == bfd_link_hash_defined |
8522 | || global_entry->type == bfd_link_hash_defweak) | |
8523 | { | |
a0c8462f AM |
8524 | *result = (global_entry->u.def.value |
8525 | + global_entry->u.def.section->output_section->vma | |
8526 | + global_entry->u.def.section->output_offset); | |
d9352518 | 8527 | #ifdef DEBUG |
0f02bbd9 AM |
8528 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
8529 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
8530 | #endif |
8531 | return TRUE; | |
a0c8462f | 8532 | } |
d9352518 | 8533 | |
d9352518 DB |
8534 | return FALSE; |
8535 | } | |
8536 | ||
37b01f6a DG |
8537 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
8538 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
8539 | names like "foo.end" which is the end address of section "foo". */ | |
07d6d2b8 | 8540 | |
d9352518 | 8541 | static bfd_boolean |
a0c8462f AM |
8542 | resolve_section (const char *name, |
8543 | asection *sections, | |
37b01f6a DG |
8544 | bfd_vma *result, |
8545 | bfd * abfd) | |
d9352518 | 8546 | { |
a0c8462f AM |
8547 | asection *curr; |
8548 | unsigned int len; | |
d9352518 | 8549 | |
a0c8462f | 8550 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8551 | if (strcmp (curr->name, name) == 0) |
8552 | { | |
8553 | *result = curr->vma; | |
8554 | return TRUE; | |
8555 | } | |
8556 | ||
8557 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 8558 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 8559 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8560 | { |
8561 | len = strlen (curr->name); | |
a0c8462f | 8562 | if (len > strlen (name)) |
d9352518 DB |
8563 | continue; |
8564 | ||
8565 | if (strncmp (curr->name, name, len) == 0) | |
8566 | { | |
8567 | if (strncmp (".end", name + len, 4) == 0) | |
8568 | { | |
61826503 | 8569 | *result = (curr->vma |
bb294208 | 8570 | + curr->size / bfd_octets_per_byte (abfd, curr)); |
d9352518 DB |
8571 | return TRUE; |
8572 | } | |
8573 | ||
8574 | /* Insert more pseudo-section names here, if you like. */ | |
8575 | } | |
8576 | } | |
a0c8462f | 8577 | |
d9352518 DB |
8578 | return FALSE; |
8579 | } | |
8580 | ||
8581 | static void | |
a0c8462f | 8582 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 8583 | { |
695344c0 | 8584 | /* xgettext:c-format */ |
a0c8462f AM |
8585 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
8586 | reftype, name); | |
d9352518 DB |
8587 | } |
8588 | ||
8589 | static bfd_boolean | |
a0c8462f AM |
8590 | eval_symbol (bfd_vma *result, |
8591 | const char **symp, | |
8592 | bfd *input_bfd, | |
8b127cbc | 8593 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8594 | bfd_vma dot, |
8595 | Elf_Internal_Sym *isymbuf, | |
8596 | size_t locsymcount, | |
8597 | int signed_p) | |
d9352518 | 8598 | { |
4b93929b NC |
8599 | size_t len; |
8600 | size_t symlen; | |
a0c8462f AM |
8601 | bfd_vma a; |
8602 | bfd_vma b; | |
4b93929b | 8603 | char symbuf[4096]; |
0f02bbd9 | 8604 | const char *sym = *symp; |
a0c8462f AM |
8605 | const char *symend; |
8606 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
8607 | |
8608 | len = strlen (sym); | |
8609 | symend = sym + len; | |
8610 | ||
4b93929b | 8611 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
8612 | { |
8613 | bfd_set_error (bfd_error_invalid_operation); | |
8614 | return FALSE; | |
8615 | } | |
a0c8462f | 8616 | |
d9352518 DB |
8617 | switch (* sym) |
8618 | { | |
8619 | case '.': | |
0f02bbd9 AM |
8620 | *result = dot; |
8621 | *symp = sym + 1; | |
d9352518 DB |
8622 | return TRUE; |
8623 | ||
8624 | case '#': | |
0f02bbd9 AM |
8625 | ++sym; |
8626 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
8627 | return TRUE; |
8628 | ||
8629 | case 'S': | |
8630 | symbol_is_section = TRUE; | |
1a0670f3 | 8631 | /* Fall through. */ |
a0c8462f | 8632 | case 's': |
0f02bbd9 AM |
8633 | ++sym; |
8634 | symlen = strtol (sym, (char **) symp, 10); | |
8635 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 8636 | |
4b93929b | 8637 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
8638 | { |
8639 | bfd_set_error (bfd_error_invalid_operation); | |
8640 | return FALSE; | |
8641 | } | |
8642 | ||
8643 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 8644 | symbuf[symlen] = '\0'; |
0f02bbd9 | 8645 | *symp = sym + symlen; |
a0c8462f AM |
8646 | |
8647 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
8648 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
8649 | interpretation here; section means "try section first", not "must be a | |
8650 | section", and likewise with symbol. */ | |
8651 | ||
a0c8462f | 8652 | if (symbol_is_section) |
d9352518 | 8653 | { |
37b01f6a | 8654 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 8655 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8656 | isymbuf, locsymcount)) |
d9352518 DB |
8657 | { |
8658 | undefined_reference ("section", symbuf); | |
8659 | return FALSE; | |
8660 | } | |
a0c8462f AM |
8661 | } |
8662 | else | |
d9352518 | 8663 | { |
8b127cbc | 8664 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8665 | isymbuf, locsymcount) |
8b127cbc | 8666 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8667 | result, input_bfd)) |
d9352518 DB |
8668 | { |
8669 | undefined_reference ("symbol", symbuf); | |
8670 | return FALSE; | |
8671 | } | |
8672 | } | |
8673 | ||
8674 | return TRUE; | |
a0c8462f | 8675 | |
d9352518 DB |
8676 | /* All that remains are operators. */ |
8677 | ||
8678 | #define UNARY_OP(op) \ | |
8679 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8680 | { \ | |
8681 | sym += strlen (#op); \ | |
a0c8462f AM |
8682 | if (*sym == ':') \ |
8683 | ++sym; \ | |
0f02bbd9 | 8684 | *symp = sym; \ |
8b127cbc | 8685 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8686 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8687 | return FALSE; \ |
8688 | if (signed_p) \ | |
0f02bbd9 | 8689 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8690 | else \ |
8691 | *result = op a; \ | |
d9352518 DB |
8692 | return TRUE; \ |
8693 | } | |
8694 | ||
8695 | #define BINARY_OP(op) \ | |
8696 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8697 | { \ | |
8698 | sym += strlen (#op); \ | |
a0c8462f AM |
8699 | if (*sym == ':') \ |
8700 | ++sym; \ | |
0f02bbd9 | 8701 | *symp = sym; \ |
8b127cbc | 8702 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8703 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8704 | return FALSE; \ |
0f02bbd9 | 8705 | ++*symp; \ |
8b127cbc | 8706 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8707 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8708 | return FALSE; \ |
8709 | if (signed_p) \ | |
0f02bbd9 | 8710 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8711 | else \ |
8712 | *result = a op b; \ | |
d9352518 DB |
8713 | return TRUE; \ |
8714 | } | |
8715 | ||
8716 | default: | |
8717 | UNARY_OP (0-); | |
8718 | BINARY_OP (<<); | |
8719 | BINARY_OP (>>); | |
8720 | BINARY_OP (==); | |
8721 | BINARY_OP (!=); | |
8722 | BINARY_OP (<=); | |
8723 | BINARY_OP (>=); | |
8724 | BINARY_OP (&&); | |
8725 | BINARY_OP (||); | |
8726 | UNARY_OP (~); | |
8727 | UNARY_OP (!); | |
8728 | BINARY_OP (*); | |
8729 | BINARY_OP (/); | |
8730 | BINARY_OP (%); | |
8731 | BINARY_OP (^); | |
8732 | BINARY_OP (|); | |
8733 | BINARY_OP (&); | |
8734 | BINARY_OP (+); | |
8735 | BINARY_OP (-); | |
8736 | BINARY_OP (<); | |
8737 | BINARY_OP (>); | |
8738 | #undef UNARY_OP | |
8739 | #undef BINARY_OP | |
8740 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8741 | bfd_set_error (bfd_error_invalid_operation); | |
8742 | return FALSE; | |
8743 | } | |
8744 | } | |
8745 | ||
d9352518 | 8746 | static void |
a0c8462f AM |
8747 | put_value (bfd_vma size, |
8748 | unsigned long chunksz, | |
8749 | bfd *input_bfd, | |
8750 | bfd_vma x, | |
8751 | bfd_byte *location) | |
d9352518 DB |
8752 | { |
8753 | location += (size - chunksz); | |
8754 | ||
41cd1ad1 | 8755 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8756 | { |
8757 | switch (chunksz) | |
8758 | { | |
d9352518 DB |
8759 | case 1: |
8760 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8761 | x >>= 8; |
d9352518 DB |
8762 | break; |
8763 | case 2: | |
8764 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8765 | x >>= 16; |
d9352518 DB |
8766 | break; |
8767 | case 4: | |
8768 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8769 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8770 | x >>= 16; | |
8771 | x >>= 16; | |
d9352518 | 8772 | break; |
d9352518 | 8773 | #ifdef BFD64 |
41cd1ad1 | 8774 | case 8: |
d9352518 | 8775 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8776 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8777 | x >>= 32; | |
8778 | x >>= 32; | |
8779 | break; | |
d9352518 | 8780 | #endif |
41cd1ad1 NC |
8781 | default: |
8782 | abort (); | |
d9352518 DB |
8783 | break; |
8784 | } | |
8785 | } | |
8786 | } | |
8787 | ||
a0c8462f AM |
8788 | static bfd_vma |
8789 | get_value (bfd_vma size, | |
8790 | unsigned long chunksz, | |
8791 | bfd *input_bfd, | |
8792 | bfd_byte *location) | |
d9352518 | 8793 | { |
9b239e0e | 8794 | int shift; |
d9352518 DB |
8795 | bfd_vma x = 0; |
8796 | ||
9b239e0e NC |
8797 | /* Sanity checks. */ |
8798 | BFD_ASSERT (chunksz <= sizeof (x) | |
8799 | && size >= chunksz | |
8800 | && chunksz != 0 | |
8801 | && (size % chunksz) == 0 | |
8802 | && input_bfd != NULL | |
8803 | && location != NULL); | |
8804 | ||
8805 | if (chunksz == sizeof (x)) | |
8806 | { | |
8807 | BFD_ASSERT (size == chunksz); | |
8808 | ||
8809 | /* Make sure that we do not perform an undefined shift operation. | |
8810 | We know that size == chunksz so there will only be one iteration | |
8811 | of the loop below. */ | |
8812 | shift = 0; | |
8813 | } | |
8814 | else | |
8815 | shift = 8 * chunksz; | |
8816 | ||
a0c8462f | 8817 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8818 | { |
8819 | switch (chunksz) | |
8820 | { | |
d9352518 | 8821 | case 1: |
9b239e0e | 8822 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8823 | break; |
8824 | case 2: | |
9b239e0e | 8825 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8826 | break; |
8827 | case 4: | |
9b239e0e | 8828 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8829 | break; |
d9352518 | 8830 | #ifdef BFD64 |
9b239e0e NC |
8831 | case 8: |
8832 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8833 | break; |
9b239e0e NC |
8834 | #endif |
8835 | default: | |
8836 | abort (); | |
d9352518 DB |
8837 | } |
8838 | } | |
8839 | return x; | |
8840 | } | |
8841 | ||
a0c8462f AM |
8842 | static void |
8843 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8844 | unsigned long *oplen, /* in bits */ | |
8845 | unsigned long *len, /* in bits */ | |
8846 | unsigned long *wordsz, /* in bytes */ | |
8847 | unsigned long *chunksz, /* in bytes */ | |
8848 | unsigned long *lsb0_p, | |
8849 | unsigned long *signed_p, | |
8850 | unsigned long *trunc_p, | |
8851 | unsigned long encoded) | |
d9352518 | 8852 | { |
07d6d2b8 AM |
8853 | * start = encoded & 0x3F; |
8854 | * len = (encoded >> 6) & 0x3F; | |
d9352518 DB |
8855 | * oplen = (encoded >> 12) & 0x3F; |
8856 | * wordsz = (encoded >> 18) & 0xF; | |
8857 | * chunksz = (encoded >> 22) & 0xF; | |
8858 | * lsb0_p = (encoded >> 27) & 1; | |
8859 | * signed_p = (encoded >> 28) & 1; | |
8860 | * trunc_p = (encoded >> 29) & 1; | |
8861 | } | |
8862 | ||
cdfeee4f | 8863 | bfd_reloc_status_type |
0f02bbd9 | 8864 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
bb294208 | 8865 | asection *input_section, |
0f02bbd9 AM |
8866 | bfd_byte *contents, |
8867 | Elf_Internal_Rela *rel, | |
8868 | bfd_vma relocation) | |
d9352518 | 8869 | { |
0f02bbd9 AM |
8870 | bfd_vma shift, x, mask; |
8871 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8872 | bfd_reloc_status_type r; |
bb294208 | 8873 | bfd_size_type octets; |
d9352518 DB |
8874 | |
8875 | /* Perform this reloc, since it is complex. | |
8876 | (this is not to say that it necessarily refers to a complex | |
8877 | symbol; merely that it is a self-describing CGEN based reloc. | |
8878 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8879 | word size, etc) encoded within it.). */ |
d9352518 | 8880 | |
a0c8462f AM |
8881 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8882 | &chunksz, &lsb0_p, &signed_p, | |
8883 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8884 | |
8885 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8886 | ||
8887 | if (lsb0_p) | |
8888 | shift = (start + 1) - len; | |
8889 | else | |
8890 | shift = (8 * wordsz) - (start + len); | |
8891 | ||
bb294208 AM |
8892 | octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section); |
8893 | x = get_value (wordsz, chunksz, input_bfd, contents + octets); | |
d9352518 DB |
8894 | |
8895 | #ifdef DEBUG | |
8896 | printf ("Doing complex reloc: " | |
8897 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8898 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8899 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8900 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8901 | oplen, (unsigned long) x, (unsigned long) mask, |
8902 | (unsigned long) relocation); | |
d9352518 DB |
8903 | #endif |
8904 | ||
cdfeee4f | 8905 | r = bfd_reloc_ok; |
d9352518 | 8906 | if (! trunc_p) |
cdfeee4f AM |
8907 | /* Now do an overflow check. */ |
8908 | r = bfd_check_overflow ((signed_p | |
8909 | ? complain_overflow_signed | |
8910 | : complain_overflow_unsigned), | |
8911 | len, 0, (8 * wordsz), | |
8912 | relocation); | |
a0c8462f | 8913 | |
d9352518 DB |
8914 | /* Do the deed. */ |
8915 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8916 | ||
8917 | #ifdef DEBUG | |
8918 | printf (" relocation: %8.8lx\n" | |
8919 | " shifted mask: %8.8lx\n" | |
8920 | " shifted/masked reloc: %8.8lx\n" | |
8921 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8922 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8923 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8924 | #endif |
bb294208 | 8925 | put_value (wordsz, chunksz, input_bfd, x, contents + octets); |
cdfeee4f | 8926 | return r; |
d9352518 DB |
8927 | } |
8928 | ||
0e287786 AM |
8929 | /* Functions to read r_offset from external (target order) reloc |
8930 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8931 | know the value is aligned. */ | |
53df40a4 | 8932 | |
0e287786 AM |
8933 | static bfd_vma |
8934 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8935 | { |
8936 | union aligned32 | |
8937 | { | |
8938 | uint32_t v; | |
8939 | unsigned char c[4]; | |
8940 | }; | |
8941 | const union aligned32 *a | |
0e287786 | 8942 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8943 | |
8944 | uint32_t aval = ( (uint32_t) a->c[0] | |
8945 | | (uint32_t) a->c[1] << 8 | |
8946 | | (uint32_t) a->c[2] << 16 | |
8947 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8948 | return aval; |
53df40a4 AM |
8949 | } |
8950 | ||
0e287786 AM |
8951 | static bfd_vma |
8952 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8953 | { |
8954 | union aligned32 | |
8955 | { | |
8956 | uint32_t v; | |
8957 | unsigned char c[4]; | |
8958 | }; | |
8959 | const union aligned32 *a | |
0e287786 | 8960 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8961 | |
8962 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8963 | | (uint32_t) a->c[1] << 16 | |
8964 | | (uint32_t) a->c[2] << 8 | |
8965 | | (uint32_t) a->c[3]); | |
0e287786 | 8966 | return aval; |
53df40a4 AM |
8967 | } |
8968 | ||
8969 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8970 | static bfd_vma |
8971 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8972 | { |
8973 | union aligned64 | |
8974 | { | |
8975 | uint64_t v; | |
8976 | unsigned char c[8]; | |
8977 | }; | |
8978 | const union aligned64 *a | |
0e287786 | 8979 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8980 | |
8981 | uint64_t aval = ( (uint64_t) a->c[0] | |
8982 | | (uint64_t) a->c[1] << 8 | |
8983 | | (uint64_t) a->c[2] << 16 | |
8984 | | (uint64_t) a->c[3] << 24 | |
8985 | | (uint64_t) a->c[4] << 32 | |
8986 | | (uint64_t) a->c[5] << 40 | |
8987 | | (uint64_t) a->c[6] << 48 | |
8988 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8989 | return aval; |
53df40a4 AM |
8990 | } |
8991 | ||
0e287786 AM |
8992 | static bfd_vma |
8993 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8994 | { |
8995 | union aligned64 | |
8996 | { | |
8997 | uint64_t v; | |
8998 | unsigned char c[8]; | |
8999 | }; | |
9000 | const union aligned64 *a | |
0e287786 | 9001 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
9002 | |
9003 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
9004 | | (uint64_t) a->c[1] << 48 | |
9005 | | (uint64_t) a->c[2] << 40 | |
9006 | | (uint64_t) a->c[3] << 32 | |
9007 | | (uint64_t) a->c[4] << 24 | |
9008 | | (uint64_t) a->c[5] << 16 | |
9009 | | (uint64_t) a->c[6] << 8 | |
9010 | | (uint64_t) a->c[7]); | |
0e287786 | 9011 | return aval; |
53df40a4 AM |
9012 | } |
9013 | #endif | |
9014 | ||
c152c796 AM |
9015 | /* When performing a relocatable link, the input relocations are |
9016 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
9017 | referenced must be updated. Update all the relocations found in |
9018 | RELDATA. */ | |
c152c796 | 9019 | |
bca6d0e3 | 9020 | static bfd_boolean |
c152c796 | 9021 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 9022 | asection *sec, |
28dbcedc | 9023 | struct bfd_elf_section_reloc_data *reldata, |
10bbbc1d NC |
9024 | bfd_boolean sort, |
9025 | struct bfd_link_info *info) | |
c152c796 AM |
9026 | { |
9027 | unsigned int i; | |
9028 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9029 | bfd_byte *erela; | |
9030 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
9031 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
9032 | bfd_vma r_type_mask; | |
9033 | int r_sym_shift; | |
d4730f92 BS |
9034 | unsigned int count = reldata->count; |
9035 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 9036 | |
d4730f92 | 9037 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
9038 | { |
9039 | swap_in = bed->s->swap_reloc_in; | |
9040 | swap_out = bed->s->swap_reloc_out; | |
9041 | } | |
d4730f92 | 9042 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
9043 | { |
9044 | swap_in = bed->s->swap_reloca_in; | |
9045 | swap_out = bed->s->swap_reloca_out; | |
9046 | } | |
9047 | else | |
9048 | abort (); | |
9049 | ||
9050 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
9051 | abort (); | |
9052 | ||
9053 | if (bed->s->arch_size == 32) | |
9054 | { | |
9055 | r_type_mask = 0xff; | |
9056 | r_sym_shift = 8; | |
9057 | } | |
9058 | else | |
9059 | { | |
9060 | r_type_mask = 0xffffffff; | |
9061 | r_sym_shift = 32; | |
9062 | } | |
9063 | ||
d4730f92 BS |
9064 | erela = reldata->hdr->contents; |
9065 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
9066 | { |
9067 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
9068 | unsigned int j; | |
9069 | ||
9070 | if (*rel_hash == NULL) | |
9071 | continue; | |
9072 | ||
10bbbc1d NC |
9073 | if ((*rel_hash)->indx == -2 |
9074 | && info->gc_sections | |
9075 | && ! info->gc_keep_exported) | |
9076 | { | |
9077 | /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ | |
9793eb77 | 9078 | _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"), |
10bbbc1d NC |
9079 | abfd, sec, |
9080 | (*rel_hash)->root.root.string); | |
9793eb77 | 9081 | _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"), |
d42c267e | 9082 | abfd, sec); |
10bbbc1d NC |
9083 | bfd_set_error (bfd_error_invalid_operation); |
9084 | return FALSE; | |
9085 | } | |
c152c796 AM |
9086 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
9087 | ||
9088 | (*swap_in) (abfd, erela, irela); | |
9089 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
9090 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
9091 | | (irela[j].r_info & r_type_mask)); | |
9092 | (*swap_out) (abfd, irela, erela); | |
9093 | } | |
53df40a4 | 9094 | |
9eaff861 AO |
9095 | if (bed->elf_backend_update_relocs) |
9096 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
9097 | ||
0e287786 | 9098 | if (sort && count != 0) |
53df40a4 | 9099 | { |
0e287786 AM |
9100 | bfd_vma (*ext_r_off) (const void *); |
9101 | bfd_vma r_off; | |
9102 | size_t elt_size; | |
9103 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 9104 | bfd_byte *buf = NULL; |
28dbcedc AM |
9105 | |
9106 | if (bed->s->arch_size == 32) | |
9107 | { | |
9108 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 9109 | ext_r_off = ext32l_r_offset; |
28dbcedc | 9110 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 9111 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
9112 | else |
9113 | abort (); | |
9114 | } | |
53df40a4 | 9115 | else |
28dbcedc | 9116 | { |
53df40a4 | 9117 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 9118 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 9119 | ext_r_off = ext64l_r_offset; |
28dbcedc | 9120 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 9121 | ext_r_off = ext64b_r_offset; |
28dbcedc | 9122 | else |
53df40a4 | 9123 | #endif |
28dbcedc AM |
9124 | abort (); |
9125 | } | |
0e287786 | 9126 | |
bca6d0e3 AM |
9127 | /* Must use a stable sort here. A modified insertion sort, |
9128 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
9129 | elt_size = reldata->hdr->sh_entsize; |
9130 | base = reldata->hdr->contents; | |
9131 | end = base + count * elt_size; | |
bca6d0e3 | 9132 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
9133 | abort (); |
9134 | ||
9135 | /* Ensure the first element is lowest. This acts as a sentinel, | |
9136 | speeding the main loop below. */ | |
9137 | r_off = (*ext_r_off) (base); | |
9138 | for (p = loc = base; (p += elt_size) < end; ) | |
9139 | { | |
9140 | bfd_vma r_off2 = (*ext_r_off) (p); | |
9141 | if (r_off > r_off2) | |
9142 | { | |
9143 | r_off = r_off2; | |
9144 | loc = p; | |
9145 | } | |
9146 | } | |
9147 | if (loc != base) | |
9148 | { | |
9149 | /* Don't just swap *base and *loc as that changes the order | |
9150 | of the original base[0] and base[1] if they happen to | |
9151 | have the same r_offset. */ | |
bca6d0e3 AM |
9152 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
9153 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 9154 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 9155 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
9156 | } |
9157 | ||
b29b8669 | 9158 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
9159 | { |
9160 | /* base to p is sorted, *p is next to insert. */ | |
9161 | r_off = (*ext_r_off) (p); | |
9162 | /* Search the sorted region for location to insert. */ | |
9163 | loc = p - elt_size; | |
9164 | while (r_off < (*ext_r_off) (loc)) | |
9165 | loc -= elt_size; | |
9166 | loc += elt_size; | |
9167 | if (loc != p) | |
9168 | { | |
bca6d0e3 AM |
9169 | /* Chances are there is a run of relocs to insert here, |
9170 | from one of more input files. Files are not always | |
9171 | linked in order due to the way elf_link_input_bfd is | |
9172 | called. See pr17666. */ | |
9173 | size_t sortlen = p - loc; | |
9174 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
9175 | size_t runlen = elt_size; | |
9176 | size_t buf_size = 96 * 1024; | |
9177 | while (p + runlen < end | |
9178 | && (sortlen <= buf_size | |
9179 | || runlen + elt_size <= buf_size) | |
9180 | && r_off2 > (*ext_r_off) (p + runlen)) | |
9181 | runlen += elt_size; | |
9182 | if (buf == NULL) | |
9183 | { | |
9184 | buf = bfd_malloc (buf_size); | |
9185 | if (buf == NULL) | |
9186 | return FALSE; | |
9187 | } | |
9188 | if (runlen < sortlen) | |
9189 | { | |
9190 | memcpy (buf, p, runlen); | |
9191 | memmove (loc + runlen, loc, sortlen); | |
9192 | memcpy (loc, buf, runlen); | |
9193 | } | |
9194 | else | |
9195 | { | |
9196 | memcpy (buf, loc, sortlen); | |
9197 | memmove (loc, p, runlen); | |
9198 | memcpy (loc + runlen, buf, sortlen); | |
9199 | } | |
b29b8669 | 9200 | p += runlen - elt_size; |
0e287786 AM |
9201 | } |
9202 | } | |
9203 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
9204 | free (reldata->hashes); |
9205 | reldata->hashes = NULL; | |
bca6d0e3 | 9206 | free (buf); |
53df40a4 | 9207 | } |
bca6d0e3 | 9208 | return TRUE; |
c152c796 AM |
9209 | } |
9210 | ||
9211 | struct elf_link_sort_rela | |
9212 | { | |
9213 | union { | |
9214 | bfd_vma offset; | |
9215 | bfd_vma sym_mask; | |
9216 | } u; | |
9217 | enum elf_reloc_type_class type; | |
9218 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
9219 | Elf_Internal_Rela rela[1]; | |
9220 | }; | |
9221 | ||
dcea6a95 AM |
9222 | /* qsort stability here and for cmp2 is only an issue if multiple |
9223 | dynamic relocations are emitted at the same address. But targets | |
9224 | that apply a series of dynamic relocations each operating on the | |
9225 | result of the prior relocation can't use -z combreloc as | |
9226 | implemented anyway. Such schemes tend to be broken by sorting on | |
9227 | symbol index. That leaves dynamic NONE relocs as the only other | |
9228 | case where ld might emit multiple relocs at the same address, and | |
9229 | those are only emitted due to target bugs. */ | |
9230 | ||
c152c796 AM |
9231 | static int |
9232 | elf_link_sort_cmp1 (const void *A, const void *B) | |
9233 | { | |
a50b1753 NC |
9234 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
9235 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
9236 | int relativea, relativeb; |
9237 | ||
9238 | relativea = a->type == reloc_class_relative; | |
9239 | relativeb = b->type == reloc_class_relative; | |
9240 | ||
9241 | if (relativea < relativeb) | |
9242 | return 1; | |
9243 | if (relativea > relativeb) | |
9244 | return -1; | |
9245 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
9246 | return -1; | |
9247 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
9248 | return 1; | |
9249 | if (a->rela->r_offset < b->rela->r_offset) | |
9250 | return -1; | |
9251 | if (a->rela->r_offset > b->rela->r_offset) | |
9252 | return 1; | |
9253 | return 0; | |
9254 | } | |
9255 | ||
9256 | static int | |
9257 | elf_link_sort_cmp2 (const void *A, const void *B) | |
9258 | { | |
a50b1753 NC |
9259 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
9260 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 9261 | |
7e612e98 | 9262 | if (a->type < b->type) |
c152c796 | 9263 | return -1; |
7e612e98 | 9264 | if (a->type > b->type) |
c152c796 | 9265 | return 1; |
7e612e98 | 9266 | if (a->u.offset < b->u.offset) |
c152c796 | 9267 | return -1; |
7e612e98 | 9268 | if (a->u.offset > b->u.offset) |
c152c796 AM |
9269 | return 1; |
9270 | if (a->rela->r_offset < b->rela->r_offset) | |
9271 | return -1; | |
9272 | if (a->rela->r_offset > b->rela->r_offset) | |
9273 | return 1; | |
9274 | return 0; | |
9275 | } | |
9276 | ||
9277 | static size_t | |
9278 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
9279 | { | |
3410fea8 | 9280 | asection *dynamic_relocs; |
fc66a176 L |
9281 | asection *rela_dyn; |
9282 | asection *rel_dyn; | |
c152c796 AM |
9283 | bfd_size_type count, size; |
9284 | size_t i, ret, sort_elt, ext_size; | |
9285 | bfd_byte *sort, *s_non_relative, *p; | |
9286 | struct elf_link_sort_rela *sq; | |
9287 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9288 | int i2e = bed->s->int_rels_per_ext_rel; | |
61826503 | 9289 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
c152c796 AM |
9290 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
9291 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
9292 | struct bfd_link_order *lo; | |
9293 | bfd_vma r_sym_mask; | |
3410fea8 | 9294 | bfd_boolean use_rela; |
c152c796 | 9295 | |
3410fea8 NC |
9296 | /* Find a dynamic reloc section. */ |
9297 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
9298 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
9299 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
9300 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 9301 | { |
3410fea8 NC |
9302 | bfd_boolean use_rela_initialised = FALSE; |
9303 | ||
9304 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 9305 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
9306 | use_rela = TRUE; |
9307 | ||
9308 | /* Both sections are present. Examine the sizes | |
9309 | of the indirect sections to help us choose. */ | |
9310 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9311 | if (lo->type == bfd_indirect_link_order) | |
9312 | { | |
9313 | asection *o = lo->u.indirect.section; | |
9314 | ||
9315 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9316 | { | |
9317 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9318 | /* Section size is divisible by both rel and rela sizes. | |
9319 | It is of no help to us. */ | |
9320 | ; | |
9321 | else | |
9322 | { | |
9323 | /* Section size is only divisible by rela. */ | |
535b785f | 9324 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9325 | { |
9793eb77 | 9326 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9327 | "they are in more than one size"), |
9328 | abfd); | |
3410fea8 NC |
9329 | bfd_set_error (bfd_error_invalid_operation); |
9330 | return 0; | |
9331 | } | |
9332 | else | |
9333 | { | |
9334 | use_rela = TRUE; | |
9335 | use_rela_initialised = TRUE; | |
9336 | } | |
9337 | } | |
9338 | } | |
9339 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9340 | { | |
9341 | /* Section size is only divisible by rel. */ | |
535b785f | 9342 | if (use_rela_initialised && use_rela) |
3410fea8 | 9343 | { |
9793eb77 | 9344 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9345 | "they are in more than one size"), |
9346 | abfd); | |
3410fea8 NC |
9347 | bfd_set_error (bfd_error_invalid_operation); |
9348 | return 0; | |
9349 | } | |
9350 | else | |
9351 | { | |
9352 | use_rela = FALSE; | |
9353 | use_rela_initialised = TRUE; | |
9354 | } | |
9355 | } | |
9356 | else | |
9357 | { | |
c8e44c6d AM |
9358 | /* The section size is not divisible by either - |
9359 | something is wrong. */ | |
9793eb77 | 9360 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9361 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9362 | bfd_set_error (bfd_error_invalid_operation); |
9363 | return 0; | |
9364 | } | |
9365 | } | |
9366 | ||
9367 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9368 | if (lo->type == bfd_indirect_link_order) | |
9369 | { | |
9370 | asection *o = lo->u.indirect.section; | |
9371 | ||
9372 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9373 | { | |
9374 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9375 | /* Section size is divisible by both rel and rela sizes. | |
9376 | It is of no help to us. */ | |
9377 | ; | |
9378 | else | |
9379 | { | |
9380 | /* Section size is only divisible by rela. */ | |
535b785f | 9381 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9382 | { |
9793eb77 | 9383 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9384 | "they are in more than one size"), |
9385 | abfd); | |
3410fea8 NC |
9386 | bfd_set_error (bfd_error_invalid_operation); |
9387 | return 0; | |
9388 | } | |
9389 | else | |
9390 | { | |
9391 | use_rela = TRUE; | |
9392 | use_rela_initialised = TRUE; | |
9393 | } | |
9394 | } | |
9395 | } | |
9396 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9397 | { | |
9398 | /* Section size is only divisible by rel. */ | |
535b785f | 9399 | if (use_rela_initialised && use_rela) |
3410fea8 | 9400 | { |
9793eb77 | 9401 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9402 | "they are in more than one size"), |
9403 | abfd); | |
3410fea8 NC |
9404 | bfd_set_error (bfd_error_invalid_operation); |
9405 | return 0; | |
9406 | } | |
9407 | else | |
9408 | { | |
9409 | use_rela = FALSE; | |
9410 | use_rela_initialised = TRUE; | |
9411 | } | |
9412 | } | |
9413 | else | |
9414 | { | |
c8e44c6d AM |
9415 | /* The section size is not divisible by either - |
9416 | something is wrong. */ | |
9793eb77 | 9417 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9418 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9419 | bfd_set_error (bfd_error_invalid_operation); |
9420 | return 0; | |
9421 | } | |
9422 | } | |
9423 | ||
9424 | if (! use_rela_initialised) | |
9425 | /* Make a guess. */ | |
9426 | use_rela = TRUE; | |
c152c796 | 9427 | } |
fc66a176 L |
9428 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
9429 | use_rela = TRUE; | |
9430 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 9431 | use_rela = FALSE; |
c152c796 | 9432 | else |
fc66a176 | 9433 | return 0; |
3410fea8 NC |
9434 | |
9435 | if (use_rela) | |
c152c796 | 9436 | { |
3410fea8 | 9437 | dynamic_relocs = rela_dyn; |
c152c796 AM |
9438 | ext_size = bed->s->sizeof_rela; |
9439 | swap_in = bed->s->swap_reloca_in; | |
9440 | swap_out = bed->s->swap_reloca_out; | |
9441 | } | |
3410fea8 NC |
9442 | else |
9443 | { | |
9444 | dynamic_relocs = rel_dyn; | |
9445 | ext_size = bed->s->sizeof_rel; | |
9446 | swap_in = bed->s->swap_reloc_in; | |
9447 | swap_out = bed->s->swap_reloc_out; | |
9448 | } | |
c152c796 AM |
9449 | |
9450 | size = 0; | |
3410fea8 | 9451 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 9452 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 9453 | size += lo->u.indirect.section->size; |
c152c796 | 9454 | |
3410fea8 | 9455 | if (size != dynamic_relocs->size) |
c152c796 AM |
9456 | return 0; |
9457 | ||
9458 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
9459 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
9460 | |
9461 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
9462 | if (count == 0) |
9463 | return 0; | |
a50b1753 | 9464 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 9465 | |
c152c796 AM |
9466 | if (sort == NULL) |
9467 | { | |
9468 | (*info->callbacks->warning) | |
9793eb77 | 9469 | (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0); |
c152c796 AM |
9470 | return 0; |
9471 | } | |
9472 | ||
9473 | if (bed->s->arch_size == 32) | |
9474 | r_sym_mask = ~(bfd_vma) 0xff; | |
9475 | else | |
9476 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
9477 | ||
3410fea8 | 9478 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9479 | if (lo->type == bfd_indirect_link_order) |
9480 | { | |
9481 | bfd_byte *erel, *erelend; | |
9482 | asection *o = lo->u.indirect.section; | |
9483 | ||
1da212d6 AM |
9484 | if (o->contents == NULL && o->size != 0) |
9485 | { | |
9486 | /* This is a reloc section that is being handled as a normal | |
9487 | section. See bfd_section_from_shdr. We can't combine | |
9488 | relocs in this case. */ | |
9489 | free (sort); | |
9490 | return 0; | |
9491 | } | |
c152c796 | 9492 | erel = o->contents; |
eea6121a | 9493 | erelend = o->contents + o->size; |
c8e44c6d | 9494 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 9495 | |
c152c796 AM |
9496 | while (erel < erelend) |
9497 | { | |
9498 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 9499 | |
c152c796 | 9500 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 9501 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
9502 | s->u.sym_mask = r_sym_mask; |
9503 | p += sort_elt; | |
9504 | erel += ext_size; | |
9505 | } | |
9506 | } | |
9507 | ||
9508 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
9509 | ||
9510 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
9511 | { | |
9512 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9513 | if (s->type != reloc_class_relative) | |
9514 | break; | |
9515 | } | |
9516 | ret = i; | |
9517 | s_non_relative = p; | |
9518 | ||
9519 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
9520 | for (; i < count; i++, p += sort_elt) | |
9521 | { | |
9522 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
9523 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
9524 | sq = sp; | |
9525 | sp->u.offset = sq->rela->r_offset; | |
9526 | } | |
9527 | ||
9528 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
9529 | ||
c8e44c6d AM |
9530 | struct elf_link_hash_table *htab = elf_hash_table (info); |
9531 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
9532 | { | |
9533 | /* We have plt relocs in .rela.dyn. */ | |
9534 | sq = (struct elf_link_sort_rela *) sort; | |
9535 | for (i = 0; i < count; i++) | |
9536 | if (sq[count - i - 1].type != reloc_class_plt) | |
9537 | break; | |
9538 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
9539 | { | |
9540 | struct bfd_link_order **plo; | |
9541 | /* Put srelplt link_order last. This is so the output_offset | |
9542 | set in the next loop is correct for DT_JMPREL. */ | |
9543 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
9544 | if ((*plo)->type == bfd_indirect_link_order | |
9545 | && (*plo)->u.indirect.section == htab->srelplt) | |
9546 | { | |
9547 | lo = *plo; | |
9548 | *plo = lo->next; | |
9549 | } | |
9550 | else | |
9551 | plo = &(*plo)->next; | |
9552 | *plo = lo; | |
9553 | lo->next = NULL; | |
9554 | dynamic_relocs->map_tail.link_order = lo; | |
9555 | } | |
9556 | } | |
9557 | ||
9558 | p = sort; | |
3410fea8 | 9559 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9560 | if (lo->type == bfd_indirect_link_order) |
9561 | { | |
9562 | bfd_byte *erel, *erelend; | |
9563 | asection *o = lo->u.indirect.section; | |
9564 | ||
9565 | erel = o->contents; | |
eea6121a | 9566 | erelend = o->contents + o->size; |
c8e44c6d | 9567 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
9568 | while (erel < erelend) |
9569 | { | |
9570 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9571 | (*swap_out) (abfd, s->rela, erel); | |
9572 | p += sort_elt; | |
9573 | erel += ext_size; | |
9574 | } | |
9575 | } | |
9576 | ||
9577 | free (sort); | |
3410fea8 | 9578 | *psec = dynamic_relocs; |
c152c796 AM |
9579 | return ret; |
9580 | } | |
9581 | ||
ef10c3ac | 9582 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 9583 | |
6e0b88f1 | 9584 | static int |
ef10c3ac L |
9585 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
9586 | const char *name, | |
9587 | Elf_Internal_Sym *elfsym, | |
9588 | asection *input_sec, | |
9589 | struct elf_link_hash_entry *h) | |
c152c796 | 9590 | { |
6e0b88f1 | 9591 | int (*output_symbol_hook) |
c152c796 AM |
9592 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
9593 | struct elf_link_hash_entry *); | |
ef10c3ac | 9594 | struct elf_link_hash_table *hash_table; |
c152c796 | 9595 | const struct elf_backend_data *bed; |
ef10c3ac | 9596 | bfd_size_type strtabsize; |
c152c796 | 9597 | |
8539e4e8 AM |
9598 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
9599 | ||
8b127cbc | 9600 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
9601 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
9602 | if (output_symbol_hook != NULL) | |
9603 | { | |
8b127cbc | 9604 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
9605 | if (ret != 1) |
9606 | return ret; | |
c152c796 AM |
9607 | } |
9608 | ||
06f44071 AM |
9609 | if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC) |
9610 | elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc; | |
9611 | if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE) | |
9612 | elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique; | |
9613 | ||
ef10c3ac L |
9614 | if (name == NULL |
9615 | || *name == '\0' | |
9616 | || (input_sec->flags & SEC_EXCLUDE)) | |
9617 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
9618 | else |
9619 | { | |
ef10c3ac L |
9620 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
9621 | to get the final offset for st_name. */ | |
9622 | elfsym->st_name | |
9623 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
9624 | name, FALSE); | |
c152c796 | 9625 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 9626 | return 0; |
c152c796 AM |
9627 | } |
9628 | ||
ef10c3ac L |
9629 | hash_table = elf_hash_table (flinfo->info); |
9630 | strtabsize = hash_table->strtabsize; | |
9631 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 9632 | { |
ef10c3ac L |
9633 | strtabsize += strtabsize; |
9634 | hash_table->strtabsize = strtabsize; | |
9635 | strtabsize *= sizeof (*hash_table->strtab); | |
9636 | hash_table->strtab | |
9637 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
9638 | strtabsize); | |
9639 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 9640 | return 0; |
c152c796 | 9641 | } |
ef10c3ac L |
9642 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
9643 | hash_table->strtab[hash_table->strtabcount].dest_index | |
9644 | = hash_table->strtabcount; | |
9645 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
9646 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
9647 | ||
ed48ec2e | 9648 | flinfo->output_bfd->symcount += 1; |
ef10c3ac L |
9649 | hash_table->strtabcount += 1; |
9650 | ||
9651 | return 1; | |
9652 | } | |
9653 | ||
9654 | /* Swap symbols out to the symbol table and flush the output symbols to | |
9655 | the file. */ | |
9656 | ||
9657 | static bfd_boolean | |
9658 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
9659 | { | |
9660 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
986f0783 | 9661 | size_t amt; |
ef53be89 | 9662 | size_t i; |
ef10c3ac L |
9663 | const struct elf_backend_data *bed; |
9664 | bfd_byte *symbuf; | |
9665 | Elf_Internal_Shdr *hdr; | |
9666 | file_ptr pos; | |
9667 | bfd_boolean ret; | |
9668 | ||
9669 | if (!hash_table->strtabcount) | |
9670 | return TRUE; | |
9671 | ||
9672 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
9673 | ||
9674 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 9675 | |
ef10c3ac L |
9676 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
9677 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
9678 | if (symbuf == NULL) | |
9679 | return FALSE; | |
1b786873 | 9680 | |
ef10c3ac | 9681 | if (flinfo->symshndxbuf) |
c152c796 | 9682 | { |
ef53be89 AM |
9683 | amt = sizeof (Elf_External_Sym_Shndx); |
9684 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
9685 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9686 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 9687 | { |
ef10c3ac L |
9688 | free (symbuf); |
9689 | return FALSE; | |
c152c796 | 9690 | } |
c152c796 AM |
9691 | } |
9692 | ||
ef10c3ac L |
9693 | for (i = 0; i < hash_table->strtabcount; i++) |
9694 | { | |
9695 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
9696 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9697 | elfsym->sym.st_name = 0; | |
9698 | else | |
9699 | elfsym->sym.st_name | |
9700 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9701 | elfsym->sym.st_name); | |
9702 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9703 | ((bfd_byte *) symbuf | |
9704 | + (elfsym->dest_index | |
9705 | * bed->s->sizeof_sym)), | |
9706 | (flinfo->symshndxbuf | |
9707 | + elfsym->destshndx_index)); | |
9708 | } | |
9709 | ||
1ff6de03 NA |
9710 | /* Allow the linker to examine the strtab and symtab now they are |
9711 | populated. */ | |
9712 | ||
9713 | if (flinfo->info->callbacks->examine_strtab) | |
9714 | flinfo->info->callbacks->examine_strtab (hash_table->strtab, | |
9715 | hash_table->strtabcount, | |
9716 | flinfo->symstrtab); | |
9717 | ||
ef10c3ac L |
9718 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; |
9719 | pos = hdr->sh_offset + hdr->sh_size; | |
9720 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9721 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9722 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9723 | { | |
9724 | hdr->sh_size += amt; | |
9725 | ret = TRUE; | |
9726 | } | |
9727 | else | |
9728 | ret = FALSE; | |
c152c796 | 9729 | |
ef10c3ac L |
9730 | free (symbuf); |
9731 | ||
9732 | free (hash_table->strtab); | |
9733 | hash_table->strtab = NULL; | |
9734 | ||
9735 | return ret; | |
c152c796 AM |
9736 | } |
9737 | ||
c0d5a53d L |
9738 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9739 | ||
9740 | static bfd_boolean | |
9741 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9742 | { | |
4fbb74a6 AM |
9743 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9744 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9745 | { |
9746 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9747 | beyond 64k. */ |
4eca0228 | 9748 | _bfd_error_handler |
695344c0 | 9749 | /* xgettext:c-format */ |
9793eb77 | 9750 | (_("%pB: too many sections: %d (>= %d)"), |
4fbb74a6 | 9751 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9752 | bfd_set_error (bfd_error_nonrepresentable_section); |
9753 | return FALSE; | |
9754 | } | |
9755 | return TRUE; | |
9756 | } | |
9757 | ||
c152c796 AM |
9758 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9759 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9760 | versioned symbol that would normally require an explicit version. | |
9761 | We also handle the case that a DSO references a hidden symbol | |
9762 | which may be satisfied by a versioned symbol in another DSO. */ | |
9763 | ||
9764 | static bfd_boolean | |
9765 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9766 | const struct elf_backend_data *bed, | |
9767 | struct elf_link_hash_entry *h) | |
9768 | { | |
9769 | bfd *abfd; | |
9770 | struct elf_link_loaded_list *loaded; | |
9771 | ||
9772 | if (!is_elf_hash_table (info->hash)) | |
9773 | return FALSE; | |
9774 | ||
90c984fc L |
9775 | /* Check indirect symbol. */ |
9776 | while (h->root.type == bfd_link_hash_indirect) | |
9777 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9778 | ||
c152c796 AM |
9779 | switch (h->root.type) |
9780 | { | |
9781 | default: | |
9782 | abfd = NULL; | |
9783 | break; | |
9784 | ||
9785 | case bfd_link_hash_undefined: | |
9786 | case bfd_link_hash_undefweak: | |
9787 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9788 | if (abfd == NULL |
9789 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9790 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9791 | return FALSE; |
9792 | break; | |
9793 | ||
9794 | case bfd_link_hash_defined: | |
9795 | case bfd_link_hash_defweak: | |
9796 | abfd = h->root.u.def.section->owner; | |
9797 | break; | |
9798 | ||
9799 | case bfd_link_hash_common: | |
9800 | abfd = h->root.u.c.p->section->owner; | |
9801 | break; | |
9802 | } | |
9803 | BFD_ASSERT (abfd != NULL); | |
9804 | ||
e310298c | 9805 | for (loaded = elf_hash_table (info)->dyn_loaded; |
c152c796 AM |
9806 | loaded != NULL; |
9807 | loaded = loaded->next) | |
9808 | { | |
9809 | bfd *input; | |
9810 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9811 | size_t symcount; |
9812 | size_t extsymcount; | |
9813 | size_t extsymoff; | |
c152c796 AM |
9814 | Elf_Internal_Shdr *versymhdr; |
9815 | Elf_Internal_Sym *isym; | |
9816 | Elf_Internal_Sym *isymend; | |
9817 | Elf_Internal_Sym *isymbuf; | |
9818 | Elf_External_Versym *ever; | |
9819 | Elf_External_Versym *extversym; | |
9820 | ||
9821 | input = loaded->abfd; | |
9822 | ||
9823 | /* We check each DSO for a possible hidden versioned definition. */ | |
9824 | if (input == abfd | |
c152c796 AM |
9825 | || elf_dynversym (input) == 0) |
9826 | continue; | |
9827 | ||
9828 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9829 | ||
9830 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9831 | if (elf_bad_symtab (input)) | |
9832 | { | |
9833 | extsymcount = symcount; | |
9834 | extsymoff = 0; | |
9835 | } | |
9836 | else | |
9837 | { | |
9838 | extsymcount = symcount - hdr->sh_info; | |
9839 | extsymoff = hdr->sh_info; | |
9840 | } | |
9841 | ||
9842 | if (extsymcount == 0) | |
9843 | continue; | |
9844 | ||
9845 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9846 | NULL, NULL, NULL); | |
9847 | if (isymbuf == NULL) | |
9848 | return FALSE; | |
9849 | ||
9850 | /* Read in any version definitions. */ | |
9851 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
c152c796 | 9852 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 |
2bb3687b AM |
9853 | || (extversym = (Elf_External_Versym *) |
9854 | _bfd_malloc_and_read (input, versymhdr->sh_size, | |
9855 | versymhdr->sh_size)) == NULL) | |
c152c796 | 9856 | { |
c152c796 AM |
9857 | free (isymbuf); |
9858 | return FALSE; | |
9859 | } | |
9860 | ||
9861 | ever = extversym + extsymoff; | |
9862 | isymend = isymbuf + extsymcount; | |
9863 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9864 | { | |
9865 | const char *name; | |
9866 | Elf_Internal_Versym iver; | |
9867 | unsigned short version_index; | |
9868 | ||
9869 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9870 | || isym->st_shndx == SHN_UNDEF) | |
9871 | continue; | |
9872 | ||
9873 | name = bfd_elf_string_from_elf_section (input, | |
9874 | hdr->sh_link, | |
9875 | isym->st_name); | |
9876 | if (strcmp (name, h->root.root.string) != 0) | |
9877 | continue; | |
9878 | ||
9879 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9880 | ||
d023c380 L |
9881 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9882 | && !(h->def_regular | |
9883 | && h->forced_local)) | |
c152c796 AM |
9884 | { |
9885 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9886 | have provided a definition for the undefined sym unless |
9887 | it is defined in a non-shared object and forced local. | |
9888 | */ | |
c152c796 AM |
9889 | abort (); |
9890 | } | |
9891 | ||
9892 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9893 | if (version_index == 1 || version_index == 2) | |
9894 | { | |
9895 | /* This is the base or first version. We can use it. */ | |
9896 | free (extversym); | |
9897 | free (isymbuf); | |
9898 | return TRUE; | |
9899 | } | |
9900 | } | |
9901 | ||
9902 | free (extversym); | |
9903 | free (isymbuf); | |
9904 | } | |
9905 | ||
9906 | return FALSE; | |
9907 | } | |
9908 | ||
b8871f35 L |
9909 | /* Convert ELF common symbol TYPE. */ |
9910 | ||
9911 | static int | |
9912 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9913 | { | |
9914 | /* Commom symbol can only appear in relocatable link. */ | |
9915 | if (!bfd_link_relocatable (info)) | |
9916 | abort (); | |
9917 | switch (info->elf_stt_common) | |
9918 | { | |
9919 | case unchanged: | |
9920 | break; | |
9921 | case elf_stt_common: | |
9922 | type = STT_COMMON; | |
9923 | break; | |
9924 | case no_elf_stt_common: | |
9925 | type = STT_OBJECT; | |
9926 | break; | |
9927 | } | |
9928 | return type; | |
9929 | } | |
9930 | ||
c152c796 AM |
9931 | /* Add an external symbol to the symbol table. This is called from |
9932 | the hash table traversal routine. When generating a shared object, | |
9933 | we go through the symbol table twice. The first time we output | |
9934 | anything that might have been forced to local scope in a version | |
9935 | script. The second time we output the symbols that are still | |
9936 | global symbols. */ | |
9937 | ||
9938 | static bfd_boolean | |
7686d77d | 9939 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9940 | { |
7686d77d | 9941 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9942 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9943 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9944 | bfd_boolean strip; |
9945 | Elf_Internal_Sym sym; | |
9946 | asection *input_sec; | |
9947 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9948 | long indx; |
9949 | int ret; | |
b8871f35 | 9950 | unsigned int type; |
c152c796 AM |
9951 | |
9952 | if (h->root.type == bfd_link_hash_warning) | |
9953 | { | |
9954 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9955 | if (h->root.type == bfd_link_hash_new) | |
9956 | return TRUE; | |
9957 | } | |
9958 | ||
9959 | /* Decide whether to output this symbol in this pass. */ | |
9960 | if (eoinfo->localsyms) | |
9961 | { | |
4deb8f71 | 9962 | if (!h->forced_local) |
c152c796 AM |
9963 | return TRUE; |
9964 | } | |
9965 | else | |
9966 | { | |
4deb8f71 | 9967 | if (h->forced_local) |
c152c796 AM |
9968 | return TRUE; |
9969 | } | |
9970 | ||
8b127cbc | 9971 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9972 | |
12ac1cf5 | 9973 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9974 | { |
12ac1cf5 NC |
9975 | /* If we have an undefined symbol reference here then it must have |
9976 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9977 | references in regular files have already been handled unless |
9978 | they are in unreferenced sections which are removed by garbage | |
9979 | collection). */ | |
12ac1cf5 NC |
9980 | bfd_boolean ignore_undef = FALSE; |
9981 | ||
9982 | /* Some symbols may be special in that the fact that they're | |
9983 | undefined can be safely ignored - let backend determine that. */ | |
9984 | if (bed->elf_backend_ignore_undef_symbol) | |
9985 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9986 | ||
9987 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9988 | if (!ignore_undef |
c54f1524 | 9989 | && h->ref_dynamic_nonweak |
8b127cbc AM |
9990 | && (!h->ref_regular || flinfo->info->gc_sections) |
9991 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9992 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
95a51568 FS |
9993 | { |
9994 | flinfo->info->callbacks->undefined_symbol | |
9995 | (flinfo->info, h->root.root.string, | |
9996 | h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0, | |
9997 | flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE | |
9998 | && !flinfo->info->warn_unresolved_syms); | |
9999 | } | |
97196564 L |
10000 | |
10001 | /* Strip a global symbol defined in a discarded section. */ | |
10002 | if (h->indx == -3) | |
10003 | return TRUE; | |
c152c796 AM |
10004 | } |
10005 | ||
10006 | /* We should also warn if a forced local symbol is referenced from | |
10007 | shared libraries. */ | |
0e1862bb | 10008 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
10009 | && h->forced_local |
10010 | && h->ref_dynamic | |
371a5866 | 10011 | && h->def_regular |
f5385ebf | 10012 | && !h->dynamic_def |
ee659f1f | 10013 | && h->ref_dynamic_nonweak |
8b127cbc | 10014 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 10015 | { |
17d078c5 AM |
10016 | bfd *def_bfd; |
10017 | const char *msg; | |
90c984fc L |
10018 | struct elf_link_hash_entry *hi = h; |
10019 | ||
10020 | /* Check indirect symbol. */ | |
10021 | while (hi->root.type == bfd_link_hash_indirect) | |
10022 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
10023 | |
10024 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 10025 | /* xgettext:c-format */ |
871b3ab2 | 10026 | msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 10027 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) |
695344c0 | 10028 | /* xgettext:c-format */ |
871b3ab2 | 10029 | msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 10030 | else |
695344c0 | 10031 | /* xgettext:c-format */ |
871b3ab2 | 10032 | msg = _("%pB: local symbol `%s' in %pB is referenced by DSO"); |
8b127cbc | 10033 | def_bfd = flinfo->output_bfd; |
90c984fc L |
10034 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
10035 | def_bfd = hi->root.u.def.section->owner; | |
c08bb8dd AM |
10036 | _bfd_error_handler (msg, flinfo->output_bfd, |
10037 | h->root.root.string, def_bfd); | |
17d078c5 | 10038 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
10039 | eoinfo->failed = TRUE; |
10040 | return FALSE; | |
10041 | } | |
10042 | ||
10043 | /* We don't want to output symbols that have never been mentioned by | |
10044 | a regular file, or that we have been told to strip. However, if | |
10045 | h->indx is set to -2, the symbol is used by a reloc and we must | |
10046 | output it. */ | |
d983c8c5 | 10047 | strip = FALSE; |
c152c796 | 10048 | if (h->indx == -2) |
d983c8c5 | 10049 | ; |
f5385ebf | 10050 | else if ((h->def_dynamic |
77cfaee6 AM |
10051 | || h->ref_dynamic |
10052 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
10053 | && !h->def_regular |
10054 | && !h->ref_regular) | |
c152c796 | 10055 | strip = TRUE; |
8b127cbc | 10056 | else if (flinfo->info->strip == strip_all) |
c152c796 | 10057 | strip = TRUE; |
8b127cbc AM |
10058 | else if (flinfo->info->strip == strip_some |
10059 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
10060 | h->root.root.string, FALSE, FALSE) == NULL) |
10061 | strip = TRUE; | |
d56d55e7 AM |
10062 | else if ((h->root.type == bfd_link_hash_defined |
10063 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 10064 | && ((flinfo->info->strip_discarded |
dbaa2011 | 10065 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
10066 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
10067 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 10068 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 10069 | strip = TRUE; |
9e2278f5 AM |
10070 | else if ((h->root.type == bfd_link_hash_undefined |
10071 | || h->root.type == bfd_link_hash_undefweak) | |
10072 | && h->root.u.undef.abfd != NULL | |
10073 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
10074 | strip = TRUE; | |
c152c796 | 10075 | |
b8871f35 L |
10076 | type = h->type; |
10077 | ||
c152c796 | 10078 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
10079 | nothing else to do. However, if it is a forced local symbol or |
10080 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
10081 | function a chance to make it dynamic. */ | |
c152c796 AM |
10082 | if (strip |
10083 | && h->dynindx == -1 | |
b8871f35 | 10084 | && type != STT_GNU_IFUNC |
f5385ebf | 10085 | && !h->forced_local) |
c152c796 AM |
10086 | return TRUE; |
10087 | ||
10088 | sym.st_value = 0; | |
10089 | sym.st_size = h->size; | |
10090 | sym.st_other = h->other; | |
c152c796 AM |
10091 | switch (h->root.type) |
10092 | { | |
10093 | default: | |
10094 | case bfd_link_hash_new: | |
10095 | case bfd_link_hash_warning: | |
10096 | abort (); | |
10097 | return FALSE; | |
10098 | ||
10099 | case bfd_link_hash_undefined: | |
10100 | case bfd_link_hash_undefweak: | |
10101 | input_sec = bfd_und_section_ptr; | |
10102 | sym.st_shndx = SHN_UNDEF; | |
10103 | break; | |
10104 | ||
10105 | case bfd_link_hash_defined: | |
10106 | case bfd_link_hash_defweak: | |
10107 | { | |
10108 | input_sec = h->root.u.def.section; | |
10109 | if (input_sec->output_section != NULL) | |
10110 | { | |
10111 | sym.st_shndx = | |
8b127cbc | 10112 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
10113 | input_sec->output_section); |
10114 | if (sym.st_shndx == SHN_BAD) | |
10115 | { | |
4eca0228 | 10116 | _bfd_error_handler |
695344c0 | 10117 | /* xgettext:c-format */ |
871b3ab2 | 10118 | (_("%pB: could not find output section %pA for input section %pA"), |
8b127cbc | 10119 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 10120 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
10121 | eoinfo->failed = TRUE; |
10122 | return FALSE; | |
10123 | } | |
10124 | ||
10125 | /* ELF symbols in relocatable files are section relative, | |
10126 | but in nonrelocatable files they are virtual | |
10127 | addresses. */ | |
10128 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 10129 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10130 | { |
10131 | sym.st_value += input_sec->output_section->vma; | |
10132 | if (h->type == STT_TLS) | |
10133 | { | |
8b127cbc | 10134 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
10135 | if (tls_sec != NULL) |
10136 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
10137 | } |
10138 | } | |
10139 | } | |
10140 | else | |
10141 | { | |
10142 | BFD_ASSERT (input_sec->owner == NULL | |
10143 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
10144 | sym.st_shndx = SHN_UNDEF; | |
10145 | input_sec = bfd_und_section_ptr; | |
10146 | } | |
10147 | } | |
10148 | break; | |
10149 | ||
10150 | case bfd_link_hash_common: | |
10151 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 10152 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
10153 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
10154 | break; | |
10155 | ||
10156 | case bfd_link_hash_indirect: | |
10157 | /* These symbols are created by symbol versioning. They point | |
10158 | to the decorated version of the name. For example, if the | |
10159 | symbol foo@@GNU_1.2 is the default, which should be used when | |
10160 | foo is used with no version, then we add an indirect symbol | |
10161 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
10162 | since the indirected symbol is already in the hash table. */ | |
10163 | return TRUE; | |
10164 | } | |
10165 | ||
b8871f35 L |
10166 | if (type == STT_COMMON || type == STT_OBJECT) |
10167 | switch (h->root.type) | |
10168 | { | |
10169 | case bfd_link_hash_common: | |
10170 | type = elf_link_convert_common_type (flinfo->info, type); | |
10171 | break; | |
10172 | case bfd_link_hash_defined: | |
10173 | case bfd_link_hash_defweak: | |
10174 | if (bed->common_definition (&sym)) | |
10175 | type = elf_link_convert_common_type (flinfo->info, type); | |
10176 | else | |
10177 | type = STT_OBJECT; | |
10178 | break; | |
10179 | case bfd_link_hash_undefined: | |
10180 | case bfd_link_hash_undefweak: | |
10181 | break; | |
10182 | default: | |
10183 | abort (); | |
10184 | } | |
10185 | ||
4deb8f71 | 10186 | if (h->forced_local) |
b8871f35 L |
10187 | { |
10188 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
10189 | /* Turn off visibility on local symbol. */ | |
10190 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
10191 | } | |
10192 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
10193 | else if (h->unique_global && h->def_regular) | |
10194 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
10195 | else if (h->root.type == bfd_link_hash_undefweak | |
10196 | || h->root.type == bfd_link_hash_defweak) | |
10197 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
10198 | else | |
10199 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
10200 | sym.st_target_internal = h->target_internal; | |
10201 | ||
c152c796 AM |
10202 | /* Give the processor backend a chance to tweak the symbol value, |
10203 | and also to finish up anything that needs to be done for this | |
10204 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 10205 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 10206 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 10207 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 10208 | && h->def_regular |
0e1862bb | 10209 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
10210 | || ((h->dynindx != -1 |
10211 | || h->forced_local) | |
0e1862bb | 10212 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
10213 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
10214 | || h->root.type != bfd_link_hash_undefweak)) | |
10215 | || !h->forced_local) | |
8b127cbc | 10216 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
10217 | { |
10218 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 10219 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
10220 | { |
10221 | eoinfo->failed = TRUE; | |
10222 | return FALSE; | |
10223 | } | |
10224 | } | |
10225 | ||
10226 | /* If we are marking the symbol as undefined, and there are no | |
10227 | non-weak references to this symbol from a regular object, then | |
10228 | mark the symbol as weak undefined; if there are non-weak | |
10229 | references, mark the symbol as strong. We can't do this earlier, | |
10230 | because it might not be marked as undefined until the | |
10231 | finish_dynamic_symbol routine gets through with it. */ | |
10232 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 10233 | && h->ref_regular |
c152c796 AM |
10234 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
10235 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
10236 | { | |
10237 | int bindtype; | |
b8871f35 | 10238 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
10239 | |
10240 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
10241 | if (type == STT_GNU_IFUNC) | |
10242 | type = STT_FUNC; | |
c152c796 | 10243 | |
f5385ebf | 10244 | if (h->ref_regular_nonweak) |
c152c796 AM |
10245 | bindtype = STB_GLOBAL; |
10246 | else | |
10247 | bindtype = STB_WEAK; | |
2955ec4c | 10248 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
10249 | } |
10250 | ||
bda987c2 CD |
10251 | /* If this is a symbol defined in a dynamic library, don't use the |
10252 | symbol size from the dynamic library. Relinking an executable | |
10253 | against a new library may introduce gratuitous changes in the | |
10254 | executable's symbols if we keep the size. */ | |
10255 | if (sym.st_shndx == SHN_UNDEF | |
10256 | && !h->def_regular | |
10257 | && h->def_dynamic) | |
10258 | sym.st_size = 0; | |
10259 | ||
c152c796 AM |
10260 | /* If a non-weak symbol with non-default visibility is not defined |
10261 | locally, it is a fatal error. */ | |
0e1862bb | 10262 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
10263 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
10264 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
10265 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 10266 | && !h->def_regular) |
c152c796 | 10267 | { |
17d078c5 AM |
10268 | const char *msg; |
10269 | ||
10270 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 10271 | /* xgettext:c-format */ |
871b3ab2 | 10272 | msg = _("%pB: protected symbol `%s' isn't defined"); |
17d078c5 | 10273 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) |
695344c0 | 10274 | /* xgettext:c-format */ |
871b3ab2 | 10275 | msg = _("%pB: internal symbol `%s' isn't defined"); |
17d078c5 | 10276 | else |
695344c0 | 10277 | /* xgettext:c-format */ |
871b3ab2 | 10278 | msg = _("%pB: hidden symbol `%s' isn't defined"); |
4eca0228 | 10279 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 10280 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
10281 | eoinfo->failed = TRUE; |
10282 | return FALSE; | |
10283 | } | |
10284 | ||
10285 | /* If this symbol should be put in the .dynsym section, then put it | |
10286 | there now. We already know the symbol index. We also fill in | |
10287 | the entry in the .hash section. */ | |
1c2649ed EB |
10288 | if (h->dynindx != -1 |
10289 | && elf_hash_table (flinfo->info)->dynamic_sections_created | |
10290 | && elf_hash_table (flinfo->info)->dynsym != NULL | |
10291 | && !discarded_section (elf_hash_table (flinfo->info)->dynsym)) | |
c152c796 | 10292 | { |
c152c796 AM |
10293 | bfd_byte *esym; |
10294 | ||
90c984fc L |
10295 | /* Since there is no version information in the dynamic string, |
10296 | if there is no version info in symbol version section, we will | |
1659f720 | 10297 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 10298 | by shared library, or not bound locally. */ |
1659f720 | 10299 | if (h->verinfo.verdef == NULL |
0e1862bb | 10300 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
10301 | || h->ref_dynamic |
10302 | || !h->def_regular)) | |
90c984fc L |
10303 | { |
10304 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
10305 | ||
10306 | if (p && p [1] != '\0') | |
10307 | { | |
4eca0228 | 10308 | _bfd_error_handler |
695344c0 | 10309 | /* xgettext:c-format */ |
9793eb77 | 10310 | (_("%pB: no symbol version section for versioned symbol `%s'"), |
90c984fc L |
10311 | flinfo->output_bfd, h->root.root.string); |
10312 | eoinfo->failed = TRUE; | |
10313 | return FALSE; | |
10314 | } | |
10315 | } | |
10316 | ||
c152c796 | 10317 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
10318 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
10319 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 10320 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
10321 | { |
10322 | eoinfo->failed = TRUE; | |
10323 | return FALSE; | |
10324 | } | |
8b127cbc | 10325 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 10326 | |
8b127cbc | 10327 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
10328 | { |
10329 | size_t hash_entry_size; | |
10330 | bfd_byte *bucketpos; | |
10331 | bfd_vma chain; | |
41198d0c L |
10332 | size_t bucketcount; |
10333 | size_t bucket; | |
10334 | ||
8b127cbc | 10335 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 10336 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
10337 | |
10338 | hash_entry_size | |
8b127cbc AM |
10339 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
10340 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 10341 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
10342 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
10343 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
10344 | bucketpos); | |
10345 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
10346 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
10347 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
10348 | } | |
c152c796 | 10349 | |
8b127cbc | 10350 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
10351 | { |
10352 | Elf_Internal_Versym iversym; | |
10353 | Elf_External_Versym *eversym; | |
10354 | ||
5fa370e4 | 10355 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
c152c796 | 10356 | { |
7b20f099 AM |
10357 | if (h->verinfo.verdef == NULL |
10358 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
10359 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
10360 | iversym.vs_vers = 0; |
10361 | else | |
10362 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
10363 | } | |
10364 | else | |
10365 | { | |
10366 | if (h->verinfo.vertree == NULL) | |
10367 | iversym.vs_vers = 1; | |
10368 | else | |
10369 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 10370 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 10371 | iversym.vs_vers++; |
c152c796 AM |
10372 | } |
10373 | ||
422f1182 | 10374 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 10375 | defined locally. */ |
422f1182 | 10376 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
10377 | iversym.vs_vers |= VERSYM_HIDDEN; |
10378 | ||
8b127cbc | 10379 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 10380 | eversym += h->dynindx; |
8b127cbc | 10381 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
10382 | } |
10383 | } | |
10384 | ||
d983c8c5 AM |
10385 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
10386 | strip it from .symtab too. Obviously we can't do this for | |
10387 | relocatable output or when needed for --emit-relocs. */ | |
10388 | else if (input_sec == bfd_und_section_ptr | |
10389 | && h->indx != -2 | |
66cae560 NC |
10390 | /* PR 22319 Do not strip global undefined symbols marked as being needed. */ |
10391 | && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) | |
0e1862bb | 10392 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 | 10393 | return TRUE; |
66cae560 | 10394 | |
d983c8c5 AM |
10395 | /* Also strip others that we couldn't earlier due to dynamic symbol |
10396 | processing. */ | |
10397 | if (strip) | |
10398 | return TRUE; | |
10399 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
10400 | return TRUE; |
10401 | ||
2ec55de3 AM |
10402 | /* Output a FILE symbol so that following locals are not associated |
10403 | with the wrong input file. We need one for forced local symbols | |
10404 | if we've seen more than one FILE symbol or when we have exactly | |
10405 | one FILE symbol but global symbols are present in a file other | |
10406 | than the one with the FILE symbol. We also need one if linker | |
10407 | defined symbols are present. In practice these conditions are | |
10408 | always met, so just emit the FILE symbol unconditionally. */ | |
10409 | if (eoinfo->localsyms | |
10410 | && !eoinfo->file_sym_done | |
10411 | && eoinfo->flinfo->filesym_count != 0) | |
10412 | { | |
10413 | Elf_Internal_Sym fsym; | |
10414 | ||
10415 | memset (&fsym, 0, sizeof (fsym)); | |
10416 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10417 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10418 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
10419 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
10420 | return FALSE; |
10421 | ||
10422 | eoinfo->file_sym_done = TRUE; | |
10423 | } | |
10424 | ||
8b127cbc | 10425 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
10426 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
10427 | input_sec, h); | |
6e0b88f1 | 10428 | if (ret == 0) |
c152c796 AM |
10429 | { |
10430 | eoinfo->failed = TRUE; | |
10431 | return FALSE; | |
10432 | } | |
6e0b88f1 AM |
10433 | else if (ret == 1) |
10434 | h->indx = indx; | |
10435 | else if (h->indx == -2) | |
10436 | abort(); | |
c152c796 AM |
10437 | |
10438 | return TRUE; | |
10439 | } | |
10440 | ||
cdd3575c AM |
10441 | /* Return TRUE if special handling is done for relocs in SEC against |
10442 | symbols defined in discarded sections. */ | |
10443 | ||
c152c796 AM |
10444 | static bfd_boolean |
10445 | elf_section_ignore_discarded_relocs (asection *sec) | |
10446 | { | |
10447 | const struct elf_backend_data *bed; | |
10448 | ||
cdd3575c AM |
10449 | switch (sec->sec_info_type) |
10450 | { | |
dbaa2011 AM |
10451 | case SEC_INFO_TYPE_STABS: |
10452 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 10453 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
10454 | return TRUE; |
10455 | default: | |
10456 | break; | |
10457 | } | |
c152c796 AM |
10458 | |
10459 | bed = get_elf_backend_data (sec->owner); | |
10460 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
10461 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
10462 | return TRUE; | |
10463 | ||
10464 | return FALSE; | |
10465 | } | |
10466 | ||
9e66c942 AM |
10467 | /* Return a mask saying how ld should treat relocations in SEC against |
10468 | symbols defined in discarded sections. If this function returns | |
10469 | COMPLAIN set, ld will issue a warning message. If this function | |
10470 | returns PRETEND set, and the discarded section was link-once and the | |
10471 | same size as the kept link-once section, ld will pretend that the | |
10472 | symbol was actually defined in the kept section. Otherwise ld will | |
10473 | zero the reloc (at least that is the intent, but some cooperation by | |
10474 | the target dependent code is needed, particularly for REL targets). */ | |
10475 | ||
8a696751 AM |
10476 | unsigned int |
10477 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 10478 | { |
9e66c942 | 10479 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 10480 | return PRETEND; |
cdd3575c AM |
10481 | |
10482 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 10483 | return 0; |
cdd3575c AM |
10484 | |
10485 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 10486 | return 0; |
cdd3575c | 10487 | |
9e66c942 | 10488 | return COMPLAIN | PRETEND; |
cdd3575c AM |
10489 | } |
10490 | ||
3d7f7666 L |
10491 | /* Find a match between a section and a member of a section group. */ |
10492 | ||
10493 | static asection * | |
c0f00686 L |
10494 | match_group_member (asection *sec, asection *group, |
10495 | struct bfd_link_info *info) | |
3d7f7666 L |
10496 | { |
10497 | asection *first = elf_next_in_group (group); | |
10498 | asection *s = first; | |
10499 | ||
10500 | while (s != NULL) | |
10501 | { | |
c0f00686 | 10502 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
10503 | return s; |
10504 | ||
83180ade | 10505 | s = elf_next_in_group (s); |
3d7f7666 L |
10506 | if (s == first) |
10507 | break; | |
10508 | } | |
10509 | ||
10510 | return NULL; | |
10511 | } | |
10512 | ||
01b3c8ab | 10513 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
10514 | to replace it. Return the replacement if it is OK. Otherwise return |
10515 | NULL. */ | |
01b3c8ab L |
10516 | |
10517 | asection * | |
c0f00686 | 10518 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
10519 | { |
10520 | asection *kept; | |
10521 | ||
10522 | kept = sec->kept_section; | |
10523 | if (kept != NULL) | |
10524 | { | |
c2370991 | 10525 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 10526 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
10527 | if (kept != NULL |
10528 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
10529 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 10530 | kept = NULL; |
c2370991 | 10531 | sec->kept_section = kept; |
01b3c8ab L |
10532 | } |
10533 | return kept; | |
10534 | } | |
10535 | ||
c152c796 AM |
10536 | /* Link an input file into the linker output file. This function |
10537 | handles all the sections and relocations of the input file at once. | |
10538 | This is so that we only have to read the local symbols once, and | |
10539 | don't have to keep them in memory. */ | |
10540 | ||
10541 | static bfd_boolean | |
8b127cbc | 10542 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 10543 | { |
ece5ef60 | 10544 | int (*relocate_section) |
c152c796 AM |
10545 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
10546 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
10547 | bfd *output_bfd; | |
10548 | Elf_Internal_Shdr *symtab_hdr; | |
10549 | size_t locsymcount; | |
10550 | size_t extsymoff; | |
10551 | Elf_Internal_Sym *isymbuf; | |
10552 | Elf_Internal_Sym *isym; | |
10553 | Elf_Internal_Sym *isymend; | |
10554 | long *pindex; | |
10555 | asection **ppsection; | |
10556 | asection *o; | |
10557 | const struct elf_backend_data *bed; | |
c152c796 | 10558 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
10559 | bfd_size_type address_size; |
10560 | bfd_vma r_type_mask; | |
10561 | int r_sym_shift; | |
ffbc01cc | 10562 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 10563 | |
8b127cbc | 10564 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
10565 | bed = get_elf_backend_data (output_bfd); |
10566 | relocate_section = bed->elf_backend_relocate_section; | |
10567 | ||
10568 | /* If this is a dynamic object, we don't want to do anything here: | |
10569 | we don't want the local symbols, and we don't want the section | |
10570 | contents. */ | |
10571 | if ((input_bfd->flags & DYNAMIC) != 0) | |
10572 | return TRUE; | |
10573 | ||
c152c796 AM |
10574 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
10575 | if (elf_bad_symtab (input_bfd)) | |
10576 | { | |
10577 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10578 | extsymoff = 0; | |
10579 | } | |
10580 | else | |
10581 | { | |
10582 | locsymcount = symtab_hdr->sh_info; | |
10583 | extsymoff = symtab_hdr->sh_info; | |
10584 | } | |
10585 | ||
10586 | /* Read the local symbols. */ | |
10587 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10588 | if (isymbuf == NULL && locsymcount != 0) | |
10589 | { | |
10590 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
10591 | flinfo->internal_syms, |
10592 | flinfo->external_syms, | |
10593 | flinfo->locsym_shndx); | |
c152c796 AM |
10594 | if (isymbuf == NULL) |
10595 | return FALSE; | |
10596 | } | |
10597 | ||
10598 | /* Find local symbol sections and adjust values of symbols in | |
10599 | SEC_MERGE sections. Write out those local symbols we know are | |
10600 | going into the output file. */ | |
10601 | isymend = isymbuf + locsymcount; | |
8b127cbc | 10602 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
10603 | isym < isymend; |
10604 | isym++, pindex++, ppsection++) | |
10605 | { | |
10606 | asection *isec; | |
10607 | const char *name; | |
10608 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
10609 | long indx; |
10610 | int ret; | |
c152c796 AM |
10611 | |
10612 | *pindex = -1; | |
10613 | ||
10614 | if (elf_bad_symtab (input_bfd)) | |
10615 | { | |
10616 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
10617 | { | |
10618 | *ppsection = NULL; | |
10619 | continue; | |
10620 | } | |
10621 | } | |
10622 | ||
10623 | if (isym->st_shndx == SHN_UNDEF) | |
10624 | isec = bfd_und_section_ptr; | |
c152c796 AM |
10625 | else if (isym->st_shndx == SHN_ABS) |
10626 | isec = bfd_abs_section_ptr; | |
10627 | else if (isym->st_shndx == SHN_COMMON) | |
10628 | isec = bfd_com_section_ptr; | |
10629 | else | |
10630 | { | |
cb33740c AM |
10631 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
10632 | if (isec == NULL) | |
10633 | { | |
10634 | /* Don't attempt to output symbols with st_shnx in the | |
10635 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
6835821b | 10636 | isec = bfd_und_section_ptr; |
cb33740c | 10637 | } |
dbaa2011 | 10638 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
10639 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
10640 | isym->st_value = | |
10641 | _bfd_merged_section_offset (output_bfd, &isec, | |
10642 | elf_section_data (isec)->sec_info, | |
10643 | isym->st_value); | |
c152c796 AM |
10644 | } |
10645 | ||
10646 | *ppsection = isec; | |
10647 | ||
d983c8c5 AM |
10648 | /* Don't output the first, undefined, symbol. In fact, don't |
10649 | output any undefined local symbol. */ | |
10650 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
10651 | continue; |
10652 | ||
10653 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
10654 | { | |
10655 | /* We never output section symbols. Instead, we use the | |
10656 | section symbol of the corresponding section in the output | |
10657 | file. */ | |
10658 | continue; | |
10659 | } | |
10660 | ||
10661 | /* If we are stripping all symbols, we don't want to output this | |
10662 | one. */ | |
8b127cbc | 10663 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10664 | continue; |
10665 | ||
10666 | /* If we are discarding all local symbols, we don't want to | |
10667 | output this one. If we are generating a relocatable output | |
10668 | file, then some of the local symbols may be required by | |
10669 | relocs; we output them below as we discover that they are | |
10670 | needed. */ | |
8b127cbc | 10671 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
10672 | continue; |
10673 | ||
10674 | /* If this symbol is defined in a section which we are | |
f02571c5 | 10675 | discarding, we don't need to keep it. */ |
abf874aa CL |
10676 | if (isym->st_shndx != SHN_UNDEF |
10677 | && isym->st_shndx < SHN_LORESERVE | |
10678 | && isec->output_section == NULL | |
10679 | && flinfo->info->non_contiguous_regions | |
10680 | && flinfo->info->non_contiguous_regions_warnings) | |
10681 | { | |
10682 | _bfd_error_handler (_("warning: --enable-non-contiguous-regions " | |
10683 | "discards section `%s' from '%s'\n"), | |
765cf5f6 | 10684 | isec->name, bfd_get_filename (isec->owner)); |
abf874aa CL |
10685 | continue; |
10686 | } | |
10687 | ||
f02571c5 | 10688 | if (isym->st_shndx != SHN_UNDEF |
4fbb74a6 AM |
10689 | && isym->st_shndx < SHN_LORESERVE |
10690 | && bfd_section_removed_from_list (output_bfd, | |
10691 | isec->output_section)) | |
e75a280b L |
10692 | continue; |
10693 | ||
c152c796 AM |
10694 | /* Get the name of the symbol. */ |
10695 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
10696 | isym->st_name); | |
10697 | if (name == NULL) | |
10698 | return FALSE; | |
10699 | ||
10700 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
10701 | if ((flinfo->info->strip == strip_some |
10702 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 10703 | == NULL)) |
8b127cbc | 10704 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10705 | && (isec->flags & SEC_MERGE) |
10706 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10707 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10708 | && bfd_is_local_label_name (input_bfd, name))) |
10709 | continue; | |
10710 | ||
ffbc01cc AM |
10711 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10712 | { | |
ce875075 AM |
10713 | if (input_bfd->lto_output) |
10714 | /* -flto puts a temp file name here. This means builds | |
10715 | are not reproducible. Discard the symbol. */ | |
10716 | continue; | |
ffbc01cc AM |
10717 | have_file_sym = TRUE; |
10718 | flinfo->filesym_count += 1; | |
10719 | } | |
10720 | if (!have_file_sym) | |
10721 | { | |
10722 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10723 | FILE symbols to determine the source file for local | |
10724 | function symbols. Provide a FILE symbol here if input | |
10725 | files lack such, so that their symbols won't be | |
10726 | associated with a previous input file. It's not the | |
10727 | source file, but the best we can do. */ | |
10728 | have_file_sym = TRUE; | |
10729 | flinfo->filesym_count += 1; | |
10730 | memset (&osym, 0, sizeof (osym)); | |
10731 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10732 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10733 | if (!elf_link_output_symstrtab (flinfo, |
10734 | (input_bfd->lto_output ? NULL | |
765cf5f6 | 10735 | : bfd_get_filename (input_bfd)), |
ef10c3ac L |
10736 | &osym, bfd_abs_section_ptr, |
10737 | NULL)) | |
ffbc01cc AM |
10738 | return FALSE; |
10739 | } | |
10740 | ||
c152c796 AM |
10741 | osym = *isym; |
10742 | ||
10743 | /* Adjust the section index for the output file. */ | |
10744 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10745 | isec->output_section); | |
10746 | if (osym.st_shndx == SHN_BAD) | |
10747 | return FALSE; | |
10748 | ||
c152c796 AM |
10749 | /* ELF symbols in relocatable files are section relative, but |
10750 | in executable files they are virtual addresses. Note that | |
10751 | this code assumes that all ELF sections have an associated | |
10752 | BFD section with a reasonable value for output_offset; below | |
10753 | we assume that they also have a reasonable value for | |
10754 | output_section. Any special sections must be set up to meet | |
10755 | these requirements. */ | |
10756 | osym.st_value += isec->output_offset; | |
0e1862bb | 10757 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10758 | { |
10759 | osym.st_value += isec->output_section->vma; | |
10760 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10761 | { | |
10762 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
102def4d AM |
10763 | if (elf_hash_table (flinfo->info)->tls_sec != NULL) |
10764 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
10765 | else | |
10766 | osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info), | |
10767 | STT_NOTYPE); | |
c152c796 AM |
10768 | } |
10769 | } | |
10770 | ||
6e0b88f1 | 10771 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10772 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10773 | if (ret == 0) |
c152c796 | 10774 | return FALSE; |
6e0b88f1 AM |
10775 | else if (ret == 1) |
10776 | *pindex = indx; | |
c152c796 AM |
10777 | } |
10778 | ||
310fd250 L |
10779 | if (bed->s->arch_size == 32) |
10780 | { | |
10781 | r_type_mask = 0xff; | |
10782 | r_sym_shift = 8; | |
10783 | address_size = 4; | |
10784 | } | |
10785 | else | |
10786 | { | |
10787 | r_type_mask = 0xffffffff; | |
10788 | r_sym_shift = 32; | |
10789 | address_size = 8; | |
10790 | } | |
10791 | ||
c152c796 AM |
10792 | /* Relocate the contents of each section. */ |
10793 | sym_hashes = elf_sym_hashes (input_bfd); | |
10794 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10795 | { | |
10796 | bfd_byte *contents; | |
10797 | ||
10798 | if (! o->linker_mark) | |
10799 | { | |
10800 | /* This section was omitted from the link. */ | |
10801 | continue; | |
10802 | } | |
10803 | ||
7bdf4127 | 10804 | if (!flinfo->info->resolve_section_groups |
bcacc0f5 AM |
10805 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10806 | { | |
10807 | /* Deal with the group signature symbol. */ | |
10808 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10809 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10810 | asection *osec = o->output_section; | |
10811 | ||
7bdf4127 | 10812 | BFD_ASSERT (bfd_link_relocatable (flinfo->info)); |
bcacc0f5 AM |
10813 | if (symndx >= locsymcount |
10814 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10815 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10816 | { |
10817 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10818 | while (h->root.type == bfd_link_hash_indirect | |
10819 | || h->root.type == bfd_link_hash_warning) | |
10820 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10821 | /* Arrange for symbol to be output. */ | |
10822 | h->indx = -2; | |
10823 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10824 | } | |
10825 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10826 | { | |
10827 | /* We'll use the output section target_index. */ | |
8b127cbc | 10828 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10829 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10830 | } | |
10831 | else | |
10832 | { | |
8b127cbc | 10833 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10834 | { |
10835 | /* Otherwise output the local symbol now. */ | |
10836 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10837 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10838 | const char *name; |
6e0b88f1 AM |
10839 | long indx; |
10840 | int ret; | |
bcacc0f5 AM |
10841 | |
10842 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10843 | symtab_hdr->sh_link, | |
10844 | sym.st_name); | |
10845 | if (name == NULL) | |
10846 | return FALSE; | |
10847 | ||
10848 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10849 | sec); | |
10850 | if (sym.st_shndx == SHN_BAD) | |
10851 | return FALSE; | |
10852 | ||
10853 | sym.st_value += o->output_offset; | |
10854 | ||
6e0b88f1 | 10855 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10856 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10857 | NULL); | |
6e0b88f1 | 10858 | if (ret == 0) |
bcacc0f5 | 10859 | return FALSE; |
6e0b88f1 | 10860 | else if (ret == 1) |
8b127cbc | 10861 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10862 | else |
10863 | abort (); | |
bcacc0f5 AM |
10864 | } |
10865 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10866 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10867 | } |
10868 | } | |
10869 | ||
c152c796 | 10870 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10871 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10872 | continue; |
10873 | ||
10874 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10875 | { | |
10876 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10877 | or somesuch. */ | |
10878 | continue; | |
10879 | } | |
10880 | ||
10881 | /* Get the contents of the section. They have been cached by a | |
10882 | relaxation routine. Note that o is a section in an input | |
10883 | file, so the contents field will not have been set by any of | |
10884 | the routines which work on output files. */ | |
10885 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10886 | { |
10887 | contents = elf_section_data (o)->this_hdr.contents; | |
10888 | if (bed->caches_rawsize | |
10889 | && o->rawsize != 0 | |
10890 | && o->rawsize < o->size) | |
10891 | { | |
10892 | memcpy (flinfo->contents, contents, o->rawsize); | |
10893 | contents = flinfo->contents; | |
10894 | } | |
10895 | } | |
c152c796 AM |
10896 | else |
10897 | { | |
8b127cbc | 10898 | contents = flinfo->contents; |
4a114e3e | 10899 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10900 | return FALSE; |
10901 | } | |
10902 | ||
10903 | if ((o->flags & SEC_RELOC) != 0) | |
10904 | { | |
10905 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10906 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10907 | int action_discarded; |
ece5ef60 | 10908 | int ret; |
c152c796 AM |
10909 | |
10910 | /* Get the swapped relocs. */ | |
10911 | internal_relocs | |
8b127cbc AM |
10912 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10913 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10914 | if (internal_relocs == NULL |
10915 | && o->reloc_count > 0) | |
10916 | return FALSE; | |
10917 | ||
310fd250 L |
10918 | /* We need to reverse-copy input .ctors/.dtors sections if |
10919 | they are placed in .init_array/.finit_array for output. */ | |
10920 | if (o->size > address_size | |
10921 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10922 | && strcmp (o->output_section->name, | |
10923 | ".init_array") == 0) | |
10924 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10925 | && strcmp (o->output_section->name, | |
10926 | ".fini_array") == 0)) | |
10927 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10928 | { |
056bafd4 MR |
10929 | if (o->size * bed->s->int_rels_per_ext_rel |
10930 | != o->reloc_count * address_size) | |
310fd250 | 10931 | { |
4eca0228 | 10932 | _bfd_error_handler |
695344c0 | 10933 | /* xgettext:c-format */ |
871b3ab2 | 10934 | (_("error: %pB: size of section %pA is not " |
310fd250 L |
10935 | "multiple of address size"), |
10936 | input_bfd, o); | |
8c6716e5 | 10937 | bfd_set_error (bfd_error_bad_value); |
310fd250 L |
10938 | return FALSE; |
10939 | } | |
10940 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10941 | } |
10942 | ||
0f02bbd9 | 10943 | action_discarded = -1; |
c152c796 | 10944 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10945 | action_discarded = (*bed->action_discarded) (o); |
10946 | ||
10947 | /* Run through the relocs evaluating complex reloc symbols and | |
10948 | looking for relocs against symbols from discarded sections | |
10949 | or section symbols from removed link-once sections. | |
10950 | Complain about relocs against discarded sections. Zero | |
10951 | relocs against removed link-once sections. */ | |
10952 | ||
10953 | rel = internal_relocs; | |
056bafd4 | 10954 | relend = rel + o->reloc_count; |
0f02bbd9 | 10955 | for ( ; rel < relend; rel++) |
c152c796 | 10956 | { |
0f02bbd9 AM |
10957 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10958 | unsigned int s_type; | |
10959 | asection **ps, *sec; | |
10960 | struct elf_link_hash_entry *h = NULL; | |
10961 | const char *sym_name; | |
c152c796 | 10962 | |
0f02bbd9 AM |
10963 | if (r_symndx == STN_UNDEF) |
10964 | continue; | |
c152c796 | 10965 | |
0f02bbd9 AM |
10966 | if (r_symndx >= locsymcount |
10967 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10968 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10969 | { |
10970 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10971 | |
0f02bbd9 AM |
10972 | /* Badly formatted input files can contain relocs that |
10973 | reference non-existant symbols. Check here so that | |
10974 | we do not seg fault. */ | |
10975 | if (h == NULL) | |
c152c796 | 10976 | { |
4eca0228 | 10977 | _bfd_error_handler |
695344c0 | 10978 | /* xgettext:c-format */ |
2dcf00ce | 10979 | (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA " |
0f02bbd9 | 10980 | "that references a non-existent global symbol"), |
2dcf00ce | 10981 | input_bfd, (uint64_t) rel->r_info, o); |
0f02bbd9 AM |
10982 | bfd_set_error (bfd_error_bad_value); |
10983 | return FALSE; | |
10984 | } | |
3b36f7e6 | 10985 | |
0f02bbd9 AM |
10986 | while (h->root.type == bfd_link_hash_indirect |
10987 | || h->root.type == bfd_link_hash_warning) | |
10988 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10989 | |
0f02bbd9 | 10990 | s_type = h->type; |
cdd3575c | 10991 | |
9e2dec47 | 10992 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10993 | mark the symbol as undefined. Note that the |
10994 | linker may attach linker created dynamic sections | |
10995 | to the plugin bfd. Symbols defined in linker | |
10996 | created sections are not plugin symbols. */ | |
bc4e12de | 10997 | if ((h->root.non_ir_ref_regular |
4070765b | 10998 | || h->root.non_ir_ref_dynamic) |
9e2dec47 L |
10999 | && (h->root.type == bfd_link_hash_defined |
11000 | || h->root.type == bfd_link_hash_defweak) | |
11001 | && (h->root.u.def.section->flags | |
11002 | & SEC_LINKER_CREATED) == 0 | |
11003 | && h->root.u.def.section->owner != NULL | |
11004 | && (h->root.u.def.section->owner->flags | |
11005 | & BFD_PLUGIN) != 0) | |
11006 | { | |
11007 | h->root.type = bfd_link_hash_undefined; | |
11008 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
11009 | } | |
11010 | ||
0f02bbd9 AM |
11011 | ps = NULL; |
11012 | if (h->root.type == bfd_link_hash_defined | |
11013 | || h->root.type == bfd_link_hash_defweak) | |
11014 | ps = &h->root.u.def.section; | |
11015 | ||
11016 | sym_name = h->root.root.string; | |
11017 | } | |
11018 | else | |
11019 | { | |
11020 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
11021 | ||
11022 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 11023 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
11024 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
11025 | sym, *ps); | |
11026 | } | |
c152c796 | 11027 | |
c301e700 | 11028 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 11029 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
11030 | { |
11031 | bfd_vma val; | |
11032 | bfd_vma dot = (rel->r_offset | |
11033 | + o->output_offset + o->output_section->vma); | |
11034 | #ifdef DEBUG | |
11035 | printf ("Encountered a complex symbol!"); | |
11036 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
765cf5f6 | 11037 | bfd_get_filename (input_bfd), o->name, |
9ccb8af9 | 11038 | (long) (rel - internal_relocs)); |
0f02bbd9 AM |
11039 | printf (" symbol: idx %8.8lx, name %s\n", |
11040 | r_symndx, sym_name); | |
11041 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
11042 | (unsigned long) rel->r_info, | |
11043 | (unsigned long) rel->r_offset); | |
11044 | #endif | |
8b127cbc | 11045 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
11046 | isymbuf, locsymcount, s_type == STT_SRELC)) |
11047 | return FALSE; | |
11048 | ||
11049 | /* Symbol evaluated OK. Update to absolute value. */ | |
11050 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
11051 | r_symndx, val); | |
11052 | continue; | |
11053 | } | |
11054 | ||
11055 | if (action_discarded != -1 && ps != NULL) | |
11056 | { | |
cdd3575c AM |
11057 | /* Complain if the definition comes from a |
11058 | discarded section. */ | |
dbaa2011 | 11059 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 11060 | { |
cf35638d | 11061 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 11062 | if (action_discarded & COMPLAIN) |
8b127cbc | 11063 | (*flinfo->info->callbacks->einfo) |
695344c0 | 11064 | /* xgettext:c-format */ |
871b3ab2 AM |
11065 | (_("%X`%s' referenced in section `%pA' of %pB: " |
11066 | "defined in discarded section `%pA' of %pB\n"), | |
e1fffbe6 | 11067 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 11068 | |
87e5235d | 11069 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 11070 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
11071 | really defined in the kept linkonce section. |
11072 | FIXME: This is quite broken. Modifying the | |
11073 | symbol here means we will be changing all later | |
e0ae6d6f | 11074 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 11075 | if (action_discarded & PRETEND) |
87e5235d | 11076 | { |
01b3c8ab L |
11077 | asection *kept; |
11078 | ||
c0f00686 | 11079 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 11080 | flinfo->info); |
01b3c8ab | 11081 | if (kept != NULL) |
87e5235d AM |
11082 | { |
11083 | *ps = kept; | |
11084 | continue; | |
11085 | } | |
11086 | } | |
c152c796 AM |
11087 | } |
11088 | } | |
11089 | } | |
11090 | ||
11091 | /* Relocate the section by invoking a back end routine. | |
11092 | ||
11093 | The back end routine is responsible for adjusting the | |
11094 | section contents as necessary, and (if using Rela relocs | |
11095 | and generating a relocatable output file) adjusting the | |
11096 | reloc addend as necessary. | |
11097 | ||
11098 | The back end routine does not have to worry about setting | |
11099 | the reloc address or the reloc symbol index. | |
11100 | ||
11101 | The back end routine is given a pointer to the swapped in | |
11102 | internal symbols, and can access the hash table entries | |
11103 | for the external symbols via elf_sym_hashes (input_bfd). | |
11104 | ||
11105 | When generating relocatable output, the back end routine | |
11106 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
11107 | output symbol is going to be a section symbol | |
11108 | corresponding to the output section, which will require | |
11109 | the addend to be adjusted. */ | |
11110 | ||
8b127cbc | 11111 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
11112 | input_bfd, o, contents, |
11113 | internal_relocs, | |
11114 | isymbuf, | |
8b127cbc | 11115 | flinfo->sections); |
ece5ef60 | 11116 | if (!ret) |
c152c796 AM |
11117 | return FALSE; |
11118 | ||
ece5ef60 | 11119 | if (ret == 2 |
0e1862bb | 11120 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 11121 | || flinfo->info->emitrelocations) |
c152c796 AM |
11122 | { |
11123 | Elf_Internal_Rela *irela; | |
d4730f92 | 11124 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
11125 | bfd_vma last_offset; |
11126 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
11127 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
11128 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 11129 | unsigned int next_erel; |
c152c796 | 11130 | bfd_boolean rela_normal; |
d4730f92 | 11131 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 11132 | |
d4730f92 BS |
11133 | esdi = elf_section_data (o); |
11134 | esdo = elf_section_data (o->output_section); | |
11135 | rela_normal = FALSE; | |
c152c796 AM |
11136 | |
11137 | /* Adjust the reloc addresses and symbol indices. */ | |
11138 | ||
11139 | irela = internal_relocs; | |
056bafd4 | 11140 | irelaend = irela + o->reloc_count; |
d4730f92 BS |
11141 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
11142 | /* We start processing the REL relocs, if any. When we reach | |
11143 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
11144 | irelamid = irela; | |
11145 | if (esdi->rel.hdr != NULL) | |
11146 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
11147 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 11148 | rel_hash_list = rel_hash; |
d4730f92 | 11149 | rela_hash_list = NULL; |
c152c796 | 11150 | last_offset = o->output_offset; |
0e1862bb | 11151 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11152 | last_offset += o->output_section->vma; |
11153 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
11154 | { | |
11155 | unsigned long r_symndx; | |
11156 | asection *sec; | |
11157 | Elf_Internal_Sym sym; | |
11158 | ||
11159 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
11160 | { | |
11161 | rel_hash++; | |
11162 | next_erel = 0; | |
11163 | } | |
11164 | ||
d4730f92 BS |
11165 | if (irela == irelamid) |
11166 | { | |
11167 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
11168 | rela_hash_list = rel_hash; | |
11169 | rela_normal = bed->rela_normal; | |
11170 | } | |
11171 | ||
c152c796 | 11172 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 11173 | flinfo->info, o, |
c152c796 AM |
11174 | irela->r_offset); |
11175 | if (irela->r_offset >= (bfd_vma) -2) | |
11176 | { | |
11177 | /* This is a reloc for a deleted entry or somesuch. | |
11178 | Turn it into an R_*_NONE reloc, at the same | |
11179 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 11180 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
11181 | being ordered. */ |
11182 | irela->r_offset = last_offset; | |
11183 | irela->r_info = 0; | |
11184 | irela->r_addend = 0; | |
11185 | continue; | |
11186 | } | |
11187 | ||
11188 | irela->r_offset += o->output_offset; | |
11189 | ||
11190 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 11191 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11192 | irela->r_offset += o->output_section->vma; |
11193 | ||
11194 | last_offset = irela->r_offset; | |
11195 | ||
11196 | r_symndx = irela->r_info >> r_sym_shift; | |
11197 | if (r_symndx == STN_UNDEF) | |
11198 | continue; | |
11199 | ||
11200 | if (r_symndx >= locsymcount | |
11201 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 11202 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
11203 | { |
11204 | struct elf_link_hash_entry *rh; | |
11205 | unsigned long indx; | |
11206 | ||
11207 | /* This is a reloc against a global symbol. We | |
11208 | have not yet output all the local symbols, so | |
11209 | we do not know the symbol index of any global | |
11210 | symbol. We set the rel_hash entry for this | |
11211 | reloc to point to the global hash table entry | |
11212 | for this symbol. The symbol index is then | |
ee75fd95 | 11213 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
11214 | indx = r_symndx - extsymoff; |
11215 | rh = elf_sym_hashes (input_bfd)[indx]; | |
11216 | while (rh->root.type == bfd_link_hash_indirect | |
11217 | || rh->root.type == bfd_link_hash_warning) | |
11218 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
11219 | ||
11220 | /* Setting the index to -2 tells | |
11221 | elf_link_output_extsym that this symbol is | |
11222 | used by a reloc. */ | |
11223 | BFD_ASSERT (rh->indx < 0); | |
11224 | rh->indx = -2; | |
c152c796 AM |
11225 | *rel_hash = rh; |
11226 | ||
11227 | continue; | |
11228 | } | |
11229 | ||
11230 | /* This is a reloc against a local symbol. */ | |
11231 | ||
11232 | *rel_hash = NULL; | |
11233 | sym = isymbuf[r_symndx]; | |
8b127cbc | 11234 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
11235 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
11236 | { | |
11237 | /* I suppose the backend ought to fill in the | |
11238 | section of any STT_SECTION symbol against a | |
6a8d1586 | 11239 | processor specific section. */ |
cf35638d | 11240 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
11241 | if (bfd_is_abs_section (sec)) |
11242 | ; | |
c152c796 AM |
11243 | else if (sec == NULL || sec->owner == NULL) |
11244 | { | |
11245 | bfd_set_error (bfd_error_bad_value); | |
11246 | return FALSE; | |
11247 | } | |
11248 | else | |
11249 | { | |
6a8d1586 AM |
11250 | asection *osec = sec->output_section; |
11251 | ||
11252 | /* If we have discarded a section, the output | |
11253 | section will be the absolute section. In | |
ab96bf03 AM |
11254 | case of discarded SEC_MERGE sections, use |
11255 | the kept section. relocate_section should | |
11256 | have already handled discarded linkonce | |
11257 | sections. */ | |
6a8d1586 AM |
11258 | if (bfd_is_abs_section (osec) |
11259 | && sec->kept_section != NULL | |
11260 | && sec->kept_section->output_section != NULL) | |
11261 | { | |
11262 | osec = sec->kept_section->output_section; | |
11263 | irela->r_addend -= osec->vma; | |
11264 | } | |
11265 | ||
11266 | if (!bfd_is_abs_section (osec)) | |
11267 | { | |
11268 | r_symndx = osec->target_index; | |
cf35638d | 11269 | if (r_symndx == STN_UNDEF) |
74541ad4 | 11270 | { |
051d833a AM |
11271 | irela->r_addend += osec->vma; |
11272 | osec = _bfd_nearby_section (output_bfd, osec, | |
11273 | osec->vma); | |
11274 | irela->r_addend -= osec->vma; | |
11275 | r_symndx = osec->target_index; | |
74541ad4 | 11276 | } |
6a8d1586 | 11277 | } |
c152c796 AM |
11278 | } |
11279 | ||
11280 | /* Adjust the addend according to where the | |
11281 | section winds up in the output section. */ | |
11282 | if (rela_normal) | |
11283 | irela->r_addend += sec->output_offset; | |
11284 | } | |
11285 | else | |
11286 | { | |
8b127cbc | 11287 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
11288 | { |
11289 | unsigned long shlink; | |
11290 | const char *name; | |
11291 | asection *osec; | |
6e0b88f1 | 11292 | long indx; |
c152c796 | 11293 | |
8b127cbc | 11294 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
11295 | { |
11296 | /* You can't do ld -r -s. */ | |
11297 | bfd_set_error (bfd_error_invalid_operation); | |
11298 | return FALSE; | |
11299 | } | |
11300 | ||
11301 | /* This symbol was skipped earlier, but | |
11302 | since it is needed by a reloc, we | |
11303 | must output it now. */ | |
11304 | shlink = symtab_hdr->sh_link; | |
11305 | name = (bfd_elf_string_from_elf_section | |
11306 | (input_bfd, shlink, sym.st_name)); | |
11307 | if (name == NULL) | |
11308 | return FALSE; | |
11309 | ||
11310 | osec = sec->output_section; | |
11311 | sym.st_shndx = | |
11312 | _bfd_elf_section_from_bfd_section (output_bfd, | |
11313 | osec); | |
11314 | if (sym.st_shndx == SHN_BAD) | |
11315 | return FALSE; | |
11316 | ||
11317 | sym.st_value += sec->output_offset; | |
0e1862bb | 11318 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11319 | { |
11320 | sym.st_value += osec->vma; | |
11321 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
11322 | { | |
102def4d AM |
11323 | struct elf_link_hash_table *htab |
11324 | = elf_hash_table (flinfo->info); | |
11325 | ||
c152c796 AM |
11326 | /* STT_TLS symbols are relative to PT_TLS |
11327 | segment base. */ | |
102def4d AM |
11328 | if (htab->tls_sec != NULL) |
11329 | sym.st_value -= htab->tls_sec->vma; | |
11330 | else | |
11331 | sym.st_info | |
11332 | = ELF_ST_INFO (ELF_ST_BIND (sym.st_info), | |
11333 | STT_NOTYPE); | |
c152c796 AM |
11334 | } |
11335 | } | |
11336 | ||
6e0b88f1 | 11337 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
11338 | ret = elf_link_output_symstrtab (flinfo, name, |
11339 | &sym, sec, | |
11340 | NULL); | |
6e0b88f1 | 11341 | if (ret == 0) |
c152c796 | 11342 | return FALSE; |
6e0b88f1 | 11343 | else if (ret == 1) |
8b127cbc | 11344 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
11345 | else |
11346 | abort (); | |
c152c796 AM |
11347 | } |
11348 | ||
8b127cbc | 11349 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
11350 | } |
11351 | ||
11352 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
11353 | | (irela->r_info & r_type_mask)); | |
11354 | } | |
11355 | ||
11356 | /* Swap out the relocs. */ | |
d4730f92 BS |
11357 | input_rel_hdr = esdi->rel.hdr; |
11358 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 11359 | { |
d4730f92 BS |
11360 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
11361 | input_rel_hdr, | |
11362 | internal_relocs, | |
11363 | rel_hash_list)) | |
11364 | return FALSE; | |
c152c796 AM |
11365 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
11366 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 11367 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
11368 | } |
11369 | ||
11370 | input_rela_hdr = esdi->rela.hdr; | |
11371 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
11372 | { | |
eac338cf | 11373 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 11374 | input_rela_hdr, |
eac338cf | 11375 | internal_relocs, |
d4730f92 | 11376 | rela_hash_list)) |
c152c796 AM |
11377 | return FALSE; |
11378 | } | |
11379 | } | |
11380 | } | |
11381 | ||
11382 | /* Write out the modified section contents. */ | |
11383 | if (bed->elf_backend_write_section | |
8b127cbc | 11384 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 11385 | contents)) |
c152c796 AM |
11386 | { |
11387 | /* Section written out. */ | |
11388 | } | |
11389 | else switch (o->sec_info_type) | |
11390 | { | |
dbaa2011 | 11391 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
11392 | if (! (_bfd_write_section_stabs |
11393 | (output_bfd, | |
8b127cbc | 11394 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
11395 | o, &elf_section_data (o)->sec_info, contents))) |
11396 | return FALSE; | |
11397 | break; | |
dbaa2011 | 11398 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
11399 | if (! _bfd_write_merged_section (output_bfd, o, |
11400 | elf_section_data (o)->sec_info)) | |
11401 | return FALSE; | |
11402 | break; | |
dbaa2011 | 11403 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 11404 | { |
8b127cbc | 11405 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
11406 | o, contents)) |
11407 | return FALSE; | |
11408 | } | |
11409 | break; | |
2f0c68f2 CM |
11410 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
11411 | { | |
11412 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
11413 | flinfo->info, | |
11414 | o, contents)) | |
11415 | return FALSE; | |
11416 | } | |
11417 | break; | |
c152c796 AM |
11418 | default: |
11419 | { | |
310fd250 L |
11420 | if (! (o->flags & SEC_EXCLUDE)) |
11421 | { | |
11422 | file_ptr offset = (file_ptr) o->output_offset; | |
11423 | bfd_size_type todo = o->size; | |
37b01f6a | 11424 | |
bb294208 | 11425 | offset *= bfd_octets_per_byte (output_bfd, o); |
37b01f6a | 11426 | |
310fd250 L |
11427 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
11428 | { | |
11429 | /* Reverse-copy input section to output. */ | |
11430 | do | |
11431 | { | |
11432 | todo -= address_size; | |
11433 | if (! bfd_set_section_contents (output_bfd, | |
11434 | o->output_section, | |
11435 | contents + todo, | |
11436 | offset, | |
11437 | address_size)) | |
11438 | return FALSE; | |
11439 | if (todo == 0) | |
11440 | break; | |
11441 | offset += address_size; | |
11442 | } | |
11443 | while (1); | |
11444 | } | |
11445 | else if (! bfd_set_section_contents (output_bfd, | |
11446 | o->output_section, | |
11447 | contents, | |
11448 | offset, todo)) | |
11449 | return FALSE; | |
11450 | } | |
c152c796 AM |
11451 | } |
11452 | break; | |
11453 | } | |
11454 | } | |
11455 | ||
11456 | return TRUE; | |
11457 | } | |
11458 | ||
11459 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 11460 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
11461 | is used to build constructor and destructor tables when linking |
11462 | with -Ur. */ | |
11463 | ||
11464 | static bfd_boolean | |
11465 | elf_reloc_link_order (bfd *output_bfd, | |
11466 | struct bfd_link_info *info, | |
11467 | asection *output_section, | |
11468 | struct bfd_link_order *link_order) | |
11469 | { | |
11470 | reloc_howto_type *howto; | |
11471 | long indx; | |
11472 | bfd_vma offset; | |
11473 | bfd_vma addend; | |
d4730f92 | 11474 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
11475 | struct elf_link_hash_entry **rel_hash_ptr; |
11476 | Elf_Internal_Shdr *rel_hdr; | |
11477 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
11478 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
11479 | bfd_byte *erel; | |
11480 | unsigned int i; | |
d4730f92 | 11481 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
11482 | |
11483 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
11484 | if (howto == NULL) | |
11485 | { | |
11486 | bfd_set_error (bfd_error_bad_value); | |
11487 | return FALSE; | |
11488 | } | |
11489 | ||
11490 | addend = link_order->u.reloc.p->addend; | |
11491 | ||
d4730f92 BS |
11492 | if (esdo->rel.hdr) |
11493 | reldata = &esdo->rel; | |
11494 | else if (esdo->rela.hdr) | |
11495 | reldata = &esdo->rela; | |
11496 | else | |
11497 | { | |
11498 | reldata = NULL; | |
11499 | BFD_ASSERT (0); | |
11500 | } | |
11501 | ||
c152c796 | 11502 | /* Figure out the symbol index. */ |
d4730f92 | 11503 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
11504 | if (link_order->type == bfd_section_reloc_link_order) |
11505 | { | |
11506 | indx = link_order->u.reloc.p->u.section->target_index; | |
11507 | BFD_ASSERT (indx != 0); | |
11508 | *rel_hash_ptr = NULL; | |
11509 | } | |
11510 | else | |
11511 | { | |
11512 | struct elf_link_hash_entry *h; | |
11513 | ||
11514 | /* Treat a reloc against a defined symbol as though it were | |
11515 | actually against the section. */ | |
11516 | h = ((struct elf_link_hash_entry *) | |
11517 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
11518 | link_order->u.reloc.p->u.name, | |
11519 | FALSE, FALSE, TRUE)); | |
11520 | if (h != NULL | |
11521 | && (h->root.type == bfd_link_hash_defined | |
11522 | || h->root.type == bfd_link_hash_defweak)) | |
11523 | { | |
11524 | asection *section; | |
11525 | ||
11526 | section = h->root.u.def.section; | |
11527 | indx = section->output_section->target_index; | |
11528 | *rel_hash_ptr = NULL; | |
11529 | /* It seems that we ought to add the symbol value to the | |
11530 | addend here, but in practice it has already been added | |
11531 | because it was passed to constructor_callback. */ | |
11532 | addend += section->output_section->vma + section->output_offset; | |
11533 | } | |
11534 | else if (h != NULL) | |
11535 | { | |
11536 | /* Setting the index to -2 tells elf_link_output_extsym that | |
11537 | this symbol is used by a reloc. */ | |
11538 | h->indx = -2; | |
11539 | *rel_hash_ptr = h; | |
11540 | indx = 0; | |
11541 | } | |
11542 | else | |
11543 | { | |
1a72702b AM |
11544 | (*info->callbacks->unattached_reloc) |
11545 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
11546 | indx = 0; |
11547 | } | |
11548 | } | |
11549 | ||
11550 | /* If this is an inplace reloc, we must write the addend into the | |
11551 | object file. */ | |
11552 | if (howto->partial_inplace && addend != 0) | |
11553 | { | |
11554 | bfd_size_type size; | |
11555 | bfd_reloc_status_type rstat; | |
11556 | bfd_byte *buf; | |
11557 | bfd_boolean ok; | |
11558 | const char *sym_name; | |
bb294208 | 11559 | bfd_size_type octets; |
c152c796 | 11560 | |
a50b1753 NC |
11561 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
11562 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 11563 | if (buf == NULL && size != 0) |
c152c796 AM |
11564 | return FALSE; |
11565 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
11566 | switch (rstat) | |
11567 | { | |
11568 | case bfd_reloc_ok: | |
11569 | break; | |
11570 | ||
11571 | default: | |
11572 | case bfd_reloc_outofrange: | |
11573 | abort (); | |
11574 | ||
11575 | case bfd_reloc_overflow: | |
11576 | if (link_order->type == bfd_section_reloc_link_order) | |
fd361982 | 11577 | sym_name = bfd_section_name (link_order->u.reloc.p->u.section); |
c152c796 AM |
11578 | else |
11579 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
11580 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
11581 | howto->name, addend, NULL, NULL, | |
11582 | (bfd_vma) 0); | |
c152c796 AM |
11583 | break; |
11584 | } | |
37b01f6a | 11585 | |
bb294208 AM |
11586 | octets = link_order->offset * bfd_octets_per_byte (output_bfd, |
11587 | output_section); | |
c152c796 | 11588 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
bb294208 | 11589 | octets, size); |
c152c796 AM |
11590 | free (buf); |
11591 | if (! ok) | |
11592 | return FALSE; | |
11593 | } | |
11594 | ||
11595 | /* The address of a reloc is relative to the section in a | |
11596 | relocatable file, and is a virtual address in an executable | |
11597 | file. */ | |
11598 | offset = link_order->offset; | |
0e1862bb | 11599 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
11600 | offset += output_section->vma; |
11601 | ||
11602 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
11603 | { | |
11604 | irel[i].r_offset = offset; | |
11605 | irel[i].r_info = 0; | |
11606 | irel[i].r_addend = 0; | |
11607 | } | |
11608 | if (bed->s->arch_size == 32) | |
11609 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
11610 | else | |
11611 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
11612 | ||
d4730f92 | 11613 | rel_hdr = reldata->hdr; |
c152c796 AM |
11614 | erel = rel_hdr->contents; |
11615 | if (rel_hdr->sh_type == SHT_REL) | |
11616 | { | |
d4730f92 | 11617 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
11618 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
11619 | } | |
11620 | else | |
11621 | { | |
11622 | irel[0].r_addend = addend; | |
d4730f92 | 11623 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
11624 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
11625 | } | |
11626 | ||
d4730f92 | 11627 | ++reldata->count; |
c152c796 AM |
11628 | |
11629 | return TRUE; | |
11630 | } | |
11631 | ||
0b52efa6 | 11632 | |
0b52efa6 PB |
11633 | /* Compare two sections based on the locations of the sections they are |
11634 | linked to. Used by elf_fixup_link_order. */ | |
11635 | ||
11636 | static int | |
8c1c5e5d | 11637 | compare_link_order (const void *a, const void *b) |
0b52efa6 | 11638 | { |
8c1c5e5d AM |
11639 | const struct bfd_link_order *alo = *(const struct bfd_link_order **) a; |
11640 | const struct bfd_link_order *blo = *(const struct bfd_link_order **) b; | |
11641 | asection *asec = elf_linked_to_section (alo->u.indirect.section); | |
11642 | asection *bsec = elf_linked_to_section (blo->u.indirect.section); | |
11643 | bfd_vma apos = asec->output_section->lma + asec->output_offset; | |
11644 | bfd_vma bpos = bsec->output_section->lma + bsec->output_offset; | |
0b52efa6 | 11645 | |
0b52efa6 PB |
11646 | if (apos < bpos) |
11647 | return -1; | |
8c1c5e5d AM |
11648 | if (apos > bpos) |
11649 | return 1; | |
11650 | ||
11651 | /* The only way we should get matching LMAs is when the first of two | |
11652 | sections has zero size. */ | |
11653 | if (asec->size < bsec->size) | |
11654 | return -1; | |
11655 | if (asec->size > bsec->size) | |
11656 | return 1; | |
11657 | ||
11658 | /* If they are both zero size then they almost certainly have the same | |
11659 | VMA and thus are not ordered with respect to each other. Test VMA | |
11660 | anyway, and fall back to id to make the result reproducible across | |
11661 | qsort implementations. */ | |
11662 | apos = asec->output_section->vma + asec->output_offset; | |
11663 | bpos = bsec->output_section->vma + bsec->output_offset; | |
11664 | if (apos < bpos) | |
11665 | return -1; | |
11666 | if (apos > bpos) | |
11667 | return 1; | |
11668 | ||
11669 | return asec->id - bsec->id; | |
0b52efa6 PB |
11670 | } |
11671 | ||
11672 | ||
11673 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
11674 | order as their linked sections. Returns false if this could not be done | |
11675 | because an output section includes both ordered and unordered | |
11676 | sections. Ideally we'd do this in the linker proper. */ | |
11677 | ||
11678 | static bfd_boolean | |
11679 | elf_fixup_link_order (bfd *abfd, asection *o) | |
11680 | { | |
8c1c5e5d AM |
11681 | size_t seen_linkorder; |
11682 | size_t seen_other; | |
11683 | size_t n; | |
0b52efa6 PB |
11684 | struct bfd_link_order *p; |
11685 | bfd *sub; | |
0b52efa6 | 11686 | struct bfd_link_order **sections; |
66631823 CE |
11687 | asection *other_sec, *linkorder_sec; |
11688 | bfd_vma offset; /* Octets. */ | |
3b36f7e6 | 11689 | |
d33cdfe3 L |
11690 | other_sec = NULL; |
11691 | linkorder_sec = NULL; | |
0b52efa6 PB |
11692 | seen_other = 0; |
11693 | seen_linkorder = 0; | |
8423293d | 11694 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 11695 | { |
d33cdfe3 | 11696 | if (p->type == bfd_indirect_link_order) |
0b52efa6 | 11697 | { |
66631823 | 11698 | asection *s = p->u.indirect.section; |
d33cdfe3 | 11699 | sub = s->owner; |
847d5183 AM |
11700 | if ((s->flags & SEC_LINKER_CREATED) == 0 |
11701 | && bfd_get_flavour (sub) == bfd_target_elf_flavour | |
8c1c5e5d AM |
11702 | && elf_section_data (s) != NULL |
11703 | && elf_linked_to_section (s) != NULL) | |
d33cdfe3 L |
11704 | { |
11705 | seen_linkorder++; | |
11706 | linkorder_sec = s; | |
11707 | } | |
0b52efa6 | 11708 | else |
d33cdfe3 L |
11709 | { |
11710 | seen_other++; | |
11711 | other_sec = s; | |
11712 | } | |
0b52efa6 PB |
11713 | } |
11714 | else | |
11715 | seen_other++; | |
d33cdfe3 L |
11716 | |
11717 | if (seen_other && seen_linkorder) | |
11718 | { | |
11719 | if (other_sec && linkorder_sec) | |
4eca0228 | 11720 | _bfd_error_handler |
695344c0 | 11721 | /* xgettext:c-format */ |
871b3ab2 AM |
11722 | (_("%pA has both ordered [`%pA' in %pB] " |
11723 | "and unordered [`%pA' in %pB] sections"), | |
63a5468a AM |
11724 | o, linkorder_sec, linkorder_sec->owner, |
11725 | other_sec, other_sec->owner); | |
d33cdfe3 | 11726 | else |
4eca0228 | 11727 | _bfd_error_handler |
871b3ab2 | 11728 | (_("%pA has both ordered and unordered sections"), o); |
d33cdfe3 L |
11729 | bfd_set_error (bfd_error_bad_value); |
11730 | return FALSE; | |
11731 | } | |
0b52efa6 PB |
11732 | } |
11733 | ||
11734 | if (!seen_linkorder) | |
11735 | return TRUE; | |
11736 | ||
8c1c5e5d | 11737 | sections = bfd_malloc (seen_linkorder * sizeof (*sections)); |
14b1c01e AM |
11738 | if (sections == NULL) |
11739 | return FALSE; | |
3b36f7e6 | 11740 | |
8c1c5e5d | 11741 | seen_linkorder = 0; |
8423293d | 11742 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
8c1c5e5d AM |
11743 | sections[seen_linkorder++] = p; |
11744 | ||
0b52efa6 | 11745 | /* Sort the input sections in the order of their linked section. */ |
8c1c5e5d | 11746 | qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order); |
0b52efa6 PB |
11747 | |
11748 | /* Change the offsets of the sections. */ | |
11749 | offset = 0; | |
11750 | for (n = 0; n < seen_linkorder; n++) | |
11751 | { | |
8c1c5e5d | 11752 | bfd_vma mask; |
66631823 CE |
11753 | asection *s = sections[n]->u.indirect.section; |
11754 | unsigned int opb = bfd_octets_per_byte (abfd, s); | |
11755 | ||
11756 | mask = ~(bfd_vma) 0 << s->alignment_power * opb; | |
8c1c5e5d | 11757 | offset = (offset + ~mask) & mask; |
66631823 | 11758 | sections[n]->offset = s->output_offset = offset / opb; |
0b52efa6 PB |
11759 | offset += sections[n]->size; |
11760 | } | |
11761 | ||
4dd07732 | 11762 | free (sections); |
0b52efa6 PB |
11763 | return TRUE; |
11764 | } | |
11765 | ||
76359541 TP |
11766 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11767 | Returns TRUE upon success, FALSE otherwise. */ | |
11768 | ||
11769 | static bfd_boolean | |
11770 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11771 | { | |
11772 | bfd_boolean ret = FALSE; | |
11773 | bfd *implib_bfd; | |
11774 | const struct elf_backend_data *bed; | |
11775 | flagword flags; | |
11776 | enum bfd_architecture arch; | |
11777 | unsigned int mach; | |
11778 | asymbol **sympp = NULL; | |
11779 | long symsize; | |
11780 | long symcount; | |
11781 | long src_count; | |
11782 | elf_symbol_type *osymbuf; | |
446f7ed5 | 11783 | size_t amt; |
76359541 TP |
11784 | |
11785 | implib_bfd = info->out_implib_bfd; | |
11786 | bed = get_elf_backend_data (abfd); | |
11787 | ||
11788 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11789 | return FALSE; | |
11790 | ||
046734ff | 11791 | /* Use flag from executable but make it a relocatable object. */ |
76359541 TP |
11792 | flags = bfd_get_file_flags (abfd); |
11793 | flags &= ~HAS_RELOC; | |
11794 | if (!bfd_set_start_address (implib_bfd, 0) | |
046734ff | 11795 | || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) |
76359541 TP |
11796 | return FALSE; |
11797 | ||
11798 | /* Copy architecture of output file to import library file. */ | |
11799 | arch = bfd_get_arch (abfd); | |
11800 | mach = bfd_get_mach (abfd); | |
11801 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11802 | && (abfd->target_defaulted | |
11803 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11804 | return FALSE; | |
11805 | ||
11806 | /* Get symbol table size. */ | |
11807 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11808 | if (symsize < 0) | |
11809 | return FALSE; | |
11810 | ||
11811 | /* Read in the symbol table. */ | |
ec9bd0a2 AM |
11812 | sympp = (asymbol **) bfd_malloc (symsize); |
11813 | if (sympp == NULL) | |
11814 | return FALSE; | |
11815 | ||
76359541 TP |
11816 | symcount = bfd_canonicalize_symtab (abfd, sympp); |
11817 | if (symcount < 0) | |
11818 | goto free_sym_buf; | |
11819 | ||
11820 | /* Allow the BFD backend to copy any private header data it | |
11821 | understands from the output BFD to the import library BFD. */ | |
11822 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11823 | goto free_sym_buf; | |
11824 | ||
11825 | /* Filter symbols to appear in the import library. */ | |
11826 | if (bed->elf_backend_filter_implib_symbols) | |
11827 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11828 | symcount); | |
11829 | else | |
11830 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11831 | if (symcount == 0) | |
11832 | { | |
5df1bc57 | 11833 | bfd_set_error (bfd_error_no_symbols); |
871b3ab2 | 11834 | _bfd_error_handler (_("%pB: no symbol found for import library"), |
4eca0228 | 11835 | implib_bfd); |
76359541 TP |
11836 | goto free_sym_buf; |
11837 | } | |
11838 | ||
11839 | ||
11840 | /* Make symbols absolute. */ | |
446f7ed5 AM |
11841 | amt = symcount * sizeof (*osymbuf); |
11842 | osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt); | |
ec9bd0a2 AM |
11843 | if (osymbuf == NULL) |
11844 | goto free_sym_buf; | |
11845 | ||
76359541 TP |
11846 | for (src_count = 0; src_count < symcount; src_count++) |
11847 | { | |
11848 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11849 | sizeof (*osymbuf)); | |
11850 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11851 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11852 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11853 | osymbuf[src_count].internal_elf_sym.st_value = | |
11854 | osymbuf[src_count].symbol.value; | |
11855 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11856 | } | |
11857 | ||
11858 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11859 | ||
11860 | /* Allow the BFD backend to copy any private data it understands | |
11861 | from the output BFD to the import library BFD. This is done last | |
11862 | to permit the routine to look at the filtered symbol table. */ | |
11863 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11864 | goto free_sym_buf; | |
11865 | ||
11866 | if (!bfd_close (implib_bfd)) | |
11867 | goto free_sym_buf; | |
11868 | ||
11869 | ret = TRUE; | |
11870 | ||
dc1e8a47 | 11871 | free_sym_buf: |
76359541 TP |
11872 | free (sympp); |
11873 | return ret; | |
11874 | } | |
11875 | ||
9f7c3e5e AM |
11876 | static void |
11877 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11878 | { | |
11879 | asection *o; | |
11880 | ||
11881 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11882 | _bfd_elf_strtab_free (flinfo->symstrtab); |
c9594989 AM |
11883 | free (flinfo->contents); |
11884 | free (flinfo->external_relocs); | |
11885 | free (flinfo->internal_relocs); | |
11886 | free (flinfo->external_syms); | |
11887 | free (flinfo->locsym_shndx); | |
11888 | free (flinfo->internal_syms); | |
11889 | free (flinfo->indices); | |
11890 | free (flinfo->sections); | |
11891 | if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1) | |
9f7c3e5e AM |
11892 | free (flinfo->symshndxbuf); |
11893 | for (o = obfd->sections; o != NULL; o = o->next) | |
11894 | { | |
11895 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
c9594989 AM |
11896 | free (esdo->rel.hashes); |
11897 | free (esdo->rela.hashes); | |
9f7c3e5e AM |
11898 | } |
11899 | } | |
0b52efa6 | 11900 | |
c152c796 AM |
11901 | /* Do the final step of an ELF link. */ |
11902 | ||
11903 | bfd_boolean | |
11904 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11905 | { | |
11906 | bfd_boolean dynamic; | |
11907 | bfd_boolean emit_relocs; | |
11908 | bfd *dynobj; | |
8b127cbc | 11909 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11910 | asection *o; |
11911 | struct bfd_link_order *p; | |
11912 | bfd *sub; | |
c152c796 AM |
11913 | bfd_size_type max_contents_size; |
11914 | bfd_size_type max_external_reloc_size; | |
11915 | bfd_size_type max_internal_reloc_count; | |
11916 | bfd_size_type max_sym_count; | |
11917 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11918 | Elf_Internal_Sym elfsym; |
11919 | unsigned int i; | |
11920 | Elf_Internal_Shdr *symtab_hdr; | |
11921 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11922 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11923 | struct elf_outext_info eoinfo; | |
11924 | bfd_boolean merged; | |
11925 | size_t relativecount = 0; | |
11926 | asection *reldyn = 0; | |
11927 | bfd_size_type amt; | |
104d59d1 JM |
11928 | asection *attr_section = NULL; |
11929 | bfd_vma attr_size = 0; | |
11930 | const char *std_attrs_section; | |
64f52338 | 11931 | struct elf_link_hash_table *htab = elf_hash_table (info); |
4c6ee646 | 11932 | bfd_boolean sections_removed; |
c152c796 | 11933 | |
64f52338 | 11934 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11935 | return FALSE; |
11936 | ||
0e1862bb | 11937 | if (bfd_link_pic (info)) |
c152c796 AM |
11938 | abfd->flags |= DYNAMIC; |
11939 | ||
64f52338 AM |
11940 | dynamic = htab->dynamic_sections_created; |
11941 | dynobj = htab->dynobj; | |
c152c796 | 11942 | |
0e1862bb | 11943 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11944 | || info->emitrelocations); |
c152c796 | 11945 | |
8b127cbc AM |
11946 | flinfo.info = info; |
11947 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11948 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11949 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11950 | return FALSE; |
11951 | ||
11952 | if (! dynamic) | |
11953 | { | |
8b127cbc AM |
11954 | flinfo.hash_sec = NULL; |
11955 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11956 | } |
11957 | else | |
11958 | { | |
3d4d4302 | 11959 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11960 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11961 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11962 | /* Note that it is OK if symver_sec is NULL. */ |
11963 | } | |
11964 | ||
8b127cbc AM |
11965 | flinfo.contents = NULL; |
11966 | flinfo.external_relocs = NULL; | |
11967 | flinfo.internal_relocs = NULL; | |
11968 | flinfo.external_syms = NULL; | |
11969 | flinfo.locsym_shndx = NULL; | |
11970 | flinfo.internal_syms = NULL; | |
11971 | flinfo.indices = NULL; | |
11972 | flinfo.sections = NULL; | |
8b127cbc | 11973 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11974 | flinfo.filesym_count = 0; |
c152c796 | 11975 | |
104d59d1 JM |
11976 | /* The object attributes have been merged. Remove the input |
11977 | sections from the link, and set the contents of the output | |
1ff6de03 | 11978 | section. */ |
4c6ee646 | 11979 | sections_removed = FALSE; |
104d59d1 JM |
11980 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; |
11981 | for (o = abfd->sections; o != NULL; o = o->next) | |
11982 | { | |
5270eddc | 11983 | bfd_boolean remove_section = FALSE; |
b8a6ced7 | 11984 | |
104d59d1 JM |
11985 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) |
11986 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11987 | { | |
11988 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11989 | { | |
11990 | asection *input_section; | |
11991 | ||
11992 | if (p->type != bfd_indirect_link_order) | |
11993 | continue; | |
11994 | input_section = p->u.indirect.section; | |
11995 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11996 | elf_link_input_bfd ignores this section. */ | |
11997 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11998 | } | |
a0c8462f | 11999 | |
104d59d1 | 12000 | attr_size = bfd_elf_obj_attr_size (abfd); |
fd361982 | 12001 | bfd_set_section_size (o, attr_size); |
b8a6ced7 AM |
12002 | /* Skip this section later on. */ |
12003 | o->map_head.link_order = NULL; | |
104d59d1 | 12004 | if (attr_size) |
b8a6ced7 | 12005 | attr_section = o; |
104d59d1 | 12006 | else |
5270eddc | 12007 | remove_section = TRUE; |
104d59d1 | 12008 | } |
6e5e9d58 AM |
12009 | else if ((o->flags & SEC_GROUP) != 0 && o->size == 0) |
12010 | { | |
12011 | /* Remove empty group section from linker output. */ | |
5270eddc | 12012 | remove_section = TRUE; |
b8a6ced7 | 12013 | } |
5270eddc | 12014 | if (remove_section) |
b8a6ced7 | 12015 | { |
6e5e9d58 AM |
12016 | o->flags |= SEC_EXCLUDE; |
12017 | bfd_section_list_remove (abfd, o); | |
12018 | abfd->section_count--; | |
4c6ee646 | 12019 | sections_removed = TRUE; |
6e5e9d58 | 12020 | } |
104d59d1 | 12021 | } |
4c6ee646 AM |
12022 | if (sections_removed) |
12023 | _bfd_fix_excluded_sec_syms (abfd, info); | |
104d59d1 | 12024 | |
c152c796 AM |
12025 | /* Count up the number of relocations we will output for each output |
12026 | section, so that we know the sizes of the reloc sections. We | |
12027 | also figure out some maximum sizes. */ | |
12028 | max_contents_size = 0; | |
12029 | max_external_reloc_size = 0; | |
12030 | max_internal_reloc_count = 0; | |
12031 | max_sym_count = 0; | |
12032 | max_sym_shndx_count = 0; | |
12033 | merged = FALSE; | |
12034 | for (o = abfd->sections; o != NULL; o = o->next) | |
12035 | { | |
12036 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
12037 | o->reloc_count = 0; | |
12038 | ||
8423293d | 12039 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12040 | { |
12041 | unsigned int reloc_count = 0; | |
9eaff861 | 12042 | unsigned int additional_reloc_count = 0; |
c152c796 | 12043 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
12044 | |
12045 | if (p->type == bfd_section_reloc_link_order | |
12046 | || p->type == bfd_symbol_reloc_link_order) | |
12047 | reloc_count = 1; | |
12048 | else if (p->type == bfd_indirect_link_order) | |
12049 | { | |
12050 | asection *sec; | |
12051 | ||
12052 | sec = p->u.indirect.section; | |
c152c796 AM |
12053 | |
12054 | /* Mark all sections which are to be included in the | |
12055 | link. This will normally be every section. We need | |
12056 | to do this so that we can identify any sections which | |
12057 | the linker has decided to not include. */ | |
12058 | sec->linker_mark = TRUE; | |
12059 | ||
12060 | if (sec->flags & SEC_MERGE) | |
12061 | merged = TRUE; | |
12062 | ||
eea6121a AM |
12063 | if (sec->rawsize > max_contents_size) |
12064 | max_contents_size = sec->rawsize; | |
12065 | if (sec->size > max_contents_size) | |
12066 | max_contents_size = sec->size; | |
c152c796 | 12067 | |
c152c796 AM |
12068 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
12069 | && (sec->owner->flags & DYNAMIC) == 0) | |
12070 | { | |
12071 | size_t sym_count; | |
12072 | ||
a961cdd5 AM |
12073 | /* We are interested in just local symbols, not all |
12074 | symbols. */ | |
c152c796 AM |
12075 | if (elf_bad_symtab (sec->owner)) |
12076 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
12077 | / bed->s->sizeof_sym); | |
12078 | else | |
12079 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
12080 | ||
12081 | if (sym_count > max_sym_count) | |
12082 | max_sym_count = sym_count; | |
12083 | ||
12084 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 12085 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
12086 | max_sym_shndx_count = sym_count; |
12087 | ||
a961cdd5 AM |
12088 | if (esdo->this_hdr.sh_type == SHT_REL |
12089 | || esdo->this_hdr.sh_type == SHT_RELA) | |
12090 | /* Some backends use reloc_count in relocation sections | |
12091 | to count particular types of relocs. Of course, | |
12092 | reloc sections themselves can't have relocations. */ | |
12093 | ; | |
12094 | else if (emit_relocs) | |
12095 | { | |
12096 | reloc_count = sec->reloc_count; | |
12097 | if (bed->elf_backend_count_additional_relocs) | |
12098 | { | |
12099 | int c; | |
12100 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
12101 | additional_reloc_count += c; | |
12102 | } | |
12103 | } | |
12104 | else if (bed->elf_backend_count_relocs) | |
12105 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
12106 | ||
12107 | esdi = elf_section_data (sec); | |
12108 | ||
c152c796 AM |
12109 | if ((sec->flags & SEC_RELOC) != 0) |
12110 | { | |
d4730f92 | 12111 | size_t ext_size = 0; |
c152c796 | 12112 | |
d4730f92 BS |
12113 | if (esdi->rel.hdr != NULL) |
12114 | ext_size = esdi->rel.hdr->sh_size; | |
12115 | if (esdi->rela.hdr != NULL) | |
12116 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 12117 | |
c152c796 AM |
12118 | if (ext_size > max_external_reloc_size) |
12119 | max_external_reloc_size = ext_size; | |
12120 | if (sec->reloc_count > max_internal_reloc_count) | |
12121 | max_internal_reloc_count = sec->reloc_count; | |
12122 | } | |
12123 | } | |
12124 | } | |
12125 | ||
12126 | if (reloc_count == 0) | |
12127 | continue; | |
12128 | ||
9eaff861 | 12129 | reloc_count += additional_reloc_count; |
c152c796 AM |
12130 | o->reloc_count += reloc_count; |
12131 | ||
0e1862bb | 12132 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 12133 | { |
d4730f92 | 12134 | if (esdi->rel.hdr) |
9eaff861 | 12135 | { |
491d01d3 | 12136 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
12137 | esdo->rel.count += additional_reloc_count; |
12138 | } | |
d4730f92 | 12139 | if (esdi->rela.hdr) |
9eaff861 | 12140 | { |
491d01d3 | 12141 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
12142 | esdo->rela.count += additional_reloc_count; |
12143 | } | |
d4730f92 BS |
12144 | } |
12145 | else | |
12146 | { | |
12147 | if (o->use_rela_p) | |
12148 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 12149 | else |
d4730f92 | 12150 | esdo->rel.count += reloc_count; |
c152c796 | 12151 | } |
c152c796 AM |
12152 | } |
12153 | ||
9eaff861 | 12154 | if (o->reloc_count > 0) |
c152c796 AM |
12155 | o->flags |= SEC_RELOC; |
12156 | else | |
12157 | { | |
12158 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
12159 | set it (this is probably a bug) and if it is set | |
12160 | assign_section_numbers will create a reloc section. */ | |
12161 | o->flags &=~ SEC_RELOC; | |
12162 | } | |
12163 | ||
12164 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
12165 | zero. This is done in elf_fake_sections as well, but forcing | |
12166 | the VMA to 0 here will ensure that relocs against these | |
12167 | sections are handled correctly. */ | |
12168 | if ((o->flags & SEC_ALLOC) == 0 | |
12169 | && ! o->user_set_vma) | |
12170 | o->vma = 0; | |
12171 | } | |
12172 | ||
0e1862bb | 12173 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 12174 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
12175 | |
12176 | /* Figure out the file positions for everything but the symbol table | |
12177 | and the relocs. We set symcount to force assign_section_numbers | |
12178 | to create a symbol table. */ | |
ed48ec2e | 12179 | abfd->symcount = info->strip != strip_all || emit_relocs; |
c152c796 AM |
12180 | BFD_ASSERT (! abfd->output_has_begun); |
12181 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
12182 | goto error_return; | |
12183 | ||
ee75fd95 | 12184 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
12185 | for (o = abfd->sections; o != NULL; o = o->next) |
12186 | { | |
d4730f92 | 12187 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
12188 | if ((o->flags & SEC_RELOC) != 0) |
12189 | { | |
d4730f92 | 12190 | if (esdo->rel.hdr |
9eaff861 | 12191 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
12192 | goto error_return; |
12193 | ||
d4730f92 | 12194 | if (esdo->rela.hdr |
9eaff861 | 12195 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
12196 | goto error_return; |
12197 | } | |
12198 | ||
48db3297 AM |
12199 | /* _bfd_elf_compute_section_file_positions makes temporary use |
12200 | of target_index. Reset it. */ | |
12201 | o->target_index = 0; | |
12202 | ||
c152c796 AM |
12203 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them |
12204 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
12205 | esdo->rel.count = 0; |
12206 | esdo->rela.count = 0; | |
0ce398f1 | 12207 | |
1ff6de03 NA |
12208 | if ((esdo->this_hdr.sh_offset == (file_ptr) -1) |
12209 | && !bfd_section_is_ctf (o)) | |
0ce398f1 L |
12210 | { |
12211 | /* Cache the section contents so that they can be compressed | |
12212 | later. Use bfd_malloc since it will be freed by | |
12213 | bfd_compress_section_contents. */ | |
12214 | unsigned char *contents = esdo->this_hdr.contents; | |
12215 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
12216 | abort (); | |
12217 | contents | |
12218 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
12219 | if (contents == NULL) | |
12220 | goto error_return; | |
12221 | esdo->this_hdr.contents = contents; | |
12222 | } | |
c152c796 AM |
12223 | } |
12224 | ||
1ff6de03 NA |
12225 | /* We have now assigned file positions for all the sections except .symtab, |
12226 | .strtab, and non-loaded reloc and compressed debugging sections. We start | |
12227 | the .symtab section at the current file position, and write directly to it. | |
12228 | We build the .strtab section in memory. */ | |
ed48ec2e | 12229 | abfd->symcount = 0; |
c152c796 AM |
12230 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
12231 | /* sh_name is set in prep_headers. */ | |
12232 | symtab_hdr->sh_type = SHT_SYMTAB; | |
12233 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
12234 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
12235 | /* sh_link is set in assign_section_numbers. */ | |
12236 | /* sh_info is set below. */ | |
12237 | /* sh_offset is set just below. */ | |
72de5009 | 12238 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 12239 | |
ef10c3ac L |
12240 | if (max_sym_count < 20) |
12241 | max_sym_count = 20; | |
64f52338 | 12242 | htab->strtabsize = max_sym_count; |
ef10c3ac | 12243 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
12244 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
12245 | if (htab->strtab == NULL) | |
c152c796 | 12246 | goto error_return; |
ef10c3ac L |
12247 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
12248 | flinfo.symshndxbuf | |
12249 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
12250 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 12251 | |
8539e4e8 | 12252 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 12253 | { |
8539e4e8 AM |
12254 | file_ptr off = elf_next_file_pos (abfd); |
12255 | ||
12256 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
12257 | ||
12258 | /* Note that at this point elf_next_file_pos (abfd) is | |
12259 | incorrect. We do not yet know the size of the .symtab section. | |
12260 | We correct next_file_pos below, after we do know the size. */ | |
12261 | ||
12262 | /* Start writing out the symbol table. The first symbol is always a | |
12263 | dummy symbol. */ | |
c152c796 AM |
12264 | elfsym.st_value = 0; |
12265 | elfsym.st_size = 0; | |
12266 | elfsym.st_info = 0; | |
12267 | elfsym.st_other = 0; | |
12268 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 12269 | elfsym.st_target_internal = 0; |
ef10c3ac L |
12270 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
12271 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 12272 | goto error_return; |
c152c796 | 12273 | |
8539e4e8 AM |
12274 | /* Output a symbol for each section. We output these even if we are |
12275 | discarding local symbols, since they are used for relocs. These | |
12276 | symbols have no names. We store the index of each one in the | |
12277 | index field of the section, so that we can find it again when | |
12278 | outputting relocs. */ | |
12279 | ||
c152c796 AM |
12280 | elfsym.st_size = 0; |
12281 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12282 | elfsym.st_other = 0; | |
f0b5bb34 | 12283 | elfsym.st_value = 0; |
35fc36a8 | 12284 | elfsym.st_target_internal = 0; |
c152c796 AM |
12285 | for (i = 1; i < elf_numsections (abfd); i++) |
12286 | { | |
12287 | o = bfd_section_from_elf_index (abfd, i); | |
12288 | if (o != NULL) | |
f0b5bb34 AM |
12289 | { |
12290 | o->target_index = bfd_get_symcount (abfd); | |
12291 | elfsym.st_shndx = i; | |
0e1862bb | 12292 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 12293 | elfsym.st_value = o->vma; |
ef10c3ac L |
12294 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
12295 | NULL) != 1) | |
f0b5bb34 AM |
12296 | goto error_return; |
12297 | } | |
c152c796 AM |
12298 | } |
12299 | } | |
12300 | ||
12301 | /* Allocate some memory to hold information read in from the input | |
12302 | files. */ | |
12303 | if (max_contents_size != 0) | |
12304 | { | |
8b127cbc AM |
12305 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
12306 | if (flinfo.contents == NULL) | |
c152c796 AM |
12307 | goto error_return; |
12308 | } | |
12309 | ||
12310 | if (max_external_reloc_size != 0) | |
12311 | { | |
8b127cbc AM |
12312 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
12313 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
12314 | goto error_return; |
12315 | } | |
12316 | ||
12317 | if (max_internal_reloc_count != 0) | |
12318 | { | |
056bafd4 | 12319 | amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); |
8b127cbc AM |
12320 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
12321 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
12322 | goto error_return; |
12323 | } | |
12324 | ||
12325 | if (max_sym_count != 0) | |
12326 | { | |
12327 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
12328 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
12329 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
12330 | goto error_return; |
12331 | ||
12332 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
12333 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
12334 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
12335 | goto error_return; |
12336 | ||
12337 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
12338 | flinfo.indices = (long int *) bfd_malloc (amt); |
12339 | if (flinfo.indices == NULL) | |
c152c796 AM |
12340 | goto error_return; |
12341 | ||
12342 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
12343 | flinfo.sections = (asection **) bfd_malloc (amt); |
12344 | if (flinfo.sections == NULL) | |
c152c796 AM |
12345 | goto error_return; |
12346 | } | |
12347 | ||
12348 | if (max_sym_shndx_count != 0) | |
12349 | { | |
12350 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
12351 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
12352 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
12353 | goto error_return; |
12354 | } | |
12355 | ||
64f52338 | 12356 | if (htab->tls_sec) |
c152c796 | 12357 | { |
66631823 | 12358 | bfd_vma base, end = 0; /* Both bytes. */ |
c152c796 AM |
12359 | asection *sec; |
12360 | ||
64f52338 | 12361 | for (sec = htab->tls_sec; |
c152c796 AM |
12362 | sec && (sec->flags & SEC_THREAD_LOCAL); |
12363 | sec = sec->next) | |
12364 | { | |
3a800eb9 | 12365 | bfd_size_type size = sec->size; |
66631823 | 12366 | unsigned int opb = bfd_octets_per_byte (abfd, sec); |
c152c796 | 12367 | |
3a800eb9 AM |
12368 | if (size == 0 |
12369 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 12370 | { |
91d6fa6a NC |
12371 | struct bfd_link_order *ord = sec->map_tail.link_order; |
12372 | ||
12373 | if (ord != NULL) | |
66631823 | 12374 | size = ord->offset * opb + ord->size; |
c152c796 | 12375 | } |
66631823 | 12376 | end = sec->vma + size / opb; |
c152c796 | 12377 | } |
64f52338 | 12378 | base = htab->tls_sec->vma; |
7dc98aea RO |
12379 | /* Only align end of TLS section if static TLS doesn't have special |
12380 | alignment requirements. */ | |
12381 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
12382 | end = align_power (end, htab->tls_sec->alignment_power); |
12383 | htab->tls_size = end - base; | |
c152c796 AM |
12384 | } |
12385 | ||
0b52efa6 PB |
12386 | /* Reorder SHF_LINK_ORDER sections. */ |
12387 | for (o = abfd->sections; o != NULL; o = o->next) | |
12388 | { | |
12389 | if (!elf_fixup_link_order (abfd, o)) | |
12390 | return FALSE; | |
12391 | } | |
12392 | ||
2f0c68f2 CM |
12393 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
12394 | return FALSE; | |
12395 | ||
c152c796 AM |
12396 | /* Since ELF permits relocations to be against local symbols, we |
12397 | must have the local symbols available when we do the relocations. | |
12398 | Since we would rather only read the local symbols once, and we | |
12399 | would rather not keep them in memory, we handle all the | |
12400 | relocations for a single input file at the same time. | |
12401 | ||
12402 | Unfortunately, there is no way to know the total number of local | |
12403 | symbols until we have seen all of them, and the local symbol | |
12404 | indices precede the global symbol indices. This means that when | |
12405 | we are generating relocatable output, and we see a reloc against | |
12406 | a global symbol, we can not know the symbol index until we have | |
12407 | finished examining all the local symbols to see which ones we are | |
12408 | going to output. To deal with this, we keep the relocations in | |
12409 | memory, and don't output them until the end of the link. This is | |
12410 | an unfortunate waste of memory, but I don't see a good way around | |
12411 | it. Fortunately, it only happens when performing a relocatable | |
12412 | link, which is not the common case. FIXME: If keep_memory is set | |
12413 | we could write the relocs out and then read them again; I don't | |
12414 | know how bad the memory loss will be. */ | |
12415 | ||
c72f2fb2 | 12416 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12417 | sub->output_has_begun = FALSE; |
12418 | for (o = abfd->sections; o != NULL; o = o->next) | |
12419 | { | |
8423293d | 12420 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12421 | { |
12422 | if (p->type == bfd_indirect_link_order | |
12423 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
12424 | == bfd_target_elf_flavour) | |
12425 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
12426 | { | |
12427 | if (! sub->output_has_begun) | |
12428 | { | |
8b127cbc | 12429 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
12430 | goto error_return; |
12431 | sub->output_has_begun = TRUE; | |
12432 | } | |
12433 | } | |
12434 | else if (p->type == bfd_section_reloc_link_order | |
12435 | || p->type == bfd_symbol_reloc_link_order) | |
12436 | { | |
12437 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
12438 | goto error_return; | |
12439 | } | |
12440 | else | |
12441 | { | |
12442 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
12443 | { |
12444 | if (p->type == bfd_indirect_link_order | |
12445 | && (bfd_get_flavour (sub) | |
12446 | == bfd_target_elf_flavour) | |
12447 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
12448 | != bed->s->elfclass)) | |
12449 | { | |
12450 | const char *iclass, *oclass; | |
12451 | ||
aebf9be7 | 12452 | switch (bed->s->elfclass) |
351f65ca | 12453 | { |
aebf9be7 NC |
12454 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
12455 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
12456 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
12457 | default: abort (); | |
351f65ca | 12458 | } |
aebf9be7 NC |
12459 | |
12460 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 12461 | { |
aebf9be7 NC |
12462 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
12463 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
12464 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
12465 | default: abort (); | |
351f65ca L |
12466 | } |
12467 | ||
12468 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 12469 | _bfd_error_handler |
695344c0 | 12470 | /* xgettext:c-format */ |
871b3ab2 | 12471 | (_("%pB: file class %s incompatible with %s"), |
351f65ca L |
12472 | sub, iclass, oclass); |
12473 | } | |
12474 | ||
12475 | goto error_return; | |
12476 | } | |
c152c796 AM |
12477 | } |
12478 | } | |
12479 | } | |
12480 | ||
c0f00686 L |
12481 | /* Free symbol buffer if needed. */ |
12482 | if (!info->reduce_memory_overheads) | |
12483 | { | |
c72f2fb2 | 12484 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c9594989 | 12485 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
c0f00686 L |
12486 | { |
12487 | free (elf_tdata (sub)->symbuf); | |
12488 | elf_tdata (sub)->symbuf = NULL; | |
12489 | } | |
12490 | } | |
12491 | ||
c152c796 AM |
12492 | /* Output any global symbols that got converted to local in a |
12493 | version script or due to symbol visibility. We do this in a | |
12494 | separate step since ELF requires all local symbols to appear | |
12495 | prior to any global symbols. FIXME: We should only do this if | |
12496 | some global symbols were, in fact, converted to become local. | |
12497 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
12498 | eoinfo.failed = FALSE; | |
8b127cbc | 12499 | eoinfo.flinfo = &flinfo; |
c152c796 | 12500 | eoinfo.localsyms = TRUE; |
34a79995 | 12501 | eoinfo.file_sym_done = FALSE; |
7686d77d | 12502 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12503 | if (eoinfo.failed) |
12504 | return FALSE; | |
12505 | ||
4e617b1e PB |
12506 | /* If backend needs to output some local symbols not present in the hash |
12507 | table, do it now. */ | |
8539e4e8 AM |
12508 | if (bed->elf_backend_output_arch_local_syms |
12509 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 12510 | { |
6e0b88f1 | 12511 | typedef int (*out_sym_func) |
4e617b1e PB |
12512 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12513 | struct elf_link_hash_entry *); | |
12514 | ||
12515 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
12516 | (abfd, info, &flinfo, |
12517 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
12518 | return FALSE; |
12519 | } | |
12520 | ||
c152c796 AM |
12521 | /* That wrote out all the local symbols. Finish up the symbol table |
12522 | with the global symbols. Even if we want to strip everything we | |
12523 | can, we still need to deal with those global symbols that got | |
12524 | converted to local in a version script. */ | |
12525 | ||
12526 | /* The sh_info field records the index of the first non local symbol. */ | |
12527 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
12528 | ||
12529 | if (dynamic | |
64f52338 AM |
12530 | && htab->dynsym != NULL |
12531 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
12532 | { |
12533 | Elf_Internal_Sym sym; | |
64f52338 | 12534 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 12535 | |
64f52338 AM |
12536 | o = htab->dynsym->output_section; |
12537 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
12538 | |
12539 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 12540 | if (bfd_link_pic (info) |
64f52338 | 12541 | || htab->is_relocatable_executable) |
c152c796 AM |
12542 | { |
12543 | asection *s; | |
12544 | ||
12545 | sym.st_size = 0; | |
12546 | sym.st_name = 0; | |
12547 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12548 | sym.st_other = 0; | |
35fc36a8 | 12549 | sym.st_target_internal = 0; |
c152c796 AM |
12550 | |
12551 | for (s = abfd->sections; s != NULL; s = s->next) | |
12552 | { | |
12553 | int indx; | |
12554 | bfd_byte *dest; | |
12555 | long dynindx; | |
12556 | ||
c152c796 | 12557 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
12558 | if (dynindx <= 0) |
12559 | continue; | |
12560 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
12561 | BFD_ASSERT (indx > 0); |
12562 | sym.st_shndx = indx; | |
c0d5a53d L |
12563 | if (! check_dynsym (abfd, &sym)) |
12564 | return FALSE; | |
c152c796 AM |
12565 | sym.st_value = s->vma; |
12566 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
12567 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12568 | } | |
c152c796 AM |
12569 | } |
12570 | ||
12571 | /* Write out the local dynsyms. */ | |
64f52338 | 12572 | if (htab->dynlocal) |
c152c796 AM |
12573 | { |
12574 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 12575 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
12576 | { |
12577 | asection *s; | |
12578 | bfd_byte *dest; | |
12579 | ||
935bd1e0 | 12580 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
12581 | Note that we saved a word of storage and overwrote |
12582 | the original st_name with the dynstr_index. */ | |
12583 | sym = e->isym; | |
935bd1e0 | 12584 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 12585 | |
cb33740c AM |
12586 | s = bfd_section_from_elf_index (e->input_bfd, |
12587 | e->isym.st_shndx); | |
12588 | if (s != NULL) | |
c152c796 | 12589 | { |
c152c796 AM |
12590 | sym.st_shndx = |
12591 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
12592 | if (! check_dynsym (abfd, &sym)) |
12593 | return FALSE; | |
c152c796 AM |
12594 | sym.st_value = (s->output_section->vma |
12595 | + s->output_offset | |
12596 | + e->isym.st_value); | |
12597 | } | |
12598 | ||
c152c796 AM |
12599 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
12600 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12601 | } | |
12602 | } | |
c152c796 AM |
12603 | } |
12604 | ||
12605 | /* We get the global symbols from the hash table. */ | |
12606 | eoinfo.failed = FALSE; | |
12607 | eoinfo.localsyms = FALSE; | |
8b127cbc | 12608 | eoinfo.flinfo = &flinfo; |
7686d77d | 12609 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12610 | if (eoinfo.failed) |
12611 | return FALSE; | |
12612 | ||
12613 | /* If backend needs to output some symbols not present in the hash | |
12614 | table, do it now. */ | |
8539e4e8 AM |
12615 | if (bed->elf_backend_output_arch_syms |
12616 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 12617 | { |
6e0b88f1 | 12618 | typedef int (*out_sym_func) |
c152c796 AM |
12619 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12620 | struct elf_link_hash_entry *); | |
12621 | ||
12622 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
12623 | (abfd, info, &flinfo, |
12624 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
12625 | return FALSE; |
12626 | } | |
12627 | ||
ef10c3ac L |
12628 | /* Finalize the .strtab section. */ |
12629 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
12630 | ||
12631 | /* Swap out the .strtab section. */ | |
12632 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
12633 | return FALSE; |
12634 | ||
12635 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
12636 | if (bfd_get_symcount (abfd) > 0) |
12637 | { | |
ee3b52e9 L |
12638 | /* Finish up and write out the symbol string table (.strtab) |
12639 | section. */ | |
ad32986f | 12640 | Elf_Internal_Shdr *symstrtab_hdr = NULL; |
8539e4e8 AM |
12641 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
12642 | ||
ad32986f | 12643 | if (elf_symtab_shndx_list (abfd)) |
8539e4e8 | 12644 | { |
ad32986f | 12645 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8539e4e8 | 12646 | |
ad32986f NC |
12647 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) |
12648 | { | |
12649 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
12650 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
12651 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
12652 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
12653 | symtab_shndx_hdr->sh_size = amt; | |
8539e4e8 | 12654 | |
ad32986f NC |
12655 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, |
12656 | off, TRUE); | |
12657 | ||
12658 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
12659 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
12660 | return FALSE; | |
12661 | } | |
8539e4e8 | 12662 | } |
ee3b52e9 L |
12663 | |
12664 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
12665 | /* sh_name was set in prep_headers. */ | |
12666 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 12667 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 12668 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 12669 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
12670 | symstrtab_hdr->sh_entsize = 0; |
12671 | symstrtab_hdr->sh_link = 0; | |
12672 | symstrtab_hdr->sh_info = 0; | |
12673 | /* sh_offset is set just below. */ | |
12674 | symstrtab_hdr->sh_addralign = 1; | |
12675 | ||
12676 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
12677 | off, TRUE); | |
12678 | elf_next_file_pos (abfd) = off; | |
12679 | ||
c152c796 | 12680 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 12681 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
12682 | return FALSE; |
12683 | } | |
12684 | ||
76359541 TP |
12685 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
12686 | { | |
871b3ab2 | 12687 | _bfd_error_handler (_("%pB: failed to generate import library"), |
4eca0228 | 12688 | info->out_implib_bfd); |
76359541 TP |
12689 | return FALSE; |
12690 | } | |
12691 | ||
c152c796 AM |
12692 | /* Adjust the relocs to have the correct symbol indices. */ |
12693 | for (o = abfd->sections; o != NULL; o = o->next) | |
12694 | { | |
d4730f92 | 12695 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 12696 | bfd_boolean sort; |
10bbbc1d | 12697 | |
c152c796 AM |
12698 | if ((o->flags & SEC_RELOC) == 0) |
12699 | continue; | |
12700 | ||
28dbcedc | 12701 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 12702 | if (esdo->rel.hdr != NULL |
10bbbc1d | 12703 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) |
bca6d0e3 AM |
12704 | return FALSE; |
12705 | if (esdo->rela.hdr != NULL | |
10bbbc1d | 12706 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) |
bca6d0e3 | 12707 | return FALSE; |
c152c796 AM |
12708 | |
12709 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
12710 | trying to swap the relocs out itself. */ | |
12711 | o->reloc_count = 0; | |
12712 | } | |
12713 | ||
12714 | if (dynamic && info->combreloc && dynobj != NULL) | |
12715 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
12716 | ||
12717 | /* If we are linking against a dynamic object, or generating a | |
12718 | shared library, finish up the dynamic linking information. */ | |
12719 | if (dynamic) | |
12720 | { | |
12721 | bfd_byte *dyncon, *dynconend; | |
12722 | ||
12723 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12724 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12725 | BFD_ASSERT (o != NULL); |
12726 | ||
12727 | dyncon = o->contents; | |
eea6121a | 12728 | dynconend = o->contents + o->size; |
c152c796 AM |
12729 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12730 | { | |
12731 | Elf_Internal_Dyn dyn; | |
12732 | const char *name; | |
12733 | unsigned int type; | |
64487780 AM |
12734 | bfd_size_type sh_size; |
12735 | bfd_vma sh_addr; | |
c152c796 AM |
12736 | |
12737 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12738 | ||
12739 | switch (dyn.d_tag) | |
12740 | { | |
12741 | default: | |
12742 | continue; | |
12743 | case DT_NULL: | |
12744 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12745 | { | |
12746 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12747 | { | |
12748 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12749 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12750 | default: continue; | |
12751 | } | |
12752 | dyn.d_un.d_val = relativecount; | |
12753 | relativecount = 0; | |
12754 | break; | |
12755 | } | |
12756 | continue; | |
12757 | ||
12758 | case DT_INIT: | |
12759 | name = info->init_function; | |
12760 | goto get_sym; | |
12761 | case DT_FINI: | |
12762 | name = info->fini_function; | |
12763 | get_sym: | |
12764 | { | |
12765 | struct elf_link_hash_entry *h; | |
12766 | ||
64f52338 | 12767 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12768 | if (h != NULL |
12769 | && (h->root.type == bfd_link_hash_defined | |
12770 | || h->root.type == bfd_link_hash_defweak)) | |
12771 | { | |
bef26483 | 12772 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12773 | o = h->root.u.def.section; |
12774 | if (o->output_section != NULL) | |
bef26483 | 12775 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12776 | + o->output_offset); |
12777 | else | |
12778 | { | |
12779 | /* The symbol is imported from another shared | |
12780 | library and does not apply to this one. */ | |
bef26483 | 12781 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12782 | } |
12783 | break; | |
12784 | } | |
12785 | } | |
12786 | continue; | |
12787 | ||
12788 | case DT_PREINIT_ARRAYSZ: | |
12789 | name = ".preinit_array"; | |
4ade44b7 | 12790 | goto get_out_size; |
c152c796 AM |
12791 | case DT_INIT_ARRAYSZ: |
12792 | name = ".init_array"; | |
4ade44b7 | 12793 | goto get_out_size; |
c152c796 AM |
12794 | case DT_FINI_ARRAYSZ: |
12795 | name = ".fini_array"; | |
4ade44b7 | 12796 | get_out_size: |
c152c796 AM |
12797 | o = bfd_get_section_by_name (abfd, name); |
12798 | if (o == NULL) | |
12799 | { | |
4eca0228 | 12800 | _bfd_error_handler |
4ade44b7 | 12801 | (_("could not find section %s"), name); |
c152c796 AM |
12802 | goto error_return; |
12803 | } | |
eea6121a | 12804 | if (o->size == 0) |
4eca0228 | 12805 | _bfd_error_handler |
c152c796 | 12806 | (_("warning: %s section has zero size"), name); |
eea6121a | 12807 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12808 | break; |
12809 | ||
12810 | case DT_PREINIT_ARRAY: | |
12811 | name = ".preinit_array"; | |
4ade44b7 | 12812 | goto get_out_vma; |
c152c796 AM |
12813 | case DT_INIT_ARRAY: |
12814 | name = ".init_array"; | |
4ade44b7 | 12815 | goto get_out_vma; |
c152c796 AM |
12816 | case DT_FINI_ARRAY: |
12817 | name = ".fini_array"; | |
4ade44b7 AM |
12818 | get_out_vma: |
12819 | o = bfd_get_section_by_name (abfd, name); | |
12820 | goto do_vma; | |
c152c796 AM |
12821 | |
12822 | case DT_HASH: | |
12823 | name = ".hash"; | |
12824 | goto get_vma; | |
fdc90cb4 JJ |
12825 | case DT_GNU_HASH: |
12826 | name = ".gnu.hash"; | |
12827 | goto get_vma; | |
c152c796 AM |
12828 | case DT_STRTAB: |
12829 | name = ".dynstr"; | |
12830 | goto get_vma; | |
12831 | case DT_SYMTAB: | |
12832 | name = ".dynsym"; | |
12833 | goto get_vma; | |
12834 | case DT_VERDEF: | |
12835 | name = ".gnu.version_d"; | |
12836 | goto get_vma; | |
12837 | case DT_VERNEED: | |
12838 | name = ".gnu.version_r"; | |
12839 | goto get_vma; | |
12840 | case DT_VERSYM: | |
12841 | name = ".gnu.version"; | |
12842 | get_vma: | |
4ade44b7 AM |
12843 | o = bfd_get_linker_section (dynobj, name); |
12844 | do_vma: | |
b3293efa | 12845 | if (o == NULL || bfd_is_abs_section (o->output_section)) |
c152c796 | 12846 | { |
4eca0228 | 12847 | _bfd_error_handler |
4ade44b7 | 12848 | (_("could not find section %s"), name); |
c152c796 AM |
12849 | goto error_return; |
12850 | } | |
894891db NC |
12851 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12852 | { | |
4eca0228 | 12853 | _bfd_error_handler |
894891db NC |
12854 | (_("warning: section '%s' is being made into a note"), name); |
12855 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12856 | goto error_return; | |
12857 | } | |
4ade44b7 | 12858 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12859 | break; |
12860 | ||
12861 | case DT_REL: | |
12862 | case DT_RELA: | |
12863 | case DT_RELSZ: | |
12864 | case DT_RELASZ: | |
12865 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12866 | type = SHT_REL; | |
12867 | else | |
12868 | type = SHT_RELA; | |
64487780 AM |
12869 | sh_size = 0; |
12870 | sh_addr = 0; | |
c152c796 AM |
12871 | for (i = 1; i < elf_numsections (abfd); i++) |
12872 | { | |
12873 | Elf_Internal_Shdr *hdr; | |
12874 | ||
12875 | hdr = elf_elfsections (abfd)[i]; | |
12876 | if (hdr->sh_type == type | |
12877 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12878 | { | |
64487780 AM |
12879 | sh_size += hdr->sh_size; |
12880 | if (sh_addr == 0 | |
12881 | || sh_addr > hdr->sh_addr) | |
12882 | sh_addr = hdr->sh_addr; | |
c152c796 AM |
12883 | } |
12884 | } | |
64487780 | 12885 | |
64f52338 AM |
12886 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12887 | { | |
66631823 CE |
12888 | unsigned int opb = bfd_octets_per_byte (abfd, o); |
12889 | ||
64f52338 AM |
12890 | /* Don't count procedure linkage table relocs in the |
12891 | overall reloc count. */ | |
64487780 AM |
12892 | sh_size -= htab->srelplt->size; |
12893 | if (sh_size == 0) | |
12894 | /* If the size is zero, make the address zero too. | |
12895 | This is to avoid a glibc bug. If the backend | |
12896 | emits DT_RELA/DT_RELASZ even when DT_RELASZ is | |
12897 | zero, then we'll put DT_RELA at the end of | |
12898 | DT_JMPREL. glibc will interpret the end of | |
12899 | DT_RELA matching the end of DT_JMPREL as the | |
12900 | case where DT_RELA includes DT_JMPREL, and for | |
12901 | LD_BIND_NOW will decide that processing DT_RELA | |
12902 | will process the PLT relocs too. Net result: | |
12903 | No PLT relocs applied. */ | |
12904 | sh_addr = 0; | |
12905 | ||
64f52338 AM |
12906 | /* If .rela.plt is the first .rela section, exclude |
12907 | it from DT_RELA. */ | |
64487780 | 12908 | else if (sh_addr == (htab->srelplt->output_section->vma |
66631823 | 12909 | + htab->srelplt->output_offset) * opb) |
64487780 | 12910 | sh_addr += htab->srelplt->size; |
64f52338 | 12911 | } |
64487780 AM |
12912 | |
12913 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12914 | dyn.d_un.d_val = sh_size; | |
12915 | else | |
12916 | dyn.d_un.d_ptr = sh_addr; | |
c152c796 AM |
12917 | break; |
12918 | } | |
12919 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12920 | } | |
12921 | } | |
12922 | ||
12923 | /* If we have created any dynamic sections, then output them. */ | |
12924 | if (dynobj != NULL) | |
12925 | { | |
12926 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12927 | goto error_return; | |
12928 | ||
943284cc | 12929 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
a6dbf402 | 12930 | if (bfd_link_textrel_check (info) |
3d4d4302 | 12931 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12932 | { |
12933 | bfd_byte *dyncon, *dynconend; | |
12934 | ||
943284cc DJ |
12935 | dyncon = o->contents; |
12936 | dynconend = o->contents + o->size; | |
12937 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12938 | { | |
12939 | Elf_Internal_Dyn dyn; | |
12940 | ||
12941 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12942 | ||
12943 | if (dyn.d_tag == DT_TEXTREL) | |
12944 | { | |
a6dbf402 | 12945 | if (info->textrel_check == textrel_check_error) |
c192a133 | 12946 | info->callbacks->einfo |
9793eb77 | 12947 | (_("%P%X: read-only segment has dynamic relocations\n")); |
a6dbf402 L |
12948 | else if (bfd_link_dll (info)) |
12949 | info->callbacks->einfo | |
12950 | (_("%P: warning: creating DT_TEXTREL in a shared object\n")); | |
c192a133 AM |
12951 | else |
12952 | info->callbacks->einfo | |
a6dbf402 | 12953 | (_("%P: warning: creating DT_TEXTREL in a PIE\n")); |
943284cc DJ |
12954 | break; |
12955 | } | |
12956 | } | |
12957 | } | |
12958 | ||
c152c796 AM |
12959 | for (o = dynobj->sections; o != NULL; o = o->next) |
12960 | { | |
12961 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12962 | || o->size == 0 |
c152c796 AM |
12963 | || o->output_section == bfd_abs_section_ptr) |
12964 | continue; | |
12965 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12966 | { | |
12967 | /* At this point, we are only interested in sections | |
12968 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12969 | continue; | |
12970 | } | |
64f52338 | 12971 | if (htab->stab_info.stabstr == o) |
3722b82f | 12972 | continue; |
64f52338 | 12973 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12974 | continue; |
3d4d4302 | 12975 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 | 12976 | { |
bb294208 AM |
12977 | bfd_size_type octets = ((file_ptr) o->output_offset |
12978 | * bfd_octets_per_byte (abfd, o)); | |
12979 | if (!bfd_set_section_contents (abfd, o->output_section, | |
12980 | o->contents, octets, o->size)) | |
c152c796 AM |
12981 | goto error_return; |
12982 | } | |
12983 | else | |
12984 | { | |
12985 | /* The contents of the .dynstr section are actually in a | |
12986 | stringtab. */ | |
8539e4e8 AM |
12987 | file_ptr off; |
12988 | ||
c152c796 AM |
12989 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12990 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12991 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12992 | goto error_return; |
12993 | } | |
12994 | } | |
12995 | } | |
12996 | ||
7bdf4127 | 12997 | if (!info->resolve_section_groups) |
c152c796 AM |
12998 | { |
12999 | bfd_boolean failed = FALSE; | |
13000 | ||
7bdf4127 | 13001 | BFD_ASSERT (bfd_link_relocatable (info)); |
c152c796 AM |
13002 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
13003 | if (failed) | |
13004 | goto error_return; | |
13005 | } | |
13006 | ||
13007 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 13008 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 13009 | { |
64f52338 | 13010 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
13011 | goto error_return; |
13012 | } | |
13013 | ||
9f7c3e5e AM |
13014 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
13015 | goto error_return; | |
c152c796 | 13016 | |
1ff6de03 NA |
13017 | if (info->callbacks->emit_ctf) |
13018 | info->callbacks->emit_ctf (); | |
13019 | ||
9f7c3e5e | 13020 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 13021 | |
104d59d1 JM |
13022 | if (attr_section) |
13023 | { | |
a50b1753 | 13024 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 13025 | if (contents == NULL) |
d0f16d5e | 13026 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
13027 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
13028 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
13029 | free (contents); | |
13030 | } | |
13031 | ||
c152c796 AM |
13032 | return TRUE; |
13033 | ||
13034 | error_return: | |
9f7c3e5e | 13035 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
13036 | return FALSE; |
13037 | } | |
13038 | \f | |
5241d853 RS |
13039 | /* Initialize COOKIE for input bfd ABFD. */ |
13040 | ||
13041 | static bfd_boolean | |
13042 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
13043 | struct bfd_link_info *info, bfd *abfd) | |
13044 | { | |
13045 | Elf_Internal_Shdr *symtab_hdr; | |
13046 | const struct elf_backend_data *bed; | |
13047 | ||
13048 | bed = get_elf_backend_data (abfd); | |
13049 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
13050 | ||
13051 | cookie->abfd = abfd; | |
13052 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
13053 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
13054 | if (cookie->bad_symtab) | |
13055 | { | |
13056 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13057 | cookie->extsymoff = 0; | |
13058 | } | |
13059 | else | |
13060 | { | |
13061 | cookie->locsymcount = symtab_hdr->sh_info; | |
13062 | cookie->extsymoff = symtab_hdr->sh_info; | |
13063 | } | |
13064 | ||
13065 | if (bed->s->arch_size == 32) | |
13066 | cookie->r_sym_shift = 8; | |
13067 | else | |
13068 | cookie->r_sym_shift = 32; | |
13069 | ||
13070 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
13071 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
13072 | { | |
13073 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
13074 | cookie->locsymcount, 0, | |
13075 | NULL, NULL, NULL); | |
13076 | if (cookie->locsyms == NULL) | |
13077 | { | |
13078 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
13079 | return FALSE; | |
13080 | } | |
13081 | if (info->keep_memory) | |
13082 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
13083 | } | |
13084 | return TRUE; | |
13085 | } | |
13086 | ||
13087 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
13088 | ||
13089 | static void | |
13090 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
13091 | { | |
13092 | Elf_Internal_Shdr *symtab_hdr; | |
13093 | ||
13094 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
c9594989 | 13095 | if (symtab_hdr->contents != (unsigned char *) cookie->locsyms) |
5241d853 RS |
13096 | free (cookie->locsyms); |
13097 | } | |
13098 | ||
13099 | /* Initialize the relocation information in COOKIE for input section SEC | |
13100 | of input bfd ABFD. */ | |
13101 | ||
13102 | static bfd_boolean | |
13103 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
13104 | struct bfd_link_info *info, bfd *abfd, | |
13105 | asection *sec) | |
13106 | { | |
5241d853 RS |
13107 | if (sec->reloc_count == 0) |
13108 | { | |
13109 | cookie->rels = NULL; | |
13110 | cookie->relend = NULL; | |
13111 | } | |
13112 | else | |
13113 | { | |
5241d853 RS |
13114 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, |
13115 | info->keep_memory); | |
13116 | if (cookie->rels == NULL) | |
13117 | return FALSE; | |
13118 | cookie->rel = cookie->rels; | |
056bafd4 | 13119 | cookie->relend = cookie->rels + sec->reloc_count; |
5241d853 RS |
13120 | } |
13121 | cookie->rel = cookie->rels; | |
13122 | return TRUE; | |
13123 | } | |
13124 | ||
13125 | /* Free the memory allocated by init_reloc_cookie_rels, | |
13126 | if appropriate. */ | |
13127 | ||
13128 | static void | |
13129 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
13130 | asection *sec) | |
13131 | { | |
c9594989 | 13132 | if (elf_section_data (sec)->relocs != cookie->rels) |
5241d853 RS |
13133 | free (cookie->rels); |
13134 | } | |
13135 | ||
13136 | /* Initialize the whole of COOKIE for input section SEC. */ | |
13137 | ||
13138 | static bfd_boolean | |
13139 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
13140 | struct bfd_link_info *info, | |
13141 | asection *sec) | |
13142 | { | |
13143 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
13144 | goto error1; | |
13145 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
13146 | goto error2; | |
13147 | return TRUE; | |
13148 | ||
13149 | error2: | |
13150 | fini_reloc_cookie (cookie, sec->owner); | |
13151 | error1: | |
13152 | return FALSE; | |
13153 | } | |
13154 | ||
13155 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
13156 | if appropriate. */ | |
13157 | ||
13158 | static void | |
13159 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
13160 | asection *sec) | |
13161 | { | |
13162 | fini_reloc_cookie_rels (cookie, sec); | |
13163 | fini_reloc_cookie (cookie, sec->owner); | |
13164 | } | |
13165 | \f | |
c152c796 AM |
13166 | /* Garbage collect unused sections. */ |
13167 | ||
07adf181 AM |
13168 | /* Default gc_mark_hook. */ |
13169 | ||
13170 | asection * | |
13171 | _bfd_elf_gc_mark_hook (asection *sec, | |
13172 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13173 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
13174 | struct elf_link_hash_entry *h, | |
13175 | Elf_Internal_Sym *sym) | |
13176 | { | |
13177 | if (h != NULL) | |
13178 | { | |
13179 | switch (h->root.type) | |
13180 | { | |
13181 | case bfd_link_hash_defined: | |
13182 | case bfd_link_hash_defweak: | |
13183 | return h->root.u.def.section; | |
13184 | ||
13185 | case bfd_link_hash_common: | |
13186 | return h->root.u.c.p->section; | |
13187 | ||
13188 | default: | |
13189 | break; | |
13190 | } | |
13191 | } | |
13192 | else | |
13193 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
13194 | ||
13195 | return NULL; | |
13196 | } | |
13197 | ||
9e223787 | 13198 | /* Return the debug definition section. */ |
b7c871ed L |
13199 | |
13200 | static asection * | |
13201 | elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, | |
13202 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13203 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
13204 | struct elf_link_hash_entry *h, | |
9e223787 | 13205 | Elf_Internal_Sym *sym) |
b7c871ed | 13206 | { |
9e223787 L |
13207 | if (h != NULL) |
13208 | { | |
13209 | /* Return the global debug definition section. */ | |
13210 | if ((h->root.type == bfd_link_hash_defined | |
13211 | || h->root.type == bfd_link_hash_defweak) | |
13212 | && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) | |
13213 | return h->root.u.def.section; | |
13214 | } | |
13215 | else | |
13216 | { | |
13217 | /* Return the local debug definition section. */ | |
13218 | asection *isec = bfd_section_from_elf_index (sec->owner, | |
13219 | sym->st_shndx); | |
13220 | if ((isec->flags & SEC_DEBUGGING) != 0) | |
13221 | return isec; | |
13222 | } | |
b7c871ed L |
13223 | |
13224 | return NULL; | |
13225 | } | |
13226 | ||
5241d853 RS |
13227 | /* COOKIE->rel describes a relocation against section SEC, which is |
13228 | a section we've decided to keep. Return the section that contains | |
13229 | the relocation symbol, or NULL if no section contains it. */ | |
13230 | ||
13231 | asection * | |
13232 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
13233 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
13234 | struct elf_reloc_cookie *cookie, |
13235 | bfd_boolean *start_stop) | |
5241d853 RS |
13236 | { |
13237 | unsigned long r_symndx; | |
3024a17a | 13238 | struct elf_link_hash_entry *h, *hw; |
5241d853 RS |
13239 | |
13240 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 13241 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
13242 | return NULL; |
13243 | ||
13244 | if (r_symndx >= cookie->locsymcount | |
13245 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13246 | { | |
13247 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
13248 | if (h == NULL) |
13249 | { | |
871b3ab2 | 13250 | info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"), |
263ddf68 L |
13251 | sec->owner); |
13252 | return NULL; | |
13253 | } | |
5241d853 RS |
13254 | while (h->root.type == bfd_link_hash_indirect |
13255 | || h->root.type == bfd_link_hash_warning) | |
13256 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 13257 | h->mark = 1; |
3024a17a AM |
13258 | /* Keep all aliases of the symbol too. If an object symbol |
13259 | needs to be copied into .dynbss then all of its aliases | |
13260 | should be present as dynamic symbols, not just the one used | |
13261 | on the copy relocation. */ | |
13262 | hw = h; | |
13263 | while (hw->is_weakalias) | |
13264 | { | |
13265 | hw = hw->u.alias; | |
13266 | hw->mark = 1; | |
13267 | } | |
1cce69b9 | 13268 | |
a6a4679f | 13269 | if (start_stop != NULL) |
1cce69b9 | 13270 | { |
7dba9362 AM |
13271 | /* To work around a glibc bug, mark XXX input sections |
13272 | when there is a reference to __start_XXX or __stop_XXX | |
13273 | symbols. */ | |
cbd0eecf | 13274 | if (h->start_stop) |
1cce69b9 | 13275 | { |
cbd0eecf | 13276 | asection *s = h->u2.start_stop_section; |
a6a4679f AM |
13277 | *start_stop = !s->gc_mark; |
13278 | return s; | |
1cce69b9 AM |
13279 | } |
13280 | } | |
13281 | ||
5241d853 RS |
13282 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
13283 | } | |
13284 | ||
13285 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
13286 | &cookie->locsyms[r_symndx]); | |
13287 | } | |
13288 | ||
13289 | /* COOKIE->rel describes a relocation against section SEC, which is | |
13290 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 13291 | the relocation symbol. */ |
5241d853 RS |
13292 | |
13293 | bfd_boolean | |
13294 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
13295 | asection *sec, | |
13296 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 13297 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
13298 | { |
13299 | asection *rsec; | |
1cce69b9 | 13300 | bfd_boolean start_stop = FALSE; |
5241d853 | 13301 | |
1cce69b9 AM |
13302 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
13303 | while (rsec != NULL) | |
5241d853 | 13304 | { |
1cce69b9 AM |
13305 | if (!rsec->gc_mark) |
13306 | { | |
13307 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
13308 | || (rsec->owner->flags & DYNAMIC) != 0) | |
13309 | rsec->gc_mark = 1; | |
13310 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
13311 | return FALSE; | |
13312 | } | |
13313 | if (!start_stop) | |
13314 | break; | |
199af150 | 13315 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
13316 | } |
13317 | return TRUE; | |
13318 | } | |
13319 | ||
07adf181 AM |
13320 | /* The mark phase of garbage collection. For a given section, mark |
13321 | it and any sections in this section's group, and all the sections | |
13322 | which define symbols to which it refers. */ | |
13323 | ||
ccfa59ea AM |
13324 | bfd_boolean |
13325 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
13326 | asection *sec, | |
6a5bb875 | 13327 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
13328 | { |
13329 | bfd_boolean ret; | |
9d0a14d3 | 13330 | asection *group_sec, *eh_frame; |
c152c796 AM |
13331 | |
13332 | sec->gc_mark = 1; | |
13333 | ||
13334 | /* Mark all the sections in the group. */ | |
13335 | group_sec = elf_section_data (sec)->next_in_group; | |
13336 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 13337 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
13338 | return FALSE; |
13339 | ||
13340 | /* Look through the section relocs. */ | |
13341 | ret = TRUE; | |
9d0a14d3 RS |
13342 | eh_frame = elf_eh_frame_section (sec->owner); |
13343 | if ((sec->flags & SEC_RELOC) != 0 | |
13344 | && sec->reloc_count > 0 | |
13345 | && sec != eh_frame) | |
c152c796 | 13346 | { |
5241d853 | 13347 | struct elf_reloc_cookie cookie; |
c152c796 | 13348 | |
5241d853 RS |
13349 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
13350 | ret = FALSE; | |
c152c796 | 13351 | else |
c152c796 | 13352 | { |
5241d853 | 13353 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 13354 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
13355 | { |
13356 | ret = FALSE; | |
13357 | break; | |
13358 | } | |
13359 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
13360 | } |
13361 | } | |
9d0a14d3 RS |
13362 | |
13363 | if (ret && eh_frame && elf_fde_list (sec)) | |
13364 | { | |
13365 | struct elf_reloc_cookie cookie; | |
13366 | ||
13367 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
13368 | ret = FALSE; | |
13369 | else | |
13370 | { | |
13371 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
13372 | gc_mark_hook, &cookie)) | |
13373 | ret = FALSE; | |
13374 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
13375 | } | |
13376 | } | |
13377 | ||
2f0c68f2 CM |
13378 | eh_frame = elf_section_eh_frame_entry (sec); |
13379 | if (ret && eh_frame && !eh_frame->gc_mark) | |
13380 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
13381 | ret = FALSE; | |
13382 | ||
c152c796 AM |
13383 | return ret; |
13384 | } | |
13385 | ||
3c758495 TG |
13386 | /* Scan and mark sections in a special or debug section group. */ |
13387 | ||
13388 | static void | |
13389 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
13390 | { | |
13391 | /* Point to first section of section group. */ | |
13392 | asection *ssec; | |
13393 | /* Used to iterate the section group. */ | |
13394 | asection *msec; | |
13395 | ||
13396 | bfd_boolean is_special_grp = TRUE; | |
13397 | bfd_boolean is_debug_grp = TRUE; | |
13398 | ||
13399 | /* First scan to see if group contains any section other than debug | |
13400 | and special section. */ | |
13401 | ssec = msec = elf_next_in_group (grp); | |
13402 | do | |
13403 | { | |
13404 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
13405 | is_debug_grp = FALSE; | |
13406 | ||
13407 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
13408 | is_special_grp = FALSE; | |
13409 | ||
13410 | msec = elf_next_in_group (msec); | |
13411 | } | |
13412 | while (msec != ssec); | |
13413 | ||
13414 | /* If this is a pure debug section group or pure special section group, | |
13415 | keep all sections in this group. */ | |
13416 | if (is_debug_grp || is_special_grp) | |
13417 | { | |
13418 | do | |
13419 | { | |
13420 | msec->gc_mark = 1; | |
13421 | msec = elf_next_in_group (msec); | |
13422 | } | |
13423 | while (msec != ssec); | |
13424 | } | |
13425 | } | |
13426 | ||
7f6ab9f8 AM |
13427 | /* Keep debug and special sections. */ |
13428 | ||
13429 | bfd_boolean | |
13430 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
b7d07216 | 13431 | elf_gc_mark_hook_fn mark_hook) |
7f6ab9f8 AM |
13432 | { |
13433 | bfd *ibfd; | |
13434 | ||
c72f2fb2 | 13435 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
13436 | { |
13437 | asection *isec; | |
13438 | bfd_boolean some_kept; | |
b40bf0a2 | 13439 | bfd_boolean debug_frag_seen; |
b7c871ed | 13440 | bfd_boolean has_kept_debug_info; |
7f6ab9f8 AM |
13441 | |
13442 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13443 | continue; | |
57963c05 AM |
13444 | isec = ibfd->sections; |
13445 | if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13446 | continue; | |
7f6ab9f8 | 13447 | |
b40bf0a2 NC |
13448 | /* Ensure all linker created sections are kept, |
13449 | see if any other section is already marked, | |
13450 | and note if we have any fragmented debug sections. */ | |
b7c871ed | 13451 | debug_frag_seen = some_kept = has_kept_debug_info = FALSE; |
7f6ab9f8 AM |
13452 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
13453 | { | |
13454 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
13455 | isec->gc_mark = 1; | |
eb026f09 AM |
13456 | else if (isec->gc_mark |
13457 | && (isec->flags & SEC_ALLOC) != 0 | |
13458 | && elf_section_type (isec) != SHT_NOTE) | |
7f6ab9f8 | 13459 | some_kept = TRUE; |
b7d07216 L |
13460 | else |
13461 | { | |
13462 | /* Since all sections, except for backend specific ones, | |
13463 | have been garbage collected, call mark_hook on this | |
13464 | section if any of its linked-to sections is marked. */ | |
13465 | asection *linked_to_sec = elf_linked_to_section (isec); | |
13466 | for (; linked_to_sec != NULL; | |
13467 | linked_to_sec = elf_linked_to_section (linked_to_sec)) | |
13468 | if (linked_to_sec->gc_mark) | |
13469 | { | |
13470 | if (!_bfd_elf_gc_mark (info, isec, mark_hook)) | |
13471 | return FALSE; | |
13472 | break; | |
13473 | } | |
13474 | } | |
b40bf0a2 | 13475 | |
535b785f | 13476 | if (!debug_frag_seen |
b40bf0a2 NC |
13477 | && (isec->flags & SEC_DEBUGGING) |
13478 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
13479 | debug_frag_seen = TRUE; | |
5242a0a0 L |
13480 | else if (strcmp (bfd_section_name (isec), |
13481 | "__patchable_function_entries") == 0 | |
13482 | && elf_linked_to_section (isec) == NULL) | |
13483 | info->callbacks->einfo (_("%F%P: %pB(%pA): error: " | |
13484 | "need linked-to section " | |
13485 | "for --gc-sections\n"), | |
13486 | isec->owner, isec); | |
7f6ab9f8 AM |
13487 | } |
13488 | ||
eb026f09 AM |
13489 | /* If no non-note alloc section in this file will be kept, then |
13490 | we can toss out the debug and special sections. */ | |
7f6ab9f8 AM |
13491 | if (!some_kept) |
13492 | continue; | |
13493 | ||
13494 | /* Keep debug and special sections like .comment when they are | |
3c758495 | 13495 | not part of a group. Also keep section groups that contain |
b7d07216 L |
13496 | just debug sections or special sections. NB: Sections with |
13497 | linked-to section has been handled above. */ | |
7f6ab9f8 | 13498 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
13499 | { |
13500 | if ((isec->flags & SEC_GROUP) != 0) | |
13501 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
13502 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
13503 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
b7d07216 L |
13504 | && elf_next_in_group (isec) == NULL |
13505 | && elf_linked_to_section (isec) == NULL) | |
3c758495 | 13506 | isec->gc_mark = 1; |
b7c871ed L |
13507 | if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) |
13508 | has_kept_debug_info = TRUE; | |
3c758495 | 13509 | } |
b40bf0a2 | 13510 | |
b40bf0a2 NC |
13511 | /* Look for CODE sections which are going to be discarded, |
13512 | and find and discard any fragmented debug sections which | |
13513 | are associated with that code section. */ | |
b7c871ed L |
13514 | if (debug_frag_seen) |
13515 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13516 | if ((isec->flags & SEC_CODE) != 0 | |
13517 | && isec->gc_mark == 0) | |
13518 | { | |
13519 | unsigned int ilen; | |
13520 | asection *dsec; | |
b40bf0a2 | 13521 | |
b7c871ed | 13522 | ilen = strlen (isec->name); |
b40bf0a2 | 13523 | |
b7c871ed | 13524 | /* Association is determined by the name of the debug |
07d6d2b8 | 13525 | section containing the name of the code section as |
b7c871ed L |
13526 | a suffix. For example .debug_line.text.foo is a |
13527 | debug section associated with .text.foo. */ | |
13528 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
13529 | { | |
13530 | unsigned int dlen; | |
b40bf0a2 | 13531 | |
b7c871ed L |
13532 | if (dsec->gc_mark == 0 |
13533 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
13534 | continue; | |
b40bf0a2 | 13535 | |
b7c871ed | 13536 | dlen = strlen (dsec->name); |
b40bf0a2 | 13537 | |
b7c871ed L |
13538 | if (dlen > ilen |
13539 | && strncmp (dsec->name + (dlen - ilen), | |
13540 | isec->name, ilen) == 0) | |
b40bf0a2 | 13541 | dsec->gc_mark = 0; |
b7c871ed | 13542 | } |
b40bf0a2 | 13543 | } |
b7c871ed L |
13544 | |
13545 | /* Mark debug sections referenced by kept debug sections. */ | |
13546 | if (has_kept_debug_info) | |
13547 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13548 | if (isec->gc_mark | |
13549 | && (isec->flags & SEC_DEBUGGING) != 0) | |
13550 | if (!_bfd_elf_gc_mark (info, isec, | |
13551 | elf_gc_mark_debug_section)) | |
13552 | return FALSE; | |
7f6ab9f8 AM |
13553 | } |
13554 | return TRUE; | |
13555 | } | |
13556 | ||
c152c796 | 13557 | static bfd_boolean |
ccabcbe5 | 13558 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
13559 | { |
13560 | bfd *sub; | |
ccabcbe5 | 13561 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 13562 | |
c72f2fb2 | 13563 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13564 | { |
13565 | asection *o; | |
13566 | ||
b19a8f85 | 13567 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13568 | || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) |
b19a8f85 | 13569 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 | 13570 | continue; |
57963c05 AM |
13571 | o = sub->sections; |
13572 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13573 | continue; | |
c152c796 AM |
13574 | |
13575 | for (o = sub->sections; o != NULL; o = o->next) | |
13576 | { | |
a33dafc3 L |
13577 | /* When any section in a section group is kept, we keep all |
13578 | sections in the section group. If the first member of | |
13579 | the section group is excluded, we will also exclude the | |
13580 | group section. */ | |
13581 | if (o->flags & SEC_GROUP) | |
13582 | { | |
13583 | asection *first = elf_next_in_group (o); | |
13584 | o->gc_mark = first->gc_mark; | |
13585 | } | |
c152c796 | 13586 | |
1e7eae0d | 13587 | if (o->gc_mark) |
c152c796 AM |
13588 | continue; |
13589 | ||
13590 | /* Skip sweeping sections already excluded. */ | |
13591 | if (o->flags & SEC_EXCLUDE) | |
13592 | continue; | |
13593 | ||
13594 | /* Since this is early in the link process, it is simple | |
13595 | to remove a section from the output. */ | |
13596 | o->flags |= SEC_EXCLUDE; | |
13597 | ||
c55fe096 | 13598 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 13599 | /* xgettext:c-format */ |
9793eb77 | 13600 | _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), |
c08bb8dd | 13601 | o, sub); |
c152c796 AM |
13602 | } |
13603 | } | |
13604 | ||
c152c796 AM |
13605 | return TRUE; |
13606 | } | |
13607 | ||
13608 | /* Propagate collected vtable information. This is called through | |
13609 | elf_link_hash_traverse. */ | |
13610 | ||
13611 | static bfd_boolean | |
13612 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
13613 | { | |
c152c796 | 13614 | /* Those that are not vtables. */ |
cbd0eecf L |
13615 | if (h->start_stop |
13616 | || h->u2.vtable == NULL | |
13617 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13618 | return TRUE; |
13619 | ||
13620 | /* Those vtables that do not have parents, we cannot merge. */ | |
cbd0eecf | 13621 | if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
13622 | return TRUE; |
13623 | ||
13624 | /* If we've already been done, exit. */ | |
cbd0eecf | 13625 | if (h->u2.vtable->used && h->u2.vtable->used[-1]) |
c152c796 AM |
13626 | return TRUE; |
13627 | ||
13628 | /* Make sure the parent's table is up to date. */ | |
cbd0eecf | 13629 | elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); |
c152c796 | 13630 | |
cbd0eecf | 13631 | if (h->u2.vtable->used == NULL) |
c152c796 AM |
13632 | { |
13633 | /* None of this table's entries were referenced. Re-use the | |
13634 | parent's table. */ | |
cbd0eecf L |
13635 | h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; |
13636 | h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; | |
c152c796 AM |
13637 | } |
13638 | else | |
13639 | { | |
13640 | size_t n; | |
13641 | bfd_boolean *cu, *pu; | |
13642 | ||
13643 | /* Or the parent's entries into ours. */ | |
cbd0eecf | 13644 | cu = h->u2.vtable->used; |
c152c796 | 13645 | cu[-1] = TRUE; |
cbd0eecf | 13646 | pu = h->u2.vtable->parent->u2.vtable->used; |
c152c796 AM |
13647 | if (pu != NULL) |
13648 | { | |
13649 | const struct elf_backend_data *bed; | |
13650 | unsigned int log_file_align; | |
13651 | ||
13652 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
13653 | log_file_align = bed->s->log_file_align; | |
cbd0eecf | 13654 | n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; |
c152c796 AM |
13655 | while (n--) |
13656 | { | |
13657 | if (*pu) | |
13658 | *cu = TRUE; | |
13659 | pu++; | |
13660 | cu++; | |
13661 | } | |
13662 | } | |
13663 | } | |
13664 | ||
13665 | return TRUE; | |
13666 | } | |
13667 | ||
13668 | static bfd_boolean | |
13669 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
13670 | { | |
13671 | asection *sec; | |
13672 | bfd_vma hstart, hend; | |
13673 | Elf_Internal_Rela *relstart, *relend, *rel; | |
13674 | const struct elf_backend_data *bed; | |
13675 | unsigned int log_file_align; | |
13676 | ||
c152c796 AM |
13677 | /* Take care of both those symbols that do not describe vtables as |
13678 | well as those that are not loaded. */ | |
cbd0eecf L |
13679 | if (h->start_stop |
13680 | || h->u2.vtable == NULL | |
13681 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13682 | return TRUE; |
13683 | ||
13684 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13685 | || h->root.type == bfd_link_hash_defweak); | |
13686 | ||
13687 | sec = h->root.u.def.section; | |
13688 | hstart = h->root.u.def.value; | |
13689 | hend = hstart + h->size; | |
13690 | ||
13691 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13692 | if (!relstart) | |
13693 | return *(bfd_boolean *) okp = FALSE; | |
13694 | bed = get_elf_backend_data (sec->owner); | |
13695 | log_file_align = bed->s->log_file_align; | |
13696 | ||
056bafd4 | 13697 | relend = relstart + sec->reloc_count; |
c152c796 AM |
13698 | |
13699 | for (rel = relstart; rel < relend; ++rel) | |
13700 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13701 | { | |
13702 | /* If the entry is in use, do nothing. */ | |
cbd0eecf L |
13703 | if (h->u2.vtable->used |
13704 | && (rel->r_offset - hstart) < h->u2.vtable->size) | |
c152c796 AM |
13705 | { |
13706 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
cbd0eecf | 13707 | if (h->u2.vtable->used[entry]) |
c152c796 AM |
13708 | continue; |
13709 | } | |
13710 | /* Otherwise, kill it. */ | |
13711 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13712 | } | |
13713 | ||
13714 | return TRUE; | |
13715 | } | |
13716 | ||
87538722 AM |
13717 | /* Mark sections containing dynamically referenced symbols. When |
13718 | building shared libraries, we must assume that any visible symbol is | |
13719 | referenced. */ | |
715df9b8 | 13720 | |
64d03ab5 AM |
13721 | bfd_boolean |
13722 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13723 | { |
87538722 | 13724 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13725 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13726 | |
715df9b8 EB |
13727 | if ((h->root.type == bfd_link_hash_defined |
13728 | || h->root.type == bfd_link_hash_defweak) | |
d664fd41 | 13729 | && ((h->ref_dynamic && !h->forced_local) |
c4621b33 | 13730 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13731 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13732 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13733 | && (!bfd_link_executable (info) |
22185505 | 13734 | || info->gc_keep_exported |
b407645f AM |
13735 | || info->export_dynamic |
13736 | || (h->dynamic | |
13737 | && d != NULL | |
13738 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13739 | && (h->versioned >= versioned |
54e8959c L |
13740 | || !bfd_hide_sym_by_version (info->version_info, |
13741 | h->root.root.string))))) | |
715df9b8 EB |
13742 | h->root.u.def.section->flags |= SEC_KEEP; |
13743 | ||
13744 | return TRUE; | |
13745 | } | |
3b36f7e6 | 13746 | |
74f0fb50 AM |
13747 | /* Keep all sections containing symbols undefined on the command-line, |
13748 | and the section containing the entry symbol. */ | |
13749 | ||
13750 | void | |
13751 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13752 | { | |
13753 | struct bfd_sym_chain *sym; | |
13754 | ||
13755 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13756 | { | |
13757 | struct elf_link_hash_entry *h; | |
13758 | ||
13759 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13760 | FALSE, FALSE, FALSE); | |
13761 | ||
13762 | if (h != NULL | |
13763 | && (h->root.type == bfd_link_hash_defined | |
13764 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13765 | && !bfd_is_abs_section (h->root.u.def.section) |
13766 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13767 | h->root.u.def.section->flags |= SEC_KEEP; |
13768 | } | |
13769 | } | |
13770 | ||
2f0c68f2 CM |
13771 | bfd_boolean |
13772 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13773 | struct bfd_link_info *info) | |
13774 | { | |
13775 | bfd *ibfd = info->input_bfds; | |
13776 | ||
13777 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13778 | { | |
13779 | asection *sec; | |
13780 | struct elf_reloc_cookie cookie; | |
13781 | ||
13782 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13783 | continue; | |
57963c05 AM |
13784 | sec = ibfd->sections; |
13785 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13786 | continue; | |
2f0c68f2 CM |
13787 | |
13788 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13789 | return FALSE; | |
13790 | ||
13791 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13792 | { | |
fd361982 | 13793 | if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry") |
2f0c68f2 CM |
13794 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) |
13795 | { | |
13796 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13797 | fini_reloc_cookie_rels (&cookie, sec); | |
13798 | } | |
13799 | } | |
13800 | } | |
13801 | return TRUE; | |
13802 | } | |
13803 | ||
c152c796 AM |
13804 | /* Do mark and sweep of unused sections. */ |
13805 | ||
13806 | bfd_boolean | |
13807 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13808 | { | |
13809 | bfd_boolean ok = TRUE; | |
13810 | bfd *sub; | |
6a5bb875 | 13811 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13812 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13813 | struct elf_link_hash_table *htab; |
c152c796 | 13814 | |
64d03ab5 | 13815 | if (!bed->can_gc_sections |
715df9b8 | 13816 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13817 | { |
9793eb77 | 13818 | _bfd_error_handler(_("warning: gc-sections option ignored")); |
c152c796 AM |
13819 | return TRUE; |
13820 | } | |
13821 | ||
74f0fb50 | 13822 | bed->gc_keep (info); |
da44f4e5 | 13823 | htab = elf_hash_table (info); |
74f0fb50 | 13824 | |
9d0a14d3 RS |
13825 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13826 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13827 | for (sub = info->input_bfds; |
13828 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13829 | sub = sub->link.next) | |
9d0a14d3 RS |
13830 | { |
13831 | asection *sec; | |
13832 | struct elf_reloc_cookie cookie; | |
13833 | ||
57963c05 AM |
13834 | sec = sub->sections; |
13835 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13836 | continue; | |
9d0a14d3 | 13837 | sec = bfd_get_section_by_name (sub, ".eh_frame"); |
9a2a56cc | 13838 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13839 | { |
13840 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13841 | if (elf_section_data (sec)->sec_info |
13842 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13843 | elf_eh_frame_section (sub) = sec; |
13844 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13845 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13846 | } |
13847 | } | |
9d0a14d3 | 13848 | |
c152c796 | 13849 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13850 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13851 | if (!ok) |
13852 | return FALSE; | |
13853 | ||
13854 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13855 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13856 | if (!ok) |
13857 | return FALSE; | |
13858 | ||
715df9b8 | 13859 | /* Mark dynamically referenced symbols. */ |
22185505 | 13860 | if (htab->dynamic_sections_created || info->gc_keep_exported) |
da44f4e5 | 13861 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); |
c152c796 | 13862 | |
715df9b8 | 13863 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13864 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13865 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13866 | { |
13867 | asection *o; | |
13868 | ||
b19a8f85 | 13869 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13870 | || elf_object_id (sub) != elf_hash_table_id (htab) |
b19a8f85 | 13871 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 AM |
13872 | continue; |
13873 | ||
57963c05 AM |
13874 | o = sub->sections; |
13875 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13876 | continue; | |
13877 | ||
7f6ab9f8 AM |
13878 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13879 | Also treat note sections as a root, if the section is not part | |
8b6f4cd3 L |
13880 | of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as |
13881 | well as FINI_ARRAY sections for ld -r. */ | |
c152c796 | 13882 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13883 | if (!o->gc_mark |
13884 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13885 | && ((o->flags & SEC_KEEP) != 0 |
8b6f4cd3 L |
13886 | || (bfd_link_relocatable (info) |
13887 | && ((elf_section_data (o)->this_hdr.sh_type | |
13888 | == SHT_PREINIT_ARRAY) | |
13889 | || (elf_section_data (o)->this_hdr.sh_type | |
13890 | == SHT_INIT_ARRAY) | |
13891 | || (elf_section_data (o)->this_hdr.sh_type | |
13892 | == SHT_FINI_ARRAY))) | |
7f6ab9f8 AM |
13893 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13894 | && elf_next_in_group (o) == NULL ))) | |
13895 | { | |
13896 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13897 | return FALSE; | |
13898 | } | |
c152c796 AM |
13899 | } |
13900 | ||
6a5bb875 | 13901 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13902 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13903 | |
c152c796 | 13904 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13905 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13906 | } |
13907 | \f | |
13908 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13909 | ||
13910 | bfd_boolean | |
13911 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13912 | asection *sec, | |
13913 | struct elf_link_hash_entry *h, | |
13914 | bfd_vma offset) | |
13915 | { | |
13916 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13917 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13918 | size_t extsymcount; |
c152c796 AM |
13919 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13920 | ||
13921 | /* The sh_info field of the symtab header tells us where the | |
13922 | external symbols start. We don't care about the local symbols at | |
13923 | this point. */ | |
13924 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13925 | if (!elf_bad_symtab (abfd)) | |
13926 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13927 | ||
13928 | sym_hashes = elf_sym_hashes (abfd); | |
13929 | sym_hashes_end = sym_hashes + extsymcount; | |
13930 | ||
13931 | /* Hunt down the child symbol, which is in this section at the same | |
13932 | offset as the relocation. */ | |
13933 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13934 | { | |
13935 | if ((child = *search) != NULL | |
13936 | && (child->root.type == bfd_link_hash_defined | |
13937 | || child->root.type == bfd_link_hash_defweak) | |
13938 | && child->root.u.def.section == sec | |
13939 | && child->root.u.def.value == offset) | |
13940 | goto win; | |
13941 | } | |
13942 | ||
695344c0 | 13943 | /* xgettext:c-format */ |
9793eb77 | 13944 | _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"), |
2dcf00ce | 13945 | abfd, sec, (uint64_t) offset); |
c152c796 AM |
13946 | bfd_set_error (bfd_error_invalid_operation); |
13947 | return FALSE; | |
13948 | ||
13949 | win: | |
cbd0eecf | 13950 | if (!child->u2.vtable) |
f6e332e6 | 13951 | { |
cbd0eecf L |
13952 | child->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13953 | bfd_zalloc (abfd, sizeof (*child->u2.vtable))); | |
13954 | if (!child->u2.vtable) | |
f6e332e6 AM |
13955 | return FALSE; |
13956 | } | |
c152c796 AM |
13957 | if (!h) |
13958 | { | |
13959 | /* This *should* only be the absolute section. It could potentially | |
13960 | be that someone has defined a non-global vtable though, which | |
13961 | would be bad. It isn't worth paging in the local symbols to be | |
13962 | sure though; that case should simply be handled by the assembler. */ | |
13963 | ||
cbd0eecf | 13964 | child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13965 | } |
13966 | else | |
cbd0eecf | 13967 | child->u2.vtable->parent = h; |
c152c796 AM |
13968 | |
13969 | return TRUE; | |
13970 | } | |
13971 | ||
13972 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13973 | ||
13974 | bfd_boolean | |
a0ea3a14 | 13975 | bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, |
c152c796 AM |
13976 | struct elf_link_hash_entry *h, |
13977 | bfd_vma addend) | |
13978 | { | |
13979 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13980 | unsigned int log_file_align = bed->s->log_file_align; | |
13981 | ||
a0ea3a14 L |
13982 | if (!h) |
13983 | { | |
13984 | /* xgettext:c-format */ | |
13985 | _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"), | |
13986 | abfd, sec); | |
13987 | bfd_set_error (bfd_error_bad_value); | |
13988 | return FALSE; | |
13989 | } | |
13990 | ||
cbd0eecf | 13991 | if (!h->u2.vtable) |
f6e332e6 | 13992 | { |
cbd0eecf L |
13993 | h->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13994 | bfd_zalloc (abfd, sizeof (*h->u2.vtable))); | |
13995 | if (!h->u2.vtable) | |
f6e332e6 AM |
13996 | return FALSE; |
13997 | } | |
13998 | ||
cbd0eecf | 13999 | if (addend >= h->u2.vtable->size) |
c152c796 AM |
14000 | { |
14001 | size_t size, bytes, file_align; | |
cbd0eecf | 14002 | bfd_boolean *ptr = h->u2.vtable->used; |
c152c796 AM |
14003 | |
14004 | /* While the symbol is undefined, we have to be prepared to handle | |
14005 | a zero size. */ | |
14006 | file_align = 1 << log_file_align; | |
14007 | if (h->root.type == bfd_link_hash_undefined) | |
14008 | size = addend + file_align; | |
14009 | else | |
14010 | { | |
14011 | size = h->size; | |
14012 | if (addend >= size) | |
14013 | { | |
14014 | /* Oops! We've got a reference past the defined end of | |
14015 | the table. This is probably a bug -- shall we warn? */ | |
14016 | size = addend + file_align; | |
14017 | } | |
14018 | } | |
14019 | size = (size + file_align - 1) & -file_align; | |
14020 | ||
14021 | /* Allocate one extra entry for use as a "done" flag for the | |
14022 | consolidation pass. */ | |
14023 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
14024 | ||
14025 | if (ptr) | |
14026 | { | |
a50b1753 | 14027 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
14028 | |
14029 | if (ptr != NULL) | |
14030 | { | |
14031 | size_t oldbytes; | |
14032 | ||
cbd0eecf | 14033 | oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) |
c152c796 AM |
14034 | * sizeof (bfd_boolean)); |
14035 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
14036 | } | |
14037 | } | |
14038 | else | |
a50b1753 | 14039 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
14040 | |
14041 | if (ptr == NULL) | |
14042 | return FALSE; | |
14043 | ||
14044 | /* And arrange for that done flag to be at index -1. */ | |
cbd0eecf L |
14045 | h->u2.vtable->used = ptr + 1; |
14046 | h->u2.vtable->size = size; | |
c152c796 AM |
14047 | } |
14048 | ||
cbd0eecf | 14049 | h->u2.vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
14050 | |
14051 | return TRUE; | |
14052 | } | |
14053 | ||
ae17ab41 CM |
14054 | /* Map an ELF section header flag to its corresponding string. */ |
14055 | typedef struct | |
14056 | { | |
14057 | char *flag_name; | |
14058 | flagword flag_value; | |
14059 | } elf_flags_to_name_table; | |
14060 | ||
14061 | static elf_flags_to_name_table elf_flags_to_names [] = | |
14062 | { | |
14063 | { "SHF_WRITE", SHF_WRITE }, | |
14064 | { "SHF_ALLOC", SHF_ALLOC }, | |
14065 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
14066 | { "SHF_MERGE", SHF_MERGE }, | |
14067 | { "SHF_STRINGS", SHF_STRINGS }, | |
14068 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
14069 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
14070 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
14071 | { "SHF_GROUP", SHF_GROUP }, | |
14072 | { "SHF_TLS", SHF_TLS }, | |
14073 | { "SHF_MASKOS", SHF_MASKOS }, | |
14074 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
14075 | }; | |
14076 | ||
b9c361e0 JL |
14077 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
14078 | bfd_boolean | |
ae17ab41 | 14079 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 14080 | struct flag_info *flaginfo, |
b9c361e0 | 14081 | asection *section) |
ae17ab41 | 14082 | { |
8b127cbc | 14083 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 14084 | |
8b127cbc | 14085 | if (!flaginfo->flags_initialized) |
ae17ab41 | 14086 | { |
8b127cbc AM |
14087 | bfd *obfd = info->output_bfd; |
14088 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
14089 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
14090 | int with_hex = 0; |
14091 | int without_hex = 0; | |
14092 | ||
8b127cbc | 14093 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 14094 | { |
b9c361e0 | 14095 | unsigned i; |
8b127cbc | 14096 | flagword (*lookup) (char *); |
ae17ab41 | 14097 | |
8b127cbc AM |
14098 | lookup = bed->elf_backend_lookup_section_flags_hook; |
14099 | if (lookup != NULL) | |
ae17ab41 | 14100 | { |
8b127cbc | 14101 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
14102 | |
14103 | if (hexval != 0) | |
14104 | { | |
14105 | if (tf->with == with_flags) | |
14106 | with_hex |= hexval; | |
14107 | else if (tf->with == without_flags) | |
14108 | without_hex |= hexval; | |
14109 | tf->valid = TRUE; | |
14110 | continue; | |
14111 | } | |
ae17ab41 | 14112 | } |
8b127cbc | 14113 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 14114 | { |
8b127cbc | 14115 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
14116 | { |
14117 | if (tf->with == with_flags) | |
14118 | with_hex |= elf_flags_to_names[i].flag_value; | |
14119 | else if (tf->with == without_flags) | |
14120 | without_hex |= elf_flags_to_names[i].flag_value; | |
14121 | tf->valid = TRUE; | |
14122 | break; | |
14123 | } | |
14124 | } | |
8b127cbc | 14125 | if (!tf->valid) |
b9c361e0 | 14126 | { |
68ffbac6 | 14127 | info->callbacks->einfo |
9793eb77 | 14128 | (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 14129 | return FALSE; |
ae17ab41 CM |
14130 | } |
14131 | } | |
8b127cbc AM |
14132 | flaginfo->flags_initialized = TRUE; |
14133 | flaginfo->only_with_flags |= with_hex; | |
14134 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 14135 | } |
ae17ab41 | 14136 | |
8b127cbc | 14137 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
14138 | return FALSE; |
14139 | ||
8b127cbc | 14140 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
14141 | return FALSE; |
14142 | ||
14143 | return TRUE; | |
ae17ab41 CM |
14144 | } |
14145 | ||
c152c796 AM |
14146 | struct alloc_got_off_arg { |
14147 | bfd_vma gotoff; | |
10455f89 | 14148 | struct bfd_link_info *info; |
c152c796 AM |
14149 | }; |
14150 | ||
14151 | /* We need a special top-level link routine to convert got reference counts | |
14152 | to real got offsets. */ | |
14153 | ||
14154 | static bfd_boolean | |
14155 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
14156 | { | |
a50b1753 | 14157 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
14158 | bfd *obfd = gofarg->info->output_bfd; |
14159 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 14160 | |
c152c796 AM |
14161 | if (h->got.refcount > 0) |
14162 | { | |
14163 | h->got.offset = gofarg->gotoff; | |
10455f89 | 14164 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
14165 | } |
14166 | else | |
14167 | h->got.offset = (bfd_vma) -1; | |
14168 | ||
14169 | return TRUE; | |
14170 | } | |
14171 | ||
14172 | /* And an accompanying bit to work out final got entry offsets once | |
14173 | we're done. Should be called from final_link. */ | |
14174 | ||
14175 | bfd_boolean | |
14176 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
14177 | struct bfd_link_info *info) | |
14178 | { | |
14179 | bfd *i; | |
14180 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14181 | bfd_vma gotoff; | |
c152c796 AM |
14182 | struct alloc_got_off_arg gofarg; |
14183 | ||
10455f89 HPN |
14184 | BFD_ASSERT (abfd == info->output_bfd); |
14185 | ||
c152c796 AM |
14186 | if (! is_elf_hash_table (info->hash)) |
14187 | return FALSE; | |
14188 | ||
14189 | /* The GOT offset is relative to the .got section, but the GOT header is | |
14190 | put into the .got.plt section, if the backend uses it. */ | |
14191 | if (bed->want_got_plt) | |
14192 | gotoff = 0; | |
14193 | else | |
14194 | gotoff = bed->got_header_size; | |
14195 | ||
14196 | /* Do the local .got entries first. */ | |
c72f2fb2 | 14197 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
14198 | { |
14199 | bfd_signed_vma *local_got; | |
ef53be89 | 14200 | size_t j, locsymcount; |
c152c796 AM |
14201 | Elf_Internal_Shdr *symtab_hdr; |
14202 | ||
14203 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
14204 | continue; | |
14205 | ||
14206 | local_got = elf_local_got_refcounts (i); | |
14207 | if (!local_got) | |
14208 | continue; | |
14209 | ||
14210 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
14211 | if (elf_bad_symtab (i)) | |
14212 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
14213 | else | |
14214 | locsymcount = symtab_hdr->sh_info; | |
14215 | ||
14216 | for (j = 0; j < locsymcount; ++j) | |
14217 | { | |
14218 | if (local_got[j] > 0) | |
14219 | { | |
14220 | local_got[j] = gotoff; | |
10455f89 | 14221 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
14222 | } |
14223 | else | |
14224 | local_got[j] = (bfd_vma) -1; | |
14225 | } | |
14226 | } | |
14227 | ||
14228 | /* Then the global .got entries. .plt refcounts are handled by | |
14229 | adjust_dynamic_symbol */ | |
14230 | gofarg.gotoff = gotoff; | |
10455f89 | 14231 | gofarg.info = info; |
c152c796 AM |
14232 | elf_link_hash_traverse (elf_hash_table (info), |
14233 | elf_gc_allocate_got_offsets, | |
14234 | &gofarg); | |
14235 | return TRUE; | |
14236 | } | |
14237 | ||
14238 | /* Many folk need no more in the way of final link than this, once | |
14239 | got entry reference counting is enabled. */ | |
14240 | ||
14241 | bfd_boolean | |
14242 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
14243 | { | |
14244 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
14245 | return FALSE; | |
14246 | ||
14247 | /* Invoke the regular ELF backend linker to do all the work. */ | |
14248 | return bfd_elf_final_link (abfd, info); | |
14249 | } | |
14250 | ||
14251 | bfd_boolean | |
14252 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
14253 | { | |
a50b1753 | 14254 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
14255 | |
14256 | if (rcookie->bad_symtab) | |
14257 | rcookie->rel = rcookie->rels; | |
14258 | ||
14259 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
14260 | { | |
14261 | unsigned long r_symndx; | |
14262 | ||
14263 | if (! rcookie->bad_symtab) | |
14264 | if (rcookie->rel->r_offset > offset) | |
14265 | return FALSE; | |
14266 | if (rcookie->rel->r_offset != offset) | |
14267 | continue; | |
14268 | ||
14269 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 14270 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
14271 | return TRUE; |
14272 | ||
14273 | if (r_symndx >= rcookie->locsymcount | |
14274 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
14275 | { | |
14276 | struct elf_link_hash_entry *h; | |
14277 | ||
14278 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
14279 | ||
14280 | while (h->root.type == bfd_link_hash_indirect | |
14281 | || h->root.type == bfd_link_hash_warning) | |
14282 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
14283 | ||
14284 | if ((h->root.type == bfd_link_hash_defined | |
14285 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
14286 | && (h->root.u.def.section->owner != rcookie->abfd |
14287 | || h->root.u.def.section->kept_section != NULL | |
14288 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 14289 | return TRUE; |
c152c796 AM |
14290 | } |
14291 | else | |
14292 | { | |
14293 | /* It's not a relocation against a global symbol, | |
14294 | but it could be a relocation against a local | |
14295 | symbol for a discarded section. */ | |
14296 | asection *isec; | |
14297 | Elf_Internal_Sym *isym; | |
14298 | ||
14299 | /* Need to: get the symbol; get the section. */ | |
14300 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 14301 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
14302 | if (isec != NULL |
14303 | && (isec->kept_section != NULL | |
14304 | || discarded_section (isec))) | |
cb33740c | 14305 | return TRUE; |
c152c796 AM |
14306 | } |
14307 | return FALSE; | |
14308 | } | |
14309 | return FALSE; | |
14310 | } | |
14311 | ||
14312 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
14313 | Returns -1 on error, 1 if any section's size was changed, 0 if |
14314 | nothing changed. This function assumes that the relocations are in | |
14315 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 14316 | |
75938853 | 14317 | int |
c152c796 AM |
14318 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
14319 | { | |
14320 | struct elf_reloc_cookie cookie; | |
18cd5bce | 14321 | asection *o; |
c152c796 | 14322 | bfd *abfd; |
75938853 | 14323 | int changed = 0; |
c152c796 AM |
14324 | |
14325 | if (info->traditional_format | |
14326 | || !is_elf_hash_table (info->hash)) | |
75938853 | 14327 | return 0; |
c152c796 | 14328 | |
18cd5bce AM |
14329 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
14330 | if (o != NULL) | |
c152c796 | 14331 | { |
18cd5bce | 14332 | asection *i; |
c152c796 | 14333 | |
18cd5bce | 14334 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 14335 | { |
18cd5bce AM |
14336 | if (i->size == 0 |
14337 | || i->reloc_count == 0 | |
14338 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
14339 | continue; | |
c152c796 | 14340 | |
18cd5bce AM |
14341 | abfd = i->owner; |
14342 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14343 | continue; | |
c152c796 | 14344 | |
18cd5bce | 14345 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 14346 | return -1; |
c152c796 | 14347 | |
18cd5bce AM |
14348 | if (_bfd_discard_section_stabs (abfd, i, |
14349 | elf_section_data (i)->sec_info, | |
5241d853 RS |
14350 | bfd_elf_reloc_symbol_deleted_p, |
14351 | &cookie)) | |
75938853 | 14352 | changed = 1; |
18cd5bce AM |
14353 | |
14354 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14355 | } |
18cd5bce AM |
14356 | } |
14357 | ||
2f0c68f2 CM |
14358 | o = NULL; |
14359 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
14360 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
14361 | if (o != NULL) |
14362 | { | |
14363 | asection *i; | |
d7153c4a | 14364 | int eh_changed = 0; |
66631823 | 14365 | unsigned int eh_alignment; /* Octets. */ |
c152c796 | 14366 | |
18cd5bce | 14367 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 14368 | { |
18cd5bce AM |
14369 | if (i->size == 0) |
14370 | continue; | |
14371 | ||
14372 | abfd = i->owner; | |
14373 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14374 | continue; | |
14375 | ||
14376 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 14377 | return -1; |
18cd5bce AM |
14378 | |
14379 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
14380 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
14381 | bfd_elf_reloc_symbol_deleted_p, |
14382 | &cookie)) | |
d7153c4a AM |
14383 | { |
14384 | eh_changed = 1; | |
14385 | if (i->size != i->rawsize) | |
14386 | changed = 1; | |
14387 | } | |
18cd5bce AM |
14388 | |
14389 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14390 | } |
9866ffe2 | 14391 | |
66631823 CE |
14392 | eh_alignment = ((1 << o->alignment_power) |
14393 | * bfd_octets_per_byte (output_bfd, o)); | |
9866ffe2 AM |
14394 | /* Skip over zero terminator, and prevent empty sections from |
14395 | adding alignment padding at the end. */ | |
14396 | for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) | |
14397 | if (i->size == 0) | |
14398 | i->flags |= SEC_EXCLUDE; | |
14399 | else if (i->size > 4) | |
14400 | break; | |
14401 | /* The last non-empty eh_frame section doesn't need padding. */ | |
14402 | if (i != NULL) | |
14403 | i = i->map_tail.s; | |
14404 | /* Any prior sections must pad the last FDE out to the output | |
14405 | section alignment. Otherwise we might have zero padding | |
14406 | between sections, which would be seen as a terminator. */ | |
14407 | for (; i != NULL; i = i->map_tail.s) | |
14408 | if (i->size == 4) | |
14409 | /* All but the last zero terminator should have been removed. */ | |
14410 | BFD_FAIL (); | |
14411 | else | |
14412 | { | |
14413 | bfd_size_type size | |
14414 | = (i->size + eh_alignment - 1) & -eh_alignment; | |
14415 | if (i->size != size) | |
af471f82 | 14416 | { |
9866ffe2 AM |
14417 | i->size = size; |
14418 | changed = 1; | |
14419 | eh_changed = 1; | |
af471f82 | 14420 | } |
9866ffe2 | 14421 | } |
d7153c4a AM |
14422 | if (eh_changed) |
14423 | elf_link_hash_traverse (elf_hash_table (info), | |
14424 | _bfd_elf_adjust_eh_frame_global_symbol, NULL); | |
18cd5bce | 14425 | } |
c152c796 | 14426 | |
18cd5bce AM |
14427 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
14428 | { | |
14429 | const struct elf_backend_data *bed; | |
57963c05 | 14430 | asection *s; |
c152c796 | 14431 | |
18cd5bce AM |
14432 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
14433 | continue; | |
57963c05 AM |
14434 | s = abfd->sections; |
14435 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14436 | continue; | |
18cd5bce AM |
14437 | |
14438 | bed = get_elf_backend_data (abfd); | |
14439 | ||
14440 | if (bed->elf_backend_discard_info != NULL) | |
14441 | { | |
14442 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 14443 | return -1; |
18cd5bce AM |
14444 | |
14445 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 14446 | changed = 1; |
18cd5bce AM |
14447 | |
14448 | fini_reloc_cookie (&cookie, abfd); | |
14449 | } | |
c152c796 AM |
14450 | } |
14451 | ||
2f0c68f2 CM |
14452 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
14453 | _bfd_elf_end_eh_frame_parsing (info); | |
14454 | ||
14455 | if (info->eh_frame_hdr_type | |
0e1862bb | 14456 | && !bfd_link_relocatable (info) |
c152c796 | 14457 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 14458 | changed = 1; |
c152c796 | 14459 | |
75938853 | 14460 | return changed; |
c152c796 | 14461 | } |
082b7297 | 14462 | |
43e1669b | 14463 | bfd_boolean |
0c511000 | 14464 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 14465 | asection *sec, |
c0f00686 | 14466 | struct bfd_link_info *info) |
082b7297 L |
14467 | { |
14468 | flagword flags; | |
c77ec726 | 14469 | const char *name, *key; |
082b7297 L |
14470 | struct bfd_section_already_linked *l; |
14471 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 14472 | |
c77ec726 AM |
14473 | if (sec->output_section == bfd_abs_section_ptr) |
14474 | return FALSE; | |
0c511000 | 14475 | |
c77ec726 | 14476 | flags = sec->flags; |
0c511000 | 14477 | |
c77ec726 AM |
14478 | /* Return if it isn't a linkonce section. A comdat group section |
14479 | also has SEC_LINK_ONCE set. */ | |
14480 | if ((flags & SEC_LINK_ONCE) == 0) | |
14481 | return FALSE; | |
0c511000 | 14482 | |
c77ec726 AM |
14483 | /* Don't put group member sections on our list of already linked |
14484 | sections. They are handled as a group via their group section. */ | |
14485 | if (elf_sec_group (sec) != NULL) | |
14486 | return FALSE; | |
0c511000 | 14487 | |
c77ec726 AM |
14488 | /* For a SHT_GROUP section, use the group signature as the key. */ |
14489 | name = sec->name; | |
14490 | if ((flags & SEC_GROUP) != 0 | |
14491 | && elf_next_in_group (sec) != NULL | |
14492 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
14493 | key = elf_group_name (elf_next_in_group (sec)); | |
14494 | else | |
14495 | { | |
14496 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 14497 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
14498 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
14499 | key++; | |
0c511000 | 14500 | else |
c77ec726 AM |
14501 | /* Must be a user linkonce section that doesn't follow gcc's |
14502 | naming convention. In this case we won't be matching | |
14503 | single member groups. */ | |
14504 | key = name; | |
0c511000 | 14505 | } |
6d2cd210 | 14506 | |
c77ec726 | 14507 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
14508 | |
14509 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14510 | { | |
c2370991 | 14511 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
14512 | sections with a signature of <key> (<key> is some string), |
14513 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
14514 | Match like sections. LTO plugin sections are an exception. | |
14515 | They are always named .gnu.linkonce.t.<key> and match either | |
14516 | type of section. */ | |
14517 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
14518 | && ((flags & SEC_GROUP) != 0 | |
14519 | || strcmp (name, l->sec->name) == 0)) | |
14520 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
14521 | { |
14522 | /* The section has already been linked. See if we should | |
6d2cd210 | 14523 | issue a warning. */ |
c77ec726 AM |
14524 | if (!_bfd_handle_already_linked (sec, l, info)) |
14525 | return FALSE; | |
082b7297 | 14526 | |
c77ec726 | 14527 | if (flags & SEC_GROUP) |
3d7f7666 | 14528 | { |
c77ec726 AM |
14529 | asection *first = elf_next_in_group (sec); |
14530 | asection *s = first; | |
3d7f7666 | 14531 | |
c77ec726 | 14532 | while (s != NULL) |
3d7f7666 | 14533 | { |
c77ec726 AM |
14534 | s->output_section = bfd_abs_section_ptr; |
14535 | /* Record which group discards it. */ | |
14536 | s->kept_section = l->sec; | |
14537 | s = elf_next_in_group (s); | |
14538 | /* These lists are circular. */ | |
14539 | if (s == first) | |
14540 | break; | |
3d7f7666 L |
14541 | } |
14542 | } | |
082b7297 | 14543 | |
43e1669b | 14544 | return TRUE; |
082b7297 L |
14545 | } |
14546 | } | |
14547 | ||
c77ec726 AM |
14548 | /* A single member comdat group section may be discarded by a |
14549 | linkonce section and vice versa. */ | |
14550 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 14551 | { |
c77ec726 | 14552 | asection *first = elf_next_in_group (sec); |
c2370991 | 14553 | |
c77ec726 AM |
14554 | if (first != NULL && elf_next_in_group (first) == first) |
14555 | /* Check this single member group against linkonce sections. */ | |
14556 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14557 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14558 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
14559 | { | |
14560 | first->output_section = bfd_abs_section_ptr; | |
14561 | first->kept_section = l->sec; | |
14562 | sec->output_section = bfd_abs_section_ptr; | |
14563 | break; | |
14564 | } | |
14565 | } | |
14566 | else | |
14567 | /* Check this linkonce section against single member groups. */ | |
14568 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14569 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 14570 | { |
c77ec726 | 14571 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 14572 | |
c77ec726 AM |
14573 | if (first != NULL |
14574 | && elf_next_in_group (first) == first | |
14575 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
14576 | { | |
14577 | sec->output_section = bfd_abs_section_ptr; | |
14578 | sec->kept_section = first; | |
14579 | break; | |
14580 | } | |
6d2cd210 | 14581 | } |
0c511000 | 14582 | |
c77ec726 AM |
14583 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
14584 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
14585 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
14586 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
14587 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
14588 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
14589 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
14590 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
14591 | The reverse order cannot happen as there is never a bfd with only the | |
14592 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
14593 | matter as here were are looking only for cross-bfd sections. */ | |
14594 | ||
14595 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
14596 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14597 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14598 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
14599 | { | |
14600 | if (abfd != l->sec->owner) | |
14601 | sec->output_section = bfd_abs_section_ptr; | |
14602 | break; | |
14603 | } | |
80c29487 | 14604 | |
082b7297 | 14605 | /* This is the first section with this name. Record it. */ |
c77ec726 | 14606 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 14607 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 14608 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 14609 | } |
81e1b023 | 14610 | |
a4d8e49b L |
14611 | bfd_boolean |
14612 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
14613 | { | |
14614 | return sym->st_shndx == SHN_COMMON; | |
14615 | } | |
14616 | ||
14617 | unsigned int | |
14618 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
14619 | { | |
14620 | return SHN_COMMON; | |
14621 | } | |
14622 | ||
14623 | asection * | |
14624 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
14625 | { | |
14626 | return bfd_com_section_ptr; | |
14627 | } | |
10455f89 HPN |
14628 | |
14629 | bfd_vma | |
14630 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
14631 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
14632 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
14633 | bfd *ibfd ATTRIBUTE_UNUSED, | |
14634 | unsigned long symndx ATTRIBUTE_UNUSED) | |
14635 | { | |
14636 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14637 | return bed->s->arch_size / 8; | |
14638 | } | |
83bac4b0 NC |
14639 | |
14640 | /* Routines to support the creation of dynamic relocs. */ | |
14641 | ||
83bac4b0 NC |
14642 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
14643 | ||
14644 | static const char * | |
14645 | get_dynamic_reloc_section_name (bfd * abfd, | |
14646 | asection * sec, | |
14647 | bfd_boolean is_rela) | |
14648 | { | |
ddcf1fcf | 14649 | char *name; |
fd361982 | 14650 | const char *old_name = bfd_section_name (sec); |
ddcf1fcf | 14651 | const char *prefix = is_rela ? ".rela" : ".rel"; |
83bac4b0 | 14652 | |
ddcf1fcf | 14653 | if (old_name == NULL) |
83bac4b0 NC |
14654 | return NULL; |
14655 | ||
ddcf1fcf | 14656 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 14657 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
14658 | |
14659 | return name; | |
14660 | } | |
14661 | ||
14662 | /* Returns the dynamic reloc section associated with SEC. | |
14663 | If necessary compute the name of the dynamic reloc section based | |
14664 | on SEC's name (looked up in ABFD's string table) and the setting | |
14665 | of IS_RELA. */ | |
14666 | ||
14667 | asection * | |
14668 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
14669 | asection * sec, | |
14670 | bfd_boolean is_rela) | |
14671 | { | |
14672 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14673 | ||
14674 | if (reloc_sec == NULL) | |
14675 | { | |
14676 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14677 | ||
14678 | if (name != NULL) | |
14679 | { | |
3d4d4302 | 14680 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
14681 | |
14682 | if (reloc_sec != NULL) | |
14683 | elf_section_data (sec)->sreloc = reloc_sec; | |
14684 | } | |
14685 | } | |
14686 | ||
14687 | return reloc_sec; | |
14688 | } | |
14689 | ||
14690 | /* Returns the dynamic reloc section associated with SEC. If the | |
14691 | section does not exist it is created and attached to the DYNOBJ | |
14692 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
14693 | structure. | |
f8076f98 | 14694 | |
83bac4b0 NC |
14695 | ALIGNMENT is the alignment for the newly created section and |
14696 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
14697 | or .rel.<SEC's name>. The section name is looked up in the | |
14698 | string table associated with ABFD. */ | |
14699 | ||
14700 | asection * | |
ca4be51c AM |
14701 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
14702 | bfd *dynobj, | |
14703 | unsigned int alignment, | |
14704 | bfd *abfd, | |
14705 | bfd_boolean is_rela) | |
83bac4b0 NC |
14706 | { |
14707 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14708 | ||
14709 | if (reloc_sec == NULL) | |
14710 | { | |
14711 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14712 | ||
14713 | if (name == NULL) | |
14714 | return NULL; | |
14715 | ||
3d4d4302 | 14716 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
14717 | |
14718 | if (reloc_sec == NULL) | |
14719 | { | |
3d4d4302 AM |
14720 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
14721 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
14722 | if ((sec->flags & SEC_ALLOC) != 0) |
14723 | flags |= SEC_ALLOC | SEC_LOAD; | |
14724 | ||
3d4d4302 | 14725 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
14726 | if (reloc_sec != NULL) |
14727 | { | |
8877b5e5 AM |
14728 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
14729 | name. Override as it may be wrong, eg. for a user | |
14730 | section named "auto" we'll get ".relauto" which is | |
14731 | seen to be a .rela section. */ | |
14732 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
fd361982 | 14733 | if (!bfd_set_section_alignment (reloc_sec, alignment)) |
83bac4b0 NC |
14734 | reloc_sec = NULL; |
14735 | } | |
14736 | } | |
14737 | ||
14738 | elf_section_data (sec)->sreloc = reloc_sec; | |
14739 | } | |
14740 | ||
14741 | return reloc_sec; | |
14742 | } | |
1338dd10 | 14743 | |
bffebb6b AM |
14744 | /* Copy the ELF symbol type and other attributes for a linker script |
14745 | assignment from HSRC to HDEST. Generally this should be treated as | |
14746 | if we found a strong non-dynamic definition for HDEST (except that | |
14747 | ld ignores multiple definition errors). */ | |
1338dd10 | 14748 | void |
bffebb6b AM |
14749 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
14750 | struct bfd_link_hash_entry *hdest, | |
14751 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 14752 | { |
bffebb6b AM |
14753 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
14754 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14755 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14756 | |
14757 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14758 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14759 | |
14760 | isym.st_other = ehsrc->other; | |
b8417128 | 14761 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14762 | } |
351f65ca L |
14763 | |
14764 | /* Append a RELA relocation REL to section S in BFD. */ | |
14765 | ||
14766 | void | |
14767 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14768 | { | |
14769 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14770 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14771 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14772 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14773 | } | |
14774 | ||
14775 | /* Append a REL relocation REL to section S in BFD. */ | |
14776 | ||
14777 | void | |
14778 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14779 | { | |
14780 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14781 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14782 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14783 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14784 | } |
7dba9362 AM |
14785 | |
14786 | /* Define __start, __stop, .startof. or .sizeof. symbol. */ | |
14787 | ||
14788 | struct bfd_link_hash_entry * | |
14789 | bfd_elf_define_start_stop (struct bfd_link_info *info, | |
14790 | const char *symbol, asection *sec) | |
14791 | { | |
487b6440 | 14792 | struct elf_link_hash_entry *h; |
7dba9362 | 14793 | |
487b6440 AM |
14794 | h = elf_link_hash_lookup (elf_hash_table (info), symbol, |
14795 | FALSE, FALSE, TRUE); | |
14796 | if (h != NULL | |
14797 | && (h->root.type == bfd_link_hash_undefined | |
14798 | || h->root.type == bfd_link_hash_undefweak | |
bf3077a6 | 14799 | || ((h->ref_regular || h->def_dynamic) && !h->def_regular))) |
7dba9362 | 14800 | { |
bf3077a6 | 14801 | bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic; |
487b6440 AM |
14802 | h->root.type = bfd_link_hash_defined; |
14803 | h->root.u.def.section = sec; | |
14804 | h->root.u.def.value = 0; | |
14805 | h->def_regular = 1; | |
14806 | h->def_dynamic = 0; | |
14807 | h->start_stop = 1; | |
14808 | h->u2.start_stop_section = sec; | |
14809 | if (symbol[0] == '.') | |
14810 | { | |
14811 | /* .startof. and .sizeof. symbols are local. */ | |
559192d8 AM |
14812 | const struct elf_backend_data *bed; |
14813 | bed = get_elf_backend_data (info->output_bfd); | |
14814 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
487b6440 | 14815 | } |
36b8fda5 AM |
14816 | else |
14817 | { | |
14818 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
14819 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; | |
bf3077a6 | 14820 | if (was_dynamic) |
36b8fda5 AM |
14821 | bfd_elf_link_record_dynamic_symbol (info, h); |
14822 | } | |
487b6440 | 14823 | return &h->root; |
7dba9362 | 14824 | } |
487b6440 | 14825 | return NULL; |
7dba9362 | 14826 | } |
5dbc8b37 L |
14827 | |
14828 | /* Find dynamic relocs for H that apply to read-only sections. */ | |
14829 | ||
14830 | asection * | |
14831 | _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h) | |
14832 | { | |
14833 | struct elf_dyn_relocs *p; | |
14834 | ||
14835 | for (p = h->dyn_relocs; p != NULL; p = p->next) | |
14836 | { | |
14837 | asection *s = p->sec->output_section; | |
14838 | ||
14839 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
14840 | return p->sec; | |
14841 | } | |
14842 | return NULL; | |
14843 | } | |
d49e5065 L |
14844 | |
14845 | /* Set DF_TEXTREL if we find any dynamic relocs that apply to | |
14846 | read-only sections. */ | |
14847 | ||
14848 | bfd_boolean | |
14849 | _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf) | |
14850 | { | |
14851 | asection *sec; | |
14852 | ||
14853 | if (h->root.type == bfd_link_hash_indirect) | |
14854 | return TRUE; | |
14855 | ||
14856 | sec = _bfd_elf_readonly_dynrelocs (h); | |
14857 | if (sec != NULL) | |
14858 | { | |
14859 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
14860 | ||
14861 | info->flags |= DF_TEXTREL; | |
14862 | /* xgettext:c-format */ | |
14863 | info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' " | |
14864 | "in read-only section `%pA'\n"), | |
14865 | sec->owner, h->root.root.string, sec); | |
14866 | ||
14867 | if (bfd_link_textrel_check (info)) | |
14868 | /* xgettext:c-format */ | |
14869 | info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' " | |
14870 | "in read-only section `%pA'\n"), | |
14871 | sec->owner, h->root.root.string, sec); | |
14872 | ||
14873 | /* Not an error, just cut short the traversal. */ | |
14874 | return FALSE; | |
14875 | } | |
14876 | return TRUE; | |
14877 | } |