Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
82704155 | 2 | Copyright (C) 1995-2019 Free Software Foundation, Inc. |
252b5132 | 3 | |
8fdd7217 | 4 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 5 | |
8fdd7217 NC |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 8 | the Free Software Foundation; either version 3 of the License, or |
8fdd7217 | 9 | (at your option) any later version. |
252b5132 | 10 | |
8fdd7217 NC |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
252b5132 | 15 | |
8fdd7217 NC |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
252b5132 | 20 | |
252b5132 | 21 | #include "sysdep.h" |
3db64b00 | 22 | #include "bfd.h" |
252b5132 RH |
23 | #include "bfdlink.h" |
24 | #include "libbfd.h" | |
25 | #define ARCH_SIZE 0 | |
26 | #include "elf-bfd.h" | |
4ad4eba5 | 27 | #include "safe-ctype.h" |
ccf2f652 | 28 | #include "libiberty.h" |
66eb6687 | 29 | #include "objalloc.h" |
08ce1d72 | 30 | #if BFD_SUPPORTS_PLUGINS |
7d0b9ebc | 31 | #include "plugin-api.h" |
7dc3990e L |
32 | #include "plugin.h" |
33 | #endif | |
252b5132 | 34 | |
28caa186 AM |
35 | /* This struct is used to pass information to routines called via |
36 | elf_link_hash_traverse which must return failure. */ | |
37 | ||
38 | struct elf_info_failed | |
39 | { | |
40 | struct bfd_link_info *info; | |
28caa186 AM |
41 | bfd_boolean failed; |
42 | }; | |
43 | ||
44 | /* This structure is used to pass information to | |
45 | _bfd_elf_link_find_version_dependencies. */ | |
46 | ||
47 | struct elf_find_verdep_info | |
48 | { | |
49 | /* General link information. */ | |
50 | struct bfd_link_info *info; | |
51 | /* The number of dependencies. */ | |
52 | unsigned int vers; | |
53 | /* Whether we had a failure. */ | |
54 | bfd_boolean failed; | |
55 | }; | |
56 | ||
57 | static bfd_boolean _bfd_elf_fix_symbol_flags | |
58 | (struct elf_link_hash_entry *, struct elf_info_failed *); | |
59 | ||
2f0c68f2 CM |
60 | asection * |
61 | _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, | |
62 | unsigned long r_symndx, | |
63 | bfd_boolean discard) | |
64 | { | |
65 | if (r_symndx >= cookie->locsymcount | |
66 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
67 | { | |
68 | struct elf_link_hash_entry *h; | |
69 | ||
70 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
71 | ||
72 | while (h->root.type == bfd_link_hash_indirect | |
73 | || h->root.type == bfd_link_hash_warning) | |
74 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
75 | ||
76 | if ((h->root.type == bfd_link_hash_defined | |
77 | || h->root.type == bfd_link_hash_defweak) | |
78 | && discarded_section (h->root.u.def.section)) | |
07d6d2b8 | 79 | return h->root.u.def.section; |
2f0c68f2 CM |
80 | else |
81 | return NULL; | |
82 | } | |
83 | else | |
84 | { | |
85 | /* It's not a relocation against a global symbol, | |
86 | but it could be a relocation against a local | |
87 | symbol for a discarded section. */ | |
88 | asection *isec; | |
89 | Elf_Internal_Sym *isym; | |
90 | ||
91 | /* Need to: get the symbol; get the section. */ | |
92 | isym = &cookie->locsyms[r_symndx]; | |
93 | isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); | |
94 | if (isec != NULL | |
95 | && discard ? discarded_section (isec) : 1) | |
96 | return isec; | |
97 | } | |
98 | return NULL; | |
99 | } | |
100 | ||
d98685ac AM |
101 | /* Define a symbol in a dynamic linkage section. */ |
102 | ||
103 | struct elf_link_hash_entry * | |
104 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
105 | struct bfd_link_info *info, | |
106 | asection *sec, | |
107 | const char *name) | |
108 | { | |
109 | struct elf_link_hash_entry *h; | |
110 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 111 | const struct elf_backend_data *bed; |
d98685ac AM |
112 | |
113 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
114 | if (h != NULL) | |
115 | { | |
116 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
117 | This is a symptom of a larger problem: Absolute symbols | |
118 | defined in shared libraries can't be overridden, because we | |
119 | lose the link to the bfd which is via the symbol section. */ | |
120 | h->root.type = bfd_link_hash_new; | |
ad32986f | 121 | bh = &h->root; |
d98685ac | 122 | } |
ad32986f NC |
123 | else |
124 | bh = NULL; | |
d98685ac | 125 | |
cf18fda4 | 126 | bed = get_elf_backend_data (abfd); |
d98685ac | 127 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, |
cf18fda4 | 128 | sec, 0, NULL, FALSE, bed->collect, |
d98685ac AM |
129 | &bh)) |
130 | return NULL; | |
131 | h = (struct elf_link_hash_entry *) bh; | |
ad32986f | 132 | BFD_ASSERT (h != NULL); |
d98685ac | 133 | h->def_regular = 1; |
e28df02b | 134 | h->non_elf = 0; |
12b2843a | 135 | h->root.linker_def = 1; |
d98685ac | 136 | h->type = STT_OBJECT; |
00b7642b AM |
137 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
138 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
d98685ac | 139 | |
ccabcbe5 | 140 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
d98685ac AM |
141 | return h; |
142 | } | |
143 | ||
b34976b6 | 144 | bfd_boolean |
268b6b39 | 145 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
146 | { |
147 | flagword flags; | |
aad5d350 | 148 | asection *s; |
252b5132 | 149 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 150 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 151 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
152 | |
153 | /* This function may be called more than once. */ | |
ce558b89 | 154 | if (htab->sgot != NULL) |
b34976b6 | 155 | return TRUE; |
252b5132 | 156 | |
e5a52504 | 157 | flags = bed->dynamic_sec_flags; |
252b5132 | 158 | |
14b2f831 AM |
159 | s = bfd_make_section_anyway_with_flags (abfd, |
160 | (bed->rela_plts_and_copies_p | |
161 | ? ".rela.got" : ".rel.got"), | |
162 | (bed->dynamic_sec_flags | |
163 | | SEC_READONLY)); | |
6de2ae4a L |
164 | if (s == NULL |
165 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
166 | return FALSE; | |
167 | htab->srelgot = s; | |
252b5132 | 168 | |
14b2f831 | 169 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d L |
170 | if (s == NULL |
171 | || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
172 | return FALSE; | |
173 | htab->sgot = s; | |
174 | ||
252b5132 RH |
175 | if (bed->want_got_plt) |
176 | { | |
14b2f831 | 177 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 178 | if (s == NULL |
6de2ae4a L |
179 | || !bfd_set_section_alignment (abfd, s, |
180 | bed->s->log_file_align)) | |
b34976b6 | 181 | return FALSE; |
6de2ae4a | 182 | htab->sgotplt = s; |
252b5132 RH |
183 | } |
184 | ||
64e77c6d L |
185 | /* The first bit of the global offset table is the header. */ |
186 | s->size += bed->got_header_size; | |
187 | ||
2517a57f AM |
188 | if (bed->want_got_sym) |
189 | { | |
190 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
191 | (or .got.plt) section. We don't do this in the linker script | |
192 | because we don't want to define the symbol if we are not creating | |
193 | a global offset table. */ | |
6de2ae4a L |
194 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
195 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 196 | elf_hash_table (info)->hgot = h; |
d98685ac AM |
197 | if (h == NULL) |
198 | return FALSE; | |
2517a57f | 199 | } |
252b5132 | 200 | |
b34976b6 | 201 | return TRUE; |
252b5132 RH |
202 | } |
203 | \f | |
7e9f0867 AM |
204 | /* Create a strtab to hold the dynamic symbol names. */ |
205 | static bfd_boolean | |
206 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) | |
207 | { | |
208 | struct elf_link_hash_table *hash_table; | |
209 | ||
210 | hash_table = elf_hash_table (info); | |
211 | if (hash_table->dynobj == NULL) | |
6cd255ca L |
212 | { |
213 | /* We may not set dynobj, an input file holding linker created | |
214 | dynamic sections to abfd, which may be a dynamic object with | |
215 | its own dynamic sections. We need to find a normal input file | |
216 | to hold linker created sections if possible. */ | |
217 | if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) | |
218 | { | |
219 | bfd *ibfd; | |
57963c05 | 220 | asection *s; |
6cd255ca | 221 | for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) |
6645479e | 222 | if ((ibfd->flags |
57963c05 AM |
223 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0 |
224 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
4de5434b | 225 | && elf_object_id (ibfd) == elf_hash_table_id (hash_table) |
57963c05 AM |
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 | ||
48e30f52 L |
689 | /* If this symbol is currently defined by a dynamic object, but not |
690 | by a regular object, then clear out any version information because | |
691 | the symbol will not be associated with the dynamic object any | |
692 | more. */ | |
693 | if (h->def_dynamic && !h->def_regular) | |
b531344c MR |
694 | h->verinfo.verdef = NULL; |
695 | ||
696 | /* Make sure this symbol is not garbage collected. */ | |
697 | h->mark = 1; | |
45d6a902 | 698 | |
f5385ebf | 699 | h->def_regular = 1; |
45d6a902 | 700 | |
eb8476a6 | 701 | if (hidden) |
fe21a8fc | 702 | { |
91d6fa6a | 703 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
704 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
705 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
fe21a8fc L |
706 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); |
707 | } | |
708 | ||
6fa3860b PB |
709 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
710 | and executables. */ | |
0e1862bb | 711 | if (!bfd_link_relocatable (info) |
6fa3860b PB |
712 | && h->dynindx != -1 |
713 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
714 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
715 | h->forced_local = 1; | |
716 | ||
f5385ebf AM |
717 | if ((h->def_dynamic |
718 | || h->ref_dynamic | |
6b3b0ab8 L |
719 | || bfd_link_dll (info) |
720 | || elf_hash_table (info)->is_relocatable_executable) | |
34a87bb0 | 721 | && !h->forced_local |
45d6a902 AM |
722 | && h->dynindx == -1) |
723 | { | |
c152c796 | 724 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
725 | return FALSE; |
726 | ||
727 | /* If this is a weak defined symbol, and we know a corresponding | |
728 | real symbol from the same dynamic object, make sure the real | |
729 | symbol is also made into a dynamic symbol. */ | |
60d67dc8 | 730 | if (h->is_weakalias) |
45d6a902 | 731 | { |
60d67dc8 AM |
732 | struct elf_link_hash_entry *def = weakdef (h); |
733 | ||
734 | if (def->dynindx == -1 | |
735 | && !bfd_elf_link_record_dynamic_symbol (info, def)) | |
45d6a902 AM |
736 | return FALSE; |
737 | } | |
738 | } | |
739 | ||
740 | return TRUE; | |
741 | } | |
42751cf3 | 742 | |
8c58d23b AM |
743 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
744 | success, and 2 on a failure caused by attempting to record a symbol | |
745 | in a discarded section, eg. a discarded link-once section symbol. */ | |
746 | ||
747 | int | |
c152c796 AM |
748 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
749 | bfd *input_bfd, | |
750 | long input_indx) | |
8c58d23b AM |
751 | { |
752 | bfd_size_type amt; | |
753 | struct elf_link_local_dynamic_entry *entry; | |
754 | struct elf_link_hash_table *eht; | |
755 | struct elf_strtab_hash *dynstr; | |
ef53be89 | 756 | size_t dynstr_index; |
8c58d23b AM |
757 | char *name; |
758 | Elf_External_Sym_Shndx eshndx; | |
759 | char esym[sizeof (Elf64_External_Sym)]; | |
760 | ||
0eddce27 | 761 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
762 | return 0; |
763 | ||
764 | /* See if the entry exists already. */ | |
765 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
766 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
767 | return 1; | |
768 | ||
769 | amt = sizeof (*entry); | |
a50b1753 | 770 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
771 | if (entry == NULL) |
772 | return 0; | |
773 | ||
774 | /* Go find the symbol, so that we can find it's name. */ | |
775 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 776 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
777 | { |
778 | bfd_release (input_bfd, entry); | |
779 | return 0; | |
780 | } | |
781 | ||
782 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 783 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
784 | { |
785 | asection *s; | |
786 | ||
787 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
788 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
789 | { | |
790 | /* We can still bfd_release here as nothing has done another | |
791 | bfd_alloc. We can't do this later in this function. */ | |
792 | bfd_release (input_bfd, entry); | |
793 | return 2; | |
794 | } | |
795 | } | |
796 | ||
797 | name = (bfd_elf_string_from_elf_section | |
798 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
799 | entry->isym.st_name)); | |
800 | ||
801 | dynstr = elf_hash_table (info)->dynstr; | |
802 | if (dynstr == NULL) | |
803 | { | |
804 | /* Create a strtab to hold the dynamic symbol names. */ | |
805 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
806 | if (dynstr == NULL) | |
807 | return 0; | |
808 | } | |
809 | ||
b34976b6 | 810 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
ef53be89 | 811 | if (dynstr_index == (size_t) -1) |
8c58d23b AM |
812 | return 0; |
813 | entry->isym.st_name = dynstr_index; | |
814 | ||
815 | eht = elf_hash_table (info); | |
816 | ||
817 | entry->next = eht->dynlocal; | |
818 | eht->dynlocal = entry; | |
819 | entry->input_bfd = input_bfd; | |
820 | entry->input_indx = input_indx; | |
821 | eht->dynsymcount++; | |
822 | ||
823 | /* Whatever binding the symbol had before, it's now local. */ | |
824 | entry->isym.st_info | |
825 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
826 | ||
827 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
828 | ||
829 | return 1; | |
830 | } | |
831 | ||
30b30c21 | 832 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 833 | |
30b30c21 | 834 | long |
268b6b39 AM |
835 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
836 | bfd *input_bfd, | |
837 | long input_indx) | |
30b30c21 RH |
838 | { |
839 | struct elf_link_local_dynamic_entry *e; | |
840 | ||
841 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
842 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
843 | return e->dynindx; | |
844 | return -1; | |
845 | } | |
846 | ||
847 | /* This function is used to renumber the dynamic symbols, if some of | |
848 | them are removed because they are marked as local. This is called | |
849 | via elf_link_hash_traverse. */ | |
850 | ||
b34976b6 | 851 | static bfd_boolean |
268b6b39 AM |
852 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
853 | void *data) | |
42751cf3 | 854 | { |
a50b1753 | 855 | size_t *count = (size_t *) data; |
30b30c21 | 856 | |
6fa3860b PB |
857 | if (h->forced_local) |
858 | return TRUE; | |
859 | ||
860 | if (h->dynindx != -1) | |
861 | h->dynindx = ++(*count); | |
862 | ||
863 | return TRUE; | |
864 | } | |
865 | ||
866 | ||
867 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
868 | STB_LOCAL binding. */ | |
869 | ||
870 | static bfd_boolean | |
871 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, | |
872 | void *data) | |
873 | { | |
a50b1753 | 874 | size_t *count = (size_t *) data; |
6fa3860b | 875 | |
6fa3860b PB |
876 | if (!h->forced_local) |
877 | return TRUE; | |
878 | ||
42751cf3 | 879 | if (h->dynindx != -1) |
30b30c21 RH |
880 | h->dynindx = ++(*count); |
881 | ||
b34976b6 | 882 | return TRUE; |
42751cf3 | 883 | } |
30b30c21 | 884 | |
aee6f5b4 AO |
885 | /* Return true if the dynamic symbol for a given section should be |
886 | omitted when creating a shared library. */ | |
887 | bfd_boolean | |
d00dd7dc AM |
888 | _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED, |
889 | struct bfd_link_info *info, | |
890 | asection *p) | |
aee6f5b4 | 891 | { |
74541ad4 | 892 | struct elf_link_hash_table *htab; |
ca55926c | 893 | asection *ip; |
74541ad4 | 894 | |
aee6f5b4 AO |
895 | switch (elf_section_data (p)->this_hdr.sh_type) |
896 | { | |
897 | case SHT_PROGBITS: | |
898 | case SHT_NOBITS: | |
899 | /* If sh_type is yet undecided, assume it could be | |
900 | SHT_PROGBITS/SHT_NOBITS. */ | |
901 | case SHT_NULL: | |
74541ad4 AM |
902 | htab = elf_hash_table (info); |
903 | if (p == htab->tls_sec) | |
904 | return FALSE; | |
905 | ||
906 | if (htab->text_index_section != NULL) | |
907 | return p != htab->text_index_section && p != htab->data_index_section; | |
908 | ||
ca55926c | 909 | return (htab->dynobj != NULL |
3d4d4302 | 910 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 911 | && ip->output_section == p); |
aee6f5b4 AO |
912 | |
913 | /* There shouldn't be section relative relocations | |
914 | against any other section. */ | |
915 | default: | |
916 | return TRUE; | |
917 | } | |
918 | } | |
919 | ||
d00dd7dc AM |
920 | bfd_boolean |
921 | _bfd_elf_omit_section_dynsym_all | |
922 | (bfd *output_bfd ATTRIBUTE_UNUSED, | |
923 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
924 | asection *p ATTRIBUTE_UNUSED) | |
925 | { | |
926 | return TRUE; | |
927 | } | |
928 | ||
062e2358 | 929 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
930 | symbol for each output section, which come first. Next come symbols |
931 | which have been forced to local binding. Then all of the back-end | |
932 | allocated local dynamic syms, followed by the rest of the global | |
63f452a8 AM |
933 | symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. |
934 | (This prevents the early call before elf_backend_init_index_section | |
935 | and strip_excluded_output_sections setting dynindx for sections | |
936 | that are stripped.) */ | |
30b30c21 | 937 | |
554220db AM |
938 | static unsigned long |
939 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
940 | struct bfd_link_info *info, | |
941 | unsigned long *section_sym_count) | |
30b30c21 RH |
942 | { |
943 | unsigned long dynsymcount = 0; | |
63f452a8 | 944 | bfd_boolean do_sec = section_sym_count != NULL; |
30b30c21 | 945 | |
0e1862bb L |
946 | if (bfd_link_pic (info) |
947 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 948 | { |
aee6f5b4 | 949 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
950 | asection *p; |
951 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 952 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 | 953 | && (p->flags & SEC_ALLOC) != 0 |
7f923b7f | 954 | && elf_hash_table (info)->dynamic_relocs |
aee6f5b4 | 955 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) |
63f452a8 AM |
956 | { |
957 | ++dynsymcount; | |
958 | if (do_sec) | |
959 | elf_section_data (p)->dynindx = dynsymcount; | |
960 | } | |
961 | else if (do_sec) | |
74541ad4 | 962 | elf_section_data (p)->dynindx = 0; |
30b30c21 | 963 | } |
63f452a8 AM |
964 | if (do_sec) |
965 | *section_sym_count = dynsymcount; | |
30b30c21 | 966 | |
6fa3860b PB |
967 | elf_link_hash_traverse (elf_hash_table (info), |
968 | elf_link_renumber_local_hash_table_dynsyms, | |
969 | &dynsymcount); | |
970 | ||
30b30c21 RH |
971 | if (elf_hash_table (info)->dynlocal) |
972 | { | |
973 | struct elf_link_local_dynamic_entry *p; | |
974 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
975 | p->dynindx = ++dynsymcount; | |
976 | } | |
90ac2420 | 977 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
978 | |
979 | elf_link_hash_traverse (elf_hash_table (info), | |
980 | elf_link_renumber_hash_table_dynsyms, | |
981 | &dynsymcount); | |
982 | ||
d5486c43 L |
983 | /* There is an unused NULL entry at the head of the table which we |
984 | must account for in our count even if the table is empty since it | |
985 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
986 | .dynamic section. */ | |
987 | dynsymcount++; | |
30b30c21 | 988 | |
ccabcbe5 AM |
989 | elf_hash_table (info)->dynsymcount = dynsymcount; |
990 | return dynsymcount; | |
30b30c21 | 991 | } |
252b5132 | 992 | |
54ac0771 L |
993 | /* Merge st_other field. */ |
994 | ||
995 | static void | |
996 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 997 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 998 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
999 | { |
1000 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1001 | ||
1002 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 1003 | code might be needed here. */ |
54ac0771 L |
1004 | if (bed->elf_backend_merge_symbol_attribute) |
1005 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
1006 | dynamic); | |
1007 | ||
cd3416da | 1008 | if (!dynamic) |
54ac0771 | 1009 | { |
cd3416da AM |
1010 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
1011 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 1012 | |
cd3416da AM |
1013 | /* Keep the most constraining visibility. Leave the remainder |
1014 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
1015 | if (symvis - 1 < hvis - 1) | |
1016 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 1017 | } |
b8417128 AM |
1018 | else if (definition |
1019 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
1020 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 1021 | h->protected_def = 1; |
54ac0771 L |
1022 | } |
1023 | ||
4f3fedcf AM |
1024 | /* This function is called when we want to merge a new symbol with an |
1025 | existing symbol. It handles the various cases which arise when we | |
1026 | find a definition in a dynamic object, or when there is already a | |
1027 | definition in a dynamic object. The new symbol is described by | |
1028 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
1029 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
1030 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
1031 | of an old common symbol. We set OVERRIDE if the old symbol is | |
1032 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
1033 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
1034 | to change. By OK to change, we mean that we shouldn't warn if the | |
1035 | type or size does change. */ | |
45d6a902 | 1036 | |
8a56bd02 | 1037 | static bfd_boolean |
268b6b39 AM |
1038 | _bfd_elf_merge_symbol (bfd *abfd, |
1039 | struct bfd_link_info *info, | |
1040 | const char *name, | |
1041 | Elf_Internal_Sym *sym, | |
1042 | asection **psec, | |
1043 | bfd_vma *pvalue, | |
4f3fedcf AM |
1044 | struct elf_link_hash_entry **sym_hash, |
1045 | bfd **poldbfd, | |
37a9e49a | 1046 | bfd_boolean *pold_weak, |
af44c138 | 1047 | unsigned int *pold_alignment, |
268b6b39 AM |
1048 | bfd_boolean *skip, |
1049 | bfd_boolean *override, | |
1050 | bfd_boolean *type_change_ok, | |
6e33951e L |
1051 | bfd_boolean *size_change_ok, |
1052 | bfd_boolean *matched) | |
252b5132 | 1053 | { |
7479dfd4 | 1054 | asection *sec, *oldsec; |
45d6a902 | 1055 | struct elf_link_hash_entry *h; |
90c984fc | 1056 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
1057 | struct elf_link_hash_entry *flip; |
1058 | int bind; | |
1059 | bfd *oldbfd; | |
1060 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 1061 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 1062 | const struct elf_backend_data *bed; |
6e33951e | 1063 | char *new_version; |
93f4de39 | 1064 | bfd_boolean default_sym = *matched; |
45d6a902 AM |
1065 | |
1066 | *skip = FALSE; | |
1067 | *override = FALSE; | |
1068 | ||
1069 | sec = *psec; | |
1070 | bind = ELF_ST_BIND (sym->st_info); | |
1071 | ||
1072 | if (! bfd_is_und_section (sec)) | |
1073 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1074 | else | |
1075 | h = ((struct elf_link_hash_entry *) | |
1076 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1077 | if (h == NULL) | |
1078 | return FALSE; | |
1079 | *sym_hash = h; | |
252b5132 | 1080 | |
88ba32a0 L |
1081 | bed = get_elf_backend_data (abfd); |
1082 | ||
6e33951e | 1083 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1084 | if (h->versioned != unversioned) |
6e33951e | 1085 | { |
422f1182 L |
1086 | /* Symbol version is unknown or versioned. */ |
1087 | new_version = strrchr (name, ELF_VER_CHR); | |
1088 | if (new_version) | |
1089 | { | |
1090 | if (h->versioned == unknown) | |
1091 | { | |
1092 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1093 | h->versioned = versioned_hidden; | |
1094 | else | |
1095 | h->versioned = versioned; | |
1096 | } | |
1097 | new_version += 1; | |
1098 | if (new_version[0] == '\0') | |
1099 | new_version = NULL; | |
1100 | } | |
1101 | else | |
1102 | h->versioned = unversioned; | |
6e33951e | 1103 | } |
422f1182 L |
1104 | else |
1105 | new_version = NULL; | |
6e33951e | 1106 | |
90c984fc L |
1107 | /* For merging, we only care about real symbols. But we need to make |
1108 | sure that indirect symbol dynamic flags are updated. */ | |
1109 | hi = h; | |
45d6a902 AM |
1110 | while (h->root.type == bfd_link_hash_indirect |
1111 | || h->root.type == bfd_link_hash_warning) | |
1112 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1113 | ||
6e33951e L |
1114 | if (!*matched) |
1115 | { | |
1116 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1117 | *matched = TRUE; | |
1118 | else | |
1119 | { | |
ae7683d2 | 1120 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1121 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1122 | true if the new symbol is only visible to the symbol with |
6e33951e | 1123 | the same symbol version. */ |
422f1182 L |
1124 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1125 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1126 | if (!old_hidden && !new_hidden) |
1127 | /* The new symbol matches the existing symbol if both | |
1128 | aren't hidden. */ | |
1129 | *matched = TRUE; | |
1130 | else | |
1131 | { | |
1132 | /* OLD_VERSION is the symbol version of the existing | |
1133 | symbol. */ | |
422f1182 L |
1134 | char *old_version; |
1135 | ||
1136 | if (h->versioned >= versioned) | |
1137 | old_version = strrchr (h->root.root.string, | |
1138 | ELF_VER_CHR) + 1; | |
1139 | else | |
1140 | old_version = NULL; | |
6e33951e L |
1141 | |
1142 | /* The new symbol matches the existing symbol if they | |
1143 | have the same symbol version. */ | |
1144 | *matched = (old_version == new_version | |
1145 | || (old_version != NULL | |
1146 | && new_version != NULL | |
1147 | && strcmp (old_version, new_version) == 0)); | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | ||
934bce08 AM |
1152 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1153 | existing symbol. */ | |
1154 | ||
1155 | oldbfd = NULL; | |
1156 | oldsec = NULL; | |
1157 | switch (h->root.type) | |
1158 | { | |
1159 | default: | |
1160 | break; | |
1161 | ||
1162 | case bfd_link_hash_undefined: | |
1163 | case bfd_link_hash_undefweak: | |
1164 | oldbfd = h->root.u.undef.abfd; | |
1165 | break; | |
1166 | ||
1167 | case bfd_link_hash_defined: | |
1168 | case bfd_link_hash_defweak: | |
1169 | oldbfd = h->root.u.def.section->owner; | |
1170 | oldsec = h->root.u.def.section; | |
1171 | break; | |
1172 | ||
1173 | case bfd_link_hash_common: | |
1174 | oldbfd = h->root.u.c.p->section->owner; | |
1175 | oldsec = h->root.u.c.p->section; | |
1176 | if (pold_alignment) | |
1177 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1178 | break; | |
1179 | } | |
1180 | if (poldbfd && *poldbfd == NULL) | |
1181 | *poldbfd = oldbfd; | |
1182 | ||
1183 | /* Differentiate strong and weak symbols. */ | |
1184 | newweak = bind == STB_WEAK; | |
1185 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1186 | || h->root.type == bfd_link_hash_undefweak); | |
1187 | if (pold_weak) | |
1188 | *pold_weak = oldweak; | |
1189 | ||
40b36307 | 1190 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1191 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1192 | symbols. */ |
1193 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1194 | ||
ee659f1f AM |
1195 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1196 | respectively, is from a dynamic object. */ | |
1197 | ||
1198 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1199 | ||
1200 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1201 | syms and defined syms in dynamic libraries respectively. | |
1202 | ref_dynamic on the other hand can be set for a symbol defined in | |
1203 | a dynamic library, and def_dynamic may not be set; When the | |
1204 | definition in a dynamic lib is overridden by a definition in the | |
1205 | executable use of the symbol in the dynamic lib becomes a | |
1206 | reference to the executable symbol. */ | |
1207 | if (newdyn) | |
1208 | { | |
1209 | if (bfd_is_und_section (sec)) | |
1210 | { | |
1211 | if (bind != STB_WEAK) | |
1212 | { | |
1213 | h->ref_dynamic_nonweak = 1; | |
1214 | hi->ref_dynamic_nonweak = 1; | |
1215 | } | |
1216 | } | |
1217 | else | |
1218 | { | |
6e33951e L |
1219 | /* Update the existing symbol only if they match. */ |
1220 | if (*matched) | |
1221 | h->dynamic_def = 1; | |
ee659f1f AM |
1222 | hi->dynamic_def = 1; |
1223 | } | |
1224 | } | |
1225 | ||
45d6a902 AM |
1226 | /* If we just created the symbol, mark it as being an ELF symbol. |
1227 | Other than that, there is nothing to do--there is no merge issue | |
1228 | with a newly defined symbol--so we just return. */ | |
1229 | ||
1230 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1231 | { |
f5385ebf | 1232 | h->non_elf = 0; |
45d6a902 AM |
1233 | return TRUE; |
1234 | } | |
252b5132 | 1235 | |
45d6a902 AM |
1236 | /* In cases involving weak versioned symbols, we may wind up trying |
1237 | to merge a symbol with itself. Catch that here, to avoid the | |
1238 | confusion that results if we try to override a symbol with | |
1239 | itself. The additional tests catch cases like | |
1240 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1241 | dynamic object, which we do want to handle here. */ | |
1242 | if (abfd == oldbfd | |
895fa45f | 1243 | && (newweak || oldweak) |
45d6a902 | 1244 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1245 | || !h->def_regular)) |
45d6a902 AM |
1246 | return TRUE; |
1247 | ||
707bba77 | 1248 | olddyn = FALSE; |
45d6a902 AM |
1249 | if (oldbfd != NULL) |
1250 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1251 | else if (oldsec != NULL) |
45d6a902 | 1252 | { |
707bba77 | 1253 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1254 | indices used by MIPS ELF. */ |
707bba77 | 1255 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1256 | } |
252b5132 | 1257 | |
1a3b5c34 AM |
1258 | /* Handle a case where plugin_notice won't be called and thus won't |
1259 | set the non_ir_ref flags on the first pass over symbols. */ | |
1260 | if (oldbfd != NULL | |
1261 | && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN) | |
1262 | && newdyn != olddyn) | |
1263 | { | |
1264 | h->root.non_ir_ref_dynamic = TRUE; | |
1265 | hi->root.non_ir_ref_dynamic = TRUE; | |
1266 | } | |
1267 | ||
45d6a902 AM |
1268 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1269 | respectively, appear to be a definition rather than reference. */ | |
1270 | ||
707bba77 | 1271 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1272 | |
707bba77 AM |
1273 | olddef = (h->root.type != bfd_link_hash_undefined |
1274 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1275 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1276 | |
0a36a439 L |
1277 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1278 | respectively, appear to be a function. */ | |
1279 | ||
1280 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1281 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1282 | ||
1283 | oldfunc = (h->type != STT_NOTYPE | |
1284 | && bed->is_function_type (h->type)); | |
1285 | ||
c5d37467 | 1286 | if (!(newfunc && oldfunc) |
5b677558 AM |
1287 | && ELF_ST_TYPE (sym->st_info) != h->type |
1288 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1289 | && h->type != STT_NOTYPE | |
c5d37467 AM |
1290 | && (newdef || bfd_is_com_section (sec)) |
1291 | && (olddef || h->root.type == bfd_link_hash_common)) | |
580a2b6e | 1292 | { |
c5d37467 AM |
1293 | /* If creating a default indirect symbol ("foo" or "foo@") from |
1294 | a dynamic versioned definition ("foo@@") skip doing so if | |
1295 | there is an existing regular definition with a different | |
1296 | type. We don't want, for example, a "time" variable in the | |
1297 | executable overriding a "time" function in a shared library. */ | |
1298 | if (newdyn | |
1299 | && !olddyn) | |
1300 | { | |
1301 | *skip = TRUE; | |
1302 | return TRUE; | |
1303 | } | |
1304 | ||
1305 | /* When adding a symbol from a regular object file after we have | |
1306 | created indirect symbols, undo the indirection and any | |
1307 | dynamic state. */ | |
1308 | if (hi != h | |
1309 | && !newdyn | |
1310 | && olddyn) | |
1311 | { | |
1312 | h = hi; | |
1313 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1314 | h->forced_local = 0; | |
1315 | h->ref_dynamic = 0; | |
1316 | h->def_dynamic = 0; | |
1317 | h->dynamic_def = 0; | |
1318 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1319 | { | |
1320 | h->root.type = bfd_link_hash_undefined; | |
1321 | h->root.u.undef.abfd = abfd; | |
1322 | } | |
1323 | else | |
1324 | { | |
1325 | h->root.type = bfd_link_hash_new; | |
1326 | h->root.u.undef.abfd = NULL; | |
1327 | } | |
1328 | return TRUE; | |
1329 | } | |
580a2b6e L |
1330 | } |
1331 | ||
4c34aff8 AM |
1332 | /* Check TLS symbols. We don't check undefined symbols introduced |
1333 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1334 | check symbols from plugins because they also have no type. */ | |
1335 | if (oldbfd != NULL | |
1336 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1337 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1338 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1339 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1340 | { |
1341 | bfd *ntbfd, *tbfd; | |
1342 | bfd_boolean ntdef, tdef; | |
1343 | asection *ntsec, *tsec; | |
1344 | ||
1345 | if (h->type == STT_TLS) | |
1346 | { | |
3b36f7e6 | 1347 | ntbfd = abfd; |
7479dfd4 L |
1348 | ntsec = sec; |
1349 | ntdef = newdef; | |
1350 | tbfd = oldbfd; | |
1351 | tsec = oldsec; | |
1352 | tdef = olddef; | |
1353 | } | |
1354 | else | |
1355 | { | |
1356 | ntbfd = oldbfd; | |
1357 | ntsec = oldsec; | |
1358 | ntdef = olddef; | |
1359 | tbfd = abfd; | |
1360 | tsec = sec; | |
1361 | tdef = newdef; | |
1362 | } | |
1363 | ||
1364 | if (tdef && ntdef) | |
4eca0228 | 1365 | _bfd_error_handler |
695344c0 | 1366 | /* xgettext:c-format */ |
871b3ab2 AM |
1367 | (_("%s: TLS definition in %pB section %pA " |
1368 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1369 | h->root.root.string, tbfd, tsec, ntbfd, ntsec); |
7479dfd4 | 1370 | else if (!tdef && !ntdef) |
4eca0228 | 1371 | _bfd_error_handler |
695344c0 | 1372 | /* xgettext:c-format */ |
871b3ab2 AM |
1373 | (_("%s: TLS reference in %pB " |
1374 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1375 | h->root.root.string, tbfd, ntbfd); |
7479dfd4 | 1376 | else if (tdef) |
4eca0228 | 1377 | _bfd_error_handler |
695344c0 | 1378 | /* xgettext:c-format */ |
871b3ab2 AM |
1379 | (_("%s: TLS definition in %pB section %pA " |
1380 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1381 | h->root.root.string, tbfd, tsec, ntbfd); |
7479dfd4 | 1382 | else |
4eca0228 | 1383 | _bfd_error_handler |
695344c0 | 1384 | /* xgettext:c-format */ |
871b3ab2 AM |
1385 | (_("%s: TLS reference in %pB " |
1386 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1387 | h->root.root.string, tbfd, ntbfd, ntsec); |
7479dfd4 L |
1388 | |
1389 | bfd_set_error (bfd_error_bad_value); | |
1390 | return FALSE; | |
1391 | } | |
1392 | ||
45d6a902 AM |
1393 | /* If the old symbol has non-default visibility, we ignore the new |
1394 | definition from a dynamic object. */ | |
1395 | if (newdyn | |
9c7a29a3 | 1396 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1397 | && !bfd_is_und_section (sec)) |
1398 | { | |
1399 | *skip = TRUE; | |
1400 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1401 | h->ref_dynamic = 1; |
90c984fc | 1402 | hi->ref_dynamic = 1; |
45d6a902 AM |
1403 | /* A protected symbol has external availability. Make sure it is |
1404 | recorded as dynamic. | |
1405 | ||
1406 | FIXME: Should we check type and size for protected symbol? */ | |
1407 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1408 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1409 | else |
1410 | return TRUE; | |
1411 | } | |
1412 | else if (!newdyn | |
9c7a29a3 | 1413 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1414 | && h->def_dynamic) |
45d6a902 AM |
1415 | { |
1416 | /* If the new symbol with non-default visibility comes from a | |
1417 | relocatable file and the old definition comes from a dynamic | |
1418 | object, we remove the old definition. */ | |
6c9b78e6 | 1419 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1420 | { |
1421 | /* Handle the case where the old dynamic definition is | |
1422 | default versioned. We need to copy the symbol info from | |
1423 | the symbol with default version to the normal one if it | |
1424 | was referenced before. */ | |
1425 | if (h->ref_regular) | |
1426 | { | |
6c9b78e6 | 1427 | hi->root.type = h->root.type; |
d2dee3b2 | 1428 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1429 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1430 | |
6c9b78e6 | 1431 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1432 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1433 | { |
aed81c4e MR |
1434 | /* If the new symbol is hidden or internal, completely undo |
1435 | any dynamic link state. */ | |
1436 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1437 | h->forced_local = 0; | |
1438 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1439 | } |
1440 | else | |
aed81c4e MR |
1441 | h->ref_dynamic = 1; |
1442 | ||
1443 | h->def_dynamic = 0; | |
aed81c4e MR |
1444 | /* FIXME: Should we check type and size for protected symbol? */ |
1445 | h->size = 0; | |
1446 | h->type = 0; | |
1447 | ||
6c9b78e6 | 1448 | h = hi; |
d2dee3b2 L |
1449 | } |
1450 | else | |
6c9b78e6 | 1451 | h = hi; |
d2dee3b2 | 1452 | } |
1de1a317 | 1453 | |
f5eda473 AM |
1454 | /* If the old symbol was undefined before, then it will still be |
1455 | on the undefs list. If the new symbol is undefined or | |
1456 | common, we can't make it bfd_link_hash_new here, because new | |
1457 | undefined or common symbols will be added to the undefs list | |
1458 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1459 | added twice to the undefs list. Also, if the new symbol is | |
1460 | undefweak then we don't want to lose the strong undef. */ | |
1461 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1462 | { |
1de1a317 | 1463 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1464 | h->root.u.undef.abfd = abfd; |
1465 | } | |
1466 | else | |
1467 | { | |
1468 | h->root.type = bfd_link_hash_new; | |
1469 | h->root.u.undef.abfd = NULL; | |
1470 | } | |
1471 | ||
f5eda473 | 1472 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1473 | { |
f5eda473 AM |
1474 | /* If the new symbol is hidden or internal, completely undo |
1475 | any dynamic link state. */ | |
1476 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1477 | h->forced_local = 0; | |
1478 | h->ref_dynamic = 0; | |
45d6a902 | 1479 | } |
f5eda473 AM |
1480 | else |
1481 | h->ref_dynamic = 1; | |
1482 | h->def_dynamic = 0; | |
45d6a902 AM |
1483 | /* FIXME: Should we check type and size for protected symbol? */ |
1484 | h->size = 0; | |
1485 | h->type = 0; | |
1486 | return TRUE; | |
1487 | } | |
14a793b2 | 1488 | |
15b43f48 AM |
1489 | /* If a new weak symbol definition comes from a regular file and the |
1490 | old symbol comes from a dynamic library, we treat the new one as | |
1491 | strong. Similarly, an old weak symbol definition from a regular | |
1492 | file is treated as strong when the new symbol comes from a dynamic | |
1493 | library. Further, an old weak symbol from a dynamic library is | |
1494 | treated as strong if the new symbol is from a dynamic library. | |
1495 | This reflects the way glibc's ld.so works. | |
1496 | ||
165f707a AM |
1497 | Also allow a weak symbol to override a linker script symbol |
1498 | defined by an early pass over the script. This is done so the | |
1499 | linker knows the symbol is defined in an object file, for the | |
1500 | DEFINED script function. | |
1501 | ||
15b43f48 AM |
1502 | Do this before setting *type_change_ok or *size_change_ok so that |
1503 | we warn properly when dynamic library symbols are overridden. */ | |
1504 | ||
165f707a | 1505 | if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) |
0f8a2703 | 1506 | newweak = FALSE; |
15b43f48 | 1507 | if (olddef && newdyn) |
0f8a2703 AM |
1508 | oldweak = FALSE; |
1509 | ||
d334575b | 1510 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1511 | if (newfunc && oldfunc) |
fcb93ecf PB |
1512 | *type_change_ok = TRUE; |
1513 | ||
79349b09 AM |
1514 | /* It's OK to change the type if either the existing symbol or the |
1515 | new symbol is weak. A type change is also OK if the old symbol | |
1516 | is undefined and the new symbol is defined. */ | |
252b5132 | 1517 | |
79349b09 AM |
1518 | if (oldweak |
1519 | || newweak | |
1520 | || (newdef | |
1521 | && h->root.type == bfd_link_hash_undefined)) | |
1522 | *type_change_ok = TRUE; | |
1523 | ||
1524 | /* It's OK to change the size if either the existing symbol or the | |
1525 | new symbol is weak, or if the old symbol is undefined. */ | |
1526 | ||
1527 | if (*type_change_ok | |
1528 | || h->root.type == bfd_link_hash_undefined) | |
1529 | *size_change_ok = TRUE; | |
45d6a902 | 1530 | |
45d6a902 AM |
1531 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1532 | symbol, respectively, appears to be a common symbol in a dynamic | |
1533 | object. If a symbol appears in an uninitialized section, and is | |
1534 | not weak, and is not a function, then it may be a common symbol | |
1535 | which was resolved when the dynamic object was created. We want | |
1536 | to treat such symbols specially, because they raise special | |
1537 | considerations when setting the symbol size: if the symbol | |
1538 | appears as a common symbol in a regular object, and the size in | |
1539 | the regular object is larger, we must make sure that we use the | |
1540 | larger size. This problematic case can always be avoided in C, | |
1541 | but it must be handled correctly when using Fortran shared | |
1542 | libraries. | |
1543 | ||
1544 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1545 | likewise for OLDDYNCOMMON and OLDDEF. | |
1546 | ||
1547 | Note that this test is just a heuristic, and that it is quite | |
1548 | possible to have an uninitialized symbol in a shared object which | |
1549 | is really a definition, rather than a common symbol. This could | |
1550 | lead to some minor confusion when the symbol really is a common | |
1551 | symbol in some regular object. However, I think it will be | |
1552 | harmless. */ | |
1553 | ||
1554 | if (newdyn | |
1555 | && newdef | |
79349b09 | 1556 | && !newweak |
45d6a902 AM |
1557 | && (sec->flags & SEC_ALLOC) != 0 |
1558 | && (sec->flags & SEC_LOAD) == 0 | |
1559 | && sym->st_size > 0 | |
0a36a439 | 1560 | && !newfunc) |
45d6a902 AM |
1561 | newdyncommon = TRUE; |
1562 | else | |
1563 | newdyncommon = FALSE; | |
1564 | ||
1565 | if (olddyn | |
1566 | && olddef | |
1567 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1568 | && h->def_dynamic |
45d6a902 AM |
1569 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1570 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1571 | && h->size > 0 | |
0a36a439 | 1572 | && !oldfunc) |
45d6a902 AM |
1573 | olddyncommon = TRUE; |
1574 | else | |
1575 | olddyncommon = FALSE; | |
1576 | ||
a4d8e49b L |
1577 | /* We now know everything about the old and new symbols. We ask the |
1578 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1579 | if (bed->merge_symbol != NULL) |
1580 | { | |
1581 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1582 | return FALSE; | |
1583 | sec = *psec; | |
1584 | } | |
a4d8e49b | 1585 | |
a83ef4d1 L |
1586 | /* There are multiple definitions of a normal symbol. Skip the |
1587 | default symbol as well as definition from an IR object. */ | |
93f4de39 | 1588 | if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak |
a83ef4d1 L |
1589 | && !default_sym && h->def_regular |
1590 | && !(oldbfd != NULL | |
1591 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
1592 | && (abfd->flags & BFD_PLUGIN) == 0)) | |
93f4de39 RL |
1593 | { |
1594 | /* Handle a multiple definition. */ | |
1595 | (*info->callbacks->multiple_definition) (info, &h->root, | |
1596 | abfd, sec, *pvalue); | |
1597 | *skip = TRUE; | |
1598 | return TRUE; | |
1599 | } | |
1600 | ||
45d6a902 AM |
1601 | /* If both the old and the new symbols look like common symbols in a |
1602 | dynamic object, set the size of the symbol to the larger of the | |
1603 | two. */ | |
1604 | ||
1605 | if (olddyncommon | |
1606 | && newdyncommon | |
1607 | && sym->st_size != h->size) | |
1608 | { | |
1609 | /* Since we think we have two common symbols, issue a multiple | |
1610 | common warning if desired. Note that we only warn if the | |
1611 | size is different. If the size is the same, we simply let | |
1612 | the old symbol override the new one as normally happens with | |
1613 | symbols defined in dynamic objects. */ | |
1614 | ||
1a72702b AM |
1615 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1616 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1617 | if (sym->st_size > h->size) |
1618 | h->size = sym->st_size; | |
252b5132 | 1619 | |
45d6a902 | 1620 | *size_change_ok = TRUE; |
252b5132 RH |
1621 | } |
1622 | ||
45d6a902 AM |
1623 | /* If we are looking at a dynamic object, and we have found a |
1624 | definition, we need to see if the symbol was already defined by | |
1625 | some other object. If so, we want to use the existing | |
1626 | definition, and we do not want to report a multiple symbol | |
1627 | definition error; we do this by clobbering *PSEC to be | |
1628 | bfd_und_section_ptr. | |
1629 | ||
1630 | We treat a common symbol as a definition if the symbol in the | |
1631 | shared library is a function, since common symbols always | |
1632 | represent variables; this can cause confusion in principle, but | |
1633 | any such confusion would seem to indicate an erroneous program or | |
1634 | shared library. We also permit a common symbol in a regular | |
8170f769 | 1635 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1636 | |
1637 | if (newdyn | |
1638 | && newdef | |
77cfaee6 | 1639 | && (olddef |
45d6a902 | 1640 | || (h->root.type == bfd_link_hash_common |
8170f769 | 1641 | && (newweak || newfunc)))) |
45d6a902 AM |
1642 | { |
1643 | *override = TRUE; | |
1644 | newdef = FALSE; | |
1645 | newdyncommon = FALSE; | |
252b5132 | 1646 | |
45d6a902 AM |
1647 | *psec = sec = bfd_und_section_ptr; |
1648 | *size_change_ok = TRUE; | |
252b5132 | 1649 | |
45d6a902 AM |
1650 | /* If we get here when the old symbol is a common symbol, then |
1651 | we are explicitly letting it override a weak symbol or | |
1652 | function in a dynamic object, and we don't want to warn about | |
1653 | a type change. If the old symbol is a defined symbol, a type | |
1654 | change warning may still be appropriate. */ | |
252b5132 | 1655 | |
45d6a902 AM |
1656 | if (h->root.type == bfd_link_hash_common) |
1657 | *type_change_ok = TRUE; | |
1658 | } | |
1659 | ||
1660 | /* Handle the special case of an old common symbol merging with a | |
1661 | new symbol which looks like a common symbol in a shared object. | |
1662 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1663 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1664 | right thing. */ | |
45d6a902 AM |
1665 | |
1666 | if (newdyncommon | |
1667 | && h->root.type == bfd_link_hash_common) | |
1668 | { | |
1669 | *override = TRUE; | |
1670 | newdef = FALSE; | |
1671 | newdyncommon = FALSE; | |
1672 | *pvalue = sym->st_size; | |
a4d8e49b | 1673 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1674 | *size_change_ok = TRUE; |
1675 | } | |
1676 | ||
c5e2cead | 1677 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1678 | if (newdef && olddef && newweak) |
54ac0771 | 1679 | { |
35ed3f94 | 1680 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1681 | if (!(oldbfd != NULL |
1682 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1683 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1684 | { |
1685 | newdef = FALSE; | |
1686 | *skip = TRUE; | |
1687 | } | |
54ac0771 L |
1688 | |
1689 | /* Merge st_other. If the symbol already has a dynamic index, | |
1690 | but visibility says it should not be visible, turn it into a | |
1691 | local symbol. */ | |
b8417128 | 1692 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1693 | if (h->dynindx != -1) |
1694 | switch (ELF_ST_VISIBILITY (h->other)) | |
1695 | { | |
1696 | case STV_INTERNAL: | |
1697 | case STV_HIDDEN: | |
1698 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1699 | break; | |
1700 | } | |
1701 | } | |
c5e2cead | 1702 | |
45d6a902 AM |
1703 | /* If the old symbol is from a dynamic object, and the new symbol is |
1704 | a definition which is not from a dynamic object, then the new | |
1705 | symbol overrides the old symbol. Symbols from regular files | |
1706 | always take precedence over symbols from dynamic objects, even if | |
1707 | they are defined after the dynamic object in the link. | |
1708 | ||
1709 | As above, we again permit a common symbol in a regular object to | |
1710 | override a definition in a shared object if the shared object | |
0f8a2703 | 1711 | symbol is a function or is weak. */ |
45d6a902 AM |
1712 | |
1713 | flip = NULL; | |
77cfaee6 | 1714 | if (!newdyn |
45d6a902 AM |
1715 | && (newdef |
1716 | || (bfd_is_com_section (sec) | |
0a36a439 | 1717 | && (oldweak || oldfunc))) |
45d6a902 AM |
1718 | && olddyn |
1719 | && olddef | |
f5385ebf | 1720 | && h->def_dynamic) |
45d6a902 AM |
1721 | { |
1722 | /* Change the hash table entry to undefined, and let | |
1723 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1724 | new definition. */ | |
1725 | ||
1726 | h->root.type = bfd_link_hash_undefined; | |
1727 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1728 | *size_change_ok = TRUE; | |
1729 | ||
1730 | olddef = FALSE; | |
1731 | olddyncommon = FALSE; | |
1732 | ||
1733 | /* We again permit a type change when a common symbol may be | |
1734 | overriding a function. */ | |
1735 | ||
1736 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1737 | { |
1738 | if (oldfunc) | |
1739 | { | |
1740 | /* If a common symbol overrides a function, make sure | |
1741 | that it isn't defined dynamically nor has type | |
1742 | function. */ | |
1743 | h->def_dynamic = 0; | |
1744 | h->type = STT_NOTYPE; | |
1745 | } | |
1746 | *type_change_ok = TRUE; | |
1747 | } | |
45d6a902 | 1748 | |
6c9b78e6 AM |
1749 | if (hi->root.type == bfd_link_hash_indirect) |
1750 | flip = hi; | |
45d6a902 AM |
1751 | else |
1752 | /* This union may have been set to be non-NULL when this symbol | |
1753 | was seen in a dynamic object. We must force the union to be | |
1754 | NULL, so that it is correct for a regular symbol. */ | |
1755 | h->verinfo.vertree = NULL; | |
1756 | } | |
1757 | ||
1758 | /* Handle the special case of a new common symbol merging with an | |
1759 | old symbol that looks like it might be a common symbol defined in | |
1760 | a shared object. Note that we have already handled the case in | |
1761 | which a new common symbol should simply override the definition | |
1762 | in the shared library. */ | |
1763 | ||
1764 | if (! newdyn | |
1765 | && bfd_is_com_section (sec) | |
1766 | && olddyncommon) | |
1767 | { | |
1768 | /* It would be best if we could set the hash table entry to a | |
1769 | common symbol, but we don't know what to use for the section | |
1770 | or the alignment. */ | |
1a72702b AM |
1771 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1772 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1773 | |
4cc11e76 | 1774 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1775 | larger, pretend that the new symbol has its size. */ |
1776 | ||
1777 | if (h->size > *pvalue) | |
1778 | *pvalue = h->size; | |
1779 | ||
af44c138 L |
1780 | /* We need to remember the alignment required by the symbol |
1781 | in the dynamic object. */ | |
1782 | BFD_ASSERT (pold_alignment); | |
1783 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1784 | |
1785 | olddef = FALSE; | |
1786 | olddyncommon = FALSE; | |
1787 | ||
1788 | h->root.type = bfd_link_hash_undefined; | |
1789 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1790 | ||
1791 | *size_change_ok = TRUE; | |
1792 | *type_change_ok = TRUE; | |
1793 | ||
6c9b78e6 AM |
1794 | if (hi->root.type == bfd_link_hash_indirect) |
1795 | flip = hi; | |
45d6a902 AM |
1796 | else |
1797 | h->verinfo.vertree = NULL; | |
1798 | } | |
1799 | ||
1800 | if (flip != NULL) | |
1801 | { | |
1802 | /* Handle the case where we had a versioned symbol in a dynamic | |
1803 | library and now find a definition in a normal object. In this | |
1804 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1805 | flip->root.type = h->root.type; |
00cbee0a | 1806 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1807 | h->root.type = bfd_link_hash_indirect; |
1808 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1809 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1810 | if (h->def_dynamic) |
45d6a902 | 1811 | { |
f5385ebf AM |
1812 | h->def_dynamic = 0; |
1813 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1814 | } |
1815 | } | |
1816 | ||
45d6a902 AM |
1817 | return TRUE; |
1818 | } | |
1819 | ||
1820 | /* This function is called to create an indirect symbol from the | |
1821 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1822 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1823 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1824 | |
28caa186 | 1825 | static bfd_boolean |
268b6b39 AM |
1826 | _bfd_elf_add_default_symbol (bfd *abfd, |
1827 | struct bfd_link_info *info, | |
1828 | struct elf_link_hash_entry *h, | |
1829 | const char *name, | |
1830 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1831 | asection *sec, |
1832 | bfd_vma value, | |
1833 | bfd **poldbfd, | |
e3c9d234 | 1834 | bfd_boolean *dynsym) |
45d6a902 AM |
1835 | { |
1836 | bfd_boolean type_change_ok; | |
1837 | bfd_boolean size_change_ok; | |
1838 | bfd_boolean skip; | |
1839 | char *shortname; | |
1840 | struct elf_link_hash_entry *hi; | |
1841 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1842 | const struct elf_backend_data *bed; |
45d6a902 AM |
1843 | bfd_boolean collect; |
1844 | bfd_boolean dynamic; | |
e3c9d234 | 1845 | bfd_boolean override; |
45d6a902 AM |
1846 | char *p; |
1847 | size_t len, shortlen; | |
ffd65175 | 1848 | asection *tmp_sec; |
6e33951e | 1849 | bfd_boolean matched; |
45d6a902 | 1850 | |
422f1182 L |
1851 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1852 | return TRUE; | |
1853 | ||
45d6a902 AM |
1854 | /* If this symbol has a version, and it is the default version, we |
1855 | create an indirect symbol from the default name to the fully | |
1856 | decorated name. This will cause external references which do not | |
1857 | specify a version to be bound to this version of the symbol. */ | |
1858 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1859 | if (h->versioned == unknown) |
1860 | { | |
1861 | if (p == NULL) | |
1862 | { | |
1863 | h->versioned = unversioned; | |
1864 | return TRUE; | |
1865 | } | |
1866 | else | |
1867 | { | |
1868 | if (p[1] != ELF_VER_CHR) | |
1869 | { | |
1870 | h->versioned = versioned_hidden; | |
1871 | return TRUE; | |
1872 | } | |
1873 | else | |
1874 | h->versioned = versioned; | |
1875 | } | |
1876 | } | |
4373f8af L |
1877 | else |
1878 | { | |
1879 | /* PR ld/19073: We may see an unversioned definition after the | |
1880 | default version. */ | |
1881 | if (p == NULL) | |
1882 | return TRUE; | |
1883 | } | |
45d6a902 | 1884 | |
45d6a902 AM |
1885 | bed = get_elf_backend_data (abfd); |
1886 | collect = bed->collect; | |
1887 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1888 | ||
1889 | shortlen = p - name; | |
a50b1753 | 1890 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1891 | if (shortname == NULL) |
1892 | return FALSE; | |
1893 | memcpy (shortname, name, shortlen); | |
1894 | shortname[shortlen] = '\0'; | |
1895 | ||
1896 | /* We are going to create a new symbol. Merge it with any existing | |
1897 | symbol with this name. For the purposes of the merge, act as | |
1898 | though we were defining the symbol we just defined, although we | |
1899 | actually going to define an indirect symbol. */ | |
1900 | type_change_ok = FALSE; | |
1901 | size_change_ok = FALSE; | |
6e33951e | 1902 | matched = TRUE; |
ffd65175 AM |
1903 | tmp_sec = sec; |
1904 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1905 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1906 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1907 | return FALSE; |
1908 | ||
1909 | if (skip) | |
1910 | goto nondefault; | |
1911 | ||
5b677558 AM |
1912 | if (hi->def_regular) |
1913 | { | |
1914 | /* If the undecorated symbol will have a version added by a | |
1915 | script different to H, then don't indirect to/from the | |
1916 | undecorated symbol. This isn't ideal because we may not yet | |
1917 | have seen symbol versions, if given by a script on the | |
1918 | command line rather than via --version-script. */ | |
1919 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1920 | { | |
1921 | bfd_boolean hide; | |
1922 | ||
1923 | hi->verinfo.vertree | |
1924 | = bfd_find_version_for_sym (info->version_info, | |
1925 | hi->root.root.string, &hide); | |
1926 | if (hi->verinfo.vertree != NULL && hide) | |
1927 | { | |
1928 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1929 | goto nondefault; | |
1930 | } | |
1931 | } | |
1932 | if (hi->verinfo.vertree != NULL | |
1933 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1934 | goto nondefault; | |
1935 | } | |
1936 | ||
45d6a902 AM |
1937 | if (! override) |
1938 | { | |
c6e8a9a8 | 1939 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1940 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1941 | { |
1942 | bh = &hi->root; | |
fbcc8baf L |
1943 | if (bh->type == bfd_link_hash_defined |
1944 | && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0) | |
1945 | { | |
1946 | /* Mark the previous definition from IR object as | |
1947 | undefined so that the generic linker will override | |
1948 | it. */ | |
1949 | bh->type = bfd_link_hash_undefined; | |
1950 | bh->u.undef.abfd = bh->u.def.section->owner; | |
1951 | } | |
c6e8a9a8 L |
1952 | if (! (_bfd_generic_link_add_one_symbol |
1953 | (info, abfd, shortname, BSF_INDIRECT, | |
1954 | bfd_ind_section_ptr, | |
1955 | 0, name, FALSE, collect, &bh))) | |
1956 | return FALSE; | |
1957 | hi = (struct elf_link_hash_entry *) bh; | |
1958 | } | |
45d6a902 AM |
1959 | } |
1960 | else | |
1961 | { | |
1962 | /* In this case the symbol named SHORTNAME is overriding the | |
1963 | indirect symbol we want to add. We were planning on making | |
1964 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1965 | is the name without a version. NAME is the fully versioned | |
1966 | name, and it is the default version. | |
1967 | ||
1968 | Overriding means that we already saw a definition for the | |
1969 | symbol SHORTNAME in a regular object, and it is overriding | |
1970 | the symbol defined in the dynamic object. | |
1971 | ||
1972 | When this happens, we actually want to change NAME, the | |
1973 | symbol we just added, to refer to SHORTNAME. This will cause | |
1974 | references to NAME in the shared object to become references | |
1975 | to SHORTNAME in the regular object. This is what we expect | |
1976 | when we override a function in a shared object: that the | |
1977 | references in the shared object will be mapped to the | |
1978 | definition in the regular object. */ | |
1979 | ||
1980 | while (hi->root.type == bfd_link_hash_indirect | |
1981 | || hi->root.type == bfd_link_hash_warning) | |
1982 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1983 | ||
1984 | h->root.type = bfd_link_hash_indirect; | |
1985 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1986 | if (h->def_dynamic) |
45d6a902 | 1987 | { |
f5385ebf AM |
1988 | h->def_dynamic = 0; |
1989 | hi->ref_dynamic = 1; | |
1990 | if (hi->ref_regular | |
1991 | || hi->def_regular) | |
45d6a902 | 1992 | { |
c152c796 | 1993 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1994 | return FALSE; |
1995 | } | |
1996 | } | |
1997 | ||
1998 | /* Now set HI to H, so that the following code will set the | |
1999 | other fields correctly. */ | |
2000 | hi = h; | |
2001 | } | |
2002 | ||
fab4a87f L |
2003 | /* Check if HI is a warning symbol. */ |
2004 | if (hi->root.type == bfd_link_hash_warning) | |
2005 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
2006 | ||
45d6a902 AM |
2007 | /* If there is a duplicate definition somewhere, then HI may not |
2008 | point to an indirect symbol. We will have reported an error to | |
2009 | the user in that case. */ | |
2010 | ||
2011 | if (hi->root.type == bfd_link_hash_indirect) | |
2012 | { | |
2013 | struct elf_link_hash_entry *ht; | |
2014 | ||
45d6a902 | 2015 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 2016 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 2017 | |
68c88cd4 AM |
2018 | /* A reference to the SHORTNAME symbol from a dynamic library |
2019 | will be satisfied by the versioned symbol at runtime. In | |
2020 | effect, we have a reference to the versioned symbol. */ | |
2021 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
2022 | hi->dynamic_def |= ht->dynamic_def; | |
2023 | ||
45d6a902 AM |
2024 | /* See if the new flags lead us to realize that the symbol must |
2025 | be dynamic. */ | |
2026 | if (! *dynsym) | |
2027 | { | |
2028 | if (! dynamic) | |
2029 | { | |
0e1862bb | 2030 | if (! bfd_link_executable (info) |
90c984fc | 2031 | || hi->def_dynamic |
f5385ebf | 2032 | || hi->ref_dynamic) |
45d6a902 AM |
2033 | *dynsym = TRUE; |
2034 | } | |
2035 | else | |
2036 | { | |
f5385ebf | 2037 | if (hi->ref_regular) |
45d6a902 AM |
2038 | *dynsym = TRUE; |
2039 | } | |
2040 | } | |
2041 | } | |
2042 | ||
2043 | /* We also need to define an indirection from the nondefault version | |
2044 | of the symbol. */ | |
2045 | ||
2046 | nondefault: | |
2047 | len = strlen (name); | |
a50b1753 | 2048 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
2049 | if (shortname == NULL) |
2050 | return FALSE; | |
2051 | memcpy (shortname, name, shortlen); | |
2052 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
2053 | ||
2054 | /* Once again, merge with any existing symbol. */ | |
2055 | type_change_ok = FALSE; | |
2056 | size_change_ok = FALSE; | |
ffd65175 AM |
2057 | tmp_sec = sec; |
2058 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 2059 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 2060 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
2061 | return FALSE; |
2062 | ||
2063 | if (skip) | |
2064 | return TRUE; | |
2065 | ||
2066 | if (override) | |
2067 | { | |
2068 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
2069 | the type of override we do in the case above unless it is | |
4cc11e76 | 2070 | overridden by a versioned definition. */ |
45d6a902 AM |
2071 | if (hi->root.type != bfd_link_hash_defined |
2072 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 2073 | _bfd_error_handler |
695344c0 | 2074 | /* xgettext:c-format */ |
871b3ab2 | 2075 | (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"), |
d003868e | 2076 | abfd, shortname); |
45d6a902 AM |
2077 | } |
2078 | else | |
2079 | { | |
2080 | bh = &hi->root; | |
2081 | if (! (_bfd_generic_link_add_one_symbol | |
2082 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 2083 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
2084 | return FALSE; |
2085 | hi = (struct elf_link_hash_entry *) bh; | |
2086 | ||
2087 | /* If there is a duplicate definition somewhere, then HI may not | |
2088 | point to an indirect symbol. We will have reported an error | |
2089 | to the user in that case. */ | |
2090 | ||
2091 | if (hi->root.type == bfd_link_hash_indirect) | |
2092 | { | |
fcfa13d2 | 2093 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
2094 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
2095 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
2096 | |
2097 | /* See if the new flags lead us to realize that the symbol | |
2098 | must be dynamic. */ | |
2099 | if (! *dynsym) | |
2100 | { | |
2101 | if (! dynamic) | |
2102 | { | |
0e1862bb | 2103 | if (! bfd_link_executable (info) |
f5385ebf | 2104 | || hi->ref_dynamic) |
45d6a902 AM |
2105 | *dynsym = TRUE; |
2106 | } | |
2107 | else | |
2108 | { | |
f5385ebf | 2109 | if (hi->ref_regular) |
45d6a902 AM |
2110 | *dynsym = TRUE; |
2111 | } | |
2112 | } | |
2113 | } | |
2114 | } | |
2115 | ||
2116 | return TRUE; | |
2117 | } | |
2118 | \f | |
2119 | /* This routine is used to export all defined symbols into the dynamic | |
2120 | symbol table. It is called via elf_link_hash_traverse. */ | |
2121 | ||
28caa186 | 2122 | static bfd_boolean |
268b6b39 | 2123 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2124 | { |
a50b1753 | 2125 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
2126 | |
2127 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2128 | if (h->root.type == bfd_link_hash_indirect) | |
2129 | return TRUE; | |
2130 | ||
7686d77d AM |
2131 | /* Ignore this if we won't export it. */ |
2132 | if (!eif->info->export_dynamic && !h->dynamic) | |
2133 | return TRUE; | |
45d6a902 AM |
2134 | |
2135 | if (h->dynindx == -1 | |
fd91d419 L |
2136 | && (h->def_regular || h->ref_regular) |
2137 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2138 | h->root.root.string)) | |
45d6a902 | 2139 | { |
fd91d419 | 2140 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2141 | { |
fd91d419 L |
2142 | eif->failed = TRUE; |
2143 | return FALSE; | |
45d6a902 AM |
2144 | } |
2145 | } | |
2146 | ||
2147 | return TRUE; | |
2148 | } | |
2149 | \f | |
2150 | /* Look through the symbols which are defined in other shared | |
2151 | libraries and referenced here. Update the list of version | |
2152 | dependencies. This will be put into the .gnu.version_r section. | |
2153 | This function is called via elf_link_hash_traverse. */ | |
2154 | ||
28caa186 | 2155 | static bfd_boolean |
268b6b39 AM |
2156 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2157 | void *data) | |
45d6a902 | 2158 | { |
a50b1753 | 2159 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2160 | Elf_Internal_Verneed *t; |
2161 | Elf_Internal_Vernaux *a; | |
2162 | bfd_size_type amt; | |
2163 | ||
45d6a902 AM |
2164 | /* We only care about symbols defined in shared objects with version |
2165 | information. */ | |
f5385ebf AM |
2166 | if (!h->def_dynamic |
2167 | || h->def_regular | |
45d6a902 | 2168 | || h->dynindx == -1 |
7b20f099 AM |
2169 | || h->verinfo.verdef == NULL |
2170 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2171 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2172 | return TRUE; |
2173 | ||
2174 | /* See if we already know about this version. */ | |
28caa186 AM |
2175 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2176 | t != NULL; | |
2177 | t = t->vn_nextref) | |
45d6a902 AM |
2178 | { |
2179 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2180 | continue; | |
2181 | ||
2182 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2183 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2184 | return TRUE; | |
2185 | ||
2186 | break; | |
2187 | } | |
2188 | ||
2189 | /* This is a new version. Add it to tree we are building. */ | |
2190 | ||
2191 | if (t == NULL) | |
2192 | { | |
2193 | amt = sizeof *t; | |
a50b1753 | 2194 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2195 | if (t == NULL) |
2196 | { | |
2197 | rinfo->failed = TRUE; | |
2198 | return FALSE; | |
2199 | } | |
2200 | ||
2201 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2202 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2203 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2204 | } |
2205 | ||
2206 | amt = sizeof *a; | |
a50b1753 | 2207 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2208 | if (a == NULL) |
2209 | { | |
2210 | rinfo->failed = TRUE; | |
2211 | return FALSE; | |
2212 | } | |
45d6a902 AM |
2213 | |
2214 | /* Note that we are copying a string pointer here, and testing it | |
2215 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2216 | discard the string data when low in memory, this will have to be | |
2217 | fixed. */ | |
2218 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2219 | ||
2220 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2221 | a->vna_nextptr = t->vn_auxptr; | |
2222 | ||
2223 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2224 | ++rinfo->vers; | |
2225 | ||
2226 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2227 | ||
2228 | t->vn_auxptr = a; | |
2229 | ||
2230 | return TRUE; | |
2231 | } | |
2232 | ||
099bb8fb L |
2233 | /* Return TRUE and set *HIDE to TRUE if the versioned symbol is |
2234 | hidden. Set *T_P to NULL if there is no match. */ | |
2235 | ||
2236 | static bfd_boolean | |
2237 | _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info, | |
2238 | struct elf_link_hash_entry *h, | |
2239 | const char *version_p, | |
2240 | struct bfd_elf_version_tree **t_p, | |
2241 | bfd_boolean *hide) | |
2242 | { | |
2243 | struct bfd_elf_version_tree *t; | |
2244 | ||
2245 | /* Look for the version. If we find it, it is no longer weak. */ | |
2246 | for (t = info->version_info; t != NULL; t = t->next) | |
2247 | { | |
2248 | if (strcmp (t->name, version_p) == 0) | |
2249 | { | |
2250 | size_t len; | |
2251 | char *alc; | |
2252 | struct bfd_elf_version_expr *d; | |
2253 | ||
2254 | len = version_p - h->root.root.string; | |
2255 | alc = (char *) bfd_malloc (len); | |
2256 | if (alc == NULL) | |
2257 | return FALSE; | |
2258 | memcpy (alc, h->root.root.string, len - 1); | |
2259 | alc[len - 1] = '\0'; | |
2260 | if (alc[len - 2] == ELF_VER_CHR) | |
2261 | alc[len - 2] = '\0'; | |
2262 | ||
2263 | h->verinfo.vertree = t; | |
2264 | t->used = TRUE; | |
2265 | d = NULL; | |
2266 | ||
2267 | if (t->globals.list != NULL) | |
2268 | d = (*t->match) (&t->globals, NULL, alc); | |
2269 | ||
2270 | /* See if there is anything to force this symbol to | |
2271 | local scope. */ | |
2272 | if (d == NULL && t->locals.list != NULL) | |
2273 | { | |
2274 | d = (*t->match) (&t->locals, NULL, alc); | |
2275 | if (d != NULL | |
2276 | && h->dynindx != -1 | |
2277 | && ! info->export_dynamic) | |
2278 | *hide = TRUE; | |
2279 | } | |
2280 | ||
2281 | free (alc); | |
2282 | break; | |
2283 | } | |
2284 | } | |
2285 | ||
2286 | *t_p = t; | |
2287 | ||
2288 | return TRUE; | |
2289 | } | |
2290 | ||
2291 | /* Return TRUE if the symbol H is hidden by version script. */ | |
2292 | ||
2293 | bfd_boolean | |
2294 | _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info, | |
2295 | struct elf_link_hash_entry *h) | |
2296 | { | |
2297 | const char *p; | |
2298 | bfd_boolean hide = FALSE; | |
2299 | const struct elf_backend_data *bed | |
2300 | = get_elf_backend_data (info->output_bfd); | |
2301 | ||
2302 | /* Version script only hides symbols defined in regular objects. */ | |
2303 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) | |
2304 | return TRUE; | |
2305 | ||
2306 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
2307 | if (p != NULL && h->verinfo.vertree == NULL) | |
2308 | { | |
2309 | struct bfd_elf_version_tree *t; | |
2310 | ||
2311 | ++p; | |
2312 | if (*p == ELF_VER_CHR) | |
2313 | ++p; | |
2314 | ||
2315 | if (*p != '\0' | |
2316 | && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide) | |
2317 | && hide) | |
2318 | { | |
2319 | if (hide) | |
2320 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2321 | return TRUE; | |
2322 | } | |
2323 | } | |
2324 | ||
2325 | /* If we don't have a version for this symbol, see if we can find | |
2326 | something. */ | |
2327 | if (h->verinfo.vertree == NULL && info->version_info != NULL) | |
2328 | { | |
2329 | h->verinfo.vertree | |
2330 | = bfd_find_version_for_sym (info->version_info, | |
2331 | h->root.root.string, &hide); | |
2332 | if (h->verinfo.vertree != NULL && hide) | |
2333 | { | |
2334 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2335 | return TRUE; | |
2336 | } | |
2337 | } | |
2338 | ||
2339 | return FALSE; | |
2340 | } | |
2341 | ||
45d6a902 AM |
2342 | /* Figure out appropriate versions for all the symbols. We may not |
2343 | have the version number script until we have read all of the input | |
2344 | files, so until that point we don't know which symbols should be | |
2345 | local. This function is called via elf_link_hash_traverse. */ | |
2346 | ||
28caa186 | 2347 | static bfd_boolean |
268b6b39 | 2348 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2349 | { |
28caa186 | 2350 | struct elf_info_failed *sinfo; |
45d6a902 | 2351 | struct bfd_link_info *info; |
9c5bfbb7 | 2352 | const struct elf_backend_data *bed; |
45d6a902 AM |
2353 | struct elf_info_failed eif; |
2354 | char *p; | |
099bb8fb | 2355 | bfd_boolean hide; |
45d6a902 | 2356 | |
a50b1753 | 2357 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2358 | info = sinfo->info; |
2359 | ||
45d6a902 AM |
2360 | /* Fix the symbol flags. */ |
2361 | eif.failed = FALSE; | |
2362 | eif.info = info; | |
2363 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2364 | { | |
2365 | if (eif.failed) | |
2366 | sinfo->failed = TRUE; | |
2367 | return FALSE; | |
2368 | } | |
2369 | ||
0a640d71 L |
2370 | bed = get_elf_backend_data (info->output_bfd); |
2371 | ||
45d6a902 AM |
2372 | /* We only need version numbers for symbols defined in regular |
2373 | objects. */ | |
f5385ebf | 2374 | if (!h->def_regular) |
0a640d71 L |
2375 | { |
2376 | /* Hide symbols defined in discarded input sections. */ | |
2377 | if ((h->root.type == bfd_link_hash_defined | |
2378 | || h->root.type == bfd_link_hash_defweak) | |
2379 | && discarded_section (h->root.u.def.section)) | |
2380 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2381 | return TRUE; | |
2382 | } | |
45d6a902 | 2383 | |
099bb8fb | 2384 | hide = FALSE; |
45d6a902 AM |
2385 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2386 | if (p != NULL && h->verinfo.vertree == NULL) | |
2387 | { | |
2388 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2389 | |
45d6a902 AM |
2390 | ++p; |
2391 | if (*p == ELF_VER_CHR) | |
6e33951e | 2392 | ++p; |
45d6a902 AM |
2393 | |
2394 | /* If there is no version string, we can just return out. */ | |
2395 | if (*p == '\0') | |
6e33951e | 2396 | return TRUE; |
45d6a902 | 2397 | |
099bb8fb | 2398 | if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)) |
45d6a902 | 2399 | { |
099bb8fb L |
2400 | sinfo->failed = TRUE; |
2401 | return FALSE; | |
45d6a902 AM |
2402 | } |
2403 | ||
099bb8fb L |
2404 | if (hide) |
2405 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2406 | ||
45d6a902 AM |
2407 | /* If we are building an application, we need to create a |
2408 | version node for this version. */ | |
0e1862bb | 2409 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2410 | { |
2411 | struct bfd_elf_version_tree **pp; | |
2412 | int version_index; | |
2413 | ||
2414 | /* If we aren't going to export this symbol, we don't need | |
2415 | to worry about it. */ | |
2416 | if (h->dynindx == -1) | |
2417 | return TRUE; | |
2418 | ||
ef53be89 AM |
2419 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2420 | sizeof *t); | |
45d6a902 AM |
2421 | if (t == NULL) |
2422 | { | |
2423 | sinfo->failed = TRUE; | |
2424 | return FALSE; | |
2425 | } | |
2426 | ||
45d6a902 | 2427 | t->name = p; |
45d6a902 AM |
2428 | t->name_indx = (unsigned int) -1; |
2429 | t->used = TRUE; | |
2430 | ||
2431 | version_index = 1; | |
2432 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2433 | if (sinfo->info->version_info != NULL |
2434 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2435 | version_index = 0; |
fd91d419 L |
2436 | for (pp = &sinfo->info->version_info; |
2437 | *pp != NULL; | |
2438 | pp = &(*pp)->next) | |
45d6a902 AM |
2439 | ++version_index; |
2440 | t->vernum = version_index; | |
2441 | ||
2442 | *pp = t; | |
2443 | ||
2444 | h->verinfo.vertree = t; | |
2445 | } | |
2446 | else if (t == NULL) | |
2447 | { | |
2448 | /* We could not find the version for a symbol when | |
2449 | generating a shared archive. Return an error. */ | |
4eca0228 | 2450 | _bfd_error_handler |
695344c0 | 2451 | /* xgettext:c-format */ |
871b3ab2 | 2452 | (_("%pB: version node not found for symbol %s"), |
28caa186 | 2453 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2454 | bfd_set_error (bfd_error_bad_value); |
2455 | sinfo->failed = TRUE; | |
2456 | return FALSE; | |
2457 | } | |
45d6a902 AM |
2458 | } |
2459 | ||
2460 | /* If we don't have a version for this symbol, see if we can find | |
2461 | something. */ | |
099bb8fb L |
2462 | if (!hide |
2463 | && h->verinfo.vertree == NULL | |
2464 | && sinfo->info->version_info != NULL) | |
45d6a902 | 2465 | { |
fd91d419 L |
2466 | h->verinfo.vertree |
2467 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2468 | h->root.root.string, &hide); | |
1e8fa21e AM |
2469 | if (h->verinfo.vertree != NULL && hide) |
2470 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2471 | } |
2472 | ||
2473 | return TRUE; | |
2474 | } | |
2475 | \f | |
45d6a902 AM |
2476 | /* Read and swap the relocs from the section indicated by SHDR. This |
2477 | may be either a REL or a RELA section. The relocations are | |
2478 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2479 | which should have already been allocated to contain enough space. | |
2480 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2481 | relocations should be stored. | |
2482 | ||
2483 | Returns FALSE if something goes wrong. */ | |
2484 | ||
2485 | static bfd_boolean | |
268b6b39 | 2486 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2487 | asection *sec, |
268b6b39 AM |
2488 | Elf_Internal_Shdr *shdr, |
2489 | void *external_relocs, | |
2490 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2491 | { |
9c5bfbb7 | 2492 | const struct elf_backend_data *bed; |
268b6b39 | 2493 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2494 | const bfd_byte *erela; |
2495 | const bfd_byte *erelaend; | |
2496 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2497 | Elf_Internal_Shdr *symtab_hdr; |
2498 | size_t nsyms; | |
45d6a902 | 2499 | |
45d6a902 AM |
2500 | /* Position ourselves at the start of the section. */ |
2501 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2502 | return FALSE; | |
2503 | ||
2504 | /* Read the relocations. */ | |
2505 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2506 | return FALSE; | |
2507 | ||
243ef1e0 | 2508 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2509 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2510 | |
45d6a902 AM |
2511 | bed = get_elf_backend_data (abfd); |
2512 | ||
2513 | /* Convert the external relocations to the internal format. */ | |
2514 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2515 | swap_in = bed->s->swap_reloc_in; | |
2516 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2517 | swap_in = bed->s->swap_reloca_in; | |
2518 | else | |
2519 | { | |
2520 | bfd_set_error (bfd_error_wrong_format); | |
2521 | return FALSE; | |
2522 | } | |
2523 | ||
a50b1753 | 2524 | erela = (const bfd_byte *) external_relocs; |
51992aec | 2525 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
2526 | irela = internal_relocs; |
2527 | while (erela < erelaend) | |
2528 | { | |
243ef1e0 L |
2529 | bfd_vma r_symndx; |
2530 | ||
45d6a902 | 2531 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2532 | r_symndx = ELF32_R_SYM (irela->r_info); |
2533 | if (bed->s->arch_size == 64) | |
2534 | r_symndx >>= 24; | |
ce98a316 NC |
2535 | if (nsyms > 0) |
2536 | { | |
2537 | if ((size_t) r_symndx >= nsyms) | |
2538 | { | |
4eca0228 | 2539 | _bfd_error_handler |
695344c0 | 2540 | /* xgettext:c-format */ |
2dcf00ce AM |
2541 | (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)" |
2542 | " for offset %#" PRIx64 " in section `%pA'"), | |
2543 | abfd, (uint64_t) r_symndx, (unsigned long) nsyms, | |
2544 | (uint64_t) irela->r_offset, sec); | |
ce98a316 NC |
2545 | bfd_set_error (bfd_error_bad_value); |
2546 | return FALSE; | |
2547 | } | |
2548 | } | |
cf35638d | 2549 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2550 | { |
4eca0228 | 2551 | _bfd_error_handler |
695344c0 | 2552 | /* xgettext:c-format */ |
2dcf00ce AM |
2553 | (_("%pB: non-zero symbol index (%#" PRIx64 ")" |
2554 | " for offset %#" PRIx64 " in section `%pA'" | |
ce98a316 | 2555 | " when the object file has no symbol table"), |
2dcf00ce AM |
2556 | abfd, (uint64_t) r_symndx, |
2557 | (uint64_t) irela->r_offset, sec); | |
243ef1e0 L |
2558 | bfd_set_error (bfd_error_bad_value); |
2559 | return FALSE; | |
2560 | } | |
45d6a902 AM |
2561 | irela += bed->s->int_rels_per_ext_rel; |
2562 | erela += shdr->sh_entsize; | |
2563 | } | |
2564 | ||
2565 | return TRUE; | |
2566 | } | |
2567 | ||
2568 | /* Read and swap the relocs for a section O. They may have been | |
2569 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2570 | not NULL, they are used as buffers to read into. They are known to | |
2571 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2572 | the return value is allocated using either malloc or bfd_alloc, | |
2573 | according to the KEEP_MEMORY argument. If O has two relocation | |
2574 | sections (both REL and RELA relocations), then the REL_HDR | |
2575 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
d4730f92 | 2576 | RELA_HDR relocations. */ |
45d6a902 AM |
2577 | |
2578 | Elf_Internal_Rela * | |
268b6b39 AM |
2579 | _bfd_elf_link_read_relocs (bfd *abfd, |
2580 | asection *o, | |
2581 | void *external_relocs, | |
2582 | Elf_Internal_Rela *internal_relocs, | |
2583 | bfd_boolean keep_memory) | |
45d6a902 | 2584 | { |
268b6b39 | 2585 | void *alloc1 = NULL; |
45d6a902 | 2586 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2587 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2588 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2589 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2590 | |
d4730f92 BS |
2591 | if (esdo->relocs != NULL) |
2592 | return esdo->relocs; | |
45d6a902 AM |
2593 | |
2594 | if (o->reloc_count == 0) | |
2595 | return NULL; | |
2596 | ||
45d6a902 AM |
2597 | if (internal_relocs == NULL) |
2598 | { | |
2599 | bfd_size_type size; | |
2600 | ||
056bafd4 | 2601 | size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); |
45d6a902 | 2602 | if (keep_memory) |
a50b1753 | 2603 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); |
45d6a902 | 2604 | else |
a50b1753 | 2605 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2606 | if (internal_relocs == NULL) |
2607 | goto error_return; | |
2608 | } | |
2609 | ||
2610 | if (external_relocs == NULL) | |
2611 | { | |
d4730f92 BS |
2612 | bfd_size_type size = 0; |
2613 | ||
2614 | if (esdo->rel.hdr) | |
2615 | size += esdo->rel.hdr->sh_size; | |
2616 | if (esdo->rela.hdr) | |
2617 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2618 | |
268b6b39 | 2619 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2620 | if (alloc1 == NULL) |
2621 | goto error_return; | |
2622 | external_relocs = alloc1; | |
2623 | } | |
2624 | ||
d4730f92 BS |
2625 | internal_rela_relocs = internal_relocs; |
2626 | if (esdo->rel.hdr) | |
2627 | { | |
2628 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2629 | external_relocs, | |
2630 | internal_relocs)) | |
2631 | goto error_return; | |
2632 | external_relocs = (((bfd_byte *) external_relocs) | |
2633 | + esdo->rel.hdr->sh_size); | |
2634 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2635 | * bed->s->int_rels_per_ext_rel); | |
2636 | } | |
2637 | ||
2638 | if (esdo->rela.hdr | |
2639 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2640 | external_relocs, | |
2641 | internal_rela_relocs))) | |
45d6a902 AM |
2642 | goto error_return; |
2643 | ||
2644 | /* Cache the results for next time, if we can. */ | |
2645 | if (keep_memory) | |
d4730f92 | 2646 | esdo->relocs = internal_relocs; |
45d6a902 AM |
2647 | |
2648 | if (alloc1 != NULL) | |
2649 | free (alloc1); | |
2650 | ||
2651 | /* Don't free alloc2, since if it was allocated we are passing it | |
2652 | back (under the name of internal_relocs). */ | |
2653 | ||
2654 | return internal_relocs; | |
2655 | ||
2656 | error_return: | |
2657 | if (alloc1 != NULL) | |
2658 | free (alloc1); | |
2659 | if (alloc2 != NULL) | |
4dd07732 AM |
2660 | { |
2661 | if (keep_memory) | |
2662 | bfd_release (abfd, alloc2); | |
2663 | else | |
2664 | free (alloc2); | |
2665 | } | |
45d6a902 AM |
2666 | return NULL; |
2667 | } | |
2668 | ||
2669 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2670 | section header for a section containing relocations for O. */ | |
2671 | ||
28caa186 | 2672 | static bfd_boolean |
9eaff861 AO |
2673 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2674 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2675 | { |
9eaff861 | 2676 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2677 | |
2678 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2679 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2680 | |
2681 | /* The contents field must last into write_object_contents, so we | |
2682 | allocate it with bfd_alloc rather than malloc. Also since we | |
2683 | cannot be sure that the contents will actually be filled in, | |
2684 | we zero the allocated space. */ | |
a50b1753 | 2685 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2686 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2687 | return FALSE; | |
2688 | ||
d4730f92 | 2689 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2690 | { |
2691 | struct elf_link_hash_entry **p; | |
2692 | ||
ca4be51c AM |
2693 | p = ((struct elf_link_hash_entry **) |
2694 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 AM |
2695 | if (p == NULL) |
2696 | return FALSE; | |
2697 | ||
d4730f92 | 2698 | reldata->hashes = p; |
45d6a902 AM |
2699 | } |
2700 | ||
2701 | return TRUE; | |
2702 | } | |
2703 | ||
2704 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2705 | originated from the section given by INPUT_REL_HDR) to the | |
2706 | OUTPUT_BFD. */ | |
2707 | ||
2708 | bfd_boolean | |
268b6b39 AM |
2709 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2710 | asection *input_section, | |
2711 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2712 | Elf_Internal_Rela *internal_relocs, |
2713 | struct elf_link_hash_entry **rel_hash | |
2714 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2715 | { |
2716 | Elf_Internal_Rela *irela; | |
2717 | Elf_Internal_Rela *irelaend; | |
2718 | bfd_byte *erel; | |
d4730f92 | 2719 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2720 | asection *output_section; |
9c5bfbb7 | 2721 | const struct elf_backend_data *bed; |
268b6b39 | 2722 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2723 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2724 | |
2725 | output_section = input_section->output_section; | |
45d6a902 | 2726 | |
d4730f92 BS |
2727 | bed = get_elf_backend_data (output_bfd); |
2728 | esdo = elf_section_data (output_section); | |
2729 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2730 | { |
d4730f92 BS |
2731 | output_reldata = &esdo->rel; |
2732 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2733 | } |
d4730f92 BS |
2734 | else if (esdo->rela.hdr |
2735 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2736 | { |
d4730f92 BS |
2737 | output_reldata = &esdo->rela; |
2738 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2739 | } |
2740 | else | |
2741 | { | |
4eca0228 | 2742 | _bfd_error_handler |
695344c0 | 2743 | /* xgettext:c-format */ |
871b3ab2 | 2744 | (_("%pB: relocation size mismatch in %pB section %pA"), |
d003868e | 2745 | output_bfd, input_section->owner, input_section); |
297d8443 | 2746 | bfd_set_error (bfd_error_wrong_format); |
45d6a902 AM |
2747 | return FALSE; |
2748 | } | |
2749 | ||
d4730f92 BS |
2750 | erel = output_reldata->hdr->contents; |
2751 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2752 | irela = internal_relocs; |
2753 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2754 | * bed->s->int_rels_per_ext_rel); | |
2755 | while (irela < irelaend) | |
2756 | { | |
2757 | (*swap_out) (output_bfd, irela, erel); | |
2758 | irela += bed->s->int_rels_per_ext_rel; | |
2759 | erel += input_rel_hdr->sh_entsize; | |
2760 | } | |
2761 | ||
2762 | /* Bump the counter, so that we know where to add the next set of | |
2763 | relocations. */ | |
d4730f92 | 2764 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 AM |
2765 | |
2766 | return TRUE; | |
2767 | } | |
2768 | \f | |
508c3946 L |
2769 | /* Make weak undefined symbols in PIE dynamic. */ |
2770 | ||
2771 | bfd_boolean | |
2772 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, | |
2773 | struct elf_link_hash_entry *h) | |
2774 | { | |
0e1862bb | 2775 | if (bfd_link_pie (info) |
508c3946 L |
2776 | && h->dynindx == -1 |
2777 | && h->root.type == bfd_link_hash_undefweak) | |
2778 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2779 | ||
2780 | return TRUE; | |
2781 | } | |
2782 | ||
45d6a902 AM |
2783 | /* Fix up the flags for a symbol. This handles various cases which |
2784 | can only be fixed after all the input files are seen. This is | |
2785 | currently called by both adjust_dynamic_symbol and | |
2786 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2787 | the face of future changes. */ | |
2788 | ||
28caa186 | 2789 | static bfd_boolean |
268b6b39 AM |
2790 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2791 | struct elf_info_failed *eif) | |
45d6a902 | 2792 | { |
33774f08 | 2793 | const struct elf_backend_data *bed; |
508c3946 | 2794 | |
45d6a902 AM |
2795 | /* If this symbol was mentioned in a non-ELF file, try to set |
2796 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2797 | permit a non-ELF file to correctly refer to a symbol defined in | |
2798 | an ELF dynamic object. */ | |
f5385ebf | 2799 | if (h->non_elf) |
45d6a902 AM |
2800 | { |
2801 | while (h->root.type == bfd_link_hash_indirect) | |
2802 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2803 | ||
2804 | if (h->root.type != bfd_link_hash_defined | |
2805 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2806 | { |
2807 | h->ref_regular = 1; | |
2808 | h->ref_regular_nonweak = 1; | |
2809 | } | |
45d6a902 AM |
2810 | else |
2811 | { | |
2812 | if (h->root.u.def.section->owner != NULL | |
2813 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2814 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2815 | { |
2816 | h->ref_regular = 1; | |
2817 | h->ref_regular_nonweak = 1; | |
2818 | } | |
45d6a902 | 2819 | else |
f5385ebf | 2820 | h->def_regular = 1; |
45d6a902 AM |
2821 | } |
2822 | ||
2823 | if (h->dynindx == -1 | |
f5385ebf AM |
2824 | && (h->def_dynamic |
2825 | || h->ref_dynamic)) | |
45d6a902 | 2826 | { |
c152c796 | 2827 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2828 | { |
2829 | eif->failed = TRUE; | |
2830 | return FALSE; | |
2831 | } | |
2832 | } | |
2833 | } | |
2834 | else | |
2835 | { | |
f5385ebf | 2836 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2837 | was first seen in a non-ELF file. Fortunately, if the symbol |
2838 | was first seen in an ELF file, we're probably OK unless the | |
2839 | symbol was defined in a non-ELF file. Catch that case here. | |
2840 | FIXME: We're still in trouble if the symbol was first seen in | |
2841 | a dynamic object, and then later in a non-ELF regular object. */ | |
2842 | if ((h->root.type == bfd_link_hash_defined | |
2843 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2844 | && !h->def_regular |
45d6a902 AM |
2845 | && (h->root.u.def.section->owner != NULL |
2846 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2847 | != bfd_target_elf_flavour) | |
2848 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2849 | && !h->def_dynamic))) |
2850 | h->def_regular = 1; | |
45d6a902 AM |
2851 | } |
2852 | ||
508c3946 | 2853 | /* Backend specific symbol fixup. */ |
33774f08 AM |
2854 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2855 | if (bed->elf_backend_fixup_symbol | |
2856 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
2857 | return FALSE; | |
508c3946 | 2858 | |
45d6a902 AM |
2859 | /* If this is a final link, and the symbol was defined as a common |
2860 | symbol in a regular object file, and there was no definition in | |
2861 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2862 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2863 | flag will not have been set. */ |
2864 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2865 | && !h->def_regular |
2866 | && h->ref_regular | |
2867 | && !h->def_dynamic | |
96f29d96 | 2868 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 2869 | h->def_regular = 1; |
45d6a902 | 2870 | |
af0bfb9c AM |
2871 | /* Symbols defined in discarded sections shouldn't be dynamic. */ |
2872 | if (h->root.type == bfd_link_hash_undefined && h->indx == -3) | |
2873 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2874 | ||
4deb8f71 L |
2875 | /* If a weak undefined symbol has non-default visibility, we also |
2876 | hide it from the dynamic linker. */ | |
af0bfb9c AM |
2877 | else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
2878 | && h->root.type == bfd_link_hash_undefweak) | |
4deb8f71 L |
2879 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
2880 | ||
2881 | /* A hidden versioned symbol in executable should be forced local if | |
2882 | it is is locally defined, not referenced by shared library and not | |
2883 | exported. */ | |
2884 | else if (bfd_link_executable (eif->info) | |
2885 | && h->versioned == versioned_hidden | |
2886 | && !eif->info->export_dynamic | |
2887 | && !h->dynamic | |
2888 | && !h->ref_dynamic | |
2889 | && h->def_regular) | |
2890 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2891 | ||
45d6a902 AM |
2892 | /* If -Bsymbolic was used (which means to bind references to global |
2893 | symbols to the definition within the shared object), and this | |
2894 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2895 | need a PLT entry. Likewise, if the symbol has non-default |
2896 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2897 | will force it local. */ |
4deb8f71 L |
2898 | else if (h->needs_plt |
2899 | && bfd_link_pic (eif->info) | |
2900 | && is_elf_hash_table (eif->info->hash) | |
2901 | && (SYMBOLIC_BIND (eif->info, h) | |
2902 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
2903 | && h->def_regular) | |
45d6a902 | 2904 | { |
45d6a902 AM |
2905 | bfd_boolean force_local; |
2906 | ||
45d6a902 AM |
2907 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
2908 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2909 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2910 | } | |
2911 | ||
45d6a902 AM |
2912 | /* If this is a weak defined symbol in a dynamic object, and we know |
2913 | the real definition in the dynamic object, copy interesting flags | |
2914 | over to the real definition. */ | |
60d67dc8 | 2915 | if (h->is_weakalias) |
45d6a902 | 2916 | { |
60d67dc8 AM |
2917 | struct elf_link_hash_entry *def = weakdef (h); |
2918 | ||
45d6a902 AM |
2919 | /* If the real definition is defined by a regular object file, |
2920 | don't do anything special. See the longer description in | |
2921 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
60d67dc8 AM |
2922 | if (def->def_regular) |
2923 | { | |
2924 | h = def; | |
2925 | while ((h = h->u.alias) != def) | |
2926 | h->is_weakalias = 0; | |
2927 | } | |
45d6a902 | 2928 | else |
a26587ba | 2929 | { |
4e6b54a6 AM |
2930 | while (h->root.type == bfd_link_hash_indirect) |
2931 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4e6b54a6 AM |
2932 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
2933 | || h->root.type == bfd_link_hash_defweak); | |
60d67dc8 AM |
2934 | BFD_ASSERT (def->def_dynamic); |
2935 | BFD_ASSERT (def->root.type == bfd_link_hash_defined); | |
2936 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); | |
a26587ba | 2937 | } |
45d6a902 AM |
2938 | } |
2939 | ||
2940 | return TRUE; | |
2941 | } | |
2942 | ||
2943 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2944 | called via elf_link_hash_traverse, and also calls itself | |
2945 | recursively. */ | |
2946 | ||
28caa186 | 2947 | static bfd_boolean |
268b6b39 | 2948 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2949 | { |
a50b1753 | 2950 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
559192d8 | 2951 | struct elf_link_hash_table *htab; |
9c5bfbb7 | 2952 | const struct elf_backend_data *bed; |
45d6a902 | 2953 | |
0eddce27 | 2954 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2955 | return FALSE; |
2956 | ||
45d6a902 AM |
2957 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2958 | if (h->root.type == bfd_link_hash_indirect) | |
2959 | return TRUE; | |
2960 | ||
2961 | /* Fix the symbol flags. */ | |
2962 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2963 | return FALSE; | |
2964 | ||
559192d8 AM |
2965 | htab = elf_hash_table (eif->info); |
2966 | bed = get_elf_backend_data (htab->dynobj); | |
2967 | ||
954b63d4 AM |
2968 | if (h->root.type == bfd_link_hash_undefweak) |
2969 | { | |
2970 | if (eif->info->dynamic_undefined_weak == 0) | |
559192d8 | 2971 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
954b63d4 AM |
2972 | else if (eif->info->dynamic_undefined_weak > 0 |
2973 | && h->ref_regular | |
2974 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2975 | && !bfd_hide_sym_by_version (eif->info->version_info, | |
2976 | h->root.root.string)) | |
2977 | { | |
2978 | if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2979 | { | |
2980 | eif->failed = TRUE; | |
2981 | return FALSE; | |
2982 | } | |
2983 | } | |
2984 | } | |
2985 | ||
45d6a902 AM |
2986 | /* If this symbol does not require a PLT entry, and it is not |
2987 | defined by a dynamic object, or is not referenced by a regular | |
2988 | object, ignore it. We do have to handle a weak defined symbol, | |
2989 | even if no regular object refers to it, if we decided to add it | |
2990 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2991 | about symbols which are defined by one dynamic object and | |
2992 | referenced by another one? */ | |
f5385ebf | 2993 | if (!h->needs_plt |
91e21fb7 | 2994 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
2995 | && (h->def_regular |
2996 | || !h->def_dynamic | |
2997 | || (!h->ref_regular | |
60d67dc8 | 2998 | && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) |
45d6a902 | 2999 | { |
a6aa5195 | 3000 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
3001 | return TRUE; |
3002 | } | |
3003 | ||
3004 | /* If we've already adjusted this symbol, don't do it again. This | |
3005 | can happen via a recursive call. */ | |
f5385ebf | 3006 | if (h->dynamic_adjusted) |
45d6a902 AM |
3007 | return TRUE; |
3008 | ||
3009 | /* Don't look at this symbol again. Note that we must set this | |
3010 | after checking the above conditions, because we may look at a | |
3011 | symbol once, decide not to do anything, and then get called | |
3012 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 3013 | h->dynamic_adjusted = 1; |
45d6a902 AM |
3014 | |
3015 | /* If this is a weak definition, and we know a real definition, and | |
3016 | the real symbol is not itself defined by a regular object file, | |
3017 | then get a good value for the real definition. We handle the | |
3018 | real symbol first, for the convenience of the backend routine. | |
3019 | ||
3020 | Note that there is a confusing case here. If the real definition | |
3021 | is defined by a regular object file, we don't get the real symbol | |
3022 | from the dynamic object, but we do get the weak symbol. If the | |
3023 | processor backend uses a COPY reloc, then if some routine in the | |
3024 | dynamic object changes the real symbol, we will not see that | |
3025 | change in the corresponding weak symbol. This is the way other | |
3026 | ELF linkers work as well, and seems to be a result of the shared | |
3027 | library model. | |
3028 | ||
3029 | I will clarify this issue. Most SVR4 shared libraries define the | |
3030 | variable _timezone and define timezone as a weak synonym. The | |
3031 | tzset call changes _timezone. If you write | |
3032 | extern int timezone; | |
3033 | int _timezone = 5; | |
3034 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
3035 | you might expect that, since timezone is a synonym for _timezone, | |
3036 | the same number will print both times. However, if the processor | |
3037 | backend uses a COPY reloc, then actually timezone will be copied | |
3038 | into your process image, and, since you define _timezone | |
3039 | yourself, _timezone will not. Thus timezone and _timezone will | |
3040 | wind up at different memory locations. The tzset call will set | |
3041 | _timezone, leaving timezone unchanged. */ | |
3042 | ||
60d67dc8 | 3043 | if (h->is_weakalias) |
45d6a902 | 3044 | { |
60d67dc8 AM |
3045 | struct elf_link_hash_entry *def = weakdef (h); |
3046 | ||
ec24dc88 | 3047 | /* If we get to this point, there is an implicit reference to |
60d67dc8 AM |
3048 | the alias by a regular object file via the weak symbol H. */ |
3049 | def->ref_regular = 1; | |
45d6a902 | 3050 | |
ec24dc88 | 3051 | /* Ensure that the backend adjust_dynamic_symbol function sees |
60d67dc8 AM |
3052 | the strong alias before H by recursively calling ourselves. */ |
3053 | if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) | |
45d6a902 AM |
3054 | return FALSE; |
3055 | } | |
3056 | ||
3057 | /* If a symbol has no type and no size and does not require a PLT | |
3058 | entry, then we are probably about to do the wrong thing here: we | |
3059 | are probably going to create a COPY reloc for an empty object. | |
3060 | This case can arise when a shared object is built with assembly | |
3061 | code, and the assembly code fails to set the symbol type. */ | |
3062 | if (h->size == 0 | |
3063 | && h->type == STT_NOTYPE | |
f5385ebf | 3064 | && !h->needs_plt) |
4eca0228 | 3065 | _bfd_error_handler |
45d6a902 AM |
3066 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
3067 | h->root.root.string); | |
3068 | ||
45d6a902 AM |
3069 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
3070 | { | |
3071 | eif->failed = TRUE; | |
3072 | return FALSE; | |
3073 | } | |
3074 | ||
3075 | return TRUE; | |
3076 | } | |
3077 | ||
027297b7 L |
3078 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
3079 | DYNBSS. */ | |
3080 | ||
3081 | bfd_boolean | |
6cabe1ea AM |
3082 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
3083 | struct elf_link_hash_entry *h, | |
027297b7 L |
3084 | asection *dynbss) |
3085 | { | |
91ac5911 | 3086 | unsigned int power_of_two; |
027297b7 L |
3087 | bfd_vma mask; |
3088 | asection *sec = h->root.u.def.section; | |
3089 | ||
de194d85 | 3090 | /* The section alignment of the definition is the maximum alignment |
91ac5911 L |
3091 | requirement of symbols defined in the section. Since we don't |
3092 | know the symbol alignment requirement, we start with the | |
3093 | maximum alignment and check low bits of the symbol address | |
3094 | for the minimum alignment. */ | |
3095 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
3096 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
3097 | while ((h->root.u.def.value & mask) != 0) | |
3098 | { | |
3099 | mask >>= 1; | |
3100 | --power_of_two; | |
3101 | } | |
027297b7 | 3102 | |
91ac5911 L |
3103 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
3104 | dynbss)) | |
027297b7 L |
3105 | { |
3106 | /* Adjust the section alignment if needed. */ | |
3107 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 3108 | power_of_two)) |
027297b7 L |
3109 | return FALSE; |
3110 | } | |
3111 | ||
91ac5911 | 3112 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
3113 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
3114 | ||
3115 | /* Define the symbol as being at this point in DYNBSS. */ | |
3116 | h->root.u.def.section = dynbss; | |
3117 | h->root.u.def.value = dynbss->size; | |
3118 | ||
3119 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
3120 | dynbss->size += h->size; | |
3121 | ||
f7483970 L |
3122 | /* No error if extern_protected_data is true. */ |
3123 | if (h->protected_def | |
889c2a67 L |
3124 | && (!info->extern_protected_data |
3125 | || (info->extern_protected_data < 0 | |
3126 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 | 3127 | info->callbacks->einfo |
c1c8c1ef | 3128 | (_("%P: copy reloc against protected `%pT' is dangerous\n"), |
d07a1b05 | 3129 | h->root.root.string); |
6cabe1ea | 3130 | |
027297b7 L |
3131 | return TRUE; |
3132 | } | |
3133 | ||
45d6a902 AM |
3134 | /* Adjust all external symbols pointing into SEC_MERGE sections |
3135 | to reflect the object merging within the sections. */ | |
3136 | ||
28caa186 | 3137 | static bfd_boolean |
268b6b39 | 3138 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
3139 | { |
3140 | asection *sec; | |
3141 | ||
45d6a902 AM |
3142 | if ((h->root.type == bfd_link_hash_defined |
3143 | || h->root.type == bfd_link_hash_defweak) | |
3144 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 3145 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 3146 | { |
a50b1753 | 3147 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
3148 | |
3149 | h->root.u.def.value = | |
3150 | _bfd_merged_section_offset (output_bfd, | |
3151 | &h->root.u.def.section, | |
3152 | elf_section_data (sec)->sec_info, | |
753731ee | 3153 | h->root.u.def.value); |
45d6a902 AM |
3154 | } |
3155 | ||
3156 | return TRUE; | |
3157 | } | |
986a241f RH |
3158 | |
3159 | /* Returns false if the symbol referred to by H should be considered | |
3160 | to resolve local to the current module, and true if it should be | |
3161 | considered to bind dynamically. */ | |
3162 | ||
3163 | bfd_boolean | |
268b6b39 AM |
3164 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
3165 | struct bfd_link_info *info, | |
89a2ee5a | 3166 | bfd_boolean not_local_protected) |
986a241f RH |
3167 | { |
3168 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
3169 | const struct elf_backend_data *bed; |
3170 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
3171 | |
3172 | if (h == NULL) | |
3173 | return FALSE; | |
3174 | ||
3175 | while (h->root.type == bfd_link_hash_indirect | |
3176 | || h->root.type == bfd_link_hash_warning) | |
3177 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3178 | ||
3179 | /* If it was forced local, then clearly it's not dynamic. */ | |
3180 | if (h->dynindx == -1) | |
3181 | return FALSE; | |
f5385ebf | 3182 | if (h->forced_local) |
986a241f RH |
3183 | return FALSE; |
3184 | ||
3185 | /* Identify the cases where name binding rules say that a | |
3186 | visible symbol resolves locally. */ | |
0e1862bb L |
3187 | binding_stays_local_p = (bfd_link_executable (info) |
3188 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
3189 | |
3190 | switch (ELF_ST_VISIBILITY (h->other)) | |
3191 | { | |
3192 | case STV_INTERNAL: | |
3193 | case STV_HIDDEN: | |
3194 | return FALSE; | |
3195 | ||
3196 | case STV_PROTECTED: | |
fcb93ecf PB |
3197 | hash_table = elf_hash_table (info); |
3198 | if (!is_elf_hash_table (hash_table)) | |
3199 | return FALSE; | |
3200 | ||
3201 | bed = get_elf_backend_data (hash_table->dynobj); | |
3202 | ||
986a241f RH |
3203 | /* Proper resolution for function pointer equality may require |
3204 | that these symbols perhaps be resolved dynamically, even though | |
3205 | we should be resolving them to the current module. */ | |
89a2ee5a | 3206 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
3207 | binding_stays_local_p = TRUE; |
3208 | break; | |
3209 | ||
3210 | default: | |
986a241f RH |
3211 | break; |
3212 | } | |
3213 | ||
aa37626c | 3214 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 3215 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
3216 | return TRUE; |
3217 | ||
986a241f RH |
3218 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
3219 | us that it remains local. */ | |
3220 | return !binding_stays_local_p; | |
3221 | } | |
f6c52c13 AM |
3222 | |
3223 | /* Return true if the symbol referred to by H should be considered | |
3224 | to resolve local to the current module, and false otherwise. Differs | |
3225 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 3226 | undefined symbols. The two functions are virtually identical except |
0fad2956 MR |
3227 | for the place where dynindx == -1 is tested. If that test is true, |
3228 | _bfd_elf_dynamic_symbol_p will say the symbol is local, while | |
3229 | _bfd_elf_symbol_refs_local_p will say the symbol is local only for | |
3230 | defined symbols. | |
89a2ee5a AM |
3231 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as |
3232 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
3233 | treatment of undefined weak symbols. For those that do not make | |
3234 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
3235 | |
3236 | bfd_boolean | |
268b6b39 AM |
3237 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3238 | struct bfd_link_info *info, | |
3239 | bfd_boolean local_protected) | |
f6c52c13 | 3240 | { |
fcb93ecf PB |
3241 | const struct elf_backend_data *bed; |
3242 | struct elf_link_hash_table *hash_table; | |
3243 | ||
f6c52c13 AM |
3244 | /* If it's a local sym, of course we resolve locally. */ |
3245 | if (h == NULL) | |
3246 | return TRUE; | |
3247 | ||
d95edcac L |
3248 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3249 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3250 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3251 | return TRUE; | |
3252 | ||
0fad2956 MR |
3253 | /* Forced local symbols resolve locally. */ |
3254 | if (h->forced_local) | |
3255 | return TRUE; | |
3256 | ||
7e2294f9 AO |
3257 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3258 | flag set, so test it first, and don't bail out. */ | |
3259 | if (ELF_COMMON_DEF_P (h)) | |
3260 | /* Do nothing. */; | |
f6c52c13 | 3261 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3262 | resolve locally. The sym is either undefined or dynamic. */ |
3263 | else if (!h->def_regular) | |
f6c52c13 AM |
3264 | return FALSE; |
3265 | ||
0fad2956 | 3266 | /* Non-dynamic symbols resolve locally. */ |
f6c52c13 AM |
3267 | if (h->dynindx == -1) |
3268 | return TRUE; | |
3269 | ||
3270 | /* At this point, we know the symbol is defined and dynamic. In an | |
3271 | executable it must resolve locally, likewise when building symbolic | |
3272 | shared libraries. */ | |
0e1862bb | 3273 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3274 | return TRUE; |
3275 | ||
3276 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3277 | with default visibility might not resolve locally. */ | |
3278 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3279 | return FALSE; | |
3280 | ||
fcb93ecf PB |
3281 | hash_table = elf_hash_table (info); |
3282 | if (!is_elf_hash_table (hash_table)) | |
3283 | return TRUE; | |
3284 | ||
3285 | bed = get_elf_backend_data (hash_table->dynobj); | |
3286 | ||
f7483970 L |
3287 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3288 | symbols are local. */ | |
889c2a67 L |
3289 | if ((!info->extern_protected_data |
3290 | || (info->extern_protected_data < 0 | |
3291 | && !bed->extern_protected_data)) | |
3292 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3293 | return TRUE; |
3294 | ||
f6c52c13 | 3295 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3296 | symbols be treated as dynamic symbols. If the address of a |
3297 | function not defined in an executable is set to that function's | |
3298 | plt entry in the executable, then the address of the function in | |
3299 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3300 | return local_protected; |
3301 | } | |
e1918d23 AM |
3302 | |
3303 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3304 | aligned. Returns the first TLS output section. */ | |
3305 | ||
3306 | struct bfd_section * | |
3307 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3308 | { | |
3309 | struct bfd_section *sec, *tls; | |
3310 | unsigned int align = 0; | |
3311 | ||
3312 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3313 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3314 | break; | |
3315 | tls = sec; | |
3316 | ||
3317 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3318 | if (sec->alignment_power > align) | |
3319 | align = sec->alignment_power; | |
3320 | ||
3321 | elf_hash_table (info)->tls_sec = tls; | |
3322 | ||
3323 | /* Ensure the alignment of the first section is the largest alignment, | |
3324 | so that the tls segment starts aligned. */ | |
3325 | if (tls != NULL) | |
3326 | tls->alignment_power = align; | |
3327 | ||
3328 | return tls; | |
3329 | } | |
0ad989f9 L |
3330 | |
3331 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3332 | static bfd_boolean | |
3333 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3334 | Elf_Internal_Sym *sym) | |
3335 | { | |
a4d8e49b L |
3336 | const struct elf_backend_data *bed; |
3337 | ||
0ad989f9 L |
3338 | /* Local symbols do not count, but target specific ones might. */ |
3339 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3340 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3341 | return FALSE; | |
3342 | ||
fcb93ecf | 3343 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3344 | /* Function symbols do not count. */ |
fcb93ecf | 3345 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3346 | return FALSE; |
3347 | ||
3348 | /* If the section is undefined, then so is the symbol. */ | |
3349 | if (sym->st_shndx == SHN_UNDEF) | |
3350 | return FALSE; | |
3351 | ||
3352 | /* If the symbol is defined in the common section, then | |
3353 | it is a common definition and so does not count. */ | |
a4d8e49b | 3354 | if (bed->common_definition (sym)) |
0ad989f9 L |
3355 | return FALSE; |
3356 | ||
3357 | /* If the symbol is in a target specific section then we | |
3358 | must rely upon the backend to tell us what it is. */ | |
3359 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3360 | /* FIXME - this function is not coded yet: | |
3361 | ||
3362 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3363 | ||
3364 | Instead for now assume that the definition is not global, | |
3365 | Even if this is wrong, at least the linker will behave | |
3366 | in the same way that it used to do. */ | |
3367 | return FALSE; | |
3368 | ||
3369 | return TRUE; | |
3370 | } | |
3371 | ||
3372 | /* Search the symbol table of the archive element of the archive ABFD | |
3373 | whose archive map contains a mention of SYMDEF, and determine if | |
3374 | the symbol is defined in this element. */ | |
3375 | static bfd_boolean | |
3376 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3377 | { | |
3378 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3379 | size_t symcount; |
3380 | size_t extsymcount; | |
3381 | size_t extsymoff; | |
0ad989f9 L |
3382 | Elf_Internal_Sym *isymbuf; |
3383 | Elf_Internal_Sym *isym; | |
3384 | Elf_Internal_Sym *isymend; | |
3385 | bfd_boolean result; | |
3386 | ||
3387 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3388 | if (abfd == NULL) | |
3389 | return FALSE; | |
3390 | ||
3391 | if (! bfd_check_format (abfd, bfd_object)) | |
3392 | return FALSE; | |
3393 | ||
7dc3990e L |
3394 | /* Select the appropriate symbol table. If we don't know if the |
3395 | object file is an IR object, give linker LTO plugin a chance to | |
3396 | get the correct symbol table. */ | |
3397 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3398 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3399 | || (abfd->plugin_format == bfd_plugin_unknown |
3400 | && bfd_link_plugin_object_p (abfd)) | |
3401 | #endif | |
3402 | ) | |
3403 | { | |
3404 | /* Use the IR symbol table if the object has been claimed by | |
3405 | plugin. */ | |
3406 | abfd = abfd->plugin_dummy_bfd; | |
3407 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3408 | } | |
3409 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3410 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3411 | else | |
3412 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3413 | ||
3414 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3415 | ||
3416 | /* The sh_info field of the symtab header tells us where the | |
3417 | external symbols start. We don't care about the local symbols. */ | |
3418 | if (elf_bad_symtab (abfd)) | |
3419 | { | |
3420 | extsymcount = symcount; | |
3421 | extsymoff = 0; | |
3422 | } | |
3423 | else | |
3424 | { | |
3425 | extsymcount = symcount - hdr->sh_info; | |
3426 | extsymoff = hdr->sh_info; | |
3427 | } | |
3428 | ||
3429 | if (extsymcount == 0) | |
3430 | return FALSE; | |
3431 | ||
3432 | /* Read in the symbol table. */ | |
3433 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3434 | NULL, NULL, NULL); | |
3435 | if (isymbuf == NULL) | |
3436 | return FALSE; | |
3437 | ||
3438 | /* Scan the symbol table looking for SYMDEF. */ | |
3439 | result = FALSE; | |
3440 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3441 | { | |
3442 | const char *name; | |
3443 | ||
3444 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3445 | isym->st_name); | |
3446 | if (name == NULL) | |
3447 | break; | |
3448 | ||
3449 | if (strcmp (name, symdef->name) == 0) | |
3450 | { | |
3451 | result = is_global_data_symbol_definition (abfd, isym); | |
3452 | break; | |
3453 | } | |
3454 | } | |
3455 | ||
3456 | free (isymbuf); | |
3457 | ||
3458 | return result; | |
3459 | } | |
3460 | \f | |
5a580b3a AM |
3461 | /* Add an entry to the .dynamic table. */ |
3462 | ||
3463 | bfd_boolean | |
3464 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3465 | bfd_vma tag, | |
3466 | bfd_vma val) | |
3467 | { | |
3468 | struct elf_link_hash_table *hash_table; | |
3469 | const struct elf_backend_data *bed; | |
3470 | asection *s; | |
3471 | bfd_size_type newsize; | |
3472 | bfd_byte *newcontents; | |
3473 | Elf_Internal_Dyn dyn; | |
3474 | ||
3475 | hash_table = elf_hash_table (info); | |
3476 | if (! is_elf_hash_table (hash_table)) | |
3477 | return FALSE; | |
3478 | ||
7f923b7f AM |
3479 | if (tag == DT_RELA || tag == DT_REL) |
3480 | hash_table->dynamic_relocs = TRUE; | |
3481 | ||
5a580b3a | 3482 | bed = get_elf_backend_data (hash_table->dynobj); |
3d4d4302 | 3483 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3484 | BFD_ASSERT (s != NULL); |
3485 | ||
eea6121a | 3486 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3487 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3488 | if (newcontents == NULL) |
3489 | return FALSE; | |
3490 | ||
3491 | dyn.d_tag = tag; | |
3492 | dyn.d_un.d_val = val; | |
eea6121a | 3493 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3494 | |
eea6121a | 3495 | s->size = newsize; |
5a580b3a AM |
3496 | s->contents = newcontents; |
3497 | ||
3498 | return TRUE; | |
3499 | } | |
3500 | ||
3501 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3502 | otherwise just check whether one already exists. Returns -1 on error, | |
3503 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3504 | ||
4ad4eba5 | 3505 | static int |
7e9f0867 AM |
3506 | elf_add_dt_needed_tag (bfd *abfd, |
3507 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3508 | const char *soname, |
3509 | bfd_boolean do_it) | |
5a580b3a AM |
3510 | { |
3511 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3512 | size_t strindex; |
5a580b3a | 3513 | |
7e9f0867 AM |
3514 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3515 | return -1; | |
3516 | ||
5a580b3a | 3517 | hash_table = elf_hash_table (info); |
5a580b3a | 3518 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3519 | if (strindex == (size_t) -1) |
5a580b3a AM |
3520 | return -1; |
3521 | ||
02be4619 | 3522 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3523 | { |
3524 | asection *sdyn; | |
3525 | const struct elf_backend_data *bed; | |
3526 | bfd_byte *extdyn; | |
3527 | ||
3528 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3529 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3530 | if (sdyn != NULL) |
3531 | for (extdyn = sdyn->contents; | |
3532 | extdyn < sdyn->contents + sdyn->size; | |
3533 | extdyn += bed->s->sizeof_dyn) | |
3534 | { | |
3535 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3536 | |
7e9f0867 AM |
3537 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3538 | if (dyn.d_tag == DT_NEEDED | |
3539 | && dyn.d_un.d_val == strindex) | |
3540 | { | |
3541 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3542 | return 1; | |
3543 | } | |
3544 | } | |
5a580b3a AM |
3545 | } |
3546 | ||
3547 | if (do_it) | |
3548 | { | |
7e9f0867 AM |
3549 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3550 | return -1; | |
3551 | ||
5a580b3a AM |
3552 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3553 | return -1; | |
3554 | } | |
3555 | else | |
3556 | /* We were just checking for existence of the tag. */ | |
3557 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3558 | ||
3559 | return 0; | |
3560 | } | |
3561 | ||
7b15fa7a AM |
3562 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3563 | (or the end of list if STOP is NULL), and needed by a library that | |
3564 | will be loaded. */ | |
3565 | ||
010e5ae2 | 3566 | static bfd_boolean |
7b15fa7a AM |
3567 | on_needed_list (const char *soname, |
3568 | struct bfd_link_needed_list *needed, | |
3569 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3570 | { |
7b15fa7a AM |
3571 | struct bfd_link_needed_list *look; |
3572 | for (look = needed; look != stop; look = look->next) | |
3573 | if (strcmp (soname, look->name) == 0 | |
3574 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3575 | /* If needed by a library that itself is not directly | |
3576 | needed, recursively check whether that library is | |
3577 | indirectly needed. Since we add DT_NEEDED entries to | |
3578 | the end of the list, library dependencies appear after | |
3579 | the library. Therefore search prior to the current | |
3580 | LOOK, preventing possible infinite recursion. */ | |
3581 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3582 | return TRUE; |
3583 | ||
3584 | return FALSE; | |
3585 | } | |
3586 | ||
14160578 | 3587 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3588 | static int |
3589 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3590 | { |
3591 | const struct elf_link_hash_entry *h1; | |
3592 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3593 | bfd_signed_vma vdiff; |
5a580b3a AM |
3594 | |
3595 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3596 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3597 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3598 | if (vdiff != 0) | |
3599 | return vdiff > 0 ? 1 : -1; | |
3600 | else | |
3601 | { | |
d3435ae8 | 3602 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3603 | if (sdiff != 0) |
3604 | return sdiff > 0 ? 1 : -1; | |
3605 | } | |
14160578 AM |
3606 | vdiff = h1->size - h2->size; |
3607 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3608 | } |
4ad4eba5 | 3609 | |
5a580b3a AM |
3610 | /* This function is used to adjust offsets into .dynstr for |
3611 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3612 | ||
3613 | static bfd_boolean | |
3614 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3615 | { | |
a50b1753 | 3616 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3617 | |
5a580b3a AM |
3618 | if (h->dynindx != -1) |
3619 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3620 | return TRUE; | |
3621 | } | |
3622 | ||
3623 | /* Assign string offsets in .dynstr, update all structures referencing | |
3624 | them. */ | |
3625 | ||
4ad4eba5 AM |
3626 | static bfd_boolean |
3627 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3628 | { |
3629 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3630 | struct elf_link_local_dynamic_entry *entry; | |
3631 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3632 | bfd *dynobj = hash_table->dynobj; | |
3633 | asection *sdyn; | |
3634 | bfd_size_type size; | |
3635 | const struct elf_backend_data *bed; | |
3636 | bfd_byte *extdyn; | |
3637 | ||
3638 | _bfd_elf_strtab_finalize (dynstr); | |
3639 | size = _bfd_elf_strtab_size (dynstr); | |
3640 | ||
3641 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3642 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3643 | BFD_ASSERT (sdyn != NULL); |
3644 | ||
3645 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3646 | for (extdyn = sdyn->contents; | |
eea6121a | 3647 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3648 | extdyn += bed->s->sizeof_dyn) |
3649 | { | |
3650 | Elf_Internal_Dyn dyn; | |
3651 | ||
3652 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3653 | switch (dyn.d_tag) | |
3654 | { | |
3655 | case DT_STRSZ: | |
3656 | dyn.d_un.d_val = size; | |
3657 | break; | |
3658 | case DT_NEEDED: | |
3659 | case DT_SONAME: | |
3660 | case DT_RPATH: | |
3661 | case DT_RUNPATH: | |
3662 | case DT_FILTER: | |
3663 | case DT_AUXILIARY: | |
7ee314fa AM |
3664 | case DT_AUDIT: |
3665 | case DT_DEPAUDIT: | |
5a580b3a AM |
3666 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3667 | break; | |
3668 | default: | |
3669 | continue; | |
3670 | } | |
3671 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3672 | } | |
3673 | ||
3674 | /* Now update local dynamic symbols. */ | |
3675 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3676 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3677 | entry->isym.st_name); | |
3678 | ||
3679 | /* And the rest of dynamic symbols. */ | |
3680 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3681 | ||
3682 | /* Adjust version definitions. */ | |
3683 | if (elf_tdata (output_bfd)->cverdefs) | |
3684 | { | |
3685 | asection *s; | |
3686 | bfd_byte *p; | |
ef53be89 | 3687 | size_t i; |
5a580b3a AM |
3688 | Elf_Internal_Verdef def; |
3689 | Elf_Internal_Verdaux defaux; | |
3690 | ||
3d4d4302 | 3691 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3692 | p = s->contents; |
3693 | do | |
3694 | { | |
3695 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3696 | &def); | |
3697 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3698 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3699 | continue; | |
5a580b3a AM |
3700 | for (i = 0; i < def.vd_cnt; ++i) |
3701 | { | |
3702 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3703 | (Elf_External_Verdaux *) p, &defaux); | |
3704 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3705 | defaux.vda_name); | |
3706 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3707 | &defaux, (Elf_External_Verdaux *) p); | |
3708 | p += sizeof (Elf_External_Verdaux); | |
3709 | } | |
3710 | } | |
3711 | while (def.vd_next); | |
3712 | } | |
3713 | ||
3714 | /* Adjust version references. */ | |
3715 | if (elf_tdata (output_bfd)->verref) | |
3716 | { | |
3717 | asection *s; | |
3718 | bfd_byte *p; | |
ef53be89 | 3719 | size_t i; |
5a580b3a AM |
3720 | Elf_Internal_Verneed need; |
3721 | Elf_Internal_Vernaux needaux; | |
3722 | ||
3d4d4302 | 3723 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3724 | p = s->contents; |
3725 | do | |
3726 | { | |
3727 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3728 | &need); | |
3729 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3730 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3731 | (Elf_External_Verneed *) p); | |
3732 | p += sizeof (Elf_External_Verneed); | |
3733 | for (i = 0; i < need.vn_cnt; ++i) | |
3734 | { | |
3735 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3736 | (Elf_External_Vernaux *) p, &needaux); | |
3737 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3738 | needaux.vna_name); | |
3739 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3740 | &needaux, | |
3741 | (Elf_External_Vernaux *) p); | |
3742 | p += sizeof (Elf_External_Vernaux); | |
3743 | } | |
3744 | } | |
3745 | while (need.vn_next); | |
3746 | } | |
3747 | ||
3748 | return TRUE; | |
3749 | } | |
3750 | \f | |
13285a1b AM |
3751 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3752 | The default is to only match when the INPUT and OUTPUT are exactly | |
3753 | the same target. */ | |
3754 | ||
3755 | bfd_boolean | |
3756 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3757 | const bfd_target *output) | |
3758 | { | |
3759 | return input == output; | |
3760 | } | |
3761 | ||
3762 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3763 | This version is used when different targets for the same architecture | |
3764 | are virtually identical. */ | |
3765 | ||
3766 | bfd_boolean | |
3767 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3768 | const bfd_target *output) | |
3769 | { | |
3770 | const struct elf_backend_data *obed, *ibed; | |
3771 | ||
3772 | if (input == output) | |
3773 | return TRUE; | |
3774 | ||
3775 | ibed = xvec_get_elf_backend_data (input); | |
3776 | obed = xvec_get_elf_backend_data (output); | |
3777 | ||
3778 | if (ibed->arch != obed->arch) | |
3779 | return FALSE; | |
3780 | ||
3781 | /* If both backends are using this function, deem them compatible. */ | |
3782 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3783 | } | |
3784 | ||
e5034e59 AM |
3785 | /* Make a special call to the linker "notice" function to tell it that |
3786 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3787 | processing the lib. */ |
e5034e59 AM |
3788 | |
3789 | bfd_boolean | |
3790 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3791 | struct bfd_link_info *info, | |
3792 | enum notice_asneeded_action act) | |
3793 | { | |
46135103 | 3794 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3795 | } |
3796 | ||
d9689752 L |
3797 | /* Check relocations an ELF object file. */ |
3798 | ||
3799 | bfd_boolean | |
3800 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3801 | { | |
3802 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3803 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3804 | ||
3805 | /* If this object is the same format as the output object, and it is | |
3806 | not a shared library, then let the backend look through the | |
3807 | relocs. | |
3808 | ||
3809 | This is required to build global offset table entries and to | |
3810 | arrange for dynamic relocs. It is not required for the | |
3811 | particular common case of linking non PIC code, even when linking | |
3812 | against shared libraries, but unfortunately there is no way of | |
3813 | knowing whether an object file has been compiled PIC or not. | |
3814 | Looking through the relocs is not particularly time consuming. | |
3815 | The problem is that we must either (1) keep the relocs in memory, | |
3816 | which causes the linker to require additional runtime memory or | |
3817 | (2) read the relocs twice from the input file, which wastes time. | |
3818 | This would be a good case for using mmap. | |
3819 | ||
3820 | I have no idea how to handle linking PIC code into a file of a | |
3821 | different format. It probably can't be done. */ | |
3822 | if ((abfd->flags & DYNAMIC) == 0 | |
3823 | && is_elf_hash_table (htab) | |
3824 | && bed->check_relocs != NULL | |
3825 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3826 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3827 | { | |
3828 | asection *o; | |
3829 | ||
3830 | for (o = abfd->sections; o != NULL; o = o->next) | |
3831 | { | |
3832 | Elf_Internal_Rela *internal_relocs; | |
3833 | bfd_boolean ok; | |
3834 | ||
5ce03cea | 3835 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3836 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3837 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3838 | || o->reloc_count == 0 |
3839 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3840 | && (o->flags & SEC_DEBUGGING) != 0) | |
3841 | || bfd_is_abs_section (o->output_section)) | |
3842 | continue; | |
3843 | ||
3844 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3845 | info->keep_memory); | |
3846 | if (internal_relocs == NULL) | |
3847 | return FALSE; | |
3848 | ||
3849 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3850 | ||
3851 | if (elf_section_data (o)->relocs != internal_relocs) | |
3852 | free (internal_relocs); | |
3853 | ||
3854 | if (! ok) | |
3855 | return FALSE; | |
3856 | } | |
3857 | } | |
3858 | ||
3859 | return TRUE; | |
3860 | } | |
3861 | ||
4ad4eba5 AM |
3862 | /* Add symbols from an ELF object file to the linker hash table. */ |
3863 | ||
3864 | static bfd_boolean | |
3865 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3866 | { | |
a0c402a5 | 3867 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3868 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3869 | size_t symcount; |
3870 | size_t extsymcount; | |
3871 | size_t extsymoff; | |
4ad4eba5 AM |
3872 | struct elf_link_hash_entry **sym_hash; |
3873 | bfd_boolean dynamic; | |
3874 | Elf_External_Versym *extversym = NULL; | |
3875 | Elf_External_Versym *ever; | |
3876 | struct elf_link_hash_entry *weaks; | |
3877 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3878 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3879 | Elf_Internal_Sym *isymbuf = NULL; |
3880 | Elf_Internal_Sym *isym; | |
3881 | Elf_Internal_Sym *isymend; | |
3882 | const struct elf_backend_data *bed; | |
3883 | bfd_boolean add_needed; | |
66eb6687 | 3884 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3885 | bfd_size_type amt; |
66eb6687 | 3886 | void *alloc_mark = NULL; |
4f87808c AM |
3887 | struct bfd_hash_entry **old_table = NULL; |
3888 | unsigned int old_size = 0; | |
3889 | unsigned int old_count = 0; | |
66eb6687 | 3890 | void *old_tab = NULL; |
66eb6687 AM |
3891 | void *old_ent; |
3892 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3893 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3894 | void *old_strtab = NULL; |
66eb6687 | 3895 | size_t tabsize = 0; |
db6a5d5f | 3896 | asection *s; |
29a9f53e | 3897 | bfd_boolean just_syms; |
4ad4eba5 | 3898 | |
66eb6687 | 3899 | htab = elf_hash_table (info); |
4ad4eba5 | 3900 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3901 | |
3902 | if ((abfd->flags & DYNAMIC) == 0) | |
3903 | dynamic = FALSE; | |
3904 | else | |
3905 | { | |
3906 | dynamic = TRUE; | |
3907 | ||
3908 | /* You can't use -r against a dynamic object. Also, there's no | |
3909 | hope of using a dynamic object which does not exactly match | |
3910 | the format of the output file. */ | |
0e1862bb | 3911 | if (bfd_link_relocatable (info) |
66eb6687 | 3912 | || !is_elf_hash_table (htab) |
f13a99db | 3913 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3914 | { |
0e1862bb | 3915 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3916 | bfd_set_error (bfd_error_invalid_operation); |
3917 | else | |
3918 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3919 | goto error_return; |
3920 | } | |
3921 | } | |
3922 | ||
a0c402a5 L |
3923 | ehdr = elf_elfheader (abfd); |
3924 | if (info->warn_alternate_em | |
3925 | && bed->elf_machine_code != ehdr->e_machine | |
3926 | && ((bed->elf_machine_alt1 != 0 | |
3927 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3928 | || (bed->elf_machine_alt2 != 0 | |
3929 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
9793eb77 | 3930 | _bfd_error_handler |
695344c0 | 3931 | /* xgettext:c-format */ |
9793eb77 | 3932 | (_("alternate ELF machine code found (%d) in %pB, expecting %d"), |
a0c402a5 L |
3933 | ehdr->e_machine, abfd, bed->elf_machine_code); |
3934 | ||
4ad4eba5 AM |
3935 | /* As a GNU extension, any input sections which are named |
3936 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3937 | symbol. This differs from .gnu.warning sections, which generate | |
3938 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3939 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3940 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3941 | { |
db6a5d5f | 3942 | const char *name; |
4ad4eba5 | 3943 | |
db6a5d5f AM |
3944 | name = bfd_get_section_name (abfd, s); |
3945 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3946 | { |
db6a5d5f AM |
3947 | char *msg; |
3948 | bfd_size_type sz; | |
3949 | ||
3950 | name += sizeof ".gnu.warning." - 1; | |
3951 | ||
3952 | /* If this is a shared object, then look up the symbol | |
3953 | in the hash table. If it is there, and it is already | |
3954 | been defined, then we will not be using the entry | |
3955 | from this shared object, so we don't need to warn. | |
3956 | FIXME: If we see the definition in a regular object | |
3957 | later on, we will warn, but we shouldn't. The only | |
3958 | fix is to keep track of what warnings we are supposed | |
3959 | to emit, and then handle them all at the end of the | |
3960 | link. */ | |
3961 | if (dynamic) | |
4ad4eba5 | 3962 | { |
db6a5d5f AM |
3963 | struct elf_link_hash_entry *h; |
3964 | ||
3965 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3966 | ||
3967 | /* FIXME: What about bfd_link_hash_common? */ | |
3968 | if (h != NULL | |
3969 | && (h->root.type == bfd_link_hash_defined | |
3970 | || h->root.type == bfd_link_hash_defweak)) | |
3971 | continue; | |
3972 | } | |
4ad4eba5 | 3973 | |
db6a5d5f AM |
3974 | sz = s->size; |
3975 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3976 | if (msg == NULL) | |
3977 | goto error_return; | |
4ad4eba5 | 3978 | |
db6a5d5f AM |
3979 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3980 | goto error_return; | |
4ad4eba5 | 3981 | |
db6a5d5f | 3982 | msg[sz] = '\0'; |
4ad4eba5 | 3983 | |
db6a5d5f AM |
3984 | if (! (_bfd_generic_link_add_one_symbol |
3985 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3986 | FALSE, bed->collect, NULL))) | |
3987 | goto error_return; | |
4ad4eba5 | 3988 | |
0e1862bb | 3989 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3990 | { |
3991 | /* Clobber the section size so that the warning does | |
3992 | not get copied into the output file. */ | |
3993 | s->size = 0; | |
11d2f718 | 3994 | |
db6a5d5f AM |
3995 | /* Also set SEC_EXCLUDE, so that symbols defined in |
3996 | the warning section don't get copied to the output. */ | |
3997 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
3998 | } |
3999 | } | |
4000 | } | |
4001 | ||
29a9f53e L |
4002 | just_syms = ((s = abfd->sections) != NULL |
4003 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
4004 | ||
4ad4eba5 AM |
4005 | add_needed = TRUE; |
4006 | if (! dynamic) | |
4007 | { | |
4008 | /* If we are creating a shared library, create all the dynamic | |
4009 | sections immediately. We need to attach them to something, | |
4010 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
4011 | format and is not from ld --just-symbols. Always create the |
4012 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
4013 | are no input BFD's of the same format as the output, we can't |
4014 | make a shared library. */ | |
4015 | if (!just_syms | |
bf89386a | 4016 | && (bfd_link_pic (info) |
9c1d7a08 | 4017 | || (!bfd_link_relocatable (info) |
3c5fce9b | 4018 | && info->nointerp |
9c1d7a08 | 4019 | && (info->export_dynamic || info->dynamic))) |
66eb6687 | 4020 | && is_elf_hash_table (htab) |
f13a99db | 4021 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 4022 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
4023 | { |
4024 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
4025 | goto error_return; | |
4026 | } | |
4027 | } | |
66eb6687 | 4028 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
4029 | goto error_return; |
4030 | else | |
4031 | { | |
4ad4eba5 | 4032 | const char *soname = NULL; |
7ee314fa | 4033 | char *audit = NULL; |
4ad4eba5 | 4034 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
9acc85a6 | 4035 | const Elf_Internal_Phdr *phdr; |
4ad4eba5 AM |
4036 | int ret; |
4037 | ||
4038 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 4039 | ld shouldn't allow it. */ |
29a9f53e | 4040 | if (just_syms) |
92fd189d | 4041 | abort (); |
4ad4eba5 AM |
4042 | |
4043 | /* If this dynamic lib was specified on the command line with | |
4044 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
4045 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
4046 | in by another lib's DT_NEEDED. When --no-add-needed is used |
4047 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
4048 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
4049 | all. */ | |
4050 | add_needed = (elf_dyn_lib_class (abfd) | |
4051 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
4052 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
4053 | |
4054 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
4055 | if (s != NULL) | |
4056 | { | |
4057 | bfd_byte *dynbuf; | |
4058 | bfd_byte *extdyn; | |
cb33740c | 4059 | unsigned int elfsec; |
4ad4eba5 AM |
4060 | unsigned long shlink; |
4061 | ||
eea6121a | 4062 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
4063 | { |
4064 | error_free_dyn: | |
4065 | free (dynbuf); | |
4066 | goto error_return; | |
4067 | } | |
4ad4eba5 AM |
4068 | |
4069 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 4070 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
4071 | goto error_free_dyn; |
4072 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
4073 | ||
4074 | for (extdyn = dynbuf; | |
eea6121a | 4075 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
4076 | extdyn += bed->s->sizeof_dyn) |
4077 | { | |
4078 | Elf_Internal_Dyn dyn; | |
4079 | ||
4080 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
4081 | if (dyn.d_tag == DT_SONAME) | |
4082 | { | |
4083 | unsigned int tagv = dyn.d_un.d_val; | |
4084 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4085 | if (soname == NULL) | |
4086 | goto error_free_dyn; | |
4087 | } | |
4088 | if (dyn.d_tag == DT_NEEDED) | |
4089 | { | |
4090 | struct bfd_link_needed_list *n, **pn; | |
4091 | char *fnm, *anm; | |
4092 | unsigned int tagv = dyn.d_un.d_val; | |
4093 | ||
4094 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4095 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4096 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4097 | if (n == NULL || fnm == NULL) | |
4098 | goto error_free_dyn; | |
4099 | amt = strlen (fnm) + 1; | |
a50b1753 | 4100 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4101 | if (anm == NULL) |
4102 | goto error_free_dyn; | |
4103 | memcpy (anm, fnm, amt); | |
4104 | n->name = anm; | |
4105 | n->by = abfd; | |
4106 | n->next = NULL; | |
66eb6687 | 4107 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4108 | ; |
4109 | *pn = n; | |
4110 | } | |
4111 | if (dyn.d_tag == DT_RUNPATH) | |
4112 | { | |
4113 | struct bfd_link_needed_list *n, **pn; | |
4114 | char *fnm, *anm; | |
4115 | unsigned int tagv = dyn.d_un.d_val; | |
4116 | ||
4117 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4118 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4119 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4120 | if (n == NULL || fnm == NULL) | |
4121 | goto error_free_dyn; | |
4122 | amt = strlen (fnm) + 1; | |
a50b1753 | 4123 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4124 | if (anm == NULL) |
4125 | goto error_free_dyn; | |
4126 | memcpy (anm, fnm, amt); | |
4127 | n->name = anm; | |
4128 | n->by = abfd; | |
4129 | n->next = NULL; | |
4130 | for (pn = & runpath; | |
4131 | *pn != NULL; | |
4132 | pn = &(*pn)->next) | |
4133 | ; | |
4134 | *pn = n; | |
4135 | } | |
4136 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
4137 | if (!runpath && dyn.d_tag == DT_RPATH) | |
4138 | { | |
4139 | struct bfd_link_needed_list *n, **pn; | |
4140 | char *fnm, *anm; | |
4141 | unsigned int tagv = dyn.d_un.d_val; | |
4142 | ||
4143 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4144 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4145 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4146 | if (n == NULL || fnm == NULL) | |
4147 | goto error_free_dyn; | |
4148 | amt = strlen (fnm) + 1; | |
a50b1753 | 4149 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 4150 | if (anm == NULL) |
f8703194 | 4151 | goto error_free_dyn; |
4ad4eba5 AM |
4152 | memcpy (anm, fnm, amt); |
4153 | n->name = anm; | |
4154 | n->by = abfd; | |
4155 | n->next = NULL; | |
4156 | for (pn = & rpath; | |
4157 | *pn != NULL; | |
4158 | pn = &(*pn)->next) | |
4159 | ; | |
4160 | *pn = n; | |
4161 | } | |
7ee314fa AM |
4162 | if (dyn.d_tag == DT_AUDIT) |
4163 | { | |
4164 | unsigned int tagv = dyn.d_un.d_val; | |
4165 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4166 | } | |
4ad4eba5 AM |
4167 | } |
4168 | ||
4169 | free (dynbuf); | |
4170 | } | |
4171 | ||
4172 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
4173 | frees all more recently bfd_alloc'd blocks as well. */ | |
4174 | if (runpath) | |
4175 | rpath = runpath; | |
4176 | ||
4177 | if (rpath) | |
4178 | { | |
4179 | struct bfd_link_needed_list **pn; | |
66eb6687 | 4180 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4181 | ; |
4182 | *pn = rpath; | |
4183 | } | |
4184 | ||
9acc85a6 AM |
4185 | /* If we have a PT_GNU_RELRO program header, mark as read-only |
4186 | all sections contained fully therein. This makes relro | |
4187 | shared library sections appear as they will at run-time. */ | |
4188 | phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; | |
54025d58 | 4189 | while (phdr-- > elf_tdata (abfd)->phdr) |
9acc85a6 AM |
4190 | if (phdr->p_type == PT_GNU_RELRO) |
4191 | { | |
4192 | for (s = abfd->sections; s != NULL; s = s->next) | |
4193 | if ((s->flags & SEC_ALLOC) != 0 | |
4194 | && s->vma >= phdr->p_vaddr | |
4195 | && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) | |
4196 | s->flags |= SEC_READONLY; | |
4197 | break; | |
4198 | } | |
4199 | ||
4ad4eba5 AM |
4200 | /* We do not want to include any of the sections in a dynamic |
4201 | object in the output file. We hack by simply clobbering the | |
4202 | list of sections in the BFD. This could be handled more | |
4203 | cleanly by, say, a new section flag; the existing | |
4204 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4205 | still implies that the section takes up space in the output | |
4206 | file. */ | |
4207 | bfd_section_list_clear (abfd); | |
4208 | ||
4ad4eba5 AM |
4209 | /* Find the name to use in a DT_NEEDED entry that refers to this |
4210 | object. If the object has a DT_SONAME entry, we use it. | |
4211 | Otherwise, if the generic linker stuck something in | |
4212 | elf_dt_name, we use that. Otherwise, we just use the file | |
4213 | name. */ | |
4214 | if (soname == NULL || *soname == '\0') | |
4215 | { | |
4216 | soname = elf_dt_name (abfd); | |
4217 | if (soname == NULL || *soname == '\0') | |
4218 | soname = bfd_get_filename (abfd); | |
4219 | } | |
4220 | ||
4221 | /* Save the SONAME because sometimes the linker emulation code | |
4222 | will need to know it. */ | |
4223 | elf_dt_name (abfd) = soname; | |
4224 | ||
7e9f0867 | 4225 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4226 | if (ret < 0) |
4227 | goto error_return; | |
4228 | ||
4229 | /* If we have already included this dynamic object in the | |
4230 | link, just ignore it. There is no reason to include a | |
4231 | particular dynamic object more than once. */ | |
4232 | if (ret > 0) | |
4233 | return TRUE; | |
7ee314fa AM |
4234 | |
4235 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 4236 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
4237 | } |
4238 | ||
4239 | /* If this is a dynamic object, we always link against the .dynsym | |
4240 | symbol table, not the .symtab symbol table. The dynamic linker | |
4241 | will only see the .dynsym symbol table, so there is no reason to | |
4242 | look at .symtab for a dynamic object. */ | |
4243 | ||
4244 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
4245 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4246 | else | |
4247 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4248 | ||
4249 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
4250 | ||
4251 | /* The sh_info field of the symtab header tells us where the | |
4252 | external symbols start. We don't care about the local symbols at | |
4253 | this point. */ | |
4254 | if (elf_bad_symtab (abfd)) | |
4255 | { | |
4256 | extsymcount = symcount; | |
4257 | extsymoff = 0; | |
4258 | } | |
4259 | else | |
4260 | { | |
4261 | extsymcount = symcount - hdr->sh_info; | |
4262 | extsymoff = hdr->sh_info; | |
4263 | } | |
4264 | ||
f45794cb | 4265 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4266 | if (extsymcount != 0) |
4ad4eba5 AM |
4267 | { |
4268 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4269 | NULL, NULL, NULL); | |
4270 | if (isymbuf == NULL) | |
4271 | goto error_return; | |
4272 | ||
4ad4eba5 | 4273 | if (sym_hash == NULL) |
012b2306 AM |
4274 | { |
4275 | /* We store a pointer to the hash table entry for each | |
4276 | external symbol. */ | |
ef53be89 AM |
4277 | amt = extsymcount; |
4278 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4279 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4280 | if (sym_hash == NULL) | |
4281 | goto error_free_sym; | |
4282 | elf_sym_hashes (abfd) = sym_hash; | |
4283 | } | |
4ad4eba5 AM |
4284 | } |
4285 | ||
4286 | if (dynamic) | |
4287 | { | |
4288 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4289 | if (!_bfd_elf_slurp_version_tables (abfd, |
4290 | info->default_imported_symver)) | |
4ad4eba5 AM |
4291 | goto error_free_sym; |
4292 | ||
4293 | /* Read in the symbol versions, but don't bother to convert them | |
4294 | to internal format. */ | |
4295 | if (elf_dynversym (abfd) != 0) | |
4296 | { | |
4297 | Elf_Internal_Shdr *versymhdr; | |
4298 | ||
4299 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
a50b1753 | 4300 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
4ad4eba5 AM |
4301 | if (extversym == NULL) |
4302 | goto error_free_sym; | |
4303 | amt = versymhdr->sh_size; | |
4304 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
4305 | || bfd_bread (extversym, amt, abfd) != amt) | |
4306 | goto error_free_vers; | |
4307 | } | |
4308 | } | |
4309 | ||
66eb6687 AM |
4310 | /* If we are loading an as-needed shared lib, save the symbol table |
4311 | state before we start adding symbols. If the lib turns out | |
4312 | to be unneeded, restore the state. */ | |
4313 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4314 | { | |
4315 | unsigned int i; | |
4316 | size_t entsize; | |
4317 | ||
4318 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4319 | { | |
4320 | struct bfd_hash_entry *p; | |
2de92251 | 4321 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4322 | |
4323 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4324 | { |
4325 | h = (struct elf_link_hash_entry *) p; | |
4326 | entsize += htab->root.table.entsize; | |
4327 | if (h->root.type == bfd_link_hash_warning) | |
4328 | entsize += htab->root.table.entsize; | |
4329 | } | |
66eb6687 AM |
4330 | } |
4331 | ||
4332 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4333 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4334 | if (old_tab == NULL) |
4335 | goto error_free_vers; | |
4336 | ||
4337 | /* Remember the current objalloc pointer, so that all mem for | |
4338 | symbols added can later be reclaimed. */ | |
4339 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4340 | if (alloc_mark == NULL) | |
4341 | goto error_free_vers; | |
4342 | ||
5061a885 AM |
4343 | /* Make a special call to the linker "notice" function to |
4344 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4345 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4346 | goto error_free_vers; |
5061a885 | 4347 | |
f45794cb AM |
4348 | /* Clone the symbol table. Remember some pointers into the |
4349 | symbol table, and dynamic symbol count. */ | |
4350 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4351 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4352 | old_undefs = htab->root.undefs; |
4353 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4354 | old_table = htab->root.table.table; |
4355 | old_size = htab->root.table.size; | |
4356 | old_count = htab->root.table.count; | |
5b677558 AM |
4357 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4358 | if (old_strtab == NULL) | |
4359 | goto error_free_vers; | |
66eb6687 AM |
4360 | |
4361 | for (i = 0; i < htab->root.table.size; i++) | |
4362 | { | |
4363 | struct bfd_hash_entry *p; | |
2de92251 | 4364 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4365 | |
4366 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4367 | { | |
4368 | memcpy (old_ent, p, htab->root.table.entsize); | |
4369 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4370 | h = (struct elf_link_hash_entry *) p; |
4371 | if (h->root.type == bfd_link_hash_warning) | |
4372 | { | |
4373 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4374 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4375 | } | |
66eb6687 AM |
4376 | } |
4377 | } | |
4378 | } | |
4ad4eba5 | 4379 | |
66eb6687 | 4380 | weaks = NULL; |
4ad4eba5 AM |
4381 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
4382 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
4383 | isym < isymend; | |
4384 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4385 | { | |
4386 | int bind; | |
4387 | bfd_vma value; | |
af44c138 | 4388 | asection *sec, *new_sec; |
4ad4eba5 AM |
4389 | flagword flags; |
4390 | const char *name; | |
4391 | struct elf_link_hash_entry *h; | |
90c984fc | 4392 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4393 | bfd_boolean definition; |
4394 | bfd_boolean size_change_ok; | |
4395 | bfd_boolean type_change_ok; | |
37a9e49a L |
4396 | bfd_boolean new_weak; |
4397 | bfd_boolean old_weak; | |
4ad4eba5 | 4398 | bfd_boolean override; |
a4d8e49b | 4399 | bfd_boolean common; |
97196564 | 4400 | bfd_boolean discarded; |
4ad4eba5 AM |
4401 | unsigned int old_alignment; |
4402 | bfd *old_bfd; | |
6e33951e | 4403 | bfd_boolean matched; |
4ad4eba5 AM |
4404 | |
4405 | override = FALSE; | |
4406 | ||
4407 | flags = BSF_NO_FLAGS; | |
4408 | sec = NULL; | |
4409 | value = isym->st_value; | |
a4d8e49b | 4410 | common = bed->common_definition (isym); |
2980ccad L |
4411 | if (common && info->inhibit_common_definition) |
4412 | { | |
4413 | /* Treat common symbol as undefined for --no-define-common. */ | |
4414 | isym->st_shndx = SHN_UNDEF; | |
4415 | common = FALSE; | |
4416 | } | |
97196564 | 4417 | discarded = FALSE; |
4ad4eba5 AM |
4418 | |
4419 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4420 | switch (bind) |
4ad4eba5 | 4421 | { |
3e7a7d11 | 4422 | case STB_LOCAL: |
4ad4eba5 AM |
4423 | /* This should be impossible, since ELF requires that all |
4424 | global symbols follow all local symbols, and that sh_info | |
4425 | point to the first global symbol. Unfortunately, Irix 5 | |
4426 | screws this up. */ | |
4427 | continue; | |
3e7a7d11 NC |
4428 | |
4429 | case STB_GLOBAL: | |
a4d8e49b | 4430 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4431 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4432 | break; |
4433 | ||
4434 | case STB_WEAK: | |
4435 | flags = BSF_WEAK; | |
4436 | break; | |
4437 | ||
4438 | case STB_GNU_UNIQUE: | |
4439 | flags = BSF_GNU_UNIQUE; | |
4440 | break; | |
4441 | ||
4442 | default: | |
4ad4eba5 | 4443 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4444 | break; |
4ad4eba5 AM |
4445 | } |
4446 | ||
4447 | if (isym->st_shndx == SHN_UNDEF) | |
4448 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4449 | else if (isym->st_shndx == SHN_ABS) |
4450 | sec = bfd_abs_section_ptr; | |
4451 | else if (isym->st_shndx == SHN_COMMON) | |
4452 | { | |
4453 | sec = bfd_com_section_ptr; | |
4454 | /* What ELF calls the size we call the value. What ELF | |
4455 | calls the value we call the alignment. */ | |
4456 | value = isym->st_size; | |
4457 | } | |
4458 | else | |
4ad4eba5 AM |
4459 | { |
4460 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4461 | if (sec == NULL) | |
4462 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4463 | else if (discarded_section (sec)) |
529fcb95 | 4464 | { |
e5d08002 L |
4465 | /* Symbols from discarded section are undefined. We keep |
4466 | its visibility. */ | |
529fcb95 | 4467 | sec = bfd_und_section_ptr; |
97196564 | 4468 | discarded = TRUE; |
529fcb95 PB |
4469 | isym->st_shndx = SHN_UNDEF; |
4470 | } | |
4ad4eba5 AM |
4471 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4472 | value -= sec->vma; | |
4473 | } | |
4ad4eba5 AM |
4474 | |
4475 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4476 | isym->st_name); | |
4477 | if (name == NULL) | |
4478 | goto error_free_vers; | |
4479 | ||
4480 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4481 | && (abfd->flags & BFD_PLUGIN) != 0) |
4482 | { | |
4483 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4484 | ||
4485 | if (xc == NULL) | |
4486 | { | |
4487 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4488 | | SEC_EXCLUDE); | |
4489 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4490 | if (xc == NULL) | |
4491 | goto error_free_vers; | |
4492 | } | |
4493 | sec = xc; | |
4494 | } | |
4495 | else if (isym->st_shndx == SHN_COMMON | |
4496 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4497 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4498 | { |
4499 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4500 | ||
4501 | if (tcomm == NULL) | |
4502 | { | |
02d00247 AM |
4503 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4504 | | SEC_LINKER_CREATED); | |
4505 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4506 | if (tcomm == NULL) |
4ad4eba5 AM |
4507 | goto error_free_vers; |
4508 | } | |
4509 | sec = tcomm; | |
4510 | } | |
66eb6687 | 4511 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4512 | { |
66eb6687 AM |
4513 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4514 | &sec, &value)) | |
4ad4eba5 AM |
4515 | goto error_free_vers; |
4516 | ||
4517 | /* The hook function sets the name to NULL if this symbol | |
4518 | should be skipped for some reason. */ | |
4519 | if (name == NULL) | |
4520 | continue; | |
4521 | } | |
4522 | ||
4523 | /* Sanity check that all possibilities were handled. */ | |
4524 | if (sec == NULL) | |
4525 | { | |
4526 | bfd_set_error (bfd_error_bad_value); | |
4527 | goto error_free_vers; | |
4528 | } | |
4529 | ||
191c0c42 AM |
4530 | /* Silently discard TLS symbols from --just-syms. There's |
4531 | no way to combine a static TLS block with a new TLS block | |
4532 | for this executable. */ | |
4533 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4534 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4535 | continue; | |
4536 | ||
4ad4eba5 AM |
4537 | if (bfd_is_und_section (sec) |
4538 | || bfd_is_com_section (sec)) | |
4539 | definition = FALSE; | |
4540 | else | |
4541 | definition = TRUE; | |
4542 | ||
4543 | size_change_ok = FALSE; | |
66eb6687 | 4544 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4545 | old_weak = FALSE; |
6e33951e | 4546 | matched = FALSE; |
4ad4eba5 AM |
4547 | old_alignment = 0; |
4548 | old_bfd = NULL; | |
af44c138 | 4549 | new_sec = sec; |
4ad4eba5 | 4550 | |
66eb6687 | 4551 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4552 | { |
4553 | Elf_Internal_Versym iver; | |
4554 | unsigned int vernum = 0; | |
4555 | bfd_boolean skip; | |
4556 | ||
fc0e6df6 | 4557 | if (ever == NULL) |
4ad4eba5 | 4558 | { |
fc0e6df6 PB |
4559 | if (info->default_imported_symver) |
4560 | /* Use the default symbol version created earlier. */ | |
4561 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4562 | else | |
4563 | iver.vs_vers = 0; | |
4564 | } | |
4565 | else | |
4566 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4567 | ||
4568 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4569 | ||
4570 | /* If this is a hidden symbol, or if it is not version | |
4571 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4572 | However, we do not modify a non-hidden absolute symbol |
4573 | if it is not a function, because it might be the version | |
4574 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4575 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4576 | || (vernum > 1 |
4577 | && (!bfd_is_abs_section (sec) | |
4578 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4579 | { |
4580 | const char *verstr; | |
4581 | size_t namelen, verlen, newlen; | |
4582 | char *newname, *p; | |
4583 | ||
4584 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4585 | { |
fc0e6df6 PB |
4586 | if (vernum > elf_tdata (abfd)->cverdefs) |
4587 | verstr = NULL; | |
4588 | else if (vernum > 1) | |
4589 | verstr = | |
4590 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4591 | else | |
4592 | verstr = ""; | |
4ad4eba5 | 4593 | |
fc0e6df6 | 4594 | if (verstr == NULL) |
4ad4eba5 | 4595 | { |
4eca0228 | 4596 | _bfd_error_handler |
695344c0 | 4597 | /* xgettext:c-format */ |
871b3ab2 | 4598 | (_("%pB: %s: invalid version %u (max %d)"), |
fc0e6df6 PB |
4599 | abfd, name, vernum, |
4600 | elf_tdata (abfd)->cverdefs); | |
4601 | bfd_set_error (bfd_error_bad_value); | |
4602 | goto error_free_vers; | |
4ad4eba5 | 4603 | } |
fc0e6df6 PB |
4604 | } |
4605 | else | |
4606 | { | |
4607 | /* We cannot simply test for the number of | |
4608 | entries in the VERNEED section since the | |
4609 | numbers for the needed versions do not start | |
4610 | at 0. */ | |
4611 | Elf_Internal_Verneed *t; | |
4612 | ||
4613 | verstr = NULL; | |
4614 | for (t = elf_tdata (abfd)->verref; | |
4615 | t != NULL; | |
4616 | t = t->vn_nextref) | |
4ad4eba5 | 4617 | { |
fc0e6df6 | 4618 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4619 | |
fc0e6df6 PB |
4620 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4621 | { | |
4622 | if (a->vna_other == vernum) | |
4ad4eba5 | 4623 | { |
fc0e6df6 PB |
4624 | verstr = a->vna_nodename; |
4625 | break; | |
4ad4eba5 | 4626 | } |
4ad4eba5 | 4627 | } |
fc0e6df6 PB |
4628 | if (a != NULL) |
4629 | break; | |
4630 | } | |
4631 | if (verstr == NULL) | |
4632 | { | |
4eca0228 | 4633 | _bfd_error_handler |
695344c0 | 4634 | /* xgettext:c-format */ |
871b3ab2 | 4635 | (_("%pB: %s: invalid needed version %d"), |
fc0e6df6 PB |
4636 | abfd, name, vernum); |
4637 | bfd_set_error (bfd_error_bad_value); | |
4638 | goto error_free_vers; | |
4ad4eba5 | 4639 | } |
4ad4eba5 | 4640 | } |
fc0e6df6 PB |
4641 | |
4642 | namelen = strlen (name); | |
4643 | verlen = strlen (verstr); | |
4644 | newlen = namelen + verlen + 2; | |
4645 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4646 | && isym->st_shndx != SHN_UNDEF) | |
4647 | ++newlen; | |
4648 | ||
a50b1753 | 4649 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4650 | if (newname == NULL) |
4651 | goto error_free_vers; | |
4652 | memcpy (newname, name, namelen); | |
4653 | p = newname + namelen; | |
4654 | *p++ = ELF_VER_CHR; | |
4655 | /* If this is a defined non-hidden version symbol, | |
4656 | we add another @ to the name. This indicates the | |
4657 | default version of the symbol. */ | |
4658 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4659 | && isym->st_shndx != SHN_UNDEF) | |
4660 | *p++ = ELF_VER_CHR; | |
4661 | memcpy (p, verstr, verlen + 1); | |
4662 | ||
4663 | name = newname; | |
4ad4eba5 AM |
4664 | } |
4665 | ||
cd3416da AM |
4666 | /* If this symbol has default visibility and the user has |
4667 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4668 | if (!bfd_is_und_section (sec) |
cd3416da | 4669 | && !dynamic |
ce875075 | 4670 | && abfd->no_export |
cd3416da AM |
4671 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4672 | isym->st_other = (STV_HIDDEN | |
4673 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4674 | ||
4f3fedcf AM |
4675 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4676 | sym_hash, &old_bfd, &old_weak, | |
4677 | &old_alignment, &skip, &override, | |
6e33951e L |
4678 | &type_change_ok, &size_change_ok, |
4679 | &matched)) | |
4ad4eba5 AM |
4680 | goto error_free_vers; |
4681 | ||
4682 | if (skip) | |
4683 | continue; | |
4684 | ||
6e33951e L |
4685 | /* Override a definition only if the new symbol matches the |
4686 | existing one. */ | |
4687 | if (override && matched) | |
4ad4eba5 AM |
4688 | definition = FALSE; |
4689 | ||
4690 | h = *sym_hash; | |
4691 | while (h->root.type == bfd_link_hash_indirect | |
4692 | || h->root.type == bfd_link_hash_warning) | |
4693 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4694 | ||
4ad4eba5 | 4695 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4696 | && vernum > 1 |
4697 | && definition) | |
4698 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4699 | } | |
4700 | ||
4701 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4702 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4703 | (struct bfd_link_hash_entry **) sym_hash))) |
4704 | goto error_free_vers; | |
4705 | ||
ac98f9e2 L |
4706 | if ((abfd->flags & DYNAMIC) == 0 |
4707 | && (bfd_get_flavour (info->output_bfd) | |
4708 | == bfd_target_elf_flavour)) | |
4709 | { | |
4710 | if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC) | |
4711 | elf_tdata (info->output_bfd)->has_gnu_symbols | |
4712 | |= elf_gnu_symbol_ifunc; | |
4713 | if ((flags & BSF_GNU_UNIQUE)) | |
4714 | elf_tdata (info->output_bfd)->has_gnu_symbols | |
4715 | |= elf_gnu_symbol_unique; | |
4716 | } | |
a43942db | 4717 | |
4ad4eba5 | 4718 | h = *sym_hash; |
90c984fc L |
4719 | /* We need to make sure that indirect symbol dynamic flags are |
4720 | updated. */ | |
4721 | hi = h; | |
4ad4eba5 AM |
4722 | while (h->root.type == bfd_link_hash_indirect |
4723 | || h->root.type == bfd_link_hash_warning) | |
4724 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4725 | |
97196564 L |
4726 | /* Setting the index to -3 tells elf_link_output_extsym that |
4727 | this symbol is defined in a discarded section. */ | |
4728 | if (discarded) | |
4729 | h->indx = -3; | |
4730 | ||
4ad4eba5 AM |
4731 | *sym_hash = h; |
4732 | ||
37a9e49a | 4733 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4734 | if (dynamic |
4735 | && definition | |
37a9e49a | 4736 | && new_weak |
fcb93ecf | 4737 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4738 | && is_elf_hash_table (htab) |
60d67dc8 | 4739 | && h->u.alias == NULL) |
4ad4eba5 AM |
4740 | { |
4741 | /* Keep a list of all weak defined non function symbols from | |
60d67dc8 AM |
4742 | a dynamic object, using the alias field. Later in this |
4743 | function we will set the alias field to the correct | |
4ad4eba5 AM |
4744 | value. We only put non-function symbols from dynamic |
4745 | objects on this list, because that happens to be the only | |
4746 | time we need to know the normal symbol corresponding to a | |
4747 | weak symbol, and the information is time consuming to | |
60d67dc8 | 4748 | figure out. If the alias field is not already NULL, |
4ad4eba5 AM |
4749 | then this symbol was already defined by some previous |
4750 | dynamic object, and we will be using that previous | |
4751 | definition anyhow. */ | |
4752 | ||
60d67dc8 | 4753 | h->u.alias = weaks; |
4ad4eba5 | 4754 | weaks = h; |
4ad4eba5 AM |
4755 | } |
4756 | ||
4757 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4758 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4759 | && h->root.type == bfd_link_hash_common) |
4760 | { | |
4761 | unsigned int align; | |
4762 | ||
a4d8e49b | 4763 | if (common) |
af44c138 L |
4764 | align = bfd_log2 (isym->st_value); |
4765 | else | |
4766 | { | |
4767 | /* The new symbol is a common symbol in a shared object. | |
4768 | We need to get the alignment from the section. */ | |
4769 | align = new_sec->alignment_power; | |
4770 | } | |
595213d4 | 4771 | if (align > old_alignment) |
4ad4eba5 AM |
4772 | h->root.u.c.p->alignment_power = align; |
4773 | else | |
4774 | h->root.u.c.p->alignment_power = old_alignment; | |
4775 | } | |
4776 | ||
66eb6687 | 4777 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4778 | { |
4f3fedcf AM |
4779 | /* Set a flag in the hash table entry indicating the type of |
4780 | reference or definition we just found. A dynamic symbol | |
4781 | is one which is referenced or defined by both a regular | |
4782 | object and a shared object. */ | |
4783 | bfd_boolean dynsym = FALSE; | |
4784 | ||
4785 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4786 | ref_regular for them, or make them dynamic. */ | |
4787 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4788 | ; | |
4789 | else if (! dynamic) | |
4790 | { | |
4791 | if (! definition) | |
4792 | { | |
4793 | h->ref_regular = 1; | |
4794 | if (bind != STB_WEAK) | |
4795 | h->ref_regular_nonweak = 1; | |
4796 | } | |
4797 | else | |
4798 | { | |
4799 | h->def_regular = 1; | |
4800 | if (h->def_dynamic) | |
4801 | { | |
4802 | h->def_dynamic = 0; | |
4803 | h->ref_dynamic = 1; | |
4804 | } | |
4805 | } | |
4806 | ||
4807 | /* If the indirect symbol has been forced local, don't | |
4808 | make the real symbol dynamic. */ | |
4809 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4810 | && (bfd_link_dll (info) |
4f3fedcf AM |
4811 | || h->def_dynamic |
4812 | || h->ref_dynamic)) | |
4813 | dynsym = TRUE; | |
4814 | } | |
4815 | else | |
4816 | { | |
4817 | if (! definition) | |
4818 | { | |
4819 | h->ref_dynamic = 1; | |
4820 | hi->ref_dynamic = 1; | |
4821 | } | |
4822 | else | |
4823 | { | |
4824 | h->def_dynamic = 1; | |
4825 | hi->def_dynamic = 1; | |
4826 | } | |
4827 | ||
4828 | /* If the indirect symbol has been forced local, don't | |
4829 | make the real symbol dynamic. */ | |
4830 | if ((h == hi || !hi->forced_local) | |
4831 | && (h->def_regular | |
4832 | || h->ref_regular | |
60d67dc8 AM |
4833 | || (h->is_weakalias |
4834 | && weakdef (h)->dynindx != -1))) | |
4f3fedcf AM |
4835 | dynsym = TRUE; |
4836 | } | |
4837 | ||
4838 | /* Check to see if we need to add an indirect symbol for | |
4839 | the default name. */ | |
4840 | if (definition | |
4841 | || (!override && h->root.type == bfd_link_hash_common)) | |
4842 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4843 | sec, value, &old_bfd, &dynsym)) | |
4844 | goto error_free_vers; | |
4ad4eba5 AM |
4845 | |
4846 | /* Check the alignment when a common symbol is involved. This | |
4847 | can change when a common symbol is overridden by a normal | |
4848 | definition or a common symbol is ignored due to the old | |
4849 | normal definition. We need to make sure the maximum | |
4850 | alignment is maintained. */ | |
a4d8e49b | 4851 | if ((old_alignment || common) |
4ad4eba5 AM |
4852 | && h->root.type != bfd_link_hash_common) |
4853 | { | |
4854 | unsigned int common_align; | |
4855 | unsigned int normal_align; | |
4856 | unsigned int symbol_align; | |
4857 | bfd *normal_bfd; | |
4858 | bfd *common_bfd; | |
4859 | ||
3a81e825 AM |
4860 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4861 | || h->root.type == bfd_link_hash_defweak); | |
4862 | ||
4ad4eba5 AM |
4863 | symbol_align = ffs (h->root.u.def.value) - 1; |
4864 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4865 | && (h->root.u.def.section->owner->flags |
4866 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4867 | { |
4868 | normal_align = h->root.u.def.section->alignment_power; | |
4869 | if (normal_align > symbol_align) | |
4870 | normal_align = symbol_align; | |
4871 | } | |
4872 | else | |
4873 | normal_align = symbol_align; | |
4874 | ||
4875 | if (old_alignment) | |
4876 | { | |
4877 | common_align = old_alignment; | |
4878 | common_bfd = old_bfd; | |
4879 | normal_bfd = abfd; | |
4880 | } | |
4881 | else | |
4882 | { | |
4883 | common_align = bfd_log2 (isym->st_value); | |
4884 | common_bfd = abfd; | |
4885 | normal_bfd = old_bfd; | |
4886 | } | |
4887 | ||
4888 | if (normal_align < common_align) | |
d07676f8 NC |
4889 | { |
4890 | /* PR binutils/2735 */ | |
4891 | if (normal_bfd == NULL) | |
4eca0228 | 4892 | _bfd_error_handler |
695344c0 | 4893 | /* xgettext:c-format */ |
9793eb77 | 4894 | (_("warning: alignment %u of common symbol `%s' in %pB is" |
871b3ab2 | 4895 | " greater than the alignment (%u) of its section %pA"), |
c08bb8dd AM |
4896 | 1 << common_align, name, common_bfd, |
4897 | 1 << normal_align, h->root.u.def.section); | |
d07676f8 | 4898 | else |
4eca0228 | 4899 | _bfd_error_handler |
695344c0 | 4900 | /* xgettext:c-format */ |
9793eb77 | 4901 | (_("warning: alignment %u of symbol `%s' in %pB" |
871b3ab2 | 4902 | " is smaller than %u in %pB"), |
c08bb8dd AM |
4903 | 1 << normal_align, name, normal_bfd, |
4904 | 1 << common_align, common_bfd); | |
d07676f8 | 4905 | } |
4ad4eba5 AM |
4906 | } |
4907 | ||
83ad0046 | 4908 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4909 | if (isym->st_size != 0 |
4910 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4911 | && (definition || h->size == 0)) |
4912 | { | |
83ad0046 L |
4913 | if (h->size != 0 |
4914 | && h->size != isym->st_size | |
4915 | && ! size_change_ok) | |
4eca0228 | 4916 | _bfd_error_handler |
695344c0 | 4917 | /* xgettext:c-format */ |
9793eb77 | 4918 | (_("warning: size of symbol `%s' changed" |
2dcf00ce AM |
4919 | " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"), |
4920 | name, (uint64_t) h->size, old_bfd, | |
4921 | (uint64_t) isym->st_size, abfd); | |
4ad4eba5 AM |
4922 | |
4923 | h->size = isym->st_size; | |
4924 | } | |
4925 | ||
4926 | /* If this is a common symbol, then we always want H->SIZE | |
4927 | to be the size of the common symbol. The code just above | |
4928 | won't fix the size if a common symbol becomes larger. We | |
4929 | don't warn about a size change here, because that is | |
4f3fedcf | 4930 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4931 | function types. */ |
4ad4eba5 AM |
4932 | if (h->root.type == bfd_link_hash_common) |
4933 | h->size = h->root.u.c.size; | |
4934 | ||
4935 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4936 | && ((definition && !new_weak) |
4937 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4938 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4939 | { |
2955ec4c L |
4940 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4941 | ||
4942 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4943 | symbol. */ | |
4944 | if (type == STT_GNU_IFUNC | |
4945 | && (abfd->flags & DYNAMIC) != 0) | |
4946 | type = STT_FUNC; | |
4ad4eba5 | 4947 | |
2955ec4c L |
4948 | if (h->type != type) |
4949 | { | |
4950 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 4951 | /* xgettext:c-format */ |
4eca0228 | 4952 | _bfd_error_handler |
9793eb77 | 4953 | (_("warning: type of symbol `%s' changed" |
871b3ab2 | 4954 | " from %d to %d in %pB"), |
c08bb8dd | 4955 | name, h->type, type, abfd); |
2955ec4c L |
4956 | |
4957 | h->type = type; | |
4958 | } | |
4ad4eba5 AM |
4959 | } |
4960 | ||
54ac0771 | 4961 | /* Merge st_other field. */ |
b8417128 | 4962 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 4963 | |
c3df8c14 | 4964 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
4965 | if (definition |
4966 | && (sec->flags & SEC_DEBUGGING) | |
4967 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
4968 | dynsym = FALSE; |
4969 | ||
4f3fedcf AM |
4970 | /* Nor should we make plugin symbols dynamic. */ |
4971 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4972 | dynsym = FALSE; | |
4973 | ||
35fc36a8 | 4974 | if (definition) |
35399224 L |
4975 | { |
4976 | h->target_internal = isym->st_target_internal; | |
4977 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
4978 | } | |
35fc36a8 | 4979 | |
4ad4eba5 AM |
4980 | if (definition && !dynamic) |
4981 | { | |
4982 | char *p = strchr (name, ELF_VER_CHR); | |
4983 | if (p != NULL && p[1] != ELF_VER_CHR) | |
4984 | { | |
4985 | /* Queue non-default versions so that .symver x, x@FOO | |
4986 | aliases can be checked. */ | |
66eb6687 | 4987 | if (!nondeflt_vers) |
4ad4eba5 | 4988 | { |
66eb6687 AM |
4989 | amt = ((isymend - isym + 1) |
4990 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
4991 | nondeflt_vers |
4992 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
4993 | if (!nondeflt_vers) |
4994 | goto error_free_vers; | |
4ad4eba5 | 4995 | } |
66eb6687 | 4996 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
4997 | } |
4998 | } | |
4999 | ||
5000 | if (dynsym && h->dynindx == -1) | |
5001 | { | |
c152c796 | 5002 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 5003 | goto error_free_vers; |
60d67dc8 AM |
5004 | if (h->is_weakalias |
5005 | && weakdef (h)->dynindx == -1) | |
4ad4eba5 | 5006 | { |
60d67dc8 | 5007 | if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) |
4ad4eba5 AM |
5008 | goto error_free_vers; |
5009 | } | |
5010 | } | |
1f599d0e | 5011 | else if (h->dynindx != -1) |
4ad4eba5 AM |
5012 | /* If the symbol already has a dynamic index, but |
5013 | visibility says it should not be visible, turn it into | |
5014 | a local symbol. */ | |
5015 | switch (ELF_ST_VISIBILITY (h->other)) | |
5016 | { | |
5017 | case STV_INTERNAL: | |
5018 | case STV_HIDDEN: | |
5019 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
5020 | dynsym = FALSE; | |
5021 | break; | |
5022 | } | |
5023 | ||
aef28989 L |
5024 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
5025 | for unmatched symbol. */ | |
4ad4eba5 | 5026 | if (!add_needed |
aef28989 | 5027 | && matched |
4ad4eba5 | 5028 | && definition |
010e5ae2 | 5029 | && ((dynsym |
ffa9430d | 5030 | && h->ref_regular_nonweak |
4f3fedcf AM |
5031 | && (old_bfd == NULL |
5032 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 5033 | || (h->ref_dynamic_nonweak |
010e5ae2 | 5034 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
5035 | && !on_needed_list (elf_dt_name (abfd), |
5036 | htab->needed, NULL)))) | |
4ad4eba5 AM |
5037 | { |
5038 | int ret; | |
5039 | const char *soname = elf_dt_name (abfd); | |
5040 | ||
16e4ecc0 AM |
5041 | info->callbacks->minfo ("%!", soname, old_bfd, |
5042 | h->root.root.string); | |
5043 | ||
4ad4eba5 AM |
5044 | /* A symbol from a library loaded via DT_NEEDED of some |
5045 | other library is referenced by a regular object. | |
e56f61be | 5046 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
5047 | --no-add-needed is used and the reference was not |
5048 | a weak one. */ | |
4f3fedcf | 5049 | if (old_bfd != NULL |
b918acf9 | 5050 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 5051 | { |
4eca0228 | 5052 | _bfd_error_handler |
695344c0 | 5053 | /* xgettext:c-format */ |
871b3ab2 | 5054 | (_("%pB: undefined reference to symbol '%s'"), |
4f3fedcf | 5055 | old_bfd, name); |
ff5ac77b | 5056 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
5057 | goto error_free_vers; |
5058 | } | |
5059 | ||
a50b1753 | 5060 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 5061 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 5062 | |
4ad4eba5 | 5063 | add_needed = TRUE; |
7e9f0867 | 5064 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
5065 | if (ret < 0) |
5066 | goto error_free_vers; | |
5067 | ||
5068 | BFD_ASSERT (ret == 0); | |
5069 | } | |
5070 | } | |
5071 | } | |
5072 | ||
a83ef4d1 L |
5073 | if (info->lto_plugin_active |
5074 | && !bfd_link_relocatable (info) | |
5075 | && (abfd->flags & BFD_PLUGIN) == 0 | |
5076 | && !just_syms | |
5077 | && extsymcount) | |
5078 | { | |
5079 | int r_sym_shift; | |
5080 | ||
5081 | if (bed->s->arch_size == 32) | |
5082 | r_sym_shift = 8; | |
5083 | else | |
5084 | r_sym_shift = 32; | |
5085 | ||
5086 | /* If linker plugin is enabled, set non_ir_ref_regular on symbols | |
5087 | referenced in regular objects so that linker plugin will get | |
5088 | the correct symbol resolution. */ | |
5089 | ||
5090 | sym_hash = elf_sym_hashes (abfd); | |
5091 | for (s = abfd->sections; s != NULL; s = s->next) | |
5092 | { | |
5093 | Elf_Internal_Rela *internal_relocs; | |
5094 | Elf_Internal_Rela *rel, *relend; | |
5095 | ||
5096 | /* Don't check relocations in excluded sections. */ | |
5097 | if ((s->flags & SEC_RELOC) == 0 | |
5098 | || s->reloc_count == 0 | |
5099 | || (s->flags & SEC_EXCLUDE) != 0 | |
5100 | || ((info->strip == strip_all | |
5101 | || info->strip == strip_debugger) | |
5102 | && (s->flags & SEC_DEBUGGING) != 0)) | |
5103 | continue; | |
5104 | ||
5105 | internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL, | |
5106 | NULL, | |
5107 | info->keep_memory); | |
5108 | if (internal_relocs == NULL) | |
5109 | goto error_free_vers; | |
5110 | ||
5111 | rel = internal_relocs; | |
5112 | relend = rel + s->reloc_count; | |
5113 | for ( ; rel < relend; rel++) | |
5114 | { | |
5115 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
5116 | struct elf_link_hash_entry *h; | |
5117 | ||
5118 | /* Skip local symbols. */ | |
5119 | if (r_symndx < extsymoff) | |
5120 | continue; | |
5121 | ||
5122 | h = sym_hash[r_symndx - extsymoff]; | |
5123 | if (h != NULL) | |
5124 | h->root.non_ir_ref_regular = 1; | |
5125 | } | |
5126 | ||
5127 | if (elf_section_data (s)->relocs != internal_relocs) | |
5128 | free (internal_relocs); | |
5129 | } | |
5130 | } | |
5131 | ||
66eb6687 AM |
5132 | if (extversym != NULL) |
5133 | { | |
5134 | free (extversym); | |
5135 | extversym = NULL; | |
5136 | } | |
5137 | ||
5138 | if (isymbuf != NULL) | |
5139 | { | |
5140 | free (isymbuf); | |
5141 | isymbuf = NULL; | |
5142 | } | |
5143 | ||
5144 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
5145 | { | |
5146 | unsigned int i; | |
5147 | ||
5148 | /* Restore the symbol table. */ | |
f45794cb AM |
5149 | old_ent = (char *) old_tab + tabsize; |
5150 | memset (elf_sym_hashes (abfd), 0, | |
5151 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
5152 | htab->root.table.table = old_table; |
5153 | htab->root.table.size = old_size; | |
5154 | htab->root.table.count = old_count; | |
66eb6687 | 5155 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
5156 | htab->root.undefs = old_undefs; |
5157 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
5158 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
5159 | free (old_strtab); | |
5160 | old_strtab = NULL; | |
66eb6687 AM |
5161 | for (i = 0; i < htab->root.table.size; i++) |
5162 | { | |
5163 | struct bfd_hash_entry *p; | |
5164 | struct elf_link_hash_entry *h; | |
3e0882af L |
5165 | bfd_size_type size; |
5166 | unsigned int alignment_power; | |
4070765b | 5167 | unsigned int non_ir_ref_dynamic; |
66eb6687 AM |
5168 | |
5169 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
5170 | { | |
5171 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
5172 | if (h->root.type == bfd_link_hash_warning) |
5173 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 5174 | |
3e0882af L |
5175 | /* Preserve the maximum alignment and size for common |
5176 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 5177 | since it can still be loaded at run time by another |
3e0882af L |
5178 | dynamic lib. */ |
5179 | if (h->root.type == bfd_link_hash_common) | |
5180 | { | |
5181 | size = h->root.u.c.size; | |
5182 | alignment_power = h->root.u.c.p->alignment_power; | |
5183 | } | |
5184 | else | |
5185 | { | |
5186 | size = 0; | |
5187 | alignment_power = 0; | |
5188 | } | |
4070765b | 5189 | /* Preserve non_ir_ref_dynamic so that this symbol |
59fa66c5 L |
5190 | will be exported when the dynamic lib becomes needed |
5191 | in the second pass. */ | |
4070765b | 5192 | non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; |
66eb6687 AM |
5193 | memcpy (p, old_ent, htab->root.table.entsize); |
5194 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
5195 | h = (struct elf_link_hash_entry *) p; |
5196 | if (h->root.type == bfd_link_hash_warning) | |
5197 | { | |
5198 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
5199 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 5200 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 5201 | } |
a4542f1b | 5202 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
5203 | { |
5204 | if (size > h->root.u.c.size) | |
5205 | h->root.u.c.size = size; | |
5206 | if (alignment_power > h->root.u.c.p->alignment_power) | |
5207 | h->root.u.c.p->alignment_power = alignment_power; | |
5208 | } | |
4070765b | 5209 | h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; |
66eb6687 AM |
5210 | } |
5211 | } | |
5212 | ||
5061a885 AM |
5213 | /* Make a special call to the linker "notice" function to |
5214 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 5215 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 5216 | goto error_free_vers; |
5061a885 | 5217 | |
66eb6687 AM |
5218 | free (old_tab); |
5219 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
5220 | alloc_mark); | |
5221 | if (nondeflt_vers != NULL) | |
5222 | free (nondeflt_vers); | |
5223 | return TRUE; | |
5224 | } | |
2de92251 | 5225 | |
66eb6687 AM |
5226 | if (old_tab != NULL) |
5227 | { | |
e5034e59 | 5228 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 5229 | goto error_free_vers; |
66eb6687 AM |
5230 | free (old_tab); |
5231 | old_tab = NULL; | |
5232 | } | |
5233 | ||
c6e8a9a8 L |
5234 | /* Now that all the symbols from this input file are created, if |
5235 | not performing a relocatable link, handle .symver foo, foo@BAR | |
5236 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 5237 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 5238 | { |
ef53be89 | 5239 | size_t cnt, symidx; |
4ad4eba5 AM |
5240 | |
5241 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
5242 | { | |
5243 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
5244 | char *shortname, *p; | |
5245 | ||
5246 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
5247 | if (p == NULL | |
5248 | || (h->root.type != bfd_link_hash_defined | |
5249 | && h->root.type != bfd_link_hash_defweak)) | |
5250 | continue; | |
5251 | ||
5252 | amt = p - h->root.root.string; | |
a50b1753 | 5253 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
5254 | if (!shortname) |
5255 | goto error_free_vers; | |
4ad4eba5 AM |
5256 | memcpy (shortname, h->root.root.string, amt); |
5257 | shortname[amt] = '\0'; | |
5258 | ||
5259 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 5260 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
5261 | FALSE, FALSE, FALSE); |
5262 | if (hi != NULL | |
5263 | && hi->root.type == h->root.type | |
5264 | && hi->root.u.def.value == h->root.u.def.value | |
5265 | && hi->root.u.def.section == h->root.u.def.section) | |
5266 | { | |
5267 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
5268 | hi->root.type = bfd_link_hash_indirect; | |
5269 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 5270 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
5271 | sym_hash = elf_sym_hashes (abfd); |
5272 | if (sym_hash) | |
5273 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
5274 | if (sym_hash[symidx] == hi) | |
5275 | { | |
5276 | sym_hash[symidx] = h; | |
5277 | break; | |
5278 | } | |
5279 | } | |
5280 | free (shortname); | |
5281 | } | |
5282 | free (nondeflt_vers); | |
5283 | nondeflt_vers = NULL; | |
5284 | } | |
5285 | ||
60d67dc8 | 5286 | /* Now set the alias field correctly for all the weak defined |
4ad4eba5 AM |
5287 | symbols we found. The only way to do this is to search all the |
5288 | symbols. Since we only need the information for non functions in | |
5289 | dynamic objects, that's the only time we actually put anything on | |
5290 | the list WEAKS. We need this information so that if a regular | |
5291 | object refers to a symbol defined weakly in a dynamic object, the | |
5292 | real symbol in the dynamic object is also put in the dynamic | |
5293 | symbols; we also must arrange for both symbols to point to the | |
5294 | same memory location. We could handle the general case of symbol | |
5295 | aliasing, but a general symbol alias can only be generated in | |
5296 | assembler code, handling it correctly would be very time | |
5297 | consuming, and other ELF linkers don't handle general aliasing | |
5298 | either. */ | |
5299 | if (weaks != NULL) | |
5300 | { | |
5301 | struct elf_link_hash_entry **hpp; | |
5302 | struct elf_link_hash_entry **hppend; | |
5303 | struct elf_link_hash_entry **sorted_sym_hash; | |
5304 | struct elf_link_hash_entry *h; | |
5305 | size_t sym_count; | |
5306 | ||
5307 | /* Since we have to search the whole symbol list for each weak | |
5308 | defined symbol, search time for N weak defined symbols will be | |
5309 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
5310 | amt = extsymcount; |
5311 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 5312 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
5313 | if (sorted_sym_hash == NULL) |
5314 | goto error_return; | |
5315 | sym_hash = sorted_sym_hash; | |
5316 | hpp = elf_sym_hashes (abfd); | |
5317 | hppend = hpp + extsymcount; | |
5318 | sym_count = 0; | |
5319 | for (; hpp < hppend; hpp++) | |
5320 | { | |
5321 | h = *hpp; | |
5322 | if (h != NULL | |
5323 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 5324 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
5325 | { |
5326 | *sym_hash = h; | |
5327 | sym_hash++; | |
5328 | sym_count++; | |
5329 | } | |
5330 | } | |
5331 | ||
5332 | qsort (sorted_sym_hash, sym_count, | |
5333 | sizeof (struct elf_link_hash_entry *), | |
5334 | elf_sort_symbol); | |
5335 | ||
5336 | while (weaks != NULL) | |
5337 | { | |
5338 | struct elf_link_hash_entry *hlook; | |
5339 | asection *slook; | |
5340 | bfd_vma vlook; | |
ed54588d | 5341 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5342 | |
5343 | hlook = weaks; | |
60d67dc8 AM |
5344 | weaks = hlook->u.alias; |
5345 | hlook->u.alias = NULL; | |
4ad4eba5 | 5346 | |
e3e53eed AM |
5347 | if (hlook->root.type != bfd_link_hash_defined |
5348 | && hlook->root.type != bfd_link_hash_defweak) | |
5349 | continue; | |
5350 | ||
4ad4eba5 AM |
5351 | slook = hlook->root.u.def.section; |
5352 | vlook = hlook->root.u.def.value; | |
5353 | ||
4ad4eba5 AM |
5354 | i = 0; |
5355 | j = sym_count; | |
14160578 | 5356 | while (i != j) |
4ad4eba5 AM |
5357 | { |
5358 | bfd_signed_vma vdiff; | |
5359 | idx = (i + j) / 2; | |
14160578 | 5360 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5361 | vdiff = vlook - h->root.u.def.value; |
5362 | if (vdiff < 0) | |
5363 | j = idx; | |
5364 | else if (vdiff > 0) | |
5365 | i = idx + 1; | |
5366 | else | |
5367 | { | |
d3435ae8 | 5368 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5369 | if (sdiff < 0) |
5370 | j = idx; | |
5371 | else if (sdiff > 0) | |
5372 | i = idx + 1; | |
5373 | else | |
14160578 | 5374 | break; |
4ad4eba5 AM |
5375 | } |
5376 | } | |
5377 | ||
5378 | /* We didn't find a value/section match. */ | |
14160578 | 5379 | if (i == j) |
4ad4eba5 AM |
5380 | continue; |
5381 | ||
14160578 AM |
5382 | /* With multiple aliases, or when the weak symbol is already |
5383 | strongly defined, we have multiple matching symbols and | |
5384 | the binary search above may land on any of them. Step | |
5385 | one past the matching symbol(s). */ | |
5386 | while (++idx != j) | |
5387 | { | |
5388 | h = sorted_sym_hash[idx]; | |
5389 | if (h->root.u.def.section != slook | |
5390 | || h->root.u.def.value != vlook) | |
5391 | break; | |
5392 | } | |
5393 | ||
5394 | /* Now look back over the aliases. Since we sorted by size | |
5395 | as well as value and section, we'll choose the one with | |
5396 | the largest size. */ | |
5397 | while (idx-- != i) | |
4ad4eba5 | 5398 | { |
14160578 | 5399 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5400 | |
5401 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5402 | if (h->root.u.def.section != slook |
5403 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5404 | break; |
5405 | else if (h != hlook) | |
5406 | { | |
60d67dc8 AM |
5407 | struct elf_link_hash_entry *t; |
5408 | ||
5409 | hlook->u.alias = h; | |
5410 | hlook->is_weakalias = 1; | |
5411 | t = h; | |
5412 | if (t->u.alias != NULL) | |
5413 | while (t->u.alias != h) | |
5414 | t = t->u.alias; | |
5415 | t->u.alias = hlook; | |
4ad4eba5 AM |
5416 | |
5417 | /* If the weak definition is in the list of dynamic | |
5418 | symbols, make sure the real definition is put | |
5419 | there as well. */ | |
5420 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5421 | { | |
c152c796 | 5422 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5423 | { |
5424 | err_free_sym_hash: | |
5425 | free (sorted_sym_hash); | |
5426 | goto error_return; | |
5427 | } | |
4ad4eba5 AM |
5428 | } |
5429 | ||
5430 | /* If the real definition is in the list of dynamic | |
5431 | symbols, make sure the weak definition is put | |
5432 | there as well. If we don't do this, then the | |
5433 | dynamic loader might not merge the entries for the | |
5434 | real definition and the weak definition. */ | |
5435 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5436 | { | |
c152c796 | 5437 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5438 | goto err_free_sym_hash; |
4ad4eba5 AM |
5439 | } |
5440 | break; | |
5441 | } | |
5442 | } | |
5443 | } | |
5444 | ||
5445 | free (sorted_sym_hash); | |
5446 | } | |
5447 | ||
33177bb1 AM |
5448 | if (bed->check_directives |
5449 | && !(*bed->check_directives) (abfd, info)) | |
5450 | return FALSE; | |
85fbca6a | 5451 | |
4ad4eba5 AM |
5452 | /* If this is a non-traditional link, try to optimize the handling |
5453 | of the .stab/.stabstr sections. */ | |
5454 | if (! dynamic | |
5455 | && ! info->traditional_format | |
66eb6687 | 5456 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5457 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5458 | { | |
5459 | asection *stabstr; | |
5460 | ||
5461 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5462 | if (stabstr != NULL) | |
5463 | { | |
5464 | bfd_size_type string_offset = 0; | |
5465 | asection *stab; | |
5466 | ||
5467 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5468 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5469 | && (!stab->name[5] || |
5470 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5471 | && (stab->flags & SEC_MERGE) == 0 | |
5472 | && !bfd_is_abs_section (stab->output_section)) | |
5473 | { | |
5474 | struct bfd_elf_section_data *secdata; | |
5475 | ||
5476 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5477 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5478 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5479 | &string_offset)) |
5480 | goto error_return; | |
5481 | if (secdata->sec_info) | |
dbaa2011 | 5482 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5483 | } |
5484 | } | |
5485 | } | |
5486 | ||
66eb6687 | 5487 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5488 | { |
5489 | /* Add this bfd to the loaded list. */ | |
5490 | struct elf_link_loaded_list *n; | |
5491 | ||
ca4be51c | 5492 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5493 | if (n == NULL) |
5494 | goto error_return; | |
5495 | n->abfd = abfd; | |
66eb6687 AM |
5496 | n->next = htab->loaded; |
5497 | htab->loaded = n; | |
4ad4eba5 AM |
5498 | } |
5499 | ||
5500 | return TRUE; | |
5501 | ||
5502 | error_free_vers: | |
66eb6687 AM |
5503 | if (old_tab != NULL) |
5504 | free (old_tab); | |
5b677558 AM |
5505 | if (old_strtab != NULL) |
5506 | free (old_strtab); | |
4ad4eba5 AM |
5507 | if (nondeflt_vers != NULL) |
5508 | free (nondeflt_vers); | |
5509 | if (extversym != NULL) | |
5510 | free (extversym); | |
5511 | error_free_sym: | |
5512 | if (isymbuf != NULL) | |
5513 | free (isymbuf); | |
5514 | error_return: | |
5515 | return FALSE; | |
5516 | } | |
5517 | ||
8387904d AM |
5518 | /* Return the linker hash table entry of a symbol that might be |
5519 | satisfied by an archive symbol. Return -1 on error. */ | |
5520 | ||
5521 | struct elf_link_hash_entry * | |
5522 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5523 | struct bfd_link_info *info, | |
5524 | const char *name) | |
5525 | { | |
5526 | struct elf_link_hash_entry *h; | |
5527 | char *p, *copy; | |
5528 | size_t len, first; | |
5529 | ||
2a41f396 | 5530 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5531 | if (h != NULL) |
5532 | return h; | |
5533 | ||
5534 | /* If this is a default version (the name contains @@), look up the | |
5535 | symbol again with only one `@' as well as without the version. | |
5536 | The effect is that references to the symbol with and without the | |
5537 | version will be matched by the default symbol in the archive. */ | |
5538 | ||
5539 | p = strchr (name, ELF_VER_CHR); | |
5540 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5541 | return h; | |
5542 | ||
5543 | /* First check with only one `@'. */ | |
5544 | len = strlen (name); | |
a50b1753 | 5545 | copy = (char *) bfd_alloc (abfd, len); |
8387904d | 5546 | if (copy == NULL) |
e99955cd | 5547 | return (struct elf_link_hash_entry *) -1; |
8387904d AM |
5548 | |
5549 | first = p - name + 1; | |
5550 | memcpy (copy, name, first); | |
5551 | memcpy (copy + first, name + first + 1, len - first); | |
5552 | ||
2a41f396 | 5553 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5554 | if (h == NULL) |
5555 | { | |
5556 | /* We also need to check references to the symbol without the | |
5557 | version. */ | |
5558 | copy[first - 1] = '\0'; | |
5559 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5560 | FALSE, FALSE, TRUE); |
8387904d AM |
5561 | } |
5562 | ||
5563 | bfd_release (abfd, copy); | |
5564 | return h; | |
5565 | } | |
5566 | ||
0ad989f9 | 5567 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5568 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5569 | handle versioned symbols. | |
0ad989f9 L |
5570 | |
5571 | Fortunately, ELF archive handling is simpler than that done by | |
5572 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5573 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5574 | symbol is currently undefined, we know that we must pull in that | |
5575 | object file. | |
5576 | ||
5577 | Unfortunately, we do have to make multiple passes over the symbol | |
5578 | table until nothing further is resolved. */ | |
5579 | ||
4ad4eba5 AM |
5580 | static bfd_boolean |
5581 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5582 | { |
5583 | symindex c; | |
13e570f8 | 5584 | unsigned char *included = NULL; |
0ad989f9 L |
5585 | carsym *symdefs; |
5586 | bfd_boolean loop; | |
5587 | bfd_size_type amt; | |
8387904d AM |
5588 | const struct elf_backend_data *bed; |
5589 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5590 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5591 | |
5592 | if (! bfd_has_map (abfd)) | |
5593 | { | |
5594 | /* An empty archive is a special case. */ | |
5595 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5596 | return TRUE; | |
5597 | bfd_set_error (bfd_error_no_armap); | |
5598 | return FALSE; | |
5599 | } | |
5600 | ||
5601 | /* Keep track of all symbols we know to be already defined, and all | |
5602 | files we know to be already included. This is to speed up the | |
5603 | second and subsequent passes. */ | |
5604 | c = bfd_ardata (abfd)->symdef_count; | |
5605 | if (c == 0) | |
5606 | return TRUE; | |
5607 | amt = c; | |
13e570f8 AM |
5608 | amt *= sizeof (*included); |
5609 | included = (unsigned char *) bfd_zmalloc (amt); | |
5610 | if (included == NULL) | |
5611 | return FALSE; | |
0ad989f9 L |
5612 | |
5613 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5614 | bed = get_elf_backend_data (abfd); |
5615 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5616 | |
5617 | do | |
5618 | { | |
5619 | file_ptr last; | |
5620 | symindex i; | |
5621 | carsym *symdef; | |
5622 | carsym *symdefend; | |
5623 | ||
5624 | loop = FALSE; | |
5625 | last = -1; | |
5626 | ||
5627 | symdef = symdefs; | |
5628 | symdefend = symdef + c; | |
5629 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5630 | { | |
5631 | struct elf_link_hash_entry *h; | |
5632 | bfd *element; | |
5633 | struct bfd_link_hash_entry *undefs_tail; | |
5634 | symindex mark; | |
5635 | ||
13e570f8 | 5636 | if (included[i]) |
0ad989f9 L |
5637 | continue; |
5638 | if (symdef->file_offset == last) | |
5639 | { | |
5640 | included[i] = TRUE; | |
5641 | continue; | |
5642 | } | |
5643 | ||
8387904d | 5644 | h = archive_symbol_lookup (abfd, info, symdef->name); |
e99955cd | 5645 | if (h == (struct elf_link_hash_entry *) -1) |
8387904d | 5646 | goto error_return; |
0ad989f9 L |
5647 | |
5648 | if (h == NULL) | |
5649 | continue; | |
5650 | ||
5651 | if (h->root.type == bfd_link_hash_common) | |
5652 | { | |
5653 | /* We currently have a common symbol. The archive map contains | |
5654 | a reference to this symbol, so we may want to include it. We | |
5655 | only want to include it however, if this archive element | |
5656 | contains a definition of the symbol, not just another common | |
5657 | declaration of it. | |
5658 | ||
5659 | Unfortunately some archivers (including GNU ar) will put | |
5660 | declarations of common symbols into their archive maps, as | |
5661 | well as real definitions, so we cannot just go by the archive | |
5662 | map alone. Instead we must read in the element's symbol | |
5663 | table and check that to see what kind of symbol definition | |
5664 | this is. */ | |
5665 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5666 | continue; | |
5667 | } | |
5668 | else if (h->root.type != bfd_link_hash_undefined) | |
5669 | { | |
5670 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5671 | /* Symbol must be defined. Don't check it again. */ |
5672 | included[i] = TRUE; | |
0ad989f9 L |
5673 | continue; |
5674 | } | |
5675 | ||
5676 | /* We need to include this archive member. */ | |
5677 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5678 | if (element == NULL) | |
5679 | goto error_return; | |
5680 | ||
5681 | if (! bfd_check_format (element, bfd_object)) | |
5682 | goto error_return; | |
5683 | ||
0ad989f9 L |
5684 | undefs_tail = info->hash->undefs_tail; |
5685 | ||
0e144ba7 AM |
5686 | if (!(*info->callbacks |
5687 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5688 | continue; |
0e144ba7 | 5689 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5690 | goto error_return; |
5691 | ||
5692 | /* If there are any new undefined symbols, we need to make | |
5693 | another pass through the archive in order to see whether | |
5694 | they can be defined. FIXME: This isn't perfect, because | |
5695 | common symbols wind up on undefs_tail and because an | |
5696 | undefined symbol which is defined later on in this pass | |
5697 | does not require another pass. This isn't a bug, but it | |
5698 | does make the code less efficient than it could be. */ | |
5699 | if (undefs_tail != info->hash->undefs_tail) | |
5700 | loop = TRUE; | |
5701 | ||
5702 | /* Look backward to mark all symbols from this object file | |
5703 | which we have already seen in this pass. */ | |
5704 | mark = i; | |
5705 | do | |
5706 | { | |
5707 | included[mark] = TRUE; | |
5708 | if (mark == 0) | |
5709 | break; | |
5710 | --mark; | |
5711 | } | |
5712 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5713 | ||
5714 | /* We mark subsequent symbols from this object file as we go | |
5715 | on through the loop. */ | |
5716 | last = symdef->file_offset; | |
5717 | } | |
5718 | } | |
5719 | while (loop); | |
5720 | ||
0ad989f9 L |
5721 | free (included); |
5722 | ||
5723 | return TRUE; | |
5724 | ||
5725 | error_return: | |
0ad989f9 L |
5726 | if (included != NULL) |
5727 | free (included); | |
5728 | return FALSE; | |
5729 | } | |
4ad4eba5 AM |
5730 | |
5731 | /* Given an ELF BFD, add symbols to the global hash table as | |
5732 | appropriate. */ | |
5733 | ||
5734 | bfd_boolean | |
5735 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5736 | { | |
5737 | switch (bfd_get_format (abfd)) | |
5738 | { | |
5739 | case bfd_object: | |
5740 | return elf_link_add_object_symbols (abfd, info); | |
5741 | case bfd_archive: | |
5742 | return elf_link_add_archive_symbols (abfd, info); | |
5743 | default: | |
5744 | bfd_set_error (bfd_error_wrong_format); | |
5745 | return FALSE; | |
5746 | } | |
5747 | } | |
5a580b3a | 5748 | \f |
14b1c01e AM |
5749 | struct hash_codes_info |
5750 | { | |
5751 | unsigned long *hashcodes; | |
5752 | bfd_boolean error; | |
5753 | }; | |
a0c8462f | 5754 | |
5a580b3a AM |
5755 | /* This function will be called though elf_link_hash_traverse to store |
5756 | all hash value of the exported symbols in an array. */ | |
5757 | ||
5758 | static bfd_boolean | |
5759 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5760 | { | |
a50b1753 | 5761 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5762 | const char *name; |
5a580b3a AM |
5763 | unsigned long ha; |
5764 | char *alc = NULL; | |
5765 | ||
5a580b3a AM |
5766 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5767 | if (h->dynindx == -1) | |
5768 | return TRUE; | |
5769 | ||
5770 | name = h->root.root.string; | |
422f1182 | 5771 | if (h->versioned >= versioned) |
5a580b3a | 5772 | { |
422f1182 L |
5773 | char *p = strchr (name, ELF_VER_CHR); |
5774 | if (p != NULL) | |
14b1c01e | 5775 | { |
422f1182 L |
5776 | alc = (char *) bfd_malloc (p - name + 1); |
5777 | if (alc == NULL) | |
5778 | { | |
5779 | inf->error = TRUE; | |
5780 | return FALSE; | |
5781 | } | |
5782 | memcpy (alc, name, p - name); | |
5783 | alc[p - name] = '\0'; | |
5784 | name = alc; | |
14b1c01e | 5785 | } |
5a580b3a AM |
5786 | } |
5787 | ||
5788 | /* Compute the hash value. */ | |
5789 | ha = bfd_elf_hash (name); | |
5790 | ||
5791 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5792 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5793 | |
5794 | /* And store it in the struct so that we can put it in the hash table | |
5795 | later. */ | |
f6e332e6 | 5796 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5797 | |
5798 | if (alc != NULL) | |
5799 | free (alc); | |
5800 | ||
5801 | return TRUE; | |
5802 | } | |
5803 | ||
fdc90cb4 JJ |
5804 | struct collect_gnu_hash_codes |
5805 | { | |
5806 | bfd *output_bfd; | |
5807 | const struct elf_backend_data *bed; | |
5808 | unsigned long int nsyms; | |
5809 | unsigned long int maskbits; | |
5810 | unsigned long int *hashcodes; | |
5811 | unsigned long int *hashval; | |
5812 | unsigned long int *indx; | |
5813 | unsigned long int *counts; | |
5814 | bfd_vma *bitmask; | |
5815 | bfd_byte *contents; | |
5816 | long int min_dynindx; | |
5817 | unsigned long int bucketcount; | |
5818 | unsigned long int symindx; | |
5819 | long int local_indx; | |
5820 | long int shift1, shift2; | |
5821 | unsigned long int mask; | |
14b1c01e | 5822 | bfd_boolean error; |
fdc90cb4 JJ |
5823 | }; |
5824 | ||
5825 | /* This function will be called though elf_link_hash_traverse to store | |
5826 | all hash value of the exported symbols in an array. */ | |
5827 | ||
5828 | static bfd_boolean | |
5829 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5830 | { | |
a50b1753 | 5831 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5832 | const char *name; |
fdc90cb4 JJ |
5833 | unsigned long ha; |
5834 | char *alc = NULL; | |
5835 | ||
fdc90cb4 JJ |
5836 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5837 | if (h->dynindx == -1) | |
5838 | return TRUE; | |
5839 | ||
5840 | /* Ignore also local symbols and undefined symbols. */ | |
5841 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5842 | return TRUE; | |
5843 | ||
5844 | name = h->root.root.string; | |
422f1182 | 5845 | if (h->versioned >= versioned) |
fdc90cb4 | 5846 | { |
422f1182 L |
5847 | char *p = strchr (name, ELF_VER_CHR); |
5848 | if (p != NULL) | |
14b1c01e | 5849 | { |
422f1182 L |
5850 | alc = (char *) bfd_malloc (p - name + 1); |
5851 | if (alc == NULL) | |
5852 | { | |
5853 | s->error = TRUE; | |
5854 | return FALSE; | |
5855 | } | |
5856 | memcpy (alc, name, p - name); | |
5857 | alc[p - name] = '\0'; | |
5858 | name = alc; | |
14b1c01e | 5859 | } |
fdc90cb4 JJ |
5860 | } |
5861 | ||
5862 | /* Compute the hash value. */ | |
5863 | ha = bfd_elf_gnu_hash (name); | |
5864 | ||
5865 | /* Store the found hash value in the array for compute_bucket_count, | |
5866 | and also for .dynsym reordering purposes. */ | |
5867 | s->hashcodes[s->nsyms] = ha; | |
5868 | s->hashval[h->dynindx] = ha; | |
5869 | ++s->nsyms; | |
5870 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5871 | s->min_dynindx = h->dynindx; | |
5872 | ||
5873 | if (alc != NULL) | |
5874 | free (alc); | |
5875 | ||
5876 | return TRUE; | |
5877 | } | |
5878 | ||
5879 | /* This function will be called though elf_link_hash_traverse to do | |
5880 | final dynaminc symbol renumbering. */ | |
5881 | ||
5882 | static bfd_boolean | |
5883 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5884 | { | |
a50b1753 | 5885 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5886 | unsigned long int bucket; |
5887 | unsigned long int val; | |
5888 | ||
fdc90cb4 JJ |
5889 | /* Ignore indirect symbols. */ |
5890 | if (h->dynindx == -1) | |
5891 | return TRUE; | |
5892 | ||
5893 | /* Ignore also local symbols and undefined symbols. */ | |
5894 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5895 | { | |
5896 | if (h->dynindx >= s->min_dynindx) | |
5897 | h->dynindx = s->local_indx++; | |
5898 | return TRUE; | |
5899 | } | |
5900 | ||
5901 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5902 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5903 | & ((s->maskbits >> s->shift1) - 1); | |
5904 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5905 | s->bitmask[val] | |
5906 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5907 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5908 | if (s->counts[bucket] == 1) | |
5909 | /* Last element terminates the chain. */ | |
5910 | val |= 1; | |
5911 | bfd_put_32 (s->output_bfd, val, | |
5912 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5913 | --s->counts[bucket]; | |
5914 | h->dynindx = s->indx[bucket]++; | |
5915 | return TRUE; | |
5916 | } | |
5917 | ||
5918 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5919 | ||
5920 | bfd_boolean | |
5921 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5922 | { | |
5923 | return !(h->forced_local | |
5924 | || h->root.type == bfd_link_hash_undefined | |
5925 | || h->root.type == bfd_link_hash_undefweak | |
5926 | || ((h->root.type == bfd_link_hash_defined | |
5927 | || h->root.type == bfd_link_hash_defweak) | |
5928 | && h->root.u.def.section->output_section == NULL)); | |
5929 | } | |
5930 | ||
5a580b3a AM |
5931 | /* Array used to determine the number of hash table buckets to use |
5932 | based on the number of symbols there are. If there are fewer than | |
5933 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5934 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5935 | than 32771 buckets. */ | |
5936 | ||
5937 | static const size_t elf_buckets[] = | |
5938 | { | |
5939 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5940 | 16411, 32771, 0 | |
5941 | }; | |
5942 | ||
5943 | /* Compute bucket count for hashing table. We do not use a static set | |
5944 | of possible tables sizes anymore. Instead we determine for all | |
5945 | possible reasonable sizes of the table the outcome (i.e., the | |
5946 | number of collisions etc) and choose the best solution. The | |
5947 | weighting functions are not too simple to allow the table to grow | |
5948 | without bounds. Instead one of the weighting factors is the size. | |
5949 | Therefore the result is always a good payoff between few collisions | |
5950 | (= short chain lengths) and table size. */ | |
5951 | static size_t | |
b20dd2ce | 5952 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5953 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5954 | unsigned long int nsyms, | |
5955 | int gnu_hash) | |
5a580b3a | 5956 | { |
5a580b3a | 5957 | size_t best_size = 0; |
5a580b3a | 5958 | unsigned long int i; |
5a580b3a | 5959 | |
5a580b3a AM |
5960 | /* We have a problem here. The following code to optimize the table |
5961 | size requires an integer type with more the 32 bits. If | |
5962 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
5963 | #ifdef BFD_HOST_U_64_BIT | |
5964 | if (info->optimize) | |
5965 | { | |
5a580b3a AM |
5966 | size_t minsize; |
5967 | size_t maxsize; | |
5968 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 5969 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 5970 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 5971 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 5972 | unsigned long int *counts; |
d40f3da9 | 5973 | bfd_size_type amt; |
0883b6e0 | 5974 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
5975 | |
5976 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
5977 | that the hashing table must at least have NSYMS/4 and at most | |
5978 | 2*NSYMS buckets. */ | |
5979 | minsize = nsyms / 4; | |
5980 | if (minsize == 0) | |
5981 | minsize = 1; | |
5982 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
5983 | if (gnu_hash) |
5984 | { | |
5985 | if (minsize < 2) | |
5986 | minsize = 2; | |
5987 | if ((best_size & 31) == 0) | |
5988 | ++best_size; | |
5989 | } | |
5a580b3a AM |
5990 | |
5991 | /* Create array where we count the collisions in. We must use bfd_malloc | |
5992 | since the size could be large. */ | |
5993 | amt = maxsize; | |
5994 | amt *= sizeof (unsigned long int); | |
a50b1753 | 5995 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 5996 | if (counts == NULL) |
fdc90cb4 | 5997 | return 0; |
5a580b3a AM |
5998 | |
5999 | /* Compute the "optimal" size for the hash table. The criteria is a | |
6000 | minimal chain length. The minor criteria is (of course) the size | |
6001 | of the table. */ | |
6002 | for (i = minsize; i < maxsize; ++i) | |
6003 | { | |
6004 | /* Walk through the array of hashcodes and count the collisions. */ | |
6005 | BFD_HOST_U_64_BIT max; | |
6006 | unsigned long int j; | |
6007 | unsigned long int fact; | |
6008 | ||
fdc90cb4 JJ |
6009 | if (gnu_hash && (i & 31) == 0) |
6010 | continue; | |
6011 | ||
5a580b3a AM |
6012 | memset (counts, '\0', i * sizeof (unsigned long int)); |
6013 | ||
6014 | /* Determine how often each hash bucket is used. */ | |
6015 | for (j = 0; j < nsyms; ++j) | |
6016 | ++counts[hashcodes[j] % i]; | |
6017 | ||
6018 | /* For the weight function we need some information about the | |
6019 | pagesize on the target. This is information need not be 100% | |
6020 | accurate. Since this information is not available (so far) we | |
6021 | define it here to a reasonable default value. If it is crucial | |
6022 | to have a better value some day simply define this value. */ | |
6023 | # ifndef BFD_TARGET_PAGESIZE | |
6024 | # define BFD_TARGET_PAGESIZE (4096) | |
6025 | # endif | |
6026 | ||
fdc90cb4 JJ |
6027 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
6028 | and the chains. */ | |
6029 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
6030 | |
6031 | # if 1 | |
6032 | /* Variant 1: optimize for short chains. We add the squares | |
6033 | of all the chain lengths (which favors many small chain | |
6034 | over a few long chains). */ | |
6035 | for (j = 0; j < i; ++j) | |
6036 | max += counts[j] * counts[j]; | |
6037 | ||
6038 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 6039 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6040 | max *= fact * fact; |
6041 | # else | |
6042 | /* Variant 2: Optimize a lot more for small table. Here we | |
6043 | also add squares of the size but we also add penalties for | |
6044 | empty slots (the +1 term). */ | |
6045 | for (j = 0; j < i; ++j) | |
6046 | max += (1 + counts[j]) * (1 + counts[j]); | |
6047 | ||
6048 | /* The overall size of the table is considered, but not as | |
6049 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 6050 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6051 | max *= fact; |
6052 | # endif | |
6053 | ||
6054 | /* Compare with current best results. */ | |
6055 | if (max < best_chlen) | |
6056 | { | |
6057 | best_chlen = max; | |
6058 | best_size = i; | |
ca4be51c | 6059 | no_improvement_count = 0; |
5a580b3a | 6060 | } |
0883b6e0 NC |
6061 | /* PR 11843: Avoid futile long searches for the best bucket size |
6062 | when there are a large number of symbols. */ | |
6063 | else if (++no_improvement_count == 100) | |
6064 | break; | |
5a580b3a AM |
6065 | } |
6066 | ||
6067 | free (counts); | |
6068 | } | |
6069 | else | |
6070 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
6071 | { | |
6072 | /* This is the fallback solution if no 64bit type is available or if we | |
6073 | are not supposed to spend much time on optimizations. We select the | |
6074 | bucket count using a fixed set of numbers. */ | |
6075 | for (i = 0; elf_buckets[i] != 0; i++) | |
6076 | { | |
6077 | best_size = elf_buckets[i]; | |
fdc90cb4 | 6078 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
6079 | break; |
6080 | } | |
fdc90cb4 JJ |
6081 | if (gnu_hash && best_size < 2) |
6082 | best_size = 2; | |
5a580b3a AM |
6083 | } |
6084 | ||
5a580b3a AM |
6085 | return best_size; |
6086 | } | |
6087 | ||
d0bf826b AM |
6088 | /* Size any SHT_GROUP section for ld -r. */ |
6089 | ||
6090 | bfd_boolean | |
6091 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
6092 | { | |
6093 | bfd *ibfd; | |
57963c05 | 6094 | asection *s; |
d0bf826b | 6095 | |
c72f2fb2 | 6096 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b | 6097 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
57963c05 AM |
6098 | && (s = ibfd->sections) != NULL |
6099 | && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
d0bf826b AM |
6100 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) |
6101 | return FALSE; | |
6102 | return TRUE; | |
6103 | } | |
6104 | ||
04c3a755 NS |
6105 | /* Set a default stack segment size. The value in INFO wins. If it |
6106 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
6107 | undefined it is initialized. */ | |
6108 | ||
6109 | bfd_boolean | |
6110 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
6111 | struct bfd_link_info *info, | |
6112 | const char *legacy_symbol, | |
6113 | bfd_vma default_size) | |
6114 | { | |
6115 | struct elf_link_hash_entry *h = NULL; | |
6116 | ||
6117 | /* Look for legacy symbol. */ | |
6118 | if (legacy_symbol) | |
6119 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
6120 | FALSE, FALSE, FALSE); | |
6121 | if (h && (h->root.type == bfd_link_hash_defined | |
6122 | || h->root.type == bfd_link_hash_defweak) | |
6123 | && h->def_regular | |
6124 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
6125 | { | |
6126 | /* The symbol has no type if specified on the command line. */ | |
6127 | h->type = STT_OBJECT; | |
6128 | if (info->stacksize) | |
695344c0 | 6129 | /* xgettext:c-format */ |
871b3ab2 | 6130 | _bfd_error_handler (_("%pB: stack size specified and %s set"), |
4eca0228 | 6131 | output_bfd, legacy_symbol); |
04c3a755 | 6132 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 6133 | /* xgettext:c-format */ |
871b3ab2 | 6134 | _bfd_error_handler (_("%pB: %s not absolute"), |
4eca0228 | 6135 | output_bfd, legacy_symbol); |
04c3a755 NS |
6136 | else |
6137 | info->stacksize = h->root.u.def.value; | |
6138 | } | |
6139 | ||
6140 | if (!info->stacksize) | |
6141 | /* If the user didn't set a size, or explicitly inhibit the | |
6142 | size, set it now. */ | |
6143 | info->stacksize = default_size; | |
6144 | ||
6145 | /* Provide the legacy symbol, if it is referenced. */ | |
6146 | if (h && (h->root.type == bfd_link_hash_undefined | |
6147 | || h->root.type == bfd_link_hash_undefweak)) | |
6148 | { | |
6149 | struct bfd_link_hash_entry *bh = NULL; | |
6150 | ||
6151 | if (!(_bfd_generic_link_add_one_symbol | |
6152 | (info, output_bfd, legacy_symbol, | |
6153 | BSF_GLOBAL, bfd_abs_section_ptr, | |
6154 | info->stacksize >= 0 ? info->stacksize : 0, | |
6155 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
6156 | return FALSE; | |
6157 | ||
6158 | h = (struct elf_link_hash_entry *) bh; | |
6159 | h->def_regular = 1; | |
6160 | h->type = STT_OBJECT; | |
6161 | } | |
6162 | ||
6163 | return TRUE; | |
6164 | } | |
6165 | ||
b531344c MR |
6166 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
6167 | ||
6168 | struct elf_gc_sweep_symbol_info | |
6169 | { | |
6170 | struct bfd_link_info *info; | |
6171 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
6172 | bfd_boolean); | |
6173 | }; | |
6174 | ||
6175 | static bfd_boolean | |
6176 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) | |
6177 | { | |
6178 | if (!h->mark | |
6179 | && (((h->root.type == bfd_link_hash_defined | |
6180 | || h->root.type == bfd_link_hash_defweak) | |
6181 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) | |
6182 | && h->root.u.def.section->gc_mark)) | |
6183 | || h->root.type == bfd_link_hash_undefined | |
6184 | || h->root.type == bfd_link_hash_undefweak)) | |
6185 | { | |
6186 | struct elf_gc_sweep_symbol_info *inf; | |
6187 | ||
6188 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
6189 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
6190 | h->def_regular = 0; | |
6191 | h->ref_regular = 0; | |
6192 | h->ref_regular_nonweak = 0; | |
6193 | } | |
6194 | ||
6195 | return TRUE; | |
6196 | } | |
6197 | ||
5a580b3a AM |
6198 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
6199 | called by the ELF linker emulation before_allocation routine. We | |
6200 | must set the sizes of the sections before the linker sets the | |
6201 | addresses of the various sections. */ | |
6202 | ||
6203 | bfd_boolean | |
6204 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
6205 | const char *soname, | |
6206 | const char *rpath, | |
6207 | const char *filter_shlib, | |
7ee314fa AM |
6208 | const char *audit, |
6209 | const char *depaudit, | |
5a580b3a AM |
6210 | const char * const *auxiliary_filters, |
6211 | struct bfd_link_info *info, | |
fd91d419 | 6212 | asection **sinterpptr) |
5a580b3a | 6213 | { |
5a580b3a AM |
6214 | bfd *dynobj; |
6215 | const struct elf_backend_data *bed; | |
5a580b3a AM |
6216 | |
6217 | *sinterpptr = NULL; | |
6218 | ||
5a580b3a AM |
6219 | if (!is_elf_hash_table (info->hash)) |
6220 | return TRUE; | |
6221 | ||
5a580b3a AM |
6222 | dynobj = elf_hash_table (info)->dynobj; |
6223 | ||
9a2a56cc | 6224 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6225 | { |
902e9fc7 MR |
6226 | struct bfd_elf_version_tree *verdefs; |
6227 | struct elf_info_failed asvinfo; | |
5a580b3a AM |
6228 | struct bfd_elf_version_tree *t; |
6229 | struct bfd_elf_version_expr *d; | |
902e9fc7 | 6230 | asection *s; |
e6699019 | 6231 | size_t soname_indx; |
7ee314fa | 6232 | |
5a580b3a AM |
6233 | /* If we are supposed to export all symbols into the dynamic symbol |
6234 | table (this is not the normal case), then do so. */ | |
55255dae | 6235 | if (info->export_dynamic |
0e1862bb | 6236 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a | 6237 | { |
3d13f3e9 AM |
6238 | struct elf_info_failed eif; |
6239 | ||
6240 | eif.info = info; | |
6241 | eif.failed = FALSE; | |
5a580b3a AM |
6242 | elf_link_hash_traverse (elf_hash_table (info), |
6243 | _bfd_elf_export_symbol, | |
6244 | &eif); | |
6245 | if (eif.failed) | |
6246 | return FALSE; | |
6247 | } | |
6248 | ||
e6699019 L |
6249 | if (soname != NULL) |
6250 | { | |
6251 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6252 | soname, TRUE); | |
6253 | if (soname_indx == (size_t) -1 | |
6254 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
6255 | return FALSE; | |
6256 | } | |
6257 | else | |
6258 | soname_indx = (size_t) -1; | |
6259 | ||
5a580b3a | 6260 | /* Make all global versions with definition. */ |
fd91d419 | 6261 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6262 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6263 | if (!d->symver && d->literal) |
5a580b3a AM |
6264 | { |
6265 | const char *verstr, *name; | |
6266 | size_t namelen, verlen, newlen; | |
93252b1c | 6267 | char *newname, *p, leading_char; |
5a580b3a AM |
6268 | struct elf_link_hash_entry *newh; |
6269 | ||
93252b1c | 6270 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6271 | name = d->pattern; |
93252b1c | 6272 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6273 | verstr = t->name; |
6274 | verlen = strlen (verstr); | |
6275 | newlen = namelen + verlen + 3; | |
6276 | ||
a50b1753 | 6277 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6278 | if (newname == NULL) |
6279 | return FALSE; | |
93252b1c MF |
6280 | newname[0] = leading_char; |
6281 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6282 | |
6283 | /* Check the hidden versioned definition. */ | |
6284 | p = newname + namelen; | |
6285 | *p++ = ELF_VER_CHR; | |
6286 | memcpy (p, verstr, verlen + 1); | |
6287 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6288 | newname, FALSE, FALSE, | |
6289 | FALSE); | |
6290 | if (newh == NULL | |
6291 | || (newh->root.type != bfd_link_hash_defined | |
6292 | && newh->root.type != bfd_link_hash_defweak)) | |
6293 | { | |
6294 | /* Check the default versioned definition. */ | |
6295 | *p++ = ELF_VER_CHR; | |
6296 | memcpy (p, verstr, verlen + 1); | |
6297 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6298 | newname, FALSE, FALSE, | |
6299 | FALSE); | |
6300 | } | |
6301 | free (newname); | |
6302 | ||
6303 | /* Mark this version if there is a definition and it is | |
6304 | not defined in a shared object. */ | |
6305 | if (newh != NULL | |
f5385ebf | 6306 | && !newh->def_dynamic |
5a580b3a AM |
6307 | && (newh->root.type == bfd_link_hash_defined |
6308 | || newh->root.type == bfd_link_hash_defweak)) | |
6309 | d->symver = 1; | |
6310 | } | |
6311 | ||
6312 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6313 | asvinfo.info = info; |
5a580b3a AM |
6314 | asvinfo.failed = FALSE; |
6315 | ||
6316 | elf_link_hash_traverse (elf_hash_table (info), | |
6317 | _bfd_elf_link_assign_sym_version, | |
6318 | &asvinfo); | |
6319 | if (asvinfo.failed) | |
6320 | return FALSE; | |
6321 | ||
6322 | if (!info->allow_undefined_version) | |
6323 | { | |
6324 | /* Check if all global versions have a definition. */ | |
3d13f3e9 | 6325 | bfd_boolean all_defined = TRUE; |
fd91d419 | 6326 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6327 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6328 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6329 | { |
4eca0228 | 6330 | _bfd_error_handler |
5a580b3a AM |
6331 | (_("%s: undefined version: %s"), |
6332 | d->pattern, t->name); | |
6333 | all_defined = FALSE; | |
6334 | } | |
6335 | ||
6336 | if (!all_defined) | |
6337 | { | |
6338 | bfd_set_error (bfd_error_bad_value); | |
6339 | return FALSE; | |
6340 | } | |
6341 | } | |
6342 | ||
902e9fc7 MR |
6343 | /* Set up the version definition section. */ |
6344 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); | |
6345 | BFD_ASSERT (s != NULL); | |
5a580b3a | 6346 | |
902e9fc7 MR |
6347 | /* We may have created additional version definitions if we are |
6348 | just linking a regular application. */ | |
6349 | verdefs = info->version_info; | |
5a580b3a | 6350 | |
902e9fc7 MR |
6351 | /* Skip anonymous version tag. */ |
6352 | if (verdefs != NULL && verdefs->vernum == 0) | |
6353 | verdefs = verdefs->next; | |
5a580b3a | 6354 | |
902e9fc7 MR |
6355 | if (verdefs == NULL && !info->create_default_symver) |
6356 | s->flags |= SEC_EXCLUDE; | |
6357 | else | |
5a580b3a | 6358 | { |
902e9fc7 MR |
6359 | unsigned int cdefs; |
6360 | bfd_size_type size; | |
6361 | bfd_byte *p; | |
6362 | Elf_Internal_Verdef def; | |
6363 | Elf_Internal_Verdaux defaux; | |
6364 | struct bfd_link_hash_entry *bh; | |
6365 | struct elf_link_hash_entry *h; | |
6366 | const char *name; | |
5a580b3a | 6367 | |
902e9fc7 MR |
6368 | cdefs = 0; |
6369 | size = 0; | |
5a580b3a | 6370 | |
902e9fc7 MR |
6371 | /* Make space for the base version. */ |
6372 | size += sizeof (Elf_External_Verdef); | |
6373 | size += sizeof (Elf_External_Verdaux); | |
6374 | ++cdefs; | |
6375 | ||
6376 | /* Make space for the default version. */ | |
6377 | if (info->create_default_symver) | |
6378 | { | |
6379 | size += sizeof (Elf_External_Verdef); | |
6380 | ++cdefs; | |
3e3b46e5 PB |
6381 | } |
6382 | ||
5a580b3a AM |
6383 | for (t = verdefs; t != NULL; t = t->next) |
6384 | { | |
6385 | struct bfd_elf_version_deps *n; | |
6386 | ||
a6cc6b3b RO |
6387 | /* Don't emit base version twice. */ |
6388 | if (t->vernum == 0) | |
6389 | continue; | |
6390 | ||
5a580b3a AM |
6391 | size += sizeof (Elf_External_Verdef); |
6392 | size += sizeof (Elf_External_Verdaux); | |
6393 | ++cdefs; | |
6394 | ||
6395 | for (n = t->deps; n != NULL; n = n->next) | |
6396 | size += sizeof (Elf_External_Verdaux); | |
6397 | } | |
6398 | ||
eea6121a | 6399 | s->size = size; |
a50b1753 | 6400 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6401 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6402 | return FALSE; |
6403 | ||
6404 | /* Fill in the version definition section. */ | |
6405 | ||
6406 | p = s->contents; | |
6407 | ||
6408 | def.vd_version = VER_DEF_CURRENT; | |
6409 | def.vd_flags = VER_FLG_BASE; | |
6410 | def.vd_ndx = 1; | |
6411 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6412 | if (info->create_default_symver) |
6413 | { | |
6414 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6415 | def.vd_next = sizeof (Elf_External_Verdef); | |
6416 | } | |
6417 | else | |
6418 | { | |
6419 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6420 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6421 | + sizeof (Elf_External_Verdaux)); | |
6422 | } | |
5a580b3a | 6423 | |
ef53be89 | 6424 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6425 | { |
6426 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6427 | soname_indx); | |
6428 | def.vd_hash = bfd_elf_hash (soname); | |
6429 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6430 | name = soname; |
5a580b3a AM |
6431 | } |
6432 | else | |
6433 | { | |
ef53be89 | 6434 | size_t indx; |
5a580b3a | 6435 | |
06084812 | 6436 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6437 | def.vd_hash = bfd_elf_hash (name); |
6438 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6439 | name, FALSE); | |
ef53be89 | 6440 | if (indx == (size_t) -1) |
5a580b3a AM |
6441 | return FALSE; |
6442 | defaux.vda_name = indx; | |
6443 | } | |
6444 | defaux.vda_next = 0; | |
6445 | ||
6446 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6447 | (Elf_External_Verdef *) p); | |
6448 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6449 | if (info->create_default_symver) |
6450 | { | |
6451 | /* Add a symbol representing this version. */ | |
6452 | bh = NULL; | |
6453 | if (! (_bfd_generic_link_add_one_symbol | |
6454 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6455 | 0, NULL, FALSE, | |
6456 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6457 | return FALSE; | |
6458 | h = (struct elf_link_hash_entry *) bh; | |
6459 | h->non_elf = 0; | |
6460 | h->def_regular = 1; | |
6461 | h->type = STT_OBJECT; | |
6462 | h->verinfo.vertree = NULL; | |
6463 | ||
6464 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6465 | return FALSE; | |
6466 | ||
6467 | /* Create a duplicate of the base version with the same | |
6468 | aux block, but different flags. */ | |
6469 | def.vd_flags = 0; | |
6470 | def.vd_ndx = 2; | |
6471 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6472 | if (verdefs) | |
6473 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6474 | + sizeof (Elf_External_Verdaux)); | |
6475 | else | |
6476 | def.vd_next = 0; | |
6477 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6478 | (Elf_External_Verdef *) p); | |
6479 | p += sizeof (Elf_External_Verdef); | |
6480 | } | |
5a580b3a AM |
6481 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6482 | (Elf_External_Verdaux *) p); | |
6483 | p += sizeof (Elf_External_Verdaux); | |
6484 | ||
6485 | for (t = verdefs; t != NULL; t = t->next) | |
6486 | { | |
6487 | unsigned int cdeps; | |
6488 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6489 | |
a6cc6b3b RO |
6490 | /* Don't emit the base version twice. */ |
6491 | if (t->vernum == 0) | |
6492 | continue; | |
6493 | ||
5a580b3a AM |
6494 | cdeps = 0; |
6495 | for (n = t->deps; n != NULL; n = n->next) | |
6496 | ++cdeps; | |
6497 | ||
6498 | /* Add a symbol representing this version. */ | |
6499 | bh = NULL; | |
6500 | if (! (_bfd_generic_link_add_one_symbol | |
6501 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6502 | 0, NULL, FALSE, | |
6503 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6504 | return FALSE; | |
6505 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6506 | h->non_elf = 0; |
6507 | h->def_regular = 1; | |
5a580b3a AM |
6508 | h->type = STT_OBJECT; |
6509 | h->verinfo.vertree = t; | |
6510 | ||
c152c796 | 6511 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6512 | return FALSE; |
6513 | ||
6514 | def.vd_version = VER_DEF_CURRENT; | |
6515 | def.vd_flags = 0; | |
6516 | if (t->globals.list == NULL | |
6517 | && t->locals.list == NULL | |
6518 | && ! t->used) | |
6519 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6520 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6521 | def.vd_cnt = cdeps + 1; |
6522 | def.vd_hash = bfd_elf_hash (t->name); | |
6523 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6524 | def.vd_next = 0; | |
a6cc6b3b RO |
6525 | |
6526 | /* If a basever node is next, it *must* be the last node in | |
6527 | the chain, otherwise Verdef construction breaks. */ | |
6528 | if (t->next != NULL && t->next->vernum == 0) | |
6529 | BFD_ASSERT (t->next->next == NULL); | |
6530 | ||
6531 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6532 | def.vd_next = (sizeof (Elf_External_Verdef) |
6533 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6534 | ||
6535 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6536 | (Elf_External_Verdef *) p); | |
6537 | p += sizeof (Elf_External_Verdef); | |
6538 | ||
6539 | defaux.vda_name = h->dynstr_index; | |
6540 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6541 | h->dynstr_index); | |
6542 | defaux.vda_next = 0; | |
6543 | if (t->deps != NULL) | |
6544 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6545 | t->name_indx = defaux.vda_name; | |
6546 | ||
6547 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6548 | (Elf_External_Verdaux *) p); | |
6549 | p += sizeof (Elf_External_Verdaux); | |
6550 | ||
6551 | for (n = t->deps; n != NULL; n = n->next) | |
6552 | { | |
6553 | if (n->version_needed == NULL) | |
6554 | { | |
6555 | /* This can happen if there was an error in the | |
6556 | version script. */ | |
6557 | defaux.vda_name = 0; | |
6558 | } | |
6559 | else | |
6560 | { | |
6561 | defaux.vda_name = n->version_needed->name_indx; | |
6562 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6563 | defaux.vda_name); | |
6564 | } | |
6565 | if (n->next == NULL) | |
6566 | defaux.vda_next = 0; | |
6567 | else | |
6568 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6569 | ||
6570 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6571 | (Elf_External_Verdaux *) p); | |
6572 | p += sizeof (Elf_External_Verdaux); | |
6573 | } | |
6574 | } | |
6575 | ||
5a580b3a AM |
6576 | elf_tdata (output_bfd)->cverdefs = cdefs; |
6577 | } | |
902e9fc7 MR |
6578 | } |
6579 | ||
6580 | bed = get_elf_backend_data (output_bfd); | |
6581 | ||
6582 | if (info->gc_sections && bed->can_gc_sections) | |
6583 | { | |
6584 | struct elf_gc_sweep_symbol_info sweep_info; | |
902e9fc7 MR |
6585 | |
6586 | /* Remove the symbols that were in the swept sections from the | |
3d13f3e9 | 6587 | dynamic symbol table. */ |
902e9fc7 MR |
6588 | sweep_info.info = info; |
6589 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
6590 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
6591 | &sweep_info); | |
3d13f3e9 AM |
6592 | } |
6593 | ||
6594 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6595 | { | |
6596 | asection *s; | |
6597 | struct elf_find_verdep_info sinfo; | |
6598 | ||
6599 | /* Work out the size of the version reference section. */ | |
6600 | ||
6601 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); | |
6602 | BFD_ASSERT (s != NULL); | |
902e9fc7 | 6603 | |
3d13f3e9 AM |
6604 | sinfo.info = info; |
6605 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6606 | if (sinfo.vers == 0) | |
6607 | sinfo.vers = 1; | |
6608 | sinfo.failed = FALSE; | |
6609 | ||
6610 | elf_link_hash_traverse (elf_hash_table (info), | |
6611 | _bfd_elf_link_find_version_dependencies, | |
6612 | &sinfo); | |
6613 | if (sinfo.failed) | |
6614 | return FALSE; | |
6615 | ||
6616 | if (elf_tdata (output_bfd)->verref == NULL) | |
6617 | s->flags |= SEC_EXCLUDE; | |
6618 | else | |
6619 | { | |
6620 | Elf_Internal_Verneed *vn; | |
6621 | unsigned int size; | |
6622 | unsigned int crefs; | |
6623 | bfd_byte *p; | |
6624 | ||
6625 | /* Build the version dependency section. */ | |
6626 | size = 0; | |
6627 | crefs = 0; | |
6628 | for (vn = elf_tdata (output_bfd)->verref; | |
6629 | vn != NULL; | |
6630 | vn = vn->vn_nextref) | |
6631 | { | |
6632 | Elf_Internal_Vernaux *a; | |
6633 | ||
6634 | size += sizeof (Elf_External_Verneed); | |
6635 | ++crefs; | |
6636 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6637 | size += sizeof (Elf_External_Vernaux); | |
6638 | } | |
6639 | ||
6640 | s->size = size; | |
6641 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); | |
6642 | if (s->contents == NULL) | |
6643 | return FALSE; | |
6644 | ||
6645 | p = s->contents; | |
6646 | for (vn = elf_tdata (output_bfd)->verref; | |
6647 | vn != NULL; | |
6648 | vn = vn->vn_nextref) | |
6649 | { | |
6650 | unsigned int caux; | |
6651 | Elf_Internal_Vernaux *a; | |
6652 | size_t indx; | |
6653 | ||
6654 | caux = 0; | |
6655 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6656 | ++caux; | |
6657 | ||
6658 | vn->vn_version = VER_NEED_CURRENT; | |
6659 | vn->vn_cnt = caux; | |
6660 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6661 | elf_dt_name (vn->vn_bfd) != NULL | |
6662 | ? elf_dt_name (vn->vn_bfd) | |
6663 | : lbasename (vn->vn_bfd->filename), | |
6664 | FALSE); | |
6665 | if (indx == (size_t) -1) | |
6666 | return FALSE; | |
6667 | vn->vn_file = indx; | |
6668 | vn->vn_aux = sizeof (Elf_External_Verneed); | |
6669 | if (vn->vn_nextref == NULL) | |
6670 | vn->vn_next = 0; | |
6671 | else | |
6672 | vn->vn_next = (sizeof (Elf_External_Verneed) | |
6673 | + caux * sizeof (Elf_External_Vernaux)); | |
6674 | ||
6675 | _bfd_elf_swap_verneed_out (output_bfd, vn, | |
6676 | (Elf_External_Verneed *) p); | |
6677 | p += sizeof (Elf_External_Verneed); | |
6678 | ||
6679 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6680 | { | |
6681 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6682 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6683 | a->vna_nodename, FALSE); | |
6684 | if (indx == (size_t) -1) | |
6685 | return FALSE; | |
6686 | a->vna_name = indx; | |
6687 | if (a->vna_nextptr == NULL) | |
6688 | a->vna_next = 0; | |
6689 | else | |
6690 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6691 | ||
6692 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6693 | (Elf_External_Vernaux *) p); | |
6694 | p += sizeof (Elf_External_Vernaux); | |
6695 | } | |
6696 | } | |
6697 | ||
6698 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6699 | } | |
902e9fc7 MR |
6700 | } |
6701 | ||
6702 | /* Any syms created from now on start with -1 in | |
6703 | got.refcount/offset and plt.refcount/offset. */ | |
6704 | elf_hash_table (info)->init_got_refcount | |
6705 | = elf_hash_table (info)->init_got_offset; | |
6706 | elf_hash_table (info)->init_plt_refcount | |
6707 | = elf_hash_table (info)->init_plt_offset; | |
6708 | ||
6709 | if (bfd_link_relocatable (info) | |
6710 | && !_bfd_elf_size_group_sections (info)) | |
6711 | return FALSE; | |
6712 | ||
6713 | /* The backend may have to create some sections regardless of whether | |
6714 | we're dynamic or not. */ | |
6715 | if (bed->elf_backend_always_size_sections | |
6716 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
6717 | return FALSE; | |
6718 | ||
6719 | /* Determine any GNU_STACK segment requirements, after the backend | |
6720 | has had a chance to set a default segment size. */ | |
6721 | if (info->execstack) | |
6722 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; | |
6723 | else if (info->noexecstack) | |
6724 | elf_stack_flags (output_bfd) = PF_R | PF_W; | |
6725 | else | |
6726 | { | |
6727 | bfd *inputobj; | |
6728 | asection *notesec = NULL; | |
6729 | int exec = 0; | |
6730 | ||
6731 | for (inputobj = info->input_bfds; | |
6732 | inputobj; | |
6733 | inputobj = inputobj->link.next) | |
6734 | { | |
6735 | asection *s; | |
6736 | ||
6737 | if (inputobj->flags | |
6738 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
6739 | continue; | |
57963c05 AM |
6740 | s = inputobj->sections; |
6741 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
6742 | continue; | |
6743 | ||
902e9fc7 MR |
6744 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); |
6745 | if (s) | |
6746 | { | |
6747 | if (s->flags & SEC_CODE) | |
6748 | exec = PF_X; | |
6749 | notesec = s; | |
6750 | } | |
6751 | else if (bed->default_execstack) | |
6752 | exec = PF_X; | |
6753 | } | |
6754 | if (notesec || info->stacksize > 0) | |
6755 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; | |
6756 | if (notesec && exec && bfd_link_relocatable (info) | |
6757 | && notesec->output_section != bfd_abs_section_ptr) | |
6758 | notesec->output_section->flags |= SEC_CODE; | |
6759 | } | |
6760 | ||
6761 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6762 | { | |
6763 | struct elf_info_failed eif; | |
6764 | struct elf_link_hash_entry *h; | |
6765 | asection *dynstr; | |
6766 | asection *s; | |
6767 | ||
6768 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); | |
6769 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); | |
6770 | ||
902e9fc7 MR |
6771 | if (info->symbolic) |
6772 | { | |
6773 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
6774 | return FALSE; | |
6775 | info->flags |= DF_SYMBOLIC; | |
6776 | } | |
6777 | ||
6778 | if (rpath != NULL) | |
6779 | { | |
6780 | size_t indx; | |
6781 | bfd_vma tag; | |
6782 | ||
6783 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
6784 | TRUE); | |
6785 | if (indx == (size_t) -1) | |
6786 | return FALSE; | |
6787 | ||
6788 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; | |
6789 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
6790 | return FALSE; | |
6791 | } | |
6792 | ||
6793 | if (filter_shlib != NULL) | |
6794 | { | |
6795 | size_t indx; | |
6796 | ||
6797 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6798 | filter_shlib, TRUE); | |
6799 | if (indx == (size_t) -1 | |
6800 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
6801 | return FALSE; | |
6802 | } | |
6803 | ||
6804 | if (auxiliary_filters != NULL) | |
6805 | { | |
6806 | const char * const *p; | |
6807 | ||
6808 | for (p = auxiliary_filters; *p != NULL; p++) | |
6809 | { | |
6810 | size_t indx; | |
6811 | ||
6812 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6813 | *p, TRUE); | |
6814 | if (indx == (size_t) -1 | |
6815 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
6816 | return FALSE; | |
6817 | } | |
6818 | } | |
6819 | ||
6820 | if (audit != NULL) | |
6821 | { | |
6822 | size_t indx; | |
6823 | ||
6824 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
6825 | TRUE); | |
6826 | if (indx == (size_t) -1 | |
6827 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
6828 | return FALSE; | |
6829 | } | |
6830 | ||
6831 | if (depaudit != NULL) | |
6832 | { | |
6833 | size_t indx; | |
6834 | ||
6835 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
6836 | TRUE); | |
6837 | if (indx == (size_t) -1 | |
6838 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
6839 | return FALSE; | |
6840 | } | |
6841 | ||
6842 | eif.info = info; | |
6843 | eif.failed = FALSE; | |
6844 | ||
6845 | /* Find all symbols which were defined in a dynamic object and make | |
6846 | the backend pick a reasonable value for them. */ | |
6847 | elf_link_hash_traverse (elf_hash_table (info), | |
6848 | _bfd_elf_adjust_dynamic_symbol, | |
6849 | &eif); | |
6850 | if (eif.failed) | |
6851 | return FALSE; | |
6852 | ||
6853 | /* Add some entries to the .dynamic section. We fill in some of the | |
6854 | values later, in bfd_elf_final_link, but we must add the entries | |
6855 | now so that we know the final size of the .dynamic section. */ | |
6856 | ||
6857 | /* If there are initialization and/or finalization functions to | |
6858 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6859 | h = (info->init_function | |
6860 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6861 | info->init_function, FALSE, | |
6862 | FALSE, FALSE) | |
6863 | : NULL); | |
6864 | if (h != NULL | |
6865 | && (h->ref_regular | |
6866 | || h->def_regular)) | |
6867 | { | |
6868 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6869 | return FALSE; | |
6870 | } | |
6871 | h = (info->fini_function | |
6872 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6873 | info->fini_function, FALSE, | |
6874 | FALSE, FALSE) | |
6875 | : NULL); | |
6876 | if (h != NULL | |
6877 | && (h->ref_regular | |
6878 | || h->def_regular)) | |
6879 | { | |
6880 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6881 | return FALSE; | |
6882 | } | |
6883 | ||
6884 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); | |
6885 | if (s != NULL && s->linker_has_input) | |
6886 | { | |
6887 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
6888 | if (! bfd_link_executable (info)) | |
6889 | { | |
6890 | bfd *sub; | |
6891 | asection *o; | |
6892 | ||
57963c05 AM |
6893 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
6894 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
6895 | && (o = sub->sections) != NULL | |
6896 | && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) | |
902e9fc7 MR |
6897 | for (o = sub->sections; o != NULL; o = o->next) |
6898 | if (elf_section_data (o)->this_hdr.sh_type | |
6899 | == SHT_PREINIT_ARRAY) | |
6900 | { | |
6901 | _bfd_error_handler | |
871b3ab2 | 6902 | (_("%pB: .preinit_array section is not allowed in DSO"), |
902e9fc7 MR |
6903 | sub); |
6904 | break; | |
6905 | } | |
6906 | ||
6907 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6908 | return FALSE; | |
6909 | } | |
6910 | ||
6911 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6912 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6913 | return FALSE; | |
6914 | } | |
6915 | s = bfd_get_section_by_name (output_bfd, ".init_array"); | |
6916 | if (s != NULL && s->linker_has_input) | |
6917 | { | |
6918 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6919 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6920 | return FALSE; | |
6921 | } | |
6922 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); | |
6923 | if (s != NULL && s->linker_has_input) | |
6924 | { | |
6925 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6926 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6927 | return FALSE; | |
6928 | } | |
6929 | ||
6930 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); | |
6931 | /* If .dynstr is excluded from the link, we don't want any of | |
6932 | these tags. Strictly, we should be checking each section | |
6933 | individually; This quick check covers for the case where | |
6934 | someone does a /DISCARD/ : { *(*) }. */ | |
6935 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6936 | { | |
6937 | bfd_size_type strsize; | |
6938 | ||
6939 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
6940 | if ((info->emit_hash | |
6941 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6942 | || (info->emit_gnu_hash | |
6943 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
6944 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
6945 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6946 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6947 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6948 | bed->s->sizeof_sym)) | |
6949 | return FALSE; | |
6950 | } | |
6951 | } | |
6952 | ||
6953 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
6954 | return FALSE; | |
6955 | ||
6956 | /* The backend must work out the sizes of all the other dynamic | |
6957 | sections. */ | |
6958 | if (dynobj != NULL | |
6959 | && bed->elf_backend_size_dynamic_sections != NULL | |
6960 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
6961 | return FALSE; | |
6962 | ||
6963 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6964 | { | |
902e9fc7 MR |
6965 | if (elf_tdata (output_bfd)->cverdefs) |
6966 | { | |
6967 | unsigned int crefs = elf_tdata (output_bfd)->cverdefs; | |
6968 | ||
6969 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
6970 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) | |
6971 | return FALSE; | |
6972 | } | |
6973 | ||
6974 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
6975 | { | |
6976 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
6977 | return FALSE; | |
6978 | } | |
6979 | else if (info->flags & DF_BIND_NOW) | |
6980 | { | |
6981 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
6982 | return FALSE; | |
6983 | } | |
6984 | ||
6985 | if (info->flags_1) | |
6986 | { | |
6987 | if (bfd_link_executable (info)) | |
6988 | info->flags_1 &= ~ (DF_1_INITFIRST | |
6989 | | DF_1_NODELETE | |
6990 | | DF_1_NOOPEN); | |
6991 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
6992 | return FALSE; | |
6993 | } | |
6994 | ||
6995 | if (elf_tdata (output_bfd)->cverrefs) | |
6996 | { | |
6997 | unsigned int crefs = elf_tdata (output_bfd)->cverrefs; | |
6998 | ||
6999 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
7000 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
7001 | return FALSE; | |
7002 | } | |
5a580b3a | 7003 | |
8423293d AM |
7004 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
7005 | && elf_tdata (output_bfd)->cverdefs == 0) | |
63f452a8 | 7006 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) |
8423293d | 7007 | { |
902e9fc7 MR |
7008 | asection *s; |
7009 | ||
3d4d4302 | 7010 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
7011 | s->flags |= SEC_EXCLUDE; |
7012 | } | |
7013 | } | |
7014 | return TRUE; | |
7015 | } | |
7016 | ||
74541ad4 AM |
7017 | /* Find the first non-excluded output section. We'll use its |
7018 | section symbol for some emitted relocs. */ | |
7019 | void | |
7020 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
7021 | { | |
7022 | asection *s; | |
7023 | ||
7024 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
7025 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
d00dd7dc | 7026 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 AM |
7027 | { |
7028 | elf_hash_table (info)->text_index_section = s; | |
7029 | break; | |
7030 | } | |
7031 | } | |
7032 | ||
7033 | /* Find two non-excluded output sections, one for code, one for data. | |
7034 | We'll use their section symbols for some emitted relocs. */ | |
7035 | void | |
7036 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
7037 | { | |
7038 | asection *s; | |
7039 | ||
266b05cf | 7040 | /* Data first, since setting text_index_section changes |
7f923b7f | 7041 | _bfd_elf_omit_section_dynsym_default. */ |
74541ad4 | 7042 | for (s = output_bfd->sections; s != NULL; s = s->next) |
266b05cf | 7043 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) |
d00dd7dc | 7044 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7045 | { |
266b05cf | 7046 | elf_hash_table (info)->data_index_section = s; |
74541ad4 AM |
7047 | break; |
7048 | } | |
7049 | ||
7050 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
266b05cf DJ |
7051 | if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) |
7052 | == (SEC_ALLOC | SEC_READONLY)) | |
d00dd7dc | 7053 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7054 | { |
266b05cf | 7055 | elf_hash_table (info)->text_index_section = s; |
74541ad4 AM |
7056 | break; |
7057 | } | |
7058 | ||
7059 | if (elf_hash_table (info)->text_index_section == NULL) | |
7060 | elf_hash_table (info)->text_index_section | |
7061 | = elf_hash_table (info)->data_index_section; | |
7062 | } | |
7063 | ||
8423293d AM |
7064 | bfd_boolean |
7065 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
7066 | { | |
74541ad4 | 7067 | const struct elf_backend_data *bed; |
23ec1e32 | 7068 | unsigned long section_sym_count; |
96d01d93 | 7069 | bfd_size_type dynsymcount = 0; |
74541ad4 | 7070 | |
8423293d AM |
7071 | if (!is_elf_hash_table (info->hash)) |
7072 | return TRUE; | |
7073 | ||
74541ad4 AM |
7074 | bed = get_elf_backend_data (output_bfd); |
7075 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
7076 | ||
23ec1e32 MR |
7077 | /* Assign dynsym indices. In a shared library we generate a section |
7078 | symbol for each output section, which come first. Next come all | |
7079 | of the back-end allocated local dynamic syms, followed by the rest | |
7080 | of the global symbols. | |
7081 | ||
7082 | This is usually not needed for static binaries, however backends | |
7083 | can request to always do it, e.g. the MIPS backend uses dynamic | |
7084 | symbol counts to lay out GOT, which will be produced in the | |
7085 | presence of GOT relocations even in static binaries (holding fixed | |
7086 | data in that case, to satisfy those relocations). */ | |
7087 | ||
7088 | if (elf_hash_table (info)->dynamic_sections_created | |
7089 | || bed->always_renumber_dynsyms) | |
7090 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
7091 | §ion_sym_count); | |
7092 | ||
8423293d AM |
7093 | if (elf_hash_table (info)->dynamic_sections_created) |
7094 | { | |
7095 | bfd *dynobj; | |
8423293d | 7096 | asection *s; |
8423293d AM |
7097 | unsigned int dtagcount; |
7098 | ||
7099 | dynobj = elf_hash_table (info)->dynobj; | |
7100 | ||
5a580b3a | 7101 | /* Work out the size of the symbol version section. */ |
3d4d4302 | 7102 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 7103 | BFD_ASSERT (s != NULL); |
d5486c43 | 7104 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 7105 | { |
eea6121a | 7106 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 7107 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
7108 | if (s->contents == NULL) |
7109 | return FALSE; | |
7110 | ||
7111 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
7112 | return FALSE; | |
7113 | } | |
7114 | ||
7115 | /* Set the size of the .dynsym and .hash sections. We counted | |
7116 | the number of dynamic symbols in elf_link_add_object_symbols. | |
7117 | We will build the contents of .dynsym and .hash when we build | |
7118 | the final symbol table, because until then we do not know the | |
7119 | correct value to give the symbols. We built the .dynstr | |
7120 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 7121 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 7122 | BFD_ASSERT (s != NULL); |
eea6121a | 7123 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 7124 | |
d5486c43 L |
7125 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
7126 | if (s->contents == NULL) | |
7127 | return FALSE; | |
5a580b3a | 7128 | |
d5486c43 L |
7129 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
7130 | section syms, in case we don't output them all. */ | |
7131 | ++section_sym_count; | |
7132 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 7133 | |
fdc90cb4 JJ |
7134 | elf_hash_table (info)->bucketcount = 0; |
7135 | ||
5a580b3a AM |
7136 | /* Compute the size of the hashing table. As a side effect this |
7137 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
7138 | if (info->emit_hash) |
7139 | { | |
7140 | unsigned long int *hashcodes; | |
14b1c01e | 7141 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
7142 | bfd_size_type amt; |
7143 | unsigned long int nsyms; | |
7144 | size_t bucketcount; | |
7145 | size_t hash_entry_size; | |
7146 | ||
7147 | /* Compute the hash values for all exported symbols. At the same | |
7148 | time store the values in an array so that we could use them for | |
7149 | optimizations. */ | |
7150 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 7151 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7152 | if (hashcodes == NULL) |
7153 | return FALSE; | |
14b1c01e AM |
7154 | hashinf.hashcodes = hashcodes; |
7155 | hashinf.error = FALSE; | |
5a580b3a | 7156 | |
fdc90cb4 JJ |
7157 | /* Put all hash values in HASHCODES. */ |
7158 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
7159 | elf_collect_hash_codes, &hashinf); |
7160 | if (hashinf.error) | |
4dd07732 AM |
7161 | { |
7162 | free (hashcodes); | |
7163 | return FALSE; | |
7164 | } | |
5a580b3a | 7165 | |
14b1c01e | 7166 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
7167 | bucketcount |
7168 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
7169 | free (hashcodes); | |
7170 | ||
4b48e2f6 | 7171 | if (bucketcount == 0 && nsyms > 0) |
fdc90cb4 | 7172 | return FALSE; |
5a580b3a | 7173 | |
fdc90cb4 JJ |
7174 | elf_hash_table (info)->bucketcount = bucketcount; |
7175 | ||
3d4d4302 | 7176 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
7177 | BFD_ASSERT (s != NULL); |
7178 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
7179 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 7180 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7181 | if (s->contents == NULL) |
7182 | return FALSE; | |
7183 | ||
7184 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
7185 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
7186 | s->contents + hash_entry_size); | |
7187 | } | |
7188 | ||
7189 | if (info->emit_gnu_hash) | |
7190 | { | |
7191 | size_t i, cnt; | |
7192 | unsigned char *contents; | |
7193 | struct collect_gnu_hash_codes cinfo; | |
7194 | bfd_size_type amt; | |
7195 | size_t bucketcount; | |
7196 | ||
7197 | memset (&cinfo, 0, sizeof (cinfo)); | |
7198 | ||
7199 | /* Compute the hash values for all exported symbols. At the same | |
7200 | time store the values in an array so that we could use them for | |
7201 | optimizations. */ | |
7202 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 7203 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7204 | if (cinfo.hashcodes == NULL) |
7205 | return FALSE; | |
7206 | ||
7207 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
7208 | cinfo.min_dynindx = -1; | |
7209 | cinfo.output_bfd = output_bfd; | |
7210 | cinfo.bed = bed; | |
7211 | ||
7212 | /* Put all hash values in HASHCODES. */ | |
7213 | elf_link_hash_traverse (elf_hash_table (info), | |
7214 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 7215 | if (cinfo.error) |
4dd07732 AM |
7216 | { |
7217 | free (cinfo.hashcodes); | |
7218 | return FALSE; | |
7219 | } | |
fdc90cb4 JJ |
7220 | |
7221 | bucketcount | |
7222 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
7223 | ||
7224 | if (bucketcount == 0) | |
7225 | { | |
7226 | free (cinfo.hashcodes); | |
7227 | return FALSE; | |
7228 | } | |
7229 | ||
3d4d4302 | 7230 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
7231 | BFD_ASSERT (s != NULL); |
7232 | ||
7233 | if (cinfo.nsyms == 0) | |
7234 | { | |
7235 | /* Empty .gnu.hash section is special. */ | |
7236 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
7237 | free (cinfo.hashcodes); | |
7238 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 7239 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7240 | if (contents == NULL) |
7241 | return FALSE; | |
7242 | s->contents = contents; | |
7243 | /* 1 empty bucket. */ | |
7244 | bfd_put_32 (output_bfd, 1, contents); | |
7245 | /* SYMIDX above the special symbol 0. */ | |
7246 | bfd_put_32 (output_bfd, 1, contents + 4); | |
7247 | /* Just one word for bitmask. */ | |
7248 | bfd_put_32 (output_bfd, 1, contents + 8); | |
7249 | /* Only hash fn bloom filter. */ | |
7250 | bfd_put_32 (output_bfd, 0, contents + 12); | |
7251 | /* No hashes are valid - empty bitmask. */ | |
7252 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
7253 | /* No hashes in the only bucket. */ | |
7254 | bfd_put_32 (output_bfd, 0, | |
7255 | contents + 16 + bed->s->arch_size / 8); | |
7256 | } | |
7257 | else | |
7258 | { | |
9e6619e2 | 7259 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 7260 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 7261 | |
9e6619e2 AM |
7262 | x = cinfo.nsyms; |
7263 | maskbitslog2 = 1; | |
7264 | while ((x >>= 1) != 0) | |
7265 | ++maskbitslog2; | |
fdc90cb4 JJ |
7266 | if (maskbitslog2 < 3) |
7267 | maskbitslog2 = 5; | |
7268 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
7269 | maskbitslog2 = maskbitslog2 + 3; | |
7270 | else | |
7271 | maskbitslog2 = maskbitslog2 + 2; | |
7272 | if (bed->s->arch_size == 64) | |
7273 | { | |
7274 | if (maskbitslog2 == 5) | |
7275 | maskbitslog2 = 6; | |
7276 | cinfo.shift1 = 6; | |
7277 | } | |
7278 | else | |
7279 | cinfo.shift1 = 5; | |
7280 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 7281 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
7282 | cinfo.maskbits = 1 << maskbitslog2; |
7283 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
7284 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
7285 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 7286 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
7287 | if (cinfo.bitmask == NULL) |
7288 | { | |
7289 | free (cinfo.hashcodes); | |
7290 | return FALSE; | |
7291 | } | |
7292 | ||
a50b1753 | 7293 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
7294 | cinfo.indx = cinfo.counts + bucketcount; |
7295 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
7296 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
7297 | ||
7298 | /* Determine how often each hash bucket is used. */ | |
7299 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
7300 | for (i = 0; i < cinfo.nsyms; ++i) | |
7301 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
7302 | ||
7303 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
7304 | if (cinfo.counts[i] != 0) | |
7305 | { | |
7306 | cinfo.indx[i] = cnt; | |
7307 | cnt += cinfo.counts[i]; | |
7308 | } | |
7309 | BFD_ASSERT (cnt == dynsymcount); | |
7310 | cinfo.bucketcount = bucketcount; | |
7311 | cinfo.local_indx = cinfo.min_dynindx; | |
7312 | ||
7313 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
7314 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 7315 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7316 | if (contents == NULL) |
7317 | { | |
7318 | free (cinfo.bitmask); | |
7319 | free (cinfo.hashcodes); | |
7320 | return FALSE; | |
7321 | } | |
7322 | ||
7323 | s->contents = contents; | |
7324 | bfd_put_32 (output_bfd, bucketcount, contents); | |
7325 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
7326 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
7327 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
7328 | contents += 16 + cinfo.maskbits / 8; | |
7329 | ||
7330 | for (i = 0; i < bucketcount; ++i) | |
7331 | { | |
7332 | if (cinfo.counts[i] == 0) | |
7333 | bfd_put_32 (output_bfd, 0, contents); | |
7334 | else | |
7335 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
7336 | contents += 4; | |
7337 | } | |
7338 | ||
7339 | cinfo.contents = contents; | |
7340 | ||
7341 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
7342 | elf_link_hash_traverse (elf_hash_table (info), | |
7343 | elf_renumber_gnu_hash_syms, &cinfo); | |
7344 | ||
7345 | contents = s->contents + 16; | |
7346 | for (i = 0; i < maskwords; ++i) | |
7347 | { | |
7348 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
7349 | contents); | |
7350 | contents += bed->s->arch_size / 8; | |
7351 | } | |
7352 | ||
7353 | free (cinfo.bitmask); | |
7354 | free (cinfo.hashcodes); | |
7355 | } | |
7356 | } | |
5a580b3a | 7357 | |
3d4d4302 | 7358 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
7359 | BFD_ASSERT (s != NULL); |
7360 | ||
4ad4eba5 | 7361 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 7362 | |
eea6121a | 7363 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
7364 | |
7365 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
7366 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
7367 | return FALSE; | |
7368 | } | |
7369 | ||
7370 | return TRUE; | |
7371 | } | |
4d269e42 | 7372 | \f |
4d269e42 AM |
7373 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
7374 | ||
7375 | static void | |
7376 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
7377 | asection *sec) | |
7378 | { | |
dbaa2011 AM |
7379 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
7380 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
7381 | } |
7382 | ||
7383 | /* Finish SHF_MERGE section merging. */ | |
7384 | ||
7385 | bfd_boolean | |
630993ec | 7386 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
7387 | { |
7388 | bfd *ibfd; | |
7389 | asection *sec; | |
7390 | ||
7391 | if (!is_elf_hash_table (info->hash)) | |
7392 | return FALSE; | |
7393 | ||
c72f2fb2 | 7394 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
7395 | if ((ibfd->flags & DYNAMIC) == 0 |
7396 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
7397 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
7398 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
7399 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
7400 | if ((sec->flags & SEC_MERGE) != 0 | |
7401 | && !bfd_is_abs_section (sec->output_section)) | |
7402 | { | |
7403 | struct bfd_elf_section_data *secdata; | |
7404 | ||
7405 | secdata = elf_section_data (sec); | |
630993ec | 7406 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
7407 | &elf_hash_table (info)->merge_info, |
7408 | sec, &secdata->sec_info)) | |
7409 | return FALSE; | |
7410 | else if (secdata->sec_info) | |
dbaa2011 | 7411 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7412 | } |
7413 | ||
7414 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7415 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
7416 | merge_sections_remove_hook); |
7417 | return TRUE; | |
7418 | } | |
7419 | ||
7420 | /* Create an entry in an ELF linker hash table. */ | |
7421 | ||
7422 | struct bfd_hash_entry * | |
7423 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7424 | struct bfd_hash_table *table, | |
7425 | const char *string) | |
7426 | { | |
7427 | /* Allocate the structure if it has not already been allocated by a | |
7428 | subclass. */ | |
7429 | if (entry == NULL) | |
7430 | { | |
a50b1753 | 7431 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7432 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7433 | if (entry == NULL) |
7434 | return entry; | |
7435 | } | |
7436 | ||
7437 | /* Call the allocation method of the superclass. */ | |
7438 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7439 | if (entry != NULL) | |
7440 | { | |
7441 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7442 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7443 | ||
7444 | /* Set local fields. */ | |
7445 | ret->indx = -1; | |
7446 | ret->dynindx = -1; | |
7447 | ret->got = htab->init_got_refcount; | |
7448 | ret->plt = htab->init_plt_refcount; | |
7449 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7450 | - offsetof (struct elf_link_hash_entry, size))); | |
7451 | /* Assume that we have been called by a non-ELF symbol reader. | |
7452 | This flag is then reset by the code which reads an ELF input | |
7453 | file. This ensures that a symbol created by a non-ELF symbol | |
7454 | reader will have the flag set correctly. */ | |
7455 | ret->non_elf = 1; | |
7456 | } | |
7457 | ||
7458 | return entry; | |
7459 | } | |
7460 | ||
7461 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7462 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7463 | ||
7464 | void | |
7465 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7466 | struct elf_link_hash_entry *dir, | |
7467 | struct elf_link_hash_entry *ind) | |
7468 | { | |
7469 | struct elf_link_hash_table *htab; | |
7470 | ||
7471 | /* Copy down any references that we may have already seen to the | |
e81830c5 | 7472 | symbol which just became indirect. */ |
4d269e42 | 7473 | |
422f1182 | 7474 | if (dir->versioned != versioned_hidden) |
e81830c5 AM |
7475 | dir->ref_dynamic |= ind->ref_dynamic; |
7476 | dir->ref_regular |= ind->ref_regular; | |
7477 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7478 | dir->non_got_ref |= ind->non_got_ref; | |
7479 | dir->needs_plt |= ind->needs_plt; | |
7480 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
4d269e42 AM |
7481 | |
7482 | if (ind->root.type != bfd_link_hash_indirect) | |
7483 | return; | |
7484 | ||
7485 | /* Copy over the global and procedure linkage table refcount entries. | |
7486 | These may have been already set up by a check_relocs routine. */ | |
7487 | htab = elf_hash_table (info); | |
7488 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7489 | { | |
7490 | if (dir->got.refcount < 0) | |
7491 | dir->got.refcount = 0; | |
7492 | dir->got.refcount += ind->got.refcount; | |
7493 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7494 | } | |
7495 | ||
7496 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7497 | { | |
7498 | if (dir->plt.refcount < 0) | |
7499 | dir->plt.refcount = 0; | |
7500 | dir->plt.refcount += ind->plt.refcount; | |
7501 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7502 | } | |
7503 | ||
7504 | if (ind->dynindx != -1) | |
7505 | { | |
7506 | if (dir->dynindx != -1) | |
7507 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7508 | dir->dynindx = ind->dynindx; | |
7509 | dir->dynstr_index = ind->dynstr_index; | |
7510 | ind->dynindx = -1; | |
7511 | ind->dynstr_index = 0; | |
7512 | } | |
7513 | } | |
7514 | ||
7515 | void | |
7516 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7517 | struct elf_link_hash_entry *h, | |
7518 | bfd_boolean force_local) | |
7519 | { | |
3aa14d16 L |
7520 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7521 | if (h->type != STT_GNU_IFUNC) | |
7522 | { | |
7523 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7524 | h->needs_plt = 0; | |
7525 | } | |
4d269e42 AM |
7526 | if (force_local) |
7527 | { | |
7528 | h->forced_local = 1; | |
7529 | if (h->dynindx != -1) | |
7530 | { | |
4d269e42 AM |
7531 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, |
7532 | h->dynstr_index); | |
641338d8 AM |
7533 | h->dynindx = -1; |
7534 | h->dynstr_index = 0; | |
4d269e42 AM |
7535 | } |
7536 | } | |
7537 | } | |
7538 | ||
34a87bb0 L |
7539 | /* Hide a symbol. */ |
7540 | ||
7541 | void | |
7542 | _bfd_elf_link_hide_symbol (bfd *output_bfd, | |
7543 | struct bfd_link_info *info, | |
7544 | struct bfd_link_hash_entry *h) | |
7545 | { | |
7546 | if (is_elf_hash_table (info->hash)) | |
7547 | { | |
7548 | const struct elf_backend_data *bed | |
7549 | = get_elf_backend_data (output_bfd); | |
7550 | struct elf_link_hash_entry *eh | |
7551 | = (struct elf_link_hash_entry *) h; | |
7552 | bed->elf_backend_hide_symbol (info, eh, TRUE); | |
7553 | eh->def_dynamic = 0; | |
7554 | eh->ref_dynamic = 0; | |
7555 | eh->dynamic_def = 0; | |
7556 | } | |
7557 | } | |
7558 | ||
7bf52ea2 AM |
7559 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7560 | caller. */ | |
4d269e42 AM |
7561 | |
7562 | bfd_boolean | |
7563 | _bfd_elf_link_hash_table_init | |
7564 | (struct elf_link_hash_table *table, | |
7565 | bfd *abfd, | |
7566 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7567 | struct bfd_hash_table *, | |
7568 | const char *), | |
4dfe6ac6 NC |
7569 | unsigned int entsize, |
7570 | enum elf_target_id target_id) | |
4d269e42 AM |
7571 | { |
7572 | bfd_boolean ret; | |
7573 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7574 | ||
4d269e42 AM |
7575 | table->init_got_refcount.refcount = can_refcount - 1; |
7576 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7577 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7578 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7579 | /* The first dynamic symbol is a dummy. */ | |
7580 | table->dynsymcount = 1; | |
7581 | ||
7582 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7583 | |
4d269e42 | 7584 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7585 | table->hash_table_id = target_id; |
4d269e42 AM |
7586 | |
7587 | return ret; | |
7588 | } | |
7589 | ||
7590 | /* Create an ELF linker hash table. */ | |
7591 | ||
7592 | struct bfd_link_hash_table * | |
7593 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7594 | { | |
7595 | struct elf_link_hash_table *ret; | |
7596 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7597 | ||
7bf52ea2 | 7598 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7599 | if (ret == NULL) |
7600 | return NULL; | |
7601 | ||
7602 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7603 | sizeof (struct elf_link_hash_entry), |
7604 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7605 | { |
7606 | free (ret); | |
7607 | return NULL; | |
7608 | } | |
d495ab0d | 7609 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7610 | |
7611 | return &ret->root; | |
7612 | } | |
7613 | ||
9f7c3e5e AM |
7614 | /* Destroy an ELF linker hash table. */ |
7615 | ||
7616 | void | |
d495ab0d | 7617 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7618 | { |
d495ab0d AM |
7619 | struct elf_link_hash_table *htab; |
7620 | ||
7621 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7622 | if (htab->dynstr != NULL) |
7623 | _bfd_elf_strtab_free (htab->dynstr); | |
7624 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7625 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7626 | } |
7627 | ||
4d269e42 AM |
7628 | /* This is a hook for the ELF emulation code in the generic linker to |
7629 | tell the backend linker what file name to use for the DT_NEEDED | |
7630 | entry for a dynamic object. */ | |
7631 | ||
7632 | void | |
7633 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7634 | { | |
7635 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7636 | && bfd_get_format (abfd) == bfd_object) | |
7637 | elf_dt_name (abfd) = name; | |
7638 | } | |
7639 | ||
7640 | int | |
7641 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7642 | { | |
7643 | int lib_class; | |
7644 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7645 | && bfd_get_format (abfd) == bfd_object) | |
7646 | lib_class = elf_dyn_lib_class (abfd); | |
7647 | else | |
7648 | lib_class = 0; | |
7649 | return lib_class; | |
7650 | } | |
7651 | ||
7652 | void | |
7653 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7654 | { | |
7655 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7656 | && bfd_get_format (abfd) == bfd_object) | |
7657 | elf_dyn_lib_class (abfd) = lib_class; | |
7658 | } | |
7659 | ||
7660 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7661 | the linker ELF emulation code. */ | |
7662 | ||
7663 | struct bfd_link_needed_list * | |
7664 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7665 | struct bfd_link_info *info) | |
7666 | { | |
7667 | if (! is_elf_hash_table (info->hash)) | |
7668 | return NULL; | |
7669 | return elf_hash_table (info)->needed; | |
7670 | } | |
7671 | ||
7672 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7673 | hook for the linker ELF emulation code. */ | |
7674 | ||
7675 | struct bfd_link_needed_list * | |
7676 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7677 | struct bfd_link_info *info) | |
7678 | { | |
7679 | if (! is_elf_hash_table (info->hash)) | |
7680 | return NULL; | |
7681 | return elf_hash_table (info)->runpath; | |
7682 | } | |
7683 | ||
7684 | /* Get the name actually used for a dynamic object for a link. This | |
7685 | is the SONAME entry if there is one. Otherwise, it is the string | |
7686 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7687 | ||
7688 | const char * | |
7689 | bfd_elf_get_dt_soname (bfd *abfd) | |
7690 | { | |
7691 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7692 | && bfd_get_format (abfd) == bfd_object) | |
7693 | return elf_dt_name (abfd); | |
7694 | return NULL; | |
7695 | } | |
7696 | ||
7697 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7698 | the ELF linker emulation code. */ | |
7699 | ||
7700 | bfd_boolean | |
7701 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7702 | struct bfd_link_needed_list **pneeded) | |
7703 | { | |
7704 | asection *s; | |
7705 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7706 | unsigned int elfsec; |
4d269e42 AM |
7707 | unsigned long shlink; |
7708 | bfd_byte *extdyn, *extdynend; | |
7709 | size_t extdynsize; | |
7710 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7711 | ||
7712 | *pneeded = NULL; | |
7713 | ||
7714 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7715 | || bfd_get_format (abfd) != bfd_object) | |
7716 | return TRUE; | |
7717 | ||
7718 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7719 | if (s == NULL || s->size == 0) | |
7720 | return TRUE; | |
7721 | ||
7722 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7723 | goto error_return; | |
7724 | ||
7725 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7726 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7727 | goto error_return; |
7728 | ||
7729 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7730 | |
4d269e42 AM |
7731 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7732 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7733 | ||
7734 | extdyn = dynbuf; | |
7735 | extdynend = extdyn + s->size; | |
7736 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7737 | { | |
7738 | Elf_Internal_Dyn dyn; | |
7739 | ||
7740 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7741 | ||
7742 | if (dyn.d_tag == DT_NULL) | |
7743 | break; | |
7744 | ||
7745 | if (dyn.d_tag == DT_NEEDED) | |
7746 | { | |
7747 | const char *string; | |
7748 | struct bfd_link_needed_list *l; | |
7749 | unsigned int tagv = dyn.d_un.d_val; | |
7750 | bfd_size_type amt; | |
7751 | ||
7752 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7753 | if (string == NULL) | |
7754 | goto error_return; | |
7755 | ||
7756 | amt = sizeof *l; | |
a50b1753 | 7757 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7758 | if (l == NULL) |
7759 | goto error_return; | |
7760 | ||
7761 | l->by = abfd; | |
7762 | l->name = string; | |
7763 | l->next = *pneeded; | |
7764 | *pneeded = l; | |
7765 | } | |
7766 | } | |
7767 | ||
7768 | free (dynbuf); | |
7769 | ||
7770 | return TRUE; | |
7771 | ||
7772 | error_return: | |
7773 | if (dynbuf != NULL) | |
7774 | free (dynbuf); | |
7775 | return FALSE; | |
7776 | } | |
7777 | ||
7778 | struct elf_symbuf_symbol | |
7779 | { | |
7780 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7781 | unsigned char st_info; /* Type and binding attributes */ | |
7782 | unsigned char st_other; /* Visibilty, and target specific */ | |
7783 | }; | |
7784 | ||
7785 | struct elf_symbuf_head | |
7786 | { | |
7787 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7788 | size_t count; |
4d269e42 AM |
7789 | unsigned int st_shndx; |
7790 | }; | |
7791 | ||
7792 | struct elf_symbol | |
7793 | { | |
7794 | union | |
7795 | { | |
7796 | Elf_Internal_Sym *isym; | |
7797 | struct elf_symbuf_symbol *ssym; | |
7798 | } u; | |
7799 | const char *name; | |
7800 | }; | |
7801 | ||
7802 | /* Sort references to symbols by ascending section number. */ | |
7803 | ||
7804 | static int | |
7805 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7806 | { | |
7807 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7808 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7809 | ||
7810 | return s1->st_shndx - s2->st_shndx; | |
7811 | } | |
7812 | ||
7813 | static int | |
7814 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7815 | { | |
7816 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7817 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7818 | return strcmp (s1->name, s2->name); | |
7819 | } | |
7820 | ||
7821 | static struct elf_symbuf_head * | |
ef53be89 | 7822 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7823 | { |
14b1c01e | 7824 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7825 | struct elf_symbuf_symbol *ssym; |
7826 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7827 | size_t i, shndx_count, total_size; |
4d269e42 | 7828 | |
a50b1753 | 7829 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7830 | if (indbuf == NULL) |
7831 | return NULL; | |
7832 | ||
7833 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7834 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7835 | *ind++ = &isymbuf[i]; | |
7836 | indbufend = ind; | |
7837 | ||
7838 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7839 | elf_sort_elf_symbol); | |
7840 | ||
7841 | shndx_count = 0; | |
7842 | if (indbufend > indbuf) | |
7843 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7844 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7845 | shndx_count++; | |
7846 | ||
3ae181ee L |
7847 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7848 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7849 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7850 | if (ssymbuf == NULL) |
7851 | { | |
7852 | free (indbuf); | |
7853 | return NULL; | |
7854 | } | |
7855 | ||
3ae181ee | 7856 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7857 | ssymbuf->ssym = NULL; |
7858 | ssymbuf->count = shndx_count; | |
7859 | ssymbuf->st_shndx = 0; | |
7860 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7861 | { | |
7862 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7863 | { | |
7864 | ssymhead++; | |
7865 | ssymhead->ssym = ssym; | |
7866 | ssymhead->count = 0; | |
7867 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7868 | } | |
7869 | ssym->st_name = (*ind)->st_name; | |
7870 | ssym->st_info = (*ind)->st_info; | |
7871 | ssym->st_other = (*ind)->st_other; | |
7872 | ssymhead->count++; | |
7873 | } | |
ef53be89 | 7874 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7875 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7876 | == total_size)); | |
4d269e42 AM |
7877 | |
7878 | free (indbuf); | |
7879 | return ssymbuf; | |
7880 | } | |
7881 | ||
7882 | /* Check if 2 sections define the same set of local and global | |
7883 | symbols. */ | |
7884 | ||
8f317e31 | 7885 | static bfd_boolean |
4d269e42 AM |
7886 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7887 | struct bfd_link_info *info) | |
7888 | { | |
7889 | bfd *bfd1, *bfd2; | |
7890 | const struct elf_backend_data *bed1, *bed2; | |
7891 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7892 | size_t symcount1, symcount2; |
4d269e42 AM |
7893 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7894 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7895 | Elf_Internal_Sym *isym, *isymend; | |
7896 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7897 | size_t count1, count2, i; |
cb33740c | 7898 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7899 | bfd_boolean result; |
7900 | ||
7901 | bfd1 = sec1->owner; | |
7902 | bfd2 = sec2->owner; | |
7903 | ||
4d269e42 AM |
7904 | /* Both sections have to be in ELF. */ |
7905 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7906 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7907 | return FALSE; | |
7908 | ||
7909 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7910 | return FALSE; | |
7911 | ||
4d269e42 AM |
7912 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7913 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7914 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7915 | return FALSE; |
7916 | ||
7917 | bed1 = get_elf_backend_data (bfd1); | |
7918 | bed2 = get_elf_backend_data (bfd2); | |
7919 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7920 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7921 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7922 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7923 | ||
7924 | if (symcount1 == 0 || symcount2 == 0) | |
7925 | return FALSE; | |
7926 | ||
7927 | result = FALSE; | |
7928 | isymbuf1 = NULL; | |
7929 | isymbuf2 = NULL; | |
a50b1753 NC |
7930 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7931 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7932 | |
7933 | if (ssymbuf1 == NULL) | |
7934 | { | |
7935 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7936 | NULL, NULL, NULL); | |
7937 | if (isymbuf1 == NULL) | |
7938 | goto done; | |
7939 | ||
7940 | if (!info->reduce_memory_overheads) | |
7941 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7942 | = elf_create_symbuf (symcount1, isymbuf1); | |
7943 | } | |
7944 | ||
7945 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7946 | { | |
7947 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7948 | NULL, NULL, NULL); | |
7949 | if (isymbuf2 == NULL) | |
7950 | goto done; | |
7951 | ||
7952 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7953 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7954 | = elf_create_symbuf (symcount2, isymbuf2); | |
7955 | } | |
7956 | ||
7957 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
7958 | { | |
7959 | /* Optimized faster version. */ | |
ef53be89 | 7960 | size_t lo, hi, mid; |
4d269e42 AM |
7961 | struct elf_symbol *symp; |
7962 | struct elf_symbuf_symbol *ssym, *ssymend; | |
7963 | ||
7964 | lo = 0; | |
7965 | hi = ssymbuf1->count; | |
7966 | ssymbuf1++; | |
7967 | count1 = 0; | |
7968 | while (lo < hi) | |
7969 | { | |
7970 | mid = (lo + hi) / 2; | |
cb33740c | 7971 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 7972 | hi = mid; |
cb33740c | 7973 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
7974 | lo = mid + 1; |
7975 | else | |
7976 | { | |
7977 | count1 = ssymbuf1[mid].count; | |
7978 | ssymbuf1 += mid; | |
7979 | break; | |
7980 | } | |
7981 | } | |
7982 | ||
7983 | lo = 0; | |
7984 | hi = ssymbuf2->count; | |
7985 | ssymbuf2++; | |
7986 | count2 = 0; | |
7987 | while (lo < hi) | |
7988 | { | |
7989 | mid = (lo + hi) / 2; | |
cb33740c | 7990 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 7991 | hi = mid; |
cb33740c | 7992 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
7993 | lo = mid + 1; |
7994 | else | |
7995 | { | |
7996 | count2 = ssymbuf2[mid].count; | |
7997 | ssymbuf2 += mid; | |
7998 | break; | |
7999 | } | |
8000 | } | |
8001 | ||
8002 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8003 | goto done; | |
8004 | ||
ca4be51c AM |
8005 | symtable1 |
8006 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
8007 | symtable2 | |
8008 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
8009 | if (symtable1 == NULL || symtable2 == NULL) |
8010 | goto done; | |
8011 | ||
8012 | symp = symtable1; | |
8013 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
8014 | ssym < ssymend; ssym++, symp++) | |
8015 | { | |
8016 | symp->u.ssym = ssym; | |
8017 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
8018 | hdr1->sh_link, | |
8019 | ssym->st_name); | |
8020 | } | |
8021 | ||
8022 | symp = symtable2; | |
8023 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
8024 | ssym < ssymend; ssym++, symp++) | |
8025 | { | |
8026 | symp->u.ssym = ssym; | |
8027 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
8028 | hdr2->sh_link, | |
8029 | ssym->st_name); | |
8030 | } | |
8031 | ||
8032 | /* Sort symbol by name. */ | |
8033 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8034 | elf_sym_name_compare); | |
8035 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8036 | elf_sym_name_compare); | |
8037 | ||
8038 | for (i = 0; i < count1; i++) | |
8039 | /* Two symbols must have the same binding, type and name. */ | |
8040 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
8041 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
8042 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8043 | goto done; | |
8044 | ||
8045 | result = TRUE; | |
8046 | goto done; | |
8047 | } | |
8048 | ||
a50b1753 NC |
8049 | symtable1 = (struct elf_symbol *) |
8050 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
8051 | symtable2 = (struct elf_symbol *) | |
8052 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
8053 | if (symtable1 == NULL || symtable2 == NULL) |
8054 | goto done; | |
8055 | ||
8056 | /* Count definitions in the section. */ | |
8057 | count1 = 0; | |
8058 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 8059 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
8060 | symtable1[count1++].u.isym = isym; |
8061 | ||
8062 | count2 = 0; | |
8063 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 8064 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
8065 | symtable2[count2++].u.isym = isym; |
8066 | ||
8067 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8068 | goto done; | |
8069 | ||
8070 | for (i = 0; i < count1; i++) | |
8071 | symtable1[i].name | |
8072 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
8073 | symtable1[i].u.isym->st_name); | |
8074 | ||
8075 | for (i = 0; i < count2; i++) | |
8076 | symtable2[i].name | |
8077 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
8078 | symtable2[i].u.isym->st_name); | |
8079 | ||
8080 | /* Sort symbol by name. */ | |
8081 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8082 | elf_sym_name_compare); | |
8083 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8084 | elf_sym_name_compare); | |
8085 | ||
8086 | for (i = 0; i < count1; i++) | |
8087 | /* Two symbols must have the same binding, type and name. */ | |
8088 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
8089 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
8090 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8091 | goto done; | |
8092 | ||
8093 | result = TRUE; | |
8094 | ||
8095 | done: | |
8096 | if (symtable1) | |
8097 | free (symtable1); | |
8098 | if (symtable2) | |
8099 | free (symtable2); | |
8100 | if (isymbuf1) | |
8101 | free (isymbuf1); | |
8102 | if (isymbuf2) | |
8103 | free (isymbuf2); | |
8104 | ||
8105 | return result; | |
8106 | } | |
8107 | ||
8108 | /* Return TRUE if 2 section types are compatible. */ | |
8109 | ||
8110 | bfd_boolean | |
8111 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
8112 | bfd *bbfd, const asection *bsec) | |
8113 | { | |
8114 | if (asec == NULL | |
8115 | || bsec == NULL | |
8116 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
8117 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
8118 | return TRUE; | |
8119 | ||
8120 | return elf_section_type (asec) == elf_section_type (bsec); | |
8121 | } | |
8122 | \f | |
c152c796 AM |
8123 | /* Final phase of ELF linker. */ |
8124 | ||
8125 | /* A structure we use to avoid passing large numbers of arguments. */ | |
8126 | ||
8127 | struct elf_final_link_info | |
8128 | { | |
8129 | /* General link information. */ | |
8130 | struct bfd_link_info *info; | |
8131 | /* Output BFD. */ | |
8132 | bfd *output_bfd; | |
8133 | /* Symbol string table. */ | |
ef10c3ac | 8134 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
8135 | /* .hash section. */ |
8136 | asection *hash_sec; | |
8137 | /* symbol version section (.gnu.version). */ | |
8138 | asection *symver_sec; | |
8139 | /* Buffer large enough to hold contents of any section. */ | |
8140 | bfd_byte *contents; | |
8141 | /* Buffer large enough to hold external relocs of any section. */ | |
8142 | void *external_relocs; | |
8143 | /* Buffer large enough to hold internal relocs of any section. */ | |
8144 | Elf_Internal_Rela *internal_relocs; | |
8145 | /* Buffer large enough to hold external local symbols of any input | |
8146 | BFD. */ | |
8147 | bfd_byte *external_syms; | |
8148 | /* And a buffer for symbol section indices. */ | |
8149 | Elf_External_Sym_Shndx *locsym_shndx; | |
8150 | /* Buffer large enough to hold internal local symbols of any input | |
8151 | BFD. */ | |
8152 | Elf_Internal_Sym *internal_syms; | |
8153 | /* Array large enough to hold a symbol index for each local symbol | |
8154 | of any input BFD. */ | |
8155 | long *indices; | |
8156 | /* Array large enough to hold a section pointer for each local | |
8157 | symbol of any input BFD. */ | |
8158 | asection **sections; | |
ef10c3ac | 8159 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 8160 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
8161 | /* Number of STT_FILE syms seen. */ |
8162 | size_t filesym_count; | |
c152c796 AM |
8163 | }; |
8164 | ||
8165 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
8166 | ||
8167 | struct elf_outext_info | |
8168 | { | |
8169 | bfd_boolean failed; | |
8170 | bfd_boolean localsyms; | |
34a79995 | 8171 | bfd_boolean file_sym_done; |
8b127cbc | 8172 | struct elf_final_link_info *flinfo; |
c152c796 AM |
8173 | }; |
8174 | ||
d9352518 DB |
8175 | |
8176 | /* Support for evaluating a complex relocation. | |
8177 | ||
8178 | Complex relocations are generalized, self-describing relocations. The | |
8179 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 8180 | relocations themselves. |
d9352518 DB |
8181 | |
8182 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
8183 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
8184 | information (start bit, end bit, word width, etc) into the addend. This | |
8185 | information is extracted from CGEN-generated operand tables within gas. | |
8186 | ||
8187 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
8188 | internal) representing prefix-notation expressions, including but not | |
8189 | limited to those sorts of expressions normally encoded as addends in the | |
8190 | addend field. The symbol mangling format is: | |
8191 | ||
8192 | <node> := <literal> | |
07d6d2b8 AM |
8193 | | <unary-operator> ':' <node> |
8194 | | <binary-operator> ':' <node> ':' <node> | |
d9352518 DB |
8195 | ; |
8196 | ||
8197 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
07d6d2b8 | 8198 | | 'S' <digits=N> ':' <N character section name> |
d9352518 DB |
8199 | | '#' <hexdigits> |
8200 | ; | |
8201 | ||
8202 | <binary-operator> := as in C | |
8203 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
8204 | ||
8205 | static void | |
a0c8462f AM |
8206 | set_symbol_value (bfd *bfd_with_globals, |
8207 | Elf_Internal_Sym *isymbuf, | |
8208 | size_t locsymcount, | |
8209 | size_t symidx, | |
8210 | bfd_vma val) | |
d9352518 | 8211 | { |
8977835c AM |
8212 | struct elf_link_hash_entry **sym_hashes; |
8213 | struct elf_link_hash_entry *h; | |
8214 | size_t extsymoff = locsymcount; | |
d9352518 | 8215 | |
8977835c | 8216 | if (symidx < locsymcount) |
d9352518 | 8217 | { |
8977835c AM |
8218 | Elf_Internal_Sym *sym; |
8219 | ||
8220 | sym = isymbuf + symidx; | |
8221 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
8222 | { | |
8223 | /* It is a local symbol: move it to the | |
8224 | "absolute" section and give it a value. */ | |
8225 | sym->st_shndx = SHN_ABS; | |
8226 | sym->st_value = val; | |
8227 | return; | |
8228 | } | |
8229 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
8230 | extsymoff = 0; | |
d9352518 | 8231 | } |
8977835c AM |
8232 | |
8233 | /* It is a global symbol: set its link type | |
8234 | to "defined" and give it a value. */ | |
8235 | ||
8236 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
8237 | h = sym_hashes [symidx - extsymoff]; | |
8238 | while (h->root.type == bfd_link_hash_indirect | |
8239 | || h->root.type == bfd_link_hash_warning) | |
8240 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8241 | h->root.type = bfd_link_hash_defined; | |
8242 | h->root.u.def.value = val; | |
8243 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
8244 | } |
8245 | ||
a0c8462f AM |
8246 | static bfd_boolean |
8247 | resolve_symbol (const char *name, | |
8248 | bfd *input_bfd, | |
8b127cbc | 8249 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8250 | bfd_vma *result, |
8251 | Elf_Internal_Sym *isymbuf, | |
8252 | size_t locsymcount) | |
d9352518 | 8253 | { |
a0c8462f AM |
8254 | Elf_Internal_Sym *sym; |
8255 | struct bfd_link_hash_entry *global_entry; | |
8256 | const char *candidate = NULL; | |
8257 | Elf_Internal_Shdr *symtab_hdr; | |
8258 | size_t i; | |
8259 | ||
d9352518 DB |
8260 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
8261 | ||
8262 | for (i = 0; i < locsymcount; ++ i) | |
8263 | { | |
8977835c | 8264 | sym = isymbuf + i; |
d9352518 DB |
8265 | |
8266 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
8267 | continue; | |
8268 | ||
8269 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
8270 | symtab_hdr->sh_link, | |
8271 | sym->st_name); | |
8272 | #ifdef DEBUG | |
0f02bbd9 AM |
8273 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
8274 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
8275 | #endif |
8276 | if (candidate && strcmp (candidate, name) == 0) | |
8277 | { | |
8b127cbc | 8278 | asection *sec = flinfo->sections [i]; |
d9352518 | 8279 | |
0f02bbd9 AM |
8280 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
8281 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 8282 | #ifdef DEBUG |
0f02bbd9 AM |
8283 | printf ("Found symbol with value %8.8lx\n", |
8284 | (unsigned long) *result); | |
d9352518 DB |
8285 | #endif |
8286 | return TRUE; | |
8287 | } | |
8288 | } | |
8289 | ||
8290 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 8291 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 8292 | FALSE, FALSE, TRUE); |
d9352518 DB |
8293 | if (!global_entry) |
8294 | return FALSE; | |
a0c8462f | 8295 | |
d9352518 DB |
8296 | if (global_entry->type == bfd_link_hash_defined |
8297 | || global_entry->type == bfd_link_hash_defweak) | |
8298 | { | |
a0c8462f AM |
8299 | *result = (global_entry->u.def.value |
8300 | + global_entry->u.def.section->output_section->vma | |
8301 | + global_entry->u.def.section->output_offset); | |
d9352518 | 8302 | #ifdef DEBUG |
0f02bbd9 AM |
8303 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
8304 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
8305 | #endif |
8306 | return TRUE; | |
a0c8462f | 8307 | } |
d9352518 | 8308 | |
d9352518 DB |
8309 | return FALSE; |
8310 | } | |
8311 | ||
37b01f6a DG |
8312 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
8313 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
8314 | names like "foo.end" which is the end address of section "foo". */ | |
07d6d2b8 | 8315 | |
d9352518 | 8316 | static bfd_boolean |
a0c8462f AM |
8317 | resolve_section (const char *name, |
8318 | asection *sections, | |
37b01f6a DG |
8319 | bfd_vma *result, |
8320 | bfd * abfd) | |
d9352518 | 8321 | { |
a0c8462f AM |
8322 | asection *curr; |
8323 | unsigned int len; | |
d9352518 | 8324 | |
a0c8462f | 8325 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8326 | if (strcmp (curr->name, name) == 0) |
8327 | { | |
8328 | *result = curr->vma; | |
8329 | return TRUE; | |
8330 | } | |
8331 | ||
8332 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 8333 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 8334 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8335 | { |
8336 | len = strlen (curr->name); | |
a0c8462f | 8337 | if (len > strlen (name)) |
d9352518 DB |
8338 | continue; |
8339 | ||
8340 | if (strncmp (curr->name, name, len) == 0) | |
8341 | { | |
8342 | if (strncmp (".end", name + len, 4) == 0) | |
8343 | { | |
37b01f6a | 8344 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
8345 | return TRUE; |
8346 | } | |
8347 | ||
8348 | /* Insert more pseudo-section names here, if you like. */ | |
8349 | } | |
8350 | } | |
a0c8462f | 8351 | |
d9352518 DB |
8352 | return FALSE; |
8353 | } | |
8354 | ||
8355 | static void | |
a0c8462f | 8356 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 8357 | { |
695344c0 | 8358 | /* xgettext:c-format */ |
a0c8462f AM |
8359 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
8360 | reftype, name); | |
d9352518 DB |
8361 | } |
8362 | ||
8363 | static bfd_boolean | |
a0c8462f AM |
8364 | eval_symbol (bfd_vma *result, |
8365 | const char **symp, | |
8366 | bfd *input_bfd, | |
8b127cbc | 8367 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8368 | bfd_vma dot, |
8369 | Elf_Internal_Sym *isymbuf, | |
8370 | size_t locsymcount, | |
8371 | int signed_p) | |
d9352518 | 8372 | { |
4b93929b NC |
8373 | size_t len; |
8374 | size_t symlen; | |
a0c8462f AM |
8375 | bfd_vma a; |
8376 | bfd_vma b; | |
4b93929b | 8377 | char symbuf[4096]; |
0f02bbd9 | 8378 | const char *sym = *symp; |
a0c8462f AM |
8379 | const char *symend; |
8380 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
8381 | |
8382 | len = strlen (sym); | |
8383 | symend = sym + len; | |
8384 | ||
4b93929b | 8385 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
8386 | { |
8387 | bfd_set_error (bfd_error_invalid_operation); | |
8388 | return FALSE; | |
8389 | } | |
a0c8462f | 8390 | |
d9352518 DB |
8391 | switch (* sym) |
8392 | { | |
8393 | case '.': | |
0f02bbd9 AM |
8394 | *result = dot; |
8395 | *symp = sym + 1; | |
d9352518 DB |
8396 | return TRUE; |
8397 | ||
8398 | case '#': | |
0f02bbd9 AM |
8399 | ++sym; |
8400 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
8401 | return TRUE; |
8402 | ||
8403 | case 'S': | |
8404 | symbol_is_section = TRUE; | |
1a0670f3 | 8405 | /* Fall through. */ |
a0c8462f | 8406 | case 's': |
0f02bbd9 AM |
8407 | ++sym; |
8408 | symlen = strtol (sym, (char **) symp, 10); | |
8409 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 8410 | |
4b93929b | 8411 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
8412 | { |
8413 | bfd_set_error (bfd_error_invalid_operation); | |
8414 | return FALSE; | |
8415 | } | |
8416 | ||
8417 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 8418 | symbuf[symlen] = '\0'; |
0f02bbd9 | 8419 | *symp = sym + symlen; |
a0c8462f AM |
8420 | |
8421 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
8422 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
8423 | interpretation here; section means "try section first", not "must be a | |
8424 | section", and likewise with symbol. */ | |
8425 | ||
a0c8462f | 8426 | if (symbol_is_section) |
d9352518 | 8427 | { |
37b01f6a | 8428 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 8429 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8430 | isymbuf, locsymcount)) |
d9352518 DB |
8431 | { |
8432 | undefined_reference ("section", symbuf); | |
8433 | return FALSE; | |
8434 | } | |
a0c8462f AM |
8435 | } |
8436 | else | |
d9352518 | 8437 | { |
8b127cbc | 8438 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8439 | isymbuf, locsymcount) |
8b127cbc | 8440 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8441 | result, input_bfd)) |
d9352518 DB |
8442 | { |
8443 | undefined_reference ("symbol", symbuf); | |
8444 | return FALSE; | |
8445 | } | |
8446 | } | |
8447 | ||
8448 | return TRUE; | |
a0c8462f | 8449 | |
d9352518 DB |
8450 | /* All that remains are operators. */ |
8451 | ||
8452 | #define UNARY_OP(op) \ | |
8453 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8454 | { \ | |
8455 | sym += strlen (#op); \ | |
a0c8462f AM |
8456 | if (*sym == ':') \ |
8457 | ++sym; \ | |
0f02bbd9 | 8458 | *symp = sym; \ |
8b127cbc | 8459 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8460 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8461 | return FALSE; \ |
8462 | if (signed_p) \ | |
0f02bbd9 | 8463 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8464 | else \ |
8465 | *result = op a; \ | |
d9352518 DB |
8466 | return TRUE; \ |
8467 | } | |
8468 | ||
8469 | #define BINARY_OP(op) \ | |
8470 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8471 | { \ | |
8472 | sym += strlen (#op); \ | |
a0c8462f AM |
8473 | if (*sym == ':') \ |
8474 | ++sym; \ | |
0f02bbd9 | 8475 | *symp = sym; \ |
8b127cbc | 8476 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8477 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8478 | return FALSE; \ |
0f02bbd9 | 8479 | ++*symp; \ |
8b127cbc | 8480 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8481 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8482 | return FALSE; \ |
8483 | if (signed_p) \ | |
0f02bbd9 | 8484 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8485 | else \ |
8486 | *result = a op b; \ | |
d9352518 DB |
8487 | return TRUE; \ |
8488 | } | |
8489 | ||
8490 | default: | |
8491 | UNARY_OP (0-); | |
8492 | BINARY_OP (<<); | |
8493 | BINARY_OP (>>); | |
8494 | BINARY_OP (==); | |
8495 | BINARY_OP (!=); | |
8496 | BINARY_OP (<=); | |
8497 | BINARY_OP (>=); | |
8498 | BINARY_OP (&&); | |
8499 | BINARY_OP (||); | |
8500 | UNARY_OP (~); | |
8501 | UNARY_OP (!); | |
8502 | BINARY_OP (*); | |
8503 | BINARY_OP (/); | |
8504 | BINARY_OP (%); | |
8505 | BINARY_OP (^); | |
8506 | BINARY_OP (|); | |
8507 | BINARY_OP (&); | |
8508 | BINARY_OP (+); | |
8509 | BINARY_OP (-); | |
8510 | BINARY_OP (<); | |
8511 | BINARY_OP (>); | |
8512 | #undef UNARY_OP | |
8513 | #undef BINARY_OP | |
8514 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8515 | bfd_set_error (bfd_error_invalid_operation); | |
8516 | return FALSE; | |
8517 | } | |
8518 | } | |
8519 | ||
d9352518 | 8520 | static void |
a0c8462f AM |
8521 | put_value (bfd_vma size, |
8522 | unsigned long chunksz, | |
8523 | bfd *input_bfd, | |
8524 | bfd_vma x, | |
8525 | bfd_byte *location) | |
d9352518 DB |
8526 | { |
8527 | location += (size - chunksz); | |
8528 | ||
41cd1ad1 | 8529 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8530 | { |
8531 | switch (chunksz) | |
8532 | { | |
d9352518 DB |
8533 | case 1: |
8534 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8535 | x >>= 8; |
d9352518 DB |
8536 | break; |
8537 | case 2: | |
8538 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8539 | x >>= 16; |
d9352518 DB |
8540 | break; |
8541 | case 4: | |
8542 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8543 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8544 | x >>= 16; | |
8545 | x >>= 16; | |
d9352518 | 8546 | break; |
d9352518 | 8547 | #ifdef BFD64 |
41cd1ad1 | 8548 | case 8: |
d9352518 | 8549 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8550 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8551 | x >>= 32; | |
8552 | x >>= 32; | |
8553 | break; | |
d9352518 | 8554 | #endif |
41cd1ad1 NC |
8555 | default: |
8556 | abort (); | |
d9352518 DB |
8557 | break; |
8558 | } | |
8559 | } | |
8560 | } | |
8561 | ||
a0c8462f AM |
8562 | static bfd_vma |
8563 | get_value (bfd_vma size, | |
8564 | unsigned long chunksz, | |
8565 | bfd *input_bfd, | |
8566 | bfd_byte *location) | |
d9352518 | 8567 | { |
9b239e0e | 8568 | int shift; |
d9352518 DB |
8569 | bfd_vma x = 0; |
8570 | ||
9b239e0e NC |
8571 | /* Sanity checks. */ |
8572 | BFD_ASSERT (chunksz <= sizeof (x) | |
8573 | && size >= chunksz | |
8574 | && chunksz != 0 | |
8575 | && (size % chunksz) == 0 | |
8576 | && input_bfd != NULL | |
8577 | && location != NULL); | |
8578 | ||
8579 | if (chunksz == sizeof (x)) | |
8580 | { | |
8581 | BFD_ASSERT (size == chunksz); | |
8582 | ||
8583 | /* Make sure that we do not perform an undefined shift operation. | |
8584 | We know that size == chunksz so there will only be one iteration | |
8585 | of the loop below. */ | |
8586 | shift = 0; | |
8587 | } | |
8588 | else | |
8589 | shift = 8 * chunksz; | |
8590 | ||
a0c8462f | 8591 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8592 | { |
8593 | switch (chunksz) | |
8594 | { | |
d9352518 | 8595 | case 1: |
9b239e0e | 8596 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8597 | break; |
8598 | case 2: | |
9b239e0e | 8599 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8600 | break; |
8601 | case 4: | |
9b239e0e | 8602 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8603 | break; |
d9352518 | 8604 | #ifdef BFD64 |
9b239e0e NC |
8605 | case 8: |
8606 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8607 | break; |
9b239e0e NC |
8608 | #endif |
8609 | default: | |
8610 | abort (); | |
d9352518 DB |
8611 | } |
8612 | } | |
8613 | return x; | |
8614 | } | |
8615 | ||
a0c8462f AM |
8616 | static void |
8617 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8618 | unsigned long *oplen, /* in bits */ | |
8619 | unsigned long *len, /* in bits */ | |
8620 | unsigned long *wordsz, /* in bytes */ | |
8621 | unsigned long *chunksz, /* in bytes */ | |
8622 | unsigned long *lsb0_p, | |
8623 | unsigned long *signed_p, | |
8624 | unsigned long *trunc_p, | |
8625 | unsigned long encoded) | |
d9352518 | 8626 | { |
07d6d2b8 AM |
8627 | * start = encoded & 0x3F; |
8628 | * len = (encoded >> 6) & 0x3F; | |
d9352518 DB |
8629 | * oplen = (encoded >> 12) & 0x3F; |
8630 | * wordsz = (encoded >> 18) & 0xF; | |
8631 | * chunksz = (encoded >> 22) & 0xF; | |
8632 | * lsb0_p = (encoded >> 27) & 1; | |
8633 | * signed_p = (encoded >> 28) & 1; | |
8634 | * trunc_p = (encoded >> 29) & 1; | |
8635 | } | |
8636 | ||
cdfeee4f | 8637 | bfd_reloc_status_type |
0f02bbd9 | 8638 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8639 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8640 | bfd_byte *contents, |
8641 | Elf_Internal_Rela *rel, | |
8642 | bfd_vma relocation) | |
d9352518 | 8643 | { |
0f02bbd9 AM |
8644 | bfd_vma shift, x, mask; |
8645 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8646 | bfd_reloc_status_type r; |
d9352518 DB |
8647 | |
8648 | /* Perform this reloc, since it is complex. | |
8649 | (this is not to say that it necessarily refers to a complex | |
8650 | symbol; merely that it is a self-describing CGEN based reloc. | |
8651 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8652 | word size, etc) encoded within it.). */ |
d9352518 | 8653 | |
a0c8462f AM |
8654 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8655 | &chunksz, &lsb0_p, &signed_p, | |
8656 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8657 | |
8658 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8659 | ||
8660 | if (lsb0_p) | |
8661 | shift = (start + 1) - len; | |
8662 | else | |
8663 | shift = (8 * wordsz) - (start + len); | |
8664 | ||
37b01f6a DG |
8665 | x = get_value (wordsz, chunksz, input_bfd, |
8666 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8667 | |
8668 | #ifdef DEBUG | |
8669 | printf ("Doing complex reloc: " | |
8670 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8671 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8672 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8673 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8674 | oplen, (unsigned long) x, (unsigned long) mask, |
8675 | (unsigned long) relocation); | |
d9352518 DB |
8676 | #endif |
8677 | ||
cdfeee4f | 8678 | r = bfd_reloc_ok; |
d9352518 | 8679 | if (! trunc_p) |
cdfeee4f AM |
8680 | /* Now do an overflow check. */ |
8681 | r = bfd_check_overflow ((signed_p | |
8682 | ? complain_overflow_signed | |
8683 | : complain_overflow_unsigned), | |
8684 | len, 0, (8 * wordsz), | |
8685 | relocation); | |
a0c8462f | 8686 | |
d9352518 DB |
8687 | /* Do the deed. */ |
8688 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8689 | ||
8690 | #ifdef DEBUG | |
8691 | printf (" relocation: %8.8lx\n" | |
8692 | " shifted mask: %8.8lx\n" | |
8693 | " shifted/masked reloc: %8.8lx\n" | |
8694 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8695 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8696 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8697 | #endif |
37b01f6a DG |
8698 | put_value (wordsz, chunksz, input_bfd, x, |
8699 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8700 | return r; |
d9352518 DB |
8701 | } |
8702 | ||
0e287786 AM |
8703 | /* Functions to read r_offset from external (target order) reloc |
8704 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8705 | know the value is aligned. */ | |
53df40a4 | 8706 | |
0e287786 AM |
8707 | static bfd_vma |
8708 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8709 | { |
8710 | union aligned32 | |
8711 | { | |
8712 | uint32_t v; | |
8713 | unsigned char c[4]; | |
8714 | }; | |
8715 | const union aligned32 *a | |
0e287786 | 8716 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8717 | |
8718 | uint32_t aval = ( (uint32_t) a->c[0] | |
8719 | | (uint32_t) a->c[1] << 8 | |
8720 | | (uint32_t) a->c[2] << 16 | |
8721 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8722 | return aval; |
53df40a4 AM |
8723 | } |
8724 | ||
0e287786 AM |
8725 | static bfd_vma |
8726 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8727 | { |
8728 | union aligned32 | |
8729 | { | |
8730 | uint32_t v; | |
8731 | unsigned char c[4]; | |
8732 | }; | |
8733 | const union aligned32 *a | |
0e287786 | 8734 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8735 | |
8736 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8737 | | (uint32_t) a->c[1] << 16 | |
8738 | | (uint32_t) a->c[2] << 8 | |
8739 | | (uint32_t) a->c[3]); | |
0e287786 | 8740 | return aval; |
53df40a4 AM |
8741 | } |
8742 | ||
8743 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8744 | static bfd_vma |
8745 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8746 | { |
8747 | union aligned64 | |
8748 | { | |
8749 | uint64_t v; | |
8750 | unsigned char c[8]; | |
8751 | }; | |
8752 | const union aligned64 *a | |
0e287786 | 8753 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8754 | |
8755 | uint64_t aval = ( (uint64_t) a->c[0] | |
8756 | | (uint64_t) a->c[1] << 8 | |
8757 | | (uint64_t) a->c[2] << 16 | |
8758 | | (uint64_t) a->c[3] << 24 | |
8759 | | (uint64_t) a->c[4] << 32 | |
8760 | | (uint64_t) a->c[5] << 40 | |
8761 | | (uint64_t) a->c[6] << 48 | |
8762 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8763 | return aval; |
53df40a4 AM |
8764 | } |
8765 | ||
0e287786 AM |
8766 | static bfd_vma |
8767 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8768 | { |
8769 | union aligned64 | |
8770 | { | |
8771 | uint64_t v; | |
8772 | unsigned char c[8]; | |
8773 | }; | |
8774 | const union aligned64 *a | |
0e287786 | 8775 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8776 | |
8777 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8778 | | (uint64_t) a->c[1] << 48 | |
8779 | | (uint64_t) a->c[2] << 40 | |
8780 | | (uint64_t) a->c[3] << 32 | |
8781 | | (uint64_t) a->c[4] << 24 | |
8782 | | (uint64_t) a->c[5] << 16 | |
8783 | | (uint64_t) a->c[6] << 8 | |
8784 | | (uint64_t) a->c[7]); | |
0e287786 | 8785 | return aval; |
53df40a4 AM |
8786 | } |
8787 | #endif | |
8788 | ||
c152c796 AM |
8789 | /* When performing a relocatable link, the input relocations are |
8790 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8791 | referenced must be updated. Update all the relocations found in |
8792 | RELDATA. */ | |
c152c796 | 8793 | |
bca6d0e3 | 8794 | static bfd_boolean |
c152c796 | 8795 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 8796 | asection *sec, |
28dbcedc | 8797 | struct bfd_elf_section_reloc_data *reldata, |
10bbbc1d NC |
8798 | bfd_boolean sort, |
8799 | struct bfd_link_info *info) | |
c152c796 AM |
8800 | { |
8801 | unsigned int i; | |
8802 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8803 | bfd_byte *erela; | |
8804 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8805 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8806 | bfd_vma r_type_mask; | |
8807 | int r_sym_shift; | |
d4730f92 BS |
8808 | unsigned int count = reldata->count; |
8809 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8810 | |
d4730f92 | 8811 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8812 | { |
8813 | swap_in = bed->s->swap_reloc_in; | |
8814 | swap_out = bed->s->swap_reloc_out; | |
8815 | } | |
d4730f92 | 8816 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8817 | { |
8818 | swap_in = bed->s->swap_reloca_in; | |
8819 | swap_out = bed->s->swap_reloca_out; | |
8820 | } | |
8821 | else | |
8822 | abort (); | |
8823 | ||
8824 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8825 | abort (); | |
8826 | ||
8827 | if (bed->s->arch_size == 32) | |
8828 | { | |
8829 | r_type_mask = 0xff; | |
8830 | r_sym_shift = 8; | |
8831 | } | |
8832 | else | |
8833 | { | |
8834 | r_type_mask = 0xffffffff; | |
8835 | r_sym_shift = 32; | |
8836 | } | |
8837 | ||
d4730f92 BS |
8838 | erela = reldata->hdr->contents; |
8839 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8840 | { |
8841 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8842 | unsigned int j; | |
8843 | ||
8844 | if (*rel_hash == NULL) | |
8845 | continue; | |
8846 | ||
10bbbc1d NC |
8847 | if ((*rel_hash)->indx == -2 |
8848 | && info->gc_sections | |
8849 | && ! info->gc_keep_exported) | |
8850 | { | |
8851 | /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ | |
9793eb77 | 8852 | _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"), |
10bbbc1d NC |
8853 | abfd, sec, |
8854 | (*rel_hash)->root.root.string); | |
9793eb77 | 8855 | _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"), |
d42c267e | 8856 | abfd, sec); |
10bbbc1d NC |
8857 | bfd_set_error (bfd_error_invalid_operation); |
8858 | return FALSE; | |
8859 | } | |
c152c796 AM |
8860 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
8861 | ||
8862 | (*swap_in) (abfd, erela, irela); | |
8863 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8864 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8865 | | (irela[j].r_info & r_type_mask)); | |
8866 | (*swap_out) (abfd, irela, erela); | |
8867 | } | |
53df40a4 | 8868 | |
9eaff861 AO |
8869 | if (bed->elf_backend_update_relocs) |
8870 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
8871 | ||
0e287786 | 8872 | if (sort && count != 0) |
53df40a4 | 8873 | { |
0e287786 AM |
8874 | bfd_vma (*ext_r_off) (const void *); |
8875 | bfd_vma r_off; | |
8876 | size_t elt_size; | |
8877 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8878 | bfd_byte *buf = NULL; |
28dbcedc AM |
8879 | |
8880 | if (bed->s->arch_size == 32) | |
8881 | { | |
8882 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8883 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8884 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8885 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8886 | else |
8887 | abort (); | |
8888 | } | |
53df40a4 | 8889 | else |
28dbcedc | 8890 | { |
53df40a4 | 8891 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8892 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8893 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8894 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8895 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8896 | else |
53df40a4 | 8897 | #endif |
28dbcedc AM |
8898 | abort (); |
8899 | } | |
0e287786 | 8900 | |
bca6d0e3 AM |
8901 | /* Must use a stable sort here. A modified insertion sort, |
8902 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8903 | elt_size = reldata->hdr->sh_entsize; |
8904 | base = reldata->hdr->contents; | |
8905 | end = base + count * elt_size; | |
bca6d0e3 | 8906 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8907 | abort (); |
8908 | ||
8909 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8910 | speeding the main loop below. */ | |
8911 | r_off = (*ext_r_off) (base); | |
8912 | for (p = loc = base; (p += elt_size) < end; ) | |
8913 | { | |
8914 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8915 | if (r_off > r_off2) | |
8916 | { | |
8917 | r_off = r_off2; | |
8918 | loc = p; | |
8919 | } | |
8920 | } | |
8921 | if (loc != base) | |
8922 | { | |
8923 | /* Don't just swap *base and *loc as that changes the order | |
8924 | of the original base[0] and base[1] if they happen to | |
8925 | have the same r_offset. */ | |
bca6d0e3 AM |
8926 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8927 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8928 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8929 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8930 | } |
8931 | ||
b29b8669 | 8932 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8933 | { |
8934 | /* base to p is sorted, *p is next to insert. */ | |
8935 | r_off = (*ext_r_off) (p); | |
8936 | /* Search the sorted region for location to insert. */ | |
8937 | loc = p - elt_size; | |
8938 | while (r_off < (*ext_r_off) (loc)) | |
8939 | loc -= elt_size; | |
8940 | loc += elt_size; | |
8941 | if (loc != p) | |
8942 | { | |
bca6d0e3 AM |
8943 | /* Chances are there is a run of relocs to insert here, |
8944 | from one of more input files. Files are not always | |
8945 | linked in order due to the way elf_link_input_bfd is | |
8946 | called. See pr17666. */ | |
8947 | size_t sortlen = p - loc; | |
8948 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8949 | size_t runlen = elt_size; | |
8950 | size_t buf_size = 96 * 1024; | |
8951 | while (p + runlen < end | |
8952 | && (sortlen <= buf_size | |
8953 | || runlen + elt_size <= buf_size) | |
8954 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8955 | runlen += elt_size; | |
8956 | if (buf == NULL) | |
8957 | { | |
8958 | buf = bfd_malloc (buf_size); | |
8959 | if (buf == NULL) | |
8960 | return FALSE; | |
8961 | } | |
8962 | if (runlen < sortlen) | |
8963 | { | |
8964 | memcpy (buf, p, runlen); | |
8965 | memmove (loc + runlen, loc, sortlen); | |
8966 | memcpy (loc, buf, runlen); | |
8967 | } | |
8968 | else | |
8969 | { | |
8970 | memcpy (buf, loc, sortlen); | |
8971 | memmove (loc, p, runlen); | |
8972 | memcpy (loc + runlen, buf, sortlen); | |
8973 | } | |
b29b8669 | 8974 | p += runlen - elt_size; |
0e287786 AM |
8975 | } |
8976 | } | |
8977 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
8978 | free (reldata->hashes); |
8979 | reldata->hashes = NULL; | |
bca6d0e3 | 8980 | free (buf); |
53df40a4 | 8981 | } |
bca6d0e3 | 8982 | return TRUE; |
c152c796 AM |
8983 | } |
8984 | ||
8985 | struct elf_link_sort_rela | |
8986 | { | |
8987 | union { | |
8988 | bfd_vma offset; | |
8989 | bfd_vma sym_mask; | |
8990 | } u; | |
8991 | enum elf_reloc_type_class type; | |
8992 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
8993 | Elf_Internal_Rela rela[1]; | |
8994 | }; | |
8995 | ||
8996 | static int | |
8997 | elf_link_sort_cmp1 (const void *A, const void *B) | |
8998 | { | |
a50b1753 NC |
8999 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
9000 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
9001 | int relativea, relativeb; |
9002 | ||
9003 | relativea = a->type == reloc_class_relative; | |
9004 | relativeb = b->type == reloc_class_relative; | |
9005 | ||
9006 | if (relativea < relativeb) | |
9007 | return 1; | |
9008 | if (relativea > relativeb) | |
9009 | return -1; | |
9010 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
9011 | return -1; | |
9012 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
9013 | return 1; | |
9014 | if (a->rela->r_offset < b->rela->r_offset) | |
9015 | return -1; | |
9016 | if (a->rela->r_offset > b->rela->r_offset) | |
9017 | return 1; | |
9018 | return 0; | |
9019 | } | |
9020 | ||
9021 | static int | |
9022 | elf_link_sort_cmp2 (const void *A, const void *B) | |
9023 | { | |
a50b1753 NC |
9024 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
9025 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 9026 | |
7e612e98 | 9027 | if (a->type < b->type) |
c152c796 | 9028 | return -1; |
7e612e98 | 9029 | if (a->type > b->type) |
c152c796 | 9030 | return 1; |
7e612e98 | 9031 | if (a->u.offset < b->u.offset) |
c152c796 | 9032 | return -1; |
7e612e98 | 9033 | if (a->u.offset > b->u.offset) |
c152c796 AM |
9034 | return 1; |
9035 | if (a->rela->r_offset < b->rela->r_offset) | |
9036 | return -1; | |
9037 | if (a->rela->r_offset > b->rela->r_offset) | |
9038 | return 1; | |
9039 | return 0; | |
9040 | } | |
9041 | ||
9042 | static size_t | |
9043 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
9044 | { | |
3410fea8 | 9045 | asection *dynamic_relocs; |
fc66a176 L |
9046 | asection *rela_dyn; |
9047 | asection *rel_dyn; | |
c152c796 AM |
9048 | bfd_size_type count, size; |
9049 | size_t i, ret, sort_elt, ext_size; | |
9050 | bfd_byte *sort, *s_non_relative, *p; | |
9051 | struct elf_link_sort_rela *sq; | |
9052 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9053 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 9054 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
9055 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
9056 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
9057 | struct bfd_link_order *lo; | |
9058 | bfd_vma r_sym_mask; | |
3410fea8 | 9059 | bfd_boolean use_rela; |
c152c796 | 9060 | |
3410fea8 NC |
9061 | /* Find a dynamic reloc section. */ |
9062 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
9063 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
9064 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
9065 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 9066 | { |
3410fea8 NC |
9067 | bfd_boolean use_rela_initialised = FALSE; |
9068 | ||
9069 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 9070 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
9071 | use_rela = TRUE; |
9072 | ||
9073 | /* Both sections are present. Examine the sizes | |
9074 | of the indirect sections to help us choose. */ | |
9075 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9076 | if (lo->type == bfd_indirect_link_order) | |
9077 | { | |
9078 | asection *o = lo->u.indirect.section; | |
9079 | ||
9080 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9081 | { | |
9082 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9083 | /* Section size is divisible by both rel and rela sizes. | |
9084 | It is of no help to us. */ | |
9085 | ; | |
9086 | else | |
9087 | { | |
9088 | /* Section size is only divisible by rela. */ | |
535b785f | 9089 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9090 | { |
9793eb77 | 9091 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9092 | "they are in more than one size"), |
9093 | abfd); | |
3410fea8 NC |
9094 | bfd_set_error (bfd_error_invalid_operation); |
9095 | return 0; | |
9096 | } | |
9097 | else | |
9098 | { | |
9099 | use_rela = TRUE; | |
9100 | use_rela_initialised = TRUE; | |
9101 | } | |
9102 | } | |
9103 | } | |
9104 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9105 | { | |
9106 | /* Section size is only divisible by rel. */ | |
535b785f | 9107 | if (use_rela_initialised && use_rela) |
3410fea8 | 9108 | { |
9793eb77 | 9109 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9110 | "they are in more than one size"), |
9111 | abfd); | |
3410fea8 NC |
9112 | bfd_set_error (bfd_error_invalid_operation); |
9113 | return 0; | |
9114 | } | |
9115 | else | |
9116 | { | |
9117 | use_rela = FALSE; | |
9118 | use_rela_initialised = TRUE; | |
9119 | } | |
9120 | } | |
9121 | else | |
9122 | { | |
c8e44c6d AM |
9123 | /* The section size is not divisible by either - |
9124 | something is wrong. */ | |
9793eb77 | 9125 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9126 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9127 | bfd_set_error (bfd_error_invalid_operation); |
9128 | return 0; | |
9129 | } | |
9130 | } | |
9131 | ||
9132 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9133 | if (lo->type == bfd_indirect_link_order) | |
9134 | { | |
9135 | asection *o = lo->u.indirect.section; | |
9136 | ||
9137 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9138 | { | |
9139 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9140 | /* Section size is divisible by both rel and rela sizes. | |
9141 | It is of no help to us. */ | |
9142 | ; | |
9143 | else | |
9144 | { | |
9145 | /* Section size is only divisible by rela. */ | |
535b785f | 9146 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9147 | { |
9793eb77 | 9148 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9149 | "they are in more than one size"), |
9150 | abfd); | |
3410fea8 NC |
9151 | bfd_set_error (bfd_error_invalid_operation); |
9152 | return 0; | |
9153 | } | |
9154 | else | |
9155 | { | |
9156 | use_rela = TRUE; | |
9157 | use_rela_initialised = TRUE; | |
9158 | } | |
9159 | } | |
9160 | } | |
9161 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9162 | { | |
9163 | /* Section size is only divisible by rel. */ | |
535b785f | 9164 | if (use_rela_initialised && use_rela) |
3410fea8 | 9165 | { |
9793eb77 | 9166 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9167 | "they are in more than one size"), |
9168 | abfd); | |
3410fea8 NC |
9169 | bfd_set_error (bfd_error_invalid_operation); |
9170 | return 0; | |
9171 | } | |
9172 | else | |
9173 | { | |
9174 | use_rela = FALSE; | |
9175 | use_rela_initialised = TRUE; | |
9176 | } | |
9177 | } | |
9178 | else | |
9179 | { | |
c8e44c6d AM |
9180 | /* The section size is not divisible by either - |
9181 | something is wrong. */ | |
9793eb77 | 9182 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9183 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9184 | bfd_set_error (bfd_error_invalid_operation); |
9185 | return 0; | |
9186 | } | |
9187 | } | |
9188 | ||
9189 | if (! use_rela_initialised) | |
9190 | /* Make a guess. */ | |
9191 | use_rela = TRUE; | |
c152c796 | 9192 | } |
fc66a176 L |
9193 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
9194 | use_rela = TRUE; | |
9195 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 9196 | use_rela = FALSE; |
c152c796 | 9197 | else |
fc66a176 | 9198 | return 0; |
3410fea8 NC |
9199 | |
9200 | if (use_rela) | |
c152c796 | 9201 | { |
3410fea8 | 9202 | dynamic_relocs = rela_dyn; |
c152c796 AM |
9203 | ext_size = bed->s->sizeof_rela; |
9204 | swap_in = bed->s->swap_reloca_in; | |
9205 | swap_out = bed->s->swap_reloca_out; | |
9206 | } | |
3410fea8 NC |
9207 | else |
9208 | { | |
9209 | dynamic_relocs = rel_dyn; | |
9210 | ext_size = bed->s->sizeof_rel; | |
9211 | swap_in = bed->s->swap_reloc_in; | |
9212 | swap_out = bed->s->swap_reloc_out; | |
9213 | } | |
c152c796 AM |
9214 | |
9215 | size = 0; | |
3410fea8 | 9216 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 9217 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 9218 | size += lo->u.indirect.section->size; |
c152c796 | 9219 | |
3410fea8 | 9220 | if (size != dynamic_relocs->size) |
c152c796 AM |
9221 | return 0; |
9222 | ||
9223 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
9224 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
9225 | |
9226 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
9227 | if (count == 0) |
9228 | return 0; | |
a50b1753 | 9229 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 9230 | |
c152c796 AM |
9231 | if (sort == NULL) |
9232 | { | |
9233 | (*info->callbacks->warning) | |
9793eb77 | 9234 | (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0); |
c152c796 AM |
9235 | return 0; |
9236 | } | |
9237 | ||
9238 | if (bed->s->arch_size == 32) | |
9239 | r_sym_mask = ~(bfd_vma) 0xff; | |
9240 | else | |
9241 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
9242 | ||
3410fea8 | 9243 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9244 | if (lo->type == bfd_indirect_link_order) |
9245 | { | |
9246 | bfd_byte *erel, *erelend; | |
9247 | asection *o = lo->u.indirect.section; | |
9248 | ||
1da212d6 AM |
9249 | if (o->contents == NULL && o->size != 0) |
9250 | { | |
9251 | /* This is a reloc section that is being handled as a normal | |
9252 | section. See bfd_section_from_shdr. We can't combine | |
9253 | relocs in this case. */ | |
9254 | free (sort); | |
9255 | return 0; | |
9256 | } | |
c152c796 | 9257 | erel = o->contents; |
eea6121a | 9258 | erelend = o->contents + o->size; |
c8e44c6d | 9259 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 9260 | |
c152c796 AM |
9261 | while (erel < erelend) |
9262 | { | |
9263 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 9264 | |
c152c796 | 9265 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 9266 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
9267 | s->u.sym_mask = r_sym_mask; |
9268 | p += sort_elt; | |
9269 | erel += ext_size; | |
9270 | } | |
9271 | } | |
9272 | ||
9273 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
9274 | ||
9275 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
9276 | { | |
9277 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9278 | if (s->type != reloc_class_relative) | |
9279 | break; | |
9280 | } | |
9281 | ret = i; | |
9282 | s_non_relative = p; | |
9283 | ||
9284 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
9285 | for (; i < count; i++, p += sort_elt) | |
9286 | { | |
9287 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
9288 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
9289 | sq = sp; | |
9290 | sp->u.offset = sq->rela->r_offset; | |
9291 | } | |
9292 | ||
9293 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
9294 | ||
c8e44c6d AM |
9295 | struct elf_link_hash_table *htab = elf_hash_table (info); |
9296 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
9297 | { | |
9298 | /* We have plt relocs in .rela.dyn. */ | |
9299 | sq = (struct elf_link_sort_rela *) sort; | |
9300 | for (i = 0; i < count; i++) | |
9301 | if (sq[count - i - 1].type != reloc_class_plt) | |
9302 | break; | |
9303 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
9304 | { | |
9305 | struct bfd_link_order **plo; | |
9306 | /* Put srelplt link_order last. This is so the output_offset | |
9307 | set in the next loop is correct for DT_JMPREL. */ | |
9308 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
9309 | if ((*plo)->type == bfd_indirect_link_order | |
9310 | && (*plo)->u.indirect.section == htab->srelplt) | |
9311 | { | |
9312 | lo = *plo; | |
9313 | *plo = lo->next; | |
9314 | } | |
9315 | else | |
9316 | plo = &(*plo)->next; | |
9317 | *plo = lo; | |
9318 | lo->next = NULL; | |
9319 | dynamic_relocs->map_tail.link_order = lo; | |
9320 | } | |
9321 | } | |
9322 | ||
9323 | p = sort; | |
3410fea8 | 9324 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9325 | if (lo->type == bfd_indirect_link_order) |
9326 | { | |
9327 | bfd_byte *erel, *erelend; | |
9328 | asection *o = lo->u.indirect.section; | |
9329 | ||
9330 | erel = o->contents; | |
eea6121a | 9331 | erelend = o->contents + o->size; |
c8e44c6d | 9332 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
9333 | while (erel < erelend) |
9334 | { | |
9335 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9336 | (*swap_out) (abfd, s->rela, erel); | |
9337 | p += sort_elt; | |
9338 | erel += ext_size; | |
9339 | } | |
9340 | } | |
9341 | ||
9342 | free (sort); | |
3410fea8 | 9343 | *psec = dynamic_relocs; |
c152c796 AM |
9344 | return ret; |
9345 | } | |
9346 | ||
ef10c3ac | 9347 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 9348 | |
6e0b88f1 | 9349 | static int |
ef10c3ac L |
9350 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
9351 | const char *name, | |
9352 | Elf_Internal_Sym *elfsym, | |
9353 | asection *input_sec, | |
9354 | struct elf_link_hash_entry *h) | |
c152c796 | 9355 | { |
6e0b88f1 | 9356 | int (*output_symbol_hook) |
c152c796 AM |
9357 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
9358 | struct elf_link_hash_entry *); | |
ef10c3ac | 9359 | struct elf_link_hash_table *hash_table; |
c152c796 | 9360 | const struct elf_backend_data *bed; |
ef10c3ac | 9361 | bfd_size_type strtabsize; |
c152c796 | 9362 | |
8539e4e8 AM |
9363 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
9364 | ||
8b127cbc | 9365 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
9366 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
9367 | if (output_symbol_hook != NULL) | |
9368 | { | |
8b127cbc | 9369 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
9370 | if (ret != 1) |
9371 | return ret; | |
c152c796 AM |
9372 | } |
9373 | ||
ef10c3ac L |
9374 | if (name == NULL |
9375 | || *name == '\0' | |
9376 | || (input_sec->flags & SEC_EXCLUDE)) | |
9377 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
9378 | else |
9379 | { | |
ef10c3ac L |
9380 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
9381 | to get the final offset for st_name. */ | |
9382 | elfsym->st_name | |
9383 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
9384 | name, FALSE); | |
c152c796 | 9385 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 9386 | return 0; |
c152c796 AM |
9387 | } |
9388 | ||
ef10c3ac L |
9389 | hash_table = elf_hash_table (flinfo->info); |
9390 | strtabsize = hash_table->strtabsize; | |
9391 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 9392 | { |
ef10c3ac L |
9393 | strtabsize += strtabsize; |
9394 | hash_table->strtabsize = strtabsize; | |
9395 | strtabsize *= sizeof (*hash_table->strtab); | |
9396 | hash_table->strtab | |
9397 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
9398 | strtabsize); | |
9399 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 9400 | return 0; |
c152c796 | 9401 | } |
ef10c3ac L |
9402 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
9403 | hash_table->strtab[hash_table->strtabcount].dest_index | |
9404 | = hash_table->strtabcount; | |
9405 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
9406 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
9407 | ||
9408 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
9409 | hash_table->strtabcount += 1; | |
9410 | ||
9411 | return 1; | |
9412 | } | |
9413 | ||
9414 | /* Swap symbols out to the symbol table and flush the output symbols to | |
9415 | the file. */ | |
9416 | ||
9417 | static bfd_boolean | |
9418 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
9419 | { | |
9420 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
9421 | bfd_size_type amt; |
9422 | size_t i; | |
ef10c3ac L |
9423 | const struct elf_backend_data *bed; |
9424 | bfd_byte *symbuf; | |
9425 | Elf_Internal_Shdr *hdr; | |
9426 | file_ptr pos; | |
9427 | bfd_boolean ret; | |
9428 | ||
9429 | if (!hash_table->strtabcount) | |
9430 | return TRUE; | |
9431 | ||
9432 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
9433 | ||
9434 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 9435 | |
ef10c3ac L |
9436 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
9437 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
9438 | if (symbuf == NULL) | |
9439 | return FALSE; | |
1b786873 | 9440 | |
ef10c3ac | 9441 | if (flinfo->symshndxbuf) |
c152c796 | 9442 | { |
ef53be89 AM |
9443 | amt = sizeof (Elf_External_Sym_Shndx); |
9444 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
9445 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9446 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 9447 | { |
ef10c3ac L |
9448 | free (symbuf); |
9449 | return FALSE; | |
c152c796 | 9450 | } |
c152c796 AM |
9451 | } |
9452 | ||
ef10c3ac L |
9453 | for (i = 0; i < hash_table->strtabcount; i++) |
9454 | { | |
9455 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
9456 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9457 | elfsym->sym.st_name = 0; | |
9458 | else | |
9459 | elfsym->sym.st_name | |
9460 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9461 | elfsym->sym.st_name); | |
9462 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9463 | ((bfd_byte *) symbuf | |
9464 | + (elfsym->dest_index | |
9465 | * bed->s->sizeof_sym)), | |
9466 | (flinfo->symshndxbuf | |
9467 | + elfsym->destshndx_index)); | |
9468 | } | |
9469 | ||
9470 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9471 | pos = hdr->sh_offset + hdr->sh_size; | |
9472 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9473 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9474 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9475 | { | |
9476 | hdr->sh_size += amt; | |
9477 | ret = TRUE; | |
9478 | } | |
9479 | else | |
9480 | ret = FALSE; | |
c152c796 | 9481 | |
ef10c3ac L |
9482 | free (symbuf); |
9483 | ||
9484 | free (hash_table->strtab); | |
9485 | hash_table->strtab = NULL; | |
9486 | ||
9487 | return ret; | |
c152c796 AM |
9488 | } |
9489 | ||
c0d5a53d L |
9490 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9491 | ||
9492 | static bfd_boolean | |
9493 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9494 | { | |
4fbb74a6 AM |
9495 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9496 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9497 | { |
9498 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9499 | beyond 64k. */ |
4eca0228 | 9500 | _bfd_error_handler |
695344c0 | 9501 | /* xgettext:c-format */ |
9793eb77 | 9502 | (_("%pB: too many sections: %d (>= %d)"), |
4fbb74a6 | 9503 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9504 | bfd_set_error (bfd_error_nonrepresentable_section); |
9505 | return FALSE; | |
9506 | } | |
9507 | return TRUE; | |
9508 | } | |
9509 | ||
c152c796 AM |
9510 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9511 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9512 | versioned symbol that would normally require an explicit version. | |
9513 | We also handle the case that a DSO references a hidden symbol | |
9514 | which may be satisfied by a versioned symbol in another DSO. */ | |
9515 | ||
9516 | static bfd_boolean | |
9517 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9518 | const struct elf_backend_data *bed, | |
9519 | struct elf_link_hash_entry *h) | |
9520 | { | |
9521 | bfd *abfd; | |
9522 | struct elf_link_loaded_list *loaded; | |
9523 | ||
9524 | if (!is_elf_hash_table (info->hash)) | |
9525 | return FALSE; | |
9526 | ||
90c984fc L |
9527 | /* Check indirect symbol. */ |
9528 | while (h->root.type == bfd_link_hash_indirect) | |
9529 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9530 | ||
c152c796 AM |
9531 | switch (h->root.type) |
9532 | { | |
9533 | default: | |
9534 | abfd = NULL; | |
9535 | break; | |
9536 | ||
9537 | case bfd_link_hash_undefined: | |
9538 | case bfd_link_hash_undefweak: | |
9539 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9540 | if (abfd == NULL |
9541 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9542 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9543 | return FALSE; |
9544 | break; | |
9545 | ||
9546 | case bfd_link_hash_defined: | |
9547 | case bfd_link_hash_defweak: | |
9548 | abfd = h->root.u.def.section->owner; | |
9549 | break; | |
9550 | ||
9551 | case bfd_link_hash_common: | |
9552 | abfd = h->root.u.c.p->section->owner; | |
9553 | break; | |
9554 | } | |
9555 | BFD_ASSERT (abfd != NULL); | |
9556 | ||
9557 | for (loaded = elf_hash_table (info)->loaded; | |
9558 | loaded != NULL; | |
9559 | loaded = loaded->next) | |
9560 | { | |
9561 | bfd *input; | |
9562 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9563 | size_t symcount; |
9564 | size_t extsymcount; | |
9565 | size_t extsymoff; | |
c152c796 AM |
9566 | Elf_Internal_Shdr *versymhdr; |
9567 | Elf_Internal_Sym *isym; | |
9568 | Elf_Internal_Sym *isymend; | |
9569 | Elf_Internal_Sym *isymbuf; | |
9570 | Elf_External_Versym *ever; | |
9571 | Elf_External_Versym *extversym; | |
9572 | ||
9573 | input = loaded->abfd; | |
9574 | ||
9575 | /* We check each DSO for a possible hidden versioned definition. */ | |
9576 | if (input == abfd | |
9577 | || (input->flags & DYNAMIC) == 0 | |
9578 | || elf_dynversym (input) == 0) | |
9579 | continue; | |
9580 | ||
9581 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9582 | ||
9583 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9584 | if (elf_bad_symtab (input)) | |
9585 | { | |
9586 | extsymcount = symcount; | |
9587 | extsymoff = 0; | |
9588 | } | |
9589 | else | |
9590 | { | |
9591 | extsymcount = symcount - hdr->sh_info; | |
9592 | extsymoff = hdr->sh_info; | |
9593 | } | |
9594 | ||
9595 | if (extsymcount == 0) | |
9596 | continue; | |
9597 | ||
9598 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9599 | NULL, NULL, NULL); | |
9600 | if (isymbuf == NULL) | |
9601 | return FALSE; | |
9602 | ||
9603 | /* Read in any version definitions. */ | |
9604 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9605 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9606 | if (extversym == NULL) |
9607 | goto error_ret; | |
9608 | ||
9609 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9610 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9611 | != versymhdr->sh_size)) | |
9612 | { | |
9613 | free (extversym); | |
9614 | error_ret: | |
9615 | free (isymbuf); | |
9616 | return FALSE; | |
9617 | } | |
9618 | ||
9619 | ever = extversym + extsymoff; | |
9620 | isymend = isymbuf + extsymcount; | |
9621 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9622 | { | |
9623 | const char *name; | |
9624 | Elf_Internal_Versym iver; | |
9625 | unsigned short version_index; | |
9626 | ||
9627 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9628 | || isym->st_shndx == SHN_UNDEF) | |
9629 | continue; | |
9630 | ||
9631 | name = bfd_elf_string_from_elf_section (input, | |
9632 | hdr->sh_link, | |
9633 | isym->st_name); | |
9634 | if (strcmp (name, h->root.root.string) != 0) | |
9635 | continue; | |
9636 | ||
9637 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9638 | ||
d023c380 L |
9639 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9640 | && !(h->def_regular | |
9641 | && h->forced_local)) | |
c152c796 AM |
9642 | { |
9643 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9644 | have provided a definition for the undefined sym unless |
9645 | it is defined in a non-shared object and forced local. | |
9646 | */ | |
c152c796 AM |
9647 | abort (); |
9648 | } | |
9649 | ||
9650 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9651 | if (version_index == 1 || version_index == 2) | |
9652 | { | |
9653 | /* This is the base or first version. We can use it. */ | |
9654 | free (extversym); | |
9655 | free (isymbuf); | |
9656 | return TRUE; | |
9657 | } | |
9658 | } | |
9659 | ||
9660 | free (extversym); | |
9661 | free (isymbuf); | |
9662 | } | |
9663 | ||
9664 | return FALSE; | |
9665 | } | |
9666 | ||
b8871f35 L |
9667 | /* Convert ELF common symbol TYPE. */ |
9668 | ||
9669 | static int | |
9670 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9671 | { | |
9672 | /* Commom symbol can only appear in relocatable link. */ | |
9673 | if (!bfd_link_relocatable (info)) | |
9674 | abort (); | |
9675 | switch (info->elf_stt_common) | |
9676 | { | |
9677 | case unchanged: | |
9678 | break; | |
9679 | case elf_stt_common: | |
9680 | type = STT_COMMON; | |
9681 | break; | |
9682 | case no_elf_stt_common: | |
9683 | type = STT_OBJECT; | |
9684 | break; | |
9685 | } | |
9686 | return type; | |
9687 | } | |
9688 | ||
c152c796 AM |
9689 | /* Add an external symbol to the symbol table. This is called from |
9690 | the hash table traversal routine. When generating a shared object, | |
9691 | we go through the symbol table twice. The first time we output | |
9692 | anything that might have been forced to local scope in a version | |
9693 | script. The second time we output the symbols that are still | |
9694 | global symbols. */ | |
9695 | ||
9696 | static bfd_boolean | |
7686d77d | 9697 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9698 | { |
7686d77d | 9699 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9700 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9701 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9702 | bfd_boolean strip; |
9703 | Elf_Internal_Sym sym; | |
9704 | asection *input_sec; | |
9705 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9706 | long indx; |
9707 | int ret; | |
b8871f35 | 9708 | unsigned int type; |
c152c796 AM |
9709 | |
9710 | if (h->root.type == bfd_link_hash_warning) | |
9711 | { | |
9712 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9713 | if (h->root.type == bfd_link_hash_new) | |
9714 | return TRUE; | |
9715 | } | |
9716 | ||
9717 | /* Decide whether to output this symbol in this pass. */ | |
9718 | if (eoinfo->localsyms) | |
9719 | { | |
4deb8f71 | 9720 | if (!h->forced_local) |
c152c796 AM |
9721 | return TRUE; |
9722 | } | |
9723 | else | |
9724 | { | |
4deb8f71 | 9725 | if (h->forced_local) |
c152c796 AM |
9726 | return TRUE; |
9727 | } | |
9728 | ||
8b127cbc | 9729 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9730 | |
12ac1cf5 | 9731 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9732 | { |
12ac1cf5 NC |
9733 | /* If we have an undefined symbol reference here then it must have |
9734 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9735 | references in regular files have already been handled unless |
9736 | they are in unreferenced sections which are removed by garbage | |
9737 | collection). */ | |
12ac1cf5 NC |
9738 | bfd_boolean ignore_undef = FALSE; |
9739 | ||
9740 | /* Some symbols may be special in that the fact that they're | |
9741 | undefined can be safely ignored - let backend determine that. */ | |
9742 | if (bed->elf_backend_ignore_undef_symbol) | |
9743 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9744 | ||
9745 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9746 | if (!ignore_undef |
12ac1cf5 | 9747 | && h->ref_dynamic |
8b127cbc AM |
9748 | && (!h->ref_regular || flinfo->info->gc_sections) |
9749 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9750 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9751 | (*flinfo->info->callbacks->undefined_symbol) |
9752 | (flinfo->info, h->root.root.string, | |
9753 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9754 | NULL, 0, | |
9755 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9756 | |
9757 | /* Strip a global symbol defined in a discarded section. */ | |
9758 | if (h->indx == -3) | |
9759 | return TRUE; | |
c152c796 AM |
9760 | } |
9761 | ||
9762 | /* We should also warn if a forced local symbol is referenced from | |
9763 | shared libraries. */ | |
0e1862bb | 9764 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9765 | && h->forced_local |
9766 | && h->ref_dynamic | |
371a5866 | 9767 | && h->def_regular |
f5385ebf | 9768 | && !h->dynamic_def |
ee659f1f | 9769 | && h->ref_dynamic_nonweak |
8b127cbc | 9770 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9771 | { |
17d078c5 AM |
9772 | bfd *def_bfd; |
9773 | const char *msg; | |
90c984fc L |
9774 | struct elf_link_hash_entry *hi = h; |
9775 | ||
9776 | /* Check indirect symbol. */ | |
9777 | while (hi->root.type == bfd_link_hash_indirect) | |
9778 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9779 | |
9780 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 9781 | /* xgettext:c-format */ |
871b3ab2 | 9782 | msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 9783 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) |
695344c0 | 9784 | /* xgettext:c-format */ |
871b3ab2 | 9785 | msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 9786 | else |
695344c0 | 9787 | /* xgettext:c-format */ |
871b3ab2 | 9788 | msg = _("%pB: local symbol `%s' in %pB is referenced by DSO"); |
8b127cbc | 9789 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9790 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9791 | def_bfd = hi->root.u.def.section->owner; | |
c08bb8dd AM |
9792 | _bfd_error_handler (msg, flinfo->output_bfd, |
9793 | h->root.root.string, def_bfd); | |
17d078c5 | 9794 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9795 | eoinfo->failed = TRUE; |
9796 | return FALSE; | |
9797 | } | |
9798 | ||
9799 | /* We don't want to output symbols that have never been mentioned by | |
9800 | a regular file, or that we have been told to strip. However, if | |
9801 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9802 | output it. */ | |
d983c8c5 | 9803 | strip = FALSE; |
c152c796 | 9804 | if (h->indx == -2) |
d983c8c5 | 9805 | ; |
f5385ebf | 9806 | else if ((h->def_dynamic |
77cfaee6 AM |
9807 | || h->ref_dynamic |
9808 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9809 | && !h->def_regular |
9810 | && !h->ref_regular) | |
c152c796 | 9811 | strip = TRUE; |
8b127cbc | 9812 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9813 | strip = TRUE; |
8b127cbc AM |
9814 | else if (flinfo->info->strip == strip_some |
9815 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9816 | h->root.root.string, FALSE, FALSE) == NULL) |
9817 | strip = TRUE; | |
d56d55e7 AM |
9818 | else if ((h->root.type == bfd_link_hash_defined |
9819 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9820 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9821 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9822 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9823 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9824 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9825 | strip = TRUE; |
9e2278f5 AM |
9826 | else if ((h->root.type == bfd_link_hash_undefined |
9827 | || h->root.type == bfd_link_hash_undefweak) | |
9828 | && h->root.u.undef.abfd != NULL | |
9829 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9830 | strip = TRUE; | |
c152c796 | 9831 | |
b8871f35 L |
9832 | type = h->type; |
9833 | ||
c152c796 | 9834 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9835 | nothing else to do. However, if it is a forced local symbol or |
9836 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9837 | function a chance to make it dynamic. */ | |
c152c796 AM |
9838 | if (strip |
9839 | && h->dynindx == -1 | |
b8871f35 | 9840 | && type != STT_GNU_IFUNC |
f5385ebf | 9841 | && !h->forced_local) |
c152c796 AM |
9842 | return TRUE; |
9843 | ||
9844 | sym.st_value = 0; | |
9845 | sym.st_size = h->size; | |
9846 | sym.st_other = h->other; | |
c152c796 AM |
9847 | switch (h->root.type) |
9848 | { | |
9849 | default: | |
9850 | case bfd_link_hash_new: | |
9851 | case bfd_link_hash_warning: | |
9852 | abort (); | |
9853 | return FALSE; | |
9854 | ||
9855 | case bfd_link_hash_undefined: | |
9856 | case bfd_link_hash_undefweak: | |
9857 | input_sec = bfd_und_section_ptr; | |
9858 | sym.st_shndx = SHN_UNDEF; | |
9859 | break; | |
9860 | ||
9861 | case bfd_link_hash_defined: | |
9862 | case bfd_link_hash_defweak: | |
9863 | { | |
9864 | input_sec = h->root.u.def.section; | |
9865 | if (input_sec->output_section != NULL) | |
9866 | { | |
9867 | sym.st_shndx = | |
8b127cbc | 9868 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9869 | input_sec->output_section); |
9870 | if (sym.st_shndx == SHN_BAD) | |
9871 | { | |
4eca0228 | 9872 | _bfd_error_handler |
695344c0 | 9873 | /* xgettext:c-format */ |
871b3ab2 | 9874 | (_("%pB: could not find output section %pA for input section %pA"), |
8b127cbc | 9875 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9876 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9877 | eoinfo->failed = TRUE; |
9878 | return FALSE; | |
9879 | } | |
9880 | ||
9881 | /* ELF symbols in relocatable files are section relative, | |
9882 | but in nonrelocatable files they are virtual | |
9883 | addresses. */ | |
9884 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9885 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9886 | { |
9887 | sym.st_value += input_sec->output_section->vma; | |
9888 | if (h->type == STT_TLS) | |
9889 | { | |
8b127cbc | 9890 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9891 | if (tls_sec != NULL) |
9892 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9893 | } |
9894 | } | |
9895 | } | |
9896 | else | |
9897 | { | |
9898 | BFD_ASSERT (input_sec->owner == NULL | |
9899 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9900 | sym.st_shndx = SHN_UNDEF; | |
9901 | input_sec = bfd_und_section_ptr; | |
9902 | } | |
9903 | } | |
9904 | break; | |
9905 | ||
9906 | case bfd_link_hash_common: | |
9907 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9908 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9909 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9910 | break; | |
9911 | ||
9912 | case bfd_link_hash_indirect: | |
9913 | /* These symbols are created by symbol versioning. They point | |
9914 | to the decorated version of the name. For example, if the | |
9915 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9916 | foo is used with no version, then we add an indirect symbol | |
9917 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9918 | since the indirected symbol is already in the hash table. */ | |
9919 | return TRUE; | |
9920 | } | |
9921 | ||
b8871f35 L |
9922 | if (type == STT_COMMON || type == STT_OBJECT) |
9923 | switch (h->root.type) | |
9924 | { | |
9925 | case bfd_link_hash_common: | |
9926 | type = elf_link_convert_common_type (flinfo->info, type); | |
9927 | break; | |
9928 | case bfd_link_hash_defined: | |
9929 | case bfd_link_hash_defweak: | |
9930 | if (bed->common_definition (&sym)) | |
9931 | type = elf_link_convert_common_type (flinfo->info, type); | |
9932 | else | |
9933 | type = STT_OBJECT; | |
9934 | break; | |
9935 | case bfd_link_hash_undefined: | |
9936 | case bfd_link_hash_undefweak: | |
9937 | break; | |
9938 | default: | |
9939 | abort (); | |
9940 | } | |
9941 | ||
4deb8f71 | 9942 | if (h->forced_local) |
b8871f35 L |
9943 | { |
9944 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9945 | /* Turn off visibility on local symbol. */ | |
9946 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9947 | } | |
9948 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9949 | else if (h->unique_global && h->def_regular) | |
9950 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
9951 | else if (h->root.type == bfd_link_hash_undefweak | |
9952 | || h->root.type == bfd_link_hash_defweak) | |
9953 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
9954 | else | |
9955 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
9956 | sym.st_target_internal = h->target_internal; | |
9957 | ||
c152c796 AM |
9958 | /* Give the processor backend a chance to tweak the symbol value, |
9959 | and also to finish up anything that needs to be done for this | |
9960 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 9961 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 9962 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 9963 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 9964 | && h->def_regular |
0e1862bb | 9965 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
9966 | || ((h->dynindx != -1 |
9967 | || h->forced_local) | |
0e1862bb | 9968 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
9969 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
9970 | || h->root.type != bfd_link_hash_undefweak)) | |
9971 | || !h->forced_local) | |
8b127cbc | 9972 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
9973 | { |
9974 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 9975 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
9976 | { |
9977 | eoinfo->failed = TRUE; | |
9978 | return FALSE; | |
9979 | } | |
9980 | } | |
9981 | ||
9982 | /* If we are marking the symbol as undefined, and there are no | |
9983 | non-weak references to this symbol from a regular object, then | |
9984 | mark the symbol as weak undefined; if there are non-weak | |
9985 | references, mark the symbol as strong. We can't do this earlier, | |
9986 | because it might not be marked as undefined until the | |
9987 | finish_dynamic_symbol routine gets through with it. */ | |
9988 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 9989 | && h->ref_regular |
c152c796 AM |
9990 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
9991 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
9992 | { | |
9993 | int bindtype; | |
b8871f35 | 9994 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
9995 | |
9996 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
9997 | if (type == STT_GNU_IFUNC) | |
9998 | type = STT_FUNC; | |
c152c796 | 9999 | |
f5385ebf | 10000 | if (h->ref_regular_nonweak) |
c152c796 AM |
10001 | bindtype = STB_GLOBAL; |
10002 | else | |
10003 | bindtype = STB_WEAK; | |
2955ec4c | 10004 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
10005 | } |
10006 | ||
bda987c2 CD |
10007 | /* If this is a symbol defined in a dynamic library, don't use the |
10008 | symbol size from the dynamic library. Relinking an executable | |
10009 | against a new library may introduce gratuitous changes in the | |
10010 | executable's symbols if we keep the size. */ | |
10011 | if (sym.st_shndx == SHN_UNDEF | |
10012 | && !h->def_regular | |
10013 | && h->def_dynamic) | |
10014 | sym.st_size = 0; | |
10015 | ||
c152c796 AM |
10016 | /* If a non-weak symbol with non-default visibility is not defined |
10017 | locally, it is a fatal error. */ | |
0e1862bb | 10018 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
10019 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
10020 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
10021 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 10022 | && !h->def_regular) |
c152c796 | 10023 | { |
17d078c5 AM |
10024 | const char *msg; |
10025 | ||
10026 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 10027 | /* xgettext:c-format */ |
871b3ab2 | 10028 | msg = _("%pB: protected symbol `%s' isn't defined"); |
17d078c5 | 10029 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) |
695344c0 | 10030 | /* xgettext:c-format */ |
871b3ab2 | 10031 | msg = _("%pB: internal symbol `%s' isn't defined"); |
17d078c5 | 10032 | else |
695344c0 | 10033 | /* xgettext:c-format */ |
871b3ab2 | 10034 | msg = _("%pB: hidden symbol `%s' isn't defined"); |
4eca0228 | 10035 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 10036 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
10037 | eoinfo->failed = TRUE; |
10038 | return FALSE; | |
10039 | } | |
10040 | ||
10041 | /* If this symbol should be put in the .dynsym section, then put it | |
10042 | there now. We already know the symbol index. We also fill in | |
10043 | the entry in the .hash section. */ | |
1c2649ed EB |
10044 | if (h->dynindx != -1 |
10045 | && elf_hash_table (flinfo->info)->dynamic_sections_created | |
10046 | && elf_hash_table (flinfo->info)->dynsym != NULL | |
10047 | && !discarded_section (elf_hash_table (flinfo->info)->dynsym)) | |
c152c796 | 10048 | { |
c152c796 AM |
10049 | bfd_byte *esym; |
10050 | ||
90c984fc L |
10051 | /* Since there is no version information in the dynamic string, |
10052 | if there is no version info in symbol version section, we will | |
1659f720 | 10053 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 10054 | by shared library, or not bound locally. */ |
1659f720 | 10055 | if (h->verinfo.verdef == NULL |
0e1862bb | 10056 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
10057 | || h->ref_dynamic |
10058 | || !h->def_regular)) | |
90c984fc L |
10059 | { |
10060 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
10061 | ||
10062 | if (p && p [1] != '\0') | |
10063 | { | |
4eca0228 | 10064 | _bfd_error_handler |
695344c0 | 10065 | /* xgettext:c-format */ |
9793eb77 | 10066 | (_("%pB: no symbol version section for versioned symbol `%s'"), |
90c984fc L |
10067 | flinfo->output_bfd, h->root.root.string); |
10068 | eoinfo->failed = TRUE; | |
10069 | return FALSE; | |
10070 | } | |
10071 | } | |
10072 | ||
c152c796 | 10073 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
10074 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
10075 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 10076 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
10077 | { |
10078 | eoinfo->failed = TRUE; | |
10079 | return FALSE; | |
10080 | } | |
8b127cbc | 10081 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 10082 | |
8b127cbc | 10083 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
10084 | { |
10085 | size_t hash_entry_size; | |
10086 | bfd_byte *bucketpos; | |
10087 | bfd_vma chain; | |
41198d0c L |
10088 | size_t bucketcount; |
10089 | size_t bucket; | |
10090 | ||
8b127cbc | 10091 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 10092 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
10093 | |
10094 | hash_entry_size | |
8b127cbc AM |
10095 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
10096 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 10097 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
10098 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
10099 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
10100 | bucketpos); | |
10101 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
10102 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
10103 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
10104 | } | |
c152c796 | 10105 | |
8b127cbc | 10106 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
10107 | { |
10108 | Elf_Internal_Versym iversym; | |
10109 | Elf_External_Versym *eversym; | |
10110 | ||
f5385ebf | 10111 | if (!h->def_regular) |
c152c796 | 10112 | { |
7b20f099 AM |
10113 | if (h->verinfo.verdef == NULL |
10114 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
10115 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
10116 | iversym.vs_vers = 0; |
10117 | else | |
10118 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
10119 | } | |
10120 | else | |
10121 | { | |
10122 | if (h->verinfo.vertree == NULL) | |
10123 | iversym.vs_vers = 1; | |
10124 | else | |
10125 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 10126 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 10127 | iversym.vs_vers++; |
c152c796 AM |
10128 | } |
10129 | ||
422f1182 | 10130 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 10131 | defined locally. */ |
422f1182 | 10132 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
10133 | iversym.vs_vers |= VERSYM_HIDDEN; |
10134 | ||
8b127cbc | 10135 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 10136 | eversym += h->dynindx; |
8b127cbc | 10137 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
10138 | } |
10139 | } | |
10140 | ||
d983c8c5 AM |
10141 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
10142 | strip it from .symtab too. Obviously we can't do this for | |
10143 | relocatable output or when needed for --emit-relocs. */ | |
10144 | else if (input_sec == bfd_und_section_ptr | |
10145 | && h->indx != -2 | |
66cae560 NC |
10146 | /* PR 22319 Do not strip global undefined symbols marked as being needed. */ |
10147 | && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) | |
0e1862bb | 10148 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 | 10149 | return TRUE; |
66cae560 | 10150 | |
d983c8c5 AM |
10151 | /* Also strip others that we couldn't earlier due to dynamic symbol |
10152 | processing. */ | |
10153 | if (strip) | |
10154 | return TRUE; | |
10155 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
10156 | return TRUE; |
10157 | ||
2ec55de3 AM |
10158 | /* Output a FILE symbol so that following locals are not associated |
10159 | with the wrong input file. We need one for forced local symbols | |
10160 | if we've seen more than one FILE symbol or when we have exactly | |
10161 | one FILE symbol but global symbols are present in a file other | |
10162 | than the one with the FILE symbol. We also need one if linker | |
10163 | defined symbols are present. In practice these conditions are | |
10164 | always met, so just emit the FILE symbol unconditionally. */ | |
10165 | if (eoinfo->localsyms | |
10166 | && !eoinfo->file_sym_done | |
10167 | && eoinfo->flinfo->filesym_count != 0) | |
10168 | { | |
10169 | Elf_Internal_Sym fsym; | |
10170 | ||
10171 | memset (&fsym, 0, sizeof (fsym)); | |
10172 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10173 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10174 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
10175 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
10176 | return FALSE; |
10177 | ||
10178 | eoinfo->file_sym_done = TRUE; | |
10179 | } | |
10180 | ||
8b127cbc | 10181 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
10182 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
10183 | input_sec, h); | |
6e0b88f1 | 10184 | if (ret == 0) |
c152c796 AM |
10185 | { |
10186 | eoinfo->failed = TRUE; | |
10187 | return FALSE; | |
10188 | } | |
6e0b88f1 AM |
10189 | else if (ret == 1) |
10190 | h->indx = indx; | |
10191 | else if (h->indx == -2) | |
10192 | abort(); | |
c152c796 AM |
10193 | |
10194 | return TRUE; | |
10195 | } | |
10196 | ||
cdd3575c AM |
10197 | /* Return TRUE if special handling is done for relocs in SEC against |
10198 | symbols defined in discarded sections. */ | |
10199 | ||
c152c796 AM |
10200 | static bfd_boolean |
10201 | elf_section_ignore_discarded_relocs (asection *sec) | |
10202 | { | |
10203 | const struct elf_backend_data *bed; | |
10204 | ||
cdd3575c AM |
10205 | switch (sec->sec_info_type) |
10206 | { | |
dbaa2011 AM |
10207 | case SEC_INFO_TYPE_STABS: |
10208 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 10209 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
10210 | return TRUE; |
10211 | default: | |
10212 | break; | |
10213 | } | |
c152c796 AM |
10214 | |
10215 | bed = get_elf_backend_data (sec->owner); | |
10216 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
10217 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
10218 | return TRUE; | |
10219 | ||
10220 | return FALSE; | |
10221 | } | |
10222 | ||
9e66c942 AM |
10223 | /* Return a mask saying how ld should treat relocations in SEC against |
10224 | symbols defined in discarded sections. If this function returns | |
10225 | COMPLAIN set, ld will issue a warning message. If this function | |
10226 | returns PRETEND set, and the discarded section was link-once and the | |
10227 | same size as the kept link-once section, ld will pretend that the | |
10228 | symbol was actually defined in the kept section. Otherwise ld will | |
10229 | zero the reloc (at least that is the intent, but some cooperation by | |
10230 | the target dependent code is needed, particularly for REL targets). */ | |
10231 | ||
8a696751 AM |
10232 | unsigned int |
10233 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 10234 | { |
9e66c942 | 10235 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 10236 | return PRETEND; |
cdd3575c AM |
10237 | |
10238 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 10239 | return 0; |
cdd3575c AM |
10240 | |
10241 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 10242 | return 0; |
cdd3575c | 10243 | |
9e66c942 | 10244 | return COMPLAIN | PRETEND; |
cdd3575c AM |
10245 | } |
10246 | ||
3d7f7666 L |
10247 | /* Find a match between a section and a member of a section group. */ |
10248 | ||
10249 | static asection * | |
c0f00686 L |
10250 | match_group_member (asection *sec, asection *group, |
10251 | struct bfd_link_info *info) | |
3d7f7666 L |
10252 | { |
10253 | asection *first = elf_next_in_group (group); | |
10254 | asection *s = first; | |
10255 | ||
10256 | while (s != NULL) | |
10257 | { | |
c0f00686 | 10258 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
10259 | return s; |
10260 | ||
83180ade | 10261 | s = elf_next_in_group (s); |
3d7f7666 L |
10262 | if (s == first) |
10263 | break; | |
10264 | } | |
10265 | ||
10266 | return NULL; | |
10267 | } | |
10268 | ||
01b3c8ab | 10269 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
10270 | to replace it. Return the replacement if it is OK. Otherwise return |
10271 | NULL. */ | |
01b3c8ab L |
10272 | |
10273 | asection * | |
c0f00686 | 10274 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
10275 | { |
10276 | asection *kept; | |
10277 | ||
10278 | kept = sec->kept_section; | |
10279 | if (kept != NULL) | |
10280 | { | |
c2370991 | 10281 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 10282 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
10283 | if (kept != NULL |
10284 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
10285 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 10286 | kept = NULL; |
c2370991 | 10287 | sec->kept_section = kept; |
01b3c8ab L |
10288 | } |
10289 | return kept; | |
10290 | } | |
10291 | ||
c152c796 AM |
10292 | /* Link an input file into the linker output file. This function |
10293 | handles all the sections and relocations of the input file at once. | |
10294 | This is so that we only have to read the local symbols once, and | |
10295 | don't have to keep them in memory. */ | |
10296 | ||
10297 | static bfd_boolean | |
8b127cbc | 10298 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 10299 | { |
ece5ef60 | 10300 | int (*relocate_section) |
c152c796 AM |
10301 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
10302 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
10303 | bfd *output_bfd; | |
10304 | Elf_Internal_Shdr *symtab_hdr; | |
10305 | size_t locsymcount; | |
10306 | size_t extsymoff; | |
10307 | Elf_Internal_Sym *isymbuf; | |
10308 | Elf_Internal_Sym *isym; | |
10309 | Elf_Internal_Sym *isymend; | |
10310 | long *pindex; | |
10311 | asection **ppsection; | |
10312 | asection *o; | |
10313 | const struct elf_backend_data *bed; | |
c152c796 | 10314 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
10315 | bfd_size_type address_size; |
10316 | bfd_vma r_type_mask; | |
10317 | int r_sym_shift; | |
ffbc01cc | 10318 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 10319 | |
8b127cbc | 10320 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
10321 | bed = get_elf_backend_data (output_bfd); |
10322 | relocate_section = bed->elf_backend_relocate_section; | |
10323 | ||
10324 | /* If this is a dynamic object, we don't want to do anything here: | |
10325 | we don't want the local symbols, and we don't want the section | |
10326 | contents. */ | |
10327 | if ((input_bfd->flags & DYNAMIC) != 0) | |
10328 | return TRUE; | |
10329 | ||
c152c796 AM |
10330 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
10331 | if (elf_bad_symtab (input_bfd)) | |
10332 | { | |
10333 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10334 | extsymoff = 0; | |
10335 | } | |
10336 | else | |
10337 | { | |
10338 | locsymcount = symtab_hdr->sh_info; | |
10339 | extsymoff = symtab_hdr->sh_info; | |
10340 | } | |
10341 | ||
10342 | /* Read the local symbols. */ | |
10343 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10344 | if (isymbuf == NULL && locsymcount != 0) | |
10345 | { | |
10346 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
10347 | flinfo->internal_syms, |
10348 | flinfo->external_syms, | |
10349 | flinfo->locsym_shndx); | |
c152c796 AM |
10350 | if (isymbuf == NULL) |
10351 | return FALSE; | |
10352 | } | |
10353 | ||
10354 | /* Find local symbol sections and adjust values of symbols in | |
10355 | SEC_MERGE sections. Write out those local symbols we know are | |
10356 | going into the output file. */ | |
10357 | isymend = isymbuf + locsymcount; | |
8b127cbc | 10358 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
10359 | isym < isymend; |
10360 | isym++, pindex++, ppsection++) | |
10361 | { | |
10362 | asection *isec; | |
10363 | const char *name; | |
10364 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
10365 | long indx; |
10366 | int ret; | |
c152c796 AM |
10367 | |
10368 | *pindex = -1; | |
10369 | ||
10370 | if (elf_bad_symtab (input_bfd)) | |
10371 | { | |
10372 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
10373 | { | |
10374 | *ppsection = NULL; | |
10375 | continue; | |
10376 | } | |
10377 | } | |
10378 | ||
10379 | if (isym->st_shndx == SHN_UNDEF) | |
10380 | isec = bfd_und_section_ptr; | |
c152c796 AM |
10381 | else if (isym->st_shndx == SHN_ABS) |
10382 | isec = bfd_abs_section_ptr; | |
10383 | else if (isym->st_shndx == SHN_COMMON) | |
10384 | isec = bfd_com_section_ptr; | |
10385 | else | |
10386 | { | |
cb33740c AM |
10387 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
10388 | if (isec == NULL) | |
10389 | { | |
10390 | /* Don't attempt to output symbols with st_shnx in the | |
10391 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
10392 | *ppsection = NULL; | |
10393 | continue; | |
10394 | } | |
dbaa2011 | 10395 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
10396 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
10397 | isym->st_value = | |
10398 | _bfd_merged_section_offset (output_bfd, &isec, | |
10399 | elf_section_data (isec)->sec_info, | |
10400 | isym->st_value); | |
c152c796 AM |
10401 | } |
10402 | ||
10403 | *ppsection = isec; | |
10404 | ||
d983c8c5 AM |
10405 | /* Don't output the first, undefined, symbol. In fact, don't |
10406 | output any undefined local symbol. */ | |
10407 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
10408 | continue; |
10409 | ||
10410 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
10411 | { | |
10412 | /* We never output section symbols. Instead, we use the | |
10413 | section symbol of the corresponding section in the output | |
10414 | file. */ | |
10415 | continue; | |
10416 | } | |
10417 | ||
10418 | /* If we are stripping all symbols, we don't want to output this | |
10419 | one. */ | |
8b127cbc | 10420 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10421 | continue; |
10422 | ||
10423 | /* If we are discarding all local symbols, we don't want to | |
10424 | output this one. If we are generating a relocatable output | |
10425 | file, then some of the local symbols may be required by | |
10426 | relocs; we output them below as we discover that they are | |
10427 | needed. */ | |
8b127cbc | 10428 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
10429 | continue; |
10430 | ||
10431 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
10432 | discarding, we don't need to keep it. */ |
10433 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
10434 | && isym->st_shndx < SHN_LORESERVE |
10435 | && bfd_section_removed_from_list (output_bfd, | |
10436 | isec->output_section)) | |
e75a280b L |
10437 | continue; |
10438 | ||
c152c796 AM |
10439 | /* Get the name of the symbol. */ |
10440 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
10441 | isym->st_name); | |
10442 | if (name == NULL) | |
10443 | return FALSE; | |
10444 | ||
10445 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
10446 | if ((flinfo->info->strip == strip_some |
10447 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 10448 | == NULL)) |
8b127cbc | 10449 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10450 | && (isec->flags & SEC_MERGE) |
10451 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10452 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10453 | && bfd_is_local_label_name (input_bfd, name))) |
10454 | continue; | |
10455 | ||
ffbc01cc AM |
10456 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10457 | { | |
ce875075 AM |
10458 | if (input_bfd->lto_output) |
10459 | /* -flto puts a temp file name here. This means builds | |
10460 | are not reproducible. Discard the symbol. */ | |
10461 | continue; | |
ffbc01cc AM |
10462 | have_file_sym = TRUE; |
10463 | flinfo->filesym_count += 1; | |
10464 | } | |
10465 | if (!have_file_sym) | |
10466 | { | |
10467 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10468 | FILE symbols to determine the source file for local | |
10469 | function symbols. Provide a FILE symbol here if input | |
10470 | files lack such, so that their symbols won't be | |
10471 | associated with a previous input file. It's not the | |
10472 | source file, but the best we can do. */ | |
10473 | have_file_sym = TRUE; | |
10474 | flinfo->filesym_count += 1; | |
10475 | memset (&osym, 0, sizeof (osym)); | |
10476 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10477 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10478 | if (!elf_link_output_symstrtab (flinfo, |
10479 | (input_bfd->lto_output ? NULL | |
10480 | : input_bfd->filename), | |
10481 | &osym, bfd_abs_section_ptr, | |
10482 | NULL)) | |
ffbc01cc AM |
10483 | return FALSE; |
10484 | } | |
10485 | ||
c152c796 AM |
10486 | osym = *isym; |
10487 | ||
10488 | /* Adjust the section index for the output file. */ | |
10489 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10490 | isec->output_section); | |
10491 | if (osym.st_shndx == SHN_BAD) | |
10492 | return FALSE; | |
10493 | ||
c152c796 AM |
10494 | /* ELF symbols in relocatable files are section relative, but |
10495 | in executable files they are virtual addresses. Note that | |
10496 | this code assumes that all ELF sections have an associated | |
10497 | BFD section with a reasonable value for output_offset; below | |
10498 | we assume that they also have a reasonable value for | |
10499 | output_section. Any special sections must be set up to meet | |
10500 | these requirements. */ | |
10501 | osym.st_value += isec->output_offset; | |
0e1862bb | 10502 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10503 | { |
10504 | osym.st_value += isec->output_section->vma; | |
10505 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10506 | { | |
10507 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
102def4d AM |
10508 | if (elf_hash_table (flinfo->info)->tls_sec != NULL) |
10509 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
10510 | else | |
10511 | osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info), | |
10512 | STT_NOTYPE); | |
c152c796 AM |
10513 | } |
10514 | } | |
10515 | ||
6e0b88f1 | 10516 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10517 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10518 | if (ret == 0) |
c152c796 | 10519 | return FALSE; |
6e0b88f1 AM |
10520 | else if (ret == 1) |
10521 | *pindex = indx; | |
c152c796 AM |
10522 | } |
10523 | ||
310fd250 L |
10524 | if (bed->s->arch_size == 32) |
10525 | { | |
10526 | r_type_mask = 0xff; | |
10527 | r_sym_shift = 8; | |
10528 | address_size = 4; | |
10529 | } | |
10530 | else | |
10531 | { | |
10532 | r_type_mask = 0xffffffff; | |
10533 | r_sym_shift = 32; | |
10534 | address_size = 8; | |
10535 | } | |
10536 | ||
c152c796 AM |
10537 | /* Relocate the contents of each section. */ |
10538 | sym_hashes = elf_sym_hashes (input_bfd); | |
10539 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10540 | { | |
10541 | bfd_byte *contents; | |
10542 | ||
10543 | if (! o->linker_mark) | |
10544 | { | |
10545 | /* This section was omitted from the link. */ | |
10546 | continue; | |
10547 | } | |
10548 | ||
7bdf4127 | 10549 | if (!flinfo->info->resolve_section_groups |
bcacc0f5 AM |
10550 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10551 | { | |
10552 | /* Deal with the group signature symbol. */ | |
10553 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10554 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10555 | asection *osec = o->output_section; | |
10556 | ||
7bdf4127 | 10557 | BFD_ASSERT (bfd_link_relocatable (flinfo->info)); |
bcacc0f5 AM |
10558 | if (symndx >= locsymcount |
10559 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10560 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10561 | { |
10562 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10563 | while (h->root.type == bfd_link_hash_indirect | |
10564 | || h->root.type == bfd_link_hash_warning) | |
10565 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10566 | /* Arrange for symbol to be output. */ | |
10567 | h->indx = -2; | |
10568 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10569 | } | |
10570 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10571 | { | |
10572 | /* We'll use the output section target_index. */ | |
8b127cbc | 10573 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10574 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10575 | } | |
10576 | else | |
10577 | { | |
8b127cbc | 10578 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10579 | { |
10580 | /* Otherwise output the local symbol now. */ | |
10581 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10582 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10583 | const char *name; |
6e0b88f1 AM |
10584 | long indx; |
10585 | int ret; | |
bcacc0f5 AM |
10586 | |
10587 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10588 | symtab_hdr->sh_link, | |
10589 | sym.st_name); | |
10590 | if (name == NULL) | |
10591 | return FALSE; | |
10592 | ||
10593 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10594 | sec); | |
10595 | if (sym.st_shndx == SHN_BAD) | |
10596 | return FALSE; | |
10597 | ||
10598 | sym.st_value += o->output_offset; | |
10599 | ||
6e0b88f1 | 10600 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10601 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10602 | NULL); | |
6e0b88f1 | 10603 | if (ret == 0) |
bcacc0f5 | 10604 | return FALSE; |
6e0b88f1 | 10605 | else if (ret == 1) |
8b127cbc | 10606 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10607 | else |
10608 | abort (); | |
bcacc0f5 AM |
10609 | } |
10610 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10611 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10612 | } |
10613 | } | |
10614 | ||
c152c796 | 10615 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10616 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10617 | continue; |
10618 | ||
10619 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10620 | { | |
10621 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10622 | or somesuch. */ | |
10623 | continue; | |
10624 | } | |
10625 | ||
10626 | /* Get the contents of the section. They have been cached by a | |
10627 | relaxation routine. Note that o is a section in an input | |
10628 | file, so the contents field will not have been set by any of | |
10629 | the routines which work on output files. */ | |
10630 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10631 | { |
10632 | contents = elf_section_data (o)->this_hdr.contents; | |
10633 | if (bed->caches_rawsize | |
10634 | && o->rawsize != 0 | |
10635 | && o->rawsize < o->size) | |
10636 | { | |
10637 | memcpy (flinfo->contents, contents, o->rawsize); | |
10638 | contents = flinfo->contents; | |
10639 | } | |
10640 | } | |
c152c796 AM |
10641 | else |
10642 | { | |
8b127cbc | 10643 | contents = flinfo->contents; |
4a114e3e | 10644 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10645 | return FALSE; |
10646 | } | |
10647 | ||
10648 | if ((o->flags & SEC_RELOC) != 0) | |
10649 | { | |
10650 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10651 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10652 | int action_discarded; |
ece5ef60 | 10653 | int ret; |
c152c796 AM |
10654 | |
10655 | /* Get the swapped relocs. */ | |
10656 | internal_relocs | |
8b127cbc AM |
10657 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10658 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10659 | if (internal_relocs == NULL |
10660 | && o->reloc_count > 0) | |
10661 | return FALSE; | |
10662 | ||
310fd250 L |
10663 | /* We need to reverse-copy input .ctors/.dtors sections if |
10664 | they are placed in .init_array/.finit_array for output. */ | |
10665 | if (o->size > address_size | |
10666 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10667 | && strcmp (o->output_section->name, | |
10668 | ".init_array") == 0) | |
10669 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10670 | && strcmp (o->output_section->name, | |
10671 | ".fini_array") == 0)) | |
10672 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10673 | { |
056bafd4 MR |
10674 | if (o->size * bed->s->int_rels_per_ext_rel |
10675 | != o->reloc_count * address_size) | |
310fd250 | 10676 | { |
4eca0228 | 10677 | _bfd_error_handler |
695344c0 | 10678 | /* xgettext:c-format */ |
871b3ab2 | 10679 | (_("error: %pB: size of section %pA is not " |
310fd250 L |
10680 | "multiple of address size"), |
10681 | input_bfd, o); | |
8c6716e5 | 10682 | bfd_set_error (bfd_error_bad_value); |
310fd250 L |
10683 | return FALSE; |
10684 | } | |
10685 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10686 | } |
10687 | ||
0f02bbd9 | 10688 | action_discarded = -1; |
c152c796 | 10689 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10690 | action_discarded = (*bed->action_discarded) (o); |
10691 | ||
10692 | /* Run through the relocs evaluating complex reloc symbols and | |
10693 | looking for relocs against symbols from discarded sections | |
10694 | or section symbols from removed link-once sections. | |
10695 | Complain about relocs against discarded sections. Zero | |
10696 | relocs against removed link-once sections. */ | |
10697 | ||
10698 | rel = internal_relocs; | |
056bafd4 | 10699 | relend = rel + o->reloc_count; |
0f02bbd9 | 10700 | for ( ; rel < relend; rel++) |
c152c796 | 10701 | { |
0f02bbd9 AM |
10702 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10703 | unsigned int s_type; | |
10704 | asection **ps, *sec; | |
10705 | struct elf_link_hash_entry *h = NULL; | |
10706 | const char *sym_name; | |
c152c796 | 10707 | |
0f02bbd9 AM |
10708 | if (r_symndx == STN_UNDEF) |
10709 | continue; | |
c152c796 | 10710 | |
0f02bbd9 AM |
10711 | if (r_symndx >= locsymcount |
10712 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10713 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10714 | { |
10715 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10716 | |
0f02bbd9 AM |
10717 | /* Badly formatted input files can contain relocs that |
10718 | reference non-existant symbols. Check here so that | |
10719 | we do not seg fault. */ | |
10720 | if (h == NULL) | |
c152c796 | 10721 | { |
4eca0228 | 10722 | _bfd_error_handler |
695344c0 | 10723 | /* xgettext:c-format */ |
2dcf00ce | 10724 | (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA " |
0f02bbd9 | 10725 | "that references a non-existent global symbol"), |
2dcf00ce | 10726 | input_bfd, (uint64_t) rel->r_info, o); |
0f02bbd9 AM |
10727 | bfd_set_error (bfd_error_bad_value); |
10728 | return FALSE; | |
10729 | } | |
3b36f7e6 | 10730 | |
0f02bbd9 AM |
10731 | while (h->root.type == bfd_link_hash_indirect |
10732 | || h->root.type == bfd_link_hash_warning) | |
10733 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10734 | |
0f02bbd9 | 10735 | s_type = h->type; |
cdd3575c | 10736 | |
9e2dec47 | 10737 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10738 | mark the symbol as undefined. Note that the |
10739 | linker may attach linker created dynamic sections | |
10740 | to the plugin bfd. Symbols defined in linker | |
10741 | created sections are not plugin symbols. */ | |
bc4e12de | 10742 | if ((h->root.non_ir_ref_regular |
4070765b | 10743 | || h->root.non_ir_ref_dynamic) |
9e2dec47 L |
10744 | && (h->root.type == bfd_link_hash_defined |
10745 | || h->root.type == bfd_link_hash_defweak) | |
10746 | && (h->root.u.def.section->flags | |
10747 | & SEC_LINKER_CREATED) == 0 | |
10748 | && h->root.u.def.section->owner != NULL | |
10749 | && (h->root.u.def.section->owner->flags | |
10750 | & BFD_PLUGIN) != 0) | |
10751 | { | |
10752 | h->root.type = bfd_link_hash_undefined; | |
10753 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10754 | } | |
10755 | ||
0f02bbd9 AM |
10756 | ps = NULL; |
10757 | if (h->root.type == bfd_link_hash_defined | |
10758 | || h->root.type == bfd_link_hash_defweak) | |
10759 | ps = &h->root.u.def.section; | |
10760 | ||
10761 | sym_name = h->root.root.string; | |
10762 | } | |
10763 | else | |
10764 | { | |
10765 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10766 | ||
10767 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10768 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10769 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10770 | sym, *ps); | |
10771 | } | |
c152c796 | 10772 | |
c301e700 | 10773 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10774 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10775 | { |
10776 | bfd_vma val; | |
10777 | bfd_vma dot = (rel->r_offset | |
10778 | + o->output_offset + o->output_section->vma); | |
10779 | #ifdef DEBUG | |
10780 | printf ("Encountered a complex symbol!"); | |
10781 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10782 | input_bfd->filename, o->name, |
10783 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10784 | printf (" symbol: idx %8.8lx, name %s\n", |
10785 | r_symndx, sym_name); | |
10786 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10787 | (unsigned long) rel->r_info, | |
10788 | (unsigned long) rel->r_offset); | |
10789 | #endif | |
8b127cbc | 10790 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10791 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10792 | return FALSE; | |
10793 | ||
10794 | /* Symbol evaluated OK. Update to absolute value. */ | |
10795 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10796 | r_symndx, val); | |
10797 | continue; | |
10798 | } | |
10799 | ||
10800 | if (action_discarded != -1 && ps != NULL) | |
10801 | { | |
cdd3575c AM |
10802 | /* Complain if the definition comes from a |
10803 | discarded section. */ | |
dbaa2011 | 10804 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10805 | { |
cf35638d | 10806 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10807 | if (action_discarded & COMPLAIN) |
8b127cbc | 10808 | (*flinfo->info->callbacks->einfo) |
695344c0 | 10809 | /* xgettext:c-format */ |
871b3ab2 AM |
10810 | (_("%X`%s' referenced in section `%pA' of %pB: " |
10811 | "defined in discarded section `%pA' of %pB\n"), | |
e1fffbe6 | 10812 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10813 | |
87e5235d | 10814 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10815 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10816 | really defined in the kept linkonce section. |
10817 | FIXME: This is quite broken. Modifying the | |
10818 | symbol here means we will be changing all later | |
e0ae6d6f | 10819 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10820 | if (action_discarded & PRETEND) |
87e5235d | 10821 | { |
01b3c8ab L |
10822 | asection *kept; |
10823 | ||
c0f00686 | 10824 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10825 | flinfo->info); |
01b3c8ab | 10826 | if (kept != NULL) |
87e5235d AM |
10827 | { |
10828 | *ps = kept; | |
10829 | continue; | |
10830 | } | |
10831 | } | |
c152c796 AM |
10832 | } |
10833 | } | |
10834 | } | |
10835 | ||
10836 | /* Relocate the section by invoking a back end routine. | |
10837 | ||
10838 | The back end routine is responsible for adjusting the | |
10839 | section contents as necessary, and (if using Rela relocs | |
10840 | and generating a relocatable output file) adjusting the | |
10841 | reloc addend as necessary. | |
10842 | ||
10843 | The back end routine does not have to worry about setting | |
10844 | the reloc address or the reloc symbol index. | |
10845 | ||
10846 | The back end routine is given a pointer to the swapped in | |
10847 | internal symbols, and can access the hash table entries | |
10848 | for the external symbols via elf_sym_hashes (input_bfd). | |
10849 | ||
10850 | When generating relocatable output, the back end routine | |
10851 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10852 | output symbol is going to be a section symbol | |
10853 | corresponding to the output section, which will require | |
10854 | the addend to be adjusted. */ | |
10855 | ||
8b127cbc | 10856 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10857 | input_bfd, o, contents, |
10858 | internal_relocs, | |
10859 | isymbuf, | |
8b127cbc | 10860 | flinfo->sections); |
ece5ef60 | 10861 | if (!ret) |
c152c796 AM |
10862 | return FALSE; |
10863 | ||
ece5ef60 | 10864 | if (ret == 2 |
0e1862bb | 10865 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10866 | || flinfo->info->emitrelocations) |
c152c796 AM |
10867 | { |
10868 | Elf_Internal_Rela *irela; | |
d4730f92 | 10869 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10870 | bfd_vma last_offset; |
10871 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10872 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10873 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10874 | unsigned int next_erel; |
c152c796 | 10875 | bfd_boolean rela_normal; |
d4730f92 | 10876 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10877 | |
d4730f92 BS |
10878 | esdi = elf_section_data (o); |
10879 | esdo = elf_section_data (o->output_section); | |
10880 | rela_normal = FALSE; | |
c152c796 AM |
10881 | |
10882 | /* Adjust the reloc addresses and symbol indices. */ | |
10883 | ||
10884 | irela = internal_relocs; | |
056bafd4 | 10885 | irelaend = irela + o->reloc_count; |
d4730f92 BS |
10886 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10887 | /* We start processing the REL relocs, if any. When we reach | |
10888 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10889 | irelamid = irela; | |
10890 | if (esdi->rel.hdr != NULL) | |
10891 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10892 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10893 | rel_hash_list = rel_hash; |
d4730f92 | 10894 | rela_hash_list = NULL; |
c152c796 | 10895 | last_offset = o->output_offset; |
0e1862bb | 10896 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10897 | last_offset += o->output_section->vma; |
10898 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10899 | { | |
10900 | unsigned long r_symndx; | |
10901 | asection *sec; | |
10902 | Elf_Internal_Sym sym; | |
10903 | ||
10904 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10905 | { | |
10906 | rel_hash++; | |
10907 | next_erel = 0; | |
10908 | } | |
10909 | ||
d4730f92 BS |
10910 | if (irela == irelamid) |
10911 | { | |
10912 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10913 | rela_hash_list = rel_hash; | |
10914 | rela_normal = bed->rela_normal; | |
10915 | } | |
10916 | ||
c152c796 | 10917 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10918 | flinfo->info, o, |
c152c796 AM |
10919 | irela->r_offset); |
10920 | if (irela->r_offset >= (bfd_vma) -2) | |
10921 | { | |
10922 | /* This is a reloc for a deleted entry or somesuch. | |
10923 | Turn it into an R_*_NONE reloc, at the same | |
10924 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10925 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10926 | being ordered. */ |
10927 | irela->r_offset = last_offset; | |
10928 | irela->r_info = 0; | |
10929 | irela->r_addend = 0; | |
10930 | continue; | |
10931 | } | |
10932 | ||
10933 | irela->r_offset += o->output_offset; | |
10934 | ||
10935 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10936 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10937 | irela->r_offset += o->output_section->vma; |
10938 | ||
10939 | last_offset = irela->r_offset; | |
10940 | ||
10941 | r_symndx = irela->r_info >> r_sym_shift; | |
10942 | if (r_symndx == STN_UNDEF) | |
10943 | continue; | |
10944 | ||
10945 | if (r_symndx >= locsymcount | |
10946 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10947 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10948 | { |
10949 | struct elf_link_hash_entry *rh; | |
10950 | unsigned long indx; | |
10951 | ||
10952 | /* This is a reloc against a global symbol. We | |
10953 | have not yet output all the local symbols, so | |
10954 | we do not know the symbol index of any global | |
10955 | symbol. We set the rel_hash entry for this | |
10956 | reloc to point to the global hash table entry | |
10957 | for this symbol. The symbol index is then | |
ee75fd95 | 10958 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
10959 | indx = r_symndx - extsymoff; |
10960 | rh = elf_sym_hashes (input_bfd)[indx]; | |
10961 | while (rh->root.type == bfd_link_hash_indirect | |
10962 | || rh->root.type == bfd_link_hash_warning) | |
10963 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
10964 | ||
10965 | /* Setting the index to -2 tells | |
10966 | elf_link_output_extsym that this symbol is | |
10967 | used by a reloc. */ | |
10968 | BFD_ASSERT (rh->indx < 0); | |
10969 | rh->indx = -2; | |
c152c796 AM |
10970 | *rel_hash = rh; |
10971 | ||
10972 | continue; | |
10973 | } | |
10974 | ||
10975 | /* This is a reloc against a local symbol. */ | |
10976 | ||
10977 | *rel_hash = NULL; | |
10978 | sym = isymbuf[r_symndx]; | |
8b127cbc | 10979 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
10980 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
10981 | { | |
10982 | /* I suppose the backend ought to fill in the | |
10983 | section of any STT_SECTION symbol against a | |
6a8d1586 | 10984 | processor specific section. */ |
cf35638d | 10985 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
10986 | if (bfd_is_abs_section (sec)) |
10987 | ; | |
c152c796 AM |
10988 | else if (sec == NULL || sec->owner == NULL) |
10989 | { | |
10990 | bfd_set_error (bfd_error_bad_value); | |
10991 | return FALSE; | |
10992 | } | |
10993 | else | |
10994 | { | |
6a8d1586 AM |
10995 | asection *osec = sec->output_section; |
10996 | ||
10997 | /* If we have discarded a section, the output | |
10998 | section will be the absolute section. In | |
ab96bf03 AM |
10999 | case of discarded SEC_MERGE sections, use |
11000 | the kept section. relocate_section should | |
11001 | have already handled discarded linkonce | |
11002 | sections. */ | |
6a8d1586 AM |
11003 | if (bfd_is_abs_section (osec) |
11004 | && sec->kept_section != NULL | |
11005 | && sec->kept_section->output_section != NULL) | |
11006 | { | |
11007 | osec = sec->kept_section->output_section; | |
11008 | irela->r_addend -= osec->vma; | |
11009 | } | |
11010 | ||
11011 | if (!bfd_is_abs_section (osec)) | |
11012 | { | |
11013 | r_symndx = osec->target_index; | |
cf35638d | 11014 | if (r_symndx == STN_UNDEF) |
74541ad4 | 11015 | { |
051d833a AM |
11016 | irela->r_addend += osec->vma; |
11017 | osec = _bfd_nearby_section (output_bfd, osec, | |
11018 | osec->vma); | |
11019 | irela->r_addend -= osec->vma; | |
11020 | r_symndx = osec->target_index; | |
74541ad4 | 11021 | } |
6a8d1586 | 11022 | } |
c152c796 AM |
11023 | } |
11024 | ||
11025 | /* Adjust the addend according to where the | |
11026 | section winds up in the output section. */ | |
11027 | if (rela_normal) | |
11028 | irela->r_addend += sec->output_offset; | |
11029 | } | |
11030 | else | |
11031 | { | |
8b127cbc | 11032 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
11033 | { |
11034 | unsigned long shlink; | |
11035 | const char *name; | |
11036 | asection *osec; | |
6e0b88f1 | 11037 | long indx; |
c152c796 | 11038 | |
8b127cbc | 11039 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
11040 | { |
11041 | /* You can't do ld -r -s. */ | |
11042 | bfd_set_error (bfd_error_invalid_operation); | |
11043 | return FALSE; | |
11044 | } | |
11045 | ||
11046 | /* This symbol was skipped earlier, but | |
11047 | since it is needed by a reloc, we | |
11048 | must output it now. */ | |
11049 | shlink = symtab_hdr->sh_link; | |
11050 | name = (bfd_elf_string_from_elf_section | |
11051 | (input_bfd, shlink, sym.st_name)); | |
11052 | if (name == NULL) | |
11053 | return FALSE; | |
11054 | ||
11055 | osec = sec->output_section; | |
11056 | sym.st_shndx = | |
11057 | _bfd_elf_section_from_bfd_section (output_bfd, | |
11058 | osec); | |
11059 | if (sym.st_shndx == SHN_BAD) | |
11060 | return FALSE; | |
11061 | ||
11062 | sym.st_value += sec->output_offset; | |
0e1862bb | 11063 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11064 | { |
11065 | sym.st_value += osec->vma; | |
11066 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
11067 | { | |
102def4d AM |
11068 | struct elf_link_hash_table *htab |
11069 | = elf_hash_table (flinfo->info); | |
11070 | ||
c152c796 AM |
11071 | /* STT_TLS symbols are relative to PT_TLS |
11072 | segment base. */ | |
102def4d AM |
11073 | if (htab->tls_sec != NULL) |
11074 | sym.st_value -= htab->tls_sec->vma; | |
11075 | else | |
11076 | sym.st_info | |
11077 | = ELF_ST_INFO (ELF_ST_BIND (sym.st_info), | |
11078 | STT_NOTYPE); | |
c152c796 AM |
11079 | } |
11080 | } | |
11081 | ||
6e0b88f1 | 11082 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
11083 | ret = elf_link_output_symstrtab (flinfo, name, |
11084 | &sym, sec, | |
11085 | NULL); | |
6e0b88f1 | 11086 | if (ret == 0) |
c152c796 | 11087 | return FALSE; |
6e0b88f1 | 11088 | else if (ret == 1) |
8b127cbc | 11089 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
11090 | else |
11091 | abort (); | |
c152c796 AM |
11092 | } |
11093 | ||
8b127cbc | 11094 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
11095 | } |
11096 | ||
11097 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
11098 | | (irela->r_info & r_type_mask)); | |
11099 | } | |
11100 | ||
11101 | /* Swap out the relocs. */ | |
d4730f92 BS |
11102 | input_rel_hdr = esdi->rel.hdr; |
11103 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 11104 | { |
d4730f92 BS |
11105 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
11106 | input_rel_hdr, | |
11107 | internal_relocs, | |
11108 | rel_hash_list)) | |
11109 | return FALSE; | |
c152c796 AM |
11110 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
11111 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 11112 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
11113 | } |
11114 | ||
11115 | input_rela_hdr = esdi->rela.hdr; | |
11116 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
11117 | { | |
eac338cf | 11118 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 11119 | input_rela_hdr, |
eac338cf | 11120 | internal_relocs, |
d4730f92 | 11121 | rela_hash_list)) |
c152c796 AM |
11122 | return FALSE; |
11123 | } | |
11124 | } | |
11125 | } | |
11126 | ||
11127 | /* Write out the modified section contents. */ | |
11128 | if (bed->elf_backend_write_section | |
8b127cbc | 11129 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 11130 | contents)) |
c152c796 AM |
11131 | { |
11132 | /* Section written out. */ | |
11133 | } | |
11134 | else switch (o->sec_info_type) | |
11135 | { | |
dbaa2011 | 11136 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
11137 | if (! (_bfd_write_section_stabs |
11138 | (output_bfd, | |
8b127cbc | 11139 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
11140 | o, &elf_section_data (o)->sec_info, contents))) |
11141 | return FALSE; | |
11142 | break; | |
dbaa2011 | 11143 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
11144 | if (! _bfd_write_merged_section (output_bfd, o, |
11145 | elf_section_data (o)->sec_info)) | |
11146 | return FALSE; | |
11147 | break; | |
dbaa2011 | 11148 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 11149 | { |
8b127cbc | 11150 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
11151 | o, contents)) |
11152 | return FALSE; | |
11153 | } | |
11154 | break; | |
2f0c68f2 CM |
11155 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
11156 | { | |
11157 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
11158 | flinfo->info, | |
11159 | o, contents)) | |
11160 | return FALSE; | |
11161 | } | |
11162 | break; | |
c152c796 AM |
11163 | default: |
11164 | { | |
310fd250 L |
11165 | if (! (o->flags & SEC_EXCLUDE)) |
11166 | { | |
11167 | file_ptr offset = (file_ptr) o->output_offset; | |
11168 | bfd_size_type todo = o->size; | |
37b01f6a DG |
11169 | |
11170 | offset *= bfd_octets_per_byte (output_bfd); | |
11171 | ||
310fd250 L |
11172 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
11173 | { | |
11174 | /* Reverse-copy input section to output. */ | |
11175 | do | |
11176 | { | |
11177 | todo -= address_size; | |
11178 | if (! bfd_set_section_contents (output_bfd, | |
11179 | o->output_section, | |
11180 | contents + todo, | |
11181 | offset, | |
11182 | address_size)) | |
11183 | return FALSE; | |
11184 | if (todo == 0) | |
11185 | break; | |
11186 | offset += address_size; | |
11187 | } | |
11188 | while (1); | |
11189 | } | |
11190 | else if (! bfd_set_section_contents (output_bfd, | |
11191 | o->output_section, | |
11192 | contents, | |
11193 | offset, todo)) | |
11194 | return FALSE; | |
11195 | } | |
c152c796 AM |
11196 | } |
11197 | break; | |
11198 | } | |
11199 | } | |
11200 | ||
11201 | return TRUE; | |
11202 | } | |
11203 | ||
11204 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 11205 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
11206 | is used to build constructor and destructor tables when linking |
11207 | with -Ur. */ | |
11208 | ||
11209 | static bfd_boolean | |
11210 | elf_reloc_link_order (bfd *output_bfd, | |
11211 | struct bfd_link_info *info, | |
11212 | asection *output_section, | |
11213 | struct bfd_link_order *link_order) | |
11214 | { | |
11215 | reloc_howto_type *howto; | |
11216 | long indx; | |
11217 | bfd_vma offset; | |
11218 | bfd_vma addend; | |
d4730f92 | 11219 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
11220 | struct elf_link_hash_entry **rel_hash_ptr; |
11221 | Elf_Internal_Shdr *rel_hdr; | |
11222 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
11223 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
11224 | bfd_byte *erel; | |
11225 | unsigned int i; | |
d4730f92 | 11226 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
11227 | |
11228 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
11229 | if (howto == NULL) | |
11230 | { | |
11231 | bfd_set_error (bfd_error_bad_value); | |
11232 | return FALSE; | |
11233 | } | |
11234 | ||
11235 | addend = link_order->u.reloc.p->addend; | |
11236 | ||
d4730f92 BS |
11237 | if (esdo->rel.hdr) |
11238 | reldata = &esdo->rel; | |
11239 | else if (esdo->rela.hdr) | |
11240 | reldata = &esdo->rela; | |
11241 | else | |
11242 | { | |
11243 | reldata = NULL; | |
11244 | BFD_ASSERT (0); | |
11245 | } | |
11246 | ||
c152c796 | 11247 | /* Figure out the symbol index. */ |
d4730f92 | 11248 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
11249 | if (link_order->type == bfd_section_reloc_link_order) |
11250 | { | |
11251 | indx = link_order->u.reloc.p->u.section->target_index; | |
11252 | BFD_ASSERT (indx != 0); | |
11253 | *rel_hash_ptr = NULL; | |
11254 | } | |
11255 | else | |
11256 | { | |
11257 | struct elf_link_hash_entry *h; | |
11258 | ||
11259 | /* Treat a reloc against a defined symbol as though it were | |
11260 | actually against the section. */ | |
11261 | h = ((struct elf_link_hash_entry *) | |
11262 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
11263 | link_order->u.reloc.p->u.name, | |
11264 | FALSE, FALSE, TRUE)); | |
11265 | if (h != NULL | |
11266 | && (h->root.type == bfd_link_hash_defined | |
11267 | || h->root.type == bfd_link_hash_defweak)) | |
11268 | { | |
11269 | asection *section; | |
11270 | ||
11271 | section = h->root.u.def.section; | |
11272 | indx = section->output_section->target_index; | |
11273 | *rel_hash_ptr = NULL; | |
11274 | /* It seems that we ought to add the symbol value to the | |
11275 | addend here, but in practice it has already been added | |
11276 | because it was passed to constructor_callback. */ | |
11277 | addend += section->output_section->vma + section->output_offset; | |
11278 | } | |
11279 | else if (h != NULL) | |
11280 | { | |
11281 | /* Setting the index to -2 tells elf_link_output_extsym that | |
11282 | this symbol is used by a reloc. */ | |
11283 | h->indx = -2; | |
11284 | *rel_hash_ptr = h; | |
11285 | indx = 0; | |
11286 | } | |
11287 | else | |
11288 | { | |
1a72702b AM |
11289 | (*info->callbacks->unattached_reloc) |
11290 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
11291 | indx = 0; |
11292 | } | |
11293 | } | |
11294 | ||
11295 | /* If this is an inplace reloc, we must write the addend into the | |
11296 | object file. */ | |
11297 | if (howto->partial_inplace && addend != 0) | |
11298 | { | |
11299 | bfd_size_type size; | |
11300 | bfd_reloc_status_type rstat; | |
11301 | bfd_byte *buf; | |
11302 | bfd_boolean ok; | |
11303 | const char *sym_name; | |
11304 | ||
a50b1753 NC |
11305 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
11306 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 11307 | if (buf == NULL && size != 0) |
c152c796 AM |
11308 | return FALSE; |
11309 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
11310 | switch (rstat) | |
11311 | { | |
11312 | case bfd_reloc_ok: | |
11313 | break; | |
11314 | ||
11315 | default: | |
11316 | case bfd_reloc_outofrange: | |
11317 | abort (); | |
11318 | ||
11319 | case bfd_reloc_overflow: | |
11320 | if (link_order->type == bfd_section_reloc_link_order) | |
11321 | sym_name = bfd_section_name (output_bfd, | |
11322 | link_order->u.reloc.p->u.section); | |
11323 | else | |
11324 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
11325 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
11326 | howto->name, addend, NULL, NULL, | |
11327 | (bfd_vma) 0); | |
c152c796 AM |
11328 | break; |
11329 | } | |
37b01f6a | 11330 | |
c152c796 | 11331 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
11332 | link_order->offset |
11333 | * bfd_octets_per_byte (output_bfd), | |
11334 | size); | |
c152c796 AM |
11335 | free (buf); |
11336 | if (! ok) | |
11337 | return FALSE; | |
11338 | } | |
11339 | ||
11340 | /* The address of a reloc is relative to the section in a | |
11341 | relocatable file, and is a virtual address in an executable | |
11342 | file. */ | |
11343 | offset = link_order->offset; | |
0e1862bb | 11344 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
11345 | offset += output_section->vma; |
11346 | ||
11347 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
11348 | { | |
11349 | irel[i].r_offset = offset; | |
11350 | irel[i].r_info = 0; | |
11351 | irel[i].r_addend = 0; | |
11352 | } | |
11353 | if (bed->s->arch_size == 32) | |
11354 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
11355 | else | |
11356 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
11357 | ||
d4730f92 | 11358 | rel_hdr = reldata->hdr; |
c152c796 AM |
11359 | erel = rel_hdr->contents; |
11360 | if (rel_hdr->sh_type == SHT_REL) | |
11361 | { | |
d4730f92 | 11362 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
11363 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
11364 | } | |
11365 | else | |
11366 | { | |
11367 | irel[0].r_addend = addend; | |
d4730f92 | 11368 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
11369 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
11370 | } | |
11371 | ||
d4730f92 | 11372 | ++reldata->count; |
c152c796 AM |
11373 | |
11374 | return TRUE; | |
11375 | } | |
11376 | ||
0b52efa6 PB |
11377 | |
11378 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
11379 | ||
11380 | static bfd_vma | |
11381 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
11382 | { | |
11383 | Elf_Internal_Shdr **elf_shdrp; | |
11384 | asection *s; | |
11385 | int elfsec; | |
11386 | ||
11387 | s = p->u.indirect.section; | |
11388 | elf_shdrp = elf_elfsections (s->owner); | |
11389 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
11390 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
11391 | /* PR 290: |
11392 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 11393 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
11394 | sh_info fields. Hence we could get the situation |
11395 | where elfsec is 0. */ | |
11396 | if (elfsec == 0) | |
11397 | { | |
11398 | const struct elf_backend_data *bed | |
11399 | = get_elf_backend_data (s->owner); | |
11400 | if (bed->link_order_error_handler) | |
d003868e | 11401 | bed->link_order_error_handler |
695344c0 | 11402 | /* xgettext:c-format */ |
871b3ab2 | 11403 | (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s); |
185d09ad L |
11404 | return 0; |
11405 | } | |
11406 | else | |
11407 | { | |
11408 | s = elf_shdrp[elfsec]->bfd_section; | |
11409 | return s->output_section->vma + s->output_offset; | |
11410 | } | |
0b52efa6 PB |
11411 | } |
11412 | ||
11413 | ||
11414 | /* Compare two sections based on the locations of the sections they are | |
11415 | linked to. Used by elf_fixup_link_order. */ | |
11416 | ||
11417 | static int | |
11418 | compare_link_order (const void * a, const void * b) | |
11419 | { | |
11420 | bfd_vma apos; | |
11421 | bfd_vma bpos; | |
11422 | ||
11423 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
11424 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
11425 | if (apos < bpos) | |
11426 | return -1; | |
11427 | return apos > bpos; | |
11428 | } | |
11429 | ||
11430 | ||
11431 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
11432 | order as their linked sections. Returns false if this could not be done | |
11433 | because an output section includes both ordered and unordered | |
11434 | sections. Ideally we'd do this in the linker proper. */ | |
11435 | ||
11436 | static bfd_boolean | |
11437 | elf_fixup_link_order (bfd *abfd, asection *o) | |
11438 | { | |
11439 | int seen_linkorder; | |
11440 | int seen_other; | |
11441 | int n; | |
11442 | struct bfd_link_order *p; | |
11443 | bfd *sub; | |
11444 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 11445 | unsigned elfsec; |
0b52efa6 | 11446 | struct bfd_link_order **sections; |
d33cdfe3 | 11447 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 11448 | bfd_vma offset; |
3b36f7e6 | 11449 | |
d33cdfe3 L |
11450 | other_sec = NULL; |
11451 | linkorder_sec = NULL; | |
0b52efa6 PB |
11452 | seen_other = 0; |
11453 | seen_linkorder = 0; | |
8423293d | 11454 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 11455 | { |
d33cdfe3 | 11456 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
11457 | { |
11458 | s = p->u.indirect.section; | |
d33cdfe3 L |
11459 | sub = s->owner; |
11460 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
11461 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
11462 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
11463 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
11464 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
11465 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
11466 | { |
11467 | seen_linkorder++; | |
11468 | linkorder_sec = s; | |
11469 | } | |
0b52efa6 | 11470 | else |
d33cdfe3 L |
11471 | { |
11472 | seen_other++; | |
11473 | other_sec = s; | |
11474 | } | |
0b52efa6 PB |
11475 | } |
11476 | else | |
11477 | seen_other++; | |
d33cdfe3 L |
11478 | |
11479 | if (seen_other && seen_linkorder) | |
11480 | { | |
11481 | if (other_sec && linkorder_sec) | |
4eca0228 | 11482 | _bfd_error_handler |
695344c0 | 11483 | /* xgettext:c-format */ |
871b3ab2 AM |
11484 | (_("%pA has both ordered [`%pA' in %pB] " |
11485 | "and unordered [`%pA' in %pB] sections"), | |
63a5468a AM |
11486 | o, linkorder_sec, linkorder_sec->owner, |
11487 | other_sec, other_sec->owner); | |
d33cdfe3 | 11488 | else |
4eca0228 | 11489 | _bfd_error_handler |
871b3ab2 | 11490 | (_("%pA has both ordered and unordered sections"), o); |
d33cdfe3 L |
11491 | bfd_set_error (bfd_error_bad_value); |
11492 | return FALSE; | |
11493 | } | |
0b52efa6 PB |
11494 | } |
11495 | ||
11496 | if (!seen_linkorder) | |
11497 | return TRUE; | |
11498 | ||
0b52efa6 | 11499 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11500 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11501 | if (sections == NULL) | |
11502 | return FALSE; | |
0b52efa6 | 11503 | seen_linkorder = 0; |
3b36f7e6 | 11504 | |
8423293d | 11505 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11506 | { |
11507 | sections[seen_linkorder++] = p; | |
11508 | } | |
11509 | /* Sort the input sections in the order of their linked section. */ | |
11510 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11511 | compare_link_order); | |
11512 | ||
11513 | /* Change the offsets of the sections. */ | |
11514 | offset = 0; | |
11515 | for (n = 0; n < seen_linkorder; n++) | |
11516 | { | |
11517 | s = sections[n]->u.indirect.section; | |
461686a3 | 11518 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11519 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11520 | sections[n]->offset = offset; |
11521 | offset += sections[n]->size; | |
11522 | } | |
11523 | ||
4dd07732 | 11524 | free (sections); |
0b52efa6 PB |
11525 | return TRUE; |
11526 | } | |
11527 | ||
76359541 TP |
11528 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11529 | Returns TRUE upon success, FALSE otherwise. */ | |
11530 | ||
11531 | static bfd_boolean | |
11532 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11533 | { | |
11534 | bfd_boolean ret = FALSE; | |
11535 | bfd *implib_bfd; | |
11536 | const struct elf_backend_data *bed; | |
11537 | flagword flags; | |
11538 | enum bfd_architecture arch; | |
11539 | unsigned int mach; | |
11540 | asymbol **sympp = NULL; | |
11541 | long symsize; | |
11542 | long symcount; | |
11543 | long src_count; | |
11544 | elf_symbol_type *osymbuf; | |
11545 | ||
11546 | implib_bfd = info->out_implib_bfd; | |
11547 | bed = get_elf_backend_data (abfd); | |
11548 | ||
11549 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11550 | return FALSE; | |
11551 | ||
046734ff | 11552 | /* Use flag from executable but make it a relocatable object. */ |
76359541 TP |
11553 | flags = bfd_get_file_flags (abfd); |
11554 | flags &= ~HAS_RELOC; | |
11555 | if (!bfd_set_start_address (implib_bfd, 0) | |
046734ff | 11556 | || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) |
76359541 TP |
11557 | return FALSE; |
11558 | ||
11559 | /* Copy architecture of output file to import library file. */ | |
11560 | arch = bfd_get_arch (abfd); | |
11561 | mach = bfd_get_mach (abfd); | |
11562 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11563 | && (abfd->target_defaulted | |
11564 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11565 | return FALSE; | |
11566 | ||
11567 | /* Get symbol table size. */ | |
11568 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11569 | if (symsize < 0) | |
11570 | return FALSE; | |
11571 | ||
11572 | /* Read in the symbol table. */ | |
11573 | sympp = (asymbol **) xmalloc (symsize); | |
11574 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11575 | if (symcount < 0) | |
11576 | goto free_sym_buf; | |
11577 | ||
11578 | /* Allow the BFD backend to copy any private header data it | |
11579 | understands from the output BFD to the import library BFD. */ | |
11580 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11581 | goto free_sym_buf; | |
11582 | ||
11583 | /* Filter symbols to appear in the import library. */ | |
11584 | if (bed->elf_backend_filter_implib_symbols) | |
11585 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11586 | symcount); | |
11587 | else | |
11588 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11589 | if (symcount == 0) | |
11590 | { | |
5df1bc57 | 11591 | bfd_set_error (bfd_error_no_symbols); |
871b3ab2 | 11592 | _bfd_error_handler (_("%pB: no symbol found for import library"), |
4eca0228 | 11593 | implib_bfd); |
76359541 TP |
11594 | goto free_sym_buf; |
11595 | } | |
11596 | ||
11597 | ||
11598 | /* Make symbols absolute. */ | |
11599 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11600 | sizeof (*osymbuf)); | |
11601 | for (src_count = 0; src_count < symcount; src_count++) | |
11602 | { | |
11603 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11604 | sizeof (*osymbuf)); | |
11605 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11606 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11607 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11608 | osymbuf[src_count].internal_elf_sym.st_value = | |
11609 | osymbuf[src_count].symbol.value; | |
11610 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11611 | } | |
11612 | ||
11613 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11614 | ||
11615 | /* Allow the BFD backend to copy any private data it understands | |
11616 | from the output BFD to the import library BFD. This is done last | |
11617 | to permit the routine to look at the filtered symbol table. */ | |
11618 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11619 | goto free_sym_buf; | |
11620 | ||
11621 | if (!bfd_close (implib_bfd)) | |
11622 | goto free_sym_buf; | |
11623 | ||
11624 | ret = TRUE; | |
11625 | ||
11626 | free_sym_buf: | |
11627 | free (sympp); | |
11628 | return ret; | |
11629 | } | |
11630 | ||
9f7c3e5e AM |
11631 | static void |
11632 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11633 | { | |
11634 | asection *o; | |
11635 | ||
11636 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11637 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11638 | if (flinfo->contents != NULL) |
11639 | free (flinfo->contents); | |
11640 | if (flinfo->external_relocs != NULL) | |
11641 | free (flinfo->external_relocs); | |
11642 | if (flinfo->internal_relocs != NULL) | |
11643 | free (flinfo->internal_relocs); | |
11644 | if (flinfo->external_syms != NULL) | |
11645 | free (flinfo->external_syms); | |
11646 | if (flinfo->locsym_shndx != NULL) | |
11647 | free (flinfo->locsym_shndx); | |
11648 | if (flinfo->internal_syms != NULL) | |
11649 | free (flinfo->internal_syms); | |
11650 | if (flinfo->indices != NULL) | |
11651 | free (flinfo->indices); | |
11652 | if (flinfo->sections != NULL) | |
11653 | free (flinfo->sections); | |
9f7c3e5e AM |
11654 | if (flinfo->symshndxbuf != NULL) |
11655 | free (flinfo->symshndxbuf); | |
11656 | for (o = obfd->sections; o != NULL; o = o->next) | |
11657 | { | |
11658 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11659 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11660 | free (esdo->rel.hashes); | |
11661 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11662 | free (esdo->rela.hashes); | |
11663 | } | |
11664 | } | |
0b52efa6 | 11665 | |
c152c796 AM |
11666 | /* Do the final step of an ELF link. */ |
11667 | ||
11668 | bfd_boolean | |
11669 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11670 | { | |
11671 | bfd_boolean dynamic; | |
11672 | bfd_boolean emit_relocs; | |
11673 | bfd *dynobj; | |
8b127cbc | 11674 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11675 | asection *o; |
11676 | struct bfd_link_order *p; | |
11677 | bfd *sub; | |
c152c796 AM |
11678 | bfd_size_type max_contents_size; |
11679 | bfd_size_type max_external_reloc_size; | |
11680 | bfd_size_type max_internal_reloc_count; | |
11681 | bfd_size_type max_sym_count; | |
11682 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11683 | Elf_Internal_Sym elfsym; |
11684 | unsigned int i; | |
11685 | Elf_Internal_Shdr *symtab_hdr; | |
11686 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11687 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11688 | struct elf_outext_info eoinfo; | |
11689 | bfd_boolean merged; | |
11690 | size_t relativecount = 0; | |
11691 | asection *reldyn = 0; | |
11692 | bfd_size_type amt; | |
104d59d1 JM |
11693 | asection *attr_section = NULL; |
11694 | bfd_vma attr_size = 0; | |
11695 | const char *std_attrs_section; | |
64f52338 | 11696 | struct elf_link_hash_table *htab = elf_hash_table (info); |
c152c796 | 11697 | |
64f52338 | 11698 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11699 | return FALSE; |
11700 | ||
0e1862bb | 11701 | if (bfd_link_pic (info)) |
c152c796 AM |
11702 | abfd->flags |= DYNAMIC; |
11703 | ||
64f52338 AM |
11704 | dynamic = htab->dynamic_sections_created; |
11705 | dynobj = htab->dynobj; | |
c152c796 | 11706 | |
0e1862bb | 11707 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11708 | || info->emitrelocations); |
c152c796 | 11709 | |
8b127cbc AM |
11710 | flinfo.info = info; |
11711 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11712 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11713 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11714 | return FALSE; |
11715 | ||
11716 | if (! dynamic) | |
11717 | { | |
8b127cbc AM |
11718 | flinfo.hash_sec = NULL; |
11719 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11720 | } |
11721 | else | |
11722 | { | |
3d4d4302 | 11723 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11724 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11725 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11726 | /* Note that it is OK if symver_sec is NULL. */ |
11727 | } | |
11728 | ||
8b127cbc AM |
11729 | flinfo.contents = NULL; |
11730 | flinfo.external_relocs = NULL; | |
11731 | flinfo.internal_relocs = NULL; | |
11732 | flinfo.external_syms = NULL; | |
11733 | flinfo.locsym_shndx = NULL; | |
11734 | flinfo.internal_syms = NULL; | |
11735 | flinfo.indices = NULL; | |
11736 | flinfo.sections = NULL; | |
8b127cbc | 11737 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11738 | flinfo.filesym_count = 0; |
c152c796 | 11739 | |
104d59d1 JM |
11740 | /* The object attributes have been merged. Remove the input |
11741 | sections from the link, and set the contents of the output | |
11742 | secton. */ | |
11743 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11744 | for (o = abfd->sections; o != NULL; o = o->next) | |
11745 | { | |
5270eddc | 11746 | bfd_boolean remove_section = FALSE; |
b8a6ced7 | 11747 | |
104d59d1 JM |
11748 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) |
11749 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11750 | { | |
11751 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11752 | { | |
11753 | asection *input_section; | |
11754 | ||
11755 | if (p->type != bfd_indirect_link_order) | |
11756 | continue; | |
11757 | input_section = p->u.indirect.section; | |
11758 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11759 | elf_link_input_bfd ignores this section. */ | |
11760 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11761 | } | |
a0c8462f | 11762 | |
104d59d1 | 11763 | attr_size = bfd_elf_obj_attr_size (abfd); |
b8a6ced7 AM |
11764 | bfd_set_section_size (abfd, o, attr_size); |
11765 | /* Skip this section later on. */ | |
11766 | o->map_head.link_order = NULL; | |
104d59d1 | 11767 | if (attr_size) |
b8a6ced7 | 11768 | attr_section = o; |
104d59d1 | 11769 | else |
5270eddc | 11770 | remove_section = TRUE; |
104d59d1 | 11771 | } |
6e5e9d58 AM |
11772 | else if ((o->flags & SEC_GROUP) != 0 && o->size == 0) |
11773 | { | |
11774 | /* Remove empty group section from linker output. */ | |
5270eddc | 11775 | remove_section = TRUE; |
b8a6ced7 | 11776 | } |
5270eddc | 11777 | if (remove_section) |
b8a6ced7 | 11778 | { |
6e5e9d58 AM |
11779 | o->flags |= SEC_EXCLUDE; |
11780 | bfd_section_list_remove (abfd, o); | |
11781 | abfd->section_count--; | |
11782 | } | |
104d59d1 JM |
11783 | } |
11784 | ||
c152c796 AM |
11785 | /* Count up the number of relocations we will output for each output |
11786 | section, so that we know the sizes of the reloc sections. We | |
11787 | also figure out some maximum sizes. */ | |
11788 | max_contents_size = 0; | |
11789 | max_external_reloc_size = 0; | |
11790 | max_internal_reloc_count = 0; | |
11791 | max_sym_count = 0; | |
11792 | max_sym_shndx_count = 0; | |
11793 | merged = FALSE; | |
11794 | for (o = abfd->sections; o != NULL; o = o->next) | |
11795 | { | |
11796 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11797 | o->reloc_count = 0; | |
11798 | ||
8423293d | 11799 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11800 | { |
11801 | unsigned int reloc_count = 0; | |
9eaff861 | 11802 | unsigned int additional_reloc_count = 0; |
c152c796 | 11803 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
11804 | |
11805 | if (p->type == bfd_section_reloc_link_order | |
11806 | || p->type == bfd_symbol_reloc_link_order) | |
11807 | reloc_count = 1; | |
11808 | else if (p->type == bfd_indirect_link_order) | |
11809 | { | |
11810 | asection *sec; | |
11811 | ||
11812 | sec = p->u.indirect.section; | |
c152c796 AM |
11813 | |
11814 | /* Mark all sections which are to be included in the | |
11815 | link. This will normally be every section. We need | |
11816 | to do this so that we can identify any sections which | |
11817 | the linker has decided to not include. */ | |
11818 | sec->linker_mark = TRUE; | |
11819 | ||
11820 | if (sec->flags & SEC_MERGE) | |
11821 | merged = TRUE; | |
11822 | ||
eea6121a AM |
11823 | if (sec->rawsize > max_contents_size) |
11824 | max_contents_size = sec->rawsize; | |
11825 | if (sec->size > max_contents_size) | |
11826 | max_contents_size = sec->size; | |
c152c796 | 11827 | |
c152c796 AM |
11828 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
11829 | && (sec->owner->flags & DYNAMIC) == 0) | |
11830 | { | |
11831 | size_t sym_count; | |
11832 | ||
a961cdd5 AM |
11833 | /* We are interested in just local symbols, not all |
11834 | symbols. */ | |
c152c796 AM |
11835 | if (elf_bad_symtab (sec->owner)) |
11836 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11837 | / bed->s->sizeof_sym); | |
11838 | else | |
11839 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11840 | ||
11841 | if (sym_count > max_sym_count) | |
11842 | max_sym_count = sym_count; | |
11843 | ||
11844 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11845 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11846 | max_sym_shndx_count = sym_count; |
11847 | ||
a961cdd5 AM |
11848 | if (esdo->this_hdr.sh_type == SHT_REL |
11849 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11850 | /* Some backends use reloc_count in relocation sections | |
11851 | to count particular types of relocs. Of course, | |
11852 | reloc sections themselves can't have relocations. */ | |
11853 | ; | |
11854 | else if (emit_relocs) | |
11855 | { | |
11856 | reloc_count = sec->reloc_count; | |
11857 | if (bed->elf_backend_count_additional_relocs) | |
11858 | { | |
11859 | int c; | |
11860 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11861 | additional_reloc_count += c; | |
11862 | } | |
11863 | } | |
11864 | else if (bed->elf_backend_count_relocs) | |
11865 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
11866 | ||
11867 | esdi = elf_section_data (sec); | |
11868 | ||
c152c796 AM |
11869 | if ((sec->flags & SEC_RELOC) != 0) |
11870 | { | |
d4730f92 | 11871 | size_t ext_size = 0; |
c152c796 | 11872 | |
d4730f92 BS |
11873 | if (esdi->rel.hdr != NULL) |
11874 | ext_size = esdi->rel.hdr->sh_size; | |
11875 | if (esdi->rela.hdr != NULL) | |
11876 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11877 | |
c152c796 AM |
11878 | if (ext_size > max_external_reloc_size) |
11879 | max_external_reloc_size = ext_size; | |
11880 | if (sec->reloc_count > max_internal_reloc_count) | |
11881 | max_internal_reloc_count = sec->reloc_count; | |
11882 | } | |
11883 | } | |
11884 | } | |
11885 | ||
11886 | if (reloc_count == 0) | |
11887 | continue; | |
11888 | ||
9eaff861 | 11889 | reloc_count += additional_reloc_count; |
c152c796 AM |
11890 | o->reloc_count += reloc_count; |
11891 | ||
0e1862bb | 11892 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11893 | { |
d4730f92 | 11894 | if (esdi->rel.hdr) |
9eaff861 | 11895 | { |
491d01d3 | 11896 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
11897 | esdo->rel.count += additional_reloc_count; |
11898 | } | |
d4730f92 | 11899 | if (esdi->rela.hdr) |
9eaff861 | 11900 | { |
491d01d3 | 11901 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
11902 | esdo->rela.count += additional_reloc_count; |
11903 | } | |
d4730f92 BS |
11904 | } |
11905 | else | |
11906 | { | |
11907 | if (o->use_rela_p) | |
11908 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11909 | else |
d4730f92 | 11910 | esdo->rel.count += reloc_count; |
c152c796 | 11911 | } |
c152c796 AM |
11912 | } |
11913 | ||
9eaff861 | 11914 | if (o->reloc_count > 0) |
c152c796 AM |
11915 | o->flags |= SEC_RELOC; |
11916 | else | |
11917 | { | |
11918 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11919 | set it (this is probably a bug) and if it is set | |
11920 | assign_section_numbers will create a reloc section. */ | |
11921 | o->flags &=~ SEC_RELOC; | |
11922 | } | |
11923 | ||
11924 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11925 | zero. This is done in elf_fake_sections as well, but forcing | |
11926 | the VMA to 0 here will ensure that relocs against these | |
11927 | sections are handled correctly. */ | |
11928 | if ((o->flags & SEC_ALLOC) == 0 | |
11929 | && ! o->user_set_vma) | |
11930 | o->vma = 0; | |
11931 | } | |
11932 | ||
0e1862bb | 11933 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 11934 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
11935 | |
11936 | /* Figure out the file positions for everything but the symbol table | |
11937 | and the relocs. We set symcount to force assign_section_numbers | |
11938 | to create a symbol table. */ | |
8539e4e8 | 11939 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11940 | BFD_ASSERT (! abfd->output_has_begun); |
11941 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11942 | goto error_return; | |
11943 | ||
ee75fd95 | 11944 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11945 | for (o = abfd->sections; o != NULL; o = o->next) |
11946 | { | |
d4730f92 | 11947 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11948 | if ((o->flags & SEC_RELOC) != 0) |
11949 | { | |
d4730f92 | 11950 | if (esdo->rel.hdr |
9eaff861 | 11951 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
11952 | goto error_return; |
11953 | ||
d4730f92 | 11954 | if (esdo->rela.hdr |
9eaff861 | 11955 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
11956 | goto error_return; |
11957 | } | |
11958 | ||
11959 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
11960 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
11961 | esdo->rel.count = 0; |
11962 | esdo->rela.count = 0; | |
0ce398f1 L |
11963 | |
11964 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
11965 | { | |
11966 | /* Cache the section contents so that they can be compressed | |
11967 | later. Use bfd_malloc since it will be freed by | |
11968 | bfd_compress_section_contents. */ | |
11969 | unsigned char *contents = esdo->this_hdr.contents; | |
11970 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
11971 | abort (); | |
11972 | contents | |
11973 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
11974 | if (contents == NULL) | |
11975 | goto error_return; | |
11976 | esdo->this_hdr.contents = contents; | |
11977 | } | |
c152c796 AM |
11978 | } |
11979 | ||
c152c796 | 11980 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
11981 | .symtab, .strtab, and non-loaded reloc sections. We start the |
11982 | .symtab section at the current file position, and write directly | |
11983 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
11984 | bfd_get_symcount (abfd) = 0; |
11985 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
11986 | /* sh_name is set in prep_headers. */ | |
11987 | symtab_hdr->sh_type = SHT_SYMTAB; | |
11988 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
11989 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
11990 | /* sh_link is set in assign_section_numbers. */ | |
11991 | /* sh_info is set below. */ | |
11992 | /* sh_offset is set just below. */ | |
72de5009 | 11993 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 11994 | |
ef10c3ac L |
11995 | if (max_sym_count < 20) |
11996 | max_sym_count = 20; | |
64f52338 | 11997 | htab->strtabsize = max_sym_count; |
ef10c3ac | 11998 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
11999 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
12000 | if (htab->strtab == NULL) | |
c152c796 | 12001 | goto error_return; |
ef10c3ac L |
12002 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
12003 | flinfo.symshndxbuf | |
12004 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
12005 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 12006 | |
8539e4e8 | 12007 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 12008 | { |
8539e4e8 AM |
12009 | file_ptr off = elf_next_file_pos (abfd); |
12010 | ||
12011 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
12012 | ||
12013 | /* Note that at this point elf_next_file_pos (abfd) is | |
12014 | incorrect. We do not yet know the size of the .symtab section. | |
12015 | We correct next_file_pos below, after we do know the size. */ | |
12016 | ||
12017 | /* Start writing out the symbol table. The first symbol is always a | |
12018 | dummy symbol. */ | |
c152c796 AM |
12019 | elfsym.st_value = 0; |
12020 | elfsym.st_size = 0; | |
12021 | elfsym.st_info = 0; | |
12022 | elfsym.st_other = 0; | |
12023 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 12024 | elfsym.st_target_internal = 0; |
ef10c3ac L |
12025 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
12026 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 12027 | goto error_return; |
c152c796 | 12028 | |
8539e4e8 AM |
12029 | /* Output a symbol for each section. We output these even if we are |
12030 | discarding local symbols, since they are used for relocs. These | |
12031 | symbols have no names. We store the index of each one in the | |
12032 | index field of the section, so that we can find it again when | |
12033 | outputting relocs. */ | |
12034 | ||
c152c796 AM |
12035 | elfsym.st_size = 0; |
12036 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12037 | elfsym.st_other = 0; | |
f0b5bb34 | 12038 | elfsym.st_value = 0; |
35fc36a8 | 12039 | elfsym.st_target_internal = 0; |
c152c796 AM |
12040 | for (i = 1; i < elf_numsections (abfd); i++) |
12041 | { | |
12042 | o = bfd_section_from_elf_index (abfd, i); | |
12043 | if (o != NULL) | |
f0b5bb34 AM |
12044 | { |
12045 | o->target_index = bfd_get_symcount (abfd); | |
12046 | elfsym.st_shndx = i; | |
0e1862bb | 12047 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 12048 | elfsym.st_value = o->vma; |
ef10c3ac L |
12049 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
12050 | NULL) != 1) | |
f0b5bb34 AM |
12051 | goto error_return; |
12052 | } | |
c152c796 AM |
12053 | } |
12054 | } | |
12055 | ||
12056 | /* Allocate some memory to hold information read in from the input | |
12057 | files. */ | |
12058 | if (max_contents_size != 0) | |
12059 | { | |
8b127cbc AM |
12060 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
12061 | if (flinfo.contents == NULL) | |
c152c796 AM |
12062 | goto error_return; |
12063 | } | |
12064 | ||
12065 | if (max_external_reloc_size != 0) | |
12066 | { | |
8b127cbc AM |
12067 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
12068 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
12069 | goto error_return; |
12070 | } | |
12071 | ||
12072 | if (max_internal_reloc_count != 0) | |
12073 | { | |
056bafd4 | 12074 | amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); |
8b127cbc AM |
12075 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
12076 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
12077 | goto error_return; |
12078 | } | |
12079 | ||
12080 | if (max_sym_count != 0) | |
12081 | { | |
12082 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
12083 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
12084 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
12085 | goto error_return; |
12086 | ||
12087 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
12088 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
12089 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
12090 | goto error_return; |
12091 | ||
12092 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
12093 | flinfo.indices = (long int *) bfd_malloc (amt); |
12094 | if (flinfo.indices == NULL) | |
c152c796 AM |
12095 | goto error_return; |
12096 | ||
12097 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
12098 | flinfo.sections = (asection **) bfd_malloc (amt); |
12099 | if (flinfo.sections == NULL) | |
c152c796 AM |
12100 | goto error_return; |
12101 | } | |
12102 | ||
12103 | if (max_sym_shndx_count != 0) | |
12104 | { | |
12105 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
12106 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
12107 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
12108 | goto error_return; |
12109 | } | |
12110 | ||
64f52338 | 12111 | if (htab->tls_sec) |
c152c796 AM |
12112 | { |
12113 | bfd_vma base, end = 0; | |
12114 | asection *sec; | |
12115 | ||
64f52338 | 12116 | for (sec = htab->tls_sec; |
c152c796 AM |
12117 | sec && (sec->flags & SEC_THREAD_LOCAL); |
12118 | sec = sec->next) | |
12119 | { | |
3a800eb9 | 12120 | bfd_size_type size = sec->size; |
c152c796 | 12121 | |
3a800eb9 AM |
12122 | if (size == 0 |
12123 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 12124 | { |
91d6fa6a NC |
12125 | struct bfd_link_order *ord = sec->map_tail.link_order; |
12126 | ||
12127 | if (ord != NULL) | |
12128 | size = ord->offset + ord->size; | |
c152c796 AM |
12129 | } |
12130 | end = sec->vma + size; | |
12131 | } | |
64f52338 | 12132 | base = htab->tls_sec->vma; |
7dc98aea RO |
12133 | /* Only align end of TLS section if static TLS doesn't have special |
12134 | alignment requirements. */ | |
12135 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
12136 | end = align_power (end, htab->tls_sec->alignment_power); |
12137 | htab->tls_size = end - base; | |
c152c796 AM |
12138 | } |
12139 | ||
0b52efa6 PB |
12140 | /* Reorder SHF_LINK_ORDER sections. */ |
12141 | for (o = abfd->sections; o != NULL; o = o->next) | |
12142 | { | |
12143 | if (!elf_fixup_link_order (abfd, o)) | |
12144 | return FALSE; | |
12145 | } | |
12146 | ||
2f0c68f2 CM |
12147 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
12148 | return FALSE; | |
12149 | ||
c152c796 AM |
12150 | /* Since ELF permits relocations to be against local symbols, we |
12151 | must have the local symbols available when we do the relocations. | |
12152 | Since we would rather only read the local symbols once, and we | |
12153 | would rather not keep them in memory, we handle all the | |
12154 | relocations for a single input file at the same time. | |
12155 | ||
12156 | Unfortunately, there is no way to know the total number of local | |
12157 | symbols until we have seen all of them, and the local symbol | |
12158 | indices precede the global symbol indices. This means that when | |
12159 | we are generating relocatable output, and we see a reloc against | |
12160 | a global symbol, we can not know the symbol index until we have | |
12161 | finished examining all the local symbols to see which ones we are | |
12162 | going to output. To deal with this, we keep the relocations in | |
12163 | memory, and don't output them until the end of the link. This is | |
12164 | an unfortunate waste of memory, but I don't see a good way around | |
12165 | it. Fortunately, it only happens when performing a relocatable | |
12166 | link, which is not the common case. FIXME: If keep_memory is set | |
12167 | we could write the relocs out and then read them again; I don't | |
12168 | know how bad the memory loss will be. */ | |
12169 | ||
c72f2fb2 | 12170 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12171 | sub->output_has_begun = FALSE; |
12172 | for (o = abfd->sections; o != NULL; o = o->next) | |
12173 | { | |
8423293d | 12174 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12175 | { |
12176 | if (p->type == bfd_indirect_link_order | |
12177 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
12178 | == bfd_target_elf_flavour) | |
12179 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
12180 | { | |
12181 | if (! sub->output_has_begun) | |
12182 | { | |
8b127cbc | 12183 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
12184 | goto error_return; |
12185 | sub->output_has_begun = TRUE; | |
12186 | } | |
12187 | } | |
12188 | else if (p->type == bfd_section_reloc_link_order | |
12189 | || p->type == bfd_symbol_reloc_link_order) | |
12190 | { | |
12191 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
12192 | goto error_return; | |
12193 | } | |
12194 | else | |
12195 | { | |
12196 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
12197 | { |
12198 | if (p->type == bfd_indirect_link_order | |
12199 | && (bfd_get_flavour (sub) | |
12200 | == bfd_target_elf_flavour) | |
12201 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
12202 | != bed->s->elfclass)) | |
12203 | { | |
12204 | const char *iclass, *oclass; | |
12205 | ||
aebf9be7 | 12206 | switch (bed->s->elfclass) |
351f65ca | 12207 | { |
aebf9be7 NC |
12208 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
12209 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
12210 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
12211 | default: abort (); | |
351f65ca | 12212 | } |
aebf9be7 NC |
12213 | |
12214 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 12215 | { |
aebf9be7 NC |
12216 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
12217 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
12218 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
12219 | default: abort (); | |
351f65ca L |
12220 | } |
12221 | ||
12222 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 12223 | _bfd_error_handler |
695344c0 | 12224 | /* xgettext:c-format */ |
871b3ab2 | 12225 | (_("%pB: file class %s incompatible with %s"), |
351f65ca L |
12226 | sub, iclass, oclass); |
12227 | } | |
12228 | ||
12229 | goto error_return; | |
12230 | } | |
c152c796 AM |
12231 | } |
12232 | } | |
12233 | } | |
12234 | ||
c0f00686 L |
12235 | /* Free symbol buffer if needed. */ |
12236 | if (!info->reduce_memory_overheads) | |
12237 | { | |
c72f2fb2 | 12238 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
12239 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
12240 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
12241 | { |
12242 | free (elf_tdata (sub)->symbuf); | |
12243 | elf_tdata (sub)->symbuf = NULL; | |
12244 | } | |
12245 | } | |
12246 | ||
c152c796 AM |
12247 | /* Output any global symbols that got converted to local in a |
12248 | version script or due to symbol visibility. We do this in a | |
12249 | separate step since ELF requires all local symbols to appear | |
12250 | prior to any global symbols. FIXME: We should only do this if | |
12251 | some global symbols were, in fact, converted to become local. | |
12252 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
12253 | eoinfo.failed = FALSE; | |
8b127cbc | 12254 | eoinfo.flinfo = &flinfo; |
c152c796 | 12255 | eoinfo.localsyms = TRUE; |
34a79995 | 12256 | eoinfo.file_sym_done = FALSE; |
7686d77d | 12257 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12258 | if (eoinfo.failed) |
12259 | return FALSE; | |
12260 | ||
4e617b1e PB |
12261 | /* If backend needs to output some local symbols not present in the hash |
12262 | table, do it now. */ | |
8539e4e8 AM |
12263 | if (bed->elf_backend_output_arch_local_syms |
12264 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 12265 | { |
6e0b88f1 | 12266 | typedef int (*out_sym_func) |
4e617b1e PB |
12267 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12268 | struct elf_link_hash_entry *); | |
12269 | ||
12270 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
12271 | (abfd, info, &flinfo, |
12272 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
12273 | return FALSE; |
12274 | } | |
12275 | ||
c152c796 AM |
12276 | /* That wrote out all the local symbols. Finish up the symbol table |
12277 | with the global symbols. Even if we want to strip everything we | |
12278 | can, we still need to deal with those global symbols that got | |
12279 | converted to local in a version script. */ | |
12280 | ||
12281 | /* The sh_info field records the index of the first non local symbol. */ | |
12282 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
12283 | ||
12284 | if (dynamic | |
64f52338 AM |
12285 | && htab->dynsym != NULL |
12286 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
12287 | { |
12288 | Elf_Internal_Sym sym; | |
64f52338 | 12289 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 12290 | |
64f52338 AM |
12291 | o = htab->dynsym->output_section; |
12292 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
12293 | |
12294 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 12295 | if (bfd_link_pic (info) |
64f52338 | 12296 | || htab->is_relocatable_executable) |
c152c796 AM |
12297 | { |
12298 | asection *s; | |
12299 | ||
12300 | sym.st_size = 0; | |
12301 | sym.st_name = 0; | |
12302 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12303 | sym.st_other = 0; | |
35fc36a8 | 12304 | sym.st_target_internal = 0; |
c152c796 AM |
12305 | |
12306 | for (s = abfd->sections; s != NULL; s = s->next) | |
12307 | { | |
12308 | int indx; | |
12309 | bfd_byte *dest; | |
12310 | long dynindx; | |
12311 | ||
c152c796 | 12312 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
12313 | if (dynindx <= 0) |
12314 | continue; | |
12315 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
12316 | BFD_ASSERT (indx > 0); |
12317 | sym.st_shndx = indx; | |
c0d5a53d L |
12318 | if (! check_dynsym (abfd, &sym)) |
12319 | return FALSE; | |
c152c796 AM |
12320 | sym.st_value = s->vma; |
12321 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
12322 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12323 | } | |
c152c796 AM |
12324 | } |
12325 | ||
12326 | /* Write out the local dynsyms. */ | |
64f52338 | 12327 | if (htab->dynlocal) |
c152c796 AM |
12328 | { |
12329 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 12330 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
12331 | { |
12332 | asection *s; | |
12333 | bfd_byte *dest; | |
12334 | ||
935bd1e0 | 12335 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
12336 | Note that we saved a word of storage and overwrote |
12337 | the original st_name with the dynstr_index. */ | |
12338 | sym = e->isym; | |
935bd1e0 | 12339 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 12340 | |
cb33740c AM |
12341 | s = bfd_section_from_elf_index (e->input_bfd, |
12342 | e->isym.st_shndx); | |
12343 | if (s != NULL) | |
c152c796 | 12344 | { |
c152c796 AM |
12345 | sym.st_shndx = |
12346 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
12347 | if (! check_dynsym (abfd, &sym)) |
12348 | return FALSE; | |
c152c796 AM |
12349 | sym.st_value = (s->output_section->vma |
12350 | + s->output_offset | |
12351 | + e->isym.st_value); | |
12352 | } | |
12353 | ||
c152c796 AM |
12354 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
12355 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12356 | } | |
12357 | } | |
c152c796 AM |
12358 | } |
12359 | ||
12360 | /* We get the global symbols from the hash table. */ | |
12361 | eoinfo.failed = FALSE; | |
12362 | eoinfo.localsyms = FALSE; | |
8b127cbc | 12363 | eoinfo.flinfo = &flinfo; |
7686d77d | 12364 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12365 | if (eoinfo.failed) |
12366 | return FALSE; | |
12367 | ||
12368 | /* If backend needs to output some symbols not present in the hash | |
12369 | table, do it now. */ | |
8539e4e8 AM |
12370 | if (bed->elf_backend_output_arch_syms |
12371 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 12372 | { |
6e0b88f1 | 12373 | typedef int (*out_sym_func) |
c152c796 AM |
12374 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12375 | struct elf_link_hash_entry *); | |
12376 | ||
12377 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
12378 | (abfd, info, &flinfo, |
12379 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
12380 | return FALSE; |
12381 | } | |
12382 | ||
ef10c3ac L |
12383 | /* Finalize the .strtab section. */ |
12384 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
12385 | ||
12386 | /* Swap out the .strtab section. */ | |
12387 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
12388 | return FALSE; |
12389 | ||
12390 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
12391 | if (bfd_get_symcount (abfd) > 0) |
12392 | { | |
ee3b52e9 L |
12393 | /* Finish up and write out the symbol string table (.strtab) |
12394 | section. */ | |
ad32986f | 12395 | Elf_Internal_Shdr *symstrtab_hdr = NULL; |
8539e4e8 AM |
12396 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
12397 | ||
ad32986f | 12398 | if (elf_symtab_shndx_list (abfd)) |
8539e4e8 | 12399 | { |
ad32986f | 12400 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8539e4e8 | 12401 | |
ad32986f NC |
12402 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) |
12403 | { | |
12404 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
12405 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
12406 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
12407 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
12408 | symtab_shndx_hdr->sh_size = amt; | |
8539e4e8 | 12409 | |
ad32986f NC |
12410 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, |
12411 | off, TRUE); | |
12412 | ||
12413 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
12414 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
12415 | return FALSE; | |
12416 | } | |
8539e4e8 | 12417 | } |
ee3b52e9 L |
12418 | |
12419 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
12420 | /* sh_name was set in prep_headers. */ | |
12421 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 12422 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 12423 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 12424 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
12425 | symstrtab_hdr->sh_entsize = 0; |
12426 | symstrtab_hdr->sh_link = 0; | |
12427 | symstrtab_hdr->sh_info = 0; | |
12428 | /* sh_offset is set just below. */ | |
12429 | symstrtab_hdr->sh_addralign = 1; | |
12430 | ||
12431 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
12432 | off, TRUE); | |
12433 | elf_next_file_pos (abfd) = off; | |
12434 | ||
c152c796 | 12435 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 12436 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
12437 | return FALSE; |
12438 | } | |
12439 | ||
76359541 TP |
12440 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
12441 | { | |
871b3ab2 | 12442 | _bfd_error_handler (_("%pB: failed to generate import library"), |
4eca0228 | 12443 | info->out_implib_bfd); |
76359541 TP |
12444 | return FALSE; |
12445 | } | |
12446 | ||
c152c796 AM |
12447 | /* Adjust the relocs to have the correct symbol indices. */ |
12448 | for (o = abfd->sections; o != NULL; o = o->next) | |
12449 | { | |
d4730f92 | 12450 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 12451 | bfd_boolean sort; |
10bbbc1d | 12452 | |
c152c796 AM |
12453 | if ((o->flags & SEC_RELOC) == 0) |
12454 | continue; | |
12455 | ||
28dbcedc | 12456 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 12457 | if (esdo->rel.hdr != NULL |
10bbbc1d | 12458 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) |
bca6d0e3 AM |
12459 | return FALSE; |
12460 | if (esdo->rela.hdr != NULL | |
10bbbc1d | 12461 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) |
bca6d0e3 | 12462 | return FALSE; |
c152c796 AM |
12463 | |
12464 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
12465 | trying to swap the relocs out itself. */ | |
12466 | o->reloc_count = 0; | |
12467 | } | |
12468 | ||
12469 | if (dynamic && info->combreloc && dynobj != NULL) | |
12470 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
12471 | ||
12472 | /* If we are linking against a dynamic object, or generating a | |
12473 | shared library, finish up the dynamic linking information. */ | |
12474 | if (dynamic) | |
12475 | { | |
12476 | bfd_byte *dyncon, *dynconend; | |
12477 | ||
12478 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12479 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12480 | BFD_ASSERT (o != NULL); |
12481 | ||
12482 | dyncon = o->contents; | |
eea6121a | 12483 | dynconend = o->contents + o->size; |
c152c796 AM |
12484 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12485 | { | |
12486 | Elf_Internal_Dyn dyn; | |
12487 | const char *name; | |
12488 | unsigned int type; | |
64487780 AM |
12489 | bfd_size_type sh_size; |
12490 | bfd_vma sh_addr; | |
c152c796 AM |
12491 | |
12492 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12493 | ||
12494 | switch (dyn.d_tag) | |
12495 | { | |
12496 | default: | |
12497 | continue; | |
12498 | case DT_NULL: | |
12499 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12500 | { | |
12501 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12502 | { | |
12503 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12504 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12505 | default: continue; | |
12506 | } | |
12507 | dyn.d_un.d_val = relativecount; | |
12508 | relativecount = 0; | |
12509 | break; | |
12510 | } | |
12511 | continue; | |
12512 | ||
12513 | case DT_INIT: | |
12514 | name = info->init_function; | |
12515 | goto get_sym; | |
12516 | case DT_FINI: | |
12517 | name = info->fini_function; | |
12518 | get_sym: | |
12519 | { | |
12520 | struct elf_link_hash_entry *h; | |
12521 | ||
64f52338 | 12522 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12523 | if (h != NULL |
12524 | && (h->root.type == bfd_link_hash_defined | |
12525 | || h->root.type == bfd_link_hash_defweak)) | |
12526 | { | |
bef26483 | 12527 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12528 | o = h->root.u.def.section; |
12529 | if (o->output_section != NULL) | |
bef26483 | 12530 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12531 | + o->output_offset); |
12532 | else | |
12533 | { | |
12534 | /* The symbol is imported from another shared | |
12535 | library and does not apply to this one. */ | |
bef26483 | 12536 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12537 | } |
12538 | break; | |
12539 | } | |
12540 | } | |
12541 | continue; | |
12542 | ||
12543 | case DT_PREINIT_ARRAYSZ: | |
12544 | name = ".preinit_array"; | |
4ade44b7 | 12545 | goto get_out_size; |
c152c796 AM |
12546 | case DT_INIT_ARRAYSZ: |
12547 | name = ".init_array"; | |
4ade44b7 | 12548 | goto get_out_size; |
c152c796 AM |
12549 | case DT_FINI_ARRAYSZ: |
12550 | name = ".fini_array"; | |
4ade44b7 | 12551 | get_out_size: |
c152c796 AM |
12552 | o = bfd_get_section_by_name (abfd, name); |
12553 | if (o == NULL) | |
12554 | { | |
4eca0228 | 12555 | _bfd_error_handler |
4ade44b7 | 12556 | (_("could not find section %s"), name); |
c152c796 AM |
12557 | goto error_return; |
12558 | } | |
eea6121a | 12559 | if (o->size == 0) |
4eca0228 | 12560 | _bfd_error_handler |
c152c796 | 12561 | (_("warning: %s section has zero size"), name); |
eea6121a | 12562 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12563 | break; |
12564 | ||
12565 | case DT_PREINIT_ARRAY: | |
12566 | name = ".preinit_array"; | |
4ade44b7 | 12567 | goto get_out_vma; |
c152c796 AM |
12568 | case DT_INIT_ARRAY: |
12569 | name = ".init_array"; | |
4ade44b7 | 12570 | goto get_out_vma; |
c152c796 AM |
12571 | case DT_FINI_ARRAY: |
12572 | name = ".fini_array"; | |
4ade44b7 AM |
12573 | get_out_vma: |
12574 | o = bfd_get_section_by_name (abfd, name); | |
12575 | goto do_vma; | |
c152c796 AM |
12576 | |
12577 | case DT_HASH: | |
12578 | name = ".hash"; | |
12579 | goto get_vma; | |
fdc90cb4 JJ |
12580 | case DT_GNU_HASH: |
12581 | name = ".gnu.hash"; | |
12582 | goto get_vma; | |
c152c796 AM |
12583 | case DT_STRTAB: |
12584 | name = ".dynstr"; | |
12585 | goto get_vma; | |
12586 | case DT_SYMTAB: | |
12587 | name = ".dynsym"; | |
12588 | goto get_vma; | |
12589 | case DT_VERDEF: | |
12590 | name = ".gnu.version_d"; | |
12591 | goto get_vma; | |
12592 | case DT_VERNEED: | |
12593 | name = ".gnu.version_r"; | |
12594 | goto get_vma; | |
12595 | case DT_VERSYM: | |
12596 | name = ".gnu.version"; | |
12597 | get_vma: | |
4ade44b7 AM |
12598 | o = bfd_get_linker_section (dynobj, name); |
12599 | do_vma: | |
b3293efa | 12600 | if (o == NULL || bfd_is_abs_section (o->output_section)) |
c152c796 | 12601 | { |
4eca0228 | 12602 | _bfd_error_handler |
4ade44b7 | 12603 | (_("could not find section %s"), name); |
c152c796 AM |
12604 | goto error_return; |
12605 | } | |
894891db NC |
12606 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12607 | { | |
4eca0228 | 12608 | _bfd_error_handler |
894891db NC |
12609 | (_("warning: section '%s' is being made into a note"), name); |
12610 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12611 | goto error_return; | |
12612 | } | |
4ade44b7 | 12613 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12614 | break; |
12615 | ||
12616 | case DT_REL: | |
12617 | case DT_RELA: | |
12618 | case DT_RELSZ: | |
12619 | case DT_RELASZ: | |
12620 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12621 | type = SHT_REL; | |
12622 | else | |
12623 | type = SHT_RELA; | |
64487780 AM |
12624 | sh_size = 0; |
12625 | sh_addr = 0; | |
c152c796 AM |
12626 | for (i = 1; i < elf_numsections (abfd); i++) |
12627 | { | |
12628 | Elf_Internal_Shdr *hdr; | |
12629 | ||
12630 | hdr = elf_elfsections (abfd)[i]; | |
12631 | if (hdr->sh_type == type | |
12632 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12633 | { | |
64487780 AM |
12634 | sh_size += hdr->sh_size; |
12635 | if (sh_addr == 0 | |
12636 | || sh_addr > hdr->sh_addr) | |
12637 | sh_addr = hdr->sh_addr; | |
c152c796 AM |
12638 | } |
12639 | } | |
64487780 | 12640 | |
64f52338 AM |
12641 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12642 | { | |
12643 | /* Don't count procedure linkage table relocs in the | |
12644 | overall reloc count. */ | |
64487780 AM |
12645 | sh_size -= htab->srelplt->size; |
12646 | if (sh_size == 0) | |
12647 | /* If the size is zero, make the address zero too. | |
12648 | This is to avoid a glibc bug. If the backend | |
12649 | emits DT_RELA/DT_RELASZ even when DT_RELASZ is | |
12650 | zero, then we'll put DT_RELA at the end of | |
12651 | DT_JMPREL. glibc will interpret the end of | |
12652 | DT_RELA matching the end of DT_JMPREL as the | |
12653 | case where DT_RELA includes DT_JMPREL, and for | |
12654 | LD_BIND_NOW will decide that processing DT_RELA | |
12655 | will process the PLT relocs too. Net result: | |
12656 | No PLT relocs applied. */ | |
12657 | sh_addr = 0; | |
12658 | ||
64f52338 AM |
12659 | /* If .rela.plt is the first .rela section, exclude |
12660 | it from DT_RELA. */ | |
64487780 AM |
12661 | else if (sh_addr == (htab->srelplt->output_section->vma |
12662 | + htab->srelplt->output_offset)) | |
12663 | sh_addr += htab->srelplt->size; | |
64f52338 | 12664 | } |
64487780 AM |
12665 | |
12666 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12667 | dyn.d_un.d_val = sh_size; | |
12668 | else | |
12669 | dyn.d_un.d_ptr = sh_addr; | |
c152c796 AM |
12670 | break; |
12671 | } | |
12672 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12673 | } | |
12674 | } | |
12675 | ||
12676 | /* If we have created any dynamic sections, then output them. */ | |
12677 | if (dynobj != NULL) | |
12678 | { | |
12679 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12680 | goto error_return; | |
12681 | ||
943284cc | 12682 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12683 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12684 | || info->error_textrel) |
3d4d4302 | 12685 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12686 | { |
12687 | bfd_byte *dyncon, *dynconend; | |
12688 | ||
943284cc DJ |
12689 | dyncon = o->contents; |
12690 | dynconend = o->contents + o->size; | |
12691 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12692 | { | |
12693 | Elf_Internal_Dyn dyn; | |
12694 | ||
12695 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12696 | ||
12697 | if (dyn.d_tag == DT_TEXTREL) | |
12698 | { | |
c192a133 AM |
12699 | if (info->error_textrel) |
12700 | info->callbacks->einfo | |
9793eb77 | 12701 | (_("%P%X: read-only segment has dynamic relocations\n")); |
c192a133 AM |
12702 | else |
12703 | info->callbacks->einfo | |
9793eb77 | 12704 | (_("%P: warning: creating a DT_TEXTREL in a shared object\n")); |
943284cc DJ |
12705 | break; |
12706 | } | |
12707 | } | |
12708 | } | |
12709 | ||
c152c796 AM |
12710 | for (o = dynobj->sections; o != NULL; o = o->next) |
12711 | { | |
12712 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12713 | || o->size == 0 |
c152c796 AM |
12714 | || o->output_section == bfd_abs_section_ptr) |
12715 | continue; | |
12716 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12717 | { | |
12718 | /* At this point, we are only interested in sections | |
12719 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12720 | continue; | |
12721 | } | |
64f52338 | 12722 | if (htab->stab_info.stabstr == o) |
3722b82f | 12723 | continue; |
64f52338 | 12724 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12725 | continue; |
3d4d4302 | 12726 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12727 | { |
12728 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12729 | o->contents, | |
37b01f6a DG |
12730 | (file_ptr) o->output_offset |
12731 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12732 | o->size)) |
c152c796 AM |
12733 | goto error_return; |
12734 | } | |
12735 | else | |
12736 | { | |
12737 | /* The contents of the .dynstr section are actually in a | |
12738 | stringtab. */ | |
8539e4e8 AM |
12739 | file_ptr off; |
12740 | ||
c152c796 AM |
12741 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12742 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12743 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12744 | goto error_return; |
12745 | } | |
12746 | } | |
12747 | } | |
12748 | ||
7bdf4127 | 12749 | if (!info->resolve_section_groups) |
c152c796 AM |
12750 | { |
12751 | bfd_boolean failed = FALSE; | |
12752 | ||
7bdf4127 | 12753 | BFD_ASSERT (bfd_link_relocatable (info)); |
c152c796 AM |
12754 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
12755 | if (failed) | |
12756 | goto error_return; | |
12757 | } | |
12758 | ||
12759 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 12760 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 12761 | { |
64f52338 | 12762 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
12763 | goto error_return; |
12764 | } | |
12765 | ||
9f7c3e5e AM |
12766 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12767 | goto error_return; | |
c152c796 | 12768 | |
9f7c3e5e | 12769 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12770 | |
12bd6957 | 12771 | elf_linker (abfd) = TRUE; |
c152c796 | 12772 | |
104d59d1 JM |
12773 | if (attr_section) |
12774 | { | |
a50b1753 | 12775 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12776 | if (contents == NULL) |
d0f16d5e | 12777 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12778 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12779 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12780 | free (contents); | |
12781 | } | |
12782 | ||
c152c796 AM |
12783 | return TRUE; |
12784 | ||
12785 | error_return: | |
9f7c3e5e | 12786 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12787 | return FALSE; |
12788 | } | |
12789 | \f | |
5241d853 RS |
12790 | /* Initialize COOKIE for input bfd ABFD. */ |
12791 | ||
12792 | static bfd_boolean | |
12793 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12794 | struct bfd_link_info *info, bfd *abfd) | |
12795 | { | |
12796 | Elf_Internal_Shdr *symtab_hdr; | |
12797 | const struct elf_backend_data *bed; | |
12798 | ||
12799 | bed = get_elf_backend_data (abfd); | |
12800 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12801 | ||
12802 | cookie->abfd = abfd; | |
12803 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12804 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12805 | if (cookie->bad_symtab) | |
12806 | { | |
12807 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12808 | cookie->extsymoff = 0; | |
12809 | } | |
12810 | else | |
12811 | { | |
12812 | cookie->locsymcount = symtab_hdr->sh_info; | |
12813 | cookie->extsymoff = symtab_hdr->sh_info; | |
12814 | } | |
12815 | ||
12816 | if (bed->s->arch_size == 32) | |
12817 | cookie->r_sym_shift = 8; | |
12818 | else | |
12819 | cookie->r_sym_shift = 32; | |
12820 | ||
12821 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12822 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12823 | { | |
12824 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12825 | cookie->locsymcount, 0, | |
12826 | NULL, NULL, NULL); | |
12827 | if (cookie->locsyms == NULL) | |
12828 | { | |
12829 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12830 | return FALSE; | |
12831 | } | |
12832 | if (info->keep_memory) | |
12833 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12834 | } | |
12835 | return TRUE; | |
12836 | } | |
12837 | ||
12838 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12839 | ||
12840 | static void | |
12841 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12842 | { | |
12843 | Elf_Internal_Shdr *symtab_hdr; | |
12844 | ||
12845 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12846 | if (cookie->locsyms != NULL | |
12847 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12848 | free (cookie->locsyms); | |
12849 | } | |
12850 | ||
12851 | /* Initialize the relocation information in COOKIE for input section SEC | |
12852 | of input bfd ABFD. */ | |
12853 | ||
12854 | static bfd_boolean | |
12855 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12856 | struct bfd_link_info *info, bfd *abfd, | |
12857 | asection *sec) | |
12858 | { | |
5241d853 RS |
12859 | if (sec->reloc_count == 0) |
12860 | { | |
12861 | cookie->rels = NULL; | |
12862 | cookie->relend = NULL; | |
12863 | } | |
12864 | else | |
12865 | { | |
5241d853 RS |
12866 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, |
12867 | info->keep_memory); | |
12868 | if (cookie->rels == NULL) | |
12869 | return FALSE; | |
12870 | cookie->rel = cookie->rels; | |
056bafd4 | 12871 | cookie->relend = cookie->rels + sec->reloc_count; |
5241d853 RS |
12872 | } |
12873 | cookie->rel = cookie->rels; | |
12874 | return TRUE; | |
12875 | } | |
12876 | ||
12877 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12878 | if appropriate. */ | |
12879 | ||
12880 | static void | |
12881 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12882 | asection *sec) | |
12883 | { | |
12884 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12885 | free (cookie->rels); | |
12886 | } | |
12887 | ||
12888 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12889 | ||
12890 | static bfd_boolean | |
12891 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12892 | struct bfd_link_info *info, | |
12893 | asection *sec) | |
12894 | { | |
12895 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12896 | goto error1; | |
12897 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12898 | goto error2; | |
12899 | return TRUE; | |
12900 | ||
12901 | error2: | |
12902 | fini_reloc_cookie (cookie, sec->owner); | |
12903 | error1: | |
12904 | return FALSE; | |
12905 | } | |
12906 | ||
12907 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12908 | if appropriate. */ | |
12909 | ||
12910 | static void | |
12911 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12912 | asection *sec) | |
12913 | { | |
12914 | fini_reloc_cookie_rels (cookie, sec); | |
12915 | fini_reloc_cookie (cookie, sec->owner); | |
12916 | } | |
12917 | \f | |
c152c796 AM |
12918 | /* Garbage collect unused sections. */ |
12919 | ||
07adf181 AM |
12920 | /* Default gc_mark_hook. */ |
12921 | ||
12922 | asection * | |
12923 | _bfd_elf_gc_mark_hook (asection *sec, | |
12924 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12925 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12926 | struct elf_link_hash_entry *h, | |
12927 | Elf_Internal_Sym *sym) | |
12928 | { | |
12929 | if (h != NULL) | |
12930 | { | |
12931 | switch (h->root.type) | |
12932 | { | |
12933 | case bfd_link_hash_defined: | |
12934 | case bfd_link_hash_defweak: | |
12935 | return h->root.u.def.section; | |
12936 | ||
12937 | case bfd_link_hash_common: | |
12938 | return h->root.u.c.p->section; | |
12939 | ||
12940 | default: | |
12941 | break; | |
12942 | } | |
12943 | } | |
12944 | else | |
12945 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12946 | ||
12947 | return NULL; | |
12948 | } | |
12949 | ||
9e223787 | 12950 | /* Return the debug definition section. */ |
b7c871ed L |
12951 | |
12952 | static asection * | |
12953 | elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, | |
12954 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12955 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12956 | struct elf_link_hash_entry *h, | |
9e223787 | 12957 | Elf_Internal_Sym *sym) |
b7c871ed | 12958 | { |
9e223787 L |
12959 | if (h != NULL) |
12960 | { | |
12961 | /* Return the global debug definition section. */ | |
12962 | if ((h->root.type == bfd_link_hash_defined | |
12963 | || h->root.type == bfd_link_hash_defweak) | |
12964 | && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) | |
12965 | return h->root.u.def.section; | |
12966 | } | |
12967 | else | |
12968 | { | |
12969 | /* Return the local debug definition section. */ | |
12970 | asection *isec = bfd_section_from_elf_index (sec->owner, | |
12971 | sym->st_shndx); | |
12972 | if ((isec->flags & SEC_DEBUGGING) != 0) | |
12973 | return isec; | |
12974 | } | |
b7c871ed L |
12975 | |
12976 | return NULL; | |
12977 | } | |
12978 | ||
5241d853 RS |
12979 | /* COOKIE->rel describes a relocation against section SEC, which is |
12980 | a section we've decided to keep. Return the section that contains | |
12981 | the relocation symbol, or NULL if no section contains it. */ | |
12982 | ||
12983 | asection * | |
12984 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
12985 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
12986 | struct elf_reloc_cookie *cookie, |
12987 | bfd_boolean *start_stop) | |
5241d853 RS |
12988 | { |
12989 | unsigned long r_symndx; | |
12990 | struct elf_link_hash_entry *h; | |
12991 | ||
12992 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 12993 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
12994 | return NULL; |
12995 | ||
12996 | if (r_symndx >= cookie->locsymcount | |
12997 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
12998 | { | |
12999 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
13000 | if (h == NULL) |
13001 | { | |
871b3ab2 | 13002 | info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"), |
263ddf68 L |
13003 | sec->owner); |
13004 | return NULL; | |
13005 | } | |
5241d853 RS |
13006 | while (h->root.type == bfd_link_hash_indirect |
13007 | || h->root.type == bfd_link_hash_warning) | |
13008 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 13009 | h->mark = 1; |
4e6b54a6 AM |
13010 | /* If this symbol is weak and there is a non-weak definition, we |
13011 | keep the non-weak definition because many backends put | |
13012 | dynamic reloc info on the non-weak definition for code | |
13013 | handling copy relocs. */ | |
60d67dc8 AM |
13014 | if (h->is_weakalias) |
13015 | weakdef (h)->mark = 1; | |
1cce69b9 | 13016 | |
a6a4679f | 13017 | if (start_stop != NULL) |
1cce69b9 | 13018 | { |
7dba9362 AM |
13019 | /* To work around a glibc bug, mark XXX input sections |
13020 | when there is a reference to __start_XXX or __stop_XXX | |
13021 | symbols. */ | |
cbd0eecf | 13022 | if (h->start_stop) |
1cce69b9 | 13023 | { |
cbd0eecf | 13024 | asection *s = h->u2.start_stop_section; |
a6a4679f AM |
13025 | *start_stop = !s->gc_mark; |
13026 | return s; | |
1cce69b9 AM |
13027 | } |
13028 | } | |
13029 | ||
5241d853 RS |
13030 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
13031 | } | |
13032 | ||
13033 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
13034 | &cookie->locsyms[r_symndx]); | |
13035 | } | |
13036 | ||
13037 | /* COOKIE->rel describes a relocation against section SEC, which is | |
13038 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 13039 | the relocation symbol. */ |
5241d853 RS |
13040 | |
13041 | bfd_boolean | |
13042 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
13043 | asection *sec, | |
13044 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 13045 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
13046 | { |
13047 | asection *rsec; | |
1cce69b9 | 13048 | bfd_boolean start_stop = FALSE; |
5241d853 | 13049 | |
1cce69b9 AM |
13050 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
13051 | while (rsec != NULL) | |
5241d853 | 13052 | { |
1cce69b9 AM |
13053 | if (!rsec->gc_mark) |
13054 | { | |
13055 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
13056 | || (rsec->owner->flags & DYNAMIC) != 0) | |
13057 | rsec->gc_mark = 1; | |
13058 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
13059 | return FALSE; | |
13060 | } | |
13061 | if (!start_stop) | |
13062 | break; | |
199af150 | 13063 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
13064 | } |
13065 | return TRUE; | |
13066 | } | |
13067 | ||
07adf181 AM |
13068 | /* The mark phase of garbage collection. For a given section, mark |
13069 | it and any sections in this section's group, and all the sections | |
13070 | which define symbols to which it refers. */ | |
13071 | ||
ccfa59ea AM |
13072 | bfd_boolean |
13073 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
13074 | asection *sec, | |
6a5bb875 | 13075 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
13076 | { |
13077 | bfd_boolean ret; | |
9d0a14d3 | 13078 | asection *group_sec, *eh_frame; |
c152c796 AM |
13079 | |
13080 | sec->gc_mark = 1; | |
13081 | ||
13082 | /* Mark all the sections in the group. */ | |
13083 | group_sec = elf_section_data (sec)->next_in_group; | |
13084 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 13085 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
13086 | return FALSE; |
13087 | ||
13088 | /* Look through the section relocs. */ | |
13089 | ret = TRUE; | |
9d0a14d3 RS |
13090 | eh_frame = elf_eh_frame_section (sec->owner); |
13091 | if ((sec->flags & SEC_RELOC) != 0 | |
13092 | && sec->reloc_count > 0 | |
13093 | && sec != eh_frame) | |
c152c796 | 13094 | { |
5241d853 | 13095 | struct elf_reloc_cookie cookie; |
c152c796 | 13096 | |
5241d853 RS |
13097 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
13098 | ret = FALSE; | |
c152c796 | 13099 | else |
c152c796 | 13100 | { |
5241d853 | 13101 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 13102 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
13103 | { |
13104 | ret = FALSE; | |
13105 | break; | |
13106 | } | |
13107 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
13108 | } |
13109 | } | |
9d0a14d3 RS |
13110 | |
13111 | if (ret && eh_frame && elf_fde_list (sec)) | |
13112 | { | |
13113 | struct elf_reloc_cookie cookie; | |
13114 | ||
13115 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
13116 | ret = FALSE; | |
13117 | else | |
13118 | { | |
13119 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
13120 | gc_mark_hook, &cookie)) | |
13121 | ret = FALSE; | |
13122 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
13123 | } | |
13124 | } | |
13125 | ||
2f0c68f2 CM |
13126 | eh_frame = elf_section_eh_frame_entry (sec); |
13127 | if (ret && eh_frame && !eh_frame->gc_mark) | |
13128 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
13129 | ret = FALSE; | |
13130 | ||
c152c796 AM |
13131 | return ret; |
13132 | } | |
13133 | ||
3c758495 TG |
13134 | /* Scan and mark sections in a special or debug section group. */ |
13135 | ||
13136 | static void | |
13137 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
13138 | { | |
13139 | /* Point to first section of section group. */ | |
13140 | asection *ssec; | |
13141 | /* Used to iterate the section group. */ | |
13142 | asection *msec; | |
13143 | ||
13144 | bfd_boolean is_special_grp = TRUE; | |
13145 | bfd_boolean is_debug_grp = TRUE; | |
13146 | ||
13147 | /* First scan to see if group contains any section other than debug | |
13148 | and special section. */ | |
13149 | ssec = msec = elf_next_in_group (grp); | |
13150 | do | |
13151 | { | |
13152 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
13153 | is_debug_grp = FALSE; | |
13154 | ||
13155 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
13156 | is_special_grp = FALSE; | |
13157 | ||
13158 | msec = elf_next_in_group (msec); | |
13159 | } | |
13160 | while (msec != ssec); | |
13161 | ||
13162 | /* If this is a pure debug section group or pure special section group, | |
13163 | keep all sections in this group. */ | |
13164 | if (is_debug_grp || is_special_grp) | |
13165 | { | |
13166 | do | |
13167 | { | |
13168 | msec->gc_mark = 1; | |
13169 | msec = elf_next_in_group (msec); | |
13170 | } | |
13171 | while (msec != ssec); | |
13172 | } | |
13173 | } | |
13174 | ||
7f6ab9f8 AM |
13175 | /* Keep debug and special sections. */ |
13176 | ||
13177 | bfd_boolean | |
13178 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
13179 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
13180 | { | |
13181 | bfd *ibfd; | |
13182 | ||
c72f2fb2 | 13183 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
13184 | { |
13185 | asection *isec; | |
13186 | bfd_boolean some_kept; | |
b40bf0a2 | 13187 | bfd_boolean debug_frag_seen; |
b7c871ed | 13188 | bfd_boolean has_kept_debug_info; |
7f6ab9f8 AM |
13189 | |
13190 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13191 | continue; | |
57963c05 AM |
13192 | isec = ibfd->sections; |
13193 | if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13194 | continue; | |
7f6ab9f8 | 13195 | |
b40bf0a2 NC |
13196 | /* Ensure all linker created sections are kept, |
13197 | see if any other section is already marked, | |
13198 | and note if we have any fragmented debug sections. */ | |
b7c871ed | 13199 | debug_frag_seen = some_kept = has_kept_debug_info = FALSE; |
7f6ab9f8 AM |
13200 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
13201 | { | |
13202 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
13203 | isec->gc_mark = 1; | |
eb026f09 AM |
13204 | else if (isec->gc_mark |
13205 | && (isec->flags & SEC_ALLOC) != 0 | |
13206 | && elf_section_type (isec) != SHT_NOTE) | |
7f6ab9f8 | 13207 | some_kept = TRUE; |
b40bf0a2 | 13208 | |
535b785f | 13209 | if (!debug_frag_seen |
b40bf0a2 NC |
13210 | && (isec->flags & SEC_DEBUGGING) |
13211 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
13212 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
13213 | } |
13214 | ||
eb026f09 AM |
13215 | /* If no non-note alloc section in this file will be kept, then |
13216 | we can toss out the debug and special sections. */ | |
7f6ab9f8 AM |
13217 | if (!some_kept) |
13218 | continue; | |
13219 | ||
13220 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
13221 | not part of a group. Also keep section groups that contain |
13222 | just debug sections or special sections. */ | |
7f6ab9f8 | 13223 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
13224 | { |
13225 | if ((isec->flags & SEC_GROUP) != 0) | |
13226 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
13227 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
13228 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
13229 | && elf_next_in_group (isec) == NULL) | |
13230 | isec->gc_mark = 1; | |
b7c871ed L |
13231 | if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) |
13232 | has_kept_debug_info = TRUE; | |
3c758495 | 13233 | } |
b40bf0a2 | 13234 | |
b40bf0a2 NC |
13235 | /* Look for CODE sections which are going to be discarded, |
13236 | and find and discard any fragmented debug sections which | |
13237 | are associated with that code section. */ | |
b7c871ed L |
13238 | if (debug_frag_seen) |
13239 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13240 | if ((isec->flags & SEC_CODE) != 0 | |
13241 | && isec->gc_mark == 0) | |
13242 | { | |
13243 | unsigned int ilen; | |
13244 | asection *dsec; | |
b40bf0a2 | 13245 | |
b7c871ed | 13246 | ilen = strlen (isec->name); |
b40bf0a2 | 13247 | |
b7c871ed | 13248 | /* Association is determined by the name of the debug |
07d6d2b8 | 13249 | section containing the name of the code section as |
b7c871ed L |
13250 | a suffix. For example .debug_line.text.foo is a |
13251 | debug section associated with .text.foo. */ | |
13252 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
13253 | { | |
13254 | unsigned int dlen; | |
b40bf0a2 | 13255 | |
b7c871ed L |
13256 | if (dsec->gc_mark == 0 |
13257 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
13258 | continue; | |
b40bf0a2 | 13259 | |
b7c871ed | 13260 | dlen = strlen (dsec->name); |
b40bf0a2 | 13261 | |
b7c871ed L |
13262 | if (dlen > ilen |
13263 | && strncmp (dsec->name + (dlen - ilen), | |
13264 | isec->name, ilen) == 0) | |
b40bf0a2 | 13265 | dsec->gc_mark = 0; |
b7c871ed | 13266 | } |
b40bf0a2 | 13267 | } |
b7c871ed L |
13268 | |
13269 | /* Mark debug sections referenced by kept debug sections. */ | |
13270 | if (has_kept_debug_info) | |
13271 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13272 | if (isec->gc_mark | |
13273 | && (isec->flags & SEC_DEBUGGING) != 0) | |
13274 | if (!_bfd_elf_gc_mark (info, isec, | |
13275 | elf_gc_mark_debug_section)) | |
13276 | return FALSE; | |
7f6ab9f8 AM |
13277 | } |
13278 | return TRUE; | |
13279 | } | |
13280 | ||
c152c796 | 13281 | static bfd_boolean |
ccabcbe5 | 13282 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
13283 | { |
13284 | bfd *sub; | |
ccabcbe5 | 13285 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 13286 | |
c72f2fb2 | 13287 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13288 | { |
13289 | asection *o; | |
13290 | ||
b19a8f85 | 13291 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13292 | || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) |
b19a8f85 | 13293 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 | 13294 | continue; |
57963c05 AM |
13295 | o = sub->sections; |
13296 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13297 | continue; | |
c152c796 AM |
13298 | |
13299 | for (o = sub->sections; o != NULL; o = o->next) | |
13300 | { | |
a33dafc3 L |
13301 | /* When any section in a section group is kept, we keep all |
13302 | sections in the section group. If the first member of | |
13303 | the section group is excluded, we will also exclude the | |
13304 | group section. */ | |
13305 | if (o->flags & SEC_GROUP) | |
13306 | { | |
13307 | asection *first = elf_next_in_group (o); | |
13308 | o->gc_mark = first->gc_mark; | |
13309 | } | |
c152c796 | 13310 | |
1e7eae0d | 13311 | if (o->gc_mark) |
c152c796 AM |
13312 | continue; |
13313 | ||
13314 | /* Skip sweeping sections already excluded. */ | |
13315 | if (o->flags & SEC_EXCLUDE) | |
13316 | continue; | |
13317 | ||
13318 | /* Since this is early in the link process, it is simple | |
13319 | to remove a section from the output. */ | |
13320 | o->flags |= SEC_EXCLUDE; | |
13321 | ||
c55fe096 | 13322 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 13323 | /* xgettext:c-format */ |
9793eb77 | 13324 | _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), |
c08bb8dd | 13325 | o, sub); |
c152c796 AM |
13326 | } |
13327 | } | |
13328 | ||
c152c796 AM |
13329 | return TRUE; |
13330 | } | |
13331 | ||
13332 | /* Propagate collected vtable information. This is called through | |
13333 | elf_link_hash_traverse. */ | |
13334 | ||
13335 | static bfd_boolean | |
13336 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
13337 | { | |
c152c796 | 13338 | /* Those that are not vtables. */ |
cbd0eecf L |
13339 | if (h->start_stop |
13340 | || h->u2.vtable == NULL | |
13341 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13342 | return TRUE; |
13343 | ||
13344 | /* Those vtables that do not have parents, we cannot merge. */ | |
cbd0eecf | 13345 | if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
13346 | return TRUE; |
13347 | ||
13348 | /* If we've already been done, exit. */ | |
cbd0eecf | 13349 | if (h->u2.vtable->used && h->u2.vtable->used[-1]) |
c152c796 AM |
13350 | return TRUE; |
13351 | ||
13352 | /* Make sure the parent's table is up to date. */ | |
cbd0eecf | 13353 | elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); |
c152c796 | 13354 | |
cbd0eecf | 13355 | if (h->u2.vtable->used == NULL) |
c152c796 AM |
13356 | { |
13357 | /* None of this table's entries were referenced. Re-use the | |
13358 | parent's table. */ | |
cbd0eecf L |
13359 | h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; |
13360 | h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; | |
c152c796 AM |
13361 | } |
13362 | else | |
13363 | { | |
13364 | size_t n; | |
13365 | bfd_boolean *cu, *pu; | |
13366 | ||
13367 | /* Or the parent's entries into ours. */ | |
cbd0eecf | 13368 | cu = h->u2.vtable->used; |
c152c796 | 13369 | cu[-1] = TRUE; |
cbd0eecf | 13370 | pu = h->u2.vtable->parent->u2.vtable->used; |
c152c796 AM |
13371 | if (pu != NULL) |
13372 | { | |
13373 | const struct elf_backend_data *bed; | |
13374 | unsigned int log_file_align; | |
13375 | ||
13376 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
13377 | log_file_align = bed->s->log_file_align; | |
cbd0eecf | 13378 | n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; |
c152c796 AM |
13379 | while (n--) |
13380 | { | |
13381 | if (*pu) | |
13382 | *cu = TRUE; | |
13383 | pu++; | |
13384 | cu++; | |
13385 | } | |
13386 | } | |
13387 | } | |
13388 | ||
13389 | return TRUE; | |
13390 | } | |
13391 | ||
13392 | static bfd_boolean | |
13393 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
13394 | { | |
13395 | asection *sec; | |
13396 | bfd_vma hstart, hend; | |
13397 | Elf_Internal_Rela *relstart, *relend, *rel; | |
13398 | const struct elf_backend_data *bed; | |
13399 | unsigned int log_file_align; | |
13400 | ||
c152c796 AM |
13401 | /* Take care of both those symbols that do not describe vtables as |
13402 | well as those that are not loaded. */ | |
cbd0eecf L |
13403 | if (h->start_stop |
13404 | || h->u2.vtable == NULL | |
13405 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13406 | return TRUE; |
13407 | ||
13408 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13409 | || h->root.type == bfd_link_hash_defweak); | |
13410 | ||
13411 | sec = h->root.u.def.section; | |
13412 | hstart = h->root.u.def.value; | |
13413 | hend = hstart + h->size; | |
13414 | ||
13415 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13416 | if (!relstart) | |
13417 | return *(bfd_boolean *) okp = FALSE; | |
13418 | bed = get_elf_backend_data (sec->owner); | |
13419 | log_file_align = bed->s->log_file_align; | |
13420 | ||
056bafd4 | 13421 | relend = relstart + sec->reloc_count; |
c152c796 AM |
13422 | |
13423 | for (rel = relstart; rel < relend; ++rel) | |
13424 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13425 | { | |
13426 | /* If the entry is in use, do nothing. */ | |
cbd0eecf L |
13427 | if (h->u2.vtable->used |
13428 | && (rel->r_offset - hstart) < h->u2.vtable->size) | |
c152c796 AM |
13429 | { |
13430 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
cbd0eecf | 13431 | if (h->u2.vtable->used[entry]) |
c152c796 AM |
13432 | continue; |
13433 | } | |
13434 | /* Otherwise, kill it. */ | |
13435 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13436 | } | |
13437 | ||
13438 | return TRUE; | |
13439 | } | |
13440 | ||
87538722 AM |
13441 | /* Mark sections containing dynamically referenced symbols. When |
13442 | building shared libraries, we must assume that any visible symbol is | |
13443 | referenced. */ | |
715df9b8 | 13444 | |
64d03ab5 AM |
13445 | bfd_boolean |
13446 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13447 | { |
87538722 | 13448 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13449 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13450 | |
715df9b8 EB |
13451 | if ((h->root.type == bfd_link_hash_defined |
13452 | || h->root.type == bfd_link_hash_defweak) | |
d664fd41 | 13453 | && ((h->ref_dynamic && !h->forced_local) |
c4621b33 | 13454 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13455 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13456 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13457 | && (!bfd_link_executable (info) |
22185505 | 13458 | || info->gc_keep_exported |
b407645f AM |
13459 | || info->export_dynamic |
13460 | || (h->dynamic | |
13461 | && d != NULL | |
13462 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13463 | && (h->versioned >= versioned |
54e8959c L |
13464 | || !bfd_hide_sym_by_version (info->version_info, |
13465 | h->root.root.string))))) | |
715df9b8 EB |
13466 | h->root.u.def.section->flags |= SEC_KEEP; |
13467 | ||
13468 | return TRUE; | |
13469 | } | |
3b36f7e6 | 13470 | |
74f0fb50 AM |
13471 | /* Keep all sections containing symbols undefined on the command-line, |
13472 | and the section containing the entry symbol. */ | |
13473 | ||
13474 | void | |
13475 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13476 | { | |
13477 | struct bfd_sym_chain *sym; | |
13478 | ||
13479 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13480 | { | |
13481 | struct elf_link_hash_entry *h; | |
13482 | ||
13483 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13484 | FALSE, FALSE, FALSE); | |
13485 | ||
13486 | if (h != NULL | |
13487 | && (h->root.type == bfd_link_hash_defined | |
13488 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13489 | && !bfd_is_abs_section (h->root.u.def.section) |
13490 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13491 | h->root.u.def.section->flags |= SEC_KEEP; |
13492 | } | |
13493 | } | |
13494 | ||
2f0c68f2 CM |
13495 | bfd_boolean |
13496 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13497 | struct bfd_link_info *info) | |
13498 | { | |
13499 | bfd *ibfd = info->input_bfds; | |
13500 | ||
13501 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13502 | { | |
13503 | asection *sec; | |
13504 | struct elf_reloc_cookie cookie; | |
13505 | ||
13506 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13507 | continue; | |
57963c05 AM |
13508 | sec = ibfd->sections; |
13509 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13510 | continue; | |
2f0c68f2 CM |
13511 | |
13512 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13513 | return FALSE; | |
13514 | ||
13515 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13516 | { | |
13517 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13518 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13519 | { | |
13520 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13521 | fini_reloc_cookie_rels (&cookie, sec); | |
13522 | } | |
13523 | } | |
13524 | } | |
13525 | return TRUE; | |
13526 | } | |
13527 | ||
c152c796 AM |
13528 | /* Do mark and sweep of unused sections. */ |
13529 | ||
13530 | bfd_boolean | |
13531 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13532 | { | |
13533 | bfd_boolean ok = TRUE; | |
13534 | bfd *sub; | |
6a5bb875 | 13535 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13536 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13537 | struct elf_link_hash_table *htab; |
c152c796 | 13538 | |
64d03ab5 | 13539 | if (!bed->can_gc_sections |
715df9b8 | 13540 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13541 | { |
9793eb77 | 13542 | _bfd_error_handler(_("warning: gc-sections option ignored")); |
c152c796 AM |
13543 | return TRUE; |
13544 | } | |
13545 | ||
74f0fb50 | 13546 | bed->gc_keep (info); |
da44f4e5 | 13547 | htab = elf_hash_table (info); |
74f0fb50 | 13548 | |
9d0a14d3 RS |
13549 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13550 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13551 | for (sub = info->input_bfds; |
13552 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13553 | sub = sub->link.next) | |
9d0a14d3 RS |
13554 | { |
13555 | asection *sec; | |
13556 | struct elf_reloc_cookie cookie; | |
13557 | ||
57963c05 AM |
13558 | sec = sub->sections; |
13559 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13560 | continue; | |
9d0a14d3 | 13561 | sec = bfd_get_section_by_name (sub, ".eh_frame"); |
9a2a56cc | 13562 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13563 | { |
13564 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13565 | if (elf_section_data (sec)->sec_info |
13566 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13567 | elf_eh_frame_section (sub) = sec; |
13568 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13569 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13570 | } |
13571 | } | |
9d0a14d3 | 13572 | |
c152c796 | 13573 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13574 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13575 | if (!ok) |
13576 | return FALSE; | |
13577 | ||
13578 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13579 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13580 | if (!ok) |
13581 | return FALSE; | |
13582 | ||
715df9b8 | 13583 | /* Mark dynamically referenced symbols. */ |
22185505 | 13584 | if (htab->dynamic_sections_created || info->gc_keep_exported) |
da44f4e5 | 13585 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); |
c152c796 | 13586 | |
715df9b8 | 13587 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13588 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13589 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13590 | { |
13591 | asection *o; | |
13592 | ||
b19a8f85 | 13593 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13594 | || elf_object_id (sub) != elf_hash_table_id (htab) |
b19a8f85 | 13595 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 AM |
13596 | continue; |
13597 | ||
57963c05 AM |
13598 | o = sub->sections; |
13599 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13600 | continue; | |
13601 | ||
7f6ab9f8 AM |
13602 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13603 | Also treat note sections as a root, if the section is not part | |
8b6f4cd3 L |
13604 | of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as |
13605 | well as FINI_ARRAY sections for ld -r. */ | |
c152c796 | 13606 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13607 | if (!o->gc_mark |
13608 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13609 | && ((o->flags & SEC_KEEP) != 0 |
8b6f4cd3 L |
13610 | || (bfd_link_relocatable (info) |
13611 | && ((elf_section_data (o)->this_hdr.sh_type | |
13612 | == SHT_PREINIT_ARRAY) | |
13613 | || (elf_section_data (o)->this_hdr.sh_type | |
13614 | == SHT_INIT_ARRAY) | |
13615 | || (elf_section_data (o)->this_hdr.sh_type | |
13616 | == SHT_FINI_ARRAY))) | |
7f6ab9f8 AM |
13617 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13618 | && elf_next_in_group (o) == NULL ))) | |
13619 | { | |
13620 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13621 | return FALSE; | |
13622 | } | |
c152c796 AM |
13623 | } |
13624 | ||
6a5bb875 | 13625 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13626 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13627 | |
c152c796 | 13628 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13629 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13630 | } |
13631 | \f | |
13632 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13633 | ||
13634 | bfd_boolean | |
13635 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13636 | asection *sec, | |
13637 | struct elf_link_hash_entry *h, | |
13638 | bfd_vma offset) | |
13639 | { | |
13640 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13641 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13642 | size_t extsymcount; |
c152c796 AM |
13643 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13644 | ||
13645 | /* The sh_info field of the symtab header tells us where the | |
13646 | external symbols start. We don't care about the local symbols at | |
13647 | this point. */ | |
13648 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13649 | if (!elf_bad_symtab (abfd)) | |
13650 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13651 | ||
13652 | sym_hashes = elf_sym_hashes (abfd); | |
13653 | sym_hashes_end = sym_hashes + extsymcount; | |
13654 | ||
13655 | /* Hunt down the child symbol, which is in this section at the same | |
13656 | offset as the relocation. */ | |
13657 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13658 | { | |
13659 | if ((child = *search) != NULL | |
13660 | && (child->root.type == bfd_link_hash_defined | |
13661 | || child->root.type == bfd_link_hash_defweak) | |
13662 | && child->root.u.def.section == sec | |
13663 | && child->root.u.def.value == offset) | |
13664 | goto win; | |
13665 | } | |
13666 | ||
695344c0 | 13667 | /* xgettext:c-format */ |
9793eb77 | 13668 | _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"), |
2dcf00ce | 13669 | abfd, sec, (uint64_t) offset); |
c152c796 AM |
13670 | bfd_set_error (bfd_error_invalid_operation); |
13671 | return FALSE; | |
13672 | ||
13673 | win: | |
cbd0eecf | 13674 | if (!child->u2.vtable) |
f6e332e6 | 13675 | { |
cbd0eecf L |
13676 | child->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13677 | bfd_zalloc (abfd, sizeof (*child->u2.vtable))); | |
13678 | if (!child->u2.vtable) | |
f6e332e6 AM |
13679 | return FALSE; |
13680 | } | |
c152c796 AM |
13681 | if (!h) |
13682 | { | |
13683 | /* This *should* only be the absolute section. It could potentially | |
13684 | be that someone has defined a non-global vtable though, which | |
13685 | would be bad. It isn't worth paging in the local symbols to be | |
13686 | sure though; that case should simply be handled by the assembler. */ | |
13687 | ||
cbd0eecf | 13688 | child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13689 | } |
13690 | else | |
cbd0eecf | 13691 | child->u2.vtable->parent = h; |
c152c796 AM |
13692 | |
13693 | return TRUE; | |
13694 | } | |
13695 | ||
13696 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13697 | ||
13698 | bfd_boolean | |
13699 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
13700 | asection *sec ATTRIBUTE_UNUSED, | |
13701 | struct elf_link_hash_entry *h, | |
13702 | bfd_vma addend) | |
13703 | { | |
13704 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13705 | unsigned int log_file_align = bed->s->log_file_align; | |
13706 | ||
cbd0eecf | 13707 | if (!h->u2.vtable) |
f6e332e6 | 13708 | { |
cbd0eecf L |
13709 | h->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13710 | bfd_zalloc (abfd, sizeof (*h->u2.vtable))); | |
13711 | if (!h->u2.vtable) | |
f6e332e6 AM |
13712 | return FALSE; |
13713 | } | |
13714 | ||
cbd0eecf | 13715 | if (addend >= h->u2.vtable->size) |
c152c796 AM |
13716 | { |
13717 | size_t size, bytes, file_align; | |
cbd0eecf | 13718 | bfd_boolean *ptr = h->u2.vtable->used; |
c152c796 AM |
13719 | |
13720 | /* While the symbol is undefined, we have to be prepared to handle | |
13721 | a zero size. */ | |
13722 | file_align = 1 << log_file_align; | |
13723 | if (h->root.type == bfd_link_hash_undefined) | |
13724 | size = addend + file_align; | |
13725 | else | |
13726 | { | |
13727 | size = h->size; | |
13728 | if (addend >= size) | |
13729 | { | |
13730 | /* Oops! We've got a reference past the defined end of | |
13731 | the table. This is probably a bug -- shall we warn? */ | |
13732 | size = addend + file_align; | |
13733 | } | |
13734 | } | |
13735 | size = (size + file_align - 1) & -file_align; | |
13736 | ||
13737 | /* Allocate one extra entry for use as a "done" flag for the | |
13738 | consolidation pass. */ | |
13739 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13740 | ||
13741 | if (ptr) | |
13742 | { | |
a50b1753 | 13743 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13744 | |
13745 | if (ptr != NULL) | |
13746 | { | |
13747 | size_t oldbytes; | |
13748 | ||
cbd0eecf | 13749 | oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) |
c152c796 AM |
13750 | * sizeof (bfd_boolean)); |
13751 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13752 | } | |
13753 | } | |
13754 | else | |
a50b1753 | 13755 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13756 | |
13757 | if (ptr == NULL) | |
13758 | return FALSE; | |
13759 | ||
13760 | /* And arrange for that done flag to be at index -1. */ | |
cbd0eecf L |
13761 | h->u2.vtable->used = ptr + 1; |
13762 | h->u2.vtable->size = size; | |
c152c796 AM |
13763 | } |
13764 | ||
cbd0eecf | 13765 | h->u2.vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13766 | |
13767 | return TRUE; | |
13768 | } | |
13769 | ||
ae17ab41 CM |
13770 | /* Map an ELF section header flag to its corresponding string. */ |
13771 | typedef struct | |
13772 | { | |
13773 | char *flag_name; | |
13774 | flagword flag_value; | |
13775 | } elf_flags_to_name_table; | |
13776 | ||
13777 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13778 | { | |
13779 | { "SHF_WRITE", SHF_WRITE }, | |
13780 | { "SHF_ALLOC", SHF_ALLOC }, | |
13781 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13782 | { "SHF_MERGE", SHF_MERGE }, | |
13783 | { "SHF_STRINGS", SHF_STRINGS }, | |
13784 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13785 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13786 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13787 | { "SHF_GROUP", SHF_GROUP }, | |
13788 | { "SHF_TLS", SHF_TLS }, | |
13789 | { "SHF_MASKOS", SHF_MASKOS }, | |
13790 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13791 | }; | |
13792 | ||
b9c361e0 JL |
13793 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13794 | bfd_boolean | |
ae17ab41 | 13795 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13796 | struct flag_info *flaginfo, |
b9c361e0 | 13797 | asection *section) |
ae17ab41 | 13798 | { |
8b127cbc | 13799 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13800 | |
8b127cbc | 13801 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13802 | { |
8b127cbc AM |
13803 | bfd *obfd = info->output_bfd; |
13804 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13805 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13806 | int with_hex = 0; |
13807 | int without_hex = 0; | |
13808 | ||
8b127cbc | 13809 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13810 | { |
b9c361e0 | 13811 | unsigned i; |
8b127cbc | 13812 | flagword (*lookup) (char *); |
ae17ab41 | 13813 | |
8b127cbc AM |
13814 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13815 | if (lookup != NULL) | |
ae17ab41 | 13816 | { |
8b127cbc | 13817 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13818 | |
13819 | if (hexval != 0) | |
13820 | { | |
13821 | if (tf->with == with_flags) | |
13822 | with_hex |= hexval; | |
13823 | else if (tf->with == without_flags) | |
13824 | without_hex |= hexval; | |
13825 | tf->valid = TRUE; | |
13826 | continue; | |
13827 | } | |
ae17ab41 | 13828 | } |
8b127cbc | 13829 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13830 | { |
8b127cbc | 13831 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13832 | { |
13833 | if (tf->with == with_flags) | |
13834 | with_hex |= elf_flags_to_names[i].flag_value; | |
13835 | else if (tf->with == without_flags) | |
13836 | without_hex |= elf_flags_to_names[i].flag_value; | |
13837 | tf->valid = TRUE; | |
13838 | break; | |
13839 | } | |
13840 | } | |
8b127cbc | 13841 | if (!tf->valid) |
b9c361e0 | 13842 | { |
68ffbac6 | 13843 | info->callbacks->einfo |
9793eb77 | 13844 | (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13845 | return FALSE; |
ae17ab41 CM |
13846 | } |
13847 | } | |
8b127cbc AM |
13848 | flaginfo->flags_initialized = TRUE; |
13849 | flaginfo->only_with_flags |= with_hex; | |
13850 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13851 | } |
ae17ab41 | 13852 | |
8b127cbc | 13853 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13854 | return FALSE; |
13855 | ||
8b127cbc | 13856 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13857 | return FALSE; |
13858 | ||
13859 | return TRUE; | |
ae17ab41 CM |
13860 | } |
13861 | ||
c152c796 AM |
13862 | struct alloc_got_off_arg { |
13863 | bfd_vma gotoff; | |
10455f89 | 13864 | struct bfd_link_info *info; |
c152c796 AM |
13865 | }; |
13866 | ||
13867 | /* We need a special top-level link routine to convert got reference counts | |
13868 | to real got offsets. */ | |
13869 | ||
13870 | static bfd_boolean | |
13871 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13872 | { | |
a50b1753 | 13873 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13874 | bfd *obfd = gofarg->info->output_bfd; |
13875 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13876 | |
c152c796 AM |
13877 | if (h->got.refcount > 0) |
13878 | { | |
13879 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13880 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13881 | } |
13882 | else | |
13883 | h->got.offset = (bfd_vma) -1; | |
13884 | ||
13885 | return TRUE; | |
13886 | } | |
13887 | ||
13888 | /* And an accompanying bit to work out final got entry offsets once | |
13889 | we're done. Should be called from final_link. */ | |
13890 | ||
13891 | bfd_boolean | |
13892 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13893 | struct bfd_link_info *info) | |
13894 | { | |
13895 | bfd *i; | |
13896 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13897 | bfd_vma gotoff; | |
c152c796 AM |
13898 | struct alloc_got_off_arg gofarg; |
13899 | ||
10455f89 HPN |
13900 | BFD_ASSERT (abfd == info->output_bfd); |
13901 | ||
c152c796 AM |
13902 | if (! is_elf_hash_table (info->hash)) |
13903 | return FALSE; | |
13904 | ||
13905 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13906 | put into the .got.plt section, if the backend uses it. */ | |
13907 | if (bed->want_got_plt) | |
13908 | gotoff = 0; | |
13909 | else | |
13910 | gotoff = bed->got_header_size; | |
13911 | ||
13912 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13913 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13914 | { |
13915 | bfd_signed_vma *local_got; | |
ef53be89 | 13916 | size_t j, locsymcount; |
c152c796 AM |
13917 | Elf_Internal_Shdr *symtab_hdr; |
13918 | ||
13919 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13920 | continue; | |
13921 | ||
13922 | local_got = elf_local_got_refcounts (i); | |
13923 | if (!local_got) | |
13924 | continue; | |
13925 | ||
13926 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13927 | if (elf_bad_symtab (i)) | |
13928 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13929 | else | |
13930 | locsymcount = symtab_hdr->sh_info; | |
13931 | ||
13932 | for (j = 0; j < locsymcount; ++j) | |
13933 | { | |
13934 | if (local_got[j] > 0) | |
13935 | { | |
13936 | local_got[j] = gotoff; | |
10455f89 | 13937 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13938 | } |
13939 | else | |
13940 | local_got[j] = (bfd_vma) -1; | |
13941 | } | |
13942 | } | |
13943 | ||
13944 | /* Then the global .got entries. .plt refcounts are handled by | |
13945 | adjust_dynamic_symbol */ | |
13946 | gofarg.gotoff = gotoff; | |
10455f89 | 13947 | gofarg.info = info; |
c152c796 AM |
13948 | elf_link_hash_traverse (elf_hash_table (info), |
13949 | elf_gc_allocate_got_offsets, | |
13950 | &gofarg); | |
13951 | return TRUE; | |
13952 | } | |
13953 | ||
13954 | /* Many folk need no more in the way of final link than this, once | |
13955 | got entry reference counting is enabled. */ | |
13956 | ||
13957 | bfd_boolean | |
13958 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
13959 | { | |
13960 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
13961 | return FALSE; | |
13962 | ||
13963 | /* Invoke the regular ELF backend linker to do all the work. */ | |
13964 | return bfd_elf_final_link (abfd, info); | |
13965 | } | |
13966 | ||
13967 | bfd_boolean | |
13968 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
13969 | { | |
a50b1753 | 13970 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
13971 | |
13972 | if (rcookie->bad_symtab) | |
13973 | rcookie->rel = rcookie->rels; | |
13974 | ||
13975 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
13976 | { | |
13977 | unsigned long r_symndx; | |
13978 | ||
13979 | if (! rcookie->bad_symtab) | |
13980 | if (rcookie->rel->r_offset > offset) | |
13981 | return FALSE; | |
13982 | if (rcookie->rel->r_offset != offset) | |
13983 | continue; | |
13984 | ||
13985 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 13986 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
13987 | return TRUE; |
13988 | ||
13989 | if (r_symndx >= rcookie->locsymcount | |
13990 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13991 | { | |
13992 | struct elf_link_hash_entry *h; | |
13993 | ||
13994 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
13995 | ||
13996 | while (h->root.type == bfd_link_hash_indirect | |
13997 | || h->root.type == bfd_link_hash_warning) | |
13998 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
13999 | ||
14000 | if ((h->root.type == bfd_link_hash_defined | |
14001 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
14002 | && (h->root.u.def.section->owner != rcookie->abfd |
14003 | || h->root.u.def.section->kept_section != NULL | |
14004 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 14005 | return TRUE; |
c152c796 AM |
14006 | } |
14007 | else | |
14008 | { | |
14009 | /* It's not a relocation against a global symbol, | |
14010 | but it could be a relocation against a local | |
14011 | symbol for a discarded section. */ | |
14012 | asection *isec; | |
14013 | Elf_Internal_Sym *isym; | |
14014 | ||
14015 | /* Need to: get the symbol; get the section. */ | |
14016 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 14017 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
14018 | if (isec != NULL |
14019 | && (isec->kept_section != NULL | |
14020 | || discarded_section (isec))) | |
cb33740c | 14021 | return TRUE; |
c152c796 AM |
14022 | } |
14023 | return FALSE; | |
14024 | } | |
14025 | return FALSE; | |
14026 | } | |
14027 | ||
14028 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
14029 | Returns -1 on error, 1 if any section's size was changed, 0 if |
14030 | nothing changed. This function assumes that the relocations are in | |
14031 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 14032 | |
75938853 | 14033 | int |
c152c796 AM |
14034 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
14035 | { | |
14036 | struct elf_reloc_cookie cookie; | |
18cd5bce | 14037 | asection *o; |
c152c796 | 14038 | bfd *abfd; |
75938853 | 14039 | int changed = 0; |
c152c796 AM |
14040 | |
14041 | if (info->traditional_format | |
14042 | || !is_elf_hash_table (info->hash)) | |
75938853 | 14043 | return 0; |
c152c796 | 14044 | |
18cd5bce AM |
14045 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
14046 | if (o != NULL) | |
c152c796 | 14047 | { |
18cd5bce | 14048 | asection *i; |
c152c796 | 14049 | |
18cd5bce | 14050 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 14051 | { |
18cd5bce AM |
14052 | if (i->size == 0 |
14053 | || i->reloc_count == 0 | |
14054 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
14055 | continue; | |
c152c796 | 14056 | |
18cd5bce AM |
14057 | abfd = i->owner; |
14058 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14059 | continue; | |
c152c796 | 14060 | |
18cd5bce | 14061 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 14062 | return -1; |
c152c796 | 14063 | |
18cd5bce AM |
14064 | if (_bfd_discard_section_stabs (abfd, i, |
14065 | elf_section_data (i)->sec_info, | |
5241d853 RS |
14066 | bfd_elf_reloc_symbol_deleted_p, |
14067 | &cookie)) | |
75938853 | 14068 | changed = 1; |
18cd5bce AM |
14069 | |
14070 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14071 | } |
18cd5bce AM |
14072 | } |
14073 | ||
2f0c68f2 CM |
14074 | o = NULL; |
14075 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
14076 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
14077 | if (o != NULL) |
14078 | { | |
14079 | asection *i; | |
d7153c4a | 14080 | int eh_changed = 0; |
79a94a2a | 14081 | unsigned int eh_alignment; |
c152c796 | 14082 | |
18cd5bce | 14083 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 14084 | { |
18cd5bce AM |
14085 | if (i->size == 0) |
14086 | continue; | |
14087 | ||
14088 | abfd = i->owner; | |
14089 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14090 | continue; | |
14091 | ||
14092 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 14093 | return -1; |
18cd5bce AM |
14094 | |
14095 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
14096 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
14097 | bfd_elf_reloc_symbol_deleted_p, |
14098 | &cookie)) | |
d7153c4a AM |
14099 | { |
14100 | eh_changed = 1; | |
14101 | if (i->size != i->rawsize) | |
14102 | changed = 1; | |
14103 | } | |
18cd5bce AM |
14104 | |
14105 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14106 | } |
9866ffe2 | 14107 | |
79a94a2a | 14108 | eh_alignment = 1 << o->alignment_power; |
9866ffe2 AM |
14109 | /* Skip over zero terminator, and prevent empty sections from |
14110 | adding alignment padding at the end. */ | |
14111 | for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) | |
14112 | if (i->size == 0) | |
14113 | i->flags |= SEC_EXCLUDE; | |
14114 | else if (i->size > 4) | |
14115 | break; | |
14116 | /* The last non-empty eh_frame section doesn't need padding. */ | |
14117 | if (i != NULL) | |
14118 | i = i->map_tail.s; | |
14119 | /* Any prior sections must pad the last FDE out to the output | |
14120 | section alignment. Otherwise we might have zero padding | |
14121 | between sections, which would be seen as a terminator. */ | |
14122 | for (; i != NULL; i = i->map_tail.s) | |
14123 | if (i->size == 4) | |
14124 | /* All but the last zero terminator should have been removed. */ | |
14125 | BFD_FAIL (); | |
14126 | else | |
14127 | { | |
14128 | bfd_size_type size | |
14129 | = (i->size + eh_alignment - 1) & -eh_alignment; | |
14130 | if (i->size != size) | |
af471f82 | 14131 | { |
9866ffe2 AM |
14132 | i->size = size; |
14133 | changed = 1; | |
14134 | eh_changed = 1; | |
af471f82 | 14135 | } |
9866ffe2 | 14136 | } |
d7153c4a AM |
14137 | if (eh_changed) |
14138 | elf_link_hash_traverse (elf_hash_table (info), | |
14139 | _bfd_elf_adjust_eh_frame_global_symbol, NULL); | |
18cd5bce | 14140 | } |
c152c796 | 14141 | |
18cd5bce AM |
14142 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
14143 | { | |
14144 | const struct elf_backend_data *bed; | |
57963c05 | 14145 | asection *s; |
c152c796 | 14146 | |
18cd5bce AM |
14147 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
14148 | continue; | |
57963c05 AM |
14149 | s = abfd->sections; |
14150 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14151 | continue; | |
18cd5bce AM |
14152 | |
14153 | bed = get_elf_backend_data (abfd); | |
14154 | ||
14155 | if (bed->elf_backend_discard_info != NULL) | |
14156 | { | |
14157 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 14158 | return -1; |
18cd5bce AM |
14159 | |
14160 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 14161 | changed = 1; |
18cd5bce AM |
14162 | |
14163 | fini_reloc_cookie (&cookie, abfd); | |
14164 | } | |
c152c796 AM |
14165 | } |
14166 | ||
2f0c68f2 CM |
14167 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
14168 | _bfd_elf_end_eh_frame_parsing (info); | |
14169 | ||
14170 | if (info->eh_frame_hdr_type | |
0e1862bb | 14171 | && !bfd_link_relocatable (info) |
c152c796 | 14172 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 14173 | changed = 1; |
c152c796 | 14174 | |
75938853 | 14175 | return changed; |
c152c796 | 14176 | } |
082b7297 | 14177 | |
43e1669b | 14178 | bfd_boolean |
0c511000 | 14179 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 14180 | asection *sec, |
c0f00686 | 14181 | struct bfd_link_info *info) |
082b7297 L |
14182 | { |
14183 | flagword flags; | |
c77ec726 | 14184 | const char *name, *key; |
082b7297 L |
14185 | struct bfd_section_already_linked *l; |
14186 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 14187 | |
c77ec726 AM |
14188 | if (sec->output_section == bfd_abs_section_ptr) |
14189 | return FALSE; | |
0c511000 | 14190 | |
c77ec726 | 14191 | flags = sec->flags; |
0c511000 | 14192 | |
c77ec726 AM |
14193 | /* Return if it isn't a linkonce section. A comdat group section |
14194 | also has SEC_LINK_ONCE set. */ | |
14195 | if ((flags & SEC_LINK_ONCE) == 0) | |
14196 | return FALSE; | |
0c511000 | 14197 | |
c77ec726 AM |
14198 | /* Don't put group member sections on our list of already linked |
14199 | sections. They are handled as a group via their group section. */ | |
14200 | if (elf_sec_group (sec) != NULL) | |
14201 | return FALSE; | |
0c511000 | 14202 | |
c77ec726 AM |
14203 | /* For a SHT_GROUP section, use the group signature as the key. */ |
14204 | name = sec->name; | |
14205 | if ((flags & SEC_GROUP) != 0 | |
14206 | && elf_next_in_group (sec) != NULL | |
14207 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
14208 | key = elf_group_name (elf_next_in_group (sec)); | |
14209 | else | |
14210 | { | |
14211 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 14212 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
14213 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
14214 | key++; | |
0c511000 | 14215 | else |
c77ec726 AM |
14216 | /* Must be a user linkonce section that doesn't follow gcc's |
14217 | naming convention. In this case we won't be matching | |
14218 | single member groups. */ | |
14219 | key = name; | |
0c511000 | 14220 | } |
6d2cd210 | 14221 | |
c77ec726 | 14222 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
14223 | |
14224 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14225 | { | |
c2370991 | 14226 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
14227 | sections with a signature of <key> (<key> is some string), |
14228 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
14229 | Match like sections. LTO plugin sections are an exception. | |
14230 | They are always named .gnu.linkonce.t.<key> and match either | |
14231 | type of section. */ | |
14232 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
14233 | && ((flags & SEC_GROUP) != 0 | |
14234 | || strcmp (name, l->sec->name) == 0)) | |
14235 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
14236 | { |
14237 | /* The section has already been linked. See if we should | |
6d2cd210 | 14238 | issue a warning. */ |
c77ec726 AM |
14239 | if (!_bfd_handle_already_linked (sec, l, info)) |
14240 | return FALSE; | |
082b7297 | 14241 | |
c77ec726 | 14242 | if (flags & SEC_GROUP) |
3d7f7666 | 14243 | { |
c77ec726 AM |
14244 | asection *first = elf_next_in_group (sec); |
14245 | asection *s = first; | |
3d7f7666 | 14246 | |
c77ec726 | 14247 | while (s != NULL) |
3d7f7666 | 14248 | { |
c77ec726 AM |
14249 | s->output_section = bfd_abs_section_ptr; |
14250 | /* Record which group discards it. */ | |
14251 | s->kept_section = l->sec; | |
14252 | s = elf_next_in_group (s); | |
14253 | /* These lists are circular. */ | |
14254 | if (s == first) | |
14255 | break; | |
3d7f7666 L |
14256 | } |
14257 | } | |
082b7297 | 14258 | |
43e1669b | 14259 | return TRUE; |
082b7297 L |
14260 | } |
14261 | } | |
14262 | ||
c77ec726 AM |
14263 | /* A single member comdat group section may be discarded by a |
14264 | linkonce section and vice versa. */ | |
14265 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 14266 | { |
c77ec726 | 14267 | asection *first = elf_next_in_group (sec); |
c2370991 | 14268 | |
c77ec726 AM |
14269 | if (first != NULL && elf_next_in_group (first) == first) |
14270 | /* Check this single member group against linkonce sections. */ | |
14271 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14272 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14273 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
14274 | { | |
14275 | first->output_section = bfd_abs_section_ptr; | |
14276 | first->kept_section = l->sec; | |
14277 | sec->output_section = bfd_abs_section_ptr; | |
14278 | break; | |
14279 | } | |
14280 | } | |
14281 | else | |
14282 | /* Check this linkonce section against single member groups. */ | |
14283 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14284 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 14285 | { |
c77ec726 | 14286 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 14287 | |
c77ec726 AM |
14288 | if (first != NULL |
14289 | && elf_next_in_group (first) == first | |
14290 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
14291 | { | |
14292 | sec->output_section = bfd_abs_section_ptr; | |
14293 | sec->kept_section = first; | |
14294 | break; | |
14295 | } | |
6d2cd210 | 14296 | } |
0c511000 | 14297 | |
c77ec726 AM |
14298 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
14299 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
14300 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
14301 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
14302 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
14303 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
14304 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
14305 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
14306 | The reverse order cannot happen as there is never a bfd with only the | |
14307 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
14308 | matter as here were are looking only for cross-bfd sections. */ | |
14309 | ||
14310 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
14311 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14312 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14313 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
14314 | { | |
14315 | if (abfd != l->sec->owner) | |
14316 | sec->output_section = bfd_abs_section_ptr; | |
14317 | break; | |
14318 | } | |
80c29487 | 14319 | |
082b7297 | 14320 | /* This is the first section with this name. Record it. */ |
c77ec726 | 14321 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 14322 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 14323 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 14324 | } |
81e1b023 | 14325 | |
a4d8e49b L |
14326 | bfd_boolean |
14327 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
14328 | { | |
14329 | return sym->st_shndx == SHN_COMMON; | |
14330 | } | |
14331 | ||
14332 | unsigned int | |
14333 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
14334 | { | |
14335 | return SHN_COMMON; | |
14336 | } | |
14337 | ||
14338 | asection * | |
14339 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
14340 | { | |
14341 | return bfd_com_section_ptr; | |
14342 | } | |
10455f89 HPN |
14343 | |
14344 | bfd_vma | |
14345 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
14346 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
14347 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
14348 | bfd *ibfd ATTRIBUTE_UNUSED, | |
14349 | unsigned long symndx ATTRIBUTE_UNUSED) | |
14350 | { | |
14351 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14352 | return bed->s->arch_size / 8; | |
14353 | } | |
83bac4b0 NC |
14354 | |
14355 | /* Routines to support the creation of dynamic relocs. */ | |
14356 | ||
83bac4b0 NC |
14357 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
14358 | ||
14359 | static const char * | |
14360 | get_dynamic_reloc_section_name (bfd * abfd, | |
14361 | asection * sec, | |
14362 | bfd_boolean is_rela) | |
14363 | { | |
ddcf1fcf BS |
14364 | char *name; |
14365 | const char *old_name = bfd_get_section_name (NULL, sec); | |
14366 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 14367 | |
ddcf1fcf | 14368 | if (old_name == NULL) |
83bac4b0 NC |
14369 | return NULL; |
14370 | ||
ddcf1fcf | 14371 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 14372 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
14373 | |
14374 | return name; | |
14375 | } | |
14376 | ||
14377 | /* Returns the dynamic reloc section associated with SEC. | |
14378 | If necessary compute the name of the dynamic reloc section based | |
14379 | on SEC's name (looked up in ABFD's string table) and the setting | |
14380 | of IS_RELA. */ | |
14381 | ||
14382 | asection * | |
14383 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
14384 | asection * sec, | |
14385 | bfd_boolean is_rela) | |
14386 | { | |
14387 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14388 | ||
14389 | if (reloc_sec == NULL) | |
14390 | { | |
14391 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14392 | ||
14393 | if (name != NULL) | |
14394 | { | |
3d4d4302 | 14395 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
14396 | |
14397 | if (reloc_sec != NULL) | |
14398 | elf_section_data (sec)->sreloc = reloc_sec; | |
14399 | } | |
14400 | } | |
14401 | ||
14402 | return reloc_sec; | |
14403 | } | |
14404 | ||
14405 | /* Returns the dynamic reloc section associated with SEC. If the | |
14406 | section does not exist it is created and attached to the DYNOBJ | |
14407 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
14408 | structure. | |
f8076f98 | 14409 | |
83bac4b0 NC |
14410 | ALIGNMENT is the alignment for the newly created section and |
14411 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
14412 | or .rel.<SEC's name>. The section name is looked up in the | |
14413 | string table associated with ABFD. */ | |
14414 | ||
14415 | asection * | |
ca4be51c AM |
14416 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
14417 | bfd *dynobj, | |
14418 | unsigned int alignment, | |
14419 | bfd *abfd, | |
14420 | bfd_boolean is_rela) | |
83bac4b0 NC |
14421 | { |
14422 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14423 | ||
14424 | if (reloc_sec == NULL) | |
14425 | { | |
14426 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14427 | ||
14428 | if (name == NULL) | |
14429 | return NULL; | |
14430 | ||
3d4d4302 | 14431 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
14432 | |
14433 | if (reloc_sec == NULL) | |
14434 | { | |
3d4d4302 AM |
14435 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
14436 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
14437 | if ((sec->flags & SEC_ALLOC) != 0) |
14438 | flags |= SEC_ALLOC | SEC_LOAD; | |
14439 | ||
3d4d4302 | 14440 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
14441 | if (reloc_sec != NULL) |
14442 | { | |
8877b5e5 AM |
14443 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
14444 | name. Override as it may be wrong, eg. for a user | |
14445 | section named "auto" we'll get ".relauto" which is | |
14446 | seen to be a .rela section. */ | |
14447 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
14448 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
14449 | reloc_sec = NULL; | |
14450 | } | |
14451 | } | |
14452 | ||
14453 | elf_section_data (sec)->sreloc = reloc_sec; | |
14454 | } | |
14455 | ||
14456 | return reloc_sec; | |
14457 | } | |
1338dd10 | 14458 | |
bffebb6b AM |
14459 | /* Copy the ELF symbol type and other attributes for a linker script |
14460 | assignment from HSRC to HDEST. Generally this should be treated as | |
14461 | if we found a strong non-dynamic definition for HDEST (except that | |
14462 | ld ignores multiple definition errors). */ | |
1338dd10 | 14463 | void |
bffebb6b AM |
14464 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
14465 | struct bfd_link_hash_entry *hdest, | |
14466 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 14467 | { |
bffebb6b AM |
14468 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
14469 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14470 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14471 | |
14472 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14473 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14474 | |
14475 | isym.st_other = ehsrc->other; | |
b8417128 | 14476 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14477 | } |
351f65ca L |
14478 | |
14479 | /* Append a RELA relocation REL to section S in BFD. */ | |
14480 | ||
14481 | void | |
14482 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14483 | { | |
14484 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14485 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14486 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14487 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14488 | } | |
14489 | ||
14490 | /* Append a REL relocation REL to section S in BFD. */ | |
14491 | ||
14492 | void | |
14493 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14494 | { | |
14495 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14496 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14497 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14498 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14499 | } |
7dba9362 AM |
14500 | |
14501 | /* Define __start, __stop, .startof. or .sizeof. symbol. */ | |
14502 | ||
14503 | struct bfd_link_hash_entry * | |
14504 | bfd_elf_define_start_stop (struct bfd_link_info *info, | |
14505 | const char *symbol, asection *sec) | |
14506 | { | |
487b6440 | 14507 | struct elf_link_hash_entry *h; |
7dba9362 | 14508 | |
487b6440 AM |
14509 | h = elf_link_hash_lookup (elf_hash_table (info), symbol, |
14510 | FALSE, FALSE, TRUE); | |
14511 | if (h != NULL | |
14512 | && (h->root.type == bfd_link_hash_undefined | |
14513 | || h->root.type == bfd_link_hash_undefweak | |
bf3077a6 | 14514 | || ((h->ref_regular || h->def_dynamic) && !h->def_regular))) |
7dba9362 | 14515 | { |
bf3077a6 | 14516 | bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic; |
487b6440 AM |
14517 | h->root.type = bfd_link_hash_defined; |
14518 | h->root.u.def.section = sec; | |
14519 | h->root.u.def.value = 0; | |
14520 | h->def_regular = 1; | |
14521 | h->def_dynamic = 0; | |
14522 | h->start_stop = 1; | |
14523 | h->u2.start_stop_section = sec; | |
14524 | if (symbol[0] == '.') | |
14525 | { | |
14526 | /* .startof. and .sizeof. symbols are local. */ | |
559192d8 AM |
14527 | const struct elf_backend_data *bed; |
14528 | bed = get_elf_backend_data (info->output_bfd); | |
14529 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
487b6440 | 14530 | } |
36b8fda5 AM |
14531 | else |
14532 | { | |
14533 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
14534 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; | |
bf3077a6 | 14535 | if (was_dynamic) |
36b8fda5 AM |
14536 | bfd_elf_link_record_dynamic_symbol (info, h); |
14537 | } | |
487b6440 | 14538 | return &h->root; |
7dba9362 | 14539 | } |
487b6440 | 14540 | return NULL; |
7dba9362 | 14541 | } |