Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
219d1afa | 2 | Copyright (C) 1995-2018 Free Software Foundation, Inc. |
252b5132 | 3 | |
8fdd7217 | 4 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 5 | |
8fdd7217 NC |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 8 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 9 | (at your option) any later version. |
252b5132 | 10 | |
8fdd7217 NC |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
252b5132 | 15 | |
8fdd7217 NC |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
252b5132 | 20 | |
252b5132 | 21 | #include "sysdep.h" |
3db64b00 | 22 | #include "bfd.h" |
53df40a4 | 23 | #include "bfd_stdint.h" |
252b5132 RH |
24 | #include "bfdlink.h" |
25 | #include "libbfd.h" | |
26 | #define ARCH_SIZE 0 | |
27 | #include "elf-bfd.h" | |
4ad4eba5 | 28 | #include "safe-ctype.h" |
ccf2f652 | 29 | #include "libiberty.h" |
66eb6687 | 30 | #include "objalloc.h" |
08ce1d72 | 31 | #if BFD_SUPPORTS_PLUGINS |
7d0b9ebc | 32 | #include "plugin-api.h" |
7dc3990e L |
33 | #include "plugin.h" |
34 | #endif | |
252b5132 | 35 | |
28caa186 AM |
36 | /* This struct is used to pass information to routines called via |
37 | elf_link_hash_traverse which must return failure. */ | |
38 | ||
39 | struct elf_info_failed | |
40 | { | |
41 | struct bfd_link_info *info; | |
28caa186 AM |
42 | bfd_boolean failed; |
43 | }; | |
44 | ||
45 | /* This structure is used to pass information to | |
46 | _bfd_elf_link_find_version_dependencies. */ | |
47 | ||
48 | struct elf_find_verdep_info | |
49 | { | |
50 | /* General link information. */ | |
51 | struct bfd_link_info *info; | |
52 | /* The number of dependencies. */ | |
53 | unsigned int vers; | |
54 | /* Whether we had a failure. */ | |
55 | bfd_boolean failed; | |
56 | }; | |
57 | ||
58 | static bfd_boolean _bfd_elf_fix_symbol_flags | |
59 | (struct elf_link_hash_entry *, struct elf_info_failed *); | |
60 | ||
2f0c68f2 CM |
61 | asection * |
62 | _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, | |
63 | unsigned long r_symndx, | |
64 | bfd_boolean discard) | |
65 | { | |
66 | if (r_symndx >= cookie->locsymcount | |
67 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
68 | { | |
69 | struct elf_link_hash_entry *h; | |
70 | ||
71 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
72 | ||
73 | while (h->root.type == bfd_link_hash_indirect | |
74 | || h->root.type == bfd_link_hash_warning) | |
75 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
76 | ||
77 | if ((h->root.type == bfd_link_hash_defined | |
78 | || h->root.type == bfd_link_hash_defweak) | |
79 | && discarded_section (h->root.u.def.section)) | |
07d6d2b8 | 80 | return h->root.u.def.section; |
2f0c68f2 CM |
81 | else |
82 | return NULL; | |
83 | } | |
84 | else | |
85 | { | |
86 | /* It's not a relocation against a global symbol, | |
87 | but it could be a relocation against a local | |
88 | symbol for a discarded section. */ | |
89 | asection *isec; | |
90 | Elf_Internal_Sym *isym; | |
91 | ||
92 | /* Need to: get the symbol; get the section. */ | |
93 | isym = &cookie->locsyms[r_symndx]; | |
94 | isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); | |
95 | if (isec != NULL | |
96 | && discard ? discarded_section (isec) : 1) | |
97 | return isec; | |
98 | } | |
99 | return NULL; | |
100 | } | |
101 | ||
d98685ac AM |
102 | /* Define a symbol in a dynamic linkage section. */ |
103 | ||
104 | struct elf_link_hash_entry * | |
105 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
106 | struct bfd_link_info *info, | |
107 | asection *sec, | |
108 | const char *name) | |
109 | { | |
110 | struct elf_link_hash_entry *h; | |
111 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 112 | const struct elf_backend_data *bed; |
d98685ac AM |
113 | |
114 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
115 | if (h != NULL) | |
116 | { | |
117 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
118 | This is a symptom of a larger problem: Absolute symbols | |
119 | defined in shared libraries can't be overridden, because we | |
120 | lose the link to the bfd which is via the symbol section. */ | |
121 | h->root.type = bfd_link_hash_new; | |
ad32986f | 122 | bh = &h->root; |
d98685ac | 123 | } |
ad32986f NC |
124 | else |
125 | bh = NULL; | |
d98685ac | 126 | |
cf18fda4 | 127 | bed = get_elf_backend_data (abfd); |
d98685ac | 128 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, |
cf18fda4 | 129 | sec, 0, NULL, FALSE, bed->collect, |
d98685ac AM |
130 | &bh)) |
131 | return NULL; | |
132 | h = (struct elf_link_hash_entry *) bh; | |
ad32986f | 133 | BFD_ASSERT (h != NULL); |
d98685ac | 134 | h->def_regular = 1; |
e28df02b | 135 | h->non_elf = 0; |
12b2843a | 136 | h->root.linker_def = 1; |
d98685ac | 137 | h->type = STT_OBJECT; |
00b7642b AM |
138 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
139 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
d98685ac | 140 | |
ccabcbe5 | 141 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
d98685ac AM |
142 | return h; |
143 | } | |
144 | ||
b34976b6 | 145 | bfd_boolean |
268b6b39 | 146 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
147 | { |
148 | flagword flags; | |
aad5d350 | 149 | asection *s; |
252b5132 | 150 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 151 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 152 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
153 | |
154 | /* This function may be called more than once. */ | |
ce558b89 | 155 | if (htab->sgot != NULL) |
b34976b6 | 156 | return TRUE; |
252b5132 | 157 | |
e5a52504 | 158 | flags = bed->dynamic_sec_flags; |
252b5132 | 159 | |
14b2f831 AM |
160 | s = bfd_make_section_anyway_with_flags (abfd, |
161 | (bed->rela_plts_and_copies_p | |
162 | ? ".rela.got" : ".rel.got"), | |
163 | (bed->dynamic_sec_flags | |
164 | | SEC_READONLY)); | |
6de2ae4a L |
165 | if (s == NULL |
166 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
167 | return FALSE; | |
168 | htab->srelgot = s; | |
252b5132 | 169 | |
14b2f831 | 170 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d L |
171 | if (s == NULL |
172 | || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
173 | return FALSE; | |
174 | htab->sgot = s; | |
175 | ||
252b5132 RH |
176 | if (bed->want_got_plt) |
177 | { | |
14b2f831 | 178 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 179 | if (s == NULL |
6de2ae4a L |
180 | || !bfd_set_section_alignment (abfd, s, |
181 | bed->s->log_file_align)) | |
b34976b6 | 182 | return FALSE; |
6de2ae4a | 183 | htab->sgotplt = s; |
252b5132 RH |
184 | } |
185 | ||
64e77c6d L |
186 | /* The first bit of the global offset table is the header. */ |
187 | s->size += bed->got_header_size; | |
188 | ||
2517a57f AM |
189 | if (bed->want_got_sym) |
190 | { | |
191 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
192 | (or .got.plt) section. We don't do this in the linker script | |
193 | because we don't want to define the symbol if we are not creating | |
194 | a global offset table. */ | |
6de2ae4a L |
195 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
196 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 197 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
198 | if (h == NULL) |
199 | return FALSE; | |
2517a57f | 200 | } |
252b5132 | 201 | |
b34976b6 | 202 | return TRUE; |
252b5132 RH |
203 | } |
204 | \f | |
7e9f0867 AM |
205 | /* Create a strtab to hold the dynamic symbol names. */ |
206 | static bfd_boolean | |
207 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
208 | { | |
209 | struct elf_link_hash_table *hash_table; | |
210 | ||
211 | hash_table = elf_hash_table (info); | |
212 | if (hash_table->dynobj == NULL) | |
6cd255ca L |
213 | { |
214 | /* We may not set dynobj, an input file holding linker created | |
215 | dynamic sections to abfd, which may be a dynamic object with | |
216 | its own dynamic sections. We need to find a normal input file | |
217 | to hold linker created sections if possible. */ | |
218 | if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) | |
219 | { | |
220 | bfd *ibfd; | |
57963c05 | 221 | asection *s; |
6cd255ca | 222 | for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) |
6645479e | 223 | if ((ibfd->flags |
57963c05 AM |
224 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0 |
225 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
226 | && !((s = ibfd->sections) != NULL | |
227 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)) | |
6cd255ca L |
228 | { |
229 | abfd = ibfd; | |
230 | break; | |
231 | } | |
232 | } | |
233 | hash_table->dynobj = abfd; | |
234 | } | |
7e9f0867 AM |
235 | |
236 | if (hash_table->dynstr == NULL) | |
237 | { | |
238 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
239 | if (hash_table->dynstr == NULL) | |
240 | return FALSE; | |
241 | } | |
242 | return TRUE; | |
243 | } | |
244 | ||
45d6a902 AM |
245 | /* Create some sections which will be filled in with dynamic linking |
246 | information. ABFD is an input file which requires dynamic sections | |
247 | to be created. The dynamic sections take up virtual memory space | |
248 | when the final executable is run, so we need to create them before | |
249 | addresses are assigned to the output sections. We work out the | |
250 | actual contents and size of these sections later. */ | |
252b5132 | 251 | |
b34976b6 | 252 | bfd_boolean |
268b6b39 | 253 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 254 | { |
45d6a902 | 255 | flagword flags; |
91d6fa6a | 256 | asection *s; |
9c5bfbb7 | 257 | const struct elf_backend_data *bed; |
9637f6ef | 258 | struct elf_link_hash_entry *h; |
252b5132 | 259 | |
0eddce27 | 260 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
261 | return FALSE; |
262 | ||
263 | if (elf_hash_table (info)->dynamic_sections_created) | |
264 | return TRUE; | |
265 | ||
7e9f0867 AM |
266 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
267 | return FALSE; | |
45d6a902 | 268 | |
7e9f0867 | 269 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
270 | bed = get_elf_backend_data (abfd); |
271 | ||
272 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
273 | |
274 | /* A dynamically linked executable has a .interp section, but a | |
275 | shared library does not. */ | |
9b8b325a | 276 | if (bfd_link_executable (info) && !info->nointerp) |
252b5132 | 277 | { |
14b2f831 AM |
278 | s = bfd_make_section_anyway_with_flags (abfd, ".interp", |
279 | flags | SEC_READONLY); | |
3496cb2a | 280 | if (s == NULL) |
45d6a902 AM |
281 | return FALSE; |
282 | } | |
bb0deeff | 283 | |
45d6a902 AM |
284 | /* Create sections to hold version informations. These are removed |
285 | if they are not needed. */ | |
14b2f831 AM |
286 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", |
287 | flags | SEC_READONLY); | |
45d6a902 | 288 | if (s == NULL |
45d6a902 AM |
289 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
290 | return FALSE; | |
291 | ||
14b2f831 AM |
292 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", |
293 | flags | SEC_READONLY); | |
45d6a902 | 294 | if (s == NULL |
45d6a902 AM |
295 | || ! bfd_set_section_alignment (abfd, s, 1)) |
296 | return FALSE; | |
297 | ||
14b2f831 AM |
298 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", |
299 | flags | SEC_READONLY); | |
45d6a902 | 300 | if (s == NULL |
45d6a902 AM |
301 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
302 | return FALSE; | |
303 | ||
14b2f831 AM |
304 | s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", |
305 | flags | SEC_READONLY); | |
45d6a902 | 306 | if (s == NULL |
45d6a902 AM |
307 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
308 | return FALSE; | |
cae1fbbb | 309 | elf_hash_table (info)->dynsym = s; |
45d6a902 | 310 | |
14b2f831 AM |
311 | s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", |
312 | flags | SEC_READONLY); | |
3496cb2a | 313 | if (s == NULL) |
45d6a902 AM |
314 | return FALSE; |
315 | ||
14b2f831 | 316 | s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 317 | if (s == NULL |
45d6a902 AM |
318 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
319 | return FALSE; | |
320 | ||
321 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
322 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
323 | only want to define it if we are, in fact, creating a .dynamic | |
324 | section. We don't want to define it if there is no .dynamic | |
325 | section, since on some ELF platforms the start up code examines it | |
326 | to decide how to initialize the process. */ | |
9637f6ef L |
327 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); |
328 | elf_hash_table (info)->hdynamic = h; | |
329 | if (h == NULL) | |
45d6a902 AM |
330 | return FALSE; |
331 | ||
fdc90cb4 JJ |
332 | if (info->emit_hash) |
333 | { | |
14b2f831 AM |
334 | s = bfd_make_section_anyway_with_flags (abfd, ".hash", |
335 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
336 | if (s == NULL |
337 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
338 | return FALSE; | |
339 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
340 | } | |
341 | ||
342 | if (info->emit_gnu_hash) | |
343 | { | |
14b2f831 AM |
344 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", |
345 | flags | SEC_READONLY); | |
fdc90cb4 JJ |
346 | if (s == NULL |
347 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
348 | return FALSE; | |
349 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: | |
350 | 4 32-bit words followed by variable count of 64-bit words, then | |
351 | variable count of 32-bit words. */ | |
352 | if (bed->s->arch_size == 64) | |
353 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
354 | else | |
355 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
356 | } | |
45d6a902 AM |
357 | |
358 | /* Let the backend create the rest of the sections. This lets the | |
359 | backend set the right flags. The backend will normally create | |
360 | the .got and .plt sections. */ | |
894891db NC |
361 | if (bed->elf_backend_create_dynamic_sections == NULL |
362 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
45d6a902 AM |
363 | return FALSE; |
364 | ||
365 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
366 | ||
367 | return TRUE; | |
368 | } | |
369 | ||
370 | /* Create dynamic sections when linking against a dynamic object. */ | |
371 | ||
372 | bfd_boolean | |
268b6b39 | 373 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
374 | { |
375 | flagword flags, pltflags; | |
7325306f | 376 | struct elf_link_hash_entry *h; |
45d6a902 | 377 | asection *s; |
9c5bfbb7 | 378 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 379 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 380 | |
252b5132 RH |
381 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
382 | .rel[a].bss sections. */ | |
e5a52504 | 383 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
384 | |
385 | pltflags = flags; | |
252b5132 | 386 | if (bed->plt_not_loaded) |
6df4d94c MM |
387 | /* We do not clear SEC_ALLOC here because we still want the OS to |
388 | allocate space for the section; it's just that there's nothing | |
389 | to read in from the object file. */ | |
5d1634d7 | 390 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
391 | else |
392 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
393 | if (bed->plt_readonly) |
394 | pltflags |= SEC_READONLY; | |
395 | ||
14b2f831 | 396 | s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); |
252b5132 | 397 | if (s == NULL |
252b5132 | 398 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) |
b34976b6 | 399 | return FALSE; |
6de2ae4a | 400 | htab->splt = s; |
252b5132 | 401 | |
d98685ac AM |
402 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
403 | .plt section. */ | |
7325306f RS |
404 | if (bed->want_plt_sym) |
405 | { | |
406 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
407 | "_PROCEDURE_LINKAGE_TABLE_"); | |
408 | elf_hash_table (info)->hplt = h; | |
409 | if (h == NULL) | |
410 | return FALSE; | |
411 | } | |
252b5132 | 412 | |
14b2f831 AM |
413 | s = bfd_make_section_anyway_with_flags (abfd, |
414 | (bed->rela_plts_and_copies_p | |
415 | ? ".rela.plt" : ".rel.plt"), | |
416 | flags | SEC_READONLY); | |
252b5132 | 417 | if (s == NULL |
45d6a902 | 418 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 419 | return FALSE; |
6de2ae4a | 420 | htab->srelplt = s; |
252b5132 RH |
421 | |
422 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 423 | return FALSE; |
252b5132 | 424 | |
3018b441 RH |
425 | if (bed->want_dynbss) |
426 | { | |
427 | /* The .dynbss section is a place to put symbols which are defined | |
428 | by dynamic objects, are referenced by regular objects, and are | |
429 | not functions. We must allocate space for them in the process | |
430 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
431 | initialize them at run time. The linker script puts the .dynbss | |
432 | section into the .bss section of the final image. */ | |
14b2f831 | 433 | s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", |
afbf7e8e | 434 | SEC_ALLOC | SEC_LINKER_CREATED); |
3496cb2a | 435 | if (s == NULL) |
b34976b6 | 436 | return FALSE; |
9d19e4fd | 437 | htab->sdynbss = s; |
252b5132 | 438 | |
5474d94f AM |
439 | if (bed->want_dynrelro) |
440 | { | |
441 | /* Similarly, but for symbols that were originally in read-only | |
afbf7e8e AM |
442 | sections. This section doesn't really need to have contents, |
443 | but make it like other .data.rel.ro sections. */ | |
5474d94f | 444 | s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro", |
afbf7e8e | 445 | flags); |
5474d94f AM |
446 | if (s == NULL) |
447 | return FALSE; | |
448 | htab->sdynrelro = s; | |
449 | } | |
450 | ||
3018b441 | 451 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
452 | normally needed. We need to create it here, though, so that the |
453 | linker will map it to an output section. We can't just create it | |
454 | only if we need it, because we will not know whether we need it | |
455 | until we have seen all the input files, and the first time the | |
456 | main linker code calls BFD after examining all the input files | |
457 | (size_dynamic_sections) the input sections have already been | |
458 | mapped to the output sections. If the section turns out not to | |
459 | be needed, we can discard it later. We will never need this | |
460 | section when generating a shared object, since they do not use | |
461 | copy relocs. */ | |
9d19e4fd | 462 | if (bfd_link_executable (info)) |
3018b441 | 463 | { |
14b2f831 AM |
464 | s = bfd_make_section_anyway_with_flags (abfd, |
465 | (bed->rela_plts_and_copies_p | |
466 | ? ".rela.bss" : ".rel.bss"), | |
467 | flags | SEC_READONLY); | |
3018b441 | 468 | if (s == NULL |
45d6a902 | 469 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 470 | return FALSE; |
9d19e4fd | 471 | htab->srelbss = s; |
5474d94f AM |
472 | |
473 | if (bed->want_dynrelro) | |
474 | { | |
475 | s = (bfd_make_section_anyway_with_flags | |
476 | (abfd, (bed->rela_plts_and_copies_p | |
477 | ? ".rela.data.rel.ro" : ".rel.data.rel.ro"), | |
478 | flags | SEC_READONLY)); | |
479 | if (s == NULL | |
480 | || ! bfd_set_section_alignment (abfd, s, | |
481 | bed->s->log_file_align)) | |
482 | return FALSE; | |
483 | htab->sreldynrelro = s; | |
484 | } | |
3018b441 | 485 | } |
252b5132 RH |
486 | } |
487 | ||
b34976b6 | 488 | return TRUE; |
252b5132 RH |
489 | } |
490 | \f | |
252b5132 RH |
491 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
492 | read the input files, since we need to have a list of all of them | |
493 | before we can determine the final sizes of the output sections. | |
494 | Note that we may actually call this function even though we are not | |
495 | going to output any dynamic symbols; in some cases we know that a | |
496 | symbol should be in the dynamic symbol table, but only if there is | |
497 | one. */ | |
498 | ||
b34976b6 | 499 | bfd_boolean |
c152c796 AM |
500 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
501 | struct elf_link_hash_entry *h) | |
252b5132 RH |
502 | { |
503 | if (h->dynindx == -1) | |
504 | { | |
2b0f7ef9 | 505 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 506 | char *p; |
252b5132 | 507 | const char *name; |
ef53be89 | 508 | size_t indx; |
252b5132 | 509 | |
7a13edea NC |
510 | /* XXX: The ABI draft says the linker must turn hidden and |
511 | internal symbols into STB_LOCAL symbols when producing the | |
512 | DSO. However, if ld.so honors st_other in the dynamic table, | |
513 | this would not be necessary. */ | |
514 | switch (ELF_ST_VISIBILITY (h->other)) | |
515 | { | |
516 | case STV_INTERNAL: | |
517 | case STV_HIDDEN: | |
9d6eee78 L |
518 | if (h->root.type != bfd_link_hash_undefined |
519 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 520 | { |
f5385ebf | 521 | h->forced_local = 1; |
67687978 PB |
522 | if (!elf_hash_table (info)->is_relocatable_executable) |
523 | return TRUE; | |
7a13edea | 524 | } |
0444bdd4 | 525 | |
7a13edea NC |
526 | default: |
527 | break; | |
528 | } | |
529 | ||
252b5132 RH |
530 | h->dynindx = elf_hash_table (info)->dynsymcount; |
531 | ++elf_hash_table (info)->dynsymcount; | |
532 | ||
533 | dynstr = elf_hash_table (info)->dynstr; | |
534 | if (dynstr == NULL) | |
535 | { | |
536 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 537 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 538 | if (dynstr == NULL) |
b34976b6 | 539 | return FALSE; |
252b5132 RH |
540 | } |
541 | ||
542 | /* We don't put any version information in the dynamic string | |
aad5d350 | 543 | table. */ |
252b5132 RH |
544 | name = h->root.root.string; |
545 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
546 | if (p != NULL) |
547 | /* We know that the p points into writable memory. In fact, | |
548 | there are only a few symbols that have read-only names, being | |
549 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
550 | by the backends. Most symbols will have names pointing into | |
551 | an ELF string table read from a file, or to objalloc memory. */ | |
552 | *p = 0; | |
553 | ||
554 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
555 | ||
556 | if (p != NULL) | |
557 | *p = ELF_VER_CHR; | |
252b5132 | 558 | |
ef53be89 | 559 | if (indx == (size_t) -1) |
b34976b6 | 560 | return FALSE; |
252b5132 RH |
561 | h->dynstr_index = indx; |
562 | } | |
563 | ||
b34976b6 | 564 | return TRUE; |
252b5132 | 565 | } |
45d6a902 | 566 | \f |
55255dae L |
567 | /* Mark a symbol dynamic. */ |
568 | ||
28caa186 | 569 | static void |
55255dae | 570 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
571 | struct elf_link_hash_entry *h, |
572 | Elf_Internal_Sym *sym) | |
55255dae | 573 | { |
40b36307 | 574 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 575 | |
40b36307 | 576 | /* It may be called more than once on the same H. */ |
0e1862bb | 577 | if(h->dynamic || bfd_link_relocatable (info)) |
55255dae L |
578 | return; |
579 | ||
40b36307 L |
580 | if ((info->dynamic_data |
581 | && (h->type == STT_OBJECT | |
b8871f35 | 582 | || h->type == STT_COMMON |
40b36307 | 583 | || (sym != NULL |
b8871f35 L |
584 | && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT |
585 | || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) | |
a0c8462f | 586 | || (d != NULL |
73ec947d | 587 | && h->non_elf |
40b36307 | 588 | && (*d->match) (&d->head, NULL, h->root.root.string))) |
416c34d6 L |
589 | { |
590 | h->dynamic = 1; | |
591 | /* NB: If a symbol is made dynamic by --dynamic-list, it has | |
592 | non-IR reference. */ | |
593 | h->root.non_ir_ref_dynamic = 1; | |
594 | } | |
55255dae L |
595 | } |
596 | ||
45d6a902 AM |
597 | /* Record an assignment to a symbol made by a linker script. We need |
598 | this in case some dynamic object refers to this symbol. */ | |
599 | ||
600 | bfd_boolean | |
fe21a8fc L |
601 | bfd_elf_record_link_assignment (bfd *output_bfd, |
602 | struct bfd_link_info *info, | |
268b6b39 | 603 | const char *name, |
fe21a8fc L |
604 | bfd_boolean provide, |
605 | bfd_boolean hidden) | |
45d6a902 | 606 | { |
00cbee0a | 607 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 608 | struct elf_link_hash_table *htab; |
00cbee0a | 609 | const struct elf_backend_data *bed; |
45d6a902 | 610 | |
0eddce27 | 611 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
612 | return TRUE; |
613 | ||
4ea42fb7 AM |
614 | htab = elf_hash_table (info); |
615 | h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); | |
45d6a902 | 616 | if (h == NULL) |
4ea42fb7 | 617 | return provide; |
45d6a902 | 618 | |
8e2a4f11 AM |
619 | if (h->root.type == bfd_link_hash_warning) |
620 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
621 | ||
0f550b3d L |
622 | if (h->versioned == unknown) |
623 | { | |
624 | /* Set versioned if symbol version is unknown. */ | |
625 | char *version = strrchr (name, ELF_VER_CHR); | |
626 | if (version) | |
627 | { | |
628 | if (version > name && version[-1] != ELF_VER_CHR) | |
629 | h->versioned = versioned_hidden; | |
630 | else | |
631 | h->versioned = versioned; | |
632 | } | |
633 | } | |
634 | ||
73ec947d AM |
635 | /* Symbols defined in a linker script but not referenced anywhere |
636 | else will have non_elf set. */ | |
637 | if (h->non_elf) | |
638 | { | |
639 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); | |
640 | h->non_elf = 0; | |
641 | } | |
642 | ||
00cbee0a | 643 | switch (h->root.type) |
77cfaee6 | 644 | { |
00cbee0a L |
645 | case bfd_link_hash_defined: |
646 | case bfd_link_hash_defweak: | |
647 | case bfd_link_hash_common: | |
648 | break; | |
649 | case bfd_link_hash_undefweak: | |
650 | case bfd_link_hash_undefined: | |
651 | /* Since we're defining the symbol, don't let it seem to have not | |
652 | been defined. record_dynamic_symbol and size_dynamic_sections | |
653 | may depend on this. */ | |
4ea42fb7 | 654 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
655 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
656 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
657 | break; |
658 | case bfd_link_hash_new: | |
00cbee0a L |
659 | break; |
660 | case bfd_link_hash_indirect: | |
661 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 662 | the versioned symbol point to this one. */ |
00cbee0a L |
663 | bed = get_elf_backend_data (output_bfd); |
664 | hv = h; | |
665 | while (hv->root.type == bfd_link_hash_indirect | |
666 | || hv->root.type == bfd_link_hash_warning) | |
667 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
668 | /* We don't need to update h->root.u since linker will set them | |
669 | later. */ | |
670 | h->root.type = bfd_link_hash_undefined; | |
671 | hv->root.type = bfd_link_hash_indirect; | |
672 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
673 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
674 | break; | |
8e2a4f11 AM |
675 | default: |
676 | BFD_FAIL (); | |
c2596ca5 | 677 | return FALSE; |
55255dae | 678 | } |
45d6a902 AM |
679 | |
680 | /* If this symbol is being provided by the linker script, and it is | |
681 | currently defined by a dynamic object, but not by a regular | |
682 | object, then mark it as undefined so that the generic linker will | |
683 | force the correct value. */ | |
684 | if (provide | |
f5385ebf AM |
685 | && h->def_dynamic |
686 | && !h->def_regular) | |
45d6a902 AM |
687 | h->root.type = bfd_link_hash_undefined; |
688 | ||
689 | /* If this symbol is not being provided by the linker script, and it is | |
690 | currently defined by a dynamic object, but not by a regular object, | |
b531344c MR |
691 | then clear out any version information because the symbol will not be |
692 | associated with the dynamic object any more. */ | |
45d6a902 | 693 | if (!provide |
f5385ebf AM |
694 | && h->def_dynamic |
695 | && !h->def_regular) | |
b531344c MR |
696 | h->verinfo.verdef = NULL; |
697 | ||
698 | /* Make sure this symbol is not garbage collected. */ | |
699 | h->mark = 1; | |
45d6a902 | 700 | |
f5385ebf | 701 | h->def_regular = 1; |
45d6a902 | 702 | |
eb8476a6 | 703 | if (hidden) |
fe21a8fc | 704 | { |
91d6fa6a | 705 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
706 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
707 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
fe21a8fc L |
708 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
709 | } | |
710 | ||
6fa3860b PB |
711 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
712 | and executables. */ | |
0e1862bb | 713 | if (!bfd_link_relocatable (info) |
6fa3860b PB |
714 | && h->dynindx != -1 |
715 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
716 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
717 | h->forced_local = 1; | |
718 | ||
f5385ebf AM |
719 | if ((h->def_dynamic |
720 | || h->ref_dynamic | |
6b3b0ab8 L |
721 | || bfd_link_dll (info) |
722 | || elf_hash_table (info)->is_relocatable_executable) | |
34a87bb0 | 723 | && !h->forced_local |
45d6a902 AM |
724 | && h->dynindx == -1) |
725 | { | |
c152c796 | 726 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
727 | return FALSE; |
728 | ||
729 | /* If this is a weak defined symbol, and we know a corresponding | |
730 | real symbol from the same dynamic object, make sure the real | |
731 | symbol is also made into a dynamic symbol. */ | |
60d67dc8 | 732 | if (h->is_weakalias) |
45d6a902 | 733 | { |
60d67dc8 AM |
734 | struct elf_link_hash_entry *def = weakdef (h); |
735 | ||
736 | if (def->dynindx == -1 | |
737 | && !bfd_elf_link_record_dynamic_symbol (info, def)) | |
45d6a902 AM |
738 | return FALSE; |
739 | } | |
740 | } | |
741 | ||
742 | return TRUE; | |
743 | } | |
42751cf3 | 744 | |
8c58d23b AM |
745 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
746 | success, and 2 on a failure caused by attempting to record a symbol | |
747 | in a discarded section, eg. a discarded link-once section symbol. */ | |
748 | ||
749 | int | |
c152c796 AM |
750 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
751 | bfd *input_bfd, | |
752 | long input_indx) | |
8c58d23b AM |
753 | { |
754 | bfd_size_type amt; | |
755 | struct elf_link_local_dynamic_entry *entry; | |
756 | struct elf_link_hash_table *eht; | |
757 | struct elf_strtab_hash *dynstr; | |
ef53be89 | 758 | size_t dynstr_index; |
8c58d23b AM |
759 | char *name; |
760 | Elf_External_Sym_Shndx eshndx; | |
761 | char esym[sizeof (Elf64_External_Sym)]; | |
762 | ||
0eddce27 | 763 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
764 | return 0; |
765 | ||
766 | /* See if the entry exists already. */ | |
767 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
768 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
769 | return 1; | |
770 | ||
771 | amt = sizeof (*entry); | |
a50b1753 | 772 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
773 | if (entry == NULL) |
774 | return 0; | |
775 | ||
776 | /* Go find the symbol, so that we can find it's name. */ | |
777 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 778 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
779 | { |
780 | bfd_release (input_bfd, entry); | |
781 | return 0; | |
782 | } | |
783 | ||
784 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 785 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
786 | { |
787 | asection *s; | |
788 | ||
789 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
790 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
791 | { | |
792 | /* We can still bfd_release here as nothing has done another | |
793 | bfd_alloc. We can't do this later in this function. */ | |
794 | bfd_release (input_bfd, entry); | |
795 | return 2; | |
796 | } | |
797 | } | |
798 | ||
799 | name = (bfd_elf_string_from_elf_section | |
800 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
801 | entry->isym.st_name)); | |
802 | ||
803 | dynstr = elf_hash_table (info)->dynstr; | |
804 | if (dynstr == NULL) | |
805 | { | |
806 | /* Create a strtab to hold the dynamic symbol names. */ | |
807 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
808 | if (dynstr == NULL) | |
809 | return 0; | |
810 | } | |
811 | ||
b34976b6 | 812 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
ef53be89 | 813 | if (dynstr_index == (size_t) -1) |
8c58d23b AM |
814 | return 0; |
815 | entry->isym.st_name = dynstr_index; | |
816 | ||
817 | eht = elf_hash_table (info); | |
818 | ||
819 | entry->next = eht->dynlocal; | |
820 | eht->dynlocal = entry; | |
821 | entry->input_bfd = input_bfd; | |
822 | entry->input_indx = input_indx; | |
823 | eht->dynsymcount++; | |
824 | ||
825 | /* Whatever binding the symbol had before, it's now local. */ | |
826 | entry->isym.st_info | |
827 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
828 | ||
829 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
830 | ||
831 | return 1; | |
832 | } | |
833 | ||
30b30c21 | 834 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 835 | |
30b30c21 | 836 | long |
268b6b39 AM |
837 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
838 | bfd *input_bfd, | |
839 | long input_indx) | |
30b30c21 RH |
840 | { |
841 | struct elf_link_local_dynamic_entry *e; | |
842 | ||
843 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
844 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
845 | return e->dynindx; | |
846 | return -1; | |
847 | } | |
848 | ||
849 | /* This function is used to renumber the dynamic symbols, if some of | |
850 | them are removed because they are marked as local. This is called | |
851 | via elf_link_hash_traverse. */ | |
852 | ||
b34976b6 | 853 | static bfd_boolean |
268b6b39 AM |
854 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
855 | void *data) | |
42751cf3 | 856 | { |
a50b1753 | 857 | size_t *count = (size_t *) data; |
30b30c21 | 858 | |
6fa3860b PB |
859 | if (h->forced_local) |
860 | return TRUE; | |
861 | ||
862 | if (h->dynindx != -1) | |
863 | h->dynindx = ++(*count); | |
864 | ||
865 | return TRUE; | |
866 | } | |
867 | ||
868 | ||
869 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
870 | STB_LOCAL binding. */ | |
871 | ||
872 | static bfd_boolean | |
873 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
874 | void *data) | |
875 | { | |
a50b1753 | 876 | size_t *count = (size_t *) data; |
6fa3860b | 877 | |
6fa3860b PB |
878 | if (!h->forced_local) |
879 | return TRUE; | |
880 | ||
42751cf3 | 881 | if (h->dynindx != -1) |
30b30c21 RH |
882 | h->dynindx = ++(*count); |
883 | ||
b34976b6 | 884 | return TRUE; |
42751cf3 | 885 | } |
30b30c21 | 886 | |
aee6f5b4 AO |
887 | /* Return true if the dynamic symbol for a given section should be |
888 | omitted when creating a shared library. */ | |
889 | bfd_boolean | |
d00dd7dc AM |
890 | _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED, |
891 | struct bfd_link_info *info, | |
892 | asection *p) | |
aee6f5b4 | 893 | { |
74541ad4 | 894 | struct elf_link_hash_table *htab; |
ca55926c | 895 | asection *ip; |
74541ad4 | 896 | |
aee6f5b4 AO |
897 | switch (elf_section_data (p)->this_hdr.sh_type) |
898 | { | |
899 | case SHT_PROGBITS: | |
900 | case SHT_NOBITS: | |
901 | /* If sh_type is yet undecided, assume it could be | |
902 | SHT_PROGBITS/SHT_NOBITS. */ | |
903 | case SHT_NULL: | |
74541ad4 AM |
904 | htab = elf_hash_table (info); |
905 | if (p == htab->tls_sec) | |
906 | return FALSE; | |
907 | ||
908 | if (htab->text_index_section != NULL) | |
909 | return p != htab->text_index_section && p != htab->data_index_section; | |
910 | ||
ca55926c | 911 | return (htab->dynobj != NULL |
3d4d4302 | 912 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 913 | && ip->output_section == p); |
aee6f5b4 AO |
914 | |
915 | /* There shouldn't be section relative relocations | |
916 | against any other section. */ | |
917 | default: | |
918 | return TRUE; | |
919 | } | |
920 | } | |
921 | ||
d00dd7dc AM |
922 | bfd_boolean |
923 | _bfd_elf_omit_section_dynsym_all | |
924 | (bfd *output_bfd ATTRIBUTE_UNUSED, | |
925 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
926 | asection *p ATTRIBUTE_UNUSED) | |
927 | { | |
928 | return TRUE; | |
929 | } | |
930 | ||
062e2358 | 931 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
932 | symbol for each output section, which come first. Next come symbols |
933 | which have been forced to local binding. Then all of the back-end | |
934 | allocated local dynamic syms, followed by the rest of the global | |
63f452a8 AM |
935 | symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. |
936 | (This prevents the early call before elf_backend_init_index_section | |
937 | and strip_excluded_output_sections setting dynindx for sections | |
938 | that are stripped.) */ | |
30b30c21 | 939 | |
554220db AM |
940 | static unsigned long |
941 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
942 | struct bfd_link_info *info, | |
943 | unsigned long *section_sym_count) | |
30b30c21 RH |
944 | { |
945 | unsigned long dynsymcount = 0; | |
63f452a8 | 946 | bfd_boolean do_sec = section_sym_count != NULL; |
30b30c21 | 947 | |
0e1862bb L |
948 | if (bfd_link_pic (info) |
949 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 950 | { |
aee6f5b4 | 951 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
952 | asection *p; |
953 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 954 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
955 | && (p->flags & SEC_ALLOC) != 0 |
956 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
63f452a8 AM |
957 | { |
958 | ++dynsymcount; | |
959 | if (do_sec) | |
960 | elf_section_data (p)->dynindx = dynsymcount; | |
961 | } | |
962 | else if (do_sec) | |
74541ad4 | 963 | elf_section_data (p)->dynindx = 0; |
30b30c21 | 964 | } |
63f452a8 AM |
965 | if (do_sec) |
966 | *section_sym_count = dynsymcount; | |
30b30c21 | 967 | |
6fa3860b PB |
968 | elf_link_hash_traverse (elf_hash_table (info), |
969 | elf_link_renumber_local_hash_table_dynsyms, | |
970 | &dynsymcount); | |
971 | ||
30b30c21 RH |
972 | if (elf_hash_table (info)->dynlocal) |
973 | { | |
974 | struct elf_link_local_dynamic_entry *p; | |
975 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
976 | p->dynindx = ++dynsymcount; | |
977 | } | |
90ac2420 | 978 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
979 | |
980 | elf_link_hash_traverse (elf_hash_table (info), | |
981 | elf_link_renumber_hash_table_dynsyms, | |
982 | &dynsymcount); | |
983 | ||
d5486c43 L |
984 | /* There is an unused NULL entry at the head of the table which we |
985 | must account for in our count even if the table is empty since it | |
986 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
987 | .dynamic section. */ | |
988 | dynsymcount++; | |
30b30c21 | 989 | |
ccabcbe5 AM |
990 | elf_hash_table (info)->dynsymcount = dynsymcount; |
991 | return dynsymcount; | |
30b30c21 | 992 | } |
252b5132 | 993 | |
54ac0771 L |
994 | /* Merge st_other field. */ |
995 | ||
996 | static void | |
997 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 998 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 999 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
1000 | { |
1001 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1002 | ||
1003 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 1004 | code might be needed here. */ |
54ac0771 L |
1005 | if (bed->elf_backend_merge_symbol_attribute) |
1006 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
1007 | dynamic); | |
1008 | ||
cd3416da | 1009 | if (!dynamic) |
54ac0771 | 1010 | { |
cd3416da AM |
1011 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
1012 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 1013 | |
cd3416da AM |
1014 | /* Keep the most constraining visibility. Leave the remainder |
1015 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
1016 | if (symvis - 1 < hvis - 1) | |
1017 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 1018 | } |
b8417128 AM |
1019 | else if (definition |
1020 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
1021 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 1022 | h->protected_def = 1; |
54ac0771 L |
1023 | } |
1024 | ||
4f3fedcf AM |
1025 | /* This function is called when we want to merge a new symbol with an |
1026 | existing symbol. It handles the various cases which arise when we | |
1027 | find a definition in a dynamic object, or when there is already a | |
1028 | definition in a dynamic object. The new symbol is described by | |
1029 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
1030 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
1031 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
1032 | of an old common symbol. We set OVERRIDE if the old symbol is | |
1033 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
1034 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
1035 | to change. By OK to change, we mean that we shouldn't warn if the | |
1036 | type or size does change. */ | |
45d6a902 | 1037 | |
8a56bd02 | 1038 | static bfd_boolean |
268b6b39 AM |
1039 | _bfd_elf_merge_symbol (bfd *abfd, |
1040 | struct bfd_link_info *info, | |
1041 | const char *name, | |
1042 | Elf_Internal_Sym *sym, | |
1043 | asection **psec, | |
1044 | bfd_vma *pvalue, | |
4f3fedcf AM |
1045 | struct elf_link_hash_entry **sym_hash, |
1046 | bfd **poldbfd, | |
37a9e49a | 1047 | bfd_boolean *pold_weak, |
af44c138 | 1048 | unsigned int *pold_alignment, |
268b6b39 AM |
1049 | bfd_boolean *skip, |
1050 | bfd_boolean *override, | |
1051 | bfd_boolean *type_change_ok, | |
6e33951e L |
1052 | bfd_boolean *size_change_ok, |
1053 | bfd_boolean *matched) | |
252b5132 | 1054 | { |
7479dfd4 | 1055 | asection *sec, *oldsec; |
45d6a902 | 1056 | struct elf_link_hash_entry *h; |
90c984fc | 1057 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
1058 | struct elf_link_hash_entry *flip; |
1059 | int bind; | |
1060 | bfd *oldbfd; | |
1061 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 1062 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 1063 | const struct elf_backend_data *bed; |
6e33951e | 1064 | char *new_version; |
93f4de39 | 1065 | bfd_boolean default_sym = *matched; |
45d6a902 AM |
1066 | |
1067 | *skip = FALSE; | |
1068 | *override = FALSE; | |
1069 | ||
1070 | sec = *psec; | |
1071 | bind = ELF_ST_BIND (sym->st_info); | |
1072 | ||
1073 | if (! bfd_is_und_section (sec)) | |
1074 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1075 | else | |
1076 | h = ((struct elf_link_hash_entry *) | |
1077 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1078 | if (h == NULL) | |
1079 | return FALSE; | |
1080 | *sym_hash = h; | |
252b5132 | 1081 | |
88ba32a0 L |
1082 | bed = get_elf_backend_data (abfd); |
1083 | ||
6e33951e | 1084 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1085 | if (h->versioned != unversioned) |
6e33951e | 1086 | { |
422f1182 L |
1087 | /* Symbol version is unknown or versioned. */ |
1088 | new_version = strrchr (name, ELF_VER_CHR); | |
1089 | if (new_version) | |
1090 | { | |
1091 | if (h->versioned == unknown) | |
1092 | { | |
1093 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1094 | h->versioned = versioned_hidden; | |
1095 | else | |
1096 | h->versioned = versioned; | |
1097 | } | |
1098 | new_version += 1; | |
1099 | if (new_version[0] == '\0') | |
1100 | new_version = NULL; | |
1101 | } | |
1102 | else | |
1103 | h->versioned = unversioned; | |
6e33951e | 1104 | } |
422f1182 L |
1105 | else |
1106 | new_version = NULL; | |
6e33951e | 1107 | |
90c984fc L |
1108 | /* For merging, we only care about real symbols. But we need to make |
1109 | sure that indirect symbol dynamic flags are updated. */ | |
1110 | hi = h; | |
45d6a902 AM |
1111 | while (h->root.type == bfd_link_hash_indirect |
1112 | || h->root.type == bfd_link_hash_warning) | |
1113 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1114 | ||
6e33951e L |
1115 | if (!*matched) |
1116 | { | |
1117 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1118 | *matched = TRUE; | |
1119 | else | |
1120 | { | |
ae7683d2 | 1121 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1122 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1123 | true if the new symbol is only visible to the symbol with |
6e33951e | 1124 | the same symbol version. */ |
422f1182 L |
1125 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1126 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1127 | if (!old_hidden && !new_hidden) |
1128 | /* The new symbol matches the existing symbol if both | |
1129 | aren't hidden. */ | |
1130 | *matched = TRUE; | |
1131 | else | |
1132 | { | |
1133 | /* OLD_VERSION is the symbol version of the existing | |
1134 | symbol. */ | |
422f1182 L |
1135 | char *old_version; |
1136 | ||
1137 | if (h->versioned >= versioned) | |
1138 | old_version = strrchr (h->root.root.string, | |
1139 | ELF_VER_CHR) + 1; | |
1140 | else | |
1141 | old_version = NULL; | |
6e33951e L |
1142 | |
1143 | /* The new symbol matches the existing symbol if they | |
1144 | have the same symbol version. */ | |
1145 | *matched = (old_version == new_version | |
1146 | || (old_version != NULL | |
1147 | && new_version != NULL | |
1148 | && strcmp (old_version, new_version) == 0)); | |
1149 | } | |
1150 | } | |
1151 | } | |
1152 | ||
934bce08 AM |
1153 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1154 | existing symbol. */ | |
1155 | ||
1156 | oldbfd = NULL; | |
1157 | oldsec = NULL; | |
1158 | switch (h->root.type) | |
1159 | { | |
1160 | default: | |
1161 | break; | |
1162 | ||
1163 | case bfd_link_hash_undefined: | |
1164 | case bfd_link_hash_undefweak: | |
1165 | oldbfd = h->root.u.undef.abfd; | |
1166 | break; | |
1167 | ||
1168 | case bfd_link_hash_defined: | |
1169 | case bfd_link_hash_defweak: | |
1170 | oldbfd = h->root.u.def.section->owner; | |
1171 | oldsec = h->root.u.def.section; | |
1172 | break; | |
1173 | ||
1174 | case bfd_link_hash_common: | |
1175 | oldbfd = h->root.u.c.p->section->owner; | |
1176 | oldsec = h->root.u.c.p->section; | |
1177 | if (pold_alignment) | |
1178 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1179 | break; | |
1180 | } | |
1181 | if (poldbfd && *poldbfd == NULL) | |
1182 | *poldbfd = oldbfd; | |
1183 | ||
1184 | /* Differentiate strong and weak symbols. */ | |
1185 | newweak = bind == STB_WEAK; | |
1186 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1187 | || h->root.type == bfd_link_hash_undefweak); | |
1188 | if (pold_weak) | |
1189 | *pold_weak = oldweak; | |
1190 | ||
40b36307 | 1191 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1192 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1193 | symbols. */ |
1194 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1195 | ||
ee659f1f AM |
1196 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1197 | respectively, is from a dynamic object. */ | |
1198 | ||
1199 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1200 | ||
1201 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1202 | syms and defined syms in dynamic libraries respectively. | |
1203 | ref_dynamic on the other hand can be set for a symbol defined in | |
1204 | a dynamic library, and def_dynamic may not be set; When the | |
1205 | definition in a dynamic lib is overridden by a definition in the | |
1206 | executable use of the symbol in the dynamic lib becomes a | |
1207 | reference to the executable symbol. */ | |
1208 | if (newdyn) | |
1209 | { | |
1210 | if (bfd_is_und_section (sec)) | |
1211 | { | |
1212 | if (bind != STB_WEAK) | |
1213 | { | |
1214 | h->ref_dynamic_nonweak = 1; | |
1215 | hi->ref_dynamic_nonweak = 1; | |
1216 | } | |
1217 | } | |
1218 | else | |
1219 | { | |
6e33951e L |
1220 | /* Update the existing symbol only if they match. */ |
1221 | if (*matched) | |
1222 | h->dynamic_def = 1; | |
ee659f1f AM |
1223 | hi->dynamic_def = 1; |
1224 | } | |
1225 | } | |
1226 | ||
45d6a902 AM |
1227 | /* If we just created the symbol, mark it as being an ELF symbol. |
1228 | Other than that, there is nothing to do--there is no merge issue | |
1229 | with a newly defined symbol--so we just return. */ | |
1230 | ||
1231 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1232 | { |
f5385ebf | 1233 | h->non_elf = 0; |
45d6a902 AM |
1234 | return TRUE; |
1235 | } | |
252b5132 | 1236 | |
45d6a902 AM |
1237 | /* In cases involving weak versioned symbols, we may wind up trying |
1238 | to merge a symbol with itself. Catch that here, to avoid the | |
1239 | confusion that results if we try to override a symbol with | |
1240 | itself. The additional tests catch cases like | |
1241 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1242 | dynamic object, which we do want to handle here. */ | |
1243 | if (abfd == oldbfd | |
895fa45f | 1244 | && (newweak || oldweak) |
45d6a902 | 1245 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1246 | || !h->def_regular)) |
45d6a902 AM |
1247 | return TRUE; |
1248 | ||
707bba77 | 1249 | olddyn = FALSE; |
45d6a902 AM |
1250 | if (oldbfd != NULL) |
1251 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1252 | else if (oldsec != NULL) |
45d6a902 | 1253 | { |
707bba77 | 1254 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1255 | indices used by MIPS ELF. */ |
707bba77 | 1256 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1257 | } |
252b5132 | 1258 | |
1a3b5c34 AM |
1259 | /* Handle a case where plugin_notice won't be called and thus won't |
1260 | set the non_ir_ref flags on the first pass over symbols. */ | |
1261 | if (oldbfd != NULL | |
1262 | && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN) | |
1263 | && newdyn != olddyn) | |
1264 | { | |
1265 | h->root.non_ir_ref_dynamic = TRUE; | |
1266 | hi->root.non_ir_ref_dynamic = TRUE; | |
1267 | } | |
1268 | ||
45d6a902 AM |
1269 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1270 | respectively, appear to be a definition rather than reference. */ | |
1271 | ||
707bba77 | 1272 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1273 | |
707bba77 AM |
1274 | olddef = (h->root.type != bfd_link_hash_undefined |
1275 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1276 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1277 | |
0a36a439 L |
1278 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1279 | respectively, appear to be a function. */ | |
1280 | ||
1281 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1282 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1283 | ||
1284 | oldfunc = (h->type != STT_NOTYPE | |
1285 | && bed->is_function_type (h->type)); | |
1286 | ||
c5d37467 | 1287 | if (!(newfunc && oldfunc) |
5b677558 AM |
1288 | && ELF_ST_TYPE (sym->st_info) != h->type |
1289 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1290 | && h->type != STT_NOTYPE | |
c5d37467 AM |
1291 | && (newdef || bfd_is_com_section (sec)) |
1292 | && (olddef || h->root.type == bfd_link_hash_common)) | |
580a2b6e | 1293 | { |
c5d37467 AM |
1294 | /* If creating a default indirect symbol ("foo" or "foo@") from |
1295 | a dynamic versioned definition ("foo@@") skip doing so if | |
1296 | there is an existing regular definition with a different | |
1297 | type. We don't want, for example, a "time" variable in the | |
1298 | executable overriding a "time" function in a shared library. */ | |
1299 | if (newdyn | |
1300 | && !olddyn) | |
1301 | { | |
1302 | *skip = TRUE; | |
1303 | return TRUE; | |
1304 | } | |
1305 | ||
1306 | /* When adding a symbol from a regular object file after we have | |
1307 | created indirect symbols, undo the indirection and any | |
1308 | dynamic state. */ | |
1309 | if (hi != h | |
1310 | && !newdyn | |
1311 | && olddyn) | |
1312 | { | |
1313 | h = hi; | |
1314 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1315 | h->forced_local = 0; | |
1316 | h->ref_dynamic = 0; | |
1317 | h->def_dynamic = 0; | |
1318 | h->dynamic_def = 0; | |
1319 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1320 | { | |
1321 | h->root.type = bfd_link_hash_undefined; | |
1322 | h->root.u.undef.abfd = abfd; | |
1323 | } | |
1324 | else | |
1325 | { | |
1326 | h->root.type = bfd_link_hash_new; | |
1327 | h->root.u.undef.abfd = NULL; | |
1328 | } | |
1329 | return TRUE; | |
1330 | } | |
580a2b6e L |
1331 | } |
1332 | ||
4c34aff8 AM |
1333 | /* Check TLS symbols. We don't check undefined symbols introduced |
1334 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1335 | check symbols from plugins because they also have no type. */ | |
1336 | if (oldbfd != NULL | |
1337 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1338 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1339 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1340 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1341 | { |
1342 | bfd *ntbfd, *tbfd; | |
1343 | bfd_boolean ntdef, tdef; | |
1344 | asection *ntsec, *tsec; | |
1345 | ||
1346 | if (h->type == STT_TLS) | |
1347 | { | |
3b36f7e6 | 1348 | ntbfd = abfd; |
7479dfd4 L |
1349 | ntsec = sec; |
1350 | ntdef = newdef; | |
1351 | tbfd = oldbfd; | |
1352 | tsec = oldsec; | |
1353 | tdef = olddef; | |
1354 | } | |
1355 | else | |
1356 | { | |
1357 | ntbfd = oldbfd; | |
1358 | ntsec = oldsec; | |
1359 | ntdef = olddef; | |
1360 | tbfd = abfd; | |
1361 | tsec = sec; | |
1362 | tdef = newdef; | |
1363 | } | |
1364 | ||
1365 | if (tdef && ntdef) | |
4eca0228 | 1366 | _bfd_error_handler |
695344c0 | 1367 | /* xgettext:c-format */ |
871b3ab2 AM |
1368 | (_("%s: TLS definition in %pB section %pA " |
1369 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1370 | h->root.root.string, tbfd, tsec, ntbfd, ntsec); |
7479dfd4 | 1371 | else if (!tdef && !ntdef) |
4eca0228 | 1372 | _bfd_error_handler |
695344c0 | 1373 | /* xgettext:c-format */ |
871b3ab2 AM |
1374 | (_("%s: TLS reference in %pB " |
1375 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1376 | h->root.root.string, tbfd, ntbfd); |
7479dfd4 | 1377 | else if (tdef) |
4eca0228 | 1378 | _bfd_error_handler |
695344c0 | 1379 | /* xgettext:c-format */ |
871b3ab2 AM |
1380 | (_("%s: TLS definition in %pB section %pA " |
1381 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1382 | h->root.root.string, tbfd, tsec, ntbfd); |
7479dfd4 | 1383 | else |
4eca0228 | 1384 | _bfd_error_handler |
695344c0 | 1385 | /* xgettext:c-format */ |
871b3ab2 AM |
1386 | (_("%s: TLS reference in %pB " |
1387 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1388 | h->root.root.string, tbfd, ntbfd, ntsec); |
7479dfd4 L |
1389 | |
1390 | bfd_set_error (bfd_error_bad_value); | |
1391 | return FALSE; | |
1392 | } | |
1393 | ||
45d6a902 AM |
1394 | /* If the old symbol has non-default visibility, we ignore the new |
1395 | definition from a dynamic object. */ | |
1396 | if (newdyn | |
9c7a29a3 | 1397 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1398 | && !bfd_is_und_section (sec)) |
1399 | { | |
1400 | *skip = TRUE; | |
1401 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1402 | h->ref_dynamic = 1; |
90c984fc | 1403 | hi->ref_dynamic = 1; |
45d6a902 AM |
1404 | /* A protected symbol has external availability. Make sure it is |
1405 | recorded as dynamic. | |
1406 | ||
1407 | FIXME: Should we check type and size for protected symbol? */ | |
1408 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1409 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1410 | else |
1411 | return TRUE; | |
1412 | } | |
1413 | else if (!newdyn | |
9c7a29a3 | 1414 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1415 | && h->def_dynamic) |
45d6a902 AM |
1416 | { |
1417 | /* If the new symbol with non-default visibility comes from a | |
1418 | relocatable file and the old definition comes from a dynamic | |
1419 | object, we remove the old definition. */ | |
6c9b78e6 | 1420 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1421 | { |
1422 | /* Handle the case where the old dynamic definition is | |
1423 | default versioned. We need to copy the symbol info from | |
1424 | the symbol with default version to the normal one if it | |
1425 | was referenced before. */ | |
1426 | if (h->ref_regular) | |
1427 | { | |
6c9b78e6 | 1428 | hi->root.type = h->root.type; |
d2dee3b2 | 1429 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1430 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1431 | |
6c9b78e6 | 1432 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1433 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1434 | { |
aed81c4e MR |
1435 | /* If the new symbol is hidden or internal, completely undo |
1436 | any dynamic link state. */ | |
1437 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1438 | h->forced_local = 0; | |
1439 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1440 | } |
1441 | else | |
aed81c4e MR |
1442 | h->ref_dynamic = 1; |
1443 | ||
1444 | h->def_dynamic = 0; | |
aed81c4e MR |
1445 | /* FIXME: Should we check type and size for protected symbol? */ |
1446 | h->size = 0; | |
1447 | h->type = 0; | |
1448 | ||
6c9b78e6 | 1449 | h = hi; |
d2dee3b2 L |
1450 | } |
1451 | else | |
6c9b78e6 | 1452 | h = hi; |
d2dee3b2 | 1453 | } |
1de1a317 | 1454 | |
f5eda473 AM |
1455 | /* If the old symbol was undefined before, then it will still be |
1456 | on the undefs list. If the new symbol is undefined or | |
1457 | common, we can't make it bfd_link_hash_new here, because new | |
1458 | undefined or common symbols will be added to the undefs list | |
1459 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1460 | added twice to the undefs list. Also, if the new symbol is | |
1461 | undefweak then we don't want to lose the strong undef. */ | |
1462 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1463 | { |
1de1a317 | 1464 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1465 | h->root.u.undef.abfd = abfd; |
1466 | } | |
1467 | else | |
1468 | { | |
1469 | h->root.type = bfd_link_hash_new; | |
1470 | h->root.u.undef.abfd = NULL; | |
1471 | } | |
1472 | ||
f5eda473 | 1473 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1474 | { |
f5eda473 AM |
1475 | /* If the new symbol is hidden or internal, completely undo |
1476 | any dynamic link state. */ | |
1477 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1478 | h->forced_local = 0; | |
1479 | h->ref_dynamic = 0; | |
45d6a902 | 1480 | } |
f5eda473 AM |
1481 | else |
1482 | h->ref_dynamic = 1; | |
1483 | h->def_dynamic = 0; | |
45d6a902 AM |
1484 | /* FIXME: Should we check type and size for protected symbol? */ |
1485 | h->size = 0; | |
1486 | h->type = 0; | |
1487 | return TRUE; | |
1488 | } | |
14a793b2 | 1489 | |
15b43f48 AM |
1490 | /* If a new weak symbol definition comes from a regular file and the |
1491 | old symbol comes from a dynamic library, we treat the new one as | |
1492 | strong. Similarly, an old weak symbol definition from a regular | |
1493 | file is treated as strong when the new symbol comes from a dynamic | |
1494 | library. Further, an old weak symbol from a dynamic library is | |
1495 | treated as strong if the new symbol is from a dynamic library. | |
1496 | This reflects the way glibc's ld.so works. | |
1497 | ||
165f707a AM |
1498 | Also allow a weak symbol to override a linker script symbol |
1499 | defined by an early pass over the script. This is done so the | |
1500 | linker knows the symbol is defined in an object file, for the | |
1501 | DEFINED script function. | |
1502 | ||
15b43f48 AM |
1503 | Do this before setting *type_change_ok or *size_change_ok so that |
1504 | we warn properly when dynamic library symbols are overridden. */ | |
1505 | ||
165f707a | 1506 | if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) |
0f8a2703 | 1507 | newweak = FALSE; |
15b43f48 | 1508 | if (olddef && newdyn) |
0f8a2703 AM |
1509 | oldweak = FALSE; |
1510 | ||
d334575b | 1511 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1512 | if (newfunc && oldfunc) |
fcb93ecf PB |
1513 | *type_change_ok = TRUE; |
1514 | ||
79349b09 AM |
1515 | /* It's OK to change the type if either the existing symbol or the |
1516 | new symbol is weak. A type change is also OK if the old symbol | |
1517 | is undefined and the new symbol is defined. */ | |
252b5132 | 1518 | |
79349b09 AM |
1519 | if (oldweak |
1520 | || newweak | |
1521 | || (newdef | |
1522 | && h->root.type == bfd_link_hash_undefined)) | |
1523 | *type_change_ok = TRUE; | |
1524 | ||
1525 | /* It's OK to change the size if either the existing symbol or the | |
1526 | new symbol is weak, or if the old symbol is undefined. */ | |
1527 | ||
1528 | if (*type_change_ok | |
1529 | || h->root.type == bfd_link_hash_undefined) | |
1530 | *size_change_ok = TRUE; | |
45d6a902 | 1531 | |
45d6a902 AM |
1532 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1533 | symbol, respectively, appears to be a common symbol in a dynamic | |
1534 | object. If a symbol appears in an uninitialized section, and is | |
1535 | not weak, and is not a function, then it may be a common symbol | |
1536 | which was resolved when the dynamic object was created. We want | |
1537 | to treat such symbols specially, because they raise special | |
1538 | considerations when setting the symbol size: if the symbol | |
1539 | appears as a common symbol in a regular object, and the size in | |
1540 | the regular object is larger, we must make sure that we use the | |
1541 | larger size. This problematic case can always be avoided in C, | |
1542 | but it must be handled correctly when using Fortran shared | |
1543 | libraries. | |
1544 | ||
1545 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1546 | likewise for OLDDYNCOMMON and OLDDEF. | |
1547 | ||
1548 | Note that this test is just a heuristic, and that it is quite | |
1549 | possible to have an uninitialized symbol in a shared object which | |
1550 | is really a definition, rather than a common symbol. This could | |
1551 | lead to some minor confusion when the symbol really is a common | |
1552 | symbol in some regular object. However, I think it will be | |
1553 | harmless. */ | |
1554 | ||
1555 | if (newdyn | |
1556 | && newdef | |
79349b09 | 1557 | && !newweak |
45d6a902 AM |
1558 | && (sec->flags & SEC_ALLOC) != 0 |
1559 | && (sec->flags & SEC_LOAD) == 0 | |
1560 | && sym->st_size > 0 | |
0a36a439 | 1561 | && !newfunc) |
45d6a902 AM |
1562 | newdyncommon = TRUE; |
1563 | else | |
1564 | newdyncommon = FALSE; | |
1565 | ||
1566 | if (olddyn | |
1567 | && olddef | |
1568 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1569 | && h->def_dynamic |
45d6a902 AM |
1570 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1571 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1572 | && h->size > 0 | |
0a36a439 | 1573 | && !oldfunc) |
45d6a902 AM |
1574 | olddyncommon = TRUE; |
1575 | else | |
1576 | olddyncommon = FALSE; | |
1577 | ||
a4d8e49b L |
1578 | /* We now know everything about the old and new symbols. We ask the |
1579 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1580 | if (bed->merge_symbol != NULL) |
1581 | { | |
1582 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1583 | return FALSE; | |
1584 | sec = *psec; | |
1585 | } | |
a4d8e49b | 1586 | |
a83ef4d1 L |
1587 | /* There are multiple definitions of a normal symbol. Skip the |
1588 | default symbol as well as definition from an IR object. */ | |
93f4de39 | 1589 | if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak |
a83ef4d1 L |
1590 | && !default_sym && h->def_regular |
1591 | && !(oldbfd != NULL | |
1592 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
1593 | && (abfd->flags & BFD_PLUGIN) == 0)) | |
93f4de39 RL |
1594 | { |
1595 | /* Handle a multiple definition. */ | |
1596 | (*info->callbacks->multiple_definition) (info, &h->root, | |
1597 | abfd, sec, *pvalue); | |
1598 | *skip = TRUE; | |
1599 | return TRUE; | |
1600 | } | |
1601 | ||
45d6a902 AM |
1602 | /* If both the old and the new symbols look like common symbols in a |
1603 | dynamic object, set the size of the symbol to the larger of the | |
1604 | two. */ | |
1605 | ||
1606 | if (olddyncommon | |
1607 | && newdyncommon | |
1608 | && sym->st_size != h->size) | |
1609 | { | |
1610 | /* Since we think we have two common symbols, issue a multiple | |
1611 | common warning if desired. Note that we only warn if the | |
1612 | size is different. If the size is the same, we simply let | |
1613 | the old symbol override the new one as normally happens with | |
1614 | symbols defined in dynamic objects. */ | |
1615 | ||
1a72702b AM |
1616 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1617 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1618 | if (sym->st_size > h->size) |
1619 | h->size = sym->st_size; | |
252b5132 | 1620 | |
45d6a902 | 1621 | *size_change_ok = TRUE; |
252b5132 RH |
1622 | } |
1623 | ||
45d6a902 AM |
1624 | /* If we are looking at a dynamic object, and we have found a |
1625 | definition, we need to see if the symbol was already defined by | |
1626 | some other object. If so, we want to use the existing | |
1627 | definition, and we do not want to report a multiple symbol | |
1628 | definition error; we do this by clobbering *PSEC to be | |
1629 | bfd_und_section_ptr. | |
1630 | ||
1631 | We treat a common symbol as a definition if the symbol in the | |
1632 | shared library is a function, since common symbols always | |
1633 | represent variables; this can cause confusion in principle, but | |
1634 | any such confusion would seem to indicate an erroneous program or | |
1635 | shared library. We also permit a common symbol in a regular | |
8170f769 | 1636 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1637 | |
1638 | if (newdyn | |
1639 | && newdef | |
77cfaee6 | 1640 | && (olddef |
45d6a902 | 1641 | || (h->root.type == bfd_link_hash_common |
8170f769 | 1642 | && (newweak || newfunc)))) |
45d6a902 AM |
1643 | { |
1644 | *override = TRUE; | |
1645 | newdef = FALSE; | |
1646 | newdyncommon = FALSE; | |
252b5132 | 1647 | |
45d6a902 AM |
1648 | *psec = sec = bfd_und_section_ptr; |
1649 | *size_change_ok = TRUE; | |
252b5132 | 1650 | |
45d6a902 AM |
1651 | /* If we get here when the old symbol is a common symbol, then |
1652 | we are explicitly letting it override a weak symbol or | |
1653 | function in a dynamic object, and we don't want to warn about | |
1654 | a type change. If the old symbol is a defined symbol, a type | |
1655 | change warning may still be appropriate. */ | |
252b5132 | 1656 | |
45d6a902 AM |
1657 | if (h->root.type == bfd_link_hash_common) |
1658 | *type_change_ok = TRUE; | |
1659 | } | |
1660 | ||
1661 | /* Handle the special case of an old common symbol merging with a | |
1662 | new symbol which looks like a common symbol in a shared object. | |
1663 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1664 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1665 | right thing. */ | |
45d6a902 AM |
1666 | |
1667 | if (newdyncommon | |
1668 | && h->root.type == bfd_link_hash_common) | |
1669 | { | |
1670 | *override = TRUE; | |
1671 | newdef = FALSE; | |
1672 | newdyncommon = FALSE; | |
1673 | *pvalue = sym->st_size; | |
a4d8e49b | 1674 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1675 | *size_change_ok = TRUE; |
1676 | } | |
1677 | ||
c5e2cead | 1678 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1679 | if (newdef && olddef && newweak) |
54ac0771 | 1680 | { |
35ed3f94 | 1681 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1682 | if (!(oldbfd != NULL |
1683 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1684 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1685 | { |
1686 | newdef = FALSE; | |
1687 | *skip = TRUE; | |
1688 | } | |
54ac0771 L |
1689 | |
1690 | /* Merge st_other. If the symbol already has a dynamic index, | |
1691 | but visibility says it should not be visible, turn it into a | |
1692 | local symbol. */ | |
b8417128 | 1693 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1694 | if (h->dynindx != -1) |
1695 | switch (ELF_ST_VISIBILITY (h->other)) | |
1696 | { | |
1697 | case STV_INTERNAL: | |
1698 | case STV_HIDDEN: | |
1699 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1700 | break; | |
1701 | } | |
1702 | } | |
c5e2cead | 1703 | |
45d6a902 AM |
1704 | /* If the old symbol is from a dynamic object, and the new symbol is |
1705 | a definition which is not from a dynamic object, then the new | |
1706 | symbol overrides the old symbol. Symbols from regular files | |
1707 | always take precedence over symbols from dynamic objects, even if | |
1708 | they are defined after the dynamic object in the link. | |
1709 | ||
1710 | As above, we again permit a common symbol in a regular object to | |
1711 | override a definition in a shared object if the shared object | |
0f8a2703 | 1712 | symbol is a function or is weak. */ |
45d6a902 AM |
1713 | |
1714 | flip = NULL; | |
77cfaee6 | 1715 | if (!newdyn |
45d6a902 AM |
1716 | && (newdef |
1717 | || (bfd_is_com_section (sec) | |
0a36a439 | 1718 | && (oldweak || oldfunc))) |
45d6a902 AM |
1719 | && olddyn |
1720 | && olddef | |
f5385ebf | 1721 | && h->def_dynamic) |
45d6a902 AM |
1722 | { |
1723 | /* Change the hash table entry to undefined, and let | |
1724 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1725 | new definition. */ | |
1726 | ||
1727 | h->root.type = bfd_link_hash_undefined; | |
1728 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1729 | *size_change_ok = TRUE; | |
1730 | ||
1731 | olddef = FALSE; | |
1732 | olddyncommon = FALSE; | |
1733 | ||
1734 | /* We again permit a type change when a common symbol may be | |
1735 | overriding a function. */ | |
1736 | ||
1737 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1738 | { |
1739 | if (oldfunc) | |
1740 | { | |
1741 | /* If a common symbol overrides a function, make sure | |
1742 | that it isn't defined dynamically nor has type | |
1743 | function. */ | |
1744 | h->def_dynamic = 0; | |
1745 | h->type = STT_NOTYPE; | |
1746 | } | |
1747 | *type_change_ok = TRUE; | |
1748 | } | |
45d6a902 | 1749 | |
6c9b78e6 AM |
1750 | if (hi->root.type == bfd_link_hash_indirect) |
1751 | flip = hi; | |
45d6a902 AM |
1752 | else |
1753 | /* This union may have been set to be non-NULL when this symbol | |
1754 | was seen in a dynamic object. We must force the union to be | |
1755 | NULL, so that it is correct for a regular symbol. */ | |
1756 | h->verinfo.vertree = NULL; | |
1757 | } | |
1758 | ||
1759 | /* Handle the special case of a new common symbol merging with an | |
1760 | old symbol that looks like it might be a common symbol defined in | |
1761 | a shared object. Note that we have already handled the case in | |
1762 | which a new common symbol should simply override the definition | |
1763 | in the shared library. */ | |
1764 | ||
1765 | if (! newdyn | |
1766 | && bfd_is_com_section (sec) | |
1767 | && olddyncommon) | |
1768 | { | |
1769 | /* It would be best if we could set the hash table entry to a | |
1770 | common symbol, but we don't know what to use for the section | |
1771 | or the alignment. */ | |
1a72702b AM |
1772 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1773 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1774 | |
4cc11e76 | 1775 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1776 | larger, pretend that the new symbol has its size. */ |
1777 | ||
1778 | if (h->size > *pvalue) | |
1779 | *pvalue = h->size; | |
1780 | ||
af44c138 L |
1781 | /* We need to remember the alignment required by the symbol |
1782 | in the dynamic object. */ | |
1783 | BFD_ASSERT (pold_alignment); | |
1784 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1785 | |
1786 | olddef = FALSE; | |
1787 | olddyncommon = FALSE; | |
1788 | ||
1789 | h->root.type = bfd_link_hash_undefined; | |
1790 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1791 | ||
1792 | *size_change_ok = TRUE; | |
1793 | *type_change_ok = TRUE; | |
1794 | ||
6c9b78e6 AM |
1795 | if (hi->root.type == bfd_link_hash_indirect) |
1796 | flip = hi; | |
45d6a902 AM |
1797 | else |
1798 | h->verinfo.vertree = NULL; | |
1799 | } | |
1800 | ||
1801 | if (flip != NULL) | |
1802 | { | |
1803 | /* Handle the case where we had a versioned symbol in a dynamic | |
1804 | library and now find a definition in a normal object. In this | |
1805 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1806 | flip->root.type = h->root.type; |
00cbee0a | 1807 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1808 | h->root.type = bfd_link_hash_indirect; |
1809 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1810 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1811 | if (h->def_dynamic) |
45d6a902 | 1812 | { |
f5385ebf AM |
1813 | h->def_dynamic = 0; |
1814 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1815 | } |
1816 | } | |
1817 | ||
45d6a902 AM |
1818 | return TRUE; |
1819 | } | |
1820 | ||
1821 | /* This function is called to create an indirect symbol from the | |
1822 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1823 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1824 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1825 | |
28caa186 | 1826 | static bfd_boolean |
268b6b39 AM |
1827 | _bfd_elf_add_default_symbol (bfd *abfd, |
1828 | struct bfd_link_info *info, | |
1829 | struct elf_link_hash_entry *h, | |
1830 | const char *name, | |
1831 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1832 | asection *sec, |
1833 | bfd_vma value, | |
1834 | bfd **poldbfd, | |
e3c9d234 | 1835 | bfd_boolean *dynsym) |
45d6a902 AM |
1836 | { |
1837 | bfd_boolean type_change_ok; | |
1838 | bfd_boolean size_change_ok; | |
1839 | bfd_boolean skip; | |
1840 | char *shortname; | |
1841 | struct elf_link_hash_entry *hi; | |
1842 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1843 | const struct elf_backend_data *bed; |
45d6a902 AM |
1844 | bfd_boolean collect; |
1845 | bfd_boolean dynamic; | |
e3c9d234 | 1846 | bfd_boolean override; |
45d6a902 AM |
1847 | char *p; |
1848 | size_t len, shortlen; | |
ffd65175 | 1849 | asection *tmp_sec; |
6e33951e | 1850 | bfd_boolean matched; |
45d6a902 | 1851 | |
422f1182 L |
1852 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1853 | return TRUE; | |
1854 | ||
45d6a902 AM |
1855 | /* If this symbol has a version, and it is the default version, we |
1856 | create an indirect symbol from the default name to the fully | |
1857 | decorated name. This will cause external references which do not | |
1858 | specify a version to be bound to this version of the symbol. */ | |
1859 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1860 | if (h->versioned == unknown) |
1861 | { | |
1862 | if (p == NULL) | |
1863 | { | |
1864 | h->versioned = unversioned; | |
1865 | return TRUE; | |
1866 | } | |
1867 | else | |
1868 | { | |
1869 | if (p[1] != ELF_VER_CHR) | |
1870 | { | |
1871 | h->versioned = versioned_hidden; | |
1872 | return TRUE; | |
1873 | } | |
1874 | else | |
1875 | h->versioned = versioned; | |
1876 | } | |
1877 | } | |
4373f8af L |
1878 | else |
1879 | { | |
1880 | /* PR ld/19073: We may see an unversioned definition after the | |
1881 | default version. */ | |
1882 | if (p == NULL) | |
1883 | return TRUE; | |
1884 | } | |
45d6a902 | 1885 | |
45d6a902 AM |
1886 | bed = get_elf_backend_data (abfd); |
1887 | collect = bed->collect; | |
1888 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1889 | ||
1890 | shortlen = p - name; | |
a50b1753 | 1891 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1892 | if (shortname == NULL) |
1893 | return FALSE; | |
1894 | memcpy (shortname, name, shortlen); | |
1895 | shortname[shortlen] = '\0'; | |
1896 | ||
1897 | /* We are going to create a new symbol. Merge it with any existing | |
1898 | symbol with this name. For the purposes of the merge, act as | |
1899 | though we were defining the symbol we just defined, although we | |
1900 | actually going to define an indirect symbol. */ | |
1901 | type_change_ok = FALSE; | |
1902 | size_change_ok = FALSE; | |
6e33951e | 1903 | matched = TRUE; |
ffd65175 AM |
1904 | tmp_sec = sec; |
1905 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1906 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1907 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1908 | return FALSE; |
1909 | ||
1910 | if (skip) | |
1911 | goto nondefault; | |
1912 | ||
5b677558 AM |
1913 | if (hi->def_regular) |
1914 | { | |
1915 | /* If the undecorated symbol will have a version added by a | |
1916 | script different to H, then don't indirect to/from the | |
1917 | undecorated symbol. This isn't ideal because we may not yet | |
1918 | have seen symbol versions, if given by a script on the | |
1919 | command line rather than via --version-script. */ | |
1920 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1921 | { | |
1922 | bfd_boolean hide; | |
1923 | ||
1924 | hi->verinfo.vertree | |
1925 | = bfd_find_version_for_sym (info->version_info, | |
1926 | hi->root.root.string, &hide); | |
1927 | if (hi->verinfo.vertree != NULL && hide) | |
1928 | { | |
1929 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1930 | goto nondefault; | |
1931 | } | |
1932 | } | |
1933 | if (hi->verinfo.vertree != NULL | |
1934 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1935 | goto nondefault; | |
1936 | } | |
1937 | ||
45d6a902 AM |
1938 | if (! override) |
1939 | { | |
c6e8a9a8 | 1940 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1941 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1942 | { |
1943 | bh = &hi->root; | |
1944 | if (! (_bfd_generic_link_add_one_symbol | |
1945 | (info, abfd, shortname, BSF_INDIRECT, | |
1946 | bfd_ind_section_ptr, | |
1947 | 0, name, FALSE, collect, &bh))) | |
1948 | return FALSE; | |
1949 | hi = (struct elf_link_hash_entry *) bh; | |
1950 | } | |
45d6a902 AM |
1951 | } |
1952 | else | |
1953 | { | |
1954 | /* In this case the symbol named SHORTNAME is overriding the | |
1955 | indirect symbol we want to add. We were planning on making | |
1956 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1957 | is the name without a version. NAME is the fully versioned | |
1958 | name, and it is the default version. | |
1959 | ||
1960 | Overriding means that we already saw a definition for the | |
1961 | symbol SHORTNAME in a regular object, and it is overriding | |
1962 | the symbol defined in the dynamic object. | |
1963 | ||
1964 | When this happens, we actually want to change NAME, the | |
1965 | symbol we just added, to refer to SHORTNAME. This will cause | |
1966 | references to NAME in the shared object to become references | |
1967 | to SHORTNAME in the regular object. This is what we expect | |
1968 | when we override a function in a shared object: that the | |
1969 | references in the shared object will be mapped to the | |
1970 | definition in the regular object. */ | |
1971 | ||
1972 | while (hi->root.type == bfd_link_hash_indirect | |
1973 | || hi->root.type == bfd_link_hash_warning) | |
1974 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1975 | ||
1976 | h->root.type = bfd_link_hash_indirect; | |
1977 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1978 | if (h->def_dynamic) |
45d6a902 | 1979 | { |
f5385ebf AM |
1980 | h->def_dynamic = 0; |
1981 | hi->ref_dynamic = 1; | |
1982 | if (hi->ref_regular | |
1983 | || hi->def_regular) | |
45d6a902 | 1984 | { |
c152c796 | 1985 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1986 | return FALSE; |
1987 | } | |
1988 | } | |
1989 | ||
1990 | /* Now set HI to H, so that the following code will set the | |
1991 | other fields correctly. */ | |
1992 | hi = h; | |
1993 | } | |
1994 | ||
fab4a87f L |
1995 | /* Check if HI is a warning symbol. */ |
1996 | if (hi->root.type == bfd_link_hash_warning) | |
1997 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1998 | ||
45d6a902 AM |
1999 | /* If there is a duplicate definition somewhere, then HI may not |
2000 | point to an indirect symbol. We will have reported an error to | |
2001 | the user in that case. */ | |
2002 | ||
2003 | if (hi->root.type == bfd_link_hash_indirect) | |
2004 | { | |
2005 | struct elf_link_hash_entry *ht; | |
2006 | ||
45d6a902 | 2007 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 2008 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 2009 | |
68c88cd4 AM |
2010 | /* A reference to the SHORTNAME symbol from a dynamic library |
2011 | will be satisfied by the versioned symbol at runtime. In | |
2012 | effect, we have a reference to the versioned symbol. */ | |
2013 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
2014 | hi->dynamic_def |= ht->dynamic_def; | |
2015 | ||
45d6a902 AM |
2016 | /* See if the new flags lead us to realize that the symbol must |
2017 | be dynamic. */ | |
2018 | if (! *dynsym) | |
2019 | { | |
2020 | if (! dynamic) | |
2021 | { | |
0e1862bb | 2022 | if (! bfd_link_executable (info) |
90c984fc | 2023 | || hi->def_dynamic |
f5385ebf | 2024 | || hi->ref_dynamic) |
45d6a902 AM |
2025 | *dynsym = TRUE; |
2026 | } | |
2027 | else | |
2028 | { | |
f5385ebf | 2029 | if (hi->ref_regular) |
45d6a902 AM |
2030 | *dynsym = TRUE; |
2031 | } | |
2032 | } | |
2033 | } | |
2034 | ||
2035 | /* We also need to define an indirection from the nondefault version | |
2036 | of the symbol. */ | |
2037 | ||
2038 | nondefault: | |
2039 | len = strlen (name); | |
a50b1753 | 2040 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
2041 | if (shortname == NULL) |
2042 | return FALSE; | |
2043 | memcpy (shortname, name, shortlen); | |
2044 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
2045 | ||
2046 | /* Once again, merge with any existing symbol. */ | |
2047 | type_change_ok = FALSE; | |
2048 | size_change_ok = FALSE; | |
ffd65175 AM |
2049 | tmp_sec = sec; |
2050 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 2051 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 2052 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
2053 | return FALSE; |
2054 | ||
2055 | if (skip) | |
2056 | return TRUE; | |
2057 | ||
2058 | if (override) | |
2059 | { | |
2060 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
2061 | the type of override we do in the case above unless it is | |
4cc11e76 | 2062 | overridden by a versioned definition. */ |
45d6a902 AM |
2063 | if (hi->root.type != bfd_link_hash_defined |
2064 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 2065 | _bfd_error_handler |
695344c0 | 2066 | /* xgettext:c-format */ |
871b3ab2 | 2067 | (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"), |
d003868e | 2068 | abfd, shortname); |
45d6a902 AM |
2069 | } |
2070 | else | |
2071 | { | |
2072 | bh = &hi->root; | |
2073 | if (! (_bfd_generic_link_add_one_symbol | |
2074 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 2075 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
2076 | return FALSE; |
2077 | hi = (struct elf_link_hash_entry *) bh; | |
2078 | ||
2079 | /* If there is a duplicate definition somewhere, then HI may not | |
2080 | point to an indirect symbol. We will have reported an error | |
2081 | to the user in that case. */ | |
2082 | ||
2083 | if (hi->root.type == bfd_link_hash_indirect) | |
2084 | { | |
fcfa13d2 | 2085 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
2086 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
2087 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
2088 | |
2089 | /* See if the new flags lead us to realize that the symbol | |
2090 | must be dynamic. */ | |
2091 | if (! *dynsym) | |
2092 | { | |
2093 | if (! dynamic) | |
2094 | { | |
0e1862bb | 2095 | if (! bfd_link_executable (info) |
f5385ebf | 2096 | || hi->ref_dynamic) |
45d6a902 AM |
2097 | *dynsym = TRUE; |
2098 | } | |
2099 | else | |
2100 | { | |
f5385ebf | 2101 | if (hi->ref_regular) |
45d6a902 AM |
2102 | *dynsym = TRUE; |
2103 | } | |
2104 | } | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | return TRUE; | |
2109 | } | |
2110 | \f | |
2111 | /* This routine is used to export all defined symbols into the dynamic | |
2112 | symbol table. It is called via elf_link_hash_traverse. */ | |
2113 | ||
28caa186 | 2114 | static bfd_boolean |
268b6b39 | 2115 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2116 | { |
a50b1753 | 2117 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
2118 | |
2119 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2120 | if (h->root.type == bfd_link_hash_indirect) | |
2121 | return TRUE; | |
2122 | ||
7686d77d AM |
2123 | /* Ignore this if we won't export it. */ |
2124 | if (!eif->info->export_dynamic && !h->dynamic) | |
2125 | return TRUE; | |
45d6a902 AM |
2126 | |
2127 | if (h->dynindx == -1 | |
fd91d419 L |
2128 | && (h->def_regular || h->ref_regular) |
2129 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2130 | h->root.root.string)) | |
45d6a902 | 2131 | { |
fd91d419 | 2132 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2133 | { |
fd91d419 L |
2134 | eif->failed = TRUE; |
2135 | return FALSE; | |
45d6a902 AM |
2136 | } |
2137 | } | |
2138 | ||
2139 | return TRUE; | |
2140 | } | |
2141 | \f | |
2142 | /* Look through the symbols which are defined in other shared | |
2143 | libraries and referenced here. Update the list of version | |
2144 | dependencies. This will be put into the .gnu.version_r section. | |
2145 | This function is called via elf_link_hash_traverse. */ | |
2146 | ||
28caa186 | 2147 | static bfd_boolean |
268b6b39 AM |
2148 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2149 | void *data) | |
45d6a902 | 2150 | { |
a50b1753 | 2151 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2152 | Elf_Internal_Verneed *t; |
2153 | Elf_Internal_Vernaux *a; | |
2154 | bfd_size_type amt; | |
2155 | ||
45d6a902 AM |
2156 | /* We only care about symbols defined in shared objects with version |
2157 | information. */ | |
f5385ebf AM |
2158 | if (!h->def_dynamic |
2159 | || h->def_regular | |
45d6a902 | 2160 | || h->dynindx == -1 |
7b20f099 AM |
2161 | || h->verinfo.verdef == NULL |
2162 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2163 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2164 | return TRUE; |
2165 | ||
2166 | /* See if we already know about this version. */ | |
28caa186 AM |
2167 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2168 | t != NULL; | |
2169 | t = t->vn_nextref) | |
45d6a902 AM |
2170 | { |
2171 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2172 | continue; | |
2173 | ||
2174 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2175 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2176 | return TRUE; | |
2177 | ||
2178 | break; | |
2179 | } | |
2180 | ||
2181 | /* This is a new version. Add it to tree we are building. */ | |
2182 | ||
2183 | if (t == NULL) | |
2184 | { | |
2185 | amt = sizeof *t; | |
a50b1753 | 2186 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2187 | if (t == NULL) |
2188 | { | |
2189 | rinfo->failed = TRUE; | |
2190 | return FALSE; | |
2191 | } | |
2192 | ||
2193 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2194 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2195 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2196 | } |
2197 | ||
2198 | amt = sizeof *a; | |
a50b1753 | 2199 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2200 | if (a == NULL) |
2201 | { | |
2202 | rinfo->failed = TRUE; | |
2203 | return FALSE; | |
2204 | } | |
45d6a902 AM |
2205 | |
2206 | /* Note that we are copying a string pointer here, and testing it | |
2207 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2208 | discard the string data when low in memory, this will have to be | |
2209 | fixed. */ | |
2210 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2211 | ||
2212 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2213 | a->vna_nextptr = t->vn_auxptr; | |
2214 | ||
2215 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2216 | ++rinfo->vers; | |
2217 | ||
2218 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2219 | ||
2220 | t->vn_auxptr = a; | |
2221 | ||
2222 | return TRUE; | |
2223 | } | |
2224 | ||
099bb8fb L |
2225 | /* Return TRUE and set *HIDE to TRUE if the versioned symbol is |
2226 | hidden. Set *T_P to NULL if there is no match. */ | |
2227 | ||
2228 | static bfd_boolean | |
2229 | _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info, | |
2230 | struct elf_link_hash_entry *h, | |
2231 | const char *version_p, | |
2232 | struct bfd_elf_version_tree **t_p, | |
2233 | bfd_boolean *hide) | |
2234 | { | |
2235 | struct bfd_elf_version_tree *t; | |
2236 | ||
2237 | /* Look for the version. If we find it, it is no longer weak. */ | |
2238 | for (t = info->version_info; t != NULL; t = t->next) | |
2239 | { | |
2240 | if (strcmp (t->name, version_p) == 0) | |
2241 | { | |
2242 | size_t len; | |
2243 | char *alc; | |
2244 | struct bfd_elf_version_expr *d; | |
2245 | ||
2246 | len = version_p - h->root.root.string; | |
2247 | alc = (char *) bfd_malloc (len); | |
2248 | if (alc == NULL) | |
2249 | return FALSE; | |
2250 | memcpy (alc, h->root.root.string, len - 1); | |
2251 | alc[len - 1] = '\0'; | |
2252 | if (alc[len - 2] == ELF_VER_CHR) | |
2253 | alc[len - 2] = '\0'; | |
2254 | ||
2255 | h->verinfo.vertree = t; | |
2256 | t->used = TRUE; | |
2257 | d = NULL; | |
2258 | ||
2259 | if (t->globals.list != NULL) | |
2260 | d = (*t->match) (&t->globals, NULL, alc); | |
2261 | ||
2262 | /* See if there is anything to force this symbol to | |
2263 | local scope. */ | |
2264 | if (d == NULL && t->locals.list != NULL) | |
2265 | { | |
2266 | d = (*t->match) (&t->locals, NULL, alc); | |
2267 | if (d != NULL | |
2268 | && h->dynindx != -1 | |
2269 | && ! info->export_dynamic) | |
2270 | *hide = TRUE; | |
2271 | } | |
2272 | ||
2273 | free (alc); | |
2274 | break; | |
2275 | } | |
2276 | } | |
2277 | ||
2278 | *t_p = t; | |
2279 | ||
2280 | return TRUE; | |
2281 | } | |
2282 | ||
2283 | /* Return TRUE if the symbol H is hidden by version script. */ | |
2284 | ||
2285 | bfd_boolean | |
2286 | _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info, | |
2287 | struct elf_link_hash_entry *h) | |
2288 | { | |
2289 | const char *p; | |
2290 | bfd_boolean hide = FALSE; | |
2291 | const struct elf_backend_data *bed | |
2292 | = get_elf_backend_data (info->output_bfd); | |
2293 | ||
2294 | /* Version script only hides symbols defined in regular objects. */ | |
2295 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) | |
2296 | return TRUE; | |
2297 | ||
2298 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
2299 | if (p != NULL && h->verinfo.vertree == NULL) | |
2300 | { | |
2301 | struct bfd_elf_version_tree *t; | |
2302 | ||
2303 | ++p; | |
2304 | if (*p == ELF_VER_CHR) | |
2305 | ++p; | |
2306 | ||
2307 | if (*p != '\0' | |
2308 | && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide) | |
2309 | && hide) | |
2310 | { | |
2311 | if (hide) | |
2312 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2313 | return TRUE; | |
2314 | } | |
2315 | } | |
2316 | ||
2317 | /* If we don't have a version for this symbol, see if we can find | |
2318 | something. */ | |
2319 | if (h->verinfo.vertree == NULL && info->version_info != NULL) | |
2320 | { | |
2321 | h->verinfo.vertree | |
2322 | = bfd_find_version_for_sym (info->version_info, | |
2323 | h->root.root.string, &hide); | |
2324 | if (h->verinfo.vertree != NULL && hide) | |
2325 | { | |
2326 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2327 | return TRUE; | |
2328 | } | |
2329 | } | |
2330 | ||
2331 | return FALSE; | |
2332 | } | |
2333 | ||
45d6a902 AM |
2334 | /* Figure out appropriate versions for all the symbols. We may not |
2335 | have the version number script until we have read all of the input | |
2336 | files, so until that point we don't know which symbols should be | |
2337 | local. This function is called via elf_link_hash_traverse. */ | |
2338 | ||
28caa186 | 2339 | static bfd_boolean |
268b6b39 | 2340 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2341 | { |
28caa186 | 2342 | struct elf_info_failed *sinfo; |
45d6a902 | 2343 | struct bfd_link_info *info; |
9c5bfbb7 | 2344 | const struct elf_backend_data *bed; |
45d6a902 AM |
2345 | struct elf_info_failed eif; |
2346 | char *p; | |
099bb8fb | 2347 | bfd_boolean hide; |
45d6a902 | 2348 | |
a50b1753 | 2349 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2350 | info = sinfo->info; |
2351 | ||
45d6a902 AM |
2352 | /* Fix the symbol flags. */ |
2353 | eif.failed = FALSE; | |
2354 | eif.info = info; | |
2355 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2356 | { | |
2357 | if (eif.failed) | |
2358 | sinfo->failed = TRUE; | |
2359 | return FALSE; | |
2360 | } | |
2361 | ||
2362 | /* We only need version numbers for symbols defined in regular | |
2363 | objects. */ | |
f5385ebf | 2364 | if (!h->def_regular) |
45d6a902 AM |
2365 | return TRUE; |
2366 | ||
099bb8fb | 2367 | hide = FALSE; |
28caa186 | 2368 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
2369 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2370 | if (p != NULL && h->verinfo.vertree == NULL) | |
2371 | { | |
2372 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2373 | |
45d6a902 AM |
2374 | ++p; |
2375 | if (*p == ELF_VER_CHR) | |
6e33951e | 2376 | ++p; |
45d6a902 AM |
2377 | |
2378 | /* If there is no version string, we can just return out. */ | |
2379 | if (*p == '\0') | |
6e33951e | 2380 | return TRUE; |
45d6a902 | 2381 | |
099bb8fb | 2382 | if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)) |
45d6a902 | 2383 | { |
099bb8fb L |
2384 | sinfo->failed = TRUE; |
2385 | return FALSE; | |
45d6a902 AM |
2386 | } |
2387 | ||
099bb8fb L |
2388 | if (hide) |
2389 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2390 | ||
45d6a902 AM |
2391 | /* If we are building an application, we need to create a |
2392 | version node for this version. */ | |
0e1862bb | 2393 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2394 | { |
2395 | struct bfd_elf_version_tree **pp; | |
2396 | int version_index; | |
2397 | ||
2398 | /* If we aren't going to export this symbol, we don't need | |
2399 | to worry about it. */ | |
2400 | if (h->dynindx == -1) | |
2401 | return TRUE; | |
2402 | ||
ef53be89 AM |
2403 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2404 | sizeof *t); | |
45d6a902 AM |
2405 | if (t == NULL) |
2406 | { | |
2407 | sinfo->failed = TRUE; | |
2408 | return FALSE; | |
2409 | } | |
2410 | ||
45d6a902 | 2411 | t->name = p; |
45d6a902 AM |
2412 | t->name_indx = (unsigned int) -1; |
2413 | t->used = TRUE; | |
2414 | ||
2415 | version_index = 1; | |
2416 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2417 | if (sinfo->info->version_info != NULL |
2418 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2419 | version_index = 0; |
fd91d419 L |
2420 | for (pp = &sinfo->info->version_info; |
2421 | *pp != NULL; | |
2422 | pp = &(*pp)->next) | |
45d6a902 AM |
2423 | ++version_index; |
2424 | t->vernum = version_index; | |
2425 | ||
2426 | *pp = t; | |
2427 | ||
2428 | h->verinfo.vertree = t; | |
2429 | } | |
2430 | else if (t == NULL) | |
2431 | { | |
2432 | /* We could not find the version for a symbol when | |
2433 | generating a shared archive. Return an error. */ | |
4eca0228 | 2434 | _bfd_error_handler |
695344c0 | 2435 | /* xgettext:c-format */ |
871b3ab2 | 2436 | (_("%pB: version node not found for symbol %s"), |
28caa186 | 2437 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2438 | bfd_set_error (bfd_error_bad_value); |
2439 | sinfo->failed = TRUE; | |
2440 | return FALSE; | |
2441 | } | |
45d6a902 AM |
2442 | } |
2443 | ||
2444 | /* If we don't have a version for this symbol, see if we can find | |
2445 | something. */ | |
099bb8fb L |
2446 | if (!hide |
2447 | && h->verinfo.vertree == NULL | |
2448 | && sinfo->info->version_info != NULL) | |
45d6a902 | 2449 | { |
fd91d419 L |
2450 | h->verinfo.vertree |
2451 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2452 | h->root.root.string, &hide); | |
1e8fa21e AM |
2453 | if (h->verinfo.vertree != NULL && hide) |
2454 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2455 | } |
2456 | ||
2457 | return TRUE; | |
2458 | } | |
2459 | \f | |
45d6a902 AM |
2460 | /* Read and swap the relocs from the section indicated by SHDR. This |
2461 | may be either a REL or a RELA section. The relocations are | |
2462 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2463 | which should have already been allocated to contain enough space. | |
2464 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2465 | relocations should be stored. | |
2466 | ||
2467 | Returns FALSE if something goes wrong. */ | |
2468 | ||
2469 | static bfd_boolean | |
268b6b39 | 2470 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2471 | asection *sec, |
268b6b39 AM |
2472 | Elf_Internal_Shdr *shdr, |
2473 | void *external_relocs, | |
2474 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2475 | { |
9c5bfbb7 | 2476 | const struct elf_backend_data *bed; |
268b6b39 | 2477 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2478 | const bfd_byte *erela; |
2479 | const bfd_byte *erelaend; | |
2480 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2481 | Elf_Internal_Shdr *symtab_hdr; |
2482 | size_t nsyms; | |
45d6a902 | 2483 | |
45d6a902 AM |
2484 | /* Position ourselves at the start of the section. */ |
2485 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2486 | return FALSE; | |
2487 | ||
2488 | /* Read the relocations. */ | |
2489 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2490 | return FALSE; | |
2491 | ||
243ef1e0 | 2492 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2493 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2494 | |
45d6a902 AM |
2495 | bed = get_elf_backend_data (abfd); |
2496 | ||
2497 | /* Convert the external relocations to the internal format. */ | |
2498 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2499 | swap_in = bed->s->swap_reloc_in; | |
2500 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2501 | swap_in = bed->s->swap_reloca_in; | |
2502 | else | |
2503 | { | |
2504 | bfd_set_error (bfd_error_wrong_format); | |
2505 | return FALSE; | |
2506 | } | |
2507 | ||
a50b1753 | 2508 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2509 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2510 | irela = internal_relocs; |
2511 | while (erela < erelaend) | |
2512 | { | |
243ef1e0 L |
2513 | bfd_vma r_symndx; |
2514 | ||
45d6a902 | 2515 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2516 | r_symndx = ELF32_R_SYM (irela->r_info); |
2517 | if (bed->s->arch_size == 64) | |
2518 | r_symndx >>= 24; | |
ce98a316 NC |
2519 | if (nsyms > 0) |
2520 | { | |
2521 | if ((size_t) r_symndx >= nsyms) | |
2522 | { | |
4eca0228 | 2523 | _bfd_error_handler |
695344c0 | 2524 | /* xgettext:c-format */ |
2dcf00ce AM |
2525 | (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)" |
2526 | " for offset %#" PRIx64 " in section `%pA'"), | |
2527 | abfd, (uint64_t) r_symndx, (unsigned long) nsyms, | |
2528 | (uint64_t) irela->r_offset, sec); | |
ce98a316 NC |
2529 | bfd_set_error (bfd_error_bad_value); |
2530 | return FALSE; | |
2531 | } | |
2532 | } | |
cf35638d | 2533 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2534 | { |
4eca0228 | 2535 | _bfd_error_handler |
695344c0 | 2536 | /* xgettext:c-format */ |
2dcf00ce AM |
2537 | (_("%pB: non-zero symbol index (%#" PRIx64 ")" |
2538 | " for offset %#" PRIx64 " in section `%pA'" | |
ce98a316 | 2539 | " when the object file has no symbol table"), |
2dcf00ce AM |
2540 | abfd, (uint64_t) r_symndx, |
2541 | (uint64_t) irela->r_offset, sec); | |
243ef1e0 L |
2542 | bfd_set_error (bfd_error_bad_value); |
2543 | return FALSE; | |
2544 | } | |
45d6a902 AM |
2545 | irela += bed->s->int_rels_per_ext_rel; |
2546 | erela += shdr->sh_entsize; | |
2547 | } | |
2548 | ||
2549 | return TRUE; | |
2550 | } | |
2551 | ||
2552 | /* Read and swap the relocs for a section O. They may have been | |
2553 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2554 | not NULL, they are used as buffers to read into. They are known to | |
2555 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2556 | the return value is allocated using either malloc or bfd_alloc, | |
2557 | according to the KEEP_MEMORY argument. If O has two relocation | |
2558 | sections (both REL and RELA relocations), then the REL_HDR | |
2559 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2560 | RELA_HDR relocations. */ |
45d6a902 AM |
2561 | |
2562 | Elf_Internal_Rela * | |
268b6b39 AM |
2563 | _bfd_elf_link_read_relocs (bfd *abfd, |
2564 | asection *o, | |
2565 | void *external_relocs, | |
2566 | Elf_Internal_Rela *internal_relocs, | |
2567 | bfd_boolean keep_memory) | |
45d6a902 | 2568 | { |
268b6b39 | 2569 | void *alloc1 = NULL; |
45d6a902 | 2570 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2571 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2572 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2573 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2574 | |
d4730f92 BS |
2575 | if (esdo->relocs != NULL) |
2576 | return esdo->relocs; | |
45d6a902 AM |
2577 | |
2578 | if (o->reloc_count == 0) | |
2579 | return NULL; | |
2580 | ||
45d6a902 AM |
2581 | if (internal_relocs == NULL) |
2582 | { | |
2583 | bfd_size_type size; | |
2584 | ||
056bafd4 | 2585 | size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); |
45d6a902 | 2586 | if (keep_memory) |
a50b1753 | 2587 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2588 | else |
a50b1753 | 2589 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2590 | if (internal_relocs == NULL) |
2591 | goto error_return; | |
2592 | } | |
2593 | ||
2594 | if (external_relocs == NULL) | |
2595 | { | |
d4730f92 BS |
2596 | bfd_size_type size = 0; |
2597 | ||
2598 | if (esdo->rel.hdr) | |
2599 | size += esdo->rel.hdr->sh_size; | |
2600 | if (esdo->rela.hdr) | |
2601 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2602 | |
268b6b39 | 2603 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2604 | if (alloc1 == NULL) |
2605 | goto error_return; | |
2606 | external_relocs = alloc1; | |
2607 | } | |
2608 | ||
d4730f92 BS |
2609 | internal_rela_relocs = internal_relocs; |
2610 | if (esdo->rel.hdr) | |
2611 | { | |
2612 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2613 | external_relocs, | |
2614 | internal_relocs)) | |
2615 | goto error_return; | |
2616 | external_relocs = (((bfd_byte *) external_relocs) | |
2617 | + esdo->rel.hdr->sh_size); | |
2618 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2619 | * bed->s->int_rels_per_ext_rel); | |
2620 | } | |
2621 | ||
2622 | if (esdo->rela.hdr | |
2623 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2624 | external_relocs, | |
2625 | internal_rela_relocs))) | |
45d6a902 AM |
2626 | goto error_return; |
2627 | ||
2628 | /* Cache the results for next time, if we can. */ | |
2629 | if (keep_memory) | |
d4730f92 | 2630 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2631 | |
2632 | if (alloc1 != NULL) | |
2633 | free (alloc1); | |
2634 | ||
2635 | /* Don't free alloc2, since if it was allocated we are passing it | |
2636 | back (under the name of internal_relocs). */ | |
2637 | ||
2638 | return internal_relocs; | |
2639 | ||
2640 | error_return: | |
2641 | if (alloc1 != NULL) | |
2642 | free (alloc1); | |
2643 | if (alloc2 != NULL) | |
4dd07732 AM |
2644 | { |
2645 | if (keep_memory) | |
2646 | bfd_release (abfd, alloc2); | |
2647 | else | |
2648 | free (alloc2); | |
2649 | } | |
45d6a902 AM |
2650 | return NULL; |
2651 | } | |
2652 | ||
2653 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2654 | section header for a section containing relocations for O. */ | |
2655 | ||
28caa186 | 2656 | static bfd_boolean |
9eaff861 AO |
2657 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2658 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2659 | { |
9eaff861 | 2660 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2661 | |
2662 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2663 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2664 | |
2665 | /* The contents field must last into write_object_contents, so we | |
2666 | allocate it with bfd_alloc rather than malloc. Also since we | |
2667 | cannot be sure that the contents will actually be filled in, | |
2668 | we zero the allocated space. */ | |
a50b1753 | 2669 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2670 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2671 | return FALSE; | |
2672 | ||
d4730f92 | 2673 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2674 | { |
2675 | struct elf_link_hash_entry **p; | |
2676 | ||
ca4be51c AM |
2677 | p = ((struct elf_link_hash_entry **) |
2678 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2679 | if (p == NULL) |
2680 | return FALSE; | |
2681 | ||
d4730f92 | 2682 | reldata->hashes = p; |
45d6a902 AM |
2683 | } |
2684 | ||
2685 | return TRUE; | |
2686 | } | |
2687 | ||
2688 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2689 | originated from the section given by INPUT_REL_HDR) to the | |
2690 | OUTPUT_BFD. */ | |
2691 | ||
2692 | bfd_boolean | |
268b6b39 AM |
2693 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2694 | asection *input_section, | |
2695 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2696 | Elf_Internal_Rela *internal_relocs, |
2697 | struct elf_link_hash_entry **rel_hash | |
2698 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2699 | { |
2700 | Elf_Internal_Rela *irela; | |
2701 | Elf_Internal_Rela *irelaend; | |
2702 | bfd_byte *erel; | |
d4730f92 | 2703 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2704 | asection *output_section; |
9c5bfbb7 | 2705 | const struct elf_backend_data *bed; |
268b6b39 | 2706 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2707 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2708 | |
2709 | output_section = input_section->output_section; | |
45d6a902 | 2710 | |
d4730f92 BS |
2711 | bed = get_elf_backend_data (output_bfd); |
2712 | esdo = elf_section_data (output_section); | |
2713 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2714 | { |
d4730f92 BS |
2715 | output_reldata = &esdo->rel; |
2716 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2717 | } |
d4730f92 BS |
2718 | else if (esdo->rela.hdr |
2719 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2720 | { |
d4730f92 BS |
2721 | output_reldata = &esdo->rela; |
2722 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2723 | } |
2724 | else | |
2725 | { | |
4eca0228 | 2726 | _bfd_error_handler |
695344c0 | 2727 | /* xgettext:c-format */ |
871b3ab2 | 2728 | (_("%pB: relocation size mismatch in %pB section %pA"), |
d003868e | 2729 | output_bfd, input_section->owner, input_section); |
297d8443 | 2730 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2731 | return FALSE; |
2732 | } | |
2733 | ||
d4730f92 BS |
2734 | erel = output_reldata->hdr->contents; |
2735 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2736 | irela = internal_relocs; |
2737 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2738 | * bed->s->int_rels_per_ext_rel); | |
2739 | while (irela < irelaend) | |
2740 | { | |
2741 | (*swap_out) (output_bfd, irela, erel); | |
2742 | irela += bed->s->int_rels_per_ext_rel; | |
2743 | erel += input_rel_hdr->sh_entsize; | |
2744 | } | |
2745 | ||
2746 | /* Bump the counter, so that we know where to add the next set of | |
2747 | relocations. */ | |
d4730f92 | 2748 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2749 | |
2750 | return TRUE; | |
2751 | } | |
2752 | \f | |
508c3946 L |
2753 | /* Make weak undefined symbols in PIE dynamic. */ |
2754 | ||
2755 | bfd_boolean | |
2756 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2757 | struct elf_link_hash_entry *h) | |
2758 | { | |
0e1862bb | 2759 | if (bfd_link_pie (info) |
508c3946 L |
2760 | && h->dynindx == -1 |
2761 | && h->root.type == bfd_link_hash_undefweak) | |
2762 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2763 | ||
2764 | return TRUE; | |
2765 | } | |
2766 | ||
45d6a902 AM |
2767 | /* Fix up the flags for a symbol. This handles various cases which |
2768 | can only be fixed after all the input files are seen. This is | |
2769 | currently called by both adjust_dynamic_symbol and | |
2770 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2771 | the face of future changes. */ | |
2772 | ||
28caa186 | 2773 | static bfd_boolean |
268b6b39 AM |
2774 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2775 | struct elf_info_failed *eif) | |
45d6a902 | 2776 | { |
33774f08 | 2777 | const struct elf_backend_data *bed; |
508c3946 | 2778 | |
45d6a902 AM |
2779 | /* If this symbol was mentioned in a non-ELF file, try to set |
2780 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2781 | permit a non-ELF file to correctly refer to a symbol defined in | |
2782 | an ELF dynamic object. */ | |
f5385ebf | 2783 | if (h->non_elf) |
45d6a902 AM |
2784 | { |
2785 | while (h->root.type == bfd_link_hash_indirect) | |
2786 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2787 | ||
2788 | if (h->root.type != bfd_link_hash_defined | |
2789 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2790 | { |
2791 | h->ref_regular = 1; | |
2792 | h->ref_regular_nonweak = 1; | |
2793 | } | |
45d6a902 AM |
2794 | else |
2795 | { | |
2796 | if (h->root.u.def.section->owner != NULL | |
2797 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2798 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2799 | { |
2800 | h->ref_regular = 1; | |
2801 | h->ref_regular_nonweak = 1; | |
2802 | } | |
45d6a902 | 2803 | else |
f5385ebf | 2804 | h->def_regular = 1; |
45d6a902 AM |
2805 | } |
2806 | ||
2807 | if (h->dynindx == -1 | |
f5385ebf AM |
2808 | && (h->def_dynamic |
2809 | || h->ref_dynamic)) | |
45d6a902 | 2810 | { |
c152c796 | 2811 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2812 | { |
2813 | eif->failed = TRUE; | |
2814 | return FALSE; | |
2815 | } | |
2816 | } | |
2817 | } | |
2818 | else | |
2819 | { | |
f5385ebf | 2820 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2821 | was first seen in a non-ELF file. Fortunately, if the symbol |
2822 | was first seen in an ELF file, we're probably OK unless the | |
2823 | symbol was defined in a non-ELF file. Catch that case here. | |
2824 | FIXME: We're still in trouble if the symbol was first seen in | |
2825 | a dynamic object, and then later in a non-ELF regular object. */ | |
2826 | if ((h->root.type == bfd_link_hash_defined | |
2827 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2828 | && !h->def_regular |
45d6a902 AM |
2829 | && (h->root.u.def.section->owner != NULL |
2830 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2831 | != bfd_target_elf_flavour) | |
2832 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2833 | && !h->def_dynamic))) |
2834 | h->def_regular = 1; | |
45d6a902 AM |
2835 | } |
2836 | ||
508c3946 | 2837 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2838 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2839 | if (bed->elf_backend_fixup_symbol | |
2840 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2841 | return FALSE; | |
508c3946 | 2842 | |
45d6a902 AM |
2843 | /* If this is a final link, and the symbol was defined as a common |
2844 | symbol in a regular object file, and there was no definition in | |
2845 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2846 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2847 | flag will not have been set. */ |
2848 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2849 | && !h->def_regular |
2850 | && h->ref_regular | |
2851 | && !h->def_dynamic | |
96f29d96 | 2852 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2853 | h->def_regular = 1; |
45d6a902 | 2854 | |
4deb8f71 L |
2855 | /* If a weak undefined symbol has non-default visibility, we also |
2856 | hide it from the dynamic linker. */ | |
2857 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
2858 | && h->root.type == bfd_link_hash_undefweak) | |
2859 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2860 | ||
2861 | /* A hidden versioned symbol in executable should be forced local if | |
2862 | it is is locally defined, not referenced by shared library and not | |
2863 | exported. */ | |
2864 | else if (bfd_link_executable (eif->info) | |
2865 | && h->versioned == versioned_hidden | |
2866 | && !eif->info->export_dynamic | |
2867 | && !h->dynamic | |
2868 | && !h->ref_dynamic | |
2869 | && h->def_regular) | |
2870 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2871 | ||
45d6a902 AM |
2872 | /* If -Bsymbolic was used (which means to bind references to global |
2873 | symbols to the definition within the shared object), and this | |
2874 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2875 | need a PLT entry. Likewise, if the symbol has non-default |
2876 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2877 | will force it local. */ |
4deb8f71 L |
2878 | else if (h->needs_plt |
2879 | && bfd_link_pic (eif->info) | |
2880 | && is_elf_hash_table (eif->info->hash) | |
2881 | && (SYMBOLIC_BIND (eif->info, h) | |
2882 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
2883 | && h->def_regular) | |
45d6a902 | 2884 | { |
45d6a902 AM |
2885 | bfd_boolean force_local; |
2886 | ||
45d6a902 AM |
2887 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2888 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2889 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2890 | } | |
2891 | ||
45d6a902 AM |
2892 | /* If this is a weak defined symbol in a dynamic object, and we know |
2893 | the real definition in the dynamic object, copy interesting flags | |
2894 | over to the real definition. */ | |
60d67dc8 | 2895 | if (h->is_weakalias) |
45d6a902 | 2896 | { |
60d67dc8 AM |
2897 | struct elf_link_hash_entry *def = weakdef (h); |
2898 | ||
45d6a902 AM |
2899 | /* If the real definition is defined by a regular object file, |
2900 | don't do anything special. See the longer description in | |
2901 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
60d67dc8 AM |
2902 | if (def->def_regular) |
2903 | { | |
2904 | h = def; | |
2905 | while ((h = h->u.alias) != def) | |
2906 | h->is_weakalias = 0; | |
2907 | } | |
45d6a902 | 2908 | else |
a26587ba | 2909 | { |
4e6b54a6 AM |
2910 | while (h->root.type == bfd_link_hash_indirect) |
2911 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4e6b54a6 AM |
2912 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
2913 | || h->root.type == bfd_link_hash_defweak); | |
60d67dc8 AM |
2914 | BFD_ASSERT (def->def_dynamic); |
2915 | BFD_ASSERT (def->root.type == bfd_link_hash_defined); | |
2916 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); | |
a26587ba | 2917 | } |
45d6a902 AM |
2918 | } |
2919 | ||
2920 | return TRUE; | |
2921 | } | |
2922 | ||
2923 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2924 | called via elf_link_hash_traverse, and also calls itself | |
2925 | recursively. */ | |
2926 | ||
28caa186 | 2927 | static bfd_boolean |
268b6b39 | 2928 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2929 | { |
a50b1753 | 2930 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
559192d8 | 2931 | struct elf_link_hash_table *htab; |
9c5bfbb7 | 2932 | const struct elf_backend_data *bed; |
45d6a902 | 2933 | |
0eddce27 | 2934 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2935 | return FALSE; |
2936 | ||
45d6a902 AM |
2937 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2938 | if (h->root.type == bfd_link_hash_indirect) | |
2939 | return TRUE; | |
2940 | ||
2941 | /* Fix the symbol flags. */ | |
2942 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2943 | return FALSE; | |
2944 | ||
559192d8 AM |
2945 | htab = elf_hash_table (eif->info); |
2946 | bed = get_elf_backend_data (htab->dynobj); | |
2947 | ||
954b63d4 AM |
2948 | if (h->root.type == bfd_link_hash_undefweak) |
2949 | { | |
2950 | if (eif->info->dynamic_undefined_weak == 0) | |
559192d8 | 2951 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
954b63d4 AM |
2952 | else if (eif->info->dynamic_undefined_weak > 0 |
2953 | && h->ref_regular | |
2954 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2955 | && !bfd_hide_sym_by_version (eif->info->version_info, | |
2956 | h->root.root.string)) | |
2957 | { | |
2958 | if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2959 | { | |
2960 | eif->failed = TRUE; | |
2961 | return FALSE; | |
2962 | } | |
2963 | } | |
2964 | } | |
2965 | ||
45d6a902 AM |
2966 | /* If this symbol does not require a PLT entry, and it is not |
2967 | defined by a dynamic object, or is not referenced by a regular | |
2968 | object, ignore it. We do have to handle a weak defined symbol, | |
2969 | even if no regular object refers to it, if we decided to add it | |
2970 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2971 | about symbols which are defined by one dynamic object and | |
2972 | referenced by another one? */ | |
f5385ebf | 2973 | if (!h->needs_plt |
91e21fb7 | 2974 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2975 | && (h->def_regular |
2976 | || !h->def_dynamic | |
2977 | || (!h->ref_regular | |
60d67dc8 | 2978 | && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) |
45d6a902 | 2979 | { |
a6aa5195 | 2980 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2981 | return TRUE; |
2982 | } | |
2983 | ||
2984 | /* If we've already adjusted this symbol, don't do it again. This | |
2985 | can happen via a recursive call. */ | |
f5385ebf | 2986 | if (h->dynamic_adjusted) |
45d6a902 AM |
2987 | return TRUE; |
2988 | ||
2989 | /* Don't look at this symbol again. Note that we must set this | |
2990 | after checking the above conditions, because we may look at a | |
2991 | symbol once, decide not to do anything, and then get called | |
2992 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2993 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2994 | |
2995 | /* If this is a weak definition, and we know a real definition, and | |
2996 | the real symbol is not itself defined by a regular object file, | |
2997 | then get a good value for the real definition. We handle the | |
2998 | real symbol first, for the convenience of the backend routine. | |
2999 | ||
3000 | Note that there is a confusing case here. If the real definition | |
3001 | is defined by a regular object file, we don't get the real symbol | |
3002 | from the dynamic object, but we do get the weak symbol. If the | |
3003 | processor backend uses a COPY reloc, then if some routine in the | |
3004 | dynamic object changes the real symbol, we will not see that | |
3005 | change in the corresponding weak symbol. This is the way other | |
3006 | ELF linkers work as well, and seems to be a result of the shared | |
3007 | library model. | |
3008 | ||
3009 | I will clarify this issue. Most SVR4 shared libraries define the | |
3010 | variable _timezone and define timezone as a weak synonym. The | |
3011 | tzset call changes _timezone. If you write | |
3012 | extern int timezone; | |
3013 | int _timezone = 5; | |
3014 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
3015 | you might expect that, since timezone is a synonym for _timezone, | |
3016 | the same number will print both times. However, if the processor | |
3017 | backend uses a COPY reloc, then actually timezone will be copied | |
3018 | into your process image, and, since you define _timezone | |
3019 | yourself, _timezone will not. Thus timezone and _timezone will | |
3020 | wind up at different memory locations. The tzset call will set | |
3021 | _timezone, leaving timezone unchanged. */ | |
3022 | ||
60d67dc8 | 3023 | if (h->is_weakalias) |
45d6a902 | 3024 | { |
60d67dc8 AM |
3025 | struct elf_link_hash_entry *def = weakdef (h); |
3026 | ||
ec24dc88 | 3027 | /* If we get to this point, there is an implicit reference to |
60d67dc8 AM |
3028 | the alias by a regular object file via the weak symbol H. */ |
3029 | def->ref_regular = 1; | |
45d6a902 | 3030 | |
ec24dc88 | 3031 | /* Ensure that the backend adjust_dynamic_symbol function sees |
60d67dc8 AM |
3032 | the strong alias before H by recursively calling ourselves. */ |
3033 | if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) | |
45d6a902 AM |
3034 | return FALSE; |
3035 | } | |
3036 | ||
3037 | /* If a symbol has no type and no size and does not require a PLT | |
3038 | entry, then we are probably about to do the wrong thing here: we | |
3039 | are probably going to create a COPY reloc for an empty object. | |
3040 | This case can arise when a shared object is built with assembly | |
3041 | code, and the assembly code fails to set the symbol type. */ | |
3042 | if (h->size == 0 | |
3043 | && h->type == STT_NOTYPE | |
f5385ebf | 3044 | && !h->needs_plt) |
4eca0228 | 3045 | _bfd_error_handler |
45d6a902 AM |
3046 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
3047 | h->root.root.string); | |
3048 | ||
45d6a902 AM |
3049 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
3050 | { | |
3051 | eif->failed = TRUE; | |
3052 | return FALSE; | |
3053 | } | |
3054 | ||
3055 | return TRUE; | |
3056 | } | |
3057 | ||
027297b7 L |
3058 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
3059 | DYNBSS. */ | |
3060 | ||
3061 | bfd_boolean | |
6cabe1ea AM |
3062 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
3063 | struct elf_link_hash_entry *h, | |
027297b7 L |
3064 | asection *dynbss) |
3065 | { | |
91ac5911 | 3066 | unsigned int power_of_two; |
027297b7 L |
3067 | bfd_vma mask; |
3068 | asection *sec = h->root.u.def.section; | |
3069 | ||
de194d85 | 3070 | /* The section alignment of the definition is the maximum alignment |
91ac5911 L |
3071 | requirement of symbols defined in the section. Since we don't |
3072 | know the symbol alignment requirement, we start with the | |
3073 | maximum alignment and check low bits of the symbol address | |
3074 | for the minimum alignment. */ | |
3075 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
3076 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
3077 | while ((h->root.u.def.value & mask) != 0) | |
3078 | { | |
3079 | mask >>= 1; | |
3080 | --power_of_two; | |
3081 | } | |
027297b7 | 3082 | |
91ac5911 L |
3083 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
3084 | dynbss)) | |
027297b7 L |
3085 | { |
3086 | /* Adjust the section alignment if needed. */ | |
3087 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 3088 | power_of_two)) |
027297b7 L |
3089 | return FALSE; |
3090 | } | |
3091 | ||
91ac5911 | 3092 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
3093 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
3094 | ||
3095 | /* Define the symbol as being at this point in DYNBSS. */ | |
3096 | h->root.u.def.section = dynbss; | |
3097 | h->root.u.def.value = dynbss->size; | |
3098 | ||
3099 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
3100 | dynbss->size += h->size; | |
3101 | ||
f7483970 L |
3102 | /* No error if extern_protected_data is true. */ |
3103 | if (h->protected_def | |
889c2a67 L |
3104 | && (!info->extern_protected_data |
3105 | || (info->extern_protected_data < 0 | |
3106 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 | 3107 | info->callbacks->einfo |
c1c8c1ef | 3108 | (_("%P: copy reloc against protected `%pT' is dangerous\n"), |
d07a1b05 | 3109 | h->root.root.string); |
6cabe1ea | 3110 | |
027297b7 L |
3111 | return TRUE; |
3112 | } | |
3113 | ||
45d6a902 AM |
3114 | /* Adjust all external symbols pointing into SEC_MERGE sections |
3115 | to reflect the object merging within the sections. */ | |
3116 | ||
28caa186 | 3117 | static bfd_boolean |
268b6b39 | 3118 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
3119 | { |
3120 | asection *sec; | |
3121 | ||
45d6a902 AM |
3122 | if ((h->root.type == bfd_link_hash_defined |
3123 | || h->root.type == bfd_link_hash_defweak) | |
3124 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 3125 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 3126 | { |
a50b1753 | 3127 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
3128 | |
3129 | h->root.u.def.value = | |
3130 | _bfd_merged_section_offset (output_bfd, | |
3131 | &h->root.u.def.section, | |
3132 | elf_section_data (sec)->sec_info, | |
753731ee | 3133 | h->root.u.def.value); |
45d6a902 AM |
3134 | } |
3135 | ||
3136 | return TRUE; | |
3137 | } | |
986a241f RH |
3138 | |
3139 | /* Returns false if the symbol referred to by H should be considered | |
3140 | to resolve local to the current module, and true if it should be | |
3141 | considered to bind dynamically. */ | |
3142 | ||
3143 | bfd_boolean | |
268b6b39 AM |
3144 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
3145 | struct bfd_link_info *info, | |
89a2ee5a | 3146 | bfd_boolean not_local_protected) |
986a241f RH |
3147 | { |
3148 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
3149 | const struct elf_backend_data *bed; |
3150 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
3151 | |
3152 | if (h == NULL) | |
3153 | return FALSE; | |
3154 | ||
3155 | while (h->root.type == bfd_link_hash_indirect | |
3156 | || h->root.type == bfd_link_hash_warning) | |
3157 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3158 | ||
3159 | /* If it was forced local, then clearly it's not dynamic. */ | |
3160 | if (h->dynindx == -1) | |
3161 | return FALSE; | |
f5385ebf | 3162 | if (h->forced_local) |
986a241f RH |
3163 | return FALSE; |
3164 | ||
3165 | /* Identify the cases where name binding rules say that a | |
3166 | visible symbol resolves locally. */ | |
0e1862bb L |
3167 | binding_stays_local_p = (bfd_link_executable (info) |
3168 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
3169 | |
3170 | switch (ELF_ST_VISIBILITY (h->other)) | |
3171 | { | |
3172 | case STV_INTERNAL: | |
3173 | case STV_HIDDEN: | |
3174 | return FALSE; | |
3175 | ||
3176 | case STV_PROTECTED: | |
fcb93ecf PB |
3177 | hash_table = elf_hash_table (info); |
3178 | if (!is_elf_hash_table (hash_table)) | |
3179 | return FALSE; | |
3180 | ||
3181 | bed = get_elf_backend_data (hash_table->dynobj); | |
3182 | ||
986a241f RH |
3183 | /* Proper resolution for function pointer equality may require |
3184 | that these symbols perhaps be resolved dynamically, even though | |
3185 | we should be resolving them to the current module. */ | |
89a2ee5a | 3186 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
3187 | binding_stays_local_p = TRUE; |
3188 | break; | |
3189 | ||
3190 | default: | |
986a241f RH |
3191 | break; |
3192 | } | |
3193 | ||
aa37626c | 3194 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 3195 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
3196 | return TRUE; |
3197 | ||
986a241f RH |
3198 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
3199 | us that it remains local. */ | |
3200 | return !binding_stays_local_p; | |
3201 | } | |
f6c52c13 AM |
3202 | |
3203 | /* Return true if the symbol referred to by H should be considered | |
3204 | to resolve local to the current module, and false otherwise. Differs | |
3205 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 3206 | undefined symbols. The two functions are virtually identical except |
0fad2956 MR |
3207 | for the place where dynindx == -1 is tested. If that test is true, |
3208 | _bfd_elf_dynamic_symbol_p will say the symbol is local, while | |
3209 | _bfd_elf_symbol_refs_local_p will say the symbol is local only for | |
3210 | defined symbols. | |
89a2ee5a AM |
3211 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as |
3212 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
3213 | treatment of undefined weak symbols. For those that do not make | |
3214 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
3215 | |
3216 | bfd_boolean | |
268b6b39 AM |
3217 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3218 | struct bfd_link_info *info, | |
3219 | bfd_boolean local_protected) | |
f6c52c13 | 3220 | { |
fcb93ecf PB |
3221 | const struct elf_backend_data *bed; |
3222 | struct elf_link_hash_table *hash_table; | |
3223 | ||
f6c52c13 AM |
3224 | /* If it's a local sym, of course we resolve locally. */ |
3225 | if (h == NULL) | |
3226 | return TRUE; | |
3227 | ||
d95edcac L |
3228 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3229 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3230 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3231 | return TRUE; | |
3232 | ||
0fad2956 MR |
3233 | /* Forced local symbols resolve locally. */ |
3234 | if (h->forced_local) | |
3235 | return TRUE; | |
3236 | ||
7e2294f9 AO |
3237 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3238 | flag set, so test it first, and don't bail out. */ | |
3239 | if (ELF_COMMON_DEF_P (h)) | |
3240 | /* Do nothing. */; | |
f6c52c13 | 3241 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3242 | resolve locally. The sym is either undefined or dynamic. */ |
3243 | else if (!h->def_regular) | |
f6c52c13 AM |
3244 | return FALSE; |
3245 | ||
0fad2956 | 3246 | /* Non-dynamic symbols resolve locally. */ |
f6c52c13 AM |
3247 | if (h->dynindx == -1) |
3248 | return TRUE; | |
3249 | ||
3250 | /* At this point, we know the symbol is defined and dynamic. In an | |
3251 | executable it must resolve locally, likewise when building symbolic | |
3252 | shared libraries. */ | |
0e1862bb | 3253 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3254 | return TRUE; |
3255 | ||
3256 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3257 | with default visibility might not resolve locally. */ | |
3258 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3259 | return FALSE; | |
3260 | ||
fcb93ecf PB |
3261 | hash_table = elf_hash_table (info); |
3262 | if (!is_elf_hash_table (hash_table)) | |
3263 | return TRUE; | |
3264 | ||
3265 | bed = get_elf_backend_data (hash_table->dynobj); | |
3266 | ||
f7483970 L |
3267 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3268 | symbols are local. */ | |
889c2a67 L |
3269 | if ((!info->extern_protected_data |
3270 | || (info->extern_protected_data < 0 | |
3271 | && !bed->extern_protected_data)) | |
3272 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3273 | return TRUE; |
3274 | ||
f6c52c13 | 3275 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3276 | symbols be treated as dynamic symbols. If the address of a |
3277 | function not defined in an executable is set to that function's | |
3278 | plt entry in the executable, then the address of the function in | |
3279 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3280 | return local_protected; |
3281 | } | |
e1918d23 AM |
3282 | |
3283 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3284 | aligned. Returns the first TLS output section. */ | |
3285 | ||
3286 | struct bfd_section * | |
3287 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3288 | { | |
3289 | struct bfd_section *sec, *tls; | |
3290 | unsigned int align = 0; | |
3291 | ||
3292 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3293 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3294 | break; | |
3295 | tls = sec; | |
3296 | ||
3297 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3298 | if (sec->alignment_power > align) | |
3299 | align = sec->alignment_power; | |
3300 | ||
3301 | elf_hash_table (info)->tls_sec = tls; | |
3302 | ||
3303 | /* Ensure the alignment of the first section is the largest alignment, | |
3304 | so that the tls segment starts aligned. */ | |
3305 | if (tls != NULL) | |
3306 | tls->alignment_power = align; | |
3307 | ||
3308 | return tls; | |
3309 | } | |
0ad989f9 L |
3310 | |
3311 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3312 | static bfd_boolean | |
3313 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3314 | Elf_Internal_Sym *sym) | |
3315 | { | |
a4d8e49b L |
3316 | const struct elf_backend_data *bed; |
3317 | ||
0ad989f9 L |
3318 | /* Local symbols do not count, but target specific ones might. */ |
3319 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3320 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3321 | return FALSE; | |
3322 | ||
fcb93ecf | 3323 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3324 | /* Function symbols do not count. */ |
fcb93ecf | 3325 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3326 | return FALSE; |
3327 | ||
3328 | /* If the section is undefined, then so is the symbol. */ | |
3329 | if (sym->st_shndx == SHN_UNDEF) | |
3330 | return FALSE; | |
3331 | ||
3332 | /* If the symbol is defined in the common section, then | |
3333 | it is a common definition and so does not count. */ | |
a4d8e49b | 3334 | if (bed->common_definition (sym)) |
0ad989f9 L |
3335 | return FALSE; |
3336 | ||
3337 | /* If the symbol is in a target specific section then we | |
3338 | must rely upon the backend to tell us what it is. */ | |
3339 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3340 | /* FIXME - this function is not coded yet: | |
3341 | ||
3342 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3343 | ||
3344 | Instead for now assume that the definition is not global, | |
3345 | Even if this is wrong, at least the linker will behave | |
3346 | in the same way that it used to do. */ | |
3347 | return FALSE; | |
3348 | ||
3349 | return TRUE; | |
3350 | } | |
3351 | ||
3352 | /* Search the symbol table of the archive element of the archive ABFD | |
3353 | whose archive map contains a mention of SYMDEF, and determine if | |
3354 | the symbol is defined in this element. */ | |
3355 | static bfd_boolean | |
3356 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3357 | { | |
3358 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3359 | size_t symcount; |
3360 | size_t extsymcount; | |
3361 | size_t extsymoff; | |
0ad989f9 L |
3362 | Elf_Internal_Sym *isymbuf; |
3363 | Elf_Internal_Sym *isym; | |
3364 | Elf_Internal_Sym *isymend; | |
3365 | bfd_boolean result; | |
3366 | ||
3367 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3368 | if (abfd == NULL) | |
3369 | return FALSE; | |
3370 | ||
3371 | if (! bfd_check_format (abfd, bfd_object)) | |
3372 | return FALSE; | |
3373 | ||
7dc3990e L |
3374 | /* Select the appropriate symbol table. If we don't know if the |
3375 | object file is an IR object, give linker LTO plugin a chance to | |
3376 | get the correct symbol table. */ | |
3377 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3378 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3379 | || (abfd->plugin_format == bfd_plugin_unknown |
3380 | && bfd_link_plugin_object_p (abfd)) | |
3381 | #endif | |
3382 | ) | |
3383 | { | |
3384 | /* Use the IR symbol table if the object has been claimed by | |
3385 | plugin. */ | |
3386 | abfd = abfd->plugin_dummy_bfd; | |
3387 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3388 | } | |
3389 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3390 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3391 | else | |
3392 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3393 | ||
3394 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3395 | ||
3396 | /* The sh_info field of the symtab header tells us where the | |
3397 | external symbols start. We don't care about the local symbols. */ | |
3398 | if (elf_bad_symtab (abfd)) | |
3399 | { | |
3400 | extsymcount = symcount; | |
3401 | extsymoff = 0; | |
3402 | } | |
3403 | else | |
3404 | { | |
3405 | extsymcount = symcount - hdr->sh_info; | |
3406 | extsymoff = hdr->sh_info; | |
3407 | } | |
3408 | ||
3409 | if (extsymcount == 0) | |
3410 | return FALSE; | |
3411 | ||
3412 | /* Read in the symbol table. */ | |
3413 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3414 | NULL, NULL, NULL); | |
3415 | if (isymbuf == NULL) | |
3416 | return FALSE; | |
3417 | ||
3418 | /* Scan the symbol table looking for SYMDEF. */ | |
3419 | result = FALSE; | |
3420 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3421 | { | |
3422 | const char *name; | |
3423 | ||
3424 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3425 | isym->st_name); | |
3426 | if (name == NULL) | |
3427 | break; | |
3428 | ||
3429 | if (strcmp (name, symdef->name) == 0) | |
3430 | { | |
3431 | result = is_global_data_symbol_definition (abfd, isym); | |
3432 | break; | |
3433 | } | |
3434 | } | |
3435 | ||
3436 | free (isymbuf); | |
3437 | ||
3438 | return result; | |
3439 | } | |
3440 | \f | |
5a580b3a AM |
3441 | /* Add an entry to the .dynamic table. */ |
3442 | ||
3443 | bfd_boolean | |
3444 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3445 | bfd_vma tag, | |
3446 | bfd_vma val) | |
3447 | { | |
3448 | struct elf_link_hash_table *hash_table; | |
3449 | const struct elf_backend_data *bed; | |
3450 | asection *s; | |
3451 | bfd_size_type newsize; | |
3452 | bfd_byte *newcontents; | |
3453 | Elf_Internal_Dyn dyn; | |
3454 | ||
3455 | hash_table = elf_hash_table (info); | |
3456 | if (! is_elf_hash_table (hash_table)) | |
3457 | return FALSE; | |
3458 | ||
3459 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3460 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3461 | BFD_ASSERT (s != NULL); |
3462 | ||
eea6121a | 3463 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3464 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3465 | if (newcontents == NULL) |
3466 | return FALSE; | |
3467 | ||
3468 | dyn.d_tag = tag; | |
3469 | dyn.d_un.d_val = val; | |
eea6121a | 3470 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3471 | |
eea6121a | 3472 | s->size = newsize; |
5a580b3a AM |
3473 | s->contents = newcontents; |
3474 | ||
3475 | return TRUE; | |
3476 | } | |
3477 | ||
3478 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3479 | otherwise just check whether one already exists. Returns -1 on error, | |
3480 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3481 | ||
4ad4eba5 | 3482 | static int |
7e9f0867 AM |
3483 | elf_add_dt_needed_tag (bfd *abfd, |
3484 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3485 | const char *soname, |
3486 | bfd_boolean do_it) | |
5a580b3a AM |
3487 | { |
3488 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3489 | size_t strindex; |
5a580b3a | 3490 | |
7e9f0867 AM |
3491 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3492 | return -1; | |
3493 | ||
5a580b3a | 3494 | hash_table = elf_hash_table (info); |
5a580b3a | 3495 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3496 | if (strindex == (size_t) -1) |
5a580b3a AM |
3497 | return -1; |
3498 | ||
02be4619 | 3499 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3500 | { |
3501 | asection *sdyn; | |
3502 | const struct elf_backend_data *bed; | |
3503 | bfd_byte *extdyn; | |
3504 | ||
3505 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3506 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3507 | if (sdyn != NULL) |
3508 | for (extdyn = sdyn->contents; | |
3509 | extdyn < sdyn->contents + sdyn->size; | |
3510 | extdyn += bed->s->sizeof_dyn) | |
3511 | { | |
3512 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3513 | |
7e9f0867 AM |
3514 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3515 | if (dyn.d_tag == DT_NEEDED | |
3516 | && dyn.d_un.d_val == strindex) | |
3517 | { | |
3518 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3519 | return 1; | |
3520 | } | |
3521 | } | |
5a580b3a AM |
3522 | } |
3523 | ||
3524 | if (do_it) | |
3525 | { | |
7e9f0867 AM |
3526 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3527 | return -1; | |
3528 | ||
5a580b3a AM |
3529 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3530 | return -1; | |
3531 | } | |
3532 | else | |
3533 | /* We were just checking for existence of the tag. */ | |
3534 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3535 | ||
3536 | return 0; | |
3537 | } | |
3538 | ||
7b15fa7a AM |
3539 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3540 | (or the end of list if STOP is NULL), and needed by a library that | |
3541 | will be loaded. */ | |
3542 | ||
010e5ae2 | 3543 | static bfd_boolean |
7b15fa7a AM |
3544 | on_needed_list (const char *soname, |
3545 | struct bfd_link_needed_list *needed, | |
3546 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3547 | { |
7b15fa7a AM |
3548 | struct bfd_link_needed_list *look; |
3549 | for (look = needed; look != stop; look = look->next) | |
3550 | if (strcmp (soname, look->name) == 0 | |
3551 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3552 | /* If needed by a library that itself is not directly | |
3553 | needed, recursively check whether that library is | |
3554 | indirectly needed. Since we add DT_NEEDED entries to | |
3555 | the end of the list, library dependencies appear after | |
3556 | the library. Therefore search prior to the current | |
3557 | LOOK, preventing possible infinite recursion. */ | |
3558 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3559 | return TRUE; |
3560 | ||
3561 | return FALSE; | |
3562 | } | |
3563 | ||
14160578 | 3564 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3565 | static int |
3566 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3567 | { |
3568 | const struct elf_link_hash_entry *h1; | |
3569 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3570 | bfd_signed_vma vdiff; |
5a580b3a AM |
3571 | |
3572 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3573 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3574 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3575 | if (vdiff != 0) | |
3576 | return vdiff > 0 ? 1 : -1; | |
3577 | else | |
3578 | { | |
d3435ae8 | 3579 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3580 | if (sdiff != 0) |
3581 | return sdiff > 0 ? 1 : -1; | |
3582 | } | |
14160578 AM |
3583 | vdiff = h1->size - h2->size; |
3584 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3585 | } |
4ad4eba5 | 3586 | |
5a580b3a AM |
3587 | /* This function is used to adjust offsets into .dynstr for |
3588 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3589 | ||
3590 | static bfd_boolean | |
3591 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3592 | { | |
a50b1753 | 3593 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3594 | |
5a580b3a AM |
3595 | if (h->dynindx != -1) |
3596 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3597 | return TRUE; | |
3598 | } | |
3599 | ||
3600 | /* Assign string offsets in .dynstr, update all structures referencing | |
3601 | them. */ | |
3602 | ||
4ad4eba5 AM |
3603 | static bfd_boolean |
3604 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3605 | { |
3606 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3607 | struct elf_link_local_dynamic_entry *entry; | |
3608 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3609 | bfd *dynobj = hash_table->dynobj; | |
3610 | asection *sdyn; | |
3611 | bfd_size_type size; | |
3612 | const struct elf_backend_data *bed; | |
3613 | bfd_byte *extdyn; | |
3614 | ||
3615 | _bfd_elf_strtab_finalize (dynstr); | |
3616 | size = _bfd_elf_strtab_size (dynstr); | |
3617 | ||
3618 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3619 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3620 | BFD_ASSERT (sdyn != NULL); |
3621 | ||
3622 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3623 | for (extdyn = sdyn->contents; | |
eea6121a | 3624 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3625 | extdyn += bed->s->sizeof_dyn) |
3626 | { | |
3627 | Elf_Internal_Dyn dyn; | |
3628 | ||
3629 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3630 | switch (dyn.d_tag) | |
3631 | { | |
3632 | case DT_STRSZ: | |
3633 | dyn.d_un.d_val = size; | |
3634 | break; | |
3635 | case DT_NEEDED: | |
3636 | case DT_SONAME: | |
3637 | case DT_RPATH: | |
3638 | case DT_RUNPATH: | |
3639 | case DT_FILTER: | |
3640 | case DT_AUXILIARY: | |
7ee314fa AM |
3641 | case DT_AUDIT: |
3642 | case DT_DEPAUDIT: | |
5a580b3a AM |
3643 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3644 | break; | |
3645 | default: | |
3646 | continue; | |
3647 | } | |
3648 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3649 | } | |
3650 | ||
3651 | /* Now update local dynamic symbols. */ | |
3652 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3653 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3654 | entry->isym.st_name); | |
3655 | ||
3656 | /* And the rest of dynamic symbols. */ | |
3657 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3658 | ||
3659 | /* Adjust version definitions. */ | |
3660 | if (elf_tdata (output_bfd)->cverdefs) | |
3661 | { | |
3662 | asection *s; | |
3663 | bfd_byte *p; | |
ef53be89 | 3664 | size_t i; |
5a580b3a AM |
3665 | Elf_Internal_Verdef def; |
3666 | Elf_Internal_Verdaux defaux; | |
3667 | ||
3d4d4302 | 3668 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3669 | p = s->contents; |
3670 | do | |
3671 | { | |
3672 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3673 | &def); | |
3674 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3675 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3676 | continue; | |
5a580b3a AM |
3677 | for (i = 0; i < def.vd_cnt; ++i) |
3678 | { | |
3679 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3680 | (Elf_External_Verdaux *) p, &defaux); | |
3681 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3682 | defaux.vda_name); | |
3683 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3684 | &defaux, (Elf_External_Verdaux *) p); | |
3685 | p += sizeof (Elf_External_Verdaux); | |
3686 | } | |
3687 | } | |
3688 | while (def.vd_next); | |
3689 | } | |
3690 | ||
3691 | /* Adjust version references. */ | |
3692 | if (elf_tdata (output_bfd)->verref) | |
3693 | { | |
3694 | asection *s; | |
3695 | bfd_byte *p; | |
ef53be89 | 3696 | size_t i; |
5a580b3a AM |
3697 | Elf_Internal_Verneed need; |
3698 | Elf_Internal_Vernaux needaux; | |
3699 | ||
3d4d4302 | 3700 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3701 | p = s->contents; |
3702 | do | |
3703 | { | |
3704 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3705 | &need); | |
3706 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3707 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3708 | (Elf_External_Verneed *) p); | |
3709 | p += sizeof (Elf_External_Verneed); | |
3710 | for (i = 0; i < need.vn_cnt; ++i) | |
3711 | { | |
3712 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3713 | (Elf_External_Vernaux *) p, &needaux); | |
3714 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3715 | needaux.vna_name); | |
3716 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3717 | &needaux, | |
3718 | (Elf_External_Vernaux *) p); | |
3719 | p += sizeof (Elf_External_Vernaux); | |
3720 | } | |
3721 | } | |
3722 | while (need.vn_next); | |
3723 | } | |
3724 | ||
3725 | return TRUE; | |
3726 | } | |
3727 | \f | |
13285a1b AM |
3728 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3729 | The default is to only match when the INPUT and OUTPUT are exactly | |
3730 | the same target. */ | |
3731 | ||
3732 | bfd_boolean | |
3733 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3734 | const bfd_target *output) | |
3735 | { | |
3736 | return input == output; | |
3737 | } | |
3738 | ||
3739 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3740 | This version is used when different targets for the same architecture | |
3741 | are virtually identical. */ | |
3742 | ||
3743 | bfd_boolean | |
3744 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3745 | const bfd_target *output) | |
3746 | { | |
3747 | const struct elf_backend_data *obed, *ibed; | |
3748 | ||
3749 | if (input == output) | |
3750 | return TRUE; | |
3751 | ||
3752 | ibed = xvec_get_elf_backend_data (input); | |
3753 | obed = xvec_get_elf_backend_data (output); | |
3754 | ||
3755 | if (ibed->arch != obed->arch) | |
3756 | return FALSE; | |
3757 | ||
3758 | /* If both backends are using this function, deem them compatible. */ | |
3759 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3760 | } | |
3761 | ||
e5034e59 AM |
3762 | /* Make a special call to the linker "notice" function to tell it that |
3763 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3764 | processing the lib. */ |
e5034e59 AM |
3765 | |
3766 | bfd_boolean | |
3767 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3768 | struct bfd_link_info *info, | |
3769 | enum notice_asneeded_action act) | |
3770 | { | |
46135103 | 3771 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3772 | } |
3773 | ||
d9689752 L |
3774 | /* Check relocations an ELF object file. */ |
3775 | ||
3776 | bfd_boolean | |
3777 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3778 | { | |
3779 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3780 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3781 | ||
3782 | /* If this object is the same format as the output object, and it is | |
3783 | not a shared library, then let the backend look through the | |
3784 | relocs. | |
3785 | ||
3786 | This is required to build global offset table entries and to | |
3787 | arrange for dynamic relocs. It is not required for the | |
3788 | particular common case of linking non PIC code, even when linking | |
3789 | against shared libraries, but unfortunately there is no way of | |
3790 | knowing whether an object file has been compiled PIC or not. | |
3791 | Looking through the relocs is not particularly time consuming. | |
3792 | The problem is that we must either (1) keep the relocs in memory, | |
3793 | which causes the linker to require additional runtime memory or | |
3794 | (2) read the relocs twice from the input file, which wastes time. | |
3795 | This would be a good case for using mmap. | |
3796 | ||
3797 | I have no idea how to handle linking PIC code into a file of a | |
3798 | different format. It probably can't be done. */ | |
3799 | if ((abfd->flags & DYNAMIC) == 0 | |
3800 | && is_elf_hash_table (htab) | |
3801 | && bed->check_relocs != NULL | |
3802 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3803 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3804 | { | |
3805 | asection *o; | |
3806 | ||
3807 | for (o = abfd->sections; o != NULL; o = o->next) | |
3808 | { | |
3809 | Elf_Internal_Rela *internal_relocs; | |
3810 | bfd_boolean ok; | |
3811 | ||
5ce03cea | 3812 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3813 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3814 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3815 | || o->reloc_count == 0 |
3816 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3817 | && (o->flags & SEC_DEBUGGING) != 0) | |
3818 | || bfd_is_abs_section (o->output_section)) | |
3819 | continue; | |
3820 | ||
3821 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3822 | info->keep_memory); | |
3823 | if (internal_relocs == NULL) | |
3824 | return FALSE; | |
3825 | ||
3826 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3827 | ||
3828 | if (elf_section_data (o)->relocs != internal_relocs) | |
3829 | free (internal_relocs); | |
3830 | ||
3831 | if (! ok) | |
3832 | return FALSE; | |
3833 | } | |
3834 | } | |
3835 | ||
3836 | return TRUE; | |
3837 | } | |
3838 | ||
4ad4eba5 AM |
3839 | /* Add symbols from an ELF object file to the linker hash table. */ |
3840 | ||
3841 | static bfd_boolean | |
3842 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3843 | { | |
a0c402a5 | 3844 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3845 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3846 | size_t symcount; |
3847 | size_t extsymcount; | |
3848 | size_t extsymoff; | |
4ad4eba5 AM |
3849 | struct elf_link_hash_entry **sym_hash; |
3850 | bfd_boolean dynamic; | |
3851 | Elf_External_Versym *extversym = NULL; | |
3852 | Elf_External_Versym *ever; | |
3853 | struct elf_link_hash_entry *weaks; | |
3854 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3855 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3856 | Elf_Internal_Sym *isymbuf = NULL; |
3857 | Elf_Internal_Sym *isym; | |
3858 | Elf_Internal_Sym *isymend; | |
3859 | const struct elf_backend_data *bed; | |
3860 | bfd_boolean add_needed; | |
66eb6687 | 3861 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3862 | bfd_size_type amt; |
66eb6687 | 3863 | void *alloc_mark = NULL; |
4f87808c AM |
3864 | struct bfd_hash_entry **old_table = NULL; |
3865 | unsigned int old_size = 0; | |
3866 | unsigned int old_count = 0; | |
66eb6687 | 3867 | void *old_tab = NULL; |
66eb6687 AM |
3868 | void *old_ent; |
3869 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3870 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3871 | void *old_strtab = NULL; |
66eb6687 | 3872 | size_t tabsize = 0; |
db6a5d5f | 3873 | asection *s; |
29a9f53e | 3874 | bfd_boolean just_syms; |
4ad4eba5 | 3875 | |
66eb6687 | 3876 | htab = elf_hash_table (info); |
4ad4eba5 | 3877 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3878 | |
3879 | if ((abfd->flags & DYNAMIC) == 0) | |
3880 | dynamic = FALSE; | |
3881 | else | |
3882 | { | |
3883 | dynamic = TRUE; | |
3884 | ||
3885 | /* You can't use -r against a dynamic object. Also, there's no | |
3886 | hope of using a dynamic object which does not exactly match | |
3887 | the format of the output file. */ | |
0e1862bb | 3888 | if (bfd_link_relocatable (info) |
66eb6687 | 3889 | || !is_elf_hash_table (htab) |
f13a99db | 3890 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3891 | { |
0e1862bb | 3892 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3893 | bfd_set_error (bfd_error_invalid_operation); |
3894 | else | |
3895 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3896 | goto error_return; |
3897 | } | |
3898 | } | |
3899 | ||
a0c402a5 L |
3900 | ehdr = elf_elfheader (abfd); |
3901 | if (info->warn_alternate_em | |
3902 | && bed->elf_machine_code != ehdr->e_machine | |
3903 | && ((bed->elf_machine_alt1 != 0 | |
3904 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3905 | || (bed->elf_machine_alt2 != 0 | |
3906 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
9793eb77 | 3907 | _bfd_error_handler |
695344c0 | 3908 | /* xgettext:c-format */ |
9793eb77 | 3909 | (_("alternate ELF machine code found (%d) in %pB, expecting %d"), |
a0c402a5 L |
3910 | ehdr->e_machine, abfd, bed->elf_machine_code); |
3911 | ||
4ad4eba5 AM |
3912 | /* As a GNU extension, any input sections which are named |
3913 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3914 | symbol. This differs from .gnu.warning sections, which generate | |
3915 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3916 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3917 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3918 | { |
db6a5d5f | 3919 | const char *name; |
4ad4eba5 | 3920 | |
db6a5d5f AM |
3921 | name = bfd_get_section_name (abfd, s); |
3922 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3923 | { |
db6a5d5f AM |
3924 | char *msg; |
3925 | bfd_size_type sz; | |
3926 | ||
3927 | name += sizeof ".gnu.warning." - 1; | |
3928 | ||
3929 | /* If this is a shared object, then look up the symbol | |
3930 | in the hash table. If it is there, and it is already | |
3931 | been defined, then we will not be using the entry | |
3932 | from this shared object, so we don't need to warn. | |
3933 | FIXME: If we see the definition in a regular object | |
3934 | later on, we will warn, but we shouldn't. The only | |
3935 | fix is to keep track of what warnings we are supposed | |
3936 | to emit, and then handle them all at the end of the | |
3937 | link. */ | |
3938 | if (dynamic) | |
4ad4eba5 | 3939 | { |
db6a5d5f AM |
3940 | struct elf_link_hash_entry *h; |
3941 | ||
3942 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3943 | ||
3944 | /* FIXME: What about bfd_link_hash_common? */ | |
3945 | if (h != NULL | |
3946 | && (h->root.type == bfd_link_hash_defined | |
3947 | || h->root.type == bfd_link_hash_defweak)) | |
3948 | continue; | |
3949 | } | |
4ad4eba5 | 3950 | |
db6a5d5f AM |
3951 | sz = s->size; |
3952 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3953 | if (msg == NULL) | |
3954 | goto error_return; | |
4ad4eba5 | 3955 | |
db6a5d5f AM |
3956 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3957 | goto error_return; | |
4ad4eba5 | 3958 | |
db6a5d5f | 3959 | msg[sz] = '\0'; |
4ad4eba5 | 3960 | |
db6a5d5f AM |
3961 | if (! (_bfd_generic_link_add_one_symbol |
3962 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3963 | FALSE, bed->collect, NULL))) | |
3964 | goto error_return; | |
4ad4eba5 | 3965 | |
0e1862bb | 3966 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3967 | { |
3968 | /* Clobber the section size so that the warning does | |
3969 | not get copied into the output file. */ | |
3970 | s->size = 0; | |
11d2f718 | 3971 | |
db6a5d5f AM |
3972 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3973 | the warning section don't get copied to the output. */ | |
3974 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3975 | } |
3976 | } | |
3977 | } | |
3978 | ||
29a9f53e L |
3979 | just_syms = ((s = abfd->sections) != NULL |
3980 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
3981 | ||
4ad4eba5 AM |
3982 | add_needed = TRUE; |
3983 | if (! dynamic) | |
3984 | { | |
3985 | /* If we are creating a shared library, create all the dynamic | |
3986 | sections immediately. We need to attach them to something, | |
3987 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
3988 | format and is not from ld --just-symbols. Always create the |
3989 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
3990 | are no input BFD's of the same format as the output, we can't |
3991 | make a shared library. */ | |
3992 | if (!just_syms | |
bf89386a | 3993 | && (bfd_link_pic (info) |
9c1d7a08 | 3994 | || (!bfd_link_relocatable (info) |
3c5fce9b | 3995 | && info->nointerp |
9c1d7a08 | 3996 | && (info->export_dynamic || info->dynamic))) |
66eb6687 | 3997 | && is_elf_hash_table (htab) |
f13a99db | 3998 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3999 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
4000 | { |
4001 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
4002 | goto error_return; | |
4003 | } | |
4004 | } | |
66eb6687 | 4005 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
4006 | goto error_return; |
4007 | else | |
4008 | { | |
4ad4eba5 | 4009 | const char *soname = NULL; |
7ee314fa | 4010 | char *audit = NULL; |
4ad4eba5 | 4011 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
9acc85a6 | 4012 | const Elf_Internal_Phdr *phdr; |
4ad4eba5 AM |
4013 | int ret; |
4014 | ||
4015 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 4016 | ld shouldn't allow it. */ |
29a9f53e | 4017 | if (just_syms) |
92fd189d | 4018 | abort (); |
4ad4eba5 AM |
4019 | |
4020 | /* If this dynamic lib was specified on the command line with | |
4021 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
4022 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
4023 | in by another lib's DT_NEEDED. When --no-add-needed is used |
4024 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
4025 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
4026 | all. */ | |
4027 | add_needed = (elf_dyn_lib_class (abfd) | |
4028 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
4029 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
4030 | |
4031 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
4032 | if (s != NULL) | |
4033 | { | |
4034 | bfd_byte *dynbuf; | |
4035 | bfd_byte *extdyn; | |
cb33740c | 4036 | unsigned int elfsec; |
4ad4eba5 AM |
4037 | unsigned long shlink; |
4038 | ||
eea6121a | 4039 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
4040 | { |
4041 | error_free_dyn: | |
4042 | free (dynbuf); | |
4043 | goto error_return; | |
4044 | } | |
4ad4eba5 AM |
4045 | |
4046 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 4047 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
4048 | goto error_free_dyn; |
4049 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
4050 | ||
4051 | for (extdyn = dynbuf; | |
eea6121a | 4052 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
4053 | extdyn += bed->s->sizeof_dyn) |
4054 | { | |
4055 | Elf_Internal_Dyn dyn; | |
4056 | ||
4057 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
4058 | if (dyn.d_tag == DT_SONAME) | |
4059 | { | |
4060 | unsigned int tagv = dyn.d_un.d_val; | |
4061 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4062 | if (soname == NULL) | |
4063 | goto error_free_dyn; | |
4064 | } | |
4065 | if (dyn.d_tag == DT_NEEDED) | |
4066 | { | |
4067 | struct bfd_link_needed_list *n, **pn; | |
4068 | char *fnm, *anm; | |
4069 | unsigned int tagv = dyn.d_un.d_val; | |
4070 | ||
4071 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4072 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4073 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4074 | if (n == NULL || fnm == NULL) | |
4075 | goto error_free_dyn; | |
4076 | amt = strlen (fnm) + 1; | |
a50b1753 | 4077 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4078 | if (anm == NULL) |
4079 | goto error_free_dyn; | |
4080 | memcpy (anm, fnm, amt); | |
4081 | n->name = anm; | |
4082 | n->by = abfd; | |
4083 | n->next = NULL; | |
66eb6687 | 4084 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4085 | ; |
4086 | *pn = n; | |
4087 | } | |
4088 | if (dyn.d_tag == DT_RUNPATH) | |
4089 | { | |
4090 | struct bfd_link_needed_list *n, **pn; | |
4091 | char *fnm, *anm; | |
4092 | unsigned int tagv = dyn.d_un.d_val; | |
4093 | ||
4094 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4095 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4096 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4097 | if (n == NULL || fnm == NULL) | |
4098 | goto error_free_dyn; | |
4099 | amt = strlen (fnm) + 1; | |
a50b1753 | 4100 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4101 | if (anm == NULL) |
4102 | goto error_free_dyn; | |
4103 | memcpy (anm, fnm, amt); | |
4104 | n->name = anm; | |
4105 | n->by = abfd; | |
4106 | n->next = NULL; | |
4107 | for (pn = & runpath; | |
4108 | *pn != NULL; | |
4109 | pn = &(*pn)->next) | |
4110 | ; | |
4111 | *pn = n; | |
4112 | } | |
4113 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
4114 | if (!runpath && dyn.d_tag == DT_RPATH) | |
4115 | { | |
4116 | struct bfd_link_needed_list *n, **pn; | |
4117 | char *fnm, *anm; | |
4118 | unsigned int tagv = dyn.d_un.d_val; | |
4119 | ||
4120 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4121 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4122 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4123 | if (n == NULL || fnm == NULL) | |
4124 | goto error_free_dyn; | |
4125 | amt = strlen (fnm) + 1; | |
a50b1753 | 4126 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 4127 | if (anm == NULL) |
f8703194 | 4128 | goto error_free_dyn; |
4ad4eba5 AM |
4129 | memcpy (anm, fnm, amt); |
4130 | n->name = anm; | |
4131 | n->by = abfd; | |
4132 | n->next = NULL; | |
4133 | for (pn = & rpath; | |
4134 | *pn != NULL; | |
4135 | pn = &(*pn)->next) | |
4136 | ; | |
4137 | *pn = n; | |
4138 | } | |
7ee314fa AM |
4139 | if (dyn.d_tag == DT_AUDIT) |
4140 | { | |
4141 | unsigned int tagv = dyn.d_un.d_val; | |
4142 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4143 | } | |
4ad4eba5 AM |
4144 | } |
4145 | ||
4146 | free (dynbuf); | |
4147 | } | |
4148 | ||
4149 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
4150 | frees all more recently bfd_alloc'd blocks as well. */ | |
4151 | if (runpath) | |
4152 | rpath = runpath; | |
4153 | ||
4154 | if (rpath) | |
4155 | { | |
4156 | struct bfd_link_needed_list **pn; | |
66eb6687 | 4157 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4158 | ; |
4159 | *pn = rpath; | |
4160 | } | |
4161 | ||
9acc85a6 AM |
4162 | /* If we have a PT_GNU_RELRO program header, mark as read-only |
4163 | all sections contained fully therein. This makes relro | |
4164 | shared library sections appear as they will at run-time. */ | |
4165 | phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; | |
4166 | while (--phdr >= elf_tdata (abfd)->phdr) | |
4167 | if (phdr->p_type == PT_GNU_RELRO) | |
4168 | { | |
4169 | for (s = abfd->sections; s != NULL; s = s->next) | |
4170 | if ((s->flags & SEC_ALLOC) != 0 | |
4171 | && s->vma >= phdr->p_vaddr | |
4172 | && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) | |
4173 | s->flags |= SEC_READONLY; | |
4174 | break; | |
4175 | } | |
4176 | ||
4ad4eba5 AM |
4177 | /* We do not want to include any of the sections in a dynamic |
4178 | object in the output file. We hack by simply clobbering the | |
4179 | list of sections in the BFD. This could be handled more | |
4180 | cleanly by, say, a new section flag; the existing | |
4181 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4182 | still implies that the section takes up space in the output | |
4183 | file. */ | |
4184 | bfd_section_list_clear (abfd); | |
4185 | ||
4ad4eba5 AM |
4186 | /* Find the name to use in a DT_NEEDED entry that refers to this |
4187 | object. If the object has a DT_SONAME entry, we use it. | |
4188 | Otherwise, if the generic linker stuck something in | |
4189 | elf_dt_name, we use that. Otherwise, we just use the file | |
4190 | name. */ | |
4191 | if (soname == NULL || *soname == '\0') | |
4192 | { | |
4193 | soname = elf_dt_name (abfd); | |
4194 | if (soname == NULL || *soname == '\0') | |
4195 | soname = bfd_get_filename (abfd); | |
4196 | } | |
4197 | ||
4198 | /* Save the SONAME because sometimes the linker emulation code | |
4199 | will need to know it. */ | |
4200 | elf_dt_name (abfd) = soname; | |
4201 | ||
7e9f0867 | 4202 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4203 | if (ret < 0) |
4204 | goto error_return; | |
4205 | ||
4206 | /* If we have already included this dynamic object in the | |
4207 | link, just ignore it. There is no reason to include a | |
4208 | particular dynamic object more than once. */ | |
4209 | if (ret > 0) | |
4210 | return TRUE; | |
7ee314fa AM |
4211 | |
4212 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 4213 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
4214 | } |
4215 | ||
4216 | /* If this is a dynamic object, we always link against the .dynsym | |
4217 | symbol table, not the .symtab symbol table. The dynamic linker | |
4218 | will only see the .dynsym symbol table, so there is no reason to | |
4219 | look at .symtab for a dynamic object. */ | |
4220 | ||
4221 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
4222 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4223 | else | |
4224 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4225 | ||
4226 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
4227 | ||
4228 | /* The sh_info field of the symtab header tells us where the | |
4229 | external symbols start. We don't care about the local symbols at | |
4230 | this point. */ | |
4231 | if (elf_bad_symtab (abfd)) | |
4232 | { | |
4233 | extsymcount = symcount; | |
4234 | extsymoff = 0; | |
4235 | } | |
4236 | else | |
4237 | { | |
4238 | extsymcount = symcount - hdr->sh_info; | |
4239 | extsymoff = hdr->sh_info; | |
4240 | } | |
4241 | ||
f45794cb | 4242 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4243 | if (extsymcount != 0) |
4ad4eba5 AM |
4244 | { |
4245 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4246 | NULL, NULL, NULL); | |
4247 | if (isymbuf == NULL) | |
4248 | goto error_return; | |
4249 | ||
4ad4eba5 | 4250 | if (sym_hash == NULL) |
012b2306 AM |
4251 | { |
4252 | /* We store a pointer to the hash table entry for each | |
4253 | external symbol. */ | |
ef53be89 AM |
4254 | amt = extsymcount; |
4255 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4256 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4257 | if (sym_hash == NULL) | |
4258 | goto error_free_sym; | |
4259 | elf_sym_hashes (abfd) = sym_hash; | |
4260 | } | |
4ad4eba5 AM |
4261 | } |
4262 | ||
4263 | if (dynamic) | |
4264 | { | |
4265 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4266 | if (!_bfd_elf_slurp_version_tables (abfd, |
4267 | info->default_imported_symver)) | |
4ad4eba5 AM |
4268 | goto error_free_sym; |
4269 | ||
4270 | /* Read in the symbol versions, but don't bother to convert them | |
4271 | to internal format. */ | |
4272 | if (elf_dynversym (abfd) != 0) | |
4273 | { | |
4274 | Elf_Internal_Shdr *versymhdr; | |
4275 | ||
4276 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 4277 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
4278 | if (extversym == NULL) |
4279 | goto error_free_sym; | |
4280 | amt = versymhdr->sh_size; | |
4281 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
4282 | || bfd_bread (extversym, amt, abfd) != amt) | |
4283 | goto error_free_vers; | |
4284 | } | |
4285 | } | |
4286 | ||
66eb6687 AM |
4287 | /* If we are loading an as-needed shared lib, save the symbol table |
4288 | state before we start adding symbols. If the lib turns out | |
4289 | to be unneeded, restore the state. */ | |
4290 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4291 | { | |
4292 | unsigned int i; | |
4293 | size_t entsize; | |
4294 | ||
4295 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4296 | { | |
4297 | struct bfd_hash_entry *p; | |
2de92251 | 4298 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4299 | |
4300 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4301 | { |
4302 | h = (struct elf_link_hash_entry *) p; | |
4303 | entsize += htab->root.table.entsize; | |
4304 | if (h->root.type == bfd_link_hash_warning) | |
4305 | entsize += htab->root.table.entsize; | |
4306 | } | |
66eb6687 AM |
4307 | } |
4308 | ||
4309 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4310 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4311 | if (old_tab == NULL) |
4312 | goto error_free_vers; | |
4313 | ||
4314 | /* Remember the current objalloc pointer, so that all mem for | |
4315 | symbols added can later be reclaimed. */ | |
4316 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4317 | if (alloc_mark == NULL) | |
4318 | goto error_free_vers; | |
4319 | ||
5061a885 AM |
4320 | /* Make a special call to the linker "notice" function to |
4321 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4322 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4323 | goto error_free_vers; |
5061a885 | 4324 | |
f45794cb AM |
4325 | /* Clone the symbol table. Remember some pointers into the |
4326 | symbol table, and dynamic symbol count. */ | |
4327 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4328 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4329 | old_undefs = htab->root.undefs; |
4330 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4331 | old_table = htab->root.table.table; |
4332 | old_size = htab->root.table.size; | |
4333 | old_count = htab->root.table.count; | |
5b677558 AM |
4334 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4335 | if (old_strtab == NULL) | |
4336 | goto error_free_vers; | |
66eb6687 AM |
4337 | |
4338 | for (i = 0; i < htab->root.table.size; i++) | |
4339 | { | |
4340 | struct bfd_hash_entry *p; | |
2de92251 | 4341 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4342 | |
4343 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4344 | { | |
4345 | memcpy (old_ent, p, htab->root.table.entsize); | |
4346 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4347 | h = (struct elf_link_hash_entry *) p; |
4348 | if (h->root.type == bfd_link_hash_warning) | |
4349 | { | |
4350 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4351 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4352 | } | |
66eb6687 AM |
4353 | } |
4354 | } | |
4355 | } | |
4ad4eba5 | 4356 | |
66eb6687 | 4357 | weaks = NULL; |
4ad4eba5 AM |
4358 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
4359 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
4360 | isym < isymend; | |
4361 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4362 | { | |
4363 | int bind; | |
4364 | bfd_vma value; | |
af44c138 | 4365 | asection *sec, *new_sec; |
4ad4eba5 AM |
4366 | flagword flags; |
4367 | const char *name; | |
4368 | struct elf_link_hash_entry *h; | |
90c984fc | 4369 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4370 | bfd_boolean definition; |
4371 | bfd_boolean size_change_ok; | |
4372 | bfd_boolean type_change_ok; | |
37a9e49a L |
4373 | bfd_boolean new_weak; |
4374 | bfd_boolean old_weak; | |
4ad4eba5 | 4375 | bfd_boolean override; |
a4d8e49b | 4376 | bfd_boolean common; |
97196564 | 4377 | bfd_boolean discarded; |
4ad4eba5 AM |
4378 | unsigned int old_alignment; |
4379 | bfd *old_bfd; | |
6e33951e | 4380 | bfd_boolean matched; |
4ad4eba5 AM |
4381 | |
4382 | override = FALSE; | |
4383 | ||
4384 | flags = BSF_NO_FLAGS; | |
4385 | sec = NULL; | |
4386 | value = isym->st_value; | |
a4d8e49b | 4387 | common = bed->common_definition (isym); |
2980ccad L |
4388 | if (common && info->inhibit_common_definition) |
4389 | { | |
4390 | /* Treat common symbol as undefined for --no-define-common. */ | |
4391 | isym->st_shndx = SHN_UNDEF; | |
4392 | common = FALSE; | |
4393 | } | |
97196564 | 4394 | discarded = FALSE; |
4ad4eba5 AM |
4395 | |
4396 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4397 | switch (bind) |
4ad4eba5 | 4398 | { |
3e7a7d11 | 4399 | case STB_LOCAL: |
4ad4eba5 AM |
4400 | /* This should be impossible, since ELF requires that all |
4401 | global symbols follow all local symbols, and that sh_info | |
4402 | point to the first global symbol. Unfortunately, Irix 5 | |
4403 | screws this up. */ | |
4404 | continue; | |
3e7a7d11 NC |
4405 | |
4406 | case STB_GLOBAL: | |
a4d8e49b | 4407 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4408 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4409 | break; |
4410 | ||
4411 | case STB_WEAK: | |
4412 | flags = BSF_WEAK; | |
4413 | break; | |
4414 | ||
4415 | case STB_GNU_UNIQUE: | |
4416 | flags = BSF_GNU_UNIQUE; | |
4417 | break; | |
4418 | ||
4419 | default: | |
4ad4eba5 | 4420 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4421 | break; |
4ad4eba5 AM |
4422 | } |
4423 | ||
4424 | if (isym->st_shndx == SHN_UNDEF) | |
4425 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4426 | else if (isym->st_shndx == SHN_ABS) |
4427 | sec = bfd_abs_section_ptr; | |
4428 | else if (isym->st_shndx == SHN_COMMON) | |
4429 | { | |
4430 | sec = bfd_com_section_ptr; | |
4431 | /* What ELF calls the size we call the value. What ELF | |
4432 | calls the value we call the alignment. */ | |
4433 | value = isym->st_size; | |
4434 | } | |
4435 | else | |
4ad4eba5 AM |
4436 | { |
4437 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4438 | if (sec == NULL) | |
4439 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4440 | else if (discarded_section (sec)) |
529fcb95 | 4441 | { |
e5d08002 L |
4442 | /* Symbols from discarded section are undefined. We keep |
4443 | its visibility. */ | |
529fcb95 | 4444 | sec = bfd_und_section_ptr; |
97196564 | 4445 | discarded = TRUE; |
529fcb95 PB |
4446 | isym->st_shndx = SHN_UNDEF; |
4447 | } | |
4ad4eba5 AM |
4448 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4449 | value -= sec->vma; | |
4450 | } | |
4ad4eba5 AM |
4451 | |
4452 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4453 | isym->st_name); | |
4454 | if (name == NULL) | |
4455 | goto error_free_vers; | |
4456 | ||
4457 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4458 | && (abfd->flags & BFD_PLUGIN) != 0) |
4459 | { | |
4460 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4461 | ||
4462 | if (xc == NULL) | |
4463 | { | |
4464 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4465 | | SEC_EXCLUDE); | |
4466 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4467 | if (xc == NULL) | |
4468 | goto error_free_vers; | |
4469 | } | |
4470 | sec = xc; | |
4471 | } | |
4472 | else if (isym->st_shndx == SHN_COMMON | |
4473 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4474 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4475 | { |
4476 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4477 | ||
4478 | if (tcomm == NULL) | |
4479 | { | |
02d00247 AM |
4480 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4481 | | SEC_LINKER_CREATED); | |
4482 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4483 | if (tcomm == NULL) |
4ad4eba5 AM |
4484 | goto error_free_vers; |
4485 | } | |
4486 | sec = tcomm; | |
4487 | } | |
66eb6687 | 4488 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4489 | { |
66eb6687 AM |
4490 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4491 | &sec, &value)) | |
4ad4eba5 AM |
4492 | goto error_free_vers; |
4493 | ||
4494 | /* The hook function sets the name to NULL if this symbol | |
4495 | should be skipped for some reason. */ | |
4496 | if (name == NULL) | |
4497 | continue; | |
4498 | } | |
4499 | ||
4500 | /* Sanity check that all possibilities were handled. */ | |
4501 | if (sec == NULL) | |
4502 | { | |
4503 | bfd_set_error (bfd_error_bad_value); | |
4504 | goto error_free_vers; | |
4505 | } | |
4506 | ||
191c0c42 AM |
4507 | /* Silently discard TLS symbols from --just-syms. There's |
4508 | no way to combine a static TLS block with a new TLS block | |
4509 | for this executable. */ | |
4510 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4511 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4512 | continue; | |
4513 | ||
4ad4eba5 AM |
4514 | if (bfd_is_und_section (sec) |
4515 | || bfd_is_com_section (sec)) | |
4516 | definition = FALSE; | |
4517 | else | |
4518 | definition = TRUE; | |
4519 | ||
4520 | size_change_ok = FALSE; | |
66eb6687 | 4521 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4522 | old_weak = FALSE; |
6e33951e | 4523 | matched = FALSE; |
4ad4eba5 AM |
4524 | old_alignment = 0; |
4525 | old_bfd = NULL; | |
af44c138 | 4526 | new_sec = sec; |
4ad4eba5 | 4527 | |
66eb6687 | 4528 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4529 | { |
4530 | Elf_Internal_Versym iver; | |
4531 | unsigned int vernum = 0; | |
4532 | bfd_boolean skip; | |
4533 | ||
fc0e6df6 | 4534 | if (ever == NULL) |
4ad4eba5 | 4535 | { |
fc0e6df6 PB |
4536 | if (info->default_imported_symver) |
4537 | /* Use the default symbol version created earlier. */ | |
4538 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4539 | else | |
4540 | iver.vs_vers = 0; | |
4541 | } | |
4542 | else | |
4543 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4544 | ||
4545 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4546 | ||
4547 | /* If this is a hidden symbol, or if it is not version | |
4548 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4549 | However, we do not modify a non-hidden absolute symbol |
4550 | if it is not a function, because it might be the version | |
4551 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4552 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4553 | || (vernum > 1 |
4554 | && (!bfd_is_abs_section (sec) | |
4555 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4556 | { |
4557 | const char *verstr; | |
4558 | size_t namelen, verlen, newlen; | |
4559 | char *newname, *p; | |
4560 | ||
4561 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4562 | { |
fc0e6df6 PB |
4563 | if (vernum > elf_tdata (abfd)->cverdefs) |
4564 | verstr = NULL; | |
4565 | else if (vernum > 1) | |
4566 | verstr = | |
4567 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4568 | else | |
4569 | verstr = ""; | |
4ad4eba5 | 4570 | |
fc0e6df6 | 4571 | if (verstr == NULL) |
4ad4eba5 | 4572 | { |
4eca0228 | 4573 | _bfd_error_handler |
695344c0 | 4574 | /* xgettext:c-format */ |
871b3ab2 | 4575 | (_("%pB: %s: invalid version %u (max %d)"), |
fc0e6df6 PB |
4576 | abfd, name, vernum, |
4577 | elf_tdata (abfd)->cverdefs); | |
4578 | bfd_set_error (bfd_error_bad_value); | |
4579 | goto error_free_vers; | |
4ad4eba5 | 4580 | } |
fc0e6df6 PB |
4581 | } |
4582 | else | |
4583 | { | |
4584 | /* We cannot simply test for the number of | |
4585 | entries in the VERNEED section since the | |
4586 | numbers for the needed versions do not start | |
4587 | at 0. */ | |
4588 | Elf_Internal_Verneed *t; | |
4589 | ||
4590 | verstr = NULL; | |
4591 | for (t = elf_tdata (abfd)->verref; | |
4592 | t != NULL; | |
4593 | t = t->vn_nextref) | |
4ad4eba5 | 4594 | { |
fc0e6df6 | 4595 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4596 | |
fc0e6df6 PB |
4597 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4598 | { | |
4599 | if (a->vna_other == vernum) | |
4ad4eba5 | 4600 | { |
fc0e6df6 PB |
4601 | verstr = a->vna_nodename; |
4602 | break; | |
4ad4eba5 | 4603 | } |
4ad4eba5 | 4604 | } |
fc0e6df6 PB |
4605 | if (a != NULL) |
4606 | break; | |
4607 | } | |
4608 | if (verstr == NULL) | |
4609 | { | |
4eca0228 | 4610 | _bfd_error_handler |
695344c0 | 4611 | /* xgettext:c-format */ |
871b3ab2 | 4612 | (_("%pB: %s: invalid needed version %d"), |
fc0e6df6 PB |
4613 | abfd, name, vernum); |
4614 | bfd_set_error (bfd_error_bad_value); | |
4615 | goto error_free_vers; | |
4ad4eba5 | 4616 | } |
4ad4eba5 | 4617 | } |
fc0e6df6 PB |
4618 | |
4619 | namelen = strlen (name); | |
4620 | verlen = strlen (verstr); | |
4621 | newlen = namelen + verlen + 2; | |
4622 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4623 | && isym->st_shndx != SHN_UNDEF) | |
4624 | ++newlen; | |
4625 | ||
a50b1753 | 4626 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4627 | if (newname == NULL) |
4628 | goto error_free_vers; | |
4629 | memcpy (newname, name, namelen); | |
4630 | p = newname + namelen; | |
4631 | *p++ = ELF_VER_CHR; | |
4632 | /* If this is a defined non-hidden version symbol, | |
4633 | we add another @ to the name. This indicates the | |
4634 | default version of the symbol. */ | |
4635 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4636 | && isym->st_shndx != SHN_UNDEF) | |
4637 | *p++ = ELF_VER_CHR; | |
4638 | memcpy (p, verstr, verlen + 1); | |
4639 | ||
4640 | name = newname; | |
4ad4eba5 AM |
4641 | } |
4642 | ||
cd3416da AM |
4643 | /* If this symbol has default visibility and the user has |
4644 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4645 | if (!bfd_is_und_section (sec) |
cd3416da | 4646 | && !dynamic |
ce875075 | 4647 | && abfd->no_export |
cd3416da AM |
4648 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4649 | isym->st_other = (STV_HIDDEN | |
4650 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4651 | ||
4f3fedcf AM |
4652 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4653 | sym_hash, &old_bfd, &old_weak, | |
4654 | &old_alignment, &skip, &override, | |
6e33951e L |
4655 | &type_change_ok, &size_change_ok, |
4656 | &matched)) | |
4ad4eba5 AM |
4657 | goto error_free_vers; |
4658 | ||
4659 | if (skip) | |
4660 | continue; | |
4661 | ||
6e33951e L |
4662 | /* Override a definition only if the new symbol matches the |
4663 | existing one. */ | |
4664 | if (override && matched) | |
4ad4eba5 AM |
4665 | definition = FALSE; |
4666 | ||
4667 | h = *sym_hash; | |
4668 | while (h->root.type == bfd_link_hash_indirect | |
4669 | || h->root.type == bfd_link_hash_warning) | |
4670 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4671 | ||
4ad4eba5 | 4672 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4673 | && vernum > 1 |
4674 | && definition) | |
4675 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4676 | } | |
4677 | ||
4678 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4679 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4680 | (struct bfd_link_hash_entry **) sym_hash))) |
4681 | goto error_free_vers; | |
4682 | ||
a43942db MR |
4683 | if ((flags & BSF_GNU_UNIQUE) |
4684 | && (abfd->flags & DYNAMIC) == 0 | |
4685 | && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) | |
4686 | elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; | |
4687 | ||
4ad4eba5 | 4688 | h = *sym_hash; |
90c984fc L |
4689 | /* We need to make sure that indirect symbol dynamic flags are |
4690 | updated. */ | |
4691 | hi = h; | |
4ad4eba5 AM |
4692 | while (h->root.type == bfd_link_hash_indirect |
4693 | || h->root.type == bfd_link_hash_warning) | |
4694 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4695 | |
97196564 L |
4696 | /* Setting the index to -3 tells elf_link_output_extsym that |
4697 | this symbol is defined in a discarded section. */ | |
4698 | if (discarded) | |
4699 | h->indx = -3; | |
4700 | ||
4ad4eba5 AM |
4701 | *sym_hash = h; |
4702 | ||
37a9e49a | 4703 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4704 | if (dynamic |
4705 | && definition | |
37a9e49a | 4706 | && new_weak |
fcb93ecf | 4707 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4708 | && is_elf_hash_table (htab) |
60d67dc8 | 4709 | && h->u.alias == NULL) |
4ad4eba5 AM |
4710 | { |
4711 | /* Keep a list of all weak defined non function symbols from | |
60d67dc8 AM |
4712 | a dynamic object, using the alias field. Later in this |
4713 | function we will set the alias field to the correct | |
4ad4eba5 AM |
4714 | value. We only put non-function symbols from dynamic |
4715 | objects on this list, because that happens to be the only | |
4716 | time we need to know the normal symbol corresponding to a | |
4717 | weak symbol, and the information is time consuming to | |
60d67dc8 | 4718 | figure out. If the alias field is not already NULL, |
4ad4eba5 AM |
4719 | then this symbol was already defined by some previous |
4720 | dynamic object, and we will be using that previous | |
4721 | definition anyhow. */ | |
4722 | ||
60d67dc8 | 4723 | h->u.alias = weaks; |
4ad4eba5 | 4724 | weaks = h; |
4ad4eba5 AM |
4725 | } |
4726 | ||
4727 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4728 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4729 | && h->root.type == bfd_link_hash_common) |
4730 | { | |
4731 | unsigned int align; | |
4732 | ||
a4d8e49b | 4733 | if (common) |
af44c138 L |
4734 | align = bfd_log2 (isym->st_value); |
4735 | else | |
4736 | { | |
4737 | /* The new symbol is a common symbol in a shared object. | |
4738 | We need to get the alignment from the section. */ | |
4739 | align = new_sec->alignment_power; | |
4740 | } | |
595213d4 | 4741 | if (align > old_alignment) |
4ad4eba5 AM |
4742 | h->root.u.c.p->alignment_power = align; |
4743 | else | |
4744 | h->root.u.c.p->alignment_power = old_alignment; | |
4745 | } | |
4746 | ||
66eb6687 | 4747 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4748 | { |
4f3fedcf AM |
4749 | /* Set a flag in the hash table entry indicating the type of |
4750 | reference or definition we just found. A dynamic symbol | |
4751 | is one which is referenced or defined by both a regular | |
4752 | object and a shared object. */ | |
4753 | bfd_boolean dynsym = FALSE; | |
4754 | ||
4755 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4756 | ref_regular for them, or make them dynamic. */ | |
4757 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4758 | ; | |
4759 | else if (! dynamic) | |
4760 | { | |
4761 | if (! definition) | |
4762 | { | |
4763 | h->ref_regular = 1; | |
4764 | if (bind != STB_WEAK) | |
4765 | h->ref_regular_nonweak = 1; | |
4766 | } | |
4767 | else | |
4768 | { | |
4769 | h->def_regular = 1; | |
4770 | if (h->def_dynamic) | |
4771 | { | |
4772 | h->def_dynamic = 0; | |
4773 | h->ref_dynamic = 1; | |
4774 | } | |
4775 | } | |
4776 | ||
4777 | /* If the indirect symbol has been forced local, don't | |
4778 | make the real symbol dynamic. */ | |
4779 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4780 | && (bfd_link_dll (info) |
4f3fedcf AM |
4781 | || h->def_dynamic |
4782 | || h->ref_dynamic)) | |
4783 | dynsym = TRUE; | |
4784 | } | |
4785 | else | |
4786 | { | |
4787 | if (! definition) | |
4788 | { | |
4789 | h->ref_dynamic = 1; | |
4790 | hi->ref_dynamic = 1; | |
4791 | } | |
4792 | else | |
4793 | { | |
4794 | h->def_dynamic = 1; | |
4795 | hi->def_dynamic = 1; | |
4796 | } | |
4797 | ||
4798 | /* If the indirect symbol has been forced local, don't | |
4799 | make the real symbol dynamic. */ | |
4800 | if ((h == hi || !hi->forced_local) | |
4801 | && (h->def_regular | |
4802 | || h->ref_regular | |
60d67dc8 AM |
4803 | || (h->is_weakalias |
4804 | && weakdef (h)->dynindx != -1))) | |
4f3fedcf AM |
4805 | dynsym = TRUE; |
4806 | } | |
4807 | ||
4808 | /* Check to see if we need to add an indirect symbol for | |
4809 | the default name. */ | |
4810 | if (definition | |
4811 | || (!override && h->root.type == bfd_link_hash_common)) | |
4812 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4813 | sec, value, &old_bfd, &dynsym)) | |
4814 | goto error_free_vers; | |
4ad4eba5 AM |
4815 | |
4816 | /* Check the alignment when a common symbol is involved. This | |
4817 | can change when a common symbol is overridden by a normal | |
4818 | definition or a common symbol is ignored due to the old | |
4819 | normal definition. We need to make sure the maximum | |
4820 | alignment is maintained. */ | |
a4d8e49b | 4821 | if ((old_alignment || common) |
4ad4eba5 AM |
4822 | && h->root.type != bfd_link_hash_common) |
4823 | { | |
4824 | unsigned int common_align; | |
4825 | unsigned int normal_align; | |
4826 | unsigned int symbol_align; | |
4827 | bfd *normal_bfd; | |
4828 | bfd *common_bfd; | |
4829 | ||
3a81e825 AM |
4830 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4831 | || h->root.type == bfd_link_hash_defweak); | |
4832 | ||
4ad4eba5 AM |
4833 | symbol_align = ffs (h->root.u.def.value) - 1; |
4834 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4835 | && (h->root.u.def.section->owner->flags |
4836 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4837 | { |
4838 | normal_align = h->root.u.def.section->alignment_power; | |
4839 | if (normal_align > symbol_align) | |
4840 | normal_align = symbol_align; | |
4841 | } | |
4842 | else | |
4843 | normal_align = symbol_align; | |
4844 | ||
4845 | if (old_alignment) | |
4846 | { | |
4847 | common_align = old_alignment; | |
4848 | common_bfd = old_bfd; | |
4849 | normal_bfd = abfd; | |
4850 | } | |
4851 | else | |
4852 | { | |
4853 | common_align = bfd_log2 (isym->st_value); | |
4854 | common_bfd = abfd; | |
4855 | normal_bfd = old_bfd; | |
4856 | } | |
4857 | ||
4858 | if (normal_align < common_align) | |
d07676f8 NC |
4859 | { |
4860 | /* PR binutils/2735 */ | |
4861 | if (normal_bfd == NULL) | |
4eca0228 | 4862 | _bfd_error_handler |
695344c0 | 4863 | /* xgettext:c-format */ |
9793eb77 | 4864 | (_("warning: alignment %u of common symbol `%s' in %pB is" |
871b3ab2 | 4865 | " greater than the alignment (%u) of its section %pA"), |
c08bb8dd AM |
4866 | 1 << common_align, name, common_bfd, |
4867 | 1 << normal_align, h->root.u.def.section); | |
d07676f8 | 4868 | else |
4eca0228 | 4869 | _bfd_error_handler |
695344c0 | 4870 | /* xgettext:c-format */ |
9793eb77 | 4871 | (_("warning: alignment %u of symbol `%s' in %pB" |
871b3ab2 | 4872 | " is smaller than %u in %pB"), |
c08bb8dd AM |
4873 | 1 << normal_align, name, normal_bfd, |
4874 | 1 << common_align, common_bfd); | |
d07676f8 | 4875 | } |
4ad4eba5 AM |
4876 | } |
4877 | ||
83ad0046 | 4878 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4879 | if (isym->st_size != 0 |
4880 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4881 | && (definition || h->size == 0)) |
4882 | { | |
83ad0046 L |
4883 | if (h->size != 0 |
4884 | && h->size != isym->st_size | |
4885 | && ! size_change_ok) | |
4eca0228 | 4886 | _bfd_error_handler |
695344c0 | 4887 | /* xgettext:c-format */ |
9793eb77 | 4888 | (_("warning: size of symbol `%s' changed" |
2dcf00ce AM |
4889 | " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"), |
4890 | name, (uint64_t) h->size, old_bfd, | |
4891 | (uint64_t) isym->st_size, abfd); | |
4ad4eba5 AM |
4892 | |
4893 | h->size = isym->st_size; | |
4894 | } | |
4895 | ||
4896 | /* If this is a common symbol, then we always want H->SIZE | |
4897 | to be the size of the common symbol. The code just above | |
4898 | won't fix the size if a common symbol becomes larger. We | |
4899 | don't warn about a size change here, because that is | |
4f3fedcf | 4900 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4901 | function types. */ |
4ad4eba5 AM |
4902 | if (h->root.type == bfd_link_hash_common) |
4903 | h->size = h->root.u.c.size; | |
4904 | ||
4905 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4906 | && ((definition && !new_weak) |
4907 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4908 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4909 | { |
2955ec4c L |
4910 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4911 | ||
4912 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4913 | symbol. */ | |
4914 | if (type == STT_GNU_IFUNC | |
4915 | && (abfd->flags & DYNAMIC) != 0) | |
4916 | type = STT_FUNC; | |
4ad4eba5 | 4917 | |
2955ec4c L |
4918 | if (h->type != type) |
4919 | { | |
4920 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 4921 | /* xgettext:c-format */ |
4eca0228 | 4922 | _bfd_error_handler |
9793eb77 | 4923 | (_("warning: type of symbol `%s' changed" |
871b3ab2 | 4924 | " from %d to %d in %pB"), |
c08bb8dd | 4925 | name, h->type, type, abfd); |
2955ec4c L |
4926 | |
4927 | h->type = type; | |
4928 | } | |
4ad4eba5 AM |
4929 | } |
4930 | ||
54ac0771 | 4931 | /* Merge st_other field. */ |
b8417128 | 4932 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4933 | |
c3df8c14 | 4934 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
4935 | if (definition |
4936 | && (sec->flags & SEC_DEBUGGING) | |
4937 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
4938 | dynsym = FALSE; |
4939 | ||
4f3fedcf AM |
4940 | /* Nor should we make plugin symbols dynamic. */ |
4941 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4942 | dynsym = FALSE; | |
4943 | ||
35fc36a8 | 4944 | if (definition) |
35399224 L |
4945 | { |
4946 | h->target_internal = isym->st_target_internal; | |
4947 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4948 | } | |
35fc36a8 | 4949 | |
4ad4eba5 AM |
4950 | if (definition && !dynamic) |
4951 | { | |
4952 | char *p = strchr (name, ELF_VER_CHR); | |
4953 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4954 | { | |
4955 | /* Queue non-default versions so that .symver x, x@FOO | |
4956 | aliases can be checked. */ | |
66eb6687 | 4957 | if (!nondeflt_vers) |
4ad4eba5 | 4958 | { |
66eb6687 AM |
4959 | amt = ((isymend - isym + 1) |
4960 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4961 | nondeflt_vers |
4962 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4963 | if (!nondeflt_vers) |
4964 | goto error_free_vers; | |
4ad4eba5 | 4965 | } |
66eb6687 | 4966 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4967 | } |
4968 | } | |
4969 | ||
4970 | if (dynsym && h->dynindx == -1) | |
4971 | { | |
c152c796 | 4972 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4973 | goto error_free_vers; |
60d67dc8 AM |
4974 | if (h->is_weakalias |
4975 | && weakdef (h)->dynindx == -1) | |
4ad4eba5 | 4976 | { |
60d67dc8 | 4977 | if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) |
4ad4eba5 AM |
4978 | goto error_free_vers; |
4979 | } | |
4980 | } | |
1f599d0e | 4981 | else if (h->dynindx != -1) |
4ad4eba5 AM |
4982 | /* If the symbol already has a dynamic index, but |
4983 | visibility says it should not be visible, turn it into | |
4984 | a local symbol. */ | |
4985 | switch (ELF_ST_VISIBILITY (h->other)) | |
4986 | { | |
4987 | case STV_INTERNAL: | |
4988 | case STV_HIDDEN: | |
4989 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4990 | dynsym = FALSE; | |
4991 | break; | |
4992 | } | |
4993 | ||
aef28989 L |
4994 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
4995 | for unmatched symbol. */ | |
4ad4eba5 | 4996 | if (!add_needed |
aef28989 | 4997 | && matched |
4ad4eba5 | 4998 | && definition |
010e5ae2 | 4999 | && ((dynsym |
ffa9430d | 5000 | && h->ref_regular_nonweak |
4f3fedcf AM |
5001 | && (old_bfd == NULL |
5002 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 5003 | || (h->ref_dynamic_nonweak |
010e5ae2 | 5004 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
5005 | && !on_needed_list (elf_dt_name (abfd), |
5006 | htab->needed, NULL)))) | |
4ad4eba5 AM |
5007 | { |
5008 | int ret; | |
5009 | const char *soname = elf_dt_name (abfd); | |
5010 | ||
16e4ecc0 AM |
5011 | info->callbacks->minfo ("%!", soname, old_bfd, |
5012 | h->root.root.string); | |
5013 | ||
4ad4eba5 AM |
5014 | /* A symbol from a library loaded via DT_NEEDED of some |
5015 | other library is referenced by a regular object. | |
e56f61be | 5016 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
5017 | --no-add-needed is used and the reference was not |
5018 | a weak one. */ | |
4f3fedcf | 5019 | if (old_bfd != NULL |
b918acf9 | 5020 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 5021 | { |
4eca0228 | 5022 | _bfd_error_handler |
695344c0 | 5023 | /* xgettext:c-format */ |
871b3ab2 | 5024 | (_("%pB: undefined reference to symbol '%s'"), |
4f3fedcf | 5025 | old_bfd, name); |
ff5ac77b | 5026 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
5027 | goto error_free_vers; |
5028 | } | |
5029 | ||
a50b1753 | 5030 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 5031 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 5032 | |
4ad4eba5 | 5033 | add_needed = TRUE; |
7e9f0867 | 5034 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
5035 | if (ret < 0) |
5036 | goto error_free_vers; | |
5037 | ||
5038 | BFD_ASSERT (ret == 0); | |
5039 | } | |
5040 | } | |
5041 | } | |
5042 | ||
a83ef4d1 L |
5043 | if (info->lto_plugin_active |
5044 | && !bfd_link_relocatable (info) | |
5045 | && (abfd->flags & BFD_PLUGIN) == 0 | |
5046 | && !just_syms | |
5047 | && extsymcount) | |
5048 | { | |
5049 | int r_sym_shift; | |
5050 | ||
5051 | if (bed->s->arch_size == 32) | |
5052 | r_sym_shift = 8; | |
5053 | else | |
5054 | r_sym_shift = 32; | |
5055 | ||
5056 | /* If linker plugin is enabled, set non_ir_ref_regular on symbols | |
5057 | referenced in regular objects so that linker plugin will get | |
5058 | the correct symbol resolution. */ | |
5059 | ||
5060 | sym_hash = elf_sym_hashes (abfd); | |
5061 | for (s = abfd->sections; s != NULL; s = s->next) | |
5062 | { | |
5063 | Elf_Internal_Rela *internal_relocs; | |
5064 | Elf_Internal_Rela *rel, *relend; | |
5065 | ||
5066 | /* Don't check relocations in excluded sections. */ | |
5067 | if ((s->flags & SEC_RELOC) == 0 | |
5068 | || s->reloc_count == 0 | |
5069 | || (s->flags & SEC_EXCLUDE) != 0 | |
5070 | || ((info->strip == strip_all | |
5071 | || info->strip == strip_debugger) | |
5072 | && (s->flags & SEC_DEBUGGING) != 0)) | |
5073 | continue; | |
5074 | ||
5075 | internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL, | |
5076 | NULL, | |
5077 | info->keep_memory); | |
5078 | if (internal_relocs == NULL) | |
5079 | goto error_free_vers; | |
5080 | ||
5081 | rel = internal_relocs; | |
5082 | relend = rel + s->reloc_count; | |
5083 | for ( ; rel < relend; rel++) | |
5084 | { | |
5085 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
5086 | struct elf_link_hash_entry *h; | |
5087 | ||
5088 | /* Skip local symbols. */ | |
5089 | if (r_symndx < extsymoff) | |
5090 | continue; | |
5091 | ||
5092 | h = sym_hash[r_symndx - extsymoff]; | |
5093 | if (h != NULL) | |
5094 | h->root.non_ir_ref_regular = 1; | |
5095 | } | |
5096 | ||
5097 | if (elf_section_data (s)->relocs != internal_relocs) | |
5098 | free (internal_relocs); | |
5099 | } | |
5100 | } | |
5101 | ||
66eb6687 AM |
5102 | if (extversym != NULL) |
5103 | { | |
5104 | free (extversym); | |
5105 | extversym = NULL; | |
5106 | } | |
5107 | ||
5108 | if (isymbuf != NULL) | |
5109 | { | |
5110 | free (isymbuf); | |
5111 | isymbuf = NULL; | |
5112 | } | |
5113 | ||
5114 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
5115 | { | |
5116 | unsigned int i; | |
5117 | ||
5118 | /* Restore the symbol table. */ | |
f45794cb AM |
5119 | old_ent = (char *) old_tab + tabsize; |
5120 | memset (elf_sym_hashes (abfd), 0, | |
5121 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
5122 | htab->root.table.table = old_table; |
5123 | htab->root.table.size = old_size; | |
5124 | htab->root.table.count = old_count; | |
66eb6687 | 5125 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
5126 | htab->root.undefs = old_undefs; |
5127 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
5128 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
5129 | free (old_strtab); | |
5130 | old_strtab = NULL; | |
66eb6687 AM |
5131 | for (i = 0; i < htab->root.table.size; i++) |
5132 | { | |
5133 | struct bfd_hash_entry *p; | |
5134 | struct elf_link_hash_entry *h; | |
3e0882af L |
5135 | bfd_size_type size; |
5136 | unsigned int alignment_power; | |
4070765b | 5137 | unsigned int non_ir_ref_dynamic; |
66eb6687 AM |
5138 | |
5139 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
5140 | { | |
5141 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
5142 | if (h->root.type == bfd_link_hash_warning) |
5143 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 5144 | |
3e0882af L |
5145 | /* Preserve the maximum alignment and size for common |
5146 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 5147 | since it can still be loaded at run time by another |
3e0882af L |
5148 | dynamic lib. */ |
5149 | if (h->root.type == bfd_link_hash_common) | |
5150 | { | |
5151 | size = h->root.u.c.size; | |
5152 | alignment_power = h->root.u.c.p->alignment_power; | |
5153 | } | |
5154 | else | |
5155 | { | |
5156 | size = 0; | |
5157 | alignment_power = 0; | |
5158 | } | |
4070765b | 5159 | /* Preserve non_ir_ref_dynamic so that this symbol |
59fa66c5 L |
5160 | will be exported when the dynamic lib becomes needed |
5161 | in the second pass. */ | |
4070765b | 5162 | non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; |
66eb6687 AM |
5163 | memcpy (p, old_ent, htab->root.table.entsize); |
5164 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
5165 | h = (struct elf_link_hash_entry *) p; |
5166 | if (h->root.type == bfd_link_hash_warning) | |
5167 | { | |
5168 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
5169 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 5170 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 5171 | } |
a4542f1b | 5172 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
5173 | { |
5174 | if (size > h->root.u.c.size) | |
5175 | h->root.u.c.size = size; | |
5176 | if (alignment_power > h->root.u.c.p->alignment_power) | |
5177 | h->root.u.c.p->alignment_power = alignment_power; | |
5178 | } | |
4070765b | 5179 | h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; |
66eb6687 AM |
5180 | } |
5181 | } | |
5182 | ||
5061a885 AM |
5183 | /* Make a special call to the linker "notice" function to |
5184 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 5185 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 5186 | goto error_free_vers; |
5061a885 | 5187 | |
66eb6687 AM |
5188 | free (old_tab); |
5189 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
5190 | alloc_mark); | |
5191 | if (nondeflt_vers != NULL) | |
5192 | free (nondeflt_vers); | |
5193 | return TRUE; | |
5194 | } | |
2de92251 | 5195 | |
66eb6687 AM |
5196 | if (old_tab != NULL) |
5197 | { | |
e5034e59 | 5198 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 5199 | goto error_free_vers; |
66eb6687 AM |
5200 | free (old_tab); |
5201 | old_tab = NULL; | |
5202 | } | |
5203 | ||
c6e8a9a8 L |
5204 | /* Now that all the symbols from this input file are created, if |
5205 | not performing a relocatable link, handle .symver foo, foo@BAR | |
5206 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 5207 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 5208 | { |
ef53be89 | 5209 | size_t cnt, symidx; |
4ad4eba5 AM |
5210 | |
5211 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
5212 | { | |
5213 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
5214 | char *shortname, *p; | |
5215 | ||
5216 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
5217 | if (p == NULL | |
5218 | || (h->root.type != bfd_link_hash_defined | |
5219 | && h->root.type != bfd_link_hash_defweak)) | |
5220 | continue; | |
5221 | ||
5222 | amt = p - h->root.root.string; | |
a50b1753 | 5223 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
5224 | if (!shortname) |
5225 | goto error_free_vers; | |
4ad4eba5 AM |
5226 | memcpy (shortname, h->root.root.string, amt); |
5227 | shortname[amt] = '\0'; | |
5228 | ||
5229 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 5230 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
5231 | FALSE, FALSE, FALSE); |
5232 | if (hi != NULL | |
5233 | && hi->root.type == h->root.type | |
5234 | && hi->root.u.def.value == h->root.u.def.value | |
5235 | && hi->root.u.def.section == h->root.u.def.section) | |
5236 | { | |
5237 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
5238 | hi->root.type = bfd_link_hash_indirect; | |
5239 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 5240 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
5241 | sym_hash = elf_sym_hashes (abfd); |
5242 | if (sym_hash) | |
5243 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
5244 | if (sym_hash[symidx] == hi) | |
5245 | { | |
5246 | sym_hash[symidx] = h; | |
5247 | break; | |
5248 | } | |
5249 | } | |
5250 | free (shortname); | |
5251 | } | |
5252 | free (nondeflt_vers); | |
5253 | nondeflt_vers = NULL; | |
5254 | } | |
5255 | ||
60d67dc8 | 5256 | /* Now set the alias field correctly for all the weak defined |
4ad4eba5 AM |
5257 | symbols we found. The only way to do this is to search all the |
5258 | symbols. Since we only need the information for non functions in | |
5259 | dynamic objects, that's the only time we actually put anything on | |
5260 | the list WEAKS. We need this information so that if a regular | |
5261 | object refers to a symbol defined weakly in a dynamic object, the | |
5262 | real symbol in the dynamic object is also put in the dynamic | |
5263 | symbols; we also must arrange for both symbols to point to the | |
5264 | same memory location. We could handle the general case of symbol | |
5265 | aliasing, but a general symbol alias can only be generated in | |
5266 | assembler code, handling it correctly would be very time | |
5267 | consuming, and other ELF linkers don't handle general aliasing | |
5268 | either. */ | |
5269 | if (weaks != NULL) | |
5270 | { | |
5271 | struct elf_link_hash_entry **hpp; | |
5272 | struct elf_link_hash_entry **hppend; | |
5273 | struct elf_link_hash_entry **sorted_sym_hash; | |
5274 | struct elf_link_hash_entry *h; | |
5275 | size_t sym_count; | |
5276 | ||
5277 | /* Since we have to search the whole symbol list for each weak | |
5278 | defined symbol, search time for N weak defined symbols will be | |
5279 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
5280 | amt = extsymcount; |
5281 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 5282 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
5283 | if (sorted_sym_hash == NULL) |
5284 | goto error_return; | |
5285 | sym_hash = sorted_sym_hash; | |
5286 | hpp = elf_sym_hashes (abfd); | |
5287 | hppend = hpp + extsymcount; | |
5288 | sym_count = 0; | |
5289 | for (; hpp < hppend; hpp++) | |
5290 | { | |
5291 | h = *hpp; | |
5292 | if (h != NULL | |
5293 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 5294 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
5295 | { |
5296 | *sym_hash = h; | |
5297 | sym_hash++; | |
5298 | sym_count++; | |
5299 | } | |
5300 | } | |
5301 | ||
5302 | qsort (sorted_sym_hash, sym_count, | |
5303 | sizeof (struct elf_link_hash_entry *), | |
5304 | elf_sort_symbol); | |
5305 | ||
5306 | while (weaks != NULL) | |
5307 | { | |
5308 | struct elf_link_hash_entry *hlook; | |
5309 | asection *slook; | |
5310 | bfd_vma vlook; | |
ed54588d | 5311 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5312 | |
5313 | hlook = weaks; | |
60d67dc8 AM |
5314 | weaks = hlook->u.alias; |
5315 | hlook->u.alias = NULL; | |
4ad4eba5 | 5316 | |
e3e53eed AM |
5317 | if (hlook->root.type != bfd_link_hash_defined |
5318 | && hlook->root.type != bfd_link_hash_defweak) | |
5319 | continue; | |
5320 | ||
4ad4eba5 AM |
5321 | slook = hlook->root.u.def.section; |
5322 | vlook = hlook->root.u.def.value; | |
5323 | ||
4ad4eba5 AM |
5324 | i = 0; |
5325 | j = sym_count; | |
14160578 | 5326 | while (i != j) |
4ad4eba5 AM |
5327 | { |
5328 | bfd_signed_vma vdiff; | |
5329 | idx = (i + j) / 2; | |
14160578 | 5330 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5331 | vdiff = vlook - h->root.u.def.value; |
5332 | if (vdiff < 0) | |
5333 | j = idx; | |
5334 | else if (vdiff > 0) | |
5335 | i = idx + 1; | |
5336 | else | |
5337 | { | |
d3435ae8 | 5338 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5339 | if (sdiff < 0) |
5340 | j = idx; | |
5341 | else if (sdiff > 0) | |
5342 | i = idx + 1; | |
5343 | else | |
14160578 | 5344 | break; |
4ad4eba5 AM |
5345 | } |
5346 | } | |
5347 | ||
5348 | /* We didn't find a value/section match. */ | |
14160578 | 5349 | if (i == j) |
4ad4eba5 AM |
5350 | continue; |
5351 | ||
14160578 AM |
5352 | /* With multiple aliases, or when the weak symbol is already |
5353 | strongly defined, we have multiple matching symbols and | |
5354 | the binary search above may land on any of them. Step | |
5355 | one past the matching symbol(s). */ | |
5356 | while (++idx != j) | |
5357 | { | |
5358 | h = sorted_sym_hash[idx]; | |
5359 | if (h->root.u.def.section != slook | |
5360 | || h->root.u.def.value != vlook) | |
5361 | break; | |
5362 | } | |
5363 | ||
5364 | /* Now look back over the aliases. Since we sorted by size | |
5365 | as well as value and section, we'll choose the one with | |
5366 | the largest size. */ | |
5367 | while (idx-- != i) | |
4ad4eba5 | 5368 | { |
14160578 | 5369 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5370 | |
5371 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5372 | if (h->root.u.def.section != slook |
5373 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5374 | break; |
5375 | else if (h != hlook) | |
5376 | { | |
60d67dc8 AM |
5377 | struct elf_link_hash_entry *t; |
5378 | ||
5379 | hlook->u.alias = h; | |
5380 | hlook->is_weakalias = 1; | |
5381 | t = h; | |
5382 | if (t->u.alias != NULL) | |
5383 | while (t->u.alias != h) | |
5384 | t = t->u.alias; | |
5385 | t->u.alias = hlook; | |
4ad4eba5 AM |
5386 | |
5387 | /* If the weak definition is in the list of dynamic | |
5388 | symbols, make sure the real definition is put | |
5389 | there as well. */ | |
5390 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5391 | { | |
c152c796 | 5392 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5393 | { |
5394 | err_free_sym_hash: | |
5395 | free (sorted_sym_hash); | |
5396 | goto error_return; | |
5397 | } | |
4ad4eba5 AM |
5398 | } |
5399 | ||
5400 | /* If the real definition is in the list of dynamic | |
5401 | symbols, make sure the weak definition is put | |
5402 | there as well. If we don't do this, then the | |
5403 | dynamic loader might not merge the entries for the | |
5404 | real definition and the weak definition. */ | |
5405 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5406 | { | |
c152c796 | 5407 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5408 | goto err_free_sym_hash; |
4ad4eba5 AM |
5409 | } |
5410 | break; | |
5411 | } | |
5412 | } | |
5413 | } | |
5414 | ||
5415 | free (sorted_sym_hash); | |
5416 | } | |
5417 | ||
33177bb1 AM |
5418 | if (bed->check_directives |
5419 | && !(*bed->check_directives) (abfd, info)) | |
5420 | return FALSE; | |
85fbca6a | 5421 | |
4ad4eba5 AM |
5422 | /* If this is a non-traditional link, try to optimize the handling |
5423 | of the .stab/.stabstr sections. */ | |
5424 | if (! dynamic | |
5425 | && ! info->traditional_format | |
66eb6687 | 5426 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5427 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5428 | { | |
5429 | asection *stabstr; | |
5430 | ||
5431 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5432 | if (stabstr != NULL) | |
5433 | { | |
5434 | bfd_size_type string_offset = 0; | |
5435 | asection *stab; | |
5436 | ||
5437 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5438 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5439 | && (!stab->name[5] || |
5440 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5441 | && (stab->flags & SEC_MERGE) == 0 | |
5442 | && !bfd_is_abs_section (stab->output_section)) | |
5443 | { | |
5444 | struct bfd_elf_section_data *secdata; | |
5445 | ||
5446 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5447 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5448 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5449 | &string_offset)) |
5450 | goto error_return; | |
5451 | if (secdata->sec_info) | |
dbaa2011 | 5452 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5453 | } |
5454 | } | |
5455 | } | |
5456 | ||
66eb6687 | 5457 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5458 | { |
5459 | /* Add this bfd to the loaded list. */ | |
5460 | struct elf_link_loaded_list *n; | |
5461 | ||
ca4be51c | 5462 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5463 | if (n == NULL) |
5464 | goto error_return; | |
5465 | n->abfd = abfd; | |
66eb6687 AM |
5466 | n->next = htab->loaded; |
5467 | htab->loaded = n; | |
4ad4eba5 AM |
5468 | } |
5469 | ||
5470 | return TRUE; | |
5471 | ||
5472 | error_free_vers: | |
66eb6687 AM |
5473 | if (old_tab != NULL) |
5474 | free (old_tab); | |
5b677558 AM |
5475 | if (old_strtab != NULL) |
5476 | free (old_strtab); | |
4ad4eba5 AM |
5477 | if (nondeflt_vers != NULL) |
5478 | free (nondeflt_vers); | |
5479 | if (extversym != NULL) | |
5480 | free (extversym); | |
5481 | error_free_sym: | |
5482 | if (isymbuf != NULL) | |
5483 | free (isymbuf); | |
5484 | error_return: | |
5485 | return FALSE; | |
5486 | } | |
5487 | ||
8387904d AM |
5488 | /* Return the linker hash table entry of a symbol that might be |
5489 | satisfied by an archive symbol. Return -1 on error. */ | |
5490 | ||
5491 | struct elf_link_hash_entry * | |
5492 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5493 | struct bfd_link_info *info, | |
5494 | const char *name) | |
5495 | { | |
5496 | struct elf_link_hash_entry *h; | |
5497 | char *p, *copy; | |
5498 | size_t len, first; | |
5499 | ||
2a41f396 | 5500 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5501 | if (h != NULL) |
5502 | return h; | |
5503 | ||
5504 | /* If this is a default version (the name contains @@), look up the | |
5505 | symbol again with only one `@' as well as without the version. | |
5506 | The effect is that references to the symbol with and without the | |
5507 | version will be matched by the default symbol in the archive. */ | |
5508 | ||
5509 | p = strchr (name, ELF_VER_CHR); | |
5510 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5511 | return h; | |
5512 | ||
5513 | /* First check with only one `@'. */ | |
5514 | len = strlen (name); | |
a50b1753 | 5515 | copy = (char *) bfd_alloc (abfd, len); |
8387904d | 5516 | if (copy == NULL) |
e99955cd | 5517 | return (struct elf_link_hash_entry *) -1; |
8387904d AM |
5518 | |
5519 | first = p - name + 1; | |
5520 | memcpy (copy, name, first); | |
5521 | memcpy (copy + first, name + first + 1, len - first); | |
5522 | ||
2a41f396 | 5523 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5524 | if (h == NULL) |
5525 | { | |
5526 | /* We also need to check references to the symbol without the | |
5527 | version. */ | |
5528 | copy[first - 1] = '\0'; | |
5529 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5530 | FALSE, FALSE, TRUE); |
8387904d AM |
5531 | } |
5532 | ||
5533 | bfd_release (abfd, copy); | |
5534 | return h; | |
5535 | } | |
5536 | ||
0ad989f9 | 5537 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5538 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5539 | handle versioned symbols. | |
0ad989f9 L |
5540 | |
5541 | Fortunately, ELF archive handling is simpler than that done by | |
5542 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5543 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5544 | symbol is currently undefined, we know that we must pull in that | |
5545 | object file. | |
5546 | ||
5547 | Unfortunately, we do have to make multiple passes over the symbol | |
5548 | table until nothing further is resolved. */ | |
5549 | ||
4ad4eba5 AM |
5550 | static bfd_boolean |
5551 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5552 | { |
5553 | symindex c; | |
13e570f8 | 5554 | unsigned char *included = NULL; |
0ad989f9 L |
5555 | carsym *symdefs; |
5556 | bfd_boolean loop; | |
5557 | bfd_size_type amt; | |
8387904d AM |
5558 | const struct elf_backend_data *bed; |
5559 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5560 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5561 | |
5562 | if (! bfd_has_map (abfd)) | |
5563 | { | |
5564 | /* An empty archive is a special case. */ | |
5565 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5566 | return TRUE; | |
5567 | bfd_set_error (bfd_error_no_armap); | |
5568 | return FALSE; | |
5569 | } | |
5570 | ||
5571 | /* Keep track of all symbols we know to be already defined, and all | |
5572 | files we know to be already included. This is to speed up the | |
5573 | second and subsequent passes. */ | |
5574 | c = bfd_ardata (abfd)->symdef_count; | |
5575 | if (c == 0) | |
5576 | return TRUE; | |
5577 | amt = c; | |
13e570f8 AM |
5578 | amt *= sizeof (*included); |
5579 | included = (unsigned char *) bfd_zmalloc (amt); | |
5580 | if (included == NULL) | |
5581 | return FALSE; | |
0ad989f9 L |
5582 | |
5583 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5584 | bed = get_elf_backend_data (abfd); |
5585 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5586 | |
5587 | do | |
5588 | { | |
5589 | file_ptr last; | |
5590 | symindex i; | |
5591 | carsym *symdef; | |
5592 | carsym *symdefend; | |
5593 | ||
5594 | loop = FALSE; | |
5595 | last = -1; | |
5596 | ||
5597 | symdef = symdefs; | |
5598 | symdefend = symdef + c; | |
5599 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5600 | { | |
5601 | struct elf_link_hash_entry *h; | |
5602 | bfd *element; | |
5603 | struct bfd_link_hash_entry *undefs_tail; | |
5604 | symindex mark; | |
5605 | ||
13e570f8 | 5606 | if (included[i]) |
0ad989f9 L |
5607 | continue; |
5608 | if (symdef->file_offset == last) | |
5609 | { | |
5610 | included[i] = TRUE; | |
5611 | continue; | |
5612 | } | |
5613 | ||
8387904d | 5614 | h = archive_symbol_lookup (abfd, info, symdef->name); |
e99955cd | 5615 | if (h == (struct elf_link_hash_entry *) -1) |
8387904d | 5616 | goto error_return; |
0ad989f9 L |
5617 | |
5618 | if (h == NULL) | |
5619 | continue; | |
5620 | ||
5621 | if (h->root.type == bfd_link_hash_common) | |
5622 | { | |
5623 | /* We currently have a common symbol. The archive map contains | |
5624 | a reference to this symbol, so we may want to include it. We | |
5625 | only want to include it however, if this archive element | |
5626 | contains a definition of the symbol, not just another common | |
5627 | declaration of it. | |
5628 | ||
5629 | Unfortunately some archivers (including GNU ar) will put | |
5630 | declarations of common symbols into their archive maps, as | |
5631 | well as real definitions, so we cannot just go by the archive | |
5632 | map alone. Instead we must read in the element's symbol | |
5633 | table and check that to see what kind of symbol definition | |
5634 | this is. */ | |
5635 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5636 | continue; | |
5637 | } | |
5638 | else if (h->root.type != bfd_link_hash_undefined) | |
5639 | { | |
5640 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5641 | /* Symbol must be defined. Don't check it again. */ |
5642 | included[i] = TRUE; | |
0ad989f9 L |
5643 | continue; |
5644 | } | |
5645 | ||
5646 | /* We need to include this archive member. */ | |
5647 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5648 | if (element == NULL) | |
5649 | goto error_return; | |
5650 | ||
5651 | if (! bfd_check_format (element, bfd_object)) | |
5652 | goto error_return; | |
5653 | ||
0ad989f9 L |
5654 | undefs_tail = info->hash->undefs_tail; |
5655 | ||
0e144ba7 AM |
5656 | if (!(*info->callbacks |
5657 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5658 | continue; |
0e144ba7 | 5659 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5660 | goto error_return; |
5661 | ||
5662 | /* If there are any new undefined symbols, we need to make | |
5663 | another pass through the archive in order to see whether | |
5664 | they can be defined. FIXME: This isn't perfect, because | |
5665 | common symbols wind up on undefs_tail and because an | |
5666 | undefined symbol which is defined later on in this pass | |
5667 | does not require another pass. This isn't a bug, but it | |
5668 | does make the code less efficient than it could be. */ | |
5669 | if (undefs_tail != info->hash->undefs_tail) | |
5670 | loop = TRUE; | |
5671 | ||
5672 | /* Look backward to mark all symbols from this object file | |
5673 | which we have already seen in this pass. */ | |
5674 | mark = i; | |
5675 | do | |
5676 | { | |
5677 | included[mark] = TRUE; | |
5678 | if (mark == 0) | |
5679 | break; | |
5680 | --mark; | |
5681 | } | |
5682 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5683 | ||
5684 | /* We mark subsequent symbols from this object file as we go | |
5685 | on through the loop. */ | |
5686 | last = symdef->file_offset; | |
5687 | } | |
5688 | } | |
5689 | while (loop); | |
5690 | ||
0ad989f9 L |
5691 | free (included); |
5692 | ||
5693 | return TRUE; | |
5694 | ||
5695 | error_return: | |
0ad989f9 L |
5696 | if (included != NULL) |
5697 | free (included); | |
5698 | return FALSE; | |
5699 | } | |
4ad4eba5 AM |
5700 | |
5701 | /* Given an ELF BFD, add symbols to the global hash table as | |
5702 | appropriate. */ | |
5703 | ||
5704 | bfd_boolean | |
5705 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5706 | { | |
5707 | switch (bfd_get_format (abfd)) | |
5708 | { | |
5709 | case bfd_object: | |
5710 | return elf_link_add_object_symbols (abfd, info); | |
5711 | case bfd_archive: | |
5712 | return elf_link_add_archive_symbols (abfd, info); | |
5713 | default: | |
5714 | bfd_set_error (bfd_error_wrong_format); | |
5715 | return FALSE; | |
5716 | } | |
5717 | } | |
5a580b3a | 5718 | \f |
14b1c01e AM |
5719 | struct hash_codes_info |
5720 | { | |
5721 | unsigned long *hashcodes; | |
5722 | bfd_boolean error; | |
5723 | }; | |
a0c8462f | 5724 | |
5a580b3a AM |
5725 | /* This function will be called though elf_link_hash_traverse to store |
5726 | all hash value of the exported symbols in an array. */ | |
5727 | ||
5728 | static bfd_boolean | |
5729 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5730 | { | |
a50b1753 | 5731 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5732 | const char *name; |
5a580b3a AM |
5733 | unsigned long ha; |
5734 | char *alc = NULL; | |
5735 | ||
5a580b3a AM |
5736 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5737 | if (h->dynindx == -1) | |
5738 | return TRUE; | |
5739 | ||
5740 | name = h->root.root.string; | |
422f1182 | 5741 | if (h->versioned >= versioned) |
5a580b3a | 5742 | { |
422f1182 L |
5743 | char *p = strchr (name, ELF_VER_CHR); |
5744 | if (p != NULL) | |
14b1c01e | 5745 | { |
422f1182 L |
5746 | alc = (char *) bfd_malloc (p - name + 1); |
5747 | if (alc == NULL) | |
5748 | { | |
5749 | inf->error = TRUE; | |
5750 | return FALSE; | |
5751 | } | |
5752 | memcpy (alc, name, p - name); | |
5753 | alc[p - name] = '\0'; | |
5754 | name = alc; | |
14b1c01e | 5755 | } |
5a580b3a AM |
5756 | } |
5757 | ||
5758 | /* Compute the hash value. */ | |
5759 | ha = bfd_elf_hash (name); | |
5760 | ||
5761 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5762 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5763 | |
5764 | /* And store it in the struct so that we can put it in the hash table | |
5765 | later. */ | |
f6e332e6 | 5766 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5767 | |
5768 | if (alc != NULL) | |
5769 | free (alc); | |
5770 | ||
5771 | return TRUE; | |
5772 | } | |
5773 | ||
fdc90cb4 JJ |
5774 | struct collect_gnu_hash_codes |
5775 | { | |
5776 | bfd *output_bfd; | |
5777 | const struct elf_backend_data *bed; | |
5778 | unsigned long int nsyms; | |
5779 | unsigned long int maskbits; | |
5780 | unsigned long int *hashcodes; | |
5781 | unsigned long int *hashval; | |
5782 | unsigned long int *indx; | |
5783 | unsigned long int *counts; | |
5784 | bfd_vma *bitmask; | |
5785 | bfd_byte *contents; | |
5786 | long int min_dynindx; | |
5787 | unsigned long int bucketcount; | |
5788 | unsigned long int symindx; | |
5789 | long int local_indx; | |
5790 | long int shift1, shift2; | |
5791 | unsigned long int mask; | |
14b1c01e | 5792 | bfd_boolean error; |
fdc90cb4 JJ |
5793 | }; |
5794 | ||
5795 | /* This function will be called though elf_link_hash_traverse to store | |
5796 | all hash value of the exported symbols in an array. */ | |
5797 | ||
5798 | static bfd_boolean | |
5799 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5800 | { | |
a50b1753 | 5801 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5802 | const char *name; |
fdc90cb4 JJ |
5803 | unsigned long ha; |
5804 | char *alc = NULL; | |
5805 | ||
fdc90cb4 JJ |
5806 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5807 | if (h->dynindx == -1) | |
5808 | return TRUE; | |
5809 | ||
5810 | /* Ignore also local symbols and undefined symbols. */ | |
5811 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5812 | return TRUE; | |
5813 | ||
5814 | name = h->root.root.string; | |
422f1182 | 5815 | if (h->versioned >= versioned) |
fdc90cb4 | 5816 | { |
422f1182 L |
5817 | char *p = strchr (name, ELF_VER_CHR); |
5818 | if (p != NULL) | |
14b1c01e | 5819 | { |
422f1182 L |
5820 | alc = (char *) bfd_malloc (p - name + 1); |
5821 | if (alc == NULL) | |
5822 | { | |
5823 | s->error = TRUE; | |
5824 | return FALSE; | |
5825 | } | |
5826 | memcpy (alc, name, p - name); | |
5827 | alc[p - name] = '\0'; | |
5828 | name = alc; | |
14b1c01e | 5829 | } |
fdc90cb4 JJ |
5830 | } |
5831 | ||
5832 | /* Compute the hash value. */ | |
5833 | ha = bfd_elf_gnu_hash (name); | |
5834 | ||
5835 | /* Store the found hash value in the array for compute_bucket_count, | |
5836 | and also for .dynsym reordering purposes. */ | |
5837 | s->hashcodes[s->nsyms] = ha; | |
5838 | s->hashval[h->dynindx] = ha; | |
5839 | ++s->nsyms; | |
5840 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5841 | s->min_dynindx = h->dynindx; | |
5842 | ||
5843 | if (alc != NULL) | |
5844 | free (alc); | |
5845 | ||
5846 | return TRUE; | |
5847 | } | |
5848 | ||
5849 | /* This function will be called though elf_link_hash_traverse to do | |
5850 | final dynaminc symbol renumbering. */ | |
5851 | ||
5852 | static bfd_boolean | |
5853 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5854 | { | |
a50b1753 | 5855 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5856 | unsigned long int bucket; |
5857 | unsigned long int val; | |
5858 | ||
fdc90cb4 JJ |
5859 | /* Ignore indirect symbols. */ |
5860 | if (h->dynindx == -1) | |
5861 | return TRUE; | |
5862 | ||
5863 | /* Ignore also local symbols and undefined symbols. */ | |
5864 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5865 | { | |
5866 | if (h->dynindx >= s->min_dynindx) | |
5867 | h->dynindx = s->local_indx++; | |
5868 | return TRUE; | |
5869 | } | |
5870 | ||
5871 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5872 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5873 | & ((s->maskbits >> s->shift1) - 1); | |
5874 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5875 | s->bitmask[val] | |
5876 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5877 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5878 | if (s->counts[bucket] == 1) | |
5879 | /* Last element terminates the chain. */ | |
5880 | val |= 1; | |
5881 | bfd_put_32 (s->output_bfd, val, | |
5882 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5883 | --s->counts[bucket]; | |
5884 | h->dynindx = s->indx[bucket]++; | |
5885 | return TRUE; | |
5886 | } | |
5887 | ||
5888 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5889 | ||
5890 | bfd_boolean | |
5891 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5892 | { | |
5893 | return !(h->forced_local | |
5894 | || h->root.type == bfd_link_hash_undefined | |
5895 | || h->root.type == bfd_link_hash_undefweak | |
5896 | || ((h->root.type == bfd_link_hash_defined | |
5897 | || h->root.type == bfd_link_hash_defweak) | |
5898 | && h->root.u.def.section->output_section == NULL)); | |
5899 | } | |
5900 | ||
5a580b3a AM |
5901 | /* Array used to determine the number of hash table buckets to use |
5902 | based on the number of symbols there are. If there are fewer than | |
5903 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5904 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5905 | than 32771 buckets. */ | |
5906 | ||
5907 | static const size_t elf_buckets[] = | |
5908 | { | |
5909 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5910 | 16411, 32771, 0 | |
5911 | }; | |
5912 | ||
5913 | /* Compute bucket count for hashing table. We do not use a static set | |
5914 | of possible tables sizes anymore. Instead we determine for all | |
5915 | possible reasonable sizes of the table the outcome (i.e., the | |
5916 | number of collisions etc) and choose the best solution. The | |
5917 | weighting functions are not too simple to allow the table to grow | |
5918 | without bounds. Instead one of the weighting factors is the size. | |
5919 | Therefore the result is always a good payoff between few collisions | |
5920 | (= short chain lengths) and table size. */ | |
5921 | static size_t | |
b20dd2ce | 5922 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5923 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5924 | unsigned long int nsyms, | |
5925 | int gnu_hash) | |
5a580b3a | 5926 | { |
5a580b3a | 5927 | size_t best_size = 0; |
5a580b3a | 5928 | unsigned long int i; |
5a580b3a | 5929 | |
5a580b3a AM |
5930 | /* We have a problem here. The following code to optimize the table |
5931 | size requires an integer type with more the 32 bits. If | |
5932 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5933 | #ifdef BFD_HOST_U_64_BIT | |
5934 | if (info->optimize) | |
5935 | { | |
5a580b3a AM |
5936 | size_t minsize; |
5937 | size_t maxsize; | |
5938 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5939 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5940 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5941 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5942 | unsigned long int *counts; |
d40f3da9 | 5943 | bfd_size_type amt; |
0883b6e0 | 5944 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5945 | |
5946 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5947 | that the hashing table must at least have NSYMS/4 and at most | |
5948 | 2*NSYMS buckets. */ | |
5949 | minsize = nsyms / 4; | |
5950 | if (minsize == 0) | |
5951 | minsize = 1; | |
5952 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5953 | if (gnu_hash) |
5954 | { | |
5955 | if (minsize < 2) | |
5956 | minsize = 2; | |
5957 | if ((best_size & 31) == 0) | |
5958 | ++best_size; | |
5959 | } | |
5a580b3a AM |
5960 | |
5961 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5962 | since the size could be large. */ | |
5963 | amt = maxsize; | |
5964 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5965 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5966 | if (counts == NULL) |
fdc90cb4 | 5967 | return 0; |
5a580b3a AM |
5968 | |
5969 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5970 | minimal chain length. The minor criteria is (of course) the size | |
5971 | of the table. */ | |
5972 | for (i = minsize; i < maxsize; ++i) | |
5973 | { | |
5974 | /* Walk through the array of hashcodes and count the collisions. */ | |
5975 | BFD_HOST_U_64_BIT max; | |
5976 | unsigned long int j; | |
5977 | unsigned long int fact; | |
5978 | ||
fdc90cb4 JJ |
5979 | if (gnu_hash && (i & 31) == 0) |
5980 | continue; | |
5981 | ||
5a580b3a AM |
5982 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5983 | ||
5984 | /* Determine how often each hash bucket is used. */ | |
5985 | for (j = 0; j < nsyms; ++j) | |
5986 | ++counts[hashcodes[j] % i]; | |
5987 | ||
5988 | /* For the weight function we need some information about the | |
5989 | pagesize on the target. This is information need not be 100% | |
5990 | accurate. Since this information is not available (so far) we | |
5991 | define it here to a reasonable default value. If it is crucial | |
5992 | to have a better value some day simply define this value. */ | |
5993 | # ifndef BFD_TARGET_PAGESIZE | |
5994 | # define BFD_TARGET_PAGESIZE (4096) | |
5995 | # endif | |
5996 | ||
fdc90cb4 JJ |
5997 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5998 | and the chains. */ | |
5999 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
6000 | |
6001 | # if 1 | |
6002 | /* Variant 1: optimize for short chains. We add the squares | |
6003 | of all the chain lengths (which favors many small chain | |
6004 | over a few long chains). */ | |
6005 | for (j = 0; j < i; ++j) | |
6006 | max += counts[j] * counts[j]; | |
6007 | ||
6008 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 6009 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6010 | max *= fact * fact; |
6011 | # else | |
6012 | /* Variant 2: Optimize a lot more for small table. Here we | |
6013 | also add squares of the size but we also add penalties for | |
6014 | empty slots (the +1 term). */ | |
6015 | for (j = 0; j < i; ++j) | |
6016 | max += (1 + counts[j]) * (1 + counts[j]); | |
6017 | ||
6018 | /* The overall size of the table is considered, but not as | |
6019 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 6020 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6021 | max *= fact; |
6022 | # endif | |
6023 | ||
6024 | /* Compare with current best results. */ | |
6025 | if (max < best_chlen) | |
6026 | { | |
6027 | best_chlen = max; | |
6028 | best_size = i; | |
ca4be51c | 6029 | no_improvement_count = 0; |
5a580b3a | 6030 | } |
0883b6e0 NC |
6031 | /* PR 11843: Avoid futile long searches for the best bucket size |
6032 | when there are a large number of symbols. */ | |
6033 | else if (++no_improvement_count == 100) | |
6034 | break; | |
5a580b3a AM |
6035 | } |
6036 | ||
6037 | free (counts); | |
6038 | } | |
6039 | else | |
6040 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
6041 | { | |
6042 | /* This is the fallback solution if no 64bit type is available or if we | |
6043 | are not supposed to spend much time on optimizations. We select the | |
6044 | bucket count using a fixed set of numbers. */ | |
6045 | for (i = 0; elf_buckets[i] != 0; i++) | |
6046 | { | |
6047 | best_size = elf_buckets[i]; | |
fdc90cb4 | 6048 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
6049 | break; |
6050 | } | |
fdc90cb4 JJ |
6051 | if (gnu_hash && best_size < 2) |
6052 | best_size = 2; | |
5a580b3a AM |
6053 | } |
6054 | ||
5a580b3a AM |
6055 | return best_size; |
6056 | } | |
6057 | ||
d0bf826b AM |
6058 | /* Size any SHT_GROUP section for ld -r. */ |
6059 | ||
6060 | bfd_boolean | |
6061 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
6062 | { | |
6063 | bfd *ibfd; | |
57963c05 | 6064 | asection *s; |
d0bf826b | 6065 | |
c72f2fb2 | 6066 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b | 6067 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
57963c05 AM |
6068 | && (s = ibfd->sections) != NULL |
6069 | && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
d0bf826b AM |
6070 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) |
6071 | return FALSE; | |
6072 | return TRUE; | |
6073 | } | |
6074 | ||
04c3a755 NS |
6075 | /* Set a default stack segment size. The value in INFO wins. If it |
6076 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
6077 | undefined it is initialized. */ | |
6078 | ||
6079 | bfd_boolean | |
6080 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
6081 | struct bfd_link_info *info, | |
6082 | const char *legacy_symbol, | |
6083 | bfd_vma default_size) | |
6084 | { | |
6085 | struct elf_link_hash_entry *h = NULL; | |
6086 | ||
6087 | /* Look for legacy symbol. */ | |
6088 | if (legacy_symbol) | |
6089 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
6090 | FALSE, FALSE, FALSE); | |
6091 | if (h && (h->root.type == bfd_link_hash_defined | |
6092 | || h->root.type == bfd_link_hash_defweak) | |
6093 | && h->def_regular | |
6094 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
6095 | { | |
6096 | /* The symbol has no type if specified on the command line. */ | |
6097 | h->type = STT_OBJECT; | |
6098 | if (info->stacksize) | |
695344c0 | 6099 | /* xgettext:c-format */ |
871b3ab2 | 6100 | _bfd_error_handler (_("%pB: stack size specified and %s set"), |
4eca0228 | 6101 | output_bfd, legacy_symbol); |
04c3a755 | 6102 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 6103 | /* xgettext:c-format */ |
871b3ab2 | 6104 | _bfd_error_handler (_("%pB: %s not absolute"), |
4eca0228 | 6105 | output_bfd, legacy_symbol); |
04c3a755 NS |
6106 | else |
6107 | info->stacksize = h->root.u.def.value; | |
6108 | } | |
6109 | ||
6110 | if (!info->stacksize) | |
6111 | /* If the user didn't set a size, or explicitly inhibit the | |
6112 | size, set it now. */ | |
6113 | info->stacksize = default_size; | |
6114 | ||
6115 | /* Provide the legacy symbol, if it is referenced. */ | |
6116 | if (h && (h->root.type == bfd_link_hash_undefined | |
6117 | || h->root.type == bfd_link_hash_undefweak)) | |
6118 | { | |
6119 | struct bfd_link_hash_entry *bh = NULL; | |
6120 | ||
6121 | if (!(_bfd_generic_link_add_one_symbol | |
6122 | (info, output_bfd, legacy_symbol, | |
6123 | BSF_GLOBAL, bfd_abs_section_ptr, | |
6124 | info->stacksize >= 0 ? info->stacksize : 0, | |
6125 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
6126 | return FALSE; | |
6127 | ||
6128 | h = (struct elf_link_hash_entry *) bh; | |
6129 | h->def_regular = 1; | |
6130 | h->type = STT_OBJECT; | |
6131 | } | |
6132 | ||
6133 | return TRUE; | |
6134 | } | |
6135 | ||
b531344c MR |
6136 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
6137 | ||
6138 | struct elf_gc_sweep_symbol_info | |
6139 | { | |
6140 | struct bfd_link_info *info; | |
6141 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
6142 | bfd_boolean); | |
6143 | }; | |
6144 | ||
6145 | static bfd_boolean | |
6146 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) | |
6147 | { | |
6148 | if (!h->mark | |
6149 | && (((h->root.type == bfd_link_hash_defined | |
6150 | || h->root.type == bfd_link_hash_defweak) | |
6151 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) | |
6152 | && h->root.u.def.section->gc_mark)) | |
6153 | || h->root.type == bfd_link_hash_undefined | |
6154 | || h->root.type == bfd_link_hash_undefweak)) | |
6155 | { | |
6156 | struct elf_gc_sweep_symbol_info *inf; | |
6157 | ||
6158 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
6159 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
6160 | h->def_regular = 0; | |
6161 | h->ref_regular = 0; | |
6162 | h->ref_regular_nonweak = 0; | |
6163 | } | |
6164 | ||
6165 | return TRUE; | |
6166 | } | |
6167 | ||
5a580b3a AM |
6168 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
6169 | called by the ELF linker emulation before_allocation routine. We | |
6170 | must set the sizes of the sections before the linker sets the | |
6171 | addresses of the various sections. */ | |
6172 | ||
6173 | bfd_boolean | |
6174 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
6175 | const char *soname, | |
6176 | const char *rpath, | |
6177 | const char *filter_shlib, | |
7ee314fa AM |
6178 | const char *audit, |
6179 | const char *depaudit, | |
5a580b3a AM |
6180 | const char * const *auxiliary_filters, |
6181 | struct bfd_link_info *info, | |
fd91d419 | 6182 | asection **sinterpptr) |
5a580b3a | 6183 | { |
5a580b3a AM |
6184 | bfd *dynobj; |
6185 | const struct elf_backend_data *bed; | |
5a580b3a AM |
6186 | |
6187 | *sinterpptr = NULL; | |
6188 | ||
5a580b3a AM |
6189 | if (!is_elf_hash_table (info->hash)) |
6190 | return TRUE; | |
6191 | ||
5a580b3a AM |
6192 | dynobj = elf_hash_table (info)->dynobj; |
6193 | ||
9a2a56cc | 6194 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6195 | { |
902e9fc7 MR |
6196 | struct bfd_elf_version_tree *verdefs; |
6197 | struct elf_info_failed asvinfo; | |
5a580b3a AM |
6198 | struct bfd_elf_version_tree *t; |
6199 | struct bfd_elf_version_expr *d; | |
902e9fc7 | 6200 | asection *s; |
e6699019 | 6201 | size_t soname_indx; |
7ee314fa | 6202 | |
5a580b3a AM |
6203 | /* If we are supposed to export all symbols into the dynamic symbol |
6204 | table (this is not the normal case), then do so. */ | |
55255dae | 6205 | if (info->export_dynamic |
0e1862bb | 6206 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a | 6207 | { |
3d13f3e9 AM |
6208 | struct elf_info_failed eif; |
6209 | ||
6210 | eif.info = info; | |
6211 | eif.failed = FALSE; | |
5a580b3a AM |
6212 | elf_link_hash_traverse (elf_hash_table (info), |
6213 | _bfd_elf_export_symbol, | |
6214 | &eif); | |
6215 | if (eif.failed) | |
6216 | return FALSE; | |
6217 | } | |
6218 | ||
e6699019 L |
6219 | if (soname != NULL) |
6220 | { | |
6221 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6222 | soname, TRUE); | |
6223 | if (soname_indx == (size_t) -1 | |
6224 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
6225 | return FALSE; | |
6226 | } | |
6227 | else | |
6228 | soname_indx = (size_t) -1; | |
6229 | ||
5a580b3a | 6230 | /* Make all global versions with definition. */ |
fd91d419 | 6231 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6232 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6233 | if (!d->symver && d->literal) |
5a580b3a AM |
6234 | { |
6235 | const char *verstr, *name; | |
6236 | size_t namelen, verlen, newlen; | |
93252b1c | 6237 | char *newname, *p, leading_char; |
5a580b3a AM |
6238 | struct elf_link_hash_entry *newh; |
6239 | ||
93252b1c | 6240 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6241 | name = d->pattern; |
93252b1c | 6242 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6243 | verstr = t->name; |
6244 | verlen = strlen (verstr); | |
6245 | newlen = namelen + verlen + 3; | |
6246 | ||
a50b1753 | 6247 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6248 | if (newname == NULL) |
6249 | return FALSE; | |
93252b1c MF |
6250 | newname[0] = leading_char; |
6251 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6252 | |
6253 | /* Check the hidden versioned definition. */ | |
6254 | p = newname + namelen; | |
6255 | *p++ = ELF_VER_CHR; | |
6256 | memcpy (p, verstr, verlen + 1); | |
6257 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6258 | newname, FALSE, FALSE, | |
6259 | FALSE); | |
6260 | if (newh == NULL | |
6261 | || (newh->root.type != bfd_link_hash_defined | |
6262 | && newh->root.type != bfd_link_hash_defweak)) | |
6263 | { | |
6264 | /* Check the default versioned definition. */ | |
6265 | *p++ = ELF_VER_CHR; | |
6266 | memcpy (p, verstr, verlen + 1); | |
6267 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6268 | newname, FALSE, FALSE, | |
6269 | FALSE); | |
6270 | } | |
6271 | free (newname); | |
6272 | ||
6273 | /* Mark this version if there is a definition and it is | |
6274 | not defined in a shared object. */ | |
6275 | if (newh != NULL | |
f5385ebf | 6276 | && !newh->def_dynamic |
5a580b3a AM |
6277 | && (newh->root.type == bfd_link_hash_defined |
6278 | || newh->root.type == bfd_link_hash_defweak)) | |
6279 | d->symver = 1; | |
6280 | } | |
6281 | ||
6282 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6283 | asvinfo.info = info; |
5a580b3a AM |
6284 | asvinfo.failed = FALSE; |
6285 | ||
6286 | elf_link_hash_traverse (elf_hash_table (info), | |
6287 | _bfd_elf_link_assign_sym_version, | |
6288 | &asvinfo); | |
6289 | if (asvinfo.failed) | |
6290 | return FALSE; | |
6291 | ||
6292 | if (!info->allow_undefined_version) | |
6293 | { | |
6294 | /* Check if all global versions have a definition. */ | |
3d13f3e9 | 6295 | bfd_boolean all_defined = TRUE; |
fd91d419 | 6296 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6297 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6298 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6299 | { |
4eca0228 | 6300 | _bfd_error_handler |
5a580b3a AM |
6301 | (_("%s: undefined version: %s"), |
6302 | d->pattern, t->name); | |
6303 | all_defined = FALSE; | |
6304 | } | |
6305 | ||
6306 | if (!all_defined) | |
6307 | { | |
6308 | bfd_set_error (bfd_error_bad_value); | |
6309 | return FALSE; | |
6310 | } | |
6311 | } | |
6312 | ||
902e9fc7 MR |
6313 | /* Set up the version definition section. */ |
6314 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); | |
6315 | BFD_ASSERT (s != NULL); | |
5a580b3a | 6316 | |
902e9fc7 MR |
6317 | /* We may have created additional version definitions if we are |
6318 | just linking a regular application. */ | |
6319 | verdefs = info->version_info; | |
5a580b3a | 6320 | |
902e9fc7 MR |
6321 | /* Skip anonymous version tag. */ |
6322 | if (verdefs != NULL && verdefs->vernum == 0) | |
6323 | verdefs = verdefs->next; | |
5a580b3a | 6324 | |
902e9fc7 MR |
6325 | if (verdefs == NULL && !info->create_default_symver) |
6326 | s->flags |= SEC_EXCLUDE; | |
6327 | else | |
5a580b3a | 6328 | { |
902e9fc7 MR |
6329 | unsigned int cdefs; |
6330 | bfd_size_type size; | |
6331 | bfd_byte *p; | |
6332 | Elf_Internal_Verdef def; | |
6333 | Elf_Internal_Verdaux defaux; | |
6334 | struct bfd_link_hash_entry *bh; | |
6335 | struct elf_link_hash_entry *h; | |
6336 | const char *name; | |
5a580b3a | 6337 | |
902e9fc7 MR |
6338 | cdefs = 0; |
6339 | size = 0; | |
5a580b3a | 6340 | |
902e9fc7 MR |
6341 | /* Make space for the base version. */ |
6342 | size += sizeof (Elf_External_Verdef); | |
6343 | size += sizeof (Elf_External_Verdaux); | |
6344 | ++cdefs; | |
6345 | ||
6346 | /* Make space for the default version. */ | |
6347 | if (info->create_default_symver) | |
6348 | { | |
6349 | size += sizeof (Elf_External_Verdef); | |
6350 | ++cdefs; | |
3e3b46e5 PB |
6351 | } |
6352 | ||
5a580b3a AM |
6353 | for (t = verdefs; t != NULL; t = t->next) |
6354 | { | |
6355 | struct bfd_elf_version_deps *n; | |
6356 | ||
a6cc6b3b RO |
6357 | /* Don't emit base version twice. */ |
6358 | if (t->vernum == 0) | |
6359 | continue; | |
6360 | ||
5a580b3a AM |
6361 | size += sizeof (Elf_External_Verdef); |
6362 | size += sizeof (Elf_External_Verdaux); | |
6363 | ++cdefs; | |
6364 | ||
6365 | for (n = t->deps; n != NULL; n = n->next) | |
6366 | size += sizeof (Elf_External_Verdaux); | |
6367 | } | |
6368 | ||
eea6121a | 6369 | s->size = size; |
a50b1753 | 6370 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6371 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6372 | return FALSE; |
6373 | ||
6374 | /* Fill in the version definition section. */ | |
6375 | ||
6376 | p = s->contents; | |
6377 | ||
6378 | def.vd_version = VER_DEF_CURRENT; | |
6379 | def.vd_flags = VER_FLG_BASE; | |
6380 | def.vd_ndx = 1; | |
6381 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6382 | if (info->create_default_symver) |
6383 | { | |
6384 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6385 | def.vd_next = sizeof (Elf_External_Verdef); | |
6386 | } | |
6387 | else | |
6388 | { | |
6389 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6390 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6391 | + sizeof (Elf_External_Verdaux)); | |
6392 | } | |
5a580b3a | 6393 | |
ef53be89 | 6394 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6395 | { |
6396 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6397 | soname_indx); | |
6398 | def.vd_hash = bfd_elf_hash (soname); | |
6399 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6400 | name = soname; |
5a580b3a AM |
6401 | } |
6402 | else | |
6403 | { | |
ef53be89 | 6404 | size_t indx; |
5a580b3a | 6405 | |
06084812 | 6406 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6407 | def.vd_hash = bfd_elf_hash (name); |
6408 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6409 | name, FALSE); | |
ef53be89 | 6410 | if (indx == (size_t) -1) |
5a580b3a AM |
6411 | return FALSE; |
6412 | defaux.vda_name = indx; | |
6413 | } | |
6414 | defaux.vda_next = 0; | |
6415 | ||
6416 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6417 | (Elf_External_Verdef *) p); | |
6418 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6419 | if (info->create_default_symver) |
6420 | { | |
6421 | /* Add a symbol representing this version. */ | |
6422 | bh = NULL; | |
6423 | if (! (_bfd_generic_link_add_one_symbol | |
6424 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6425 | 0, NULL, FALSE, | |
6426 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6427 | return FALSE; | |
6428 | h = (struct elf_link_hash_entry *) bh; | |
6429 | h->non_elf = 0; | |
6430 | h->def_regular = 1; | |
6431 | h->type = STT_OBJECT; | |
6432 | h->verinfo.vertree = NULL; | |
6433 | ||
6434 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6435 | return FALSE; | |
6436 | ||
6437 | /* Create a duplicate of the base version with the same | |
6438 | aux block, but different flags. */ | |
6439 | def.vd_flags = 0; | |
6440 | def.vd_ndx = 2; | |
6441 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6442 | if (verdefs) | |
6443 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6444 | + sizeof (Elf_External_Verdaux)); | |
6445 | else | |
6446 | def.vd_next = 0; | |
6447 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6448 | (Elf_External_Verdef *) p); | |
6449 | p += sizeof (Elf_External_Verdef); | |
6450 | } | |
5a580b3a AM |
6451 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6452 | (Elf_External_Verdaux *) p); | |
6453 | p += sizeof (Elf_External_Verdaux); | |
6454 | ||
6455 | for (t = verdefs; t != NULL; t = t->next) | |
6456 | { | |
6457 | unsigned int cdeps; | |
6458 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6459 | |
a6cc6b3b RO |
6460 | /* Don't emit the base version twice. */ |
6461 | if (t->vernum == 0) | |
6462 | continue; | |
6463 | ||
5a580b3a AM |
6464 | cdeps = 0; |
6465 | for (n = t->deps; n != NULL; n = n->next) | |
6466 | ++cdeps; | |
6467 | ||
6468 | /* Add a symbol representing this version. */ | |
6469 | bh = NULL; | |
6470 | if (! (_bfd_generic_link_add_one_symbol | |
6471 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6472 | 0, NULL, FALSE, | |
6473 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6474 | return FALSE; | |
6475 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6476 | h->non_elf = 0; |
6477 | h->def_regular = 1; | |
5a580b3a AM |
6478 | h->type = STT_OBJECT; |
6479 | h->verinfo.vertree = t; | |
6480 | ||
c152c796 | 6481 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6482 | return FALSE; |
6483 | ||
6484 | def.vd_version = VER_DEF_CURRENT; | |
6485 | def.vd_flags = 0; | |
6486 | if (t->globals.list == NULL | |
6487 | && t->locals.list == NULL | |
6488 | && ! t->used) | |
6489 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6490 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6491 | def.vd_cnt = cdeps + 1; |
6492 | def.vd_hash = bfd_elf_hash (t->name); | |
6493 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6494 | def.vd_next = 0; | |
a6cc6b3b RO |
6495 | |
6496 | /* If a basever node is next, it *must* be the last node in | |
6497 | the chain, otherwise Verdef construction breaks. */ | |
6498 | if (t->next != NULL && t->next->vernum == 0) | |
6499 | BFD_ASSERT (t->next->next == NULL); | |
6500 | ||
6501 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6502 | def.vd_next = (sizeof (Elf_External_Verdef) |
6503 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6504 | ||
6505 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6506 | (Elf_External_Verdef *) p); | |
6507 | p += sizeof (Elf_External_Verdef); | |
6508 | ||
6509 | defaux.vda_name = h->dynstr_index; | |
6510 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6511 | h->dynstr_index); | |
6512 | defaux.vda_next = 0; | |
6513 | if (t->deps != NULL) | |
6514 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6515 | t->name_indx = defaux.vda_name; | |
6516 | ||
6517 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6518 | (Elf_External_Verdaux *) p); | |
6519 | p += sizeof (Elf_External_Verdaux); | |
6520 | ||
6521 | for (n = t->deps; n != NULL; n = n->next) | |
6522 | { | |
6523 | if (n->version_needed == NULL) | |
6524 | { | |
6525 | /* This can happen if there was an error in the | |
6526 | version script. */ | |
6527 | defaux.vda_name = 0; | |
6528 | } | |
6529 | else | |
6530 | { | |
6531 | defaux.vda_name = n->version_needed->name_indx; | |
6532 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6533 | defaux.vda_name); | |
6534 | } | |
6535 | if (n->next == NULL) | |
6536 | defaux.vda_next = 0; | |
6537 | else | |
6538 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6539 | ||
6540 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6541 | (Elf_External_Verdaux *) p); | |
6542 | p += sizeof (Elf_External_Verdaux); | |
6543 | } | |
6544 | } | |
6545 | ||
5a580b3a AM |
6546 | elf_tdata (output_bfd)->cverdefs = cdefs; |
6547 | } | |
902e9fc7 MR |
6548 | } |
6549 | ||
6550 | bed = get_elf_backend_data (output_bfd); | |
6551 | ||
6552 | if (info->gc_sections && bed->can_gc_sections) | |
6553 | { | |
6554 | struct elf_gc_sweep_symbol_info sweep_info; | |
902e9fc7 MR |
6555 | |
6556 | /* Remove the symbols that were in the swept sections from the | |
3d13f3e9 | 6557 | dynamic symbol table. */ |
902e9fc7 MR |
6558 | sweep_info.info = info; |
6559 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
6560 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
6561 | &sweep_info); | |
3d13f3e9 AM |
6562 | } |
6563 | ||
6564 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6565 | { | |
6566 | asection *s; | |
6567 | struct elf_find_verdep_info sinfo; | |
6568 | ||
6569 | /* Work out the size of the version reference section. */ | |
6570 | ||
6571 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); | |
6572 | BFD_ASSERT (s != NULL); | |
902e9fc7 | 6573 | |
3d13f3e9 AM |
6574 | sinfo.info = info; |
6575 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6576 | if (sinfo.vers == 0) | |
6577 | sinfo.vers = 1; | |
6578 | sinfo.failed = FALSE; | |
6579 | ||
6580 | elf_link_hash_traverse (elf_hash_table (info), | |
6581 | _bfd_elf_link_find_version_dependencies, | |
6582 | &sinfo); | |
6583 | if (sinfo.failed) | |
6584 | return FALSE; | |
6585 | ||
6586 | if (elf_tdata (output_bfd)->verref == NULL) | |
6587 | s->flags |= SEC_EXCLUDE; | |
6588 | else | |
6589 | { | |
6590 | Elf_Internal_Verneed *vn; | |
6591 | unsigned int size; | |
6592 | unsigned int crefs; | |
6593 | bfd_byte *p; | |
6594 | ||
6595 | /* Build the version dependency section. */ | |
6596 | size = 0; | |
6597 | crefs = 0; | |
6598 | for (vn = elf_tdata (output_bfd)->verref; | |
6599 | vn != NULL; | |
6600 | vn = vn->vn_nextref) | |
6601 | { | |
6602 | Elf_Internal_Vernaux *a; | |
6603 | ||
6604 | size += sizeof (Elf_External_Verneed); | |
6605 | ++crefs; | |
6606 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6607 | size += sizeof (Elf_External_Vernaux); | |
6608 | } | |
6609 | ||
6610 | s->size = size; | |
6611 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); | |
6612 | if (s->contents == NULL) | |
6613 | return FALSE; | |
6614 | ||
6615 | p = s->contents; | |
6616 | for (vn = elf_tdata (output_bfd)->verref; | |
6617 | vn != NULL; | |
6618 | vn = vn->vn_nextref) | |
6619 | { | |
6620 | unsigned int caux; | |
6621 | Elf_Internal_Vernaux *a; | |
6622 | size_t indx; | |
6623 | ||
6624 | caux = 0; | |
6625 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6626 | ++caux; | |
6627 | ||
6628 | vn->vn_version = VER_NEED_CURRENT; | |
6629 | vn->vn_cnt = caux; | |
6630 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6631 | elf_dt_name (vn->vn_bfd) != NULL | |
6632 | ? elf_dt_name (vn->vn_bfd) | |
6633 | : lbasename (vn->vn_bfd->filename), | |
6634 | FALSE); | |
6635 | if (indx == (size_t) -1) | |
6636 | return FALSE; | |
6637 | vn->vn_file = indx; | |
6638 | vn->vn_aux = sizeof (Elf_External_Verneed); | |
6639 | if (vn->vn_nextref == NULL) | |
6640 | vn->vn_next = 0; | |
6641 | else | |
6642 | vn->vn_next = (sizeof (Elf_External_Verneed) | |
6643 | + caux * sizeof (Elf_External_Vernaux)); | |
6644 | ||
6645 | _bfd_elf_swap_verneed_out (output_bfd, vn, | |
6646 | (Elf_External_Verneed *) p); | |
6647 | p += sizeof (Elf_External_Verneed); | |
6648 | ||
6649 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6650 | { | |
6651 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6652 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6653 | a->vna_nodename, FALSE); | |
6654 | if (indx == (size_t) -1) | |
6655 | return FALSE; | |
6656 | a->vna_name = indx; | |
6657 | if (a->vna_nextptr == NULL) | |
6658 | a->vna_next = 0; | |
6659 | else | |
6660 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6661 | ||
6662 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6663 | (Elf_External_Vernaux *) p); | |
6664 | p += sizeof (Elf_External_Vernaux); | |
6665 | } | |
6666 | } | |
6667 | ||
6668 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6669 | } | |
902e9fc7 MR |
6670 | } |
6671 | ||
6672 | /* Any syms created from now on start with -1 in | |
6673 | got.refcount/offset and plt.refcount/offset. */ | |
6674 | elf_hash_table (info)->init_got_refcount | |
6675 | = elf_hash_table (info)->init_got_offset; | |
6676 | elf_hash_table (info)->init_plt_refcount | |
6677 | = elf_hash_table (info)->init_plt_offset; | |
6678 | ||
6679 | if (bfd_link_relocatable (info) | |
6680 | && !_bfd_elf_size_group_sections (info)) | |
6681 | return FALSE; | |
6682 | ||
6683 | /* The backend may have to create some sections regardless of whether | |
6684 | we're dynamic or not. */ | |
6685 | if (bed->elf_backend_always_size_sections | |
6686 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
6687 | return FALSE; | |
6688 | ||
6689 | /* Determine any GNU_STACK segment requirements, after the backend | |
6690 | has had a chance to set a default segment size. */ | |
6691 | if (info->execstack) | |
6692 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; | |
6693 | else if (info->noexecstack) | |
6694 | elf_stack_flags (output_bfd) = PF_R | PF_W; | |
6695 | else | |
6696 | { | |
6697 | bfd *inputobj; | |
6698 | asection *notesec = NULL; | |
6699 | int exec = 0; | |
6700 | ||
6701 | for (inputobj = info->input_bfds; | |
6702 | inputobj; | |
6703 | inputobj = inputobj->link.next) | |
6704 | { | |
6705 | asection *s; | |
6706 | ||
6707 | if (inputobj->flags | |
6708 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
6709 | continue; | |
57963c05 AM |
6710 | s = inputobj->sections; |
6711 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
6712 | continue; | |
6713 | ||
902e9fc7 MR |
6714 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); |
6715 | if (s) | |
6716 | { | |
6717 | if (s->flags & SEC_CODE) | |
6718 | exec = PF_X; | |
6719 | notesec = s; | |
6720 | } | |
6721 | else if (bed->default_execstack) | |
6722 | exec = PF_X; | |
6723 | } | |
6724 | if (notesec || info->stacksize > 0) | |
6725 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; | |
6726 | if (notesec && exec && bfd_link_relocatable (info) | |
6727 | && notesec->output_section != bfd_abs_section_ptr) | |
6728 | notesec->output_section->flags |= SEC_CODE; | |
6729 | } | |
6730 | ||
6731 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6732 | { | |
6733 | struct elf_info_failed eif; | |
6734 | struct elf_link_hash_entry *h; | |
6735 | asection *dynstr; | |
6736 | asection *s; | |
6737 | ||
6738 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); | |
6739 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); | |
6740 | ||
902e9fc7 MR |
6741 | if (info->symbolic) |
6742 | { | |
6743 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
6744 | return FALSE; | |
6745 | info->flags |= DF_SYMBOLIC; | |
6746 | } | |
6747 | ||
6748 | if (rpath != NULL) | |
6749 | { | |
6750 | size_t indx; | |
6751 | bfd_vma tag; | |
6752 | ||
6753 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
6754 | TRUE); | |
6755 | if (indx == (size_t) -1) | |
6756 | return FALSE; | |
6757 | ||
6758 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; | |
6759 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
6760 | return FALSE; | |
6761 | } | |
6762 | ||
6763 | if (filter_shlib != NULL) | |
6764 | { | |
6765 | size_t indx; | |
6766 | ||
6767 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6768 | filter_shlib, TRUE); | |
6769 | if (indx == (size_t) -1 | |
6770 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
6771 | return FALSE; | |
6772 | } | |
6773 | ||
6774 | if (auxiliary_filters != NULL) | |
6775 | { | |
6776 | const char * const *p; | |
6777 | ||
6778 | for (p = auxiliary_filters; *p != NULL; p++) | |
6779 | { | |
6780 | size_t indx; | |
6781 | ||
6782 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6783 | *p, TRUE); | |
6784 | if (indx == (size_t) -1 | |
6785 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
6786 | return FALSE; | |
6787 | } | |
6788 | } | |
6789 | ||
6790 | if (audit != NULL) | |
6791 | { | |
6792 | size_t indx; | |
6793 | ||
6794 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
6795 | TRUE); | |
6796 | if (indx == (size_t) -1 | |
6797 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
6798 | return FALSE; | |
6799 | } | |
6800 | ||
6801 | if (depaudit != NULL) | |
6802 | { | |
6803 | size_t indx; | |
6804 | ||
6805 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
6806 | TRUE); | |
6807 | if (indx == (size_t) -1 | |
6808 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
6809 | return FALSE; | |
6810 | } | |
6811 | ||
6812 | eif.info = info; | |
6813 | eif.failed = FALSE; | |
6814 | ||
6815 | /* Find all symbols which were defined in a dynamic object and make | |
6816 | the backend pick a reasonable value for them. */ | |
6817 | elf_link_hash_traverse (elf_hash_table (info), | |
6818 | _bfd_elf_adjust_dynamic_symbol, | |
6819 | &eif); | |
6820 | if (eif.failed) | |
6821 | return FALSE; | |
6822 | ||
6823 | /* Add some entries to the .dynamic section. We fill in some of the | |
6824 | values later, in bfd_elf_final_link, but we must add the entries | |
6825 | now so that we know the final size of the .dynamic section. */ | |
6826 | ||
6827 | /* If there are initialization and/or finalization functions to | |
6828 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6829 | h = (info->init_function | |
6830 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6831 | info->init_function, FALSE, | |
6832 | FALSE, FALSE) | |
6833 | : NULL); | |
6834 | if (h != NULL | |
6835 | && (h->ref_regular | |
6836 | || h->def_regular)) | |
6837 | { | |
6838 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6839 | return FALSE; | |
6840 | } | |
6841 | h = (info->fini_function | |
6842 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6843 | info->fini_function, FALSE, | |
6844 | FALSE, FALSE) | |
6845 | : NULL); | |
6846 | if (h != NULL | |
6847 | && (h->ref_regular | |
6848 | || h->def_regular)) | |
6849 | { | |
6850 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6851 | return FALSE; | |
6852 | } | |
6853 | ||
6854 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); | |
6855 | if (s != NULL && s->linker_has_input) | |
6856 | { | |
6857 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
6858 | if (! bfd_link_executable (info)) | |
6859 | { | |
6860 | bfd *sub; | |
6861 | asection *o; | |
6862 | ||
57963c05 AM |
6863 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
6864 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
6865 | && (o = sub->sections) != NULL | |
6866 | && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) | |
902e9fc7 MR |
6867 | for (o = sub->sections; o != NULL; o = o->next) |
6868 | if (elf_section_data (o)->this_hdr.sh_type | |
6869 | == SHT_PREINIT_ARRAY) | |
6870 | { | |
6871 | _bfd_error_handler | |
871b3ab2 | 6872 | (_("%pB: .preinit_array section is not allowed in DSO"), |
902e9fc7 MR |
6873 | sub); |
6874 | break; | |
6875 | } | |
6876 | ||
6877 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6878 | return FALSE; | |
6879 | } | |
6880 | ||
6881 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6882 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6883 | return FALSE; | |
6884 | } | |
6885 | s = bfd_get_section_by_name (output_bfd, ".init_array"); | |
6886 | if (s != NULL && s->linker_has_input) | |
6887 | { | |
6888 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6889 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6890 | return FALSE; | |
6891 | } | |
6892 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); | |
6893 | if (s != NULL && s->linker_has_input) | |
6894 | { | |
6895 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6896 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6897 | return FALSE; | |
6898 | } | |
6899 | ||
6900 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); | |
6901 | /* If .dynstr is excluded from the link, we don't want any of | |
6902 | these tags. Strictly, we should be checking each section | |
6903 | individually; This quick check covers for the case where | |
6904 | someone does a /DISCARD/ : { *(*) }. */ | |
6905 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6906 | { | |
6907 | bfd_size_type strsize; | |
6908 | ||
6909 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
6910 | if ((info->emit_hash | |
6911 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6912 | || (info->emit_gnu_hash | |
6913 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
6914 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
6915 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6916 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6917 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6918 | bed->s->sizeof_sym)) | |
6919 | return FALSE; | |
6920 | } | |
6921 | } | |
6922 | ||
6923 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
6924 | return FALSE; | |
6925 | ||
6926 | /* The backend must work out the sizes of all the other dynamic | |
6927 | sections. */ | |
6928 | if (dynobj != NULL | |
6929 | && bed->elf_backend_size_dynamic_sections != NULL | |
6930 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
6931 | return FALSE; | |
6932 | ||
6933 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6934 | { | |
902e9fc7 MR |
6935 | if (elf_tdata (output_bfd)->cverdefs) |
6936 | { | |
6937 | unsigned int crefs = elf_tdata (output_bfd)->cverdefs; | |
6938 | ||
6939 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6940 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) | |
6941 | return FALSE; | |
6942 | } | |
6943 | ||
6944 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6945 | { | |
6946 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6947 | return FALSE; | |
6948 | } | |
6949 | else if (info->flags & DF_BIND_NOW) | |
6950 | { | |
6951 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6952 | return FALSE; | |
6953 | } | |
6954 | ||
6955 | if (info->flags_1) | |
6956 | { | |
6957 | if (bfd_link_executable (info)) | |
6958 | info->flags_1 &= ~ (DF_1_INITFIRST | |
6959 | | DF_1_NODELETE | |
6960 | | DF_1_NOOPEN); | |
6961 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6962 | return FALSE; | |
6963 | } | |
6964 | ||
6965 | if (elf_tdata (output_bfd)->cverrefs) | |
6966 | { | |
6967 | unsigned int crefs = elf_tdata (output_bfd)->cverrefs; | |
6968 | ||
6969 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6970 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6971 | return FALSE; | |
6972 | } | |
5a580b3a | 6973 | |
8423293d AM |
6974 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6975 | && elf_tdata (output_bfd)->cverdefs == 0) | |
63f452a8 | 6976 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) |
8423293d | 6977 | { |
902e9fc7 MR |
6978 | asection *s; |
6979 | ||
3d4d4302 | 6980 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
6981 | s->flags |= SEC_EXCLUDE; |
6982 | } | |
6983 | } | |
6984 | return TRUE; | |
6985 | } | |
6986 | ||
74541ad4 AM |
6987 | /* Find the first non-excluded output section. We'll use its |
6988 | section symbol for some emitted relocs. */ | |
6989 | void | |
6990 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6991 | { | |
6992 | asection *s; | |
6993 | ||
6994 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6995 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
d00dd7dc | 6996 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 AM |
6997 | { |
6998 | elf_hash_table (info)->text_index_section = s; | |
6999 | break; | |
7000 | } | |
7001 | } | |
7002 | ||
7003 | /* Find two non-excluded output sections, one for code, one for data. | |
7004 | We'll use their section symbols for some emitted relocs. */ | |
7005 | void | |
7006 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
7007 | { | |
7008 | asection *s; | |
7009 | ||
266b05cf DJ |
7010 | /* Data first, since setting text_index_section changes |
7011 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 7012 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 7013 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
d00dd7dc | 7014 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7015 | { |
266b05cf | 7016 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
7017 | break; |
7018 | } | |
7019 | ||
7020 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
7021 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
7022 | == (SEC_ALLOC | SEC_READONLY)) | |
d00dd7dc | 7023 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7024 | { |
266b05cf | 7025 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
7026 | break; |
7027 | } | |
7028 | ||
7029 | if (elf_hash_table (info)->text_index_section == NULL) | |
7030 | elf_hash_table (info)->text_index_section | |
7031 | = elf_hash_table (info)->data_index_section; | |
7032 | } | |
7033 | ||
8423293d AM |
7034 | bfd_boolean |
7035 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
7036 | { | |
74541ad4 | 7037 | const struct elf_backend_data *bed; |
23ec1e32 | 7038 | unsigned long section_sym_count; |
96d01d93 | 7039 | bfd_size_type dynsymcount = 0; |
74541ad4 | 7040 | |
8423293d AM |
7041 | if (!is_elf_hash_table (info->hash)) |
7042 | return TRUE; | |
7043 | ||
74541ad4 AM |
7044 | bed = get_elf_backend_data (output_bfd); |
7045 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
7046 | ||
23ec1e32 MR |
7047 | /* Assign dynsym indices. In a shared library we generate a section |
7048 | symbol for each output section, which come first. Next come all | |
7049 | of the back-end allocated local dynamic syms, followed by the rest | |
7050 | of the global symbols. | |
7051 | ||
7052 | This is usually not needed for static binaries, however backends | |
7053 | can request to always do it, e.g. the MIPS backend uses dynamic | |
7054 | symbol counts to lay out GOT, which will be produced in the | |
7055 | presence of GOT relocations even in static binaries (holding fixed | |
7056 | data in that case, to satisfy those relocations). */ | |
7057 | ||
7058 | if (elf_hash_table (info)->dynamic_sections_created | |
7059 | || bed->always_renumber_dynsyms) | |
7060 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
7061 | §ion_sym_count); | |
7062 | ||
8423293d AM |
7063 | if (elf_hash_table (info)->dynamic_sections_created) |
7064 | { | |
7065 | bfd *dynobj; | |
8423293d | 7066 | asection *s; |
8423293d AM |
7067 | unsigned int dtagcount; |
7068 | ||
7069 | dynobj = elf_hash_table (info)->dynobj; | |
7070 | ||
5a580b3a | 7071 | /* Work out the size of the symbol version section. */ |
3d4d4302 | 7072 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 7073 | BFD_ASSERT (s != NULL); |
d5486c43 | 7074 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 7075 | { |
eea6121a | 7076 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 7077 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
7078 | if (s->contents == NULL) |
7079 | return FALSE; | |
7080 | ||
7081 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
7082 | return FALSE; | |
7083 | } | |
7084 | ||
7085 | /* Set the size of the .dynsym and .hash sections. We counted | |
7086 | the number of dynamic symbols in elf_link_add_object_symbols. | |
7087 | We will build the contents of .dynsym and .hash when we build | |
7088 | the final symbol table, because until then we do not know the | |
7089 | correct value to give the symbols. We built the .dynstr | |
7090 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 7091 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 7092 | BFD_ASSERT (s != NULL); |
eea6121a | 7093 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 7094 | |
d5486c43 L |
7095 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
7096 | if (s->contents == NULL) | |
7097 | return FALSE; | |
5a580b3a | 7098 | |
d5486c43 L |
7099 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
7100 | section syms, in case we don't output them all. */ | |
7101 | ++section_sym_count; | |
7102 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 7103 | |
fdc90cb4 JJ |
7104 | elf_hash_table (info)->bucketcount = 0; |
7105 | ||
5a580b3a AM |
7106 | /* Compute the size of the hashing table. As a side effect this |
7107 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
7108 | if (info->emit_hash) |
7109 | { | |
7110 | unsigned long int *hashcodes; | |
14b1c01e | 7111 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
7112 | bfd_size_type amt; |
7113 | unsigned long int nsyms; | |
7114 | size_t bucketcount; | |
7115 | size_t hash_entry_size; | |
7116 | ||
7117 | /* Compute the hash values for all exported symbols. At the same | |
7118 | time store the values in an array so that we could use them for | |
7119 | optimizations. */ | |
7120 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 7121 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7122 | if (hashcodes == NULL) |
7123 | return FALSE; | |
14b1c01e AM |
7124 | hashinf.hashcodes = hashcodes; |
7125 | hashinf.error = FALSE; | |
5a580b3a | 7126 | |
fdc90cb4 JJ |
7127 | /* Put all hash values in HASHCODES. */ |
7128 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
7129 | elf_collect_hash_codes, &hashinf); |
7130 | if (hashinf.error) | |
4dd07732 AM |
7131 | { |
7132 | free (hashcodes); | |
7133 | return FALSE; | |
7134 | } | |
5a580b3a | 7135 | |
14b1c01e | 7136 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
7137 | bucketcount |
7138 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
7139 | free (hashcodes); | |
7140 | ||
4b48e2f6 | 7141 | if (bucketcount == 0 && nsyms > 0) |
fdc90cb4 | 7142 | return FALSE; |
5a580b3a | 7143 | |
fdc90cb4 JJ |
7144 | elf_hash_table (info)->bucketcount = bucketcount; |
7145 | ||
3d4d4302 | 7146 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
7147 | BFD_ASSERT (s != NULL); |
7148 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
7149 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 7150 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7151 | if (s->contents == NULL) |
7152 | return FALSE; | |
7153 | ||
7154 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
7155 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
7156 | s->contents + hash_entry_size); | |
7157 | } | |
7158 | ||
7159 | if (info->emit_gnu_hash) | |
7160 | { | |
7161 | size_t i, cnt; | |
7162 | unsigned char *contents; | |
7163 | struct collect_gnu_hash_codes cinfo; | |
7164 | bfd_size_type amt; | |
7165 | size_t bucketcount; | |
7166 | ||
7167 | memset (&cinfo, 0, sizeof (cinfo)); | |
7168 | ||
7169 | /* Compute the hash values for all exported symbols. At the same | |
7170 | time store the values in an array so that we could use them for | |
7171 | optimizations. */ | |
7172 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 7173 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7174 | if (cinfo.hashcodes == NULL) |
7175 | return FALSE; | |
7176 | ||
7177 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
7178 | cinfo.min_dynindx = -1; | |
7179 | cinfo.output_bfd = output_bfd; | |
7180 | cinfo.bed = bed; | |
7181 | ||
7182 | /* Put all hash values in HASHCODES. */ | |
7183 | elf_link_hash_traverse (elf_hash_table (info), | |
7184 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 7185 | if (cinfo.error) |
4dd07732 AM |
7186 | { |
7187 | free (cinfo.hashcodes); | |
7188 | return FALSE; | |
7189 | } | |
fdc90cb4 JJ |
7190 | |
7191 | bucketcount | |
7192 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
7193 | ||
7194 | if (bucketcount == 0) | |
7195 | { | |
7196 | free (cinfo.hashcodes); | |
7197 | return FALSE; | |
7198 | } | |
7199 | ||
3d4d4302 | 7200 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
7201 | BFD_ASSERT (s != NULL); |
7202 | ||
7203 | if (cinfo.nsyms == 0) | |
7204 | { | |
7205 | /* Empty .gnu.hash section is special. */ | |
7206 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
7207 | free (cinfo.hashcodes); | |
7208 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 7209 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7210 | if (contents == NULL) |
7211 | return FALSE; | |
7212 | s->contents = contents; | |
7213 | /* 1 empty bucket. */ | |
7214 | bfd_put_32 (output_bfd, 1, contents); | |
7215 | /* SYMIDX above the special symbol 0. */ | |
7216 | bfd_put_32 (output_bfd, 1, contents + 4); | |
7217 | /* Just one word for bitmask. */ | |
7218 | bfd_put_32 (output_bfd, 1, contents + 8); | |
7219 | /* Only hash fn bloom filter. */ | |
7220 | bfd_put_32 (output_bfd, 0, contents + 12); | |
7221 | /* No hashes are valid - empty bitmask. */ | |
7222 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
7223 | /* No hashes in the only bucket. */ | |
7224 | bfd_put_32 (output_bfd, 0, | |
7225 | contents + 16 + bed->s->arch_size / 8); | |
7226 | } | |
7227 | else | |
7228 | { | |
9e6619e2 | 7229 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 7230 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 7231 | |
9e6619e2 AM |
7232 | x = cinfo.nsyms; |
7233 | maskbitslog2 = 1; | |
7234 | while ((x >>= 1) != 0) | |
7235 | ++maskbitslog2; | |
fdc90cb4 JJ |
7236 | if (maskbitslog2 < 3) |
7237 | maskbitslog2 = 5; | |
7238 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
7239 | maskbitslog2 = maskbitslog2 + 3; | |
7240 | else | |
7241 | maskbitslog2 = maskbitslog2 + 2; | |
7242 | if (bed->s->arch_size == 64) | |
7243 | { | |
7244 | if (maskbitslog2 == 5) | |
7245 | maskbitslog2 = 6; | |
7246 | cinfo.shift1 = 6; | |
7247 | } | |
7248 | else | |
7249 | cinfo.shift1 = 5; | |
7250 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 7251 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
7252 | cinfo.maskbits = 1 << maskbitslog2; |
7253 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
7254 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
7255 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 7256 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
7257 | if (cinfo.bitmask == NULL) |
7258 | { | |
7259 | free (cinfo.hashcodes); | |
7260 | return FALSE; | |
7261 | } | |
7262 | ||
a50b1753 | 7263 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
7264 | cinfo.indx = cinfo.counts + bucketcount; |
7265 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
7266 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
7267 | ||
7268 | /* Determine how often each hash bucket is used. */ | |
7269 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
7270 | for (i = 0; i < cinfo.nsyms; ++i) | |
7271 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
7272 | ||
7273 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
7274 | if (cinfo.counts[i] != 0) | |
7275 | { | |
7276 | cinfo.indx[i] = cnt; | |
7277 | cnt += cinfo.counts[i]; | |
7278 | } | |
7279 | BFD_ASSERT (cnt == dynsymcount); | |
7280 | cinfo.bucketcount = bucketcount; | |
7281 | cinfo.local_indx = cinfo.min_dynindx; | |
7282 | ||
7283 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
7284 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 7285 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7286 | if (contents == NULL) |
7287 | { | |
7288 | free (cinfo.bitmask); | |
7289 | free (cinfo.hashcodes); | |
7290 | return FALSE; | |
7291 | } | |
7292 | ||
7293 | s->contents = contents; | |
7294 | bfd_put_32 (output_bfd, bucketcount, contents); | |
7295 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
7296 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
7297 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
7298 | contents += 16 + cinfo.maskbits / 8; | |
7299 | ||
7300 | for (i = 0; i < bucketcount; ++i) | |
7301 | { | |
7302 | if (cinfo.counts[i] == 0) | |
7303 | bfd_put_32 (output_bfd, 0, contents); | |
7304 | else | |
7305 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
7306 | contents += 4; | |
7307 | } | |
7308 | ||
7309 | cinfo.contents = contents; | |
7310 | ||
7311 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
7312 | elf_link_hash_traverse (elf_hash_table (info), | |
7313 | elf_renumber_gnu_hash_syms, &cinfo); | |
7314 | ||
7315 | contents = s->contents + 16; | |
7316 | for (i = 0; i < maskwords; ++i) | |
7317 | { | |
7318 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
7319 | contents); | |
7320 | contents += bed->s->arch_size / 8; | |
7321 | } | |
7322 | ||
7323 | free (cinfo.bitmask); | |
7324 | free (cinfo.hashcodes); | |
7325 | } | |
7326 | } | |
5a580b3a | 7327 | |
3d4d4302 | 7328 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
7329 | BFD_ASSERT (s != NULL); |
7330 | ||
4ad4eba5 | 7331 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 7332 | |
eea6121a | 7333 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
7334 | |
7335 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
7336 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
7337 | return FALSE; | |
7338 | } | |
7339 | ||
7340 | return TRUE; | |
7341 | } | |
4d269e42 | 7342 | \f |
4d269e42 AM |
7343 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
7344 | ||
7345 | static void | |
7346 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
7347 | asection *sec) | |
7348 | { | |
dbaa2011 AM |
7349 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
7350 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
7351 | } |
7352 | ||
7353 | /* Finish SHF_MERGE section merging. */ | |
7354 | ||
7355 | bfd_boolean | |
630993ec | 7356 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
7357 | { |
7358 | bfd *ibfd; | |
7359 | asection *sec; | |
7360 | ||
7361 | if (!is_elf_hash_table (info->hash)) | |
7362 | return FALSE; | |
7363 | ||
c72f2fb2 | 7364 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
7365 | if ((ibfd->flags & DYNAMIC) == 0 |
7366 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
7367 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
7368 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
7369 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
7370 | if ((sec->flags & SEC_MERGE) != 0 | |
7371 | && !bfd_is_abs_section (sec->output_section)) | |
7372 | { | |
7373 | struct bfd_elf_section_data *secdata; | |
7374 | ||
7375 | secdata = elf_section_data (sec); | |
630993ec | 7376 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
7377 | &elf_hash_table (info)->merge_info, |
7378 | sec, &secdata->sec_info)) | |
7379 | return FALSE; | |
7380 | else if (secdata->sec_info) | |
dbaa2011 | 7381 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7382 | } |
7383 | ||
7384 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7385 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
7386 | merge_sections_remove_hook); |
7387 | return TRUE; | |
7388 | } | |
7389 | ||
7390 | /* Create an entry in an ELF linker hash table. */ | |
7391 | ||
7392 | struct bfd_hash_entry * | |
7393 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7394 | struct bfd_hash_table *table, | |
7395 | const char *string) | |
7396 | { | |
7397 | /* Allocate the structure if it has not already been allocated by a | |
7398 | subclass. */ | |
7399 | if (entry == NULL) | |
7400 | { | |
a50b1753 | 7401 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7402 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7403 | if (entry == NULL) |
7404 | return entry; | |
7405 | } | |
7406 | ||
7407 | /* Call the allocation method of the superclass. */ | |
7408 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7409 | if (entry != NULL) | |
7410 | { | |
7411 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7412 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7413 | ||
7414 | /* Set local fields. */ | |
7415 | ret->indx = -1; | |
7416 | ret->dynindx = -1; | |
7417 | ret->got = htab->init_got_refcount; | |
7418 | ret->plt = htab->init_plt_refcount; | |
7419 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7420 | - offsetof (struct elf_link_hash_entry, size))); | |
7421 | /* Assume that we have been called by a non-ELF symbol reader. | |
7422 | This flag is then reset by the code which reads an ELF input | |
7423 | file. This ensures that a symbol created by a non-ELF symbol | |
7424 | reader will have the flag set correctly. */ | |
7425 | ret->non_elf = 1; | |
7426 | } | |
7427 | ||
7428 | return entry; | |
7429 | } | |
7430 | ||
7431 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7432 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7433 | ||
7434 | void | |
7435 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7436 | struct elf_link_hash_entry *dir, | |
7437 | struct elf_link_hash_entry *ind) | |
7438 | { | |
7439 | struct elf_link_hash_table *htab; | |
7440 | ||
7441 | /* Copy down any references that we may have already seen to the | |
e81830c5 | 7442 | symbol which just became indirect. */ |
4d269e42 | 7443 | |
422f1182 | 7444 | if (dir->versioned != versioned_hidden) |
e81830c5 AM |
7445 | dir->ref_dynamic |= ind->ref_dynamic; |
7446 | dir->ref_regular |= ind->ref_regular; | |
7447 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7448 | dir->non_got_ref |= ind->non_got_ref; | |
7449 | dir->needs_plt |= ind->needs_plt; | |
7450 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
4d269e42 AM |
7451 | |
7452 | if (ind->root.type != bfd_link_hash_indirect) | |
7453 | return; | |
7454 | ||
7455 | /* Copy over the global and procedure linkage table refcount entries. | |
7456 | These may have been already set up by a check_relocs routine. */ | |
7457 | htab = elf_hash_table (info); | |
7458 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7459 | { | |
7460 | if (dir->got.refcount < 0) | |
7461 | dir->got.refcount = 0; | |
7462 | dir->got.refcount += ind->got.refcount; | |
7463 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7464 | } | |
7465 | ||
7466 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7467 | { | |
7468 | if (dir->plt.refcount < 0) | |
7469 | dir->plt.refcount = 0; | |
7470 | dir->plt.refcount += ind->plt.refcount; | |
7471 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7472 | } | |
7473 | ||
7474 | if (ind->dynindx != -1) | |
7475 | { | |
7476 | if (dir->dynindx != -1) | |
7477 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7478 | dir->dynindx = ind->dynindx; | |
7479 | dir->dynstr_index = ind->dynstr_index; | |
7480 | ind->dynindx = -1; | |
7481 | ind->dynstr_index = 0; | |
7482 | } | |
7483 | } | |
7484 | ||
7485 | void | |
7486 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7487 | struct elf_link_hash_entry *h, | |
7488 | bfd_boolean force_local) | |
7489 | { | |
3aa14d16 L |
7490 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7491 | if (h->type != STT_GNU_IFUNC) | |
7492 | { | |
7493 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7494 | h->needs_plt = 0; | |
7495 | } | |
4d269e42 AM |
7496 | if (force_local) |
7497 | { | |
7498 | h->forced_local = 1; | |
7499 | if (h->dynindx != -1) | |
7500 | { | |
4d269e42 AM |
7501 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, |
7502 | h->dynstr_index); | |
641338d8 AM |
7503 | h->dynindx = -1; |
7504 | h->dynstr_index = 0; | |
4d269e42 AM |
7505 | } |
7506 | } | |
7507 | } | |
7508 | ||
34a87bb0 L |
7509 | /* Hide a symbol. */ |
7510 | ||
7511 | void | |
7512 | _bfd_elf_link_hide_symbol (bfd *output_bfd, | |
7513 | struct bfd_link_info *info, | |
7514 | struct bfd_link_hash_entry *h) | |
7515 | { | |
7516 | if (is_elf_hash_table (info->hash)) | |
7517 | { | |
7518 | const struct elf_backend_data *bed | |
7519 | = get_elf_backend_data (output_bfd); | |
7520 | struct elf_link_hash_entry *eh | |
7521 | = (struct elf_link_hash_entry *) h; | |
7522 | bed->elf_backend_hide_symbol (info, eh, TRUE); | |
7523 | eh->def_dynamic = 0; | |
7524 | eh->ref_dynamic = 0; | |
7525 | eh->dynamic_def = 0; | |
7526 | } | |
7527 | } | |
7528 | ||
7bf52ea2 AM |
7529 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7530 | caller. */ | |
4d269e42 AM |
7531 | |
7532 | bfd_boolean | |
7533 | _bfd_elf_link_hash_table_init | |
7534 | (struct elf_link_hash_table *table, | |
7535 | bfd *abfd, | |
7536 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7537 | struct bfd_hash_table *, | |
7538 | const char *), | |
4dfe6ac6 NC |
7539 | unsigned int entsize, |
7540 | enum elf_target_id target_id) | |
4d269e42 AM |
7541 | { |
7542 | bfd_boolean ret; | |
7543 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7544 | ||
4d269e42 AM |
7545 | table->init_got_refcount.refcount = can_refcount - 1; |
7546 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7547 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7548 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7549 | /* The first dynamic symbol is a dummy. */ | |
7550 | table->dynsymcount = 1; | |
7551 | ||
7552 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7553 | |
4d269e42 | 7554 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7555 | table->hash_table_id = target_id; |
4d269e42 AM |
7556 | |
7557 | return ret; | |
7558 | } | |
7559 | ||
7560 | /* Create an ELF linker hash table. */ | |
7561 | ||
7562 | struct bfd_link_hash_table * | |
7563 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7564 | { | |
7565 | struct elf_link_hash_table *ret; | |
7566 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7567 | ||
7bf52ea2 | 7568 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7569 | if (ret == NULL) |
7570 | return NULL; | |
7571 | ||
7572 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7573 | sizeof (struct elf_link_hash_entry), |
7574 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7575 | { |
7576 | free (ret); | |
7577 | return NULL; | |
7578 | } | |
d495ab0d | 7579 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7580 | |
7581 | return &ret->root; | |
7582 | } | |
7583 | ||
9f7c3e5e AM |
7584 | /* Destroy an ELF linker hash table. */ |
7585 | ||
7586 | void | |
d495ab0d | 7587 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7588 | { |
d495ab0d AM |
7589 | struct elf_link_hash_table *htab; |
7590 | ||
7591 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7592 | if (htab->dynstr != NULL) |
7593 | _bfd_elf_strtab_free (htab->dynstr); | |
7594 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7595 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7596 | } |
7597 | ||
4d269e42 AM |
7598 | /* This is a hook for the ELF emulation code in the generic linker to |
7599 | tell the backend linker what file name to use for the DT_NEEDED | |
7600 | entry for a dynamic object. */ | |
7601 | ||
7602 | void | |
7603 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7604 | { | |
7605 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7606 | && bfd_get_format (abfd) == bfd_object) | |
7607 | elf_dt_name (abfd) = name; | |
7608 | } | |
7609 | ||
7610 | int | |
7611 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7612 | { | |
7613 | int lib_class; | |
7614 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7615 | && bfd_get_format (abfd) == bfd_object) | |
7616 | lib_class = elf_dyn_lib_class (abfd); | |
7617 | else | |
7618 | lib_class = 0; | |
7619 | return lib_class; | |
7620 | } | |
7621 | ||
7622 | void | |
7623 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7624 | { | |
7625 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7626 | && bfd_get_format (abfd) == bfd_object) | |
7627 | elf_dyn_lib_class (abfd) = lib_class; | |
7628 | } | |
7629 | ||
7630 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7631 | the linker ELF emulation code. */ | |
7632 | ||
7633 | struct bfd_link_needed_list * | |
7634 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7635 | struct bfd_link_info *info) | |
7636 | { | |
7637 | if (! is_elf_hash_table (info->hash)) | |
7638 | return NULL; | |
7639 | return elf_hash_table (info)->needed; | |
7640 | } | |
7641 | ||
7642 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7643 | hook for the linker ELF emulation code. */ | |
7644 | ||
7645 | struct bfd_link_needed_list * | |
7646 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7647 | struct bfd_link_info *info) | |
7648 | { | |
7649 | if (! is_elf_hash_table (info->hash)) | |
7650 | return NULL; | |
7651 | return elf_hash_table (info)->runpath; | |
7652 | } | |
7653 | ||
7654 | /* Get the name actually used for a dynamic object for a link. This | |
7655 | is the SONAME entry if there is one. Otherwise, it is the string | |
7656 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7657 | ||
7658 | const char * | |
7659 | bfd_elf_get_dt_soname (bfd *abfd) | |
7660 | { | |
7661 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7662 | && bfd_get_format (abfd) == bfd_object) | |
7663 | return elf_dt_name (abfd); | |
7664 | return NULL; | |
7665 | } | |
7666 | ||
7667 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7668 | the ELF linker emulation code. */ | |
7669 | ||
7670 | bfd_boolean | |
7671 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7672 | struct bfd_link_needed_list **pneeded) | |
7673 | { | |
7674 | asection *s; | |
7675 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7676 | unsigned int elfsec; |
4d269e42 AM |
7677 | unsigned long shlink; |
7678 | bfd_byte *extdyn, *extdynend; | |
7679 | size_t extdynsize; | |
7680 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7681 | ||
7682 | *pneeded = NULL; | |
7683 | ||
7684 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7685 | || bfd_get_format (abfd) != bfd_object) | |
7686 | return TRUE; | |
7687 | ||
7688 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7689 | if (s == NULL || s->size == 0) | |
7690 | return TRUE; | |
7691 | ||
7692 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7693 | goto error_return; | |
7694 | ||
7695 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7696 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7697 | goto error_return; |
7698 | ||
7699 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7700 | |
4d269e42 AM |
7701 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7702 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7703 | ||
7704 | extdyn = dynbuf; | |
7705 | extdynend = extdyn + s->size; | |
7706 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7707 | { | |
7708 | Elf_Internal_Dyn dyn; | |
7709 | ||
7710 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7711 | ||
7712 | if (dyn.d_tag == DT_NULL) | |
7713 | break; | |
7714 | ||
7715 | if (dyn.d_tag == DT_NEEDED) | |
7716 | { | |
7717 | const char *string; | |
7718 | struct bfd_link_needed_list *l; | |
7719 | unsigned int tagv = dyn.d_un.d_val; | |
7720 | bfd_size_type amt; | |
7721 | ||
7722 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7723 | if (string == NULL) | |
7724 | goto error_return; | |
7725 | ||
7726 | amt = sizeof *l; | |
a50b1753 | 7727 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7728 | if (l == NULL) |
7729 | goto error_return; | |
7730 | ||
7731 | l->by = abfd; | |
7732 | l->name = string; | |
7733 | l->next = *pneeded; | |
7734 | *pneeded = l; | |
7735 | } | |
7736 | } | |
7737 | ||
7738 | free (dynbuf); | |
7739 | ||
7740 | return TRUE; | |
7741 | ||
7742 | error_return: | |
7743 | if (dynbuf != NULL) | |
7744 | free (dynbuf); | |
7745 | return FALSE; | |
7746 | } | |
7747 | ||
7748 | struct elf_symbuf_symbol | |
7749 | { | |
7750 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7751 | unsigned char st_info; /* Type and binding attributes */ | |
7752 | unsigned char st_other; /* Visibilty, and target specific */ | |
7753 | }; | |
7754 | ||
7755 | struct elf_symbuf_head | |
7756 | { | |
7757 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7758 | size_t count; |
4d269e42 AM |
7759 | unsigned int st_shndx; |
7760 | }; | |
7761 | ||
7762 | struct elf_symbol | |
7763 | { | |
7764 | union | |
7765 | { | |
7766 | Elf_Internal_Sym *isym; | |
7767 | struct elf_symbuf_symbol *ssym; | |
7768 | } u; | |
7769 | const char *name; | |
7770 | }; | |
7771 | ||
7772 | /* Sort references to symbols by ascending section number. */ | |
7773 | ||
7774 | static int | |
7775 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7776 | { | |
7777 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7778 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7779 | ||
7780 | return s1->st_shndx - s2->st_shndx; | |
7781 | } | |
7782 | ||
7783 | static int | |
7784 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7785 | { | |
7786 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7787 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7788 | return strcmp (s1->name, s2->name); | |
7789 | } | |
7790 | ||
7791 | static struct elf_symbuf_head * | |
ef53be89 | 7792 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7793 | { |
14b1c01e | 7794 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7795 | struct elf_symbuf_symbol *ssym; |
7796 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7797 | size_t i, shndx_count, total_size; |
4d269e42 | 7798 | |
a50b1753 | 7799 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7800 | if (indbuf == NULL) |
7801 | return NULL; | |
7802 | ||
7803 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7804 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7805 | *ind++ = &isymbuf[i]; | |
7806 | indbufend = ind; | |
7807 | ||
7808 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7809 | elf_sort_elf_symbol); | |
7810 | ||
7811 | shndx_count = 0; | |
7812 | if (indbufend > indbuf) | |
7813 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7814 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7815 | shndx_count++; | |
7816 | ||
3ae181ee L |
7817 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7818 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7819 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7820 | if (ssymbuf == NULL) |
7821 | { | |
7822 | free (indbuf); | |
7823 | return NULL; | |
7824 | } | |
7825 | ||
3ae181ee | 7826 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7827 | ssymbuf->ssym = NULL; |
7828 | ssymbuf->count = shndx_count; | |
7829 | ssymbuf->st_shndx = 0; | |
7830 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7831 | { | |
7832 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7833 | { | |
7834 | ssymhead++; | |
7835 | ssymhead->ssym = ssym; | |
7836 | ssymhead->count = 0; | |
7837 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7838 | } | |
7839 | ssym->st_name = (*ind)->st_name; | |
7840 | ssym->st_info = (*ind)->st_info; | |
7841 | ssym->st_other = (*ind)->st_other; | |
7842 | ssymhead->count++; | |
7843 | } | |
ef53be89 | 7844 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7845 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7846 | == total_size)); | |
4d269e42 AM |
7847 | |
7848 | free (indbuf); | |
7849 | return ssymbuf; | |
7850 | } | |
7851 | ||
7852 | /* Check if 2 sections define the same set of local and global | |
7853 | symbols. */ | |
7854 | ||
8f317e31 | 7855 | static bfd_boolean |
4d269e42 AM |
7856 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7857 | struct bfd_link_info *info) | |
7858 | { | |
7859 | bfd *bfd1, *bfd2; | |
7860 | const struct elf_backend_data *bed1, *bed2; | |
7861 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7862 | size_t symcount1, symcount2; |
4d269e42 AM |
7863 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7864 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7865 | Elf_Internal_Sym *isym, *isymend; | |
7866 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7867 | size_t count1, count2, i; |
cb33740c | 7868 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7869 | bfd_boolean result; |
7870 | ||
7871 | bfd1 = sec1->owner; | |
7872 | bfd2 = sec2->owner; | |
7873 | ||
4d269e42 AM |
7874 | /* Both sections have to be in ELF. */ |
7875 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7876 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7877 | return FALSE; | |
7878 | ||
7879 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7880 | return FALSE; | |
7881 | ||
4d269e42 AM |
7882 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7883 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7884 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7885 | return FALSE; |
7886 | ||
7887 | bed1 = get_elf_backend_data (bfd1); | |
7888 | bed2 = get_elf_backend_data (bfd2); | |
7889 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7890 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7891 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7892 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7893 | ||
7894 | if (symcount1 == 0 || symcount2 == 0) | |
7895 | return FALSE; | |
7896 | ||
7897 | result = FALSE; | |
7898 | isymbuf1 = NULL; | |
7899 | isymbuf2 = NULL; | |
a50b1753 NC |
7900 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7901 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7902 | |
7903 | if (ssymbuf1 == NULL) | |
7904 | { | |
7905 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7906 | NULL, NULL, NULL); | |
7907 | if (isymbuf1 == NULL) | |
7908 | goto done; | |
7909 | ||
7910 | if (!info->reduce_memory_overheads) | |
7911 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7912 | = elf_create_symbuf (symcount1, isymbuf1); | |
7913 | } | |
7914 | ||
7915 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7916 | { | |
7917 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7918 | NULL, NULL, NULL); | |
7919 | if (isymbuf2 == NULL) | |
7920 | goto done; | |
7921 | ||
7922 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7923 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7924 | = elf_create_symbuf (symcount2, isymbuf2); | |
7925 | } | |
7926 | ||
7927 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7928 | { | |
7929 | /* Optimized faster version. */ | |
ef53be89 | 7930 | size_t lo, hi, mid; |
4d269e42 AM |
7931 | struct elf_symbol *symp; |
7932 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7933 | ||
7934 | lo = 0; | |
7935 | hi = ssymbuf1->count; | |
7936 | ssymbuf1++; | |
7937 | count1 = 0; | |
7938 | while (lo < hi) | |
7939 | { | |
7940 | mid = (lo + hi) / 2; | |
cb33740c | 7941 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7942 | hi = mid; |
cb33740c | 7943 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7944 | lo = mid + 1; |
7945 | else | |
7946 | { | |
7947 | count1 = ssymbuf1[mid].count; | |
7948 | ssymbuf1 += mid; | |
7949 | break; | |
7950 | } | |
7951 | } | |
7952 | ||
7953 | lo = 0; | |
7954 | hi = ssymbuf2->count; | |
7955 | ssymbuf2++; | |
7956 | count2 = 0; | |
7957 | while (lo < hi) | |
7958 | { | |
7959 | mid = (lo + hi) / 2; | |
cb33740c | 7960 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7961 | hi = mid; |
cb33740c | 7962 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7963 | lo = mid + 1; |
7964 | else | |
7965 | { | |
7966 | count2 = ssymbuf2[mid].count; | |
7967 | ssymbuf2 += mid; | |
7968 | break; | |
7969 | } | |
7970 | } | |
7971 | ||
7972 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7973 | goto done; | |
7974 | ||
ca4be51c AM |
7975 | symtable1 |
7976 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
7977 | symtable2 | |
7978 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
7979 | if (symtable1 == NULL || symtable2 == NULL) |
7980 | goto done; | |
7981 | ||
7982 | symp = symtable1; | |
7983 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7984 | ssym < ssymend; ssym++, symp++) | |
7985 | { | |
7986 | symp->u.ssym = ssym; | |
7987 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7988 | hdr1->sh_link, | |
7989 | ssym->st_name); | |
7990 | } | |
7991 | ||
7992 | symp = symtable2; | |
7993 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7994 | ssym < ssymend; ssym++, symp++) | |
7995 | { | |
7996 | symp->u.ssym = ssym; | |
7997 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7998 | hdr2->sh_link, | |
7999 | ssym->st_name); | |
8000 | } | |
8001 | ||
8002 | /* Sort symbol by name. */ | |
8003 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8004 | elf_sym_name_compare); | |
8005 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8006 | elf_sym_name_compare); | |
8007 | ||
8008 | for (i = 0; i < count1; i++) | |
8009 | /* Two symbols must have the same binding, type and name. */ | |
8010 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
8011 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
8012 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8013 | goto done; | |
8014 | ||
8015 | result = TRUE; | |
8016 | goto done; | |
8017 | } | |
8018 | ||
a50b1753 NC |
8019 | symtable1 = (struct elf_symbol *) |
8020 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
8021 | symtable2 = (struct elf_symbol *) | |
8022 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
8023 | if (symtable1 == NULL || symtable2 == NULL) |
8024 | goto done; | |
8025 | ||
8026 | /* Count definitions in the section. */ | |
8027 | count1 = 0; | |
8028 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 8029 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
8030 | symtable1[count1++].u.isym = isym; |
8031 | ||
8032 | count2 = 0; | |
8033 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 8034 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
8035 | symtable2[count2++].u.isym = isym; |
8036 | ||
8037 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8038 | goto done; | |
8039 | ||
8040 | for (i = 0; i < count1; i++) | |
8041 | symtable1[i].name | |
8042 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
8043 | symtable1[i].u.isym->st_name); | |
8044 | ||
8045 | for (i = 0; i < count2; i++) | |
8046 | symtable2[i].name | |
8047 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
8048 | symtable2[i].u.isym->st_name); | |
8049 | ||
8050 | /* Sort symbol by name. */ | |
8051 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8052 | elf_sym_name_compare); | |
8053 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8054 | elf_sym_name_compare); | |
8055 | ||
8056 | for (i = 0; i < count1; i++) | |
8057 | /* Two symbols must have the same binding, type and name. */ | |
8058 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
8059 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
8060 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8061 | goto done; | |
8062 | ||
8063 | result = TRUE; | |
8064 | ||
8065 | done: | |
8066 | if (symtable1) | |
8067 | free (symtable1); | |
8068 | if (symtable2) | |
8069 | free (symtable2); | |
8070 | if (isymbuf1) | |
8071 | free (isymbuf1); | |
8072 | if (isymbuf2) | |
8073 | free (isymbuf2); | |
8074 | ||
8075 | return result; | |
8076 | } | |
8077 | ||
8078 | /* Return TRUE if 2 section types are compatible. */ | |
8079 | ||
8080 | bfd_boolean | |
8081 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
8082 | bfd *bbfd, const asection *bsec) | |
8083 | { | |
8084 | if (asec == NULL | |
8085 | || bsec == NULL | |
8086 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
8087 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
8088 | return TRUE; | |
8089 | ||
8090 | return elf_section_type (asec) == elf_section_type (bsec); | |
8091 | } | |
8092 | \f | |
c152c796 AM |
8093 | /* Final phase of ELF linker. */ |
8094 | ||
8095 | /* A structure we use to avoid passing large numbers of arguments. */ | |
8096 | ||
8097 | struct elf_final_link_info | |
8098 | { | |
8099 | /* General link information. */ | |
8100 | struct bfd_link_info *info; | |
8101 | /* Output BFD. */ | |
8102 | bfd *output_bfd; | |
8103 | /* Symbol string table. */ | |
ef10c3ac | 8104 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
8105 | /* .hash section. */ |
8106 | asection *hash_sec; | |
8107 | /* symbol version section (.gnu.version). */ | |
8108 | asection *symver_sec; | |
8109 | /* Buffer large enough to hold contents of any section. */ | |
8110 | bfd_byte *contents; | |
8111 | /* Buffer large enough to hold external relocs of any section. */ | |
8112 | void *external_relocs; | |
8113 | /* Buffer large enough to hold internal relocs of any section. */ | |
8114 | Elf_Internal_Rela *internal_relocs; | |
8115 | /* Buffer large enough to hold external local symbols of any input | |
8116 | BFD. */ | |
8117 | bfd_byte *external_syms; | |
8118 | /* And a buffer for symbol section indices. */ | |
8119 | Elf_External_Sym_Shndx *locsym_shndx; | |
8120 | /* Buffer large enough to hold internal local symbols of any input | |
8121 | BFD. */ | |
8122 | Elf_Internal_Sym *internal_syms; | |
8123 | /* Array large enough to hold a symbol index for each local symbol | |
8124 | of any input BFD. */ | |
8125 | long *indices; | |
8126 | /* Array large enough to hold a section pointer for each local | |
8127 | symbol of any input BFD. */ | |
8128 | asection **sections; | |
ef10c3ac | 8129 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 8130 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
8131 | /* Number of STT_FILE syms seen. */ |
8132 | size_t filesym_count; | |
c152c796 AM |
8133 | }; |
8134 | ||
8135 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
8136 | ||
8137 | struct elf_outext_info | |
8138 | { | |
8139 | bfd_boolean failed; | |
8140 | bfd_boolean localsyms; | |
34a79995 | 8141 | bfd_boolean file_sym_done; |
8b127cbc | 8142 | struct elf_final_link_info *flinfo; |
c152c796 AM |
8143 | }; |
8144 | ||
d9352518 DB |
8145 | |
8146 | /* Support for evaluating a complex relocation. | |
8147 | ||
8148 | Complex relocations are generalized, self-describing relocations. The | |
8149 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 8150 | relocations themselves. |
d9352518 DB |
8151 | |
8152 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
8153 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
8154 | information (start bit, end bit, word width, etc) into the addend. This | |
8155 | information is extracted from CGEN-generated operand tables within gas. | |
8156 | ||
8157 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
8158 | internal) representing prefix-notation expressions, including but not | |
8159 | limited to those sorts of expressions normally encoded as addends in the | |
8160 | addend field. The symbol mangling format is: | |
8161 | ||
8162 | <node> := <literal> | |
07d6d2b8 AM |
8163 | | <unary-operator> ':' <node> |
8164 | | <binary-operator> ':' <node> ':' <node> | |
d9352518 DB |
8165 | ; |
8166 | ||
8167 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
07d6d2b8 | 8168 | | 'S' <digits=N> ':' <N character section name> |
d9352518 DB |
8169 | | '#' <hexdigits> |
8170 | ; | |
8171 | ||
8172 | <binary-operator> := as in C | |
8173 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
8174 | ||
8175 | static void | |
a0c8462f AM |
8176 | set_symbol_value (bfd *bfd_with_globals, |
8177 | Elf_Internal_Sym *isymbuf, | |
8178 | size_t locsymcount, | |
8179 | size_t symidx, | |
8180 | bfd_vma val) | |
d9352518 | 8181 | { |
8977835c AM |
8182 | struct elf_link_hash_entry **sym_hashes; |
8183 | struct elf_link_hash_entry *h; | |
8184 | size_t extsymoff = locsymcount; | |
d9352518 | 8185 | |
8977835c | 8186 | if (symidx < locsymcount) |
d9352518 | 8187 | { |
8977835c AM |
8188 | Elf_Internal_Sym *sym; |
8189 | ||
8190 | sym = isymbuf + symidx; | |
8191 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
8192 | { | |
8193 | /* It is a local symbol: move it to the | |
8194 | "absolute" section and give it a value. */ | |
8195 | sym->st_shndx = SHN_ABS; | |
8196 | sym->st_value = val; | |
8197 | return; | |
8198 | } | |
8199 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
8200 | extsymoff = 0; | |
d9352518 | 8201 | } |
8977835c AM |
8202 | |
8203 | /* It is a global symbol: set its link type | |
8204 | to "defined" and give it a value. */ | |
8205 | ||
8206 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
8207 | h = sym_hashes [symidx - extsymoff]; | |
8208 | while (h->root.type == bfd_link_hash_indirect | |
8209 | || h->root.type == bfd_link_hash_warning) | |
8210 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8211 | h->root.type = bfd_link_hash_defined; | |
8212 | h->root.u.def.value = val; | |
8213 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
8214 | } |
8215 | ||
a0c8462f AM |
8216 | static bfd_boolean |
8217 | resolve_symbol (const char *name, | |
8218 | bfd *input_bfd, | |
8b127cbc | 8219 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8220 | bfd_vma *result, |
8221 | Elf_Internal_Sym *isymbuf, | |
8222 | size_t locsymcount) | |
d9352518 | 8223 | { |
a0c8462f AM |
8224 | Elf_Internal_Sym *sym; |
8225 | struct bfd_link_hash_entry *global_entry; | |
8226 | const char *candidate = NULL; | |
8227 | Elf_Internal_Shdr *symtab_hdr; | |
8228 | size_t i; | |
8229 | ||
d9352518 DB |
8230 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
8231 | ||
8232 | for (i = 0; i < locsymcount; ++ i) | |
8233 | { | |
8977835c | 8234 | sym = isymbuf + i; |
d9352518 DB |
8235 | |
8236 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
8237 | continue; | |
8238 | ||
8239 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
8240 | symtab_hdr->sh_link, | |
8241 | sym->st_name); | |
8242 | #ifdef DEBUG | |
0f02bbd9 AM |
8243 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
8244 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
8245 | #endif |
8246 | if (candidate && strcmp (candidate, name) == 0) | |
8247 | { | |
8b127cbc | 8248 | asection *sec = flinfo->sections [i]; |
d9352518 | 8249 | |
0f02bbd9 AM |
8250 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
8251 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 8252 | #ifdef DEBUG |
0f02bbd9 AM |
8253 | printf ("Found symbol with value %8.8lx\n", |
8254 | (unsigned long) *result); | |
d9352518 DB |
8255 | #endif |
8256 | return TRUE; | |
8257 | } | |
8258 | } | |
8259 | ||
8260 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 8261 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 8262 | FALSE, FALSE, TRUE); |
d9352518 DB |
8263 | if (!global_entry) |
8264 | return FALSE; | |
a0c8462f | 8265 | |
d9352518 DB |
8266 | if (global_entry->type == bfd_link_hash_defined |
8267 | || global_entry->type == bfd_link_hash_defweak) | |
8268 | { | |
a0c8462f AM |
8269 | *result = (global_entry->u.def.value |
8270 | + global_entry->u.def.section->output_section->vma | |
8271 | + global_entry->u.def.section->output_offset); | |
d9352518 | 8272 | #ifdef DEBUG |
0f02bbd9 AM |
8273 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
8274 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
8275 | #endif |
8276 | return TRUE; | |
a0c8462f | 8277 | } |
d9352518 | 8278 | |
d9352518 DB |
8279 | return FALSE; |
8280 | } | |
8281 | ||
37b01f6a DG |
8282 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
8283 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
8284 | names like "foo.end" which is the end address of section "foo". */ | |
07d6d2b8 | 8285 | |
d9352518 | 8286 | static bfd_boolean |
a0c8462f AM |
8287 | resolve_section (const char *name, |
8288 | asection *sections, | |
37b01f6a DG |
8289 | bfd_vma *result, |
8290 | bfd * abfd) | |
d9352518 | 8291 | { |
a0c8462f AM |
8292 | asection *curr; |
8293 | unsigned int len; | |
d9352518 | 8294 | |
a0c8462f | 8295 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8296 | if (strcmp (curr->name, name) == 0) |
8297 | { | |
8298 | *result = curr->vma; | |
8299 | return TRUE; | |
8300 | } | |
8301 | ||
8302 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 8303 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 8304 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8305 | { |
8306 | len = strlen (curr->name); | |
a0c8462f | 8307 | if (len > strlen (name)) |
d9352518 DB |
8308 | continue; |
8309 | ||
8310 | if (strncmp (curr->name, name, len) == 0) | |
8311 | { | |
8312 | if (strncmp (".end", name + len, 4) == 0) | |
8313 | { | |
37b01f6a | 8314 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
8315 | return TRUE; |
8316 | } | |
8317 | ||
8318 | /* Insert more pseudo-section names here, if you like. */ | |
8319 | } | |
8320 | } | |
a0c8462f | 8321 | |
d9352518 DB |
8322 | return FALSE; |
8323 | } | |
8324 | ||
8325 | static void | |
a0c8462f | 8326 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 8327 | { |
695344c0 | 8328 | /* xgettext:c-format */ |
a0c8462f AM |
8329 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
8330 | reftype, name); | |
d9352518 DB |
8331 | } |
8332 | ||
8333 | static bfd_boolean | |
a0c8462f AM |
8334 | eval_symbol (bfd_vma *result, |
8335 | const char **symp, | |
8336 | bfd *input_bfd, | |
8b127cbc | 8337 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8338 | bfd_vma dot, |
8339 | Elf_Internal_Sym *isymbuf, | |
8340 | size_t locsymcount, | |
8341 | int signed_p) | |
d9352518 | 8342 | { |
4b93929b NC |
8343 | size_t len; |
8344 | size_t symlen; | |
a0c8462f AM |
8345 | bfd_vma a; |
8346 | bfd_vma b; | |
4b93929b | 8347 | char symbuf[4096]; |
0f02bbd9 | 8348 | const char *sym = *symp; |
a0c8462f AM |
8349 | const char *symend; |
8350 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
8351 | |
8352 | len = strlen (sym); | |
8353 | symend = sym + len; | |
8354 | ||
4b93929b | 8355 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
8356 | { |
8357 | bfd_set_error (bfd_error_invalid_operation); | |
8358 | return FALSE; | |
8359 | } | |
a0c8462f | 8360 | |
d9352518 DB |
8361 | switch (* sym) |
8362 | { | |
8363 | case '.': | |
0f02bbd9 AM |
8364 | *result = dot; |
8365 | *symp = sym + 1; | |
d9352518 DB |
8366 | return TRUE; |
8367 | ||
8368 | case '#': | |
0f02bbd9 AM |
8369 | ++sym; |
8370 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
8371 | return TRUE; |
8372 | ||
8373 | case 'S': | |
8374 | symbol_is_section = TRUE; | |
1a0670f3 | 8375 | /* Fall through. */ |
a0c8462f | 8376 | case 's': |
0f02bbd9 AM |
8377 | ++sym; |
8378 | symlen = strtol (sym, (char **) symp, 10); | |
8379 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 8380 | |
4b93929b | 8381 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
8382 | { |
8383 | bfd_set_error (bfd_error_invalid_operation); | |
8384 | return FALSE; | |
8385 | } | |
8386 | ||
8387 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 8388 | symbuf[symlen] = '\0'; |
0f02bbd9 | 8389 | *symp = sym + symlen; |
a0c8462f AM |
8390 | |
8391 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
8392 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
8393 | interpretation here; section means "try section first", not "must be a | |
8394 | section", and likewise with symbol. */ | |
8395 | ||
a0c8462f | 8396 | if (symbol_is_section) |
d9352518 | 8397 | { |
37b01f6a | 8398 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 8399 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8400 | isymbuf, locsymcount)) |
d9352518 DB |
8401 | { |
8402 | undefined_reference ("section", symbuf); | |
8403 | return FALSE; | |
8404 | } | |
a0c8462f AM |
8405 | } |
8406 | else | |
d9352518 | 8407 | { |
8b127cbc | 8408 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8409 | isymbuf, locsymcount) |
8b127cbc | 8410 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8411 | result, input_bfd)) |
d9352518 DB |
8412 | { |
8413 | undefined_reference ("symbol", symbuf); | |
8414 | return FALSE; | |
8415 | } | |
8416 | } | |
8417 | ||
8418 | return TRUE; | |
a0c8462f | 8419 | |
d9352518 DB |
8420 | /* All that remains are operators. */ |
8421 | ||
8422 | #define UNARY_OP(op) \ | |
8423 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8424 | { \ | |
8425 | sym += strlen (#op); \ | |
a0c8462f AM |
8426 | if (*sym == ':') \ |
8427 | ++sym; \ | |
0f02bbd9 | 8428 | *symp = sym; \ |
8b127cbc | 8429 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8430 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8431 | return FALSE; \ |
8432 | if (signed_p) \ | |
0f02bbd9 | 8433 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8434 | else \ |
8435 | *result = op a; \ | |
d9352518 DB |
8436 | return TRUE; \ |
8437 | } | |
8438 | ||
8439 | #define BINARY_OP(op) \ | |
8440 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8441 | { \ | |
8442 | sym += strlen (#op); \ | |
a0c8462f AM |
8443 | if (*sym == ':') \ |
8444 | ++sym; \ | |
0f02bbd9 | 8445 | *symp = sym; \ |
8b127cbc | 8446 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8447 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8448 | return FALSE; \ |
0f02bbd9 | 8449 | ++*symp; \ |
8b127cbc | 8450 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8451 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8452 | return FALSE; \ |
8453 | if (signed_p) \ | |
0f02bbd9 | 8454 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8455 | else \ |
8456 | *result = a op b; \ | |
d9352518 DB |
8457 | return TRUE; \ |
8458 | } | |
8459 | ||
8460 | default: | |
8461 | UNARY_OP (0-); | |
8462 | BINARY_OP (<<); | |
8463 | BINARY_OP (>>); | |
8464 | BINARY_OP (==); | |
8465 | BINARY_OP (!=); | |
8466 | BINARY_OP (<=); | |
8467 | BINARY_OP (>=); | |
8468 | BINARY_OP (&&); | |
8469 | BINARY_OP (||); | |
8470 | UNARY_OP (~); | |
8471 | UNARY_OP (!); | |
8472 | BINARY_OP (*); | |
8473 | BINARY_OP (/); | |
8474 | BINARY_OP (%); | |
8475 | BINARY_OP (^); | |
8476 | BINARY_OP (|); | |
8477 | BINARY_OP (&); | |
8478 | BINARY_OP (+); | |
8479 | BINARY_OP (-); | |
8480 | BINARY_OP (<); | |
8481 | BINARY_OP (>); | |
8482 | #undef UNARY_OP | |
8483 | #undef BINARY_OP | |
8484 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8485 | bfd_set_error (bfd_error_invalid_operation); | |
8486 | return FALSE; | |
8487 | } | |
8488 | } | |
8489 | ||
d9352518 | 8490 | static void |
a0c8462f AM |
8491 | put_value (bfd_vma size, |
8492 | unsigned long chunksz, | |
8493 | bfd *input_bfd, | |
8494 | bfd_vma x, | |
8495 | bfd_byte *location) | |
d9352518 DB |
8496 | { |
8497 | location += (size - chunksz); | |
8498 | ||
41cd1ad1 | 8499 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8500 | { |
8501 | switch (chunksz) | |
8502 | { | |
d9352518 DB |
8503 | case 1: |
8504 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8505 | x >>= 8; |
d9352518 DB |
8506 | break; |
8507 | case 2: | |
8508 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8509 | x >>= 16; |
d9352518 DB |
8510 | break; |
8511 | case 4: | |
8512 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8513 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8514 | x >>= 16; | |
8515 | x >>= 16; | |
d9352518 | 8516 | break; |
d9352518 | 8517 | #ifdef BFD64 |
41cd1ad1 | 8518 | case 8: |
d9352518 | 8519 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8520 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8521 | x >>= 32; | |
8522 | x >>= 32; | |
8523 | break; | |
d9352518 | 8524 | #endif |
41cd1ad1 NC |
8525 | default: |
8526 | abort (); | |
d9352518 DB |
8527 | break; |
8528 | } | |
8529 | } | |
8530 | } | |
8531 | ||
a0c8462f AM |
8532 | static bfd_vma |
8533 | get_value (bfd_vma size, | |
8534 | unsigned long chunksz, | |
8535 | bfd *input_bfd, | |
8536 | bfd_byte *location) | |
d9352518 | 8537 | { |
9b239e0e | 8538 | int shift; |
d9352518 DB |
8539 | bfd_vma x = 0; |
8540 | ||
9b239e0e NC |
8541 | /* Sanity checks. */ |
8542 | BFD_ASSERT (chunksz <= sizeof (x) | |
8543 | && size >= chunksz | |
8544 | && chunksz != 0 | |
8545 | && (size % chunksz) == 0 | |
8546 | && input_bfd != NULL | |
8547 | && location != NULL); | |
8548 | ||
8549 | if (chunksz == sizeof (x)) | |
8550 | { | |
8551 | BFD_ASSERT (size == chunksz); | |
8552 | ||
8553 | /* Make sure that we do not perform an undefined shift operation. | |
8554 | We know that size == chunksz so there will only be one iteration | |
8555 | of the loop below. */ | |
8556 | shift = 0; | |
8557 | } | |
8558 | else | |
8559 | shift = 8 * chunksz; | |
8560 | ||
a0c8462f | 8561 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8562 | { |
8563 | switch (chunksz) | |
8564 | { | |
d9352518 | 8565 | case 1: |
9b239e0e | 8566 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8567 | break; |
8568 | case 2: | |
9b239e0e | 8569 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8570 | break; |
8571 | case 4: | |
9b239e0e | 8572 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8573 | break; |
d9352518 | 8574 | #ifdef BFD64 |
9b239e0e NC |
8575 | case 8: |
8576 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8577 | break; |
9b239e0e NC |
8578 | #endif |
8579 | default: | |
8580 | abort (); | |
d9352518 DB |
8581 | } |
8582 | } | |
8583 | return x; | |
8584 | } | |
8585 | ||
a0c8462f AM |
8586 | static void |
8587 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8588 | unsigned long *oplen, /* in bits */ | |
8589 | unsigned long *len, /* in bits */ | |
8590 | unsigned long *wordsz, /* in bytes */ | |
8591 | unsigned long *chunksz, /* in bytes */ | |
8592 | unsigned long *lsb0_p, | |
8593 | unsigned long *signed_p, | |
8594 | unsigned long *trunc_p, | |
8595 | unsigned long encoded) | |
d9352518 | 8596 | { |
07d6d2b8 AM |
8597 | * start = encoded & 0x3F; |
8598 | * len = (encoded >> 6) & 0x3F; | |
d9352518 DB |
8599 | * oplen = (encoded >> 12) & 0x3F; |
8600 | * wordsz = (encoded >> 18) & 0xF; | |
8601 | * chunksz = (encoded >> 22) & 0xF; | |
8602 | * lsb0_p = (encoded >> 27) & 1; | |
8603 | * signed_p = (encoded >> 28) & 1; | |
8604 | * trunc_p = (encoded >> 29) & 1; | |
8605 | } | |
8606 | ||
cdfeee4f | 8607 | bfd_reloc_status_type |
0f02bbd9 | 8608 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8609 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8610 | bfd_byte *contents, |
8611 | Elf_Internal_Rela *rel, | |
8612 | bfd_vma relocation) | |
d9352518 | 8613 | { |
0f02bbd9 AM |
8614 | bfd_vma shift, x, mask; |
8615 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8616 | bfd_reloc_status_type r; |
d9352518 DB |
8617 | |
8618 | /* Perform this reloc, since it is complex. | |
8619 | (this is not to say that it necessarily refers to a complex | |
8620 | symbol; merely that it is a self-describing CGEN based reloc. | |
8621 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8622 | word size, etc) encoded within it.). */ |
d9352518 | 8623 | |
a0c8462f AM |
8624 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8625 | &chunksz, &lsb0_p, &signed_p, | |
8626 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8627 | |
8628 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8629 | ||
8630 | if (lsb0_p) | |
8631 | shift = (start + 1) - len; | |
8632 | else | |
8633 | shift = (8 * wordsz) - (start + len); | |
8634 | ||
37b01f6a DG |
8635 | x = get_value (wordsz, chunksz, input_bfd, |
8636 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8637 | |
8638 | #ifdef DEBUG | |
8639 | printf ("Doing complex reloc: " | |
8640 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8641 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8642 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8643 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8644 | oplen, (unsigned long) x, (unsigned long) mask, |
8645 | (unsigned long) relocation); | |
d9352518 DB |
8646 | #endif |
8647 | ||
cdfeee4f | 8648 | r = bfd_reloc_ok; |
d9352518 | 8649 | if (! trunc_p) |
cdfeee4f AM |
8650 | /* Now do an overflow check. */ |
8651 | r = bfd_check_overflow ((signed_p | |
8652 | ? complain_overflow_signed | |
8653 | : complain_overflow_unsigned), | |
8654 | len, 0, (8 * wordsz), | |
8655 | relocation); | |
a0c8462f | 8656 | |
d9352518 DB |
8657 | /* Do the deed. */ |
8658 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8659 | ||
8660 | #ifdef DEBUG | |
8661 | printf (" relocation: %8.8lx\n" | |
8662 | " shifted mask: %8.8lx\n" | |
8663 | " shifted/masked reloc: %8.8lx\n" | |
8664 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8665 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8666 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8667 | #endif |
37b01f6a DG |
8668 | put_value (wordsz, chunksz, input_bfd, x, |
8669 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8670 | return r; |
d9352518 DB |
8671 | } |
8672 | ||
0e287786 AM |
8673 | /* Functions to read r_offset from external (target order) reloc |
8674 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8675 | know the value is aligned. */ | |
53df40a4 | 8676 | |
0e287786 AM |
8677 | static bfd_vma |
8678 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8679 | { |
8680 | union aligned32 | |
8681 | { | |
8682 | uint32_t v; | |
8683 | unsigned char c[4]; | |
8684 | }; | |
8685 | const union aligned32 *a | |
0e287786 | 8686 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8687 | |
8688 | uint32_t aval = ( (uint32_t) a->c[0] | |
8689 | | (uint32_t) a->c[1] << 8 | |
8690 | | (uint32_t) a->c[2] << 16 | |
8691 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8692 | return aval; |
53df40a4 AM |
8693 | } |
8694 | ||
0e287786 AM |
8695 | static bfd_vma |
8696 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8697 | { |
8698 | union aligned32 | |
8699 | { | |
8700 | uint32_t v; | |
8701 | unsigned char c[4]; | |
8702 | }; | |
8703 | const union aligned32 *a | |
0e287786 | 8704 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8705 | |
8706 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8707 | | (uint32_t) a->c[1] << 16 | |
8708 | | (uint32_t) a->c[2] << 8 | |
8709 | | (uint32_t) a->c[3]); | |
0e287786 | 8710 | return aval; |
53df40a4 AM |
8711 | } |
8712 | ||
8713 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8714 | static bfd_vma |
8715 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8716 | { |
8717 | union aligned64 | |
8718 | { | |
8719 | uint64_t v; | |
8720 | unsigned char c[8]; | |
8721 | }; | |
8722 | const union aligned64 *a | |
0e287786 | 8723 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8724 | |
8725 | uint64_t aval = ( (uint64_t) a->c[0] | |
8726 | | (uint64_t) a->c[1] << 8 | |
8727 | | (uint64_t) a->c[2] << 16 | |
8728 | | (uint64_t) a->c[3] << 24 | |
8729 | | (uint64_t) a->c[4] << 32 | |
8730 | | (uint64_t) a->c[5] << 40 | |
8731 | | (uint64_t) a->c[6] << 48 | |
8732 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8733 | return aval; |
53df40a4 AM |
8734 | } |
8735 | ||
0e287786 AM |
8736 | static bfd_vma |
8737 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8738 | { |
8739 | union aligned64 | |
8740 | { | |
8741 | uint64_t v; | |
8742 | unsigned char c[8]; | |
8743 | }; | |
8744 | const union aligned64 *a | |
0e287786 | 8745 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8746 | |
8747 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8748 | | (uint64_t) a->c[1] << 48 | |
8749 | | (uint64_t) a->c[2] << 40 | |
8750 | | (uint64_t) a->c[3] << 32 | |
8751 | | (uint64_t) a->c[4] << 24 | |
8752 | | (uint64_t) a->c[5] << 16 | |
8753 | | (uint64_t) a->c[6] << 8 | |
8754 | | (uint64_t) a->c[7]); | |
0e287786 | 8755 | return aval; |
53df40a4 AM |
8756 | } |
8757 | #endif | |
8758 | ||
c152c796 AM |
8759 | /* When performing a relocatable link, the input relocations are |
8760 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8761 | referenced must be updated. Update all the relocations found in |
8762 | RELDATA. */ | |
c152c796 | 8763 | |
bca6d0e3 | 8764 | static bfd_boolean |
c152c796 | 8765 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 8766 | asection *sec, |
28dbcedc | 8767 | struct bfd_elf_section_reloc_data *reldata, |
10bbbc1d NC |
8768 | bfd_boolean sort, |
8769 | struct bfd_link_info *info) | |
c152c796 AM |
8770 | { |
8771 | unsigned int i; | |
8772 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8773 | bfd_byte *erela; | |
8774 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8775 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8776 | bfd_vma r_type_mask; | |
8777 | int r_sym_shift; | |
d4730f92 BS |
8778 | unsigned int count = reldata->count; |
8779 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8780 | |
d4730f92 | 8781 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8782 | { |
8783 | swap_in = bed->s->swap_reloc_in; | |
8784 | swap_out = bed->s->swap_reloc_out; | |
8785 | } | |
d4730f92 | 8786 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8787 | { |
8788 | swap_in = bed->s->swap_reloca_in; | |
8789 | swap_out = bed->s->swap_reloca_out; | |
8790 | } | |
8791 | else | |
8792 | abort (); | |
8793 | ||
8794 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8795 | abort (); | |
8796 | ||
8797 | if (bed->s->arch_size == 32) | |
8798 | { | |
8799 | r_type_mask = 0xff; | |
8800 | r_sym_shift = 8; | |
8801 | } | |
8802 | else | |
8803 | { | |
8804 | r_type_mask = 0xffffffff; | |
8805 | r_sym_shift = 32; | |
8806 | } | |
8807 | ||
d4730f92 BS |
8808 | erela = reldata->hdr->contents; |
8809 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8810 | { |
8811 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8812 | unsigned int j; | |
8813 | ||
8814 | if (*rel_hash == NULL) | |
8815 | continue; | |
8816 | ||
10bbbc1d NC |
8817 | if ((*rel_hash)->indx == -2 |
8818 | && info->gc_sections | |
8819 | && ! info->gc_keep_exported) | |
8820 | { | |
8821 | /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ | |
9793eb77 | 8822 | _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"), |
10bbbc1d NC |
8823 | abfd, sec, |
8824 | (*rel_hash)->root.root.string); | |
9793eb77 | 8825 | _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"), |
d42c267e | 8826 | abfd, sec); |
10bbbc1d NC |
8827 | bfd_set_error (bfd_error_invalid_operation); |
8828 | return FALSE; | |
8829 | } | |
c152c796 AM |
8830 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
8831 | ||
8832 | (*swap_in) (abfd, erela, irela); | |
8833 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8834 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8835 | | (irela[j].r_info & r_type_mask)); | |
8836 | (*swap_out) (abfd, irela, erela); | |
8837 | } | |
53df40a4 | 8838 | |
9eaff861 AO |
8839 | if (bed->elf_backend_update_relocs) |
8840 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
8841 | ||
0e287786 | 8842 | if (sort && count != 0) |
53df40a4 | 8843 | { |
0e287786 AM |
8844 | bfd_vma (*ext_r_off) (const void *); |
8845 | bfd_vma r_off; | |
8846 | size_t elt_size; | |
8847 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8848 | bfd_byte *buf = NULL; |
28dbcedc AM |
8849 | |
8850 | if (bed->s->arch_size == 32) | |
8851 | { | |
8852 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8853 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8854 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8855 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8856 | else |
8857 | abort (); | |
8858 | } | |
53df40a4 | 8859 | else |
28dbcedc | 8860 | { |
53df40a4 | 8861 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8862 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8863 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8864 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8865 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8866 | else |
53df40a4 | 8867 | #endif |
28dbcedc AM |
8868 | abort (); |
8869 | } | |
0e287786 | 8870 | |
bca6d0e3 AM |
8871 | /* Must use a stable sort here. A modified insertion sort, |
8872 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8873 | elt_size = reldata->hdr->sh_entsize; |
8874 | base = reldata->hdr->contents; | |
8875 | end = base + count * elt_size; | |
bca6d0e3 | 8876 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8877 | abort (); |
8878 | ||
8879 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8880 | speeding the main loop below. */ | |
8881 | r_off = (*ext_r_off) (base); | |
8882 | for (p = loc = base; (p += elt_size) < end; ) | |
8883 | { | |
8884 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8885 | if (r_off > r_off2) | |
8886 | { | |
8887 | r_off = r_off2; | |
8888 | loc = p; | |
8889 | } | |
8890 | } | |
8891 | if (loc != base) | |
8892 | { | |
8893 | /* Don't just swap *base and *loc as that changes the order | |
8894 | of the original base[0] and base[1] if they happen to | |
8895 | have the same r_offset. */ | |
bca6d0e3 AM |
8896 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8897 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8898 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8899 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8900 | } |
8901 | ||
b29b8669 | 8902 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8903 | { |
8904 | /* base to p is sorted, *p is next to insert. */ | |
8905 | r_off = (*ext_r_off) (p); | |
8906 | /* Search the sorted region for location to insert. */ | |
8907 | loc = p - elt_size; | |
8908 | while (r_off < (*ext_r_off) (loc)) | |
8909 | loc -= elt_size; | |
8910 | loc += elt_size; | |
8911 | if (loc != p) | |
8912 | { | |
bca6d0e3 AM |
8913 | /* Chances are there is a run of relocs to insert here, |
8914 | from one of more input files. Files are not always | |
8915 | linked in order due to the way elf_link_input_bfd is | |
8916 | called. See pr17666. */ | |
8917 | size_t sortlen = p - loc; | |
8918 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8919 | size_t runlen = elt_size; | |
8920 | size_t buf_size = 96 * 1024; | |
8921 | while (p + runlen < end | |
8922 | && (sortlen <= buf_size | |
8923 | || runlen + elt_size <= buf_size) | |
8924 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8925 | runlen += elt_size; | |
8926 | if (buf == NULL) | |
8927 | { | |
8928 | buf = bfd_malloc (buf_size); | |
8929 | if (buf == NULL) | |
8930 | return FALSE; | |
8931 | } | |
8932 | if (runlen < sortlen) | |
8933 | { | |
8934 | memcpy (buf, p, runlen); | |
8935 | memmove (loc + runlen, loc, sortlen); | |
8936 | memcpy (loc, buf, runlen); | |
8937 | } | |
8938 | else | |
8939 | { | |
8940 | memcpy (buf, loc, sortlen); | |
8941 | memmove (loc, p, runlen); | |
8942 | memcpy (loc + runlen, buf, sortlen); | |
8943 | } | |
b29b8669 | 8944 | p += runlen - elt_size; |
0e287786 AM |
8945 | } |
8946 | } | |
8947 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
8948 | free (reldata->hashes); |
8949 | reldata->hashes = NULL; | |
bca6d0e3 | 8950 | free (buf); |
53df40a4 | 8951 | } |
bca6d0e3 | 8952 | return TRUE; |
c152c796 AM |
8953 | } |
8954 | ||
8955 | struct elf_link_sort_rela | |
8956 | { | |
8957 | union { | |
8958 | bfd_vma offset; | |
8959 | bfd_vma sym_mask; | |
8960 | } u; | |
8961 | enum elf_reloc_type_class type; | |
8962 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8963 | Elf_Internal_Rela rela[1]; | |
8964 | }; | |
8965 | ||
8966 | static int | |
8967 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8968 | { | |
a50b1753 NC |
8969 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8970 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8971 | int relativea, relativeb; |
8972 | ||
8973 | relativea = a->type == reloc_class_relative; | |
8974 | relativeb = b->type == reloc_class_relative; | |
8975 | ||
8976 | if (relativea < relativeb) | |
8977 | return 1; | |
8978 | if (relativea > relativeb) | |
8979 | return -1; | |
8980 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
8981 | return -1; | |
8982 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
8983 | return 1; | |
8984 | if (a->rela->r_offset < b->rela->r_offset) | |
8985 | return -1; | |
8986 | if (a->rela->r_offset > b->rela->r_offset) | |
8987 | return 1; | |
8988 | return 0; | |
8989 | } | |
8990 | ||
8991 | static int | |
8992 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8993 | { | |
a50b1753 NC |
8994 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8995 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 8996 | |
7e612e98 | 8997 | if (a->type < b->type) |
c152c796 | 8998 | return -1; |
7e612e98 | 8999 | if (a->type > b->type) |
c152c796 | 9000 | return 1; |
7e612e98 | 9001 | if (a->u.offset < b->u.offset) |
c152c796 | 9002 | return -1; |
7e612e98 | 9003 | if (a->u.offset > b->u.offset) |
c152c796 AM |
9004 | return 1; |
9005 | if (a->rela->r_offset < b->rela->r_offset) | |
9006 | return -1; | |
9007 | if (a->rela->r_offset > b->rela->r_offset) | |
9008 | return 1; | |
9009 | return 0; | |
9010 | } | |
9011 | ||
9012 | static size_t | |
9013 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
9014 | { | |
3410fea8 | 9015 | asection *dynamic_relocs; |
fc66a176 L |
9016 | asection *rela_dyn; |
9017 | asection *rel_dyn; | |
c152c796 AM |
9018 | bfd_size_type count, size; |
9019 | size_t i, ret, sort_elt, ext_size; | |
9020 | bfd_byte *sort, *s_non_relative, *p; | |
9021 | struct elf_link_sort_rela *sq; | |
9022 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9023 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 9024 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
9025 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
9026 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
9027 | struct bfd_link_order *lo; | |
9028 | bfd_vma r_sym_mask; | |
3410fea8 | 9029 | bfd_boolean use_rela; |
c152c796 | 9030 | |
3410fea8 NC |
9031 | /* Find a dynamic reloc section. */ |
9032 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
9033 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
9034 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
9035 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 9036 | { |
3410fea8 NC |
9037 | bfd_boolean use_rela_initialised = FALSE; |
9038 | ||
9039 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 9040 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
9041 | use_rela = TRUE; |
9042 | ||
9043 | /* Both sections are present. Examine the sizes | |
9044 | of the indirect sections to help us choose. */ | |
9045 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9046 | if (lo->type == bfd_indirect_link_order) | |
9047 | { | |
9048 | asection *o = lo->u.indirect.section; | |
9049 | ||
9050 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9051 | { | |
9052 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9053 | /* Section size is divisible by both rel and rela sizes. | |
9054 | It is of no help to us. */ | |
9055 | ; | |
9056 | else | |
9057 | { | |
9058 | /* Section size is only divisible by rela. */ | |
535b785f | 9059 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9060 | { |
9793eb77 | 9061 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9062 | "they are in more than one size"), |
9063 | abfd); | |
3410fea8 NC |
9064 | bfd_set_error (bfd_error_invalid_operation); |
9065 | return 0; | |
9066 | } | |
9067 | else | |
9068 | { | |
9069 | use_rela = TRUE; | |
9070 | use_rela_initialised = TRUE; | |
9071 | } | |
9072 | } | |
9073 | } | |
9074 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9075 | { | |
9076 | /* Section size is only divisible by rel. */ | |
535b785f | 9077 | if (use_rela_initialised && use_rela) |
3410fea8 | 9078 | { |
9793eb77 | 9079 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9080 | "they are in more than one size"), |
9081 | abfd); | |
3410fea8 NC |
9082 | bfd_set_error (bfd_error_invalid_operation); |
9083 | return 0; | |
9084 | } | |
9085 | else | |
9086 | { | |
9087 | use_rela = FALSE; | |
9088 | use_rela_initialised = TRUE; | |
9089 | } | |
9090 | } | |
9091 | else | |
9092 | { | |
c8e44c6d AM |
9093 | /* The section size is not divisible by either - |
9094 | something is wrong. */ | |
9793eb77 | 9095 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9096 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9097 | bfd_set_error (bfd_error_invalid_operation); |
9098 | return 0; | |
9099 | } | |
9100 | } | |
9101 | ||
9102 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9103 | if (lo->type == bfd_indirect_link_order) | |
9104 | { | |
9105 | asection *o = lo->u.indirect.section; | |
9106 | ||
9107 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9108 | { | |
9109 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9110 | /* Section size is divisible by both rel and rela sizes. | |
9111 | It is of no help to us. */ | |
9112 | ; | |
9113 | else | |
9114 | { | |
9115 | /* Section size is only divisible by rela. */ | |
535b785f | 9116 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9117 | { |
9793eb77 | 9118 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9119 | "they are in more than one size"), |
9120 | abfd); | |
3410fea8 NC |
9121 | bfd_set_error (bfd_error_invalid_operation); |
9122 | return 0; | |
9123 | } | |
9124 | else | |
9125 | { | |
9126 | use_rela = TRUE; | |
9127 | use_rela_initialised = TRUE; | |
9128 | } | |
9129 | } | |
9130 | } | |
9131 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9132 | { | |
9133 | /* Section size is only divisible by rel. */ | |
535b785f | 9134 | if (use_rela_initialised && use_rela) |
3410fea8 | 9135 | { |
9793eb77 | 9136 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9137 | "they are in more than one size"), |
9138 | abfd); | |
3410fea8 NC |
9139 | bfd_set_error (bfd_error_invalid_operation); |
9140 | return 0; | |
9141 | } | |
9142 | else | |
9143 | { | |
9144 | use_rela = FALSE; | |
9145 | use_rela_initialised = TRUE; | |
9146 | } | |
9147 | } | |
9148 | else | |
9149 | { | |
c8e44c6d AM |
9150 | /* The section size is not divisible by either - |
9151 | something is wrong. */ | |
9793eb77 | 9152 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9153 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9154 | bfd_set_error (bfd_error_invalid_operation); |
9155 | return 0; | |
9156 | } | |
9157 | } | |
9158 | ||
9159 | if (! use_rela_initialised) | |
9160 | /* Make a guess. */ | |
9161 | use_rela = TRUE; | |
c152c796 | 9162 | } |
fc66a176 L |
9163 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
9164 | use_rela = TRUE; | |
9165 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 9166 | use_rela = FALSE; |
c152c796 | 9167 | else |
fc66a176 | 9168 | return 0; |
3410fea8 NC |
9169 | |
9170 | if (use_rela) | |
c152c796 | 9171 | { |
3410fea8 | 9172 | dynamic_relocs = rela_dyn; |
c152c796 AM |
9173 | ext_size = bed->s->sizeof_rela; |
9174 | swap_in = bed->s->swap_reloca_in; | |
9175 | swap_out = bed->s->swap_reloca_out; | |
9176 | } | |
3410fea8 NC |
9177 | else |
9178 | { | |
9179 | dynamic_relocs = rel_dyn; | |
9180 | ext_size = bed->s->sizeof_rel; | |
9181 | swap_in = bed->s->swap_reloc_in; | |
9182 | swap_out = bed->s->swap_reloc_out; | |
9183 | } | |
c152c796 AM |
9184 | |
9185 | size = 0; | |
3410fea8 | 9186 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 9187 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 9188 | size += lo->u.indirect.section->size; |
c152c796 | 9189 | |
3410fea8 | 9190 | if (size != dynamic_relocs->size) |
c152c796 AM |
9191 | return 0; |
9192 | ||
9193 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
9194 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
9195 | |
9196 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
9197 | if (count == 0) |
9198 | return 0; | |
a50b1753 | 9199 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 9200 | |
c152c796 AM |
9201 | if (sort == NULL) |
9202 | { | |
9203 | (*info->callbacks->warning) | |
9793eb77 | 9204 | (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0); |
c152c796 AM |
9205 | return 0; |
9206 | } | |
9207 | ||
9208 | if (bed->s->arch_size == 32) | |
9209 | r_sym_mask = ~(bfd_vma) 0xff; | |
9210 | else | |
9211 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
9212 | ||
3410fea8 | 9213 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9214 | if (lo->type == bfd_indirect_link_order) |
9215 | { | |
9216 | bfd_byte *erel, *erelend; | |
9217 | asection *o = lo->u.indirect.section; | |
9218 | ||
1da212d6 AM |
9219 | if (o->contents == NULL && o->size != 0) |
9220 | { | |
9221 | /* This is a reloc section that is being handled as a normal | |
9222 | section. See bfd_section_from_shdr. We can't combine | |
9223 | relocs in this case. */ | |
9224 | free (sort); | |
9225 | return 0; | |
9226 | } | |
c152c796 | 9227 | erel = o->contents; |
eea6121a | 9228 | erelend = o->contents + o->size; |
c8e44c6d | 9229 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 9230 | |
c152c796 AM |
9231 | while (erel < erelend) |
9232 | { | |
9233 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 9234 | |
c152c796 | 9235 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 9236 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
9237 | s->u.sym_mask = r_sym_mask; |
9238 | p += sort_elt; | |
9239 | erel += ext_size; | |
9240 | } | |
9241 | } | |
9242 | ||
9243 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
9244 | ||
9245 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
9246 | { | |
9247 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9248 | if (s->type != reloc_class_relative) | |
9249 | break; | |
9250 | } | |
9251 | ret = i; | |
9252 | s_non_relative = p; | |
9253 | ||
9254 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
9255 | for (; i < count; i++, p += sort_elt) | |
9256 | { | |
9257 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
9258 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
9259 | sq = sp; | |
9260 | sp->u.offset = sq->rela->r_offset; | |
9261 | } | |
9262 | ||
9263 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
9264 | ||
c8e44c6d AM |
9265 | struct elf_link_hash_table *htab = elf_hash_table (info); |
9266 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
9267 | { | |
9268 | /* We have plt relocs in .rela.dyn. */ | |
9269 | sq = (struct elf_link_sort_rela *) sort; | |
9270 | for (i = 0; i < count; i++) | |
9271 | if (sq[count - i - 1].type != reloc_class_plt) | |
9272 | break; | |
9273 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
9274 | { | |
9275 | struct bfd_link_order **plo; | |
9276 | /* Put srelplt link_order last. This is so the output_offset | |
9277 | set in the next loop is correct for DT_JMPREL. */ | |
9278 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
9279 | if ((*plo)->type == bfd_indirect_link_order | |
9280 | && (*plo)->u.indirect.section == htab->srelplt) | |
9281 | { | |
9282 | lo = *plo; | |
9283 | *plo = lo->next; | |
9284 | } | |
9285 | else | |
9286 | plo = &(*plo)->next; | |
9287 | *plo = lo; | |
9288 | lo->next = NULL; | |
9289 | dynamic_relocs->map_tail.link_order = lo; | |
9290 | } | |
9291 | } | |
9292 | ||
9293 | p = sort; | |
3410fea8 | 9294 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9295 | if (lo->type == bfd_indirect_link_order) |
9296 | { | |
9297 | bfd_byte *erel, *erelend; | |
9298 | asection *o = lo->u.indirect.section; | |
9299 | ||
9300 | erel = o->contents; | |
eea6121a | 9301 | erelend = o->contents + o->size; |
c8e44c6d | 9302 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
9303 | while (erel < erelend) |
9304 | { | |
9305 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9306 | (*swap_out) (abfd, s->rela, erel); | |
9307 | p += sort_elt; | |
9308 | erel += ext_size; | |
9309 | } | |
9310 | } | |
9311 | ||
9312 | free (sort); | |
3410fea8 | 9313 | *psec = dynamic_relocs; |
c152c796 AM |
9314 | return ret; |
9315 | } | |
9316 | ||
ef10c3ac | 9317 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 9318 | |
6e0b88f1 | 9319 | static int |
ef10c3ac L |
9320 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
9321 | const char *name, | |
9322 | Elf_Internal_Sym *elfsym, | |
9323 | asection *input_sec, | |
9324 | struct elf_link_hash_entry *h) | |
c152c796 | 9325 | { |
6e0b88f1 | 9326 | int (*output_symbol_hook) |
c152c796 AM |
9327 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
9328 | struct elf_link_hash_entry *); | |
ef10c3ac | 9329 | struct elf_link_hash_table *hash_table; |
c152c796 | 9330 | const struct elf_backend_data *bed; |
ef10c3ac | 9331 | bfd_size_type strtabsize; |
c152c796 | 9332 | |
8539e4e8 AM |
9333 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
9334 | ||
8b127cbc | 9335 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
9336 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
9337 | if (output_symbol_hook != NULL) | |
9338 | { | |
8b127cbc | 9339 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
9340 | if (ret != 1) |
9341 | return ret; | |
c152c796 AM |
9342 | } |
9343 | ||
ef10c3ac L |
9344 | if (name == NULL |
9345 | || *name == '\0' | |
9346 | || (input_sec->flags & SEC_EXCLUDE)) | |
9347 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
9348 | else |
9349 | { | |
ef10c3ac L |
9350 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
9351 | to get the final offset for st_name. */ | |
9352 | elfsym->st_name | |
9353 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
9354 | name, FALSE); | |
c152c796 | 9355 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 9356 | return 0; |
c152c796 AM |
9357 | } |
9358 | ||
ef10c3ac L |
9359 | hash_table = elf_hash_table (flinfo->info); |
9360 | strtabsize = hash_table->strtabsize; | |
9361 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 9362 | { |
ef10c3ac L |
9363 | strtabsize += strtabsize; |
9364 | hash_table->strtabsize = strtabsize; | |
9365 | strtabsize *= sizeof (*hash_table->strtab); | |
9366 | hash_table->strtab | |
9367 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
9368 | strtabsize); | |
9369 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 9370 | return 0; |
c152c796 | 9371 | } |
ef10c3ac L |
9372 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
9373 | hash_table->strtab[hash_table->strtabcount].dest_index | |
9374 | = hash_table->strtabcount; | |
9375 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
9376 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
9377 | ||
9378 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
9379 | hash_table->strtabcount += 1; | |
9380 | ||
9381 | return 1; | |
9382 | } | |
9383 | ||
9384 | /* Swap symbols out to the symbol table and flush the output symbols to | |
9385 | the file. */ | |
9386 | ||
9387 | static bfd_boolean | |
9388 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
9389 | { | |
9390 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
9391 | bfd_size_type amt; |
9392 | size_t i; | |
ef10c3ac L |
9393 | const struct elf_backend_data *bed; |
9394 | bfd_byte *symbuf; | |
9395 | Elf_Internal_Shdr *hdr; | |
9396 | file_ptr pos; | |
9397 | bfd_boolean ret; | |
9398 | ||
9399 | if (!hash_table->strtabcount) | |
9400 | return TRUE; | |
9401 | ||
9402 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
9403 | ||
9404 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 9405 | |
ef10c3ac L |
9406 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
9407 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
9408 | if (symbuf == NULL) | |
9409 | return FALSE; | |
1b786873 | 9410 | |
ef10c3ac | 9411 | if (flinfo->symshndxbuf) |
c152c796 | 9412 | { |
ef53be89 AM |
9413 | amt = sizeof (Elf_External_Sym_Shndx); |
9414 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
9415 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9416 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 9417 | { |
ef10c3ac L |
9418 | free (symbuf); |
9419 | return FALSE; | |
c152c796 | 9420 | } |
c152c796 AM |
9421 | } |
9422 | ||
ef10c3ac L |
9423 | for (i = 0; i < hash_table->strtabcount; i++) |
9424 | { | |
9425 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
9426 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9427 | elfsym->sym.st_name = 0; | |
9428 | else | |
9429 | elfsym->sym.st_name | |
9430 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9431 | elfsym->sym.st_name); | |
9432 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9433 | ((bfd_byte *) symbuf | |
9434 | + (elfsym->dest_index | |
9435 | * bed->s->sizeof_sym)), | |
9436 | (flinfo->symshndxbuf | |
9437 | + elfsym->destshndx_index)); | |
9438 | } | |
9439 | ||
9440 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9441 | pos = hdr->sh_offset + hdr->sh_size; | |
9442 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9443 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9444 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9445 | { | |
9446 | hdr->sh_size += amt; | |
9447 | ret = TRUE; | |
9448 | } | |
9449 | else | |
9450 | ret = FALSE; | |
c152c796 | 9451 | |
ef10c3ac L |
9452 | free (symbuf); |
9453 | ||
9454 | free (hash_table->strtab); | |
9455 | hash_table->strtab = NULL; | |
9456 | ||
9457 | return ret; | |
c152c796 AM |
9458 | } |
9459 | ||
c0d5a53d L |
9460 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9461 | ||
9462 | static bfd_boolean | |
9463 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9464 | { | |
4fbb74a6 AM |
9465 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9466 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9467 | { |
9468 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9469 | beyond 64k. */ |
4eca0228 | 9470 | _bfd_error_handler |
695344c0 | 9471 | /* xgettext:c-format */ |
9793eb77 | 9472 | (_("%pB: too many sections: %d (>= %d)"), |
4fbb74a6 | 9473 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9474 | bfd_set_error (bfd_error_nonrepresentable_section); |
9475 | return FALSE; | |
9476 | } | |
9477 | return TRUE; | |
9478 | } | |
9479 | ||
c152c796 AM |
9480 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9481 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9482 | versioned symbol that would normally require an explicit version. | |
9483 | We also handle the case that a DSO references a hidden symbol | |
9484 | which may be satisfied by a versioned symbol in another DSO. */ | |
9485 | ||
9486 | static bfd_boolean | |
9487 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9488 | const struct elf_backend_data *bed, | |
9489 | struct elf_link_hash_entry *h) | |
9490 | { | |
9491 | bfd *abfd; | |
9492 | struct elf_link_loaded_list *loaded; | |
9493 | ||
9494 | if (!is_elf_hash_table (info->hash)) | |
9495 | return FALSE; | |
9496 | ||
90c984fc L |
9497 | /* Check indirect symbol. */ |
9498 | while (h->root.type == bfd_link_hash_indirect) | |
9499 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9500 | ||
c152c796 AM |
9501 | switch (h->root.type) |
9502 | { | |
9503 | default: | |
9504 | abfd = NULL; | |
9505 | break; | |
9506 | ||
9507 | case bfd_link_hash_undefined: | |
9508 | case bfd_link_hash_undefweak: | |
9509 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9510 | if (abfd == NULL |
9511 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9512 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9513 | return FALSE; |
9514 | break; | |
9515 | ||
9516 | case bfd_link_hash_defined: | |
9517 | case bfd_link_hash_defweak: | |
9518 | abfd = h->root.u.def.section->owner; | |
9519 | break; | |
9520 | ||
9521 | case bfd_link_hash_common: | |
9522 | abfd = h->root.u.c.p->section->owner; | |
9523 | break; | |
9524 | } | |
9525 | BFD_ASSERT (abfd != NULL); | |
9526 | ||
9527 | for (loaded = elf_hash_table (info)->loaded; | |
9528 | loaded != NULL; | |
9529 | loaded = loaded->next) | |
9530 | { | |
9531 | bfd *input; | |
9532 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9533 | size_t symcount; |
9534 | size_t extsymcount; | |
9535 | size_t extsymoff; | |
c152c796 AM |
9536 | Elf_Internal_Shdr *versymhdr; |
9537 | Elf_Internal_Sym *isym; | |
9538 | Elf_Internal_Sym *isymend; | |
9539 | Elf_Internal_Sym *isymbuf; | |
9540 | Elf_External_Versym *ever; | |
9541 | Elf_External_Versym *extversym; | |
9542 | ||
9543 | input = loaded->abfd; | |
9544 | ||
9545 | /* We check each DSO for a possible hidden versioned definition. */ | |
9546 | if (input == abfd | |
9547 | || (input->flags & DYNAMIC) == 0 | |
9548 | || elf_dynversym (input) == 0) | |
9549 | continue; | |
9550 | ||
9551 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9552 | ||
9553 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9554 | if (elf_bad_symtab (input)) | |
9555 | { | |
9556 | extsymcount = symcount; | |
9557 | extsymoff = 0; | |
9558 | } | |
9559 | else | |
9560 | { | |
9561 | extsymcount = symcount - hdr->sh_info; | |
9562 | extsymoff = hdr->sh_info; | |
9563 | } | |
9564 | ||
9565 | if (extsymcount == 0) | |
9566 | continue; | |
9567 | ||
9568 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9569 | NULL, NULL, NULL); | |
9570 | if (isymbuf == NULL) | |
9571 | return FALSE; | |
9572 | ||
9573 | /* Read in any version definitions. */ | |
9574 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9575 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9576 | if (extversym == NULL) |
9577 | goto error_ret; | |
9578 | ||
9579 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9580 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9581 | != versymhdr->sh_size)) | |
9582 | { | |
9583 | free (extversym); | |
9584 | error_ret: | |
9585 | free (isymbuf); | |
9586 | return FALSE; | |
9587 | } | |
9588 | ||
9589 | ever = extversym + extsymoff; | |
9590 | isymend = isymbuf + extsymcount; | |
9591 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9592 | { | |
9593 | const char *name; | |
9594 | Elf_Internal_Versym iver; | |
9595 | unsigned short version_index; | |
9596 | ||
9597 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9598 | || isym->st_shndx == SHN_UNDEF) | |
9599 | continue; | |
9600 | ||
9601 | name = bfd_elf_string_from_elf_section (input, | |
9602 | hdr->sh_link, | |
9603 | isym->st_name); | |
9604 | if (strcmp (name, h->root.root.string) != 0) | |
9605 | continue; | |
9606 | ||
9607 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9608 | ||
d023c380 L |
9609 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9610 | && !(h->def_regular | |
9611 | && h->forced_local)) | |
c152c796 AM |
9612 | { |
9613 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9614 | have provided a definition for the undefined sym unless |
9615 | it is defined in a non-shared object and forced local. | |
9616 | */ | |
c152c796 AM |
9617 | abort (); |
9618 | } | |
9619 | ||
9620 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9621 | if (version_index == 1 || version_index == 2) | |
9622 | { | |
9623 | /* This is the base or first version. We can use it. */ | |
9624 | free (extversym); | |
9625 | free (isymbuf); | |
9626 | return TRUE; | |
9627 | } | |
9628 | } | |
9629 | ||
9630 | free (extversym); | |
9631 | free (isymbuf); | |
9632 | } | |
9633 | ||
9634 | return FALSE; | |
9635 | } | |
9636 | ||
b8871f35 L |
9637 | /* Convert ELF common symbol TYPE. */ |
9638 | ||
9639 | static int | |
9640 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9641 | { | |
9642 | /* Commom symbol can only appear in relocatable link. */ | |
9643 | if (!bfd_link_relocatable (info)) | |
9644 | abort (); | |
9645 | switch (info->elf_stt_common) | |
9646 | { | |
9647 | case unchanged: | |
9648 | break; | |
9649 | case elf_stt_common: | |
9650 | type = STT_COMMON; | |
9651 | break; | |
9652 | case no_elf_stt_common: | |
9653 | type = STT_OBJECT; | |
9654 | break; | |
9655 | } | |
9656 | return type; | |
9657 | } | |
9658 | ||
c152c796 AM |
9659 | /* Add an external symbol to the symbol table. This is called from |
9660 | the hash table traversal routine. When generating a shared object, | |
9661 | we go through the symbol table twice. The first time we output | |
9662 | anything that might have been forced to local scope in a version | |
9663 | script. The second time we output the symbols that are still | |
9664 | global symbols. */ | |
9665 | ||
9666 | static bfd_boolean | |
7686d77d | 9667 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9668 | { |
7686d77d | 9669 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9670 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9671 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9672 | bfd_boolean strip; |
9673 | Elf_Internal_Sym sym; | |
9674 | asection *input_sec; | |
9675 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9676 | long indx; |
9677 | int ret; | |
b8871f35 | 9678 | unsigned int type; |
c152c796 AM |
9679 | |
9680 | if (h->root.type == bfd_link_hash_warning) | |
9681 | { | |
9682 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9683 | if (h->root.type == bfd_link_hash_new) | |
9684 | return TRUE; | |
9685 | } | |
9686 | ||
9687 | /* Decide whether to output this symbol in this pass. */ | |
9688 | if (eoinfo->localsyms) | |
9689 | { | |
4deb8f71 | 9690 | if (!h->forced_local) |
c152c796 AM |
9691 | return TRUE; |
9692 | } | |
9693 | else | |
9694 | { | |
4deb8f71 | 9695 | if (h->forced_local) |
c152c796 AM |
9696 | return TRUE; |
9697 | } | |
9698 | ||
8b127cbc | 9699 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9700 | |
12ac1cf5 | 9701 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9702 | { |
12ac1cf5 NC |
9703 | /* If we have an undefined symbol reference here then it must have |
9704 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9705 | references in regular files have already been handled unless |
9706 | they are in unreferenced sections which are removed by garbage | |
9707 | collection). */ | |
12ac1cf5 NC |
9708 | bfd_boolean ignore_undef = FALSE; |
9709 | ||
9710 | /* Some symbols may be special in that the fact that they're | |
9711 | undefined can be safely ignored - let backend determine that. */ | |
9712 | if (bed->elf_backend_ignore_undef_symbol) | |
9713 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9714 | ||
9715 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9716 | if (!ignore_undef |
12ac1cf5 | 9717 | && h->ref_dynamic |
8b127cbc AM |
9718 | && (!h->ref_regular || flinfo->info->gc_sections) |
9719 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9720 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9721 | (*flinfo->info->callbacks->undefined_symbol) |
9722 | (flinfo->info, h->root.root.string, | |
9723 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9724 | NULL, 0, | |
9725 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9726 | |
9727 | /* Strip a global symbol defined in a discarded section. */ | |
9728 | if (h->indx == -3) | |
9729 | return TRUE; | |
c152c796 AM |
9730 | } |
9731 | ||
9732 | /* We should also warn if a forced local symbol is referenced from | |
9733 | shared libraries. */ | |
0e1862bb | 9734 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9735 | && h->forced_local |
9736 | && h->ref_dynamic | |
371a5866 | 9737 | && h->def_regular |
f5385ebf | 9738 | && !h->dynamic_def |
ee659f1f | 9739 | && h->ref_dynamic_nonweak |
8b127cbc | 9740 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9741 | { |
17d078c5 AM |
9742 | bfd *def_bfd; |
9743 | const char *msg; | |
90c984fc L |
9744 | struct elf_link_hash_entry *hi = h; |
9745 | ||
9746 | /* Check indirect symbol. */ | |
9747 | while (hi->root.type == bfd_link_hash_indirect) | |
9748 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9749 | |
9750 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 9751 | /* xgettext:c-format */ |
871b3ab2 | 9752 | msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 9753 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) |
695344c0 | 9754 | /* xgettext:c-format */ |
871b3ab2 | 9755 | msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 9756 | else |
695344c0 | 9757 | /* xgettext:c-format */ |
871b3ab2 | 9758 | msg = _("%pB: local symbol `%s' in %pB is referenced by DSO"); |
8b127cbc | 9759 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9760 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9761 | def_bfd = hi->root.u.def.section->owner; | |
c08bb8dd AM |
9762 | _bfd_error_handler (msg, flinfo->output_bfd, |
9763 | h->root.root.string, def_bfd); | |
17d078c5 | 9764 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9765 | eoinfo->failed = TRUE; |
9766 | return FALSE; | |
9767 | } | |
9768 | ||
9769 | /* We don't want to output symbols that have never been mentioned by | |
9770 | a regular file, or that we have been told to strip. However, if | |
9771 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9772 | output it. */ | |
d983c8c5 | 9773 | strip = FALSE; |
c152c796 | 9774 | if (h->indx == -2) |
d983c8c5 | 9775 | ; |
f5385ebf | 9776 | else if ((h->def_dynamic |
77cfaee6 AM |
9777 | || h->ref_dynamic |
9778 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9779 | && !h->def_regular |
9780 | && !h->ref_regular) | |
c152c796 | 9781 | strip = TRUE; |
8b127cbc | 9782 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9783 | strip = TRUE; |
8b127cbc AM |
9784 | else if (flinfo->info->strip == strip_some |
9785 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9786 | h->root.root.string, FALSE, FALSE) == NULL) |
9787 | strip = TRUE; | |
d56d55e7 AM |
9788 | else if ((h->root.type == bfd_link_hash_defined |
9789 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9790 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9791 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9792 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9793 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9794 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9795 | strip = TRUE; |
9e2278f5 AM |
9796 | else if ((h->root.type == bfd_link_hash_undefined |
9797 | || h->root.type == bfd_link_hash_undefweak) | |
9798 | && h->root.u.undef.abfd != NULL | |
9799 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9800 | strip = TRUE; | |
c152c796 | 9801 | |
b8871f35 L |
9802 | type = h->type; |
9803 | ||
c152c796 | 9804 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9805 | nothing else to do. However, if it is a forced local symbol or |
9806 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9807 | function a chance to make it dynamic. */ | |
c152c796 AM |
9808 | if (strip |
9809 | && h->dynindx == -1 | |
b8871f35 | 9810 | && type != STT_GNU_IFUNC |
f5385ebf | 9811 | && !h->forced_local) |
c152c796 AM |
9812 | return TRUE; |
9813 | ||
9814 | sym.st_value = 0; | |
9815 | sym.st_size = h->size; | |
9816 | sym.st_other = h->other; | |
c152c796 AM |
9817 | switch (h->root.type) |
9818 | { | |
9819 | default: | |
9820 | case bfd_link_hash_new: | |
9821 | case bfd_link_hash_warning: | |
9822 | abort (); | |
9823 | return FALSE; | |
9824 | ||
9825 | case bfd_link_hash_undefined: | |
9826 | case bfd_link_hash_undefweak: | |
9827 | input_sec = bfd_und_section_ptr; | |
9828 | sym.st_shndx = SHN_UNDEF; | |
9829 | break; | |
9830 | ||
9831 | case bfd_link_hash_defined: | |
9832 | case bfd_link_hash_defweak: | |
9833 | { | |
9834 | input_sec = h->root.u.def.section; | |
9835 | if (input_sec->output_section != NULL) | |
9836 | { | |
9837 | sym.st_shndx = | |
8b127cbc | 9838 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9839 | input_sec->output_section); |
9840 | if (sym.st_shndx == SHN_BAD) | |
9841 | { | |
4eca0228 | 9842 | _bfd_error_handler |
695344c0 | 9843 | /* xgettext:c-format */ |
871b3ab2 | 9844 | (_("%pB: could not find output section %pA for input section %pA"), |
8b127cbc | 9845 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9846 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9847 | eoinfo->failed = TRUE; |
9848 | return FALSE; | |
9849 | } | |
9850 | ||
9851 | /* ELF symbols in relocatable files are section relative, | |
9852 | but in nonrelocatable files they are virtual | |
9853 | addresses. */ | |
9854 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9855 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9856 | { |
9857 | sym.st_value += input_sec->output_section->vma; | |
9858 | if (h->type == STT_TLS) | |
9859 | { | |
8b127cbc | 9860 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9861 | if (tls_sec != NULL) |
9862 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9863 | } |
9864 | } | |
9865 | } | |
9866 | else | |
9867 | { | |
9868 | BFD_ASSERT (input_sec->owner == NULL | |
9869 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9870 | sym.st_shndx = SHN_UNDEF; | |
9871 | input_sec = bfd_und_section_ptr; | |
9872 | } | |
9873 | } | |
9874 | break; | |
9875 | ||
9876 | case bfd_link_hash_common: | |
9877 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9878 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9879 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9880 | break; | |
9881 | ||
9882 | case bfd_link_hash_indirect: | |
9883 | /* These symbols are created by symbol versioning. They point | |
9884 | to the decorated version of the name. For example, if the | |
9885 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9886 | foo is used with no version, then we add an indirect symbol | |
9887 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9888 | since the indirected symbol is already in the hash table. */ | |
9889 | return TRUE; | |
9890 | } | |
9891 | ||
b8871f35 L |
9892 | if (type == STT_COMMON || type == STT_OBJECT) |
9893 | switch (h->root.type) | |
9894 | { | |
9895 | case bfd_link_hash_common: | |
9896 | type = elf_link_convert_common_type (flinfo->info, type); | |
9897 | break; | |
9898 | case bfd_link_hash_defined: | |
9899 | case bfd_link_hash_defweak: | |
9900 | if (bed->common_definition (&sym)) | |
9901 | type = elf_link_convert_common_type (flinfo->info, type); | |
9902 | else | |
9903 | type = STT_OBJECT; | |
9904 | break; | |
9905 | case bfd_link_hash_undefined: | |
9906 | case bfd_link_hash_undefweak: | |
9907 | break; | |
9908 | default: | |
9909 | abort (); | |
9910 | } | |
9911 | ||
4deb8f71 | 9912 | if (h->forced_local) |
b8871f35 L |
9913 | { |
9914 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9915 | /* Turn off visibility on local symbol. */ | |
9916 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9917 | } | |
9918 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9919 | else if (h->unique_global && h->def_regular) | |
9920 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
9921 | else if (h->root.type == bfd_link_hash_undefweak | |
9922 | || h->root.type == bfd_link_hash_defweak) | |
9923 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
9924 | else | |
9925 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
9926 | sym.st_target_internal = h->target_internal; | |
9927 | ||
c152c796 AM |
9928 | /* Give the processor backend a chance to tweak the symbol value, |
9929 | and also to finish up anything that needs to be done for this | |
9930 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9931 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9932 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9933 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9934 | && h->def_regular |
0e1862bb | 9935 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
9936 | || ((h->dynindx != -1 |
9937 | || h->forced_local) | |
0e1862bb | 9938 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
9939 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9940 | || h->root.type != bfd_link_hash_undefweak)) | |
9941 | || !h->forced_local) | |
8b127cbc | 9942 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9943 | { |
9944 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9945 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9946 | { |
9947 | eoinfo->failed = TRUE; | |
9948 | return FALSE; | |
9949 | } | |
9950 | } | |
9951 | ||
9952 | /* If we are marking the symbol as undefined, and there are no | |
9953 | non-weak references to this symbol from a regular object, then | |
9954 | mark the symbol as weak undefined; if there are non-weak | |
9955 | references, mark the symbol as strong. We can't do this earlier, | |
9956 | because it might not be marked as undefined until the | |
9957 | finish_dynamic_symbol routine gets through with it. */ | |
9958 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9959 | && h->ref_regular |
c152c796 AM |
9960 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9961 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9962 | { | |
9963 | int bindtype; | |
b8871f35 | 9964 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
9965 | |
9966 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9967 | if (type == STT_GNU_IFUNC) | |
9968 | type = STT_FUNC; | |
c152c796 | 9969 | |
f5385ebf | 9970 | if (h->ref_regular_nonweak) |
c152c796 AM |
9971 | bindtype = STB_GLOBAL; |
9972 | else | |
9973 | bindtype = STB_WEAK; | |
2955ec4c | 9974 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
9975 | } |
9976 | ||
bda987c2 CD |
9977 | /* If this is a symbol defined in a dynamic library, don't use the |
9978 | symbol size from the dynamic library. Relinking an executable | |
9979 | against a new library may introduce gratuitous changes in the | |
9980 | executable's symbols if we keep the size. */ | |
9981 | if (sym.st_shndx == SHN_UNDEF | |
9982 | && !h->def_regular | |
9983 | && h->def_dynamic) | |
9984 | sym.st_size = 0; | |
9985 | ||
c152c796 AM |
9986 | /* If a non-weak symbol with non-default visibility is not defined |
9987 | locally, it is a fatal error. */ | |
0e1862bb | 9988 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
9989 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
9990 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
9991 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 9992 | && !h->def_regular) |
c152c796 | 9993 | { |
17d078c5 AM |
9994 | const char *msg; |
9995 | ||
9996 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 9997 | /* xgettext:c-format */ |
871b3ab2 | 9998 | msg = _("%pB: protected symbol `%s' isn't defined"); |
17d078c5 | 9999 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) |
695344c0 | 10000 | /* xgettext:c-format */ |
871b3ab2 | 10001 | msg = _("%pB: internal symbol `%s' isn't defined"); |
17d078c5 | 10002 | else |
695344c0 | 10003 | /* xgettext:c-format */ |
871b3ab2 | 10004 | msg = _("%pB: hidden symbol `%s' isn't defined"); |
4eca0228 | 10005 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 10006 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
10007 | eoinfo->failed = TRUE; |
10008 | return FALSE; | |
10009 | } | |
10010 | ||
10011 | /* If this symbol should be put in the .dynsym section, then put it | |
10012 | there now. We already know the symbol index. We also fill in | |
10013 | the entry in the .hash section. */ | |
cae1fbbb | 10014 | if (elf_hash_table (flinfo->info)->dynsym != NULL |
202e2356 | 10015 | && h->dynindx != -1 |
8b127cbc | 10016 | && elf_hash_table (flinfo->info)->dynamic_sections_created) |
c152c796 | 10017 | { |
c152c796 AM |
10018 | bfd_byte *esym; |
10019 | ||
90c984fc L |
10020 | /* Since there is no version information in the dynamic string, |
10021 | if there is no version info in symbol version section, we will | |
1659f720 | 10022 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 10023 | by shared library, or not bound locally. */ |
1659f720 | 10024 | if (h->verinfo.verdef == NULL |
0e1862bb | 10025 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
10026 | || h->ref_dynamic |
10027 | || !h->def_regular)) | |
90c984fc L |
10028 | { |
10029 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
10030 | ||
10031 | if (p && p [1] != '\0') | |
10032 | { | |
4eca0228 | 10033 | _bfd_error_handler |
695344c0 | 10034 | /* xgettext:c-format */ |
9793eb77 | 10035 | (_("%pB: no symbol version section for versioned symbol `%s'"), |
90c984fc L |
10036 | flinfo->output_bfd, h->root.root.string); |
10037 | eoinfo->failed = TRUE; | |
10038 | return FALSE; | |
10039 | } | |
10040 | } | |
10041 | ||
c152c796 | 10042 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
10043 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
10044 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 10045 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
10046 | { |
10047 | eoinfo->failed = TRUE; | |
10048 | return FALSE; | |
10049 | } | |
8b127cbc | 10050 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 10051 | |
8b127cbc | 10052 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
10053 | { |
10054 | size_t hash_entry_size; | |
10055 | bfd_byte *bucketpos; | |
10056 | bfd_vma chain; | |
41198d0c L |
10057 | size_t bucketcount; |
10058 | size_t bucket; | |
10059 | ||
8b127cbc | 10060 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 10061 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
10062 | |
10063 | hash_entry_size | |
8b127cbc AM |
10064 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
10065 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 10066 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
10067 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
10068 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
10069 | bucketpos); | |
10070 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
10071 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
10072 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
10073 | } | |
c152c796 | 10074 | |
8b127cbc | 10075 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
10076 | { |
10077 | Elf_Internal_Versym iversym; | |
10078 | Elf_External_Versym *eversym; | |
10079 | ||
f5385ebf | 10080 | if (!h->def_regular) |
c152c796 | 10081 | { |
7b20f099 AM |
10082 | if (h->verinfo.verdef == NULL |
10083 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
10084 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
10085 | iversym.vs_vers = 0; |
10086 | else | |
10087 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
10088 | } | |
10089 | else | |
10090 | { | |
10091 | if (h->verinfo.vertree == NULL) | |
10092 | iversym.vs_vers = 1; | |
10093 | else | |
10094 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 10095 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 10096 | iversym.vs_vers++; |
c152c796 AM |
10097 | } |
10098 | ||
422f1182 | 10099 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 10100 | defined locally. */ |
422f1182 | 10101 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
10102 | iversym.vs_vers |= VERSYM_HIDDEN; |
10103 | ||
8b127cbc | 10104 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 10105 | eversym += h->dynindx; |
8b127cbc | 10106 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
10107 | } |
10108 | } | |
10109 | ||
d983c8c5 AM |
10110 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
10111 | strip it from .symtab too. Obviously we can't do this for | |
10112 | relocatable output or when needed for --emit-relocs. */ | |
10113 | else if (input_sec == bfd_und_section_ptr | |
10114 | && h->indx != -2 | |
66cae560 NC |
10115 | /* PR 22319 Do not strip global undefined symbols marked as being needed. */ |
10116 | && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) | |
0e1862bb | 10117 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 | 10118 | return TRUE; |
66cae560 | 10119 | |
d983c8c5 AM |
10120 | /* Also strip others that we couldn't earlier due to dynamic symbol |
10121 | processing. */ | |
10122 | if (strip) | |
10123 | return TRUE; | |
10124 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
10125 | return TRUE; |
10126 | ||
2ec55de3 AM |
10127 | /* Output a FILE symbol so that following locals are not associated |
10128 | with the wrong input file. We need one for forced local symbols | |
10129 | if we've seen more than one FILE symbol or when we have exactly | |
10130 | one FILE symbol but global symbols are present in a file other | |
10131 | than the one with the FILE symbol. We also need one if linker | |
10132 | defined symbols are present. In practice these conditions are | |
10133 | always met, so just emit the FILE symbol unconditionally. */ | |
10134 | if (eoinfo->localsyms | |
10135 | && !eoinfo->file_sym_done | |
10136 | && eoinfo->flinfo->filesym_count != 0) | |
10137 | { | |
10138 | Elf_Internal_Sym fsym; | |
10139 | ||
10140 | memset (&fsym, 0, sizeof (fsym)); | |
10141 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10142 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10143 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
10144 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
10145 | return FALSE; |
10146 | ||
10147 | eoinfo->file_sym_done = TRUE; | |
10148 | } | |
10149 | ||
8b127cbc | 10150 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
10151 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
10152 | input_sec, h); | |
6e0b88f1 | 10153 | if (ret == 0) |
c152c796 AM |
10154 | { |
10155 | eoinfo->failed = TRUE; | |
10156 | return FALSE; | |
10157 | } | |
6e0b88f1 AM |
10158 | else if (ret == 1) |
10159 | h->indx = indx; | |
10160 | else if (h->indx == -2) | |
10161 | abort(); | |
c152c796 AM |
10162 | |
10163 | return TRUE; | |
10164 | } | |
10165 | ||
cdd3575c AM |
10166 | /* Return TRUE if special handling is done for relocs in SEC against |
10167 | symbols defined in discarded sections. */ | |
10168 | ||
c152c796 AM |
10169 | static bfd_boolean |
10170 | elf_section_ignore_discarded_relocs (asection *sec) | |
10171 | { | |
10172 | const struct elf_backend_data *bed; | |
10173 | ||
cdd3575c AM |
10174 | switch (sec->sec_info_type) |
10175 | { | |
dbaa2011 AM |
10176 | case SEC_INFO_TYPE_STABS: |
10177 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 10178 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
10179 | return TRUE; |
10180 | default: | |
10181 | break; | |
10182 | } | |
c152c796 AM |
10183 | |
10184 | bed = get_elf_backend_data (sec->owner); | |
10185 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
10186 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
10187 | return TRUE; | |
10188 | ||
10189 | return FALSE; | |
10190 | } | |
10191 | ||
9e66c942 AM |
10192 | /* Return a mask saying how ld should treat relocations in SEC against |
10193 | symbols defined in discarded sections. If this function returns | |
10194 | COMPLAIN set, ld will issue a warning message. If this function | |
10195 | returns PRETEND set, and the discarded section was link-once and the | |
10196 | same size as the kept link-once section, ld will pretend that the | |
10197 | symbol was actually defined in the kept section. Otherwise ld will | |
10198 | zero the reloc (at least that is the intent, but some cooperation by | |
10199 | the target dependent code is needed, particularly for REL targets). */ | |
10200 | ||
8a696751 AM |
10201 | unsigned int |
10202 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 10203 | { |
9e66c942 | 10204 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 10205 | return PRETEND; |
cdd3575c AM |
10206 | |
10207 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 10208 | return 0; |
cdd3575c AM |
10209 | |
10210 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 10211 | return 0; |
cdd3575c | 10212 | |
9e66c942 | 10213 | return COMPLAIN | PRETEND; |
cdd3575c AM |
10214 | } |
10215 | ||
3d7f7666 L |
10216 | /* Find a match between a section and a member of a section group. */ |
10217 | ||
10218 | static asection * | |
c0f00686 L |
10219 | match_group_member (asection *sec, asection *group, |
10220 | struct bfd_link_info *info) | |
3d7f7666 L |
10221 | { |
10222 | asection *first = elf_next_in_group (group); | |
10223 | asection *s = first; | |
10224 | ||
10225 | while (s != NULL) | |
10226 | { | |
c0f00686 | 10227 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
10228 | return s; |
10229 | ||
83180ade | 10230 | s = elf_next_in_group (s); |
3d7f7666 L |
10231 | if (s == first) |
10232 | break; | |
10233 | } | |
10234 | ||
10235 | return NULL; | |
10236 | } | |
10237 | ||
01b3c8ab | 10238 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
10239 | to replace it. Return the replacement if it is OK. Otherwise return |
10240 | NULL. */ | |
01b3c8ab L |
10241 | |
10242 | asection * | |
c0f00686 | 10243 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
10244 | { |
10245 | asection *kept; | |
10246 | ||
10247 | kept = sec->kept_section; | |
10248 | if (kept != NULL) | |
10249 | { | |
c2370991 | 10250 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 10251 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
10252 | if (kept != NULL |
10253 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
10254 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 10255 | kept = NULL; |
c2370991 | 10256 | sec->kept_section = kept; |
01b3c8ab L |
10257 | } |
10258 | return kept; | |
10259 | } | |
10260 | ||
c152c796 AM |
10261 | /* Link an input file into the linker output file. This function |
10262 | handles all the sections and relocations of the input file at once. | |
10263 | This is so that we only have to read the local symbols once, and | |
10264 | don't have to keep them in memory. */ | |
10265 | ||
10266 | static bfd_boolean | |
8b127cbc | 10267 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 10268 | { |
ece5ef60 | 10269 | int (*relocate_section) |
c152c796 AM |
10270 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
10271 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
10272 | bfd *output_bfd; | |
10273 | Elf_Internal_Shdr *symtab_hdr; | |
10274 | size_t locsymcount; | |
10275 | size_t extsymoff; | |
10276 | Elf_Internal_Sym *isymbuf; | |
10277 | Elf_Internal_Sym *isym; | |
10278 | Elf_Internal_Sym *isymend; | |
10279 | long *pindex; | |
10280 | asection **ppsection; | |
10281 | asection *o; | |
10282 | const struct elf_backend_data *bed; | |
c152c796 | 10283 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
10284 | bfd_size_type address_size; |
10285 | bfd_vma r_type_mask; | |
10286 | int r_sym_shift; | |
ffbc01cc | 10287 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 10288 | |
8b127cbc | 10289 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
10290 | bed = get_elf_backend_data (output_bfd); |
10291 | relocate_section = bed->elf_backend_relocate_section; | |
10292 | ||
10293 | /* If this is a dynamic object, we don't want to do anything here: | |
10294 | we don't want the local symbols, and we don't want the section | |
10295 | contents. */ | |
10296 | if ((input_bfd->flags & DYNAMIC) != 0) | |
10297 | return TRUE; | |
10298 | ||
c152c796 AM |
10299 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
10300 | if (elf_bad_symtab (input_bfd)) | |
10301 | { | |
10302 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10303 | extsymoff = 0; | |
10304 | } | |
10305 | else | |
10306 | { | |
10307 | locsymcount = symtab_hdr->sh_info; | |
10308 | extsymoff = symtab_hdr->sh_info; | |
10309 | } | |
10310 | ||
10311 | /* Read the local symbols. */ | |
10312 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10313 | if (isymbuf == NULL && locsymcount != 0) | |
10314 | { | |
10315 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
10316 | flinfo->internal_syms, |
10317 | flinfo->external_syms, | |
10318 | flinfo->locsym_shndx); | |
c152c796 AM |
10319 | if (isymbuf == NULL) |
10320 | return FALSE; | |
10321 | } | |
10322 | ||
10323 | /* Find local symbol sections and adjust values of symbols in | |
10324 | SEC_MERGE sections. Write out those local symbols we know are | |
10325 | going into the output file. */ | |
10326 | isymend = isymbuf + locsymcount; | |
8b127cbc | 10327 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
10328 | isym < isymend; |
10329 | isym++, pindex++, ppsection++) | |
10330 | { | |
10331 | asection *isec; | |
10332 | const char *name; | |
10333 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
10334 | long indx; |
10335 | int ret; | |
c152c796 AM |
10336 | |
10337 | *pindex = -1; | |
10338 | ||
10339 | if (elf_bad_symtab (input_bfd)) | |
10340 | { | |
10341 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
10342 | { | |
10343 | *ppsection = NULL; | |
10344 | continue; | |
10345 | } | |
10346 | } | |
10347 | ||
10348 | if (isym->st_shndx == SHN_UNDEF) | |
10349 | isec = bfd_und_section_ptr; | |
c152c796 AM |
10350 | else if (isym->st_shndx == SHN_ABS) |
10351 | isec = bfd_abs_section_ptr; | |
10352 | else if (isym->st_shndx == SHN_COMMON) | |
10353 | isec = bfd_com_section_ptr; | |
10354 | else | |
10355 | { | |
cb33740c AM |
10356 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
10357 | if (isec == NULL) | |
10358 | { | |
10359 | /* Don't attempt to output symbols with st_shnx in the | |
10360 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
10361 | *ppsection = NULL; | |
10362 | continue; | |
10363 | } | |
dbaa2011 | 10364 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
10365 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
10366 | isym->st_value = | |
10367 | _bfd_merged_section_offset (output_bfd, &isec, | |
10368 | elf_section_data (isec)->sec_info, | |
10369 | isym->st_value); | |
c152c796 AM |
10370 | } |
10371 | ||
10372 | *ppsection = isec; | |
10373 | ||
d983c8c5 AM |
10374 | /* Don't output the first, undefined, symbol. In fact, don't |
10375 | output any undefined local symbol. */ | |
10376 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
10377 | continue; |
10378 | ||
10379 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
10380 | { | |
10381 | /* We never output section symbols. Instead, we use the | |
10382 | section symbol of the corresponding section in the output | |
10383 | file. */ | |
10384 | continue; | |
10385 | } | |
10386 | ||
10387 | /* If we are stripping all symbols, we don't want to output this | |
10388 | one. */ | |
8b127cbc | 10389 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10390 | continue; |
10391 | ||
10392 | /* If we are discarding all local symbols, we don't want to | |
10393 | output this one. If we are generating a relocatable output | |
10394 | file, then some of the local symbols may be required by | |
10395 | relocs; we output them below as we discover that they are | |
10396 | needed. */ | |
8b127cbc | 10397 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
10398 | continue; |
10399 | ||
10400 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
10401 | discarding, we don't need to keep it. */ |
10402 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
10403 | && isym->st_shndx < SHN_LORESERVE |
10404 | && bfd_section_removed_from_list (output_bfd, | |
10405 | isec->output_section)) | |
e75a280b L |
10406 | continue; |
10407 | ||
c152c796 AM |
10408 | /* Get the name of the symbol. */ |
10409 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
10410 | isym->st_name); | |
10411 | if (name == NULL) | |
10412 | return FALSE; | |
10413 | ||
10414 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
10415 | if ((flinfo->info->strip == strip_some |
10416 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 10417 | == NULL)) |
8b127cbc | 10418 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10419 | && (isec->flags & SEC_MERGE) |
10420 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10421 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10422 | && bfd_is_local_label_name (input_bfd, name))) |
10423 | continue; | |
10424 | ||
ffbc01cc AM |
10425 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10426 | { | |
ce875075 AM |
10427 | if (input_bfd->lto_output) |
10428 | /* -flto puts a temp file name here. This means builds | |
10429 | are not reproducible. Discard the symbol. */ | |
10430 | continue; | |
ffbc01cc AM |
10431 | have_file_sym = TRUE; |
10432 | flinfo->filesym_count += 1; | |
10433 | } | |
10434 | if (!have_file_sym) | |
10435 | { | |
10436 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10437 | FILE symbols to determine the source file for local | |
10438 | function symbols. Provide a FILE symbol here if input | |
10439 | files lack such, so that their symbols won't be | |
10440 | associated with a previous input file. It's not the | |
10441 | source file, but the best we can do. */ | |
10442 | have_file_sym = TRUE; | |
10443 | flinfo->filesym_count += 1; | |
10444 | memset (&osym, 0, sizeof (osym)); | |
10445 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10446 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10447 | if (!elf_link_output_symstrtab (flinfo, |
10448 | (input_bfd->lto_output ? NULL | |
10449 | : input_bfd->filename), | |
10450 | &osym, bfd_abs_section_ptr, | |
10451 | NULL)) | |
ffbc01cc AM |
10452 | return FALSE; |
10453 | } | |
10454 | ||
c152c796 AM |
10455 | osym = *isym; |
10456 | ||
10457 | /* Adjust the section index for the output file. */ | |
10458 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10459 | isec->output_section); | |
10460 | if (osym.st_shndx == SHN_BAD) | |
10461 | return FALSE; | |
10462 | ||
c152c796 AM |
10463 | /* ELF symbols in relocatable files are section relative, but |
10464 | in executable files they are virtual addresses. Note that | |
10465 | this code assumes that all ELF sections have an associated | |
10466 | BFD section with a reasonable value for output_offset; below | |
10467 | we assume that they also have a reasonable value for | |
10468 | output_section. Any special sections must be set up to meet | |
10469 | these requirements. */ | |
10470 | osym.st_value += isec->output_offset; | |
0e1862bb | 10471 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10472 | { |
10473 | osym.st_value += isec->output_section->vma; | |
10474 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10475 | { | |
10476 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8b127cbc AM |
10477 | BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); |
10478 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
c152c796 AM |
10479 | } |
10480 | } | |
10481 | ||
6e0b88f1 | 10482 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10483 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10484 | if (ret == 0) |
c152c796 | 10485 | return FALSE; |
6e0b88f1 AM |
10486 | else if (ret == 1) |
10487 | *pindex = indx; | |
c152c796 AM |
10488 | } |
10489 | ||
310fd250 L |
10490 | if (bed->s->arch_size == 32) |
10491 | { | |
10492 | r_type_mask = 0xff; | |
10493 | r_sym_shift = 8; | |
10494 | address_size = 4; | |
10495 | } | |
10496 | else | |
10497 | { | |
10498 | r_type_mask = 0xffffffff; | |
10499 | r_sym_shift = 32; | |
10500 | address_size = 8; | |
10501 | } | |
10502 | ||
c152c796 AM |
10503 | /* Relocate the contents of each section. */ |
10504 | sym_hashes = elf_sym_hashes (input_bfd); | |
10505 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10506 | { | |
10507 | bfd_byte *contents; | |
10508 | ||
10509 | if (! o->linker_mark) | |
10510 | { | |
10511 | /* This section was omitted from the link. */ | |
10512 | continue; | |
10513 | } | |
10514 | ||
7bdf4127 | 10515 | if (!flinfo->info->resolve_section_groups |
bcacc0f5 AM |
10516 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10517 | { | |
10518 | /* Deal with the group signature symbol. */ | |
10519 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10520 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10521 | asection *osec = o->output_section; | |
10522 | ||
7bdf4127 | 10523 | BFD_ASSERT (bfd_link_relocatable (flinfo->info)); |
bcacc0f5 AM |
10524 | if (symndx >= locsymcount |
10525 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10526 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10527 | { |
10528 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10529 | while (h->root.type == bfd_link_hash_indirect | |
10530 | || h->root.type == bfd_link_hash_warning) | |
10531 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10532 | /* Arrange for symbol to be output. */ | |
10533 | h->indx = -2; | |
10534 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10535 | } | |
10536 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10537 | { | |
10538 | /* We'll use the output section target_index. */ | |
8b127cbc | 10539 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10540 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10541 | } | |
10542 | else | |
10543 | { | |
8b127cbc | 10544 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10545 | { |
10546 | /* Otherwise output the local symbol now. */ | |
10547 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10548 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10549 | const char *name; |
6e0b88f1 AM |
10550 | long indx; |
10551 | int ret; | |
bcacc0f5 AM |
10552 | |
10553 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10554 | symtab_hdr->sh_link, | |
10555 | sym.st_name); | |
10556 | if (name == NULL) | |
10557 | return FALSE; | |
10558 | ||
10559 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10560 | sec); | |
10561 | if (sym.st_shndx == SHN_BAD) | |
10562 | return FALSE; | |
10563 | ||
10564 | sym.st_value += o->output_offset; | |
10565 | ||
6e0b88f1 | 10566 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10567 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10568 | NULL); | |
6e0b88f1 | 10569 | if (ret == 0) |
bcacc0f5 | 10570 | return FALSE; |
6e0b88f1 | 10571 | else if (ret == 1) |
8b127cbc | 10572 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10573 | else |
10574 | abort (); | |
bcacc0f5 AM |
10575 | } |
10576 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10577 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10578 | } |
10579 | } | |
10580 | ||
c152c796 | 10581 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10582 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10583 | continue; |
10584 | ||
10585 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10586 | { | |
10587 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10588 | or somesuch. */ | |
10589 | continue; | |
10590 | } | |
10591 | ||
10592 | /* Get the contents of the section. They have been cached by a | |
10593 | relaxation routine. Note that o is a section in an input | |
10594 | file, so the contents field will not have been set by any of | |
10595 | the routines which work on output files. */ | |
10596 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10597 | { |
10598 | contents = elf_section_data (o)->this_hdr.contents; | |
10599 | if (bed->caches_rawsize | |
10600 | && o->rawsize != 0 | |
10601 | && o->rawsize < o->size) | |
10602 | { | |
10603 | memcpy (flinfo->contents, contents, o->rawsize); | |
10604 | contents = flinfo->contents; | |
10605 | } | |
10606 | } | |
c152c796 AM |
10607 | else |
10608 | { | |
8b127cbc | 10609 | contents = flinfo->contents; |
4a114e3e | 10610 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10611 | return FALSE; |
10612 | } | |
10613 | ||
10614 | if ((o->flags & SEC_RELOC) != 0) | |
10615 | { | |
10616 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10617 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10618 | int action_discarded; |
ece5ef60 | 10619 | int ret; |
c152c796 AM |
10620 | |
10621 | /* Get the swapped relocs. */ | |
10622 | internal_relocs | |
8b127cbc AM |
10623 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10624 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10625 | if (internal_relocs == NULL |
10626 | && o->reloc_count > 0) | |
10627 | return FALSE; | |
10628 | ||
310fd250 L |
10629 | /* We need to reverse-copy input .ctors/.dtors sections if |
10630 | they are placed in .init_array/.finit_array for output. */ | |
10631 | if (o->size > address_size | |
10632 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10633 | && strcmp (o->output_section->name, | |
10634 | ".init_array") == 0) | |
10635 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10636 | && strcmp (o->output_section->name, | |
10637 | ".fini_array") == 0)) | |
10638 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10639 | { |
056bafd4 MR |
10640 | if (o->size * bed->s->int_rels_per_ext_rel |
10641 | != o->reloc_count * address_size) | |
310fd250 | 10642 | { |
4eca0228 | 10643 | _bfd_error_handler |
695344c0 | 10644 | /* xgettext:c-format */ |
871b3ab2 | 10645 | (_("error: %pB: size of section %pA is not " |
310fd250 L |
10646 | "multiple of address size"), |
10647 | input_bfd, o); | |
8c6716e5 | 10648 | bfd_set_error (bfd_error_bad_value); |
310fd250 L |
10649 | return FALSE; |
10650 | } | |
10651 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10652 | } |
10653 | ||
0f02bbd9 | 10654 | action_discarded = -1; |
c152c796 | 10655 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10656 | action_discarded = (*bed->action_discarded) (o); |
10657 | ||
10658 | /* Run through the relocs evaluating complex reloc symbols and | |
10659 | looking for relocs against symbols from discarded sections | |
10660 | or section symbols from removed link-once sections. | |
10661 | Complain about relocs against discarded sections. Zero | |
10662 | relocs against removed link-once sections. */ | |
10663 | ||
10664 | rel = internal_relocs; | |
056bafd4 | 10665 | relend = rel + o->reloc_count; |
0f02bbd9 | 10666 | for ( ; rel < relend; rel++) |
c152c796 | 10667 | { |
0f02bbd9 AM |
10668 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10669 | unsigned int s_type; | |
10670 | asection **ps, *sec; | |
10671 | struct elf_link_hash_entry *h = NULL; | |
10672 | const char *sym_name; | |
c152c796 | 10673 | |
0f02bbd9 AM |
10674 | if (r_symndx == STN_UNDEF) |
10675 | continue; | |
c152c796 | 10676 | |
0f02bbd9 AM |
10677 | if (r_symndx >= locsymcount |
10678 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10679 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10680 | { |
10681 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10682 | |
0f02bbd9 AM |
10683 | /* Badly formatted input files can contain relocs that |
10684 | reference non-existant symbols. Check here so that | |
10685 | we do not seg fault. */ | |
10686 | if (h == NULL) | |
c152c796 | 10687 | { |
4eca0228 | 10688 | _bfd_error_handler |
695344c0 | 10689 | /* xgettext:c-format */ |
2dcf00ce | 10690 | (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA " |
0f02bbd9 | 10691 | "that references a non-existent global symbol"), |
2dcf00ce | 10692 | input_bfd, (uint64_t) rel->r_info, o); |
0f02bbd9 AM |
10693 | bfd_set_error (bfd_error_bad_value); |
10694 | return FALSE; | |
10695 | } | |
3b36f7e6 | 10696 | |
0f02bbd9 AM |
10697 | while (h->root.type == bfd_link_hash_indirect |
10698 | || h->root.type == bfd_link_hash_warning) | |
10699 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10700 | |
0f02bbd9 | 10701 | s_type = h->type; |
cdd3575c | 10702 | |
9e2dec47 | 10703 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10704 | mark the symbol as undefined. Note that the |
10705 | linker may attach linker created dynamic sections | |
10706 | to the plugin bfd. Symbols defined in linker | |
10707 | created sections are not plugin symbols. */ | |
bc4e12de | 10708 | if ((h->root.non_ir_ref_regular |
4070765b | 10709 | || h->root.non_ir_ref_dynamic) |
9e2dec47 L |
10710 | && (h->root.type == bfd_link_hash_defined |
10711 | || h->root.type == bfd_link_hash_defweak) | |
10712 | && (h->root.u.def.section->flags | |
10713 | & SEC_LINKER_CREATED) == 0 | |
10714 | && h->root.u.def.section->owner != NULL | |
10715 | && (h->root.u.def.section->owner->flags | |
10716 | & BFD_PLUGIN) != 0) | |
10717 | { | |
10718 | h->root.type = bfd_link_hash_undefined; | |
10719 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10720 | } | |
10721 | ||
0f02bbd9 AM |
10722 | ps = NULL; |
10723 | if (h->root.type == bfd_link_hash_defined | |
10724 | || h->root.type == bfd_link_hash_defweak) | |
10725 | ps = &h->root.u.def.section; | |
10726 | ||
10727 | sym_name = h->root.root.string; | |
10728 | } | |
10729 | else | |
10730 | { | |
10731 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10732 | ||
10733 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10734 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10735 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10736 | sym, *ps); | |
10737 | } | |
c152c796 | 10738 | |
c301e700 | 10739 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10740 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10741 | { |
10742 | bfd_vma val; | |
10743 | bfd_vma dot = (rel->r_offset | |
10744 | + o->output_offset + o->output_section->vma); | |
10745 | #ifdef DEBUG | |
10746 | printf ("Encountered a complex symbol!"); | |
10747 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10748 | input_bfd->filename, o->name, |
10749 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10750 | printf (" symbol: idx %8.8lx, name %s\n", |
10751 | r_symndx, sym_name); | |
10752 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10753 | (unsigned long) rel->r_info, | |
10754 | (unsigned long) rel->r_offset); | |
10755 | #endif | |
8b127cbc | 10756 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10757 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10758 | return FALSE; | |
10759 | ||
10760 | /* Symbol evaluated OK. Update to absolute value. */ | |
10761 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10762 | r_symndx, val); | |
10763 | continue; | |
10764 | } | |
10765 | ||
10766 | if (action_discarded != -1 && ps != NULL) | |
10767 | { | |
cdd3575c AM |
10768 | /* Complain if the definition comes from a |
10769 | discarded section. */ | |
dbaa2011 | 10770 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10771 | { |
cf35638d | 10772 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10773 | if (action_discarded & COMPLAIN) |
8b127cbc | 10774 | (*flinfo->info->callbacks->einfo) |
695344c0 | 10775 | /* xgettext:c-format */ |
871b3ab2 AM |
10776 | (_("%X`%s' referenced in section `%pA' of %pB: " |
10777 | "defined in discarded section `%pA' of %pB\n"), | |
e1fffbe6 | 10778 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10779 | |
87e5235d | 10780 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10781 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10782 | really defined in the kept linkonce section. |
10783 | FIXME: This is quite broken. Modifying the | |
10784 | symbol here means we will be changing all later | |
e0ae6d6f | 10785 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10786 | if (action_discarded & PRETEND) |
87e5235d | 10787 | { |
01b3c8ab L |
10788 | asection *kept; |
10789 | ||
c0f00686 | 10790 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10791 | flinfo->info); |
01b3c8ab | 10792 | if (kept != NULL) |
87e5235d AM |
10793 | { |
10794 | *ps = kept; | |
10795 | continue; | |
10796 | } | |
10797 | } | |
c152c796 AM |
10798 | } |
10799 | } | |
10800 | } | |
10801 | ||
10802 | /* Relocate the section by invoking a back end routine. | |
10803 | ||
10804 | The back end routine is responsible for adjusting the | |
10805 | section contents as necessary, and (if using Rela relocs | |
10806 | and generating a relocatable output file) adjusting the | |
10807 | reloc addend as necessary. | |
10808 | ||
10809 | The back end routine does not have to worry about setting | |
10810 | the reloc address or the reloc symbol index. | |
10811 | ||
10812 | The back end routine is given a pointer to the swapped in | |
10813 | internal symbols, and can access the hash table entries | |
10814 | for the external symbols via elf_sym_hashes (input_bfd). | |
10815 | ||
10816 | When generating relocatable output, the back end routine | |
10817 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10818 | output symbol is going to be a section symbol | |
10819 | corresponding to the output section, which will require | |
10820 | the addend to be adjusted. */ | |
10821 | ||
8b127cbc | 10822 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10823 | input_bfd, o, contents, |
10824 | internal_relocs, | |
10825 | isymbuf, | |
8b127cbc | 10826 | flinfo->sections); |
ece5ef60 | 10827 | if (!ret) |
c152c796 AM |
10828 | return FALSE; |
10829 | ||
ece5ef60 | 10830 | if (ret == 2 |
0e1862bb | 10831 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10832 | || flinfo->info->emitrelocations) |
c152c796 AM |
10833 | { |
10834 | Elf_Internal_Rela *irela; | |
d4730f92 | 10835 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10836 | bfd_vma last_offset; |
10837 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10838 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10839 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10840 | unsigned int next_erel; |
c152c796 | 10841 | bfd_boolean rela_normal; |
d4730f92 | 10842 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10843 | |
d4730f92 BS |
10844 | esdi = elf_section_data (o); |
10845 | esdo = elf_section_data (o->output_section); | |
10846 | rela_normal = FALSE; | |
c152c796 AM |
10847 | |
10848 | /* Adjust the reloc addresses and symbol indices. */ | |
10849 | ||
10850 | irela = internal_relocs; | |
056bafd4 | 10851 | irelaend = irela + o->reloc_count; |
d4730f92 BS |
10852 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10853 | /* We start processing the REL relocs, if any. When we reach | |
10854 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10855 | irelamid = irela; | |
10856 | if (esdi->rel.hdr != NULL) | |
10857 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10858 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10859 | rel_hash_list = rel_hash; |
d4730f92 | 10860 | rela_hash_list = NULL; |
c152c796 | 10861 | last_offset = o->output_offset; |
0e1862bb | 10862 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10863 | last_offset += o->output_section->vma; |
10864 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10865 | { | |
10866 | unsigned long r_symndx; | |
10867 | asection *sec; | |
10868 | Elf_Internal_Sym sym; | |
10869 | ||
10870 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10871 | { | |
10872 | rel_hash++; | |
10873 | next_erel = 0; | |
10874 | } | |
10875 | ||
d4730f92 BS |
10876 | if (irela == irelamid) |
10877 | { | |
10878 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10879 | rela_hash_list = rel_hash; | |
10880 | rela_normal = bed->rela_normal; | |
10881 | } | |
10882 | ||
c152c796 | 10883 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10884 | flinfo->info, o, |
c152c796 AM |
10885 | irela->r_offset); |
10886 | if (irela->r_offset >= (bfd_vma) -2) | |
10887 | { | |
10888 | /* This is a reloc for a deleted entry or somesuch. | |
10889 | Turn it into an R_*_NONE reloc, at the same | |
10890 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10891 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10892 | being ordered. */ |
10893 | irela->r_offset = last_offset; | |
10894 | irela->r_info = 0; | |
10895 | irela->r_addend = 0; | |
10896 | continue; | |
10897 | } | |
10898 | ||
10899 | irela->r_offset += o->output_offset; | |
10900 | ||
10901 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10902 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10903 | irela->r_offset += o->output_section->vma; |
10904 | ||
10905 | last_offset = irela->r_offset; | |
10906 | ||
10907 | r_symndx = irela->r_info >> r_sym_shift; | |
10908 | if (r_symndx == STN_UNDEF) | |
10909 | continue; | |
10910 | ||
10911 | if (r_symndx >= locsymcount | |
10912 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10913 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10914 | { |
10915 | struct elf_link_hash_entry *rh; | |
10916 | unsigned long indx; | |
10917 | ||
10918 | /* This is a reloc against a global symbol. We | |
10919 | have not yet output all the local symbols, so | |
10920 | we do not know the symbol index of any global | |
10921 | symbol. We set the rel_hash entry for this | |
10922 | reloc to point to the global hash table entry | |
10923 | for this symbol. The symbol index is then | |
ee75fd95 | 10924 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10925 | indx = r_symndx - extsymoff; |
10926 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10927 | while (rh->root.type == bfd_link_hash_indirect | |
10928 | || rh->root.type == bfd_link_hash_warning) | |
10929 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10930 | ||
10931 | /* Setting the index to -2 tells | |
10932 | elf_link_output_extsym that this symbol is | |
10933 | used by a reloc. */ | |
10934 | BFD_ASSERT (rh->indx < 0); | |
10935 | rh->indx = -2; | |
c152c796 AM |
10936 | *rel_hash = rh; |
10937 | ||
10938 | continue; | |
10939 | } | |
10940 | ||
10941 | /* This is a reloc against a local symbol. */ | |
10942 | ||
10943 | *rel_hash = NULL; | |
10944 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10945 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10946 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10947 | { | |
10948 | /* I suppose the backend ought to fill in the | |
10949 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10950 | processor specific section. */ |
cf35638d | 10951 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10952 | if (bfd_is_abs_section (sec)) |
10953 | ; | |
c152c796 AM |
10954 | else if (sec == NULL || sec->owner == NULL) |
10955 | { | |
10956 | bfd_set_error (bfd_error_bad_value); | |
10957 | return FALSE; | |
10958 | } | |
10959 | else | |
10960 | { | |
6a8d1586 AM |
10961 | asection *osec = sec->output_section; |
10962 | ||
10963 | /* If we have discarded a section, the output | |
10964 | section will be the absolute section. In | |
ab96bf03 AM |
10965 | case of discarded SEC_MERGE sections, use |
10966 | the kept section. relocate_section should | |
10967 | have already handled discarded linkonce | |
10968 | sections. */ | |
6a8d1586 AM |
10969 | if (bfd_is_abs_section (osec) |
10970 | && sec->kept_section != NULL | |
10971 | && sec->kept_section->output_section != NULL) | |
10972 | { | |
10973 | osec = sec->kept_section->output_section; | |
10974 | irela->r_addend -= osec->vma; | |
10975 | } | |
10976 | ||
10977 | if (!bfd_is_abs_section (osec)) | |
10978 | { | |
10979 | r_symndx = osec->target_index; | |
cf35638d | 10980 | if (r_symndx == STN_UNDEF) |
74541ad4 | 10981 | { |
051d833a AM |
10982 | irela->r_addend += osec->vma; |
10983 | osec = _bfd_nearby_section (output_bfd, osec, | |
10984 | osec->vma); | |
10985 | irela->r_addend -= osec->vma; | |
10986 | r_symndx = osec->target_index; | |
74541ad4 | 10987 | } |
6a8d1586 | 10988 | } |
c152c796 AM |
10989 | } |
10990 | ||
10991 | /* Adjust the addend according to where the | |
10992 | section winds up in the output section. */ | |
10993 | if (rela_normal) | |
10994 | irela->r_addend += sec->output_offset; | |
10995 | } | |
10996 | else | |
10997 | { | |
8b127cbc | 10998 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
10999 | { |
11000 | unsigned long shlink; | |
11001 | const char *name; | |
11002 | asection *osec; | |
6e0b88f1 | 11003 | long indx; |
c152c796 | 11004 | |
8b127cbc | 11005 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
11006 | { |
11007 | /* You can't do ld -r -s. */ | |
11008 | bfd_set_error (bfd_error_invalid_operation); | |
11009 | return FALSE; | |
11010 | } | |
11011 | ||
11012 | /* This symbol was skipped earlier, but | |
11013 | since it is needed by a reloc, we | |
11014 | must output it now. */ | |
11015 | shlink = symtab_hdr->sh_link; | |
11016 | name = (bfd_elf_string_from_elf_section | |
11017 | (input_bfd, shlink, sym.st_name)); | |
11018 | if (name == NULL) | |
11019 | return FALSE; | |
11020 | ||
11021 | osec = sec->output_section; | |
11022 | sym.st_shndx = | |
11023 | _bfd_elf_section_from_bfd_section (output_bfd, | |
11024 | osec); | |
11025 | if (sym.st_shndx == SHN_BAD) | |
11026 | return FALSE; | |
11027 | ||
11028 | sym.st_value += sec->output_offset; | |
0e1862bb | 11029 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11030 | { |
11031 | sym.st_value += osec->vma; | |
11032 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
11033 | { | |
11034 | /* STT_TLS symbols are relative to PT_TLS | |
11035 | segment base. */ | |
8b127cbc | 11036 | BFD_ASSERT (elf_hash_table (flinfo->info) |
c152c796 | 11037 | ->tls_sec != NULL); |
8b127cbc | 11038 | sym.st_value -= (elf_hash_table (flinfo->info) |
c152c796 AM |
11039 | ->tls_sec->vma); |
11040 | } | |
11041 | } | |
11042 | ||
6e0b88f1 | 11043 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
11044 | ret = elf_link_output_symstrtab (flinfo, name, |
11045 | &sym, sec, | |
11046 | NULL); | |
6e0b88f1 | 11047 | if (ret == 0) |
c152c796 | 11048 | return FALSE; |
6e0b88f1 | 11049 | else if (ret == 1) |
8b127cbc | 11050 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
11051 | else |
11052 | abort (); | |
c152c796 AM |
11053 | } |
11054 | ||
8b127cbc | 11055 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
11056 | } |
11057 | ||
11058 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
11059 | | (irela->r_info & r_type_mask)); | |
11060 | } | |
11061 | ||
11062 | /* Swap out the relocs. */ | |
d4730f92 BS |
11063 | input_rel_hdr = esdi->rel.hdr; |
11064 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 11065 | { |
d4730f92 BS |
11066 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
11067 | input_rel_hdr, | |
11068 | internal_relocs, | |
11069 | rel_hash_list)) | |
11070 | return FALSE; | |
c152c796 AM |
11071 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
11072 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 11073 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
11074 | } |
11075 | ||
11076 | input_rela_hdr = esdi->rela.hdr; | |
11077 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
11078 | { | |
eac338cf | 11079 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 11080 | input_rela_hdr, |
eac338cf | 11081 | internal_relocs, |
d4730f92 | 11082 | rela_hash_list)) |
c152c796 AM |
11083 | return FALSE; |
11084 | } | |
11085 | } | |
11086 | } | |
11087 | ||
11088 | /* Write out the modified section contents. */ | |
11089 | if (bed->elf_backend_write_section | |
8b127cbc | 11090 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 11091 | contents)) |
c152c796 AM |
11092 | { |
11093 | /* Section written out. */ | |
11094 | } | |
11095 | else switch (o->sec_info_type) | |
11096 | { | |
dbaa2011 | 11097 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
11098 | if (! (_bfd_write_section_stabs |
11099 | (output_bfd, | |
8b127cbc | 11100 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
11101 | o, &elf_section_data (o)->sec_info, contents))) |
11102 | return FALSE; | |
11103 | break; | |
dbaa2011 | 11104 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
11105 | if (! _bfd_write_merged_section (output_bfd, o, |
11106 | elf_section_data (o)->sec_info)) | |
11107 | return FALSE; | |
11108 | break; | |
dbaa2011 | 11109 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 11110 | { |
8b127cbc | 11111 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
11112 | o, contents)) |
11113 | return FALSE; | |
11114 | } | |
11115 | break; | |
2f0c68f2 CM |
11116 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
11117 | { | |
11118 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
11119 | flinfo->info, | |
11120 | o, contents)) | |
11121 | return FALSE; | |
11122 | } | |
11123 | break; | |
c152c796 AM |
11124 | default: |
11125 | { | |
310fd250 L |
11126 | if (! (o->flags & SEC_EXCLUDE)) |
11127 | { | |
11128 | file_ptr offset = (file_ptr) o->output_offset; | |
11129 | bfd_size_type todo = o->size; | |
37b01f6a DG |
11130 | |
11131 | offset *= bfd_octets_per_byte (output_bfd); | |
11132 | ||
310fd250 L |
11133 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
11134 | { | |
11135 | /* Reverse-copy input section to output. */ | |
11136 | do | |
11137 | { | |
11138 | todo -= address_size; | |
11139 | if (! bfd_set_section_contents (output_bfd, | |
11140 | o->output_section, | |
11141 | contents + todo, | |
11142 | offset, | |
11143 | address_size)) | |
11144 | return FALSE; | |
11145 | if (todo == 0) | |
11146 | break; | |
11147 | offset += address_size; | |
11148 | } | |
11149 | while (1); | |
11150 | } | |
11151 | else if (! bfd_set_section_contents (output_bfd, | |
11152 | o->output_section, | |
11153 | contents, | |
11154 | offset, todo)) | |
11155 | return FALSE; | |
11156 | } | |
c152c796 AM |
11157 | } |
11158 | break; | |
11159 | } | |
11160 | } | |
11161 | ||
11162 | return TRUE; | |
11163 | } | |
11164 | ||
11165 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 11166 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
11167 | is used to build constructor and destructor tables when linking |
11168 | with -Ur. */ | |
11169 | ||
11170 | static bfd_boolean | |
11171 | elf_reloc_link_order (bfd *output_bfd, | |
11172 | struct bfd_link_info *info, | |
11173 | asection *output_section, | |
11174 | struct bfd_link_order *link_order) | |
11175 | { | |
11176 | reloc_howto_type *howto; | |
11177 | long indx; | |
11178 | bfd_vma offset; | |
11179 | bfd_vma addend; | |
d4730f92 | 11180 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
11181 | struct elf_link_hash_entry **rel_hash_ptr; |
11182 | Elf_Internal_Shdr *rel_hdr; | |
11183 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
11184 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
11185 | bfd_byte *erel; | |
11186 | unsigned int i; | |
d4730f92 | 11187 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
11188 | |
11189 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
11190 | if (howto == NULL) | |
11191 | { | |
11192 | bfd_set_error (bfd_error_bad_value); | |
11193 | return FALSE; | |
11194 | } | |
11195 | ||
11196 | addend = link_order->u.reloc.p->addend; | |
11197 | ||
d4730f92 BS |
11198 | if (esdo->rel.hdr) |
11199 | reldata = &esdo->rel; | |
11200 | else if (esdo->rela.hdr) | |
11201 | reldata = &esdo->rela; | |
11202 | else | |
11203 | { | |
11204 | reldata = NULL; | |
11205 | BFD_ASSERT (0); | |
11206 | } | |
11207 | ||
c152c796 | 11208 | /* Figure out the symbol index. */ |
d4730f92 | 11209 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
11210 | if (link_order->type == bfd_section_reloc_link_order) |
11211 | { | |
11212 | indx = link_order->u.reloc.p->u.section->target_index; | |
11213 | BFD_ASSERT (indx != 0); | |
11214 | *rel_hash_ptr = NULL; | |
11215 | } | |
11216 | else | |
11217 | { | |
11218 | struct elf_link_hash_entry *h; | |
11219 | ||
11220 | /* Treat a reloc against a defined symbol as though it were | |
11221 | actually against the section. */ | |
11222 | h = ((struct elf_link_hash_entry *) | |
11223 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
11224 | link_order->u.reloc.p->u.name, | |
11225 | FALSE, FALSE, TRUE)); | |
11226 | if (h != NULL | |
11227 | && (h->root.type == bfd_link_hash_defined | |
11228 | || h->root.type == bfd_link_hash_defweak)) | |
11229 | { | |
11230 | asection *section; | |
11231 | ||
11232 | section = h->root.u.def.section; | |
11233 | indx = section->output_section->target_index; | |
11234 | *rel_hash_ptr = NULL; | |
11235 | /* It seems that we ought to add the symbol value to the | |
11236 | addend here, but in practice it has already been added | |
11237 | because it was passed to constructor_callback. */ | |
11238 | addend += section->output_section->vma + section->output_offset; | |
11239 | } | |
11240 | else if (h != NULL) | |
11241 | { | |
11242 | /* Setting the index to -2 tells elf_link_output_extsym that | |
11243 | this symbol is used by a reloc. */ | |
11244 | h->indx = -2; | |
11245 | *rel_hash_ptr = h; | |
11246 | indx = 0; | |
11247 | } | |
11248 | else | |
11249 | { | |
1a72702b AM |
11250 | (*info->callbacks->unattached_reloc) |
11251 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
11252 | indx = 0; |
11253 | } | |
11254 | } | |
11255 | ||
11256 | /* If this is an inplace reloc, we must write the addend into the | |
11257 | object file. */ | |
11258 | if (howto->partial_inplace && addend != 0) | |
11259 | { | |
11260 | bfd_size_type size; | |
11261 | bfd_reloc_status_type rstat; | |
11262 | bfd_byte *buf; | |
11263 | bfd_boolean ok; | |
11264 | const char *sym_name; | |
11265 | ||
a50b1753 NC |
11266 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
11267 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 11268 | if (buf == NULL && size != 0) |
c152c796 AM |
11269 | return FALSE; |
11270 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
11271 | switch (rstat) | |
11272 | { | |
11273 | case bfd_reloc_ok: | |
11274 | break; | |
11275 | ||
11276 | default: | |
11277 | case bfd_reloc_outofrange: | |
11278 | abort (); | |
11279 | ||
11280 | case bfd_reloc_overflow: | |
11281 | if (link_order->type == bfd_section_reloc_link_order) | |
11282 | sym_name = bfd_section_name (output_bfd, | |
11283 | link_order->u.reloc.p->u.section); | |
11284 | else | |
11285 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
11286 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
11287 | howto->name, addend, NULL, NULL, | |
11288 | (bfd_vma) 0); | |
c152c796 AM |
11289 | break; |
11290 | } | |
37b01f6a | 11291 | |
c152c796 | 11292 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
11293 | link_order->offset |
11294 | * bfd_octets_per_byte (output_bfd), | |
11295 | size); | |
c152c796 AM |
11296 | free (buf); |
11297 | if (! ok) | |
11298 | return FALSE; | |
11299 | } | |
11300 | ||
11301 | /* The address of a reloc is relative to the section in a | |
11302 | relocatable file, and is a virtual address in an executable | |
11303 | file. */ | |
11304 | offset = link_order->offset; | |
0e1862bb | 11305 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
11306 | offset += output_section->vma; |
11307 | ||
11308 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
11309 | { | |
11310 | irel[i].r_offset = offset; | |
11311 | irel[i].r_info = 0; | |
11312 | irel[i].r_addend = 0; | |
11313 | } | |
11314 | if (bed->s->arch_size == 32) | |
11315 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
11316 | else | |
11317 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
11318 | ||
d4730f92 | 11319 | rel_hdr = reldata->hdr; |
c152c796 AM |
11320 | erel = rel_hdr->contents; |
11321 | if (rel_hdr->sh_type == SHT_REL) | |
11322 | { | |
d4730f92 | 11323 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
11324 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
11325 | } | |
11326 | else | |
11327 | { | |
11328 | irel[0].r_addend = addend; | |
d4730f92 | 11329 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
11330 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
11331 | } | |
11332 | ||
d4730f92 | 11333 | ++reldata->count; |
c152c796 AM |
11334 | |
11335 | return TRUE; | |
11336 | } | |
11337 | ||
0b52efa6 PB |
11338 | |
11339 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
11340 | ||
11341 | static bfd_vma | |
11342 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
11343 | { | |
11344 | Elf_Internal_Shdr **elf_shdrp; | |
11345 | asection *s; | |
11346 | int elfsec; | |
11347 | ||
11348 | s = p->u.indirect.section; | |
11349 | elf_shdrp = elf_elfsections (s->owner); | |
11350 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
11351 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
11352 | /* PR 290: |
11353 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 11354 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
11355 | sh_info fields. Hence we could get the situation |
11356 | where elfsec is 0. */ | |
11357 | if (elfsec == 0) | |
11358 | { | |
11359 | const struct elf_backend_data *bed | |
11360 | = get_elf_backend_data (s->owner); | |
11361 | if (bed->link_order_error_handler) | |
d003868e | 11362 | bed->link_order_error_handler |
695344c0 | 11363 | /* xgettext:c-format */ |
871b3ab2 | 11364 | (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s); |
185d09ad L |
11365 | return 0; |
11366 | } | |
11367 | else | |
11368 | { | |
11369 | s = elf_shdrp[elfsec]->bfd_section; | |
11370 | return s->output_section->vma + s->output_offset; | |
11371 | } | |
0b52efa6 PB |
11372 | } |
11373 | ||
11374 | ||
11375 | /* Compare two sections based on the locations of the sections they are | |
11376 | linked to. Used by elf_fixup_link_order. */ | |
11377 | ||
11378 | static int | |
11379 | compare_link_order (const void * a, const void * b) | |
11380 | { | |
11381 | bfd_vma apos; | |
11382 | bfd_vma bpos; | |
11383 | ||
11384 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
11385 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
11386 | if (apos < bpos) | |
11387 | return -1; | |
11388 | return apos > bpos; | |
11389 | } | |
11390 | ||
11391 | ||
11392 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
11393 | order as their linked sections. Returns false if this could not be done | |
11394 | because an output section includes both ordered and unordered | |
11395 | sections. Ideally we'd do this in the linker proper. */ | |
11396 | ||
11397 | static bfd_boolean | |
11398 | elf_fixup_link_order (bfd *abfd, asection *o) | |
11399 | { | |
11400 | int seen_linkorder; | |
11401 | int seen_other; | |
11402 | int n; | |
11403 | struct bfd_link_order *p; | |
11404 | bfd *sub; | |
11405 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 11406 | unsigned elfsec; |
0b52efa6 | 11407 | struct bfd_link_order **sections; |
d33cdfe3 | 11408 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 11409 | bfd_vma offset; |
3b36f7e6 | 11410 | |
d33cdfe3 L |
11411 | other_sec = NULL; |
11412 | linkorder_sec = NULL; | |
0b52efa6 PB |
11413 | seen_other = 0; |
11414 | seen_linkorder = 0; | |
8423293d | 11415 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 11416 | { |
d33cdfe3 | 11417 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
11418 | { |
11419 | s = p->u.indirect.section; | |
d33cdfe3 L |
11420 | sub = s->owner; |
11421 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
11422 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
11423 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
11424 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
11425 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
11426 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
11427 | { |
11428 | seen_linkorder++; | |
11429 | linkorder_sec = s; | |
11430 | } | |
0b52efa6 | 11431 | else |
d33cdfe3 L |
11432 | { |
11433 | seen_other++; | |
11434 | other_sec = s; | |
11435 | } | |
0b52efa6 PB |
11436 | } |
11437 | else | |
11438 | seen_other++; | |
d33cdfe3 L |
11439 | |
11440 | if (seen_other && seen_linkorder) | |
11441 | { | |
11442 | if (other_sec && linkorder_sec) | |
4eca0228 | 11443 | _bfd_error_handler |
695344c0 | 11444 | /* xgettext:c-format */ |
871b3ab2 AM |
11445 | (_("%pA has both ordered [`%pA' in %pB] " |
11446 | "and unordered [`%pA' in %pB] sections"), | |
63a5468a AM |
11447 | o, linkorder_sec, linkorder_sec->owner, |
11448 | other_sec, other_sec->owner); | |
d33cdfe3 | 11449 | else |
4eca0228 | 11450 | _bfd_error_handler |
871b3ab2 | 11451 | (_("%pA has both ordered and unordered sections"), o); |
d33cdfe3 L |
11452 | bfd_set_error (bfd_error_bad_value); |
11453 | return FALSE; | |
11454 | } | |
0b52efa6 PB |
11455 | } |
11456 | ||
11457 | if (!seen_linkorder) | |
11458 | return TRUE; | |
11459 | ||
0b52efa6 | 11460 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11461 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11462 | if (sections == NULL) | |
11463 | return FALSE; | |
0b52efa6 | 11464 | seen_linkorder = 0; |
3b36f7e6 | 11465 | |
8423293d | 11466 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11467 | { |
11468 | sections[seen_linkorder++] = p; | |
11469 | } | |
11470 | /* Sort the input sections in the order of their linked section. */ | |
11471 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11472 | compare_link_order); | |
11473 | ||
11474 | /* Change the offsets of the sections. */ | |
11475 | offset = 0; | |
11476 | for (n = 0; n < seen_linkorder; n++) | |
11477 | { | |
11478 | s = sections[n]->u.indirect.section; | |
461686a3 | 11479 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11480 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11481 | sections[n]->offset = offset; |
11482 | offset += sections[n]->size; | |
11483 | } | |
11484 | ||
4dd07732 | 11485 | free (sections); |
0b52efa6 PB |
11486 | return TRUE; |
11487 | } | |
11488 | ||
76359541 TP |
11489 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11490 | Returns TRUE upon success, FALSE otherwise. */ | |
11491 | ||
11492 | static bfd_boolean | |
11493 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11494 | { | |
11495 | bfd_boolean ret = FALSE; | |
11496 | bfd *implib_bfd; | |
11497 | const struct elf_backend_data *bed; | |
11498 | flagword flags; | |
11499 | enum bfd_architecture arch; | |
11500 | unsigned int mach; | |
11501 | asymbol **sympp = NULL; | |
11502 | long symsize; | |
11503 | long symcount; | |
11504 | long src_count; | |
11505 | elf_symbol_type *osymbuf; | |
11506 | ||
11507 | implib_bfd = info->out_implib_bfd; | |
11508 | bed = get_elf_backend_data (abfd); | |
11509 | ||
11510 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11511 | return FALSE; | |
11512 | ||
046734ff | 11513 | /* Use flag from executable but make it a relocatable object. */ |
76359541 TP |
11514 | flags = bfd_get_file_flags (abfd); |
11515 | flags &= ~HAS_RELOC; | |
11516 | if (!bfd_set_start_address (implib_bfd, 0) | |
046734ff | 11517 | || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) |
76359541 TP |
11518 | return FALSE; |
11519 | ||
11520 | /* Copy architecture of output file to import library file. */ | |
11521 | arch = bfd_get_arch (abfd); | |
11522 | mach = bfd_get_mach (abfd); | |
11523 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11524 | && (abfd->target_defaulted | |
11525 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11526 | return FALSE; | |
11527 | ||
11528 | /* Get symbol table size. */ | |
11529 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11530 | if (symsize < 0) | |
11531 | return FALSE; | |
11532 | ||
11533 | /* Read in the symbol table. */ | |
11534 | sympp = (asymbol **) xmalloc (symsize); | |
11535 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11536 | if (symcount < 0) | |
11537 | goto free_sym_buf; | |
11538 | ||
11539 | /* Allow the BFD backend to copy any private header data it | |
11540 | understands from the output BFD to the import library BFD. */ | |
11541 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11542 | goto free_sym_buf; | |
11543 | ||
11544 | /* Filter symbols to appear in the import library. */ | |
11545 | if (bed->elf_backend_filter_implib_symbols) | |
11546 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11547 | symcount); | |
11548 | else | |
11549 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11550 | if (symcount == 0) | |
11551 | { | |
5df1bc57 | 11552 | bfd_set_error (bfd_error_no_symbols); |
871b3ab2 | 11553 | _bfd_error_handler (_("%pB: no symbol found for import library"), |
4eca0228 | 11554 | implib_bfd); |
76359541 TP |
11555 | goto free_sym_buf; |
11556 | } | |
11557 | ||
11558 | ||
11559 | /* Make symbols absolute. */ | |
11560 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11561 | sizeof (*osymbuf)); | |
11562 | for (src_count = 0; src_count < symcount; src_count++) | |
11563 | { | |
11564 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11565 | sizeof (*osymbuf)); | |
11566 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11567 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11568 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11569 | osymbuf[src_count].internal_elf_sym.st_value = | |
11570 | osymbuf[src_count].symbol.value; | |
11571 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11572 | } | |
11573 | ||
11574 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11575 | ||
11576 | /* Allow the BFD backend to copy any private data it understands | |
11577 | from the output BFD to the import library BFD. This is done last | |
11578 | to permit the routine to look at the filtered symbol table. */ | |
11579 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11580 | goto free_sym_buf; | |
11581 | ||
11582 | if (!bfd_close (implib_bfd)) | |
11583 | goto free_sym_buf; | |
11584 | ||
11585 | ret = TRUE; | |
11586 | ||
11587 | free_sym_buf: | |
11588 | free (sympp); | |
11589 | return ret; | |
11590 | } | |
11591 | ||
9f7c3e5e AM |
11592 | static void |
11593 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11594 | { | |
11595 | asection *o; | |
11596 | ||
11597 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11598 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11599 | if (flinfo->contents != NULL) |
11600 | free (flinfo->contents); | |
11601 | if (flinfo->external_relocs != NULL) | |
11602 | free (flinfo->external_relocs); | |
11603 | if (flinfo->internal_relocs != NULL) | |
11604 | free (flinfo->internal_relocs); | |
11605 | if (flinfo->external_syms != NULL) | |
11606 | free (flinfo->external_syms); | |
11607 | if (flinfo->locsym_shndx != NULL) | |
11608 | free (flinfo->locsym_shndx); | |
11609 | if (flinfo->internal_syms != NULL) | |
11610 | free (flinfo->internal_syms); | |
11611 | if (flinfo->indices != NULL) | |
11612 | free (flinfo->indices); | |
11613 | if (flinfo->sections != NULL) | |
11614 | free (flinfo->sections); | |
9f7c3e5e AM |
11615 | if (flinfo->symshndxbuf != NULL) |
11616 | free (flinfo->symshndxbuf); | |
11617 | for (o = obfd->sections; o != NULL; o = o->next) | |
11618 | { | |
11619 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11620 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11621 | free (esdo->rel.hashes); | |
11622 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11623 | free (esdo->rela.hashes); | |
11624 | } | |
11625 | } | |
0b52efa6 | 11626 | |
c152c796 AM |
11627 | /* Do the final step of an ELF link. */ |
11628 | ||
11629 | bfd_boolean | |
11630 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11631 | { | |
11632 | bfd_boolean dynamic; | |
11633 | bfd_boolean emit_relocs; | |
11634 | bfd *dynobj; | |
8b127cbc | 11635 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11636 | asection *o; |
11637 | struct bfd_link_order *p; | |
11638 | bfd *sub; | |
c152c796 AM |
11639 | bfd_size_type max_contents_size; |
11640 | bfd_size_type max_external_reloc_size; | |
11641 | bfd_size_type max_internal_reloc_count; | |
11642 | bfd_size_type max_sym_count; | |
11643 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11644 | Elf_Internal_Sym elfsym; |
11645 | unsigned int i; | |
11646 | Elf_Internal_Shdr *symtab_hdr; | |
11647 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11648 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11649 | struct elf_outext_info eoinfo; | |
11650 | bfd_boolean merged; | |
11651 | size_t relativecount = 0; | |
11652 | asection *reldyn = 0; | |
11653 | bfd_size_type amt; | |
104d59d1 JM |
11654 | asection *attr_section = NULL; |
11655 | bfd_vma attr_size = 0; | |
11656 | const char *std_attrs_section; | |
64f52338 | 11657 | struct elf_link_hash_table *htab = elf_hash_table (info); |
c152c796 | 11658 | |
64f52338 | 11659 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11660 | return FALSE; |
11661 | ||
0e1862bb | 11662 | if (bfd_link_pic (info)) |
c152c796 AM |
11663 | abfd->flags |= DYNAMIC; |
11664 | ||
64f52338 AM |
11665 | dynamic = htab->dynamic_sections_created; |
11666 | dynobj = htab->dynobj; | |
c152c796 | 11667 | |
0e1862bb | 11668 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11669 | || info->emitrelocations); |
c152c796 | 11670 | |
8b127cbc AM |
11671 | flinfo.info = info; |
11672 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11673 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11674 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11675 | return FALSE; |
11676 | ||
11677 | if (! dynamic) | |
11678 | { | |
8b127cbc AM |
11679 | flinfo.hash_sec = NULL; |
11680 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11681 | } |
11682 | else | |
11683 | { | |
3d4d4302 | 11684 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11685 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11686 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11687 | /* Note that it is OK if symver_sec is NULL. */ |
11688 | } | |
11689 | ||
8b127cbc AM |
11690 | flinfo.contents = NULL; |
11691 | flinfo.external_relocs = NULL; | |
11692 | flinfo.internal_relocs = NULL; | |
11693 | flinfo.external_syms = NULL; | |
11694 | flinfo.locsym_shndx = NULL; | |
11695 | flinfo.internal_syms = NULL; | |
11696 | flinfo.indices = NULL; | |
11697 | flinfo.sections = NULL; | |
8b127cbc | 11698 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11699 | flinfo.filesym_count = 0; |
c152c796 | 11700 | |
104d59d1 JM |
11701 | /* The object attributes have been merged. Remove the input |
11702 | sections from the link, and set the contents of the output | |
11703 | secton. */ | |
11704 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11705 | for (o = abfd->sections; o != NULL; o = o->next) | |
11706 | { | |
11707 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
11708 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11709 | { | |
11710 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11711 | { | |
11712 | asection *input_section; | |
11713 | ||
11714 | if (p->type != bfd_indirect_link_order) | |
11715 | continue; | |
11716 | input_section = p->u.indirect.section; | |
11717 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11718 | elf_link_input_bfd ignores this section. */ | |
11719 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11720 | } | |
a0c8462f | 11721 | |
104d59d1 JM |
11722 | attr_size = bfd_elf_obj_attr_size (abfd); |
11723 | if (attr_size) | |
11724 | { | |
11725 | bfd_set_section_size (abfd, o, attr_size); | |
11726 | attr_section = o; | |
11727 | /* Skip this section later on. */ | |
11728 | o->map_head.link_order = NULL; | |
11729 | } | |
11730 | else | |
11731 | o->flags |= SEC_EXCLUDE; | |
11732 | } | |
6e5e9d58 AM |
11733 | else if ((o->flags & SEC_GROUP) != 0 && o->size == 0) |
11734 | { | |
11735 | /* Remove empty group section from linker output. */ | |
11736 | o->flags |= SEC_EXCLUDE; | |
11737 | bfd_section_list_remove (abfd, o); | |
11738 | abfd->section_count--; | |
11739 | } | |
104d59d1 JM |
11740 | } |
11741 | ||
c152c796 AM |
11742 | /* Count up the number of relocations we will output for each output |
11743 | section, so that we know the sizes of the reloc sections. We | |
11744 | also figure out some maximum sizes. */ | |
11745 | max_contents_size = 0; | |
11746 | max_external_reloc_size = 0; | |
11747 | max_internal_reloc_count = 0; | |
11748 | max_sym_count = 0; | |
11749 | max_sym_shndx_count = 0; | |
11750 | merged = FALSE; | |
11751 | for (o = abfd->sections; o != NULL; o = o->next) | |
11752 | { | |
11753 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11754 | o->reloc_count = 0; | |
11755 | ||
8423293d | 11756 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11757 | { |
11758 | unsigned int reloc_count = 0; | |
9eaff861 | 11759 | unsigned int additional_reloc_count = 0; |
c152c796 | 11760 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
11761 | |
11762 | if (p->type == bfd_section_reloc_link_order | |
11763 | || p->type == bfd_symbol_reloc_link_order) | |
11764 | reloc_count = 1; | |
11765 | else if (p->type == bfd_indirect_link_order) | |
11766 | { | |
11767 | asection *sec; | |
11768 | ||
11769 | sec = p->u.indirect.section; | |
c152c796 AM |
11770 | |
11771 | /* Mark all sections which are to be included in the | |
11772 | link. This will normally be every section. We need | |
11773 | to do this so that we can identify any sections which | |
11774 | the linker has decided to not include. */ | |
11775 | sec->linker_mark = TRUE; | |
11776 | ||
11777 | if (sec->flags & SEC_MERGE) | |
11778 | merged = TRUE; | |
11779 | ||
eea6121a AM |
11780 | if (sec->rawsize > max_contents_size) |
11781 | max_contents_size = sec->rawsize; | |
11782 | if (sec->size > max_contents_size) | |
11783 | max_contents_size = sec->size; | |
c152c796 | 11784 | |
c152c796 AM |
11785 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
11786 | && (sec->owner->flags & DYNAMIC) == 0) | |
11787 | { | |
11788 | size_t sym_count; | |
11789 | ||
a961cdd5 AM |
11790 | /* We are interested in just local symbols, not all |
11791 | symbols. */ | |
c152c796 AM |
11792 | if (elf_bad_symtab (sec->owner)) |
11793 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11794 | / bed->s->sizeof_sym); | |
11795 | else | |
11796 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11797 | ||
11798 | if (sym_count > max_sym_count) | |
11799 | max_sym_count = sym_count; | |
11800 | ||
11801 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11802 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11803 | max_sym_shndx_count = sym_count; |
11804 | ||
a961cdd5 AM |
11805 | if (esdo->this_hdr.sh_type == SHT_REL |
11806 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11807 | /* Some backends use reloc_count in relocation sections | |
11808 | to count particular types of relocs. Of course, | |
11809 | reloc sections themselves can't have relocations. */ | |
11810 | ; | |
11811 | else if (emit_relocs) | |
11812 | { | |
11813 | reloc_count = sec->reloc_count; | |
11814 | if (bed->elf_backend_count_additional_relocs) | |
11815 | { | |
11816 | int c; | |
11817 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11818 | additional_reloc_count += c; | |
11819 | } | |
11820 | } | |
11821 | else if (bed->elf_backend_count_relocs) | |
11822 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
11823 | ||
11824 | esdi = elf_section_data (sec); | |
11825 | ||
c152c796 AM |
11826 | if ((sec->flags & SEC_RELOC) != 0) |
11827 | { | |
d4730f92 | 11828 | size_t ext_size = 0; |
c152c796 | 11829 | |
d4730f92 BS |
11830 | if (esdi->rel.hdr != NULL) |
11831 | ext_size = esdi->rel.hdr->sh_size; | |
11832 | if (esdi->rela.hdr != NULL) | |
11833 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11834 | |
c152c796 AM |
11835 | if (ext_size > max_external_reloc_size) |
11836 | max_external_reloc_size = ext_size; | |
11837 | if (sec->reloc_count > max_internal_reloc_count) | |
11838 | max_internal_reloc_count = sec->reloc_count; | |
11839 | } | |
11840 | } | |
11841 | } | |
11842 | ||
11843 | if (reloc_count == 0) | |
11844 | continue; | |
11845 | ||
9eaff861 | 11846 | reloc_count += additional_reloc_count; |
c152c796 AM |
11847 | o->reloc_count += reloc_count; |
11848 | ||
0e1862bb | 11849 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11850 | { |
d4730f92 | 11851 | if (esdi->rel.hdr) |
9eaff861 | 11852 | { |
491d01d3 | 11853 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
11854 | esdo->rel.count += additional_reloc_count; |
11855 | } | |
d4730f92 | 11856 | if (esdi->rela.hdr) |
9eaff861 | 11857 | { |
491d01d3 | 11858 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
11859 | esdo->rela.count += additional_reloc_count; |
11860 | } | |
d4730f92 BS |
11861 | } |
11862 | else | |
11863 | { | |
11864 | if (o->use_rela_p) | |
11865 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11866 | else |
d4730f92 | 11867 | esdo->rel.count += reloc_count; |
c152c796 | 11868 | } |
c152c796 AM |
11869 | } |
11870 | ||
9eaff861 | 11871 | if (o->reloc_count > 0) |
c152c796 AM |
11872 | o->flags |= SEC_RELOC; |
11873 | else | |
11874 | { | |
11875 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11876 | set it (this is probably a bug) and if it is set | |
11877 | assign_section_numbers will create a reloc section. */ | |
11878 | o->flags &=~ SEC_RELOC; | |
11879 | } | |
11880 | ||
11881 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11882 | zero. This is done in elf_fake_sections as well, but forcing | |
11883 | the VMA to 0 here will ensure that relocs against these | |
11884 | sections are handled correctly. */ | |
11885 | if ((o->flags & SEC_ALLOC) == 0 | |
11886 | && ! o->user_set_vma) | |
11887 | o->vma = 0; | |
11888 | } | |
11889 | ||
0e1862bb | 11890 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 11891 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
11892 | |
11893 | /* Figure out the file positions for everything but the symbol table | |
11894 | and the relocs. We set symcount to force assign_section_numbers | |
11895 | to create a symbol table. */ | |
8539e4e8 | 11896 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11897 | BFD_ASSERT (! abfd->output_has_begun); |
11898 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11899 | goto error_return; | |
11900 | ||
ee75fd95 | 11901 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11902 | for (o = abfd->sections; o != NULL; o = o->next) |
11903 | { | |
d4730f92 | 11904 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11905 | if ((o->flags & SEC_RELOC) != 0) |
11906 | { | |
d4730f92 | 11907 | if (esdo->rel.hdr |
9eaff861 | 11908 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
11909 | goto error_return; |
11910 | ||
d4730f92 | 11911 | if (esdo->rela.hdr |
9eaff861 | 11912 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
11913 | goto error_return; |
11914 | } | |
11915 | ||
11916 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
11917 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
11918 | esdo->rel.count = 0; |
11919 | esdo->rela.count = 0; | |
0ce398f1 L |
11920 | |
11921 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
11922 | { | |
11923 | /* Cache the section contents so that they can be compressed | |
11924 | later. Use bfd_malloc since it will be freed by | |
11925 | bfd_compress_section_contents. */ | |
11926 | unsigned char *contents = esdo->this_hdr.contents; | |
11927 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
11928 | abort (); | |
11929 | contents | |
11930 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
11931 | if (contents == NULL) | |
11932 | goto error_return; | |
11933 | esdo->this_hdr.contents = contents; | |
11934 | } | |
c152c796 AM |
11935 | } |
11936 | ||
c152c796 | 11937 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
11938 | .symtab, .strtab, and non-loaded reloc sections. We start the |
11939 | .symtab section at the current file position, and write directly | |
11940 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
11941 | bfd_get_symcount (abfd) = 0; |
11942 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11943 | /* sh_name is set in prep_headers. */ | |
11944 | symtab_hdr->sh_type = SHT_SYMTAB; | |
11945 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
11946 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
11947 | /* sh_link is set in assign_section_numbers. */ | |
11948 | /* sh_info is set below. */ | |
11949 | /* sh_offset is set just below. */ | |
72de5009 | 11950 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 11951 | |
ef10c3ac L |
11952 | if (max_sym_count < 20) |
11953 | max_sym_count = 20; | |
64f52338 | 11954 | htab->strtabsize = max_sym_count; |
ef10c3ac | 11955 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
11956 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
11957 | if (htab->strtab == NULL) | |
c152c796 | 11958 | goto error_return; |
ef10c3ac L |
11959 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
11960 | flinfo.symshndxbuf | |
11961 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
11962 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 11963 | |
8539e4e8 | 11964 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 11965 | { |
8539e4e8 AM |
11966 | file_ptr off = elf_next_file_pos (abfd); |
11967 | ||
11968 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
11969 | ||
11970 | /* Note that at this point elf_next_file_pos (abfd) is | |
11971 | incorrect. We do not yet know the size of the .symtab section. | |
11972 | We correct next_file_pos below, after we do know the size. */ | |
11973 | ||
11974 | /* Start writing out the symbol table. The first symbol is always a | |
11975 | dummy symbol. */ | |
c152c796 AM |
11976 | elfsym.st_value = 0; |
11977 | elfsym.st_size = 0; | |
11978 | elfsym.st_info = 0; | |
11979 | elfsym.st_other = 0; | |
11980 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 11981 | elfsym.st_target_internal = 0; |
ef10c3ac L |
11982 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
11983 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 11984 | goto error_return; |
c152c796 | 11985 | |
8539e4e8 AM |
11986 | /* Output a symbol for each section. We output these even if we are |
11987 | discarding local symbols, since they are used for relocs. These | |
11988 | symbols have no names. We store the index of each one in the | |
11989 | index field of the section, so that we can find it again when | |
11990 | outputting relocs. */ | |
11991 | ||
c152c796 AM |
11992 | elfsym.st_size = 0; |
11993 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11994 | elfsym.st_other = 0; | |
f0b5bb34 | 11995 | elfsym.st_value = 0; |
35fc36a8 | 11996 | elfsym.st_target_internal = 0; |
c152c796 AM |
11997 | for (i = 1; i < elf_numsections (abfd); i++) |
11998 | { | |
11999 | o = bfd_section_from_elf_index (abfd, i); | |
12000 | if (o != NULL) | |
f0b5bb34 AM |
12001 | { |
12002 | o->target_index = bfd_get_symcount (abfd); | |
12003 | elfsym.st_shndx = i; | |
0e1862bb | 12004 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 12005 | elfsym.st_value = o->vma; |
ef10c3ac L |
12006 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
12007 | NULL) != 1) | |
f0b5bb34 AM |
12008 | goto error_return; |
12009 | } | |
c152c796 AM |
12010 | } |
12011 | } | |
12012 | ||
12013 | /* Allocate some memory to hold information read in from the input | |
12014 | files. */ | |
12015 | if (max_contents_size != 0) | |
12016 | { | |
8b127cbc AM |
12017 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
12018 | if (flinfo.contents == NULL) | |
c152c796 AM |
12019 | goto error_return; |
12020 | } | |
12021 | ||
12022 | if (max_external_reloc_size != 0) | |
12023 | { | |
8b127cbc AM |
12024 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
12025 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
12026 | goto error_return; |
12027 | } | |
12028 | ||
12029 | if (max_internal_reloc_count != 0) | |
12030 | { | |
056bafd4 | 12031 | amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); |
8b127cbc AM |
12032 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
12033 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
12034 | goto error_return; |
12035 | } | |
12036 | ||
12037 | if (max_sym_count != 0) | |
12038 | { | |
12039 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
12040 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
12041 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
12042 | goto error_return; |
12043 | ||
12044 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
12045 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
12046 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
12047 | goto error_return; |
12048 | ||
12049 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
12050 | flinfo.indices = (long int *) bfd_malloc (amt); |
12051 | if (flinfo.indices == NULL) | |
c152c796 AM |
12052 | goto error_return; |
12053 | ||
12054 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
12055 | flinfo.sections = (asection **) bfd_malloc (amt); |
12056 | if (flinfo.sections == NULL) | |
c152c796 AM |
12057 | goto error_return; |
12058 | } | |
12059 | ||
12060 | if (max_sym_shndx_count != 0) | |
12061 | { | |
12062 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
12063 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
12064 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
12065 | goto error_return; |
12066 | } | |
12067 | ||
64f52338 | 12068 | if (htab->tls_sec) |
c152c796 AM |
12069 | { |
12070 | bfd_vma base, end = 0; | |
12071 | asection *sec; | |
12072 | ||
64f52338 | 12073 | for (sec = htab->tls_sec; |
c152c796 AM |
12074 | sec && (sec->flags & SEC_THREAD_LOCAL); |
12075 | sec = sec->next) | |
12076 | { | |
3a800eb9 | 12077 | bfd_size_type size = sec->size; |
c152c796 | 12078 | |
3a800eb9 AM |
12079 | if (size == 0 |
12080 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 12081 | { |
91d6fa6a NC |
12082 | struct bfd_link_order *ord = sec->map_tail.link_order; |
12083 | ||
12084 | if (ord != NULL) | |
12085 | size = ord->offset + ord->size; | |
c152c796 AM |
12086 | } |
12087 | end = sec->vma + size; | |
12088 | } | |
64f52338 | 12089 | base = htab->tls_sec->vma; |
7dc98aea RO |
12090 | /* Only align end of TLS section if static TLS doesn't have special |
12091 | alignment requirements. */ | |
12092 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
12093 | end = align_power (end, htab->tls_sec->alignment_power); |
12094 | htab->tls_size = end - base; | |
c152c796 AM |
12095 | } |
12096 | ||
0b52efa6 PB |
12097 | /* Reorder SHF_LINK_ORDER sections. */ |
12098 | for (o = abfd->sections; o != NULL; o = o->next) | |
12099 | { | |
12100 | if (!elf_fixup_link_order (abfd, o)) | |
12101 | return FALSE; | |
12102 | } | |
12103 | ||
2f0c68f2 CM |
12104 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
12105 | return FALSE; | |
12106 | ||
c152c796 AM |
12107 | /* Since ELF permits relocations to be against local symbols, we |
12108 | must have the local symbols available when we do the relocations. | |
12109 | Since we would rather only read the local symbols once, and we | |
12110 | would rather not keep them in memory, we handle all the | |
12111 | relocations for a single input file at the same time. | |
12112 | ||
12113 | Unfortunately, there is no way to know the total number of local | |
12114 | symbols until we have seen all of them, and the local symbol | |
12115 | indices precede the global symbol indices. This means that when | |
12116 | we are generating relocatable output, and we see a reloc against | |
12117 | a global symbol, we can not know the symbol index until we have | |
12118 | finished examining all the local symbols to see which ones we are | |
12119 | going to output. To deal with this, we keep the relocations in | |
12120 | memory, and don't output them until the end of the link. This is | |
12121 | an unfortunate waste of memory, but I don't see a good way around | |
12122 | it. Fortunately, it only happens when performing a relocatable | |
12123 | link, which is not the common case. FIXME: If keep_memory is set | |
12124 | we could write the relocs out and then read them again; I don't | |
12125 | know how bad the memory loss will be. */ | |
12126 | ||
c72f2fb2 | 12127 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12128 | sub->output_has_begun = FALSE; |
12129 | for (o = abfd->sections; o != NULL; o = o->next) | |
12130 | { | |
8423293d | 12131 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12132 | { |
12133 | if (p->type == bfd_indirect_link_order | |
12134 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
12135 | == bfd_target_elf_flavour) | |
12136 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
12137 | { | |
12138 | if (! sub->output_has_begun) | |
12139 | { | |
8b127cbc | 12140 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
12141 | goto error_return; |
12142 | sub->output_has_begun = TRUE; | |
12143 | } | |
12144 | } | |
12145 | else if (p->type == bfd_section_reloc_link_order | |
12146 | || p->type == bfd_symbol_reloc_link_order) | |
12147 | { | |
12148 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
12149 | goto error_return; | |
12150 | } | |
12151 | else | |
12152 | { | |
12153 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
12154 | { |
12155 | if (p->type == bfd_indirect_link_order | |
12156 | && (bfd_get_flavour (sub) | |
12157 | == bfd_target_elf_flavour) | |
12158 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
12159 | != bed->s->elfclass)) | |
12160 | { | |
12161 | const char *iclass, *oclass; | |
12162 | ||
aebf9be7 | 12163 | switch (bed->s->elfclass) |
351f65ca | 12164 | { |
aebf9be7 NC |
12165 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
12166 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
12167 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
12168 | default: abort (); | |
351f65ca | 12169 | } |
aebf9be7 NC |
12170 | |
12171 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 12172 | { |
aebf9be7 NC |
12173 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
12174 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
12175 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
12176 | default: abort (); | |
351f65ca L |
12177 | } |
12178 | ||
12179 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 12180 | _bfd_error_handler |
695344c0 | 12181 | /* xgettext:c-format */ |
871b3ab2 | 12182 | (_("%pB: file class %s incompatible with %s"), |
351f65ca L |
12183 | sub, iclass, oclass); |
12184 | } | |
12185 | ||
12186 | goto error_return; | |
12187 | } | |
c152c796 AM |
12188 | } |
12189 | } | |
12190 | } | |
12191 | ||
c0f00686 L |
12192 | /* Free symbol buffer if needed. */ |
12193 | if (!info->reduce_memory_overheads) | |
12194 | { | |
c72f2fb2 | 12195 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
12196 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
12197 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
12198 | { |
12199 | free (elf_tdata (sub)->symbuf); | |
12200 | elf_tdata (sub)->symbuf = NULL; | |
12201 | } | |
12202 | } | |
12203 | ||
c152c796 AM |
12204 | /* Output any global symbols that got converted to local in a |
12205 | version script or due to symbol visibility. We do this in a | |
12206 | separate step since ELF requires all local symbols to appear | |
12207 | prior to any global symbols. FIXME: We should only do this if | |
12208 | some global symbols were, in fact, converted to become local. | |
12209 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
12210 | eoinfo.failed = FALSE; | |
8b127cbc | 12211 | eoinfo.flinfo = &flinfo; |
c152c796 | 12212 | eoinfo.localsyms = TRUE; |
34a79995 | 12213 | eoinfo.file_sym_done = FALSE; |
7686d77d | 12214 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12215 | if (eoinfo.failed) |
12216 | return FALSE; | |
12217 | ||
4e617b1e PB |
12218 | /* If backend needs to output some local symbols not present in the hash |
12219 | table, do it now. */ | |
8539e4e8 AM |
12220 | if (bed->elf_backend_output_arch_local_syms |
12221 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 12222 | { |
6e0b88f1 | 12223 | typedef int (*out_sym_func) |
4e617b1e PB |
12224 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12225 | struct elf_link_hash_entry *); | |
12226 | ||
12227 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
12228 | (abfd, info, &flinfo, |
12229 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
12230 | return FALSE; |
12231 | } | |
12232 | ||
c152c796 AM |
12233 | /* That wrote out all the local symbols. Finish up the symbol table |
12234 | with the global symbols. Even if we want to strip everything we | |
12235 | can, we still need to deal with those global symbols that got | |
12236 | converted to local in a version script. */ | |
12237 | ||
12238 | /* The sh_info field records the index of the first non local symbol. */ | |
12239 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
12240 | ||
12241 | if (dynamic | |
64f52338 AM |
12242 | && htab->dynsym != NULL |
12243 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
12244 | { |
12245 | Elf_Internal_Sym sym; | |
64f52338 | 12246 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 12247 | |
64f52338 AM |
12248 | o = htab->dynsym->output_section; |
12249 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
12250 | |
12251 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 12252 | if (bfd_link_pic (info) |
64f52338 | 12253 | || htab->is_relocatable_executable) |
c152c796 AM |
12254 | { |
12255 | asection *s; | |
12256 | ||
12257 | sym.st_size = 0; | |
12258 | sym.st_name = 0; | |
12259 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12260 | sym.st_other = 0; | |
35fc36a8 | 12261 | sym.st_target_internal = 0; |
c152c796 AM |
12262 | |
12263 | for (s = abfd->sections; s != NULL; s = s->next) | |
12264 | { | |
12265 | int indx; | |
12266 | bfd_byte *dest; | |
12267 | long dynindx; | |
12268 | ||
c152c796 | 12269 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
12270 | if (dynindx <= 0) |
12271 | continue; | |
12272 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
12273 | BFD_ASSERT (indx > 0); |
12274 | sym.st_shndx = indx; | |
c0d5a53d L |
12275 | if (! check_dynsym (abfd, &sym)) |
12276 | return FALSE; | |
c152c796 AM |
12277 | sym.st_value = s->vma; |
12278 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
12279 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12280 | } | |
c152c796 AM |
12281 | } |
12282 | ||
12283 | /* Write out the local dynsyms. */ | |
64f52338 | 12284 | if (htab->dynlocal) |
c152c796 AM |
12285 | { |
12286 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 12287 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
12288 | { |
12289 | asection *s; | |
12290 | bfd_byte *dest; | |
12291 | ||
935bd1e0 | 12292 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
12293 | Note that we saved a word of storage and overwrote |
12294 | the original st_name with the dynstr_index. */ | |
12295 | sym = e->isym; | |
935bd1e0 | 12296 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 12297 | |
cb33740c AM |
12298 | s = bfd_section_from_elf_index (e->input_bfd, |
12299 | e->isym.st_shndx); | |
12300 | if (s != NULL) | |
c152c796 | 12301 | { |
c152c796 AM |
12302 | sym.st_shndx = |
12303 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
12304 | if (! check_dynsym (abfd, &sym)) |
12305 | return FALSE; | |
c152c796 AM |
12306 | sym.st_value = (s->output_section->vma |
12307 | + s->output_offset | |
12308 | + e->isym.st_value); | |
12309 | } | |
12310 | ||
c152c796 AM |
12311 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
12312 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12313 | } | |
12314 | } | |
c152c796 AM |
12315 | } |
12316 | ||
12317 | /* We get the global symbols from the hash table. */ | |
12318 | eoinfo.failed = FALSE; | |
12319 | eoinfo.localsyms = FALSE; | |
8b127cbc | 12320 | eoinfo.flinfo = &flinfo; |
7686d77d | 12321 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12322 | if (eoinfo.failed) |
12323 | return FALSE; | |
12324 | ||
12325 | /* If backend needs to output some symbols not present in the hash | |
12326 | table, do it now. */ | |
8539e4e8 AM |
12327 | if (bed->elf_backend_output_arch_syms |
12328 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 12329 | { |
6e0b88f1 | 12330 | typedef int (*out_sym_func) |
c152c796 AM |
12331 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12332 | struct elf_link_hash_entry *); | |
12333 | ||
12334 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
12335 | (abfd, info, &flinfo, |
12336 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
12337 | return FALSE; |
12338 | } | |
12339 | ||
ef10c3ac L |
12340 | /* Finalize the .strtab section. */ |
12341 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
12342 | ||
12343 | /* Swap out the .strtab section. */ | |
12344 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
12345 | return FALSE; |
12346 | ||
12347 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
12348 | if (bfd_get_symcount (abfd) > 0) |
12349 | { | |
ee3b52e9 L |
12350 | /* Finish up and write out the symbol string table (.strtab) |
12351 | section. */ | |
ad32986f | 12352 | Elf_Internal_Shdr *symstrtab_hdr = NULL; |
8539e4e8 AM |
12353 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
12354 | ||
ad32986f | 12355 | if (elf_symtab_shndx_list (abfd)) |
8539e4e8 | 12356 | { |
ad32986f | 12357 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8539e4e8 | 12358 | |
ad32986f NC |
12359 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) |
12360 | { | |
12361 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
12362 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
12363 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
12364 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
12365 | symtab_shndx_hdr->sh_size = amt; | |
8539e4e8 | 12366 | |
ad32986f NC |
12367 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, |
12368 | off, TRUE); | |
12369 | ||
12370 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
12371 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
12372 | return FALSE; | |
12373 | } | |
8539e4e8 | 12374 | } |
ee3b52e9 L |
12375 | |
12376 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
12377 | /* sh_name was set in prep_headers. */ | |
12378 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 12379 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 12380 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 12381 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
12382 | symstrtab_hdr->sh_entsize = 0; |
12383 | symstrtab_hdr->sh_link = 0; | |
12384 | symstrtab_hdr->sh_info = 0; | |
12385 | /* sh_offset is set just below. */ | |
12386 | symstrtab_hdr->sh_addralign = 1; | |
12387 | ||
12388 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
12389 | off, TRUE); | |
12390 | elf_next_file_pos (abfd) = off; | |
12391 | ||
c152c796 | 12392 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 12393 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
12394 | return FALSE; |
12395 | } | |
12396 | ||
76359541 TP |
12397 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
12398 | { | |
871b3ab2 | 12399 | _bfd_error_handler (_("%pB: failed to generate import library"), |
4eca0228 | 12400 | info->out_implib_bfd); |
76359541 TP |
12401 | return FALSE; |
12402 | } | |
12403 | ||
c152c796 AM |
12404 | /* Adjust the relocs to have the correct symbol indices. */ |
12405 | for (o = abfd->sections; o != NULL; o = o->next) | |
12406 | { | |
d4730f92 | 12407 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 12408 | bfd_boolean sort; |
10bbbc1d | 12409 | |
c152c796 AM |
12410 | if ((o->flags & SEC_RELOC) == 0) |
12411 | continue; | |
12412 | ||
28dbcedc | 12413 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 12414 | if (esdo->rel.hdr != NULL |
10bbbc1d | 12415 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) |
bca6d0e3 AM |
12416 | return FALSE; |
12417 | if (esdo->rela.hdr != NULL | |
10bbbc1d | 12418 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) |
bca6d0e3 | 12419 | return FALSE; |
c152c796 AM |
12420 | |
12421 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
12422 | trying to swap the relocs out itself. */ | |
12423 | o->reloc_count = 0; | |
12424 | } | |
12425 | ||
12426 | if (dynamic && info->combreloc && dynobj != NULL) | |
12427 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
12428 | ||
12429 | /* If we are linking against a dynamic object, or generating a | |
12430 | shared library, finish up the dynamic linking information. */ | |
12431 | if (dynamic) | |
12432 | { | |
12433 | bfd_byte *dyncon, *dynconend; | |
12434 | ||
12435 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12436 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12437 | BFD_ASSERT (o != NULL); |
12438 | ||
12439 | dyncon = o->contents; | |
eea6121a | 12440 | dynconend = o->contents + o->size; |
c152c796 AM |
12441 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12442 | { | |
12443 | Elf_Internal_Dyn dyn; | |
12444 | const char *name; | |
12445 | unsigned int type; | |
64487780 AM |
12446 | bfd_size_type sh_size; |
12447 | bfd_vma sh_addr; | |
c152c796 AM |
12448 | |
12449 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12450 | ||
12451 | switch (dyn.d_tag) | |
12452 | { | |
12453 | default: | |
12454 | continue; | |
12455 | case DT_NULL: | |
12456 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12457 | { | |
12458 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12459 | { | |
12460 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12461 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12462 | default: continue; | |
12463 | } | |
12464 | dyn.d_un.d_val = relativecount; | |
12465 | relativecount = 0; | |
12466 | break; | |
12467 | } | |
12468 | continue; | |
12469 | ||
12470 | case DT_INIT: | |
12471 | name = info->init_function; | |
12472 | goto get_sym; | |
12473 | case DT_FINI: | |
12474 | name = info->fini_function; | |
12475 | get_sym: | |
12476 | { | |
12477 | struct elf_link_hash_entry *h; | |
12478 | ||
64f52338 | 12479 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12480 | if (h != NULL |
12481 | && (h->root.type == bfd_link_hash_defined | |
12482 | || h->root.type == bfd_link_hash_defweak)) | |
12483 | { | |
bef26483 | 12484 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12485 | o = h->root.u.def.section; |
12486 | if (o->output_section != NULL) | |
bef26483 | 12487 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12488 | + o->output_offset); |
12489 | else | |
12490 | { | |
12491 | /* The symbol is imported from another shared | |
12492 | library and does not apply to this one. */ | |
bef26483 | 12493 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12494 | } |
12495 | break; | |
12496 | } | |
12497 | } | |
12498 | continue; | |
12499 | ||
12500 | case DT_PREINIT_ARRAYSZ: | |
12501 | name = ".preinit_array"; | |
4ade44b7 | 12502 | goto get_out_size; |
c152c796 AM |
12503 | case DT_INIT_ARRAYSZ: |
12504 | name = ".init_array"; | |
4ade44b7 | 12505 | goto get_out_size; |
c152c796 AM |
12506 | case DT_FINI_ARRAYSZ: |
12507 | name = ".fini_array"; | |
4ade44b7 | 12508 | get_out_size: |
c152c796 AM |
12509 | o = bfd_get_section_by_name (abfd, name); |
12510 | if (o == NULL) | |
12511 | { | |
4eca0228 | 12512 | _bfd_error_handler |
4ade44b7 | 12513 | (_("could not find section %s"), name); |
c152c796 AM |
12514 | goto error_return; |
12515 | } | |
eea6121a | 12516 | if (o->size == 0) |
4eca0228 | 12517 | _bfd_error_handler |
c152c796 | 12518 | (_("warning: %s section has zero size"), name); |
eea6121a | 12519 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12520 | break; |
12521 | ||
12522 | case DT_PREINIT_ARRAY: | |
12523 | name = ".preinit_array"; | |
4ade44b7 | 12524 | goto get_out_vma; |
c152c796 AM |
12525 | case DT_INIT_ARRAY: |
12526 | name = ".init_array"; | |
4ade44b7 | 12527 | goto get_out_vma; |
c152c796 AM |
12528 | case DT_FINI_ARRAY: |
12529 | name = ".fini_array"; | |
4ade44b7 AM |
12530 | get_out_vma: |
12531 | o = bfd_get_section_by_name (abfd, name); | |
12532 | goto do_vma; | |
c152c796 AM |
12533 | |
12534 | case DT_HASH: | |
12535 | name = ".hash"; | |
12536 | goto get_vma; | |
fdc90cb4 JJ |
12537 | case DT_GNU_HASH: |
12538 | name = ".gnu.hash"; | |
12539 | goto get_vma; | |
c152c796 AM |
12540 | case DT_STRTAB: |
12541 | name = ".dynstr"; | |
12542 | goto get_vma; | |
12543 | case DT_SYMTAB: | |
12544 | name = ".dynsym"; | |
12545 | goto get_vma; | |
12546 | case DT_VERDEF: | |
12547 | name = ".gnu.version_d"; | |
12548 | goto get_vma; | |
12549 | case DT_VERNEED: | |
12550 | name = ".gnu.version_r"; | |
12551 | goto get_vma; | |
12552 | case DT_VERSYM: | |
12553 | name = ".gnu.version"; | |
12554 | get_vma: | |
4ade44b7 AM |
12555 | o = bfd_get_linker_section (dynobj, name); |
12556 | do_vma: | |
b3293efa | 12557 | if (o == NULL || bfd_is_abs_section (o->output_section)) |
c152c796 | 12558 | { |
4eca0228 | 12559 | _bfd_error_handler |
4ade44b7 | 12560 | (_("could not find section %s"), name); |
c152c796 AM |
12561 | goto error_return; |
12562 | } | |
894891db NC |
12563 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12564 | { | |
4eca0228 | 12565 | _bfd_error_handler |
894891db NC |
12566 | (_("warning: section '%s' is being made into a note"), name); |
12567 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12568 | goto error_return; | |
12569 | } | |
4ade44b7 | 12570 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12571 | break; |
12572 | ||
12573 | case DT_REL: | |
12574 | case DT_RELA: | |
12575 | case DT_RELSZ: | |
12576 | case DT_RELASZ: | |
12577 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12578 | type = SHT_REL; | |
12579 | else | |
12580 | type = SHT_RELA; | |
64487780 AM |
12581 | sh_size = 0; |
12582 | sh_addr = 0; | |
c152c796 AM |
12583 | for (i = 1; i < elf_numsections (abfd); i++) |
12584 | { | |
12585 | Elf_Internal_Shdr *hdr; | |
12586 | ||
12587 | hdr = elf_elfsections (abfd)[i]; | |
12588 | if (hdr->sh_type == type | |
12589 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12590 | { | |
64487780 AM |
12591 | sh_size += hdr->sh_size; |
12592 | if (sh_addr == 0 | |
12593 | || sh_addr > hdr->sh_addr) | |
12594 | sh_addr = hdr->sh_addr; | |
c152c796 AM |
12595 | } |
12596 | } | |
64487780 | 12597 | |
64f52338 AM |
12598 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12599 | { | |
12600 | /* Don't count procedure linkage table relocs in the | |
12601 | overall reloc count. */ | |
64487780 AM |
12602 | sh_size -= htab->srelplt->size; |
12603 | if (sh_size == 0) | |
12604 | /* If the size is zero, make the address zero too. | |
12605 | This is to avoid a glibc bug. If the backend | |
12606 | emits DT_RELA/DT_RELASZ even when DT_RELASZ is | |
12607 | zero, then we'll put DT_RELA at the end of | |
12608 | DT_JMPREL. glibc will interpret the end of | |
12609 | DT_RELA matching the end of DT_JMPREL as the | |
12610 | case where DT_RELA includes DT_JMPREL, and for | |
12611 | LD_BIND_NOW will decide that processing DT_RELA | |
12612 | will process the PLT relocs too. Net result: | |
12613 | No PLT relocs applied. */ | |
12614 | sh_addr = 0; | |
12615 | ||
64f52338 AM |
12616 | /* If .rela.plt is the first .rela section, exclude |
12617 | it from DT_RELA. */ | |
64487780 AM |
12618 | else if (sh_addr == (htab->srelplt->output_section->vma |
12619 | + htab->srelplt->output_offset)) | |
12620 | sh_addr += htab->srelplt->size; | |
64f52338 | 12621 | } |
64487780 AM |
12622 | |
12623 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12624 | dyn.d_un.d_val = sh_size; | |
12625 | else | |
12626 | dyn.d_un.d_ptr = sh_addr; | |
c152c796 AM |
12627 | break; |
12628 | } | |
12629 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12630 | } | |
12631 | } | |
12632 | ||
12633 | /* If we have created any dynamic sections, then output them. */ | |
12634 | if (dynobj != NULL) | |
12635 | { | |
12636 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12637 | goto error_return; | |
12638 | ||
943284cc | 12639 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12640 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12641 | || info->error_textrel) |
3d4d4302 | 12642 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12643 | { |
12644 | bfd_byte *dyncon, *dynconend; | |
12645 | ||
943284cc DJ |
12646 | dyncon = o->contents; |
12647 | dynconend = o->contents + o->size; | |
12648 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12649 | { | |
12650 | Elf_Internal_Dyn dyn; | |
12651 | ||
12652 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12653 | ||
12654 | if (dyn.d_tag == DT_TEXTREL) | |
12655 | { | |
c192a133 AM |
12656 | if (info->error_textrel) |
12657 | info->callbacks->einfo | |
9793eb77 | 12658 | (_("%P%X: read-only segment has dynamic relocations\n")); |
c192a133 AM |
12659 | else |
12660 | info->callbacks->einfo | |
9793eb77 | 12661 | (_("%P: warning: creating a DT_TEXTREL in a shared object\n")); |
943284cc DJ |
12662 | break; |
12663 | } | |
12664 | } | |
12665 | } | |
12666 | ||
c152c796 AM |
12667 | for (o = dynobj->sections; o != NULL; o = o->next) |
12668 | { | |
12669 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12670 | || o->size == 0 |
c152c796 AM |
12671 | || o->output_section == bfd_abs_section_ptr) |
12672 | continue; | |
12673 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12674 | { | |
12675 | /* At this point, we are only interested in sections | |
12676 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12677 | continue; | |
12678 | } | |
64f52338 | 12679 | if (htab->stab_info.stabstr == o) |
3722b82f | 12680 | continue; |
64f52338 | 12681 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12682 | continue; |
3d4d4302 | 12683 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12684 | { |
12685 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12686 | o->contents, | |
37b01f6a DG |
12687 | (file_ptr) o->output_offset |
12688 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12689 | o->size)) |
c152c796 AM |
12690 | goto error_return; |
12691 | } | |
12692 | else | |
12693 | { | |
12694 | /* The contents of the .dynstr section are actually in a | |
12695 | stringtab. */ | |
8539e4e8 AM |
12696 | file_ptr off; |
12697 | ||
c152c796 AM |
12698 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12699 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12700 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12701 | goto error_return; |
12702 | } | |
12703 | } | |
12704 | } | |
12705 | ||
7bdf4127 | 12706 | if (!info->resolve_section_groups) |
c152c796 AM |
12707 | { |
12708 | bfd_boolean failed = FALSE; | |
12709 | ||
7bdf4127 | 12710 | BFD_ASSERT (bfd_link_relocatable (info)); |
c152c796 AM |
12711 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
12712 | if (failed) | |
12713 | goto error_return; | |
12714 | } | |
12715 | ||
12716 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 12717 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 12718 | { |
64f52338 | 12719 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
12720 | goto error_return; |
12721 | } | |
12722 | ||
9f7c3e5e AM |
12723 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12724 | goto error_return; | |
c152c796 | 12725 | |
9f7c3e5e | 12726 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12727 | |
12bd6957 | 12728 | elf_linker (abfd) = TRUE; |
c152c796 | 12729 | |
104d59d1 JM |
12730 | if (attr_section) |
12731 | { | |
a50b1753 | 12732 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12733 | if (contents == NULL) |
d0f16d5e | 12734 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12735 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12736 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12737 | free (contents); | |
12738 | } | |
12739 | ||
c152c796 AM |
12740 | return TRUE; |
12741 | ||
12742 | error_return: | |
9f7c3e5e | 12743 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12744 | return FALSE; |
12745 | } | |
12746 | \f | |
5241d853 RS |
12747 | /* Initialize COOKIE for input bfd ABFD. */ |
12748 | ||
12749 | static bfd_boolean | |
12750 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12751 | struct bfd_link_info *info, bfd *abfd) | |
12752 | { | |
12753 | Elf_Internal_Shdr *symtab_hdr; | |
12754 | const struct elf_backend_data *bed; | |
12755 | ||
12756 | bed = get_elf_backend_data (abfd); | |
12757 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12758 | ||
12759 | cookie->abfd = abfd; | |
12760 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12761 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12762 | if (cookie->bad_symtab) | |
12763 | { | |
12764 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12765 | cookie->extsymoff = 0; | |
12766 | } | |
12767 | else | |
12768 | { | |
12769 | cookie->locsymcount = symtab_hdr->sh_info; | |
12770 | cookie->extsymoff = symtab_hdr->sh_info; | |
12771 | } | |
12772 | ||
12773 | if (bed->s->arch_size == 32) | |
12774 | cookie->r_sym_shift = 8; | |
12775 | else | |
12776 | cookie->r_sym_shift = 32; | |
12777 | ||
12778 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12779 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12780 | { | |
12781 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12782 | cookie->locsymcount, 0, | |
12783 | NULL, NULL, NULL); | |
12784 | if (cookie->locsyms == NULL) | |
12785 | { | |
12786 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12787 | return FALSE; | |
12788 | } | |
12789 | if (info->keep_memory) | |
12790 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12791 | } | |
12792 | return TRUE; | |
12793 | } | |
12794 | ||
12795 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12796 | ||
12797 | static void | |
12798 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12799 | { | |
12800 | Elf_Internal_Shdr *symtab_hdr; | |
12801 | ||
12802 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12803 | if (cookie->locsyms != NULL | |
12804 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12805 | free (cookie->locsyms); | |
12806 | } | |
12807 | ||
12808 | /* Initialize the relocation information in COOKIE for input section SEC | |
12809 | of input bfd ABFD. */ | |
12810 | ||
12811 | static bfd_boolean | |
12812 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12813 | struct bfd_link_info *info, bfd *abfd, | |
12814 | asection *sec) | |
12815 | { | |
5241d853 RS |
12816 | if (sec->reloc_count == 0) |
12817 | { | |
12818 | cookie->rels = NULL; | |
12819 | cookie->relend = NULL; | |
12820 | } | |
12821 | else | |
12822 | { | |
5241d853 RS |
12823 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, |
12824 | info->keep_memory); | |
12825 | if (cookie->rels == NULL) | |
12826 | return FALSE; | |
12827 | cookie->rel = cookie->rels; | |
056bafd4 | 12828 | cookie->relend = cookie->rels + sec->reloc_count; |
5241d853 RS |
12829 | } |
12830 | cookie->rel = cookie->rels; | |
12831 | return TRUE; | |
12832 | } | |
12833 | ||
12834 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12835 | if appropriate. */ | |
12836 | ||
12837 | static void | |
12838 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12839 | asection *sec) | |
12840 | { | |
12841 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12842 | free (cookie->rels); | |
12843 | } | |
12844 | ||
12845 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12846 | ||
12847 | static bfd_boolean | |
12848 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12849 | struct bfd_link_info *info, | |
12850 | asection *sec) | |
12851 | { | |
12852 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12853 | goto error1; | |
12854 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12855 | goto error2; | |
12856 | return TRUE; | |
12857 | ||
12858 | error2: | |
12859 | fini_reloc_cookie (cookie, sec->owner); | |
12860 | error1: | |
12861 | return FALSE; | |
12862 | } | |
12863 | ||
12864 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12865 | if appropriate. */ | |
12866 | ||
12867 | static void | |
12868 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12869 | asection *sec) | |
12870 | { | |
12871 | fini_reloc_cookie_rels (cookie, sec); | |
12872 | fini_reloc_cookie (cookie, sec->owner); | |
12873 | } | |
12874 | \f | |
c152c796 AM |
12875 | /* Garbage collect unused sections. */ |
12876 | ||
07adf181 AM |
12877 | /* Default gc_mark_hook. */ |
12878 | ||
12879 | asection * | |
12880 | _bfd_elf_gc_mark_hook (asection *sec, | |
12881 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12882 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12883 | struct elf_link_hash_entry *h, | |
12884 | Elf_Internal_Sym *sym) | |
12885 | { | |
12886 | if (h != NULL) | |
12887 | { | |
12888 | switch (h->root.type) | |
12889 | { | |
12890 | case bfd_link_hash_defined: | |
12891 | case bfd_link_hash_defweak: | |
12892 | return h->root.u.def.section; | |
12893 | ||
12894 | case bfd_link_hash_common: | |
12895 | return h->root.u.c.p->section; | |
12896 | ||
12897 | default: | |
12898 | break; | |
12899 | } | |
12900 | } | |
12901 | else | |
12902 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12903 | ||
12904 | return NULL; | |
12905 | } | |
12906 | ||
9e223787 | 12907 | /* Return the debug definition section. */ |
b7c871ed L |
12908 | |
12909 | static asection * | |
12910 | elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, | |
12911 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12912 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12913 | struct elf_link_hash_entry *h, | |
9e223787 | 12914 | Elf_Internal_Sym *sym) |
b7c871ed | 12915 | { |
9e223787 L |
12916 | if (h != NULL) |
12917 | { | |
12918 | /* Return the global debug definition section. */ | |
12919 | if ((h->root.type == bfd_link_hash_defined | |
12920 | || h->root.type == bfd_link_hash_defweak) | |
12921 | && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) | |
12922 | return h->root.u.def.section; | |
12923 | } | |
12924 | else | |
12925 | { | |
12926 | /* Return the local debug definition section. */ | |
12927 | asection *isec = bfd_section_from_elf_index (sec->owner, | |
12928 | sym->st_shndx); | |
12929 | if ((isec->flags & SEC_DEBUGGING) != 0) | |
12930 | return isec; | |
12931 | } | |
b7c871ed L |
12932 | |
12933 | return NULL; | |
12934 | } | |
12935 | ||
5241d853 RS |
12936 | /* COOKIE->rel describes a relocation against section SEC, which is |
12937 | a section we've decided to keep. Return the section that contains | |
12938 | the relocation symbol, or NULL if no section contains it. */ | |
12939 | ||
12940 | asection * | |
12941 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
12942 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
12943 | struct elf_reloc_cookie *cookie, |
12944 | bfd_boolean *start_stop) | |
5241d853 RS |
12945 | { |
12946 | unsigned long r_symndx; | |
12947 | struct elf_link_hash_entry *h; | |
12948 | ||
12949 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 12950 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
12951 | return NULL; |
12952 | ||
12953 | if (r_symndx >= cookie->locsymcount | |
12954 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12955 | { | |
12956 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
12957 | if (h == NULL) |
12958 | { | |
871b3ab2 | 12959 | info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"), |
263ddf68 L |
12960 | sec->owner); |
12961 | return NULL; | |
12962 | } | |
5241d853 RS |
12963 | while (h->root.type == bfd_link_hash_indirect |
12964 | || h->root.type == bfd_link_hash_warning) | |
12965 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 12966 | h->mark = 1; |
4e6b54a6 AM |
12967 | /* If this symbol is weak and there is a non-weak definition, we |
12968 | keep the non-weak definition because many backends put | |
12969 | dynamic reloc info on the non-weak definition for code | |
12970 | handling copy relocs. */ | |
60d67dc8 AM |
12971 | if (h->is_weakalias) |
12972 | weakdef (h)->mark = 1; | |
1cce69b9 | 12973 | |
a6a4679f | 12974 | if (start_stop != NULL) |
1cce69b9 | 12975 | { |
7dba9362 AM |
12976 | /* To work around a glibc bug, mark XXX input sections |
12977 | when there is a reference to __start_XXX or __stop_XXX | |
12978 | symbols. */ | |
cbd0eecf | 12979 | if (h->start_stop) |
1cce69b9 | 12980 | { |
cbd0eecf | 12981 | asection *s = h->u2.start_stop_section; |
a6a4679f AM |
12982 | *start_stop = !s->gc_mark; |
12983 | return s; | |
1cce69b9 AM |
12984 | } |
12985 | } | |
12986 | ||
5241d853 RS |
12987 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
12988 | } | |
12989 | ||
12990 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
12991 | &cookie->locsyms[r_symndx]); | |
12992 | } | |
12993 | ||
12994 | /* COOKIE->rel describes a relocation against section SEC, which is | |
12995 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 12996 | the relocation symbol. */ |
5241d853 RS |
12997 | |
12998 | bfd_boolean | |
12999 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
13000 | asection *sec, | |
13001 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 13002 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
13003 | { |
13004 | asection *rsec; | |
1cce69b9 | 13005 | bfd_boolean start_stop = FALSE; |
5241d853 | 13006 | |
1cce69b9 AM |
13007 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
13008 | while (rsec != NULL) | |
5241d853 | 13009 | { |
1cce69b9 AM |
13010 | if (!rsec->gc_mark) |
13011 | { | |
13012 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
13013 | || (rsec->owner->flags & DYNAMIC) != 0) | |
13014 | rsec->gc_mark = 1; | |
13015 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
13016 | return FALSE; | |
13017 | } | |
13018 | if (!start_stop) | |
13019 | break; | |
199af150 | 13020 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
13021 | } |
13022 | return TRUE; | |
13023 | } | |
13024 | ||
07adf181 AM |
13025 | /* The mark phase of garbage collection. For a given section, mark |
13026 | it and any sections in this section's group, and all the sections | |
13027 | which define symbols to which it refers. */ | |
13028 | ||
ccfa59ea AM |
13029 | bfd_boolean |
13030 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
13031 | asection *sec, | |
6a5bb875 | 13032 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
13033 | { |
13034 | bfd_boolean ret; | |
9d0a14d3 | 13035 | asection *group_sec, *eh_frame; |
c152c796 AM |
13036 | |
13037 | sec->gc_mark = 1; | |
13038 | ||
13039 | /* Mark all the sections in the group. */ | |
13040 | group_sec = elf_section_data (sec)->next_in_group; | |
13041 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 13042 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
13043 | return FALSE; |
13044 | ||
13045 | /* Look through the section relocs. */ | |
13046 | ret = TRUE; | |
9d0a14d3 RS |
13047 | eh_frame = elf_eh_frame_section (sec->owner); |
13048 | if ((sec->flags & SEC_RELOC) != 0 | |
13049 | && sec->reloc_count > 0 | |
13050 | && sec != eh_frame) | |
c152c796 | 13051 | { |
5241d853 | 13052 | struct elf_reloc_cookie cookie; |
c152c796 | 13053 | |
5241d853 RS |
13054 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
13055 | ret = FALSE; | |
c152c796 | 13056 | else |
c152c796 | 13057 | { |
5241d853 | 13058 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 13059 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
13060 | { |
13061 | ret = FALSE; | |
13062 | break; | |
13063 | } | |
13064 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
13065 | } |
13066 | } | |
9d0a14d3 RS |
13067 | |
13068 | if (ret && eh_frame && elf_fde_list (sec)) | |
13069 | { | |
13070 | struct elf_reloc_cookie cookie; | |
13071 | ||
13072 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
13073 | ret = FALSE; | |
13074 | else | |
13075 | { | |
13076 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
13077 | gc_mark_hook, &cookie)) | |
13078 | ret = FALSE; | |
13079 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
13080 | } | |
13081 | } | |
13082 | ||
2f0c68f2 CM |
13083 | eh_frame = elf_section_eh_frame_entry (sec); |
13084 | if (ret && eh_frame && !eh_frame->gc_mark) | |
13085 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
13086 | ret = FALSE; | |
13087 | ||
c152c796 AM |
13088 | return ret; |
13089 | } | |
13090 | ||
3c758495 TG |
13091 | /* Scan and mark sections in a special or debug section group. */ |
13092 | ||
13093 | static void | |
13094 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
13095 | { | |
13096 | /* Point to first section of section group. */ | |
13097 | asection *ssec; | |
13098 | /* Used to iterate the section group. */ | |
13099 | asection *msec; | |
13100 | ||
13101 | bfd_boolean is_special_grp = TRUE; | |
13102 | bfd_boolean is_debug_grp = TRUE; | |
13103 | ||
13104 | /* First scan to see if group contains any section other than debug | |
13105 | and special section. */ | |
13106 | ssec = msec = elf_next_in_group (grp); | |
13107 | do | |
13108 | { | |
13109 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
13110 | is_debug_grp = FALSE; | |
13111 | ||
13112 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
13113 | is_special_grp = FALSE; | |
13114 | ||
13115 | msec = elf_next_in_group (msec); | |
13116 | } | |
13117 | while (msec != ssec); | |
13118 | ||
13119 | /* If this is a pure debug section group or pure special section group, | |
13120 | keep all sections in this group. */ | |
13121 | if (is_debug_grp || is_special_grp) | |
13122 | { | |
13123 | do | |
13124 | { | |
13125 | msec->gc_mark = 1; | |
13126 | msec = elf_next_in_group (msec); | |
13127 | } | |
13128 | while (msec != ssec); | |
13129 | } | |
13130 | } | |
13131 | ||
7f6ab9f8 AM |
13132 | /* Keep debug and special sections. */ |
13133 | ||
13134 | bfd_boolean | |
13135 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
13136 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
13137 | { | |
13138 | bfd *ibfd; | |
13139 | ||
c72f2fb2 | 13140 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
13141 | { |
13142 | asection *isec; | |
13143 | bfd_boolean some_kept; | |
b40bf0a2 | 13144 | bfd_boolean debug_frag_seen; |
b7c871ed | 13145 | bfd_boolean has_kept_debug_info; |
7f6ab9f8 AM |
13146 | |
13147 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13148 | continue; | |
57963c05 AM |
13149 | isec = ibfd->sections; |
13150 | if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13151 | continue; | |
7f6ab9f8 | 13152 | |
b40bf0a2 NC |
13153 | /* Ensure all linker created sections are kept, |
13154 | see if any other section is already marked, | |
13155 | and note if we have any fragmented debug sections. */ | |
b7c871ed | 13156 | debug_frag_seen = some_kept = has_kept_debug_info = FALSE; |
7f6ab9f8 AM |
13157 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
13158 | { | |
13159 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
13160 | isec->gc_mark = 1; | |
eb026f09 AM |
13161 | else if (isec->gc_mark |
13162 | && (isec->flags & SEC_ALLOC) != 0 | |
13163 | && elf_section_type (isec) != SHT_NOTE) | |
7f6ab9f8 | 13164 | some_kept = TRUE; |
b40bf0a2 | 13165 | |
535b785f | 13166 | if (!debug_frag_seen |
b40bf0a2 NC |
13167 | && (isec->flags & SEC_DEBUGGING) |
13168 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
13169 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
13170 | } |
13171 | ||
eb026f09 AM |
13172 | /* If no non-note alloc section in this file will be kept, then |
13173 | we can toss out the debug and special sections. */ | |
7f6ab9f8 AM |
13174 | if (!some_kept) |
13175 | continue; | |
13176 | ||
13177 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
13178 | not part of a group. Also keep section groups that contain |
13179 | just debug sections or special sections. */ | |
7f6ab9f8 | 13180 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
13181 | { |
13182 | if ((isec->flags & SEC_GROUP) != 0) | |
13183 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
13184 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
13185 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
13186 | && elf_next_in_group (isec) == NULL) | |
13187 | isec->gc_mark = 1; | |
b7c871ed L |
13188 | if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) |
13189 | has_kept_debug_info = TRUE; | |
3c758495 | 13190 | } |
b40bf0a2 | 13191 | |
b40bf0a2 NC |
13192 | /* Look for CODE sections which are going to be discarded, |
13193 | and find and discard any fragmented debug sections which | |
13194 | are associated with that code section. */ | |
b7c871ed L |
13195 | if (debug_frag_seen) |
13196 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13197 | if ((isec->flags & SEC_CODE) != 0 | |
13198 | && isec->gc_mark == 0) | |
13199 | { | |
13200 | unsigned int ilen; | |
13201 | asection *dsec; | |
b40bf0a2 | 13202 | |
b7c871ed | 13203 | ilen = strlen (isec->name); |
b40bf0a2 | 13204 | |
b7c871ed | 13205 | /* Association is determined by the name of the debug |
07d6d2b8 | 13206 | section containing the name of the code section as |
b7c871ed L |
13207 | a suffix. For example .debug_line.text.foo is a |
13208 | debug section associated with .text.foo. */ | |
13209 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
13210 | { | |
13211 | unsigned int dlen; | |
b40bf0a2 | 13212 | |
b7c871ed L |
13213 | if (dsec->gc_mark == 0 |
13214 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
13215 | continue; | |
b40bf0a2 | 13216 | |
b7c871ed | 13217 | dlen = strlen (dsec->name); |
b40bf0a2 | 13218 | |
b7c871ed L |
13219 | if (dlen > ilen |
13220 | && strncmp (dsec->name + (dlen - ilen), | |
13221 | isec->name, ilen) == 0) | |
b40bf0a2 | 13222 | dsec->gc_mark = 0; |
b7c871ed | 13223 | } |
b40bf0a2 | 13224 | } |
b7c871ed L |
13225 | |
13226 | /* Mark debug sections referenced by kept debug sections. */ | |
13227 | if (has_kept_debug_info) | |
13228 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13229 | if (isec->gc_mark | |
13230 | && (isec->flags & SEC_DEBUGGING) != 0) | |
13231 | if (!_bfd_elf_gc_mark (info, isec, | |
13232 | elf_gc_mark_debug_section)) | |
13233 | return FALSE; | |
7f6ab9f8 AM |
13234 | } |
13235 | return TRUE; | |
13236 | } | |
13237 | ||
c152c796 | 13238 | static bfd_boolean |
ccabcbe5 | 13239 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
13240 | { |
13241 | bfd *sub; | |
ccabcbe5 | 13242 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 13243 | |
c72f2fb2 | 13244 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13245 | { |
13246 | asection *o; | |
13247 | ||
b19a8f85 | 13248 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13249 | || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) |
b19a8f85 | 13250 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 | 13251 | continue; |
57963c05 AM |
13252 | o = sub->sections; |
13253 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13254 | continue; | |
c152c796 AM |
13255 | |
13256 | for (o = sub->sections; o != NULL; o = o->next) | |
13257 | { | |
a33dafc3 L |
13258 | /* When any section in a section group is kept, we keep all |
13259 | sections in the section group. If the first member of | |
13260 | the section group is excluded, we will also exclude the | |
13261 | group section. */ | |
13262 | if (o->flags & SEC_GROUP) | |
13263 | { | |
13264 | asection *first = elf_next_in_group (o); | |
13265 | o->gc_mark = first->gc_mark; | |
13266 | } | |
c152c796 | 13267 | |
1e7eae0d | 13268 | if (o->gc_mark) |
c152c796 AM |
13269 | continue; |
13270 | ||
13271 | /* Skip sweeping sections already excluded. */ | |
13272 | if (o->flags & SEC_EXCLUDE) | |
13273 | continue; | |
13274 | ||
13275 | /* Since this is early in the link process, it is simple | |
13276 | to remove a section from the output. */ | |
13277 | o->flags |= SEC_EXCLUDE; | |
13278 | ||
c55fe096 | 13279 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 13280 | /* xgettext:c-format */ |
9793eb77 | 13281 | _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), |
c08bb8dd | 13282 | o, sub); |
c152c796 AM |
13283 | } |
13284 | } | |
13285 | ||
c152c796 AM |
13286 | return TRUE; |
13287 | } | |
13288 | ||
13289 | /* Propagate collected vtable information. This is called through | |
13290 | elf_link_hash_traverse. */ | |
13291 | ||
13292 | static bfd_boolean | |
13293 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
13294 | { | |
c152c796 | 13295 | /* Those that are not vtables. */ |
cbd0eecf L |
13296 | if (h->start_stop |
13297 | || h->u2.vtable == NULL | |
13298 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13299 | return TRUE; |
13300 | ||
13301 | /* Those vtables that do not have parents, we cannot merge. */ | |
cbd0eecf | 13302 | if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
13303 | return TRUE; |
13304 | ||
13305 | /* If we've already been done, exit. */ | |
cbd0eecf | 13306 | if (h->u2.vtable->used && h->u2.vtable->used[-1]) |
c152c796 AM |
13307 | return TRUE; |
13308 | ||
13309 | /* Make sure the parent's table is up to date. */ | |
cbd0eecf | 13310 | elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); |
c152c796 | 13311 | |
cbd0eecf | 13312 | if (h->u2.vtable->used == NULL) |
c152c796 AM |
13313 | { |
13314 | /* None of this table's entries were referenced. Re-use the | |
13315 | parent's table. */ | |
cbd0eecf L |
13316 | h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; |
13317 | h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; | |
c152c796 AM |
13318 | } |
13319 | else | |
13320 | { | |
13321 | size_t n; | |
13322 | bfd_boolean *cu, *pu; | |
13323 | ||
13324 | /* Or the parent's entries into ours. */ | |
cbd0eecf | 13325 | cu = h->u2.vtable->used; |
c152c796 | 13326 | cu[-1] = TRUE; |
cbd0eecf | 13327 | pu = h->u2.vtable->parent->u2.vtable->used; |
c152c796 AM |
13328 | if (pu != NULL) |
13329 | { | |
13330 | const struct elf_backend_data *bed; | |
13331 | unsigned int log_file_align; | |
13332 | ||
13333 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
13334 | log_file_align = bed->s->log_file_align; | |
cbd0eecf | 13335 | n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; |
c152c796 AM |
13336 | while (n--) |
13337 | { | |
13338 | if (*pu) | |
13339 | *cu = TRUE; | |
13340 | pu++; | |
13341 | cu++; | |
13342 | } | |
13343 | } | |
13344 | } | |
13345 | ||
13346 | return TRUE; | |
13347 | } | |
13348 | ||
13349 | static bfd_boolean | |
13350 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
13351 | { | |
13352 | asection *sec; | |
13353 | bfd_vma hstart, hend; | |
13354 | Elf_Internal_Rela *relstart, *relend, *rel; | |
13355 | const struct elf_backend_data *bed; | |
13356 | unsigned int log_file_align; | |
13357 | ||
c152c796 AM |
13358 | /* Take care of both those symbols that do not describe vtables as |
13359 | well as those that are not loaded. */ | |
cbd0eecf L |
13360 | if (h->start_stop |
13361 | || h->u2.vtable == NULL | |
13362 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13363 | return TRUE; |
13364 | ||
13365 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13366 | || h->root.type == bfd_link_hash_defweak); | |
13367 | ||
13368 | sec = h->root.u.def.section; | |
13369 | hstart = h->root.u.def.value; | |
13370 | hend = hstart + h->size; | |
13371 | ||
13372 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13373 | if (!relstart) | |
13374 | return *(bfd_boolean *) okp = FALSE; | |
13375 | bed = get_elf_backend_data (sec->owner); | |
13376 | log_file_align = bed->s->log_file_align; | |
13377 | ||
056bafd4 | 13378 | relend = relstart + sec->reloc_count; |
c152c796 AM |
13379 | |
13380 | for (rel = relstart; rel < relend; ++rel) | |
13381 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13382 | { | |
13383 | /* If the entry is in use, do nothing. */ | |
cbd0eecf L |
13384 | if (h->u2.vtable->used |
13385 | && (rel->r_offset - hstart) < h->u2.vtable->size) | |
c152c796 AM |
13386 | { |
13387 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
cbd0eecf | 13388 | if (h->u2.vtable->used[entry]) |
c152c796 AM |
13389 | continue; |
13390 | } | |
13391 | /* Otherwise, kill it. */ | |
13392 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13393 | } | |
13394 | ||
13395 | return TRUE; | |
13396 | } | |
13397 | ||
87538722 AM |
13398 | /* Mark sections containing dynamically referenced symbols. When |
13399 | building shared libraries, we must assume that any visible symbol is | |
13400 | referenced. */ | |
715df9b8 | 13401 | |
64d03ab5 AM |
13402 | bfd_boolean |
13403 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13404 | { |
87538722 | 13405 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13406 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13407 | |
715df9b8 EB |
13408 | if ((h->root.type == bfd_link_hash_defined |
13409 | || h->root.type == bfd_link_hash_defweak) | |
d664fd41 | 13410 | && ((h->ref_dynamic && !h->forced_local) |
c4621b33 | 13411 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13412 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13413 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13414 | && (!bfd_link_executable (info) |
22185505 | 13415 | || info->gc_keep_exported |
b407645f AM |
13416 | || info->export_dynamic |
13417 | || (h->dynamic | |
13418 | && d != NULL | |
13419 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13420 | && (h->versioned >= versioned |
54e8959c L |
13421 | || !bfd_hide_sym_by_version (info->version_info, |
13422 | h->root.root.string))))) | |
715df9b8 EB |
13423 | h->root.u.def.section->flags |= SEC_KEEP; |
13424 | ||
13425 | return TRUE; | |
13426 | } | |
3b36f7e6 | 13427 | |
74f0fb50 AM |
13428 | /* Keep all sections containing symbols undefined on the command-line, |
13429 | and the section containing the entry symbol. */ | |
13430 | ||
13431 | void | |
13432 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13433 | { | |
13434 | struct bfd_sym_chain *sym; | |
13435 | ||
13436 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13437 | { | |
13438 | struct elf_link_hash_entry *h; | |
13439 | ||
13440 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13441 | FALSE, FALSE, FALSE); | |
13442 | ||
13443 | if (h != NULL | |
13444 | && (h->root.type == bfd_link_hash_defined | |
13445 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13446 | && !bfd_is_abs_section (h->root.u.def.section) |
13447 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13448 | h->root.u.def.section->flags |= SEC_KEEP; |
13449 | } | |
13450 | } | |
13451 | ||
2f0c68f2 CM |
13452 | bfd_boolean |
13453 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13454 | struct bfd_link_info *info) | |
13455 | { | |
13456 | bfd *ibfd = info->input_bfds; | |
13457 | ||
13458 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13459 | { | |
13460 | asection *sec; | |
13461 | struct elf_reloc_cookie cookie; | |
13462 | ||
13463 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13464 | continue; | |
57963c05 AM |
13465 | sec = ibfd->sections; |
13466 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13467 | continue; | |
2f0c68f2 CM |
13468 | |
13469 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13470 | return FALSE; | |
13471 | ||
13472 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13473 | { | |
13474 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13475 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13476 | { | |
13477 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13478 | fini_reloc_cookie_rels (&cookie, sec); | |
13479 | } | |
13480 | } | |
13481 | } | |
13482 | return TRUE; | |
13483 | } | |
13484 | ||
c152c796 AM |
13485 | /* Do mark and sweep of unused sections. */ |
13486 | ||
13487 | bfd_boolean | |
13488 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13489 | { | |
13490 | bfd_boolean ok = TRUE; | |
13491 | bfd *sub; | |
6a5bb875 | 13492 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13493 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13494 | struct elf_link_hash_table *htab; |
c152c796 | 13495 | |
64d03ab5 | 13496 | if (!bed->can_gc_sections |
715df9b8 | 13497 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13498 | { |
9793eb77 | 13499 | _bfd_error_handler(_("warning: gc-sections option ignored")); |
c152c796 AM |
13500 | return TRUE; |
13501 | } | |
13502 | ||
74f0fb50 | 13503 | bed->gc_keep (info); |
da44f4e5 | 13504 | htab = elf_hash_table (info); |
74f0fb50 | 13505 | |
9d0a14d3 RS |
13506 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13507 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13508 | for (sub = info->input_bfds; |
13509 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13510 | sub = sub->link.next) | |
9d0a14d3 RS |
13511 | { |
13512 | asection *sec; | |
13513 | struct elf_reloc_cookie cookie; | |
13514 | ||
57963c05 AM |
13515 | sec = sub->sections; |
13516 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13517 | continue; | |
9d0a14d3 | 13518 | sec = bfd_get_section_by_name (sub, ".eh_frame"); |
9a2a56cc | 13519 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13520 | { |
13521 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13522 | if (elf_section_data (sec)->sec_info |
13523 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13524 | elf_eh_frame_section (sub) = sec; |
13525 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13526 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13527 | } |
13528 | } | |
9d0a14d3 | 13529 | |
c152c796 | 13530 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13531 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13532 | if (!ok) |
13533 | return FALSE; | |
13534 | ||
13535 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13536 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13537 | if (!ok) |
13538 | return FALSE; | |
13539 | ||
715df9b8 | 13540 | /* Mark dynamically referenced symbols. */ |
22185505 | 13541 | if (htab->dynamic_sections_created || info->gc_keep_exported) |
da44f4e5 | 13542 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); |
c152c796 | 13543 | |
715df9b8 | 13544 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13545 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13546 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13547 | { |
13548 | asection *o; | |
13549 | ||
b19a8f85 | 13550 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13551 | || elf_object_id (sub) != elf_hash_table_id (htab) |
b19a8f85 | 13552 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 AM |
13553 | continue; |
13554 | ||
57963c05 AM |
13555 | o = sub->sections; |
13556 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13557 | continue; | |
13558 | ||
7f6ab9f8 AM |
13559 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13560 | Also treat note sections as a root, if the section is not part | |
8b6f4cd3 L |
13561 | of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as |
13562 | well as FINI_ARRAY sections for ld -r. */ | |
c152c796 | 13563 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13564 | if (!o->gc_mark |
13565 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13566 | && ((o->flags & SEC_KEEP) != 0 |
8b6f4cd3 L |
13567 | || (bfd_link_relocatable (info) |
13568 | && ((elf_section_data (o)->this_hdr.sh_type | |
13569 | == SHT_PREINIT_ARRAY) | |
13570 | || (elf_section_data (o)->this_hdr.sh_type | |
13571 | == SHT_INIT_ARRAY) | |
13572 | || (elf_section_data (o)->this_hdr.sh_type | |
13573 | == SHT_FINI_ARRAY))) | |
7f6ab9f8 AM |
13574 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13575 | && elf_next_in_group (o) == NULL ))) | |
13576 | { | |
13577 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13578 | return FALSE; | |
13579 | } | |
c152c796 AM |
13580 | } |
13581 | ||
6a5bb875 | 13582 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13583 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13584 | |
c152c796 | 13585 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13586 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13587 | } |
13588 | \f | |
13589 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13590 | ||
13591 | bfd_boolean | |
13592 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13593 | asection *sec, | |
13594 | struct elf_link_hash_entry *h, | |
13595 | bfd_vma offset) | |
13596 | { | |
13597 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13598 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13599 | size_t extsymcount; |
c152c796 AM |
13600 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13601 | ||
13602 | /* The sh_info field of the symtab header tells us where the | |
13603 | external symbols start. We don't care about the local symbols at | |
13604 | this point. */ | |
13605 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13606 | if (!elf_bad_symtab (abfd)) | |
13607 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13608 | ||
13609 | sym_hashes = elf_sym_hashes (abfd); | |
13610 | sym_hashes_end = sym_hashes + extsymcount; | |
13611 | ||
13612 | /* Hunt down the child symbol, which is in this section at the same | |
13613 | offset as the relocation. */ | |
13614 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13615 | { | |
13616 | if ((child = *search) != NULL | |
13617 | && (child->root.type == bfd_link_hash_defined | |
13618 | || child->root.type == bfd_link_hash_defweak) | |
13619 | && child->root.u.def.section == sec | |
13620 | && child->root.u.def.value == offset) | |
13621 | goto win; | |
13622 | } | |
13623 | ||
695344c0 | 13624 | /* xgettext:c-format */ |
9793eb77 | 13625 | _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"), |
2dcf00ce | 13626 | abfd, sec, (uint64_t) offset); |
c152c796 AM |
13627 | bfd_set_error (bfd_error_invalid_operation); |
13628 | return FALSE; | |
13629 | ||
13630 | win: | |
cbd0eecf | 13631 | if (!child->u2.vtable) |
f6e332e6 | 13632 | { |
cbd0eecf L |
13633 | child->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13634 | bfd_zalloc (abfd, sizeof (*child->u2.vtable))); | |
13635 | if (!child->u2.vtable) | |
f6e332e6 AM |
13636 | return FALSE; |
13637 | } | |
c152c796 AM |
13638 | if (!h) |
13639 | { | |
13640 | /* This *should* only be the absolute section. It could potentially | |
13641 | be that someone has defined a non-global vtable though, which | |
13642 | would be bad. It isn't worth paging in the local symbols to be | |
13643 | sure though; that case should simply be handled by the assembler. */ | |
13644 | ||
cbd0eecf | 13645 | child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13646 | } |
13647 | else | |
cbd0eecf | 13648 | child->u2.vtable->parent = h; |
c152c796 AM |
13649 | |
13650 | return TRUE; | |
13651 | } | |
13652 | ||
13653 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13654 | ||
13655 | bfd_boolean | |
13656 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
13657 | asection *sec ATTRIBUTE_UNUSED, | |
13658 | struct elf_link_hash_entry *h, | |
13659 | bfd_vma addend) | |
13660 | { | |
13661 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13662 | unsigned int log_file_align = bed->s->log_file_align; | |
13663 | ||
cbd0eecf | 13664 | if (!h->u2.vtable) |
f6e332e6 | 13665 | { |
cbd0eecf L |
13666 | h->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13667 | bfd_zalloc (abfd, sizeof (*h->u2.vtable))); | |
13668 | if (!h->u2.vtable) | |
f6e332e6 AM |
13669 | return FALSE; |
13670 | } | |
13671 | ||
cbd0eecf | 13672 | if (addend >= h->u2.vtable->size) |
c152c796 AM |
13673 | { |
13674 | size_t size, bytes, file_align; | |
cbd0eecf | 13675 | bfd_boolean *ptr = h->u2.vtable->used; |
c152c796 AM |
13676 | |
13677 | /* While the symbol is undefined, we have to be prepared to handle | |
13678 | a zero size. */ | |
13679 | file_align = 1 << log_file_align; | |
13680 | if (h->root.type == bfd_link_hash_undefined) | |
13681 | size = addend + file_align; | |
13682 | else | |
13683 | { | |
13684 | size = h->size; | |
13685 | if (addend >= size) | |
13686 | { | |
13687 | /* Oops! We've got a reference past the defined end of | |
13688 | the table. This is probably a bug -- shall we warn? */ | |
13689 | size = addend + file_align; | |
13690 | } | |
13691 | } | |
13692 | size = (size + file_align - 1) & -file_align; | |
13693 | ||
13694 | /* Allocate one extra entry for use as a "done" flag for the | |
13695 | consolidation pass. */ | |
13696 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13697 | ||
13698 | if (ptr) | |
13699 | { | |
a50b1753 | 13700 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13701 | |
13702 | if (ptr != NULL) | |
13703 | { | |
13704 | size_t oldbytes; | |
13705 | ||
cbd0eecf | 13706 | oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) |
c152c796 AM |
13707 | * sizeof (bfd_boolean)); |
13708 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13709 | } | |
13710 | } | |
13711 | else | |
a50b1753 | 13712 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13713 | |
13714 | if (ptr == NULL) | |
13715 | return FALSE; | |
13716 | ||
13717 | /* And arrange for that done flag to be at index -1. */ | |
cbd0eecf L |
13718 | h->u2.vtable->used = ptr + 1; |
13719 | h->u2.vtable->size = size; | |
c152c796 AM |
13720 | } |
13721 | ||
cbd0eecf | 13722 | h->u2.vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13723 | |
13724 | return TRUE; | |
13725 | } | |
13726 | ||
ae17ab41 CM |
13727 | /* Map an ELF section header flag to its corresponding string. */ |
13728 | typedef struct | |
13729 | { | |
13730 | char *flag_name; | |
13731 | flagword flag_value; | |
13732 | } elf_flags_to_name_table; | |
13733 | ||
13734 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13735 | { | |
13736 | { "SHF_WRITE", SHF_WRITE }, | |
13737 | { "SHF_ALLOC", SHF_ALLOC }, | |
13738 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13739 | { "SHF_MERGE", SHF_MERGE }, | |
13740 | { "SHF_STRINGS", SHF_STRINGS }, | |
13741 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13742 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13743 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13744 | { "SHF_GROUP", SHF_GROUP }, | |
13745 | { "SHF_TLS", SHF_TLS }, | |
13746 | { "SHF_MASKOS", SHF_MASKOS }, | |
13747 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13748 | }; | |
13749 | ||
b9c361e0 JL |
13750 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13751 | bfd_boolean | |
ae17ab41 | 13752 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13753 | struct flag_info *flaginfo, |
b9c361e0 | 13754 | asection *section) |
ae17ab41 | 13755 | { |
8b127cbc | 13756 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13757 | |
8b127cbc | 13758 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13759 | { |
8b127cbc AM |
13760 | bfd *obfd = info->output_bfd; |
13761 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13762 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13763 | int with_hex = 0; |
13764 | int without_hex = 0; | |
13765 | ||
8b127cbc | 13766 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13767 | { |
b9c361e0 | 13768 | unsigned i; |
8b127cbc | 13769 | flagword (*lookup) (char *); |
ae17ab41 | 13770 | |
8b127cbc AM |
13771 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13772 | if (lookup != NULL) | |
ae17ab41 | 13773 | { |
8b127cbc | 13774 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13775 | |
13776 | if (hexval != 0) | |
13777 | { | |
13778 | if (tf->with == with_flags) | |
13779 | with_hex |= hexval; | |
13780 | else if (tf->with == without_flags) | |
13781 | without_hex |= hexval; | |
13782 | tf->valid = TRUE; | |
13783 | continue; | |
13784 | } | |
ae17ab41 | 13785 | } |
8b127cbc | 13786 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13787 | { |
8b127cbc | 13788 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13789 | { |
13790 | if (tf->with == with_flags) | |
13791 | with_hex |= elf_flags_to_names[i].flag_value; | |
13792 | else if (tf->with == without_flags) | |
13793 | without_hex |= elf_flags_to_names[i].flag_value; | |
13794 | tf->valid = TRUE; | |
13795 | break; | |
13796 | } | |
13797 | } | |
8b127cbc | 13798 | if (!tf->valid) |
b9c361e0 | 13799 | { |
68ffbac6 | 13800 | info->callbacks->einfo |
9793eb77 | 13801 | (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13802 | return FALSE; |
ae17ab41 CM |
13803 | } |
13804 | } | |
8b127cbc AM |
13805 | flaginfo->flags_initialized = TRUE; |
13806 | flaginfo->only_with_flags |= with_hex; | |
13807 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13808 | } |
ae17ab41 | 13809 | |
8b127cbc | 13810 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13811 | return FALSE; |
13812 | ||
8b127cbc | 13813 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13814 | return FALSE; |
13815 | ||
13816 | return TRUE; | |
ae17ab41 CM |
13817 | } |
13818 | ||
c152c796 AM |
13819 | struct alloc_got_off_arg { |
13820 | bfd_vma gotoff; | |
10455f89 | 13821 | struct bfd_link_info *info; |
c152c796 AM |
13822 | }; |
13823 | ||
13824 | /* We need a special top-level link routine to convert got reference counts | |
13825 | to real got offsets. */ | |
13826 | ||
13827 | static bfd_boolean | |
13828 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13829 | { | |
a50b1753 | 13830 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13831 | bfd *obfd = gofarg->info->output_bfd; |
13832 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13833 | |
c152c796 AM |
13834 | if (h->got.refcount > 0) |
13835 | { | |
13836 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13837 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13838 | } |
13839 | else | |
13840 | h->got.offset = (bfd_vma) -1; | |
13841 | ||
13842 | return TRUE; | |
13843 | } | |
13844 | ||
13845 | /* And an accompanying bit to work out final got entry offsets once | |
13846 | we're done. Should be called from final_link. */ | |
13847 | ||
13848 | bfd_boolean | |
13849 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13850 | struct bfd_link_info *info) | |
13851 | { | |
13852 | bfd *i; | |
13853 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13854 | bfd_vma gotoff; | |
c152c796 AM |
13855 | struct alloc_got_off_arg gofarg; |
13856 | ||
10455f89 HPN |
13857 | BFD_ASSERT (abfd == info->output_bfd); |
13858 | ||
c152c796 AM |
13859 | if (! is_elf_hash_table (info->hash)) |
13860 | return FALSE; | |
13861 | ||
13862 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13863 | put into the .got.plt section, if the backend uses it. */ | |
13864 | if (bed->want_got_plt) | |
13865 | gotoff = 0; | |
13866 | else | |
13867 | gotoff = bed->got_header_size; | |
13868 | ||
13869 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13870 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13871 | { |
13872 | bfd_signed_vma *local_got; | |
ef53be89 | 13873 | size_t j, locsymcount; |
c152c796 AM |
13874 | Elf_Internal_Shdr *symtab_hdr; |
13875 | ||
13876 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13877 | continue; | |
13878 | ||
13879 | local_got = elf_local_got_refcounts (i); | |
13880 | if (!local_got) | |
13881 | continue; | |
13882 | ||
13883 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13884 | if (elf_bad_symtab (i)) | |
13885 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13886 | else | |
13887 | locsymcount = symtab_hdr->sh_info; | |
13888 | ||
13889 | for (j = 0; j < locsymcount; ++j) | |
13890 | { | |
13891 | if (local_got[j] > 0) | |
13892 | { | |
13893 | local_got[j] = gotoff; | |
10455f89 | 13894 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13895 | } |
13896 | else | |
13897 | local_got[j] = (bfd_vma) -1; | |
13898 | } | |
13899 | } | |
13900 | ||
13901 | /* Then the global .got entries. .plt refcounts are handled by | |
13902 | adjust_dynamic_symbol */ | |
13903 | gofarg.gotoff = gotoff; | |
10455f89 | 13904 | gofarg.info = info; |
c152c796 AM |
13905 | elf_link_hash_traverse (elf_hash_table (info), |
13906 | elf_gc_allocate_got_offsets, | |
13907 | &gofarg); | |
13908 | return TRUE; | |
13909 | } | |
13910 | ||
13911 | /* Many folk need no more in the way of final link than this, once | |
13912 | got entry reference counting is enabled. */ | |
13913 | ||
13914 | bfd_boolean | |
13915 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
13916 | { | |
13917 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
13918 | return FALSE; | |
13919 | ||
13920 | /* Invoke the regular ELF backend linker to do all the work. */ | |
13921 | return bfd_elf_final_link (abfd, info); | |
13922 | } | |
13923 | ||
13924 | bfd_boolean | |
13925 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
13926 | { | |
a50b1753 | 13927 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
13928 | |
13929 | if (rcookie->bad_symtab) | |
13930 | rcookie->rel = rcookie->rels; | |
13931 | ||
13932 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
13933 | { | |
13934 | unsigned long r_symndx; | |
13935 | ||
13936 | if (! rcookie->bad_symtab) | |
13937 | if (rcookie->rel->r_offset > offset) | |
13938 | return FALSE; | |
13939 | if (rcookie->rel->r_offset != offset) | |
13940 | continue; | |
13941 | ||
13942 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 13943 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
13944 | return TRUE; |
13945 | ||
13946 | if (r_symndx >= rcookie->locsymcount | |
13947 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13948 | { | |
13949 | struct elf_link_hash_entry *h; | |
13950 | ||
13951 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
13952 | ||
13953 | while (h->root.type == bfd_link_hash_indirect | |
13954 | || h->root.type == bfd_link_hash_warning) | |
13955 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
13956 | ||
13957 | if ((h->root.type == bfd_link_hash_defined | |
13958 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
13959 | && (h->root.u.def.section->owner != rcookie->abfd |
13960 | || h->root.u.def.section->kept_section != NULL | |
13961 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 13962 | return TRUE; |
c152c796 AM |
13963 | } |
13964 | else | |
13965 | { | |
13966 | /* It's not a relocation against a global symbol, | |
13967 | but it could be a relocation against a local | |
13968 | symbol for a discarded section. */ | |
13969 | asection *isec; | |
13970 | Elf_Internal_Sym *isym; | |
13971 | ||
13972 | /* Need to: get the symbol; get the section. */ | |
13973 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 13974 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
13975 | if (isec != NULL |
13976 | && (isec->kept_section != NULL | |
13977 | || discarded_section (isec))) | |
cb33740c | 13978 | return TRUE; |
c152c796 AM |
13979 | } |
13980 | return FALSE; | |
13981 | } | |
13982 | return FALSE; | |
13983 | } | |
13984 | ||
13985 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
13986 | Returns -1 on error, 1 if any section's size was changed, 0 if |
13987 | nothing changed. This function assumes that the relocations are in | |
13988 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 13989 | |
75938853 | 13990 | int |
c152c796 AM |
13991 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
13992 | { | |
13993 | struct elf_reloc_cookie cookie; | |
18cd5bce | 13994 | asection *o; |
c152c796 | 13995 | bfd *abfd; |
75938853 | 13996 | int changed = 0; |
c152c796 AM |
13997 | |
13998 | if (info->traditional_format | |
13999 | || !is_elf_hash_table (info->hash)) | |
75938853 | 14000 | return 0; |
c152c796 | 14001 | |
18cd5bce AM |
14002 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
14003 | if (o != NULL) | |
c152c796 | 14004 | { |
18cd5bce | 14005 | asection *i; |
c152c796 | 14006 | |
18cd5bce | 14007 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 14008 | { |
18cd5bce AM |
14009 | if (i->size == 0 |
14010 | || i->reloc_count == 0 | |
14011 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
14012 | continue; | |
c152c796 | 14013 | |
18cd5bce AM |
14014 | abfd = i->owner; |
14015 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14016 | continue; | |
c152c796 | 14017 | |
18cd5bce | 14018 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 14019 | return -1; |
c152c796 | 14020 | |
18cd5bce AM |
14021 | if (_bfd_discard_section_stabs (abfd, i, |
14022 | elf_section_data (i)->sec_info, | |
5241d853 RS |
14023 | bfd_elf_reloc_symbol_deleted_p, |
14024 | &cookie)) | |
75938853 | 14025 | changed = 1; |
18cd5bce AM |
14026 | |
14027 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14028 | } |
18cd5bce AM |
14029 | } |
14030 | ||
2f0c68f2 CM |
14031 | o = NULL; |
14032 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
14033 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
14034 | if (o != NULL) |
14035 | { | |
14036 | asection *i; | |
d7153c4a | 14037 | int eh_changed = 0; |
79a94a2a | 14038 | unsigned int eh_alignment; |
c152c796 | 14039 | |
18cd5bce | 14040 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 14041 | { |
18cd5bce AM |
14042 | if (i->size == 0) |
14043 | continue; | |
14044 | ||
14045 | abfd = i->owner; | |
14046 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14047 | continue; | |
14048 | ||
14049 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 14050 | return -1; |
18cd5bce AM |
14051 | |
14052 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
14053 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
14054 | bfd_elf_reloc_symbol_deleted_p, |
14055 | &cookie)) | |
d7153c4a AM |
14056 | { |
14057 | eh_changed = 1; | |
14058 | if (i->size != i->rawsize) | |
14059 | changed = 1; | |
14060 | } | |
18cd5bce AM |
14061 | |
14062 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14063 | } |
9866ffe2 | 14064 | |
79a94a2a | 14065 | eh_alignment = 1 << o->alignment_power; |
9866ffe2 AM |
14066 | /* Skip over zero terminator, and prevent empty sections from |
14067 | adding alignment padding at the end. */ | |
14068 | for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) | |
14069 | if (i->size == 0) | |
14070 | i->flags |= SEC_EXCLUDE; | |
14071 | else if (i->size > 4) | |
14072 | break; | |
14073 | /* The last non-empty eh_frame section doesn't need padding. */ | |
14074 | if (i != NULL) | |
14075 | i = i->map_tail.s; | |
14076 | /* Any prior sections must pad the last FDE out to the output | |
14077 | section alignment. Otherwise we might have zero padding | |
14078 | between sections, which would be seen as a terminator. */ | |
14079 | for (; i != NULL; i = i->map_tail.s) | |
14080 | if (i->size == 4) | |
14081 | /* All but the last zero terminator should have been removed. */ | |
14082 | BFD_FAIL (); | |
14083 | else | |
14084 | { | |
14085 | bfd_size_type size | |
14086 | = (i->size + eh_alignment - 1) & -eh_alignment; | |
14087 | if (i->size != size) | |
af471f82 | 14088 | { |
9866ffe2 AM |
14089 | i->size = size; |
14090 | changed = 1; | |
14091 | eh_changed = 1; | |
af471f82 | 14092 | } |
9866ffe2 | 14093 | } |
d7153c4a AM |
14094 | if (eh_changed) |
14095 | elf_link_hash_traverse (elf_hash_table (info), | |
14096 | _bfd_elf_adjust_eh_frame_global_symbol, NULL); | |
18cd5bce | 14097 | } |
c152c796 | 14098 | |
18cd5bce AM |
14099 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
14100 | { | |
14101 | const struct elf_backend_data *bed; | |
57963c05 | 14102 | asection *s; |
c152c796 | 14103 | |
18cd5bce AM |
14104 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
14105 | continue; | |
57963c05 AM |
14106 | s = abfd->sections; |
14107 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14108 | continue; | |
18cd5bce AM |
14109 | |
14110 | bed = get_elf_backend_data (abfd); | |
14111 | ||
14112 | if (bed->elf_backend_discard_info != NULL) | |
14113 | { | |
14114 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 14115 | return -1; |
18cd5bce AM |
14116 | |
14117 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 14118 | changed = 1; |
18cd5bce AM |
14119 | |
14120 | fini_reloc_cookie (&cookie, abfd); | |
14121 | } | |
c152c796 AM |
14122 | } |
14123 | ||
2f0c68f2 CM |
14124 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
14125 | _bfd_elf_end_eh_frame_parsing (info); | |
14126 | ||
14127 | if (info->eh_frame_hdr_type | |
0e1862bb | 14128 | && !bfd_link_relocatable (info) |
c152c796 | 14129 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 14130 | changed = 1; |
c152c796 | 14131 | |
75938853 | 14132 | return changed; |
c152c796 | 14133 | } |
082b7297 | 14134 | |
43e1669b | 14135 | bfd_boolean |
0c511000 | 14136 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 14137 | asection *sec, |
c0f00686 | 14138 | struct bfd_link_info *info) |
082b7297 L |
14139 | { |
14140 | flagword flags; | |
c77ec726 | 14141 | const char *name, *key; |
082b7297 L |
14142 | struct bfd_section_already_linked *l; |
14143 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 14144 | |
c77ec726 AM |
14145 | if (sec->output_section == bfd_abs_section_ptr) |
14146 | return FALSE; | |
0c511000 | 14147 | |
c77ec726 | 14148 | flags = sec->flags; |
0c511000 | 14149 | |
c77ec726 AM |
14150 | /* Return if it isn't a linkonce section. A comdat group section |
14151 | also has SEC_LINK_ONCE set. */ | |
14152 | if ((flags & SEC_LINK_ONCE) == 0) | |
14153 | return FALSE; | |
0c511000 | 14154 | |
c77ec726 AM |
14155 | /* Don't put group member sections on our list of already linked |
14156 | sections. They are handled as a group via their group section. */ | |
14157 | if (elf_sec_group (sec) != NULL) | |
14158 | return FALSE; | |
0c511000 | 14159 | |
c77ec726 AM |
14160 | /* For a SHT_GROUP section, use the group signature as the key. */ |
14161 | name = sec->name; | |
14162 | if ((flags & SEC_GROUP) != 0 | |
14163 | && elf_next_in_group (sec) != NULL | |
14164 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
14165 | key = elf_group_name (elf_next_in_group (sec)); | |
14166 | else | |
14167 | { | |
14168 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 14169 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
14170 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
14171 | key++; | |
0c511000 | 14172 | else |
c77ec726 AM |
14173 | /* Must be a user linkonce section that doesn't follow gcc's |
14174 | naming convention. In this case we won't be matching | |
14175 | single member groups. */ | |
14176 | key = name; | |
0c511000 | 14177 | } |
6d2cd210 | 14178 | |
c77ec726 | 14179 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
14180 | |
14181 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14182 | { | |
c2370991 | 14183 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
14184 | sections with a signature of <key> (<key> is some string), |
14185 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
14186 | Match like sections. LTO plugin sections are an exception. | |
14187 | They are always named .gnu.linkonce.t.<key> and match either | |
14188 | type of section. */ | |
14189 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
14190 | && ((flags & SEC_GROUP) != 0 | |
14191 | || strcmp (name, l->sec->name) == 0)) | |
14192 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
14193 | { |
14194 | /* The section has already been linked. See if we should | |
6d2cd210 | 14195 | issue a warning. */ |
c77ec726 AM |
14196 | if (!_bfd_handle_already_linked (sec, l, info)) |
14197 | return FALSE; | |
082b7297 | 14198 | |
c77ec726 | 14199 | if (flags & SEC_GROUP) |
3d7f7666 | 14200 | { |
c77ec726 AM |
14201 | asection *first = elf_next_in_group (sec); |
14202 | asection *s = first; | |
3d7f7666 | 14203 | |
c77ec726 | 14204 | while (s != NULL) |
3d7f7666 | 14205 | { |
c77ec726 AM |
14206 | s->output_section = bfd_abs_section_ptr; |
14207 | /* Record which group discards it. */ | |
14208 | s->kept_section = l->sec; | |
14209 | s = elf_next_in_group (s); | |
14210 | /* These lists are circular. */ | |
14211 | if (s == first) | |
14212 | break; | |
3d7f7666 L |
14213 | } |
14214 | } | |
082b7297 | 14215 | |
43e1669b | 14216 | return TRUE; |
082b7297 L |
14217 | } |
14218 | } | |
14219 | ||
c77ec726 AM |
14220 | /* A single member comdat group section may be discarded by a |
14221 | linkonce section and vice versa. */ | |
14222 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 14223 | { |
c77ec726 | 14224 | asection *first = elf_next_in_group (sec); |
c2370991 | 14225 | |
c77ec726 AM |
14226 | if (first != NULL && elf_next_in_group (first) == first) |
14227 | /* Check this single member group against linkonce sections. */ | |
14228 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14229 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14230 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
14231 | { | |
14232 | first->output_section = bfd_abs_section_ptr; | |
14233 | first->kept_section = l->sec; | |
14234 | sec->output_section = bfd_abs_section_ptr; | |
14235 | break; | |
14236 | } | |
14237 | } | |
14238 | else | |
14239 | /* Check this linkonce section against single member groups. */ | |
14240 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14241 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 14242 | { |
c77ec726 | 14243 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 14244 | |
c77ec726 AM |
14245 | if (first != NULL |
14246 | && elf_next_in_group (first) == first | |
14247 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
14248 | { | |
14249 | sec->output_section = bfd_abs_section_ptr; | |
14250 | sec->kept_section = first; | |
14251 | break; | |
14252 | } | |
6d2cd210 | 14253 | } |
0c511000 | 14254 | |
c77ec726 AM |
14255 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
14256 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
14257 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
14258 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
14259 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
14260 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
14261 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
14262 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
14263 | The reverse order cannot happen as there is never a bfd with only the | |
14264 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
14265 | matter as here were are looking only for cross-bfd sections. */ | |
14266 | ||
14267 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
14268 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14269 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14270 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
14271 | { | |
14272 | if (abfd != l->sec->owner) | |
14273 | sec->output_section = bfd_abs_section_ptr; | |
14274 | break; | |
14275 | } | |
80c29487 | 14276 | |
082b7297 | 14277 | /* This is the first section with this name. Record it. */ |
c77ec726 | 14278 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 14279 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 14280 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 14281 | } |
81e1b023 | 14282 | |
a4d8e49b L |
14283 | bfd_boolean |
14284 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
14285 | { | |
14286 | return sym->st_shndx == SHN_COMMON; | |
14287 | } | |
14288 | ||
14289 | unsigned int | |
14290 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
14291 | { | |
14292 | return SHN_COMMON; | |
14293 | } | |
14294 | ||
14295 | asection * | |
14296 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
14297 | { | |
14298 | return bfd_com_section_ptr; | |
14299 | } | |
10455f89 HPN |
14300 | |
14301 | bfd_vma | |
14302 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
14303 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
14304 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
14305 | bfd *ibfd ATTRIBUTE_UNUSED, | |
14306 | unsigned long symndx ATTRIBUTE_UNUSED) | |
14307 | { | |
14308 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14309 | return bed->s->arch_size / 8; | |
14310 | } | |
83bac4b0 NC |
14311 | |
14312 | /* Routines to support the creation of dynamic relocs. */ | |
14313 | ||
83bac4b0 NC |
14314 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
14315 | ||
14316 | static const char * | |
14317 | get_dynamic_reloc_section_name (bfd * abfd, | |
14318 | asection * sec, | |
14319 | bfd_boolean is_rela) | |
14320 | { | |
ddcf1fcf BS |
14321 | char *name; |
14322 | const char *old_name = bfd_get_section_name (NULL, sec); | |
14323 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 14324 | |
ddcf1fcf | 14325 | if (old_name == NULL) |
83bac4b0 NC |
14326 | return NULL; |
14327 | ||
ddcf1fcf | 14328 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 14329 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
14330 | |
14331 | return name; | |
14332 | } | |
14333 | ||
14334 | /* Returns the dynamic reloc section associated with SEC. | |
14335 | If necessary compute the name of the dynamic reloc section based | |
14336 | on SEC's name (looked up in ABFD's string table) and the setting | |
14337 | of IS_RELA. */ | |
14338 | ||
14339 | asection * | |
14340 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
14341 | asection * sec, | |
14342 | bfd_boolean is_rela) | |
14343 | { | |
14344 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14345 | ||
14346 | if (reloc_sec == NULL) | |
14347 | { | |
14348 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14349 | ||
14350 | if (name != NULL) | |
14351 | { | |
3d4d4302 | 14352 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
14353 | |
14354 | if (reloc_sec != NULL) | |
14355 | elf_section_data (sec)->sreloc = reloc_sec; | |
14356 | } | |
14357 | } | |
14358 | ||
14359 | return reloc_sec; | |
14360 | } | |
14361 | ||
14362 | /* Returns the dynamic reloc section associated with SEC. If the | |
14363 | section does not exist it is created and attached to the DYNOBJ | |
14364 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
14365 | structure. | |
f8076f98 | 14366 | |
83bac4b0 NC |
14367 | ALIGNMENT is the alignment for the newly created section and |
14368 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
14369 | or .rel.<SEC's name>. The section name is looked up in the | |
14370 | string table associated with ABFD. */ | |
14371 | ||
14372 | asection * | |
ca4be51c AM |
14373 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
14374 | bfd *dynobj, | |
14375 | unsigned int alignment, | |
14376 | bfd *abfd, | |
14377 | bfd_boolean is_rela) | |
83bac4b0 NC |
14378 | { |
14379 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14380 | ||
14381 | if (reloc_sec == NULL) | |
14382 | { | |
14383 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14384 | ||
14385 | if (name == NULL) | |
14386 | return NULL; | |
14387 | ||
3d4d4302 | 14388 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
14389 | |
14390 | if (reloc_sec == NULL) | |
14391 | { | |
3d4d4302 AM |
14392 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
14393 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
14394 | if ((sec->flags & SEC_ALLOC) != 0) |
14395 | flags |= SEC_ALLOC | SEC_LOAD; | |
14396 | ||
3d4d4302 | 14397 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
14398 | if (reloc_sec != NULL) |
14399 | { | |
8877b5e5 AM |
14400 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
14401 | name. Override as it may be wrong, eg. for a user | |
14402 | section named "auto" we'll get ".relauto" which is | |
14403 | seen to be a .rela section. */ | |
14404 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
14405 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
14406 | reloc_sec = NULL; | |
14407 | } | |
14408 | } | |
14409 | ||
14410 | elf_section_data (sec)->sreloc = reloc_sec; | |
14411 | } | |
14412 | ||
14413 | return reloc_sec; | |
14414 | } | |
1338dd10 | 14415 | |
bffebb6b AM |
14416 | /* Copy the ELF symbol type and other attributes for a linker script |
14417 | assignment from HSRC to HDEST. Generally this should be treated as | |
14418 | if we found a strong non-dynamic definition for HDEST (except that | |
14419 | ld ignores multiple definition errors). */ | |
1338dd10 | 14420 | void |
bffebb6b AM |
14421 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
14422 | struct bfd_link_hash_entry *hdest, | |
14423 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 14424 | { |
bffebb6b AM |
14425 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
14426 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14427 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14428 | |
14429 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14430 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14431 | |
14432 | isym.st_other = ehsrc->other; | |
b8417128 | 14433 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14434 | } |
351f65ca L |
14435 | |
14436 | /* Append a RELA relocation REL to section S in BFD. */ | |
14437 | ||
14438 | void | |
14439 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14440 | { | |
14441 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14442 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14443 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14444 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14445 | } | |
14446 | ||
14447 | /* Append a REL relocation REL to section S in BFD. */ | |
14448 | ||
14449 | void | |
14450 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14451 | { | |
14452 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14453 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14454 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14455 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14456 | } |
7dba9362 AM |
14457 | |
14458 | /* Define __start, __stop, .startof. or .sizeof. symbol. */ | |
14459 | ||
14460 | struct bfd_link_hash_entry * | |
14461 | bfd_elf_define_start_stop (struct bfd_link_info *info, | |
14462 | const char *symbol, asection *sec) | |
14463 | { | |
487b6440 | 14464 | struct elf_link_hash_entry *h; |
7dba9362 | 14465 | |
487b6440 AM |
14466 | h = elf_link_hash_lookup (elf_hash_table (info), symbol, |
14467 | FALSE, FALSE, TRUE); | |
14468 | if (h != NULL | |
14469 | && (h->root.type == bfd_link_hash_undefined | |
14470 | || h->root.type == bfd_link_hash_undefweak | |
bf3077a6 | 14471 | || ((h->ref_regular || h->def_dynamic) && !h->def_regular))) |
7dba9362 | 14472 | { |
bf3077a6 | 14473 | bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic; |
487b6440 AM |
14474 | h->root.type = bfd_link_hash_defined; |
14475 | h->root.u.def.section = sec; | |
14476 | h->root.u.def.value = 0; | |
14477 | h->def_regular = 1; | |
14478 | h->def_dynamic = 0; | |
14479 | h->start_stop = 1; | |
14480 | h->u2.start_stop_section = sec; | |
14481 | if (symbol[0] == '.') | |
14482 | { | |
14483 | /* .startof. and .sizeof. symbols are local. */ | |
559192d8 AM |
14484 | const struct elf_backend_data *bed; |
14485 | bed = get_elf_backend_data (info->output_bfd); | |
14486 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
487b6440 | 14487 | } |
36b8fda5 AM |
14488 | else |
14489 | { | |
14490 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
14491 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; | |
bf3077a6 | 14492 | if (was_dynamic) |
36b8fda5 AM |
14493 | bfd_elf_link_record_dynamic_symbol (info, h); |
14494 | } | |
487b6440 | 14495 | return &h->root; |
7dba9362 | 14496 | } |
487b6440 | 14497 | return NULL; |
7dba9362 | 14498 | } |