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 | ||
2225 | /* Figure out appropriate versions for all the symbols. We may not | |
2226 | have the version number script until we have read all of the input | |
2227 | files, so until that point we don't know which symbols should be | |
2228 | local. This function is called via elf_link_hash_traverse. */ | |
2229 | ||
28caa186 | 2230 | static bfd_boolean |
268b6b39 | 2231 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2232 | { |
28caa186 | 2233 | struct elf_info_failed *sinfo; |
45d6a902 | 2234 | struct bfd_link_info *info; |
9c5bfbb7 | 2235 | const struct elf_backend_data *bed; |
45d6a902 AM |
2236 | struct elf_info_failed eif; |
2237 | char *p; | |
45d6a902 | 2238 | |
a50b1753 | 2239 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2240 | info = sinfo->info; |
2241 | ||
45d6a902 AM |
2242 | /* Fix the symbol flags. */ |
2243 | eif.failed = FALSE; | |
2244 | eif.info = info; | |
2245 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2246 | { | |
2247 | if (eif.failed) | |
2248 | sinfo->failed = TRUE; | |
2249 | return FALSE; | |
2250 | } | |
2251 | ||
2252 | /* We only need version numbers for symbols defined in regular | |
2253 | objects. */ | |
f5385ebf | 2254 | if (!h->def_regular) |
45d6a902 AM |
2255 | return TRUE; |
2256 | ||
28caa186 | 2257 | bed = get_elf_backend_data (info->output_bfd); |
45d6a902 AM |
2258 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2259 | if (p != NULL && h->verinfo.vertree == NULL) | |
2260 | { | |
2261 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2262 | |
45d6a902 AM |
2263 | ++p; |
2264 | if (*p == ELF_VER_CHR) | |
6e33951e | 2265 | ++p; |
45d6a902 AM |
2266 | |
2267 | /* If there is no version string, we can just return out. */ | |
2268 | if (*p == '\0') | |
6e33951e | 2269 | return TRUE; |
45d6a902 AM |
2270 | |
2271 | /* Look for the version. If we find it, it is no longer weak. */ | |
fd91d419 | 2272 | for (t = sinfo->info->version_info; t != NULL; t = t->next) |
45d6a902 AM |
2273 | { |
2274 | if (strcmp (t->name, p) == 0) | |
2275 | { | |
2276 | size_t len; | |
2277 | char *alc; | |
2278 | struct bfd_elf_version_expr *d; | |
2279 | ||
2280 | len = p - h->root.root.string; | |
a50b1753 | 2281 | alc = (char *) bfd_malloc (len); |
45d6a902 | 2282 | if (alc == NULL) |
14b1c01e AM |
2283 | { |
2284 | sinfo->failed = TRUE; | |
2285 | return FALSE; | |
2286 | } | |
45d6a902 AM |
2287 | memcpy (alc, h->root.root.string, len - 1); |
2288 | alc[len - 1] = '\0'; | |
2289 | if (alc[len - 2] == ELF_VER_CHR) | |
2290 | alc[len - 2] = '\0'; | |
2291 | ||
2292 | h->verinfo.vertree = t; | |
2293 | t->used = TRUE; | |
2294 | d = NULL; | |
2295 | ||
108ba305 JJ |
2296 | if (t->globals.list != NULL) |
2297 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
2298 | |
2299 | /* See if there is anything to force this symbol to | |
2300 | local scope. */ | |
108ba305 | 2301 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 2302 | { |
108ba305 JJ |
2303 | d = (*t->match) (&t->locals, NULL, alc); |
2304 | if (d != NULL | |
2305 | && h->dynindx != -1 | |
108ba305 JJ |
2306 | && ! info->export_dynamic) |
2307 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2308 | } |
2309 | ||
2310 | free (alc); | |
2311 | break; | |
2312 | } | |
2313 | } | |
2314 | ||
2315 | /* If we are building an application, we need to create a | |
2316 | version node for this version. */ | |
0e1862bb | 2317 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2318 | { |
2319 | struct bfd_elf_version_tree **pp; | |
2320 | int version_index; | |
2321 | ||
2322 | /* If we aren't going to export this symbol, we don't need | |
2323 | to worry about it. */ | |
2324 | if (h->dynindx == -1) | |
2325 | return TRUE; | |
2326 | ||
ef53be89 AM |
2327 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2328 | sizeof *t); | |
45d6a902 AM |
2329 | if (t == NULL) |
2330 | { | |
2331 | sinfo->failed = TRUE; | |
2332 | return FALSE; | |
2333 | } | |
2334 | ||
45d6a902 | 2335 | t->name = p; |
45d6a902 AM |
2336 | t->name_indx = (unsigned int) -1; |
2337 | t->used = TRUE; | |
2338 | ||
2339 | version_index = 1; | |
2340 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2341 | if (sinfo->info->version_info != NULL |
2342 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2343 | version_index = 0; |
fd91d419 L |
2344 | for (pp = &sinfo->info->version_info; |
2345 | *pp != NULL; | |
2346 | pp = &(*pp)->next) | |
45d6a902 AM |
2347 | ++version_index; |
2348 | t->vernum = version_index; | |
2349 | ||
2350 | *pp = t; | |
2351 | ||
2352 | h->verinfo.vertree = t; | |
2353 | } | |
2354 | else if (t == NULL) | |
2355 | { | |
2356 | /* We could not find the version for a symbol when | |
2357 | generating a shared archive. Return an error. */ | |
4eca0228 | 2358 | _bfd_error_handler |
695344c0 | 2359 | /* xgettext:c-format */ |
871b3ab2 | 2360 | (_("%pB: version node not found for symbol %s"), |
28caa186 | 2361 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2362 | bfd_set_error (bfd_error_bad_value); |
2363 | sinfo->failed = TRUE; | |
2364 | return FALSE; | |
2365 | } | |
45d6a902 AM |
2366 | } |
2367 | ||
2368 | /* If we don't have a version for this symbol, see if we can find | |
2369 | something. */ | |
fd91d419 | 2370 | if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) |
45d6a902 | 2371 | { |
1e8fa21e | 2372 | bfd_boolean hide; |
ae5a3597 | 2373 | |
fd91d419 L |
2374 | h->verinfo.vertree |
2375 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2376 | h->root.root.string, &hide); | |
1e8fa21e AM |
2377 | if (h->verinfo.vertree != NULL && hide) |
2378 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2379 | } |
2380 | ||
2381 | return TRUE; | |
2382 | } | |
2383 | \f | |
45d6a902 AM |
2384 | /* Read and swap the relocs from the section indicated by SHDR. This |
2385 | may be either a REL or a RELA section. The relocations are | |
2386 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2387 | which should have already been allocated to contain enough space. | |
2388 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2389 | relocations should be stored. | |
2390 | ||
2391 | Returns FALSE if something goes wrong. */ | |
2392 | ||
2393 | static bfd_boolean | |
268b6b39 | 2394 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2395 | asection *sec, |
268b6b39 AM |
2396 | Elf_Internal_Shdr *shdr, |
2397 | void *external_relocs, | |
2398 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2399 | { |
9c5bfbb7 | 2400 | const struct elf_backend_data *bed; |
268b6b39 | 2401 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2402 | const bfd_byte *erela; |
2403 | const bfd_byte *erelaend; | |
2404 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2405 | Elf_Internal_Shdr *symtab_hdr; |
2406 | size_t nsyms; | |
45d6a902 | 2407 | |
45d6a902 AM |
2408 | /* Position ourselves at the start of the section. */ |
2409 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2410 | return FALSE; | |
2411 | ||
2412 | /* Read the relocations. */ | |
2413 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2414 | return FALSE; | |
2415 | ||
243ef1e0 | 2416 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2417 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2418 | |
45d6a902 AM |
2419 | bed = get_elf_backend_data (abfd); |
2420 | ||
2421 | /* Convert the external relocations to the internal format. */ | |
2422 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2423 | swap_in = bed->s->swap_reloc_in; | |
2424 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2425 | swap_in = bed->s->swap_reloca_in; | |
2426 | else | |
2427 | { | |
2428 | bfd_set_error (bfd_error_wrong_format); | |
2429 | return FALSE; | |
2430 | } | |
2431 | ||
a50b1753 | 2432 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2433 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2434 | irela = internal_relocs; |
2435 | while (erela < erelaend) | |
2436 | { | |
243ef1e0 L |
2437 | bfd_vma r_symndx; |
2438 | ||
45d6a902 | 2439 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2440 | r_symndx = ELF32_R_SYM (irela->r_info); |
2441 | if (bed->s->arch_size == 64) | |
2442 | r_symndx >>= 24; | |
ce98a316 NC |
2443 | if (nsyms > 0) |
2444 | { | |
2445 | if ((size_t) r_symndx >= nsyms) | |
2446 | { | |
4eca0228 | 2447 | _bfd_error_handler |
695344c0 | 2448 | /* xgettext:c-format */ |
2dcf00ce AM |
2449 | (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)" |
2450 | " for offset %#" PRIx64 " in section `%pA'"), | |
2451 | abfd, (uint64_t) r_symndx, (unsigned long) nsyms, | |
2452 | (uint64_t) irela->r_offset, sec); | |
ce98a316 NC |
2453 | bfd_set_error (bfd_error_bad_value); |
2454 | return FALSE; | |
2455 | } | |
2456 | } | |
cf35638d | 2457 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2458 | { |
4eca0228 | 2459 | _bfd_error_handler |
695344c0 | 2460 | /* xgettext:c-format */ |
2dcf00ce AM |
2461 | (_("%pB: non-zero symbol index (%#" PRIx64 ")" |
2462 | " for offset %#" PRIx64 " in section `%pA'" | |
ce98a316 | 2463 | " when the object file has no symbol table"), |
2dcf00ce AM |
2464 | abfd, (uint64_t) r_symndx, |
2465 | (uint64_t) irela->r_offset, sec); | |
243ef1e0 L |
2466 | bfd_set_error (bfd_error_bad_value); |
2467 | return FALSE; | |
2468 | } | |
45d6a902 AM |
2469 | irela += bed->s->int_rels_per_ext_rel; |
2470 | erela += shdr->sh_entsize; | |
2471 | } | |
2472 | ||
2473 | return TRUE; | |
2474 | } | |
2475 | ||
2476 | /* Read and swap the relocs for a section O. They may have been | |
2477 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2478 | not NULL, they are used as buffers to read into. They are known to | |
2479 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2480 | the return value is allocated using either malloc or bfd_alloc, | |
2481 | according to the KEEP_MEMORY argument. If O has two relocation | |
2482 | sections (both REL and RELA relocations), then the REL_HDR | |
2483 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2484 | RELA_HDR relocations. */ |
45d6a902 AM |
2485 | |
2486 | Elf_Internal_Rela * | |
268b6b39 AM |
2487 | _bfd_elf_link_read_relocs (bfd *abfd, |
2488 | asection *o, | |
2489 | void *external_relocs, | |
2490 | Elf_Internal_Rela *internal_relocs, | |
2491 | bfd_boolean keep_memory) | |
45d6a902 | 2492 | { |
268b6b39 | 2493 | void *alloc1 = NULL; |
45d6a902 | 2494 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2495 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2496 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2497 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2498 | |
d4730f92 BS |
2499 | if (esdo->relocs != NULL) |
2500 | return esdo->relocs; | |
45d6a902 AM |
2501 | |
2502 | if (o->reloc_count == 0) | |
2503 | return NULL; | |
2504 | ||
45d6a902 AM |
2505 | if (internal_relocs == NULL) |
2506 | { | |
2507 | bfd_size_type size; | |
2508 | ||
056bafd4 | 2509 | size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); |
45d6a902 | 2510 | if (keep_memory) |
a50b1753 | 2511 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2512 | else |
a50b1753 | 2513 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2514 | if (internal_relocs == NULL) |
2515 | goto error_return; | |
2516 | } | |
2517 | ||
2518 | if (external_relocs == NULL) | |
2519 | { | |
d4730f92 BS |
2520 | bfd_size_type size = 0; |
2521 | ||
2522 | if (esdo->rel.hdr) | |
2523 | size += esdo->rel.hdr->sh_size; | |
2524 | if (esdo->rela.hdr) | |
2525 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2526 | |
268b6b39 | 2527 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2528 | if (alloc1 == NULL) |
2529 | goto error_return; | |
2530 | external_relocs = alloc1; | |
2531 | } | |
2532 | ||
d4730f92 BS |
2533 | internal_rela_relocs = internal_relocs; |
2534 | if (esdo->rel.hdr) | |
2535 | { | |
2536 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2537 | external_relocs, | |
2538 | internal_relocs)) | |
2539 | goto error_return; | |
2540 | external_relocs = (((bfd_byte *) external_relocs) | |
2541 | + esdo->rel.hdr->sh_size); | |
2542 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2543 | * bed->s->int_rels_per_ext_rel); | |
2544 | } | |
2545 | ||
2546 | if (esdo->rela.hdr | |
2547 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2548 | external_relocs, | |
2549 | internal_rela_relocs))) | |
45d6a902 AM |
2550 | goto error_return; |
2551 | ||
2552 | /* Cache the results for next time, if we can. */ | |
2553 | if (keep_memory) | |
d4730f92 | 2554 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2555 | |
2556 | if (alloc1 != NULL) | |
2557 | free (alloc1); | |
2558 | ||
2559 | /* Don't free alloc2, since if it was allocated we are passing it | |
2560 | back (under the name of internal_relocs). */ | |
2561 | ||
2562 | return internal_relocs; | |
2563 | ||
2564 | error_return: | |
2565 | if (alloc1 != NULL) | |
2566 | free (alloc1); | |
2567 | if (alloc2 != NULL) | |
4dd07732 AM |
2568 | { |
2569 | if (keep_memory) | |
2570 | bfd_release (abfd, alloc2); | |
2571 | else | |
2572 | free (alloc2); | |
2573 | } | |
45d6a902 AM |
2574 | return NULL; |
2575 | } | |
2576 | ||
2577 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2578 | section header for a section containing relocations for O. */ | |
2579 | ||
28caa186 | 2580 | static bfd_boolean |
9eaff861 AO |
2581 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2582 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2583 | { |
9eaff861 | 2584 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2585 | |
2586 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2587 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2588 | |
2589 | /* The contents field must last into write_object_contents, so we | |
2590 | allocate it with bfd_alloc rather than malloc. Also since we | |
2591 | cannot be sure that the contents will actually be filled in, | |
2592 | we zero the allocated space. */ | |
a50b1753 | 2593 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2594 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2595 | return FALSE; | |
2596 | ||
d4730f92 | 2597 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2598 | { |
2599 | struct elf_link_hash_entry **p; | |
2600 | ||
ca4be51c AM |
2601 | p = ((struct elf_link_hash_entry **) |
2602 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2603 | if (p == NULL) |
2604 | return FALSE; | |
2605 | ||
d4730f92 | 2606 | reldata->hashes = p; |
45d6a902 AM |
2607 | } |
2608 | ||
2609 | return TRUE; | |
2610 | } | |
2611 | ||
2612 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2613 | originated from the section given by INPUT_REL_HDR) to the | |
2614 | OUTPUT_BFD. */ | |
2615 | ||
2616 | bfd_boolean | |
268b6b39 AM |
2617 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2618 | asection *input_section, | |
2619 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2620 | Elf_Internal_Rela *internal_relocs, |
2621 | struct elf_link_hash_entry **rel_hash | |
2622 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2623 | { |
2624 | Elf_Internal_Rela *irela; | |
2625 | Elf_Internal_Rela *irelaend; | |
2626 | bfd_byte *erel; | |
d4730f92 | 2627 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2628 | asection *output_section; |
9c5bfbb7 | 2629 | const struct elf_backend_data *bed; |
268b6b39 | 2630 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2631 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2632 | |
2633 | output_section = input_section->output_section; | |
45d6a902 | 2634 | |
d4730f92 BS |
2635 | bed = get_elf_backend_data (output_bfd); |
2636 | esdo = elf_section_data (output_section); | |
2637 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2638 | { |
d4730f92 BS |
2639 | output_reldata = &esdo->rel; |
2640 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2641 | } |
d4730f92 BS |
2642 | else if (esdo->rela.hdr |
2643 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2644 | { |
d4730f92 BS |
2645 | output_reldata = &esdo->rela; |
2646 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2647 | } |
2648 | else | |
2649 | { | |
4eca0228 | 2650 | _bfd_error_handler |
695344c0 | 2651 | /* xgettext:c-format */ |
871b3ab2 | 2652 | (_("%pB: relocation size mismatch in %pB section %pA"), |
d003868e | 2653 | output_bfd, input_section->owner, input_section); |
297d8443 | 2654 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2655 | return FALSE; |
2656 | } | |
2657 | ||
d4730f92 BS |
2658 | erel = output_reldata->hdr->contents; |
2659 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2660 | irela = internal_relocs; |
2661 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2662 | * bed->s->int_rels_per_ext_rel); | |
2663 | while (irela < irelaend) | |
2664 | { | |
2665 | (*swap_out) (output_bfd, irela, erel); | |
2666 | irela += bed->s->int_rels_per_ext_rel; | |
2667 | erel += input_rel_hdr->sh_entsize; | |
2668 | } | |
2669 | ||
2670 | /* Bump the counter, so that we know where to add the next set of | |
2671 | relocations. */ | |
d4730f92 | 2672 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2673 | |
2674 | return TRUE; | |
2675 | } | |
2676 | \f | |
508c3946 L |
2677 | /* Make weak undefined symbols in PIE dynamic. */ |
2678 | ||
2679 | bfd_boolean | |
2680 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2681 | struct elf_link_hash_entry *h) | |
2682 | { | |
0e1862bb | 2683 | if (bfd_link_pie (info) |
508c3946 L |
2684 | && h->dynindx == -1 |
2685 | && h->root.type == bfd_link_hash_undefweak) | |
2686 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2687 | ||
2688 | return TRUE; | |
2689 | } | |
2690 | ||
45d6a902 AM |
2691 | /* Fix up the flags for a symbol. This handles various cases which |
2692 | can only be fixed after all the input files are seen. This is | |
2693 | currently called by both adjust_dynamic_symbol and | |
2694 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2695 | the face of future changes. */ | |
2696 | ||
28caa186 | 2697 | static bfd_boolean |
268b6b39 AM |
2698 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2699 | struct elf_info_failed *eif) | |
45d6a902 | 2700 | { |
33774f08 | 2701 | const struct elf_backend_data *bed; |
508c3946 | 2702 | |
45d6a902 AM |
2703 | /* If this symbol was mentioned in a non-ELF file, try to set |
2704 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2705 | permit a non-ELF file to correctly refer to a symbol defined in | |
2706 | an ELF dynamic object. */ | |
f5385ebf | 2707 | if (h->non_elf) |
45d6a902 AM |
2708 | { |
2709 | while (h->root.type == bfd_link_hash_indirect) | |
2710 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2711 | ||
2712 | if (h->root.type != bfd_link_hash_defined | |
2713 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2714 | { |
2715 | h->ref_regular = 1; | |
2716 | h->ref_regular_nonweak = 1; | |
2717 | } | |
45d6a902 AM |
2718 | else |
2719 | { | |
2720 | if (h->root.u.def.section->owner != NULL | |
2721 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2722 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2723 | { |
2724 | h->ref_regular = 1; | |
2725 | h->ref_regular_nonweak = 1; | |
2726 | } | |
45d6a902 | 2727 | else |
f5385ebf | 2728 | h->def_regular = 1; |
45d6a902 AM |
2729 | } |
2730 | ||
2731 | if (h->dynindx == -1 | |
f5385ebf AM |
2732 | && (h->def_dynamic |
2733 | || h->ref_dynamic)) | |
45d6a902 | 2734 | { |
c152c796 | 2735 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2736 | { |
2737 | eif->failed = TRUE; | |
2738 | return FALSE; | |
2739 | } | |
2740 | } | |
2741 | } | |
2742 | else | |
2743 | { | |
f5385ebf | 2744 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2745 | was first seen in a non-ELF file. Fortunately, if the symbol |
2746 | was first seen in an ELF file, we're probably OK unless the | |
2747 | symbol was defined in a non-ELF file. Catch that case here. | |
2748 | FIXME: We're still in trouble if the symbol was first seen in | |
2749 | a dynamic object, and then later in a non-ELF regular object. */ | |
2750 | if ((h->root.type == bfd_link_hash_defined | |
2751 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2752 | && !h->def_regular |
45d6a902 AM |
2753 | && (h->root.u.def.section->owner != NULL |
2754 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2755 | != bfd_target_elf_flavour) | |
2756 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2757 | && !h->def_dynamic))) |
2758 | h->def_regular = 1; | |
45d6a902 AM |
2759 | } |
2760 | ||
508c3946 | 2761 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2762 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2763 | if (bed->elf_backend_fixup_symbol | |
2764 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2765 | return FALSE; | |
508c3946 | 2766 | |
45d6a902 AM |
2767 | /* If this is a final link, and the symbol was defined as a common |
2768 | symbol in a regular object file, and there was no definition in | |
2769 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2770 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2771 | flag will not have been set. */ |
2772 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2773 | && !h->def_regular |
2774 | && h->ref_regular | |
2775 | && !h->def_dynamic | |
96f29d96 | 2776 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2777 | h->def_regular = 1; |
45d6a902 | 2778 | |
4deb8f71 L |
2779 | /* If a weak undefined symbol has non-default visibility, we also |
2780 | hide it from the dynamic linker. */ | |
2781 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
2782 | && h->root.type == bfd_link_hash_undefweak) | |
2783 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2784 | ||
2785 | /* A hidden versioned symbol in executable should be forced local if | |
2786 | it is is locally defined, not referenced by shared library and not | |
2787 | exported. */ | |
2788 | else if (bfd_link_executable (eif->info) | |
2789 | && h->versioned == versioned_hidden | |
2790 | && !eif->info->export_dynamic | |
2791 | && !h->dynamic | |
2792 | && !h->ref_dynamic | |
2793 | && h->def_regular) | |
2794 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2795 | ||
45d6a902 AM |
2796 | /* If -Bsymbolic was used (which means to bind references to global |
2797 | symbols to the definition within the shared object), and this | |
2798 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2799 | need a PLT entry. Likewise, if the symbol has non-default |
2800 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2801 | will force it local. */ |
4deb8f71 L |
2802 | else if (h->needs_plt |
2803 | && bfd_link_pic (eif->info) | |
2804 | && is_elf_hash_table (eif->info->hash) | |
2805 | && (SYMBOLIC_BIND (eif->info, h) | |
2806 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
2807 | && h->def_regular) | |
45d6a902 | 2808 | { |
45d6a902 AM |
2809 | bfd_boolean force_local; |
2810 | ||
45d6a902 AM |
2811 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2812 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2813 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2814 | } | |
2815 | ||
45d6a902 AM |
2816 | /* If this is a weak defined symbol in a dynamic object, and we know |
2817 | the real definition in the dynamic object, copy interesting flags | |
2818 | over to the real definition. */ | |
60d67dc8 | 2819 | if (h->is_weakalias) |
45d6a902 | 2820 | { |
60d67dc8 AM |
2821 | struct elf_link_hash_entry *def = weakdef (h); |
2822 | ||
45d6a902 AM |
2823 | /* If the real definition is defined by a regular object file, |
2824 | don't do anything special. See the longer description in | |
2825 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
60d67dc8 AM |
2826 | if (def->def_regular) |
2827 | { | |
2828 | h = def; | |
2829 | while ((h = h->u.alias) != def) | |
2830 | h->is_weakalias = 0; | |
2831 | } | |
45d6a902 | 2832 | else |
a26587ba | 2833 | { |
4e6b54a6 AM |
2834 | while (h->root.type == bfd_link_hash_indirect) |
2835 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4e6b54a6 AM |
2836 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
2837 | || h->root.type == bfd_link_hash_defweak); | |
60d67dc8 AM |
2838 | BFD_ASSERT (def->def_dynamic); |
2839 | BFD_ASSERT (def->root.type == bfd_link_hash_defined); | |
2840 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); | |
a26587ba | 2841 | } |
45d6a902 AM |
2842 | } |
2843 | ||
2844 | return TRUE; | |
2845 | } | |
2846 | ||
2847 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2848 | called via elf_link_hash_traverse, and also calls itself | |
2849 | recursively. */ | |
2850 | ||
28caa186 | 2851 | static bfd_boolean |
268b6b39 | 2852 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2853 | { |
a50b1753 | 2854 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
559192d8 | 2855 | struct elf_link_hash_table *htab; |
9c5bfbb7 | 2856 | const struct elf_backend_data *bed; |
45d6a902 | 2857 | |
0eddce27 | 2858 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2859 | return FALSE; |
2860 | ||
45d6a902 AM |
2861 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2862 | if (h->root.type == bfd_link_hash_indirect) | |
2863 | return TRUE; | |
2864 | ||
2865 | /* Fix the symbol flags. */ | |
2866 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2867 | return FALSE; | |
2868 | ||
559192d8 AM |
2869 | htab = elf_hash_table (eif->info); |
2870 | bed = get_elf_backend_data (htab->dynobj); | |
2871 | ||
954b63d4 AM |
2872 | if (h->root.type == bfd_link_hash_undefweak) |
2873 | { | |
2874 | if (eif->info->dynamic_undefined_weak == 0) | |
559192d8 | 2875 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
954b63d4 AM |
2876 | else if (eif->info->dynamic_undefined_weak > 0 |
2877 | && h->ref_regular | |
2878 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2879 | && !bfd_hide_sym_by_version (eif->info->version_info, | |
2880 | h->root.root.string)) | |
2881 | { | |
2882 | if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2883 | { | |
2884 | eif->failed = TRUE; | |
2885 | return FALSE; | |
2886 | } | |
2887 | } | |
2888 | } | |
2889 | ||
45d6a902 AM |
2890 | /* If this symbol does not require a PLT entry, and it is not |
2891 | defined by a dynamic object, or is not referenced by a regular | |
2892 | object, ignore it. We do have to handle a weak defined symbol, | |
2893 | even if no regular object refers to it, if we decided to add it | |
2894 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2895 | about symbols which are defined by one dynamic object and | |
2896 | referenced by another one? */ | |
f5385ebf | 2897 | if (!h->needs_plt |
91e21fb7 | 2898 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2899 | && (h->def_regular |
2900 | || !h->def_dynamic | |
2901 | || (!h->ref_regular | |
60d67dc8 | 2902 | && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) |
45d6a902 | 2903 | { |
a6aa5195 | 2904 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
2905 | return TRUE; |
2906 | } | |
2907 | ||
2908 | /* If we've already adjusted this symbol, don't do it again. This | |
2909 | can happen via a recursive call. */ | |
f5385ebf | 2910 | if (h->dynamic_adjusted) |
45d6a902 AM |
2911 | return TRUE; |
2912 | ||
2913 | /* Don't look at this symbol again. Note that we must set this | |
2914 | after checking the above conditions, because we may look at a | |
2915 | symbol once, decide not to do anything, and then get called | |
2916 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2917 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2918 | |
2919 | /* If this is a weak definition, and we know a real definition, and | |
2920 | the real symbol is not itself defined by a regular object file, | |
2921 | then get a good value for the real definition. We handle the | |
2922 | real symbol first, for the convenience of the backend routine. | |
2923 | ||
2924 | Note that there is a confusing case here. If the real definition | |
2925 | is defined by a regular object file, we don't get the real symbol | |
2926 | from the dynamic object, but we do get the weak symbol. If the | |
2927 | processor backend uses a COPY reloc, then if some routine in the | |
2928 | dynamic object changes the real symbol, we will not see that | |
2929 | change in the corresponding weak symbol. This is the way other | |
2930 | ELF linkers work as well, and seems to be a result of the shared | |
2931 | library model. | |
2932 | ||
2933 | I will clarify this issue. Most SVR4 shared libraries define the | |
2934 | variable _timezone and define timezone as a weak synonym. The | |
2935 | tzset call changes _timezone. If you write | |
2936 | extern int timezone; | |
2937 | int _timezone = 5; | |
2938 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2939 | you might expect that, since timezone is a synonym for _timezone, | |
2940 | the same number will print both times. However, if the processor | |
2941 | backend uses a COPY reloc, then actually timezone will be copied | |
2942 | into your process image, and, since you define _timezone | |
2943 | yourself, _timezone will not. Thus timezone and _timezone will | |
2944 | wind up at different memory locations. The tzset call will set | |
2945 | _timezone, leaving timezone unchanged. */ | |
2946 | ||
60d67dc8 | 2947 | if (h->is_weakalias) |
45d6a902 | 2948 | { |
60d67dc8 AM |
2949 | struct elf_link_hash_entry *def = weakdef (h); |
2950 | ||
ec24dc88 | 2951 | /* If we get to this point, there is an implicit reference to |
60d67dc8 AM |
2952 | the alias by a regular object file via the weak symbol H. */ |
2953 | def->ref_regular = 1; | |
45d6a902 | 2954 | |
ec24dc88 | 2955 | /* Ensure that the backend adjust_dynamic_symbol function sees |
60d67dc8 AM |
2956 | the strong alias before H by recursively calling ourselves. */ |
2957 | if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) | |
45d6a902 AM |
2958 | return FALSE; |
2959 | } | |
2960 | ||
2961 | /* If a symbol has no type and no size and does not require a PLT | |
2962 | entry, then we are probably about to do the wrong thing here: we | |
2963 | are probably going to create a COPY reloc for an empty object. | |
2964 | This case can arise when a shared object is built with assembly | |
2965 | code, and the assembly code fails to set the symbol type. */ | |
2966 | if (h->size == 0 | |
2967 | && h->type == STT_NOTYPE | |
f5385ebf | 2968 | && !h->needs_plt) |
4eca0228 | 2969 | _bfd_error_handler |
45d6a902 AM |
2970 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
2971 | h->root.root.string); | |
2972 | ||
45d6a902 AM |
2973 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
2974 | { | |
2975 | eif->failed = TRUE; | |
2976 | return FALSE; | |
2977 | } | |
2978 | ||
2979 | return TRUE; | |
2980 | } | |
2981 | ||
027297b7 L |
2982 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
2983 | DYNBSS. */ | |
2984 | ||
2985 | bfd_boolean | |
6cabe1ea AM |
2986 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
2987 | struct elf_link_hash_entry *h, | |
027297b7 L |
2988 | asection *dynbss) |
2989 | { | |
91ac5911 | 2990 | unsigned int power_of_two; |
027297b7 L |
2991 | bfd_vma mask; |
2992 | asection *sec = h->root.u.def.section; | |
2993 | ||
de194d85 | 2994 | /* The section alignment of the definition is the maximum alignment |
91ac5911 L |
2995 | requirement of symbols defined in the section. Since we don't |
2996 | know the symbol alignment requirement, we start with the | |
2997 | maximum alignment and check low bits of the symbol address | |
2998 | for the minimum alignment. */ | |
2999 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
3000 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
3001 | while ((h->root.u.def.value & mask) != 0) | |
3002 | { | |
3003 | mask >>= 1; | |
3004 | --power_of_two; | |
3005 | } | |
027297b7 | 3006 | |
91ac5911 L |
3007 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
3008 | dynbss)) | |
027297b7 L |
3009 | { |
3010 | /* Adjust the section alignment if needed. */ | |
3011 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 3012 | power_of_two)) |
027297b7 L |
3013 | return FALSE; |
3014 | } | |
3015 | ||
91ac5911 | 3016 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
3017 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
3018 | ||
3019 | /* Define the symbol as being at this point in DYNBSS. */ | |
3020 | h->root.u.def.section = dynbss; | |
3021 | h->root.u.def.value = dynbss->size; | |
3022 | ||
3023 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
3024 | dynbss->size += h->size; | |
3025 | ||
f7483970 L |
3026 | /* No error if extern_protected_data is true. */ |
3027 | if (h->protected_def | |
889c2a67 L |
3028 | && (!info->extern_protected_data |
3029 | || (info->extern_protected_data < 0 | |
3030 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 | 3031 | info->callbacks->einfo |
c1c8c1ef | 3032 | (_("%P: copy reloc against protected `%pT' is dangerous\n"), |
d07a1b05 | 3033 | h->root.root.string); |
6cabe1ea | 3034 | |
027297b7 L |
3035 | return TRUE; |
3036 | } | |
3037 | ||
45d6a902 AM |
3038 | /* Adjust all external symbols pointing into SEC_MERGE sections |
3039 | to reflect the object merging within the sections. */ | |
3040 | ||
28caa186 | 3041 | static bfd_boolean |
268b6b39 | 3042 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
3043 | { |
3044 | asection *sec; | |
3045 | ||
45d6a902 AM |
3046 | if ((h->root.type == bfd_link_hash_defined |
3047 | || h->root.type == bfd_link_hash_defweak) | |
3048 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 3049 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 3050 | { |
a50b1753 | 3051 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
3052 | |
3053 | h->root.u.def.value = | |
3054 | _bfd_merged_section_offset (output_bfd, | |
3055 | &h->root.u.def.section, | |
3056 | elf_section_data (sec)->sec_info, | |
753731ee | 3057 | h->root.u.def.value); |
45d6a902 AM |
3058 | } |
3059 | ||
3060 | return TRUE; | |
3061 | } | |
986a241f RH |
3062 | |
3063 | /* Returns false if the symbol referred to by H should be considered | |
3064 | to resolve local to the current module, and true if it should be | |
3065 | considered to bind dynamically. */ | |
3066 | ||
3067 | bfd_boolean | |
268b6b39 AM |
3068 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
3069 | struct bfd_link_info *info, | |
89a2ee5a | 3070 | bfd_boolean not_local_protected) |
986a241f RH |
3071 | { |
3072 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
3073 | const struct elf_backend_data *bed; |
3074 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
3075 | |
3076 | if (h == NULL) | |
3077 | return FALSE; | |
3078 | ||
3079 | while (h->root.type == bfd_link_hash_indirect | |
3080 | || h->root.type == bfd_link_hash_warning) | |
3081 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3082 | ||
3083 | /* If it was forced local, then clearly it's not dynamic. */ | |
3084 | if (h->dynindx == -1) | |
3085 | return FALSE; | |
f5385ebf | 3086 | if (h->forced_local) |
986a241f RH |
3087 | return FALSE; |
3088 | ||
3089 | /* Identify the cases where name binding rules say that a | |
3090 | visible symbol resolves locally. */ | |
0e1862bb L |
3091 | binding_stays_local_p = (bfd_link_executable (info) |
3092 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
3093 | |
3094 | switch (ELF_ST_VISIBILITY (h->other)) | |
3095 | { | |
3096 | case STV_INTERNAL: | |
3097 | case STV_HIDDEN: | |
3098 | return FALSE; | |
3099 | ||
3100 | case STV_PROTECTED: | |
fcb93ecf PB |
3101 | hash_table = elf_hash_table (info); |
3102 | if (!is_elf_hash_table (hash_table)) | |
3103 | return FALSE; | |
3104 | ||
3105 | bed = get_elf_backend_data (hash_table->dynobj); | |
3106 | ||
986a241f RH |
3107 | /* Proper resolution for function pointer equality may require |
3108 | that these symbols perhaps be resolved dynamically, even though | |
3109 | we should be resolving them to the current module. */ | |
89a2ee5a | 3110 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
3111 | binding_stays_local_p = TRUE; |
3112 | break; | |
3113 | ||
3114 | default: | |
986a241f RH |
3115 | break; |
3116 | } | |
3117 | ||
aa37626c | 3118 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 3119 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
3120 | return TRUE; |
3121 | ||
986a241f RH |
3122 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
3123 | us that it remains local. */ | |
3124 | return !binding_stays_local_p; | |
3125 | } | |
f6c52c13 AM |
3126 | |
3127 | /* Return true if the symbol referred to by H should be considered | |
3128 | to resolve local to the current module, and false otherwise. Differs | |
3129 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 3130 | undefined symbols. The two functions are virtually identical except |
0fad2956 MR |
3131 | for the place where dynindx == -1 is tested. If that test is true, |
3132 | _bfd_elf_dynamic_symbol_p will say the symbol is local, while | |
3133 | _bfd_elf_symbol_refs_local_p will say the symbol is local only for | |
3134 | defined symbols. | |
89a2ee5a AM |
3135 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as |
3136 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
3137 | treatment of undefined weak symbols. For those that do not make | |
3138 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
3139 | |
3140 | bfd_boolean | |
268b6b39 AM |
3141 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3142 | struct bfd_link_info *info, | |
3143 | bfd_boolean local_protected) | |
f6c52c13 | 3144 | { |
fcb93ecf PB |
3145 | const struct elf_backend_data *bed; |
3146 | struct elf_link_hash_table *hash_table; | |
3147 | ||
f6c52c13 AM |
3148 | /* If it's a local sym, of course we resolve locally. */ |
3149 | if (h == NULL) | |
3150 | return TRUE; | |
3151 | ||
d95edcac L |
3152 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3153 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3154 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3155 | return TRUE; | |
3156 | ||
0fad2956 MR |
3157 | /* Forced local symbols resolve locally. */ |
3158 | if (h->forced_local) | |
3159 | return TRUE; | |
3160 | ||
7e2294f9 AO |
3161 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3162 | flag set, so test it first, and don't bail out. */ | |
3163 | if (ELF_COMMON_DEF_P (h)) | |
3164 | /* Do nothing. */; | |
f6c52c13 | 3165 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3166 | resolve locally. The sym is either undefined or dynamic. */ |
3167 | else if (!h->def_regular) | |
f6c52c13 AM |
3168 | return FALSE; |
3169 | ||
0fad2956 | 3170 | /* Non-dynamic symbols resolve locally. */ |
f6c52c13 AM |
3171 | if (h->dynindx == -1) |
3172 | return TRUE; | |
3173 | ||
3174 | /* At this point, we know the symbol is defined and dynamic. In an | |
3175 | executable it must resolve locally, likewise when building symbolic | |
3176 | shared libraries. */ | |
0e1862bb | 3177 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3178 | return TRUE; |
3179 | ||
3180 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3181 | with default visibility might not resolve locally. */ | |
3182 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3183 | return FALSE; | |
3184 | ||
fcb93ecf PB |
3185 | hash_table = elf_hash_table (info); |
3186 | if (!is_elf_hash_table (hash_table)) | |
3187 | return TRUE; | |
3188 | ||
3189 | bed = get_elf_backend_data (hash_table->dynobj); | |
3190 | ||
f7483970 L |
3191 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3192 | symbols are local. */ | |
889c2a67 L |
3193 | if ((!info->extern_protected_data |
3194 | || (info->extern_protected_data < 0 | |
3195 | && !bed->extern_protected_data)) | |
3196 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3197 | return TRUE; |
3198 | ||
f6c52c13 | 3199 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3200 | symbols be treated as dynamic symbols. If the address of a |
3201 | function not defined in an executable is set to that function's | |
3202 | plt entry in the executable, then the address of the function in | |
3203 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3204 | return local_protected; |
3205 | } | |
e1918d23 AM |
3206 | |
3207 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3208 | aligned. Returns the first TLS output section. */ | |
3209 | ||
3210 | struct bfd_section * | |
3211 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3212 | { | |
3213 | struct bfd_section *sec, *tls; | |
3214 | unsigned int align = 0; | |
3215 | ||
3216 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3217 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3218 | break; | |
3219 | tls = sec; | |
3220 | ||
3221 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3222 | if (sec->alignment_power > align) | |
3223 | align = sec->alignment_power; | |
3224 | ||
3225 | elf_hash_table (info)->tls_sec = tls; | |
3226 | ||
3227 | /* Ensure the alignment of the first section is the largest alignment, | |
3228 | so that the tls segment starts aligned. */ | |
3229 | if (tls != NULL) | |
3230 | tls->alignment_power = align; | |
3231 | ||
3232 | return tls; | |
3233 | } | |
0ad989f9 L |
3234 | |
3235 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3236 | static bfd_boolean | |
3237 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3238 | Elf_Internal_Sym *sym) | |
3239 | { | |
a4d8e49b L |
3240 | const struct elf_backend_data *bed; |
3241 | ||
0ad989f9 L |
3242 | /* Local symbols do not count, but target specific ones might. */ |
3243 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3244 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3245 | return FALSE; | |
3246 | ||
fcb93ecf | 3247 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3248 | /* Function symbols do not count. */ |
fcb93ecf | 3249 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3250 | return FALSE; |
3251 | ||
3252 | /* If the section is undefined, then so is the symbol. */ | |
3253 | if (sym->st_shndx == SHN_UNDEF) | |
3254 | return FALSE; | |
3255 | ||
3256 | /* If the symbol is defined in the common section, then | |
3257 | it is a common definition and so does not count. */ | |
a4d8e49b | 3258 | if (bed->common_definition (sym)) |
0ad989f9 L |
3259 | return FALSE; |
3260 | ||
3261 | /* If the symbol is in a target specific section then we | |
3262 | must rely upon the backend to tell us what it is. */ | |
3263 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3264 | /* FIXME - this function is not coded yet: | |
3265 | ||
3266 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3267 | ||
3268 | Instead for now assume that the definition is not global, | |
3269 | Even if this is wrong, at least the linker will behave | |
3270 | in the same way that it used to do. */ | |
3271 | return FALSE; | |
3272 | ||
3273 | return TRUE; | |
3274 | } | |
3275 | ||
3276 | /* Search the symbol table of the archive element of the archive ABFD | |
3277 | whose archive map contains a mention of SYMDEF, and determine if | |
3278 | the symbol is defined in this element. */ | |
3279 | static bfd_boolean | |
3280 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3281 | { | |
3282 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3283 | size_t symcount; |
3284 | size_t extsymcount; | |
3285 | size_t extsymoff; | |
0ad989f9 L |
3286 | Elf_Internal_Sym *isymbuf; |
3287 | Elf_Internal_Sym *isym; | |
3288 | Elf_Internal_Sym *isymend; | |
3289 | bfd_boolean result; | |
3290 | ||
3291 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3292 | if (abfd == NULL) | |
3293 | return FALSE; | |
3294 | ||
3295 | if (! bfd_check_format (abfd, bfd_object)) | |
3296 | return FALSE; | |
3297 | ||
7dc3990e L |
3298 | /* Select the appropriate symbol table. If we don't know if the |
3299 | object file is an IR object, give linker LTO plugin a chance to | |
3300 | get the correct symbol table. */ | |
3301 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3302 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3303 | || (abfd->plugin_format == bfd_plugin_unknown |
3304 | && bfd_link_plugin_object_p (abfd)) | |
3305 | #endif | |
3306 | ) | |
3307 | { | |
3308 | /* Use the IR symbol table if the object has been claimed by | |
3309 | plugin. */ | |
3310 | abfd = abfd->plugin_dummy_bfd; | |
3311 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3312 | } | |
3313 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3314 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3315 | else | |
3316 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3317 | ||
3318 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3319 | ||
3320 | /* The sh_info field of the symtab header tells us where the | |
3321 | external symbols start. We don't care about the local symbols. */ | |
3322 | if (elf_bad_symtab (abfd)) | |
3323 | { | |
3324 | extsymcount = symcount; | |
3325 | extsymoff = 0; | |
3326 | } | |
3327 | else | |
3328 | { | |
3329 | extsymcount = symcount - hdr->sh_info; | |
3330 | extsymoff = hdr->sh_info; | |
3331 | } | |
3332 | ||
3333 | if (extsymcount == 0) | |
3334 | return FALSE; | |
3335 | ||
3336 | /* Read in the symbol table. */ | |
3337 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3338 | NULL, NULL, NULL); | |
3339 | if (isymbuf == NULL) | |
3340 | return FALSE; | |
3341 | ||
3342 | /* Scan the symbol table looking for SYMDEF. */ | |
3343 | result = FALSE; | |
3344 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3345 | { | |
3346 | const char *name; | |
3347 | ||
3348 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3349 | isym->st_name); | |
3350 | if (name == NULL) | |
3351 | break; | |
3352 | ||
3353 | if (strcmp (name, symdef->name) == 0) | |
3354 | { | |
3355 | result = is_global_data_symbol_definition (abfd, isym); | |
3356 | break; | |
3357 | } | |
3358 | } | |
3359 | ||
3360 | free (isymbuf); | |
3361 | ||
3362 | return result; | |
3363 | } | |
3364 | \f | |
5a580b3a AM |
3365 | /* Add an entry to the .dynamic table. */ |
3366 | ||
3367 | bfd_boolean | |
3368 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3369 | bfd_vma tag, | |
3370 | bfd_vma val) | |
3371 | { | |
3372 | struct elf_link_hash_table *hash_table; | |
3373 | const struct elf_backend_data *bed; | |
3374 | asection *s; | |
3375 | bfd_size_type newsize; | |
3376 | bfd_byte *newcontents; | |
3377 | Elf_Internal_Dyn dyn; | |
3378 | ||
3379 | hash_table = elf_hash_table (info); | |
3380 | if (! is_elf_hash_table (hash_table)) | |
3381 | return FALSE; | |
3382 | ||
3383 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3384 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3385 | BFD_ASSERT (s != NULL); |
3386 | ||
eea6121a | 3387 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3388 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3389 | if (newcontents == NULL) |
3390 | return FALSE; | |
3391 | ||
3392 | dyn.d_tag = tag; | |
3393 | dyn.d_un.d_val = val; | |
eea6121a | 3394 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3395 | |
eea6121a | 3396 | s->size = newsize; |
5a580b3a AM |
3397 | s->contents = newcontents; |
3398 | ||
3399 | return TRUE; | |
3400 | } | |
3401 | ||
3402 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3403 | otherwise just check whether one already exists. Returns -1 on error, | |
3404 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3405 | ||
4ad4eba5 | 3406 | static int |
7e9f0867 AM |
3407 | elf_add_dt_needed_tag (bfd *abfd, |
3408 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3409 | const char *soname, |
3410 | bfd_boolean do_it) | |
5a580b3a AM |
3411 | { |
3412 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3413 | size_t strindex; |
5a580b3a | 3414 | |
7e9f0867 AM |
3415 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3416 | return -1; | |
3417 | ||
5a580b3a | 3418 | hash_table = elf_hash_table (info); |
5a580b3a | 3419 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3420 | if (strindex == (size_t) -1) |
5a580b3a AM |
3421 | return -1; |
3422 | ||
02be4619 | 3423 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3424 | { |
3425 | asection *sdyn; | |
3426 | const struct elf_backend_data *bed; | |
3427 | bfd_byte *extdyn; | |
3428 | ||
3429 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3430 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3431 | if (sdyn != NULL) |
3432 | for (extdyn = sdyn->contents; | |
3433 | extdyn < sdyn->contents + sdyn->size; | |
3434 | extdyn += bed->s->sizeof_dyn) | |
3435 | { | |
3436 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3437 | |
7e9f0867 AM |
3438 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3439 | if (dyn.d_tag == DT_NEEDED | |
3440 | && dyn.d_un.d_val == strindex) | |
3441 | { | |
3442 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3443 | return 1; | |
3444 | } | |
3445 | } | |
5a580b3a AM |
3446 | } |
3447 | ||
3448 | if (do_it) | |
3449 | { | |
7e9f0867 AM |
3450 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3451 | return -1; | |
3452 | ||
5a580b3a AM |
3453 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3454 | return -1; | |
3455 | } | |
3456 | else | |
3457 | /* We were just checking for existence of the tag. */ | |
3458 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3459 | ||
3460 | return 0; | |
3461 | } | |
3462 | ||
7b15fa7a AM |
3463 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3464 | (or the end of list if STOP is NULL), and needed by a library that | |
3465 | will be loaded. */ | |
3466 | ||
010e5ae2 | 3467 | static bfd_boolean |
7b15fa7a AM |
3468 | on_needed_list (const char *soname, |
3469 | struct bfd_link_needed_list *needed, | |
3470 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3471 | { |
7b15fa7a AM |
3472 | struct bfd_link_needed_list *look; |
3473 | for (look = needed; look != stop; look = look->next) | |
3474 | if (strcmp (soname, look->name) == 0 | |
3475 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3476 | /* If needed by a library that itself is not directly | |
3477 | needed, recursively check whether that library is | |
3478 | indirectly needed. Since we add DT_NEEDED entries to | |
3479 | the end of the list, library dependencies appear after | |
3480 | the library. Therefore search prior to the current | |
3481 | LOOK, preventing possible infinite recursion. */ | |
3482 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3483 | return TRUE; |
3484 | ||
3485 | return FALSE; | |
3486 | } | |
3487 | ||
14160578 | 3488 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3489 | static int |
3490 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3491 | { |
3492 | const struct elf_link_hash_entry *h1; | |
3493 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3494 | bfd_signed_vma vdiff; |
5a580b3a AM |
3495 | |
3496 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3497 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3498 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3499 | if (vdiff != 0) | |
3500 | return vdiff > 0 ? 1 : -1; | |
3501 | else | |
3502 | { | |
d3435ae8 | 3503 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3504 | if (sdiff != 0) |
3505 | return sdiff > 0 ? 1 : -1; | |
3506 | } | |
14160578 AM |
3507 | vdiff = h1->size - h2->size; |
3508 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3509 | } |
4ad4eba5 | 3510 | |
5a580b3a AM |
3511 | /* This function is used to adjust offsets into .dynstr for |
3512 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3513 | ||
3514 | static bfd_boolean | |
3515 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3516 | { | |
a50b1753 | 3517 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3518 | |
5a580b3a AM |
3519 | if (h->dynindx != -1) |
3520 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3521 | return TRUE; | |
3522 | } | |
3523 | ||
3524 | /* Assign string offsets in .dynstr, update all structures referencing | |
3525 | them. */ | |
3526 | ||
4ad4eba5 AM |
3527 | static bfd_boolean |
3528 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3529 | { |
3530 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3531 | struct elf_link_local_dynamic_entry *entry; | |
3532 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3533 | bfd *dynobj = hash_table->dynobj; | |
3534 | asection *sdyn; | |
3535 | bfd_size_type size; | |
3536 | const struct elf_backend_data *bed; | |
3537 | bfd_byte *extdyn; | |
3538 | ||
3539 | _bfd_elf_strtab_finalize (dynstr); | |
3540 | size = _bfd_elf_strtab_size (dynstr); | |
3541 | ||
3542 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3543 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3544 | BFD_ASSERT (sdyn != NULL); |
3545 | ||
3546 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3547 | for (extdyn = sdyn->contents; | |
eea6121a | 3548 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3549 | extdyn += bed->s->sizeof_dyn) |
3550 | { | |
3551 | Elf_Internal_Dyn dyn; | |
3552 | ||
3553 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3554 | switch (dyn.d_tag) | |
3555 | { | |
3556 | case DT_STRSZ: | |
3557 | dyn.d_un.d_val = size; | |
3558 | break; | |
3559 | case DT_NEEDED: | |
3560 | case DT_SONAME: | |
3561 | case DT_RPATH: | |
3562 | case DT_RUNPATH: | |
3563 | case DT_FILTER: | |
3564 | case DT_AUXILIARY: | |
7ee314fa AM |
3565 | case DT_AUDIT: |
3566 | case DT_DEPAUDIT: | |
5a580b3a AM |
3567 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3568 | break; | |
3569 | default: | |
3570 | continue; | |
3571 | } | |
3572 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3573 | } | |
3574 | ||
3575 | /* Now update local dynamic symbols. */ | |
3576 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3577 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3578 | entry->isym.st_name); | |
3579 | ||
3580 | /* And the rest of dynamic symbols. */ | |
3581 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3582 | ||
3583 | /* Adjust version definitions. */ | |
3584 | if (elf_tdata (output_bfd)->cverdefs) | |
3585 | { | |
3586 | asection *s; | |
3587 | bfd_byte *p; | |
ef53be89 | 3588 | size_t i; |
5a580b3a AM |
3589 | Elf_Internal_Verdef def; |
3590 | Elf_Internal_Verdaux defaux; | |
3591 | ||
3d4d4302 | 3592 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3593 | p = s->contents; |
3594 | do | |
3595 | { | |
3596 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3597 | &def); | |
3598 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3599 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3600 | continue; | |
5a580b3a AM |
3601 | for (i = 0; i < def.vd_cnt; ++i) |
3602 | { | |
3603 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3604 | (Elf_External_Verdaux *) p, &defaux); | |
3605 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3606 | defaux.vda_name); | |
3607 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3608 | &defaux, (Elf_External_Verdaux *) p); | |
3609 | p += sizeof (Elf_External_Verdaux); | |
3610 | } | |
3611 | } | |
3612 | while (def.vd_next); | |
3613 | } | |
3614 | ||
3615 | /* Adjust version references. */ | |
3616 | if (elf_tdata (output_bfd)->verref) | |
3617 | { | |
3618 | asection *s; | |
3619 | bfd_byte *p; | |
ef53be89 | 3620 | size_t i; |
5a580b3a AM |
3621 | Elf_Internal_Verneed need; |
3622 | Elf_Internal_Vernaux needaux; | |
3623 | ||
3d4d4302 | 3624 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3625 | p = s->contents; |
3626 | do | |
3627 | { | |
3628 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3629 | &need); | |
3630 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3631 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3632 | (Elf_External_Verneed *) p); | |
3633 | p += sizeof (Elf_External_Verneed); | |
3634 | for (i = 0; i < need.vn_cnt; ++i) | |
3635 | { | |
3636 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3637 | (Elf_External_Vernaux *) p, &needaux); | |
3638 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3639 | needaux.vna_name); | |
3640 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3641 | &needaux, | |
3642 | (Elf_External_Vernaux *) p); | |
3643 | p += sizeof (Elf_External_Vernaux); | |
3644 | } | |
3645 | } | |
3646 | while (need.vn_next); | |
3647 | } | |
3648 | ||
3649 | return TRUE; | |
3650 | } | |
3651 | \f | |
13285a1b AM |
3652 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3653 | The default is to only match when the INPUT and OUTPUT are exactly | |
3654 | the same target. */ | |
3655 | ||
3656 | bfd_boolean | |
3657 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3658 | const bfd_target *output) | |
3659 | { | |
3660 | return input == output; | |
3661 | } | |
3662 | ||
3663 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3664 | This version is used when different targets for the same architecture | |
3665 | are virtually identical. */ | |
3666 | ||
3667 | bfd_boolean | |
3668 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3669 | const bfd_target *output) | |
3670 | { | |
3671 | const struct elf_backend_data *obed, *ibed; | |
3672 | ||
3673 | if (input == output) | |
3674 | return TRUE; | |
3675 | ||
3676 | ibed = xvec_get_elf_backend_data (input); | |
3677 | obed = xvec_get_elf_backend_data (output); | |
3678 | ||
3679 | if (ibed->arch != obed->arch) | |
3680 | return FALSE; | |
3681 | ||
3682 | /* If both backends are using this function, deem them compatible. */ | |
3683 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3684 | } | |
3685 | ||
e5034e59 AM |
3686 | /* Make a special call to the linker "notice" function to tell it that |
3687 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3688 | processing the lib. */ |
e5034e59 AM |
3689 | |
3690 | bfd_boolean | |
3691 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3692 | struct bfd_link_info *info, | |
3693 | enum notice_asneeded_action act) | |
3694 | { | |
46135103 | 3695 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3696 | } |
3697 | ||
d9689752 L |
3698 | /* Check relocations an ELF object file. */ |
3699 | ||
3700 | bfd_boolean | |
3701 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3702 | { | |
3703 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3704 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3705 | ||
3706 | /* If this object is the same format as the output object, and it is | |
3707 | not a shared library, then let the backend look through the | |
3708 | relocs. | |
3709 | ||
3710 | This is required to build global offset table entries and to | |
3711 | arrange for dynamic relocs. It is not required for the | |
3712 | particular common case of linking non PIC code, even when linking | |
3713 | against shared libraries, but unfortunately there is no way of | |
3714 | knowing whether an object file has been compiled PIC or not. | |
3715 | Looking through the relocs is not particularly time consuming. | |
3716 | The problem is that we must either (1) keep the relocs in memory, | |
3717 | which causes the linker to require additional runtime memory or | |
3718 | (2) read the relocs twice from the input file, which wastes time. | |
3719 | This would be a good case for using mmap. | |
3720 | ||
3721 | I have no idea how to handle linking PIC code into a file of a | |
3722 | different format. It probably can't be done. */ | |
3723 | if ((abfd->flags & DYNAMIC) == 0 | |
3724 | && is_elf_hash_table (htab) | |
3725 | && bed->check_relocs != NULL | |
3726 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3727 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3728 | { | |
3729 | asection *o; | |
3730 | ||
3731 | for (o = abfd->sections; o != NULL; o = o->next) | |
3732 | { | |
3733 | Elf_Internal_Rela *internal_relocs; | |
3734 | bfd_boolean ok; | |
3735 | ||
5ce03cea | 3736 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3737 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3738 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3739 | || o->reloc_count == 0 |
3740 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3741 | && (o->flags & SEC_DEBUGGING) != 0) | |
3742 | || bfd_is_abs_section (o->output_section)) | |
3743 | continue; | |
3744 | ||
3745 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3746 | info->keep_memory); | |
3747 | if (internal_relocs == NULL) | |
3748 | return FALSE; | |
3749 | ||
3750 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3751 | ||
3752 | if (elf_section_data (o)->relocs != internal_relocs) | |
3753 | free (internal_relocs); | |
3754 | ||
3755 | if (! ok) | |
3756 | return FALSE; | |
3757 | } | |
3758 | } | |
3759 | ||
3760 | return TRUE; | |
3761 | } | |
3762 | ||
4ad4eba5 AM |
3763 | /* Add symbols from an ELF object file to the linker hash table. */ |
3764 | ||
3765 | static bfd_boolean | |
3766 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3767 | { | |
a0c402a5 | 3768 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3769 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3770 | size_t symcount; |
3771 | size_t extsymcount; | |
3772 | size_t extsymoff; | |
4ad4eba5 AM |
3773 | struct elf_link_hash_entry **sym_hash; |
3774 | bfd_boolean dynamic; | |
3775 | Elf_External_Versym *extversym = NULL; | |
3776 | Elf_External_Versym *ever; | |
3777 | struct elf_link_hash_entry *weaks; | |
3778 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3779 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3780 | Elf_Internal_Sym *isymbuf = NULL; |
3781 | Elf_Internal_Sym *isym; | |
3782 | Elf_Internal_Sym *isymend; | |
3783 | const struct elf_backend_data *bed; | |
3784 | bfd_boolean add_needed; | |
66eb6687 | 3785 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3786 | bfd_size_type amt; |
66eb6687 | 3787 | void *alloc_mark = NULL; |
4f87808c AM |
3788 | struct bfd_hash_entry **old_table = NULL; |
3789 | unsigned int old_size = 0; | |
3790 | unsigned int old_count = 0; | |
66eb6687 | 3791 | void *old_tab = NULL; |
66eb6687 AM |
3792 | void *old_ent; |
3793 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3794 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3795 | void *old_strtab = NULL; |
66eb6687 | 3796 | size_t tabsize = 0; |
db6a5d5f | 3797 | asection *s; |
29a9f53e | 3798 | bfd_boolean just_syms; |
4ad4eba5 | 3799 | |
66eb6687 | 3800 | htab = elf_hash_table (info); |
4ad4eba5 | 3801 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3802 | |
3803 | if ((abfd->flags & DYNAMIC) == 0) | |
3804 | dynamic = FALSE; | |
3805 | else | |
3806 | { | |
3807 | dynamic = TRUE; | |
3808 | ||
3809 | /* You can't use -r against a dynamic object. Also, there's no | |
3810 | hope of using a dynamic object which does not exactly match | |
3811 | the format of the output file. */ | |
0e1862bb | 3812 | if (bfd_link_relocatable (info) |
66eb6687 | 3813 | || !is_elf_hash_table (htab) |
f13a99db | 3814 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3815 | { |
0e1862bb | 3816 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3817 | bfd_set_error (bfd_error_invalid_operation); |
3818 | else | |
3819 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3820 | goto error_return; |
3821 | } | |
3822 | } | |
3823 | ||
a0c402a5 L |
3824 | ehdr = elf_elfheader (abfd); |
3825 | if (info->warn_alternate_em | |
3826 | && bed->elf_machine_code != ehdr->e_machine | |
3827 | && ((bed->elf_machine_alt1 != 0 | |
3828 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3829 | || (bed->elf_machine_alt2 != 0 | |
3830 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
9793eb77 | 3831 | _bfd_error_handler |
695344c0 | 3832 | /* xgettext:c-format */ |
9793eb77 | 3833 | (_("alternate ELF machine code found (%d) in %pB, expecting %d"), |
a0c402a5 L |
3834 | ehdr->e_machine, abfd, bed->elf_machine_code); |
3835 | ||
4ad4eba5 AM |
3836 | /* As a GNU extension, any input sections which are named |
3837 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3838 | symbol. This differs from .gnu.warning sections, which generate | |
3839 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3840 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3841 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3842 | { |
db6a5d5f | 3843 | const char *name; |
4ad4eba5 | 3844 | |
db6a5d5f AM |
3845 | name = bfd_get_section_name (abfd, s); |
3846 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3847 | { |
db6a5d5f AM |
3848 | char *msg; |
3849 | bfd_size_type sz; | |
3850 | ||
3851 | name += sizeof ".gnu.warning." - 1; | |
3852 | ||
3853 | /* If this is a shared object, then look up the symbol | |
3854 | in the hash table. If it is there, and it is already | |
3855 | been defined, then we will not be using the entry | |
3856 | from this shared object, so we don't need to warn. | |
3857 | FIXME: If we see the definition in a regular object | |
3858 | later on, we will warn, but we shouldn't. The only | |
3859 | fix is to keep track of what warnings we are supposed | |
3860 | to emit, and then handle them all at the end of the | |
3861 | link. */ | |
3862 | if (dynamic) | |
4ad4eba5 | 3863 | { |
db6a5d5f AM |
3864 | struct elf_link_hash_entry *h; |
3865 | ||
3866 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3867 | ||
3868 | /* FIXME: What about bfd_link_hash_common? */ | |
3869 | if (h != NULL | |
3870 | && (h->root.type == bfd_link_hash_defined | |
3871 | || h->root.type == bfd_link_hash_defweak)) | |
3872 | continue; | |
3873 | } | |
4ad4eba5 | 3874 | |
db6a5d5f AM |
3875 | sz = s->size; |
3876 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3877 | if (msg == NULL) | |
3878 | goto error_return; | |
4ad4eba5 | 3879 | |
db6a5d5f AM |
3880 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3881 | goto error_return; | |
4ad4eba5 | 3882 | |
db6a5d5f | 3883 | msg[sz] = '\0'; |
4ad4eba5 | 3884 | |
db6a5d5f AM |
3885 | if (! (_bfd_generic_link_add_one_symbol |
3886 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3887 | FALSE, bed->collect, NULL))) | |
3888 | goto error_return; | |
4ad4eba5 | 3889 | |
0e1862bb | 3890 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3891 | { |
3892 | /* Clobber the section size so that the warning does | |
3893 | not get copied into the output file. */ | |
3894 | s->size = 0; | |
11d2f718 | 3895 | |
db6a5d5f AM |
3896 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3897 | the warning section don't get copied to the output. */ | |
3898 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3899 | } |
3900 | } | |
3901 | } | |
3902 | ||
29a9f53e L |
3903 | just_syms = ((s = abfd->sections) != NULL |
3904 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
3905 | ||
4ad4eba5 AM |
3906 | add_needed = TRUE; |
3907 | if (! dynamic) | |
3908 | { | |
3909 | /* If we are creating a shared library, create all the dynamic | |
3910 | sections immediately. We need to attach them to something, | |
3911 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
3912 | format and is not from ld --just-symbols. Always create the |
3913 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
3914 | are no input BFD's of the same format as the output, we can't |
3915 | make a shared library. */ | |
3916 | if (!just_syms | |
bf89386a | 3917 | && (bfd_link_pic (info) |
9c1d7a08 | 3918 | || (!bfd_link_relocatable (info) |
3c5fce9b | 3919 | && info->nointerp |
9c1d7a08 | 3920 | && (info->export_dynamic || info->dynamic))) |
66eb6687 | 3921 | && is_elf_hash_table (htab) |
f13a99db | 3922 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 3923 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
3924 | { |
3925 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3926 | goto error_return; | |
3927 | } | |
3928 | } | |
66eb6687 | 3929 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
3930 | goto error_return; |
3931 | else | |
3932 | { | |
4ad4eba5 | 3933 | const char *soname = NULL; |
7ee314fa | 3934 | char *audit = NULL; |
4ad4eba5 | 3935 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
9acc85a6 | 3936 | const Elf_Internal_Phdr *phdr; |
4ad4eba5 AM |
3937 | int ret; |
3938 | ||
3939 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 3940 | ld shouldn't allow it. */ |
29a9f53e | 3941 | if (just_syms) |
92fd189d | 3942 | abort (); |
4ad4eba5 AM |
3943 | |
3944 | /* If this dynamic lib was specified on the command line with | |
3945 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3946 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3947 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3948 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3949 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3950 | all. */ | |
3951 | add_needed = (elf_dyn_lib_class (abfd) | |
3952 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3953 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3954 | |
3955 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3956 | if (s != NULL) | |
3957 | { | |
3958 | bfd_byte *dynbuf; | |
3959 | bfd_byte *extdyn; | |
cb33740c | 3960 | unsigned int elfsec; |
4ad4eba5 AM |
3961 | unsigned long shlink; |
3962 | ||
eea6121a | 3963 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
3964 | { |
3965 | error_free_dyn: | |
3966 | free (dynbuf); | |
3967 | goto error_return; | |
3968 | } | |
4ad4eba5 AM |
3969 | |
3970 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 3971 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
3972 | goto error_free_dyn; |
3973 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3974 | ||
3975 | for (extdyn = dynbuf; | |
eea6121a | 3976 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3977 | extdyn += bed->s->sizeof_dyn) |
3978 | { | |
3979 | Elf_Internal_Dyn dyn; | |
3980 | ||
3981 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3982 | if (dyn.d_tag == DT_SONAME) | |
3983 | { | |
3984 | unsigned int tagv = dyn.d_un.d_val; | |
3985 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3986 | if (soname == NULL) | |
3987 | goto error_free_dyn; | |
3988 | } | |
3989 | if (dyn.d_tag == DT_NEEDED) | |
3990 | { | |
3991 | struct bfd_link_needed_list *n, **pn; | |
3992 | char *fnm, *anm; | |
3993 | unsigned int tagv = dyn.d_un.d_val; | |
3994 | ||
3995 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 3996 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
3997 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
3998 | if (n == NULL || fnm == NULL) | |
3999 | goto error_free_dyn; | |
4000 | amt = strlen (fnm) + 1; | |
a50b1753 | 4001 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4002 | if (anm == NULL) |
4003 | goto error_free_dyn; | |
4004 | memcpy (anm, fnm, amt); | |
4005 | n->name = anm; | |
4006 | n->by = abfd; | |
4007 | n->next = NULL; | |
66eb6687 | 4008 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4009 | ; |
4010 | *pn = n; | |
4011 | } | |
4012 | if (dyn.d_tag == DT_RUNPATH) | |
4013 | { | |
4014 | struct bfd_link_needed_list *n, **pn; | |
4015 | char *fnm, *anm; | |
4016 | unsigned int tagv = dyn.d_un.d_val; | |
4017 | ||
4018 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4019 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4020 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4021 | if (n == NULL || fnm == NULL) | |
4022 | goto error_free_dyn; | |
4023 | amt = strlen (fnm) + 1; | |
a50b1753 | 4024 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4025 | if (anm == NULL) |
4026 | goto error_free_dyn; | |
4027 | memcpy (anm, fnm, amt); | |
4028 | n->name = anm; | |
4029 | n->by = abfd; | |
4030 | n->next = NULL; | |
4031 | for (pn = & runpath; | |
4032 | *pn != NULL; | |
4033 | pn = &(*pn)->next) | |
4034 | ; | |
4035 | *pn = n; | |
4036 | } | |
4037 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
4038 | if (!runpath && dyn.d_tag == DT_RPATH) | |
4039 | { | |
4040 | struct bfd_link_needed_list *n, **pn; | |
4041 | char *fnm, *anm; | |
4042 | unsigned int tagv = dyn.d_un.d_val; | |
4043 | ||
4044 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4045 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4046 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4047 | if (n == NULL || fnm == NULL) | |
4048 | goto error_free_dyn; | |
4049 | amt = strlen (fnm) + 1; | |
a50b1753 | 4050 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 4051 | if (anm == NULL) |
f8703194 | 4052 | goto error_free_dyn; |
4ad4eba5 AM |
4053 | memcpy (anm, fnm, amt); |
4054 | n->name = anm; | |
4055 | n->by = abfd; | |
4056 | n->next = NULL; | |
4057 | for (pn = & rpath; | |
4058 | *pn != NULL; | |
4059 | pn = &(*pn)->next) | |
4060 | ; | |
4061 | *pn = n; | |
4062 | } | |
7ee314fa AM |
4063 | if (dyn.d_tag == DT_AUDIT) |
4064 | { | |
4065 | unsigned int tagv = dyn.d_un.d_val; | |
4066 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4067 | } | |
4ad4eba5 AM |
4068 | } |
4069 | ||
4070 | free (dynbuf); | |
4071 | } | |
4072 | ||
4073 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
4074 | frees all more recently bfd_alloc'd blocks as well. */ | |
4075 | if (runpath) | |
4076 | rpath = runpath; | |
4077 | ||
4078 | if (rpath) | |
4079 | { | |
4080 | struct bfd_link_needed_list **pn; | |
66eb6687 | 4081 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4082 | ; |
4083 | *pn = rpath; | |
4084 | } | |
4085 | ||
9acc85a6 AM |
4086 | /* If we have a PT_GNU_RELRO program header, mark as read-only |
4087 | all sections contained fully therein. This makes relro | |
4088 | shared library sections appear as they will at run-time. */ | |
4089 | phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; | |
4090 | while (--phdr >= elf_tdata (abfd)->phdr) | |
4091 | if (phdr->p_type == PT_GNU_RELRO) | |
4092 | { | |
4093 | for (s = abfd->sections; s != NULL; s = s->next) | |
4094 | if ((s->flags & SEC_ALLOC) != 0 | |
4095 | && s->vma >= phdr->p_vaddr | |
4096 | && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) | |
4097 | s->flags |= SEC_READONLY; | |
4098 | break; | |
4099 | } | |
4100 | ||
4ad4eba5 AM |
4101 | /* We do not want to include any of the sections in a dynamic |
4102 | object in the output file. We hack by simply clobbering the | |
4103 | list of sections in the BFD. This could be handled more | |
4104 | cleanly by, say, a new section flag; the existing | |
4105 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4106 | still implies that the section takes up space in the output | |
4107 | file. */ | |
4108 | bfd_section_list_clear (abfd); | |
4109 | ||
4ad4eba5 AM |
4110 | /* Find the name to use in a DT_NEEDED entry that refers to this |
4111 | object. If the object has a DT_SONAME entry, we use it. | |
4112 | Otherwise, if the generic linker stuck something in | |
4113 | elf_dt_name, we use that. Otherwise, we just use the file | |
4114 | name. */ | |
4115 | if (soname == NULL || *soname == '\0') | |
4116 | { | |
4117 | soname = elf_dt_name (abfd); | |
4118 | if (soname == NULL || *soname == '\0') | |
4119 | soname = bfd_get_filename (abfd); | |
4120 | } | |
4121 | ||
4122 | /* Save the SONAME because sometimes the linker emulation code | |
4123 | will need to know it. */ | |
4124 | elf_dt_name (abfd) = soname; | |
4125 | ||
7e9f0867 | 4126 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4127 | if (ret < 0) |
4128 | goto error_return; | |
4129 | ||
4130 | /* If we have already included this dynamic object in the | |
4131 | link, just ignore it. There is no reason to include a | |
4132 | particular dynamic object more than once. */ | |
4133 | if (ret > 0) | |
4134 | return TRUE; | |
7ee314fa AM |
4135 | |
4136 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 4137 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
4138 | } |
4139 | ||
4140 | /* If this is a dynamic object, we always link against the .dynsym | |
4141 | symbol table, not the .symtab symbol table. The dynamic linker | |
4142 | will only see the .dynsym symbol table, so there is no reason to | |
4143 | look at .symtab for a dynamic object. */ | |
4144 | ||
4145 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
4146 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4147 | else | |
4148 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4149 | ||
4150 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
4151 | ||
4152 | /* The sh_info field of the symtab header tells us where the | |
4153 | external symbols start. We don't care about the local symbols at | |
4154 | this point. */ | |
4155 | if (elf_bad_symtab (abfd)) | |
4156 | { | |
4157 | extsymcount = symcount; | |
4158 | extsymoff = 0; | |
4159 | } | |
4160 | else | |
4161 | { | |
4162 | extsymcount = symcount - hdr->sh_info; | |
4163 | extsymoff = hdr->sh_info; | |
4164 | } | |
4165 | ||
f45794cb | 4166 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4167 | if (extsymcount != 0) |
4ad4eba5 AM |
4168 | { |
4169 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4170 | NULL, NULL, NULL); | |
4171 | if (isymbuf == NULL) | |
4172 | goto error_return; | |
4173 | ||
4ad4eba5 | 4174 | if (sym_hash == NULL) |
012b2306 AM |
4175 | { |
4176 | /* We store a pointer to the hash table entry for each | |
4177 | external symbol. */ | |
ef53be89 AM |
4178 | amt = extsymcount; |
4179 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4180 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4181 | if (sym_hash == NULL) | |
4182 | goto error_free_sym; | |
4183 | elf_sym_hashes (abfd) = sym_hash; | |
4184 | } | |
4ad4eba5 AM |
4185 | } |
4186 | ||
4187 | if (dynamic) | |
4188 | { | |
4189 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4190 | if (!_bfd_elf_slurp_version_tables (abfd, |
4191 | info->default_imported_symver)) | |
4ad4eba5 AM |
4192 | goto error_free_sym; |
4193 | ||
4194 | /* Read in the symbol versions, but don't bother to convert them | |
4195 | to internal format. */ | |
4196 | if (elf_dynversym (abfd) != 0) | |
4197 | { | |
4198 | Elf_Internal_Shdr *versymhdr; | |
4199 | ||
4200 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 4201 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
4202 | if (extversym == NULL) |
4203 | goto error_free_sym; | |
4204 | amt = versymhdr->sh_size; | |
4205 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
4206 | || bfd_bread (extversym, amt, abfd) != amt) | |
4207 | goto error_free_vers; | |
4208 | } | |
4209 | } | |
4210 | ||
66eb6687 AM |
4211 | /* If we are loading an as-needed shared lib, save the symbol table |
4212 | state before we start adding symbols. If the lib turns out | |
4213 | to be unneeded, restore the state. */ | |
4214 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4215 | { | |
4216 | unsigned int i; | |
4217 | size_t entsize; | |
4218 | ||
4219 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4220 | { | |
4221 | struct bfd_hash_entry *p; | |
2de92251 | 4222 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4223 | |
4224 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4225 | { |
4226 | h = (struct elf_link_hash_entry *) p; | |
4227 | entsize += htab->root.table.entsize; | |
4228 | if (h->root.type == bfd_link_hash_warning) | |
4229 | entsize += htab->root.table.entsize; | |
4230 | } | |
66eb6687 AM |
4231 | } |
4232 | ||
4233 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4234 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4235 | if (old_tab == NULL) |
4236 | goto error_free_vers; | |
4237 | ||
4238 | /* Remember the current objalloc pointer, so that all mem for | |
4239 | symbols added can later be reclaimed. */ | |
4240 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4241 | if (alloc_mark == NULL) | |
4242 | goto error_free_vers; | |
4243 | ||
5061a885 AM |
4244 | /* Make a special call to the linker "notice" function to |
4245 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4246 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4247 | goto error_free_vers; |
5061a885 | 4248 | |
f45794cb AM |
4249 | /* Clone the symbol table. Remember some pointers into the |
4250 | symbol table, and dynamic symbol count. */ | |
4251 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4252 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4253 | old_undefs = htab->root.undefs; |
4254 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4255 | old_table = htab->root.table.table; |
4256 | old_size = htab->root.table.size; | |
4257 | old_count = htab->root.table.count; | |
5b677558 AM |
4258 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4259 | if (old_strtab == NULL) | |
4260 | goto error_free_vers; | |
66eb6687 AM |
4261 | |
4262 | for (i = 0; i < htab->root.table.size; i++) | |
4263 | { | |
4264 | struct bfd_hash_entry *p; | |
2de92251 | 4265 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4266 | |
4267 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4268 | { | |
4269 | memcpy (old_ent, p, htab->root.table.entsize); | |
4270 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4271 | h = (struct elf_link_hash_entry *) p; |
4272 | if (h->root.type == bfd_link_hash_warning) | |
4273 | { | |
4274 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4275 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4276 | } | |
66eb6687 AM |
4277 | } |
4278 | } | |
4279 | } | |
4ad4eba5 | 4280 | |
66eb6687 | 4281 | weaks = NULL; |
4ad4eba5 AM |
4282 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
4283 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
4284 | isym < isymend; | |
4285 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4286 | { | |
4287 | int bind; | |
4288 | bfd_vma value; | |
af44c138 | 4289 | asection *sec, *new_sec; |
4ad4eba5 AM |
4290 | flagword flags; |
4291 | const char *name; | |
4292 | struct elf_link_hash_entry *h; | |
90c984fc | 4293 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4294 | bfd_boolean definition; |
4295 | bfd_boolean size_change_ok; | |
4296 | bfd_boolean type_change_ok; | |
37a9e49a L |
4297 | bfd_boolean new_weak; |
4298 | bfd_boolean old_weak; | |
4ad4eba5 | 4299 | bfd_boolean override; |
a4d8e49b | 4300 | bfd_boolean common; |
97196564 | 4301 | bfd_boolean discarded; |
4ad4eba5 AM |
4302 | unsigned int old_alignment; |
4303 | bfd *old_bfd; | |
6e33951e | 4304 | bfd_boolean matched; |
4ad4eba5 AM |
4305 | |
4306 | override = FALSE; | |
4307 | ||
4308 | flags = BSF_NO_FLAGS; | |
4309 | sec = NULL; | |
4310 | value = isym->st_value; | |
a4d8e49b | 4311 | common = bed->common_definition (isym); |
2980ccad L |
4312 | if (common && info->inhibit_common_definition) |
4313 | { | |
4314 | /* Treat common symbol as undefined for --no-define-common. */ | |
4315 | isym->st_shndx = SHN_UNDEF; | |
4316 | common = FALSE; | |
4317 | } | |
97196564 | 4318 | discarded = FALSE; |
4ad4eba5 AM |
4319 | |
4320 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4321 | switch (bind) |
4ad4eba5 | 4322 | { |
3e7a7d11 | 4323 | case STB_LOCAL: |
4ad4eba5 AM |
4324 | /* This should be impossible, since ELF requires that all |
4325 | global symbols follow all local symbols, and that sh_info | |
4326 | point to the first global symbol. Unfortunately, Irix 5 | |
4327 | screws this up. */ | |
4328 | continue; | |
3e7a7d11 NC |
4329 | |
4330 | case STB_GLOBAL: | |
a4d8e49b | 4331 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4332 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4333 | break; |
4334 | ||
4335 | case STB_WEAK: | |
4336 | flags = BSF_WEAK; | |
4337 | break; | |
4338 | ||
4339 | case STB_GNU_UNIQUE: | |
4340 | flags = BSF_GNU_UNIQUE; | |
4341 | break; | |
4342 | ||
4343 | default: | |
4ad4eba5 | 4344 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4345 | break; |
4ad4eba5 AM |
4346 | } |
4347 | ||
4348 | if (isym->st_shndx == SHN_UNDEF) | |
4349 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4350 | else if (isym->st_shndx == SHN_ABS) |
4351 | sec = bfd_abs_section_ptr; | |
4352 | else if (isym->st_shndx == SHN_COMMON) | |
4353 | { | |
4354 | sec = bfd_com_section_ptr; | |
4355 | /* What ELF calls the size we call the value. What ELF | |
4356 | calls the value we call the alignment. */ | |
4357 | value = isym->st_size; | |
4358 | } | |
4359 | else | |
4ad4eba5 AM |
4360 | { |
4361 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4362 | if (sec == NULL) | |
4363 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4364 | else if (discarded_section (sec)) |
529fcb95 | 4365 | { |
e5d08002 L |
4366 | /* Symbols from discarded section are undefined. We keep |
4367 | its visibility. */ | |
529fcb95 | 4368 | sec = bfd_und_section_ptr; |
97196564 | 4369 | discarded = TRUE; |
529fcb95 PB |
4370 | isym->st_shndx = SHN_UNDEF; |
4371 | } | |
4ad4eba5 AM |
4372 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4373 | value -= sec->vma; | |
4374 | } | |
4ad4eba5 AM |
4375 | |
4376 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4377 | isym->st_name); | |
4378 | if (name == NULL) | |
4379 | goto error_free_vers; | |
4380 | ||
4381 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4382 | && (abfd->flags & BFD_PLUGIN) != 0) |
4383 | { | |
4384 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4385 | ||
4386 | if (xc == NULL) | |
4387 | { | |
4388 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4389 | | SEC_EXCLUDE); | |
4390 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4391 | if (xc == NULL) | |
4392 | goto error_free_vers; | |
4393 | } | |
4394 | sec = xc; | |
4395 | } | |
4396 | else if (isym->st_shndx == SHN_COMMON | |
4397 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4398 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4399 | { |
4400 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4401 | ||
4402 | if (tcomm == NULL) | |
4403 | { | |
02d00247 AM |
4404 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4405 | | SEC_LINKER_CREATED); | |
4406 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4407 | if (tcomm == NULL) |
4ad4eba5 AM |
4408 | goto error_free_vers; |
4409 | } | |
4410 | sec = tcomm; | |
4411 | } | |
66eb6687 | 4412 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4413 | { |
66eb6687 AM |
4414 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4415 | &sec, &value)) | |
4ad4eba5 AM |
4416 | goto error_free_vers; |
4417 | ||
4418 | /* The hook function sets the name to NULL if this symbol | |
4419 | should be skipped for some reason. */ | |
4420 | if (name == NULL) | |
4421 | continue; | |
4422 | } | |
4423 | ||
4424 | /* Sanity check that all possibilities were handled. */ | |
4425 | if (sec == NULL) | |
4426 | { | |
4427 | bfd_set_error (bfd_error_bad_value); | |
4428 | goto error_free_vers; | |
4429 | } | |
4430 | ||
191c0c42 AM |
4431 | /* Silently discard TLS symbols from --just-syms. There's |
4432 | no way to combine a static TLS block with a new TLS block | |
4433 | for this executable. */ | |
4434 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4435 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4436 | continue; | |
4437 | ||
4ad4eba5 AM |
4438 | if (bfd_is_und_section (sec) |
4439 | || bfd_is_com_section (sec)) | |
4440 | definition = FALSE; | |
4441 | else | |
4442 | definition = TRUE; | |
4443 | ||
4444 | size_change_ok = FALSE; | |
66eb6687 | 4445 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4446 | old_weak = FALSE; |
6e33951e | 4447 | matched = FALSE; |
4ad4eba5 AM |
4448 | old_alignment = 0; |
4449 | old_bfd = NULL; | |
af44c138 | 4450 | new_sec = sec; |
4ad4eba5 | 4451 | |
66eb6687 | 4452 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4453 | { |
4454 | Elf_Internal_Versym iver; | |
4455 | unsigned int vernum = 0; | |
4456 | bfd_boolean skip; | |
4457 | ||
fc0e6df6 | 4458 | if (ever == NULL) |
4ad4eba5 | 4459 | { |
fc0e6df6 PB |
4460 | if (info->default_imported_symver) |
4461 | /* Use the default symbol version created earlier. */ | |
4462 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4463 | else | |
4464 | iver.vs_vers = 0; | |
4465 | } | |
4466 | else | |
4467 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4468 | ||
4469 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4470 | ||
4471 | /* If this is a hidden symbol, or if it is not version | |
4472 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4473 | However, we do not modify a non-hidden absolute symbol |
4474 | if it is not a function, because it might be the version | |
4475 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4476 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4477 | || (vernum > 1 |
4478 | && (!bfd_is_abs_section (sec) | |
4479 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4480 | { |
4481 | const char *verstr; | |
4482 | size_t namelen, verlen, newlen; | |
4483 | char *newname, *p; | |
4484 | ||
4485 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4486 | { |
fc0e6df6 PB |
4487 | if (vernum > elf_tdata (abfd)->cverdefs) |
4488 | verstr = NULL; | |
4489 | else if (vernum > 1) | |
4490 | verstr = | |
4491 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4492 | else | |
4493 | verstr = ""; | |
4ad4eba5 | 4494 | |
fc0e6df6 | 4495 | if (verstr == NULL) |
4ad4eba5 | 4496 | { |
4eca0228 | 4497 | _bfd_error_handler |
695344c0 | 4498 | /* xgettext:c-format */ |
871b3ab2 | 4499 | (_("%pB: %s: invalid version %u (max %d)"), |
fc0e6df6 PB |
4500 | abfd, name, vernum, |
4501 | elf_tdata (abfd)->cverdefs); | |
4502 | bfd_set_error (bfd_error_bad_value); | |
4503 | goto error_free_vers; | |
4ad4eba5 | 4504 | } |
fc0e6df6 PB |
4505 | } |
4506 | else | |
4507 | { | |
4508 | /* We cannot simply test for the number of | |
4509 | entries in the VERNEED section since the | |
4510 | numbers for the needed versions do not start | |
4511 | at 0. */ | |
4512 | Elf_Internal_Verneed *t; | |
4513 | ||
4514 | verstr = NULL; | |
4515 | for (t = elf_tdata (abfd)->verref; | |
4516 | t != NULL; | |
4517 | t = t->vn_nextref) | |
4ad4eba5 | 4518 | { |
fc0e6df6 | 4519 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4520 | |
fc0e6df6 PB |
4521 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4522 | { | |
4523 | if (a->vna_other == vernum) | |
4ad4eba5 | 4524 | { |
fc0e6df6 PB |
4525 | verstr = a->vna_nodename; |
4526 | break; | |
4ad4eba5 | 4527 | } |
4ad4eba5 | 4528 | } |
fc0e6df6 PB |
4529 | if (a != NULL) |
4530 | break; | |
4531 | } | |
4532 | if (verstr == NULL) | |
4533 | { | |
4eca0228 | 4534 | _bfd_error_handler |
695344c0 | 4535 | /* xgettext:c-format */ |
871b3ab2 | 4536 | (_("%pB: %s: invalid needed version %d"), |
fc0e6df6 PB |
4537 | abfd, name, vernum); |
4538 | bfd_set_error (bfd_error_bad_value); | |
4539 | goto error_free_vers; | |
4ad4eba5 | 4540 | } |
4ad4eba5 | 4541 | } |
fc0e6df6 PB |
4542 | |
4543 | namelen = strlen (name); | |
4544 | verlen = strlen (verstr); | |
4545 | newlen = namelen + verlen + 2; | |
4546 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4547 | && isym->st_shndx != SHN_UNDEF) | |
4548 | ++newlen; | |
4549 | ||
a50b1753 | 4550 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4551 | if (newname == NULL) |
4552 | goto error_free_vers; | |
4553 | memcpy (newname, name, namelen); | |
4554 | p = newname + namelen; | |
4555 | *p++ = ELF_VER_CHR; | |
4556 | /* If this is a defined non-hidden version symbol, | |
4557 | we add another @ to the name. This indicates the | |
4558 | default version of the symbol. */ | |
4559 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4560 | && isym->st_shndx != SHN_UNDEF) | |
4561 | *p++ = ELF_VER_CHR; | |
4562 | memcpy (p, verstr, verlen + 1); | |
4563 | ||
4564 | name = newname; | |
4ad4eba5 AM |
4565 | } |
4566 | ||
cd3416da AM |
4567 | /* If this symbol has default visibility and the user has |
4568 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4569 | if (!bfd_is_und_section (sec) |
cd3416da | 4570 | && !dynamic |
ce875075 | 4571 | && abfd->no_export |
cd3416da AM |
4572 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4573 | isym->st_other = (STV_HIDDEN | |
4574 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4575 | ||
4f3fedcf AM |
4576 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4577 | sym_hash, &old_bfd, &old_weak, | |
4578 | &old_alignment, &skip, &override, | |
6e33951e L |
4579 | &type_change_ok, &size_change_ok, |
4580 | &matched)) | |
4ad4eba5 AM |
4581 | goto error_free_vers; |
4582 | ||
4583 | if (skip) | |
4584 | continue; | |
4585 | ||
6e33951e L |
4586 | /* Override a definition only if the new symbol matches the |
4587 | existing one. */ | |
4588 | if (override && matched) | |
4ad4eba5 AM |
4589 | definition = FALSE; |
4590 | ||
4591 | h = *sym_hash; | |
4592 | while (h->root.type == bfd_link_hash_indirect | |
4593 | || h->root.type == bfd_link_hash_warning) | |
4594 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4595 | ||
4ad4eba5 | 4596 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4597 | && vernum > 1 |
4598 | && definition) | |
4599 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4600 | } | |
4601 | ||
4602 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4603 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4604 | (struct bfd_link_hash_entry **) sym_hash))) |
4605 | goto error_free_vers; | |
4606 | ||
a43942db MR |
4607 | if ((flags & BSF_GNU_UNIQUE) |
4608 | && (abfd->flags & DYNAMIC) == 0 | |
4609 | && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) | |
4610 | elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; | |
4611 | ||
4ad4eba5 | 4612 | h = *sym_hash; |
90c984fc L |
4613 | /* We need to make sure that indirect symbol dynamic flags are |
4614 | updated. */ | |
4615 | hi = h; | |
4ad4eba5 AM |
4616 | while (h->root.type == bfd_link_hash_indirect |
4617 | || h->root.type == bfd_link_hash_warning) | |
4618 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4619 | |
97196564 L |
4620 | /* Setting the index to -3 tells elf_link_output_extsym that |
4621 | this symbol is defined in a discarded section. */ | |
4622 | if (discarded) | |
4623 | h->indx = -3; | |
4624 | ||
4ad4eba5 AM |
4625 | *sym_hash = h; |
4626 | ||
37a9e49a | 4627 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4628 | if (dynamic |
4629 | && definition | |
37a9e49a | 4630 | && new_weak |
fcb93ecf | 4631 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4632 | && is_elf_hash_table (htab) |
60d67dc8 | 4633 | && h->u.alias == NULL) |
4ad4eba5 AM |
4634 | { |
4635 | /* Keep a list of all weak defined non function symbols from | |
60d67dc8 AM |
4636 | a dynamic object, using the alias field. Later in this |
4637 | function we will set the alias field to the correct | |
4ad4eba5 AM |
4638 | value. We only put non-function symbols from dynamic |
4639 | objects on this list, because that happens to be the only | |
4640 | time we need to know the normal symbol corresponding to a | |
4641 | weak symbol, and the information is time consuming to | |
60d67dc8 | 4642 | figure out. If the alias field is not already NULL, |
4ad4eba5 AM |
4643 | then this symbol was already defined by some previous |
4644 | dynamic object, and we will be using that previous | |
4645 | definition anyhow. */ | |
4646 | ||
60d67dc8 | 4647 | h->u.alias = weaks; |
4ad4eba5 | 4648 | weaks = h; |
4ad4eba5 AM |
4649 | } |
4650 | ||
4651 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4652 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4653 | && h->root.type == bfd_link_hash_common) |
4654 | { | |
4655 | unsigned int align; | |
4656 | ||
a4d8e49b | 4657 | if (common) |
af44c138 L |
4658 | align = bfd_log2 (isym->st_value); |
4659 | else | |
4660 | { | |
4661 | /* The new symbol is a common symbol in a shared object. | |
4662 | We need to get the alignment from the section. */ | |
4663 | align = new_sec->alignment_power; | |
4664 | } | |
595213d4 | 4665 | if (align > old_alignment) |
4ad4eba5 AM |
4666 | h->root.u.c.p->alignment_power = align; |
4667 | else | |
4668 | h->root.u.c.p->alignment_power = old_alignment; | |
4669 | } | |
4670 | ||
66eb6687 | 4671 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4672 | { |
4f3fedcf AM |
4673 | /* Set a flag in the hash table entry indicating the type of |
4674 | reference or definition we just found. A dynamic symbol | |
4675 | is one which is referenced or defined by both a regular | |
4676 | object and a shared object. */ | |
4677 | bfd_boolean dynsym = FALSE; | |
4678 | ||
4679 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4680 | ref_regular for them, or make them dynamic. */ | |
4681 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4682 | ; | |
4683 | else if (! dynamic) | |
4684 | { | |
4685 | if (! definition) | |
4686 | { | |
4687 | h->ref_regular = 1; | |
4688 | if (bind != STB_WEAK) | |
4689 | h->ref_regular_nonweak = 1; | |
4690 | } | |
4691 | else | |
4692 | { | |
4693 | h->def_regular = 1; | |
4694 | if (h->def_dynamic) | |
4695 | { | |
4696 | h->def_dynamic = 0; | |
4697 | h->ref_dynamic = 1; | |
4698 | } | |
4699 | } | |
4700 | ||
4701 | /* If the indirect symbol has been forced local, don't | |
4702 | make the real symbol dynamic. */ | |
4703 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4704 | && (bfd_link_dll (info) |
4f3fedcf AM |
4705 | || h->def_dynamic |
4706 | || h->ref_dynamic)) | |
4707 | dynsym = TRUE; | |
4708 | } | |
4709 | else | |
4710 | { | |
4711 | if (! definition) | |
4712 | { | |
4713 | h->ref_dynamic = 1; | |
4714 | hi->ref_dynamic = 1; | |
4715 | } | |
4716 | else | |
4717 | { | |
4718 | h->def_dynamic = 1; | |
4719 | hi->def_dynamic = 1; | |
4720 | } | |
4721 | ||
4722 | /* If the indirect symbol has been forced local, don't | |
4723 | make the real symbol dynamic. */ | |
4724 | if ((h == hi || !hi->forced_local) | |
4725 | && (h->def_regular | |
4726 | || h->ref_regular | |
60d67dc8 AM |
4727 | || (h->is_weakalias |
4728 | && weakdef (h)->dynindx != -1))) | |
4f3fedcf AM |
4729 | dynsym = TRUE; |
4730 | } | |
4731 | ||
4732 | /* Check to see if we need to add an indirect symbol for | |
4733 | the default name. */ | |
4734 | if (definition | |
4735 | || (!override && h->root.type == bfd_link_hash_common)) | |
4736 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4737 | sec, value, &old_bfd, &dynsym)) | |
4738 | goto error_free_vers; | |
4ad4eba5 AM |
4739 | |
4740 | /* Check the alignment when a common symbol is involved. This | |
4741 | can change when a common symbol is overridden by a normal | |
4742 | definition or a common symbol is ignored due to the old | |
4743 | normal definition. We need to make sure the maximum | |
4744 | alignment is maintained. */ | |
a4d8e49b | 4745 | if ((old_alignment || common) |
4ad4eba5 AM |
4746 | && h->root.type != bfd_link_hash_common) |
4747 | { | |
4748 | unsigned int common_align; | |
4749 | unsigned int normal_align; | |
4750 | unsigned int symbol_align; | |
4751 | bfd *normal_bfd; | |
4752 | bfd *common_bfd; | |
4753 | ||
3a81e825 AM |
4754 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4755 | || h->root.type == bfd_link_hash_defweak); | |
4756 | ||
4ad4eba5 AM |
4757 | symbol_align = ffs (h->root.u.def.value) - 1; |
4758 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4759 | && (h->root.u.def.section->owner->flags |
4760 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4761 | { |
4762 | normal_align = h->root.u.def.section->alignment_power; | |
4763 | if (normal_align > symbol_align) | |
4764 | normal_align = symbol_align; | |
4765 | } | |
4766 | else | |
4767 | normal_align = symbol_align; | |
4768 | ||
4769 | if (old_alignment) | |
4770 | { | |
4771 | common_align = old_alignment; | |
4772 | common_bfd = old_bfd; | |
4773 | normal_bfd = abfd; | |
4774 | } | |
4775 | else | |
4776 | { | |
4777 | common_align = bfd_log2 (isym->st_value); | |
4778 | common_bfd = abfd; | |
4779 | normal_bfd = old_bfd; | |
4780 | } | |
4781 | ||
4782 | if (normal_align < common_align) | |
d07676f8 NC |
4783 | { |
4784 | /* PR binutils/2735 */ | |
4785 | if (normal_bfd == NULL) | |
4eca0228 | 4786 | _bfd_error_handler |
695344c0 | 4787 | /* xgettext:c-format */ |
9793eb77 | 4788 | (_("warning: alignment %u of common symbol `%s' in %pB is" |
871b3ab2 | 4789 | " greater than the alignment (%u) of its section %pA"), |
c08bb8dd AM |
4790 | 1 << common_align, name, common_bfd, |
4791 | 1 << normal_align, h->root.u.def.section); | |
d07676f8 | 4792 | else |
4eca0228 | 4793 | _bfd_error_handler |
695344c0 | 4794 | /* xgettext:c-format */ |
9793eb77 | 4795 | (_("warning: alignment %u of symbol `%s' in %pB" |
871b3ab2 | 4796 | " is smaller than %u in %pB"), |
c08bb8dd AM |
4797 | 1 << normal_align, name, normal_bfd, |
4798 | 1 << common_align, common_bfd); | |
d07676f8 | 4799 | } |
4ad4eba5 AM |
4800 | } |
4801 | ||
83ad0046 | 4802 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4803 | if (isym->st_size != 0 |
4804 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4805 | && (definition || h->size == 0)) |
4806 | { | |
83ad0046 L |
4807 | if (h->size != 0 |
4808 | && h->size != isym->st_size | |
4809 | && ! size_change_ok) | |
4eca0228 | 4810 | _bfd_error_handler |
695344c0 | 4811 | /* xgettext:c-format */ |
9793eb77 | 4812 | (_("warning: size of symbol `%s' changed" |
2dcf00ce AM |
4813 | " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"), |
4814 | name, (uint64_t) h->size, old_bfd, | |
4815 | (uint64_t) isym->st_size, abfd); | |
4ad4eba5 AM |
4816 | |
4817 | h->size = isym->st_size; | |
4818 | } | |
4819 | ||
4820 | /* If this is a common symbol, then we always want H->SIZE | |
4821 | to be the size of the common symbol. The code just above | |
4822 | won't fix the size if a common symbol becomes larger. We | |
4823 | don't warn about a size change here, because that is | |
4f3fedcf | 4824 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4825 | function types. */ |
4ad4eba5 AM |
4826 | if (h->root.type == bfd_link_hash_common) |
4827 | h->size = h->root.u.c.size; | |
4828 | ||
4829 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4830 | && ((definition && !new_weak) |
4831 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4832 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4833 | { |
2955ec4c L |
4834 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4835 | ||
4836 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4837 | symbol. */ | |
4838 | if (type == STT_GNU_IFUNC | |
4839 | && (abfd->flags & DYNAMIC) != 0) | |
4840 | type = STT_FUNC; | |
4ad4eba5 | 4841 | |
2955ec4c L |
4842 | if (h->type != type) |
4843 | { | |
4844 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 4845 | /* xgettext:c-format */ |
4eca0228 | 4846 | _bfd_error_handler |
9793eb77 | 4847 | (_("warning: type of symbol `%s' changed" |
871b3ab2 | 4848 | " from %d to %d in %pB"), |
c08bb8dd | 4849 | name, h->type, type, abfd); |
2955ec4c L |
4850 | |
4851 | h->type = type; | |
4852 | } | |
4ad4eba5 AM |
4853 | } |
4854 | ||
54ac0771 | 4855 | /* Merge st_other field. */ |
b8417128 | 4856 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4857 | |
c3df8c14 | 4858 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
4859 | if (definition |
4860 | && (sec->flags & SEC_DEBUGGING) | |
4861 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
4862 | dynsym = FALSE; |
4863 | ||
4f3fedcf AM |
4864 | /* Nor should we make plugin symbols dynamic. */ |
4865 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4866 | dynsym = FALSE; | |
4867 | ||
35fc36a8 | 4868 | if (definition) |
35399224 L |
4869 | { |
4870 | h->target_internal = isym->st_target_internal; | |
4871 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4872 | } | |
35fc36a8 | 4873 | |
4ad4eba5 AM |
4874 | if (definition && !dynamic) |
4875 | { | |
4876 | char *p = strchr (name, ELF_VER_CHR); | |
4877 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4878 | { | |
4879 | /* Queue non-default versions so that .symver x, x@FOO | |
4880 | aliases can be checked. */ | |
66eb6687 | 4881 | if (!nondeflt_vers) |
4ad4eba5 | 4882 | { |
66eb6687 AM |
4883 | amt = ((isymend - isym + 1) |
4884 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4885 | nondeflt_vers |
4886 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4887 | if (!nondeflt_vers) |
4888 | goto error_free_vers; | |
4ad4eba5 | 4889 | } |
66eb6687 | 4890 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4891 | } |
4892 | } | |
4893 | ||
4894 | if (dynsym && h->dynindx == -1) | |
4895 | { | |
c152c796 | 4896 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 4897 | goto error_free_vers; |
60d67dc8 AM |
4898 | if (h->is_weakalias |
4899 | && weakdef (h)->dynindx == -1) | |
4ad4eba5 | 4900 | { |
60d67dc8 | 4901 | if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) |
4ad4eba5 AM |
4902 | goto error_free_vers; |
4903 | } | |
4904 | } | |
1f599d0e | 4905 | else if (h->dynindx != -1) |
4ad4eba5 AM |
4906 | /* If the symbol already has a dynamic index, but |
4907 | visibility says it should not be visible, turn it into | |
4908 | a local symbol. */ | |
4909 | switch (ELF_ST_VISIBILITY (h->other)) | |
4910 | { | |
4911 | case STV_INTERNAL: | |
4912 | case STV_HIDDEN: | |
4913 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
4914 | dynsym = FALSE; | |
4915 | break; | |
4916 | } | |
4917 | ||
aef28989 L |
4918 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
4919 | for unmatched symbol. */ | |
4ad4eba5 | 4920 | if (!add_needed |
aef28989 | 4921 | && matched |
4ad4eba5 | 4922 | && definition |
010e5ae2 | 4923 | && ((dynsym |
ffa9430d | 4924 | && h->ref_regular_nonweak |
4f3fedcf AM |
4925 | && (old_bfd == NULL |
4926 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 4927 | || (h->ref_dynamic_nonweak |
010e5ae2 | 4928 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
4929 | && !on_needed_list (elf_dt_name (abfd), |
4930 | htab->needed, NULL)))) | |
4ad4eba5 AM |
4931 | { |
4932 | int ret; | |
4933 | const char *soname = elf_dt_name (abfd); | |
4934 | ||
16e4ecc0 AM |
4935 | info->callbacks->minfo ("%!", soname, old_bfd, |
4936 | h->root.root.string); | |
4937 | ||
4ad4eba5 AM |
4938 | /* A symbol from a library loaded via DT_NEEDED of some |
4939 | other library is referenced by a regular object. | |
e56f61be | 4940 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
4941 | --no-add-needed is used and the reference was not |
4942 | a weak one. */ | |
4f3fedcf | 4943 | if (old_bfd != NULL |
b918acf9 | 4944 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 4945 | { |
4eca0228 | 4946 | _bfd_error_handler |
695344c0 | 4947 | /* xgettext:c-format */ |
871b3ab2 | 4948 | (_("%pB: undefined reference to symbol '%s'"), |
4f3fedcf | 4949 | old_bfd, name); |
ff5ac77b | 4950 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
4951 | goto error_free_vers; |
4952 | } | |
4953 | ||
a50b1753 | 4954 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 4955 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 4956 | |
4ad4eba5 | 4957 | add_needed = TRUE; |
7e9f0867 | 4958 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4959 | if (ret < 0) |
4960 | goto error_free_vers; | |
4961 | ||
4962 | BFD_ASSERT (ret == 0); | |
4963 | } | |
4964 | } | |
4965 | } | |
4966 | ||
a83ef4d1 L |
4967 | if (info->lto_plugin_active |
4968 | && !bfd_link_relocatable (info) | |
4969 | && (abfd->flags & BFD_PLUGIN) == 0 | |
4970 | && !just_syms | |
4971 | && extsymcount) | |
4972 | { | |
4973 | int r_sym_shift; | |
4974 | ||
4975 | if (bed->s->arch_size == 32) | |
4976 | r_sym_shift = 8; | |
4977 | else | |
4978 | r_sym_shift = 32; | |
4979 | ||
4980 | /* If linker plugin is enabled, set non_ir_ref_regular on symbols | |
4981 | referenced in regular objects so that linker plugin will get | |
4982 | the correct symbol resolution. */ | |
4983 | ||
4984 | sym_hash = elf_sym_hashes (abfd); | |
4985 | for (s = abfd->sections; s != NULL; s = s->next) | |
4986 | { | |
4987 | Elf_Internal_Rela *internal_relocs; | |
4988 | Elf_Internal_Rela *rel, *relend; | |
4989 | ||
4990 | /* Don't check relocations in excluded sections. */ | |
4991 | if ((s->flags & SEC_RELOC) == 0 | |
4992 | || s->reloc_count == 0 | |
4993 | || (s->flags & SEC_EXCLUDE) != 0 | |
4994 | || ((info->strip == strip_all | |
4995 | || info->strip == strip_debugger) | |
4996 | && (s->flags & SEC_DEBUGGING) != 0)) | |
4997 | continue; | |
4998 | ||
4999 | internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL, | |
5000 | NULL, | |
5001 | info->keep_memory); | |
5002 | if (internal_relocs == NULL) | |
5003 | goto error_free_vers; | |
5004 | ||
5005 | rel = internal_relocs; | |
5006 | relend = rel + s->reloc_count; | |
5007 | for ( ; rel < relend; rel++) | |
5008 | { | |
5009 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
5010 | struct elf_link_hash_entry *h; | |
5011 | ||
5012 | /* Skip local symbols. */ | |
5013 | if (r_symndx < extsymoff) | |
5014 | continue; | |
5015 | ||
5016 | h = sym_hash[r_symndx - extsymoff]; | |
5017 | if (h != NULL) | |
5018 | h->root.non_ir_ref_regular = 1; | |
5019 | } | |
5020 | ||
5021 | if (elf_section_data (s)->relocs != internal_relocs) | |
5022 | free (internal_relocs); | |
5023 | } | |
5024 | } | |
5025 | ||
66eb6687 AM |
5026 | if (extversym != NULL) |
5027 | { | |
5028 | free (extversym); | |
5029 | extversym = NULL; | |
5030 | } | |
5031 | ||
5032 | if (isymbuf != NULL) | |
5033 | { | |
5034 | free (isymbuf); | |
5035 | isymbuf = NULL; | |
5036 | } | |
5037 | ||
5038 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
5039 | { | |
5040 | unsigned int i; | |
5041 | ||
5042 | /* Restore the symbol table. */ | |
f45794cb AM |
5043 | old_ent = (char *) old_tab + tabsize; |
5044 | memset (elf_sym_hashes (abfd), 0, | |
5045 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
5046 | htab->root.table.table = old_table; |
5047 | htab->root.table.size = old_size; | |
5048 | htab->root.table.count = old_count; | |
66eb6687 | 5049 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
5050 | htab->root.undefs = old_undefs; |
5051 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
5052 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
5053 | free (old_strtab); | |
5054 | old_strtab = NULL; | |
66eb6687 AM |
5055 | for (i = 0; i < htab->root.table.size; i++) |
5056 | { | |
5057 | struct bfd_hash_entry *p; | |
5058 | struct elf_link_hash_entry *h; | |
3e0882af L |
5059 | bfd_size_type size; |
5060 | unsigned int alignment_power; | |
4070765b | 5061 | unsigned int non_ir_ref_dynamic; |
66eb6687 AM |
5062 | |
5063 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
5064 | { | |
5065 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
5066 | if (h->root.type == bfd_link_hash_warning) |
5067 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 5068 | |
3e0882af L |
5069 | /* Preserve the maximum alignment and size for common |
5070 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 5071 | since it can still be loaded at run time by another |
3e0882af L |
5072 | dynamic lib. */ |
5073 | if (h->root.type == bfd_link_hash_common) | |
5074 | { | |
5075 | size = h->root.u.c.size; | |
5076 | alignment_power = h->root.u.c.p->alignment_power; | |
5077 | } | |
5078 | else | |
5079 | { | |
5080 | size = 0; | |
5081 | alignment_power = 0; | |
5082 | } | |
4070765b | 5083 | /* Preserve non_ir_ref_dynamic so that this symbol |
59fa66c5 L |
5084 | will be exported when the dynamic lib becomes needed |
5085 | in the second pass. */ | |
4070765b | 5086 | non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; |
66eb6687 AM |
5087 | memcpy (p, old_ent, htab->root.table.entsize); |
5088 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
5089 | h = (struct elf_link_hash_entry *) p; |
5090 | if (h->root.type == bfd_link_hash_warning) | |
5091 | { | |
5092 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
5093 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 5094 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 5095 | } |
a4542f1b | 5096 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
5097 | { |
5098 | if (size > h->root.u.c.size) | |
5099 | h->root.u.c.size = size; | |
5100 | if (alignment_power > h->root.u.c.p->alignment_power) | |
5101 | h->root.u.c.p->alignment_power = alignment_power; | |
5102 | } | |
4070765b | 5103 | h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; |
66eb6687 AM |
5104 | } |
5105 | } | |
5106 | ||
5061a885 AM |
5107 | /* Make a special call to the linker "notice" function to |
5108 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 5109 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 5110 | goto error_free_vers; |
5061a885 | 5111 | |
66eb6687 AM |
5112 | free (old_tab); |
5113 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
5114 | alloc_mark); | |
5115 | if (nondeflt_vers != NULL) | |
5116 | free (nondeflt_vers); | |
5117 | return TRUE; | |
5118 | } | |
2de92251 | 5119 | |
66eb6687 AM |
5120 | if (old_tab != NULL) |
5121 | { | |
e5034e59 | 5122 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 5123 | goto error_free_vers; |
66eb6687 AM |
5124 | free (old_tab); |
5125 | old_tab = NULL; | |
5126 | } | |
5127 | ||
c6e8a9a8 L |
5128 | /* Now that all the symbols from this input file are created, if |
5129 | not performing a relocatable link, handle .symver foo, foo@BAR | |
5130 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 5131 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 5132 | { |
ef53be89 | 5133 | size_t cnt, symidx; |
4ad4eba5 AM |
5134 | |
5135 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
5136 | { | |
5137 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
5138 | char *shortname, *p; | |
5139 | ||
5140 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
5141 | if (p == NULL | |
5142 | || (h->root.type != bfd_link_hash_defined | |
5143 | && h->root.type != bfd_link_hash_defweak)) | |
5144 | continue; | |
5145 | ||
5146 | amt = p - h->root.root.string; | |
a50b1753 | 5147 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
5148 | if (!shortname) |
5149 | goto error_free_vers; | |
4ad4eba5 AM |
5150 | memcpy (shortname, h->root.root.string, amt); |
5151 | shortname[amt] = '\0'; | |
5152 | ||
5153 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 5154 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
5155 | FALSE, FALSE, FALSE); |
5156 | if (hi != NULL | |
5157 | && hi->root.type == h->root.type | |
5158 | && hi->root.u.def.value == h->root.u.def.value | |
5159 | && hi->root.u.def.section == h->root.u.def.section) | |
5160 | { | |
5161 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
5162 | hi->root.type = bfd_link_hash_indirect; | |
5163 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 5164 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
5165 | sym_hash = elf_sym_hashes (abfd); |
5166 | if (sym_hash) | |
5167 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
5168 | if (sym_hash[symidx] == hi) | |
5169 | { | |
5170 | sym_hash[symidx] = h; | |
5171 | break; | |
5172 | } | |
5173 | } | |
5174 | free (shortname); | |
5175 | } | |
5176 | free (nondeflt_vers); | |
5177 | nondeflt_vers = NULL; | |
5178 | } | |
5179 | ||
60d67dc8 | 5180 | /* Now set the alias field correctly for all the weak defined |
4ad4eba5 AM |
5181 | symbols we found. The only way to do this is to search all the |
5182 | symbols. Since we only need the information for non functions in | |
5183 | dynamic objects, that's the only time we actually put anything on | |
5184 | the list WEAKS. We need this information so that if a regular | |
5185 | object refers to a symbol defined weakly in a dynamic object, the | |
5186 | real symbol in the dynamic object is also put in the dynamic | |
5187 | symbols; we also must arrange for both symbols to point to the | |
5188 | same memory location. We could handle the general case of symbol | |
5189 | aliasing, but a general symbol alias can only be generated in | |
5190 | assembler code, handling it correctly would be very time | |
5191 | consuming, and other ELF linkers don't handle general aliasing | |
5192 | either. */ | |
5193 | if (weaks != NULL) | |
5194 | { | |
5195 | struct elf_link_hash_entry **hpp; | |
5196 | struct elf_link_hash_entry **hppend; | |
5197 | struct elf_link_hash_entry **sorted_sym_hash; | |
5198 | struct elf_link_hash_entry *h; | |
5199 | size_t sym_count; | |
5200 | ||
5201 | /* Since we have to search the whole symbol list for each weak | |
5202 | defined symbol, search time for N weak defined symbols will be | |
5203 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
5204 | amt = extsymcount; |
5205 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 5206 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
5207 | if (sorted_sym_hash == NULL) |
5208 | goto error_return; | |
5209 | sym_hash = sorted_sym_hash; | |
5210 | hpp = elf_sym_hashes (abfd); | |
5211 | hppend = hpp + extsymcount; | |
5212 | sym_count = 0; | |
5213 | for (; hpp < hppend; hpp++) | |
5214 | { | |
5215 | h = *hpp; | |
5216 | if (h != NULL | |
5217 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 5218 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
5219 | { |
5220 | *sym_hash = h; | |
5221 | sym_hash++; | |
5222 | sym_count++; | |
5223 | } | |
5224 | } | |
5225 | ||
5226 | qsort (sorted_sym_hash, sym_count, | |
5227 | sizeof (struct elf_link_hash_entry *), | |
5228 | elf_sort_symbol); | |
5229 | ||
5230 | while (weaks != NULL) | |
5231 | { | |
5232 | struct elf_link_hash_entry *hlook; | |
5233 | asection *slook; | |
5234 | bfd_vma vlook; | |
ed54588d | 5235 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5236 | |
5237 | hlook = weaks; | |
60d67dc8 AM |
5238 | weaks = hlook->u.alias; |
5239 | hlook->u.alias = NULL; | |
4ad4eba5 | 5240 | |
e3e53eed AM |
5241 | if (hlook->root.type != bfd_link_hash_defined |
5242 | && hlook->root.type != bfd_link_hash_defweak) | |
5243 | continue; | |
5244 | ||
4ad4eba5 AM |
5245 | slook = hlook->root.u.def.section; |
5246 | vlook = hlook->root.u.def.value; | |
5247 | ||
4ad4eba5 AM |
5248 | i = 0; |
5249 | j = sym_count; | |
14160578 | 5250 | while (i != j) |
4ad4eba5 AM |
5251 | { |
5252 | bfd_signed_vma vdiff; | |
5253 | idx = (i + j) / 2; | |
14160578 | 5254 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5255 | vdiff = vlook - h->root.u.def.value; |
5256 | if (vdiff < 0) | |
5257 | j = idx; | |
5258 | else if (vdiff > 0) | |
5259 | i = idx + 1; | |
5260 | else | |
5261 | { | |
d3435ae8 | 5262 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5263 | if (sdiff < 0) |
5264 | j = idx; | |
5265 | else if (sdiff > 0) | |
5266 | i = idx + 1; | |
5267 | else | |
14160578 | 5268 | break; |
4ad4eba5 AM |
5269 | } |
5270 | } | |
5271 | ||
5272 | /* We didn't find a value/section match. */ | |
14160578 | 5273 | if (i == j) |
4ad4eba5 AM |
5274 | continue; |
5275 | ||
14160578 AM |
5276 | /* With multiple aliases, or when the weak symbol is already |
5277 | strongly defined, we have multiple matching symbols and | |
5278 | the binary search above may land on any of them. Step | |
5279 | one past the matching symbol(s). */ | |
5280 | while (++idx != j) | |
5281 | { | |
5282 | h = sorted_sym_hash[idx]; | |
5283 | if (h->root.u.def.section != slook | |
5284 | || h->root.u.def.value != vlook) | |
5285 | break; | |
5286 | } | |
5287 | ||
5288 | /* Now look back over the aliases. Since we sorted by size | |
5289 | as well as value and section, we'll choose the one with | |
5290 | the largest size. */ | |
5291 | while (idx-- != i) | |
4ad4eba5 | 5292 | { |
14160578 | 5293 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5294 | |
5295 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5296 | if (h->root.u.def.section != slook |
5297 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5298 | break; |
5299 | else if (h != hlook) | |
5300 | { | |
60d67dc8 AM |
5301 | struct elf_link_hash_entry *t; |
5302 | ||
5303 | hlook->u.alias = h; | |
5304 | hlook->is_weakalias = 1; | |
5305 | t = h; | |
5306 | if (t->u.alias != NULL) | |
5307 | while (t->u.alias != h) | |
5308 | t = t->u.alias; | |
5309 | t->u.alias = hlook; | |
4ad4eba5 AM |
5310 | |
5311 | /* If the weak definition is in the list of dynamic | |
5312 | symbols, make sure the real definition is put | |
5313 | there as well. */ | |
5314 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5315 | { | |
c152c796 | 5316 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5317 | { |
5318 | err_free_sym_hash: | |
5319 | free (sorted_sym_hash); | |
5320 | goto error_return; | |
5321 | } | |
4ad4eba5 AM |
5322 | } |
5323 | ||
5324 | /* If the real definition is in the list of dynamic | |
5325 | symbols, make sure the weak definition is put | |
5326 | there as well. If we don't do this, then the | |
5327 | dynamic loader might not merge the entries for the | |
5328 | real definition and the weak definition. */ | |
5329 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5330 | { | |
c152c796 | 5331 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5332 | goto err_free_sym_hash; |
4ad4eba5 AM |
5333 | } |
5334 | break; | |
5335 | } | |
5336 | } | |
5337 | } | |
5338 | ||
5339 | free (sorted_sym_hash); | |
5340 | } | |
5341 | ||
33177bb1 AM |
5342 | if (bed->check_directives |
5343 | && !(*bed->check_directives) (abfd, info)) | |
5344 | return FALSE; | |
85fbca6a | 5345 | |
4ad4eba5 AM |
5346 | /* If this is a non-traditional link, try to optimize the handling |
5347 | of the .stab/.stabstr sections. */ | |
5348 | if (! dynamic | |
5349 | && ! info->traditional_format | |
66eb6687 | 5350 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5351 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5352 | { | |
5353 | asection *stabstr; | |
5354 | ||
5355 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5356 | if (stabstr != NULL) | |
5357 | { | |
5358 | bfd_size_type string_offset = 0; | |
5359 | asection *stab; | |
5360 | ||
5361 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5362 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5363 | && (!stab->name[5] || |
5364 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5365 | && (stab->flags & SEC_MERGE) == 0 | |
5366 | && !bfd_is_abs_section (stab->output_section)) | |
5367 | { | |
5368 | struct bfd_elf_section_data *secdata; | |
5369 | ||
5370 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5371 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5372 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5373 | &string_offset)) |
5374 | goto error_return; | |
5375 | if (secdata->sec_info) | |
dbaa2011 | 5376 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5377 | } |
5378 | } | |
5379 | } | |
5380 | ||
66eb6687 | 5381 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5382 | { |
5383 | /* Add this bfd to the loaded list. */ | |
5384 | struct elf_link_loaded_list *n; | |
5385 | ||
ca4be51c | 5386 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5387 | if (n == NULL) |
5388 | goto error_return; | |
5389 | n->abfd = abfd; | |
66eb6687 AM |
5390 | n->next = htab->loaded; |
5391 | htab->loaded = n; | |
4ad4eba5 AM |
5392 | } |
5393 | ||
5394 | return TRUE; | |
5395 | ||
5396 | error_free_vers: | |
66eb6687 AM |
5397 | if (old_tab != NULL) |
5398 | free (old_tab); | |
5b677558 AM |
5399 | if (old_strtab != NULL) |
5400 | free (old_strtab); | |
4ad4eba5 AM |
5401 | if (nondeflt_vers != NULL) |
5402 | free (nondeflt_vers); | |
5403 | if (extversym != NULL) | |
5404 | free (extversym); | |
5405 | error_free_sym: | |
5406 | if (isymbuf != NULL) | |
5407 | free (isymbuf); | |
5408 | error_return: | |
5409 | return FALSE; | |
5410 | } | |
5411 | ||
8387904d AM |
5412 | /* Return the linker hash table entry of a symbol that might be |
5413 | satisfied by an archive symbol. Return -1 on error. */ | |
5414 | ||
5415 | struct elf_link_hash_entry * | |
5416 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5417 | struct bfd_link_info *info, | |
5418 | const char *name) | |
5419 | { | |
5420 | struct elf_link_hash_entry *h; | |
5421 | char *p, *copy; | |
5422 | size_t len, first; | |
5423 | ||
2a41f396 | 5424 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5425 | if (h != NULL) |
5426 | return h; | |
5427 | ||
5428 | /* If this is a default version (the name contains @@), look up the | |
5429 | symbol again with only one `@' as well as without the version. | |
5430 | The effect is that references to the symbol with and without the | |
5431 | version will be matched by the default symbol in the archive. */ | |
5432 | ||
5433 | p = strchr (name, ELF_VER_CHR); | |
5434 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5435 | return h; | |
5436 | ||
5437 | /* First check with only one `@'. */ | |
5438 | len = strlen (name); | |
a50b1753 | 5439 | copy = (char *) bfd_alloc (abfd, len); |
8387904d | 5440 | if (copy == NULL) |
e99955cd | 5441 | return (struct elf_link_hash_entry *) -1; |
8387904d AM |
5442 | |
5443 | first = p - name + 1; | |
5444 | memcpy (copy, name, first); | |
5445 | memcpy (copy + first, name + first + 1, len - first); | |
5446 | ||
2a41f396 | 5447 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5448 | if (h == NULL) |
5449 | { | |
5450 | /* We also need to check references to the symbol without the | |
5451 | version. */ | |
5452 | copy[first - 1] = '\0'; | |
5453 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5454 | FALSE, FALSE, TRUE); |
8387904d AM |
5455 | } |
5456 | ||
5457 | bfd_release (abfd, copy); | |
5458 | return h; | |
5459 | } | |
5460 | ||
0ad989f9 | 5461 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5462 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5463 | handle versioned symbols. | |
0ad989f9 L |
5464 | |
5465 | Fortunately, ELF archive handling is simpler than that done by | |
5466 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5467 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5468 | symbol is currently undefined, we know that we must pull in that | |
5469 | object file. | |
5470 | ||
5471 | Unfortunately, we do have to make multiple passes over the symbol | |
5472 | table until nothing further is resolved. */ | |
5473 | ||
4ad4eba5 AM |
5474 | static bfd_boolean |
5475 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5476 | { |
5477 | symindex c; | |
13e570f8 | 5478 | unsigned char *included = NULL; |
0ad989f9 L |
5479 | carsym *symdefs; |
5480 | bfd_boolean loop; | |
5481 | bfd_size_type amt; | |
8387904d AM |
5482 | const struct elf_backend_data *bed; |
5483 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5484 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5485 | |
5486 | if (! bfd_has_map (abfd)) | |
5487 | { | |
5488 | /* An empty archive is a special case. */ | |
5489 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5490 | return TRUE; | |
5491 | bfd_set_error (bfd_error_no_armap); | |
5492 | return FALSE; | |
5493 | } | |
5494 | ||
5495 | /* Keep track of all symbols we know to be already defined, and all | |
5496 | files we know to be already included. This is to speed up the | |
5497 | second and subsequent passes. */ | |
5498 | c = bfd_ardata (abfd)->symdef_count; | |
5499 | if (c == 0) | |
5500 | return TRUE; | |
5501 | amt = c; | |
13e570f8 AM |
5502 | amt *= sizeof (*included); |
5503 | included = (unsigned char *) bfd_zmalloc (amt); | |
5504 | if (included == NULL) | |
5505 | return FALSE; | |
0ad989f9 L |
5506 | |
5507 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5508 | bed = get_elf_backend_data (abfd); |
5509 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5510 | |
5511 | do | |
5512 | { | |
5513 | file_ptr last; | |
5514 | symindex i; | |
5515 | carsym *symdef; | |
5516 | carsym *symdefend; | |
5517 | ||
5518 | loop = FALSE; | |
5519 | last = -1; | |
5520 | ||
5521 | symdef = symdefs; | |
5522 | symdefend = symdef + c; | |
5523 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5524 | { | |
5525 | struct elf_link_hash_entry *h; | |
5526 | bfd *element; | |
5527 | struct bfd_link_hash_entry *undefs_tail; | |
5528 | symindex mark; | |
5529 | ||
13e570f8 | 5530 | if (included[i]) |
0ad989f9 L |
5531 | continue; |
5532 | if (symdef->file_offset == last) | |
5533 | { | |
5534 | included[i] = TRUE; | |
5535 | continue; | |
5536 | } | |
5537 | ||
8387904d | 5538 | h = archive_symbol_lookup (abfd, info, symdef->name); |
e99955cd | 5539 | if (h == (struct elf_link_hash_entry *) -1) |
8387904d | 5540 | goto error_return; |
0ad989f9 L |
5541 | |
5542 | if (h == NULL) | |
5543 | continue; | |
5544 | ||
5545 | if (h->root.type == bfd_link_hash_common) | |
5546 | { | |
5547 | /* We currently have a common symbol. The archive map contains | |
5548 | a reference to this symbol, so we may want to include it. We | |
5549 | only want to include it however, if this archive element | |
5550 | contains a definition of the symbol, not just another common | |
5551 | declaration of it. | |
5552 | ||
5553 | Unfortunately some archivers (including GNU ar) will put | |
5554 | declarations of common symbols into their archive maps, as | |
5555 | well as real definitions, so we cannot just go by the archive | |
5556 | map alone. Instead we must read in the element's symbol | |
5557 | table and check that to see what kind of symbol definition | |
5558 | this is. */ | |
5559 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5560 | continue; | |
5561 | } | |
5562 | else if (h->root.type != bfd_link_hash_undefined) | |
5563 | { | |
5564 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5565 | /* Symbol must be defined. Don't check it again. */ |
5566 | included[i] = TRUE; | |
0ad989f9 L |
5567 | continue; |
5568 | } | |
5569 | ||
5570 | /* We need to include this archive member. */ | |
5571 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5572 | if (element == NULL) | |
5573 | goto error_return; | |
5574 | ||
5575 | if (! bfd_check_format (element, bfd_object)) | |
5576 | goto error_return; | |
5577 | ||
0ad989f9 L |
5578 | undefs_tail = info->hash->undefs_tail; |
5579 | ||
0e144ba7 AM |
5580 | if (!(*info->callbacks |
5581 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5582 | continue; |
0e144ba7 | 5583 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5584 | goto error_return; |
5585 | ||
5586 | /* If there are any new undefined symbols, we need to make | |
5587 | another pass through the archive in order to see whether | |
5588 | they can be defined. FIXME: This isn't perfect, because | |
5589 | common symbols wind up on undefs_tail and because an | |
5590 | undefined symbol which is defined later on in this pass | |
5591 | does not require another pass. This isn't a bug, but it | |
5592 | does make the code less efficient than it could be. */ | |
5593 | if (undefs_tail != info->hash->undefs_tail) | |
5594 | loop = TRUE; | |
5595 | ||
5596 | /* Look backward to mark all symbols from this object file | |
5597 | which we have already seen in this pass. */ | |
5598 | mark = i; | |
5599 | do | |
5600 | { | |
5601 | included[mark] = TRUE; | |
5602 | if (mark == 0) | |
5603 | break; | |
5604 | --mark; | |
5605 | } | |
5606 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5607 | ||
5608 | /* We mark subsequent symbols from this object file as we go | |
5609 | on through the loop. */ | |
5610 | last = symdef->file_offset; | |
5611 | } | |
5612 | } | |
5613 | while (loop); | |
5614 | ||
0ad989f9 L |
5615 | free (included); |
5616 | ||
5617 | return TRUE; | |
5618 | ||
5619 | error_return: | |
0ad989f9 L |
5620 | if (included != NULL) |
5621 | free (included); | |
5622 | return FALSE; | |
5623 | } | |
4ad4eba5 AM |
5624 | |
5625 | /* Given an ELF BFD, add symbols to the global hash table as | |
5626 | appropriate. */ | |
5627 | ||
5628 | bfd_boolean | |
5629 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5630 | { | |
5631 | switch (bfd_get_format (abfd)) | |
5632 | { | |
5633 | case bfd_object: | |
5634 | return elf_link_add_object_symbols (abfd, info); | |
5635 | case bfd_archive: | |
5636 | return elf_link_add_archive_symbols (abfd, info); | |
5637 | default: | |
5638 | bfd_set_error (bfd_error_wrong_format); | |
5639 | return FALSE; | |
5640 | } | |
5641 | } | |
5a580b3a | 5642 | \f |
14b1c01e AM |
5643 | struct hash_codes_info |
5644 | { | |
5645 | unsigned long *hashcodes; | |
5646 | bfd_boolean error; | |
5647 | }; | |
a0c8462f | 5648 | |
5a580b3a AM |
5649 | /* This function will be called though elf_link_hash_traverse to store |
5650 | all hash value of the exported symbols in an array. */ | |
5651 | ||
5652 | static bfd_boolean | |
5653 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5654 | { | |
a50b1753 | 5655 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5656 | const char *name; |
5a580b3a AM |
5657 | unsigned long ha; |
5658 | char *alc = NULL; | |
5659 | ||
5a580b3a AM |
5660 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5661 | if (h->dynindx == -1) | |
5662 | return TRUE; | |
5663 | ||
5664 | name = h->root.root.string; | |
422f1182 | 5665 | if (h->versioned >= versioned) |
5a580b3a | 5666 | { |
422f1182 L |
5667 | char *p = strchr (name, ELF_VER_CHR); |
5668 | if (p != NULL) | |
14b1c01e | 5669 | { |
422f1182 L |
5670 | alc = (char *) bfd_malloc (p - name + 1); |
5671 | if (alc == NULL) | |
5672 | { | |
5673 | inf->error = TRUE; | |
5674 | return FALSE; | |
5675 | } | |
5676 | memcpy (alc, name, p - name); | |
5677 | alc[p - name] = '\0'; | |
5678 | name = alc; | |
14b1c01e | 5679 | } |
5a580b3a AM |
5680 | } |
5681 | ||
5682 | /* Compute the hash value. */ | |
5683 | ha = bfd_elf_hash (name); | |
5684 | ||
5685 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5686 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5687 | |
5688 | /* And store it in the struct so that we can put it in the hash table | |
5689 | later. */ | |
f6e332e6 | 5690 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5691 | |
5692 | if (alc != NULL) | |
5693 | free (alc); | |
5694 | ||
5695 | return TRUE; | |
5696 | } | |
5697 | ||
fdc90cb4 JJ |
5698 | struct collect_gnu_hash_codes |
5699 | { | |
5700 | bfd *output_bfd; | |
5701 | const struct elf_backend_data *bed; | |
5702 | unsigned long int nsyms; | |
5703 | unsigned long int maskbits; | |
5704 | unsigned long int *hashcodes; | |
5705 | unsigned long int *hashval; | |
5706 | unsigned long int *indx; | |
5707 | unsigned long int *counts; | |
5708 | bfd_vma *bitmask; | |
5709 | bfd_byte *contents; | |
5710 | long int min_dynindx; | |
5711 | unsigned long int bucketcount; | |
5712 | unsigned long int symindx; | |
5713 | long int local_indx; | |
5714 | long int shift1, shift2; | |
5715 | unsigned long int mask; | |
14b1c01e | 5716 | bfd_boolean error; |
fdc90cb4 JJ |
5717 | }; |
5718 | ||
5719 | /* This function will be called though elf_link_hash_traverse to store | |
5720 | all hash value of the exported symbols in an array. */ | |
5721 | ||
5722 | static bfd_boolean | |
5723 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5724 | { | |
a50b1753 | 5725 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5726 | const char *name; |
fdc90cb4 JJ |
5727 | unsigned long ha; |
5728 | char *alc = NULL; | |
5729 | ||
fdc90cb4 JJ |
5730 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5731 | if (h->dynindx == -1) | |
5732 | return TRUE; | |
5733 | ||
5734 | /* Ignore also local symbols and undefined symbols. */ | |
5735 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5736 | return TRUE; | |
5737 | ||
5738 | name = h->root.root.string; | |
422f1182 | 5739 | if (h->versioned >= versioned) |
fdc90cb4 | 5740 | { |
422f1182 L |
5741 | char *p = strchr (name, ELF_VER_CHR); |
5742 | if (p != NULL) | |
14b1c01e | 5743 | { |
422f1182 L |
5744 | alc = (char *) bfd_malloc (p - name + 1); |
5745 | if (alc == NULL) | |
5746 | { | |
5747 | s->error = TRUE; | |
5748 | return FALSE; | |
5749 | } | |
5750 | memcpy (alc, name, p - name); | |
5751 | alc[p - name] = '\0'; | |
5752 | name = alc; | |
14b1c01e | 5753 | } |
fdc90cb4 JJ |
5754 | } |
5755 | ||
5756 | /* Compute the hash value. */ | |
5757 | ha = bfd_elf_gnu_hash (name); | |
5758 | ||
5759 | /* Store the found hash value in the array for compute_bucket_count, | |
5760 | and also for .dynsym reordering purposes. */ | |
5761 | s->hashcodes[s->nsyms] = ha; | |
5762 | s->hashval[h->dynindx] = ha; | |
5763 | ++s->nsyms; | |
5764 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5765 | s->min_dynindx = h->dynindx; | |
5766 | ||
5767 | if (alc != NULL) | |
5768 | free (alc); | |
5769 | ||
5770 | return TRUE; | |
5771 | } | |
5772 | ||
5773 | /* This function will be called though elf_link_hash_traverse to do | |
5774 | final dynaminc symbol renumbering. */ | |
5775 | ||
5776 | static bfd_boolean | |
5777 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5778 | { | |
a50b1753 | 5779 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5780 | unsigned long int bucket; |
5781 | unsigned long int val; | |
5782 | ||
fdc90cb4 JJ |
5783 | /* Ignore indirect symbols. */ |
5784 | if (h->dynindx == -1) | |
5785 | return TRUE; | |
5786 | ||
5787 | /* Ignore also local symbols and undefined symbols. */ | |
5788 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5789 | { | |
5790 | if (h->dynindx >= s->min_dynindx) | |
5791 | h->dynindx = s->local_indx++; | |
5792 | return TRUE; | |
5793 | } | |
5794 | ||
5795 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5796 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5797 | & ((s->maskbits >> s->shift1) - 1); | |
5798 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5799 | s->bitmask[val] | |
5800 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5801 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5802 | if (s->counts[bucket] == 1) | |
5803 | /* Last element terminates the chain. */ | |
5804 | val |= 1; | |
5805 | bfd_put_32 (s->output_bfd, val, | |
5806 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5807 | --s->counts[bucket]; | |
5808 | h->dynindx = s->indx[bucket]++; | |
5809 | return TRUE; | |
5810 | } | |
5811 | ||
5812 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5813 | ||
5814 | bfd_boolean | |
5815 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5816 | { | |
5817 | return !(h->forced_local | |
5818 | || h->root.type == bfd_link_hash_undefined | |
5819 | || h->root.type == bfd_link_hash_undefweak | |
5820 | || ((h->root.type == bfd_link_hash_defined | |
5821 | || h->root.type == bfd_link_hash_defweak) | |
5822 | && h->root.u.def.section->output_section == NULL)); | |
5823 | } | |
5824 | ||
5a580b3a AM |
5825 | /* Array used to determine the number of hash table buckets to use |
5826 | based on the number of symbols there are. If there are fewer than | |
5827 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5828 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5829 | than 32771 buckets. */ | |
5830 | ||
5831 | static const size_t elf_buckets[] = | |
5832 | { | |
5833 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5834 | 16411, 32771, 0 | |
5835 | }; | |
5836 | ||
5837 | /* Compute bucket count for hashing table. We do not use a static set | |
5838 | of possible tables sizes anymore. Instead we determine for all | |
5839 | possible reasonable sizes of the table the outcome (i.e., the | |
5840 | number of collisions etc) and choose the best solution. The | |
5841 | weighting functions are not too simple to allow the table to grow | |
5842 | without bounds. Instead one of the weighting factors is the size. | |
5843 | Therefore the result is always a good payoff between few collisions | |
5844 | (= short chain lengths) and table size. */ | |
5845 | static size_t | |
b20dd2ce | 5846 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5847 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5848 | unsigned long int nsyms, | |
5849 | int gnu_hash) | |
5a580b3a | 5850 | { |
5a580b3a | 5851 | size_t best_size = 0; |
5a580b3a | 5852 | unsigned long int i; |
5a580b3a | 5853 | |
5a580b3a AM |
5854 | /* We have a problem here. The following code to optimize the table |
5855 | size requires an integer type with more the 32 bits. If | |
5856 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5857 | #ifdef BFD_HOST_U_64_BIT | |
5858 | if (info->optimize) | |
5859 | { | |
5a580b3a AM |
5860 | size_t minsize; |
5861 | size_t maxsize; | |
5862 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5863 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5864 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5865 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5866 | unsigned long int *counts; |
d40f3da9 | 5867 | bfd_size_type amt; |
0883b6e0 | 5868 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5869 | |
5870 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5871 | that the hashing table must at least have NSYMS/4 and at most | |
5872 | 2*NSYMS buckets. */ | |
5873 | minsize = nsyms / 4; | |
5874 | if (minsize == 0) | |
5875 | minsize = 1; | |
5876 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5877 | if (gnu_hash) |
5878 | { | |
5879 | if (minsize < 2) | |
5880 | minsize = 2; | |
5881 | if ((best_size & 31) == 0) | |
5882 | ++best_size; | |
5883 | } | |
5a580b3a AM |
5884 | |
5885 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5886 | since the size could be large. */ | |
5887 | amt = maxsize; | |
5888 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5889 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5890 | if (counts == NULL) |
fdc90cb4 | 5891 | return 0; |
5a580b3a AM |
5892 | |
5893 | /* Compute the "optimal" size for the hash table. The criteria is a | |
5894 | minimal chain length. The minor criteria is (of course) the size | |
5895 | of the table. */ | |
5896 | for (i = minsize; i < maxsize; ++i) | |
5897 | { | |
5898 | /* Walk through the array of hashcodes and count the collisions. */ | |
5899 | BFD_HOST_U_64_BIT max; | |
5900 | unsigned long int j; | |
5901 | unsigned long int fact; | |
5902 | ||
fdc90cb4 JJ |
5903 | if (gnu_hash && (i & 31) == 0) |
5904 | continue; | |
5905 | ||
5a580b3a AM |
5906 | memset (counts, '\0', i * sizeof (unsigned long int)); |
5907 | ||
5908 | /* Determine how often each hash bucket is used. */ | |
5909 | for (j = 0; j < nsyms; ++j) | |
5910 | ++counts[hashcodes[j] % i]; | |
5911 | ||
5912 | /* For the weight function we need some information about the | |
5913 | pagesize on the target. This is information need not be 100% | |
5914 | accurate. Since this information is not available (so far) we | |
5915 | define it here to a reasonable default value. If it is crucial | |
5916 | to have a better value some day simply define this value. */ | |
5917 | # ifndef BFD_TARGET_PAGESIZE | |
5918 | # define BFD_TARGET_PAGESIZE (4096) | |
5919 | # endif | |
5920 | ||
fdc90cb4 JJ |
5921 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
5922 | and the chains. */ | |
5923 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
5924 | |
5925 | # if 1 | |
5926 | /* Variant 1: optimize for short chains. We add the squares | |
5927 | of all the chain lengths (which favors many small chain | |
5928 | over a few long chains). */ | |
5929 | for (j = 0; j < i; ++j) | |
5930 | max += counts[j] * counts[j]; | |
5931 | ||
5932 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 5933 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5934 | max *= fact * fact; |
5935 | # else | |
5936 | /* Variant 2: Optimize a lot more for small table. Here we | |
5937 | also add squares of the size but we also add penalties for | |
5938 | empty slots (the +1 term). */ | |
5939 | for (j = 0; j < i; ++j) | |
5940 | max += (1 + counts[j]) * (1 + counts[j]); | |
5941 | ||
5942 | /* The overall size of the table is considered, but not as | |
5943 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 5944 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
5945 | max *= fact; |
5946 | # endif | |
5947 | ||
5948 | /* Compare with current best results. */ | |
5949 | if (max < best_chlen) | |
5950 | { | |
5951 | best_chlen = max; | |
5952 | best_size = i; | |
ca4be51c | 5953 | no_improvement_count = 0; |
5a580b3a | 5954 | } |
0883b6e0 NC |
5955 | /* PR 11843: Avoid futile long searches for the best bucket size |
5956 | when there are a large number of symbols. */ | |
5957 | else if (++no_improvement_count == 100) | |
5958 | break; | |
5a580b3a AM |
5959 | } |
5960 | ||
5961 | free (counts); | |
5962 | } | |
5963 | else | |
5964 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
5965 | { | |
5966 | /* This is the fallback solution if no 64bit type is available or if we | |
5967 | are not supposed to spend much time on optimizations. We select the | |
5968 | bucket count using a fixed set of numbers. */ | |
5969 | for (i = 0; elf_buckets[i] != 0; i++) | |
5970 | { | |
5971 | best_size = elf_buckets[i]; | |
fdc90cb4 | 5972 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
5973 | break; |
5974 | } | |
fdc90cb4 JJ |
5975 | if (gnu_hash && best_size < 2) |
5976 | best_size = 2; | |
5a580b3a AM |
5977 | } |
5978 | ||
5a580b3a AM |
5979 | return best_size; |
5980 | } | |
5981 | ||
d0bf826b AM |
5982 | /* Size any SHT_GROUP section for ld -r. */ |
5983 | ||
5984 | bfd_boolean | |
5985 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
5986 | { | |
5987 | bfd *ibfd; | |
57963c05 | 5988 | asection *s; |
d0bf826b | 5989 | |
c72f2fb2 | 5990 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b | 5991 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
57963c05 AM |
5992 | && (s = ibfd->sections) != NULL |
5993 | && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
d0bf826b AM |
5994 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) |
5995 | return FALSE; | |
5996 | return TRUE; | |
5997 | } | |
5998 | ||
04c3a755 NS |
5999 | /* Set a default stack segment size. The value in INFO wins. If it |
6000 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
6001 | undefined it is initialized. */ | |
6002 | ||
6003 | bfd_boolean | |
6004 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
6005 | struct bfd_link_info *info, | |
6006 | const char *legacy_symbol, | |
6007 | bfd_vma default_size) | |
6008 | { | |
6009 | struct elf_link_hash_entry *h = NULL; | |
6010 | ||
6011 | /* Look for legacy symbol. */ | |
6012 | if (legacy_symbol) | |
6013 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
6014 | FALSE, FALSE, FALSE); | |
6015 | if (h && (h->root.type == bfd_link_hash_defined | |
6016 | || h->root.type == bfd_link_hash_defweak) | |
6017 | && h->def_regular | |
6018 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
6019 | { | |
6020 | /* The symbol has no type if specified on the command line. */ | |
6021 | h->type = STT_OBJECT; | |
6022 | if (info->stacksize) | |
695344c0 | 6023 | /* xgettext:c-format */ |
871b3ab2 | 6024 | _bfd_error_handler (_("%pB: stack size specified and %s set"), |
4eca0228 | 6025 | output_bfd, legacy_symbol); |
04c3a755 | 6026 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 6027 | /* xgettext:c-format */ |
871b3ab2 | 6028 | _bfd_error_handler (_("%pB: %s not absolute"), |
4eca0228 | 6029 | output_bfd, legacy_symbol); |
04c3a755 NS |
6030 | else |
6031 | info->stacksize = h->root.u.def.value; | |
6032 | } | |
6033 | ||
6034 | if (!info->stacksize) | |
6035 | /* If the user didn't set a size, or explicitly inhibit the | |
6036 | size, set it now. */ | |
6037 | info->stacksize = default_size; | |
6038 | ||
6039 | /* Provide the legacy symbol, if it is referenced. */ | |
6040 | if (h && (h->root.type == bfd_link_hash_undefined | |
6041 | || h->root.type == bfd_link_hash_undefweak)) | |
6042 | { | |
6043 | struct bfd_link_hash_entry *bh = NULL; | |
6044 | ||
6045 | if (!(_bfd_generic_link_add_one_symbol | |
6046 | (info, output_bfd, legacy_symbol, | |
6047 | BSF_GLOBAL, bfd_abs_section_ptr, | |
6048 | info->stacksize >= 0 ? info->stacksize : 0, | |
6049 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
6050 | return FALSE; | |
6051 | ||
6052 | h = (struct elf_link_hash_entry *) bh; | |
6053 | h->def_regular = 1; | |
6054 | h->type = STT_OBJECT; | |
6055 | } | |
6056 | ||
6057 | return TRUE; | |
6058 | } | |
6059 | ||
b531344c MR |
6060 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
6061 | ||
6062 | struct elf_gc_sweep_symbol_info | |
6063 | { | |
6064 | struct bfd_link_info *info; | |
6065 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
6066 | bfd_boolean); | |
6067 | }; | |
6068 | ||
6069 | static bfd_boolean | |
6070 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) | |
6071 | { | |
6072 | if (!h->mark | |
6073 | && (((h->root.type == bfd_link_hash_defined | |
6074 | || h->root.type == bfd_link_hash_defweak) | |
6075 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) | |
6076 | && h->root.u.def.section->gc_mark)) | |
6077 | || h->root.type == bfd_link_hash_undefined | |
6078 | || h->root.type == bfd_link_hash_undefweak)) | |
6079 | { | |
6080 | struct elf_gc_sweep_symbol_info *inf; | |
6081 | ||
6082 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
6083 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
6084 | h->def_regular = 0; | |
6085 | h->ref_regular = 0; | |
6086 | h->ref_regular_nonweak = 0; | |
6087 | } | |
6088 | ||
6089 | return TRUE; | |
6090 | } | |
6091 | ||
5a580b3a AM |
6092 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
6093 | called by the ELF linker emulation before_allocation routine. We | |
6094 | must set the sizes of the sections before the linker sets the | |
6095 | addresses of the various sections. */ | |
6096 | ||
6097 | bfd_boolean | |
6098 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
6099 | const char *soname, | |
6100 | const char *rpath, | |
6101 | const char *filter_shlib, | |
7ee314fa AM |
6102 | const char *audit, |
6103 | const char *depaudit, | |
5a580b3a AM |
6104 | const char * const *auxiliary_filters, |
6105 | struct bfd_link_info *info, | |
fd91d419 | 6106 | asection **sinterpptr) |
5a580b3a | 6107 | { |
5a580b3a AM |
6108 | bfd *dynobj; |
6109 | const struct elf_backend_data *bed; | |
5a580b3a AM |
6110 | |
6111 | *sinterpptr = NULL; | |
6112 | ||
5a580b3a AM |
6113 | if (!is_elf_hash_table (info->hash)) |
6114 | return TRUE; | |
6115 | ||
5a580b3a AM |
6116 | dynobj = elf_hash_table (info)->dynobj; |
6117 | ||
9a2a56cc | 6118 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6119 | { |
902e9fc7 MR |
6120 | struct bfd_elf_version_tree *verdefs; |
6121 | struct elf_info_failed asvinfo; | |
5a580b3a AM |
6122 | struct bfd_elf_version_tree *t; |
6123 | struct bfd_elf_version_expr *d; | |
902e9fc7 | 6124 | asection *s; |
e6699019 | 6125 | size_t soname_indx; |
7ee314fa | 6126 | |
5a580b3a AM |
6127 | /* If we are supposed to export all symbols into the dynamic symbol |
6128 | table (this is not the normal case), then do so. */ | |
55255dae | 6129 | if (info->export_dynamic |
0e1862bb | 6130 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a | 6131 | { |
3d13f3e9 AM |
6132 | struct elf_info_failed eif; |
6133 | ||
6134 | eif.info = info; | |
6135 | eif.failed = FALSE; | |
5a580b3a AM |
6136 | elf_link_hash_traverse (elf_hash_table (info), |
6137 | _bfd_elf_export_symbol, | |
6138 | &eif); | |
6139 | if (eif.failed) | |
6140 | return FALSE; | |
6141 | } | |
6142 | ||
e6699019 L |
6143 | if (soname != NULL) |
6144 | { | |
6145 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6146 | soname, TRUE); | |
6147 | if (soname_indx == (size_t) -1 | |
6148 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
6149 | return FALSE; | |
6150 | } | |
6151 | else | |
6152 | soname_indx = (size_t) -1; | |
6153 | ||
5a580b3a | 6154 | /* Make all global versions with definition. */ |
fd91d419 | 6155 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6156 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6157 | if (!d->symver && d->literal) |
5a580b3a AM |
6158 | { |
6159 | const char *verstr, *name; | |
6160 | size_t namelen, verlen, newlen; | |
93252b1c | 6161 | char *newname, *p, leading_char; |
5a580b3a AM |
6162 | struct elf_link_hash_entry *newh; |
6163 | ||
93252b1c | 6164 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6165 | name = d->pattern; |
93252b1c | 6166 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6167 | verstr = t->name; |
6168 | verlen = strlen (verstr); | |
6169 | newlen = namelen + verlen + 3; | |
6170 | ||
a50b1753 | 6171 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6172 | if (newname == NULL) |
6173 | return FALSE; | |
93252b1c MF |
6174 | newname[0] = leading_char; |
6175 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6176 | |
6177 | /* Check the hidden versioned definition. */ | |
6178 | p = newname + namelen; | |
6179 | *p++ = ELF_VER_CHR; | |
6180 | memcpy (p, verstr, verlen + 1); | |
6181 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6182 | newname, FALSE, FALSE, | |
6183 | FALSE); | |
6184 | if (newh == NULL | |
6185 | || (newh->root.type != bfd_link_hash_defined | |
6186 | && newh->root.type != bfd_link_hash_defweak)) | |
6187 | { | |
6188 | /* Check the default versioned definition. */ | |
6189 | *p++ = ELF_VER_CHR; | |
6190 | memcpy (p, verstr, verlen + 1); | |
6191 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6192 | newname, FALSE, FALSE, | |
6193 | FALSE); | |
6194 | } | |
6195 | free (newname); | |
6196 | ||
6197 | /* Mark this version if there is a definition and it is | |
6198 | not defined in a shared object. */ | |
6199 | if (newh != NULL | |
f5385ebf | 6200 | && !newh->def_dynamic |
5a580b3a AM |
6201 | && (newh->root.type == bfd_link_hash_defined |
6202 | || newh->root.type == bfd_link_hash_defweak)) | |
6203 | d->symver = 1; | |
6204 | } | |
6205 | ||
6206 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6207 | asvinfo.info = info; |
5a580b3a AM |
6208 | asvinfo.failed = FALSE; |
6209 | ||
6210 | elf_link_hash_traverse (elf_hash_table (info), | |
6211 | _bfd_elf_link_assign_sym_version, | |
6212 | &asvinfo); | |
6213 | if (asvinfo.failed) | |
6214 | return FALSE; | |
6215 | ||
6216 | if (!info->allow_undefined_version) | |
6217 | { | |
6218 | /* Check if all global versions have a definition. */ | |
3d13f3e9 | 6219 | bfd_boolean all_defined = TRUE; |
fd91d419 | 6220 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6221 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6222 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6223 | { |
4eca0228 | 6224 | _bfd_error_handler |
5a580b3a AM |
6225 | (_("%s: undefined version: %s"), |
6226 | d->pattern, t->name); | |
6227 | all_defined = FALSE; | |
6228 | } | |
6229 | ||
6230 | if (!all_defined) | |
6231 | { | |
6232 | bfd_set_error (bfd_error_bad_value); | |
6233 | return FALSE; | |
6234 | } | |
6235 | } | |
6236 | ||
902e9fc7 MR |
6237 | /* Set up the version definition section. */ |
6238 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); | |
6239 | BFD_ASSERT (s != NULL); | |
5a580b3a | 6240 | |
902e9fc7 MR |
6241 | /* We may have created additional version definitions if we are |
6242 | just linking a regular application. */ | |
6243 | verdefs = info->version_info; | |
5a580b3a | 6244 | |
902e9fc7 MR |
6245 | /* Skip anonymous version tag. */ |
6246 | if (verdefs != NULL && verdefs->vernum == 0) | |
6247 | verdefs = verdefs->next; | |
5a580b3a | 6248 | |
902e9fc7 MR |
6249 | if (verdefs == NULL && !info->create_default_symver) |
6250 | s->flags |= SEC_EXCLUDE; | |
6251 | else | |
5a580b3a | 6252 | { |
902e9fc7 MR |
6253 | unsigned int cdefs; |
6254 | bfd_size_type size; | |
6255 | bfd_byte *p; | |
6256 | Elf_Internal_Verdef def; | |
6257 | Elf_Internal_Verdaux defaux; | |
6258 | struct bfd_link_hash_entry *bh; | |
6259 | struct elf_link_hash_entry *h; | |
6260 | const char *name; | |
5a580b3a | 6261 | |
902e9fc7 MR |
6262 | cdefs = 0; |
6263 | size = 0; | |
5a580b3a | 6264 | |
902e9fc7 MR |
6265 | /* Make space for the base version. */ |
6266 | size += sizeof (Elf_External_Verdef); | |
6267 | size += sizeof (Elf_External_Verdaux); | |
6268 | ++cdefs; | |
6269 | ||
6270 | /* Make space for the default version. */ | |
6271 | if (info->create_default_symver) | |
6272 | { | |
6273 | size += sizeof (Elf_External_Verdef); | |
6274 | ++cdefs; | |
3e3b46e5 PB |
6275 | } |
6276 | ||
5a580b3a AM |
6277 | for (t = verdefs; t != NULL; t = t->next) |
6278 | { | |
6279 | struct bfd_elf_version_deps *n; | |
6280 | ||
a6cc6b3b RO |
6281 | /* Don't emit base version twice. */ |
6282 | if (t->vernum == 0) | |
6283 | continue; | |
6284 | ||
5a580b3a AM |
6285 | size += sizeof (Elf_External_Verdef); |
6286 | size += sizeof (Elf_External_Verdaux); | |
6287 | ++cdefs; | |
6288 | ||
6289 | for (n = t->deps; n != NULL; n = n->next) | |
6290 | size += sizeof (Elf_External_Verdaux); | |
6291 | } | |
6292 | ||
eea6121a | 6293 | s->size = size; |
a50b1753 | 6294 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6295 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6296 | return FALSE; |
6297 | ||
6298 | /* Fill in the version definition section. */ | |
6299 | ||
6300 | p = s->contents; | |
6301 | ||
6302 | def.vd_version = VER_DEF_CURRENT; | |
6303 | def.vd_flags = VER_FLG_BASE; | |
6304 | def.vd_ndx = 1; | |
6305 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6306 | if (info->create_default_symver) |
6307 | { | |
6308 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6309 | def.vd_next = sizeof (Elf_External_Verdef); | |
6310 | } | |
6311 | else | |
6312 | { | |
6313 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6314 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6315 | + sizeof (Elf_External_Verdaux)); | |
6316 | } | |
5a580b3a | 6317 | |
ef53be89 | 6318 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6319 | { |
6320 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6321 | soname_indx); | |
6322 | def.vd_hash = bfd_elf_hash (soname); | |
6323 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6324 | name = soname; |
5a580b3a AM |
6325 | } |
6326 | else | |
6327 | { | |
ef53be89 | 6328 | size_t indx; |
5a580b3a | 6329 | |
06084812 | 6330 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6331 | def.vd_hash = bfd_elf_hash (name); |
6332 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6333 | name, FALSE); | |
ef53be89 | 6334 | if (indx == (size_t) -1) |
5a580b3a AM |
6335 | return FALSE; |
6336 | defaux.vda_name = indx; | |
6337 | } | |
6338 | defaux.vda_next = 0; | |
6339 | ||
6340 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6341 | (Elf_External_Verdef *) p); | |
6342 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6343 | if (info->create_default_symver) |
6344 | { | |
6345 | /* Add a symbol representing this version. */ | |
6346 | bh = NULL; | |
6347 | if (! (_bfd_generic_link_add_one_symbol | |
6348 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6349 | 0, NULL, FALSE, | |
6350 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6351 | return FALSE; | |
6352 | h = (struct elf_link_hash_entry *) bh; | |
6353 | h->non_elf = 0; | |
6354 | h->def_regular = 1; | |
6355 | h->type = STT_OBJECT; | |
6356 | h->verinfo.vertree = NULL; | |
6357 | ||
6358 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6359 | return FALSE; | |
6360 | ||
6361 | /* Create a duplicate of the base version with the same | |
6362 | aux block, but different flags. */ | |
6363 | def.vd_flags = 0; | |
6364 | def.vd_ndx = 2; | |
6365 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6366 | if (verdefs) | |
6367 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6368 | + sizeof (Elf_External_Verdaux)); | |
6369 | else | |
6370 | def.vd_next = 0; | |
6371 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6372 | (Elf_External_Verdef *) p); | |
6373 | p += sizeof (Elf_External_Verdef); | |
6374 | } | |
5a580b3a AM |
6375 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6376 | (Elf_External_Verdaux *) p); | |
6377 | p += sizeof (Elf_External_Verdaux); | |
6378 | ||
6379 | for (t = verdefs; t != NULL; t = t->next) | |
6380 | { | |
6381 | unsigned int cdeps; | |
6382 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6383 | |
a6cc6b3b RO |
6384 | /* Don't emit the base version twice. */ |
6385 | if (t->vernum == 0) | |
6386 | continue; | |
6387 | ||
5a580b3a AM |
6388 | cdeps = 0; |
6389 | for (n = t->deps; n != NULL; n = n->next) | |
6390 | ++cdeps; | |
6391 | ||
6392 | /* Add a symbol representing this version. */ | |
6393 | bh = NULL; | |
6394 | if (! (_bfd_generic_link_add_one_symbol | |
6395 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6396 | 0, NULL, FALSE, | |
6397 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6398 | return FALSE; | |
6399 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6400 | h->non_elf = 0; |
6401 | h->def_regular = 1; | |
5a580b3a AM |
6402 | h->type = STT_OBJECT; |
6403 | h->verinfo.vertree = t; | |
6404 | ||
c152c796 | 6405 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6406 | return FALSE; |
6407 | ||
6408 | def.vd_version = VER_DEF_CURRENT; | |
6409 | def.vd_flags = 0; | |
6410 | if (t->globals.list == NULL | |
6411 | && t->locals.list == NULL | |
6412 | && ! t->used) | |
6413 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6414 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6415 | def.vd_cnt = cdeps + 1; |
6416 | def.vd_hash = bfd_elf_hash (t->name); | |
6417 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6418 | def.vd_next = 0; | |
a6cc6b3b RO |
6419 | |
6420 | /* If a basever node is next, it *must* be the last node in | |
6421 | the chain, otherwise Verdef construction breaks. */ | |
6422 | if (t->next != NULL && t->next->vernum == 0) | |
6423 | BFD_ASSERT (t->next->next == NULL); | |
6424 | ||
6425 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6426 | def.vd_next = (sizeof (Elf_External_Verdef) |
6427 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6428 | ||
6429 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6430 | (Elf_External_Verdef *) p); | |
6431 | p += sizeof (Elf_External_Verdef); | |
6432 | ||
6433 | defaux.vda_name = h->dynstr_index; | |
6434 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6435 | h->dynstr_index); | |
6436 | defaux.vda_next = 0; | |
6437 | if (t->deps != NULL) | |
6438 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6439 | t->name_indx = defaux.vda_name; | |
6440 | ||
6441 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6442 | (Elf_External_Verdaux *) p); | |
6443 | p += sizeof (Elf_External_Verdaux); | |
6444 | ||
6445 | for (n = t->deps; n != NULL; n = n->next) | |
6446 | { | |
6447 | if (n->version_needed == NULL) | |
6448 | { | |
6449 | /* This can happen if there was an error in the | |
6450 | version script. */ | |
6451 | defaux.vda_name = 0; | |
6452 | } | |
6453 | else | |
6454 | { | |
6455 | defaux.vda_name = n->version_needed->name_indx; | |
6456 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6457 | defaux.vda_name); | |
6458 | } | |
6459 | if (n->next == NULL) | |
6460 | defaux.vda_next = 0; | |
6461 | else | |
6462 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6463 | ||
6464 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6465 | (Elf_External_Verdaux *) p); | |
6466 | p += sizeof (Elf_External_Verdaux); | |
6467 | } | |
6468 | } | |
6469 | ||
5a580b3a AM |
6470 | elf_tdata (output_bfd)->cverdefs = cdefs; |
6471 | } | |
902e9fc7 MR |
6472 | } |
6473 | ||
6474 | bed = get_elf_backend_data (output_bfd); | |
6475 | ||
6476 | if (info->gc_sections && bed->can_gc_sections) | |
6477 | { | |
6478 | struct elf_gc_sweep_symbol_info sweep_info; | |
902e9fc7 MR |
6479 | |
6480 | /* Remove the symbols that were in the swept sections from the | |
3d13f3e9 | 6481 | dynamic symbol table. */ |
902e9fc7 MR |
6482 | sweep_info.info = info; |
6483 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
6484 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
6485 | &sweep_info); | |
3d13f3e9 AM |
6486 | } |
6487 | ||
6488 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6489 | { | |
6490 | asection *s; | |
6491 | struct elf_find_verdep_info sinfo; | |
6492 | ||
6493 | /* Work out the size of the version reference section. */ | |
6494 | ||
6495 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); | |
6496 | BFD_ASSERT (s != NULL); | |
902e9fc7 | 6497 | |
3d13f3e9 AM |
6498 | sinfo.info = info; |
6499 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6500 | if (sinfo.vers == 0) | |
6501 | sinfo.vers = 1; | |
6502 | sinfo.failed = FALSE; | |
6503 | ||
6504 | elf_link_hash_traverse (elf_hash_table (info), | |
6505 | _bfd_elf_link_find_version_dependencies, | |
6506 | &sinfo); | |
6507 | if (sinfo.failed) | |
6508 | return FALSE; | |
6509 | ||
6510 | if (elf_tdata (output_bfd)->verref == NULL) | |
6511 | s->flags |= SEC_EXCLUDE; | |
6512 | else | |
6513 | { | |
6514 | Elf_Internal_Verneed *vn; | |
6515 | unsigned int size; | |
6516 | unsigned int crefs; | |
6517 | bfd_byte *p; | |
6518 | ||
6519 | /* Build the version dependency section. */ | |
6520 | size = 0; | |
6521 | crefs = 0; | |
6522 | for (vn = elf_tdata (output_bfd)->verref; | |
6523 | vn != NULL; | |
6524 | vn = vn->vn_nextref) | |
6525 | { | |
6526 | Elf_Internal_Vernaux *a; | |
6527 | ||
6528 | size += sizeof (Elf_External_Verneed); | |
6529 | ++crefs; | |
6530 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6531 | size += sizeof (Elf_External_Vernaux); | |
6532 | } | |
6533 | ||
6534 | s->size = size; | |
6535 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); | |
6536 | if (s->contents == NULL) | |
6537 | return FALSE; | |
6538 | ||
6539 | p = s->contents; | |
6540 | for (vn = elf_tdata (output_bfd)->verref; | |
6541 | vn != NULL; | |
6542 | vn = vn->vn_nextref) | |
6543 | { | |
6544 | unsigned int caux; | |
6545 | Elf_Internal_Vernaux *a; | |
6546 | size_t indx; | |
6547 | ||
6548 | caux = 0; | |
6549 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6550 | ++caux; | |
6551 | ||
6552 | vn->vn_version = VER_NEED_CURRENT; | |
6553 | vn->vn_cnt = caux; | |
6554 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6555 | elf_dt_name (vn->vn_bfd) != NULL | |
6556 | ? elf_dt_name (vn->vn_bfd) | |
6557 | : lbasename (vn->vn_bfd->filename), | |
6558 | FALSE); | |
6559 | if (indx == (size_t) -1) | |
6560 | return FALSE; | |
6561 | vn->vn_file = indx; | |
6562 | vn->vn_aux = sizeof (Elf_External_Verneed); | |
6563 | if (vn->vn_nextref == NULL) | |
6564 | vn->vn_next = 0; | |
6565 | else | |
6566 | vn->vn_next = (sizeof (Elf_External_Verneed) | |
6567 | + caux * sizeof (Elf_External_Vernaux)); | |
6568 | ||
6569 | _bfd_elf_swap_verneed_out (output_bfd, vn, | |
6570 | (Elf_External_Verneed *) p); | |
6571 | p += sizeof (Elf_External_Verneed); | |
6572 | ||
6573 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6574 | { | |
6575 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6576 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6577 | a->vna_nodename, FALSE); | |
6578 | if (indx == (size_t) -1) | |
6579 | return FALSE; | |
6580 | a->vna_name = indx; | |
6581 | if (a->vna_nextptr == NULL) | |
6582 | a->vna_next = 0; | |
6583 | else | |
6584 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6585 | ||
6586 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6587 | (Elf_External_Vernaux *) p); | |
6588 | p += sizeof (Elf_External_Vernaux); | |
6589 | } | |
6590 | } | |
6591 | ||
6592 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6593 | } | |
902e9fc7 MR |
6594 | } |
6595 | ||
6596 | /* Any syms created from now on start with -1 in | |
6597 | got.refcount/offset and plt.refcount/offset. */ | |
6598 | elf_hash_table (info)->init_got_refcount | |
6599 | = elf_hash_table (info)->init_got_offset; | |
6600 | elf_hash_table (info)->init_plt_refcount | |
6601 | = elf_hash_table (info)->init_plt_offset; | |
6602 | ||
6603 | if (bfd_link_relocatable (info) | |
6604 | && !_bfd_elf_size_group_sections (info)) | |
6605 | return FALSE; | |
6606 | ||
6607 | /* The backend may have to create some sections regardless of whether | |
6608 | we're dynamic or not. */ | |
6609 | if (bed->elf_backend_always_size_sections | |
6610 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
6611 | return FALSE; | |
6612 | ||
6613 | /* Determine any GNU_STACK segment requirements, after the backend | |
6614 | has had a chance to set a default segment size. */ | |
6615 | if (info->execstack) | |
6616 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; | |
6617 | else if (info->noexecstack) | |
6618 | elf_stack_flags (output_bfd) = PF_R | PF_W; | |
6619 | else | |
6620 | { | |
6621 | bfd *inputobj; | |
6622 | asection *notesec = NULL; | |
6623 | int exec = 0; | |
6624 | ||
6625 | for (inputobj = info->input_bfds; | |
6626 | inputobj; | |
6627 | inputobj = inputobj->link.next) | |
6628 | { | |
6629 | asection *s; | |
6630 | ||
6631 | if (inputobj->flags | |
6632 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
6633 | continue; | |
57963c05 AM |
6634 | s = inputobj->sections; |
6635 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
6636 | continue; | |
6637 | ||
902e9fc7 MR |
6638 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); |
6639 | if (s) | |
6640 | { | |
6641 | if (s->flags & SEC_CODE) | |
6642 | exec = PF_X; | |
6643 | notesec = s; | |
6644 | } | |
6645 | else if (bed->default_execstack) | |
6646 | exec = PF_X; | |
6647 | } | |
6648 | if (notesec || info->stacksize > 0) | |
6649 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; | |
6650 | if (notesec && exec && bfd_link_relocatable (info) | |
6651 | && notesec->output_section != bfd_abs_section_ptr) | |
6652 | notesec->output_section->flags |= SEC_CODE; | |
6653 | } | |
6654 | ||
6655 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6656 | { | |
6657 | struct elf_info_failed eif; | |
6658 | struct elf_link_hash_entry *h; | |
6659 | asection *dynstr; | |
6660 | asection *s; | |
6661 | ||
6662 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); | |
6663 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); | |
6664 | ||
902e9fc7 MR |
6665 | if (info->symbolic) |
6666 | { | |
6667 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
6668 | return FALSE; | |
6669 | info->flags |= DF_SYMBOLIC; | |
6670 | } | |
6671 | ||
6672 | if (rpath != NULL) | |
6673 | { | |
6674 | size_t indx; | |
6675 | bfd_vma tag; | |
6676 | ||
6677 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
6678 | TRUE); | |
6679 | if (indx == (size_t) -1) | |
6680 | return FALSE; | |
6681 | ||
6682 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; | |
6683 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
6684 | return FALSE; | |
6685 | } | |
6686 | ||
6687 | if (filter_shlib != NULL) | |
6688 | { | |
6689 | size_t indx; | |
6690 | ||
6691 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6692 | filter_shlib, TRUE); | |
6693 | if (indx == (size_t) -1 | |
6694 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
6695 | return FALSE; | |
6696 | } | |
6697 | ||
6698 | if (auxiliary_filters != NULL) | |
6699 | { | |
6700 | const char * const *p; | |
6701 | ||
6702 | for (p = auxiliary_filters; *p != NULL; p++) | |
6703 | { | |
6704 | size_t indx; | |
6705 | ||
6706 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6707 | *p, TRUE); | |
6708 | if (indx == (size_t) -1 | |
6709 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
6710 | return FALSE; | |
6711 | } | |
6712 | } | |
6713 | ||
6714 | if (audit != NULL) | |
6715 | { | |
6716 | size_t indx; | |
6717 | ||
6718 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
6719 | TRUE); | |
6720 | if (indx == (size_t) -1 | |
6721 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
6722 | return FALSE; | |
6723 | } | |
6724 | ||
6725 | if (depaudit != NULL) | |
6726 | { | |
6727 | size_t indx; | |
6728 | ||
6729 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
6730 | TRUE); | |
6731 | if (indx == (size_t) -1 | |
6732 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
6733 | return FALSE; | |
6734 | } | |
6735 | ||
6736 | eif.info = info; | |
6737 | eif.failed = FALSE; | |
6738 | ||
6739 | /* Find all symbols which were defined in a dynamic object and make | |
6740 | the backend pick a reasonable value for them. */ | |
6741 | elf_link_hash_traverse (elf_hash_table (info), | |
6742 | _bfd_elf_adjust_dynamic_symbol, | |
6743 | &eif); | |
6744 | if (eif.failed) | |
6745 | return FALSE; | |
6746 | ||
6747 | /* Add some entries to the .dynamic section. We fill in some of the | |
6748 | values later, in bfd_elf_final_link, but we must add the entries | |
6749 | now so that we know the final size of the .dynamic section. */ | |
6750 | ||
6751 | /* If there are initialization and/or finalization functions to | |
6752 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6753 | h = (info->init_function | |
6754 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6755 | info->init_function, FALSE, | |
6756 | FALSE, FALSE) | |
6757 | : NULL); | |
6758 | if (h != NULL | |
6759 | && (h->ref_regular | |
6760 | || h->def_regular)) | |
6761 | { | |
6762 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6763 | return FALSE; | |
6764 | } | |
6765 | h = (info->fini_function | |
6766 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6767 | info->fini_function, FALSE, | |
6768 | FALSE, FALSE) | |
6769 | : NULL); | |
6770 | if (h != NULL | |
6771 | && (h->ref_regular | |
6772 | || h->def_regular)) | |
6773 | { | |
6774 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6775 | return FALSE; | |
6776 | } | |
6777 | ||
6778 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); | |
6779 | if (s != NULL && s->linker_has_input) | |
6780 | { | |
6781 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
6782 | if (! bfd_link_executable (info)) | |
6783 | { | |
6784 | bfd *sub; | |
6785 | asection *o; | |
6786 | ||
57963c05 AM |
6787 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
6788 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
6789 | && (o = sub->sections) != NULL | |
6790 | && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) | |
902e9fc7 MR |
6791 | for (o = sub->sections; o != NULL; o = o->next) |
6792 | if (elf_section_data (o)->this_hdr.sh_type | |
6793 | == SHT_PREINIT_ARRAY) | |
6794 | { | |
6795 | _bfd_error_handler | |
871b3ab2 | 6796 | (_("%pB: .preinit_array section is not allowed in DSO"), |
902e9fc7 MR |
6797 | sub); |
6798 | break; | |
6799 | } | |
6800 | ||
6801 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6802 | return FALSE; | |
6803 | } | |
6804 | ||
6805 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6806 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6807 | return FALSE; | |
6808 | } | |
6809 | s = bfd_get_section_by_name (output_bfd, ".init_array"); | |
6810 | if (s != NULL && s->linker_has_input) | |
6811 | { | |
6812 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6813 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6814 | return FALSE; | |
6815 | } | |
6816 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); | |
6817 | if (s != NULL && s->linker_has_input) | |
6818 | { | |
6819 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6820 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6821 | return FALSE; | |
6822 | } | |
6823 | ||
6824 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); | |
6825 | /* If .dynstr is excluded from the link, we don't want any of | |
6826 | these tags. Strictly, we should be checking each section | |
6827 | individually; This quick check covers for the case where | |
6828 | someone does a /DISCARD/ : { *(*) }. */ | |
6829 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6830 | { | |
6831 | bfd_size_type strsize; | |
6832 | ||
6833 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
6834 | if ((info->emit_hash | |
6835 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6836 | || (info->emit_gnu_hash | |
6837 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
6838 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
6839 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6840 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6841 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6842 | bed->s->sizeof_sym)) | |
6843 | return FALSE; | |
6844 | } | |
6845 | } | |
6846 | ||
6847 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
6848 | return FALSE; | |
6849 | ||
6850 | /* The backend must work out the sizes of all the other dynamic | |
6851 | sections. */ | |
6852 | if (dynobj != NULL | |
6853 | && bed->elf_backend_size_dynamic_sections != NULL | |
6854 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
6855 | return FALSE; | |
6856 | ||
6857 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6858 | { | |
902e9fc7 MR |
6859 | if (elf_tdata (output_bfd)->cverdefs) |
6860 | { | |
6861 | unsigned int crefs = elf_tdata (output_bfd)->cverdefs; | |
6862 | ||
6863 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6864 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) | |
6865 | return FALSE; | |
6866 | } | |
6867 | ||
6868 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6869 | { | |
6870 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6871 | return FALSE; | |
6872 | } | |
6873 | else if (info->flags & DF_BIND_NOW) | |
6874 | { | |
6875 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6876 | return FALSE; | |
6877 | } | |
6878 | ||
6879 | if (info->flags_1) | |
6880 | { | |
6881 | if (bfd_link_executable (info)) | |
6882 | info->flags_1 &= ~ (DF_1_INITFIRST | |
6883 | | DF_1_NODELETE | |
6884 | | DF_1_NOOPEN); | |
6885 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6886 | return FALSE; | |
6887 | } | |
6888 | ||
6889 | if (elf_tdata (output_bfd)->cverrefs) | |
6890 | { | |
6891 | unsigned int crefs = elf_tdata (output_bfd)->cverrefs; | |
6892 | ||
6893 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
6894 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
6895 | return FALSE; | |
6896 | } | |
5a580b3a | 6897 | |
8423293d AM |
6898 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
6899 | && elf_tdata (output_bfd)->cverdefs == 0) | |
63f452a8 | 6900 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) |
8423293d | 6901 | { |
902e9fc7 MR |
6902 | asection *s; |
6903 | ||
3d4d4302 | 6904 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
6905 | s->flags |= SEC_EXCLUDE; |
6906 | } | |
6907 | } | |
6908 | return TRUE; | |
6909 | } | |
6910 | ||
74541ad4 AM |
6911 | /* Find the first non-excluded output section. We'll use its |
6912 | section symbol for some emitted relocs. */ | |
6913 | void | |
6914 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
6915 | { | |
6916 | asection *s; | |
6917 | ||
6918 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
6919 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
d00dd7dc | 6920 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 AM |
6921 | { |
6922 | elf_hash_table (info)->text_index_section = s; | |
6923 | break; | |
6924 | } | |
6925 | } | |
6926 | ||
6927 | /* Find two non-excluded output sections, one for code, one for data. | |
6928 | We'll use their section symbols for some emitted relocs. */ | |
6929 | void | |
6930 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
6931 | { | |
6932 | asection *s; | |
6933 | ||
266b05cf DJ |
6934 | /* Data first, since setting text_index_section changes |
6935 | _bfd_elf_link_omit_section_dynsym. */ | |
74541ad4 | 6936 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 6937 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
d00dd7dc | 6938 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 6939 | { |
266b05cf | 6940 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
6941 | break; |
6942 | } | |
6943 | ||
6944 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
6945 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
6946 | == (SEC_ALLOC | SEC_READONLY)) | |
d00dd7dc | 6947 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 6948 | { |
266b05cf | 6949 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
6950 | break; |
6951 | } | |
6952 | ||
6953 | if (elf_hash_table (info)->text_index_section == NULL) | |
6954 | elf_hash_table (info)->text_index_section | |
6955 | = elf_hash_table (info)->data_index_section; | |
6956 | } | |
6957 | ||
8423293d AM |
6958 | bfd_boolean |
6959 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
6960 | { | |
74541ad4 | 6961 | const struct elf_backend_data *bed; |
23ec1e32 | 6962 | unsigned long section_sym_count; |
96d01d93 | 6963 | bfd_size_type dynsymcount = 0; |
74541ad4 | 6964 | |
8423293d AM |
6965 | if (!is_elf_hash_table (info->hash)) |
6966 | return TRUE; | |
6967 | ||
74541ad4 AM |
6968 | bed = get_elf_backend_data (output_bfd); |
6969 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
6970 | ||
23ec1e32 MR |
6971 | /* Assign dynsym indices. In a shared library we generate a section |
6972 | symbol for each output section, which come first. Next come all | |
6973 | of the back-end allocated local dynamic syms, followed by the rest | |
6974 | of the global symbols. | |
6975 | ||
6976 | This is usually not needed for static binaries, however backends | |
6977 | can request to always do it, e.g. the MIPS backend uses dynamic | |
6978 | symbol counts to lay out GOT, which will be produced in the | |
6979 | presence of GOT relocations even in static binaries (holding fixed | |
6980 | data in that case, to satisfy those relocations). */ | |
6981 | ||
6982 | if (elf_hash_table (info)->dynamic_sections_created | |
6983 | || bed->always_renumber_dynsyms) | |
6984 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
6985 | §ion_sym_count); | |
6986 | ||
8423293d AM |
6987 | if (elf_hash_table (info)->dynamic_sections_created) |
6988 | { | |
6989 | bfd *dynobj; | |
8423293d | 6990 | asection *s; |
8423293d AM |
6991 | unsigned int dtagcount; |
6992 | ||
6993 | dynobj = elf_hash_table (info)->dynobj; | |
6994 | ||
5a580b3a | 6995 | /* Work out the size of the symbol version section. */ |
3d4d4302 | 6996 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 6997 | BFD_ASSERT (s != NULL); |
d5486c43 | 6998 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 6999 | { |
eea6121a | 7000 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 7001 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
7002 | if (s->contents == NULL) |
7003 | return FALSE; | |
7004 | ||
7005 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
7006 | return FALSE; | |
7007 | } | |
7008 | ||
7009 | /* Set the size of the .dynsym and .hash sections. We counted | |
7010 | the number of dynamic symbols in elf_link_add_object_symbols. | |
7011 | We will build the contents of .dynsym and .hash when we build | |
7012 | the final symbol table, because until then we do not know the | |
7013 | correct value to give the symbols. We built the .dynstr | |
7014 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 7015 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 7016 | BFD_ASSERT (s != NULL); |
eea6121a | 7017 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 7018 | |
d5486c43 L |
7019 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
7020 | if (s->contents == NULL) | |
7021 | return FALSE; | |
5a580b3a | 7022 | |
d5486c43 L |
7023 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
7024 | section syms, in case we don't output them all. */ | |
7025 | ++section_sym_count; | |
7026 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 7027 | |
fdc90cb4 JJ |
7028 | elf_hash_table (info)->bucketcount = 0; |
7029 | ||
5a580b3a AM |
7030 | /* Compute the size of the hashing table. As a side effect this |
7031 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
7032 | if (info->emit_hash) |
7033 | { | |
7034 | unsigned long int *hashcodes; | |
14b1c01e | 7035 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
7036 | bfd_size_type amt; |
7037 | unsigned long int nsyms; | |
7038 | size_t bucketcount; | |
7039 | size_t hash_entry_size; | |
7040 | ||
7041 | /* Compute the hash values for all exported symbols. At the same | |
7042 | time store the values in an array so that we could use them for | |
7043 | optimizations. */ | |
7044 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 7045 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7046 | if (hashcodes == NULL) |
7047 | return FALSE; | |
14b1c01e AM |
7048 | hashinf.hashcodes = hashcodes; |
7049 | hashinf.error = FALSE; | |
5a580b3a | 7050 | |
fdc90cb4 JJ |
7051 | /* Put all hash values in HASHCODES. */ |
7052 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
7053 | elf_collect_hash_codes, &hashinf); |
7054 | if (hashinf.error) | |
4dd07732 AM |
7055 | { |
7056 | free (hashcodes); | |
7057 | return FALSE; | |
7058 | } | |
5a580b3a | 7059 | |
14b1c01e | 7060 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
7061 | bucketcount |
7062 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
7063 | free (hashcodes); | |
7064 | ||
4b48e2f6 | 7065 | if (bucketcount == 0 && nsyms > 0) |
fdc90cb4 | 7066 | return FALSE; |
5a580b3a | 7067 | |
fdc90cb4 JJ |
7068 | elf_hash_table (info)->bucketcount = bucketcount; |
7069 | ||
3d4d4302 | 7070 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
7071 | BFD_ASSERT (s != NULL); |
7072 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
7073 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 7074 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7075 | if (s->contents == NULL) |
7076 | return FALSE; | |
7077 | ||
7078 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
7079 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
7080 | s->contents + hash_entry_size); | |
7081 | } | |
7082 | ||
7083 | if (info->emit_gnu_hash) | |
7084 | { | |
7085 | size_t i, cnt; | |
7086 | unsigned char *contents; | |
7087 | struct collect_gnu_hash_codes cinfo; | |
7088 | bfd_size_type amt; | |
7089 | size_t bucketcount; | |
7090 | ||
7091 | memset (&cinfo, 0, sizeof (cinfo)); | |
7092 | ||
7093 | /* Compute the hash values for all exported symbols. At the same | |
7094 | time store the values in an array so that we could use them for | |
7095 | optimizations. */ | |
7096 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 7097 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7098 | if (cinfo.hashcodes == NULL) |
7099 | return FALSE; | |
7100 | ||
7101 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
7102 | cinfo.min_dynindx = -1; | |
7103 | cinfo.output_bfd = output_bfd; | |
7104 | cinfo.bed = bed; | |
7105 | ||
7106 | /* Put all hash values in HASHCODES. */ | |
7107 | elf_link_hash_traverse (elf_hash_table (info), | |
7108 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 7109 | if (cinfo.error) |
4dd07732 AM |
7110 | { |
7111 | free (cinfo.hashcodes); | |
7112 | return FALSE; | |
7113 | } | |
fdc90cb4 JJ |
7114 | |
7115 | bucketcount | |
7116 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
7117 | ||
7118 | if (bucketcount == 0) | |
7119 | { | |
7120 | free (cinfo.hashcodes); | |
7121 | return FALSE; | |
7122 | } | |
7123 | ||
3d4d4302 | 7124 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
7125 | BFD_ASSERT (s != NULL); |
7126 | ||
7127 | if (cinfo.nsyms == 0) | |
7128 | { | |
7129 | /* Empty .gnu.hash section is special. */ | |
7130 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
7131 | free (cinfo.hashcodes); | |
7132 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 7133 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7134 | if (contents == NULL) |
7135 | return FALSE; | |
7136 | s->contents = contents; | |
7137 | /* 1 empty bucket. */ | |
7138 | bfd_put_32 (output_bfd, 1, contents); | |
7139 | /* SYMIDX above the special symbol 0. */ | |
7140 | bfd_put_32 (output_bfd, 1, contents + 4); | |
7141 | /* Just one word for bitmask. */ | |
7142 | bfd_put_32 (output_bfd, 1, contents + 8); | |
7143 | /* Only hash fn bloom filter. */ | |
7144 | bfd_put_32 (output_bfd, 0, contents + 12); | |
7145 | /* No hashes are valid - empty bitmask. */ | |
7146 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
7147 | /* No hashes in the only bucket. */ | |
7148 | bfd_put_32 (output_bfd, 0, | |
7149 | contents + 16 + bed->s->arch_size / 8); | |
7150 | } | |
7151 | else | |
7152 | { | |
9e6619e2 | 7153 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 7154 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 7155 | |
9e6619e2 AM |
7156 | x = cinfo.nsyms; |
7157 | maskbitslog2 = 1; | |
7158 | while ((x >>= 1) != 0) | |
7159 | ++maskbitslog2; | |
fdc90cb4 JJ |
7160 | if (maskbitslog2 < 3) |
7161 | maskbitslog2 = 5; | |
7162 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
7163 | maskbitslog2 = maskbitslog2 + 3; | |
7164 | else | |
7165 | maskbitslog2 = maskbitslog2 + 2; | |
7166 | if (bed->s->arch_size == 64) | |
7167 | { | |
7168 | if (maskbitslog2 == 5) | |
7169 | maskbitslog2 = 6; | |
7170 | cinfo.shift1 = 6; | |
7171 | } | |
7172 | else | |
7173 | cinfo.shift1 = 5; | |
7174 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 7175 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
7176 | cinfo.maskbits = 1 << maskbitslog2; |
7177 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
7178 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
7179 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 7180 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
7181 | if (cinfo.bitmask == NULL) |
7182 | { | |
7183 | free (cinfo.hashcodes); | |
7184 | return FALSE; | |
7185 | } | |
7186 | ||
a50b1753 | 7187 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
7188 | cinfo.indx = cinfo.counts + bucketcount; |
7189 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
7190 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
7191 | ||
7192 | /* Determine how often each hash bucket is used. */ | |
7193 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
7194 | for (i = 0; i < cinfo.nsyms; ++i) | |
7195 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
7196 | ||
7197 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
7198 | if (cinfo.counts[i] != 0) | |
7199 | { | |
7200 | cinfo.indx[i] = cnt; | |
7201 | cnt += cinfo.counts[i]; | |
7202 | } | |
7203 | BFD_ASSERT (cnt == dynsymcount); | |
7204 | cinfo.bucketcount = bucketcount; | |
7205 | cinfo.local_indx = cinfo.min_dynindx; | |
7206 | ||
7207 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
7208 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 7209 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7210 | if (contents == NULL) |
7211 | { | |
7212 | free (cinfo.bitmask); | |
7213 | free (cinfo.hashcodes); | |
7214 | return FALSE; | |
7215 | } | |
7216 | ||
7217 | s->contents = contents; | |
7218 | bfd_put_32 (output_bfd, bucketcount, contents); | |
7219 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
7220 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
7221 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
7222 | contents += 16 + cinfo.maskbits / 8; | |
7223 | ||
7224 | for (i = 0; i < bucketcount; ++i) | |
7225 | { | |
7226 | if (cinfo.counts[i] == 0) | |
7227 | bfd_put_32 (output_bfd, 0, contents); | |
7228 | else | |
7229 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
7230 | contents += 4; | |
7231 | } | |
7232 | ||
7233 | cinfo.contents = contents; | |
7234 | ||
7235 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
7236 | elf_link_hash_traverse (elf_hash_table (info), | |
7237 | elf_renumber_gnu_hash_syms, &cinfo); | |
7238 | ||
7239 | contents = s->contents + 16; | |
7240 | for (i = 0; i < maskwords; ++i) | |
7241 | { | |
7242 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
7243 | contents); | |
7244 | contents += bed->s->arch_size / 8; | |
7245 | } | |
7246 | ||
7247 | free (cinfo.bitmask); | |
7248 | free (cinfo.hashcodes); | |
7249 | } | |
7250 | } | |
5a580b3a | 7251 | |
3d4d4302 | 7252 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
7253 | BFD_ASSERT (s != NULL); |
7254 | ||
4ad4eba5 | 7255 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 7256 | |
eea6121a | 7257 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
7258 | |
7259 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
7260 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
7261 | return FALSE; | |
7262 | } | |
7263 | ||
7264 | return TRUE; | |
7265 | } | |
4d269e42 | 7266 | \f |
4d269e42 AM |
7267 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
7268 | ||
7269 | static void | |
7270 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
7271 | asection *sec) | |
7272 | { | |
dbaa2011 AM |
7273 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
7274 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
7275 | } |
7276 | ||
7277 | /* Finish SHF_MERGE section merging. */ | |
7278 | ||
7279 | bfd_boolean | |
630993ec | 7280 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
7281 | { |
7282 | bfd *ibfd; | |
7283 | asection *sec; | |
7284 | ||
7285 | if (!is_elf_hash_table (info->hash)) | |
7286 | return FALSE; | |
7287 | ||
c72f2fb2 | 7288 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
7289 | if ((ibfd->flags & DYNAMIC) == 0 |
7290 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
7291 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
7292 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
7293 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
7294 | if ((sec->flags & SEC_MERGE) != 0 | |
7295 | && !bfd_is_abs_section (sec->output_section)) | |
7296 | { | |
7297 | struct bfd_elf_section_data *secdata; | |
7298 | ||
7299 | secdata = elf_section_data (sec); | |
630993ec | 7300 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
7301 | &elf_hash_table (info)->merge_info, |
7302 | sec, &secdata->sec_info)) | |
7303 | return FALSE; | |
7304 | else if (secdata->sec_info) | |
dbaa2011 | 7305 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7306 | } |
7307 | ||
7308 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7309 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
7310 | merge_sections_remove_hook); |
7311 | return TRUE; | |
7312 | } | |
7313 | ||
7314 | /* Create an entry in an ELF linker hash table. */ | |
7315 | ||
7316 | struct bfd_hash_entry * | |
7317 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7318 | struct bfd_hash_table *table, | |
7319 | const char *string) | |
7320 | { | |
7321 | /* Allocate the structure if it has not already been allocated by a | |
7322 | subclass. */ | |
7323 | if (entry == NULL) | |
7324 | { | |
a50b1753 | 7325 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7326 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7327 | if (entry == NULL) |
7328 | return entry; | |
7329 | } | |
7330 | ||
7331 | /* Call the allocation method of the superclass. */ | |
7332 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7333 | if (entry != NULL) | |
7334 | { | |
7335 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7336 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7337 | ||
7338 | /* Set local fields. */ | |
7339 | ret->indx = -1; | |
7340 | ret->dynindx = -1; | |
7341 | ret->got = htab->init_got_refcount; | |
7342 | ret->plt = htab->init_plt_refcount; | |
7343 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7344 | - offsetof (struct elf_link_hash_entry, size))); | |
7345 | /* Assume that we have been called by a non-ELF symbol reader. | |
7346 | This flag is then reset by the code which reads an ELF input | |
7347 | file. This ensures that a symbol created by a non-ELF symbol | |
7348 | reader will have the flag set correctly. */ | |
7349 | ret->non_elf = 1; | |
7350 | } | |
7351 | ||
7352 | return entry; | |
7353 | } | |
7354 | ||
7355 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7356 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7357 | ||
7358 | void | |
7359 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7360 | struct elf_link_hash_entry *dir, | |
7361 | struct elf_link_hash_entry *ind) | |
7362 | { | |
7363 | struct elf_link_hash_table *htab; | |
7364 | ||
7365 | /* Copy down any references that we may have already seen to the | |
e81830c5 | 7366 | symbol which just became indirect. */ |
4d269e42 | 7367 | |
422f1182 | 7368 | if (dir->versioned != versioned_hidden) |
e81830c5 AM |
7369 | dir->ref_dynamic |= ind->ref_dynamic; |
7370 | dir->ref_regular |= ind->ref_regular; | |
7371 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7372 | dir->non_got_ref |= ind->non_got_ref; | |
7373 | dir->needs_plt |= ind->needs_plt; | |
7374 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
4d269e42 AM |
7375 | |
7376 | if (ind->root.type != bfd_link_hash_indirect) | |
7377 | return; | |
7378 | ||
7379 | /* Copy over the global and procedure linkage table refcount entries. | |
7380 | These may have been already set up by a check_relocs routine. */ | |
7381 | htab = elf_hash_table (info); | |
7382 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7383 | { | |
7384 | if (dir->got.refcount < 0) | |
7385 | dir->got.refcount = 0; | |
7386 | dir->got.refcount += ind->got.refcount; | |
7387 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7388 | } | |
7389 | ||
7390 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7391 | { | |
7392 | if (dir->plt.refcount < 0) | |
7393 | dir->plt.refcount = 0; | |
7394 | dir->plt.refcount += ind->plt.refcount; | |
7395 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7396 | } | |
7397 | ||
7398 | if (ind->dynindx != -1) | |
7399 | { | |
7400 | if (dir->dynindx != -1) | |
7401 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7402 | dir->dynindx = ind->dynindx; | |
7403 | dir->dynstr_index = ind->dynstr_index; | |
7404 | ind->dynindx = -1; | |
7405 | ind->dynstr_index = 0; | |
7406 | } | |
7407 | } | |
7408 | ||
7409 | void | |
7410 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7411 | struct elf_link_hash_entry *h, | |
7412 | bfd_boolean force_local) | |
7413 | { | |
3aa14d16 L |
7414 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7415 | if (h->type != STT_GNU_IFUNC) | |
7416 | { | |
7417 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7418 | h->needs_plt = 0; | |
7419 | } | |
4d269e42 AM |
7420 | if (force_local) |
7421 | { | |
7422 | h->forced_local = 1; | |
7423 | if (h->dynindx != -1) | |
7424 | { | |
4d269e42 AM |
7425 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, |
7426 | h->dynstr_index); | |
641338d8 AM |
7427 | h->dynindx = -1; |
7428 | h->dynstr_index = 0; | |
4d269e42 AM |
7429 | } |
7430 | } | |
7431 | } | |
7432 | ||
34a87bb0 L |
7433 | /* Hide a symbol. */ |
7434 | ||
7435 | void | |
7436 | _bfd_elf_link_hide_symbol (bfd *output_bfd, | |
7437 | struct bfd_link_info *info, | |
7438 | struct bfd_link_hash_entry *h) | |
7439 | { | |
7440 | if (is_elf_hash_table (info->hash)) | |
7441 | { | |
7442 | const struct elf_backend_data *bed | |
7443 | = get_elf_backend_data (output_bfd); | |
7444 | struct elf_link_hash_entry *eh | |
7445 | = (struct elf_link_hash_entry *) h; | |
7446 | bed->elf_backend_hide_symbol (info, eh, TRUE); | |
7447 | eh->def_dynamic = 0; | |
7448 | eh->ref_dynamic = 0; | |
7449 | eh->dynamic_def = 0; | |
7450 | } | |
7451 | } | |
7452 | ||
7bf52ea2 AM |
7453 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7454 | caller. */ | |
4d269e42 AM |
7455 | |
7456 | bfd_boolean | |
7457 | _bfd_elf_link_hash_table_init | |
7458 | (struct elf_link_hash_table *table, | |
7459 | bfd *abfd, | |
7460 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7461 | struct bfd_hash_table *, | |
7462 | const char *), | |
4dfe6ac6 NC |
7463 | unsigned int entsize, |
7464 | enum elf_target_id target_id) | |
4d269e42 AM |
7465 | { |
7466 | bfd_boolean ret; | |
7467 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7468 | ||
4d269e42 AM |
7469 | table->init_got_refcount.refcount = can_refcount - 1; |
7470 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7471 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7472 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7473 | /* The first dynamic symbol is a dummy. */ | |
7474 | table->dynsymcount = 1; | |
7475 | ||
7476 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7477 | |
4d269e42 | 7478 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7479 | table->hash_table_id = target_id; |
4d269e42 AM |
7480 | |
7481 | return ret; | |
7482 | } | |
7483 | ||
7484 | /* Create an ELF linker hash table. */ | |
7485 | ||
7486 | struct bfd_link_hash_table * | |
7487 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7488 | { | |
7489 | struct elf_link_hash_table *ret; | |
7490 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7491 | ||
7bf52ea2 | 7492 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7493 | if (ret == NULL) |
7494 | return NULL; | |
7495 | ||
7496 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7497 | sizeof (struct elf_link_hash_entry), |
7498 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7499 | { |
7500 | free (ret); | |
7501 | return NULL; | |
7502 | } | |
d495ab0d | 7503 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7504 | |
7505 | return &ret->root; | |
7506 | } | |
7507 | ||
9f7c3e5e AM |
7508 | /* Destroy an ELF linker hash table. */ |
7509 | ||
7510 | void | |
d495ab0d | 7511 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7512 | { |
d495ab0d AM |
7513 | struct elf_link_hash_table *htab; |
7514 | ||
7515 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7516 | if (htab->dynstr != NULL) |
7517 | _bfd_elf_strtab_free (htab->dynstr); | |
7518 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7519 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7520 | } |
7521 | ||
4d269e42 AM |
7522 | /* This is a hook for the ELF emulation code in the generic linker to |
7523 | tell the backend linker what file name to use for the DT_NEEDED | |
7524 | entry for a dynamic object. */ | |
7525 | ||
7526 | void | |
7527 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7528 | { | |
7529 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7530 | && bfd_get_format (abfd) == bfd_object) | |
7531 | elf_dt_name (abfd) = name; | |
7532 | } | |
7533 | ||
7534 | int | |
7535 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7536 | { | |
7537 | int lib_class; | |
7538 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7539 | && bfd_get_format (abfd) == bfd_object) | |
7540 | lib_class = elf_dyn_lib_class (abfd); | |
7541 | else | |
7542 | lib_class = 0; | |
7543 | return lib_class; | |
7544 | } | |
7545 | ||
7546 | void | |
7547 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7548 | { | |
7549 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7550 | && bfd_get_format (abfd) == bfd_object) | |
7551 | elf_dyn_lib_class (abfd) = lib_class; | |
7552 | } | |
7553 | ||
7554 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7555 | the linker ELF emulation code. */ | |
7556 | ||
7557 | struct bfd_link_needed_list * | |
7558 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7559 | struct bfd_link_info *info) | |
7560 | { | |
7561 | if (! is_elf_hash_table (info->hash)) | |
7562 | return NULL; | |
7563 | return elf_hash_table (info)->needed; | |
7564 | } | |
7565 | ||
7566 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7567 | hook for the linker ELF emulation code. */ | |
7568 | ||
7569 | struct bfd_link_needed_list * | |
7570 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7571 | struct bfd_link_info *info) | |
7572 | { | |
7573 | if (! is_elf_hash_table (info->hash)) | |
7574 | return NULL; | |
7575 | return elf_hash_table (info)->runpath; | |
7576 | } | |
7577 | ||
7578 | /* Get the name actually used for a dynamic object for a link. This | |
7579 | is the SONAME entry if there is one. Otherwise, it is the string | |
7580 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7581 | ||
7582 | const char * | |
7583 | bfd_elf_get_dt_soname (bfd *abfd) | |
7584 | { | |
7585 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7586 | && bfd_get_format (abfd) == bfd_object) | |
7587 | return elf_dt_name (abfd); | |
7588 | return NULL; | |
7589 | } | |
7590 | ||
7591 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7592 | the ELF linker emulation code. */ | |
7593 | ||
7594 | bfd_boolean | |
7595 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7596 | struct bfd_link_needed_list **pneeded) | |
7597 | { | |
7598 | asection *s; | |
7599 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7600 | unsigned int elfsec; |
4d269e42 AM |
7601 | unsigned long shlink; |
7602 | bfd_byte *extdyn, *extdynend; | |
7603 | size_t extdynsize; | |
7604 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7605 | ||
7606 | *pneeded = NULL; | |
7607 | ||
7608 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7609 | || bfd_get_format (abfd) != bfd_object) | |
7610 | return TRUE; | |
7611 | ||
7612 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7613 | if (s == NULL || s->size == 0) | |
7614 | return TRUE; | |
7615 | ||
7616 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7617 | goto error_return; | |
7618 | ||
7619 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7620 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7621 | goto error_return; |
7622 | ||
7623 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7624 | |
4d269e42 AM |
7625 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7626 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7627 | ||
7628 | extdyn = dynbuf; | |
7629 | extdynend = extdyn + s->size; | |
7630 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7631 | { | |
7632 | Elf_Internal_Dyn dyn; | |
7633 | ||
7634 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7635 | ||
7636 | if (dyn.d_tag == DT_NULL) | |
7637 | break; | |
7638 | ||
7639 | if (dyn.d_tag == DT_NEEDED) | |
7640 | { | |
7641 | const char *string; | |
7642 | struct bfd_link_needed_list *l; | |
7643 | unsigned int tagv = dyn.d_un.d_val; | |
7644 | bfd_size_type amt; | |
7645 | ||
7646 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7647 | if (string == NULL) | |
7648 | goto error_return; | |
7649 | ||
7650 | amt = sizeof *l; | |
a50b1753 | 7651 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7652 | if (l == NULL) |
7653 | goto error_return; | |
7654 | ||
7655 | l->by = abfd; | |
7656 | l->name = string; | |
7657 | l->next = *pneeded; | |
7658 | *pneeded = l; | |
7659 | } | |
7660 | } | |
7661 | ||
7662 | free (dynbuf); | |
7663 | ||
7664 | return TRUE; | |
7665 | ||
7666 | error_return: | |
7667 | if (dynbuf != NULL) | |
7668 | free (dynbuf); | |
7669 | return FALSE; | |
7670 | } | |
7671 | ||
7672 | struct elf_symbuf_symbol | |
7673 | { | |
7674 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7675 | unsigned char st_info; /* Type and binding attributes */ | |
7676 | unsigned char st_other; /* Visibilty, and target specific */ | |
7677 | }; | |
7678 | ||
7679 | struct elf_symbuf_head | |
7680 | { | |
7681 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7682 | size_t count; |
4d269e42 AM |
7683 | unsigned int st_shndx; |
7684 | }; | |
7685 | ||
7686 | struct elf_symbol | |
7687 | { | |
7688 | union | |
7689 | { | |
7690 | Elf_Internal_Sym *isym; | |
7691 | struct elf_symbuf_symbol *ssym; | |
7692 | } u; | |
7693 | const char *name; | |
7694 | }; | |
7695 | ||
7696 | /* Sort references to symbols by ascending section number. */ | |
7697 | ||
7698 | static int | |
7699 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7700 | { | |
7701 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7702 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7703 | ||
7704 | return s1->st_shndx - s2->st_shndx; | |
7705 | } | |
7706 | ||
7707 | static int | |
7708 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7709 | { | |
7710 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7711 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7712 | return strcmp (s1->name, s2->name); | |
7713 | } | |
7714 | ||
7715 | static struct elf_symbuf_head * | |
ef53be89 | 7716 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7717 | { |
14b1c01e | 7718 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7719 | struct elf_symbuf_symbol *ssym; |
7720 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7721 | size_t i, shndx_count, total_size; |
4d269e42 | 7722 | |
a50b1753 | 7723 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7724 | if (indbuf == NULL) |
7725 | return NULL; | |
7726 | ||
7727 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7728 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7729 | *ind++ = &isymbuf[i]; | |
7730 | indbufend = ind; | |
7731 | ||
7732 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7733 | elf_sort_elf_symbol); | |
7734 | ||
7735 | shndx_count = 0; | |
7736 | if (indbufend > indbuf) | |
7737 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7738 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7739 | shndx_count++; | |
7740 | ||
3ae181ee L |
7741 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7742 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7743 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7744 | if (ssymbuf == NULL) |
7745 | { | |
7746 | free (indbuf); | |
7747 | return NULL; | |
7748 | } | |
7749 | ||
3ae181ee | 7750 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7751 | ssymbuf->ssym = NULL; |
7752 | ssymbuf->count = shndx_count; | |
7753 | ssymbuf->st_shndx = 0; | |
7754 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7755 | { | |
7756 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7757 | { | |
7758 | ssymhead++; | |
7759 | ssymhead->ssym = ssym; | |
7760 | ssymhead->count = 0; | |
7761 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7762 | } | |
7763 | ssym->st_name = (*ind)->st_name; | |
7764 | ssym->st_info = (*ind)->st_info; | |
7765 | ssym->st_other = (*ind)->st_other; | |
7766 | ssymhead->count++; | |
7767 | } | |
ef53be89 | 7768 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7769 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7770 | == total_size)); | |
4d269e42 AM |
7771 | |
7772 | free (indbuf); | |
7773 | return ssymbuf; | |
7774 | } | |
7775 | ||
7776 | /* Check if 2 sections define the same set of local and global | |
7777 | symbols. */ | |
7778 | ||
8f317e31 | 7779 | static bfd_boolean |
4d269e42 AM |
7780 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7781 | struct bfd_link_info *info) | |
7782 | { | |
7783 | bfd *bfd1, *bfd2; | |
7784 | const struct elf_backend_data *bed1, *bed2; | |
7785 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7786 | size_t symcount1, symcount2; |
4d269e42 AM |
7787 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7788 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7789 | Elf_Internal_Sym *isym, *isymend; | |
7790 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7791 | size_t count1, count2, i; |
cb33740c | 7792 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7793 | bfd_boolean result; |
7794 | ||
7795 | bfd1 = sec1->owner; | |
7796 | bfd2 = sec2->owner; | |
7797 | ||
4d269e42 AM |
7798 | /* Both sections have to be in ELF. */ |
7799 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7800 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7801 | return FALSE; | |
7802 | ||
7803 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7804 | return FALSE; | |
7805 | ||
4d269e42 AM |
7806 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7807 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7808 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7809 | return FALSE; |
7810 | ||
7811 | bed1 = get_elf_backend_data (bfd1); | |
7812 | bed2 = get_elf_backend_data (bfd2); | |
7813 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7814 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7815 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7816 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7817 | ||
7818 | if (symcount1 == 0 || symcount2 == 0) | |
7819 | return FALSE; | |
7820 | ||
7821 | result = FALSE; | |
7822 | isymbuf1 = NULL; | |
7823 | isymbuf2 = NULL; | |
a50b1753 NC |
7824 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7825 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7826 | |
7827 | if (ssymbuf1 == NULL) | |
7828 | { | |
7829 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7830 | NULL, NULL, NULL); | |
7831 | if (isymbuf1 == NULL) | |
7832 | goto done; | |
7833 | ||
7834 | if (!info->reduce_memory_overheads) | |
7835 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7836 | = elf_create_symbuf (symcount1, isymbuf1); | |
7837 | } | |
7838 | ||
7839 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7840 | { | |
7841 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7842 | NULL, NULL, NULL); | |
7843 | if (isymbuf2 == NULL) | |
7844 | goto done; | |
7845 | ||
7846 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7847 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7848 | = elf_create_symbuf (symcount2, isymbuf2); | |
7849 | } | |
7850 | ||
7851 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7852 | { | |
7853 | /* Optimized faster version. */ | |
ef53be89 | 7854 | size_t lo, hi, mid; |
4d269e42 AM |
7855 | struct elf_symbol *symp; |
7856 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7857 | ||
7858 | lo = 0; | |
7859 | hi = ssymbuf1->count; | |
7860 | ssymbuf1++; | |
7861 | count1 = 0; | |
7862 | while (lo < hi) | |
7863 | { | |
7864 | mid = (lo + hi) / 2; | |
cb33740c | 7865 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7866 | hi = mid; |
cb33740c | 7867 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7868 | lo = mid + 1; |
7869 | else | |
7870 | { | |
7871 | count1 = ssymbuf1[mid].count; | |
7872 | ssymbuf1 += mid; | |
7873 | break; | |
7874 | } | |
7875 | } | |
7876 | ||
7877 | lo = 0; | |
7878 | hi = ssymbuf2->count; | |
7879 | ssymbuf2++; | |
7880 | count2 = 0; | |
7881 | while (lo < hi) | |
7882 | { | |
7883 | mid = (lo + hi) / 2; | |
cb33740c | 7884 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7885 | hi = mid; |
cb33740c | 7886 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7887 | lo = mid + 1; |
7888 | else | |
7889 | { | |
7890 | count2 = ssymbuf2[mid].count; | |
7891 | ssymbuf2 += mid; | |
7892 | break; | |
7893 | } | |
7894 | } | |
7895 | ||
7896 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7897 | goto done; | |
7898 | ||
ca4be51c AM |
7899 | symtable1 |
7900 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
7901 | symtable2 | |
7902 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
7903 | if (symtable1 == NULL || symtable2 == NULL) |
7904 | goto done; | |
7905 | ||
7906 | symp = symtable1; | |
7907 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
7908 | ssym < ssymend; ssym++, symp++) | |
7909 | { | |
7910 | symp->u.ssym = ssym; | |
7911 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
7912 | hdr1->sh_link, | |
7913 | ssym->st_name); | |
7914 | } | |
7915 | ||
7916 | symp = symtable2; | |
7917 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
7918 | ssym < ssymend; ssym++, symp++) | |
7919 | { | |
7920 | symp->u.ssym = ssym; | |
7921 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
7922 | hdr2->sh_link, | |
7923 | ssym->st_name); | |
7924 | } | |
7925 | ||
7926 | /* Sort symbol by name. */ | |
7927 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7928 | elf_sym_name_compare); | |
7929 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7930 | elf_sym_name_compare); | |
7931 | ||
7932 | for (i = 0; i < count1; i++) | |
7933 | /* Two symbols must have the same binding, type and name. */ | |
7934 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
7935 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
7936 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7937 | goto done; | |
7938 | ||
7939 | result = TRUE; | |
7940 | goto done; | |
7941 | } | |
7942 | ||
a50b1753 NC |
7943 | symtable1 = (struct elf_symbol *) |
7944 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
7945 | symtable2 = (struct elf_symbol *) | |
7946 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
7947 | if (symtable1 == NULL || symtable2 == NULL) |
7948 | goto done; | |
7949 | ||
7950 | /* Count definitions in the section. */ | |
7951 | count1 = 0; | |
7952 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 7953 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
7954 | symtable1[count1++].u.isym = isym; |
7955 | ||
7956 | count2 = 0; | |
7957 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 7958 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
7959 | symtable2[count2++].u.isym = isym; |
7960 | ||
7961 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
7962 | goto done; | |
7963 | ||
7964 | for (i = 0; i < count1; i++) | |
7965 | symtable1[i].name | |
7966 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
7967 | symtable1[i].u.isym->st_name); | |
7968 | ||
7969 | for (i = 0; i < count2; i++) | |
7970 | symtable2[i].name | |
7971 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
7972 | symtable2[i].u.isym->st_name); | |
7973 | ||
7974 | /* Sort symbol by name. */ | |
7975 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
7976 | elf_sym_name_compare); | |
7977 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
7978 | elf_sym_name_compare); | |
7979 | ||
7980 | for (i = 0; i < count1; i++) | |
7981 | /* Two symbols must have the same binding, type and name. */ | |
7982 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
7983 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
7984 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
7985 | goto done; | |
7986 | ||
7987 | result = TRUE; | |
7988 | ||
7989 | done: | |
7990 | if (symtable1) | |
7991 | free (symtable1); | |
7992 | if (symtable2) | |
7993 | free (symtable2); | |
7994 | if (isymbuf1) | |
7995 | free (isymbuf1); | |
7996 | if (isymbuf2) | |
7997 | free (isymbuf2); | |
7998 | ||
7999 | return result; | |
8000 | } | |
8001 | ||
8002 | /* Return TRUE if 2 section types are compatible. */ | |
8003 | ||
8004 | bfd_boolean | |
8005 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
8006 | bfd *bbfd, const asection *bsec) | |
8007 | { | |
8008 | if (asec == NULL | |
8009 | || bsec == NULL | |
8010 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
8011 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
8012 | return TRUE; | |
8013 | ||
8014 | return elf_section_type (asec) == elf_section_type (bsec); | |
8015 | } | |
8016 | \f | |
c152c796 AM |
8017 | /* Final phase of ELF linker. */ |
8018 | ||
8019 | /* A structure we use to avoid passing large numbers of arguments. */ | |
8020 | ||
8021 | struct elf_final_link_info | |
8022 | { | |
8023 | /* General link information. */ | |
8024 | struct bfd_link_info *info; | |
8025 | /* Output BFD. */ | |
8026 | bfd *output_bfd; | |
8027 | /* Symbol string table. */ | |
ef10c3ac | 8028 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
8029 | /* .hash section. */ |
8030 | asection *hash_sec; | |
8031 | /* symbol version section (.gnu.version). */ | |
8032 | asection *symver_sec; | |
8033 | /* Buffer large enough to hold contents of any section. */ | |
8034 | bfd_byte *contents; | |
8035 | /* Buffer large enough to hold external relocs of any section. */ | |
8036 | void *external_relocs; | |
8037 | /* Buffer large enough to hold internal relocs of any section. */ | |
8038 | Elf_Internal_Rela *internal_relocs; | |
8039 | /* Buffer large enough to hold external local symbols of any input | |
8040 | BFD. */ | |
8041 | bfd_byte *external_syms; | |
8042 | /* And a buffer for symbol section indices. */ | |
8043 | Elf_External_Sym_Shndx *locsym_shndx; | |
8044 | /* Buffer large enough to hold internal local symbols of any input | |
8045 | BFD. */ | |
8046 | Elf_Internal_Sym *internal_syms; | |
8047 | /* Array large enough to hold a symbol index for each local symbol | |
8048 | of any input BFD. */ | |
8049 | long *indices; | |
8050 | /* Array large enough to hold a section pointer for each local | |
8051 | symbol of any input BFD. */ | |
8052 | asection **sections; | |
ef10c3ac | 8053 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 8054 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
8055 | /* Number of STT_FILE syms seen. */ |
8056 | size_t filesym_count; | |
c152c796 AM |
8057 | }; |
8058 | ||
8059 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
8060 | ||
8061 | struct elf_outext_info | |
8062 | { | |
8063 | bfd_boolean failed; | |
8064 | bfd_boolean localsyms; | |
34a79995 | 8065 | bfd_boolean file_sym_done; |
8b127cbc | 8066 | struct elf_final_link_info *flinfo; |
c152c796 AM |
8067 | }; |
8068 | ||
d9352518 DB |
8069 | |
8070 | /* Support for evaluating a complex relocation. | |
8071 | ||
8072 | Complex relocations are generalized, self-describing relocations. The | |
8073 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 8074 | relocations themselves. |
d9352518 DB |
8075 | |
8076 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
8077 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
8078 | information (start bit, end bit, word width, etc) into the addend. This | |
8079 | information is extracted from CGEN-generated operand tables within gas. | |
8080 | ||
8081 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
8082 | internal) representing prefix-notation expressions, including but not | |
8083 | limited to those sorts of expressions normally encoded as addends in the | |
8084 | addend field. The symbol mangling format is: | |
8085 | ||
8086 | <node> := <literal> | |
07d6d2b8 AM |
8087 | | <unary-operator> ':' <node> |
8088 | | <binary-operator> ':' <node> ':' <node> | |
d9352518 DB |
8089 | ; |
8090 | ||
8091 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
07d6d2b8 | 8092 | | 'S' <digits=N> ':' <N character section name> |
d9352518 DB |
8093 | | '#' <hexdigits> |
8094 | ; | |
8095 | ||
8096 | <binary-operator> := as in C | |
8097 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
8098 | ||
8099 | static void | |
a0c8462f AM |
8100 | set_symbol_value (bfd *bfd_with_globals, |
8101 | Elf_Internal_Sym *isymbuf, | |
8102 | size_t locsymcount, | |
8103 | size_t symidx, | |
8104 | bfd_vma val) | |
d9352518 | 8105 | { |
8977835c AM |
8106 | struct elf_link_hash_entry **sym_hashes; |
8107 | struct elf_link_hash_entry *h; | |
8108 | size_t extsymoff = locsymcount; | |
d9352518 | 8109 | |
8977835c | 8110 | if (symidx < locsymcount) |
d9352518 | 8111 | { |
8977835c AM |
8112 | Elf_Internal_Sym *sym; |
8113 | ||
8114 | sym = isymbuf + symidx; | |
8115 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
8116 | { | |
8117 | /* It is a local symbol: move it to the | |
8118 | "absolute" section and give it a value. */ | |
8119 | sym->st_shndx = SHN_ABS; | |
8120 | sym->st_value = val; | |
8121 | return; | |
8122 | } | |
8123 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
8124 | extsymoff = 0; | |
d9352518 | 8125 | } |
8977835c AM |
8126 | |
8127 | /* It is a global symbol: set its link type | |
8128 | to "defined" and give it a value. */ | |
8129 | ||
8130 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
8131 | h = sym_hashes [symidx - extsymoff]; | |
8132 | while (h->root.type == bfd_link_hash_indirect | |
8133 | || h->root.type == bfd_link_hash_warning) | |
8134 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8135 | h->root.type = bfd_link_hash_defined; | |
8136 | h->root.u.def.value = val; | |
8137 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
8138 | } |
8139 | ||
a0c8462f AM |
8140 | static bfd_boolean |
8141 | resolve_symbol (const char *name, | |
8142 | bfd *input_bfd, | |
8b127cbc | 8143 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8144 | bfd_vma *result, |
8145 | Elf_Internal_Sym *isymbuf, | |
8146 | size_t locsymcount) | |
d9352518 | 8147 | { |
a0c8462f AM |
8148 | Elf_Internal_Sym *sym; |
8149 | struct bfd_link_hash_entry *global_entry; | |
8150 | const char *candidate = NULL; | |
8151 | Elf_Internal_Shdr *symtab_hdr; | |
8152 | size_t i; | |
8153 | ||
d9352518 DB |
8154 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
8155 | ||
8156 | for (i = 0; i < locsymcount; ++ i) | |
8157 | { | |
8977835c | 8158 | sym = isymbuf + i; |
d9352518 DB |
8159 | |
8160 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
8161 | continue; | |
8162 | ||
8163 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
8164 | symtab_hdr->sh_link, | |
8165 | sym->st_name); | |
8166 | #ifdef DEBUG | |
0f02bbd9 AM |
8167 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
8168 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
8169 | #endif |
8170 | if (candidate && strcmp (candidate, name) == 0) | |
8171 | { | |
8b127cbc | 8172 | asection *sec = flinfo->sections [i]; |
d9352518 | 8173 | |
0f02bbd9 AM |
8174 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
8175 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 8176 | #ifdef DEBUG |
0f02bbd9 AM |
8177 | printf ("Found symbol with value %8.8lx\n", |
8178 | (unsigned long) *result); | |
d9352518 DB |
8179 | #endif |
8180 | return TRUE; | |
8181 | } | |
8182 | } | |
8183 | ||
8184 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 8185 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 8186 | FALSE, FALSE, TRUE); |
d9352518 DB |
8187 | if (!global_entry) |
8188 | return FALSE; | |
a0c8462f | 8189 | |
d9352518 DB |
8190 | if (global_entry->type == bfd_link_hash_defined |
8191 | || global_entry->type == bfd_link_hash_defweak) | |
8192 | { | |
a0c8462f AM |
8193 | *result = (global_entry->u.def.value |
8194 | + global_entry->u.def.section->output_section->vma | |
8195 | + global_entry->u.def.section->output_offset); | |
d9352518 | 8196 | #ifdef DEBUG |
0f02bbd9 AM |
8197 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
8198 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
8199 | #endif |
8200 | return TRUE; | |
a0c8462f | 8201 | } |
d9352518 | 8202 | |
d9352518 DB |
8203 | return FALSE; |
8204 | } | |
8205 | ||
37b01f6a DG |
8206 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
8207 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
8208 | names like "foo.end" which is the end address of section "foo". */ | |
07d6d2b8 | 8209 | |
d9352518 | 8210 | static bfd_boolean |
a0c8462f AM |
8211 | resolve_section (const char *name, |
8212 | asection *sections, | |
37b01f6a DG |
8213 | bfd_vma *result, |
8214 | bfd * abfd) | |
d9352518 | 8215 | { |
a0c8462f AM |
8216 | asection *curr; |
8217 | unsigned int len; | |
d9352518 | 8218 | |
a0c8462f | 8219 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8220 | if (strcmp (curr->name, name) == 0) |
8221 | { | |
8222 | *result = curr->vma; | |
8223 | return TRUE; | |
8224 | } | |
8225 | ||
8226 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 8227 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 8228 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8229 | { |
8230 | len = strlen (curr->name); | |
a0c8462f | 8231 | if (len > strlen (name)) |
d9352518 DB |
8232 | continue; |
8233 | ||
8234 | if (strncmp (curr->name, name, len) == 0) | |
8235 | { | |
8236 | if (strncmp (".end", name + len, 4) == 0) | |
8237 | { | |
37b01f6a | 8238 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
8239 | return TRUE; |
8240 | } | |
8241 | ||
8242 | /* Insert more pseudo-section names here, if you like. */ | |
8243 | } | |
8244 | } | |
a0c8462f | 8245 | |
d9352518 DB |
8246 | return FALSE; |
8247 | } | |
8248 | ||
8249 | static void | |
a0c8462f | 8250 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 8251 | { |
695344c0 | 8252 | /* xgettext:c-format */ |
a0c8462f AM |
8253 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
8254 | reftype, name); | |
d9352518 DB |
8255 | } |
8256 | ||
8257 | static bfd_boolean | |
a0c8462f AM |
8258 | eval_symbol (bfd_vma *result, |
8259 | const char **symp, | |
8260 | bfd *input_bfd, | |
8b127cbc | 8261 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8262 | bfd_vma dot, |
8263 | Elf_Internal_Sym *isymbuf, | |
8264 | size_t locsymcount, | |
8265 | int signed_p) | |
d9352518 | 8266 | { |
4b93929b NC |
8267 | size_t len; |
8268 | size_t symlen; | |
a0c8462f AM |
8269 | bfd_vma a; |
8270 | bfd_vma b; | |
4b93929b | 8271 | char symbuf[4096]; |
0f02bbd9 | 8272 | const char *sym = *symp; |
a0c8462f AM |
8273 | const char *symend; |
8274 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
8275 | |
8276 | len = strlen (sym); | |
8277 | symend = sym + len; | |
8278 | ||
4b93929b | 8279 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
8280 | { |
8281 | bfd_set_error (bfd_error_invalid_operation); | |
8282 | return FALSE; | |
8283 | } | |
a0c8462f | 8284 | |
d9352518 DB |
8285 | switch (* sym) |
8286 | { | |
8287 | case '.': | |
0f02bbd9 AM |
8288 | *result = dot; |
8289 | *symp = sym + 1; | |
d9352518 DB |
8290 | return TRUE; |
8291 | ||
8292 | case '#': | |
0f02bbd9 AM |
8293 | ++sym; |
8294 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
8295 | return TRUE; |
8296 | ||
8297 | case 'S': | |
8298 | symbol_is_section = TRUE; | |
1a0670f3 | 8299 | /* Fall through. */ |
a0c8462f | 8300 | case 's': |
0f02bbd9 AM |
8301 | ++sym; |
8302 | symlen = strtol (sym, (char **) symp, 10); | |
8303 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 8304 | |
4b93929b | 8305 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
8306 | { |
8307 | bfd_set_error (bfd_error_invalid_operation); | |
8308 | return FALSE; | |
8309 | } | |
8310 | ||
8311 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 8312 | symbuf[symlen] = '\0'; |
0f02bbd9 | 8313 | *symp = sym + symlen; |
a0c8462f AM |
8314 | |
8315 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
8316 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
8317 | interpretation here; section means "try section first", not "must be a | |
8318 | section", and likewise with symbol. */ | |
8319 | ||
a0c8462f | 8320 | if (symbol_is_section) |
d9352518 | 8321 | { |
37b01f6a | 8322 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 8323 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8324 | isymbuf, locsymcount)) |
d9352518 DB |
8325 | { |
8326 | undefined_reference ("section", symbuf); | |
8327 | return FALSE; | |
8328 | } | |
a0c8462f AM |
8329 | } |
8330 | else | |
d9352518 | 8331 | { |
8b127cbc | 8332 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8333 | isymbuf, locsymcount) |
8b127cbc | 8334 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8335 | result, input_bfd)) |
d9352518 DB |
8336 | { |
8337 | undefined_reference ("symbol", symbuf); | |
8338 | return FALSE; | |
8339 | } | |
8340 | } | |
8341 | ||
8342 | return TRUE; | |
a0c8462f | 8343 | |
d9352518 DB |
8344 | /* All that remains are operators. */ |
8345 | ||
8346 | #define UNARY_OP(op) \ | |
8347 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8348 | { \ | |
8349 | sym += strlen (#op); \ | |
a0c8462f AM |
8350 | if (*sym == ':') \ |
8351 | ++sym; \ | |
0f02bbd9 | 8352 | *symp = sym; \ |
8b127cbc | 8353 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8354 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8355 | return FALSE; \ |
8356 | if (signed_p) \ | |
0f02bbd9 | 8357 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8358 | else \ |
8359 | *result = op a; \ | |
d9352518 DB |
8360 | return TRUE; \ |
8361 | } | |
8362 | ||
8363 | #define BINARY_OP(op) \ | |
8364 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8365 | { \ | |
8366 | sym += strlen (#op); \ | |
a0c8462f AM |
8367 | if (*sym == ':') \ |
8368 | ++sym; \ | |
0f02bbd9 | 8369 | *symp = sym; \ |
8b127cbc | 8370 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8371 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8372 | return FALSE; \ |
0f02bbd9 | 8373 | ++*symp; \ |
8b127cbc | 8374 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8375 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8376 | return FALSE; \ |
8377 | if (signed_p) \ | |
0f02bbd9 | 8378 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8379 | else \ |
8380 | *result = a op b; \ | |
d9352518 DB |
8381 | return TRUE; \ |
8382 | } | |
8383 | ||
8384 | default: | |
8385 | UNARY_OP (0-); | |
8386 | BINARY_OP (<<); | |
8387 | BINARY_OP (>>); | |
8388 | BINARY_OP (==); | |
8389 | BINARY_OP (!=); | |
8390 | BINARY_OP (<=); | |
8391 | BINARY_OP (>=); | |
8392 | BINARY_OP (&&); | |
8393 | BINARY_OP (||); | |
8394 | UNARY_OP (~); | |
8395 | UNARY_OP (!); | |
8396 | BINARY_OP (*); | |
8397 | BINARY_OP (/); | |
8398 | BINARY_OP (%); | |
8399 | BINARY_OP (^); | |
8400 | BINARY_OP (|); | |
8401 | BINARY_OP (&); | |
8402 | BINARY_OP (+); | |
8403 | BINARY_OP (-); | |
8404 | BINARY_OP (<); | |
8405 | BINARY_OP (>); | |
8406 | #undef UNARY_OP | |
8407 | #undef BINARY_OP | |
8408 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8409 | bfd_set_error (bfd_error_invalid_operation); | |
8410 | return FALSE; | |
8411 | } | |
8412 | } | |
8413 | ||
d9352518 | 8414 | static void |
a0c8462f AM |
8415 | put_value (bfd_vma size, |
8416 | unsigned long chunksz, | |
8417 | bfd *input_bfd, | |
8418 | bfd_vma x, | |
8419 | bfd_byte *location) | |
d9352518 DB |
8420 | { |
8421 | location += (size - chunksz); | |
8422 | ||
41cd1ad1 | 8423 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8424 | { |
8425 | switch (chunksz) | |
8426 | { | |
d9352518 DB |
8427 | case 1: |
8428 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8429 | x >>= 8; |
d9352518 DB |
8430 | break; |
8431 | case 2: | |
8432 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8433 | x >>= 16; |
d9352518 DB |
8434 | break; |
8435 | case 4: | |
8436 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8437 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8438 | x >>= 16; | |
8439 | x >>= 16; | |
d9352518 | 8440 | break; |
d9352518 | 8441 | #ifdef BFD64 |
41cd1ad1 | 8442 | case 8: |
d9352518 | 8443 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8444 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8445 | x >>= 32; | |
8446 | x >>= 32; | |
8447 | break; | |
d9352518 | 8448 | #endif |
41cd1ad1 NC |
8449 | default: |
8450 | abort (); | |
d9352518 DB |
8451 | break; |
8452 | } | |
8453 | } | |
8454 | } | |
8455 | ||
a0c8462f AM |
8456 | static bfd_vma |
8457 | get_value (bfd_vma size, | |
8458 | unsigned long chunksz, | |
8459 | bfd *input_bfd, | |
8460 | bfd_byte *location) | |
d9352518 | 8461 | { |
9b239e0e | 8462 | int shift; |
d9352518 DB |
8463 | bfd_vma x = 0; |
8464 | ||
9b239e0e NC |
8465 | /* Sanity checks. */ |
8466 | BFD_ASSERT (chunksz <= sizeof (x) | |
8467 | && size >= chunksz | |
8468 | && chunksz != 0 | |
8469 | && (size % chunksz) == 0 | |
8470 | && input_bfd != NULL | |
8471 | && location != NULL); | |
8472 | ||
8473 | if (chunksz == sizeof (x)) | |
8474 | { | |
8475 | BFD_ASSERT (size == chunksz); | |
8476 | ||
8477 | /* Make sure that we do not perform an undefined shift operation. | |
8478 | We know that size == chunksz so there will only be one iteration | |
8479 | of the loop below. */ | |
8480 | shift = 0; | |
8481 | } | |
8482 | else | |
8483 | shift = 8 * chunksz; | |
8484 | ||
a0c8462f | 8485 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8486 | { |
8487 | switch (chunksz) | |
8488 | { | |
d9352518 | 8489 | case 1: |
9b239e0e | 8490 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8491 | break; |
8492 | case 2: | |
9b239e0e | 8493 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8494 | break; |
8495 | case 4: | |
9b239e0e | 8496 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8497 | break; |
d9352518 | 8498 | #ifdef BFD64 |
9b239e0e NC |
8499 | case 8: |
8500 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8501 | break; |
9b239e0e NC |
8502 | #endif |
8503 | default: | |
8504 | abort (); | |
d9352518 DB |
8505 | } |
8506 | } | |
8507 | return x; | |
8508 | } | |
8509 | ||
a0c8462f AM |
8510 | static void |
8511 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8512 | unsigned long *oplen, /* in bits */ | |
8513 | unsigned long *len, /* in bits */ | |
8514 | unsigned long *wordsz, /* in bytes */ | |
8515 | unsigned long *chunksz, /* in bytes */ | |
8516 | unsigned long *lsb0_p, | |
8517 | unsigned long *signed_p, | |
8518 | unsigned long *trunc_p, | |
8519 | unsigned long encoded) | |
d9352518 | 8520 | { |
07d6d2b8 AM |
8521 | * start = encoded & 0x3F; |
8522 | * len = (encoded >> 6) & 0x3F; | |
d9352518 DB |
8523 | * oplen = (encoded >> 12) & 0x3F; |
8524 | * wordsz = (encoded >> 18) & 0xF; | |
8525 | * chunksz = (encoded >> 22) & 0xF; | |
8526 | * lsb0_p = (encoded >> 27) & 1; | |
8527 | * signed_p = (encoded >> 28) & 1; | |
8528 | * trunc_p = (encoded >> 29) & 1; | |
8529 | } | |
8530 | ||
cdfeee4f | 8531 | bfd_reloc_status_type |
0f02bbd9 | 8532 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8533 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8534 | bfd_byte *contents, |
8535 | Elf_Internal_Rela *rel, | |
8536 | bfd_vma relocation) | |
d9352518 | 8537 | { |
0f02bbd9 AM |
8538 | bfd_vma shift, x, mask; |
8539 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8540 | bfd_reloc_status_type r; |
d9352518 DB |
8541 | |
8542 | /* Perform this reloc, since it is complex. | |
8543 | (this is not to say that it necessarily refers to a complex | |
8544 | symbol; merely that it is a self-describing CGEN based reloc. | |
8545 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8546 | word size, etc) encoded within it.). */ |
d9352518 | 8547 | |
a0c8462f AM |
8548 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8549 | &chunksz, &lsb0_p, &signed_p, | |
8550 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8551 | |
8552 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8553 | ||
8554 | if (lsb0_p) | |
8555 | shift = (start + 1) - len; | |
8556 | else | |
8557 | shift = (8 * wordsz) - (start + len); | |
8558 | ||
37b01f6a DG |
8559 | x = get_value (wordsz, chunksz, input_bfd, |
8560 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8561 | |
8562 | #ifdef DEBUG | |
8563 | printf ("Doing complex reloc: " | |
8564 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8565 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8566 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8567 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8568 | oplen, (unsigned long) x, (unsigned long) mask, |
8569 | (unsigned long) relocation); | |
d9352518 DB |
8570 | #endif |
8571 | ||
cdfeee4f | 8572 | r = bfd_reloc_ok; |
d9352518 | 8573 | if (! trunc_p) |
cdfeee4f AM |
8574 | /* Now do an overflow check. */ |
8575 | r = bfd_check_overflow ((signed_p | |
8576 | ? complain_overflow_signed | |
8577 | : complain_overflow_unsigned), | |
8578 | len, 0, (8 * wordsz), | |
8579 | relocation); | |
a0c8462f | 8580 | |
d9352518 DB |
8581 | /* Do the deed. */ |
8582 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8583 | ||
8584 | #ifdef DEBUG | |
8585 | printf (" relocation: %8.8lx\n" | |
8586 | " shifted mask: %8.8lx\n" | |
8587 | " shifted/masked reloc: %8.8lx\n" | |
8588 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8589 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8590 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8591 | #endif |
37b01f6a DG |
8592 | put_value (wordsz, chunksz, input_bfd, x, |
8593 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8594 | return r; |
d9352518 DB |
8595 | } |
8596 | ||
0e287786 AM |
8597 | /* Functions to read r_offset from external (target order) reloc |
8598 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8599 | know the value is aligned. */ | |
53df40a4 | 8600 | |
0e287786 AM |
8601 | static bfd_vma |
8602 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8603 | { |
8604 | union aligned32 | |
8605 | { | |
8606 | uint32_t v; | |
8607 | unsigned char c[4]; | |
8608 | }; | |
8609 | const union aligned32 *a | |
0e287786 | 8610 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8611 | |
8612 | uint32_t aval = ( (uint32_t) a->c[0] | |
8613 | | (uint32_t) a->c[1] << 8 | |
8614 | | (uint32_t) a->c[2] << 16 | |
8615 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8616 | return aval; |
53df40a4 AM |
8617 | } |
8618 | ||
0e287786 AM |
8619 | static bfd_vma |
8620 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8621 | { |
8622 | union aligned32 | |
8623 | { | |
8624 | uint32_t v; | |
8625 | unsigned char c[4]; | |
8626 | }; | |
8627 | const union aligned32 *a | |
0e287786 | 8628 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8629 | |
8630 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8631 | | (uint32_t) a->c[1] << 16 | |
8632 | | (uint32_t) a->c[2] << 8 | |
8633 | | (uint32_t) a->c[3]); | |
0e287786 | 8634 | return aval; |
53df40a4 AM |
8635 | } |
8636 | ||
8637 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8638 | static bfd_vma |
8639 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8640 | { |
8641 | union aligned64 | |
8642 | { | |
8643 | uint64_t v; | |
8644 | unsigned char c[8]; | |
8645 | }; | |
8646 | const union aligned64 *a | |
0e287786 | 8647 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8648 | |
8649 | uint64_t aval = ( (uint64_t) a->c[0] | |
8650 | | (uint64_t) a->c[1] << 8 | |
8651 | | (uint64_t) a->c[2] << 16 | |
8652 | | (uint64_t) a->c[3] << 24 | |
8653 | | (uint64_t) a->c[4] << 32 | |
8654 | | (uint64_t) a->c[5] << 40 | |
8655 | | (uint64_t) a->c[6] << 48 | |
8656 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8657 | return aval; |
53df40a4 AM |
8658 | } |
8659 | ||
0e287786 AM |
8660 | static bfd_vma |
8661 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8662 | { |
8663 | union aligned64 | |
8664 | { | |
8665 | uint64_t v; | |
8666 | unsigned char c[8]; | |
8667 | }; | |
8668 | const union aligned64 *a | |
0e287786 | 8669 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8670 | |
8671 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8672 | | (uint64_t) a->c[1] << 48 | |
8673 | | (uint64_t) a->c[2] << 40 | |
8674 | | (uint64_t) a->c[3] << 32 | |
8675 | | (uint64_t) a->c[4] << 24 | |
8676 | | (uint64_t) a->c[5] << 16 | |
8677 | | (uint64_t) a->c[6] << 8 | |
8678 | | (uint64_t) a->c[7]); | |
0e287786 | 8679 | return aval; |
53df40a4 AM |
8680 | } |
8681 | #endif | |
8682 | ||
c152c796 AM |
8683 | /* When performing a relocatable link, the input relocations are |
8684 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8685 | referenced must be updated. Update all the relocations found in |
8686 | RELDATA. */ | |
c152c796 | 8687 | |
bca6d0e3 | 8688 | static bfd_boolean |
c152c796 | 8689 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 8690 | asection *sec, |
28dbcedc | 8691 | struct bfd_elf_section_reloc_data *reldata, |
10bbbc1d NC |
8692 | bfd_boolean sort, |
8693 | struct bfd_link_info *info) | |
c152c796 AM |
8694 | { |
8695 | unsigned int i; | |
8696 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8697 | bfd_byte *erela; | |
8698 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8699 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8700 | bfd_vma r_type_mask; | |
8701 | int r_sym_shift; | |
d4730f92 BS |
8702 | unsigned int count = reldata->count; |
8703 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8704 | |
d4730f92 | 8705 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8706 | { |
8707 | swap_in = bed->s->swap_reloc_in; | |
8708 | swap_out = bed->s->swap_reloc_out; | |
8709 | } | |
d4730f92 | 8710 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8711 | { |
8712 | swap_in = bed->s->swap_reloca_in; | |
8713 | swap_out = bed->s->swap_reloca_out; | |
8714 | } | |
8715 | else | |
8716 | abort (); | |
8717 | ||
8718 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8719 | abort (); | |
8720 | ||
8721 | if (bed->s->arch_size == 32) | |
8722 | { | |
8723 | r_type_mask = 0xff; | |
8724 | r_sym_shift = 8; | |
8725 | } | |
8726 | else | |
8727 | { | |
8728 | r_type_mask = 0xffffffff; | |
8729 | r_sym_shift = 32; | |
8730 | } | |
8731 | ||
d4730f92 BS |
8732 | erela = reldata->hdr->contents; |
8733 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8734 | { |
8735 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8736 | unsigned int j; | |
8737 | ||
8738 | if (*rel_hash == NULL) | |
8739 | continue; | |
8740 | ||
10bbbc1d NC |
8741 | if ((*rel_hash)->indx == -2 |
8742 | && info->gc_sections | |
8743 | && ! info->gc_keep_exported) | |
8744 | { | |
8745 | /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ | |
9793eb77 | 8746 | _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"), |
10bbbc1d NC |
8747 | abfd, sec, |
8748 | (*rel_hash)->root.root.string); | |
9793eb77 | 8749 | _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"), |
d42c267e | 8750 | abfd, sec); |
10bbbc1d NC |
8751 | bfd_set_error (bfd_error_invalid_operation); |
8752 | return FALSE; | |
8753 | } | |
c152c796 AM |
8754 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
8755 | ||
8756 | (*swap_in) (abfd, erela, irela); | |
8757 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8758 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8759 | | (irela[j].r_info & r_type_mask)); | |
8760 | (*swap_out) (abfd, irela, erela); | |
8761 | } | |
53df40a4 | 8762 | |
9eaff861 AO |
8763 | if (bed->elf_backend_update_relocs) |
8764 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
8765 | ||
0e287786 | 8766 | if (sort && count != 0) |
53df40a4 | 8767 | { |
0e287786 AM |
8768 | bfd_vma (*ext_r_off) (const void *); |
8769 | bfd_vma r_off; | |
8770 | size_t elt_size; | |
8771 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8772 | bfd_byte *buf = NULL; |
28dbcedc AM |
8773 | |
8774 | if (bed->s->arch_size == 32) | |
8775 | { | |
8776 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8777 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8778 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8779 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8780 | else |
8781 | abort (); | |
8782 | } | |
53df40a4 | 8783 | else |
28dbcedc | 8784 | { |
53df40a4 | 8785 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8786 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8787 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8788 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8789 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8790 | else |
53df40a4 | 8791 | #endif |
28dbcedc AM |
8792 | abort (); |
8793 | } | |
0e287786 | 8794 | |
bca6d0e3 AM |
8795 | /* Must use a stable sort here. A modified insertion sort, |
8796 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8797 | elt_size = reldata->hdr->sh_entsize; |
8798 | base = reldata->hdr->contents; | |
8799 | end = base + count * elt_size; | |
bca6d0e3 | 8800 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8801 | abort (); |
8802 | ||
8803 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8804 | speeding the main loop below. */ | |
8805 | r_off = (*ext_r_off) (base); | |
8806 | for (p = loc = base; (p += elt_size) < end; ) | |
8807 | { | |
8808 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8809 | if (r_off > r_off2) | |
8810 | { | |
8811 | r_off = r_off2; | |
8812 | loc = p; | |
8813 | } | |
8814 | } | |
8815 | if (loc != base) | |
8816 | { | |
8817 | /* Don't just swap *base and *loc as that changes the order | |
8818 | of the original base[0] and base[1] if they happen to | |
8819 | have the same r_offset. */ | |
bca6d0e3 AM |
8820 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8821 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8822 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8823 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8824 | } |
8825 | ||
b29b8669 | 8826 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8827 | { |
8828 | /* base to p is sorted, *p is next to insert. */ | |
8829 | r_off = (*ext_r_off) (p); | |
8830 | /* Search the sorted region for location to insert. */ | |
8831 | loc = p - elt_size; | |
8832 | while (r_off < (*ext_r_off) (loc)) | |
8833 | loc -= elt_size; | |
8834 | loc += elt_size; | |
8835 | if (loc != p) | |
8836 | { | |
bca6d0e3 AM |
8837 | /* Chances are there is a run of relocs to insert here, |
8838 | from one of more input files. Files are not always | |
8839 | linked in order due to the way elf_link_input_bfd is | |
8840 | called. See pr17666. */ | |
8841 | size_t sortlen = p - loc; | |
8842 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8843 | size_t runlen = elt_size; | |
8844 | size_t buf_size = 96 * 1024; | |
8845 | while (p + runlen < end | |
8846 | && (sortlen <= buf_size | |
8847 | || runlen + elt_size <= buf_size) | |
8848 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8849 | runlen += elt_size; | |
8850 | if (buf == NULL) | |
8851 | { | |
8852 | buf = bfd_malloc (buf_size); | |
8853 | if (buf == NULL) | |
8854 | return FALSE; | |
8855 | } | |
8856 | if (runlen < sortlen) | |
8857 | { | |
8858 | memcpy (buf, p, runlen); | |
8859 | memmove (loc + runlen, loc, sortlen); | |
8860 | memcpy (loc, buf, runlen); | |
8861 | } | |
8862 | else | |
8863 | { | |
8864 | memcpy (buf, loc, sortlen); | |
8865 | memmove (loc, p, runlen); | |
8866 | memcpy (loc + runlen, buf, sortlen); | |
8867 | } | |
b29b8669 | 8868 | p += runlen - elt_size; |
0e287786 AM |
8869 | } |
8870 | } | |
8871 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
8872 | free (reldata->hashes); |
8873 | reldata->hashes = NULL; | |
bca6d0e3 | 8874 | free (buf); |
53df40a4 | 8875 | } |
bca6d0e3 | 8876 | return TRUE; |
c152c796 AM |
8877 | } |
8878 | ||
8879 | struct elf_link_sort_rela | |
8880 | { | |
8881 | union { | |
8882 | bfd_vma offset; | |
8883 | bfd_vma sym_mask; | |
8884 | } u; | |
8885 | enum elf_reloc_type_class type; | |
8886 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8887 | Elf_Internal_Rela rela[1]; | |
8888 | }; | |
8889 | ||
8890 | static int | |
8891 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8892 | { | |
a50b1753 NC |
8893 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8894 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
8895 | int relativea, relativeb; |
8896 | ||
8897 | relativea = a->type == reloc_class_relative; | |
8898 | relativeb = b->type == reloc_class_relative; | |
8899 | ||
8900 | if (relativea < relativeb) | |
8901 | return 1; | |
8902 | if (relativea > relativeb) | |
8903 | return -1; | |
8904 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
8905 | return -1; | |
8906 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
8907 | return 1; | |
8908 | if (a->rela->r_offset < b->rela->r_offset) | |
8909 | return -1; | |
8910 | if (a->rela->r_offset > b->rela->r_offset) | |
8911 | return 1; | |
8912 | return 0; | |
8913 | } | |
8914 | ||
8915 | static int | |
8916 | elf_link_sort_cmp2 (const void *A, const void *B) | |
8917 | { | |
a50b1753 NC |
8918 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
8919 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 8920 | |
7e612e98 | 8921 | if (a->type < b->type) |
c152c796 | 8922 | return -1; |
7e612e98 | 8923 | if (a->type > b->type) |
c152c796 | 8924 | return 1; |
7e612e98 | 8925 | if (a->u.offset < b->u.offset) |
c152c796 | 8926 | return -1; |
7e612e98 | 8927 | if (a->u.offset > b->u.offset) |
c152c796 AM |
8928 | return 1; |
8929 | if (a->rela->r_offset < b->rela->r_offset) | |
8930 | return -1; | |
8931 | if (a->rela->r_offset > b->rela->r_offset) | |
8932 | return 1; | |
8933 | return 0; | |
8934 | } | |
8935 | ||
8936 | static size_t | |
8937 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
8938 | { | |
3410fea8 | 8939 | asection *dynamic_relocs; |
fc66a176 L |
8940 | asection *rela_dyn; |
8941 | asection *rel_dyn; | |
c152c796 AM |
8942 | bfd_size_type count, size; |
8943 | size_t i, ret, sort_elt, ext_size; | |
8944 | bfd_byte *sort, *s_non_relative, *p; | |
8945 | struct elf_link_sort_rela *sq; | |
8946 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8947 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 8948 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
8949 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
8950 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8951 | struct bfd_link_order *lo; | |
8952 | bfd_vma r_sym_mask; | |
3410fea8 | 8953 | bfd_boolean use_rela; |
c152c796 | 8954 | |
3410fea8 NC |
8955 | /* Find a dynamic reloc section. */ |
8956 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
8957 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
8958 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
8959 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 8960 | { |
3410fea8 NC |
8961 | bfd_boolean use_rela_initialised = FALSE; |
8962 | ||
8963 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 8964 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
8965 | use_rela = TRUE; |
8966 | ||
8967 | /* Both sections are present. Examine the sizes | |
8968 | of the indirect sections to help us choose. */ | |
8969 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
8970 | if (lo->type == bfd_indirect_link_order) | |
8971 | { | |
8972 | asection *o = lo->u.indirect.section; | |
8973 | ||
8974 | if ((o->size % bed->s->sizeof_rela) == 0) | |
8975 | { | |
8976 | if ((o->size % bed->s->sizeof_rel) == 0) | |
8977 | /* Section size is divisible by both rel and rela sizes. | |
8978 | It is of no help to us. */ | |
8979 | ; | |
8980 | else | |
8981 | { | |
8982 | /* Section size is only divisible by rela. */ | |
535b785f | 8983 | if (use_rela_initialised && !use_rela) |
3410fea8 | 8984 | { |
9793eb77 | 8985 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
8986 | "they are in more than one size"), |
8987 | abfd); | |
3410fea8 NC |
8988 | bfd_set_error (bfd_error_invalid_operation); |
8989 | return 0; | |
8990 | } | |
8991 | else | |
8992 | { | |
8993 | use_rela = TRUE; | |
8994 | use_rela_initialised = TRUE; | |
8995 | } | |
8996 | } | |
8997 | } | |
8998 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
8999 | { | |
9000 | /* Section size is only divisible by rel. */ | |
535b785f | 9001 | if (use_rela_initialised && use_rela) |
3410fea8 | 9002 | { |
9793eb77 | 9003 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9004 | "they are in more than one size"), |
9005 | abfd); | |
3410fea8 NC |
9006 | bfd_set_error (bfd_error_invalid_operation); |
9007 | return 0; | |
9008 | } | |
9009 | else | |
9010 | { | |
9011 | use_rela = FALSE; | |
9012 | use_rela_initialised = TRUE; | |
9013 | } | |
9014 | } | |
9015 | else | |
9016 | { | |
c8e44c6d AM |
9017 | /* The section size is not divisible by either - |
9018 | something is wrong. */ | |
9793eb77 | 9019 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9020 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9021 | bfd_set_error (bfd_error_invalid_operation); |
9022 | return 0; | |
9023 | } | |
9024 | } | |
9025 | ||
9026 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9027 | if (lo->type == bfd_indirect_link_order) | |
9028 | { | |
9029 | asection *o = lo->u.indirect.section; | |
9030 | ||
9031 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9032 | { | |
9033 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9034 | /* Section size is divisible by both rel and rela sizes. | |
9035 | It is of no help to us. */ | |
9036 | ; | |
9037 | else | |
9038 | { | |
9039 | /* Section size is only divisible by rela. */ | |
535b785f | 9040 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9041 | { |
9793eb77 | 9042 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9043 | "they are in more than one size"), |
9044 | abfd); | |
3410fea8 NC |
9045 | bfd_set_error (bfd_error_invalid_operation); |
9046 | return 0; | |
9047 | } | |
9048 | else | |
9049 | { | |
9050 | use_rela = TRUE; | |
9051 | use_rela_initialised = TRUE; | |
9052 | } | |
9053 | } | |
9054 | } | |
9055 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9056 | { | |
9057 | /* Section size is only divisible by rel. */ | |
535b785f | 9058 | if (use_rela_initialised && use_rela) |
3410fea8 | 9059 | { |
9793eb77 | 9060 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9061 | "they are in more than one size"), |
9062 | abfd); | |
3410fea8 NC |
9063 | bfd_set_error (bfd_error_invalid_operation); |
9064 | return 0; | |
9065 | } | |
9066 | else | |
9067 | { | |
9068 | use_rela = FALSE; | |
9069 | use_rela_initialised = TRUE; | |
9070 | } | |
9071 | } | |
9072 | else | |
9073 | { | |
c8e44c6d AM |
9074 | /* The section size is not divisible by either - |
9075 | something is wrong. */ | |
9793eb77 | 9076 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9077 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9078 | bfd_set_error (bfd_error_invalid_operation); |
9079 | return 0; | |
9080 | } | |
9081 | } | |
9082 | ||
9083 | if (! use_rela_initialised) | |
9084 | /* Make a guess. */ | |
9085 | use_rela = TRUE; | |
c152c796 | 9086 | } |
fc66a176 L |
9087 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
9088 | use_rela = TRUE; | |
9089 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 9090 | use_rela = FALSE; |
c152c796 | 9091 | else |
fc66a176 | 9092 | return 0; |
3410fea8 NC |
9093 | |
9094 | if (use_rela) | |
c152c796 | 9095 | { |
3410fea8 | 9096 | dynamic_relocs = rela_dyn; |
c152c796 AM |
9097 | ext_size = bed->s->sizeof_rela; |
9098 | swap_in = bed->s->swap_reloca_in; | |
9099 | swap_out = bed->s->swap_reloca_out; | |
9100 | } | |
3410fea8 NC |
9101 | else |
9102 | { | |
9103 | dynamic_relocs = rel_dyn; | |
9104 | ext_size = bed->s->sizeof_rel; | |
9105 | swap_in = bed->s->swap_reloc_in; | |
9106 | swap_out = bed->s->swap_reloc_out; | |
9107 | } | |
c152c796 AM |
9108 | |
9109 | size = 0; | |
3410fea8 | 9110 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 9111 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 9112 | size += lo->u.indirect.section->size; |
c152c796 | 9113 | |
3410fea8 | 9114 | if (size != dynamic_relocs->size) |
c152c796 AM |
9115 | return 0; |
9116 | ||
9117 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
9118 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
9119 | |
9120 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
9121 | if (count == 0) |
9122 | return 0; | |
a50b1753 | 9123 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 9124 | |
c152c796 AM |
9125 | if (sort == NULL) |
9126 | { | |
9127 | (*info->callbacks->warning) | |
9793eb77 | 9128 | (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0); |
c152c796 AM |
9129 | return 0; |
9130 | } | |
9131 | ||
9132 | if (bed->s->arch_size == 32) | |
9133 | r_sym_mask = ~(bfd_vma) 0xff; | |
9134 | else | |
9135 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
9136 | ||
3410fea8 | 9137 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9138 | if (lo->type == bfd_indirect_link_order) |
9139 | { | |
9140 | bfd_byte *erel, *erelend; | |
9141 | asection *o = lo->u.indirect.section; | |
9142 | ||
1da212d6 AM |
9143 | if (o->contents == NULL && o->size != 0) |
9144 | { | |
9145 | /* This is a reloc section that is being handled as a normal | |
9146 | section. See bfd_section_from_shdr. We can't combine | |
9147 | relocs in this case. */ | |
9148 | free (sort); | |
9149 | return 0; | |
9150 | } | |
c152c796 | 9151 | erel = o->contents; |
eea6121a | 9152 | erelend = o->contents + o->size; |
c8e44c6d | 9153 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 9154 | |
c152c796 AM |
9155 | while (erel < erelend) |
9156 | { | |
9157 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 9158 | |
c152c796 | 9159 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 9160 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
9161 | s->u.sym_mask = r_sym_mask; |
9162 | p += sort_elt; | |
9163 | erel += ext_size; | |
9164 | } | |
9165 | } | |
9166 | ||
9167 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
9168 | ||
9169 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
9170 | { | |
9171 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9172 | if (s->type != reloc_class_relative) | |
9173 | break; | |
9174 | } | |
9175 | ret = i; | |
9176 | s_non_relative = p; | |
9177 | ||
9178 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
9179 | for (; i < count; i++, p += sort_elt) | |
9180 | { | |
9181 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
9182 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
9183 | sq = sp; | |
9184 | sp->u.offset = sq->rela->r_offset; | |
9185 | } | |
9186 | ||
9187 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
9188 | ||
c8e44c6d AM |
9189 | struct elf_link_hash_table *htab = elf_hash_table (info); |
9190 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
9191 | { | |
9192 | /* We have plt relocs in .rela.dyn. */ | |
9193 | sq = (struct elf_link_sort_rela *) sort; | |
9194 | for (i = 0; i < count; i++) | |
9195 | if (sq[count - i - 1].type != reloc_class_plt) | |
9196 | break; | |
9197 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
9198 | { | |
9199 | struct bfd_link_order **plo; | |
9200 | /* Put srelplt link_order last. This is so the output_offset | |
9201 | set in the next loop is correct for DT_JMPREL. */ | |
9202 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
9203 | if ((*plo)->type == bfd_indirect_link_order | |
9204 | && (*plo)->u.indirect.section == htab->srelplt) | |
9205 | { | |
9206 | lo = *plo; | |
9207 | *plo = lo->next; | |
9208 | } | |
9209 | else | |
9210 | plo = &(*plo)->next; | |
9211 | *plo = lo; | |
9212 | lo->next = NULL; | |
9213 | dynamic_relocs->map_tail.link_order = lo; | |
9214 | } | |
9215 | } | |
9216 | ||
9217 | p = sort; | |
3410fea8 | 9218 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9219 | if (lo->type == bfd_indirect_link_order) |
9220 | { | |
9221 | bfd_byte *erel, *erelend; | |
9222 | asection *o = lo->u.indirect.section; | |
9223 | ||
9224 | erel = o->contents; | |
eea6121a | 9225 | erelend = o->contents + o->size; |
c8e44c6d | 9226 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
9227 | while (erel < erelend) |
9228 | { | |
9229 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9230 | (*swap_out) (abfd, s->rela, erel); | |
9231 | p += sort_elt; | |
9232 | erel += ext_size; | |
9233 | } | |
9234 | } | |
9235 | ||
9236 | free (sort); | |
3410fea8 | 9237 | *psec = dynamic_relocs; |
c152c796 AM |
9238 | return ret; |
9239 | } | |
9240 | ||
ef10c3ac | 9241 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 9242 | |
6e0b88f1 | 9243 | static int |
ef10c3ac L |
9244 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
9245 | const char *name, | |
9246 | Elf_Internal_Sym *elfsym, | |
9247 | asection *input_sec, | |
9248 | struct elf_link_hash_entry *h) | |
c152c796 | 9249 | { |
6e0b88f1 | 9250 | int (*output_symbol_hook) |
c152c796 AM |
9251 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
9252 | struct elf_link_hash_entry *); | |
ef10c3ac | 9253 | struct elf_link_hash_table *hash_table; |
c152c796 | 9254 | const struct elf_backend_data *bed; |
ef10c3ac | 9255 | bfd_size_type strtabsize; |
c152c796 | 9256 | |
8539e4e8 AM |
9257 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
9258 | ||
8b127cbc | 9259 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
9260 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
9261 | if (output_symbol_hook != NULL) | |
9262 | { | |
8b127cbc | 9263 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
9264 | if (ret != 1) |
9265 | return ret; | |
c152c796 AM |
9266 | } |
9267 | ||
ef10c3ac L |
9268 | if (name == NULL |
9269 | || *name == '\0' | |
9270 | || (input_sec->flags & SEC_EXCLUDE)) | |
9271 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
9272 | else |
9273 | { | |
ef10c3ac L |
9274 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
9275 | to get the final offset for st_name. */ | |
9276 | elfsym->st_name | |
9277 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
9278 | name, FALSE); | |
c152c796 | 9279 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 9280 | return 0; |
c152c796 AM |
9281 | } |
9282 | ||
ef10c3ac L |
9283 | hash_table = elf_hash_table (flinfo->info); |
9284 | strtabsize = hash_table->strtabsize; | |
9285 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 9286 | { |
ef10c3ac L |
9287 | strtabsize += strtabsize; |
9288 | hash_table->strtabsize = strtabsize; | |
9289 | strtabsize *= sizeof (*hash_table->strtab); | |
9290 | hash_table->strtab | |
9291 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
9292 | strtabsize); | |
9293 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 9294 | return 0; |
c152c796 | 9295 | } |
ef10c3ac L |
9296 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
9297 | hash_table->strtab[hash_table->strtabcount].dest_index | |
9298 | = hash_table->strtabcount; | |
9299 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
9300 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
9301 | ||
9302 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
9303 | hash_table->strtabcount += 1; | |
9304 | ||
9305 | return 1; | |
9306 | } | |
9307 | ||
9308 | /* Swap symbols out to the symbol table and flush the output symbols to | |
9309 | the file. */ | |
9310 | ||
9311 | static bfd_boolean | |
9312 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
9313 | { | |
9314 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
9315 | bfd_size_type amt; |
9316 | size_t i; | |
ef10c3ac L |
9317 | const struct elf_backend_data *bed; |
9318 | bfd_byte *symbuf; | |
9319 | Elf_Internal_Shdr *hdr; | |
9320 | file_ptr pos; | |
9321 | bfd_boolean ret; | |
9322 | ||
9323 | if (!hash_table->strtabcount) | |
9324 | return TRUE; | |
9325 | ||
9326 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
9327 | ||
9328 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 9329 | |
ef10c3ac L |
9330 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
9331 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
9332 | if (symbuf == NULL) | |
9333 | return FALSE; | |
1b786873 | 9334 | |
ef10c3ac | 9335 | if (flinfo->symshndxbuf) |
c152c796 | 9336 | { |
ef53be89 AM |
9337 | amt = sizeof (Elf_External_Sym_Shndx); |
9338 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
9339 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9340 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 9341 | { |
ef10c3ac L |
9342 | free (symbuf); |
9343 | return FALSE; | |
c152c796 | 9344 | } |
c152c796 AM |
9345 | } |
9346 | ||
ef10c3ac L |
9347 | for (i = 0; i < hash_table->strtabcount; i++) |
9348 | { | |
9349 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
9350 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9351 | elfsym->sym.st_name = 0; | |
9352 | else | |
9353 | elfsym->sym.st_name | |
9354 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9355 | elfsym->sym.st_name); | |
9356 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9357 | ((bfd_byte *) symbuf | |
9358 | + (elfsym->dest_index | |
9359 | * bed->s->sizeof_sym)), | |
9360 | (flinfo->symshndxbuf | |
9361 | + elfsym->destshndx_index)); | |
9362 | } | |
9363 | ||
9364 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9365 | pos = hdr->sh_offset + hdr->sh_size; | |
9366 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9367 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9368 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9369 | { | |
9370 | hdr->sh_size += amt; | |
9371 | ret = TRUE; | |
9372 | } | |
9373 | else | |
9374 | ret = FALSE; | |
c152c796 | 9375 | |
ef10c3ac L |
9376 | free (symbuf); |
9377 | ||
9378 | free (hash_table->strtab); | |
9379 | hash_table->strtab = NULL; | |
9380 | ||
9381 | return ret; | |
c152c796 AM |
9382 | } |
9383 | ||
c0d5a53d L |
9384 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9385 | ||
9386 | static bfd_boolean | |
9387 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9388 | { | |
4fbb74a6 AM |
9389 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9390 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9391 | { |
9392 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9393 | beyond 64k. */ |
4eca0228 | 9394 | _bfd_error_handler |
695344c0 | 9395 | /* xgettext:c-format */ |
9793eb77 | 9396 | (_("%pB: too many sections: %d (>= %d)"), |
4fbb74a6 | 9397 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9398 | bfd_set_error (bfd_error_nonrepresentable_section); |
9399 | return FALSE; | |
9400 | } | |
9401 | return TRUE; | |
9402 | } | |
9403 | ||
c152c796 AM |
9404 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9405 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9406 | versioned symbol that would normally require an explicit version. | |
9407 | We also handle the case that a DSO references a hidden symbol | |
9408 | which may be satisfied by a versioned symbol in another DSO. */ | |
9409 | ||
9410 | static bfd_boolean | |
9411 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9412 | const struct elf_backend_data *bed, | |
9413 | struct elf_link_hash_entry *h) | |
9414 | { | |
9415 | bfd *abfd; | |
9416 | struct elf_link_loaded_list *loaded; | |
9417 | ||
9418 | if (!is_elf_hash_table (info->hash)) | |
9419 | return FALSE; | |
9420 | ||
90c984fc L |
9421 | /* Check indirect symbol. */ |
9422 | while (h->root.type == bfd_link_hash_indirect) | |
9423 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9424 | ||
c152c796 AM |
9425 | switch (h->root.type) |
9426 | { | |
9427 | default: | |
9428 | abfd = NULL; | |
9429 | break; | |
9430 | ||
9431 | case bfd_link_hash_undefined: | |
9432 | case bfd_link_hash_undefweak: | |
9433 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9434 | if (abfd == NULL |
9435 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9436 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9437 | return FALSE; |
9438 | break; | |
9439 | ||
9440 | case bfd_link_hash_defined: | |
9441 | case bfd_link_hash_defweak: | |
9442 | abfd = h->root.u.def.section->owner; | |
9443 | break; | |
9444 | ||
9445 | case bfd_link_hash_common: | |
9446 | abfd = h->root.u.c.p->section->owner; | |
9447 | break; | |
9448 | } | |
9449 | BFD_ASSERT (abfd != NULL); | |
9450 | ||
9451 | for (loaded = elf_hash_table (info)->loaded; | |
9452 | loaded != NULL; | |
9453 | loaded = loaded->next) | |
9454 | { | |
9455 | bfd *input; | |
9456 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9457 | size_t symcount; |
9458 | size_t extsymcount; | |
9459 | size_t extsymoff; | |
c152c796 AM |
9460 | Elf_Internal_Shdr *versymhdr; |
9461 | Elf_Internal_Sym *isym; | |
9462 | Elf_Internal_Sym *isymend; | |
9463 | Elf_Internal_Sym *isymbuf; | |
9464 | Elf_External_Versym *ever; | |
9465 | Elf_External_Versym *extversym; | |
9466 | ||
9467 | input = loaded->abfd; | |
9468 | ||
9469 | /* We check each DSO for a possible hidden versioned definition. */ | |
9470 | if (input == abfd | |
9471 | || (input->flags & DYNAMIC) == 0 | |
9472 | || elf_dynversym (input) == 0) | |
9473 | continue; | |
9474 | ||
9475 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9476 | ||
9477 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9478 | if (elf_bad_symtab (input)) | |
9479 | { | |
9480 | extsymcount = symcount; | |
9481 | extsymoff = 0; | |
9482 | } | |
9483 | else | |
9484 | { | |
9485 | extsymcount = symcount - hdr->sh_info; | |
9486 | extsymoff = hdr->sh_info; | |
9487 | } | |
9488 | ||
9489 | if (extsymcount == 0) | |
9490 | continue; | |
9491 | ||
9492 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9493 | NULL, NULL, NULL); | |
9494 | if (isymbuf == NULL) | |
9495 | return FALSE; | |
9496 | ||
9497 | /* Read in any version definitions. */ | |
9498 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9499 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9500 | if (extversym == NULL) |
9501 | goto error_ret; | |
9502 | ||
9503 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9504 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9505 | != versymhdr->sh_size)) | |
9506 | { | |
9507 | free (extversym); | |
9508 | error_ret: | |
9509 | free (isymbuf); | |
9510 | return FALSE; | |
9511 | } | |
9512 | ||
9513 | ever = extversym + extsymoff; | |
9514 | isymend = isymbuf + extsymcount; | |
9515 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9516 | { | |
9517 | const char *name; | |
9518 | Elf_Internal_Versym iver; | |
9519 | unsigned short version_index; | |
9520 | ||
9521 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9522 | || isym->st_shndx == SHN_UNDEF) | |
9523 | continue; | |
9524 | ||
9525 | name = bfd_elf_string_from_elf_section (input, | |
9526 | hdr->sh_link, | |
9527 | isym->st_name); | |
9528 | if (strcmp (name, h->root.root.string) != 0) | |
9529 | continue; | |
9530 | ||
9531 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9532 | ||
d023c380 L |
9533 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9534 | && !(h->def_regular | |
9535 | && h->forced_local)) | |
c152c796 AM |
9536 | { |
9537 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9538 | have provided a definition for the undefined sym unless |
9539 | it is defined in a non-shared object and forced local. | |
9540 | */ | |
c152c796 AM |
9541 | abort (); |
9542 | } | |
9543 | ||
9544 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9545 | if (version_index == 1 || version_index == 2) | |
9546 | { | |
9547 | /* This is the base or first version. We can use it. */ | |
9548 | free (extversym); | |
9549 | free (isymbuf); | |
9550 | return TRUE; | |
9551 | } | |
9552 | } | |
9553 | ||
9554 | free (extversym); | |
9555 | free (isymbuf); | |
9556 | } | |
9557 | ||
9558 | return FALSE; | |
9559 | } | |
9560 | ||
b8871f35 L |
9561 | /* Convert ELF common symbol TYPE. */ |
9562 | ||
9563 | static int | |
9564 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9565 | { | |
9566 | /* Commom symbol can only appear in relocatable link. */ | |
9567 | if (!bfd_link_relocatable (info)) | |
9568 | abort (); | |
9569 | switch (info->elf_stt_common) | |
9570 | { | |
9571 | case unchanged: | |
9572 | break; | |
9573 | case elf_stt_common: | |
9574 | type = STT_COMMON; | |
9575 | break; | |
9576 | case no_elf_stt_common: | |
9577 | type = STT_OBJECT; | |
9578 | break; | |
9579 | } | |
9580 | return type; | |
9581 | } | |
9582 | ||
c152c796 AM |
9583 | /* Add an external symbol to the symbol table. This is called from |
9584 | the hash table traversal routine. When generating a shared object, | |
9585 | we go through the symbol table twice. The first time we output | |
9586 | anything that might have been forced to local scope in a version | |
9587 | script. The second time we output the symbols that are still | |
9588 | global symbols. */ | |
9589 | ||
9590 | static bfd_boolean | |
7686d77d | 9591 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9592 | { |
7686d77d | 9593 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9594 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9595 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9596 | bfd_boolean strip; |
9597 | Elf_Internal_Sym sym; | |
9598 | asection *input_sec; | |
9599 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9600 | long indx; |
9601 | int ret; | |
b8871f35 | 9602 | unsigned int type; |
c152c796 AM |
9603 | |
9604 | if (h->root.type == bfd_link_hash_warning) | |
9605 | { | |
9606 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9607 | if (h->root.type == bfd_link_hash_new) | |
9608 | return TRUE; | |
9609 | } | |
9610 | ||
9611 | /* Decide whether to output this symbol in this pass. */ | |
9612 | if (eoinfo->localsyms) | |
9613 | { | |
4deb8f71 | 9614 | if (!h->forced_local) |
c152c796 AM |
9615 | return TRUE; |
9616 | } | |
9617 | else | |
9618 | { | |
4deb8f71 | 9619 | if (h->forced_local) |
c152c796 AM |
9620 | return TRUE; |
9621 | } | |
9622 | ||
8b127cbc | 9623 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9624 | |
12ac1cf5 | 9625 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9626 | { |
12ac1cf5 NC |
9627 | /* If we have an undefined symbol reference here then it must have |
9628 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9629 | references in regular files have already been handled unless |
9630 | they are in unreferenced sections which are removed by garbage | |
9631 | collection). */ | |
12ac1cf5 NC |
9632 | bfd_boolean ignore_undef = FALSE; |
9633 | ||
9634 | /* Some symbols may be special in that the fact that they're | |
9635 | undefined can be safely ignored - let backend determine that. */ | |
9636 | if (bed->elf_backend_ignore_undef_symbol) | |
9637 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9638 | ||
9639 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9640 | if (!ignore_undef |
12ac1cf5 | 9641 | && h->ref_dynamic |
8b127cbc AM |
9642 | && (!h->ref_regular || flinfo->info->gc_sections) |
9643 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9644 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9645 | (*flinfo->info->callbacks->undefined_symbol) |
9646 | (flinfo->info, h->root.root.string, | |
9647 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9648 | NULL, 0, | |
9649 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9650 | |
9651 | /* Strip a global symbol defined in a discarded section. */ | |
9652 | if (h->indx == -3) | |
9653 | return TRUE; | |
c152c796 AM |
9654 | } |
9655 | ||
9656 | /* We should also warn if a forced local symbol is referenced from | |
9657 | shared libraries. */ | |
0e1862bb | 9658 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9659 | && h->forced_local |
9660 | && h->ref_dynamic | |
371a5866 | 9661 | && h->def_regular |
f5385ebf | 9662 | && !h->dynamic_def |
ee659f1f | 9663 | && h->ref_dynamic_nonweak |
8b127cbc | 9664 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9665 | { |
17d078c5 AM |
9666 | bfd *def_bfd; |
9667 | const char *msg; | |
90c984fc L |
9668 | struct elf_link_hash_entry *hi = h; |
9669 | ||
9670 | /* Check indirect symbol. */ | |
9671 | while (hi->root.type == bfd_link_hash_indirect) | |
9672 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9673 | |
9674 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 9675 | /* xgettext:c-format */ |
871b3ab2 | 9676 | msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 9677 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) |
695344c0 | 9678 | /* xgettext:c-format */ |
871b3ab2 | 9679 | msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 9680 | else |
695344c0 | 9681 | /* xgettext:c-format */ |
871b3ab2 | 9682 | msg = _("%pB: local symbol `%s' in %pB is referenced by DSO"); |
8b127cbc | 9683 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9684 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9685 | def_bfd = hi->root.u.def.section->owner; | |
c08bb8dd AM |
9686 | _bfd_error_handler (msg, flinfo->output_bfd, |
9687 | h->root.root.string, def_bfd); | |
17d078c5 | 9688 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9689 | eoinfo->failed = TRUE; |
9690 | return FALSE; | |
9691 | } | |
9692 | ||
9693 | /* We don't want to output symbols that have never been mentioned by | |
9694 | a regular file, or that we have been told to strip. However, if | |
9695 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9696 | output it. */ | |
d983c8c5 | 9697 | strip = FALSE; |
c152c796 | 9698 | if (h->indx == -2) |
d983c8c5 | 9699 | ; |
f5385ebf | 9700 | else if ((h->def_dynamic |
77cfaee6 AM |
9701 | || h->ref_dynamic |
9702 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9703 | && !h->def_regular |
9704 | && !h->ref_regular) | |
c152c796 | 9705 | strip = TRUE; |
8b127cbc | 9706 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9707 | strip = TRUE; |
8b127cbc AM |
9708 | else if (flinfo->info->strip == strip_some |
9709 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9710 | h->root.root.string, FALSE, FALSE) == NULL) |
9711 | strip = TRUE; | |
d56d55e7 AM |
9712 | else if ((h->root.type == bfd_link_hash_defined |
9713 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9714 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9715 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9716 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9717 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9718 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9719 | strip = TRUE; |
9e2278f5 AM |
9720 | else if ((h->root.type == bfd_link_hash_undefined |
9721 | || h->root.type == bfd_link_hash_undefweak) | |
9722 | && h->root.u.undef.abfd != NULL | |
9723 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9724 | strip = TRUE; | |
c152c796 | 9725 | |
b8871f35 L |
9726 | type = h->type; |
9727 | ||
c152c796 | 9728 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9729 | nothing else to do. However, if it is a forced local symbol or |
9730 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9731 | function a chance to make it dynamic. */ | |
c152c796 AM |
9732 | if (strip |
9733 | && h->dynindx == -1 | |
b8871f35 | 9734 | && type != STT_GNU_IFUNC |
f5385ebf | 9735 | && !h->forced_local) |
c152c796 AM |
9736 | return TRUE; |
9737 | ||
9738 | sym.st_value = 0; | |
9739 | sym.st_size = h->size; | |
9740 | sym.st_other = h->other; | |
c152c796 AM |
9741 | switch (h->root.type) |
9742 | { | |
9743 | default: | |
9744 | case bfd_link_hash_new: | |
9745 | case bfd_link_hash_warning: | |
9746 | abort (); | |
9747 | return FALSE; | |
9748 | ||
9749 | case bfd_link_hash_undefined: | |
9750 | case bfd_link_hash_undefweak: | |
9751 | input_sec = bfd_und_section_ptr; | |
9752 | sym.st_shndx = SHN_UNDEF; | |
9753 | break; | |
9754 | ||
9755 | case bfd_link_hash_defined: | |
9756 | case bfd_link_hash_defweak: | |
9757 | { | |
9758 | input_sec = h->root.u.def.section; | |
9759 | if (input_sec->output_section != NULL) | |
9760 | { | |
9761 | sym.st_shndx = | |
8b127cbc | 9762 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9763 | input_sec->output_section); |
9764 | if (sym.st_shndx == SHN_BAD) | |
9765 | { | |
4eca0228 | 9766 | _bfd_error_handler |
695344c0 | 9767 | /* xgettext:c-format */ |
871b3ab2 | 9768 | (_("%pB: could not find output section %pA for input section %pA"), |
8b127cbc | 9769 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9770 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9771 | eoinfo->failed = TRUE; |
9772 | return FALSE; | |
9773 | } | |
9774 | ||
9775 | /* ELF symbols in relocatable files are section relative, | |
9776 | but in nonrelocatable files they are virtual | |
9777 | addresses. */ | |
9778 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9779 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9780 | { |
9781 | sym.st_value += input_sec->output_section->vma; | |
9782 | if (h->type == STT_TLS) | |
9783 | { | |
8b127cbc | 9784 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9785 | if (tls_sec != NULL) |
9786 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9787 | } |
9788 | } | |
9789 | } | |
9790 | else | |
9791 | { | |
9792 | BFD_ASSERT (input_sec->owner == NULL | |
9793 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9794 | sym.st_shndx = SHN_UNDEF; | |
9795 | input_sec = bfd_und_section_ptr; | |
9796 | } | |
9797 | } | |
9798 | break; | |
9799 | ||
9800 | case bfd_link_hash_common: | |
9801 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9802 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9803 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9804 | break; | |
9805 | ||
9806 | case bfd_link_hash_indirect: | |
9807 | /* These symbols are created by symbol versioning. They point | |
9808 | to the decorated version of the name. For example, if the | |
9809 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9810 | foo is used with no version, then we add an indirect symbol | |
9811 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9812 | since the indirected symbol is already in the hash table. */ | |
9813 | return TRUE; | |
9814 | } | |
9815 | ||
b8871f35 L |
9816 | if (type == STT_COMMON || type == STT_OBJECT) |
9817 | switch (h->root.type) | |
9818 | { | |
9819 | case bfd_link_hash_common: | |
9820 | type = elf_link_convert_common_type (flinfo->info, type); | |
9821 | break; | |
9822 | case bfd_link_hash_defined: | |
9823 | case bfd_link_hash_defweak: | |
9824 | if (bed->common_definition (&sym)) | |
9825 | type = elf_link_convert_common_type (flinfo->info, type); | |
9826 | else | |
9827 | type = STT_OBJECT; | |
9828 | break; | |
9829 | case bfd_link_hash_undefined: | |
9830 | case bfd_link_hash_undefweak: | |
9831 | break; | |
9832 | default: | |
9833 | abort (); | |
9834 | } | |
9835 | ||
4deb8f71 | 9836 | if (h->forced_local) |
b8871f35 L |
9837 | { |
9838 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9839 | /* Turn off visibility on local symbol. */ | |
9840 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9841 | } | |
9842 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9843 | else if (h->unique_global && h->def_regular) | |
9844 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
9845 | else if (h->root.type == bfd_link_hash_undefweak | |
9846 | || h->root.type == bfd_link_hash_defweak) | |
9847 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
9848 | else | |
9849 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
9850 | sym.st_target_internal = h->target_internal; | |
9851 | ||
c152c796 AM |
9852 | /* Give the processor backend a chance to tweak the symbol value, |
9853 | and also to finish up anything that needs to be done for this | |
9854 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9855 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9856 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9857 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9858 | && h->def_regular |
0e1862bb | 9859 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
9860 | || ((h->dynindx != -1 |
9861 | || h->forced_local) | |
0e1862bb | 9862 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
9863 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9864 | || h->root.type != bfd_link_hash_undefweak)) | |
9865 | || !h->forced_local) | |
8b127cbc | 9866 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9867 | { |
9868 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9869 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9870 | { |
9871 | eoinfo->failed = TRUE; | |
9872 | return FALSE; | |
9873 | } | |
9874 | } | |
9875 | ||
9876 | /* If we are marking the symbol as undefined, and there are no | |
9877 | non-weak references to this symbol from a regular object, then | |
9878 | mark the symbol as weak undefined; if there are non-weak | |
9879 | references, mark the symbol as strong. We can't do this earlier, | |
9880 | because it might not be marked as undefined until the | |
9881 | finish_dynamic_symbol routine gets through with it. */ | |
9882 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9883 | && h->ref_regular |
c152c796 AM |
9884 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9885 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9886 | { | |
9887 | int bindtype; | |
b8871f35 | 9888 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
9889 | |
9890 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9891 | if (type == STT_GNU_IFUNC) | |
9892 | type = STT_FUNC; | |
c152c796 | 9893 | |
f5385ebf | 9894 | if (h->ref_regular_nonweak) |
c152c796 AM |
9895 | bindtype = STB_GLOBAL; |
9896 | else | |
9897 | bindtype = STB_WEAK; | |
2955ec4c | 9898 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
9899 | } |
9900 | ||
bda987c2 CD |
9901 | /* If this is a symbol defined in a dynamic library, don't use the |
9902 | symbol size from the dynamic library. Relinking an executable | |
9903 | against a new library may introduce gratuitous changes in the | |
9904 | executable's symbols if we keep the size. */ | |
9905 | if (sym.st_shndx == SHN_UNDEF | |
9906 | && !h->def_regular | |
9907 | && h->def_dynamic) | |
9908 | sym.st_size = 0; | |
9909 | ||
c152c796 AM |
9910 | /* If a non-weak symbol with non-default visibility is not defined |
9911 | locally, it is a fatal error. */ | |
0e1862bb | 9912 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
9913 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
9914 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
9915 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 9916 | && !h->def_regular) |
c152c796 | 9917 | { |
17d078c5 AM |
9918 | const char *msg; |
9919 | ||
9920 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 9921 | /* xgettext:c-format */ |
871b3ab2 | 9922 | msg = _("%pB: protected symbol `%s' isn't defined"); |
17d078c5 | 9923 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) |
695344c0 | 9924 | /* xgettext:c-format */ |
871b3ab2 | 9925 | msg = _("%pB: internal symbol `%s' isn't defined"); |
17d078c5 | 9926 | else |
695344c0 | 9927 | /* xgettext:c-format */ |
871b3ab2 | 9928 | msg = _("%pB: hidden symbol `%s' isn't defined"); |
4eca0228 | 9929 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 9930 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9931 | eoinfo->failed = TRUE; |
9932 | return FALSE; | |
9933 | } | |
9934 | ||
9935 | /* If this symbol should be put in the .dynsym section, then put it | |
9936 | there now. We already know the symbol index. We also fill in | |
9937 | the entry in the .hash section. */ | |
cae1fbbb | 9938 | if (elf_hash_table (flinfo->info)->dynsym != NULL |
202e2356 | 9939 | && h->dynindx != -1 |
8b127cbc | 9940 | && elf_hash_table (flinfo->info)->dynamic_sections_created) |
c152c796 | 9941 | { |
c152c796 AM |
9942 | bfd_byte *esym; |
9943 | ||
90c984fc L |
9944 | /* Since there is no version information in the dynamic string, |
9945 | if there is no version info in symbol version section, we will | |
1659f720 | 9946 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 9947 | by shared library, or not bound locally. */ |
1659f720 | 9948 | if (h->verinfo.verdef == NULL |
0e1862bb | 9949 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
9950 | || h->ref_dynamic |
9951 | || !h->def_regular)) | |
90c984fc L |
9952 | { |
9953 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
9954 | ||
9955 | if (p && p [1] != '\0') | |
9956 | { | |
4eca0228 | 9957 | _bfd_error_handler |
695344c0 | 9958 | /* xgettext:c-format */ |
9793eb77 | 9959 | (_("%pB: no symbol version section for versioned symbol `%s'"), |
90c984fc L |
9960 | flinfo->output_bfd, h->root.root.string); |
9961 | eoinfo->failed = TRUE; | |
9962 | return FALSE; | |
9963 | } | |
9964 | } | |
9965 | ||
c152c796 | 9966 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
9967 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
9968 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 9969 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
9970 | { |
9971 | eoinfo->failed = TRUE; | |
9972 | return FALSE; | |
9973 | } | |
8b127cbc | 9974 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 9975 | |
8b127cbc | 9976 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
9977 | { |
9978 | size_t hash_entry_size; | |
9979 | bfd_byte *bucketpos; | |
9980 | bfd_vma chain; | |
41198d0c L |
9981 | size_t bucketcount; |
9982 | size_t bucket; | |
9983 | ||
8b127cbc | 9984 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 9985 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
9986 | |
9987 | hash_entry_size | |
8b127cbc AM |
9988 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
9989 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 9990 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
9991 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
9992 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
9993 | bucketpos); | |
9994 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
9995 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
9996 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
9997 | } | |
c152c796 | 9998 | |
8b127cbc | 9999 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
10000 | { |
10001 | Elf_Internal_Versym iversym; | |
10002 | Elf_External_Versym *eversym; | |
10003 | ||
f5385ebf | 10004 | if (!h->def_regular) |
c152c796 | 10005 | { |
7b20f099 AM |
10006 | if (h->verinfo.verdef == NULL |
10007 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
10008 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
10009 | iversym.vs_vers = 0; |
10010 | else | |
10011 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
10012 | } | |
10013 | else | |
10014 | { | |
10015 | if (h->verinfo.vertree == NULL) | |
10016 | iversym.vs_vers = 1; | |
10017 | else | |
10018 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 10019 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 10020 | iversym.vs_vers++; |
c152c796 AM |
10021 | } |
10022 | ||
422f1182 | 10023 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 10024 | defined locally. */ |
422f1182 | 10025 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
10026 | iversym.vs_vers |= VERSYM_HIDDEN; |
10027 | ||
8b127cbc | 10028 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 10029 | eversym += h->dynindx; |
8b127cbc | 10030 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
10031 | } |
10032 | } | |
10033 | ||
d983c8c5 AM |
10034 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
10035 | strip it from .symtab too. Obviously we can't do this for | |
10036 | relocatable output or when needed for --emit-relocs. */ | |
10037 | else if (input_sec == bfd_und_section_ptr | |
10038 | && h->indx != -2 | |
66cae560 NC |
10039 | /* PR 22319 Do not strip global undefined symbols marked as being needed. */ |
10040 | && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) | |
0e1862bb | 10041 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 | 10042 | return TRUE; |
66cae560 | 10043 | |
d983c8c5 AM |
10044 | /* Also strip others that we couldn't earlier due to dynamic symbol |
10045 | processing. */ | |
10046 | if (strip) | |
10047 | return TRUE; | |
10048 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
10049 | return TRUE; |
10050 | ||
2ec55de3 AM |
10051 | /* Output a FILE symbol so that following locals are not associated |
10052 | with the wrong input file. We need one for forced local symbols | |
10053 | if we've seen more than one FILE symbol or when we have exactly | |
10054 | one FILE symbol but global symbols are present in a file other | |
10055 | than the one with the FILE symbol. We also need one if linker | |
10056 | defined symbols are present. In practice these conditions are | |
10057 | always met, so just emit the FILE symbol unconditionally. */ | |
10058 | if (eoinfo->localsyms | |
10059 | && !eoinfo->file_sym_done | |
10060 | && eoinfo->flinfo->filesym_count != 0) | |
10061 | { | |
10062 | Elf_Internal_Sym fsym; | |
10063 | ||
10064 | memset (&fsym, 0, sizeof (fsym)); | |
10065 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10066 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10067 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
10068 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
10069 | return FALSE; |
10070 | ||
10071 | eoinfo->file_sym_done = TRUE; | |
10072 | } | |
10073 | ||
8b127cbc | 10074 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
10075 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
10076 | input_sec, h); | |
6e0b88f1 | 10077 | if (ret == 0) |
c152c796 AM |
10078 | { |
10079 | eoinfo->failed = TRUE; | |
10080 | return FALSE; | |
10081 | } | |
6e0b88f1 AM |
10082 | else if (ret == 1) |
10083 | h->indx = indx; | |
10084 | else if (h->indx == -2) | |
10085 | abort(); | |
c152c796 AM |
10086 | |
10087 | return TRUE; | |
10088 | } | |
10089 | ||
cdd3575c AM |
10090 | /* Return TRUE if special handling is done for relocs in SEC against |
10091 | symbols defined in discarded sections. */ | |
10092 | ||
c152c796 AM |
10093 | static bfd_boolean |
10094 | elf_section_ignore_discarded_relocs (asection *sec) | |
10095 | { | |
10096 | const struct elf_backend_data *bed; | |
10097 | ||
cdd3575c AM |
10098 | switch (sec->sec_info_type) |
10099 | { | |
dbaa2011 AM |
10100 | case SEC_INFO_TYPE_STABS: |
10101 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 10102 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
10103 | return TRUE; |
10104 | default: | |
10105 | break; | |
10106 | } | |
c152c796 AM |
10107 | |
10108 | bed = get_elf_backend_data (sec->owner); | |
10109 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
10110 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
10111 | return TRUE; | |
10112 | ||
10113 | return FALSE; | |
10114 | } | |
10115 | ||
9e66c942 AM |
10116 | /* Return a mask saying how ld should treat relocations in SEC against |
10117 | symbols defined in discarded sections. If this function returns | |
10118 | COMPLAIN set, ld will issue a warning message. If this function | |
10119 | returns PRETEND set, and the discarded section was link-once and the | |
10120 | same size as the kept link-once section, ld will pretend that the | |
10121 | symbol was actually defined in the kept section. Otherwise ld will | |
10122 | zero the reloc (at least that is the intent, but some cooperation by | |
10123 | the target dependent code is needed, particularly for REL targets). */ | |
10124 | ||
8a696751 AM |
10125 | unsigned int |
10126 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 10127 | { |
9e66c942 | 10128 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 10129 | return PRETEND; |
cdd3575c AM |
10130 | |
10131 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 10132 | return 0; |
cdd3575c AM |
10133 | |
10134 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 10135 | return 0; |
cdd3575c | 10136 | |
9e66c942 | 10137 | return COMPLAIN | PRETEND; |
cdd3575c AM |
10138 | } |
10139 | ||
3d7f7666 L |
10140 | /* Find a match between a section and a member of a section group. */ |
10141 | ||
10142 | static asection * | |
c0f00686 L |
10143 | match_group_member (asection *sec, asection *group, |
10144 | struct bfd_link_info *info) | |
3d7f7666 L |
10145 | { |
10146 | asection *first = elf_next_in_group (group); | |
10147 | asection *s = first; | |
10148 | ||
10149 | while (s != NULL) | |
10150 | { | |
c0f00686 | 10151 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
10152 | return s; |
10153 | ||
83180ade | 10154 | s = elf_next_in_group (s); |
3d7f7666 L |
10155 | if (s == first) |
10156 | break; | |
10157 | } | |
10158 | ||
10159 | return NULL; | |
10160 | } | |
10161 | ||
01b3c8ab | 10162 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
10163 | to replace it. Return the replacement if it is OK. Otherwise return |
10164 | NULL. */ | |
01b3c8ab L |
10165 | |
10166 | asection * | |
c0f00686 | 10167 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
10168 | { |
10169 | asection *kept; | |
10170 | ||
10171 | kept = sec->kept_section; | |
10172 | if (kept != NULL) | |
10173 | { | |
c2370991 | 10174 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 10175 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
10176 | if (kept != NULL |
10177 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
10178 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 10179 | kept = NULL; |
c2370991 | 10180 | sec->kept_section = kept; |
01b3c8ab L |
10181 | } |
10182 | return kept; | |
10183 | } | |
10184 | ||
c152c796 AM |
10185 | /* Link an input file into the linker output file. This function |
10186 | handles all the sections and relocations of the input file at once. | |
10187 | This is so that we only have to read the local symbols once, and | |
10188 | don't have to keep them in memory. */ | |
10189 | ||
10190 | static bfd_boolean | |
8b127cbc | 10191 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 10192 | { |
ece5ef60 | 10193 | int (*relocate_section) |
c152c796 AM |
10194 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
10195 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
10196 | bfd *output_bfd; | |
10197 | Elf_Internal_Shdr *symtab_hdr; | |
10198 | size_t locsymcount; | |
10199 | size_t extsymoff; | |
10200 | Elf_Internal_Sym *isymbuf; | |
10201 | Elf_Internal_Sym *isym; | |
10202 | Elf_Internal_Sym *isymend; | |
10203 | long *pindex; | |
10204 | asection **ppsection; | |
10205 | asection *o; | |
10206 | const struct elf_backend_data *bed; | |
c152c796 | 10207 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
10208 | bfd_size_type address_size; |
10209 | bfd_vma r_type_mask; | |
10210 | int r_sym_shift; | |
ffbc01cc | 10211 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 10212 | |
8b127cbc | 10213 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
10214 | bed = get_elf_backend_data (output_bfd); |
10215 | relocate_section = bed->elf_backend_relocate_section; | |
10216 | ||
10217 | /* If this is a dynamic object, we don't want to do anything here: | |
10218 | we don't want the local symbols, and we don't want the section | |
10219 | contents. */ | |
10220 | if ((input_bfd->flags & DYNAMIC) != 0) | |
10221 | return TRUE; | |
10222 | ||
c152c796 AM |
10223 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
10224 | if (elf_bad_symtab (input_bfd)) | |
10225 | { | |
10226 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10227 | extsymoff = 0; | |
10228 | } | |
10229 | else | |
10230 | { | |
10231 | locsymcount = symtab_hdr->sh_info; | |
10232 | extsymoff = symtab_hdr->sh_info; | |
10233 | } | |
10234 | ||
10235 | /* Read the local symbols. */ | |
10236 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10237 | if (isymbuf == NULL && locsymcount != 0) | |
10238 | { | |
10239 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
10240 | flinfo->internal_syms, |
10241 | flinfo->external_syms, | |
10242 | flinfo->locsym_shndx); | |
c152c796 AM |
10243 | if (isymbuf == NULL) |
10244 | return FALSE; | |
10245 | } | |
10246 | ||
10247 | /* Find local symbol sections and adjust values of symbols in | |
10248 | SEC_MERGE sections. Write out those local symbols we know are | |
10249 | going into the output file. */ | |
10250 | isymend = isymbuf + locsymcount; | |
8b127cbc | 10251 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
10252 | isym < isymend; |
10253 | isym++, pindex++, ppsection++) | |
10254 | { | |
10255 | asection *isec; | |
10256 | const char *name; | |
10257 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
10258 | long indx; |
10259 | int ret; | |
c152c796 AM |
10260 | |
10261 | *pindex = -1; | |
10262 | ||
10263 | if (elf_bad_symtab (input_bfd)) | |
10264 | { | |
10265 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
10266 | { | |
10267 | *ppsection = NULL; | |
10268 | continue; | |
10269 | } | |
10270 | } | |
10271 | ||
10272 | if (isym->st_shndx == SHN_UNDEF) | |
10273 | isec = bfd_und_section_ptr; | |
c152c796 AM |
10274 | else if (isym->st_shndx == SHN_ABS) |
10275 | isec = bfd_abs_section_ptr; | |
10276 | else if (isym->st_shndx == SHN_COMMON) | |
10277 | isec = bfd_com_section_ptr; | |
10278 | else | |
10279 | { | |
cb33740c AM |
10280 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
10281 | if (isec == NULL) | |
10282 | { | |
10283 | /* Don't attempt to output symbols with st_shnx in the | |
10284 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
10285 | *ppsection = NULL; | |
10286 | continue; | |
10287 | } | |
dbaa2011 | 10288 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
10289 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
10290 | isym->st_value = | |
10291 | _bfd_merged_section_offset (output_bfd, &isec, | |
10292 | elf_section_data (isec)->sec_info, | |
10293 | isym->st_value); | |
c152c796 AM |
10294 | } |
10295 | ||
10296 | *ppsection = isec; | |
10297 | ||
d983c8c5 AM |
10298 | /* Don't output the first, undefined, symbol. In fact, don't |
10299 | output any undefined local symbol. */ | |
10300 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
10301 | continue; |
10302 | ||
10303 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
10304 | { | |
10305 | /* We never output section symbols. Instead, we use the | |
10306 | section symbol of the corresponding section in the output | |
10307 | file. */ | |
10308 | continue; | |
10309 | } | |
10310 | ||
10311 | /* If we are stripping all symbols, we don't want to output this | |
10312 | one. */ | |
8b127cbc | 10313 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10314 | continue; |
10315 | ||
10316 | /* If we are discarding all local symbols, we don't want to | |
10317 | output this one. If we are generating a relocatable output | |
10318 | file, then some of the local symbols may be required by | |
10319 | relocs; we output them below as we discover that they are | |
10320 | needed. */ | |
8b127cbc | 10321 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
10322 | continue; |
10323 | ||
10324 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
10325 | discarding, we don't need to keep it. */ |
10326 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
10327 | && isym->st_shndx < SHN_LORESERVE |
10328 | && bfd_section_removed_from_list (output_bfd, | |
10329 | isec->output_section)) | |
e75a280b L |
10330 | continue; |
10331 | ||
c152c796 AM |
10332 | /* Get the name of the symbol. */ |
10333 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
10334 | isym->st_name); | |
10335 | if (name == NULL) | |
10336 | return FALSE; | |
10337 | ||
10338 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
10339 | if ((flinfo->info->strip == strip_some |
10340 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 10341 | == NULL)) |
8b127cbc | 10342 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10343 | && (isec->flags & SEC_MERGE) |
10344 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10345 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10346 | && bfd_is_local_label_name (input_bfd, name))) |
10347 | continue; | |
10348 | ||
ffbc01cc AM |
10349 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10350 | { | |
ce875075 AM |
10351 | if (input_bfd->lto_output) |
10352 | /* -flto puts a temp file name here. This means builds | |
10353 | are not reproducible. Discard the symbol. */ | |
10354 | continue; | |
ffbc01cc AM |
10355 | have_file_sym = TRUE; |
10356 | flinfo->filesym_count += 1; | |
10357 | } | |
10358 | if (!have_file_sym) | |
10359 | { | |
10360 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10361 | FILE symbols to determine the source file for local | |
10362 | function symbols. Provide a FILE symbol here if input | |
10363 | files lack such, so that their symbols won't be | |
10364 | associated with a previous input file. It's not the | |
10365 | source file, but the best we can do. */ | |
10366 | have_file_sym = TRUE; | |
10367 | flinfo->filesym_count += 1; | |
10368 | memset (&osym, 0, sizeof (osym)); | |
10369 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10370 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10371 | if (!elf_link_output_symstrtab (flinfo, |
10372 | (input_bfd->lto_output ? NULL | |
10373 | : input_bfd->filename), | |
10374 | &osym, bfd_abs_section_ptr, | |
10375 | NULL)) | |
ffbc01cc AM |
10376 | return FALSE; |
10377 | } | |
10378 | ||
c152c796 AM |
10379 | osym = *isym; |
10380 | ||
10381 | /* Adjust the section index for the output file. */ | |
10382 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10383 | isec->output_section); | |
10384 | if (osym.st_shndx == SHN_BAD) | |
10385 | return FALSE; | |
10386 | ||
c152c796 AM |
10387 | /* ELF symbols in relocatable files are section relative, but |
10388 | in executable files they are virtual addresses. Note that | |
10389 | this code assumes that all ELF sections have an associated | |
10390 | BFD section with a reasonable value for output_offset; below | |
10391 | we assume that they also have a reasonable value for | |
10392 | output_section. Any special sections must be set up to meet | |
10393 | these requirements. */ | |
10394 | osym.st_value += isec->output_offset; | |
0e1862bb | 10395 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10396 | { |
10397 | osym.st_value += isec->output_section->vma; | |
10398 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10399 | { | |
10400 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
8b127cbc AM |
10401 | BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); |
10402 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
c152c796 AM |
10403 | } |
10404 | } | |
10405 | ||
6e0b88f1 | 10406 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10407 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10408 | if (ret == 0) |
c152c796 | 10409 | return FALSE; |
6e0b88f1 AM |
10410 | else if (ret == 1) |
10411 | *pindex = indx; | |
c152c796 AM |
10412 | } |
10413 | ||
310fd250 L |
10414 | if (bed->s->arch_size == 32) |
10415 | { | |
10416 | r_type_mask = 0xff; | |
10417 | r_sym_shift = 8; | |
10418 | address_size = 4; | |
10419 | } | |
10420 | else | |
10421 | { | |
10422 | r_type_mask = 0xffffffff; | |
10423 | r_sym_shift = 32; | |
10424 | address_size = 8; | |
10425 | } | |
10426 | ||
c152c796 AM |
10427 | /* Relocate the contents of each section. */ |
10428 | sym_hashes = elf_sym_hashes (input_bfd); | |
10429 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10430 | { | |
10431 | bfd_byte *contents; | |
10432 | ||
10433 | if (! o->linker_mark) | |
10434 | { | |
10435 | /* This section was omitted from the link. */ | |
10436 | continue; | |
10437 | } | |
10438 | ||
7bdf4127 | 10439 | if (!flinfo->info->resolve_section_groups |
bcacc0f5 AM |
10440 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10441 | { | |
10442 | /* Deal with the group signature symbol. */ | |
10443 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10444 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10445 | asection *osec = o->output_section; | |
10446 | ||
7bdf4127 | 10447 | BFD_ASSERT (bfd_link_relocatable (flinfo->info)); |
bcacc0f5 AM |
10448 | if (symndx >= locsymcount |
10449 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10450 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10451 | { |
10452 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10453 | while (h->root.type == bfd_link_hash_indirect | |
10454 | || h->root.type == bfd_link_hash_warning) | |
10455 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10456 | /* Arrange for symbol to be output. */ | |
10457 | h->indx = -2; | |
10458 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10459 | } | |
10460 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10461 | { | |
10462 | /* We'll use the output section target_index. */ | |
8b127cbc | 10463 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10464 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10465 | } | |
10466 | else | |
10467 | { | |
8b127cbc | 10468 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10469 | { |
10470 | /* Otherwise output the local symbol now. */ | |
10471 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10472 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10473 | const char *name; |
6e0b88f1 AM |
10474 | long indx; |
10475 | int ret; | |
bcacc0f5 AM |
10476 | |
10477 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10478 | symtab_hdr->sh_link, | |
10479 | sym.st_name); | |
10480 | if (name == NULL) | |
10481 | return FALSE; | |
10482 | ||
10483 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10484 | sec); | |
10485 | if (sym.st_shndx == SHN_BAD) | |
10486 | return FALSE; | |
10487 | ||
10488 | sym.st_value += o->output_offset; | |
10489 | ||
6e0b88f1 | 10490 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10491 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10492 | NULL); | |
6e0b88f1 | 10493 | if (ret == 0) |
bcacc0f5 | 10494 | return FALSE; |
6e0b88f1 | 10495 | else if (ret == 1) |
8b127cbc | 10496 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10497 | else |
10498 | abort (); | |
bcacc0f5 AM |
10499 | } |
10500 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10501 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10502 | } |
10503 | } | |
10504 | ||
c152c796 | 10505 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10506 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10507 | continue; |
10508 | ||
10509 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10510 | { | |
10511 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10512 | or somesuch. */ | |
10513 | continue; | |
10514 | } | |
10515 | ||
10516 | /* Get the contents of the section. They have been cached by a | |
10517 | relaxation routine. Note that o is a section in an input | |
10518 | file, so the contents field will not have been set by any of | |
10519 | the routines which work on output files. */ | |
10520 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10521 | { |
10522 | contents = elf_section_data (o)->this_hdr.contents; | |
10523 | if (bed->caches_rawsize | |
10524 | && o->rawsize != 0 | |
10525 | && o->rawsize < o->size) | |
10526 | { | |
10527 | memcpy (flinfo->contents, contents, o->rawsize); | |
10528 | contents = flinfo->contents; | |
10529 | } | |
10530 | } | |
c152c796 AM |
10531 | else |
10532 | { | |
8b127cbc | 10533 | contents = flinfo->contents; |
4a114e3e | 10534 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10535 | return FALSE; |
10536 | } | |
10537 | ||
10538 | if ((o->flags & SEC_RELOC) != 0) | |
10539 | { | |
10540 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10541 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10542 | int action_discarded; |
ece5ef60 | 10543 | int ret; |
c152c796 AM |
10544 | |
10545 | /* Get the swapped relocs. */ | |
10546 | internal_relocs | |
8b127cbc AM |
10547 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10548 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10549 | if (internal_relocs == NULL |
10550 | && o->reloc_count > 0) | |
10551 | return FALSE; | |
10552 | ||
310fd250 L |
10553 | /* We need to reverse-copy input .ctors/.dtors sections if |
10554 | they are placed in .init_array/.finit_array for output. */ | |
10555 | if (o->size > address_size | |
10556 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10557 | && strcmp (o->output_section->name, | |
10558 | ".init_array") == 0) | |
10559 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10560 | && strcmp (o->output_section->name, | |
10561 | ".fini_array") == 0)) | |
10562 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10563 | { |
056bafd4 MR |
10564 | if (o->size * bed->s->int_rels_per_ext_rel |
10565 | != o->reloc_count * address_size) | |
310fd250 | 10566 | { |
4eca0228 | 10567 | _bfd_error_handler |
695344c0 | 10568 | /* xgettext:c-format */ |
871b3ab2 | 10569 | (_("error: %pB: size of section %pA is not " |
310fd250 L |
10570 | "multiple of address size"), |
10571 | input_bfd, o); | |
8c6716e5 | 10572 | bfd_set_error (bfd_error_bad_value); |
310fd250 L |
10573 | return FALSE; |
10574 | } | |
10575 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10576 | } |
10577 | ||
0f02bbd9 | 10578 | action_discarded = -1; |
c152c796 | 10579 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10580 | action_discarded = (*bed->action_discarded) (o); |
10581 | ||
10582 | /* Run through the relocs evaluating complex reloc symbols and | |
10583 | looking for relocs against symbols from discarded sections | |
10584 | or section symbols from removed link-once sections. | |
10585 | Complain about relocs against discarded sections. Zero | |
10586 | relocs against removed link-once sections. */ | |
10587 | ||
10588 | rel = internal_relocs; | |
056bafd4 | 10589 | relend = rel + o->reloc_count; |
0f02bbd9 | 10590 | for ( ; rel < relend; rel++) |
c152c796 | 10591 | { |
0f02bbd9 AM |
10592 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10593 | unsigned int s_type; | |
10594 | asection **ps, *sec; | |
10595 | struct elf_link_hash_entry *h = NULL; | |
10596 | const char *sym_name; | |
c152c796 | 10597 | |
0f02bbd9 AM |
10598 | if (r_symndx == STN_UNDEF) |
10599 | continue; | |
c152c796 | 10600 | |
0f02bbd9 AM |
10601 | if (r_symndx >= locsymcount |
10602 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10603 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10604 | { |
10605 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10606 | |
0f02bbd9 AM |
10607 | /* Badly formatted input files can contain relocs that |
10608 | reference non-existant symbols. Check here so that | |
10609 | we do not seg fault. */ | |
10610 | if (h == NULL) | |
c152c796 | 10611 | { |
4eca0228 | 10612 | _bfd_error_handler |
695344c0 | 10613 | /* xgettext:c-format */ |
2dcf00ce | 10614 | (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA " |
0f02bbd9 | 10615 | "that references a non-existent global symbol"), |
2dcf00ce | 10616 | input_bfd, (uint64_t) rel->r_info, o); |
0f02bbd9 AM |
10617 | bfd_set_error (bfd_error_bad_value); |
10618 | return FALSE; | |
10619 | } | |
3b36f7e6 | 10620 | |
0f02bbd9 AM |
10621 | while (h->root.type == bfd_link_hash_indirect |
10622 | || h->root.type == bfd_link_hash_warning) | |
10623 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10624 | |
0f02bbd9 | 10625 | s_type = h->type; |
cdd3575c | 10626 | |
9e2dec47 | 10627 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10628 | mark the symbol as undefined. Note that the |
10629 | linker may attach linker created dynamic sections | |
10630 | to the plugin bfd. Symbols defined in linker | |
10631 | created sections are not plugin symbols. */ | |
bc4e12de | 10632 | if ((h->root.non_ir_ref_regular |
4070765b | 10633 | || h->root.non_ir_ref_dynamic) |
9e2dec47 L |
10634 | && (h->root.type == bfd_link_hash_defined |
10635 | || h->root.type == bfd_link_hash_defweak) | |
10636 | && (h->root.u.def.section->flags | |
10637 | & SEC_LINKER_CREATED) == 0 | |
10638 | && h->root.u.def.section->owner != NULL | |
10639 | && (h->root.u.def.section->owner->flags | |
10640 | & BFD_PLUGIN) != 0) | |
10641 | { | |
10642 | h->root.type = bfd_link_hash_undefined; | |
10643 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10644 | } | |
10645 | ||
0f02bbd9 AM |
10646 | ps = NULL; |
10647 | if (h->root.type == bfd_link_hash_defined | |
10648 | || h->root.type == bfd_link_hash_defweak) | |
10649 | ps = &h->root.u.def.section; | |
10650 | ||
10651 | sym_name = h->root.root.string; | |
10652 | } | |
10653 | else | |
10654 | { | |
10655 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10656 | ||
10657 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10658 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10659 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10660 | sym, *ps); | |
10661 | } | |
c152c796 | 10662 | |
c301e700 | 10663 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10664 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10665 | { |
10666 | bfd_vma val; | |
10667 | bfd_vma dot = (rel->r_offset | |
10668 | + o->output_offset + o->output_section->vma); | |
10669 | #ifdef DEBUG | |
10670 | printf ("Encountered a complex symbol!"); | |
10671 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10672 | input_bfd->filename, o->name, |
10673 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10674 | printf (" symbol: idx %8.8lx, name %s\n", |
10675 | r_symndx, sym_name); | |
10676 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10677 | (unsigned long) rel->r_info, | |
10678 | (unsigned long) rel->r_offset); | |
10679 | #endif | |
8b127cbc | 10680 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10681 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10682 | return FALSE; | |
10683 | ||
10684 | /* Symbol evaluated OK. Update to absolute value. */ | |
10685 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10686 | r_symndx, val); | |
10687 | continue; | |
10688 | } | |
10689 | ||
10690 | if (action_discarded != -1 && ps != NULL) | |
10691 | { | |
cdd3575c AM |
10692 | /* Complain if the definition comes from a |
10693 | discarded section. */ | |
dbaa2011 | 10694 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10695 | { |
cf35638d | 10696 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10697 | if (action_discarded & COMPLAIN) |
8b127cbc | 10698 | (*flinfo->info->callbacks->einfo) |
695344c0 | 10699 | /* xgettext:c-format */ |
871b3ab2 AM |
10700 | (_("%X`%s' referenced in section `%pA' of %pB: " |
10701 | "defined in discarded section `%pA' of %pB\n"), | |
e1fffbe6 | 10702 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10703 | |
87e5235d | 10704 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10705 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10706 | really defined in the kept linkonce section. |
10707 | FIXME: This is quite broken. Modifying the | |
10708 | symbol here means we will be changing all later | |
e0ae6d6f | 10709 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10710 | if (action_discarded & PRETEND) |
87e5235d | 10711 | { |
01b3c8ab L |
10712 | asection *kept; |
10713 | ||
c0f00686 | 10714 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10715 | flinfo->info); |
01b3c8ab | 10716 | if (kept != NULL) |
87e5235d AM |
10717 | { |
10718 | *ps = kept; | |
10719 | continue; | |
10720 | } | |
10721 | } | |
c152c796 AM |
10722 | } |
10723 | } | |
10724 | } | |
10725 | ||
10726 | /* Relocate the section by invoking a back end routine. | |
10727 | ||
10728 | The back end routine is responsible for adjusting the | |
10729 | section contents as necessary, and (if using Rela relocs | |
10730 | and generating a relocatable output file) adjusting the | |
10731 | reloc addend as necessary. | |
10732 | ||
10733 | The back end routine does not have to worry about setting | |
10734 | the reloc address or the reloc symbol index. | |
10735 | ||
10736 | The back end routine is given a pointer to the swapped in | |
10737 | internal symbols, and can access the hash table entries | |
10738 | for the external symbols via elf_sym_hashes (input_bfd). | |
10739 | ||
10740 | When generating relocatable output, the back end routine | |
10741 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10742 | output symbol is going to be a section symbol | |
10743 | corresponding to the output section, which will require | |
10744 | the addend to be adjusted. */ | |
10745 | ||
8b127cbc | 10746 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10747 | input_bfd, o, contents, |
10748 | internal_relocs, | |
10749 | isymbuf, | |
8b127cbc | 10750 | flinfo->sections); |
ece5ef60 | 10751 | if (!ret) |
c152c796 AM |
10752 | return FALSE; |
10753 | ||
ece5ef60 | 10754 | if (ret == 2 |
0e1862bb | 10755 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10756 | || flinfo->info->emitrelocations) |
c152c796 AM |
10757 | { |
10758 | Elf_Internal_Rela *irela; | |
d4730f92 | 10759 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10760 | bfd_vma last_offset; |
10761 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10762 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10763 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10764 | unsigned int next_erel; |
c152c796 | 10765 | bfd_boolean rela_normal; |
d4730f92 | 10766 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10767 | |
d4730f92 BS |
10768 | esdi = elf_section_data (o); |
10769 | esdo = elf_section_data (o->output_section); | |
10770 | rela_normal = FALSE; | |
c152c796 AM |
10771 | |
10772 | /* Adjust the reloc addresses and symbol indices. */ | |
10773 | ||
10774 | irela = internal_relocs; | |
056bafd4 | 10775 | irelaend = irela + o->reloc_count; |
d4730f92 BS |
10776 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10777 | /* We start processing the REL relocs, if any. When we reach | |
10778 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10779 | irelamid = irela; | |
10780 | if (esdi->rel.hdr != NULL) | |
10781 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10782 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10783 | rel_hash_list = rel_hash; |
d4730f92 | 10784 | rela_hash_list = NULL; |
c152c796 | 10785 | last_offset = o->output_offset; |
0e1862bb | 10786 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10787 | last_offset += o->output_section->vma; |
10788 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10789 | { | |
10790 | unsigned long r_symndx; | |
10791 | asection *sec; | |
10792 | Elf_Internal_Sym sym; | |
10793 | ||
10794 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10795 | { | |
10796 | rel_hash++; | |
10797 | next_erel = 0; | |
10798 | } | |
10799 | ||
d4730f92 BS |
10800 | if (irela == irelamid) |
10801 | { | |
10802 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10803 | rela_hash_list = rel_hash; | |
10804 | rela_normal = bed->rela_normal; | |
10805 | } | |
10806 | ||
c152c796 | 10807 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10808 | flinfo->info, o, |
c152c796 AM |
10809 | irela->r_offset); |
10810 | if (irela->r_offset >= (bfd_vma) -2) | |
10811 | { | |
10812 | /* This is a reloc for a deleted entry or somesuch. | |
10813 | Turn it into an R_*_NONE reloc, at the same | |
10814 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10815 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10816 | being ordered. */ |
10817 | irela->r_offset = last_offset; | |
10818 | irela->r_info = 0; | |
10819 | irela->r_addend = 0; | |
10820 | continue; | |
10821 | } | |
10822 | ||
10823 | irela->r_offset += o->output_offset; | |
10824 | ||
10825 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10826 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10827 | irela->r_offset += o->output_section->vma; |
10828 | ||
10829 | last_offset = irela->r_offset; | |
10830 | ||
10831 | r_symndx = irela->r_info >> r_sym_shift; | |
10832 | if (r_symndx == STN_UNDEF) | |
10833 | continue; | |
10834 | ||
10835 | if (r_symndx >= locsymcount | |
10836 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10837 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10838 | { |
10839 | struct elf_link_hash_entry *rh; | |
10840 | unsigned long indx; | |
10841 | ||
10842 | /* This is a reloc against a global symbol. We | |
10843 | have not yet output all the local symbols, so | |
10844 | we do not know the symbol index of any global | |
10845 | symbol. We set the rel_hash entry for this | |
10846 | reloc to point to the global hash table entry | |
10847 | for this symbol. The symbol index is then | |
ee75fd95 | 10848 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10849 | indx = r_symndx - extsymoff; |
10850 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10851 | while (rh->root.type == bfd_link_hash_indirect | |
10852 | || rh->root.type == bfd_link_hash_warning) | |
10853 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10854 | ||
10855 | /* Setting the index to -2 tells | |
10856 | elf_link_output_extsym that this symbol is | |
10857 | used by a reloc. */ | |
10858 | BFD_ASSERT (rh->indx < 0); | |
10859 | rh->indx = -2; | |
c152c796 AM |
10860 | *rel_hash = rh; |
10861 | ||
10862 | continue; | |
10863 | } | |
10864 | ||
10865 | /* This is a reloc against a local symbol. */ | |
10866 | ||
10867 | *rel_hash = NULL; | |
10868 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10869 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10870 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10871 | { | |
10872 | /* I suppose the backend ought to fill in the | |
10873 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10874 | processor specific section. */ |
cf35638d | 10875 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10876 | if (bfd_is_abs_section (sec)) |
10877 | ; | |
c152c796 AM |
10878 | else if (sec == NULL || sec->owner == NULL) |
10879 | { | |
10880 | bfd_set_error (bfd_error_bad_value); | |
10881 | return FALSE; | |
10882 | } | |
10883 | else | |
10884 | { | |
6a8d1586 AM |
10885 | asection *osec = sec->output_section; |
10886 | ||
10887 | /* If we have discarded a section, the output | |
10888 | section will be the absolute section. In | |
ab96bf03 AM |
10889 | case of discarded SEC_MERGE sections, use |
10890 | the kept section. relocate_section should | |
10891 | have already handled discarded linkonce | |
10892 | sections. */ | |
6a8d1586 AM |
10893 | if (bfd_is_abs_section (osec) |
10894 | && sec->kept_section != NULL | |
10895 | && sec->kept_section->output_section != NULL) | |
10896 | { | |
10897 | osec = sec->kept_section->output_section; | |
10898 | irela->r_addend -= osec->vma; | |
10899 | } | |
10900 | ||
10901 | if (!bfd_is_abs_section (osec)) | |
10902 | { | |
10903 | r_symndx = osec->target_index; | |
cf35638d | 10904 | if (r_symndx == STN_UNDEF) |
74541ad4 | 10905 | { |
051d833a AM |
10906 | irela->r_addend += osec->vma; |
10907 | osec = _bfd_nearby_section (output_bfd, osec, | |
10908 | osec->vma); | |
10909 | irela->r_addend -= osec->vma; | |
10910 | r_symndx = osec->target_index; | |
74541ad4 | 10911 | } |
6a8d1586 | 10912 | } |
c152c796 AM |
10913 | } |
10914 | ||
10915 | /* Adjust the addend according to where the | |
10916 | section winds up in the output section. */ | |
10917 | if (rela_normal) | |
10918 | irela->r_addend += sec->output_offset; | |
10919 | } | |
10920 | else | |
10921 | { | |
8b127cbc | 10922 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
10923 | { |
10924 | unsigned long shlink; | |
10925 | const char *name; | |
10926 | asection *osec; | |
6e0b88f1 | 10927 | long indx; |
c152c796 | 10928 | |
8b127cbc | 10929 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10930 | { |
10931 | /* You can't do ld -r -s. */ | |
10932 | bfd_set_error (bfd_error_invalid_operation); | |
10933 | return FALSE; | |
10934 | } | |
10935 | ||
10936 | /* This symbol was skipped earlier, but | |
10937 | since it is needed by a reloc, we | |
10938 | must output it now. */ | |
10939 | shlink = symtab_hdr->sh_link; | |
10940 | name = (bfd_elf_string_from_elf_section | |
10941 | (input_bfd, shlink, sym.st_name)); | |
10942 | if (name == NULL) | |
10943 | return FALSE; | |
10944 | ||
10945 | osec = sec->output_section; | |
10946 | sym.st_shndx = | |
10947 | _bfd_elf_section_from_bfd_section (output_bfd, | |
10948 | osec); | |
10949 | if (sym.st_shndx == SHN_BAD) | |
10950 | return FALSE; | |
10951 | ||
10952 | sym.st_value += sec->output_offset; | |
0e1862bb | 10953 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10954 | { |
10955 | sym.st_value += osec->vma; | |
10956 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
10957 | { | |
10958 | /* STT_TLS symbols are relative to PT_TLS | |
10959 | segment base. */ | |
8b127cbc | 10960 | BFD_ASSERT (elf_hash_table (flinfo->info) |
c152c796 | 10961 | ->tls_sec != NULL); |
8b127cbc | 10962 | sym.st_value -= (elf_hash_table (flinfo->info) |
c152c796 AM |
10963 | ->tls_sec->vma); |
10964 | } | |
10965 | } | |
10966 | ||
6e0b88f1 | 10967 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10968 | ret = elf_link_output_symstrtab (flinfo, name, |
10969 | &sym, sec, | |
10970 | NULL); | |
6e0b88f1 | 10971 | if (ret == 0) |
c152c796 | 10972 | return FALSE; |
6e0b88f1 | 10973 | else if (ret == 1) |
8b127cbc | 10974 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
10975 | else |
10976 | abort (); | |
c152c796 AM |
10977 | } |
10978 | ||
8b127cbc | 10979 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
10980 | } |
10981 | ||
10982 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
10983 | | (irela->r_info & r_type_mask)); | |
10984 | } | |
10985 | ||
10986 | /* Swap out the relocs. */ | |
d4730f92 BS |
10987 | input_rel_hdr = esdi->rel.hdr; |
10988 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 10989 | { |
d4730f92 BS |
10990 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
10991 | input_rel_hdr, | |
10992 | internal_relocs, | |
10993 | rel_hash_list)) | |
10994 | return FALSE; | |
c152c796 AM |
10995 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
10996 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10997 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
10998 | } |
10999 | ||
11000 | input_rela_hdr = esdi->rela.hdr; | |
11001 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
11002 | { | |
eac338cf | 11003 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 11004 | input_rela_hdr, |
eac338cf | 11005 | internal_relocs, |
d4730f92 | 11006 | rela_hash_list)) |
c152c796 AM |
11007 | return FALSE; |
11008 | } | |
11009 | } | |
11010 | } | |
11011 | ||
11012 | /* Write out the modified section contents. */ | |
11013 | if (bed->elf_backend_write_section | |
8b127cbc | 11014 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 11015 | contents)) |
c152c796 AM |
11016 | { |
11017 | /* Section written out. */ | |
11018 | } | |
11019 | else switch (o->sec_info_type) | |
11020 | { | |
dbaa2011 | 11021 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
11022 | if (! (_bfd_write_section_stabs |
11023 | (output_bfd, | |
8b127cbc | 11024 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
11025 | o, &elf_section_data (o)->sec_info, contents))) |
11026 | return FALSE; | |
11027 | break; | |
dbaa2011 | 11028 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
11029 | if (! _bfd_write_merged_section (output_bfd, o, |
11030 | elf_section_data (o)->sec_info)) | |
11031 | return FALSE; | |
11032 | break; | |
dbaa2011 | 11033 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 11034 | { |
8b127cbc | 11035 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
11036 | o, contents)) |
11037 | return FALSE; | |
11038 | } | |
11039 | break; | |
2f0c68f2 CM |
11040 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
11041 | { | |
11042 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
11043 | flinfo->info, | |
11044 | o, contents)) | |
11045 | return FALSE; | |
11046 | } | |
11047 | break; | |
c152c796 AM |
11048 | default: |
11049 | { | |
310fd250 L |
11050 | if (! (o->flags & SEC_EXCLUDE)) |
11051 | { | |
11052 | file_ptr offset = (file_ptr) o->output_offset; | |
11053 | bfd_size_type todo = o->size; | |
37b01f6a DG |
11054 | |
11055 | offset *= bfd_octets_per_byte (output_bfd); | |
11056 | ||
310fd250 L |
11057 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
11058 | { | |
11059 | /* Reverse-copy input section to output. */ | |
11060 | do | |
11061 | { | |
11062 | todo -= address_size; | |
11063 | if (! bfd_set_section_contents (output_bfd, | |
11064 | o->output_section, | |
11065 | contents + todo, | |
11066 | offset, | |
11067 | address_size)) | |
11068 | return FALSE; | |
11069 | if (todo == 0) | |
11070 | break; | |
11071 | offset += address_size; | |
11072 | } | |
11073 | while (1); | |
11074 | } | |
11075 | else if (! bfd_set_section_contents (output_bfd, | |
11076 | o->output_section, | |
11077 | contents, | |
11078 | offset, todo)) | |
11079 | return FALSE; | |
11080 | } | |
c152c796 AM |
11081 | } |
11082 | break; | |
11083 | } | |
11084 | } | |
11085 | ||
11086 | return TRUE; | |
11087 | } | |
11088 | ||
11089 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 11090 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
11091 | is used to build constructor and destructor tables when linking |
11092 | with -Ur. */ | |
11093 | ||
11094 | static bfd_boolean | |
11095 | elf_reloc_link_order (bfd *output_bfd, | |
11096 | struct bfd_link_info *info, | |
11097 | asection *output_section, | |
11098 | struct bfd_link_order *link_order) | |
11099 | { | |
11100 | reloc_howto_type *howto; | |
11101 | long indx; | |
11102 | bfd_vma offset; | |
11103 | bfd_vma addend; | |
d4730f92 | 11104 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
11105 | struct elf_link_hash_entry **rel_hash_ptr; |
11106 | Elf_Internal_Shdr *rel_hdr; | |
11107 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
11108 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
11109 | bfd_byte *erel; | |
11110 | unsigned int i; | |
d4730f92 | 11111 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
11112 | |
11113 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
11114 | if (howto == NULL) | |
11115 | { | |
11116 | bfd_set_error (bfd_error_bad_value); | |
11117 | return FALSE; | |
11118 | } | |
11119 | ||
11120 | addend = link_order->u.reloc.p->addend; | |
11121 | ||
d4730f92 BS |
11122 | if (esdo->rel.hdr) |
11123 | reldata = &esdo->rel; | |
11124 | else if (esdo->rela.hdr) | |
11125 | reldata = &esdo->rela; | |
11126 | else | |
11127 | { | |
11128 | reldata = NULL; | |
11129 | BFD_ASSERT (0); | |
11130 | } | |
11131 | ||
c152c796 | 11132 | /* Figure out the symbol index. */ |
d4730f92 | 11133 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
11134 | if (link_order->type == bfd_section_reloc_link_order) |
11135 | { | |
11136 | indx = link_order->u.reloc.p->u.section->target_index; | |
11137 | BFD_ASSERT (indx != 0); | |
11138 | *rel_hash_ptr = NULL; | |
11139 | } | |
11140 | else | |
11141 | { | |
11142 | struct elf_link_hash_entry *h; | |
11143 | ||
11144 | /* Treat a reloc against a defined symbol as though it were | |
11145 | actually against the section. */ | |
11146 | h = ((struct elf_link_hash_entry *) | |
11147 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
11148 | link_order->u.reloc.p->u.name, | |
11149 | FALSE, FALSE, TRUE)); | |
11150 | if (h != NULL | |
11151 | && (h->root.type == bfd_link_hash_defined | |
11152 | || h->root.type == bfd_link_hash_defweak)) | |
11153 | { | |
11154 | asection *section; | |
11155 | ||
11156 | section = h->root.u.def.section; | |
11157 | indx = section->output_section->target_index; | |
11158 | *rel_hash_ptr = NULL; | |
11159 | /* It seems that we ought to add the symbol value to the | |
11160 | addend here, but in practice it has already been added | |
11161 | because it was passed to constructor_callback. */ | |
11162 | addend += section->output_section->vma + section->output_offset; | |
11163 | } | |
11164 | else if (h != NULL) | |
11165 | { | |
11166 | /* Setting the index to -2 tells elf_link_output_extsym that | |
11167 | this symbol is used by a reloc. */ | |
11168 | h->indx = -2; | |
11169 | *rel_hash_ptr = h; | |
11170 | indx = 0; | |
11171 | } | |
11172 | else | |
11173 | { | |
1a72702b AM |
11174 | (*info->callbacks->unattached_reloc) |
11175 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
11176 | indx = 0; |
11177 | } | |
11178 | } | |
11179 | ||
11180 | /* If this is an inplace reloc, we must write the addend into the | |
11181 | object file. */ | |
11182 | if (howto->partial_inplace && addend != 0) | |
11183 | { | |
11184 | bfd_size_type size; | |
11185 | bfd_reloc_status_type rstat; | |
11186 | bfd_byte *buf; | |
11187 | bfd_boolean ok; | |
11188 | const char *sym_name; | |
11189 | ||
a50b1753 NC |
11190 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
11191 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 11192 | if (buf == NULL && size != 0) |
c152c796 AM |
11193 | return FALSE; |
11194 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
11195 | switch (rstat) | |
11196 | { | |
11197 | case bfd_reloc_ok: | |
11198 | break; | |
11199 | ||
11200 | default: | |
11201 | case bfd_reloc_outofrange: | |
11202 | abort (); | |
11203 | ||
11204 | case bfd_reloc_overflow: | |
11205 | if (link_order->type == bfd_section_reloc_link_order) | |
11206 | sym_name = bfd_section_name (output_bfd, | |
11207 | link_order->u.reloc.p->u.section); | |
11208 | else | |
11209 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
11210 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
11211 | howto->name, addend, NULL, NULL, | |
11212 | (bfd_vma) 0); | |
c152c796 AM |
11213 | break; |
11214 | } | |
37b01f6a | 11215 | |
c152c796 | 11216 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
11217 | link_order->offset |
11218 | * bfd_octets_per_byte (output_bfd), | |
11219 | size); | |
c152c796 AM |
11220 | free (buf); |
11221 | if (! ok) | |
11222 | return FALSE; | |
11223 | } | |
11224 | ||
11225 | /* The address of a reloc is relative to the section in a | |
11226 | relocatable file, and is a virtual address in an executable | |
11227 | file. */ | |
11228 | offset = link_order->offset; | |
0e1862bb | 11229 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
11230 | offset += output_section->vma; |
11231 | ||
11232 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
11233 | { | |
11234 | irel[i].r_offset = offset; | |
11235 | irel[i].r_info = 0; | |
11236 | irel[i].r_addend = 0; | |
11237 | } | |
11238 | if (bed->s->arch_size == 32) | |
11239 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
11240 | else | |
11241 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
11242 | ||
d4730f92 | 11243 | rel_hdr = reldata->hdr; |
c152c796 AM |
11244 | erel = rel_hdr->contents; |
11245 | if (rel_hdr->sh_type == SHT_REL) | |
11246 | { | |
d4730f92 | 11247 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
11248 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
11249 | } | |
11250 | else | |
11251 | { | |
11252 | irel[0].r_addend = addend; | |
d4730f92 | 11253 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
11254 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
11255 | } | |
11256 | ||
d4730f92 | 11257 | ++reldata->count; |
c152c796 AM |
11258 | |
11259 | return TRUE; | |
11260 | } | |
11261 | ||
0b52efa6 PB |
11262 | |
11263 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
11264 | ||
11265 | static bfd_vma | |
11266 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
11267 | { | |
11268 | Elf_Internal_Shdr **elf_shdrp; | |
11269 | asection *s; | |
11270 | int elfsec; | |
11271 | ||
11272 | s = p->u.indirect.section; | |
11273 | elf_shdrp = elf_elfsections (s->owner); | |
11274 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
11275 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
11276 | /* PR 290: |
11277 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 11278 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
11279 | sh_info fields. Hence we could get the situation |
11280 | where elfsec is 0. */ | |
11281 | if (elfsec == 0) | |
11282 | { | |
11283 | const struct elf_backend_data *bed | |
11284 | = get_elf_backend_data (s->owner); | |
11285 | if (bed->link_order_error_handler) | |
d003868e | 11286 | bed->link_order_error_handler |
695344c0 | 11287 | /* xgettext:c-format */ |
871b3ab2 | 11288 | (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s); |
185d09ad L |
11289 | return 0; |
11290 | } | |
11291 | else | |
11292 | { | |
11293 | s = elf_shdrp[elfsec]->bfd_section; | |
11294 | return s->output_section->vma + s->output_offset; | |
11295 | } | |
0b52efa6 PB |
11296 | } |
11297 | ||
11298 | ||
11299 | /* Compare two sections based on the locations of the sections they are | |
11300 | linked to. Used by elf_fixup_link_order. */ | |
11301 | ||
11302 | static int | |
11303 | compare_link_order (const void * a, const void * b) | |
11304 | { | |
11305 | bfd_vma apos; | |
11306 | bfd_vma bpos; | |
11307 | ||
11308 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
11309 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
11310 | if (apos < bpos) | |
11311 | return -1; | |
11312 | return apos > bpos; | |
11313 | } | |
11314 | ||
11315 | ||
11316 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
11317 | order as their linked sections. Returns false if this could not be done | |
11318 | because an output section includes both ordered and unordered | |
11319 | sections. Ideally we'd do this in the linker proper. */ | |
11320 | ||
11321 | static bfd_boolean | |
11322 | elf_fixup_link_order (bfd *abfd, asection *o) | |
11323 | { | |
11324 | int seen_linkorder; | |
11325 | int seen_other; | |
11326 | int n; | |
11327 | struct bfd_link_order *p; | |
11328 | bfd *sub; | |
11329 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 11330 | unsigned elfsec; |
0b52efa6 | 11331 | struct bfd_link_order **sections; |
d33cdfe3 | 11332 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 11333 | bfd_vma offset; |
3b36f7e6 | 11334 | |
d33cdfe3 L |
11335 | other_sec = NULL; |
11336 | linkorder_sec = NULL; | |
0b52efa6 PB |
11337 | seen_other = 0; |
11338 | seen_linkorder = 0; | |
8423293d | 11339 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 11340 | { |
d33cdfe3 | 11341 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
11342 | { |
11343 | s = p->u.indirect.section; | |
d33cdfe3 L |
11344 | sub = s->owner; |
11345 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
11346 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
11347 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
11348 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
11349 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
11350 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
11351 | { |
11352 | seen_linkorder++; | |
11353 | linkorder_sec = s; | |
11354 | } | |
0b52efa6 | 11355 | else |
d33cdfe3 L |
11356 | { |
11357 | seen_other++; | |
11358 | other_sec = s; | |
11359 | } | |
0b52efa6 PB |
11360 | } |
11361 | else | |
11362 | seen_other++; | |
d33cdfe3 L |
11363 | |
11364 | if (seen_other && seen_linkorder) | |
11365 | { | |
11366 | if (other_sec && linkorder_sec) | |
4eca0228 | 11367 | _bfd_error_handler |
695344c0 | 11368 | /* xgettext:c-format */ |
871b3ab2 AM |
11369 | (_("%pA has both ordered [`%pA' in %pB] " |
11370 | "and unordered [`%pA' in %pB] sections"), | |
63a5468a AM |
11371 | o, linkorder_sec, linkorder_sec->owner, |
11372 | other_sec, other_sec->owner); | |
d33cdfe3 | 11373 | else |
4eca0228 | 11374 | _bfd_error_handler |
871b3ab2 | 11375 | (_("%pA has both ordered and unordered sections"), o); |
d33cdfe3 L |
11376 | bfd_set_error (bfd_error_bad_value); |
11377 | return FALSE; | |
11378 | } | |
0b52efa6 PB |
11379 | } |
11380 | ||
11381 | if (!seen_linkorder) | |
11382 | return TRUE; | |
11383 | ||
0b52efa6 | 11384 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11385 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11386 | if (sections == NULL) | |
11387 | return FALSE; | |
0b52efa6 | 11388 | seen_linkorder = 0; |
3b36f7e6 | 11389 | |
8423293d | 11390 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11391 | { |
11392 | sections[seen_linkorder++] = p; | |
11393 | } | |
11394 | /* Sort the input sections in the order of their linked section. */ | |
11395 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11396 | compare_link_order); | |
11397 | ||
11398 | /* Change the offsets of the sections. */ | |
11399 | offset = 0; | |
11400 | for (n = 0; n < seen_linkorder; n++) | |
11401 | { | |
11402 | s = sections[n]->u.indirect.section; | |
461686a3 | 11403 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11404 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11405 | sections[n]->offset = offset; |
11406 | offset += sections[n]->size; | |
11407 | } | |
11408 | ||
4dd07732 | 11409 | free (sections); |
0b52efa6 PB |
11410 | return TRUE; |
11411 | } | |
11412 | ||
76359541 TP |
11413 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11414 | Returns TRUE upon success, FALSE otherwise. */ | |
11415 | ||
11416 | static bfd_boolean | |
11417 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11418 | { | |
11419 | bfd_boolean ret = FALSE; | |
11420 | bfd *implib_bfd; | |
11421 | const struct elf_backend_data *bed; | |
11422 | flagword flags; | |
11423 | enum bfd_architecture arch; | |
11424 | unsigned int mach; | |
11425 | asymbol **sympp = NULL; | |
11426 | long symsize; | |
11427 | long symcount; | |
11428 | long src_count; | |
11429 | elf_symbol_type *osymbuf; | |
11430 | ||
11431 | implib_bfd = info->out_implib_bfd; | |
11432 | bed = get_elf_backend_data (abfd); | |
11433 | ||
11434 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11435 | return FALSE; | |
11436 | ||
046734ff | 11437 | /* Use flag from executable but make it a relocatable object. */ |
76359541 TP |
11438 | flags = bfd_get_file_flags (abfd); |
11439 | flags &= ~HAS_RELOC; | |
11440 | if (!bfd_set_start_address (implib_bfd, 0) | |
046734ff | 11441 | || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) |
76359541 TP |
11442 | return FALSE; |
11443 | ||
11444 | /* Copy architecture of output file to import library file. */ | |
11445 | arch = bfd_get_arch (abfd); | |
11446 | mach = bfd_get_mach (abfd); | |
11447 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11448 | && (abfd->target_defaulted | |
11449 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11450 | return FALSE; | |
11451 | ||
11452 | /* Get symbol table size. */ | |
11453 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11454 | if (symsize < 0) | |
11455 | return FALSE; | |
11456 | ||
11457 | /* Read in the symbol table. */ | |
11458 | sympp = (asymbol **) xmalloc (symsize); | |
11459 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11460 | if (symcount < 0) | |
11461 | goto free_sym_buf; | |
11462 | ||
11463 | /* Allow the BFD backend to copy any private header data it | |
11464 | understands from the output BFD to the import library BFD. */ | |
11465 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11466 | goto free_sym_buf; | |
11467 | ||
11468 | /* Filter symbols to appear in the import library. */ | |
11469 | if (bed->elf_backend_filter_implib_symbols) | |
11470 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11471 | symcount); | |
11472 | else | |
11473 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11474 | if (symcount == 0) | |
11475 | { | |
5df1bc57 | 11476 | bfd_set_error (bfd_error_no_symbols); |
871b3ab2 | 11477 | _bfd_error_handler (_("%pB: no symbol found for import library"), |
4eca0228 | 11478 | implib_bfd); |
76359541 TP |
11479 | goto free_sym_buf; |
11480 | } | |
11481 | ||
11482 | ||
11483 | /* Make symbols absolute. */ | |
11484 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11485 | sizeof (*osymbuf)); | |
11486 | for (src_count = 0; src_count < symcount; src_count++) | |
11487 | { | |
11488 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11489 | sizeof (*osymbuf)); | |
11490 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11491 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11492 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11493 | osymbuf[src_count].internal_elf_sym.st_value = | |
11494 | osymbuf[src_count].symbol.value; | |
11495 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11496 | } | |
11497 | ||
11498 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11499 | ||
11500 | /* Allow the BFD backend to copy any private data it understands | |
11501 | from the output BFD to the import library BFD. This is done last | |
11502 | to permit the routine to look at the filtered symbol table. */ | |
11503 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11504 | goto free_sym_buf; | |
11505 | ||
11506 | if (!bfd_close (implib_bfd)) | |
11507 | goto free_sym_buf; | |
11508 | ||
11509 | ret = TRUE; | |
11510 | ||
11511 | free_sym_buf: | |
11512 | free (sympp); | |
11513 | return ret; | |
11514 | } | |
11515 | ||
9f7c3e5e AM |
11516 | static void |
11517 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11518 | { | |
11519 | asection *o; | |
11520 | ||
11521 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11522 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11523 | if (flinfo->contents != NULL) |
11524 | free (flinfo->contents); | |
11525 | if (flinfo->external_relocs != NULL) | |
11526 | free (flinfo->external_relocs); | |
11527 | if (flinfo->internal_relocs != NULL) | |
11528 | free (flinfo->internal_relocs); | |
11529 | if (flinfo->external_syms != NULL) | |
11530 | free (flinfo->external_syms); | |
11531 | if (flinfo->locsym_shndx != NULL) | |
11532 | free (flinfo->locsym_shndx); | |
11533 | if (flinfo->internal_syms != NULL) | |
11534 | free (flinfo->internal_syms); | |
11535 | if (flinfo->indices != NULL) | |
11536 | free (flinfo->indices); | |
11537 | if (flinfo->sections != NULL) | |
11538 | free (flinfo->sections); | |
9f7c3e5e AM |
11539 | if (flinfo->symshndxbuf != NULL) |
11540 | free (flinfo->symshndxbuf); | |
11541 | for (o = obfd->sections; o != NULL; o = o->next) | |
11542 | { | |
11543 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11544 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11545 | free (esdo->rel.hashes); | |
11546 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11547 | free (esdo->rela.hashes); | |
11548 | } | |
11549 | } | |
0b52efa6 | 11550 | |
c152c796 AM |
11551 | /* Do the final step of an ELF link. */ |
11552 | ||
11553 | bfd_boolean | |
11554 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11555 | { | |
11556 | bfd_boolean dynamic; | |
11557 | bfd_boolean emit_relocs; | |
11558 | bfd *dynobj; | |
8b127cbc | 11559 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11560 | asection *o; |
11561 | struct bfd_link_order *p; | |
11562 | bfd *sub; | |
c152c796 AM |
11563 | bfd_size_type max_contents_size; |
11564 | bfd_size_type max_external_reloc_size; | |
11565 | bfd_size_type max_internal_reloc_count; | |
11566 | bfd_size_type max_sym_count; | |
11567 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11568 | Elf_Internal_Sym elfsym; |
11569 | unsigned int i; | |
11570 | Elf_Internal_Shdr *symtab_hdr; | |
11571 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11572 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11573 | struct elf_outext_info eoinfo; | |
11574 | bfd_boolean merged; | |
11575 | size_t relativecount = 0; | |
11576 | asection *reldyn = 0; | |
11577 | bfd_size_type amt; | |
104d59d1 JM |
11578 | asection *attr_section = NULL; |
11579 | bfd_vma attr_size = 0; | |
11580 | const char *std_attrs_section; | |
64f52338 | 11581 | struct elf_link_hash_table *htab = elf_hash_table (info); |
c152c796 | 11582 | |
64f52338 | 11583 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11584 | return FALSE; |
11585 | ||
0e1862bb | 11586 | if (bfd_link_pic (info)) |
c152c796 AM |
11587 | abfd->flags |= DYNAMIC; |
11588 | ||
64f52338 AM |
11589 | dynamic = htab->dynamic_sections_created; |
11590 | dynobj = htab->dynobj; | |
c152c796 | 11591 | |
0e1862bb | 11592 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11593 | || info->emitrelocations); |
c152c796 | 11594 | |
8b127cbc AM |
11595 | flinfo.info = info; |
11596 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11597 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11598 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11599 | return FALSE; |
11600 | ||
11601 | if (! dynamic) | |
11602 | { | |
8b127cbc AM |
11603 | flinfo.hash_sec = NULL; |
11604 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11605 | } |
11606 | else | |
11607 | { | |
3d4d4302 | 11608 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11609 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11610 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11611 | /* Note that it is OK if symver_sec is NULL. */ |
11612 | } | |
11613 | ||
8b127cbc AM |
11614 | flinfo.contents = NULL; |
11615 | flinfo.external_relocs = NULL; | |
11616 | flinfo.internal_relocs = NULL; | |
11617 | flinfo.external_syms = NULL; | |
11618 | flinfo.locsym_shndx = NULL; | |
11619 | flinfo.internal_syms = NULL; | |
11620 | flinfo.indices = NULL; | |
11621 | flinfo.sections = NULL; | |
8b127cbc | 11622 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11623 | flinfo.filesym_count = 0; |
c152c796 | 11624 | |
104d59d1 JM |
11625 | /* The object attributes have been merged. Remove the input |
11626 | sections from the link, and set the contents of the output | |
11627 | secton. */ | |
11628 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11629 | for (o = abfd->sections; o != NULL; o = o->next) | |
11630 | { | |
11631 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) | |
11632 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11633 | { | |
11634 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11635 | { | |
11636 | asection *input_section; | |
11637 | ||
11638 | if (p->type != bfd_indirect_link_order) | |
11639 | continue; | |
11640 | input_section = p->u.indirect.section; | |
11641 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11642 | elf_link_input_bfd ignores this section. */ | |
11643 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11644 | } | |
a0c8462f | 11645 | |
104d59d1 JM |
11646 | attr_size = bfd_elf_obj_attr_size (abfd); |
11647 | if (attr_size) | |
11648 | { | |
11649 | bfd_set_section_size (abfd, o, attr_size); | |
11650 | attr_section = o; | |
11651 | /* Skip this section later on. */ | |
11652 | o->map_head.link_order = NULL; | |
11653 | } | |
11654 | else | |
11655 | o->flags |= SEC_EXCLUDE; | |
11656 | } | |
6e5e9d58 AM |
11657 | else if ((o->flags & SEC_GROUP) != 0 && o->size == 0) |
11658 | { | |
11659 | /* Remove empty group section from linker output. */ | |
11660 | o->flags |= SEC_EXCLUDE; | |
11661 | bfd_section_list_remove (abfd, o); | |
11662 | abfd->section_count--; | |
11663 | } | |
104d59d1 JM |
11664 | } |
11665 | ||
c152c796 AM |
11666 | /* Count up the number of relocations we will output for each output |
11667 | section, so that we know the sizes of the reloc sections. We | |
11668 | also figure out some maximum sizes. */ | |
11669 | max_contents_size = 0; | |
11670 | max_external_reloc_size = 0; | |
11671 | max_internal_reloc_count = 0; | |
11672 | max_sym_count = 0; | |
11673 | max_sym_shndx_count = 0; | |
11674 | merged = FALSE; | |
11675 | for (o = abfd->sections; o != NULL; o = o->next) | |
11676 | { | |
11677 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11678 | o->reloc_count = 0; | |
11679 | ||
8423293d | 11680 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11681 | { |
11682 | unsigned int reloc_count = 0; | |
9eaff861 | 11683 | unsigned int additional_reloc_count = 0; |
c152c796 | 11684 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
11685 | |
11686 | if (p->type == bfd_section_reloc_link_order | |
11687 | || p->type == bfd_symbol_reloc_link_order) | |
11688 | reloc_count = 1; | |
11689 | else if (p->type == bfd_indirect_link_order) | |
11690 | { | |
11691 | asection *sec; | |
11692 | ||
11693 | sec = p->u.indirect.section; | |
c152c796 AM |
11694 | |
11695 | /* Mark all sections which are to be included in the | |
11696 | link. This will normally be every section. We need | |
11697 | to do this so that we can identify any sections which | |
11698 | the linker has decided to not include. */ | |
11699 | sec->linker_mark = TRUE; | |
11700 | ||
11701 | if (sec->flags & SEC_MERGE) | |
11702 | merged = TRUE; | |
11703 | ||
eea6121a AM |
11704 | if (sec->rawsize > max_contents_size) |
11705 | max_contents_size = sec->rawsize; | |
11706 | if (sec->size > max_contents_size) | |
11707 | max_contents_size = sec->size; | |
c152c796 | 11708 | |
c152c796 AM |
11709 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
11710 | && (sec->owner->flags & DYNAMIC) == 0) | |
11711 | { | |
11712 | size_t sym_count; | |
11713 | ||
a961cdd5 AM |
11714 | /* We are interested in just local symbols, not all |
11715 | symbols. */ | |
c152c796 AM |
11716 | if (elf_bad_symtab (sec->owner)) |
11717 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11718 | / bed->s->sizeof_sym); | |
11719 | else | |
11720 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11721 | ||
11722 | if (sym_count > max_sym_count) | |
11723 | max_sym_count = sym_count; | |
11724 | ||
11725 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11726 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11727 | max_sym_shndx_count = sym_count; |
11728 | ||
a961cdd5 AM |
11729 | if (esdo->this_hdr.sh_type == SHT_REL |
11730 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11731 | /* Some backends use reloc_count in relocation sections | |
11732 | to count particular types of relocs. Of course, | |
11733 | reloc sections themselves can't have relocations. */ | |
11734 | ; | |
11735 | else if (emit_relocs) | |
11736 | { | |
11737 | reloc_count = sec->reloc_count; | |
11738 | if (bed->elf_backend_count_additional_relocs) | |
11739 | { | |
11740 | int c; | |
11741 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11742 | additional_reloc_count += c; | |
11743 | } | |
11744 | } | |
11745 | else if (bed->elf_backend_count_relocs) | |
11746 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
11747 | ||
11748 | esdi = elf_section_data (sec); | |
11749 | ||
c152c796 AM |
11750 | if ((sec->flags & SEC_RELOC) != 0) |
11751 | { | |
d4730f92 | 11752 | size_t ext_size = 0; |
c152c796 | 11753 | |
d4730f92 BS |
11754 | if (esdi->rel.hdr != NULL) |
11755 | ext_size = esdi->rel.hdr->sh_size; | |
11756 | if (esdi->rela.hdr != NULL) | |
11757 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11758 | |
c152c796 AM |
11759 | if (ext_size > max_external_reloc_size) |
11760 | max_external_reloc_size = ext_size; | |
11761 | if (sec->reloc_count > max_internal_reloc_count) | |
11762 | max_internal_reloc_count = sec->reloc_count; | |
11763 | } | |
11764 | } | |
11765 | } | |
11766 | ||
11767 | if (reloc_count == 0) | |
11768 | continue; | |
11769 | ||
9eaff861 | 11770 | reloc_count += additional_reloc_count; |
c152c796 AM |
11771 | o->reloc_count += reloc_count; |
11772 | ||
0e1862bb | 11773 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11774 | { |
d4730f92 | 11775 | if (esdi->rel.hdr) |
9eaff861 | 11776 | { |
491d01d3 | 11777 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
11778 | esdo->rel.count += additional_reloc_count; |
11779 | } | |
d4730f92 | 11780 | if (esdi->rela.hdr) |
9eaff861 | 11781 | { |
491d01d3 | 11782 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
11783 | esdo->rela.count += additional_reloc_count; |
11784 | } | |
d4730f92 BS |
11785 | } |
11786 | else | |
11787 | { | |
11788 | if (o->use_rela_p) | |
11789 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11790 | else |
d4730f92 | 11791 | esdo->rel.count += reloc_count; |
c152c796 | 11792 | } |
c152c796 AM |
11793 | } |
11794 | ||
9eaff861 | 11795 | if (o->reloc_count > 0) |
c152c796 AM |
11796 | o->flags |= SEC_RELOC; |
11797 | else | |
11798 | { | |
11799 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11800 | set it (this is probably a bug) and if it is set | |
11801 | assign_section_numbers will create a reloc section. */ | |
11802 | o->flags &=~ SEC_RELOC; | |
11803 | } | |
11804 | ||
11805 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11806 | zero. This is done in elf_fake_sections as well, but forcing | |
11807 | the VMA to 0 here will ensure that relocs against these | |
11808 | sections are handled correctly. */ | |
11809 | if ((o->flags & SEC_ALLOC) == 0 | |
11810 | && ! o->user_set_vma) | |
11811 | o->vma = 0; | |
11812 | } | |
11813 | ||
0e1862bb | 11814 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 11815 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
11816 | |
11817 | /* Figure out the file positions for everything but the symbol table | |
11818 | and the relocs. We set symcount to force assign_section_numbers | |
11819 | to create a symbol table. */ | |
8539e4e8 | 11820 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11821 | BFD_ASSERT (! abfd->output_has_begun); |
11822 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11823 | goto error_return; | |
11824 | ||
ee75fd95 | 11825 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11826 | for (o = abfd->sections; o != NULL; o = o->next) |
11827 | { | |
d4730f92 | 11828 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11829 | if ((o->flags & SEC_RELOC) != 0) |
11830 | { | |
d4730f92 | 11831 | if (esdo->rel.hdr |
9eaff861 | 11832 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
11833 | goto error_return; |
11834 | ||
d4730f92 | 11835 | if (esdo->rela.hdr |
9eaff861 | 11836 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
11837 | goto error_return; |
11838 | } | |
11839 | ||
11840 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
11841 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
11842 | esdo->rel.count = 0; |
11843 | esdo->rela.count = 0; | |
0ce398f1 L |
11844 | |
11845 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
11846 | { | |
11847 | /* Cache the section contents so that they can be compressed | |
11848 | later. Use bfd_malloc since it will be freed by | |
11849 | bfd_compress_section_contents. */ | |
11850 | unsigned char *contents = esdo->this_hdr.contents; | |
11851 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
11852 | abort (); | |
11853 | contents | |
11854 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
11855 | if (contents == NULL) | |
11856 | goto error_return; | |
11857 | esdo->this_hdr.contents = contents; | |
11858 | } | |
c152c796 AM |
11859 | } |
11860 | ||
c152c796 | 11861 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
11862 | .symtab, .strtab, and non-loaded reloc sections. We start the |
11863 | .symtab section at the current file position, and write directly | |
11864 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
11865 | bfd_get_symcount (abfd) = 0; |
11866 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11867 | /* sh_name is set in prep_headers. */ | |
11868 | symtab_hdr->sh_type = SHT_SYMTAB; | |
11869 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
11870 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
11871 | /* sh_link is set in assign_section_numbers. */ | |
11872 | /* sh_info is set below. */ | |
11873 | /* sh_offset is set just below. */ | |
72de5009 | 11874 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 11875 | |
ef10c3ac L |
11876 | if (max_sym_count < 20) |
11877 | max_sym_count = 20; | |
64f52338 | 11878 | htab->strtabsize = max_sym_count; |
ef10c3ac | 11879 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
11880 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
11881 | if (htab->strtab == NULL) | |
c152c796 | 11882 | goto error_return; |
ef10c3ac L |
11883 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
11884 | flinfo.symshndxbuf | |
11885 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
11886 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 11887 | |
8539e4e8 | 11888 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 11889 | { |
8539e4e8 AM |
11890 | file_ptr off = elf_next_file_pos (abfd); |
11891 | ||
11892 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
11893 | ||
11894 | /* Note that at this point elf_next_file_pos (abfd) is | |
11895 | incorrect. We do not yet know the size of the .symtab section. | |
11896 | We correct next_file_pos below, after we do know the size. */ | |
11897 | ||
11898 | /* Start writing out the symbol table. The first symbol is always a | |
11899 | dummy symbol. */ | |
c152c796 AM |
11900 | elfsym.st_value = 0; |
11901 | elfsym.st_size = 0; | |
11902 | elfsym.st_info = 0; | |
11903 | elfsym.st_other = 0; | |
11904 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 11905 | elfsym.st_target_internal = 0; |
ef10c3ac L |
11906 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
11907 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 11908 | goto error_return; |
c152c796 | 11909 | |
8539e4e8 AM |
11910 | /* Output a symbol for each section. We output these even if we are |
11911 | discarding local symbols, since they are used for relocs. These | |
11912 | symbols have no names. We store the index of each one in the | |
11913 | index field of the section, so that we can find it again when | |
11914 | outputting relocs. */ | |
11915 | ||
c152c796 AM |
11916 | elfsym.st_size = 0; |
11917 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
11918 | elfsym.st_other = 0; | |
f0b5bb34 | 11919 | elfsym.st_value = 0; |
35fc36a8 | 11920 | elfsym.st_target_internal = 0; |
c152c796 AM |
11921 | for (i = 1; i < elf_numsections (abfd); i++) |
11922 | { | |
11923 | o = bfd_section_from_elf_index (abfd, i); | |
11924 | if (o != NULL) | |
f0b5bb34 AM |
11925 | { |
11926 | o->target_index = bfd_get_symcount (abfd); | |
11927 | elfsym.st_shndx = i; | |
0e1862bb | 11928 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 11929 | elfsym.st_value = o->vma; |
ef10c3ac L |
11930 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
11931 | NULL) != 1) | |
f0b5bb34 AM |
11932 | goto error_return; |
11933 | } | |
c152c796 AM |
11934 | } |
11935 | } | |
11936 | ||
11937 | /* Allocate some memory to hold information read in from the input | |
11938 | files. */ | |
11939 | if (max_contents_size != 0) | |
11940 | { | |
8b127cbc AM |
11941 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
11942 | if (flinfo.contents == NULL) | |
c152c796 AM |
11943 | goto error_return; |
11944 | } | |
11945 | ||
11946 | if (max_external_reloc_size != 0) | |
11947 | { | |
8b127cbc AM |
11948 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
11949 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
11950 | goto error_return; |
11951 | } | |
11952 | ||
11953 | if (max_internal_reloc_count != 0) | |
11954 | { | |
056bafd4 | 11955 | amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); |
8b127cbc AM |
11956 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
11957 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
11958 | goto error_return; |
11959 | } | |
11960 | ||
11961 | if (max_sym_count != 0) | |
11962 | { | |
11963 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
11964 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
11965 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
11966 | goto error_return; |
11967 | ||
11968 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
11969 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
11970 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
11971 | goto error_return; |
11972 | ||
11973 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
11974 | flinfo.indices = (long int *) bfd_malloc (amt); |
11975 | if (flinfo.indices == NULL) | |
c152c796 AM |
11976 | goto error_return; |
11977 | ||
11978 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
11979 | flinfo.sections = (asection **) bfd_malloc (amt); |
11980 | if (flinfo.sections == NULL) | |
c152c796 AM |
11981 | goto error_return; |
11982 | } | |
11983 | ||
11984 | if (max_sym_shndx_count != 0) | |
11985 | { | |
11986 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
11987 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
11988 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
11989 | goto error_return; |
11990 | } | |
11991 | ||
64f52338 | 11992 | if (htab->tls_sec) |
c152c796 AM |
11993 | { |
11994 | bfd_vma base, end = 0; | |
11995 | asection *sec; | |
11996 | ||
64f52338 | 11997 | for (sec = htab->tls_sec; |
c152c796 AM |
11998 | sec && (sec->flags & SEC_THREAD_LOCAL); |
11999 | sec = sec->next) | |
12000 | { | |
3a800eb9 | 12001 | bfd_size_type size = sec->size; |
c152c796 | 12002 | |
3a800eb9 AM |
12003 | if (size == 0 |
12004 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 12005 | { |
91d6fa6a NC |
12006 | struct bfd_link_order *ord = sec->map_tail.link_order; |
12007 | ||
12008 | if (ord != NULL) | |
12009 | size = ord->offset + ord->size; | |
c152c796 AM |
12010 | } |
12011 | end = sec->vma + size; | |
12012 | } | |
64f52338 | 12013 | base = htab->tls_sec->vma; |
7dc98aea RO |
12014 | /* Only align end of TLS section if static TLS doesn't have special |
12015 | alignment requirements. */ | |
12016 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
12017 | end = align_power (end, htab->tls_sec->alignment_power); |
12018 | htab->tls_size = end - base; | |
c152c796 AM |
12019 | } |
12020 | ||
0b52efa6 PB |
12021 | /* Reorder SHF_LINK_ORDER sections. */ |
12022 | for (o = abfd->sections; o != NULL; o = o->next) | |
12023 | { | |
12024 | if (!elf_fixup_link_order (abfd, o)) | |
12025 | return FALSE; | |
12026 | } | |
12027 | ||
2f0c68f2 CM |
12028 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
12029 | return FALSE; | |
12030 | ||
c152c796 AM |
12031 | /* Since ELF permits relocations to be against local symbols, we |
12032 | must have the local symbols available when we do the relocations. | |
12033 | Since we would rather only read the local symbols once, and we | |
12034 | would rather not keep them in memory, we handle all the | |
12035 | relocations for a single input file at the same time. | |
12036 | ||
12037 | Unfortunately, there is no way to know the total number of local | |
12038 | symbols until we have seen all of them, and the local symbol | |
12039 | indices precede the global symbol indices. This means that when | |
12040 | we are generating relocatable output, and we see a reloc against | |
12041 | a global symbol, we can not know the symbol index until we have | |
12042 | finished examining all the local symbols to see which ones we are | |
12043 | going to output. To deal with this, we keep the relocations in | |
12044 | memory, and don't output them until the end of the link. This is | |
12045 | an unfortunate waste of memory, but I don't see a good way around | |
12046 | it. Fortunately, it only happens when performing a relocatable | |
12047 | link, which is not the common case. FIXME: If keep_memory is set | |
12048 | we could write the relocs out and then read them again; I don't | |
12049 | know how bad the memory loss will be. */ | |
12050 | ||
c72f2fb2 | 12051 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12052 | sub->output_has_begun = FALSE; |
12053 | for (o = abfd->sections; o != NULL; o = o->next) | |
12054 | { | |
8423293d | 12055 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12056 | { |
12057 | if (p->type == bfd_indirect_link_order | |
12058 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
12059 | == bfd_target_elf_flavour) | |
12060 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
12061 | { | |
12062 | if (! sub->output_has_begun) | |
12063 | { | |
8b127cbc | 12064 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
12065 | goto error_return; |
12066 | sub->output_has_begun = TRUE; | |
12067 | } | |
12068 | } | |
12069 | else if (p->type == bfd_section_reloc_link_order | |
12070 | || p->type == bfd_symbol_reloc_link_order) | |
12071 | { | |
12072 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
12073 | goto error_return; | |
12074 | } | |
12075 | else | |
12076 | { | |
12077 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
12078 | { |
12079 | if (p->type == bfd_indirect_link_order | |
12080 | && (bfd_get_flavour (sub) | |
12081 | == bfd_target_elf_flavour) | |
12082 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
12083 | != bed->s->elfclass)) | |
12084 | { | |
12085 | const char *iclass, *oclass; | |
12086 | ||
aebf9be7 | 12087 | switch (bed->s->elfclass) |
351f65ca | 12088 | { |
aebf9be7 NC |
12089 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
12090 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
12091 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
12092 | default: abort (); | |
351f65ca | 12093 | } |
aebf9be7 NC |
12094 | |
12095 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 12096 | { |
aebf9be7 NC |
12097 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
12098 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
12099 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
12100 | default: abort (); | |
351f65ca L |
12101 | } |
12102 | ||
12103 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 12104 | _bfd_error_handler |
695344c0 | 12105 | /* xgettext:c-format */ |
871b3ab2 | 12106 | (_("%pB: file class %s incompatible with %s"), |
351f65ca L |
12107 | sub, iclass, oclass); |
12108 | } | |
12109 | ||
12110 | goto error_return; | |
12111 | } | |
c152c796 AM |
12112 | } |
12113 | } | |
12114 | } | |
12115 | ||
c0f00686 L |
12116 | /* Free symbol buffer if needed. */ |
12117 | if (!info->reduce_memory_overheads) | |
12118 | { | |
c72f2fb2 | 12119 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
12120 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
12121 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
12122 | { |
12123 | free (elf_tdata (sub)->symbuf); | |
12124 | elf_tdata (sub)->symbuf = NULL; | |
12125 | } | |
12126 | } | |
12127 | ||
c152c796 AM |
12128 | /* Output any global symbols that got converted to local in a |
12129 | version script or due to symbol visibility. We do this in a | |
12130 | separate step since ELF requires all local symbols to appear | |
12131 | prior to any global symbols. FIXME: We should only do this if | |
12132 | some global symbols were, in fact, converted to become local. | |
12133 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
12134 | eoinfo.failed = FALSE; | |
8b127cbc | 12135 | eoinfo.flinfo = &flinfo; |
c152c796 | 12136 | eoinfo.localsyms = TRUE; |
34a79995 | 12137 | eoinfo.file_sym_done = FALSE; |
7686d77d | 12138 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12139 | if (eoinfo.failed) |
12140 | return FALSE; | |
12141 | ||
4e617b1e PB |
12142 | /* If backend needs to output some local symbols not present in the hash |
12143 | table, do it now. */ | |
8539e4e8 AM |
12144 | if (bed->elf_backend_output_arch_local_syms |
12145 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 12146 | { |
6e0b88f1 | 12147 | typedef int (*out_sym_func) |
4e617b1e PB |
12148 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12149 | struct elf_link_hash_entry *); | |
12150 | ||
12151 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
12152 | (abfd, info, &flinfo, |
12153 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
12154 | return FALSE; |
12155 | } | |
12156 | ||
c152c796 AM |
12157 | /* That wrote out all the local symbols. Finish up the symbol table |
12158 | with the global symbols. Even if we want to strip everything we | |
12159 | can, we still need to deal with those global symbols that got | |
12160 | converted to local in a version script. */ | |
12161 | ||
12162 | /* The sh_info field records the index of the first non local symbol. */ | |
12163 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
12164 | ||
12165 | if (dynamic | |
64f52338 AM |
12166 | && htab->dynsym != NULL |
12167 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
12168 | { |
12169 | Elf_Internal_Sym sym; | |
64f52338 | 12170 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 12171 | |
64f52338 AM |
12172 | o = htab->dynsym->output_section; |
12173 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
12174 | |
12175 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 12176 | if (bfd_link_pic (info) |
64f52338 | 12177 | || htab->is_relocatable_executable) |
c152c796 AM |
12178 | { |
12179 | asection *s; | |
12180 | ||
12181 | sym.st_size = 0; | |
12182 | sym.st_name = 0; | |
12183 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12184 | sym.st_other = 0; | |
35fc36a8 | 12185 | sym.st_target_internal = 0; |
c152c796 AM |
12186 | |
12187 | for (s = abfd->sections; s != NULL; s = s->next) | |
12188 | { | |
12189 | int indx; | |
12190 | bfd_byte *dest; | |
12191 | long dynindx; | |
12192 | ||
c152c796 | 12193 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
12194 | if (dynindx <= 0) |
12195 | continue; | |
12196 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
12197 | BFD_ASSERT (indx > 0); |
12198 | sym.st_shndx = indx; | |
c0d5a53d L |
12199 | if (! check_dynsym (abfd, &sym)) |
12200 | return FALSE; | |
c152c796 AM |
12201 | sym.st_value = s->vma; |
12202 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
12203 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12204 | } | |
c152c796 AM |
12205 | } |
12206 | ||
12207 | /* Write out the local dynsyms. */ | |
64f52338 | 12208 | if (htab->dynlocal) |
c152c796 AM |
12209 | { |
12210 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 12211 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
12212 | { |
12213 | asection *s; | |
12214 | bfd_byte *dest; | |
12215 | ||
935bd1e0 | 12216 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
12217 | Note that we saved a word of storage and overwrote |
12218 | the original st_name with the dynstr_index. */ | |
12219 | sym = e->isym; | |
935bd1e0 | 12220 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 12221 | |
cb33740c AM |
12222 | s = bfd_section_from_elf_index (e->input_bfd, |
12223 | e->isym.st_shndx); | |
12224 | if (s != NULL) | |
c152c796 | 12225 | { |
c152c796 AM |
12226 | sym.st_shndx = |
12227 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
12228 | if (! check_dynsym (abfd, &sym)) |
12229 | return FALSE; | |
c152c796 AM |
12230 | sym.st_value = (s->output_section->vma |
12231 | + s->output_offset | |
12232 | + e->isym.st_value); | |
12233 | } | |
12234 | ||
c152c796 AM |
12235 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
12236 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12237 | } | |
12238 | } | |
c152c796 AM |
12239 | } |
12240 | ||
12241 | /* We get the global symbols from the hash table. */ | |
12242 | eoinfo.failed = FALSE; | |
12243 | eoinfo.localsyms = FALSE; | |
8b127cbc | 12244 | eoinfo.flinfo = &flinfo; |
7686d77d | 12245 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12246 | if (eoinfo.failed) |
12247 | return FALSE; | |
12248 | ||
12249 | /* If backend needs to output some symbols not present in the hash | |
12250 | table, do it now. */ | |
8539e4e8 AM |
12251 | if (bed->elf_backend_output_arch_syms |
12252 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 12253 | { |
6e0b88f1 | 12254 | typedef int (*out_sym_func) |
c152c796 AM |
12255 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12256 | struct elf_link_hash_entry *); | |
12257 | ||
12258 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
12259 | (abfd, info, &flinfo, |
12260 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
12261 | return FALSE; |
12262 | } | |
12263 | ||
ef10c3ac L |
12264 | /* Finalize the .strtab section. */ |
12265 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
12266 | ||
12267 | /* Swap out the .strtab section. */ | |
12268 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
12269 | return FALSE; |
12270 | ||
12271 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
12272 | if (bfd_get_symcount (abfd) > 0) |
12273 | { | |
ee3b52e9 L |
12274 | /* Finish up and write out the symbol string table (.strtab) |
12275 | section. */ | |
ad32986f | 12276 | Elf_Internal_Shdr *symstrtab_hdr = NULL; |
8539e4e8 AM |
12277 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
12278 | ||
ad32986f | 12279 | if (elf_symtab_shndx_list (abfd)) |
8539e4e8 | 12280 | { |
ad32986f | 12281 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8539e4e8 | 12282 | |
ad32986f NC |
12283 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) |
12284 | { | |
12285 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
12286 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
12287 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
12288 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
12289 | symtab_shndx_hdr->sh_size = amt; | |
8539e4e8 | 12290 | |
ad32986f NC |
12291 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, |
12292 | off, TRUE); | |
12293 | ||
12294 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
12295 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
12296 | return FALSE; | |
12297 | } | |
8539e4e8 | 12298 | } |
ee3b52e9 L |
12299 | |
12300 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
12301 | /* sh_name was set in prep_headers. */ | |
12302 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 12303 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 12304 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 12305 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
12306 | symstrtab_hdr->sh_entsize = 0; |
12307 | symstrtab_hdr->sh_link = 0; | |
12308 | symstrtab_hdr->sh_info = 0; | |
12309 | /* sh_offset is set just below. */ | |
12310 | symstrtab_hdr->sh_addralign = 1; | |
12311 | ||
12312 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
12313 | off, TRUE); | |
12314 | elf_next_file_pos (abfd) = off; | |
12315 | ||
c152c796 | 12316 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 12317 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
12318 | return FALSE; |
12319 | } | |
12320 | ||
76359541 TP |
12321 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
12322 | { | |
871b3ab2 | 12323 | _bfd_error_handler (_("%pB: failed to generate import library"), |
4eca0228 | 12324 | info->out_implib_bfd); |
76359541 TP |
12325 | return FALSE; |
12326 | } | |
12327 | ||
c152c796 AM |
12328 | /* Adjust the relocs to have the correct symbol indices. */ |
12329 | for (o = abfd->sections; o != NULL; o = o->next) | |
12330 | { | |
d4730f92 | 12331 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 12332 | bfd_boolean sort; |
10bbbc1d | 12333 | |
c152c796 AM |
12334 | if ((o->flags & SEC_RELOC) == 0) |
12335 | continue; | |
12336 | ||
28dbcedc | 12337 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 12338 | if (esdo->rel.hdr != NULL |
10bbbc1d | 12339 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) |
bca6d0e3 AM |
12340 | return FALSE; |
12341 | if (esdo->rela.hdr != NULL | |
10bbbc1d | 12342 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) |
bca6d0e3 | 12343 | return FALSE; |
c152c796 AM |
12344 | |
12345 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
12346 | trying to swap the relocs out itself. */ | |
12347 | o->reloc_count = 0; | |
12348 | } | |
12349 | ||
12350 | if (dynamic && info->combreloc && dynobj != NULL) | |
12351 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
12352 | ||
12353 | /* If we are linking against a dynamic object, or generating a | |
12354 | shared library, finish up the dynamic linking information. */ | |
12355 | if (dynamic) | |
12356 | { | |
12357 | bfd_byte *dyncon, *dynconend; | |
12358 | ||
12359 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12360 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12361 | BFD_ASSERT (o != NULL); |
12362 | ||
12363 | dyncon = o->contents; | |
eea6121a | 12364 | dynconend = o->contents + o->size; |
c152c796 AM |
12365 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12366 | { | |
12367 | Elf_Internal_Dyn dyn; | |
12368 | const char *name; | |
12369 | unsigned int type; | |
64487780 AM |
12370 | bfd_size_type sh_size; |
12371 | bfd_vma sh_addr; | |
c152c796 AM |
12372 | |
12373 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12374 | ||
12375 | switch (dyn.d_tag) | |
12376 | { | |
12377 | default: | |
12378 | continue; | |
12379 | case DT_NULL: | |
12380 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12381 | { | |
12382 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12383 | { | |
12384 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12385 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12386 | default: continue; | |
12387 | } | |
12388 | dyn.d_un.d_val = relativecount; | |
12389 | relativecount = 0; | |
12390 | break; | |
12391 | } | |
12392 | continue; | |
12393 | ||
12394 | case DT_INIT: | |
12395 | name = info->init_function; | |
12396 | goto get_sym; | |
12397 | case DT_FINI: | |
12398 | name = info->fini_function; | |
12399 | get_sym: | |
12400 | { | |
12401 | struct elf_link_hash_entry *h; | |
12402 | ||
64f52338 | 12403 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12404 | if (h != NULL |
12405 | && (h->root.type == bfd_link_hash_defined | |
12406 | || h->root.type == bfd_link_hash_defweak)) | |
12407 | { | |
bef26483 | 12408 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12409 | o = h->root.u.def.section; |
12410 | if (o->output_section != NULL) | |
bef26483 | 12411 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12412 | + o->output_offset); |
12413 | else | |
12414 | { | |
12415 | /* The symbol is imported from another shared | |
12416 | library and does not apply to this one. */ | |
bef26483 | 12417 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12418 | } |
12419 | break; | |
12420 | } | |
12421 | } | |
12422 | continue; | |
12423 | ||
12424 | case DT_PREINIT_ARRAYSZ: | |
12425 | name = ".preinit_array"; | |
4ade44b7 | 12426 | goto get_out_size; |
c152c796 AM |
12427 | case DT_INIT_ARRAYSZ: |
12428 | name = ".init_array"; | |
4ade44b7 | 12429 | goto get_out_size; |
c152c796 AM |
12430 | case DT_FINI_ARRAYSZ: |
12431 | name = ".fini_array"; | |
4ade44b7 | 12432 | get_out_size: |
c152c796 AM |
12433 | o = bfd_get_section_by_name (abfd, name); |
12434 | if (o == NULL) | |
12435 | { | |
4eca0228 | 12436 | _bfd_error_handler |
4ade44b7 | 12437 | (_("could not find section %s"), name); |
c152c796 AM |
12438 | goto error_return; |
12439 | } | |
eea6121a | 12440 | if (o->size == 0) |
4eca0228 | 12441 | _bfd_error_handler |
c152c796 | 12442 | (_("warning: %s section has zero size"), name); |
eea6121a | 12443 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12444 | break; |
12445 | ||
12446 | case DT_PREINIT_ARRAY: | |
12447 | name = ".preinit_array"; | |
4ade44b7 | 12448 | goto get_out_vma; |
c152c796 AM |
12449 | case DT_INIT_ARRAY: |
12450 | name = ".init_array"; | |
4ade44b7 | 12451 | goto get_out_vma; |
c152c796 AM |
12452 | case DT_FINI_ARRAY: |
12453 | name = ".fini_array"; | |
4ade44b7 AM |
12454 | get_out_vma: |
12455 | o = bfd_get_section_by_name (abfd, name); | |
12456 | goto do_vma; | |
c152c796 AM |
12457 | |
12458 | case DT_HASH: | |
12459 | name = ".hash"; | |
12460 | goto get_vma; | |
fdc90cb4 JJ |
12461 | case DT_GNU_HASH: |
12462 | name = ".gnu.hash"; | |
12463 | goto get_vma; | |
c152c796 AM |
12464 | case DT_STRTAB: |
12465 | name = ".dynstr"; | |
12466 | goto get_vma; | |
12467 | case DT_SYMTAB: | |
12468 | name = ".dynsym"; | |
12469 | goto get_vma; | |
12470 | case DT_VERDEF: | |
12471 | name = ".gnu.version_d"; | |
12472 | goto get_vma; | |
12473 | case DT_VERNEED: | |
12474 | name = ".gnu.version_r"; | |
12475 | goto get_vma; | |
12476 | case DT_VERSYM: | |
12477 | name = ".gnu.version"; | |
12478 | get_vma: | |
4ade44b7 AM |
12479 | o = bfd_get_linker_section (dynobj, name); |
12480 | do_vma: | |
b3293efa | 12481 | if (o == NULL || bfd_is_abs_section (o->output_section)) |
c152c796 | 12482 | { |
4eca0228 | 12483 | _bfd_error_handler |
4ade44b7 | 12484 | (_("could not find section %s"), name); |
c152c796 AM |
12485 | goto error_return; |
12486 | } | |
894891db NC |
12487 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12488 | { | |
4eca0228 | 12489 | _bfd_error_handler |
894891db NC |
12490 | (_("warning: section '%s' is being made into a note"), name); |
12491 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12492 | goto error_return; | |
12493 | } | |
4ade44b7 | 12494 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12495 | break; |
12496 | ||
12497 | case DT_REL: | |
12498 | case DT_RELA: | |
12499 | case DT_RELSZ: | |
12500 | case DT_RELASZ: | |
12501 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12502 | type = SHT_REL; | |
12503 | else | |
12504 | type = SHT_RELA; | |
64487780 AM |
12505 | sh_size = 0; |
12506 | sh_addr = 0; | |
c152c796 AM |
12507 | for (i = 1; i < elf_numsections (abfd); i++) |
12508 | { | |
12509 | Elf_Internal_Shdr *hdr; | |
12510 | ||
12511 | hdr = elf_elfsections (abfd)[i]; | |
12512 | if (hdr->sh_type == type | |
12513 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12514 | { | |
64487780 AM |
12515 | sh_size += hdr->sh_size; |
12516 | if (sh_addr == 0 | |
12517 | || sh_addr > hdr->sh_addr) | |
12518 | sh_addr = hdr->sh_addr; | |
c152c796 AM |
12519 | } |
12520 | } | |
64487780 | 12521 | |
64f52338 AM |
12522 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12523 | { | |
12524 | /* Don't count procedure linkage table relocs in the | |
12525 | overall reloc count. */ | |
64487780 AM |
12526 | sh_size -= htab->srelplt->size; |
12527 | if (sh_size == 0) | |
12528 | /* If the size is zero, make the address zero too. | |
12529 | This is to avoid a glibc bug. If the backend | |
12530 | emits DT_RELA/DT_RELASZ even when DT_RELASZ is | |
12531 | zero, then we'll put DT_RELA at the end of | |
12532 | DT_JMPREL. glibc will interpret the end of | |
12533 | DT_RELA matching the end of DT_JMPREL as the | |
12534 | case where DT_RELA includes DT_JMPREL, and for | |
12535 | LD_BIND_NOW will decide that processing DT_RELA | |
12536 | will process the PLT relocs too. Net result: | |
12537 | No PLT relocs applied. */ | |
12538 | sh_addr = 0; | |
12539 | ||
64f52338 AM |
12540 | /* If .rela.plt is the first .rela section, exclude |
12541 | it from DT_RELA. */ | |
64487780 AM |
12542 | else if (sh_addr == (htab->srelplt->output_section->vma |
12543 | + htab->srelplt->output_offset)) | |
12544 | sh_addr += htab->srelplt->size; | |
64f52338 | 12545 | } |
64487780 AM |
12546 | |
12547 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12548 | dyn.d_un.d_val = sh_size; | |
12549 | else | |
12550 | dyn.d_un.d_ptr = sh_addr; | |
c152c796 AM |
12551 | break; |
12552 | } | |
12553 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12554 | } | |
12555 | } | |
12556 | ||
12557 | /* If we have created any dynamic sections, then output them. */ | |
12558 | if (dynobj != NULL) | |
12559 | { | |
12560 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12561 | goto error_return; | |
12562 | ||
943284cc | 12563 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12564 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12565 | || info->error_textrel) |
3d4d4302 | 12566 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12567 | { |
12568 | bfd_byte *dyncon, *dynconend; | |
12569 | ||
943284cc DJ |
12570 | dyncon = o->contents; |
12571 | dynconend = o->contents + o->size; | |
12572 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12573 | { | |
12574 | Elf_Internal_Dyn dyn; | |
12575 | ||
12576 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12577 | ||
12578 | if (dyn.d_tag == DT_TEXTREL) | |
12579 | { | |
c192a133 AM |
12580 | if (info->error_textrel) |
12581 | info->callbacks->einfo | |
9793eb77 | 12582 | (_("%P%X: read-only segment has dynamic relocations\n")); |
c192a133 AM |
12583 | else |
12584 | info->callbacks->einfo | |
9793eb77 | 12585 | (_("%P: warning: creating a DT_TEXTREL in a shared object\n")); |
943284cc DJ |
12586 | break; |
12587 | } | |
12588 | } | |
12589 | } | |
12590 | ||
c152c796 AM |
12591 | for (o = dynobj->sections; o != NULL; o = o->next) |
12592 | { | |
12593 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12594 | || o->size == 0 |
c152c796 AM |
12595 | || o->output_section == bfd_abs_section_ptr) |
12596 | continue; | |
12597 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12598 | { | |
12599 | /* At this point, we are only interested in sections | |
12600 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12601 | continue; | |
12602 | } | |
64f52338 | 12603 | if (htab->stab_info.stabstr == o) |
3722b82f | 12604 | continue; |
64f52338 | 12605 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12606 | continue; |
3d4d4302 | 12607 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12608 | { |
12609 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12610 | o->contents, | |
37b01f6a DG |
12611 | (file_ptr) o->output_offset |
12612 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12613 | o->size)) |
c152c796 AM |
12614 | goto error_return; |
12615 | } | |
12616 | else | |
12617 | { | |
12618 | /* The contents of the .dynstr section are actually in a | |
12619 | stringtab. */ | |
8539e4e8 AM |
12620 | file_ptr off; |
12621 | ||
c152c796 AM |
12622 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12623 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12624 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12625 | goto error_return; |
12626 | } | |
12627 | } | |
12628 | } | |
12629 | ||
7bdf4127 | 12630 | if (!info->resolve_section_groups) |
c152c796 AM |
12631 | { |
12632 | bfd_boolean failed = FALSE; | |
12633 | ||
7bdf4127 | 12634 | BFD_ASSERT (bfd_link_relocatable (info)); |
c152c796 AM |
12635 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
12636 | if (failed) | |
12637 | goto error_return; | |
12638 | } | |
12639 | ||
12640 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 12641 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 12642 | { |
64f52338 | 12643 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
12644 | goto error_return; |
12645 | } | |
12646 | ||
9f7c3e5e AM |
12647 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12648 | goto error_return; | |
c152c796 | 12649 | |
9f7c3e5e | 12650 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12651 | |
12bd6957 | 12652 | elf_linker (abfd) = TRUE; |
c152c796 | 12653 | |
104d59d1 JM |
12654 | if (attr_section) |
12655 | { | |
a50b1753 | 12656 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12657 | if (contents == NULL) |
d0f16d5e | 12658 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12659 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12660 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12661 | free (contents); | |
12662 | } | |
12663 | ||
c152c796 AM |
12664 | return TRUE; |
12665 | ||
12666 | error_return: | |
9f7c3e5e | 12667 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12668 | return FALSE; |
12669 | } | |
12670 | \f | |
5241d853 RS |
12671 | /* Initialize COOKIE for input bfd ABFD. */ |
12672 | ||
12673 | static bfd_boolean | |
12674 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12675 | struct bfd_link_info *info, bfd *abfd) | |
12676 | { | |
12677 | Elf_Internal_Shdr *symtab_hdr; | |
12678 | const struct elf_backend_data *bed; | |
12679 | ||
12680 | bed = get_elf_backend_data (abfd); | |
12681 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12682 | ||
12683 | cookie->abfd = abfd; | |
12684 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12685 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12686 | if (cookie->bad_symtab) | |
12687 | { | |
12688 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12689 | cookie->extsymoff = 0; | |
12690 | } | |
12691 | else | |
12692 | { | |
12693 | cookie->locsymcount = symtab_hdr->sh_info; | |
12694 | cookie->extsymoff = symtab_hdr->sh_info; | |
12695 | } | |
12696 | ||
12697 | if (bed->s->arch_size == 32) | |
12698 | cookie->r_sym_shift = 8; | |
12699 | else | |
12700 | cookie->r_sym_shift = 32; | |
12701 | ||
12702 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12703 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12704 | { | |
12705 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12706 | cookie->locsymcount, 0, | |
12707 | NULL, NULL, NULL); | |
12708 | if (cookie->locsyms == NULL) | |
12709 | { | |
12710 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12711 | return FALSE; | |
12712 | } | |
12713 | if (info->keep_memory) | |
12714 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12715 | } | |
12716 | return TRUE; | |
12717 | } | |
12718 | ||
12719 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12720 | ||
12721 | static void | |
12722 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12723 | { | |
12724 | Elf_Internal_Shdr *symtab_hdr; | |
12725 | ||
12726 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12727 | if (cookie->locsyms != NULL | |
12728 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12729 | free (cookie->locsyms); | |
12730 | } | |
12731 | ||
12732 | /* Initialize the relocation information in COOKIE for input section SEC | |
12733 | of input bfd ABFD. */ | |
12734 | ||
12735 | static bfd_boolean | |
12736 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12737 | struct bfd_link_info *info, bfd *abfd, | |
12738 | asection *sec) | |
12739 | { | |
5241d853 RS |
12740 | if (sec->reloc_count == 0) |
12741 | { | |
12742 | cookie->rels = NULL; | |
12743 | cookie->relend = NULL; | |
12744 | } | |
12745 | else | |
12746 | { | |
5241d853 RS |
12747 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, |
12748 | info->keep_memory); | |
12749 | if (cookie->rels == NULL) | |
12750 | return FALSE; | |
12751 | cookie->rel = cookie->rels; | |
056bafd4 | 12752 | cookie->relend = cookie->rels + sec->reloc_count; |
5241d853 RS |
12753 | } |
12754 | cookie->rel = cookie->rels; | |
12755 | return TRUE; | |
12756 | } | |
12757 | ||
12758 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12759 | if appropriate. */ | |
12760 | ||
12761 | static void | |
12762 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12763 | asection *sec) | |
12764 | { | |
12765 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12766 | free (cookie->rels); | |
12767 | } | |
12768 | ||
12769 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12770 | ||
12771 | static bfd_boolean | |
12772 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12773 | struct bfd_link_info *info, | |
12774 | asection *sec) | |
12775 | { | |
12776 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12777 | goto error1; | |
12778 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12779 | goto error2; | |
12780 | return TRUE; | |
12781 | ||
12782 | error2: | |
12783 | fini_reloc_cookie (cookie, sec->owner); | |
12784 | error1: | |
12785 | return FALSE; | |
12786 | } | |
12787 | ||
12788 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12789 | if appropriate. */ | |
12790 | ||
12791 | static void | |
12792 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12793 | asection *sec) | |
12794 | { | |
12795 | fini_reloc_cookie_rels (cookie, sec); | |
12796 | fini_reloc_cookie (cookie, sec->owner); | |
12797 | } | |
12798 | \f | |
c152c796 AM |
12799 | /* Garbage collect unused sections. */ |
12800 | ||
07adf181 AM |
12801 | /* Default gc_mark_hook. */ |
12802 | ||
12803 | asection * | |
12804 | _bfd_elf_gc_mark_hook (asection *sec, | |
12805 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12806 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12807 | struct elf_link_hash_entry *h, | |
12808 | Elf_Internal_Sym *sym) | |
12809 | { | |
12810 | if (h != NULL) | |
12811 | { | |
12812 | switch (h->root.type) | |
12813 | { | |
12814 | case bfd_link_hash_defined: | |
12815 | case bfd_link_hash_defweak: | |
12816 | return h->root.u.def.section; | |
12817 | ||
12818 | case bfd_link_hash_common: | |
12819 | return h->root.u.c.p->section; | |
12820 | ||
12821 | default: | |
12822 | break; | |
12823 | } | |
12824 | } | |
12825 | else | |
12826 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12827 | ||
12828 | return NULL; | |
12829 | } | |
12830 | ||
9e223787 | 12831 | /* Return the debug definition section. */ |
b7c871ed L |
12832 | |
12833 | static asection * | |
12834 | elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, | |
12835 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12836 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12837 | struct elf_link_hash_entry *h, | |
9e223787 | 12838 | Elf_Internal_Sym *sym) |
b7c871ed | 12839 | { |
9e223787 L |
12840 | if (h != NULL) |
12841 | { | |
12842 | /* Return the global debug definition section. */ | |
12843 | if ((h->root.type == bfd_link_hash_defined | |
12844 | || h->root.type == bfd_link_hash_defweak) | |
12845 | && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) | |
12846 | return h->root.u.def.section; | |
12847 | } | |
12848 | else | |
12849 | { | |
12850 | /* Return the local debug definition section. */ | |
12851 | asection *isec = bfd_section_from_elf_index (sec->owner, | |
12852 | sym->st_shndx); | |
12853 | if ((isec->flags & SEC_DEBUGGING) != 0) | |
12854 | return isec; | |
12855 | } | |
b7c871ed L |
12856 | |
12857 | return NULL; | |
12858 | } | |
12859 | ||
5241d853 RS |
12860 | /* COOKIE->rel describes a relocation against section SEC, which is |
12861 | a section we've decided to keep. Return the section that contains | |
12862 | the relocation symbol, or NULL if no section contains it. */ | |
12863 | ||
12864 | asection * | |
12865 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
12866 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
12867 | struct elf_reloc_cookie *cookie, |
12868 | bfd_boolean *start_stop) | |
5241d853 RS |
12869 | { |
12870 | unsigned long r_symndx; | |
12871 | struct elf_link_hash_entry *h; | |
12872 | ||
12873 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 12874 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
12875 | return NULL; |
12876 | ||
12877 | if (r_symndx >= cookie->locsymcount | |
12878 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12879 | { | |
12880 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
12881 | if (h == NULL) |
12882 | { | |
871b3ab2 | 12883 | info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"), |
263ddf68 L |
12884 | sec->owner); |
12885 | return NULL; | |
12886 | } | |
5241d853 RS |
12887 | while (h->root.type == bfd_link_hash_indirect |
12888 | || h->root.type == bfd_link_hash_warning) | |
12889 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 12890 | h->mark = 1; |
4e6b54a6 AM |
12891 | /* If this symbol is weak and there is a non-weak definition, we |
12892 | keep the non-weak definition because many backends put | |
12893 | dynamic reloc info on the non-weak definition for code | |
12894 | handling copy relocs. */ | |
60d67dc8 AM |
12895 | if (h->is_weakalias) |
12896 | weakdef (h)->mark = 1; | |
1cce69b9 | 12897 | |
a6a4679f | 12898 | if (start_stop != NULL) |
1cce69b9 | 12899 | { |
7dba9362 AM |
12900 | /* To work around a glibc bug, mark XXX input sections |
12901 | when there is a reference to __start_XXX or __stop_XXX | |
12902 | symbols. */ | |
cbd0eecf | 12903 | if (h->start_stop) |
1cce69b9 | 12904 | { |
cbd0eecf | 12905 | asection *s = h->u2.start_stop_section; |
a6a4679f AM |
12906 | *start_stop = !s->gc_mark; |
12907 | return s; | |
1cce69b9 AM |
12908 | } |
12909 | } | |
12910 | ||
5241d853 RS |
12911 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
12912 | } | |
12913 | ||
12914 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
12915 | &cookie->locsyms[r_symndx]); | |
12916 | } | |
12917 | ||
12918 | /* COOKIE->rel describes a relocation against section SEC, which is | |
12919 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 12920 | the relocation symbol. */ |
5241d853 RS |
12921 | |
12922 | bfd_boolean | |
12923 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
12924 | asection *sec, | |
12925 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 12926 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
12927 | { |
12928 | asection *rsec; | |
1cce69b9 | 12929 | bfd_boolean start_stop = FALSE; |
5241d853 | 12930 | |
1cce69b9 AM |
12931 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
12932 | while (rsec != NULL) | |
5241d853 | 12933 | { |
1cce69b9 AM |
12934 | if (!rsec->gc_mark) |
12935 | { | |
12936 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
12937 | || (rsec->owner->flags & DYNAMIC) != 0) | |
12938 | rsec->gc_mark = 1; | |
12939 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
12940 | return FALSE; | |
12941 | } | |
12942 | if (!start_stop) | |
12943 | break; | |
199af150 | 12944 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
12945 | } |
12946 | return TRUE; | |
12947 | } | |
12948 | ||
07adf181 AM |
12949 | /* The mark phase of garbage collection. For a given section, mark |
12950 | it and any sections in this section's group, and all the sections | |
12951 | which define symbols to which it refers. */ | |
12952 | ||
ccfa59ea AM |
12953 | bfd_boolean |
12954 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
12955 | asection *sec, | |
6a5bb875 | 12956 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
12957 | { |
12958 | bfd_boolean ret; | |
9d0a14d3 | 12959 | asection *group_sec, *eh_frame; |
c152c796 AM |
12960 | |
12961 | sec->gc_mark = 1; | |
12962 | ||
12963 | /* Mark all the sections in the group. */ | |
12964 | group_sec = elf_section_data (sec)->next_in_group; | |
12965 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 12966 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
12967 | return FALSE; |
12968 | ||
12969 | /* Look through the section relocs. */ | |
12970 | ret = TRUE; | |
9d0a14d3 RS |
12971 | eh_frame = elf_eh_frame_section (sec->owner); |
12972 | if ((sec->flags & SEC_RELOC) != 0 | |
12973 | && sec->reloc_count > 0 | |
12974 | && sec != eh_frame) | |
c152c796 | 12975 | { |
5241d853 | 12976 | struct elf_reloc_cookie cookie; |
c152c796 | 12977 | |
5241d853 RS |
12978 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
12979 | ret = FALSE; | |
c152c796 | 12980 | else |
c152c796 | 12981 | { |
5241d853 | 12982 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 12983 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
12984 | { |
12985 | ret = FALSE; | |
12986 | break; | |
12987 | } | |
12988 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
12989 | } |
12990 | } | |
9d0a14d3 RS |
12991 | |
12992 | if (ret && eh_frame && elf_fde_list (sec)) | |
12993 | { | |
12994 | struct elf_reloc_cookie cookie; | |
12995 | ||
12996 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
12997 | ret = FALSE; | |
12998 | else | |
12999 | { | |
13000 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
13001 | gc_mark_hook, &cookie)) | |
13002 | ret = FALSE; | |
13003 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
13004 | } | |
13005 | } | |
13006 | ||
2f0c68f2 CM |
13007 | eh_frame = elf_section_eh_frame_entry (sec); |
13008 | if (ret && eh_frame && !eh_frame->gc_mark) | |
13009 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
13010 | ret = FALSE; | |
13011 | ||
c152c796 AM |
13012 | return ret; |
13013 | } | |
13014 | ||
3c758495 TG |
13015 | /* Scan and mark sections in a special or debug section group. */ |
13016 | ||
13017 | static void | |
13018 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
13019 | { | |
13020 | /* Point to first section of section group. */ | |
13021 | asection *ssec; | |
13022 | /* Used to iterate the section group. */ | |
13023 | asection *msec; | |
13024 | ||
13025 | bfd_boolean is_special_grp = TRUE; | |
13026 | bfd_boolean is_debug_grp = TRUE; | |
13027 | ||
13028 | /* First scan to see if group contains any section other than debug | |
13029 | and special section. */ | |
13030 | ssec = msec = elf_next_in_group (grp); | |
13031 | do | |
13032 | { | |
13033 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
13034 | is_debug_grp = FALSE; | |
13035 | ||
13036 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
13037 | is_special_grp = FALSE; | |
13038 | ||
13039 | msec = elf_next_in_group (msec); | |
13040 | } | |
13041 | while (msec != ssec); | |
13042 | ||
13043 | /* If this is a pure debug section group or pure special section group, | |
13044 | keep all sections in this group. */ | |
13045 | if (is_debug_grp || is_special_grp) | |
13046 | { | |
13047 | do | |
13048 | { | |
13049 | msec->gc_mark = 1; | |
13050 | msec = elf_next_in_group (msec); | |
13051 | } | |
13052 | while (msec != ssec); | |
13053 | } | |
13054 | } | |
13055 | ||
7f6ab9f8 AM |
13056 | /* Keep debug and special sections. */ |
13057 | ||
13058 | bfd_boolean | |
13059 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
13060 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
13061 | { | |
13062 | bfd *ibfd; | |
13063 | ||
c72f2fb2 | 13064 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
13065 | { |
13066 | asection *isec; | |
13067 | bfd_boolean some_kept; | |
b40bf0a2 | 13068 | bfd_boolean debug_frag_seen; |
b7c871ed | 13069 | bfd_boolean has_kept_debug_info; |
7f6ab9f8 AM |
13070 | |
13071 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13072 | continue; | |
57963c05 AM |
13073 | isec = ibfd->sections; |
13074 | if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13075 | continue; | |
7f6ab9f8 | 13076 | |
b40bf0a2 NC |
13077 | /* Ensure all linker created sections are kept, |
13078 | see if any other section is already marked, | |
13079 | and note if we have any fragmented debug sections. */ | |
b7c871ed | 13080 | debug_frag_seen = some_kept = has_kept_debug_info = FALSE; |
7f6ab9f8 AM |
13081 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
13082 | { | |
13083 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
13084 | isec->gc_mark = 1; | |
eb026f09 AM |
13085 | else if (isec->gc_mark |
13086 | && (isec->flags & SEC_ALLOC) != 0 | |
13087 | && elf_section_type (isec) != SHT_NOTE) | |
7f6ab9f8 | 13088 | some_kept = TRUE; |
b40bf0a2 | 13089 | |
535b785f | 13090 | if (!debug_frag_seen |
b40bf0a2 NC |
13091 | && (isec->flags & SEC_DEBUGGING) |
13092 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
13093 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
13094 | } |
13095 | ||
eb026f09 AM |
13096 | /* If no non-note alloc section in this file will be kept, then |
13097 | we can toss out the debug and special sections. */ | |
7f6ab9f8 AM |
13098 | if (!some_kept) |
13099 | continue; | |
13100 | ||
13101 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
13102 | not part of a group. Also keep section groups that contain |
13103 | just debug sections or special sections. */ | |
7f6ab9f8 | 13104 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
13105 | { |
13106 | if ((isec->flags & SEC_GROUP) != 0) | |
13107 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
13108 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
13109 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
13110 | && elf_next_in_group (isec) == NULL) | |
13111 | isec->gc_mark = 1; | |
b7c871ed L |
13112 | if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) |
13113 | has_kept_debug_info = TRUE; | |
3c758495 | 13114 | } |
b40bf0a2 | 13115 | |
b40bf0a2 NC |
13116 | /* Look for CODE sections which are going to be discarded, |
13117 | and find and discard any fragmented debug sections which | |
13118 | are associated with that code section. */ | |
b7c871ed L |
13119 | if (debug_frag_seen) |
13120 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13121 | if ((isec->flags & SEC_CODE) != 0 | |
13122 | && isec->gc_mark == 0) | |
13123 | { | |
13124 | unsigned int ilen; | |
13125 | asection *dsec; | |
b40bf0a2 | 13126 | |
b7c871ed | 13127 | ilen = strlen (isec->name); |
b40bf0a2 | 13128 | |
b7c871ed | 13129 | /* Association is determined by the name of the debug |
07d6d2b8 | 13130 | section containing the name of the code section as |
b7c871ed L |
13131 | a suffix. For example .debug_line.text.foo is a |
13132 | debug section associated with .text.foo. */ | |
13133 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
13134 | { | |
13135 | unsigned int dlen; | |
b40bf0a2 | 13136 | |
b7c871ed L |
13137 | if (dsec->gc_mark == 0 |
13138 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
13139 | continue; | |
b40bf0a2 | 13140 | |
b7c871ed | 13141 | dlen = strlen (dsec->name); |
b40bf0a2 | 13142 | |
b7c871ed L |
13143 | if (dlen > ilen |
13144 | && strncmp (dsec->name + (dlen - ilen), | |
13145 | isec->name, ilen) == 0) | |
b40bf0a2 | 13146 | dsec->gc_mark = 0; |
b7c871ed | 13147 | } |
b40bf0a2 | 13148 | } |
b7c871ed L |
13149 | |
13150 | /* Mark debug sections referenced by kept debug sections. */ | |
13151 | if (has_kept_debug_info) | |
13152 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13153 | if (isec->gc_mark | |
13154 | && (isec->flags & SEC_DEBUGGING) != 0) | |
13155 | if (!_bfd_elf_gc_mark (info, isec, | |
13156 | elf_gc_mark_debug_section)) | |
13157 | return FALSE; | |
7f6ab9f8 AM |
13158 | } |
13159 | return TRUE; | |
13160 | } | |
13161 | ||
c152c796 | 13162 | static bfd_boolean |
ccabcbe5 | 13163 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
13164 | { |
13165 | bfd *sub; | |
ccabcbe5 | 13166 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 13167 | |
c72f2fb2 | 13168 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13169 | { |
13170 | asection *o; | |
13171 | ||
b19a8f85 | 13172 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13173 | || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) |
b19a8f85 | 13174 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 | 13175 | continue; |
57963c05 AM |
13176 | o = sub->sections; |
13177 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13178 | continue; | |
c152c796 AM |
13179 | |
13180 | for (o = sub->sections; o != NULL; o = o->next) | |
13181 | { | |
a33dafc3 L |
13182 | /* When any section in a section group is kept, we keep all |
13183 | sections in the section group. If the first member of | |
13184 | the section group is excluded, we will also exclude the | |
13185 | group section. */ | |
13186 | if (o->flags & SEC_GROUP) | |
13187 | { | |
13188 | asection *first = elf_next_in_group (o); | |
13189 | o->gc_mark = first->gc_mark; | |
13190 | } | |
c152c796 | 13191 | |
1e7eae0d | 13192 | if (o->gc_mark) |
c152c796 AM |
13193 | continue; |
13194 | ||
13195 | /* Skip sweeping sections already excluded. */ | |
13196 | if (o->flags & SEC_EXCLUDE) | |
13197 | continue; | |
13198 | ||
13199 | /* Since this is early in the link process, it is simple | |
13200 | to remove a section from the output. */ | |
13201 | o->flags |= SEC_EXCLUDE; | |
13202 | ||
c55fe096 | 13203 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 13204 | /* xgettext:c-format */ |
9793eb77 | 13205 | _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), |
c08bb8dd | 13206 | o, sub); |
c152c796 AM |
13207 | } |
13208 | } | |
13209 | ||
c152c796 AM |
13210 | return TRUE; |
13211 | } | |
13212 | ||
13213 | /* Propagate collected vtable information. This is called through | |
13214 | elf_link_hash_traverse. */ | |
13215 | ||
13216 | static bfd_boolean | |
13217 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
13218 | { | |
c152c796 | 13219 | /* Those that are not vtables. */ |
cbd0eecf L |
13220 | if (h->start_stop |
13221 | || h->u2.vtable == NULL | |
13222 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13223 | return TRUE; |
13224 | ||
13225 | /* Those vtables that do not have parents, we cannot merge. */ | |
cbd0eecf | 13226 | if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
13227 | return TRUE; |
13228 | ||
13229 | /* If we've already been done, exit. */ | |
cbd0eecf | 13230 | if (h->u2.vtable->used && h->u2.vtable->used[-1]) |
c152c796 AM |
13231 | return TRUE; |
13232 | ||
13233 | /* Make sure the parent's table is up to date. */ | |
cbd0eecf | 13234 | elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); |
c152c796 | 13235 | |
cbd0eecf | 13236 | if (h->u2.vtable->used == NULL) |
c152c796 AM |
13237 | { |
13238 | /* None of this table's entries were referenced. Re-use the | |
13239 | parent's table. */ | |
cbd0eecf L |
13240 | h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; |
13241 | h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; | |
c152c796 AM |
13242 | } |
13243 | else | |
13244 | { | |
13245 | size_t n; | |
13246 | bfd_boolean *cu, *pu; | |
13247 | ||
13248 | /* Or the parent's entries into ours. */ | |
cbd0eecf | 13249 | cu = h->u2.vtable->used; |
c152c796 | 13250 | cu[-1] = TRUE; |
cbd0eecf | 13251 | pu = h->u2.vtable->parent->u2.vtable->used; |
c152c796 AM |
13252 | if (pu != NULL) |
13253 | { | |
13254 | const struct elf_backend_data *bed; | |
13255 | unsigned int log_file_align; | |
13256 | ||
13257 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
13258 | log_file_align = bed->s->log_file_align; | |
cbd0eecf | 13259 | n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; |
c152c796 AM |
13260 | while (n--) |
13261 | { | |
13262 | if (*pu) | |
13263 | *cu = TRUE; | |
13264 | pu++; | |
13265 | cu++; | |
13266 | } | |
13267 | } | |
13268 | } | |
13269 | ||
13270 | return TRUE; | |
13271 | } | |
13272 | ||
13273 | static bfd_boolean | |
13274 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
13275 | { | |
13276 | asection *sec; | |
13277 | bfd_vma hstart, hend; | |
13278 | Elf_Internal_Rela *relstart, *relend, *rel; | |
13279 | const struct elf_backend_data *bed; | |
13280 | unsigned int log_file_align; | |
13281 | ||
c152c796 AM |
13282 | /* Take care of both those symbols that do not describe vtables as |
13283 | well as those that are not loaded. */ | |
cbd0eecf L |
13284 | if (h->start_stop |
13285 | || h->u2.vtable == NULL | |
13286 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13287 | return TRUE; |
13288 | ||
13289 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13290 | || h->root.type == bfd_link_hash_defweak); | |
13291 | ||
13292 | sec = h->root.u.def.section; | |
13293 | hstart = h->root.u.def.value; | |
13294 | hend = hstart + h->size; | |
13295 | ||
13296 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13297 | if (!relstart) | |
13298 | return *(bfd_boolean *) okp = FALSE; | |
13299 | bed = get_elf_backend_data (sec->owner); | |
13300 | log_file_align = bed->s->log_file_align; | |
13301 | ||
056bafd4 | 13302 | relend = relstart + sec->reloc_count; |
c152c796 AM |
13303 | |
13304 | for (rel = relstart; rel < relend; ++rel) | |
13305 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13306 | { | |
13307 | /* If the entry is in use, do nothing. */ | |
cbd0eecf L |
13308 | if (h->u2.vtable->used |
13309 | && (rel->r_offset - hstart) < h->u2.vtable->size) | |
c152c796 AM |
13310 | { |
13311 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
cbd0eecf | 13312 | if (h->u2.vtable->used[entry]) |
c152c796 AM |
13313 | continue; |
13314 | } | |
13315 | /* Otherwise, kill it. */ | |
13316 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13317 | } | |
13318 | ||
13319 | return TRUE; | |
13320 | } | |
13321 | ||
87538722 AM |
13322 | /* Mark sections containing dynamically referenced symbols. When |
13323 | building shared libraries, we must assume that any visible symbol is | |
13324 | referenced. */ | |
715df9b8 | 13325 | |
64d03ab5 AM |
13326 | bfd_boolean |
13327 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13328 | { |
87538722 | 13329 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13330 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13331 | |
715df9b8 EB |
13332 | if ((h->root.type == bfd_link_hash_defined |
13333 | || h->root.type == bfd_link_hash_defweak) | |
d664fd41 | 13334 | && ((h->ref_dynamic && !h->forced_local) |
c4621b33 | 13335 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13336 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13337 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13338 | && (!bfd_link_executable (info) |
22185505 | 13339 | || info->gc_keep_exported |
b407645f AM |
13340 | || info->export_dynamic |
13341 | || (h->dynamic | |
13342 | && d != NULL | |
13343 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13344 | && (h->versioned >= versioned |
54e8959c L |
13345 | || !bfd_hide_sym_by_version (info->version_info, |
13346 | h->root.root.string))))) | |
715df9b8 EB |
13347 | h->root.u.def.section->flags |= SEC_KEEP; |
13348 | ||
13349 | return TRUE; | |
13350 | } | |
3b36f7e6 | 13351 | |
74f0fb50 AM |
13352 | /* Keep all sections containing symbols undefined on the command-line, |
13353 | and the section containing the entry symbol. */ | |
13354 | ||
13355 | void | |
13356 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13357 | { | |
13358 | struct bfd_sym_chain *sym; | |
13359 | ||
13360 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13361 | { | |
13362 | struct elf_link_hash_entry *h; | |
13363 | ||
13364 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13365 | FALSE, FALSE, FALSE); | |
13366 | ||
13367 | if (h != NULL | |
13368 | && (h->root.type == bfd_link_hash_defined | |
13369 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13370 | && !bfd_is_abs_section (h->root.u.def.section) |
13371 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13372 | h->root.u.def.section->flags |= SEC_KEEP; |
13373 | } | |
13374 | } | |
13375 | ||
2f0c68f2 CM |
13376 | bfd_boolean |
13377 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13378 | struct bfd_link_info *info) | |
13379 | { | |
13380 | bfd *ibfd = info->input_bfds; | |
13381 | ||
13382 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13383 | { | |
13384 | asection *sec; | |
13385 | struct elf_reloc_cookie cookie; | |
13386 | ||
13387 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13388 | continue; | |
57963c05 AM |
13389 | sec = ibfd->sections; |
13390 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13391 | continue; | |
2f0c68f2 CM |
13392 | |
13393 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13394 | return FALSE; | |
13395 | ||
13396 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13397 | { | |
13398 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13399 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13400 | { | |
13401 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13402 | fini_reloc_cookie_rels (&cookie, sec); | |
13403 | } | |
13404 | } | |
13405 | } | |
13406 | return TRUE; | |
13407 | } | |
13408 | ||
c152c796 AM |
13409 | /* Do mark and sweep of unused sections. */ |
13410 | ||
13411 | bfd_boolean | |
13412 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13413 | { | |
13414 | bfd_boolean ok = TRUE; | |
13415 | bfd *sub; | |
6a5bb875 | 13416 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13417 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13418 | struct elf_link_hash_table *htab; |
c152c796 | 13419 | |
64d03ab5 | 13420 | if (!bed->can_gc_sections |
715df9b8 | 13421 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13422 | { |
9793eb77 | 13423 | _bfd_error_handler(_("warning: gc-sections option ignored")); |
c152c796 AM |
13424 | return TRUE; |
13425 | } | |
13426 | ||
74f0fb50 | 13427 | bed->gc_keep (info); |
da44f4e5 | 13428 | htab = elf_hash_table (info); |
74f0fb50 | 13429 | |
9d0a14d3 RS |
13430 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13431 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13432 | for (sub = info->input_bfds; |
13433 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13434 | sub = sub->link.next) | |
9d0a14d3 RS |
13435 | { |
13436 | asection *sec; | |
13437 | struct elf_reloc_cookie cookie; | |
13438 | ||
57963c05 AM |
13439 | sec = sub->sections; |
13440 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13441 | continue; | |
9d0a14d3 | 13442 | sec = bfd_get_section_by_name (sub, ".eh_frame"); |
9a2a56cc | 13443 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13444 | { |
13445 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13446 | if (elf_section_data (sec)->sec_info |
13447 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13448 | elf_eh_frame_section (sub) = sec; |
13449 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13450 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13451 | } |
13452 | } | |
9d0a14d3 | 13453 | |
c152c796 | 13454 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13455 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13456 | if (!ok) |
13457 | return FALSE; | |
13458 | ||
13459 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13460 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13461 | if (!ok) |
13462 | return FALSE; | |
13463 | ||
715df9b8 | 13464 | /* Mark dynamically referenced symbols. */ |
22185505 | 13465 | if (htab->dynamic_sections_created || info->gc_keep_exported) |
da44f4e5 | 13466 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); |
c152c796 | 13467 | |
715df9b8 | 13468 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13469 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13470 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13471 | { |
13472 | asection *o; | |
13473 | ||
b19a8f85 | 13474 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13475 | || elf_object_id (sub) != elf_hash_table_id (htab) |
b19a8f85 | 13476 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 AM |
13477 | continue; |
13478 | ||
57963c05 AM |
13479 | o = sub->sections; |
13480 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13481 | continue; | |
13482 | ||
7f6ab9f8 AM |
13483 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13484 | Also treat note sections as a root, if the section is not part | |
8b6f4cd3 L |
13485 | of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as |
13486 | well as FINI_ARRAY sections for ld -r. */ | |
c152c796 | 13487 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13488 | if (!o->gc_mark |
13489 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13490 | && ((o->flags & SEC_KEEP) != 0 |
8b6f4cd3 L |
13491 | || (bfd_link_relocatable (info) |
13492 | && ((elf_section_data (o)->this_hdr.sh_type | |
13493 | == SHT_PREINIT_ARRAY) | |
13494 | || (elf_section_data (o)->this_hdr.sh_type | |
13495 | == SHT_INIT_ARRAY) | |
13496 | || (elf_section_data (o)->this_hdr.sh_type | |
13497 | == SHT_FINI_ARRAY))) | |
7f6ab9f8 AM |
13498 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13499 | && elf_next_in_group (o) == NULL ))) | |
13500 | { | |
13501 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13502 | return FALSE; | |
13503 | } | |
c152c796 AM |
13504 | } |
13505 | ||
6a5bb875 | 13506 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13507 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13508 | |
c152c796 | 13509 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13510 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13511 | } |
13512 | \f | |
13513 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13514 | ||
13515 | bfd_boolean | |
13516 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13517 | asection *sec, | |
13518 | struct elf_link_hash_entry *h, | |
13519 | bfd_vma offset) | |
13520 | { | |
13521 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13522 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13523 | size_t extsymcount; |
c152c796 AM |
13524 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13525 | ||
13526 | /* The sh_info field of the symtab header tells us where the | |
13527 | external symbols start. We don't care about the local symbols at | |
13528 | this point. */ | |
13529 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13530 | if (!elf_bad_symtab (abfd)) | |
13531 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13532 | ||
13533 | sym_hashes = elf_sym_hashes (abfd); | |
13534 | sym_hashes_end = sym_hashes + extsymcount; | |
13535 | ||
13536 | /* Hunt down the child symbol, which is in this section at the same | |
13537 | offset as the relocation. */ | |
13538 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13539 | { | |
13540 | if ((child = *search) != NULL | |
13541 | && (child->root.type == bfd_link_hash_defined | |
13542 | || child->root.type == bfd_link_hash_defweak) | |
13543 | && child->root.u.def.section == sec | |
13544 | && child->root.u.def.value == offset) | |
13545 | goto win; | |
13546 | } | |
13547 | ||
695344c0 | 13548 | /* xgettext:c-format */ |
9793eb77 | 13549 | _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"), |
2dcf00ce | 13550 | abfd, sec, (uint64_t) offset); |
c152c796 AM |
13551 | bfd_set_error (bfd_error_invalid_operation); |
13552 | return FALSE; | |
13553 | ||
13554 | win: | |
cbd0eecf | 13555 | if (!child->u2.vtable) |
f6e332e6 | 13556 | { |
cbd0eecf L |
13557 | child->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13558 | bfd_zalloc (abfd, sizeof (*child->u2.vtable))); | |
13559 | if (!child->u2.vtable) | |
f6e332e6 AM |
13560 | return FALSE; |
13561 | } | |
c152c796 AM |
13562 | if (!h) |
13563 | { | |
13564 | /* This *should* only be the absolute section. It could potentially | |
13565 | be that someone has defined a non-global vtable though, which | |
13566 | would be bad. It isn't worth paging in the local symbols to be | |
13567 | sure though; that case should simply be handled by the assembler. */ | |
13568 | ||
cbd0eecf | 13569 | child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13570 | } |
13571 | else | |
cbd0eecf | 13572 | child->u2.vtable->parent = h; |
c152c796 AM |
13573 | |
13574 | return TRUE; | |
13575 | } | |
13576 | ||
13577 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13578 | ||
13579 | bfd_boolean | |
13580 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
13581 | asection *sec ATTRIBUTE_UNUSED, | |
13582 | struct elf_link_hash_entry *h, | |
13583 | bfd_vma addend) | |
13584 | { | |
13585 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13586 | unsigned int log_file_align = bed->s->log_file_align; | |
13587 | ||
cbd0eecf | 13588 | if (!h->u2.vtable) |
f6e332e6 | 13589 | { |
cbd0eecf L |
13590 | h->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13591 | bfd_zalloc (abfd, sizeof (*h->u2.vtable))); | |
13592 | if (!h->u2.vtable) | |
f6e332e6 AM |
13593 | return FALSE; |
13594 | } | |
13595 | ||
cbd0eecf | 13596 | if (addend >= h->u2.vtable->size) |
c152c796 AM |
13597 | { |
13598 | size_t size, bytes, file_align; | |
cbd0eecf | 13599 | bfd_boolean *ptr = h->u2.vtable->used; |
c152c796 AM |
13600 | |
13601 | /* While the symbol is undefined, we have to be prepared to handle | |
13602 | a zero size. */ | |
13603 | file_align = 1 << log_file_align; | |
13604 | if (h->root.type == bfd_link_hash_undefined) | |
13605 | size = addend + file_align; | |
13606 | else | |
13607 | { | |
13608 | size = h->size; | |
13609 | if (addend >= size) | |
13610 | { | |
13611 | /* Oops! We've got a reference past the defined end of | |
13612 | the table. This is probably a bug -- shall we warn? */ | |
13613 | size = addend + file_align; | |
13614 | } | |
13615 | } | |
13616 | size = (size + file_align - 1) & -file_align; | |
13617 | ||
13618 | /* Allocate one extra entry for use as a "done" flag for the | |
13619 | consolidation pass. */ | |
13620 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13621 | ||
13622 | if (ptr) | |
13623 | { | |
a50b1753 | 13624 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13625 | |
13626 | if (ptr != NULL) | |
13627 | { | |
13628 | size_t oldbytes; | |
13629 | ||
cbd0eecf | 13630 | oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) |
c152c796 AM |
13631 | * sizeof (bfd_boolean)); |
13632 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13633 | } | |
13634 | } | |
13635 | else | |
a50b1753 | 13636 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13637 | |
13638 | if (ptr == NULL) | |
13639 | return FALSE; | |
13640 | ||
13641 | /* And arrange for that done flag to be at index -1. */ | |
cbd0eecf L |
13642 | h->u2.vtable->used = ptr + 1; |
13643 | h->u2.vtable->size = size; | |
c152c796 AM |
13644 | } |
13645 | ||
cbd0eecf | 13646 | h->u2.vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13647 | |
13648 | return TRUE; | |
13649 | } | |
13650 | ||
ae17ab41 CM |
13651 | /* Map an ELF section header flag to its corresponding string. */ |
13652 | typedef struct | |
13653 | { | |
13654 | char *flag_name; | |
13655 | flagword flag_value; | |
13656 | } elf_flags_to_name_table; | |
13657 | ||
13658 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13659 | { | |
13660 | { "SHF_WRITE", SHF_WRITE }, | |
13661 | { "SHF_ALLOC", SHF_ALLOC }, | |
13662 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13663 | { "SHF_MERGE", SHF_MERGE }, | |
13664 | { "SHF_STRINGS", SHF_STRINGS }, | |
13665 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13666 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13667 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13668 | { "SHF_GROUP", SHF_GROUP }, | |
13669 | { "SHF_TLS", SHF_TLS }, | |
13670 | { "SHF_MASKOS", SHF_MASKOS }, | |
13671 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13672 | }; | |
13673 | ||
b9c361e0 JL |
13674 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13675 | bfd_boolean | |
ae17ab41 | 13676 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13677 | struct flag_info *flaginfo, |
b9c361e0 | 13678 | asection *section) |
ae17ab41 | 13679 | { |
8b127cbc | 13680 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13681 | |
8b127cbc | 13682 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13683 | { |
8b127cbc AM |
13684 | bfd *obfd = info->output_bfd; |
13685 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13686 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13687 | int with_hex = 0; |
13688 | int without_hex = 0; | |
13689 | ||
8b127cbc | 13690 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13691 | { |
b9c361e0 | 13692 | unsigned i; |
8b127cbc | 13693 | flagword (*lookup) (char *); |
ae17ab41 | 13694 | |
8b127cbc AM |
13695 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13696 | if (lookup != NULL) | |
ae17ab41 | 13697 | { |
8b127cbc | 13698 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13699 | |
13700 | if (hexval != 0) | |
13701 | { | |
13702 | if (tf->with == with_flags) | |
13703 | with_hex |= hexval; | |
13704 | else if (tf->with == without_flags) | |
13705 | without_hex |= hexval; | |
13706 | tf->valid = TRUE; | |
13707 | continue; | |
13708 | } | |
ae17ab41 | 13709 | } |
8b127cbc | 13710 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13711 | { |
8b127cbc | 13712 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13713 | { |
13714 | if (tf->with == with_flags) | |
13715 | with_hex |= elf_flags_to_names[i].flag_value; | |
13716 | else if (tf->with == without_flags) | |
13717 | without_hex |= elf_flags_to_names[i].flag_value; | |
13718 | tf->valid = TRUE; | |
13719 | break; | |
13720 | } | |
13721 | } | |
8b127cbc | 13722 | if (!tf->valid) |
b9c361e0 | 13723 | { |
68ffbac6 | 13724 | info->callbacks->einfo |
9793eb77 | 13725 | (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13726 | return FALSE; |
ae17ab41 CM |
13727 | } |
13728 | } | |
8b127cbc AM |
13729 | flaginfo->flags_initialized = TRUE; |
13730 | flaginfo->only_with_flags |= with_hex; | |
13731 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13732 | } |
ae17ab41 | 13733 | |
8b127cbc | 13734 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13735 | return FALSE; |
13736 | ||
8b127cbc | 13737 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13738 | return FALSE; |
13739 | ||
13740 | return TRUE; | |
ae17ab41 CM |
13741 | } |
13742 | ||
c152c796 AM |
13743 | struct alloc_got_off_arg { |
13744 | bfd_vma gotoff; | |
10455f89 | 13745 | struct bfd_link_info *info; |
c152c796 AM |
13746 | }; |
13747 | ||
13748 | /* We need a special top-level link routine to convert got reference counts | |
13749 | to real got offsets. */ | |
13750 | ||
13751 | static bfd_boolean | |
13752 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13753 | { | |
a50b1753 | 13754 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13755 | bfd *obfd = gofarg->info->output_bfd; |
13756 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13757 | |
c152c796 AM |
13758 | if (h->got.refcount > 0) |
13759 | { | |
13760 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13761 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13762 | } |
13763 | else | |
13764 | h->got.offset = (bfd_vma) -1; | |
13765 | ||
13766 | return TRUE; | |
13767 | } | |
13768 | ||
13769 | /* And an accompanying bit to work out final got entry offsets once | |
13770 | we're done. Should be called from final_link. */ | |
13771 | ||
13772 | bfd_boolean | |
13773 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13774 | struct bfd_link_info *info) | |
13775 | { | |
13776 | bfd *i; | |
13777 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13778 | bfd_vma gotoff; | |
c152c796 AM |
13779 | struct alloc_got_off_arg gofarg; |
13780 | ||
10455f89 HPN |
13781 | BFD_ASSERT (abfd == info->output_bfd); |
13782 | ||
c152c796 AM |
13783 | if (! is_elf_hash_table (info->hash)) |
13784 | return FALSE; | |
13785 | ||
13786 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13787 | put into the .got.plt section, if the backend uses it. */ | |
13788 | if (bed->want_got_plt) | |
13789 | gotoff = 0; | |
13790 | else | |
13791 | gotoff = bed->got_header_size; | |
13792 | ||
13793 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13794 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13795 | { |
13796 | bfd_signed_vma *local_got; | |
ef53be89 | 13797 | size_t j, locsymcount; |
c152c796 AM |
13798 | Elf_Internal_Shdr *symtab_hdr; |
13799 | ||
13800 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13801 | continue; | |
13802 | ||
13803 | local_got = elf_local_got_refcounts (i); | |
13804 | if (!local_got) | |
13805 | continue; | |
13806 | ||
13807 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13808 | if (elf_bad_symtab (i)) | |
13809 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13810 | else | |
13811 | locsymcount = symtab_hdr->sh_info; | |
13812 | ||
13813 | for (j = 0; j < locsymcount; ++j) | |
13814 | { | |
13815 | if (local_got[j] > 0) | |
13816 | { | |
13817 | local_got[j] = gotoff; | |
10455f89 | 13818 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13819 | } |
13820 | else | |
13821 | local_got[j] = (bfd_vma) -1; | |
13822 | } | |
13823 | } | |
13824 | ||
13825 | /* Then the global .got entries. .plt refcounts are handled by | |
13826 | adjust_dynamic_symbol */ | |
13827 | gofarg.gotoff = gotoff; | |
10455f89 | 13828 | gofarg.info = info; |
c152c796 AM |
13829 | elf_link_hash_traverse (elf_hash_table (info), |
13830 | elf_gc_allocate_got_offsets, | |
13831 | &gofarg); | |
13832 | return TRUE; | |
13833 | } | |
13834 | ||
13835 | /* Many folk need no more in the way of final link than this, once | |
13836 | got entry reference counting is enabled. */ | |
13837 | ||
13838 | bfd_boolean | |
13839 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
13840 | { | |
13841 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
13842 | return FALSE; | |
13843 | ||
13844 | /* Invoke the regular ELF backend linker to do all the work. */ | |
13845 | return bfd_elf_final_link (abfd, info); | |
13846 | } | |
13847 | ||
13848 | bfd_boolean | |
13849 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
13850 | { | |
a50b1753 | 13851 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
13852 | |
13853 | if (rcookie->bad_symtab) | |
13854 | rcookie->rel = rcookie->rels; | |
13855 | ||
13856 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
13857 | { | |
13858 | unsigned long r_symndx; | |
13859 | ||
13860 | if (! rcookie->bad_symtab) | |
13861 | if (rcookie->rel->r_offset > offset) | |
13862 | return FALSE; | |
13863 | if (rcookie->rel->r_offset != offset) | |
13864 | continue; | |
13865 | ||
13866 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 13867 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
13868 | return TRUE; |
13869 | ||
13870 | if (r_symndx >= rcookie->locsymcount | |
13871 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13872 | { | |
13873 | struct elf_link_hash_entry *h; | |
13874 | ||
13875 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
13876 | ||
13877 | while (h->root.type == bfd_link_hash_indirect | |
13878 | || h->root.type == bfd_link_hash_warning) | |
13879 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
13880 | ||
13881 | if ((h->root.type == bfd_link_hash_defined | |
13882 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
13883 | && (h->root.u.def.section->owner != rcookie->abfd |
13884 | || h->root.u.def.section->kept_section != NULL | |
13885 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 13886 | return TRUE; |
c152c796 AM |
13887 | } |
13888 | else | |
13889 | { | |
13890 | /* It's not a relocation against a global symbol, | |
13891 | but it could be a relocation against a local | |
13892 | symbol for a discarded section. */ | |
13893 | asection *isec; | |
13894 | Elf_Internal_Sym *isym; | |
13895 | ||
13896 | /* Need to: get the symbol; get the section. */ | |
13897 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 13898 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
13899 | if (isec != NULL |
13900 | && (isec->kept_section != NULL | |
13901 | || discarded_section (isec))) | |
cb33740c | 13902 | return TRUE; |
c152c796 AM |
13903 | } |
13904 | return FALSE; | |
13905 | } | |
13906 | return FALSE; | |
13907 | } | |
13908 | ||
13909 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
13910 | Returns -1 on error, 1 if any section's size was changed, 0 if |
13911 | nothing changed. This function assumes that the relocations are in | |
13912 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 13913 | |
75938853 | 13914 | int |
c152c796 AM |
13915 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
13916 | { | |
13917 | struct elf_reloc_cookie cookie; | |
18cd5bce | 13918 | asection *o; |
c152c796 | 13919 | bfd *abfd; |
75938853 | 13920 | int changed = 0; |
c152c796 AM |
13921 | |
13922 | if (info->traditional_format | |
13923 | || !is_elf_hash_table (info->hash)) | |
75938853 | 13924 | return 0; |
c152c796 | 13925 | |
18cd5bce AM |
13926 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
13927 | if (o != NULL) | |
c152c796 | 13928 | { |
18cd5bce | 13929 | asection *i; |
c152c796 | 13930 | |
18cd5bce | 13931 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 13932 | { |
18cd5bce AM |
13933 | if (i->size == 0 |
13934 | || i->reloc_count == 0 | |
13935 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
13936 | continue; | |
c152c796 | 13937 | |
18cd5bce AM |
13938 | abfd = i->owner; |
13939 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13940 | continue; | |
c152c796 | 13941 | |
18cd5bce | 13942 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 13943 | return -1; |
c152c796 | 13944 | |
18cd5bce AM |
13945 | if (_bfd_discard_section_stabs (abfd, i, |
13946 | elf_section_data (i)->sec_info, | |
5241d853 RS |
13947 | bfd_elf_reloc_symbol_deleted_p, |
13948 | &cookie)) | |
75938853 | 13949 | changed = 1; |
18cd5bce AM |
13950 | |
13951 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13952 | } |
18cd5bce AM |
13953 | } |
13954 | ||
2f0c68f2 CM |
13955 | o = NULL; |
13956 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
13957 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
13958 | if (o != NULL) |
13959 | { | |
13960 | asection *i; | |
d7153c4a | 13961 | int eh_changed = 0; |
79a94a2a | 13962 | unsigned int eh_alignment; |
c152c796 | 13963 | |
18cd5bce | 13964 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 13965 | { |
18cd5bce AM |
13966 | if (i->size == 0) |
13967 | continue; | |
13968 | ||
13969 | abfd = i->owner; | |
13970 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
13971 | continue; | |
13972 | ||
13973 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 13974 | return -1; |
18cd5bce AM |
13975 | |
13976 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
13977 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
13978 | bfd_elf_reloc_symbol_deleted_p, |
13979 | &cookie)) | |
d7153c4a AM |
13980 | { |
13981 | eh_changed = 1; | |
13982 | if (i->size != i->rawsize) | |
13983 | changed = 1; | |
13984 | } | |
18cd5bce AM |
13985 | |
13986 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 13987 | } |
9866ffe2 | 13988 | |
79a94a2a | 13989 | eh_alignment = 1 << o->alignment_power; |
9866ffe2 AM |
13990 | /* Skip over zero terminator, and prevent empty sections from |
13991 | adding alignment padding at the end. */ | |
13992 | for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) | |
13993 | if (i->size == 0) | |
13994 | i->flags |= SEC_EXCLUDE; | |
13995 | else if (i->size > 4) | |
13996 | break; | |
13997 | /* The last non-empty eh_frame section doesn't need padding. */ | |
13998 | if (i != NULL) | |
13999 | i = i->map_tail.s; | |
14000 | /* Any prior sections must pad the last FDE out to the output | |
14001 | section alignment. Otherwise we might have zero padding | |
14002 | between sections, which would be seen as a terminator. */ | |
14003 | for (; i != NULL; i = i->map_tail.s) | |
14004 | if (i->size == 4) | |
14005 | /* All but the last zero terminator should have been removed. */ | |
14006 | BFD_FAIL (); | |
14007 | else | |
14008 | { | |
14009 | bfd_size_type size | |
14010 | = (i->size + eh_alignment - 1) & -eh_alignment; | |
14011 | if (i->size != size) | |
af471f82 | 14012 | { |
9866ffe2 AM |
14013 | i->size = size; |
14014 | changed = 1; | |
14015 | eh_changed = 1; | |
af471f82 | 14016 | } |
9866ffe2 | 14017 | } |
d7153c4a AM |
14018 | if (eh_changed) |
14019 | elf_link_hash_traverse (elf_hash_table (info), | |
14020 | _bfd_elf_adjust_eh_frame_global_symbol, NULL); | |
18cd5bce | 14021 | } |
c152c796 | 14022 | |
18cd5bce AM |
14023 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
14024 | { | |
14025 | const struct elf_backend_data *bed; | |
57963c05 | 14026 | asection *s; |
c152c796 | 14027 | |
18cd5bce AM |
14028 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
14029 | continue; | |
57963c05 AM |
14030 | s = abfd->sections; |
14031 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14032 | continue; | |
18cd5bce AM |
14033 | |
14034 | bed = get_elf_backend_data (abfd); | |
14035 | ||
14036 | if (bed->elf_backend_discard_info != NULL) | |
14037 | { | |
14038 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 14039 | return -1; |
18cd5bce AM |
14040 | |
14041 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 14042 | changed = 1; |
18cd5bce AM |
14043 | |
14044 | fini_reloc_cookie (&cookie, abfd); | |
14045 | } | |
c152c796 AM |
14046 | } |
14047 | ||
2f0c68f2 CM |
14048 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
14049 | _bfd_elf_end_eh_frame_parsing (info); | |
14050 | ||
14051 | if (info->eh_frame_hdr_type | |
0e1862bb | 14052 | && !bfd_link_relocatable (info) |
c152c796 | 14053 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 14054 | changed = 1; |
c152c796 | 14055 | |
75938853 | 14056 | return changed; |
c152c796 | 14057 | } |
082b7297 | 14058 | |
43e1669b | 14059 | bfd_boolean |
0c511000 | 14060 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 14061 | asection *sec, |
c0f00686 | 14062 | struct bfd_link_info *info) |
082b7297 L |
14063 | { |
14064 | flagword flags; | |
c77ec726 | 14065 | const char *name, *key; |
082b7297 L |
14066 | struct bfd_section_already_linked *l; |
14067 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 14068 | |
c77ec726 AM |
14069 | if (sec->output_section == bfd_abs_section_ptr) |
14070 | return FALSE; | |
0c511000 | 14071 | |
c77ec726 | 14072 | flags = sec->flags; |
0c511000 | 14073 | |
c77ec726 AM |
14074 | /* Return if it isn't a linkonce section. A comdat group section |
14075 | also has SEC_LINK_ONCE set. */ | |
14076 | if ((flags & SEC_LINK_ONCE) == 0) | |
14077 | return FALSE; | |
0c511000 | 14078 | |
c77ec726 AM |
14079 | /* Don't put group member sections on our list of already linked |
14080 | sections. They are handled as a group via their group section. */ | |
14081 | if (elf_sec_group (sec) != NULL) | |
14082 | return FALSE; | |
0c511000 | 14083 | |
c77ec726 AM |
14084 | /* For a SHT_GROUP section, use the group signature as the key. */ |
14085 | name = sec->name; | |
14086 | if ((flags & SEC_GROUP) != 0 | |
14087 | && elf_next_in_group (sec) != NULL | |
14088 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
14089 | key = elf_group_name (elf_next_in_group (sec)); | |
14090 | else | |
14091 | { | |
14092 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 14093 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
14094 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
14095 | key++; | |
0c511000 | 14096 | else |
c77ec726 AM |
14097 | /* Must be a user linkonce section that doesn't follow gcc's |
14098 | naming convention. In this case we won't be matching | |
14099 | single member groups. */ | |
14100 | key = name; | |
0c511000 | 14101 | } |
6d2cd210 | 14102 | |
c77ec726 | 14103 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
14104 | |
14105 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14106 | { | |
c2370991 | 14107 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
14108 | sections with a signature of <key> (<key> is some string), |
14109 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
14110 | Match like sections. LTO plugin sections are an exception. | |
14111 | They are always named .gnu.linkonce.t.<key> and match either | |
14112 | type of section. */ | |
14113 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
14114 | && ((flags & SEC_GROUP) != 0 | |
14115 | || strcmp (name, l->sec->name) == 0)) | |
14116 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
14117 | { |
14118 | /* The section has already been linked. See if we should | |
6d2cd210 | 14119 | issue a warning. */ |
c77ec726 AM |
14120 | if (!_bfd_handle_already_linked (sec, l, info)) |
14121 | return FALSE; | |
082b7297 | 14122 | |
c77ec726 | 14123 | if (flags & SEC_GROUP) |
3d7f7666 | 14124 | { |
c77ec726 AM |
14125 | asection *first = elf_next_in_group (sec); |
14126 | asection *s = first; | |
3d7f7666 | 14127 | |
c77ec726 | 14128 | while (s != NULL) |
3d7f7666 | 14129 | { |
c77ec726 AM |
14130 | s->output_section = bfd_abs_section_ptr; |
14131 | /* Record which group discards it. */ | |
14132 | s->kept_section = l->sec; | |
14133 | s = elf_next_in_group (s); | |
14134 | /* These lists are circular. */ | |
14135 | if (s == first) | |
14136 | break; | |
3d7f7666 L |
14137 | } |
14138 | } | |
082b7297 | 14139 | |
43e1669b | 14140 | return TRUE; |
082b7297 L |
14141 | } |
14142 | } | |
14143 | ||
c77ec726 AM |
14144 | /* A single member comdat group section may be discarded by a |
14145 | linkonce section and vice versa. */ | |
14146 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 14147 | { |
c77ec726 | 14148 | asection *first = elf_next_in_group (sec); |
c2370991 | 14149 | |
c77ec726 AM |
14150 | if (first != NULL && elf_next_in_group (first) == first) |
14151 | /* Check this single member group against linkonce sections. */ | |
14152 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14153 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14154 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
14155 | { | |
14156 | first->output_section = bfd_abs_section_ptr; | |
14157 | first->kept_section = l->sec; | |
14158 | sec->output_section = bfd_abs_section_ptr; | |
14159 | break; | |
14160 | } | |
14161 | } | |
14162 | else | |
14163 | /* Check this linkonce section against single member groups. */ | |
14164 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14165 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 14166 | { |
c77ec726 | 14167 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 14168 | |
c77ec726 AM |
14169 | if (first != NULL |
14170 | && elf_next_in_group (first) == first | |
14171 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
14172 | { | |
14173 | sec->output_section = bfd_abs_section_ptr; | |
14174 | sec->kept_section = first; | |
14175 | break; | |
14176 | } | |
6d2cd210 | 14177 | } |
0c511000 | 14178 | |
c77ec726 AM |
14179 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
14180 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
14181 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
14182 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
14183 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
14184 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
14185 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
14186 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
14187 | The reverse order cannot happen as there is never a bfd with only the | |
14188 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
14189 | matter as here were are looking only for cross-bfd sections. */ | |
14190 | ||
14191 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
14192 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14193 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14194 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
14195 | { | |
14196 | if (abfd != l->sec->owner) | |
14197 | sec->output_section = bfd_abs_section_ptr; | |
14198 | break; | |
14199 | } | |
80c29487 | 14200 | |
082b7297 | 14201 | /* This is the first section with this name. Record it. */ |
c77ec726 | 14202 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 14203 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 14204 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 14205 | } |
81e1b023 | 14206 | |
a4d8e49b L |
14207 | bfd_boolean |
14208 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
14209 | { | |
14210 | return sym->st_shndx == SHN_COMMON; | |
14211 | } | |
14212 | ||
14213 | unsigned int | |
14214 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
14215 | { | |
14216 | return SHN_COMMON; | |
14217 | } | |
14218 | ||
14219 | asection * | |
14220 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
14221 | { | |
14222 | return bfd_com_section_ptr; | |
14223 | } | |
10455f89 HPN |
14224 | |
14225 | bfd_vma | |
14226 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
14227 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
14228 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
14229 | bfd *ibfd ATTRIBUTE_UNUSED, | |
14230 | unsigned long symndx ATTRIBUTE_UNUSED) | |
14231 | { | |
14232 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14233 | return bed->s->arch_size / 8; | |
14234 | } | |
83bac4b0 NC |
14235 | |
14236 | /* Routines to support the creation of dynamic relocs. */ | |
14237 | ||
83bac4b0 NC |
14238 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
14239 | ||
14240 | static const char * | |
14241 | get_dynamic_reloc_section_name (bfd * abfd, | |
14242 | asection * sec, | |
14243 | bfd_boolean is_rela) | |
14244 | { | |
ddcf1fcf BS |
14245 | char *name; |
14246 | const char *old_name = bfd_get_section_name (NULL, sec); | |
14247 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 14248 | |
ddcf1fcf | 14249 | if (old_name == NULL) |
83bac4b0 NC |
14250 | return NULL; |
14251 | ||
ddcf1fcf | 14252 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 14253 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
14254 | |
14255 | return name; | |
14256 | } | |
14257 | ||
14258 | /* Returns the dynamic reloc section associated with SEC. | |
14259 | If necessary compute the name of the dynamic reloc section based | |
14260 | on SEC's name (looked up in ABFD's string table) and the setting | |
14261 | of IS_RELA. */ | |
14262 | ||
14263 | asection * | |
14264 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
14265 | asection * sec, | |
14266 | bfd_boolean is_rela) | |
14267 | { | |
14268 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14269 | ||
14270 | if (reloc_sec == NULL) | |
14271 | { | |
14272 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14273 | ||
14274 | if (name != NULL) | |
14275 | { | |
3d4d4302 | 14276 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
14277 | |
14278 | if (reloc_sec != NULL) | |
14279 | elf_section_data (sec)->sreloc = reloc_sec; | |
14280 | } | |
14281 | } | |
14282 | ||
14283 | return reloc_sec; | |
14284 | } | |
14285 | ||
14286 | /* Returns the dynamic reloc section associated with SEC. If the | |
14287 | section does not exist it is created and attached to the DYNOBJ | |
14288 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
14289 | structure. | |
f8076f98 | 14290 | |
83bac4b0 NC |
14291 | ALIGNMENT is the alignment for the newly created section and |
14292 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
14293 | or .rel.<SEC's name>. The section name is looked up in the | |
14294 | string table associated with ABFD. */ | |
14295 | ||
14296 | asection * | |
ca4be51c AM |
14297 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
14298 | bfd *dynobj, | |
14299 | unsigned int alignment, | |
14300 | bfd *abfd, | |
14301 | bfd_boolean is_rela) | |
83bac4b0 NC |
14302 | { |
14303 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14304 | ||
14305 | if (reloc_sec == NULL) | |
14306 | { | |
14307 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14308 | ||
14309 | if (name == NULL) | |
14310 | return NULL; | |
14311 | ||
3d4d4302 | 14312 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
14313 | |
14314 | if (reloc_sec == NULL) | |
14315 | { | |
3d4d4302 AM |
14316 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
14317 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
14318 | if ((sec->flags & SEC_ALLOC) != 0) |
14319 | flags |= SEC_ALLOC | SEC_LOAD; | |
14320 | ||
3d4d4302 | 14321 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
14322 | if (reloc_sec != NULL) |
14323 | { | |
8877b5e5 AM |
14324 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
14325 | name. Override as it may be wrong, eg. for a user | |
14326 | section named "auto" we'll get ".relauto" which is | |
14327 | seen to be a .rela section. */ | |
14328 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
14329 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
14330 | reloc_sec = NULL; | |
14331 | } | |
14332 | } | |
14333 | ||
14334 | elf_section_data (sec)->sreloc = reloc_sec; | |
14335 | } | |
14336 | ||
14337 | return reloc_sec; | |
14338 | } | |
1338dd10 | 14339 | |
bffebb6b AM |
14340 | /* Copy the ELF symbol type and other attributes for a linker script |
14341 | assignment from HSRC to HDEST. Generally this should be treated as | |
14342 | if we found a strong non-dynamic definition for HDEST (except that | |
14343 | ld ignores multiple definition errors). */ | |
1338dd10 | 14344 | void |
bffebb6b AM |
14345 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
14346 | struct bfd_link_hash_entry *hdest, | |
14347 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 14348 | { |
bffebb6b AM |
14349 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
14350 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14351 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14352 | |
14353 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14354 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14355 | |
14356 | isym.st_other = ehsrc->other; | |
b8417128 | 14357 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14358 | } |
351f65ca L |
14359 | |
14360 | /* Append a RELA relocation REL to section S in BFD. */ | |
14361 | ||
14362 | void | |
14363 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14364 | { | |
14365 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14366 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14367 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14368 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14369 | } | |
14370 | ||
14371 | /* Append a REL relocation REL to section S in BFD. */ | |
14372 | ||
14373 | void | |
14374 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14375 | { | |
14376 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14377 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14378 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14379 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14380 | } |
7dba9362 AM |
14381 | |
14382 | /* Define __start, __stop, .startof. or .sizeof. symbol. */ | |
14383 | ||
14384 | struct bfd_link_hash_entry * | |
14385 | bfd_elf_define_start_stop (struct bfd_link_info *info, | |
14386 | const char *symbol, asection *sec) | |
14387 | { | |
487b6440 | 14388 | struct elf_link_hash_entry *h; |
7dba9362 | 14389 | |
487b6440 AM |
14390 | h = elf_link_hash_lookup (elf_hash_table (info), symbol, |
14391 | FALSE, FALSE, TRUE); | |
14392 | if (h != NULL | |
14393 | && (h->root.type == bfd_link_hash_undefined | |
14394 | || h->root.type == bfd_link_hash_undefweak | |
bf3077a6 | 14395 | || ((h->ref_regular || h->def_dynamic) && !h->def_regular))) |
7dba9362 | 14396 | { |
bf3077a6 | 14397 | bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic; |
487b6440 AM |
14398 | h->root.type = bfd_link_hash_defined; |
14399 | h->root.u.def.section = sec; | |
14400 | h->root.u.def.value = 0; | |
14401 | h->def_regular = 1; | |
14402 | h->def_dynamic = 0; | |
14403 | h->start_stop = 1; | |
14404 | h->u2.start_stop_section = sec; | |
14405 | if (symbol[0] == '.') | |
14406 | { | |
14407 | /* .startof. and .sizeof. symbols are local. */ | |
559192d8 AM |
14408 | const struct elf_backend_data *bed; |
14409 | bed = get_elf_backend_data (info->output_bfd); | |
14410 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
487b6440 | 14411 | } |
36b8fda5 AM |
14412 | else |
14413 | { | |
14414 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
14415 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; | |
bf3077a6 | 14416 | if (was_dynamic) |
36b8fda5 AM |
14417 | bfd_elf_link_record_dynamic_symbol (info, h); |
14418 | } | |
487b6440 | 14419 | return &h->root; |
7dba9362 | 14420 | } |
487b6440 | 14421 | return NULL; |
7dba9362 | 14422 | } |