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 | 902 | htab = elf_hash_table (info); |
74541ad4 AM |
903 | if (htab->text_index_section != NULL) |
904 | return p != htab->text_index_section && p != htab->data_index_section; | |
905 | ||
ca55926c | 906 | return (htab->dynobj != NULL |
3d4d4302 | 907 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 908 | && ip->output_section == p); |
aee6f5b4 AO |
909 | |
910 | /* There shouldn't be section relative relocations | |
911 | against any other section. */ | |
912 | default: | |
913 | return TRUE; | |
914 | } | |
915 | } | |
916 | ||
d00dd7dc AM |
917 | bfd_boolean |
918 | _bfd_elf_omit_section_dynsym_all | |
919 | (bfd *output_bfd ATTRIBUTE_UNUSED, | |
920 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
921 | asection *p ATTRIBUTE_UNUSED) | |
922 | { | |
923 | return TRUE; | |
924 | } | |
925 | ||
062e2358 | 926 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
927 | symbol for each output section, which come first. Next come symbols |
928 | which have been forced to local binding. Then all of the back-end | |
929 | allocated local dynamic syms, followed by the rest of the global | |
63f452a8 AM |
930 | symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. |
931 | (This prevents the early call before elf_backend_init_index_section | |
932 | and strip_excluded_output_sections setting dynindx for sections | |
933 | that are stripped.) */ | |
30b30c21 | 934 | |
554220db AM |
935 | static unsigned long |
936 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
937 | struct bfd_link_info *info, | |
938 | unsigned long *section_sym_count) | |
30b30c21 RH |
939 | { |
940 | unsigned long dynsymcount = 0; | |
63f452a8 | 941 | bfd_boolean do_sec = section_sym_count != NULL; |
30b30c21 | 942 | |
0e1862bb L |
943 | if (bfd_link_pic (info) |
944 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 945 | { |
aee6f5b4 | 946 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
947 | asection *p; |
948 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 949 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 | 950 | && (p->flags & SEC_ALLOC) != 0 |
7f923b7f | 951 | && elf_hash_table (info)->dynamic_relocs |
aee6f5b4 | 952 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) |
63f452a8 AM |
953 | { |
954 | ++dynsymcount; | |
955 | if (do_sec) | |
956 | elf_section_data (p)->dynindx = dynsymcount; | |
957 | } | |
958 | else if (do_sec) | |
74541ad4 | 959 | elf_section_data (p)->dynindx = 0; |
30b30c21 | 960 | } |
63f452a8 AM |
961 | if (do_sec) |
962 | *section_sym_count = dynsymcount; | |
30b30c21 | 963 | |
6fa3860b PB |
964 | elf_link_hash_traverse (elf_hash_table (info), |
965 | elf_link_renumber_local_hash_table_dynsyms, | |
966 | &dynsymcount); | |
967 | ||
30b30c21 RH |
968 | if (elf_hash_table (info)->dynlocal) |
969 | { | |
970 | struct elf_link_local_dynamic_entry *p; | |
971 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
972 | p->dynindx = ++dynsymcount; | |
973 | } | |
90ac2420 | 974 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
975 | |
976 | elf_link_hash_traverse (elf_hash_table (info), | |
977 | elf_link_renumber_hash_table_dynsyms, | |
978 | &dynsymcount); | |
979 | ||
d5486c43 L |
980 | /* There is an unused NULL entry at the head of the table which we |
981 | must account for in our count even if the table is empty since it | |
982 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
983 | .dynamic section. */ | |
984 | dynsymcount++; | |
30b30c21 | 985 | |
ccabcbe5 AM |
986 | elf_hash_table (info)->dynsymcount = dynsymcount; |
987 | return dynsymcount; | |
30b30c21 | 988 | } |
252b5132 | 989 | |
54ac0771 L |
990 | /* Merge st_other field. */ |
991 | ||
992 | static void | |
993 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
b8417128 | 994 | const Elf_Internal_Sym *isym, asection *sec, |
cd3416da | 995 | bfd_boolean definition, bfd_boolean dynamic) |
54ac0771 L |
996 | { |
997 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
998 | ||
999 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 1000 | code might be needed here. */ |
54ac0771 L |
1001 | if (bed->elf_backend_merge_symbol_attribute) |
1002 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
1003 | dynamic); | |
1004 | ||
cd3416da | 1005 | if (!dynamic) |
54ac0771 | 1006 | { |
cd3416da AM |
1007 | unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); |
1008 | unsigned hvis = ELF_ST_VISIBILITY (h->other); | |
54ac0771 | 1009 | |
cd3416da AM |
1010 | /* Keep the most constraining visibility. Leave the remainder |
1011 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
1012 | if (symvis - 1 < hvis - 1) | |
1013 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 1014 | } |
b8417128 AM |
1015 | else if (definition |
1016 | && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT | |
1017 | && (sec->flags & SEC_READONLY) == 0) | |
6cabe1ea | 1018 | h->protected_def = 1; |
54ac0771 L |
1019 | } |
1020 | ||
4f3fedcf AM |
1021 | /* This function is called when we want to merge a new symbol with an |
1022 | existing symbol. It handles the various cases which arise when we | |
1023 | find a definition in a dynamic object, or when there is already a | |
1024 | definition in a dynamic object. The new symbol is described by | |
1025 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
1026 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
1027 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
1028 | of an old common symbol. We set OVERRIDE if the old symbol is | |
1029 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
1030 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
1031 | to change. By OK to change, we mean that we shouldn't warn if the | |
1032 | type or size does change. */ | |
45d6a902 | 1033 | |
8a56bd02 | 1034 | static bfd_boolean |
268b6b39 AM |
1035 | _bfd_elf_merge_symbol (bfd *abfd, |
1036 | struct bfd_link_info *info, | |
1037 | const char *name, | |
1038 | Elf_Internal_Sym *sym, | |
1039 | asection **psec, | |
1040 | bfd_vma *pvalue, | |
4f3fedcf AM |
1041 | struct elf_link_hash_entry **sym_hash, |
1042 | bfd **poldbfd, | |
37a9e49a | 1043 | bfd_boolean *pold_weak, |
af44c138 | 1044 | unsigned int *pold_alignment, |
268b6b39 AM |
1045 | bfd_boolean *skip, |
1046 | bfd_boolean *override, | |
1047 | bfd_boolean *type_change_ok, | |
6e33951e L |
1048 | bfd_boolean *size_change_ok, |
1049 | bfd_boolean *matched) | |
252b5132 | 1050 | { |
7479dfd4 | 1051 | asection *sec, *oldsec; |
45d6a902 | 1052 | struct elf_link_hash_entry *h; |
90c984fc | 1053 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
1054 | struct elf_link_hash_entry *flip; |
1055 | int bind; | |
1056 | bfd *oldbfd; | |
1057 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
0a36a439 | 1058 | bfd_boolean newweak, oldweak, newfunc, oldfunc; |
a4d8e49b | 1059 | const struct elf_backend_data *bed; |
6e33951e | 1060 | char *new_version; |
93f4de39 | 1061 | bfd_boolean default_sym = *matched; |
45d6a902 AM |
1062 | |
1063 | *skip = FALSE; | |
1064 | *override = FALSE; | |
1065 | ||
1066 | sec = *psec; | |
1067 | bind = ELF_ST_BIND (sym->st_info); | |
1068 | ||
1069 | if (! bfd_is_und_section (sec)) | |
1070 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
1071 | else | |
1072 | h = ((struct elf_link_hash_entry *) | |
1073 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
1074 | if (h == NULL) | |
1075 | return FALSE; | |
1076 | *sym_hash = h; | |
252b5132 | 1077 | |
88ba32a0 L |
1078 | bed = get_elf_backend_data (abfd); |
1079 | ||
6e33951e | 1080 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1081 | if (h->versioned != unversioned) |
6e33951e | 1082 | { |
422f1182 L |
1083 | /* Symbol version is unknown or versioned. */ |
1084 | new_version = strrchr (name, ELF_VER_CHR); | |
1085 | if (new_version) | |
1086 | { | |
1087 | if (h->versioned == unknown) | |
1088 | { | |
1089 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1090 | h->versioned = versioned_hidden; | |
1091 | else | |
1092 | h->versioned = versioned; | |
1093 | } | |
1094 | new_version += 1; | |
1095 | if (new_version[0] == '\0') | |
1096 | new_version = NULL; | |
1097 | } | |
1098 | else | |
1099 | h->versioned = unversioned; | |
6e33951e | 1100 | } |
422f1182 L |
1101 | else |
1102 | new_version = NULL; | |
6e33951e | 1103 | |
90c984fc L |
1104 | /* For merging, we only care about real symbols. But we need to make |
1105 | sure that indirect symbol dynamic flags are updated. */ | |
1106 | hi = h; | |
45d6a902 AM |
1107 | while (h->root.type == bfd_link_hash_indirect |
1108 | || h->root.type == bfd_link_hash_warning) | |
1109 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1110 | ||
6e33951e L |
1111 | if (!*matched) |
1112 | { | |
1113 | if (hi == h || h->root.type == bfd_link_hash_new) | |
1114 | *matched = TRUE; | |
1115 | else | |
1116 | { | |
ae7683d2 | 1117 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1118 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1119 | true if the new symbol is only visible to the symbol with |
6e33951e | 1120 | the same symbol version. */ |
422f1182 L |
1121 | bfd_boolean old_hidden = h->versioned == versioned_hidden; |
1122 | bfd_boolean new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1123 | if (!old_hidden && !new_hidden) |
1124 | /* The new symbol matches the existing symbol if both | |
1125 | aren't hidden. */ | |
1126 | *matched = TRUE; | |
1127 | else | |
1128 | { | |
1129 | /* OLD_VERSION is the symbol version of the existing | |
1130 | symbol. */ | |
422f1182 L |
1131 | char *old_version; |
1132 | ||
1133 | if (h->versioned >= versioned) | |
1134 | old_version = strrchr (h->root.root.string, | |
1135 | ELF_VER_CHR) + 1; | |
1136 | else | |
1137 | old_version = NULL; | |
6e33951e L |
1138 | |
1139 | /* The new symbol matches the existing symbol if they | |
1140 | have the same symbol version. */ | |
1141 | *matched = (old_version == new_version | |
1142 | || (old_version != NULL | |
1143 | && new_version != NULL | |
1144 | && strcmp (old_version, new_version) == 0)); | |
1145 | } | |
1146 | } | |
1147 | } | |
1148 | ||
934bce08 AM |
1149 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1150 | existing symbol. */ | |
1151 | ||
1152 | oldbfd = NULL; | |
1153 | oldsec = NULL; | |
1154 | switch (h->root.type) | |
1155 | { | |
1156 | default: | |
1157 | break; | |
1158 | ||
1159 | case bfd_link_hash_undefined: | |
1160 | case bfd_link_hash_undefweak: | |
1161 | oldbfd = h->root.u.undef.abfd; | |
1162 | break; | |
1163 | ||
1164 | case bfd_link_hash_defined: | |
1165 | case bfd_link_hash_defweak: | |
1166 | oldbfd = h->root.u.def.section->owner; | |
1167 | oldsec = h->root.u.def.section; | |
1168 | break; | |
1169 | ||
1170 | case bfd_link_hash_common: | |
1171 | oldbfd = h->root.u.c.p->section->owner; | |
1172 | oldsec = h->root.u.c.p->section; | |
1173 | if (pold_alignment) | |
1174 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1175 | break; | |
1176 | } | |
1177 | if (poldbfd && *poldbfd == NULL) | |
1178 | *poldbfd = oldbfd; | |
1179 | ||
1180 | /* Differentiate strong and weak symbols. */ | |
1181 | newweak = bind == STB_WEAK; | |
1182 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1183 | || h->root.type == bfd_link_hash_undefweak); | |
1184 | if (pold_weak) | |
1185 | *pold_weak = oldweak; | |
1186 | ||
40b36307 | 1187 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1188 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1189 | symbols. */ |
1190 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1191 | ||
ee659f1f AM |
1192 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1193 | respectively, is from a dynamic object. */ | |
1194 | ||
1195 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1196 | ||
1197 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1198 | syms and defined syms in dynamic libraries respectively. | |
1199 | ref_dynamic on the other hand can be set for a symbol defined in | |
1200 | a dynamic library, and def_dynamic may not be set; When the | |
1201 | definition in a dynamic lib is overridden by a definition in the | |
1202 | executable use of the symbol in the dynamic lib becomes a | |
1203 | reference to the executable symbol. */ | |
1204 | if (newdyn) | |
1205 | { | |
1206 | if (bfd_is_und_section (sec)) | |
1207 | { | |
1208 | if (bind != STB_WEAK) | |
1209 | { | |
1210 | h->ref_dynamic_nonweak = 1; | |
1211 | hi->ref_dynamic_nonweak = 1; | |
1212 | } | |
1213 | } | |
1214 | else | |
1215 | { | |
6e33951e L |
1216 | /* Update the existing symbol only if they match. */ |
1217 | if (*matched) | |
1218 | h->dynamic_def = 1; | |
ee659f1f AM |
1219 | hi->dynamic_def = 1; |
1220 | } | |
1221 | } | |
1222 | ||
45d6a902 AM |
1223 | /* If we just created the symbol, mark it as being an ELF symbol. |
1224 | Other than that, there is nothing to do--there is no merge issue | |
1225 | with a newly defined symbol--so we just return. */ | |
1226 | ||
1227 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1228 | { |
f5385ebf | 1229 | h->non_elf = 0; |
45d6a902 AM |
1230 | return TRUE; |
1231 | } | |
252b5132 | 1232 | |
45d6a902 AM |
1233 | /* In cases involving weak versioned symbols, we may wind up trying |
1234 | to merge a symbol with itself. Catch that here, to avoid the | |
1235 | confusion that results if we try to override a symbol with | |
1236 | itself. The additional tests catch cases like | |
1237 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1238 | dynamic object, which we do want to handle here. */ | |
1239 | if (abfd == oldbfd | |
895fa45f | 1240 | && (newweak || oldweak) |
45d6a902 | 1241 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1242 | || !h->def_regular)) |
45d6a902 AM |
1243 | return TRUE; |
1244 | ||
707bba77 | 1245 | olddyn = FALSE; |
45d6a902 AM |
1246 | if (oldbfd != NULL) |
1247 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1248 | else if (oldsec != NULL) |
45d6a902 | 1249 | { |
707bba77 | 1250 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1251 | indices used by MIPS ELF. */ |
707bba77 | 1252 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1253 | } |
252b5132 | 1254 | |
1a3b5c34 AM |
1255 | /* Handle a case where plugin_notice won't be called and thus won't |
1256 | set the non_ir_ref flags on the first pass over symbols. */ | |
1257 | if (oldbfd != NULL | |
1258 | && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN) | |
1259 | && newdyn != olddyn) | |
1260 | { | |
1261 | h->root.non_ir_ref_dynamic = TRUE; | |
1262 | hi->root.non_ir_ref_dynamic = TRUE; | |
1263 | } | |
1264 | ||
45d6a902 AM |
1265 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1266 | respectively, appear to be a definition rather than reference. */ | |
1267 | ||
707bba77 | 1268 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1269 | |
707bba77 AM |
1270 | olddef = (h->root.type != bfd_link_hash_undefined |
1271 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1272 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1273 | |
0a36a439 L |
1274 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1275 | respectively, appear to be a function. */ | |
1276 | ||
1277 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1278 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1279 | ||
1280 | oldfunc = (h->type != STT_NOTYPE | |
1281 | && bed->is_function_type (h->type)); | |
1282 | ||
c5d37467 | 1283 | if (!(newfunc && oldfunc) |
5b677558 AM |
1284 | && ELF_ST_TYPE (sym->st_info) != h->type |
1285 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1286 | && h->type != STT_NOTYPE | |
c5d37467 AM |
1287 | && (newdef || bfd_is_com_section (sec)) |
1288 | && (olddef || h->root.type == bfd_link_hash_common)) | |
580a2b6e | 1289 | { |
c5d37467 AM |
1290 | /* If creating a default indirect symbol ("foo" or "foo@") from |
1291 | a dynamic versioned definition ("foo@@") skip doing so if | |
1292 | there is an existing regular definition with a different | |
1293 | type. We don't want, for example, a "time" variable in the | |
1294 | executable overriding a "time" function in a shared library. */ | |
1295 | if (newdyn | |
1296 | && !olddyn) | |
1297 | { | |
1298 | *skip = TRUE; | |
1299 | return TRUE; | |
1300 | } | |
1301 | ||
1302 | /* When adding a symbol from a regular object file after we have | |
1303 | created indirect symbols, undo the indirection and any | |
1304 | dynamic state. */ | |
1305 | if (hi != h | |
1306 | && !newdyn | |
1307 | && olddyn) | |
1308 | { | |
1309 | h = hi; | |
1310 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1311 | h->forced_local = 0; | |
1312 | h->ref_dynamic = 0; | |
1313 | h->def_dynamic = 0; | |
1314 | h->dynamic_def = 0; | |
1315 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1316 | { | |
1317 | h->root.type = bfd_link_hash_undefined; | |
1318 | h->root.u.undef.abfd = abfd; | |
1319 | } | |
1320 | else | |
1321 | { | |
1322 | h->root.type = bfd_link_hash_new; | |
1323 | h->root.u.undef.abfd = NULL; | |
1324 | } | |
1325 | return TRUE; | |
1326 | } | |
580a2b6e L |
1327 | } |
1328 | ||
4c34aff8 AM |
1329 | /* Check TLS symbols. We don't check undefined symbols introduced |
1330 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1331 | check symbols from plugins because they also have no type. */ | |
1332 | if (oldbfd != NULL | |
1333 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1334 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1335 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1336 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1337 | { |
1338 | bfd *ntbfd, *tbfd; | |
1339 | bfd_boolean ntdef, tdef; | |
1340 | asection *ntsec, *tsec; | |
1341 | ||
1342 | if (h->type == STT_TLS) | |
1343 | { | |
3b36f7e6 | 1344 | ntbfd = abfd; |
7479dfd4 L |
1345 | ntsec = sec; |
1346 | ntdef = newdef; | |
1347 | tbfd = oldbfd; | |
1348 | tsec = oldsec; | |
1349 | tdef = olddef; | |
1350 | } | |
1351 | else | |
1352 | { | |
1353 | ntbfd = oldbfd; | |
1354 | ntsec = oldsec; | |
1355 | ntdef = olddef; | |
1356 | tbfd = abfd; | |
1357 | tsec = sec; | |
1358 | tdef = newdef; | |
1359 | } | |
1360 | ||
1361 | if (tdef && ntdef) | |
4eca0228 | 1362 | _bfd_error_handler |
695344c0 | 1363 | /* xgettext:c-format */ |
871b3ab2 AM |
1364 | (_("%s: TLS definition in %pB section %pA " |
1365 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1366 | h->root.root.string, tbfd, tsec, ntbfd, ntsec); |
7479dfd4 | 1367 | else if (!tdef && !ntdef) |
4eca0228 | 1368 | _bfd_error_handler |
695344c0 | 1369 | /* xgettext:c-format */ |
871b3ab2 AM |
1370 | (_("%s: TLS reference in %pB " |
1371 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1372 | h->root.root.string, tbfd, ntbfd); |
7479dfd4 | 1373 | else if (tdef) |
4eca0228 | 1374 | _bfd_error_handler |
695344c0 | 1375 | /* xgettext:c-format */ |
871b3ab2 AM |
1376 | (_("%s: TLS definition in %pB section %pA " |
1377 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1378 | h->root.root.string, tbfd, tsec, ntbfd); |
7479dfd4 | 1379 | else |
4eca0228 | 1380 | _bfd_error_handler |
695344c0 | 1381 | /* xgettext:c-format */ |
871b3ab2 AM |
1382 | (_("%s: TLS reference in %pB " |
1383 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1384 | h->root.root.string, tbfd, ntbfd, ntsec); |
7479dfd4 L |
1385 | |
1386 | bfd_set_error (bfd_error_bad_value); | |
1387 | return FALSE; | |
1388 | } | |
1389 | ||
45d6a902 AM |
1390 | /* If the old symbol has non-default visibility, we ignore the new |
1391 | definition from a dynamic object. */ | |
1392 | if (newdyn | |
9c7a29a3 | 1393 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1394 | && !bfd_is_und_section (sec)) |
1395 | { | |
1396 | *skip = TRUE; | |
1397 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 1398 | h->ref_dynamic = 1; |
90c984fc | 1399 | hi->ref_dynamic = 1; |
45d6a902 AM |
1400 | /* A protected symbol has external availability. Make sure it is |
1401 | recorded as dynamic. | |
1402 | ||
1403 | FIXME: Should we check type and size for protected symbol? */ | |
1404 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1405 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
1406 | else |
1407 | return TRUE; | |
1408 | } | |
1409 | else if (!newdyn | |
9c7a29a3 | 1410 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1411 | && h->def_dynamic) |
45d6a902 AM |
1412 | { |
1413 | /* If the new symbol with non-default visibility comes from a | |
1414 | relocatable file and the old definition comes from a dynamic | |
1415 | object, we remove the old definition. */ | |
6c9b78e6 | 1416 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1417 | { |
1418 | /* Handle the case where the old dynamic definition is | |
1419 | default versioned. We need to copy the symbol info from | |
1420 | the symbol with default version to the normal one if it | |
1421 | was referenced before. */ | |
1422 | if (h->ref_regular) | |
1423 | { | |
6c9b78e6 | 1424 | hi->root.type = h->root.type; |
d2dee3b2 | 1425 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1426 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1427 | |
6c9b78e6 | 1428 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1429 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1430 | { |
aed81c4e MR |
1431 | /* If the new symbol is hidden or internal, completely undo |
1432 | any dynamic link state. */ | |
1433 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1434 | h->forced_local = 0; | |
1435 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1436 | } |
1437 | else | |
aed81c4e MR |
1438 | h->ref_dynamic = 1; |
1439 | ||
1440 | h->def_dynamic = 0; | |
aed81c4e MR |
1441 | /* FIXME: Should we check type and size for protected symbol? */ |
1442 | h->size = 0; | |
1443 | h->type = 0; | |
1444 | ||
6c9b78e6 | 1445 | h = hi; |
d2dee3b2 L |
1446 | } |
1447 | else | |
6c9b78e6 | 1448 | h = hi; |
d2dee3b2 | 1449 | } |
1de1a317 | 1450 | |
f5eda473 AM |
1451 | /* If the old symbol was undefined before, then it will still be |
1452 | on the undefs list. If the new symbol is undefined or | |
1453 | common, we can't make it bfd_link_hash_new here, because new | |
1454 | undefined or common symbols will be added to the undefs list | |
1455 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1456 | added twice to the undefs list. Also, if the new symbol is | |
1457 | undefweak then we don't want to lose the strong undef. */ | |
1458 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1459 | { |
1de1a317 | 1460 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1461 | h->root.u.undef.abfd = abfd; |
1462 | } | |
1463 | else | |
1464 | { | |
1465 | h->root.type = bfd_link_hash_new; | |
1466 | h->root.u.undef.abfd = NULL; | |
1467 | } | |
1468 | ||
f5eda473 | 1469 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1470 | { |
f5eda473 AM |
1471 | /* If the new symbol is hidden or internal, completely undo |
1472 | any dynamic link state. */ | |
1473 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1474 | h->forced_local = 0; | |
1475 | h->ref_dynamic = 0; | |
45d6a902 | 1476 | } |
f5eda473 AM |
1477 | else |
1478 | h->ref_dynamic = 1; | |
1479 | h->def_dynamic = 0; | |
45d6a902 AM |
1480 | /* FIXME: Should we check type and size for protected symbol? */ |
1481 | h->size = 0; | |
1482 | h->type = 0; | |
1483 | return TRUE; | |
1484 | } | |
14a793b2 | 1485 | |
15b43f48 AM |
1486 | /* If a new weak symbol definition comes from a regular file and the |
1487 | old symbol comes from a dynamic library, we treat the new one as | |
1488 | strong. Similarly, an old weak symbol definition from a regular | |
1489 | file is treated as strong when the new symbol comes from a dynamic | |
1490 | library. Further, an old weak symbol from a dynamic library is | |
1491 | treated as strong if the new symbol is from a dynamic library. | |
1492 | This reflects the way glibc's ld.so works. | |
1493 | ||
165f707a AM |
1494 | Also allow a weak symbol to override a linker script symbol |
1495 | defined by an early pass over the script. This is done so the | |
1496 | linker knows the symbol is defined in an object file, for the | |
1497 | DEFINED script function. | |
1498 | ||
15b43f48 AM |
1499 | Do this before setting *type_change_ok or *size_change_ok so that |
1500 | we warn properly when dynamic library symbols are overridden. */ | |
1501 | ||
165f707a | 1502 | if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) |
0f8a2703 | 1503 | newweak = FALSE; |
15b43f48 | 1504 | if (olddef && newdyn) |
0f8a2703 AM |
1505 | oldweak = FALSE; |
1506 | ||
d334575b | 1507 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1508 | if (newfunc && oldfunc) |
fcb93ecf PB |
1509 | *type_change_ok = TRUE; |
1510 | ||
79349b09 AM |
1511 | /* It's OK to change the type if either the existing symbol or the |
1512 | new symbol is weak. A type change is also OK if the old symbol | |
1513 | is undefined and the new symbol is defined. */ | |
252b5132 | 1514 | |
79349b09 AM |
1515 | if (oldweak |
1516 | || newweak | |
1517 | || (newdef | |
1518 | && h->root.type == bfd_link_hash_undefined)) | |
1519 | *type_change_ok = TRUE; | |
1520 | ||
1521 | /* It's OK to change the size if either the existing symbol or the | |
1522 | new symbol is weak, or if the old symbol is undefined. */ | |
1523 | ||
1524 | if (*type_change_ok | |
1525 | || h->root.type == bfd_link_hash_undefined) | |
1526 | *size_change_ok = TRUE; | |
45d6a902 | 1527 | |
45d6a902 AM |
1528 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1529 | symbol, respectively, appears to be a common symbol in a dynamic | |
1530 | object. If a symbol appears in an uninitialized section, and is | |
1531 | not weak, and is not a function, then it may be a common symbol | |
1532 | which was resolved when the dynamic object was created. We want | |
1533 | to treat such symbols specially, because they raise special | |
1534 | considerations when setting the symbol size: if the symbol | |
1535 | appears as a common symbol in a regular object, and the size in | |
1536 | the regular object is larger, we must make sure that we use the | |
1537 | larger size. This problematic case can always be avoided in C, | |
1538 | but it must be handled correctly when using Fortran shared | |
1539 | libraries. | |
1540 | ||
1541 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1542 | likewise for OLDDYNCOMMON and OLDDEF. | |
1543 | ||
1544 | Note that this test is just a heuristic, and that it is quite | |
1545 | possible to have an uninitialized symbol in a shared object which | |
1546 | is really a definition, rather than a common symbol. This could | |
1547 | lead to some minor confusion when the symbol really is a common | |
1548 | symbol in some regular object. However, I think it will be | |
1549 | harmless. */ | |
1550 | ||
1551 | if (newdyn | |
1552 | && newdef | |
79349b09 | 1553 | && !newweak |
45d6a902 AM |
1554 | && (sec->flags & SEC_ALLOC) != 0 |
1555 | && (sec->flags & SEC_LOAD) == 0 | |
1556 | && sym->st_size > 0 | |
0a36a439 | 1557 | && !newfunc) |
45d6a902 AM |
1558 | newdyncommon = TRUE; |
1559 | else | |
1560 | newdyncommon = FALSE; | |
1561 | ||
1562 | if (olddyn | |
1563 | && olddef | |
1564 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1565 | && h->def_dynamic |
45d6a902 AM |
1566 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1567 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1568 | && h->size > 0 | |
0a36a439 | 1569 | && !oldfunc) |
45d6a902 AM |
1570 | olddyncommon = TRUE; |
1571 | else | |
1572 | olddyncommon = FALSE; | |
1573 | ||
a4d8e49b L |
1574 | /* We now know everything about the old and new symbols. We ask the |
1575 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1576 | if (bed->merge_symbol != NULL) |
1577 | { | |
1578 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
1579 | return FALSE; | |
1580 | sec = *psec; | |
1581 | } | |
a4d8e49b | 1582 | |
a83ef4d1 L |
1583 | /* There are multiple definitions of a normal symbol. Skip the |
1584 | default symbol as well as definition from an IR object. */ | |
93f4de39 | 1585 | if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak |
a83ef4d1 L |
1586 | && !default_sym && h->def_regular |
1587 | && !(oldbfd != NULL | |
1588 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
1589 | && (abfd->flags & BFD_PLUGIN) == 0)) | |
93f4de39 RL |
1590 | { |
1591 | /* Handle a multiple definition. */ | |
1592 | (*info->callbacks->multiple_definition) (info, &h->root, | |
1593 | abfd, sec, *pvalue); | |
1594 | *skip = TRUE; | |
1595 | return TRUE; | |
1596 | } | |
1597 | ||
45d6a902 AM |
1598 | /* If both the old and the new symbols look like common symbols in a |
1599 | dynamic object, set the size of the symbol to the larger of the | |
1600 | two. */ | |
1601 | ||
1602 | if (olddyncommon | |
1603 | && newdyncommon | |
1604 | && sym->st_size != h->size) | |
1605 | { | |
1606 | /* Since we think we have two common symbols, issue a multiple | |
1607 | common warning if desired. Note that we only warn if the | |
1608 | size is different. If the size is the same, we simply let | |
1609 | the old symbol override the new one as normally happens with | |
1610 | symbols defined in dynamic objects. */ | |
1611 | ||
1a72702b AM |
1612 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1613 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1614 | if (sym->st_size > h->size) |
1615 | h->size = sym->st_size; | |
252b5132 | 1616 | |
45d6a902 | 1617 | *size_change_ok = TRUE; |
252b5132 RH |
1618 | } |
1619 | ||
45d6a902 AM |
1620 | /* If we are looking at a dynamic object, and we have found a |
1621 | definition, we need to see if the symbol was already defined by | |
1622 | some other object. If so, we want to use the existing | |
1623 | definition, and we do not want to report a multiple symbol | |
1624 | definition error; we do this by clobbering *PSEC to be | |
1625 | bfd_und_section_ptr. | |
1626 | ||
1627 | We treat a common symbol as a definition if the symbol in the | |
1628 | shared library is a function, since common symbols always | |
1629 | represent variables; this can cause confusion in principle, but | |
1630 | any such confusion would seem to indicate an erroneous program or | |
1631 | shared library. We also permit a common symbol in a regular | |
8170f769 | 1632 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1633 | |
1634 | if (newdyn | |
1635 | && newdef | |
77cfaee6 | 1636 | && (olddef |
45d6a902 | 1637 | || (h->root.type == bfd_link_hash_common |
8170f769 | 1638 | && (newweak || newfunc)))) |
45d6a902 AM |
1639 | { |
1640 | *override = TRUE; | |
1641 | newdef = FALSE; | |
1642 | newdyncommon = FALSE; | |
252b5132 | 1643 | |
45d6a902 AM |
1644 | *psec = sec = bfd_und_section_ptr; |
1645 | *size_change_ok = TRUE; | |
252b5132 | 1646 | |
45d6a902 AM |
1647 | /* If we get here when the old symbol is a common symbol, then |
1648 | we are explicitly letting it override a weak symbol or | |
1649 | function in a dynamic object, and we don't want to warn about | |
1650 | a type change. If the old symbol is a defined symbol, a type | |
1651 | change warning may still be appropriate. */ | |
252b5132 | 1652 | |
45d6a902 AM |
1653 | if (h->root.type == bfd_link_hash_common) |
1654 | *type_change_ok = TRUE; | |
1655 | } | |
1656 | ||
1657 | /* Handle the special case of an old common symbol merging with a | |
1658 | new symbol which looks like a common symbol in a shared object. | |
1659 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1660 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1661 | right thing. */ | |
45d6a902 AM |
1662 | |
1663 | if (newdyncommon | |
1664 | && h->root.type == bfd_link_hash_common) | |
1665 | { | |
1666 | *override = TRUE; | |
1667 | newdef = FALSE; | |
1668 | newdyncommon = FALSE; | |
1669 | *pvalue = sym->st_size; | |
a4d8e49b | 1670 | *psec = sec = bed->common_section (oldsec); |
45d6a902 AM |
1671 | *size_change_ok = TRUE; |
1672 | } | |
1673 | ||
c5e2cead | 1674 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1675 | if (newdef && olddef && newweak) |
54ac0771 | 1676 | { |
35ed3f94 | 1677 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1678 | if (!(oldbfd != NULL |
1679 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1680 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c AM |
1681 | { |
1682 | newdef = FALSE; | |
1683 | *skip = TRUE; | |
1684 | } | |
54ac0771 L |
1685 | |
1686 | /* Merge st_other. If the symbol already has a dynamic index, | |
1687 | but visibility says it should not be visible, turn it into a | |
1688 | local symbol. */ | |
b8417128 | 1689 | elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); |
54ac0771 L |
1690 | if (h->dynindx != -1) |
1691 | switch (ELF_ST_VISIBILITY (h->other)) | |
1692 | { | |
1693 | case STV_INTERNAL: | |
1694 | case STV_HIDDEN: | |
1695 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1696 | break; | |
1697 | } | |
1698 | } | |
c5e2cead | 1699 | |
45d6a902 AM |
1700 | /* If the old symbol is from a dynamic object, and the new symbol is |
1701 | a definition which is not from a dynamic object, then the new | |
1702 | symbol overrides the old symbol. Symbols from regular files | |
1703 | always take precedence over symbols from dynamic objects, even if | |
1704 | they are defined after the dynamic object in the link. | |
1705 | ||
1706 | As above, we again permit a common symbol in a regular object to | |
1707 | override a definition in a shared object if the shared object | |
0f8a2703 | 1708 | symbol is a function or is weak. */ |
45d6a902 AM |
1709 | |
1710 | flip = NULL; | |
77cfaee6 | 1711 | if (!newdyn |
45d6a902 AM |
1712 | && (newdef |
1713 | || (bfd_is_com_section (sec) | |
0a36a439 | 1714 | && (oldweak || oldfunc))) |
45d6a902 AM |
1715 | && olddyn |
1716 | && olddef | |
f5385ebf | 1717 | && h->def_dynamic) |
45d6a902 AM |
1718 | { |
1719 | /* Change the hash table entry to undefined, and let | |
1720 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1721 | new definition. */ | |
1722 | ||
1723 | h->root.type = bfd_link_hash_undefined; | |
1724 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1725 | *size_change_ok = TRUE; | |
1726 | ||
1727 | olddef = FALSE; | |
1728 | olddyncommon = FALSE; | |
1729 | ||
1730 | /* We again permit a type change when a common symbol may be | |
1731 | overriding a function. */ | |
1732 | ||
1733 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1734 | { |
1735 | if (oldfunc) | |
1736 | { | |
1737 | /* If a common symbol overrides a function, make sure | |
1738 | that it isn't defined dynamically nor has type | |
1739 | function. */ | |
1740 | h->def_dynamic = 0; | |
1741 | h->type = STT_NOTYPE; | |
1742 | } | |
1743 | *type_change_ok = TRUE; | |
1744 | } | |
45d6a902 | 1745 | |
6c9b78e6 AM |
1746 | if (hi->root.type == bfd_link_hash_indirect) |
1747 | flip = hi; | |
45d6a902 AM |
1748 | else |
1749 | /* This union may have been set to be non-NULL when this symbol | |
1750 | was seen in a dynamic object. We must force the union to be | |
1751 | NULL, so that it is correct for a regular symbol. */ | |
1752 | h->verinfo.vertree = NULL; | |
1753 | } | |
1754 | ||
1755 | /* Handle the special case of a new common symbol merging with an | |
1756 | old symbol that looks like it might be a common symbol defined in | |
1757 | a shared object. Note that we have already handled the case in | |
1758 | which a new common symbol should simply override the definition | |
1759 | in the shared library. */ | |
1760 | ||
1761 | if (! newdyn | |
1762 | && bfd_is_com_section (sec) | |
1763 | && olddyncommon) | |
1764 | { | |
1765 | /* It would be best if we could set the hash table entry to a | |
1766 | common symbol, but we don't know what to use for the section | |
1767 | or the alignment. */ | |
1a72702b AM |
1768 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1769 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1770 | |
4cc11e76 | 1771 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1772 | larger, pretend that the new symbol has its size. */ |
1773 | ||
1774 | if (h->size > *pvalue) | |
1775 | *pvalue = h->size; | |
1776 | ||
af44c138 L |
1777 | /* We need to remember the alignment required by the symbol |
1778 | in the dynamic object. */ | |
1779 | BFD_ASSERT (pold_alignment); | |
1780 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 AM |
1781 | |
1782 | olddef = FALSE; | |
1783 | olddyncommon = FALSE; | |
1784 | ||
1785 | h->root.type = bfd_link_hash_undefined; | |
1786 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1787 | ||
1788 | *size_change_ok = TRUE; | |
1789 | *type_change_ok = TRUE; | |
1790 | ||
6c9b78e6 AM |
1791 | if (hi->root.type == bfd_link_hash_indirect) |
1792 | flip = hi; | |
45d6a902 AM |
1793 | else |
1794 | h->verinfo.vertree = NULL; | |
1795 | } | |
1796 | ||
1797 | if (flip != NULL) | |
1798 | { | |
1799 | /* Handle the case where we had a versioned symbol in a dynamic | |
1800 | library and now find a definition in a normal object. In this | |
1801 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1802 | flip->root.type = h->root.type; |
00cbee0a | 1803 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1804 | h->root.type = bfd_link_hash_indirect; |
1805 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1806 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1807 | if (h->def_dynamic) |
45d6a902 | 1808 | { |
f5385ebf AM |
1809 | h->def_dynamic = 0; |
1810 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1811 | } |
1812 | } | |
1813 | ||
45d6a902 AM |
1814 | return TRUE; |
1815 | } | |
1816 | ||
1817 | /* This function is called to create an indirect symbol from the | |
1818 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1819 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1820 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1821 | |
28caa186 | 1822 | static bfd_boolean |
268b6b39 AM |
1823 | _bfd_elf_add_default_symbol (bfd *abfd, |
1824 | struct bfd_link_info *info, | |
1825 | struct elf_link_hash_entry *h, | |
1826 | const char *name, | |
1827 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1828 | asection *sec, |
1829 | bfd_vma value, | |
1830 | bfd **poldbfd, | |
e3c9d234 | 1831 | bfd_boolean *dynsym) |
45d6a902 AM |
1832 | { |
1833 | bfd_boolean type_change_ok; | |
1834 | bfd_boolean size_change_ok; | |
1835 | bfd_boolean skip; | |
1836 | char *shortname; | |
1837 | struct elf_link_hash_entry *hi; | |
1838 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1839 | const struct elf_backend_data *bed; |
45d6a902 AM |
1840 | bfd_boolean collect; |
1841 | bfd_boolean dynamic; | |
e3c9d234 | 1842 | bfd_boolean override; |
45d6a902 AM |
1843 | char *p; |
1844 | size_t len, shortlen; | |
ffd65175 | 1845 | asection *tmp_sec; |
6e33951e | 1846 | bfd_boolean matched; |
45d6a902 | 1847 | |
422f1182 L |
1848 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
1849 | return TRUE; | |
1850 | ||
45d6a902 AM |
1851 | /* If this symbol has a version, and it is the default version, we |
1852 | create an indirect symbol from the default name to the fully | |
1853 | decorated name. This will cause external references which do not | |
1854 | specify a version to be bound to this version of the symbol. */ | |
1855 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1856 | if (h->versioned == unknown) |
1857 | { | |
1858 | if (p == NULL) | |
1859 | { | |
1860 | h->versioned = unversioned; | |
1861 | return TRUE; | |
1862 | } | |
1863 | else | |
1864 | { | |
1865 | if (p[1] != ELF_VER_CHR) | |
1866 | { | |
1867 | h->versioned = versioned_hidden; | |
1868 | return TRUE; | |
1869 | } | |
1870 | else | |
1871 | h->versioned = versioned; | |
1872 | } | |
1873 | } | |
4373f8af L |
1874 | else |
1875 | { | |
1876 | /* PR ld/19073: We may see an unversioned definition after the | |
1877 | default version. */ | |
1878 | if (p == NULL) | |
1879 | return TRUE; | |
1880 | } | |
45d6a902 | 1881 | |
45d6a902 AM |
1882 | bed = get_elf_backend_data (abfd); |
1883 | collect = bed->collect; | |
1884 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1885 | ||
1886 | shortlen = p - name; | |
a50b1753 | 1887 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 AM |
1888 | if (shortname == NULL) |
1889 | return FALSE; | |
1890 | memcpy (shortname, name, shortlen); | |
1891 | shortname[shortlen] = '\0'; | |
1892 | ||
1893 | /* We are going to create a new symbol. Merge it with any existing | |
1894 | symbol with this name. For the purposes of the merge, act as | |
1895 | though we were defining the symbol we just defined, although we | |
1896 | actually going to define an indirect symbol. */ | |
1897 | type_change_ok = FALSE; | |
1898 | size_change_ok = FALSE; | |
6e33951e | 1899 | matched = TRUE; |
ffd65175 AM |
1900 | tmp_sec = sec; |
1901 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
4f3fedcf | 1902 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1903 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
1904 | return FALSE; |
1905 | ||
1906 | if (skip) | |
1907 | goto nondefault; | |
1908 | ||
5b677558 AM |
1909 | if (hi->def_regular) |
1910 | { | |
1911 | /* If the undecorated symbol will have a version added by a | |
1912 | script different to H, then don't indirect to/from the | |
1913 | undecorated symbol. This isn't ideal because we may not yet | |
1914 | have seen symbol versions, if given by a script on the | |
1915 | command line rather than via --version-script. */ | |
1916 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1917 | { | |
1918 | bfd_boolean hide; | |
1919 | ||
1920 | hi->verinfo.vertree | |
1921 | = bfd_find_version_for_sym (info->version_info, | |
1922 | hi->root.root.string, &hide); | |
1923 | if (hi->verinfo.vertree != NULL && hide) | |
1924 | { | |
1925 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
1926 | goto nondefault; | |
1927 | } | |
1928 | } | |
1929 | if (hi->verinfo.vertree != NULL | |
1930 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
1931 | goto nondefault; | |
1932 | } | |
1933 | ||
45d6a902 AM |
1934 | if (! override) |
1935 | { | |
c6e8a9a8 | 1936 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1937 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1938 | { |
1939 | bh = &hi->root; | |
fbcc8baf | 1940 | if (bh->type == bfd_link_hash_defined |
6cc71b82 | 1941 | && bh->u.def.section->owner != NULL |
fbcc8baf L |
1942 | && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0) |
1943 | { | |
1944 | /* Mark the previous definition from IR object as | |
1945 | undefined so that the generic linker will override | |
1946 | it. */ | |
1947 | bh->type = bfd_link_hash_undefined; | |
1948 | bh->u.undef.abfd = bh->u.def.section->owner; | |
1949 | } | |
c6e8a9a8 L |
1950 | if (! (_bfd_generic_link_add_one_symbol |
1951 | (info, abfd, shortname, BSF_INDIRECT, | |
1952 | bfd_ind_section_ptr, | |
1953 | 0, name, FALSE, collect, &bh))) | |
1954 | return FALSE; | |
1955 | hi = (struct elf_link_hash_entry *) bh; | |
1956 | } | |
45d6a902 AM |
1957 | } |
1958 | else | |
1959 | { | |
1960 | /* In this case the symbol named SHORTNAME is overriding the | |
1961 | indirect symbol we want to add. We were planning on making | |
1962 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1963 | is the name without a version. NAME is the fully versioned | |
1964 | name, and it is the default version. | |
1965 | ||
1966 | Overriding means that we already saw a definition for the | |
1967 | symbol SHORTNAME in a regular object, and it is overriding | |
1968 | the symbol defined in the dynamic object. | |
1969 | ||
1970 | When this happens, we actually want to change NAME, the | |
1971 | symbol we just added, to refer to SHORTNAME. This will cause | |
1972 | references to NAME in the shared object to become references | |
1973 | to SHORTNAME in the regular object. This is what we expect | |
1974 | when we override a function in a shared object: that the | |
1975 | references in the shared object will be mapped to the | |
1976 | definition in the regular object. */ | |
1977 | ||
1978 | while (hi->root.type == bfd_link_hash_indirect | |
1979 | || hi->root.type == bfd_link_hash_warning) | |
1980 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1981 | ||
1982 | h->root.type = bfd_link_hash_indirect; | |
1983 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1984 | if (h->def_dynamic) |
45d6a902 | 1985 | { |
f5385ebf AM |
1986 | h->def_dynamic = 0; |
1987 | hi->ref_dynamic = 1; | |
1988 | if (hi->ref_regular | |
1989 | || hi->def_regular) | |
45d6a902 | 1990 | { |
c152c796 | 1991 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1992 | return FALSE; |
1993 | } | |
1994 | } | |
1995 | ||
1996 | /* Now set HI to H, so that the following code will set the | |
1997 | other fields correctly. */ | |
1998 | hi = h; | |
1999 | } | |
2000 | ||
fab4a87f L |
2001 | /* Check if HI is a warning symbol. */ |
2002 | if (hi->root.type == bfd_link_hash_warning) | |
2003 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
2004 | ||
45d6a902 AM |
2005 | /* If there is a duplicate definition somewhere, then HI may not |
2006 | point to an indirect symbol. We will have reported an error to | |
2007 | the user in that case. */ | |
2008 | ||
2009 | if (hi->root.type == bfd_link_hash_indirect) | |
2010 | { | |
2011 | struct elf_link_hash_entry *ht; | |
2012 | ||
45d6a902 | 2013 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 2014 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 2015 | |
68c88cd4 AM |
2016 | /* A reference to the SHORTNAME symbol from a dynamic library |
2017 | will be satisfied by the versioned symbol at runtime. In | |
2018 | effect, we have a reference to the versioned symbol. */ | |
2019 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
2020 | hi->dynamic_def |= ht->dynamic_def; | |
2021 | ||
45d6a902 AM |
2022 | /* See if the new flags lead us to realize that the symbol must |
2023 | be dynamic. */ | |
2024 | if (! *dynsym) | |
2025 | { | |
2026 | if (! dynamic) | |
2027 | { | |
0e1862bb | 2028 | if (! bfd_link_executable (info) |
90c984fc | 2029 | || hi->def_dynamic |
f5385ebf | 2030 | || hi->ref_dynamic) |
45d6a902 AM |
2031 | *dynsym = TRUE; |
2032 | } | |
2033 | else | |
2034 | { | |
f5385ebf | 2035 | if (hi->ref_regular) |
45d6a902 AM |
2036 | *dynsym = TRUE; |
2037 | } | |
2038 | } | |
2039 | } | |
2040 | ||
2041 | /* We also need to define an indirection from the nondefault version | |
2042 | of the symbol. */ | |
2043 | ||
2044 | nondefault: | |
2045 | len = strlen (name); | |
a50b1753 | 2046 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 AM |
2047 | if (shortname == NULL) |
2048 | return FALSE; | |
2049 | memcpy (shortname, name, shortlen); | |
2050 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
2051 | ||
2052 | /* Once again, merge with any existing symbol. */ | |
2053 | type_change_ok = FALSE; | |
2054 | size_change_ok = FALSE; | |
ffd65175 AM |
2055 | tmp_sec = sec; |
2056 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
115c6d5c | 2057 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 2058 | &type_change_ok, &size_change_ok, &matched)) |
45d6a902 AM |
2059 | return FALSE; |
2060 | ||
2061 | if (skip) | |
2062 | return TRUE; | |
2063 | ||
2064 | if (override) | |
2065 | { | |
2066 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
2067 | the type of override we do in the case above unless it is | |
4cc11e76 | 2068 | overridden by a versioned definition. */ |
45d6a902 AM |
2069 | if (hi->root.type != bfd_link_hash_defined |
2070 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 2071 | _bfd_error_handler |
695344c0 | 2072 | /* xgettext:c-format */ |
871b3ab2 | 2073 | (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"), |
d003868e | 2074 | abfd, shortname); |
45d6a902 AM |
2075 | } |
2076 | else | |
2077 | { | |
2078 | bh = &hi->root; | |
2079 | if (! (_bfd_generic_link_add_one_symbol | |
2080 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 2081 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
2082 | return FALSE; |
2083 | hi = (struct elf_link_hash_entry *) bh; | |
2084 | ||
2085 | /* If there is a duplicate definition somewhere, then HI may not | |
2086 | point to an indirect symbol. We will have reported an error | |
2087 | to the user in that case. */ | |
2088 | ||
2089 | if (hi->root.type == bfd_link_hash_indirect) | |
2090 | { | |
fcfa13d2 | 2091 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
68c88cd4 AM |
2092 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; |
2093 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 AM |
2094 | |
2095 | /* See if the new flags lead us to realize that the symbol | |
2096 | must be dynamic. */ | |
2097 | if (! *dynsym) | |
2098 | { | |
2099 | if (! dynamic) | |
2100 | { | |
0e1862bb | 2101 | if (! bfd_link_executable (info) |
f5385ebf | 2102 | || hi->ref_dynamic) |
45d6a902 AM |
2103 | *dynsym = TRUE; |
2104 | } | |
2105 | else | |
2106 | { | |
f5385ebf | 2107 | if (hi->ref_regular) |
45d6a902 AM |
2108 | *dynsym = TRUE; |
2109 | } | |
2110 | } | |
2111 | } | |
2112 | } | |
2113 | ||
2114 | return TRUE; | |
2115 | } | |
2116 | \f | |
2117 | /* This routine is used to export all defined symbols into the dynamic | |
2118 | symbol table. It is called via elf_link_hash_traverse. */ | |
2119 | ||
28caa186 | 2120 | static bfd_boolean |
268b6b39 | 2121 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2122 | { |
a50b1753 | 2123 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
2124 | |
2125 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2126 | if (h->root.type == bfd_link_hash_indirect) | |
2127 | return TRUE; | |
2128 | ||
7686d77d AM |
2129 | /* Ignore this if we won't export it. */ |
2130 | if (!eif->info->export_dynamic && !h->dynamic) | |
2131 | return TRUE; | |
45d6a902 AM |
2132 | |
2133 | if (h->dynindx == -1 | |
fd91d419 L |
2134 | && (h->def_regular || h->ref_regular) |
2135 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2136 | h->root.root.string)) | |
45d6a902 | 2137 | { |
fd91d419 | 2138 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2139 | { |
fd91d419 L |
2140 | eif->failed = TRUE; |
2141 | return FALSE; | |
45d6a902 AM |
2142 | } |
2143 | } | |
2144 | ||
2145 | return TRUE; | |
2146 | } | |
2147 | \f | |
2148 | /* Look through the symbols which are defined in other shared | |
2149 | libraries and referenced here. Update the list of version | |
2150 | dependencies. This will be put into the .gnu.version_r section. | |
2151 | This function is called via elf_link_hash_traverse. */ | |
2152 | ||
28caa186 | 2153 | static bfd_boolean |
268b6b39 AM |
2154 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2155 | void *data) | |
45d6a902 | 2156 | { |
a50b1753 | 2157 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2158 | Elf_Internal_Verneed *t; |
2159 | Elf_Internal_Vernaux *a; | |
2160 | bfd_size_type amt; | |
2161 | ||
45d6a902 AM |
2162 | /* We only care about symbols defined in shared objects with version |
2163 | information. */ | |
f5385ebf AM |
2164 | if (!h->def_dynamic |
2165 | || h->def_regular | |
45d6a902 | 2166 | || h->dynindx == -1 |
7b20f099 AM |
2167 | || h->verinfo.verdef == NULL |
2168 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2169 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
45d6a902 AM |
2170 | return TRUE; |
2171 | ||
2172 | /* See if we already know about this version. */ | |
28caa186 AM |
2173 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2174 | t != NULL; | |
2175 | t = t->vn_nextref) | |
45d6a902 AM |
2176 | { |
2177 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2178 | continue; | |
2179 | ||
2180 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2181 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2182 | return TRUE; | |
2183 | ||
2184 | break; | |
2185 | } | |
2186 | ||
2187 | /* This is a new version. Add it to tree we are building. */ | |
2188 | ||
2189 | if (t == NULL) | |
2190 | { | |
2191 | amt = sizeof *t; | |
a50b1753 | 2192 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2193 | if (t == NULL) |
2194 | { | |
2195 | rinfo->failed = TRUE; | |
2196 | return FALSE; | |
2197 | } | |
2198 | ||
2199 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2200 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2201 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2202 | } |
2203 | ||
2204 | amt = sizeof *a; | |
a50b1753 | 2205 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2206 | if (a == NULL) |
2207 | { | |
2208 | rinfo->failed = TRUE; | |
2209 | return FALSE; | |
2210 | } | |
45d6a902 AM |
2211 | |
2212 | /* Note that we are copying a string pointer here, and testing it | |
2213 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2214 | discard the string data when low in memory, this will have to be | |
2215 | fixed. */ | |
2216 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2217 | ||
2218 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2219 | a->vna_nextptr = t->vn_auxptr; | |
2220 | ||
2221 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2222 | ++rinfo->vers; | |
2223 | ||
2224 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2225 | ||
2226 | t->vn_auxptr = a; | |
2227 | ||
2228 | return TRUE; | |
2229 | } | |
2230 | ||
099bb8fb L |
2231 | /* Return TRUE and set *HIDE to TRUE if the versioned symbol is |
2232 | hidden. Set *T_P to NULL if there is no match. */ | |
2233 | ||
2234 | static bfd_boolean | |
2235 | _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info, | |
2236 | struct elf_link_hash_entry *h, | |
2237 | const char *version_p, | |
2238 | struct bfd_elf_version_tree **t_p, | |
2239 | bfd_boolean *hide) | |
2240 | { | |
2241 | struct bfd_elf_version_tree *t; | |
2242 | ||
2243 | /* Look for the version. If we find it, it is no longer weak. */ | |
2244 | for (t = info->version_info; t != NULL; t = t->next) | |
2245 | { | |
2246 | if (strcmp (t->name, version_p) == 0) | |
2247 | { | |
2248 | size_t len; | |
2249 | char *alc; | |
2250 | struct bfd_elf_version_expr *d; | |
2251 | ||
2252 | len = version_p - h->root.root.string; | |
2253 | alc = (char *) bfd_malloc (len); | |
2254 | if (alc == NULL) | |
2255 | return FALSE; | |
2256 | memcpy (alc, h->root.root.string, len - 1); | |
2257 | alc[len - 1] = '\0'; | |
2258 | if (alc[len - 2] == ELF_VER_CHR) | |
2259 | alc[len - 2] = '\0'; | |
2260 | ||
2261 | h->verinfo.vertree = t; | |
2262 | t->used = TRUE; | |
2263 | d = NULL; | |
2264 | ||
2265 | if (t->globals.list != NULL) | |
2266 | d = (*t->match) (&t->globals, NULL, alc); | |
2267 | ||
2268 | /* See if there is anything to force this symbol to | |
2269 | local scope. */ | |
2270 | if (d == NULL && t->locals.list != NULL) | |
2271 | { | |
2272 | d = (*t->match) (&t->locals, NULL, alc); | |
2273 | if (d != NULL | |
2274 | && h->dynindx != -1 | |
2275 | && ! info->export_dynamic) | |
2276 | *hide = TRUE; | |
2277 | } | |
2278 | ||
2279 | free (alc); | |
2280 | break; | |
2281 | } | |
2282 | } | |
2283 | ||
2284 | *t_p = t; | |
2285 | ||
2286 | return TRUE; | |
2287 | } | |
2288 | ||
2289 | /* Return TRUE if the symbol H is hidden by version script. */ | |
2290 | ||
2291 | bfd_boolean | |
2292 | _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info, | |
2293 | struct elf_link_hash_entry *h) | |
2294 | { | |
2295 | const char *p; | |
2296 | bfd_boolean hide = FALSE; | |
2297 | const struct elf_backend_data *bed | |
2298 | = get_elf_backend_data (info->output_bfd); | |
2299 | ||
2300 | /* Version script only hides symbols defined in regular objects. */ | |
2301 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) | |
2302 | return TRUE; | |
2303 | ||
2304 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
2305 | if (p != NULL && h->verinfo.vertree == NULL) | |
2306 | { | |
2307 | struct bfd_elf_version_tree *t; | |
2308 | ||
2309 | ++p; | |
2310 | if (*p == ELF_VER_CHR) | |
2311 | ++p; | |
2312 | ||
2313 | if (*p != '\0' | |
2314 | && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide) | |
2315 | && hide) | |
2316 | { | |
2317 | if (hide) | |
2318 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2319 | return TRUE; | |
2320 | } | |
2321 | } | |
2322 | ||
2323 | /* If we don't have a version for this symbol, see if we can find | |
2324 | something. */ | |
2325 | if (h->verinfo.vertree == NULL && info->version_info != NULL) | |
2326 | { | |
2327 | h->verinfo.vertree | |
2328 | = bfd_find_version_for_sym (info->version_info, | |
2329 | h->root.root.string, &hide); | |
2330 | if (h->verinfo.vertree != NULL && hide) | |
2331 | { | |
2332 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2333 | return TRUE; | |
2334 | } | |
2335 | } | |
2336 | ||
2337 | return FALSE; | |
2338 | } | |
2339 | ||
45d6a902 AM |
2340 | /* Figure out appropriate versions for all the symbols. We may not |
2341 | have the version number script until we have read all of the input | |
2342 | files, so until that point we don't know which symbols should be | |
2343 | local. This function is called via elf_link_hash_traverse. */ | |
2344 | ||
28caa186 | 2345 | static bfd_boolean |
268b6b39 | 2346 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2347 | { |
28caa186 | 2348 | struct elf_info_failed *sinfo; |
45d6a902 | 2349 | struct bfd_link_info *info; |
9c5bfbb7 | 2350 | const struct elf_backend_data *bed; |
45d6a902 AM |
2351 | struct elf_info_failed eif; |
2352 | char *p; | |
099bb8fb | 2353 | bfd_boolean hide; |
45d6a902 | 2354 | |
a50b1753 | 2355 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2356 | info = sinfo->info; |
2357 | ||
45d6a902 AM |
2358 | /* Fix the symbol flags. */ |
2359 | eif.failed = FALSE; | |
2360 | eif.info = info; | |
2361 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2362 | { | |
2363 | if (eif.failed) | |
2364 | sinfo->failed = TRUE; | |
2365 | return FALSE; | |
2366 | } | |
2367 | ||
0a640d71 L |
2368 | bed = get_elf_backend_data (info->output_bfd); |
2369 | ||
45d6a902 AM |
2370 | /* We only need version numbers for symbols defined in regular |
2371 | objects. */ | |
f5385ebf | 2372 | if (!h->def_regular) |
0a640d71 L |
2373 | { |
2374 | /* Hide symbols defined in discarded input sections. */ | |
2375 | if ((h->root.type == bfd_link_hash_defined | |
2376 | || h->root.type == bfd_link_hash_defweak) | |
2377 | && discarded_section (h->root.u.def.section)) | |
2378 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2379 | return TRUE; | |
2380 | } | |
45d6a902 | 2381 | |
099bb8fb | 2382 | hide = FALSE; |
45d6a902 AM |
2383 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2384 | if (p != NULL && h->verinfo.vertree == NULL) | |
2385 | { | |
2386 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2387 | |
45d6a902 AM |
2388 | ++p; |
2389 | if (*p == ELF_VER_CHR) | |
6e33951e | 2390 | ++p; |
45d6a902 AM |
2391 | |
2392 | /* If there is no version string, we can just return out. */ | |
2393 | if (*p == '\0') | |
6e33951e | 2394 | return TRUE; |
45d6a902 | 2395 | |
099bb8fb | 2396 | if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)) |
45d6a902 | 2397 | { |
099bb8fb L |
2398 | sinfo->failed = TRUE; |
2399 | return FALSE; | |
45d6a902 AM |
2400 | } |
2401 | ||
099bb8fb L |
2402 | if (hide) |
2403 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
2404 | ||
45d6a902 AM |
2405 | /* If we are building an application, we need to create a |
2406 | version node for this version. */ | |
0e1862bb | 2407 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2408 | { |
2409 | struct bfd_elf_version_tree **pp; | |
2410 | int version_index; | |
2411 | ||
2412 | /* If we aren't going to export this symbol, we don't need | |
2413 | to worry about it. */ | |
2414 | if (h->dynindx == -1) | |
2415 | return TRUE; | |
2416 | ||
ef53be89 AM |
2417 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2418 | sizeof *t); | |
45d6a902 AM |
2419 | if (t == NULL) |
2420 | { | |
2421 | sinfo->failed = TRUE; | |
2422 | return FALSE; | |
2423 | } | |
2424 | ||
45d6a902 | 2425 | t->name = p; |
45d6a902 AM |
2426 | t->name_indx = (unsigned int) -1; |
2427 | t->used = TRUE; | |
2428 | ||
2429 | version_index = 1; | |
2430 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2431 | if (sinfo->info->version_info != NULL |
2432 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2433 | version_index = 0; |
fd91d419 L |
2434 | for (pp = &sinfo->info->version_info; |
2435 | *pp != NULL; | |
2436 | pp = &(*pp)->next) | |
45d6a902 AM |
2437 | ++version_index; |
2438 | t->vernum = version_index; | |
2439 | ||
2440 | *pp = t; | |
2441 | ||
2442 | h->verinfo.vertree = t; | |
2443 | } | |
2444 | else if (t == NULL) | |
2445 | { | |
2446 | /* We could not find the version for a symbol when | |
2447 | generating a shared archive. Return an error. */ | |
4eca0228 | 2448 | _bfd_error_handler |
695344c0 | 2449 | /* xgettext:c-format */ |
871b3ab2 | 2450 | (_("%pB: version node not found for symbol %s"), |
28caa186 | 2451 | info->output_bfd, h->root.root.string); |
45d6a902 AM |
2452 | bfd_set_error (bfd_error_bad_value); |
2453 | sinfo->failed = TRUE; | |
2454 | return FALSE; | |
2455 | } | |
45d6a902 AM |
2456 | } |
2457 | ||
2458 | /* If we don't have a version for this symbol, see if we can find | |
2459 | something. */ | |
099bb8fb L |
2460 | if (!hide |
2461 | && h->verinfo.vertree == NULL | |
2462 | && sinfo->info->version_info != NULL) | |
45d6a902 | 2463 | { |
fd91d419 L |
2464 | h->verinfo.vertree |
2465 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2466 | h->root.root.string, &hide); | |
1e8fa21e AM |
2467 | if (h->verinfo.vertree != NULL && hide) |
2468 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
2469 | } |
2470 | ||
2471 | return TRUE; | |
2472 | } | |
2473 | \f | |
45d6a902 AM |
2474 | /* Read and swap the relocs from the section indicated by SHDR. This |
2475 | may be either a REL or a RELA section. The relocations are | |
2476 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2477 | which should have already been allocated to contain enough space. | |
2478 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2479 | relocations should be stored. | |
2480 | ||
2481 | Returns FALSE if something goes wrong. */ | |
2482 | ||
2483 | static bfd_boolean | |
268b6b39 | 2484 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2485 | asection *sec, |
268b6b39 AM |
2486 | Elf_Internal_Shdr *shdr, |
2487 | void *external_relocs, | |
2488 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2489 | { |
9c5bfbb7 | 2490 | const struct elf_backend_data *bed; |
268b6b39 | 2491 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2492 | const bfd_byte *erela; |
2493 | const bfd_byte *erelaend; | |
2494 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2495 | Elf_Internal_Shdr *symtab_hdr; |
2496 | size_t nsyms; | |
45d6a902 | 2497 | |
45d6a902 AM |
2498 | /* Position ourselves at the start of the section. */ |
2499 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
2500 | return FALSE; | |
2501 | ||
2502 | /* Read the relocations. */ | |
2503 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
2504 | return FALSE; | |
2505 | ||
243ef1e0 | 2506 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2507 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2508 | |
45d6a902 AM |
2509 | bed = get_elf_backend_data (abfd); |
2510 | ||
2511 | /* Convert the external relocations to the internal format. */ | |
2512 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2513 | swap_in = bed->s->swap_reloc_in; | |
2514 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2515 | swap_in = bed->s->swap_reloca_in; | |
2516 | else | |
2517 | { | |
2518 | bfd_set_error (bfd_error_wrong_format); | |
2519 | return FALSE; | |
2520 | } | |
2521 | ||
a50b1753 | 2522 | erela = (const bfd_byte *) external_relocs; |
f55b1e32 AM |
2523 | /* Setting erelaend like this and comparing with <= handles case of |
2524 | a fuzzed object with sh_size not a multiple of sh_entsize. */ | |
2525 | erelaend = erela + shdr->sh_size - shdr->sh_entsize; | |
45d6a902 | 2526 | irela = internal_relocs; |
f55b1e32 | 2527 | while (erela <= erelaend) |
45d6a902 | 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 | |
5b9d7a9a AM |
2921 | _bfd_elf_adjust_dynamic_symbol, below. If the def is not |
2922 | bfd_link_hash_defined as it was when put on the alias list | |
2923 | then it must have originally been a versioned symbol (for | |
2924 | which a non-versioned indirect symbol is created) and later | |
2925 | a definition for the non-versioned symbol is found. In that | |
2926 | case the indirection is flipped with the versioned symbol | |
2927 | becoming an indirect pointing at the non-versioned symbol. | |
2928 | Thus, not an alias any more. */ | |
2929 | if (def->def_regular | |
2930 | || def->root.type != bfd_link_hash_defined) | |
60d67dc8 AM |
2931 | { |
2932 | h = def; | |
2933 | while ((h = h->u.alias) != def) | |
2934 | h->is_weakalias = 0; | |
2935 | } | |
45d6a902 | 2936 | else |
a26587ba | 2937 | { |
4e6b54a6 AM |
2938 | while (h->root.type == bfd_link_hash_indirect) |
2939 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4e6b54a6 AM |
2940 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
2941 | || h->root.type == bfd_link_hash_defweak); | |
60d67dc8 | 2942 | BFD_ASSERT (def->def_dynamic); |
60d67dc8 | 2943 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); |
a26587ba | 2944 | } |
45d6a902 AM |
2945 | } |
2946 | ||
2947 | return TRUE; | |
2948 | } | |
2949 | ||
2950 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2951 | called via elf_link_hash_traverse, and also calls itself | |
2952 | recursively. */ | |
2953 | ||
28caa186 | 2954 | static bfd_boolean |
268b6b39 | 2955 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2956 | { |
a50b1753 | 2957 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
559192d8 | 2958 | struct elf_link_hash_table *htab; |
9c5bfbb7 | 2959 | const struct elf_backend_data *bed; |
45d6a902 | 2960 | |
0eddce27 | 2961 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2962 | return FALSE; |
2963 | ||
45d6a902 AM |
2964 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2965 | if (h->root.type == bfd_link_hash_indirect) | |
2966 | return TRUE; | |
2967 | ||
2968 | /* Fix the symbol flags. */ | |
2969 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2970 | return FALSE; | |
2971 | ||
559192d8 AM |
2972 | htab = elf_hash_table (eif->info); |
2973 | bed = get_elf_backend_data (htab->dynobj); | |
2974 | ||
954b63d4 AM |
2975 | if (h->root.type == bfd_link_hash_undefweak) |
2976 | { | |
2977 | if (eif->info->dynamic_undefined_weak == 0) | |
559192d8 | 2978 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); |
954b63d4 AM |
2979 | else if (eif->info->dynamic_undefined_weak > 0 |
2980 | && h->ref_regular | |
2981 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2982 | && !bfd_hide_sym_by_version (eif->info->version_info, | |
2983 | h->root.root.string)) | |
2984 | { | |
2985 | if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2986 | { | |
2987 | eif->failed = TRUE; | |
2988 | return FALSE; | |
2989 | } | |
2990 | } | |
2991 | } | |
2992 | ||
45d6a902 AM |
2993 | /* If this symbol does not require a PLT entry, and it is not |
2994 | defined by a dynamic object, or is not referenced by a regular | |
2995 | object, ignore it. We do have to handle a weak defined symbol, | |
2996 | even if no regular object refers to it, if we decided to add it | |
2997 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2998 | about symbols which are defined by one dynamic object and | |
2999 | referenced by another one? */ | |
f5385ebf | 3000 | if (!h->needs_plt |
91e21fb7 | 3001 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
3002 | && (h->def_regular |
3003 | || !h->def_dynamic | |
3004 | || (!h->ref_regular | |
60d67dc8 | 3005 | && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) |
45d6a902 | 3006 | { |
a6aa5195 | 3007 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
45d6a902 AM |
3008 | return TRUE; |
3009 | } | |
3010 | ||
3011 | /* If we've already adjusted this symbol, don't do it again. This | |
3012 | can happen via a recursive call. */ | |
f5385ebf | 3013 | if (h->dynamic_adjusted) |
45d6a902 AM |
3014 | return TRUE; |
3015 | ||
3016 | /* Don't look at this symbol again. Note that we must set this | |
3017 | after checking the above conditions, because we may look at a | |
3018 | symbol once, decide not to do anything, and then get called | |
3019 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 3020 | h->dynamic_adjusted = 1; |
45d6a902 AM |
3021 | |
3022 | /* If this is a weak definition, and we know a real definition, and | |
3023 | the real symbol is not itself defined by a regular object file, | |
3024 | then get a good value for the real definition. We handle the | |
3025 | real symbol first, for the convenience of the backend routine. | |
3026 | ||
3027 | Note that there is a confusing case here. If the real definition | |
3028 | is defined by a regular object file, we don't get the real symbol | |
3029 | from the dynamic object, but we do get the weak symbol. If the | |
3030 | processor backend uses a COPY reloc, then if some routine in the | |
3031 | dynamic object changes the real symbol, we will not see that | |
3032 | change in the corresponding weak symbol. This is the way other | |
3033 | ELF linkers work as well, and seems to be a result of the shared | |
3034 | library model. | |
3035 | ||
3036 | I will clarify this issue. Most SVR4 shared libraries define the | |
3037 | variable _timezone and define timezone as a weak synonym. The | |
3038 | tzset call changes _timezone. If you write | |
3039 | extern int timezone; | |
3040 | int _timezone = 5; | |
3041 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
3042 | you might expect that, since timezone is a synonym for _timezone, | |
3043 | the same number will print both times. However, if the processor | |
3044 | backend uses a COPY reloc, then actually timezone will be copied | |
3045 | into your process image, and, since you define _timezone | |
3046 | yourself, _timezone will not. Thus timezone and _timezone will | |
3047 | wind up at different memory locations. The tzset call will set | |
3048 | _timezone, leaving timezone unchanged. */ | |
3049 | ||
60d67dc8 | 3050 | if (h->is_weakalias) |
45d6a902 | 3051 | { |
60d67dc8 AM |
3052 | struct elf_link_hash_entry *def = weakdef (h); |
3053 | ||
ec24dc88 | 3054 | /* If we get to this point, there is an implicit reference to |
60d67dc8 AM |
3055 | the alias by a regular object file via the weak symbol H. */ |
3056 | def->ref_regular = 1; | |
45d6a902 | 3057 | |
ec24dc88 | 3058 | /* Ensure that the backend adjust_dynamic_symbol function sees |
60d67dc8 AM |
3059 | the strong alias before H by recursively calling ourselves. */ |
3060 | if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) | |
45d6a902 AM |
3061 | return FALSE; |
3062 | } | |
3063 | ||
3064 | /* If a symbol has no type and no size and does not require a PLT | |
3065 | entry, then we are probably about to do the wrong thing here: we | |
3066 | are probably going to create a COPY reloc for an empty object. | |
3067 | This case can arise when a shared object is built with assembly | |
3068 | code, and the assembly code fails to set the symbol type. */ | |
3069 | if (h->size == 0 | |
3070 | && h->type == STT_NOTYPE | |
f5385ebf | 3071 | && !h->needs_plt) |
4eca0228 | 3072 | _bfd_error_handler |
45d6a902 AM |
3073 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
3074 | h->root.root.string); | |
3075 | ||
45d6a902 AM |
3076 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
3077 | { | |
3078 | eif->failed = TRUE; | |
3079 | return FALSE; | |
3080 | } | |
3081 | ||
3082 | return TRUE; | |
3083 | } | |
3084 | ||
027297b7 L |
3085 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
3086 | DYNBSS. */ | |
3087 | ||
3088 | bfd_boolean | |
6cabe1ea AM |
3089 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
3090 | struct elf_link_hash_entry *h, | |
027297b7 L |
3091 | asection *dynbss) |
3092 | { | |
91ac5911 | 3093 | unsigned int power_of_two; |
027297b7 L |
3094 | bfd_vma mask; |
3095 | asection *sec = h->root.u.def.section; | |
3096 | ||
de194d85 | 3097 | /* The section alignment of the definition is the maximum alignment |
91ac5911 L |
3098 | requirement of symbols defined in the section. Since we don't |
3099 | know the symbol alignment requirement, we start with the | |
3100 | maximum alignment and check low bits of the symbol address | |
3101 | for the minimum alignment. */ | |
3102 | power_of_two = bfd_get_section_alignment (sec->owner, sec); | |
3103 | mask = ((bfd_vma) 1 << power_of_two) - 1; | |
3104 | while ((h->root.u.def.value & mask) != 0) | |
3105 | { | |
3106 | mask >>= 1; | |
3107 | --power_of_two; | |
3108 | } | |
027297b7 | 3109 | |
91ac5911 L |
3110 | if (power_of_two > bfd_get_section_alignment (dynbss->owner, |
3111 | dynbss)) | |
027297b7 L |
3112 | { |
3113 | /* Adjust the section alignment if needed. */ | |
3114 | if (! bfd_set_section_alignment (dynbss->owner, dynbss, | |
91ac5911 | 3115 | power_of_two)) |
027297b7 L |
3116 | return FALSE; |
3117 | } | |
3118 | ||
91ac5911 | 3119 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
3120 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
3121 | ||
3122 | /* Define the symbol as being at this point in DYNBSS. */ | |
3123 | h->root.u.def.section = dynbss; | |
3124 | h->root.u.def.value = dynbss->size; | |
3125 | ||
3126 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
3127 | dynbss->size += h->size; | |
3128 | ||
f7483970 L |
3129 | /* No error if extern_protected_data is true. */ |
3130 | if (h->protected_def | |
889c2a67 L |
3131 | && (!info->extern_protected_data |
3132 | || (info->extern_protected_data < 0 | |
3133 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 | 3134 | info->callbacks->einfo |
c1c8c1ef | 3135 | (_("%P: copy reloc against protected `%pT' is dangerous\n"), |
d07a1b05 | 3136 | h->root.root.string); |
6cabe1ea | 3137 | |
027297b7 L |
3138 | return TRUE; |
3139 | } | |
3140 | ||
45d6a902 AM |
3141 | /* Adjust all external symbols pointing into SEC_MERGE sections |
3142 | to reflect the object merging within the sections. */ | |
3143 | ||
28caa186 | 3144 | static bfd_boolean |
268b6b39 | 3145 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
3146 | { |
3147 | asection *sec; | |
3148 | ||
45d6a902 AM |
3149 | if ((h->root.type == bfd_link_hash_defined |
3150 | || h->root.type == bfd_link_hash_defweak) | |
3151 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 3152 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 3153 | { |
a50b1753 | 3154 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
3155 | |
3156 | h->root.u.def.value = | |
3157 | _bfd_merged_section_offset (output_bfd, | |
3158 | &h->root.u.def.section, | |
3159 | elf_section_data (sec)->sec_info, | |
753731ee | 3160 | h->root.u.def.value); |
45d6a902 AM |
3161 | } |
3162 | ||
3163 | return TRUE; | |
3164 | } | |
986a241f RH |
3165 | |
3166 | /* Returns false if the symbol referred to by H should be considered | |
3167 | to resolve local to the current module, and true if it should be | |
3168 | considered to bind dynamically. */ | |
3169 | ||
3170 | bfd_boolean | |
268b6b39 AM |
3171 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
3172 | struct bfd_link_info *info, | |
89a2ee5a | 3173 | bfd_boolean not_local_protected) |
986a241f RH |
3174 | { |
3175 | bfd_boolean binding_stays_local_p; | |
fcb93ecf PB |
3176 | const struct elf_backend_data *bed; |
3177 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
3178 | |
3179 | if (h == NULL) | |
3180 | return FALSE; | |
3181 | ||
3182 | while (h->root.type == bfd_link_hash_indirect | |
3183 | || h->root.type == bfd_link_hash_warning) | |
3184 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3185 | ||
3186 | /* If it was forced local, then clearly it's not dynamic. */ | |
3187 | if (h->dynindx == -1) | |
3188 | return FALSE; | |
f5385ebf | 3189 | if (h->forced_local) |
986a241f RH |
3190 | return FALSE; |
3191 | ||
3192 | /* Identify the cases where name binding rules say that a | |
3193 | visible symbol resolves locally. */ | |
0e1862bb L |
3194 | binding_stays_local_p = (bfd_link_executable (info) |
3195 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
3196 | |
3197 | switch (ELF_ST_VISIBILITY (h->other)) | |
3198 | { | |
3199 | case STV_INTERNAL: | |
3200 | case STV_HIDDEN: | |
3201 | return FALSE; | |
3202 | ||
3203 | case STV_PROTECTED: | |
fcb93ecf PB |
3204 | hash_table = elf_hash_table (info); |
3205 | if (!is_elf_hash_table (hash_table)) | |
3206 | return FALSE; | |
3207 | ||
3208 | bed = get_elf_backend_data (hash_table->dynobj); | |
3209 | ||
986a241f RH |
3210 | /* Proper resolution for function pointer equality may require |
3211 | that these symbols perhaps be resolved dynamically, even though | |
3212 | we should be resolving them to the current module. */ | |
89a2ee5a | 3213 | if (!not_local_protected || !bed->is_function_type (h->type)) |
986a241f RH |
3214 | binding_stays_local_p = TRUE; |
3215 | break; | |
3216 | ||
3217 | default: | |
986a241f RH |
3218 | break; |
3219 | } | |
3220 | ||
aa37626c | 3221 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 3222 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
aa37626c L |
3223 | return TRUE; |
3224 | ||
986a241f RH |
3225 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
3226 | us that it remains local. */ | |
3227 | return !binding_stays_local_p; | |
3228 | } | |
f6c52c13 AM |
3229 | |
3230 | /* Return true if the symbol referred to by H should be considered | |
3231 | to resolve local to the current module, and false otherwise. Differs | |
3232 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 3233 | undefined symbols. The two functions are virtually identical except |
0fad2956 MR |
3234 | for the place where dynindx == -1 is tested. If that test is true, |
3235 | _bfd_elf_dynamic_symbol_p will say the symbol is local, while | |
3236 | _bfd_elf_symbol_refs_local_p will say the symbol is local only for | |
3237 | defined symbols. | |
89a2ee5a AM |
3238 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as |
3239 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
3240 | treatment of undefined weak symbols. For those that do not make | |
3241 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 AM |
3242 | |
3243 | bfd_boolean | |
268b6b39 AM |
3244 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3245 | struct bfd_link_info *info, | |
3246 | bfd_boolean local_protected) | |
f6c52c13 | 3247 | { |
fcb93ecf PB |
3248 | const struct elf_backend_data *bed; |
3249 | struct elf_link_hash_table *hash_table; | |
3250 | ||
f6c52c13 AM |
3251 | /* If it's a local sym, of course we resolve locally. */ |
3252 | if (h == NULL) | |
3253 | return TRUE; | |
3254 | ||
d95edcac L |
3255 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3256 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3257 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
3258 | return TRUE; | |
3259 | ||
0fad2956 MR |
3260 | /* Forced local symbols resolve locally. */ |
3261 | if (h->forced_local) | |
3262 | return TRUE; | |
3263 | ||
7e2294f9 AO |
3264 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3265 | flag set, so test it first, and don't bail out. */ | |
3266 | if (ELF_COMMON_DEF_P (h)) | |
3267 | /* Do nothing. */; | |
f6c52c13 | 3268 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3269 | resolve locally. The sym is either undefined or dynamic. */ |
3270 | else if (!h->def_regular) | |
f6c52c13 AM |
3271 | return FALSE; |
3272 | ||
0fad2956 | 3273 | /* Non-dynamic symbols resolve locally. */ |
f6c52c13 AM |
3274 | if (h->dynindx == -1) |
3275 | return TRUE; | |
3276 | ||
3277 | /* At this point, we know the symbol is defined and dynamic. In an | |
3278 | executable it must resolve locally, likewise when building symbolic | |
3279 | shared libraries. */ | |
0e1862bb | 3280 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
f6c52c13 AM |
3281 | return TRUE; |
3282 | ||
3283 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3284 | with default visibility might not resolve locally. */ | |
3285 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
3286 | return FALSE; | |
3287 | ||
fcb93ecf PB |
3288 | hash_table = elf_hash_table (info); |
3289 | if (!is_elf_hash_table (hash_table)) | |
3290 | return TRUE; | |
3291 | ||
3292 | bed = get_elf_backend_data (hash_table->dynobj); | |
3293 | ||
f7483970 L |
3294 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3295 | symbols are local. */ | |
889c2a67 L |
3296 | if ((!info->extern_protected_data |
3297 | || (info->extern_protected_data < 0 | |
3298 | && !bed->extern_protected_data)) | |
3299 | && !bed->is_function_type (h->type)) | |
1c16dfa5 L |
3300 | return TRUE; |
3301 | ||
f6c52c13 | 3302 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3303 | symbols be treated as dynamic symbols. If the address of a |
3304 | function not defined in an executable is set to that function's | |
3305 | plt entry in the executable, then the address of the function in | |
3306 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3307 | return local_protected; |
3308 | } | |
e1918d23 AM |
3309 | |
3310 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3311 | aligned. Returns the first TLS output section. */ | |
3312 | ||
3313 | struct bfd_section * | |
3314 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3315 | { | |
3316 | struct bfd_section *sec, *tls; | |
3317 | unsigned int align = 0; | |
3318 | ||
3319 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3320 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3321 | break; | |
3322 | tls = sec; | |
3323 | ||
3324 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3325 | if (sec->alignment_power > align) | |
3326 | align = sec->alignment_power; | |
3327 | ||
3328 | elf_hash_table (info)->tls_sec = tls; | |
3329 | ||
3330 | /* Ensure the alignment of the first section is the largest alignment, | |
3331 | so that the tls segment starts aligned. */ | |
3332 | if (tls != NULL) | |
3333 | tls->alignment_power = align; | |
3334 | ||
3335 | return tls; | |
3336 | } | |
0ad989f9 L |
3337 | |
3338 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
3339 | static bfd_boolean | |
3340 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
3341 | Elf_Internal_Sym *sym) | |
3342 | { | |
a4d8e49b L |
3343 | const struct elf_backend_data *bed; |
3344 | ||
0ad989f9 L |
3345 | /* Local symbols do not count, but target specific ones might. */ |
3346 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3347 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
3348 | return FALSE; | |
3349 | ||
fcb93ecf | 3350 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3351 | /* Function symbols do not count. */ |
fcb93ecf | 3352 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0ad989f9 L |
3353 | return FALSE; |
3354 | ||
3355 | /* If the section is undefined, then so is the symbol. */ | |
3356 | if (sym->st_shndx == SHN_UNDEF) | |
3357 | return FALSE; | |
3358 | ||
3359 | /* If the symbol is defined in the common section, then | |
3360 | it is a common definition and so does not count. */ | |
a4d8e49b | 3361 | if (bed->common_definition (sym)) |
0ad989f9 L |
3362 | return FALSE; |
3363 | ||
3364 | /* If the symbol is in a target specific section then we | |
3365 | must rely upon the backend to tell us what it is. */ | |
3366 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3367 | /* FIXME - this function is not coded yet: | |
3368 | ||
3369 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3370 | ||
3371 | Instead for now assume that the definition is not global, | |
3372 | Even if this is wrong, at least the linker will behave | |
3373 | in the same way that it used to do. */ | |
3374 | return FALSE; | |
3375 | ||
3376 | return TRUE; | |
3377 | } | |
3378 | ||
3379 | /* Search the symbol table of the archive element of the archive ABFD | |
3380 | whose archive map contains a mention of SYMDEF, and determine if | |
3381 | the symbol is defined in this element. */ | |
3382 | static bfd_boolean | |
3383 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
3384 | { | |
3385 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3386 | size_t symcount; |
3387 | size_t extsymcount; | |
3388 | size_t extsymoff; | |
0ad989f9 L |
3389 | Elf_Internal_Sym *isymbuf; |
3390 | Elf_Internal_Sym *isym; | |
3391 | Elf_Internal_Sym *isymend; | |
3392 | bfd_boolean result; | |
3393 | ||
3394 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3395 | if (abfd == NULL) | |
3396 | return FALSE; | |
3397 | ||
3398 | if (! bfd_check_format (abfd, bfd_object)) | |
3399 | return FALSE; | |
3400 | ||
7dc3990e L |
3401 | /* Select the appropriate symbol table. If we don't know if the |
3402 | object file is an IR object, give linker LTO plugin a chance to | |
3403 | get the correct symbol table. */ | |
3404 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3405 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3406 | || (abfd->plugin_format == bfd_plugin_unknown |
3407 | && bfd_link_plugin_object_p (abfd)) | |
3408 | #endif | |
3409 | ) | |
3410 | { | |
3411 | /* Use the IR symbol table if the object has been claimed by | |
3412 | plugin. */ | |
3413 | abfd = abfd->plugin_dummy_bfd; | |
3414 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3415 | } | |
3416 | else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
0ad989f9 L |
3417 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3418 | else | |
3419 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3420 | ||
3421 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3422 | ||
3423 | /* The sh_info field of the symtab header tells us where the | |
3424 | external symbols start. We don't care about the local symbols. */ | |
3425 | if (elf_bad_symtab (abfd)) | |
3426 | { | |
3427 | extsymcount = symcount; | |
3428 | extsymoff = 0; | |
3429 | } | |
3430 | else | |
3431 | { | |
3432 | extsymcount = symcount - hdr->sh_info; | |
3433 | extsymoff = hdr->sh_info; | |
3434 | } | |
3435 | ||
3436 | if (extsymcount == 0) | |
3437 | return FALSE; | |
3438 | ||
3439 | /* Read in the symbol table. */ | |
3440 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3441 | NULL, NULL, NULL); | |
3442 | if (isymbuf == NULL) | |
3443 | return FALSE; | |
3444 | ||
3445 | /* Scan the symbol table looking for SYMDEF. */ | |
3446 | result = FALSE; | |
3447 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
3448 | { | |
3449 | const char *name; | |
3450 | ||
3451 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3452 | isym->st_name); | |
3453 | if (name == NULL) | |
3454 | break; | |
3455 | ||
3456 | if (strcmp (name, symdef->name) == 0) | |
3457 | { | |
3458 | result = is_global_data_symbol_definition (abfd, isym); | |
3459 | break; | |
3460 | } | |
3461 | } | |
3462 | ||
3463 | free (isymbuf); | |
3464 | ||
3465 | return result; | |
3466 | } | |
3467 | \f | |
5a580b3a AM |
3468 | /* Add an entry to the .dynamic table. */ |
3469 | ||
3470 | bfd_boolean | |
3471 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
3472 | bfd_vma tag, | |
3473 | bfd_vma val) | |
3474 | { | |
3475 | struct elf_link_hash_table *hash_table; | |
3476 | const struct elf_backend_data *bed; | |
3477 | asection *s; | |
3478 | bfd_size_type newsize; | |
3479 | bfd_byte *newcontents; | |
3480 | Elf_Internal_Dyn dyn; | |
3481 | ||
3482 | hash_table = elf_hash_table (info); | |
3483 | if (! is_elf_hash_table (hash_table)) | |
3484 | return FALSE; | |
3485 | ||
7f923b7f AM |
3486 | if (tag == DT_RELA || tag == DT_REL) |
3487 | hash_table->dynamic_relocs = TRUE; | |
3488 | ||
5a580b3a | 3489 | bed = get_elf_backend_data (hash_table->dynobj); |
3d4d4302 | 3490 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3491 | BFD_ASSERT (s != NULL); |
3492 | ||
eea6121a | 3493 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3494 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a AM |
3495 | if (newcontents == NULL) |
3496 | return FALSE; | |
3497 | ||
3498 | dyn.d_tag = tag; | |
3499 | dyn.d_un.d_val = val; | |
eea6121a | 3500 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3501 | |
eea6121a | 3502 | s->size = newsize; |
5a580b3a AM |
3503 | s->contents = newcontents; |
3504 | ||
3505 | return TRUE; | |
3506 | } | |
3507 | ||
3508 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
3509 | otherwise just check whether one already exists. Returns -1 on error, | |
3510 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
3511 | ||
4ad4eba5 | 3512 | static int |
7e9f0867 AM |
3513 | elf_add_dt_needed_tag (bfd *abfd, |
3514 | struct bfd_link_info *info, | |
4ad4eba5 AM |
3515 | const char *soname, |
3516 | bfd_boolean do_it) | |
5a580b3a AM |
3517 | { |
3518 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3519 | size_t strindex; |
5a580b3a | 3520 | |
7e9f0867 AM |
3521 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3522 | return -1; | |
3523 | ||
5a580b3a | 3524 | hash_table = elf_hash_table (info); |
5a580b3a | 3525 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); |
ef53be89 | 3526 | if (strindex == (size_t) -1) |
5a580b3a AM |
3527 | return -1; |
3528 | ||
02be4619 | 3529 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3530 | { |
3531 | asection *sdyn; | |
3532 | const struct elf_backend_data *bed; | |
3533 | bfd_byte *extdyn; | |
3534 | ||
3535 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3536 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
7e9f0867 AM |
3537 | if (sdyn != NULL) |
3538 | for (extdyn = sdyn->contents; | |
3539 | extdyn < sdyn->contents + sdyn->size; | |
3540 | extdyn += bed->s->sizeof_dyn) | |
3541 | { | |
3542 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3543 | |
7e9f0867 AM |
3544 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3545 | if (dyn.d_tag == DT_NEEDED | |
3546 | && dyn.d_un.d_val == strindex) | |
3547 | { | |
3548 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3549 | return 1; | |
3550 | } | |
3551 | } | |
5a580b3a AM |
3552 | } |
3553 | ||
3554 | if (do_it) | |
3555 | { | |
7e9f0867 AM |
3556 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3557 | return -1; | |
3558 | ||
5a580b3a AM |
3559 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3560 | return -1; | |
3561 | } | |
3562 | else | |
3563 | /* We were just checking for existence of the tag. */ | |
3564 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3565 | ||
3566 | return 0; | |
3567 | } | |
3568 | ||
7b15fa7a AM |
3569 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3570 | (or the end of list if STOP is NULL), and needed by a library that | |
3571 | will be loaded. */ | |
3572 | ||
010e5ae2 | 3573 | static bfd_boolean |
7b15fa7a AM |
3574 | on_needed_list (const char *soname, |
3575 | struct bfd_link_needed_list *needed, | |
3576 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3577 | { |
7b15fa7a AM |
3578 | struct bfd_link_needed_list *look; |
3579 | for (look = needed; look != stop; look = look->next) | |
3580 | if (strcmp (soname, look->name) == 0 | |
3581 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3582 | /* If needed by a library that itself is not directly | |
3583 | needed, recursively check whether that library is | |
3584 | indirectly needed. Since we add DT_NEEDED entries to | |
3585 | the end of the list, library dependencies appear after | |
3586 | the library. Therefore search prior to the current | |
3587 | LOOK, preventing possible infinite recursion. */ | |
3588 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
010e5ae2 AM |
3589 | return TRUE; |
3590 | ||
3591 | return FALSE; | |
3592 | } | |
3593 | ||
14160578 | 3594 | /* Sort symbol by value, section, and size. */ |
4ad4eba5 AM |
3595 | static int |
3596 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3597 | { |
3598 | const struct elf_link_hash_entry *h1; | |
3599 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3600 | bfd_signed_vma vdiff; |
5a580b3a AM |
3601 | |
3602 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3603 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3604 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3605 | if (vdiff != 0) | |
3606 | return vdiff > 0 ? 1 : -1; | |
3607 | else | |
3608 | { | |
d3435ae8 | 3609 | int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; |
10b7e05b NC |
3610 | if (sdiff != 0) |
3611 | return sdiff > 0 ? 1 : -1; | |
3612 | } | |
14160578 AM |
3613 | vdiff = h1->size - h2->size; |
3614 | return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; | |
5a580b3a | 3615 | } |
4ad4eba5 | 3616 | |
5a580b3a AM |
3617 | /* This function is used to adjust offsets into .dynstr for |
3618 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3619 | ||
3620 | static bfd_boolean | |
3621 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
3622 | { | |
a50b1753 | 3623 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3624 | |
5a580b3a AM |
3625 | if (h->dynindx != -1) |
3626 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
3627 | return TRUE; | |
3628 | } | |
3629 | ||
3630 | /* Assign string offsets in .dynstr, update all structures referencing | |
3631 | them. */ | |
3632 | ||
4ad4eba5 AM |
3633 | static bfd_boolean |
3634 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
3635 | { |
3636 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3637 | struct elf_link_local_dynamic_entry *entry; | |
3638 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3639 | bfd *dynobj = hash_table->dynobj; | |
3640 | asection *sdyn; | |
3641 | bfd_size_type size; | |
3642 | const struct elf_backend_data *bed; | |
3643 | bfd_byte *extdyn; | |
3644 | ||
3645 | _bfd_elf_strtab_finalize (dynstr); | |
3646 | size = _bfd_elf_strtab_size (dynstr); | |
3647 | ||
3648 | bed = get_elf_backend_data (dynobj); | |
3d4d4302 | 3649 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3650 | BFD_ASSERT (sdyn != NULL); |
3651 | ||
3652 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3653 | for (extdyn = sdyn->contents; | |
eea6121a | 3654 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
3655 | extdyn += bed->s->sizeof_dyn) |
3656 | { | |
3657 | Elf_Internal_Dyn dyn; | |
3658 | ||
3659 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3660 | switch (dyn.d_tag) | |
3661 | { | |
3662 | case DT_STRSZ: | |
3663 | dyn.d_un.d_val = size; | |
3664 | break; | |
3665 | case DT_NEEDED: | |
3666 | case DT_SONAME: | |
3667 | case DT_RPATH: | |
3668 | case DT_RUNPATH: | |
3669 | case DT_FILTER: | |
3670 | case DT_AUXILIARY: | |
7ee314fa AM |
3671 | case DT_AUDIT: |
3672 | case DT_DEPAUDIT: | |
5a580b3a AM |
3673 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3674 | break; | |
3675 | default: | |
3676 | continue; | |
3677 | } | |
3678 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3679 | } | |
3680 | ||
3681 | /* Now update local dynamic symbols. */ | |
3682 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3683 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3684 | entry->isym.st_name); | |
3685 | ||
3686 | /* And the rest of dynamic symbols. */ | |
3687 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3688 | ||
3689 | /* Adjust version definitions. */ | |
3690 | if (elf_tdata (output_bfd)->cverdefs) | |
3691 | { | |
3692 | asection *s; | |
3693 | bfd_byte *p; | |
ef53be89 | 3694 | size_t i; |
5a580b3a AM |
3695 | Elf_Internal_Verdef def; |
3696 | Elf_Internal_Verdaux defaux; | |
3697 | ||
3d4d4302 | 3698 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
3699 | p = s->contents; |
3700 | do | |
3701 | { | |
3702 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
3703 | &def); | |
3704 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
3705 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
3706 | continue; | |
5a580b3a AM |
3707 | for (i = 0; i < def.vd_cnt; ++i) |
3708 | { | |
3709 | _bfd_elf_swap_verdaux_in (output_bfd, | |
3710 | (Elf_External_Verdaux *) p, &defaux); | |
3711 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
3712 | defaux.vda_name); | |
3713 | _bfd_elf_swap_verdaux_out (output_bfd, | |
3714 | &defaux, (Elf_External_Verdaux *) p); | |
3715 | p += sizeof (Elf_External_Verdaux); | |
3716 | } | |
3717 | } | |
3718 | while (def.vd_next); | |
3719 | } | |
3720 | ||
3721 | /* Adjust version references. */ | |
3722 | if (elf_tdata (output_bfd)->verref) | |
3723 | { | |
3724 | asection *s; | |
3725 | bfd_byte *p; | |
ef53be89 | 3726 | size_t i; |
5a580b3a AM |
3727 | Elf_Internal_Verneed need; |
3728 | Elf_Internal_Vernaux needaux; | |
3729 | ||
3d4d4302 | 3730 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
3731 | p = s->contents; |
3732 | do | |
3733 | { | |
3734 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
3735 | &need); | |
3736 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
3737 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
3738 | (Elf_External_Verneed *) p); | |
3739 | p += sizeof (Elf_External_Verneed); | |
3740 | for (i = 0; i < need.vn_cnt; ++i) | |
3741 | { | |
3742 | _bfd_elf_swap_vernaux_in (output_bfd, | |
3743 | (Elf_External_Vernaux *) p, &needaux); | |
3744 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
3745 | needaux.vna_name); | |
3746 | _bfd_elf_swap_vernaux_out (output_bfd, | |
3747 | &needaux, | |
3748 | (Elf_External_Vernaux *) p); | |
3749 | p += sizeof (Elf_External_Vernaux); | |
3750 | } | |
3751 | } | |
3752 | while (need.vn_next); | |
3753 | } | |
3754 | ||
3755 | return TRUE; | |
3756 | } | |
3757 | \f | |
13285a1b AM |
3758 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
3759 | The default is to only match when the INPUT and OUTPUT are exactly | |
3760 | the same target. */ | |
3761 | ||
3762 | bfd_boolean | |
3763 | _bfd_elf_default_relocs_compatible (const bfd_target *input, | |
3764 | const bfd_target *output) | |
3765 | { | |
3766 | return input == output; | |
3767 | } | |
3768 | ||
3769 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
3770 | This version is used when different targets for the same architecture | |
3771 | are virtually identical. */ | |
3772 | ||
3773 | bfd_boolean | |
3774 | _bfd_elf_relocs_compatible (const bfd_target *input, | |
3775 | const bfd_target *output) | |
3776 | { | |
3777 | const struct elf_backend_data *obed, *ibed; | |
3778 | ||
3779 | if (input == output) | |
3780 | return TRUE; | |
3781 | ||
3782 | ibed = xvec_get_elf_backend_data (input); | |
3783 | obed = xvec_get_elf_backend_data (output); | |
3784 | ||
3785 | if (ibed->arch != obed->arch) | |
3786 | return FALSE; | |
3787 | ||
3788 | /* If both backends are using this function, deem them compatible. */ | |
3789 | return ibed->relocs_compatible == obed->relocs_compatible; | |
3790 | } | |
3791 | ||
e5034e59 AM |
3792 | /* Make a special call to the linker "notice" function to tell it that |
3793 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 3794 | processing the lib. */ |
e5034e59 AM |
3795 | |
3796 | bfd_boolean | |
3797 | _bfd_elf_notice_as_needed (bfd *ibfd, | |
3798 | struct bfd_link_info *info, | |
3799 | enum notice_asneeded_action act) | |
3800 | { | |
46135103 | 3801 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
3802 | } |
3803 | ||
d9689752 L |
3804 | /* Check relocations an ELF object file. */ |
3805 | ||
3806 | bfd_boolean | |
3807 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
3808 | { | |
3809 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3810 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
3811 | ||
3812 | /* If this object is the same format as the output object, and it is | |
3813 | not a shared library, then let the backend look through the | |
3814 | relocs. | |
3815 | ||
3816 | This is required to build global offset table entries and to | |
3817 | arrange for dynamic relocs. It is not required for the | |
3818 | particular common case of linking non PIC code, even when linking | |
3819 | against shared libraries, but unfortunately there is no way of | |
3820 | knowing whether an object file has been compiled PIC or not. | |
3821 | Looking through the relocs is not particularly time consuming. | |
3822 | The problem is that we must either (1) keep the relocs in memory, | |
3823 | which causes the linker to require additional runtime memory or | |
3824 | (2) read the relocs twice from the input file, which wastes time. | |
3825 | This would be a good case for using mmap. | |
3826 | ||
3827 | I have no idea how to handle linking PIC code into a file of a | |
3828 | different format. It probably can't be done. */ | |
3829 | if ((abfd->flags & DYNAMIC) == 0 | |
3830 | && is_elf_hash_table (htab) | |
3831 | && bed->check_relocs != NULL | |
3832 | && elf_object_id (abfd) == elf_hash_table_id (htab) | |
3833 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
3834 | { | |
3835 | asection *o; | |
3836 | ||
3837 | for (o = abfd->sections; o != NULL; o = o->next) | |
3838 | { | |
3839 | Elf_Internal_Rela *internal_relocs; | |
3840 | bfd_boolean ok; | |
3841 | ||
5ce03cea | 3842 | /* Don't check relocations in excluded sections. */ |
d9689752 | 3843 | if ((o->flags & SEC_RELOC) == 0 |
5ce03cea | 3844 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
3845 | || o->reloc_count == 0 |
3846 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
3847 | && (o->flags & SEC_DEBUGGING) != 0) | |
3848 | || bfd_is_abs_section (o->output_section)) | |
3849 | continue; | |
3850 | ||
3851 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
3852 | info->keep_memory); | |
3853 | if (internal_relocs == NULL) | |
3854 | return FALSE; | |
3855 | ||
3856 | ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); | |
3857 | ||
3858 | if (elf_section_data (o)->relocs != internal_relocs) | |
3859 | free (internal_relocs); | |
3860 | ||
3861 | if (! ok) | |
3862 | return FALSE; | |
3863 | } | |
3864 | } | |
3865 | ||
3866 | return TRUE; | |
3867 | } | |
3868 | ||
4ad4eba5 AM |
3869 | /* Add symbols from an ELF object file to the linker hash table. */ |
3870 | ||
3871 | static bfd_boolean | |
3872 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
3873 | { | |
a0c402a5 | 3874 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 3875 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
3876 | size_t symcount; |
3877 | size_t extsymcount; | |
3878 | size_t extsymoff; | |
4ad4eba5 AM |
3879 | struct elf_link_hash_entry **sym_hash; |
3880 | bfd_boolean dynamic; | |
3881 | Elf_External_Versym *extversym = NULL; | |
be22c732 | 3882 | Elf_External_Versym *extversym_end = NULL; |
4ad4eba5 AM |
3883 | Elf_External_Versym *ever; |
3884 | struct elf_link_hash_entry *weaks; | |
3885 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 3886 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
3887 | Elf_Internal_Sym *isymbuf = NULL; |
3888 | Elf_Internal_Sym *isym; | |
3889 | Elf_Internal_Sym *isymend; | |
3890 | const struct elf_backend_data *bed; | |
3891 | bfd_boolean add_needed; | |
66eb6687 | 3892 | struct elf_link_hash_table *htab; |
4ad4eba5 | 3893 | bfd_size_type amt; |
66eb6687 | 3894 | void *alloc_mark = NULL; |
4f87808c AM |
3895 | struct bfd_hash_entry **old_table = NULL; |
3896 | unsigned int old_size = 0; | |
3897 | unsigned int old_count = 0; | |
66eb6687 | 3898 | void *old_tab = NULL; |
66eb6687 AM |
3899 | void *old_ent; |
3900 | struct bfd_link_hash_entry *old_undefs = NULL; | |
3901 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 3902 | void *old_strtab = NULL; |
66eb6687 | 3903 | size_t tabsize = 0; |
db6a5d5f | 3904 | asection *s; |
29a9f53e | 3905 | bfd_boolean just_syms; |
4ad4eba5 | 3906 | |
66eb6687 | 3907 | htab = elf_hash_table (info); |
4ad4eba5 | 3908 | bed = get_elf_backend_data (abfd); |
4ad4eba5 AM |
3909 | |
3910 | if ((abfd->flags & DYNAMIC) == 0) | |
3911 | dynamic = FALSE; | |
3912 | else | |
3913 | { | |
3914 | dynamic = TRUE; | |
3915 | ||
3916 | /* You can't use -r against a dynamic object. Also, there's no | |
3917 | hope of using a dynamic object which does not exactly match | |
3918 | the format of the output file. */ | |
0e1862bb | 3919 | if (bfd_link_relocatable (info) |
66eb6687 | 3920 | || !is_elf_hash_table (htab) |
f13a99db | 3921 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 3922 | { |
0e1862bb | 3923 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
3924 | bfd_set_error (bfd_error_invalid_operation); |
3925 | else | |
3926 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3927 | goto error_return; |
3928 | } | |
3929 | } | |
3930 | ||
a0c402a5 L |
3931 | ehdr = elf_elfheader (abfd); |
3932 | if (info->warn_alternate_em | |
3933 | && bed->elf_machine_code != ehdr->e_machine | |
3934 | && ((bed->elf_machine_alt1 != 0 | |
3935 | && ehdr->e_machine == bed->elf_machine_alt1) | |
3936 | || (bed->elf_machine_alt2 != 0 | |
3937 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
9793eb77 | 3938 | _bfd_error_handler |
695344c0 | 3939 | /* xgettext:c-format */ |
9793eb77 | 3940 | (_("alternate ELF machine code found (%d) in %pB, expecting %d"), |
a0c402a5 L |
3941 | ehdr->e_machine, abfd, bed->elf_machine_code); |
3942 | ||
4ad4eba5 AM |
3943 | /* As a GNU extension, any input sections which are named |
3944 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3945 | symbol. This differs from .gnu.warning sections, which generate | |
3946 | warnings when they are included in an output file. */ | |
dd98f8d2 | 3947 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 3948 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 3949 | { |
db6a5d5f | 3950 | const char *name; |
4ad4eba5 | 3951 | |
db6a5d5f AM |
3952 | name = bfd_get_section_name (abfd, s); |
3953 | if (CONST_STRNEQ (name, ".gnu.warning.")) | |
4ad4eba5 | 3954 | { |
db6a5d5f AM |
3955 | char *msg; |
3956 | bfd_size_type sz; | |
3957 | ||
3958 | name += sizeof ".gnu.warning." - 1; | |
3959 | ||
3960 | /* If this is a shared object, then look up the symbol | |
3961 | in the hash table. If it is there, and it is already | |
3962 | been defined, then we will not be using the entry | |
3963 | from this shared object, so we don't need to warn. | |
3964 | FIXME: If we see the definition in a regular object | |
3965 | later on, we will warn, but we shouldn't. The only | |
3966 | fix is to keep track of what warnings we are supposed | |
3967 | to emit, and then handle them all at the end of the | |
3968 | link. */ | |
3969 | if (dynamic) | |
4ad4eba5 | 3970 | { |
db6a5d5f AM |
3971 | struct elf_link_hash_entry *h; |
3972 | ||
3973 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); | |
3974 | ||
3975 | /* FIXME: What about bfd_link_hash_common? */ | |
3976 | if (h != NULL | |
3977 | && (h->root.type == bfd_link_hash_defined | |
3978 | || h->root.type == bfd_link_hash_defweak)) | |
3979 | continue; | |
3980 | } | |
4ad4eba5 | 3981 | |
db6a5d5f AM |
3982 | sz = s->size; |
3983 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
3984 | if (msg == NULL) | |
3985 | goto error_return; | |
4ad4eba5 | 3986 | |
db6a5d5f AM |
3987 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
3988 | goto error_return; | |
4ad4eba5 | 3989 | |
db6a5d5f | 3990 | msg[sz] = '\0'; |
4ad4eba5 | 3991 | |
db6a5d5f AM |
3992 | if (! (_bfd_generic_link_add_one_symbol |
3993 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3994 | FALSE, bed->collect, NULL))) | |
3995 | goto error_return; | |
4ad4eba5 | 3996 | |
0e1862bb | 3997 | if (bfd_link_executable (info)) |
db6a5d5f AM |
3998 | { |
3999 | /* Clobber the section size so that the warning does | |
4000 | not get copied into the output file. */ | |
4001 | s->size = 0; | |
11d2f718 | 4002 | |
db6a5d5f AM |
4003 | /* Also set SEC_EXCLUDE, so that symbols defined in |
4004 | the warning section don't get copied to the output. */ | |
4005 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
4006 | } |
4007 | } | |
4008 | } | |
4009 | ||
29a9f53e L |
4010 | just_syms = ((s = abfd->sections) != NULL |
4011 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
4012 | ||
4ad4eba5 AM |
4013 | add_needed = TRUE; |
4014 | if (! dynamic) | |
4015 | { | |
4016 | /* If we are creating a shared library, create all the dynamic | |
4017 | sections immediately. We need to attach them to something, | |
4018 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
4019 | format and is not from ld --just-symbols. Always create the |
4020 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
4021 | are no input BFD's of the same format as the output, we can't |
4022 | make a shared library. */ | |
4023 | if (!just_syms | |
bf89386a | 4024 | && (bfd_link_pic (info) |
9c1d7a08 | 4025 | || (!bfd_link_relocatable (info) |
3c5fce9b | 4026 | && info->nointerp |
9c1d7a08 | 4027 | && (info->export_dynamic || info->dynamic))) |
66eb6687 | 4028 | && is_elf_hash_table (htab) |
f13a99db | 4029 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 4030 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
4031 | { |
4032 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
4033 | goto error_return; | |
4034 | } | |
4035 | } | |
66eb6687 | 4036 | else if (!is_elf_hash_table (htab)) |
4ad4eba5 AM |
4037 | goto error_return; |
4038 | else | |
4039 | { | |
4ad4eba5 | 4040 | const char *soname = NULL; |
7ee314fa | 4041 | char *audit = NULL; |
4ad4eba5 | 4042 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
9acc85a6 | 4043 | const Elf_Internal_Phdr *phdr; |
4ad4eba5 AM |
4044 | int ret; |
4045 | ||
4046 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 4047 | ld shouldn't allow it. */ |
29a9f53e | 4048 | if (just_syms) |
92fd189d | 4049 | abort (); |
4ad4eba5 AM |
4050 | |
4051 | /* If this dynamic lib was specified on the command line with | |
4052 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
4053 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
4054 | in by another lib's DT_NEEDED. When --no-add-needed is used |
4055 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
4056 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
4057 | all. */ | |
4058 | add_needed = (elf_dyn_lib_class (abfd) | |
4059 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
4060 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
4061 | |
4062 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
4063 | if (s != NULL) | |
4064 | { | |
4065 | bfd_byte *dynbuf; | |
4066 | bfd_byte *extdyn; | |
cb33740c | 4067 | unsigned int elfsec; |
4ad4eba5 AM |
4068 | unsigned long shlink; |
4069 | ||
eea6121a | 4070 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 L |
4071 | { |
4072 | error_free_dyn: | |
4073 | free (dynbuf); | |
4074 | goto error_return; | |
4075 | } | |
4ad4eba5 AM |
4076 | |
4077 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 4078 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
4079 | goto error_free_dyn; |
4080 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
4081 | ||
4082 | for (extdyn = dynbuf; | |
9bff840e | 4083 | extdyn <= dynbuf + s->size - bed->s->sizeof_dyn; |
4ad4eba5 AM |
4084 | extdyn += bed->s->sizeof_dyn) |
4085 | { | |
4086 | Elf_Internal_Dyn dyn; | |
4087 | ||
4088 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
4089 | if (dyn.d_tag == DT_SONAME) | |
4090 | { | |
4091 | unsigned int tagv = dyn.d_un.d_val; | |
4092 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4093 | if (soname == NULL) | |
4094 | goto error_free_dyn; | |
4095 | } | |
4096 | if (dyn.d_tag == DT_NEEDED) | |
4097 | { | |
4098 | struct bfd_link_needed_list *n, **pn; | |
4099 | char *fnm, *anm; | |
4100 | unsigned int tagv = dyn.d_un.d_val; | |
4101 | ||
4102 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4103 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4104 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4105 | if (n == NULL || fnm == NULL) | |
4106 | goto error_free_dyn; | |
4107 | amt = strlen (fnm) + 1; | |
a50b1753 | 4108 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4109 | if (anm == NULL) |
4110 | goto error_free_dyn; | |
4111 | memcpy (anm, fnm, amt); | |
4112 | n->name = anm; | |
4113 | n->by = abfd; | |
4114 | n->next = NULL; | |
66eb6687 | 4115 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4116 | ; |
4117 | *pn = n; | |
4118 | } | |
4119 | if (dyn.d_tag == DT_RUNPATH) | |
4120 | { | |
4121 | struct bfd_link_needed_list *n, **pn; | |
4122 | char *fnm, *anm; | |
4123 | unsigned int tagv = dyn.d_un.d_val; | |
4124 | ||
4125 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4126 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4127 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4128 | if (n == NULL || fnm == NULL) | |
4129 | goto error_free_dyn; | |
4130 | amt = strlen (fnm) + 1; | |
a50b1753 | 4131 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4132 | if (anm == NULL) |
4133 | goto error_free_dyn; | |
4134 | memcpy (anm, fnm, amt); | |
4135 | n->name = anm; | |
4136 | n->by = abfd; | |
4137 | n->next = NULL; | |
4138 | for (pn = & runpath; | |
4139 | *pn != NULL; | |
4140 | pn = &(*pn)->next) | |
4141 | ; | |
4142 | *pn = n; | |
4143 | } | |
4144 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
4145 | if (!runpath && dyn.d_tag == DT_RPATH) | |
4146 | { | |
4147 | struct bfd_link_needed_list *n, **pn; | |
4148 | char *fnm, *anm; | |
4149 | unsigned int tagv = dyn.d_un.d_val; | |
4150 | ||
4151 | amt = sizeof (struct bfd_link_needed_list); | |
a50b1753 | 4152 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4153 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4154 | if (n == NULL || fnm == NULL) | |
4155 | goto error_free_dyn; | |
4156 | amt = strlen (fnm) + 1; | |
a50b1753 | 4157 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 4158 | if (anm == NULL) |
f8703194 | 4159 | goto error_free_dyn; |
4ad4eba5 AM |
4160 | memcpy (anm, fnm, amt); |
4161 | n->name = anm; | |
4162 | n->by = abfd; | |
4163 | n->next = NULL; | |
4164 | for (pn = & rpath; | |
4165 | *pn != NULL; | |
4166 | pn = &(*pn)->next) | |
4167 | ; | |
4168 | *pn = n; | |
4169 | } | |
7ee314fa AM |
4170 | if (dyn.d_tag == DT_AUDIT) |
4171 | { | |
4172 | unsigned int tagv = dyn.d_un.d_val; | |
4173 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4174 | } | |
4ad4eba5 AM |
4175 | } |
4176 | ||
4177 | free (dynbuf); | |
4178 | } | |
4179 | ||
4180 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
4181 | frees all more recently bfd_alloc'd blocks as well. */ | |
4182 | if (runpath) | |
4183 | rpath = runpath; | |
4184 | ||
4185 | if (rpath) | |
4186 | { | |
4187 | struct bfd_link_needed_list **pn; | |
66eb6687 | 4188 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4189 | ; |
4190 | *pn = rpath; | |
4191 | } | |
4192 | ||
9acc85a6 AM |
4193 | /* If we have a PT_GNU_RELRO program header, mark as read-only |
4194 | all sections contained fully therein. This makes relro | |
4195 | shared library sections appear as they will at run-time. */ | |
4196 | phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; | |
54025d58 | 4197 | while (phdr-- > elf_tdata (abfd)->phdr) |
9acc85a6 AM |
4198 | if (phdr->p_type == PT_GNU_RELRO) |
4199 | { | |
4200 | for (s = abfd->sections; s != NULL; s = s->next) | |
4201 | if ((s->flags & SEC_ALLOC) != 0 | |
4202 | && s->vma >= phdr->p_vaddr | |
4203 | && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) | |
4204 | s->flags |= SEC_READONLY; | |
4205 | break; | |
4206 | } | |
4207 | ||
4ad4eba5 AM |
4208 | /* We do not want to include any of the sections in a dynamic |
4209 | object in the output file. We hack by simply clobbering the | |
4210 | list of sections in the BFD. This could be handled more | |
4211 | cleanly by, say, a new section flag; the existing | |
4212 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4213 | still implies that the section takes up space in the output | |
4214 | file. */ | |
4215 | bfd_section_list_clear (abfd); | |
4216 | ||
4ad4eba5 AM |
4217 | /* Find the name to use in a DT_NEEDED entry that refers to this |
4218 | object. If the object has a DT_SONAME entry, we use it. | |
4219 | Otherwise, if the generic linker stuck something in | |
4220 | elf_dt_name, we use that. Otherwise, we just use the file | |
4221 | name. */ | |
4222 | if (soname == NULL || *soname == '\0') | |
4223 | { | |
4224 | soname = elf_dt_name (abfd); | |
4225 | if (soname == NULL || *soname == '\0') | |
4226 | soname = bfd_get_filename (abfd); | |
4227 | } | |
4228 | ||
4229 | /* Save the SONAME because sometimes the linker emulation code | |
4230 | will need to know it. */ | |
4231 | elf_dt_name (abfd) = soname; | |
4232 | ||
7e9f0867 | 4233 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
4234 | if (ret < 0) |
4235 | goto error_return; | |
4236 | ||
4237 | /* If we have already included this dynamic object in the | |
4238 | link, just ignore it. There is no reason to include a | |
4239 | particular dynamic object more than once. */ | |
4240 | if (ret > 0) | |
4241 | return TRUE; | |
7ee314fa AM |
4242 | |
4243 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 4244 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
4245 | } |
4246 | ||
4247 | /* If this is a dynamic object, we always link against the .dynsym | |
4248 | symbol table, not the .symtab symbol table. The dynamic linker | |
4249 | will only see the .dynsym symbol table, so there is no reason to | |
4250 | look at .symtab for a dynamic object. */ | |
4251 | ||
4252 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
4253 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4254 | else | |
4255 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4256 | ||
4257 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
4258 | ||
4259 | /* The sh_info field of the symtab header tells us where the | |
4260 | external symbols start. We don't care about the local symbols at | |
4261 | this point. */ | |
4262 | if (elf_bad_symtab (abfd)) | |
4263 | { | |
4264 | extsymcount = symcount; | |
4265 | extsymoff = 0; | |
4266 | } | |
4267 | else | |
4268 | { | |
4269 | extsymcount = symcount - hdr->sh_info; | |
4270 | extsymoff = hdr->sh_info; | |
4271 | } | |
4272 | ||
f45794cb | 4273 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4274 | if (extsymcount != 0) |
4ad4eba5 AM |
4275 | { |
4276 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4277 | NULL, NULL, NULL); | |
4278 | if (isymbuf == NULL) | |
4279 | goto error_return; | |
4280 | ||
4ad4eba5 | 4281 | if (sym_hash == NULL) |
012b2306 AM |
4282 | { |
4283 | /* We store a pointer to the hash table entry for each | |
4284 | external symbol. */ | |
ef53be89 AM |
4285 | amt = extsymcount; |
4286 | amt *= sizeof (struct elf_link_hash_entry *); | |
012b2306 AM |
4287 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4288 | if (sym_hash == NULL) | |
4289 | goto error_free_sym; | |
4290 | elf_sym_hashes (abfd) = sym_hash; | |
4291 | } | |
4ad4eba5 AM |
4292 | } |
4293 | ||
4294 | if (dynamic) | |
4295 | { | |
4296 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4297 | if (!_bfd_elf_slurp_version_tables (abfd, |
4298 | info->default_imported_symver)) | |
4ad4eba5 AM |
4299 | goto error_free_sym; |
4300 | ||
4301 | /* Read in the symbol versions, but don't bother to convert them | |
4302 | to internal format. */ | |
4303 | if (elf_dynversym (abfd) != 0) | |
4304 | { | |
4305 | Elf_Internal_Shdr *versymhdr; | |
4306 | ||
4307 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
be22c732 NC |
4308 | amt = versymhdr->sh_size; |
4309 | extversym = (Elf_External_Versym *) bfd_malloc (amt); | |
4ad4eba5 AM |
4310 | if (extversym == NULL) |
4311 | goto error_free_sym; | |
4ad4eba5 AM |
4312 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 |
4313 | || bfd_bread (extversym, amt, abfd) != amt) | |
4314 | goto error_free_vers; | |
be22c732 | 4315 | extversym_end = extversym + (amt / sizeof (* extversym)); |
4ad4eba5 AM |
4316 | } |
4317 | } | |
4318 | ||
66eb6687 AM |
4319 | /* If we are loading an as-needed shared lib, save the symbol table |
4320 | state before we start adding symbols. If the lib turns out | |
4321 | to be unneeded, restore the state. */ | |
4322 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4323 | { | |
4324 | unsigned int i; | |
4325 | size_t entsize; | |
4326 | ||
4327 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4328 | { | |
4329 | struct bfd_hash_entry *p; | |
2de92251 | 4330 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4331 | |
4332 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4333 | { |
4334 | h = (struct elf_link_hash_entry *) p; | |
4335 | entsize += htab->root.table.entsize; | |
4336 | if (h->root.type == bfd_link_hash_warning) | |
4337 | entsize += htab->root.table.entsize; | |
4338 | } | |
66eb6687 AM |
4339 | } |
4340 | ||
4341 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4342 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4343 | if (old_tab == NULL) |
4344 | goto error_free_vers; | |
4345 | ||
4346 | /* Remember the current objalloc pointer, so that all mem for | |
4347 | symbols added can later be reclaimed. */ | |
4348 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4349 | if (alloc_mark == NULL) | |
4350 | goto error_free_vers; | |
4351 | ||
5061a885 AM |
4352 | /* Make a special call to the linker "notice" function to |
4353 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4354 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4355 | goto error_free_vers; |
5061a885 | 4356 | |
f45794cb AM |
4357 | /* Clone the symbol table. Remember some pointers into the |
4358 | symbol table, and dynamic symbol count. */ | |
4359 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4360 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4361 | old_undefs = htab->root.undefs; |
4362 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4363 | old_table = htab->root.table.table; |
4364 | old_size = htab->root.table.size; | |
4365 | old_count = htab->root.table.count; | |
5b677558 AM |
4366 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); |
4367 | if (old_strtab == NULL) | |
4368 | goto error_free_vers; | |
66eb6687 AM |
4369 | |
4370 | for (i = 0; i < htab->root.table.size; i++) | |
4371 | { | |
4372 | struct bfd_hash_entry *p; | |
2de92251 | 4373 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4374 | |
4375 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4376 | { | |
4377 | memcpy (old_ent, p, htab->root.table.entsize); | |
4378 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4379 | h = (struct elf_link_hash_entry *) p; |
4380 | if (h->root.type == bfd_link_hash_warning) | |
4381 | { | |
4382 | memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); | |
4383 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
4384 | } | |
66eb6687 AM |
4385 | } |
4386 | } | |
4387 | } | |
4ad4eba5 | 4388 | |
66eb6687 | 4389 | weaks = NULL; |
be22c732 NC |
4390 | if (extversym == NULL) |
4391 | ever = NULL; | |
4392 | else if (extversym + extsymoff < extversym_end) | |
4393 | ever = extversym + extsymoff; | |
4394 | else | |
4395 | { | |
4396 | /* xgettext:c-format */ | |
4397 | _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"), | |
4398 | abfd, (long) extsymoff, | |
4399 | (long) (extversym_end - extversym) / sizeof (* extversym)); | |
4400 | bfd_set_error (bfd_error_bad_value); | |
4401 | goto error_free_vers; | |
4402 | } | |
4403 | ||
cc5277b1 ML |
4404 | if (abfd->lto_slim_object) |
4405 | { | |
4406 | _bfd_error_handler | |
4407 | (_("%pB: plugin needed to handle lto object"), abfd); | |
4408 | } | |
4409 | ||
4ad4eba5 AM |
4410 | for (isym = isymbuf, isymend = isymbuf + extsymcount; |
4411 | isym < isymend; | |
4412 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4413 | { | |
4414 | int bind; | |
4415 | bfd_vma value; | |
af44c138 | 4416 | asection *sec, *new_sec; |
4ad4eba5 AM |
4417 | flagword flags; |
4418 | const char *name; | |
4419 | struct elf_link_hash_entry *h; | |
90c984fc | 4420 | struct elf_link_hash_entry *hi; |
4ad4eba5 AM |
4421 | bfd_boolean definition; |
4422 | bfd_boolean size_change_ok; | |
4423 | bfd_boolean type_change_ok; | |
37a9e49a L |
4424 | bfd_boolean new_weak; |
4425 | bfd_boolean old_weak; | |
4ad4eba5 | 4426 | bfd_boolean override; |
a4d8e49b | 4427 | bfd_boolean common; |
97196564 | 4428 | bfd_boolean discarded; |
4ad4eba5 | 4429 | unsigned int old_alignment; |
4538d1c7 | 4430 | unsigned int shindex; |
4ad4eba5 | 4431 | bfd *old_bfd; |
6e33951e | 4432 | bfd_boolean matched; |
4ad4eba5 AM |
4433 | |
4434 | override = FALSE; | |
4435 | ||
4436 | flags = BSF_NO_FLAGS; | |
4437 | sec = NULL; | |
4438 | value = isym->st_value; | |
a4d8e49b | 4439 | common = bed->common_definition (isym); |
2980ccad L |
4440 | if (common && info->inhibit_common_definition) |
4441 | { | |
4442 | /* Treat common symbol as undefined for --no-define-common. */ | |
4443 | isym->st_shndx = SHN_UNDEF; | |
4444 | common = FALSE; | |
4445 | } | |
97196564 | 4446 | discarded = FALSE; |
4ad4eba5 AM |
4447 | |
4448 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4449 | switch (bind) |
4ad4eba5 | 4450 | { |
3e7a7d11 | 4451 | case STB_LOCAL: |
4ad4eba5 AM |
4452 | /* This should be impossible, since ELF requires that all |
4453 | global symbols follow all local symbols, and that sh_info | |
4454 | point to the first global symbol. Unfortunately, Irix 5 | |
4455 | screws this up. */ | |
fe3fef62 AM |
4456 | if (elf_bad_symtab (abfd)) |
4457 | continue; | |
4458 | ||
4459 | /* If we aren't prepared to handle locals within the globals | |
4538d1c7 AM |
4460 | then we'll likely segfault on a NULL symbol hash if the |
4461 | symbol is ever referenced in relocations. */ | |
4462 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
4463 | name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name); | |
4464 | _bfd_error_handler (_("%pB: %s local symbol at index %lu" | |
4465 | " (>= sh_info of %lu)"), | |
4466 | abfd, name, (long) (isym - isymbuf + extsymoff), | |
4467 | (long) extsymoff); | |
4468 | ||
4469 | /* Dynamic object relocations are not processed by ld, so | |
4470 | ld won't run into the problem mentioned above. */ | |
4471 | if (dynamic) | |
4472 | continue; | |
fe3fef62 AM |
4473 | bfd_set_error (bfd_error_bad_value); |
4474 | goto error_free_vers; | |
3e7a7d11 NC |
4475 | |
4476 | case STB_GLOBAL: | |
a4d8e49b | 4477 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4478 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4479 | break; |
4480 | ||
4481 | case STB_WEAK: | |
4482 | flags = BSF_WEAK; | |
4483 | break; | |
4484 | ||
4485 | case STB_GNU_UNIQUE: | |
4486 | flags = BSF_GNU_UNIQUE; | |
4487 | break; | |
4488 | ||
4489 | default: | |
4ad4eba5 | 4490 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4491 | break; |
4ad4eba5 AM |
4492 | } |
4493 | ||
4494 | if (isym->st_shndx == SHN_UNDEF) | |
4495 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4496 | else if (isym->st_shndx == SHN_ABS) |
4497 | sec = bfd_abs_section_ptr; | |
4498 | else if (isym->st_shndx == SHN_COMMON) | |
4499 | { | |
4500 | sec = bfd_com_section_ptr; | |
4501 | /* What ELF calls the size we call the value. What ELF | |
4502 | calls the value we call the alignment. */ | |
4503 | value = isym->st_size; | |
4504 | } | |
4505 | else | |
4ad4eba5 AM |
4506 | { |
4507 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4508 | if (sec == NULL) | |
4509 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4510 | else if (discarded_section (sec)) |
529fcb95 | 4511 | { |
e5d08002 L |
4512 | /* Symbols from discarded section are undefined. We keep |
4513 | its visibility. */ | |
529fcb95 | 4514 | sec = bfd_und_section_ptr; |
97196564 | 4515 | discarded = TRUE; |
529fcb95 PB |
4516 | isym->st_shndx = SHN_UNDEF; |
4517 | } | |
4ad4eba5 AM |
4518 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4519 | value -= sec->vma; | |
4520 | } | |
4ad4eba5 AM |
4521 | |
4522 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4523 | isym->st_name); | |
4524 | if (name == NULL) | |
4525 | goto error_free_vers; | |
4526 | ||
4527 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4528 | && (abfd->flags & BFD_PLUGIN) != 0) |
4529 | { | |
4530 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4531 | ||
4532 | if (xc == NULL) | |
4533 | { | |
4534 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4535 | | SEC_EXCLUDE); | |
4536 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4537 | if (xc == NULL) | |
4538 | goto error_free_vers; | |
4539 | } | |
4540 | sec = xc; | |
4541 | } | |
4542 | else if (isym->st_shndx == SHN_COMMON | |
4543 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4544 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4545 | { |
4546 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4547 | ||
4548 | if (tcomm == NULL) | |
4549 | { | |
02d00247 AM |
4550 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4551 | | SEC_LINKER_CREATED); | |
4552 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4553 | if (tcomm == NULL) |
4ad4eba5 AM |
4554 | goto error_free_vers; |
4555 | } | |
4556 | sec = tcomm; | |
4557 | } | |
66eb6687 | 4558 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4559 | { |
66eb6687 AM |
4560 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4561 | &sec, &value)) | |
4ad4eba5 AM |
4562 | goto error_free_vers; |
4563 | ||
4564 | /* The hook function sets the name to NULL if this symbol | |
4565 | should be skipped for some reason. */ | |
4566 | if (name == NULL) | |
4567 | continue; | |
4568 | } | |
4569 | ||
4570 | /* Sanity check that all possibilities were handled. */ | |
4571 | if (sec == NULL) | |
4538d1c7 | 4572 | abort (); |
4ad4eba5 | 4573 | |
191c0c42 AM |
4574 | /* Silently discard TLS symbols from --just-syms. There's |
4575 | no way to combine a static TLS block with a new TLS block | |
4576 | for this executable. */ | |
4577 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4578 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4579 | continue; | |
4580 | ||
4ad4eba5 AM |
4581 | if (bfd_is_und_section (sec) |
4582 | || bfd_is_com_section (sec)) | |
4583 | definition = FALSE; | |
4584 | else | |
4585 | definition = TRUE; | |
4586 | ||
4587 | size_change_ok = FALSE; | |
66eb6687 | 4588 | type_change_ok = bed->type_change_ok; |
37a9e49a | 4589 | old_weak = FALSE; |
6e33951e | 4590 | matched = FALSE; |
4ad4eba5 AM |
4591 | old_alignment = 0; |
4592 | old_bfd = NULL; | |
af44c138 | 4593 | new_sec = sec; |
4ad4eba5 | 4594 | |
66eb6687 | 4595 | if (is_elf_hash_table (htab)) |
4ad4eba5 AM |
4596 | { |
4597 | Elf_Internal_Versym iver; | |
4598 | unsigned int vernum = 0; | |
4599 | bfd_boolean skip; | |
4600 | ||
fc0e6df6 | 4601 | if (ever == NULL) |
4ad4eba5 | 4602 | { |
fc0e6df6 PB |
4603 | if (info->default_imported_symver) |
4604 | /* Use the default symbol version created earlier. */ | |
4605 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4606 | else | |
4607 | iver.vs_vers = 0; | |
4608 | } | |
be22c732 NC |
4609 | else if (ever >= extversym_end) |
4610 | { | |
4611 | /* xgettext:c-format */ | |
4612 | _bfd_error_handler (_("%pB: not enough version information"), | |
4613 | abfd); | |
4614 | bfd_set_error (bfd_error_bad_value); | |
4615 | goto error_free_vers; | |
4616 | } | |
fc0e6df6 PB |
4617 | else |
4618 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4619 | ||
4620 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4621 | ||
4622 | /* If this is a hidden symbol, or if it is not version | |
4623 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4624 | However, we do not modify a non-hidden absolute symbol |
4625 | if it is not a function, because it might be the version | |
4626 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4627 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4628 | || (vernum > 1 |
4629 | && (!bfd_is_abs_section (sec) | |
4630 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4631 | { |
4632 | const char *verstr; | |
4633 | size_t namelen, verlen, newlen; | |
4634 | char *newname, *p; | |
4635 | ||
4636 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 4637 | { |
fc0e6df6 PB |
4638 | if (vernum > elf_tdata (abfd)->cverdefs) |
4639 | verstr = NULL; | |
4640 | else if (vernum > 1) | |
4641 | verstr = | |
4642 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
4643 | else | |
4644 | verstr = ""; | |
4ad4eba5 | 4645 | |
fc0e6df6 | 4646 | if (verstr == NULL) |
4ad4eba5 | 4647 | { |
4eca0228 | 4648 | _bfd_error_handler |
695344c0 | 4649 | /* xgettext:c-format */ |
871b3ab2 | 4650 | (_("%pB: %s: invalid version %u (max %d)"), |
fc0e6df6 PB |
4651 | abfd, name, vernum, |
4652 | elf_tdata (abfd)->cverdefs); | |
4653 | bfd_set_error (bfd_error_bad_value); | |
4654 | goto error_free_vers; | |
4ad4eba5 | 4655 | } |
fc0e6df6 PB |
4656 | } |
4657 | else | |
4658 | { | |
4659 | /* We cannot simply test for the number of | |
4660 | entries in the VERNEED section since the | |
4661 | numbers for the needed versions do not start | |
4662 | at 0. */ | |
4663 | Elf_Internal_Verneed *t; | |
4664 | ||
4665 | verstr = NULL; | |
4666 | for (t = elf_tdata (abfd)->verref; | |
4667 | t != NULL; | |
4668 | t = t->vn_nextref) | |
4ad4eba5 | 4669 | { |
fc0e6df6 | 4670 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 4671 | |
fc0e6df6 PB |
4672 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
4673 | { | |
4674 | if (a->vna_other == vernum) | |
4ad4eba5 | 4675 | { |
fc0e6df6 PB |
4676 | verstr = a->vna_nodename; |
4677 | break; | |
4ad4eba5 | 4678 | } |
4ad4eba5 | 4679 | } |
fc0e6df6 PB |
4680 | if (a != NULL) |
4681 | break; | |
4682 | } | |
4683 | if (verstr == NULL) | |
4684 | { | |
4eca0228 | 4685 | _bfd_error_handler |
695344c0 | 4686 | /* xgettext:c-format */ |
871b3ab2 | 4687 | (_("%pB: %s: invalid needed version %d"), |
fc0e6df6 PB |
4688 | abfd, name, vernum); |
4689 | bfd_set_error (bfd_error_bad_value); | |
4690 | goto error_free_vers; | |
4ad4eba5 | 4691 | } |
4ad4eba5 | 4692 | } |
fc0e6df6 PB |
4693 | |
4694 | namelen = strlen (name); | |
4695 | verlen = strlen (verstr); | |
4696 | newlen = namelen + verlen + 2; | |
4697 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4698 | && isym->st_shndx != SHN_UNDEF) | |
4699 | ++newlen; | |
4700 | ||
a50b1753 | 4701 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
4702 | if (newname == NULL) |
4703 | goto error_free_vers; | |
4704 | memcpy (newname, name, namelen); | |
4705 | p = newname + namelen; | |
4706 | *p++ = ELF_VER_CHR; | |
4707 | /* If this is a defined non-hidden version symbol, | |
4708 | we add another @ to the name. This indicates the | |
4709 | default version of the symbol. */ | |
4710 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
4711 | && isym->st_shndx != SHN_UNDEF) | |
4712 | *p++ = ELF_VER_CHR; | |
4713 | memcpy (p, verstr, verlen + 1); | |
4714 | ||
4715 | name = newname; | |
4ad4eba5 AM |
4716 | } |
4717 | ||
cd3416da AM |
4718 | /* If this symbol has default visibility and the user has |
4719 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 4720 | if (!bfd_is_und_section (sec) |
cd3416da | 4721 | && !dynamic |
ce875075 | 4722 | && abfd->no_export |
cd3416da AM |
4723 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
4724 | isym->st_other = (STV_HIDDEN | |
4725 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
4726 | ||
4f3fedcf AM |
4727 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
4728 | sym_hash, &old_bfd, &old_weak, | |
4729 | &old_alignment, &skip, &override, | |
6e33951e L |
4730 | &type_change_ok, &size_change_ok, |
4731 | &matched)) | |
4ad4eba5 AM |
4732 | goto error_free_vers; |
4733 | ||
4734 | if (skip) | |
4735 | continue; | |
4736 | ||
6e33951e L |
4737 | /* Override a definition only if the new symbol matches the |
4738 | existing one. */ | |
4739 | if (override && matched) | |
4ad4eba5 AM |
4740 | definition = FALSE; |
4741 | ||
4742 | h = *sym_hash; | |
4743 | while (h->root.type == bfd_link_hash_indirect | |
4744 | || h->root.type == bfd_link_hash_warning) | |
4745 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4746 | ||
4ad4eba5 | 4747 | if (elf_tdata (abfd)->verdef != NULL |
4ad4eba5 AM |
4748 | && vernum > 1 |
4749 | && definition) | |
4750 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
4751 | } | |
4752 | ||
4753 | if (! (_bfd_generic_link_add_one_symbol | |
66eb6687 | 4754 | (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, |
4ad4eba5 AM |
4755 | (struct bfd_link_hash_entry **) sym_hash))) |
4756 | goto error_free_vers; | |
4757 | ||
4758 | h = *sym_hash; | |
90c984fc L |
4759 | /* We need to make sure that indirect symbol dynamic flags are |
4760 | updated. */ | |
4761 | hi = h; | |
4ad4eba5 AM |
4762 | while (h->root.type == bfd_link_hash_indirect |
4763 | || h->root.type == bfd_link_hash_warning) | |
4764 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 4765 | |
97196564 L |
4766 | /* Setting the index to -3 tells elf_link_output_extsym that |
4767 | this symbol is defined in a discarded section. */ | |
4768 | if (discarded) | |
4769 | h->indx = -3; | |
4770 | ||
4ad4eba5 AM |
4771 | *sym_hash = h; |
4772 | ||
37a9e49a | 4773 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
4774 | if (dynamic |
4775 | && definition | |
37a9e49a | 4776 | && new_weak |
fcb93ecf | 4777 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
66eb6687 | 4778 | && is_elf_hash_table (htab) |
60d67dc8 | 4779 | && h->u.alias == NULL) |
4ad4eba5 AM |
4780 | { |
4781 | /* Keep a list of all weak defined non function symbols from | |
60d67dc8 AM |
4782 | a dynamic object, using the alias field. Later in this |
4783 | function we will set the alias field to the correct | |
4ad4eba5 AM |
4784 | value. We only put non-function symbols from dynamic |
4785 | objects on this list, because that happens to be the only | |
4786 | time we need to know the normal symbol corresponding to a | |
4787 | weak symbol, and the information is time consuming to | |
60d67dc8 | 4788 | figure out. If the alias field is not already NULL, |
4ad4eba5 AM |
4789 | then this symbol was already defined by some previous |
4790 | dynamic object, and we will be using that previous | |
4791 | definition anyhow. */ | |
4792 | ||
60d67dc8 | 4793 | h->u.alias = weaks; |
4ad4eba5 | 4794 | weaks = h; |
4ad4eba5 AM |
4795 | } |
4796 | ||
4797 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 4798 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
4799 | && h->root.type == bfd_link_hash_common) |
4800 | { | |
4801 | unsigned int align; | |
4802 | ||
a4d8e49b | 4803 | if (common) |
af44c138 L |
4804 | align = bfd_log2 (isym->st_value); |
4805 | else | |
4806 | { | |
4807 | /* The new symbol is a common symbol in a shared object. | |
4808 | We need to get the alignment from the section. */ | |
4809 | align = new_sec->alignment_power; | |
4810 | } | |
595213d4 | 4811 | if (align > old_alignment) |
4ad4eba5 AM |
4812 | h->root.u.c.p->alignment_power = align; |
4813 | else | |
4814 | h->root.u.c.p->alignment_power = old_alignment; | |
4815 | } | |
4816 | ||
66eb6687 | 4817 | if (is_elf_hash_table (htab)) |
4ad4eba5 | 4818 | { |
4f3fedcf AM |
4819 | /* Set a flag in the hash table entry indicating the type of |
4820 | reference or definition we just found. A dynamic symbol | |
4821 | is one which is referenced or defined by both a regular | |
4822 | object and a shared object. */ | |
4823 | bfd_boolean dynsym = FALSE; | |
4824 | ||
4825 | /* Plugin symbols aren't normal. Don't set def_regular or | |
4826 | ref_regular for them, or make them dynamic. */ | |
4827 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
4828 | ; | |
4829 | else if (! dynamic) | |
4830 | { | |
4831 | if (! definition) | |
4832 | { | |
4833 | h->ref_regular = 1; | |
4834 | if (bind != STB_WEAK) | |
4835 | h->ref_regular_nonweak = 1; | |
4836 | } | |
4837 | else | |
4838 | { | |
4839 | h->def_regular = 1; | |
4840 | if (h->def_dynamic) | |
4841 | { | |
4842 | h->def_dynamic = 0; | |
4843 | h->ref_dynamic = 1; | |
4844 | } | |
4845 | } | |
4846 | ||
4847 | /* If the indirect symbol has been forced local, don't | |
4848 | make the real symbol dynamic. */ | |
4849 | if ((h == hi || !hi->forced_local) | |
0e1862bb | 4850 | && (bfd_link_dll (info) |
4f3fedcf AM |
4851 | || h->def_dynamic |
4852 | || h->ref_dynamic)) | |
4853 | dynsym = TRUE; | |
4854 | } | |
4855 | else | |
4856 | { | |
4857 | if (! definition) | |
4858 | { | |
4859 | h->ref_dynamic = 1; | |
4860 | hi->ref_dynamic = 1; | |
4861 | } | |
4862 | else | |
4863 | { | |
4864 | h->def_dynamic = 1; | |
4865 | hi->def_dynamic = 1; | |
4866 | } | |
4867 | ||
4868 | /* If the indirect symbol has been forced local, don't | |
4869 | make the real symbol dynamic. */ | |
4870 | if ((h == hi || !hi->forced_local) | |
4871 | && (h->def_regular | |
4872 | || h->ref_regular | |
60d67dc8 AM |
4873 | || (h->is_weakalias |
4874 | && weakdef (h)->dynindx != -1))) | |
4f3fedcf AM |
4875 | dynsym = TRUE; |
4876 | } | |
4877 | ||
4878 | /* Check to see if we need to add an indirect symbol for | |
4879 | the default name. */ | |
4880 | if (definition | |
4881 | || (!override && h->root.type == bfd_link_hash_common)) | |
4882 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
4883 | sec, value, &old_bfd, &dynsym)) | |
4884 | goto error_free_vers; | |
4ad4eba5 AM |
4885 | |
4886 | /* Check the alignment when a common symbol is involved. This | |
4887 | can change when a common symbol is overridden by a normal | |
4888 | definition or a common symbol is ignored due to the old | |
4889 | normal definition. We need to make sure the maximum | |
4890 | alignment is maintained. */ | |
a4d8e49b | 4891 | if ((old_alignment || common) |
4ad4eba5 AM |
4892 | && h->root.type != bfd_link_hash_common) |
4893 | { | |
4894 | unsigned int common_align; | |
4895 | unsigned int normal_align; | |
4896 | unsigned int symbol_align; | |
4897 | bfd *normal_bfd; | |
4898 | bfd *common_bfd; | |
4899 | ||
3a81e825 AM |
4900 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
4901 | || h->root.type == bfd_link_hash_defweak); | |
4902 | ||
4ad4eba5 AM |
4903 | symbol_align = ffs (h->root.u.def.value) - 1; |
4904 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
4905 | && (h->root.u.def.section->owner->flags |
4906 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
4907 | { |
4908 | normal_align = h->root.u.def.section->alignment_power; | |
4909 | if (normal_align > symbol_align) | |
4910 | normal_align = symbol_align; | |
4911 | } | |
4912 | else | |
4913 | normal_align = symbol_align; | |
4914 | ||
4915 | if (old_alignment) | |
4916 | { | |
4917 | common_align = old_alignment; | |
4918 | common_bfd = old_bfd; | |
4919 | normal_bfd = abfd; | |
4920 | } | |
4921 | else | |
4922 | { | |
4923 | common_align = bfd_log2 (isym->st_value); | |
4924 | common_bfd = abfd; | |
4925 | normal_bfd = old_bfd; | |
4926 | } | |
4927 | ||
4928 | if (normal_align < common_align) | |
d07676f8 NC |
4929 | { |
4930 | /* PR binutils/2735 */ | |
4931 | if (normal_bfd == NULL) | |
4eca0228 | 4932 | _bfd_error_handler |
695344c0 | 4933 | /* xgettext:c-format */ |
9793eb77 | 4934 | (_("warning: alignment %u of common symbol `%s' in %pB is" |
871b3ab2 | 4935 | " greater than the alignment (%u) of its section %pA"), |
c08bb8dd AM |
4936 | 1 << common_align, name, common_bfd, |
4937 | 1 << normal_align, h->root.u.def.section); | |
d07676f8 | 4938 | else |
4eca0228 | 4939 | _bfd_error_handler |
695344c0 | 4940 | /* xgettext:c-format */ |
9793eb77 | 4941 | (_("warning: alignment %u of symbol `%s' in %pB" |
871b3ab2 | 4942 | " is smaller than %u in %pB"), |
c08bb8dd AM |
4943 | 1 << normal_align, name, normal_bfd, |
4944 | 1 << common_align, common_bfd); | |
d07676f8 | 4945 | } |
4ad4eba5 AM |
4946 | } |
4947 | ||
83ad0046 | 4948 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
4949 | if (isym->st_size != 0 |
4950 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
4951 | && (definition || h->size == 0)) |
4952 | { | |
83ad0046 L |
4953 | if (h->size != 0 |
4954 | && h->size != isym->st_size | |
4955 | && ! size_change_ok) | |
4eca0228 | 4956 | _bfd_error_handler |
695344c0 | 4957 | /* xgettext:c-format */ |
9793eb77 | 4958 | (_("warning: size of symbol `%s' changed" |
2dcf00ce AM |
4959 | " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"), |
4960 | name, (uint64_t) h->size, old_bfd, | |
4961 | (uint64_t) isym->st_size, abfd); | |
4ad4eba5 AM |
4962 | |
4963 | h->size = isym->st_size; | |
4964 | } | |
4965 | ||
4966 | /* If this is a common symbol, then we always want H->SIZE | |
4967 | to be the size of the common symbol. The code just above | |
4968 | won't fix the size if a common symbol becomes larger. We | |
4969 | don't warn about a size change here, because that is | |
4f3fedcf | 4970 | covered by --warn-common. Allow changes between different |
fcb93ecf | 4971 | function types. */ |
4ad4eba5 AM |
4972 | if (h->root.type == bfd_link_hash_common) |
4973 | h->size = h->root.u.c.size; | |
4974 | ||
4975 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
4976 | && ((definition && !new_weak) |
4977 | || (old_weak && h->root.type == bfd_link_hash_common) | |
4978 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 4979 | { |
2955ec4c L |
4980 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
4981 | ||
4982 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
4983 | symbol. */ | |
4984 | if (type == STT_GNU_IFUNC | |
4985 | && (abfd->flags & DYNAMIC) != 0) | |
4986 | type = STT_FUNC; | |
4ad4eba5 | 4987 | |
2955ec4c L |
4988 | if (h->type != type) |
4989 | { | |
4990 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 4991 | /* xgettext:c-format */ |
4eca0228 | 4992 | _bfd_error_handler |
9793eb77 | 4993 | (_("warning: type of symbol `%s' changed" |
871b3ab2 | 4994 | " from %d to %d in %pB"), |
c08bb8dd | 4995 | name, h->type, type, abfd); |
2955ec4c L |
4996 | |
4997 | h->type = type; | |
4998 | } | |
4ad4eba5 AM |
4999 | } |
5000 | ||
54ac0771 | 5001 | /* Merge st_other field. */ |
b8417128 | 5002 | elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); |
4ad4eba5 | 5003 | |
c3df8c14 | 5004 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
5005 | if (definition |
5006 | && (sec->flags & SEC_DEBUGGING) | |
5007 | && !bfd_link_relocatable (info)) | |
c3df8c14 AM |
5008 | dynsym = FALSE; |
5009 | ||
4f3fedcf AM |
5010 | /* Nor should we make plugin symbols dynamic. */ |
5011 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
5012 | dynsym = FALSE; | |
5013 | ||
35fc36a8 | 5014 | if (definition) |
35399224 L |
5015 | { |
5016 | h->target_internal = isym->st_target_internal; | |
5017 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
5018 | } | |
35fc36a8 | 5019 | |
4ad4eba5 AM |
5020 | if (definition && !dynamic) |
5021 | { | |
5022 | char *p = strchr (name, ELF_VER_CHR); | |
5023 | if (p != NULL && p[1] != ELF_VER_CHR) | |
5024 | { | |
5025 | /* Queue non-default versions so that .symver x, x@FOO | |
5026 | aliases can be checked. */ | |
66eb6687 | 5027 | if (!nondeflt_vers) |
4ad4eba5 | 5028 | { |
66eb6687 AM |
5029 | amt = ((isymend - isym + 1) |
5030 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
5031 | nondeflt_vers |
5032 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
5033 | if (!nondeflt_vers) |
5034 | goto error_free_vers; | |
4ad4eba5 | 5035 | } |
66eb6687 | 5036 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
5037 | } |
5038 | } | |
5039 | ||
5040 | if (dynsym && h->dynindx == -1) | |
5041 | { | |
c152c796 | 5042 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 5043 | goto error_free_vers; |
60d67dc8 AM |
5044 | if (h->is_weakalias |
5045 | && weakdef (h)->dynindx == -1) | |
4ad4eba5 | 5046 | { |
60d67dc8 | 5047 | if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) |
4ad4eba5 AM |
5048 | goto error_free_vers; |
5049 | } | |
5050 | } | |
1f599d0e | 5051 | else if (h->dynindx != -1) |
4ad4eba5 AM |
5052 | /* If the symbol already has a dynamic index, but |
5053 | visibility says it should not be visible, turn it into | |
5054 | a local symbol. */ | |
5055 | switch (ELF_ST_VISIBILITY (h->other)) | |
5056 | { | |
5057 | case STV_INTERNAL: | |
5058 | case STV_HIDDEN: | |
5059 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
5060 | dynsym = FALSE; | |
5061 | break; | |
5062 | } | |
5063 | ||
aef28989 L |
5064 | /* Don't add DT_NEEDED for references from the dummy bfd nor |
5065 | for unmatched symbol. */ | |
4ad4eba5 | 5066 | if (!add_needed |
aef28989 | 5067 | && matched |
4ad4eba5 | 5068 | && definition |
010e5ae2 | 5069 | && ((dynsym |
ffa9430d | 5070 | && h->ref_regular_nonweak |
4f3fedcf AM |
5071 | && (old_bfd == NULL |
5072 | || (old_bfd->flags & BFD_PLUGIN) == 0)) | |
ffa9430d | 5073 | || (h->ref_dynamic_nonweak |
010e5ae2 | 5074 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
5075 | && !on_needed_list (elf_dt_name (abfd), |
5076 | htab->needed, NULL)))) | |
4ad4eba5 AM |
5077 | { |
5078 | int ret; | |
5079 | const char *soname = elf_dt_name (abfd); | |
5080 | ||
16e4ecc0 AM |
5081 | info->callbacks->minfo ("%!", soname, old_bfd, |
5082 | h->root.root.string); | |
5083 | ||
4ad4eba5 AM |
5084 | /* A symbol from a library loaded via DT_NEEDED of some |
5085 | other library is referenced by a regular object. | |
e56f61be | 5086 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
5087 | --no-add-needed is used and the reference was not |
5088 | a weak one. */ | |
4f3fedcf | 5089 | if (old_bfd != NULL |
b918acf9 | 5090 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 5091 | { |
4eca0228 | 5092 | _bfd_error_handler |
695344c0 | 5093 | /* xgettext:c-format */ |
871b3ab2 | 5094 | (_("%pB: undefined reference to symbol '%s'"), |
4f3fedcf | 5095 | old_bfd, name); |
ff5ac77b | 5096 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
5097 | goto error_free_vers; |
5098 | } | |
5099 | ||
a50b1753 | 5100 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 5101 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 5102 | |
4ad4eba5 | 5103 | add_needed = TRUE; |
7e9f0867 | 5104 | ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); |
4ad4eba5 AM |
5105 | if (ret < 0) |
5106 | goto error_free_vers; | |
5107 | ||
5108 | BFD_ASSERT (ret == 0); | |
5109 | } | |
5110 | } | |
5111 | } | |
5112 | ||
a83ef4d1 L |
5113 | if (info->lto_plugin_active |
5114 | && !bfd_link_relocatable (info) | |
5115 | && (abfd->flags & BFD_PLUGIN) == 0 | |
5116 | && !just_syms | |
5117 | && extsymcount) | |
5118 | { | |
5119 | int r_sym_shift; | |
5120 | ||
5121 | if (bed->s->arch_size == 32) | |
5122 | r_sym_shift = 8; | |
5123 | else | |
5124 | r_sym_shift = 32; | |
5125 | ||
5126 | /* If linker plugin is enabled, set non_ir_ref_regular on symbols | |
5127 | referenced in regular objects so that linker plugin will get | |
5128 | the correct symbol resolution. */ | |
5129 | ||
5130 | sym_hash = elf_sym_hashes (abfd); | |
5131 | for (s = abfd->sections; s != NULL; s = s->next) | |
5132 | { | |
5133 | Elf_Internal_Rela *internal_relocs; | |
5134 | Elf_Internal_Rela *rel, *relend; | |
5135 | ||
5136 | /* Don't check relocations in excluded sections. */ | |
5137 | if ((s->flags & SEC_RELOC) == 0 | |
5138 | || s->reloc_count == 0 | |
5139 | || (s->flags & SEC_EXCLUDE) != 0 | |
5140 | || ((info->strip == strip_all | |
5141 | || info->strip == strip_debugger) | |
5142 | && (s->flags & SEC_DEBUGGING) != 0)) | |
5143 | continue; | |
5144 | ||
5145 | internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL, | |
5146 | NULL, | |
5147 | info->keep_memory); | |
5148 | if (internal_relocs == NULL) | |
5149 | goto error_free_vers; | |
5150 | ||
5151 | rel = internal_relocs; | |
5152 | relend = rel + s->reloc_count; | |
5153 | for ( ; rel < relend; rel++) | |
5154 | { | |
5155 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
5156 | struct elf_link_hash_entry *h; | |
5157 | ||
5158 | /* Skip local symbols. */ | |
5159 | if (r_symndx < extsymoff) | |
5160 | continue; | |
5161 | ||
5162 | h = sym_hash[r_symndx - extsymoff]; | |
5163 | if (h != NULL) | |
5164 | h->root.non_ir_ref_regular = 1; | |
5165 | } | |
5166 | ||
5167 | if (elf_section_data (s)->relocs != internal_relocs) | |
5168 | free (internal_relocs); | |
5169 | } | |
5170 | } | |
5171 | ||
66eb6687 AM |
5172 | if (extversym != NULL) |
5173 | { | |
5174 | free (extversym); | |
5175 | extversym = NULL; | |
5176 | } | |
5177 | ||
5178 | if (isymbuf != NULL) | |
5179 | { | |
5180 | free (isymbuf); | |
5181 | isymbuf = NULL; | |
5182 | } | |
5183 | ||
5184 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
5185 | { | |
5186 | unsigned int i; | |
5187 | ||
5188 | /* Restore the symbol table. */ | |
f45794cb AM |
5189 | old_ent = (char *) old_tab + tabsize; |
5190 | memset (elf_sym_hashes (abfd), 0, | |
5191 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
5192 | htab->root.table.table = old_table; |
5193 | htab->root.table.size = old_size; | |
5194 | htab->root.table.count = old_count; | |
66eb6687 | 5195 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
5196 | htab->root.undefs = old_undefs; |
5197 | htab->root.undefs_tail = old_undefs_tail; | |
5b677558 AM |
5198 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); |
5199 | free (old_strtab); | |
5200 | old_strtab = NULL; | |
66eb6687 AM |
5201 | for (i = 0; i < htab->root.table.size; i++) |
5202 | { | |
5203 | struct bfd_hash_entry *p; | |
5204 | struct elf_link_hash_entry *h; | |
3e0882af L |
5205 | bfd_size_type size; |
5206 | unsigned int alignment_power; | |
4070765b | 5207 | unsigned int non_ir_ref_dynamic; |
66eb6687 AM |
5208 | |
5209 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
5210 | { | |
5211 | h = (struct elf_link_hash_entry *) p; | |
2de92251 AM |
5212 | if (h->root.type == bfd_link_hash_warning) |
5213 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2de92251 | 5214 | |
3e0882af L |
5215 | /* Preserve the maximum alignment and size for common |
5216 | symbols even if this dynamic lib isn't on DT_NEEDED | |
a4542f1b | 5217 | since it can still be loaded at run time by another |
3e0882af L |
5218 | dynamic lib. */ |
5219 | if (h->root.type == bfd_link_hash_common) | |
5220 | { | |
5221 | size = h->root.u.c.size; | |
5222 | alignment_power = h->root.u.c.p->alignment_power; | |
5223 | } | |
5224 | else | |
5225 | { | |
5226 | size = 0; | |
5227 | alignment_power = 0; | |
5228 | } | |
4070765b | 5229 | /* Preserve non_ir_ref_dynamic so that this symbol |
59fa66c5 L |
5230 | will be exported when the dynamic lib becomes needed |
5231 | in the second pass. */ | |
4070765b | 5232 | non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; |
66eb6687 AM |
5233 | memcpy (p, old_ent, htab->root.table.entsize); |
5234 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
5235 | h = (struct elf_link_hash_entry *) p; |
5236 | if (h->root.type == bfd_link_hash_warning) | |
5237 | { | |
5238 | memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); | |
5239 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
a4542f1b | 5240 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2de92251 | 5241 | } |
a4542f1b | 5242 | if (h->root.type == bfd_link_hash_common) |
3e0882af L |
5243 | { |
5244 | if (size > h->root.u.c.size) | |
5245 | h->root.u.c.size = size; | |
5246 | if (alignment_power > h->root.u.c.p->alignment_power) | |
5247 | h->root.u.c.p->alignment_power = alignment_power; | |
5248 | } | |
4070765b | 5249 | h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; |
66eb6687 AM |
5250 | } |
5251 | } | |
5252 | ||
5061a885 AM |
5253 | /* Make a special call to the linker "notice" function to |
5254 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 5255 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 5256 | goto error_free_vers; |
5061a885 | 5257 | |
66eb6687 AM |
5258 | free (old_tab); |
5259 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
5260 | alloc_mark); | |
5261 | if (nondeflt_vers != NULL) | |
5262 | free (nondeflt_vers); | |
5263 | return TRUE; | |
5264 | } | |
2de92251 | 5265 | |
66eb6687 AM |
5266 | if (old_tab != NULL) |
5267 | { | |
e5034e59 | 5268 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 5269 | goto error_free_vers; |
66eb6687 AM |
5270 | free (old_tab); |
5271 | old_tab = NULL; | |
5272 | } | |
5273 | ||
c6e8a9a8 L |
5274 | /* Now that all the symbols from this input file are created, if |
5275 | not performing a relocatable link, handle .symver foo, foo@BAR | |
5276 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 5277 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 5278 | { |
ef53be89 | 5279 | size_t cnt, symidx; |
4ad4eba5 AM |
5280 | |
5281 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
5282 | { | |
5283 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
5284 | char *shortname, *p; | |
5285 | ||
5286 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
5287 | if (p == NULL | |
5288 | || (h->root.type != bfd_link_hash_defined | |
5289 | && h->root.type != bfd_link_hash_defweak)) | |
5290 | continue; | |
5291 | ||
5292 | amt = p - h->root.root.string; | |
a50b1753 | 5293 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
5294 | if (!shortname) |
5295 | goto error_free_vers; | |
4ad4eba5 AM |
5296 | memcpy (shortname, h->root.root.string, amt); |
5297 | shortname[amt] = '\0'; | |
5298 | ||
5299 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 5300 | bfd_link_hash_lookup (&htab->root, shortname, |
4ad4eba5 AM |
5301 | FALSE, FALSE, FALSE); |
5302 | if (hi != NULL | |
5303 | && hi->root.type == h->root.type | |
5304 | && hi->root.u.def.value == h->root.u.def.value | |
5305 | && hi->root.u.def.section == h->root.u.def.section) | |
5306 | { | |
5307 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
5308 | hi->root.type = bfd_link_hash_indirect; | |
5309 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 5310 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
5311 | sym_hash = elf_sym_hashes (abfd); |
5312 | if (sym_hash) | |
5313 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
5314 | if (sym_hash[symidx] == hi) | |
5315 | { | |
5316 | sym_hash[symidx] = h; | |
5317 | break; | |
5318 | } | |
5319 | } | |
5320 | free (shortname); | |
5321 | } | |
5322 | free (nondeflt_vers); | |
5323 | nondeflt_vers = NULL; | |
5324 | } | |
5325 | ||
60d67dc8 | 5326 | /* Now set the alias field correctly for all the weak defined |
4ad4eba5 AM |
5327 | symbols we found. The only way to do this is to search all the |
5328 | symbols. Since we only need the information for non functions in | |
5329 | dynamic objects, that's the only time we actually put anything on | |
5330 | the list WEAKS. We need this information so that if a regular | |
5331 | object refers to a symbol defined weakly in a dynamic object, the | |
5332 | real symbol in the dynamic object is also put in the dynamic | |
5333 | symbols; we also must arrange for both symbols to point to the | |
5334 | same memory location. We could handle the general case of symbol | |
5335 | aliasing, but a general symbol alias can only be generated in | |
5336 | assembler code, handling it correctly would be very time | |
5337 | consuming, and other ELF linkers don't handle general aliasing | |
5338 | either. */ | |
5339 | if (weaks != NULL) | |
5340 | { | |
5341 | struct elf_link_hash_entry **hpp; | |
5342 | struct elf_link_hash_entry **hppend; | |
5343 | struct elf_link_hash_entry **sorted_sym_hash; | |
5344 | struct elf_link_hash_entry *h; | |
5345 | size_t sym_count; | |
5346 | ||
5347 | /* Since we have to search the whole symbol list for each weak | |
5348 | defined symbol, search time for N weak defined symbols will be | |
5349 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
ef53be89 AM |
5350 | amt = extsymcount; |
5351 | amt *= sizeof (struct elf_link_hash_entry *); | |
a50b1753 | 5352 | sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); |
4ad4eba5 AM |
5353 | if (sorted_sym_hash == NULL) |
5354 | goto error_return; | |
5355 | sym_hash = sorted_sym_hash; | |
5356 | hpp = elf_sym_hashes (abfd); | |
5357 | hppend = hpp + extsymcount; | |
5358 | sym_count = 0; | |
5359 | for (; hpp < hppend; hpp++) | |
5360 | { | |
5361 | h = *hpp; | |
5362 | if (h != NULL | |
5363 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 5364 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
5365 | { |
5366 | *sym_hash = h; | |
5367 | sym_hash++; | |
5368 | sym_count++; | |
5369 | } | |
5370 | } | |
5371 | ||
5372 | qsort (sorted_sym_hash, sym_count, | |
5373 | sizeof (struct elf_link_hash_entry *), | |
5374 | elf_sort_symbol); | |
5375 | ||
5376 | while (weaks != NULL) | |
5377 | { | |
5378 | struct elf_link_hash_entry *hlook; | |
5379 | asection *slook; | |
5380 | bfd_vma vlook; | |
ed54588d | 5381 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5382 | |
5383 | hlook = weaks; | |
60d67dc8 AM |
5384 | weaks = hlook->u.alias; |
5385 | hlook->u.alias = NULL; | |
4ad4eba5 | 5386 | |
e3e53eed AM |
5387 | if (hlook->root.type != bfd_link_hash_defined |
5388 | && hlook->root.type != bfd_link_hash_defweak) | |
5389 | continue; | |
5390 | ||
4ad4eba5 AM |
5391 | slook = hlook->root.u.def.section; |
5392 | vlook = hlook->root.u.def.value; | |
5393 | ||
4ad4eba5 AM |
5394 | i = 0; |
5395 | j = sym_count; | |
14160578 | 5396 | while (i != j) |
4ad4eba5 AM |
5397 | { |
5398 | bfd_signed_vma vdiff; | |
5399 | idx = (i + j) / 2; | |
14160578 | 5400 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5401 | vdiff = vlook - h->root.u.def.value; |
5402 | if (vdiff < 0) | |
5403 | j = idx; | |
5404 | else if (vdiff > 0) | |
5405 | i = idx + 1; | |
5406 | else | |
5407 | { | |
d3435ae8 | 5408 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5409 | if (sdiff < 0) |
5410 | j = idx; | |
5411 | else if (sdiff > 0) | |
5412 | i = idx + 1; | |
5413 | else | |
14160578 | 5414 | break; |
4ad4eba5 AM |
5415 | } |
5416 | } | |
5417 | ||
5418 | /* We didn't find a value/section match. */ | |
14160578 | 5419 | if (i == j) |
4ad4eba5 AM |
5420 | continue; |
5421 | ||
14160578 AM |
5422 | /* With multiple aliases, or when the weak symbol is already |
5423 | strongly defined, we have multiple matching symbols and | |
5424 | the binary search above may land on any of them. Step | |
5425 | one past the matching symbol(s). */ | |
5426 | while (++idx != j) | |
5427 | { | |
5428 | h = sorted_sym_hash[idx]; | |
5429 | if (h->root.u.def.section != slook | |
5430 | || h->root.u.def.value != vlook) | |
5431 | break; | |
5432 | } | |
5433 | ||
5434 | /* Now look back over the aliases. Since we sorted by size | |
5435 | as well as value and section, we'll choose the one with | |
5436 | the largest size. */ | |
5437 | while (idx-- != i) | |
4ad4eba5 | 5438 | { |
14160578 | 5439 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5440 | |
5441 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5442 | if (h->root.u.def.section != slook |
5443 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5444 | break; |
5445 | else if (h != hlook) | |
5446 | { | |
60d67dc8 AM |
5447 | struct elf_link_hash_entry *t; |
5448 | ||
5449 | hlook->u.alias = h; | |
5450 | hlook->is_weakalias = 1; | |
5451 | t = h; | |
5452 | if (t->u.alias != NULL) | |
5453 | while (t->u.alias != h) | |
5454 | t = t->u.alias; | |
5455 | t->u.alias = hlook; | |
4ad4eba5 AM |
5456 | |
5457 | /* If the weak definition is in the list of dynamic | |
5458 | symbols, make sure the real definition is put | |
5459 | there as well. */ | |
5460 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5461 | { | |
c152c796 | 5462 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5463 | { |
5464 | err_free_sym_hash: | |
5465 | free (sorted_sym_hash); | |
5466 | goto error_return; | |
5467 | } | |
4ad4eba5 AM |
5468 | } |
5469 | ||
5470 | /* If the real definition is in the list of dynamic | |
5471 | symbols, make sure the weak definition is put | |
5472 | there as well. If we don't do this, then the | |
5473 | dynamic loader might not merge the entries for the | |
5474 | real definition and the weak definition. */ | |
5475 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5476 | { | |
c152c796 | 5477 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5478 | goto err_free_sym_hash; |
4ad4eba5 AM |
5479 | } |
5480 | break; | |
5481 | } | |
5482 | } | |
5483 | } | |
5484 | ||
5485 | free (sorted_sym_hash); | |
5486 | } | |
5487 | ||
33177bb1 AM |
5488 | if (bed->check_directives |
5489 | && !(*bed->check_directives) (abfd, info)) | |
5490 | return FALSE; | |
85fbca6a | 5491 | |
4ad4eba5 AM |
5492 | /* If this is a non-traditional link, try to optimize the handling |
5493 | of the .stab/.stabstr sections. */ | |
5494 | if (! dynamic | |
5495 | && ! info->traditional_format | |
66eb6687 | 5496 | && is_elf_hash_table (htab) |
4ad4eba5 AM |
5497 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5498 | { | |
5499 | asection *stabstr; | |
5500 | ||
5501 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5502 | if (stabstr != NULL) | |
5503 | { | |
5504 | bfd_size_type string_offset = 0; | |
5505 | asection *stab; | |
5506 | ||
5507 | for (stab = abfd->sections; stab; stab = stab->next) | |
0112cd26 | 5508 | if (CONST_STRNEQ (stab->name, ".stab") |
4ad4eba5 AM |
5509 | && (!stab->name[5] || |
5510 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5511 | && (stab->flags & SEC_MERGE) == 0 | |
5512 | && !bfd_is_abs_section (stab->output_section)) | |
5513 | { | |
5514 | struct bfd_elf_section_data *secdata; | |
5515 | ||
5516 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5517 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5518 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5519 | &string_offset)) |
5520 | goto error_return; | |
5521 | if (secdata->sec_info) | |
dbaa2011 | 5522 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5523 | } |
5524 | } | |
5525 | } | |
5526 | ||
66eb6687 | 5527 | if (is_elf_hash_table (htab) && add_needed) |
4ad4eba5 AM |
5528 | { |
5529 | /* Add this bfd to the loaded list. */ | |
5530 | struct elf_link_loaded_list *n; | |
5531 | ||
ca4be51c | 5532 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5533 | if (n == NULL) |
5534 | goto error_return; | |
5535 | n->abfd = abfd; | |
66eb6687 AM |
5536 | n->next = htab->loaded; |
5537 | htab->loaded = n; | |
4ad4eba5 AM |
5538 | } |
5539 | ||
5540 | return TRUE; | |
5541 | ||
5542 | error_free_vers: | |
66eb6687 AM |
5543 | if (old_tab != NULL) |
5544 | free (old_tab); | |
5b677558 AM |
5545 | if (old_strtab != NULL) |
5546 | free (old_strtab); | |
4ad4eba5 AM |
5547 | if (nondeflt_vers != NULL) |
5548 | free (nondeflt_vers); | |
5549 | if (extversym != NULL) | |
5550 | free (extversym); | |
5551 | error_free_sym: | |
5552 | if (isymbuf != NULL) | |
5553 | free (isymbuf); | |
5554 | error_return: | |
5555 | return FALSE; | |
5556 | } | |
5557 | ||
8387904d AM |
5558 | /* Return the linker hash table entry of a symbol that might be |
5559 | satisfied by an archive symbol. Return -1 on error. */ | |
5560 | ||
5561 | struct elf_link_hash_entry * | |
5562 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
5563 | struct bfd_link_info *info, | |
5564 | const char *name) | |
5565 | { | |
5566 | struct elf_link_hash_entry *h; | |
5567 | char *p, *copy; | |
5568 | size_t len, first; | |
5569 | ||
2a41f396 | 5570 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); |
8387904d AM |
5571 | if (h != NULL) |
5572 | return h; | |
5573 | ||
5574 | /* If this is a default version (the name contains @@), look up the | |
5575 | symbol again with only one `@' as well as without the version. | |
5576 | The effect is that references to the symbol with and without the | |
5577 | version will be matched by the default symbol in the archive. */ | |
5578 | ||
5579 | p = strchr (name, ELF_VER_CHR); | |
5580 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5581 | return h; | |
5582 | ||
5583 | /* First check with only one `@'. */ | |
5584 | len = strlen (name); | |
a50b1753 | 5585 | copy = (char *) bfd_alloc (abfd, len); |
8387904d | 5586 | if (copy == NULL) |
e99955cd | 5587 | return (struct elf_link_hash_entry *) -1; |
8387904d AM |
5588 | |
5589 | first = p - name + 1; | |
5590 | memcpy (copy, name, first); | |
5591 | memcpy (copy + first, name + first + 1, len - first); | |
5592 | ||
2a41f396 | 5593 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); |
8387904d AM |
5594 | if (h == NULL) |
5595 | { | |
5596 | /* We also need to check references to the symbol without the | |
5597 | version. */ | |
5598 | copy[first - 1] = '\0'; | |
5599 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2a41f396 | 5600 | FALSE, FALSE, TRUE); |
8387904d AM |
5601 | } |
5602 | ||
5603 | bfd_release (abfd, copy); | |
5604 | return h; | |
5605 | } | |
5606 | ||
0ad989f9 | 5607 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5608 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5609 | handle versioned symbols. | |
0ad989f9 L |
5610 | |
5611 | Fortunately, ELF archive handling is simpler than that done by | |
5612 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5613 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5614 | symbol is currently undefined, we know that we must pull in that | |
5615 | object file. | |
5616 | ||
5617 | Unfortunately, we do have to make multiple passes over the symbol | |
5618 | table until nothing further is resolved. */ | |
5619 | ||
4ad4eba5 AM |
5620 | static bfd_boolean |
5621 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
5622 | { |
5623 | symindex c; | |
13e570f8 | 5624 | unsigned char *included = NULL; |
0ad989f9 L |
5625 | carsym *symdefs; |
5626 | bfd_boolean loop; | |
5627 | bfd_size_type amt; | |
8387904d AM |
5628 | const struct elf_backend_data *bed; |
5629 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
5630 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
5631 | |
5632 | if (! bfd_has_map (abfd)) | |
5633 | { | |
5634 | /* An empty archive is a special case. */ | |
5635 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
5636 | return TRUE; | |
5637 | bfd_set_error (bfd_error_no_armap); | |
5638 | return FALSE; | |
5639 | } | |
5640 | ||
5641 | /* Keep track of all symbols we know to be already defined, and all | |
5642 | files we know to be already included. This is to speed up the | |
5643 | second and subsequent passes. */ | |
5644 | c = bfd_ardata (abfd)->symdef_count; | |
5645 | if (c == 0) | |
5646 | return TRUE; | |
5647 | amt = c; | |
13e570f8 AM |
5648 | amt *= sizeof (*included); |
5649 | included = (unsigned char *) bfd_zmalloc (amt); | |
5650 | if (included == NULL) | |
5651 | return FALSE; | |
0ad989f9 L |
5652 | |
5653 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
5654 | bed = get_elf_backend_data (abfd); |
5655 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
5656 | |
5657 | do | |
5658 | { | |
5659 | file_ptr last; | |
5660 | symindex i; | |
5661 | carsym *symdef; | |
5662 | carsym *symdefend; | |
5663 | ||
5664 | loop = FALSE; | |
5665 | last = -1; | |
5666 | ||
5667 | symdef = symdefs; | |
5668 | symdefend = symdef + c; | |
5669 | for (i = 0; symdef < symdefend; symdef++, i++) | |
5670 | { | |
5671 | struct elf_link_hash_entry *h; | |
5672 | bfd *element; | |
5673 | struct bfd_link_hash_entry *undefs_tail; | |
5674 | symindex mark; | |
5675 | ||
13e570f8 | 5676 | if (included[i]) |
0ad989f9 L |
5677 | continue; |
5678 | if (symdef->file_offset == last) | |
5679 | { | |
5680 | included[i] = TRUE; | |
5681 | continue; | |
5682 | } | |
5683 | ||
8387904d | 5684 | h = archive_symbol_lookup (abfd, info, symdef->name); |
e99955cd | 5685 | if (h == (struct elf_link_hash_entry *) -1) |
8387904d | 5686 | goto error_return; |
0ad989f9 L |
5687 | |
5688 | if (h == NULL) | |
5689 | continue; | |
5690 | ||
5691 | if (h->root.type == bfd_link_hash_common) | |
5692 | { | |
5693 | /* We currently have a common symbol. The archive map contains | |
5694 | a reference to this symbol, so we may want to include it. We | |
5695 | only want to include it however, if this archive element | |
5696 | contains a definition of the symbol, not just another common | |
5697 | declaration of it. | |
5698 | ||
5699 | Unfortunately some archivers (including GNU ar) will put | |
5700 | declarations of common symbols into their archive maps, as | |
5701 | well as real definitions, so we cannot just go by the archive | |
5702 | map alone. Instead we must read in the element's symbol | |
5703 | table and check that to see what kind of symbol definition | |
5704 | this is. */ | |
5705 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
5706 | continue; | |
5707 | } | |
5708 | else if (h->root.type != bfd_link_hash_undefined) | |
5709 | { | |
5710 | if (h->root.type != bfd_link_hash_undefweak) | |
13e570f8 AM |
5711 | /* Symbol must be defined. Don't check it again. */ |
5712 | included[i] = TRUE; | |
0ad989f9 L |
5713 | continue; |
5714 | } | |
5715 | ||
5716 | /* We need to include this archive member. */ | |
5717 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
5718 | if (element == NULL) | |
5719 | goto error_return; | |
5720 | ||
5721 | if (! bfd_check_format (element, bfd_object)) | |
5722 | goto error_return; | |
5723 | ||
0ad989f9 L |
5724 | undefs_tail = info->hash->undefs_tail; |
5725 | ||
0e144ba7 AM |
5726 | if (!(*info->callbacks |
5727 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 5728 | continue; |
0e144ba7 | 5729 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
5730 | goto error_return; |
5731 | ||
5732 | /* If there are any new undefined symbols, we need to make | |
5733 | another pass through the archive in order to see whether | |
5734 | they can be defined. FIXME: This isn't perfect, because | |
5735 | common symbols wind up on undefs_tail and because an | |
5736 | undefined symbol which is defined later on in this pass | |
5737 | does not require another pass. This isn't a bug, but it | |
5738 | does make the code less efficient than it could be. */ | |
5739 | if (undefs_tail != info->hash->undefs_tail) | |
5740 | loop = TRUE; | |
5741 | ||
5742 | /* Look backward to mark all symbols from this object file | |
5743 | which we have already seen in this pass. */ | |
5744 | mark = i; | |
5745 | do | |
5746 | { | |
5747 | included[mark] = TRUE; | |
5748 | if (mark == 0) | |
5749 | break; | |
5750 | --mark; | |
5751 | } | |
5752 | while (symdefs[mark].file_offset == symdef->file_offset); | |
5753 | ||
5754 | /* We mark subsequent symbols from this object file as we go | |
5755 | on through the loop. */ | |
5756 | last = symdef->file_offset; | |
5757 | } | |
5758 | } | |
5759 | while (loop); | |
5760 | ||
0ad989f9 L |
5761 | free (included); |
5762 | ||
5763 | return TRUE; | |
5764 | ||
5765 | error_return: | |
0ad989f9 L |
5766 | if (included != NULL) |
5767 | free (included); | |
5768 | return FALSE; | |
5769 | } | |
4ad4eba5 AM |
5770 | |
5771 | /* Given an ELF BFD, add symbols to the global hash table as | |
5772 | appropriate. */ | |
5773 | ||
5774 | bfd_boolean | |
5775 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
5776 | { | |
5777 | switch (bfd_get_format (abfd)) | |
5778 | { | |
5779 | case bfd_object: | |
5780 | return elf_link_add_object_symbols (abfd, info); | |
5781 | case bfd_archive: | |
5782 | return elf_link_add_archive_symbols (abfd, info); | |
5783 | default: | |
5784 | bfd_set_error (bfd_error_wrong_format); | |
5785 | return FALSE; | |
5786 | } | |
5787 | } | |
5a580b3a | 5788 | \f |
14b1c01e AM |
5789 | struct hash_codes_info |
5790 | { | |
5791 | unsigned long *hashcodes; | |
5792 | bfd_boolean error; | |
5793 | }; | |
a0c8462f | 5794 | |
5a580b3a AM |
5795 | /* This function will be called though elf_link_hash_traverse to store |
5796 | all hash value of the exported symbols in an array. */ | |
5797 | ||
5798 | static bfd_boolean | |
5799 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5800 | { | |
a50b1753 | 5801 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 5802 | const char *name; |
5a580b3a AM |
5803 | unsigned long ha; |
5804 | char *alc = NULL; | |
5805 | ||
5a580b3a AM |
5806 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5807 | if (h->dynindx == -1) | |
5808 | return TRUE; | |
5809 | ||
5810 | name = h->root.root.string; | |
422f1182 | 5811 | if (h->versioned >= versioned) |
5a580b3a | 5812 | { |
422f1182 L |
5813 | char *p = strchr (name, ELF_VER_CHR); |
5814 | if (p != NULL) | |
14b1c01e | 5815 | { |
422f1182 L |
5816 | alc = (char *) bfd_malloc (p - name + 1); |
5817 | if (alc == NULL) | |
5818 | { | |
5819 | inf->error = TRUE; | |
5820 | return FALSE; | |
5821 | } | |
5822 | memcpy (alc, name, p - name); | |
5823 | alc[p - name] = '\0'; | |
5824 | name = alc; | |
14b1c01e | 5825 | } |
5a580b3a AM |
5826 | } |
5827 | ||
5828 | /* Compute the hash value. */ | |
5829 | ha = bfd_elf_hash (name); | |
5830 | ||
5831 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 5832 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
5833 | |
5834 | /* And store it in the struct so that we can put it in the hash table | |
5835 | later. */ | |
f6e332e6 | 5836 | h->u.elf_hash_value = ha; |
5a580b3a AM |
5837 | |
5838 | if (alc != NULL) | |
5839 | free (alc); | |
5840 | ||
5841 | return TRUE; | |
5842 | } | |
5843 | ||
fdc90cb4 JJ |
5844 | struct collect_gnu_hash_codes |
5845 | { | |
5846 | bfd *output_bfd; | |
5847 | const struct elf_backend_data *bed; | |
5848 | unsigned long int nsyms; | |
5849 | unsigned long int maskbits; | |
5850 | unsigned long int *hashcodes; | |
5851 | unsigned long int *hashval; | |
5852 | unsigned long int *indx; | |
5853 | unsigned long int *counts; | |
5854 | bfd_vma *bitmask; | |
5855 | bfd_byte *contents; | |
5856 | long int min_dynindx; | |
5857 | unsigned long int bucketcount; | |
5858 | unsigned long int symindx; | |
5859 | long int local_indx; | |
5860 | long int shift1, shift2; | |
5861 | unsigned long int mask; | |
14b1c01e | 5862 | bfd_boolean error; |
fdc90cb4 JJ |
5863 | }; |
5864 | ||
5865 | /* This function will be called though elf_link_hash_traverse to store | |
5866 | all hash value of the exported symbols in an array. */ | |
5867 | ||
5868 | static bfd_boolean | |
5869 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) | |
5870 | { | |
a50b1753 | 5871 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 5872 | const char *name; |
fdc90cb4 JJ |
5873 | unsigned long ha; |
5874 | char *alc = NULL; | |
5875 | ||
fdc90cb4 JJ |
5876 | /* Ignore indirect symbols. These are added by the versioning code. */ |
5877 | if (h->dynindx == -1) | |
5878 | return TRUE; | |
5879 | ||
5880 | /* Ignore also local symbols and undefined symbols. */ | |
5881 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5882 | return TRUE; | |
5883 | ||
5884 | name = h->root.root.string; | |
422f1182 | 5885 | if (h->versioned >= versioned) |
fdc90cb4 | 5886 | { |
422f1182 L |
5887 | char *p = strchr (name, ELF_VER_CHR); |
5888 | if (p != NULL) | |
14b1c01e | 5889 | { |
422f1182 L |
5890 | alc = (char *) bfd_malloc (p - name + 1); |
5891 | if (alc == NULL) | |
5892 | { | |
5893 | s->error = TRUE; | |
5894 | return FALSE; | |
5895 | } | |
5896 | memcpy (alc, name, p - name); | |
5897 | alc[p - name] = '\0'; | |
5898 | name = alc; | |
14b1c01e | 5899 | } |
fdc90cb4 JJ |
5900 | } |
5901 | ||
5902 | /* Compute the hash value. */ | |
5903 | ha = bfd_elf_gnu_hash (name); | |
5904 | ||
5905 | /* Store the found hash value in the array for compute_bucket_count, | |
5906 | and also for .dynsym reordering purposes. */ | |
5907 | s->hashcodes[s->nsyms] = ha; | |
5908 | s->hashval[h->dynindx] = ha; | |
5909 | ++s->nsyms; | |
5910 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
5911 | s->min_dynindx = h->dynindx; | |
5912 | ||
5913 | if (alc != NULL) | |
5914 | free (alc); | |
5915 | ||
5916 | return TRUE; | |
5917 | } | |
5918 | ||
5919 | /* This function will be called though elf_link_hash_traverse to do | |
5920 | final dynaminc symbol renumbering. */ | |
5921 | ||
5922 | static bfd_boolean | |
5923 | elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) | |
5924 | { | |
a50b1753 | 5925 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
5926 | unsigned long int bucket; |
5927 | unsigned long int val; | |
5928 | ||
fdc90cb4 JJ |
5929 | /* Ignore indirect symbols. */ |
5930 | if (h->dynindx == -1) | |
5931 | return TRUE; | |
5932 | ||
5933 | /* Ignore also local symbols and undefined symbols. */ | |
5934 | if (! (*s->bed->elf_hash_symbol) (h)) | |
5935 | { | |
5936 | if (h->dynindx >= s->min_dynindx) | |
5937 | h->dynindx = s->local_indx++; | |
5938 | return TRUE; | |
5939 | } | |
5940 | ||
5941 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
5942 | val = (s->hashval[h->dynindx] >> s->shift1) | |
5943 | & ((s->maskbits >> s->shift1) - 1); | |
5944 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
5945 | s->bitmask[val] | |
5946 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
5947 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
5948 | if (s->counts[bucket] == 1) | |
5949 | /* Last element terminates the chain. */ | |
5950 | val |= 1; | |
5951 | bfd_put_32 (s->output_bfd, val, | |
5952 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
5953 | --s->counts[bucket]; | |
5954 | h->dynindx = s->indx[bucket]++; | |
5955 | return TRUE; | |
5956 | } | |
5957 | ||
5958 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
5959 | ||
5960 | bfd_boolean | |
5961 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) | |
5962 | { | |
5963 | return !(h->forced_local | |
5964 | || h->root.type == bfd_link_hash_undefined | |
5965 | || h->root.type == bfd_link_hash_undefweak | |
5966 | || ((h->root.type == bfd_link_hash_defined | |
5967 | || h->root.type == bfd_link_hash_defweak) | |
5968 | && h->root.u.def.section->output_section == NULL)); | |
5969 | } | |
5970 | ||
5a580b3a AM |
5971 | /* Array used to determine the number of hash table buckets to use |
5972 | based on the number of symbols there are. If there are fewer than | |
5973 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
5974 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
5975 | than 32771 buckets. */ | |
5976 | ||
5977 | static const size_t elf_buckets[] = | |
5978 | { | |
5979 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
5980 | 16411, 32771, 0 | |
5981 | }; | |
5982 | ||
5983 | /* Compute bucket count for hashing table. We do not use a static set | |
5984 | of possible tables sizes anymore. Instead we determine for all | |
5985 | possible reasonable sizes of the table the outcome (i.e., the | |
5986 | number of collisions etc) and choose the best solution. The | |
5987 | weighting functions are not too simple to allow the table to grow | |
5988 | without bounds. Instead one of the weighting factors is the size. | |
5989 | Therefore the result is always a good payoff between few collisions | |
5990 | (= short chain lengths) and table size. */ | |
5991 | static size_t | |
b20dd2ce | 5992 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
5993 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
5994 | unsigned long int nsyms, | |
5995 | int gnu_hash) | |
5a580b3a | 5996 | { |
5a580b3a | 5997 | size_t best_size = 0; |
5a580b3a | 5998 | unsigned long int i; |
5a580b3a | 5999 | |
5a580b3a AM |
6000 | /* We have a problem here. The following code to optimize the table |
6001 | size requires an integer type with more the 32 bits. If | |
6002 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
6003 | #ifdef BFD_HOST_U_64_BIT | |
6004 | if (info->optimize) | |
6005 | { | |
5a580b3a AM |
6006 | size_t minsize; |
6007 | size_t maxsize; | |
6008 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
5a580b3a | 6009 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 6010 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 6011 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 6012 | unsigned long int *counts; |
d40f3da9 | 6013 | bfd_size_type amt; |
0883b6e0 | 6014 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
6015 | |
6016 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
6017 | that the hashing table must at least have NSYMS/4 and at most | |
6018 | 2*NSYMS buckets. */ | |
6019 | minsize = nsyms / 4; | |
6020 | if (minsize == 0) | |
6021 | minsize = 1; | |
6022 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
6023 | if (gnu_hash) |
6024 | { | |
6025 | if (minsize < 2) | |
6026 | minsize = 2; | |
6027 | if ((best_size & 31) == 0) | |
6028 | ++best_size; | |
6029 | } | |
5a580b3a AM |
6030 | |
6031 | /* Create array where we count the collisions in. We must use bfd_malloc | |
6032 | since the size could be large. */ | |
6033 | amt = maxsize; | |
6034 | amt *= sizeof (unsigned long int); | |
a50b1753 | 6035 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 6036 | if (counts == NULL) |
fdc90cb4 | 6037 | return 0; |
5a580b3a AM |
6038 | |
6039 | /* Compute the "optimal" size for the hash table. The criteria is a | |
6040 | minimal chain length. The minor criteria is (of course) the size | |
6041 | of the table. */ | |
6042 | for (i = minsize; i < maxsize; ++i) | |
6043 | { | |
6044 | /* Walk through the array of hashcodes and count the collisions. */ | |
6045 | BFD_HOST_U_64_BIT max; | |
6046 | unsigned long int j; | |
6047 | unsigned long int fact; | |
6048 | ||
fdc90cb4 JJ |
6049 | if (gnu_hash && (i & 31) == 0) |
6050 | continue; | |
6051 | ||
5a580b3a AM |
6052 | memset (counts, '\0', i * sizeof (unsigned long int)); |
6053 | ||
6054 | /* Determine how often each hash bucket is used. */ | |
6055 | for (j = 0; j < nsyms; ++j) | |
6056 | ++counts[hashcodes[j] % i]; | |
6057 | ||
6058 | /* For the weight function we need some information about the | |
6059 | pagesize on the target. This is information need not be 100% | |
6060 | accurate. Since this information is not available (so far) we | |
6061 | define it here to a reasonable default value. If it is crucial | |
6062 | to have a better value some day simply define this value. */ | |
6063 | # ifndef BFD_TARGET_PAGESIZE | |
6064 | # define BFD_TARGET_PAGESIZE (4096) | |
6065 | # endif | |
6066 | ||
fdc90cb4 JJ |
6067 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
6068 | and the chains. */ | |
6069 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
6070 | |
6071 | # if 1 | |
6072 | /* Variant 1: optimize for short chains. We add the squares | |
6073 | of all the chain lengths (which favors many small chain | |
6074 | over a few long chains). */ | |
6075 | for (j = 0; j < i; ++j) | |
6076 | max += counts[j] * counts[j]; | |
6077 | ||
6078 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 6079 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6080 | max *= fact * fact; |
6081 | # else | |
6082 | /* Variant 2: Optimize a lot more for small table. Here we | |
6083 | also add squares of the size but we also add penalties for | |
6084 | empty slots (the +1 term). */ | |
6085 | for (j = 0; j < i; ++j) | |
6086 | max += (1 + counts[j]) * (1 + counts[j]); | |
6087 | ||
6088 | /* The overall size of the table is considered, but not as | |
6089 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 6090 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6091 | max *= fact; |
6092 | # endif | |
6093 | ||
6094 | /* Compare with current best results. */ | |
6095 | if (max < best_chlen) | |
6096 | { | |
6097 | best_chlen = max; | |
6098 | best_size = i; | |
ca4be51c | 6099 | no_improvement_count = 0; |
5a580b3a | 6100 | } |
0883b6e0 NC |
6101 | /* PR 11843: Avoid futile long searches for the best bucket size |
6102 | when there are a large number of symbols. */ | |
6103 | else if (++no_improvement_count == 100) | |
6104 | break; | |
5a580b3a AM |
6105 | } |
6106 | ||
6107 | free (counts); | |
6108 | } | |
6109 | else | |
6110 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
6111 | { | |
6112 | /* This is the fallback solution if no 64bit type is available or if we | |
6113 | are not supposed to spend much time on optimizations. We select the | |
6114 | bucket count using a fixed set of numbers. */ | |
6115 | for (i = 0; elf_buckets[i] != 0; i++) | |
6116 | { | |
6117 | best_size = elf_buckets[i]; | |
fdc90cb4 | 6118 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
6119 | break; |
6120 | } | |
fdc90cb4 JJ |
6121 | if (gnu_hash && best_size < 2) |
6122 | best_size = 2; | |
5a580b3a AM |
6123 | } |
6124 | ||
5a580b3a AM |
6125 | return best_size; |
6126 | } | |
6127 | ||
d0bf826b AM |
6128 | /* Size any SHT_GROUP section for ld -r. */ |
6129 | ||
6130 | bfd_boolean | |
6131 | _bfd_elf_size_group_sections (struct bfd_link_info *info) | |
6132 | { | |
6133 | bfd *ibfd; | |
57963c05 | 6134 | asection *s; |
d0bf826b | 6135 | |
c72f2fb2 | 6136 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b | 6137 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
57963c05 AM |
6138 | && (s = ibfd->sections) != NULL |
6139 | && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
d0bf826b AM |
6140 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) |
6141 | return FALSE; | |
6142 | return TRUE; | |
6143 | } | |
6144 | ||
04c3a755 NS |
6145 | /* Set a default stack segment size. The value in INFO wins. If it |
6146 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
6147 | undefined it is initialized. */ | |
6148 | ||
6149 | bfd_boolean | |
6150 | bfd_elf_stack_segment_size (bfd *output_bfd, | |
6151 | struct bfd_link_info *info, | |
6152 | const char *legacy_symbol, | |
6153 | bfd_vma default_size) | |
6154 | { | |
6155 | struct elf_link_hash_entry *h = NULL; | |
6156 | ||
6157 | /* Look for legacy symbol. */ | |
6158 | if (legacy_symbol) | |
6159 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
6160 | FALSE, FALSE, FALSE); | |
6161 | if (h && (h->root.type == bfd_link_hash_defined | |
6162 | || h->root.type == bfd_link_hash_defweak) | |
6163 | && h->def_regular | |
6164 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
6165 | { | |
6166 | /* The symbol has no type if specified on the command line. */ | |
6167 | h->type = STT_OBJECT; | |
6168 | if (info->stacksize) | |
695344c0 | 6169 | /* xgettext:c-format */ |
871b3ab2 | 6170 | _bfd_error_handler (_("%pB: stack size specified and %s set"), |
4eca0228 | 6171 | output_bfd, legacy_symbol); |
04c3a755 | 6172 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 6173 | /* xgettext:c-format */ |
871b3ab2 | 6174 | _bfd_error_handler (_("%pB: %s not absolute"), |
4eca0228 | 6175 | output_bfd, legacy_symbol); |
04c3a755 NS |
6176 | else |
6177 | info->stacksize = h->root.u.def.value; | |
6178 | } | |
6179 | ||
6180 | if (!info->stacksize) | |
6181 | /* If the user didn't set a size, or explicitly inhibit the | |
6182 | size, set it now. */ | |
6183 | info->stacksize = default_size; | |
6184 | ||
6185 | /* Provide the legacy symbol, if it is referenced. */ | |
6186 | if (h && (h->root.type == bfd_link_hash_undefined | |
6187 | || h->root.type == bfd_link_hash_undefweak)) | |
6188 | { | |
6189 | struct bfd_link_hash_entry *bh = NULL; | |
6190 | ||
6191 | if (!(_bfd_generic_link_add_one_symbol | |
6192 | (info, output_bfd, legacy_symbol, | |
6193 | BSF_GLOBAL, bfd_abs_section_ptr, | |
6194 | info->stacksize >= 0 ? info->stacksize : 0, | |
6195 | NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) | |
6196 | return FALSE; | |
6197 | ||
6198 | h = (struct elf_link_hash_entry *) bh; | |
6199 | h->def_regular = 1; | |
6200 | h->type = STT_OBJECT; | |
6201 | } | |
6202 | ||
6203 | return TRUE; | |
6204 | } | |
6205 | ||
b531344c MR |
6206 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
6207 | ||
6208 | struct elf_gc_sweep_symbol_info | |
6209 | { | |
6210 | struct bfd_link_info *info; | |
6211 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
6212 | bfd_boolean); | |
6213 | }; | |
6214 | ||
6215 | static bfd_boolean | |
6216 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) | |
6217 | { | |
6218 | if (!h->mark | |
6219 | && (((h->root.type == bfd_link_hash_defined | |
6220 | || h->root.type == bfd_link_hash_defweak) | |
6221 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) | |
6222 | && h->root.u.def.section->gc_mark)) | |
6223 | || h->root.type == bfd_link_hash_undefined | |
6224 | || h->root.type == bfd_link_hash_undefweak)) | |
6225 | { | |
6226 | struct elf_gc_sweep_symbol_info *inf; | |
6227 | ||
6228 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
6229 | (*inf->hide_symbol) (inf->info, h, TRUE); | |
6230 | h->def_regular = 0; | |
6231 | h->ref_regular = 0; | |
6232 | h->ref_regular_nonweak = 0; | |
6233 | } | |
6234 | ||
6235 | return TRUE; | |
6236 | } | |
6237 | ||
5a580b3a AM |
6238 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
6239 | called by the ELF linker emulation before_allocation routine. We | |
6240 | must set the sizes of the sections before the linker sets the | |
6241 | addresses of the various sections. */ | |
6242 | ||
6243 | bfd_boolean | |
6244 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
6245 | const char *soname, | |
6246 | const char *rpath, | |
6247 | const char *filter_shlib, | |
7ee314fa AM |
6248 | const char *audit, |
6249 | const char *depaudit, | |
5a580b3a AM |
6250 | const char * const *auxiliary_filters, |
6251 | struct bfd_link_info *info, | |
fd91d419 | 6252 | asection **sinterpptr) |
5a580b3a | 6253 | { |
5a580b3a AM |
6254 | bfd *dynobj; |
6255 | const struct elf_backend_data *bed; | |
5a580b3a AM |
6256 | |
6257 | *sinterpptr = NULL; | |
6258 | ||
5a580b3a AM |
6259 | if (!is_elf_hash_table (info->hash)) |
6260 | return TRUE; | |
6261 | ||
5a580b3a AM |
6262 | dynobj = elf_hash_table (info)->dynobj; |
6263 | ||
9a2a56cc | 6264 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6265 | { |
902e9fc7 MR |
6266 | struct bfd_elf_version_tree *verdefs; |
6267 | struct elf_info_failed asvinfo; | |
5a580b3a AM |
6268 | struct bfd_elf_version_tree *t; |
6269 | struct bfd_elf_version_expr *d; | |
902e9fc7 | 6270 | asection *s; |
e6699019 | 6271 | size_t soname_indx; |
7ee314fa | 6272 | |
5a580b3a AM |
6273 | /* If we are supposed to export all symbols into the dynamic symbol |
6274 | table (this is not the normal case), then do so. */ | |
55255dae | 6275 | if (info->export_dynamic |
0e1862bb | 6276 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a | 6277 | { |
3d13f3e9 AM |
6278 | struct elf_info_failed eif; |
6279 | ||
6280 | eif.info = info; | |
6281 | eif.failed = FALSE; | |
5a580b3a AM |
6282 | elf_link_hash_traverse (elf_hash_table (info), |
6283 | _bfd_elf_export_symbol, | |
6284 | &eif); | |
6285 | if (eif.failed) | |
6286 | return FALSE; | |
6287 | } | |
6288 | ||
e6699019 L |
6289 | if (soname != NULL) |
6290 | { | |
6291 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6292 | soname, TRUE); | |
6293 | if (soname_indx == (size_t) -1 | |
6294 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
6295 | return FALSE; | |
6296 | } | |
6297 | else | |
6298 | soname_indx = (size_t) -1; | |
6299 | ||
5a580b3a | 6300 | /* Make all global versions with definition. */ |
fd91d419 | 6301 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6302 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6303 | if (!d->symver && d->literal) |
5a580b3a AM |
6304 | { |
6305 | const char *verstr, *name; | |
6306 | size_t namelen, verlen, newlen; | |
93252b1c | 6307 | char *newname, *p, leading_char; |
5a580b3a AM |
6308 | struct elf_link_hash_entry *newh; |
6309 | ||
93252b1c | 6310 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6311 | name = d->pattern; |
93252b1c | 6312 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6313 | verstr = t->name; |
6314 | verlen = strlen (verstr); | |
6315 | newlen = namelen + verlen + 3; | |
6316 | ||
a50b1753 | 6317 | newname = (char *) bfd_malloc (newlen); |
5a580b3a AM |
6318 | if (newname == NULL) |
6319 | return FALSE; | |
93252b1c MF |
6320 | newname[0] = leading_char; |
6321 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6322 | |
6323 | /* Check the hidden versioned definition. */ | |
6324 | p = newname + namelen; | |
6325 | *p++ = ELF_VER_CHR; | |
6326 | memcpy (p, verstr, verlen + 1); | |
6327 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6328 | newname, FALSE, FALSE, | |
6329 | FALSE); | |
6330 | if (newh == NULL | |
6331 | || (newh->root.type != bfd_link_hash_defined | |
6332 | && newh->root.type != bfd_link_hash_defweak)) | |
6333 | { | |
6334 | /* Check the default versioned definition. */ | |
6335 | *p++ = ELF_VER_CHR; | |
6336 | memcpy (p, verstr, verlen + 1); | |
6337 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
6338 | newname, FALSE, FALSE, | |
6339 | FALSE); | |
6340 | } | |
6341 | free (newname); | |
6342 | ||
6343 | /* Mark this version if there is a definition and it is | |
6344 | not defined in a shared object. */ | |
6345 | if (newh != NULL | |
f5385ebf | 6346 | && !newh->def_dynamic |
5a580b3a AM |
6347 | && (newh->root.type == bfd_link_hash_defined |
6348 | || newh->root.type == bfd_link_hash_defweak)) | |
6349 | d->symver = 1; | |
6350 | } | |
6351 | ||
6352 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6353 | asvinfo.info = info; |
5a580b3a AM |
6354 | asvinfo.failed = FALSE; |
6355 | ||
6356 | elf_link_hash_traverse (elf_hash_table (info), | |
6357 | _bfd_elf_link_assign_sym_version, | |
6358 | &asvinfo); | |
6359 | if (asvinfo.failed) | |
6360 | return FALSE; | |
6361 | ||
6362 | if (!info->allow_undefined_version) | |
6363 | { | |
6364 | /* Check if all global versions have a definition. */ | |
3d13f3e9 | 6365 | bfd_boolean all_defined = TRUE; |
fd91d419 | 6366 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6367 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6368 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6369 | { |
4eca0228 | 6370 | _bfd_error_handler |
5a580b3a AM |
6371 | (_("%s: undefined version: %s"), |
6372 | d->pattern, t->name); | |
6373 | all_defined = FALSE; | |
6374 | } | |
6375 | ||
6376 | if (!all_defined) | |
6377 | { | |
6378 | bfd_set_error (bfd_error_bad_value); | |
6379 | return FALSE; | |
6380 | } | |
6381 | } | |
6382 | ||
902e9fc7 MR |
6383 | /* Set up the version definition section. */ |
6384 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); | |
6385 | BFD_ASSERT (s != NULL); | |
5a580b3a | 6386 | |
902e9fc7 MR |
6387 | /* We may have created additional version definitions if we are |
6388 | just linking a regular application. */ | |
6389 | verdefs = info->version_info; | |
5a580b3a | 6390 | |
902e9fc7 MR |
6391 | /* Skip anonymous version tag. */ |
6392 | if (verdefs != NULL && verdefs->vernum == 0) | |
6393 | verdefs = verdefs->next; | |
5a580b3a | 6394 | |
902e9fc7 MR |
6395 | if (verdefs == NULL && !info->create_default_symver) |
6396 | s->flags |= SEC_EXCLUDE; | |
6397 | else | |
5a580b3a | 6398 | { |
902e9fc7 MR |
6399 | unsigned int cdefs; |
6400 | bfd_size_type size; | |
6401 | bfd_byte *p; | |
6402 | Elf_Internal_Verdef def; | |
6403 | Elf_Internal_Verdaux defaux; | |
6404 | struct bfd_link_hash_entry *bh; | |
6405 | struct elf_link_hash_entry *h; | |
6406 | const char *name; | |
5a580b3a | 6407 | |
902e9fc7 MR |
6408 | cdefs = 0; |
6409 | size = 0; | |
5a580b3a | 6410 | |
902e9fc7 MR |
6411 | /* Make space for the base version. */ |
6412 | size += sizeof (Elf_External_Verdef); | |
6413 | size += sizeof (Elf_External_Verdaux); | |
6414 | ++cdefs; | |
6415 | ||
6416 | /* Make space for the default version. */ | |
6417 | if (info->create_default_symver) | |
6418 | { | |
6419 | size += sizeof (Elf_External_Verdef); | |
6420 | ++cdefs; | |
3e3b46e5 PB |
6421 | } |
6422 | ||
5a580b3a AM |
6423 | for (t = verdefs; t != NULL; t = t->next) |
6424 | { | |
6425 | struct bfd_elf_version_deps *n; | |
6426 | ||
a6cc6b3b RO |
6427 | /* Don't emit base version twice. */ |
6428 | if (t->vernum == 0) | |
6429 | continue; | |
6430 | ||
5a580b3a AM |
6431 | size += sizeof (Elf_External_Verdef); |
6432 | size += sizeof (Elf_External_Verdaux); | |
6433 | ++cdefs; | |
6434 | ||
6435 | for (n = t->deps; n != NULL; n = n->next) | |
6436 | size += sizeof (Elf_External_Verdaux); | |
6437 | } | |
6438 | ||
eea6121a | 6439 | s->size = size; |
a50b1753 | 6440 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6441 | if (s->contents == NULL && s->size != 0) |
5a580b3a AM |
6442 | return FALSE; |
6443 | ||
6444 | /* Fill in the version definition section. */ | |
6445 | ||
6446 | p = s->contents; | |
6447 | ||
6448 | def.vd_version = VER_DEF_CURRENT; | |
6449 | def.vd_flags = VER_FLG_BASE; | |
6450 | def.vd_ndx = 1; | |
6451 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6452 | if (info->create_default_symver) |
6453 | { | |
6454 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6455 | def.vd_next = sizeof (Elf_External_Verdef); | |
6456 | } | |
6457 | else | |
6458 | { | |
6459 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6460 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6461 | + sizeof (Elf_External_Verdaux)); | |
6462 | } | |
5a580b3a | 6463 | |
ef53be89 | 6464 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6465 | { |
6466 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6467 | soname_indx); | |
6468 | def.vd_hash = bfd_elf_hash (soname); | |
6469 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6470 | name = soname; |
5a580b3a AM |
6471 | } |
6472 | else | |
6473 | { | |
ef53be89 | 6474 | size_t indx; |
5a580b3a | 6475 | |
06084812 | 6476 | name = lbasename (output_bfd->filename); |
5a580b3a AM |
6477 | def.vd_hash = bfd_elf_hash (name); |
6478 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6479 | name, FALSE); | |
ef53be89 | 6480 | if (indx == (size_t) -1) |
5a580b3a AM |
6481 | return FALSE; |
6482 | defaux.vda_name = indx; | |
6483 | } | |
6484 | defaux.vda_next = 0; | |
6485 | ||
6486 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6487 | (Elf_External_Verdef *) p); | |
6488 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6489 | if (info->create_default_symver) |
6490 | { | |
6491 | /* Add a symbol representing this version. */ | |
6492 | bh = NULL; | |
6493 | if (! (_bfd_generic_link_add_one_symbol | |
6494 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6495 | 0, NULL, FALSE, | |
6496 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6497 | return FALSE; | |
6498 | h = (struct elf_link_hash_entry *) bh; | |
6499 | h->non_elf = 0; | |
6500 | h->def_regular = 1; | |
6501 | h->type = STT_OBJECT; | |
6502 | h->verinfo.vertree = NULL; | |
6503 | ||
6504 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
6505 | return FALSE; | |
6506 | ||
6507 | /* Create a duplicate of the base version with the same | |
6508 | aux block, but different flags. */ | |
6509 | def.vd_flags = 0; | |
6510 | def.vd_ndx = 2; | |
6511 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6512 | if (verdefs) | |
6513 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6514 | + sizeof (Elf_External_Verdaux)); | |
6515 | else | |
6516 | def.vd_next = 0; | |
6517 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6518 | (Elf_External_Verdef *) p); | |
6519 | p += sizeof (Elf_External_Verdef); | |
6520 | } | |
5a580b3a AM |
6521 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6522 | (Elf_External_Verdaux *) p); | |
6523 | p += sizeof (Elf_External_Verdaux); | |
6524 | ||
6525 | for (t = verdefs; t != NULL; t = t->next) | |
6526 | { | |
6527 | unsigned int cdeps; | |
6528 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6529 | |
a6cc6b3b RO |
6530 | /* Don't emit the base version twice. */ |
6531 | if (t->vernum == 0) | |
6532 | continue; | |
6533 | ||
5a580b3a AM |
6534 | cdeps = 0; |
6535 | for (n = t->deps; n != NULL; n = n->next) | |
6536 | ++cdeps; | |
6537 | ||
6538 | /* Add a symbol representing this version. */ | |
6539 | bh = NULL; | |
6540 | if (! (_bfd_generic_link_add_one_symbol | |
6541 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
6542 | 0, NULL, FALSE, | |
6543 | get_elf_backend_data (dynobj)->collect, &bh))) | |
6544 | return FALSE; | |
6545 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
6546 | h->non_elf = 0; |
6547 | h->def_regular = 1; | |
5a580b3a AM |
6548 | h->type = STT_OBJECT; |
6549 | h->verinfo.vertree = t; | |
6550 | ||
c152c796 | 6551 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
6552 | return FALSE; |
6553 | ||
6554 | def.vd_version = VER_DEF_CURRENT; | |
6555 | def.vd_flags = 0; | |
6556 | if (t->globals.list == NULL | |
6557 | && t->locals.list == NULL | |
6558 | && ! t->used) | |
6559 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6560 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6561 | def.vd_cnt = cdeps + 1; |
6562 | def.vd_hash = bfd_elf_hash (t->name); | |
6563 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6564 | def.vd_next = 0; | |
a6cc6b3b RO |
6565 | |
6566 | /* If a basever node is next, it *must* be the last node in | |
6567 | the chain, otherwise Verdef construction breaks. */ | |
6568 | if (t->next != NULL && t->next->vernum == 0) | |
6569 | BFD_ASSERT (t->next->next == NULL); | |
6570 | ||
6571 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6572 | def.vd_next = (sizeof (Elf_External_Verdef) |
6573 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6574 | ||
6575 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6576 | (Elf_External_Verdef *) p); | |
6577 | p += sizeof (Elf_External_Verdef); | |
6578 | ||
6579 | defaux.vda_name = h->dynstr_index; | |
6580 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6581 | h->dynstr_index); | |
6582 | defaux.vda_next = 0; | |
6583 | if (t->deps != NULL) | |
6584 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6585 | t->name_indx = defaux.vda_name; | |
6586 | ||
6587 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6588 | (Elf_External_Verdaux *) p); | |
6589 | p += sizeof (Elf_External_Verdaux); | |
6590 | ||
6591 | for (n = t->deps; n != NULL; n = n->next) | |
6592 | { | |
6593 | if (n->version_needed == NULL) | |
6594 | { | |
6595 | /* This can happen if there was an error in the | |
6596 | version script. */ | |
6597 | defaux.vda_name = 0; | |
6598 | } | |
6599 | else | |
6600 | { | |
6601 | defaux.vda_name = n->version_needed->name_indx; | |
6602 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6603 | defaux.vda_name); | |
6604 | } | |
6605 | if (n->next == NULL) | |
6606 | defaux.vda_next = 0; | |
6607 | else | |
6608 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6609 | ||
6610 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6611 | (Elf_External_Verdaux *) p); | |
6612 | p += sizeof (Elf_External_Verdaux); | |
6613 | } | |
6614 | } | |
6615 | ||
5a580b3a AM |
6616 | elf_tdata (output_bfd)->cverdefs = cdefs; |
6617 | } | |
902e9fc7 MR |
6618 | } |
6619 | ||
6620 | bed = get_elf_backend_data (output_bfd); | |
6621 | ||
6622 | if (info->gc_sections && bed->can_gc_sections) | |
6623 | { | |
6624 | struct elf_gc_sweep_symbol_info sweep_info; | |
902e9fc7 MR |
6625 | |
6626 | /* Remove the symbols that were in the swept sections from the | |
3d13f3e9 | 6627 | dynamic symbol table. */ |
902e9fc7 MR |
6628 | sweep_info.info = info; |
6629 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
6630 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
6631 | &sweep_info); | |
3d13f3e9 AM |
6632 | } |
6633 | ||
6634 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6635 | { | |
6636 | asection *s; | |
6637 | struct elf_find_verdep_info sinfo; | |
6638 | ||
6639 | /* Work out the size of the version reference section. */ | |
6640 | ||
6641 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); | |
6642 | BFD_ASSERT (s != NULL); | |
902e9fc7 | 6643 | |
3d13f3e9 AM |
6644 | sinfo.info = info; |
6645 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
6646 | if (sinfo.vers == 0) | |
6647 | sinfo.vers = 1; | |
6648 | sinfo.failed = FALSE; | |
6649 | ||
6650 | elf_link_hash_traverse (elf_hash_table (info), | |
6651 | _bfd_elf_link_find_version_dependencies, | |
6652 | &sinfo); | |
6653 | if (sinfo.failed) | |
6654 | return FALSE; | |
6655 | ||
6656 | if (elf_tdata (output_bfd)->verref == NULL) | |
6657 | s->flags |= SEC_EXCLUDE; | |
6658 | else | |
6659 | { | |
6660 | Elf_Internal_Verneed *vn; | |
6661 | unsigned int size; | |
6662 | unsigned int crefs; | |
6663 | bfd_byte *p; | |
6664 | ||
6665 | /* Build the version dependency section. */ | |
6666 | size = 0; | |
6667 | crefs = 0; | |
6668 | for (vn = elf_tdata (output_bfd)->verref; | |
6669 | vn != NULL; | |
6670 | vn = vn->vn_nextref) | |
6671 | { | |
6672 | Elf_Internal_Vernaux *a; | |
6673 | ||
6674 | size += sizeof (Elf_External_Verneed); | |
6675 | ++crefs; | |
6676 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6677 | size += sizeof (Elf_External_Vernaux); | |
6678 | } | |
6679 | ||
6680 | s->size = size; | |
6681 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); | |
6682 | if (s->contents == NULL) | |
6683 | return FALSE; | |
6684 | ||
6685 | p = s->contents; | |
6686 | for (vn = elf_tdata (output_bfd)->verref; | |
6687 | vn != NULL; | |
6688 | vn = vn->vn_nextref) | |
6689 | { | |
6690 | unsigned int caux; | |
6691 | Elf_Internal_Vernaux *a; | |
6692 | size_t indx; | |
6693 | ||
6694 | caux = 0; | |
6695 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6696 | ++caux; | |
6697 | ||
6698 | vn->vn_version = VER_NEED_CURRENT; | |
6699 | vn->vn_cnt = caux; | |
6700 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6701 | elf_dt_name (vn->vn_bfd) != NULL | |
6702 | ? elf_dt_name (vn->vn_bfd) | |
6703 | : lbasename (vn->vn_bfd->filename), | |
6704 | FALSE); | |
6705 | if (indx == (size_t) -1) | |
6706 | return FALSE; | |
6707 | vn->vn_file = indx; | |
6708 | vn->vn_aux = sizeof (Elf_External_Verneed); | |
6709 | if (vn->vn_nextref == NULL) | |
6710 | vn->vn_next = 0; | |
6711 | else | |
6712 | vn->vn_next = (sizeof (Elf_External_Verneed) | |
6713 | + caux * sizeof (Elf_External_Vernaux)); | |
6714 | ||
6715 | _bfd_elf_swap_verneed_out (output_bfd, vn, | |
6716 | (Elf_External_Verneed *) p); | |
6717 | p += sizeof (Elf_External_Verneed); | |
6718 | ||
6719 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
6720 | { | |
6721 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
6722 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6723 | a->vna_nodename, FALSE); | |
6724 | if (indx == (size_t) -1) | |
6725 | return FALSE; | |
6726 | a->vna_name = indx; | |
6727 | if (a->vna_nextptr == NULL) | |
6728 | a->vna_next = 0; | |
6729 | else | |
6730 | a->vna_next = sizeof (Elf_External_Vernaux); | |
6731 | ||
6732 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
6733 | (Elf_External_Vernaux *) p); | |
6734 | p += sizeof (Elf_External_Vernaux); | |
6735 | } | |
6736 | } | |
6737 | ||
6738 | elf_tdata (output_bfd)->cverrefs = crefs; | |
6739 | } | |
902e9fc7 MR |
6740 | } |
6741 | ||
6742 | /* Any syms created from now on start with -1 in | |
6743 | got.refcount/offset and plt.refcount/offset. */ | |
6744 | elf_hash_table (info)->init_got_refcount | |
6745 | = elf_hash_table (info)->init_got_offset; | |
6746 | elf_hash_table (info)->init_plt_refcount | |
6747 | = elf_hash_table (info)->init_plt_offset; | |
6748 | ||
6749 | if (bfd_link_relocatable (info) | |
6750 | && !_bfd_elf_size_group_sections (info)) | |
6751 | return FALSE; | |
6752 | ||
6753 | /* The backend may have to create some sections regardless of whether | |
6754 | we're dynamic or not. */ | |
6755 | if (bed->elf_backend_always_size_sections | |
6756 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
6757 | return FALSE; | |
6758 | ||
6759 | /* Determine any GNU_STACK segment requirements, after the backend | |
6760 | has had a chance to set a default segment size. */ | |
6761 | if (info->execstack) | |
6762 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; | |
6763 | else if (info->noexecstack) | |
6764 | elf_stack_flags (output_bfd) = PF_R | PF_W; | |
6765 | else | |
6766 | { | |
6767 | bfd *inputobj; | |
6768 | asection *notesec = NULL; | |
6769 | int exec = 0; | |
6770 | ||
6771 | for (inputobj = info->input_bfds; | |
6772 | inputobj; | |
6773 | inputobj = inputobj->link.next) | |
6774 | { | |
6775 | asection *s; | |
6776 | ||
6777 | if (inputobj->flags | |
6778 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
6779 | continue; | |
57963c05 AM |
6780 | s = inputobj->sections; |
6781 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
6782 | continue; | |
6783 | ||
902e9fc7 MR |
6784 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); |
6785 | if (s) | |
6786 | { | |
6787 | if (s->flags & SEC_CODE) | |
6788 | exec = PF_X; | |
6789 | notesec = s; | |
6790 | } | |
6791 | else if (bed->default_execstack) | |
6792 | exec = PF_X; | |
6793 | } | |
6794 | if (notesec || info->stacksize > 0) | |
6795 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; | |
6796 | if (notesec && exec && bfd_link_relocatable (info) | |
6797 | && notesec->output_section != bfd_abs_section_ptr) | |
6798 | notesec->output_section->flags |= SEC_CODE; | |
6799 | } | |
6800 | ||
6801 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
6802 | { | |
6803 | struct elf_info_failed eif; | |
6804 | struct elf_link_hash_entry *h; | |
6805 | asection *dynstr; | |
6806 | asection *s; | |
6807 | ||
6808 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); | |
6809 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); | |
6810 | ||
902e9fc7 MR |
6811 | if (info->symbolic) |
6812 | { | |
6813 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
6814 | return FALSE; | |
6815 | info->flags |= DF_SYMBOLIC; | |
6816 | } | |
6817 | ||
6818 | if (rpath != NULL) | |
6819 | { | |
6820 | size_t indx; | |
6821 | bfd_vma tag; | |
6822 | ||
6823 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
6824 | TRUE); | |
6825 | if (indx == (size_t) -1) | |
6826 | return FALSE; | |
6827 | ||
6828 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; | |
6829 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
6830 | return FALSE; | |
6831 | } | |
6832 | ||
6833 | if (filter_shlib != NULL) | |
6834 | { | |
6835 | size_t indx; | |
6836 | ||
6837 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6838 | filter_shlib, TRUE); | |
6839 | if (indx == (size_t) -1 | |
6840 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
6841 | return FALSE; | |
6842 | } | |
6843 | ||
6844 | if (auxiliary_filters != NULL) | |
6845 | { | |
6846 | const char * const *p; | |
6847 | ||
6848 | for (p = auxiliary_filters; *p != NULL; p++) | |
6849 | { | |
6850 | size_t indx; | |
6851 | ||
6852 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
6853 | *p, TRUE); | |
6854 | if (indx == (size_t) -1 | |
6855 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
6856 | return FALSE; | |
6857 | } | |
6858 | } | |
6859 | ||
6860 | if (audit != NULL) | |
6861 | { | |
6862 | size_t indx; | |
6863 | ||
6864 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
6865 | TRUE); | |
6866 | if (indx == (size_t) -1 | |
6867 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
6868 | return FALSE; | |
6869 | } | |
6870 | ||
6871 | if (depaudit != NULL) | |
6872 | { | |
6873 | size_t indx; | |
6874 | ||
6875 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
6876 | TRUE); | |
6877 | if (indx == (size_t) -1 | |
6878 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
6879 | return FALSE; | |
6880 | } | |
6881 | ||
6882 | eif.info = info; | |
6883 | eif.failed = FALSE; | |
6884 | ||
6885 | /* Find all symbols which were defined in a dynamic object and make | |
6886 | the backend pick a reasonable value for them. */ | |
6887 | elf_link_hash_traverse (elf_hash_table (info), | |
6888 | _bfd_elf_adjust_dynamic_symbol, | |
6889 | &eif); | |
6890 | if (eif.failed) | |
6891 | return FALSE; | |
6892 | ||
6893 | /* Add some entries to the .dynamic section. We fill in some of the | |
6894 | values later, in bfd_elf_final_link, but we must add the entries | |
6895 | now so that we know the final size of the .dynamic section. */ | |
6896 | ||
6897 | /* If there are initialization and/or finalization functions to | |
6898 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
6899 | h = (info->init_function | |
6900 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6901 | info->init_function, FALSE, | |
6902 | FALSE, FALSE) | |
6903 | : NULL); | |
6904 | if (h != NULL | |
6905 | && (h->ref_regular | |
6906 | || h->def_regular)) | |
6907 | { | |
6908 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
6909 | return FALSE; | |
6910 | } | |
6911 | h = (info->fini_function | |
6912 | ? elf_link_hash_lookup (elf_hash_table (info), | |
6913 | info->fini_function, FALSE, | |
6914 | FALSE, FALSE) | |
6915 | : NULL); | |
6916 | if (h != NULL | |
6917 | && (h->ref_regular | |
6918 | || h->def_regular)) | |
6919 | { | |
6920 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
6921 | return FALSE; | |
6922 | } | |
6923 | ||
6924 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); | |
6925 | if (s != NULL && s->linker_has_input) | |
6926 | { | |
6927 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
6928 | if (! bfd_link_executable (info)) | |
6929 | { | |
6930 | bfd *sub; | |
6931 | asection *o; | |
6932 | ||
57963c05 AM |
6933 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
6934 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
6935 | && (o = sub->sections) != NULL | |
6936 | && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) | |
902e9fc7 MR |
6937 | for (o = sub->sections; o != NULL; o = o->next) |
6938 | if (elf_section_data (o)->this_hdr.sh_type | |
6939 | == SHT_PREINIT_ARRAY) | |
6940 | { | |
6941 | _bfd_error_handler | |
871b3ab2 | 6942 | (_("%pB: .preinit_array section is not allowed in DSO"), |
902e9fc7 MR |
6943 | sub); |
6944 | break; | |
6945 | } | |
6946 | ||
6947 | bfd_set_error (bfd_error_nonrepresentable_section); | |
6948 | return FALSE; | |
6949 | } | |
6950 | ||
6951 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
6952 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
6953 | return FALSE; | |
6954 | } | |
6955 | s = bfd_get_section_by_name (output_bfd, ".init_array"); | |
6956 | if (s != NULL && s->linker_has_input) | |
6957 | { | |
6958 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
6959 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
6960 | return FALSE; | |
6961 | } | |
6962 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); | |
6963 | if (s != NULL && s->linker_has_input) | |
6964 | { | |
6965 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
6966 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
6967 | return FALSE; | |
6968 | } | |
6969 | ||
6970 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); | |
6971 | /* If .dynstr is excluded from the link, we don't want any of | |
6972 | these tags. Strictly, we should be checking each section | |
6973 | individually; This quick check covers for the case where | |
6974 | someone does a /DISCARD/ : { *(*) }. */ | |
6975 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
6976 | { | |
6977 | bfd_size_type strsize; | |
6978 | ||
6979 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
6980 | if ((info->emit_hash | |
6981 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
6982 | || (info->emit_gnu_hash | |
6983 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) | |
6984 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
6985 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
6986 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
6987 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6988 | bed->s->sizeof_sym)) | |
6989 | return FALSE; | |
6990 | } | |
6991 | } | |
6992 | ||
6993 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
6994 | return FALSE; | |
6995 | ||
6996 | /* The backend must work out the sizes of all the other dynamic | |
6997 | sections. */ | |
6998 | if (dynobj != NULL | |
6999 | && bed->elf_backend_size_dynamic_sections != NULL | |
7000 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
7001 | return FALSE; | |
7002 | ||
7003 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
7004 | { | |
902e9fc7 MR |
7005 | if (elf_tdata (output_bfd)->cverdefs) |
7006 | { | |
7007 | unsigned int crefs = elf_tdata (output_bfd)->cverdefs; | |
7008 | ||
7009 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
7010 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) | |
7011 | return FALSE; | |
7012 | } | |
7013 | ||
7014 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
7015 | { | |
7016 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
7017 | return FALSE; | |
7018 | } | |
7019 | else if (info->flags & DF_BIND_NOW) | |
7020 | { | |
7021 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
7022 | return FALSE; | |
7023 | } | |
7024 | ||
7025 | if (info->flags_1) | |
7026 | { | |
7027 | if (bfd_link_executable (info)) | |
7028 | info->flags_1 &= ~ (DF_1_INITFIRST | |
7029 | | DF_1_NODELETE | |
7030 | | DF_1_NOOPEN); | |
7031 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
7032 | return FALSE; | |
7033 | } | |
7034 | ||
7035 | if (elf_tdata (output_bfd)->cverrefs) | |
7036 | { | |
7037 | unsigned int crefs = elf_tdata (output_bfd)->cverrefs; | |
7038 | ||
7039 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
7040 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
7041 | return FALSE; | |
7042 | } | |
5a580b3a | 7043 | |
8423293d AM |
7044 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
7045 | && elf_tdata (output_bfd)->cverdefs == 0) | |
63f452a8 | 7046 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) |
8423293d | 7047 | { |
902e9fc7 MR |
7048 | asection *s; |
7049 | ||
3d4d4302 | 7050 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
7051 | s->flags |= SEC_EXCLUDE; |
7052 | } | |
7053 | } | |
7054 | return TRUE; | |
7055 | } | |
7056 | ||
74541ad4 AM |
7057 | /* Find the first non-excluded output section. We'll use its |
7058 | section symbol for some emitted relocs. */ | |
7059 | void | |
7060 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
7061 | { | |
7062 | asection *s; | |
f26a3287 | 7063 | asection *found = NULL; |
74541ad4 AM |
7064 | |
7065 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
7066 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
d00dd7dc | 7067 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7068 | { |
f26a3287 AM |
7069 | found = s; |
7070 | if ((s->flags & SEC_THREAD_LOCAL) == 0) | |
7071 | break; | |
74541ad4 | 7072 | } |
f26a3287 | 7073 | elf_hash_table (info)->text_index_section = found; |
74541ad4 AM |
7074 | } |
7075 | ||
7076 | /* Find two non-excluded output sections, one for code, one for data. | |
7077 | We'll use their section symbols for some emitted relocs. */ | |
7078 | void | |
7079 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
7080 | { | |
7081 | asection *s; | |
f26a3287 | 7082 | asection *found = NULL; |
74541ad4 | 7083 | |
266b05cf | 7084 | /* Data first, since setting text_index_section changes |
7f923b7f | 7085 | _bfd_elf_omit_section_dynsym_default. */ |
74541ad4 | 7086 | for (s = output_bfd->sections; s != NULL; s = s->next) |
f26a3287 AM |
7087 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC |
7088 | && !(s->flags & SEC_READONLY) | |
d00dd7dc | 7089 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7090 | { |
f26a3287 AM |
7091 | found = s; |
7092 | if ((s->flags & SEC_THREAD_LOCAL) == 0) | |
7093 | break; | |
74541ad4 | 7094 | } |
f26a3287 | 7095 | elf_hash_table (info)->data_index_section = found; |
74541ad4 AM |
7096 | |
7097 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
f26a3287 AM |
7098 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC |
7099 | && (s->flags & SEC_READONLY) | |
d00dd7dc | 7100 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7101 | { |
f26a3287 | 7102 | found = s; |
74541ad4 AM |
7103 | break; |
7104 | } | |
f26a3287 | 7105 | elf_hash_table (info)->text_index_section = found; |
74541ad4 AM |
7106 | } |
7107 | ||
8423293d AM |
7108 | bfd_boolean |
7109 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
7110 | { | |
74541ad4 | 7111 | const struct elf_backend_data *bed; |
23ec1e32 | 7112 | unsigned long section_sym_count; |
96d01d93 | 7113 | bfd_size_type dynsymcount = 0; |
74541ad4 | 7114 | |
8423293d AM |
7115 | if (!is_elf_hash_table (info->hash)) |
7116 | return TRUE; | |
7117 | ||
74541ad4 AM |
7118 | bed = get_elf_backend_data (output_bfd); |
7119 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
7120 | ||
23ec1e32 MR |
7121 | /* Assign dynsym indices. In a shared library we generate a section |
7122 | symbol for each output section, which come first. Next come all | |
7123 | of the back-end allocated local dynamic syms, followed by the rest | |
7124 | of the global symbols. | |
7125 | ||
7126 | This is usually not needed for static binaries, however backends | |
7127 | can request to always do it, e.g. the MIPS backend uses dynamic | |
7128 | symbol counts to lay out GOT, which will be produced in the | |
7129 | presence of GOT relocations even in static binaries (holding fixed | |
7130 | data in that case, to satisfy those relocations). */ | |
7131 | ||
7132 | if (elf_hash_table (info)->dynamic_sections_created | |
7133 | || bed->always_renumber_dynsyms) | |
7134 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
7135 | §ion_sym_count); | |
7136 | ||
8423293d AM |
7137 | if (elf_hash_table (info)->dynamic_sections_created) |
7138 | { | |
7139 | bfd *dynobj; | |
8423293d | 7140 | asection *s; |
8423293d AM |
7141 | unsigned int dtagcount; |
7142 | ||
7143 | dynobj = elf_hash_table (info)->dynobj; | |
7144 | ||
5a580b3a | 7145 | /* Work out the size of the symbol version section. */ |
3d4d4302 | 7146 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 7147 | BFD_ASSERT (s != NULL); |
d5486c43 | 7148 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 7149 | { |
eea6121a | 7150 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 7151 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a AM |
7152 | if (s->contents == NULL) |
7153 | return FALSE; | |
7154 | ||
7155 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
7156 | return FALSE; | |
7157 | } | |
7158 | ||
7159 | /* Set the size of the .dynsym and .hash sections. We counted | |
7160 | the number of dynamic symbols in elf_link_add_object_symbols. | |
7161 | We will build the contents of .dynsym and .hash when we build | |
7162 | the final symbol table, because until then we do not know the | |
7163 | correct value to give the symbols. We built the .dynstr | |
7164 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 7165 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 7166 | BFD_ASSERT (s != NULL); |
eea6121a | 7167 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 7168 | |
d5486c43 L |
7169 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
7170 | if (s->contents == NULL) | |
7171 | return FALSE; | |
5a580b3a | 7172 | |
d5486c43 L |
7173 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
7174 | section syms, in case we don't output them all. */ | |
7175 | ++section_sym_count; | |
7176 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 7177 | |
fdc90cb4 JJ |
7178 | elf_hash_table (info)->bucketcount = 0; |
7179 | ||
5a580b3a AM |
7180 | /* Compute the size of the hashing table. As a side effect this |
7181 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
7182 | if (info->emit_hash) |
7183 | { | |
7184 | unsigned long int *hashcodes; | |
14b1c01e | 7185 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
7186 | bfd_size_type amt; |
7187 | unsigned long int nsyms; | |
7188 | size_t bucketcount; | |
7189 | size_t hash_entry_size; | |
7190 | ||
7191 | /* Compute the hash values for all exported symbols. At the same | |
7192 | time store the values in an array so that we could use them for | |
7193 | optimizations. */ | |
7194 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 7195 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7196 | if (hashcodes == NULL) |
7197 | return FALSE; | |
14b1c01e AM |
7198 | hashinf.hashcodes = hashcodes; |
7199 | hashinf.error = FALSE; | |
5a580b3a | 7200 | |
fdc90cb4 JJ |
7201 | /* Put all hash values in HASHCODES. */ |
7202 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
7203 | elf_collect_hash_codes, &hashinf); |
7204 | if (hashinf.error) | |
4dd07732 AM |
7205 | { |
7206 | free (hashcodes); | |
7207 | return FALSE; | |
7208 | } | |
5a580b3a | 7209 | |
14b1c01e | 7210 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
7211 | bucketcount |
7212 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
7213 | free (hashcodes); | |
7214 | ||
4b48e2f6 | 7215 | if (bucketcount == 0 && nsyms > 0) |
fdc90cb4 | 7216 | return FALSE; |
5a580b3a | 7217 | |
fdc90cb4 JJ |
7218 | elf_hash_table (info)->bucketcount = bucketcount; |
7219 | ||
3d4d4302 | 7220 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
7221 | BFD_ASSERT (s != NULL); |
7222 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
7223 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 7224 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7225 | if (s->contents == NULL) |
7226 | return FALSE; | |
7227 | ||
7228 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
7229 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
7230 | s->contents + hash_entry_size); | |
7231 | } | |
7232 | ||
7233 | if (info->emit_gnu_hash) | |
7234 | { | |
7235 | size_t i, cnt; | |
7236 | unsigned char *contents; | |
7237 | struct collect_gnu_hash_codes cinfo; | |
7238 | bfd_size_type amt; | |
7239 | size_t bucketcount; | |
7240 | ||
7241 | memset (&cinfo, 0, sizeof (cinfo)); | |
7242 | ||
7243 | /* Compute the hash values for all exported symbols. At the same | |
7244 | time store the values in an array so that we could use them for | |
7245 | optimizations. */ | |
7246 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 7247 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 JJ |
7248 | if (cinfo.hashcodes == NULL) |
7249 | return FALSE; | |
7250 | ||
7251 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
7252 | cinfo.min_dynindx = -1; | |
7253 | cinfo.output_bfd = output_bfd; | |
7254 | cinfo.bed = bed; | |
7255 | ||
7256 | /* Put all hash values in HASHCODES. */ | |
7257 | elf_link_hash_traverse (elf_hash_table (info), | |
7258 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 7259 | if (cinfo.error) |
4dd07732 AM |
7260 | { |
7261 | free (cinfo.hashcodes); | |
7262 | return FALSE; | |
7263 | } | |
fdc90cb4 JJ |
7264 | |
7265 | bucketcount | |
7266 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
7267 | ||
7268 | if (bucketcount == 0) | |
7269 | { | |
7270 | free (cinfo.hashcodes); | |
7271 | return FALSE; | |
7272 | } | |
7273 | ||
3d4d4302 | 7274 | s = bfd_get_linker_section (dynobj, ".gnu.hash"); |
fdc90cb4 JJ |
7275 | BFD_ASSERT (s != NULL); |
7276 | ||
7277 | if (cinfo.nsyms == 0) | |
7278 | { | |
7279 | /* Empty .gnu.hash section is special. */ | |
7280 | BFD_ASSERT (cinfo.min_dynindx == -1); | |
7281 | free (cinfo.hashcodes); | |
7282 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 7283 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7284 | if (contents == NULL) |
7285 | return FALSE; | |
7286 | s->contents = contents; | |
7287 | /* 1 empty bucket. */ | |
7288 | bfd_put_32 (output_bfd, 1, contents); | |
7289 | /* SYMIDX above the special symbol 0. */ | |
7290 | bfd_put_32 (output_bfd, 1, contents + 4); | |
7291 | /* Just one word for bitmask. */ | |
7292 | bfd_put_32 (output_bfd, 1, contents + 8); | |
7293 | /* Only hash fn bloom filter. */ | |
7294 | bfd_put_32 (output_bfd, 0, contents + 12); | |
7295 | /* No hashes are valid - empty bitmask. */ | |
7296 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
7297 | /* No hashes in the only bucket. */ | |
7298 | bfd_put_32 (output_bfd, 0, | |
7299 | contents + 16 + bed->s->arch_size / 8); | |
7300 | } | |
7301 | else | |
7302 | { | |
9e6619e2 | 7303 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 7304 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 7305 | |
9e6619e2 AM |
7306 | x = cinfo.nsyms; |
7307 | maskbitslog2 = 1; | |
7308 | while ((x >>= 1) != 0) | |
7309 | ++maskbitslog2; | |
fdc90cb4 JJ |
7310 | if (maskbitslog2 < 3) |
7311 | maskbitslog2 = 5; | |
7312 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
7313 | maskbitslog2 = maskbitslog2 + 3; | |
7314 | else | |
7315 | maskbitslog2 = maskbitslog2 + 2; | |
7316 | if (bed->s->arch_size == 64) | |
7317 | { | |
7318 | if (maskbitslog2 == 5) | |
7319 | maskbitslog2 = 6; | |
7320 | cinfo.shift1 = 6; | |
7321 | } | |
7322 | else | |
7323 | cinfo.shift1 = 5; | |
7324 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 7325 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
7326 | cinfo.maskbits = 1 << maskbitslog2; |
7327 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
7328 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
7329 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 7330 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
7331 | if (cinfo.bitmask == NULL) |
7332 | { | |
7333 | free (cinfo.hashcodes); | |
7334 | return FALSE; | |
7335 | } | |
7336 | ||
a50b1753 | 7337 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
7338 | cinfo.indx = cinfo.counts + bucketcount; |
7339 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
7340 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
7341 | ||
7342 | /* Determine how often each hash bucket is used. */ | |
7343 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
7344 | for (i = 0; i < cinfo.nsyms; ++i) | |
7345 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
7346 | ||
7347 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
7348 | if (cinfo.counts[i] != 0) | |
7349 | { | |
7350 | cinfo.indx[i] = cnt; | |
7351 | cnt += cinfo.counts[i]; | |
7352 | } | |
7353 | BFD_ASSERT (cnt == dynsymcount); | |
7354 | cinfo.bucketcount = bucketcount; | |
7355 | cinfo.local_indx = cinfo.min_dynindx; | |
7356 | ||
7357 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
7358 | s->size += cinfo.maskbits / 8; | |
a50b1753 | 7359 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7360 | if (contents == NULL) |
7361 | { | |
7362 | free (cinfo.bitmask); | |
7363 | free (cinfo.hashcodes); | |
7364 | return FALSE; | |
7365 | } | |
7366 | ||
7367 | s->contents = contents; | |
7368 | bfd_put_32 (output_bfd, bucketcount, contents); | |
7369 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
7370 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
7371 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
7372 | contents += 16 + cinfo.maskbits / 8; | |
7373 | ||
7374 | for (i = 0; i < bucketcount; ++i) | |
7375 | { | |
7376 | if (cinfo.counts[i] == 0) | |
7377 | bfd_put_32 (output_bfd, 0, contents); | |
7378 | else | |
7379 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
7380 | contents += 4; | |
7381 | } | |
7382 | ||
7383 | cinfo.contents = contents; | |
7384 | ||
7385 | /* Renumber dynamic symbols, populate .gnu.hash section. */ | |
7386 | elf_link_hash_traverse (elf_hash_table (info), | |
7387 | elf_renumber_gnu_hash_syms, &cinfo); | |
7388 | ||
7389 | contents = s->contents + 16; | |
7390 | for (i = 0; i < maskwords; ++i) | |
7391 | { | |
7392 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
7393 | contents); | |
7394 | contents += bed->s->arch_size / 8; | |
7395 | } | |
7396 | ||
7397 | free (cinfo.bitmask); | |
7398 | free (cinfo.hashcodes); | |
7399 | } | |
7400 | } | |
5a580b3a | 7401 | |
3d4d4302 | 7402 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
7403 | BFD_ASSERT (s != NULL); |
7404 | ||
4ad4eba5 | 7405 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 7406 | |
eea6121a | 7407 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
7408 | |
7409 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
7410 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
7411 | return FALSE; | |
7412 | } | |
7413 | ||
7414 | return TRUE; | |
7415 | } | |
4d269e42 | 7416 | \f |
4d269e42 AM |
7417 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
7418 | ||
7419 | static void | |
7420 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
7421 | asection *sec) | |
7422 | { | |
dbaa2011 AM |
7423 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
7424 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
7425 | } |
7426 | ||
7427 | /* Finish SHF_MERGE section merging. */ | |
7428 | ||
7429 | bfd_boolean | |
630993ec | 7430 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
7431 | { |
7432 | bfd *ibfd; | |
7433 | asection *sec; | |
7434 | ||
7435 | if (!is_elf_hash_table (info->hash)) | |
7436 | return FALSE; | |
7437 | ||
c72f2fb2 | 7438 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
7439 | if ((ibfd->flags & DYNAMIC) == 0 |
7440 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
7441 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
7442 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
7443 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
7444 | if ((sec->flags & SEC_MERGE) != 0 | |
7445 | && !bfd_is_abs_section (sec->output_section)) | |
7446 | { | |
7447 | struct bfd_elf_section_data *secdata; | |
7448 | ||
7449 | secdata = elf_section_data (sec); | |
630993ec | 7450 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
7451 | &elf_hash_table (info)->merge_info, |
7452 | sec, &secdata->sec_info)) | |
7453 | return FALSE; | |
7454 | else if (secdata->sec_info) | |
dbaa2011 | 7455 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7456 | } |
7457 | ||
7458 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7459 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 AM |
7460 | merge_sections_remove_hook); |
7461 | return TRUE; | |
7462 | } | |
7463 | ||
7464 | /* Create an entry in an ELF linker hash table. */ | |
7465 | ||
7466 | struct bfd_hash_entry * | |
7467 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7468 | struct bfd_hash_table *table, | |
7469 | const char *string) | |
7470 | { | |
7471 | /* Allocate the structure if it has not already been allocated by a | |
7472 | subclass. */ | |
7473 | if (entry == NULL) | |
7474 | { | |
a50b1753 | 7475 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7476 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7477 | if (entry == NULL) |
7478 | return entry; | |
7479 | } | |
7480 | ||
7481 | /* Call the allocation method of the superclass. */ | |
7482 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7483 | if (entry != NULL) | |
7484 | { | |
7485 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7486 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7487 | ||
7488 | /* Set local fields. */ | |
7489 | ret->indx = -1; | |
7490 | ret->dynindx = -1; | |
7491 | ret->got = htab->init_got_refcount; | |
7492 | ret->plt = htab->init_plt_refcount; | |
7493 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7494 | - offsetof (struct elf_link_hash_entry, size))); | |
7495 | /* Assume that we have been called by a non-ELF symbol reader. | |
7496 | This flag is then reset by the code which reads an ELF input | |
7497 | file. This ensures that a symbol created by a non-ELF symbol | |
7498 | reader will have the flag set correctly. */ | |
7499 | ret->non_elf = 1; | |
7500 | } | |
7501 | ||
7502 | return entry; | |
7503 | } | |
7504 | ||
7505 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7506 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7507 | ||
7508 | void | |
7509 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7510 | struct elf_link_hash_entry *dir, | |
7511 | struct elf_link_hash_entry *ind) | |
7512 | { | |
7513 | struct elf_link_hash_table *htab; | |
7514 | ||
7515 | /* Copy down any references that we may have already seen to the | |
e81830c5 | 7516 | symbol which just became indirect. */ |
4d269e42 | 7517 | |
422f1182 | 7518 | if (dir->versioned != versioned_hidden) |
e81830c5 AM |
7519 | dir->ref_dynamic |= ind->ref_dynamic; |
7520 | dir->ref_regular |= ind->ref_regular; | |
7521 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
7522 | dir->non_got_ref |= ind->non_got_ref; | |
7523 | dir->needs_plt |= ind->needs_plt; | |
7524 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
4d269e42 AM |
7525 | |
7526 | if (ind->root.type != bfd_link_hash_indirect) | |
7527 | return; | |
7528 | ||
7529 | /* Copy over the global and procedure linkage table refcount entries. | |
7530 | These may have been already set up by a check_relocs routine. */ | |
7531 | htab = elf_hash_table (info); | |
7532 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
7533 | { | |
7534 | if (dir->got.refcount < 0) | |
7535 | dir->got.refcount = 0; | |
7536 | dir->got.refcount += ind->got.refcount; | |
7537 | ind->got.refcount = htab->init_got_refcount.refcount; | |
7538 | } | |
7539 | ||
7540 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
7541 | { | |
7542 | if (dir->plt.refcount < 0) | |
7543 | dir->plt.refcount = 0; | |
7544 | dir->plt.refcount += ind->plt.refcount; | |
7545 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
7546 | } | |
7547 | ||
7548 | if (ind->dynindx != -1) | |
7549 | { | |
7550 | if (dir->dynindx != -1) | |
7551 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
7552 | dir->dynindx = ind->dynindx; | |
7553 | dir->dynstr_index = ind->dynstr_index; | |
7554 | ind->dynindx = -1; | |
7555 | ind->dynstr_index = 0; | |
7556 | } | |
7557 | } | |
7558 | ||
7559 | void | |
7560 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
7561 | struct elf_link_hash_entry *h, | |
7562 | bfd_boolean force_local) | |
7563 | { | |
3aa14d16 L |
7564 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
7565 | if (h->type != STT_GNU_IFUNC) | |
7566 | { | |
7567 | h->plt = elf_hash_table (info)->init_plt_offset; | |
7568 | h->needs_plt = 0; | |
7569 | } | |
4d269e42 AM |
7570 | if (force_local) |
7571 | { | |
7572 | h->forced_local = 1; | |
7573 | if (h->dynindx != -1) | |
7574 | { | |
4d269e42 AM |
7575 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, |
7576 | h->dynstr_index); | |
641338d8 AM |
7577 | h->dynindx = -1; |
7578 | h->dynstr_index = 0; | |
4d269e42 AM |
7579 | } |
7580 | } | |
7581 | } | |
7582 | ||
34a87bb0 L |
7583 | /* Hide a symbol. */ |
7584 | ||
7585 | void | |
7586 | _bfd_elf_link_hide_symbol (bfd *output_bfd, | |
7587 | struct bfd_link_info *info, | |
7588 | struct bfd_link_hash_entry *h) | |
7589 | { | |
7590 | if (is_elf_hash_table (info->hash)) | |
7591 | { | |
7592 | const struct elf_backend_data *bed | |
7593 | = get_elf_backend_data (output_bfd); | |
7594 | struct elf_link_hash_entry *eh | |
7595 | = (struct elf_link_hash_entry *) h; | |
7596 | bed->elf_backend_hide_symbol (info, eh, TRUE); | |
7597 | eh->def_dynamic = 0; | |
7598 | eh->ref_dynamic = 0; | |
7599 | eh->dynamic_def = 0; | |
7600 | } | |
7601 | } | |
7602 | ||
7bf52ea2 AM |
7603 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
7604 | caller. */ | |
4d269e42 AM |
7605 | |
7606 | bfd_boolean | |
7607 | _bfd_elf_link_hash_table_init | |
7608 | (struct elf_link_hash_table *table, | |
7609 | bfd *abfd, | |
7610 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
7611 | struct bfd_hash_table *, | |
7612 | const char *), | |
4dfe6ac6 NC |
7613 | unsigned int entsize, |
7614 | enum elf_target_id target_id) | |
4d269e42 AM |
7615 | { |
7616 | bfd_boolean ret; | |
7617 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; | |
7618 | ||
4d269e42 AM |
7619 | table->init_got_refcount.refcount = can_refcount - 1; |
7620 | table->init_plt_refcount.refcount = can_refcount - 1; | |
7621 | table->init_got_offset.offset = -(bfd_vma) 1; | |
7622 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
7623 | /* The first dynamic symbol is a dummy. */ | |
7624 | table->dynsymcount = 1; | |
7625 | ||
7626 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 7627 | |
4d269e42 | 7628 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 7629 | table->hash_table_id = target_id; |
4d269e42 AM |
7630 | |
7631 | return ret; | |
7632 | } | |
7633 | ||
7634 | /* Create an ELF linker hash table. */ | |
7635 | ||
7636 | struct bfd_link_hash_table * | |
7637 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
7638 | { | |
7639 | struct elf_link_hash_table *ret; | |
7640 | bfd_size_type amt = sizeof (struct elf_link_hash_table); | |
7641 | ||
7bf52ea2 | 7642 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
7643 | if (ret == NULL) |
7644 | return NULL; | |
7645 | ||
7646 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
7647 | sizeof (struct elf_link_hash_entry), |
7648 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
7649 | { |
7650 | free (ret); | |
7651 | return NULL; | |
7652 | } | |
d495ab0d | 7653 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
7654 | |
7655 | return &ret->root; | |
7656 | } | |
7657 | ||
9f7c3e5e AM |
7658 | /* Destroy an ELF linker hash table. */ |
7659 | ||
7660 | void | |
d495ab0d | 7661 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 7662 | { |
d495ab0d AM |
7663 | struct elf_link_hash_table *htab; |
7664 | ||
7665 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
7666 | if (htab->dynstr != NULL) |
7667 | _bfd_elf_strtab_free (htab->dynstr); | |
7668 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 7669 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
7670 | } |
7671 | ||
4d269e42 AM |
7672 | /* This is a hook for the ELF emulation code in the generic linker to |
7673 | tell the backend linker what file name to use for the DT_NEEDED | |
7674 | entry for a dynamic object. */ | |
7675 | ||
7676 | void | |
7677 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
7678 | { | |
7679 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7680 | && bfd_get_format (abfd) == bfd_object) | |
7681 | elf_dt_name (abfd) = name; | |
7682 | } | |
7683 | ||
7684 | int | |
7685 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
7686 | { | |
7687 | int lib_class; | |
7688 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7689 | && bfd_get_format (abfd) == bfd_object) | |
7690 | lib_class = elf_dyn_lib_class (abfd); | |
7691 | else | |
7692 | lib_class = 0; | |
7693 | return lib_class; | |
7694 | } | |
7695 | ||
7696 | void | |
7697 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
7698 | { | |
7699 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7700 | && bfd_get_format (abfd) == bfd_object) | |
7701 | elf_dyn_lib_class (abfd) = lib_class; | |
7702 | } | |
7703 | ||
7704 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
7705 | the linker ELF emulation code. */ | |
7706 | ||
7707 | struct bfd_link_needed_list * | |
7708 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7709 | struct bfd_link_info *info) | |
7710 | { | |
7711 | if (! is_elf_hash_table (info->hash)) | |
7712 | return NULL; | |
7713 | return elf_hash_table (info)->needed; | |
7714 | } | |
7715 | ||
7716 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
7717 | hook for the linker ELF emulation code. */ | |
7718 | ||
7719 | struct bfd_link_needed_list * | |
7720 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
7721 | struct bfd_link_info *info) | |
7722 | { | |
7723 | if (! is_elf_hash_table (info->hash)) | |
7724 | return NULL; | |
7725 | return elf_hash_table (info)->runpath; | |
7726 | } | |
7727 | ||
7728 | /* Get the name actually used for a dynamic object for a link. This | |
7729 | is the SONAME entry if there is one. Otherwise, it is the string | |
7730 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
7731 | ||
7732 | const char * | |
7733 | bfd_elf_get_dt_soname (bfd *abfd) | |
7734 | { | |
7735 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
7736 | && bfd_get_format (abfd) == bfd_object) | |
7737 | return elf_dt_name (abfd); | |
7738 | return NULL; | |
7739 | } | |
7740 | ||
7741 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
7742 | the ELF linker emulation code. */ | |
7743 | ||
7744 | bfd_boolean | |
7745 | bfd_elf_get_bfd_needed_list (bfd *abfd, | |
7746 | struct bfd_link_needed_list **pneeded) | |
7747 | { | |
7748 | asection *s; | |
7749 | bfd_byte *dynbuf = NULL; | |
cb33740c | 7750 | unsigned int elfsec; |
4d269e42 AM |
7751 | unsigned long shlink; |
7752 | bfd_byte *extdyn, *extdynend; | |
7753 | size_t extdynsize; | |
7754 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
7755 | ||
7756 | *pneeded = NULL; | |
7757 | ||
7758 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
7759 | || bfd_get_format (abfd) != bfd_object) | |
7760 | return TRUE; | |
7761 | ||
7762 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
7763 | if (s == NULL || s->size == 0) | |
7764 | return TRUE; | |
7765 | ||
7766 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
7767 | goto error_return; | |
7768 | ||
7769 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 7770 | if (elfsec == SHN_BAD) |
4d269e42 AM |
7771 | goto error_return; |
7772 | ||
7773 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 7774 | |
4d269e42 AM |
7775 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
7776 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
7777 | ||
7778 | extdyn = dynbuf; | |
7779 | extdynend = extdyn + s->size; | |
7780 | for (; extdyn < extdynend; extdyn += extdynsize) | |
7781 | { | |
7782 | Elf_Internal_Dyn dyn; | |
7783 | ||
7784 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
7785 | ||
7786 | if (dyn.d_tag == DT_NULL) | |
7787 | break; | |
7788 | ||
7789 | if (dyn.d_tag == DT_NEEDED) | |
7790 | { | |
7791 | const char *string; | |
7792 | struct bfd_link_needed_list *l; | |
7793 | unsigned int tagv = dyn.d_un.d_val; | |
7794 | bfd_size_type amt; | |
7795 | ||
7796 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
7797 | if (string == NULL) | |
7798 | goto error_return; | |
7799 | ||
7800 | amt = sizeof *l; | |
a50b1753 | 7801 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
7802 | if (l == NULL) |
7803 | goto error_return; | |
7804 | ||
7805 | l->by = abfd; | |
7806 | l->name = string; | |
7807 | l->next = *pneeded; | |
7808 | *pneeded = l; | |
7809 | } | |
7810 | } | |
7811 | ||
7812 | free (dynbuf); | |
7813 | ||
7814 | return TRUE; | |
7815 | ||
7816 | error_return: | |
7817 | if (dynbuf != NULL) | |
7818 | free (dynbuf); | |
7819 | return FALSE; | |
7820 | } | |
7821 | ||
7822 | struct elf_symbuf_symbol | |
7823 | { | |
7824 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
7825 | unsigned char st_info; /* Type and binding attributes */ | |
7826 | unsigned char st_other; /* Visibilty, and target specific */ | |
7827 | }; | |
7828 | ||
7829 | struct elf_symbuf_head | |
7830 | { | |
7831 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 7832 | size_t count; |
4d269e42 AM |
7833 | unsigned int st_shndx; |
7834 | }; | |
7835 | ||
7836 | struct elf_symbol | |
7837 | { | |
7838 | union | |
7839 | { | |
7840 | Elf_Internal_Sym *isym; | |
7841 | struct elf_symbuf_symbol *ssym; | |
7842 | } u; | |
7843 | const char *name; | |
7844 | }; | |
7845 | ||
7846 | /* Sort references to symbols by ascending section number. */ | |
7847 | ||
7848 | static int | |
7849 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
7850 | { | |
7851 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
7852 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
7853 | ||
7854 | return s1->st_shndx - s2->st_shndx; | |
7855 | } | |
7856 | ||
7857 | static int | |
7858 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
7859 | { | |
7860 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
7861 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
7862 | return strcmp (s1->name, s2->name); | |
7863 | } | |
7864 | ||
7865 | static struct elf_symbuf_head * | |
ef53be89 | 7866 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 7867 | { |
14b1c01e | 7868 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
7869 | struct elf_symbuf_symbol *ssym; |
7870 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
ef53be89 | 7871 | size_t i, shndx_count, total_size; |
4d269e42 | 7872 | |
a50b1753 | 7873 | indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); |
4d269e42 AM |
7874 | if (indbuf == NULL) |
7875 | return NULL; | |
7876 | ||
7877 | for (ind = indbuf, i = 0; i < symcount; i++) | |
7878 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
7879 | *ind++ = &isymbuf[i]; | |
7880 | indbufend = ind; | |
7881 | ||
7882 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
7883 | elf_sort_elf_symbol); | |
7884 | ||
7885 | shndx_count = 0; | |
7886 | if (indbufend > indbuf) | |
7887 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
7888 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
7889 | shndx_count++; | |
7890 | ||
3ae181ee L |
7891 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
7892 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 7893 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
7894 | if (ssymbuf == NULL) |
7895 | { | |
7896 | free (indbuf); | |
7897 | return NULL; | |
7898 | } | |
7899 | ||
3ae181ee | 7900 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
7901 | ssymbuf->ssym = NULL; |
7902 | ssymbuf->count = shndx_count; | |
7903 | ssymbuf->st_shndx = 0; | |
7904 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
7905 | { | |
7906 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
7907 | { | |
7908 | ssymhead++; | |
7909 | ssymhead->ssym = ssym; | |
7910 | ssymhead->count = 0; | |
7911 | ssymhead->st_shndx = (*ind)->st_shndx; | |
7912 | } | |
7913 | ssym->st_name = (*ind)->st_name; | |
7914 | ssym->st_info = (*ind)->st_info; | |
7915 | ssym->st_other = (*ind)->st_other; | |
7916 | ssymhead->count++; | |
7917 | } | |
ef53be89 | 7918 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
3ae181ee L |
7919 | && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) |
7920 | == total_size)); | |
4d269e42 AM |
7921 | |
7922 | free (indbuf); | |
7923 | return ssymbuf; | |
7924 | } | |
7925 | ||
7926 | /* Check if 2 sections define the same set of local and global | |
7927 | symbols. */ | |
7928 | ||
8f317e31 | 7929 | static bfd_boolean |
4d269e42 AM |
7930 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
7931 | struct bfd_link_info *info) | |
7932 | { | |
7933 | bfd *bfd1, *bfd2; | |
7934 | const struct elf_backend_data *bed1, *bed2; | |
7935 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 7936 | size_t symcount1, symcount2; |
4d269e42 AM |
7937 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
7938 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
7939 | Elf_Internal_Sym *isym, *isymend; | |
7940 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
ef53be89 | 7941 | size_t count1, count2, i; |
cb33740c | 7942 | unsigned int shndx1, shndx2; |
4d269e42 AM |
7943 | bfd_boolean result; |
7944 | ||
7945 | bfd1 = sec1->owner; | |
7946 | bfd2 = sec2->owner; | |
7947 | ||
4d269e42 AM |
7948 | /* Both sections have to be in ELF. */ |
7949 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
7950 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
7951 | return FALSE; | |
7952 | ||
7953 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
7954 | return FALSE; | |
7955 | ||
4d269e42 AM |
7956 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
7957 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 7958 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
4d269e42 AM |
7959 | return FALSE; |
7960 | ||
7961 | bed1 = get_elf_backend_data (bfd1); | |
7962 | bed2 = get_elf_backend_data (bfd2); | |
7963 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
7964 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
7965 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
7966 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
7967 | ||
7968 | if (symcount1 == 0 || symcount2 == 0) | |
7969 | return FALSE; | |
7970 | ||
7971 | result = FALSE; | |
7972 | isymbuf1 = NULL; | |
7973 | isymbuf2 = NULL; | |
a50b1753 NC |
7974 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
7975 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 AM |
7976 | |
7977 | if (ssymbuf1 == NULL) | |
7978 | { | |
7979 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
7980 | NULL, NULL, NULL); | |
7981 | if (isymbuf1 == NULL) | |
7982 | goto done; | |
7983 | ||
7984 | if (!info->reduce_memory_overheads) | |
7985 | elf_tdata (bfd1)->symbuf = ssymbuf1 | |
7986 | = elf_create_symbuf (symcount1, isymbuf1); | |
7987 | } | |
7988 | ||
7989 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
7990 | { | |
7991 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
7992 | NULL, NULL, NULL); | |
7993 | if (isymbuf2 == NULL) | |
7994 | goto done; | |
7995 | ||
7996 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
7997 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
7998 | = elf_create_symbuf (symcount2, isymbuf2); | |
7999 | } | |
8000 | ||
8001 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
8002 | { | |
8003 | /* Optimized faster version. */ | |
ef53be89 | 8004 | size_t lo, hi, mid; |
4d269e42 AM |
8005 | struct elf_symbol *symp; |
8006 | struct elf_symbuf_symbol *ssym, *ssymend; | |
8007 | ||
8008 | lo = 0; | |
8009 | hi = ssymbuf1->count; | |
8010 | ssymbuf1++; | |
8011 | count1 = 0; | |
8012 | while (lo < hi) | |
8013 | { | |
8014 | mid = (lo + hi) / 2; | |
cb33740c | 8015 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 8016 | hi = mid; |
cb33740c | 8017 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
8018 | lo = mid + 1; |
8019 | else | |
8020 | { | |
8021 | count1 = ssymbuf1[mid].count; | |
8022 | ssymbuf1 += mid; | |
8023 | break; | |
8024 | } | |
8025 | } | |
8026 | ||
8027 | lo = 0; | |
8028 | hi = ssymbuf2->count; | |
8029 | ssymbuf2++; | |
8030 | count2 = 0; | |
8031 | while (lo < hi) | |
8032 | { | |
8033 | mid = (lo + hi) / 2; | |
cb33740c | 8034 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 8035 | hi = mid; |
cb33740c | 8036 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
8037 | lo = mid + 1; |
8038 | else | |
8039 | { | |
8040 | count2 = ssymbuf2[mid].count; | |
8041 | ssymbuf2 += mid; | |
8042 | break; | |
8043 | } | |
8044 | } | |
8045 | ||
8046 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8047 | goto done; | |
8048 | ||
ca4be51c AM |
8049 | symtable1 |
8050 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
8051 | symtable2 | |
8052 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
8053 | if (symtable1 == NULL || symtable2 == NULL) |
8054 | goto done; | |
8055 | ||
8056 | symp = symtable1; | |
8057 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
8058 | ssym < ssymend; ssym++, symp++) | |
8059 | { | |
8060 | symp->u.ssym = ssym; | |
8061 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
8062 | hdr1->sh_link, | |
8063 | ssym->st_name); | |
8064 | } | |
8065 | ||
8066 | symp = symtable2; | |
8067 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
8068 | ssym < ssymend; ssym++, symp++) | |
8069 | { | |
8070 | symp->u.ssym = ssym; | |
8071 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
8072 | hdr2->sh_link, | |
8073 | ssym->st_name); | |
8074 | } | |
8075 | ||
8076 | /* Sort symbol by name. */ | |
8077 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8078 | elf_sym_name_compare); | |
8079 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8080 | elf_sym_name_compare); | |
8081 | ||
8082 | for (i = 0; i < count1; i++) | |
8083 | /* Two symbols must have the same binding, type and name. */ | |
8084 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
8085 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
8086 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8087 | goto done; | |
8088 | ||
8089 | result = TRUE; | |
8090 | goto done; | |
8091 | } | |
8092 | ||
a50b1753 NC |
8093 | symtable1 = (struct elf_symbol *) |
8094 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
8095 | symtable2 = (struct elf_symbol *) | |
8096 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
8097 | if (symtable1 == NULL || symtable2 == NULL) |
8098 | goto done; | |
8099 | ||
8100 | /* Count definitions in the section. */ | |
8101 | count1 = 0; | |
8102 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
cb33740c | 8103 | if (isym->st_shndx == shndx1) |
4d269e42 AM |
8104 | symtable1[count1++].u.isym = isym; |
8105 | ||
8106 | count2 = 0; | |
8107 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
cb33740c | 8108 | if (isym->st_shndx == shndx2) |
4d269e42 AM |
8109 | symtable2[count2++].u.isym = isym; |
8110 | ||
8111 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8112 | goto done; | |
8113 | ||
8114 | for (i = 0; i < count1; i++) | |
8115 | symtable1[i].name | |
8116 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
8117 | symtable1[i].u.isym->st_name); | |
8118 | ||
8119 | for (i = 0; i < count2; i++) | |
8120 | symtable2[i].name | |
8121 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
8122 | symtable2[i].u.isym->st_name); | |
8123 | ||
8124 | /* Sort symbol by name. */ | |
8125 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8126 | elf_sym_name_compare); | |
8127 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8128 | elf_sym_name_compare); | |
8129 | ||
8130 | for (i = 0; i < count1; i++) | |
8131 | /* Two symbols must have the same binding, type and name. */ | |
8132 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
8133 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
8134 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8135 | goto done; | |
8136 | ||
8137 | result = TRUE; | |
8138 | ||
8139 | done: | |
8140 | if (symtable1) | |
8141 | free (symtable1); | |
8142 | if (symtable2) | |
8143 | free (symtable2); | |
8144 | if (isymbuf1) | |
8145 | free (isymbuf1); | |
8146 | if (isymbuf2) | |
8147 | free (isymbuf2); | |
8148 | ||
8149 | return result; | |
8150 | } | |
8151 | ||
8152 | /* Return TRUE if 2 section types are compatible. */ | |
8153 | ||
8154 | bfd_boolean | |
8155 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
8156 | bfd *bbfd, const asection *bsec) | |
8157 | { | |
8158 | if (asec == NULL | |
8159 | || bsec == NULL | |
8160 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
8161 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
8162 | return TRUE; | |
8163 | ||
8164 | return elf_section_type (asec) == elf_section_type (bsec); | |
8165 | } | |
8166 | \f | |
c152c796 AM |
8167 | /* Final phase of ELF linker. */ |
8168 | ||
8169 | /* A structure we use to avoid passing large numbers of arguments. */ | |
8170 | ||
8171 | struct elf_final_link_info | |
8172 | { | |
8173 | /* General link information. */ | |
8174 | struct bfd_link_info *info; | |
8175 | /* Output BFD. */ | |
8176 | bfd *output_bfd; | |
8177 | /* Symbol string table. */ | |
ef10c3ac | 8178 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
8179 | /* .hash section. */ |
8180 | asection *hash_sec; | |
8181 | /* symbol version section (.gnu.version). */ | |
8182 | asection *symver_sec; | |
8183 | /* Buffer large enough to hold contents of any section. */ | |
8184 | bfd_byte *contents; | |
8185 | /* Buffer large enough to hold external relocs of any section. */ | |
8186 | void *external_relocs; | |
8187 | /* Buffer large enough to hold internal relocs of any section. */ | |
8188 | Elf_Internal_Rela *internal_relocs; | |
8189 | /* Buffer large enough to hold external local symbols of any input | |
8190 | BFD. */ | |
8191 | bfd_byte *external_syms; | |
8192 | /* And a buffer for symbol section indices. */ | |
8193 | Elf_External_Sym_Shndx *locsym_shndx; | |
8194 | /* Buffer large enough to hold internal local symbols of any input | |
8195 | BFD. */ | |
8196 | Elf_Internal_Sym *internal_syms; | |
8197 | /* Array large enough to hold a symbol index for each local symbol | |
8198 | of any input BFD. */ | |
8199 | long *indices; | |
8200 | /* Array large enough to hold a section pointer for each local | |
8201 | symbol of any input BFD. */ | |
8202 | asection **sections; | |
ef10c3ac | 8203 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 8204 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
8205 | /* Number of STT_FILE syms seen. */ |
8206 | size_t filesym_count; | |
c152c796 AM |
8207 | }; |
8208 | ||
8209 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
8210 | ||
8211 | struct elf_outext_info | |
8212 | { | |
8213 | bfd_boolean failed; | |
8214 | bfd_boolean localsyms; | |
34a79995 | 8215 | bfd_boolean file_sym_done; |
8b127cbc | 8216 | struct elf_final_link_info *flinfo; |
c152c796 AM |
8217 | }; |
8218 | ||
d9352518 DB |
8219 | |
8220 | /* Support for evaluating a complex relocation. | |
8221 | ||
8222 | Complex relocations are generalized, self-describing relocations. The | |
8223 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 8224 | relocations themselves. |
d9352518 DB |
8225 | |
8226 | The relocations are use a reserved elf-wide relocation type code (R_RELC | |
8227 | external / BFD_RELOC_RELC internal) and an encoding of relocation field | |
8228 | information (start bit, end bit, word width, etc) into the addend. This | |
8229 | information is extracted from CGEN-generated operand tables within gas. | |
8230 | ||
8231 | Complex symbols are mangled symbols (BSF_RELC external / STT_RELC | |
8232 | internal) representing prefix-notation expressions, including but not | |
8233 | limited to those sorts of expressions normally encoded as addends in the | |
8234 | addend field. The symbol mangling format is: | |
8235 | ||
8236 | <node> := <literal> | |
07d6d2b8 AM |
8237 | | <unary-operator> ':' <node> |
8238 | | <binary-operator> ':' <node> ':' <node> | |
d9352518 DB |
8239 | ; |
8240 | ||
8241 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
07d6d2b8 | 8242 | | 'S' <digits=N> ':' <N character section name> |
d9352518 DB |
8243 | | '#' <hexdigits> |
8244 | ; | |
8245 | ||
8246 | <binary-operator> := as in C | |
8247 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
8248 | ||
8249 | static void | |
a0c8462f AM |
8250 | set_symbol_value (bfd *bfd_with_globals, |
8251 | Elf_Internal_Sym *isymbuf, | |
8252 | size_t locsymcount, | |
8253 | size_t symidx, | |
8254 | bfd_vma val) | |
d9352518 | 8255 | { |
8977835c AM |
8256 | struct elf_link_hash_entry **sym_hashes; |
8257 | struct elf_link_hash_entry *h; | |
8258 | size_t extsymoff = locsymcount; | |
d9352518 | 8259 | |
8977835c | 8260 | if (symidx < locsymcount) |
d9352518 | 8261 | { |
8977835c AM |
8262 | Elf_Internal_Sym *sym; |
8263 | ||
8264 | sym = isymbuf + symidx; | |
8265 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
8266 | { | |
8267 | /* It is a local symbol: move it to the | |
8268 | "absolute" section and give it a value. */ | |
8269 | sym->st_shndx = SHN_ABS; | |
8270 | sym->st_value = val; | |
8271 | return; | |
8272 | } | |
8273 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
8274 | extsymoff = 0; | |
d9352518 | 8275 | } |
8977835c AM |
8276 | |
8277 | /* It is a global symbol: set its link type | |
8278 | to "defined" and give it a value. */ | |
8279 | ||
8280 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
8281 | h = sym_hashes [symidx - extsymoff]; | |
8282 | while (h->root.type == bfd_link_hash_indirect | |
8283 | || h->root.type == bfd_link_hash_warning) | |
8284 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8285 | h->root.type = bfd_link_hash_defined; | |
8286 | h->root.u.def.value = val; | |
8287 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
8288 | } |
8289 | ||
a0c8462f AM |
8290 | static bfd_boolean |
8291 | resolve_symbol (const char *name, | |
8292 | bfd *input_bfd, | |
8b127cbc | 8293 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8294 | bfd_vma *result, |
8295 | Elf_Internal_Sym *isymbuf, | |
8296 | size_t locsymcount) | |
d9352518 | 8297 | { |
a0c8462f AM |
8298 | Elf_Internal_Sym *sym; |
8299 | struct bfd_link_hash_entry *global_entry; | |
8300 | const char *candidate = NULL; | |
8301 | Elf_Internal_Shdr *symtab_hdr; | |
8302 | size_t i; | |
8303 | ||
d9352518 DB |
8304 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
8305 | ||
8306 | for (i = 0; i < locsymcount; ++ i) | |
8307 | { | |
8977835c | 8308 | sym = isymbuf + i; |
d9352518 DB |
8309 | |
8310 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
8311 | continue; | |
8312 | ||
8313 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
8314 | symtab_hdr->sh_link, | |
8315 | sym->st_name); | |
8316 | #ifdef DEBUG | |
0f02bbd9 AM |
8317 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
8318 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
8319 | #endif |
8320 | if (candidate && strcmp (candidate, name) == 0) | |
8321 | { | |
8b127cbc | 8322 | asection *sec = flinfo->sections [i]; |
d9352518 | 8323 | |
0f02bbd9 AM |
8324 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
8325 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 8326 | #ifdef DEBUG |
0f02bbd9 AM |
8327 | printf ("Found symbol with value %8.8lx\n", |
8328 | (unsigned long) *result); | |
d9352518 DB |
8329 | #endif |
8330 | return TRUE; | |
8331 | } | |
8332 | } | |
8333 | ||
8334 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 8335 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
a0c8462f | 8336 | FALSE, FALSE, TRUE); |
d9352518 DB |
8337 | if (!global_entry) |
8338 | return FALSE; | |
a0c8462f | 8339 | |
d9352518 DB |
8340 | if (global_entry->type == bfd_link_hash_defined |
8341 | || global_entry->type == bfd_link_hash_defweak) | |
8342 | { | |
a0c8462f AM |
8343 | *result = (global_entry->u.def.value |
8344 | + global_entry->u.def.section->output_section->vma | |
8345 | + global_entry->u.def.section->output_offset); | |
d9352518 | 8346 | #ifdef DEBUG |
0f02bbd9 AM |
8347 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
8348 | global_entry->root.string, (unsigned long) *result); | |
d9352518 DB |
8349 | #endif |
8350 | return TRUE; | |
a0c8462f | 8351 | } |
d9352518 | 8352 | |
d9352518 DB |
8353 | return FALSE; |
8354 | } | |
8355 | ||
37b01f6a DG |
8356 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
8357 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
8358 | names like "foo.end" which is the end address of section "foo". */ | |
07d6d2b8 | 8359 | |
d9352518 | 8360 | static bfd_boolean |
a0c8462f AM |
8361 | resolve_section (const char *name, |
8362 | asection *sections, | |
37b01f6a DG |
8363 | bfd_vma *result, |
8364 | bfd * abfd) | |
d9352518 | 8365 | { |
a0c8462f AM |
8366 | asection *curr; |
8367 | unsigned int len; | |
d9352518 | 8368 | |
a0c8462f | 8369 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8370 | if (strcmp (curr->name, name) == 0) |
8371 | { | |
8372 | *result = curr->vma; | |
8373 | return TRUE; | |
8374 | } | |
8375 | ||
8376 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 8377 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 8378 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8379 | { |
8380 | len = strlen (curr->name); | |
a0c8462f | 8381 | if (len > strlen (name)) |
d9352518 DB |
8382 | continue; |
8383 | ||
8384 | if (strncmp (curr->name, name, len) == 0) | |
8385 | { | |
8386 | if (strncmp (".end", name + len, 4) == 0) | |
8387 | { | |
37b01f6a | 8388 | *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); |
d9352518 DB |
8389 | return TRUE; |
8390 | } | |
8391 | ||
8392 | /* Insert more pseudo-section names here, if you like. */ | |
8393 | } | |
8394 | } | |
a0c8462f | 8395 | |
d9352518 DB |
8396 | return FALSE; |
8397 | } | |
8398 | ||
8399 | static void | |
a0c8462f | 8400 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 8401 | { |
695344c0 | 8402 | /* xgettext:c-format */ |
a0c8462f AM |
8403 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
8404 | reftype, name); | |
d9352518 DB |
8405 | } |
8406 | ||
8407 | static bfd_boolean | |
a0c8462f AM |
8408 | eval_symbol (bfd_vma *result, |
8409 | const char **symp, | |
8410 | bfd *input_bfd, | |
8b127cbc | 8411 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8412 | bfd_vma dot, |
8413 | Elf_Internal_Sym *isymbuf, | |
8414 | size_t locsymcount, | |
8415 | int signed_p) | |
d9352518 | 8416 | { |
4b93929b NC |
8417 | size_t len; |
8418 | size_t symlen; | |
a0c8462f AM |
8419 | bfd_vma a; |
8420 | bfd_vma b; | |
4b93929b | 8421 | char symbuf[4096]; |
0f02bbd9 | 8422 | const char *sym = *symp; |
a0c8462f AM |
8423 | const char *symend; |
8424 | bfd_boolean symbol_is_section = FALSE; | |
d9352518 DB |
8425 | |
8426 | len = strlen (sym); | |
8427 | symend = sym + len; | |
8428 | ||
4b93929b | 8429 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
8430 | { |
8431 | bfd_set_error (bfd_error_invalid_operation); | |
8432 | return FALSE; | |
8433 | } | |
a0c8462f | 8434 | |
d9352518 DB |
8435 | switch (* sym) |
8436 | { | |
8437 | case '.': | |
0f02bbd9 AM |
8438 | *result = dot; |
8439 | *symp = sym + 1; | |
d9352518 DB |
8440 | return TRUE; |
8441 | ||
8442 | case '#': | |
0f02bbd9 AM |
8443 | ++sym; |
8444 | *result = strtoul (sym, (char **) symp, 16); | |
d9352518 DB |
8445 | return TRUE; |
8446 | ||
8447 | case 'S': | |
8448 | symbol_is_section = TRUE; | |
1a0670f3 | 8449 | /* Fall through. */ |
a0c8462f | 8450 | case 's': |
0f02bbd9 AM |
8451 | ++sym; |
8452 | symlen = strtol (sym, (char **) symp, 10); | |
8453 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 8454 | |
4b93929b | 8455 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
8456 | { |
8457 | bfd_set_error (bfd_error_invalid_operation); | |
8458 | return FALSE; | |
8459 | } | |
8460 | ||
8461 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 8462 | symbuf[symlen] = '\0'; |
0f02bbd9 | 8463 | *symp = sym + symlen; |
a0c8462f AM |
8464 | |
8465 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
8466 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
8467 | interpretation here; section means "try section first", not "must be a | |
8468 | section", and likewise with symbol. */ | |
8469 | ||
a0c8462f | 8470 | if (symbol_is_section) |
d9352518 | 8471 | { |
37b01f6a | 8472 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 8473 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8474 | isymbuf, locsymcount)) |
d9352518 DB |
8475 | { |
8476 | undefined_reference ("section", symbuf); | |
8477 | return FALSE; | |
8478 | } | |
a0c8462f AM |
8479 | } |
8480 | else | |
d9352518 | 8481 | { |
8b127cbc | 8482 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 8483 | isymbuf, locsymcount) |
8b127cbc | 8484 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 8485 | result, input_bfd)) |
d9352518 DB |
8486 | { |
8487 | undefined_reference ("symbol", symbuf); | |
8488 | return FALSE; | |
8489 | } | |
8490 | } | |
8491 | ||
8492 | return TRUE; | |
a0c8462f | 8493 | |
d9352518 DB |
8494 | /* All that remains are operators. */ |
8495 | ||
8496 | #define UNARY_OP(op) \ | |
8497 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8498 | { \ | |
8499 | sym += strlen (#op); \ | |
a0c8462f AM |
8500 | if (*sym == ':') \ |
8501 | ++sym; \ | |
0f02bbd9 | 8502 | *symp = sym; \ |
8b127cbc | 8503 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8504 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8505 | return FALSE; \ |
8506 | if (signed_p) \ | |
0f02bbd9 | 8507 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
8508 | else \ |
8509 | *result = op a; \ | |
d9352518 DB |
8510 | return TRUE; \ |
8511 | } | |
8512 | ||
8513 | #define BINARY_OP(op) \ | |
8514 | if (strncmp (sym, #op, strlen (#op)) == 0) \ | |
8515 | { \ | |
8516 | sym += strlen (#op); \ | |
a0c8462f AM |
8517 | if (*sym == ':') \ |
8518 | ++sym; \ | |
0f02bbd9 | 8519 | *symp = sym; \ |
8b127cbc | 8520 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8521 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f | 8522 | return FALSE; \ |
0f02bbd9 | 8523 | ++*symp; \ |
8b127cbc | 8524 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 8525 | isymbuf, locsymcount, signed_p)) \ |
a0c8462f AM |
8526 | return FALSE; \ |
8527 | if (signed_p) \ | |
0f02bbd9 | 8528 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
8529 | else \ |
8530 | *result = a op b; \ | |
d9352518 DB |
8531 | return TRUE; \ |
8532 | } | |
8533 | ||
8534 | default: | |
8535 | UNARY_OP (0-); | |
8536 | BINARY_OP (<<); | |
8537 | BINARY_OP (>>); | |
8538 | BINARY_OP (==); | |
8539 | BINARY_OP (!=); | |
8540 | BINARY_OP (<=); | |
8541 | BINARY_OP (>=); | |
8542 | BINARY_OP (&&); | |
8543 | BINARY_OP (||); | |
8544 | UNARY_OP (~); | |
8545 | UNARY_OP (!); | |
8546 | BINARY_OP (*); | |
8547 | BINARY_OP (/); | |
8548 | BINARY_OP (%); | |
8549 | BINARY_OP (^); | |
8550 | BINARY_OP (|); | |
8551 | BINARY_OP (&); | |
8552 | BINARY_OP (+); | |
8553 | BINARY_OP (-); | |
8554 | BINARY_OP (<); | |
8555 | BINARY_OP (>); | |
8556 | #undef UNARY_OP | |
8557 | #undef BINARY_OP | |
8558 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
8559 | bfd_set_error (bfd_error_invalid_operation); | |
8560 | return FALSE; | |
8561 | } | |
8562 | } | |
8563 | ||
d9352518 | 8564 | static void |
a0c8462f AM |
8565 | put_value (bfd_vma size, |
8566 | unsigned long chunksz, | |
8567 | bfd *input_bfd, | |
8568 | bfd_vma x, | |
8569 | bfd_byte *location) | |
d9352518 DB |
8570 | { |
8571 | location += (size - chunksz); | |
8572 | ||
41cd1ad1 | 8573 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
8574 | { |
8575 | switch (chunksz) | |
8576 | { | |
d9352518 DB |
8577 | case 1: |
8578 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 8579 | x >>= 8; |
d9352518 DB |
8580 | break; |
8581 | case 2: | |
8582 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 8583 | x >>= 16; |
d9352518 DB |
8584 | break; |
8585 | case 4: | |
8586 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
8587 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
8588 | x >>= 16; | |
8589 | x >>= 16; | |
d9352518 | 8590 | break; |
d9352518 | 8591 | #ifdef BFD64 |
41cd1ad1 | 8592 | case 8: |
d9352518 | 8593 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
8594 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
8595 | x >>= 32; | |
8596 | x >>= 32; | |
8597 | break; | |
d9352518 | 8598 | #endif |
41cd1ad1 NC |
8599 | default: |
8600 | abort (); | |
d9352518 DB |
8601 | break; |
8602 | } | |
8603 | } | |
8604 | } | |
8605 | ||
a0c8462f AM |
8606 | static bfd_vma |
8607 | get_value (bfd_vma size, | |
8608 | unsigned long chunksz, | |
8609 | bfd *input_bfd, | |
8610 | bfd_byte *location) | |
d9352518 | 8611 | { |
9b239e0e | 8612 | int shift; |
d9352518 DB |
8613 | bfd_vma x = 0; |
8614 | ||
9b239e0e NC |
8615 | /* Sanity checks. */ |
8616 | BFD_ASSERT (chunksz <= sizeof (x) | |
8617 | && size >= chunksz | |
8618 | && chunksz != 0 | |
8619 | && (size % chunksz) == 0 | |
8620 | && input_bfd != NULL | |
8621 | && location != NULL); | |
8622 | ||
8623 | if (chunksz == sizeof (x)) | |
8624 | { | |
8625 | BFD_ASSERT (size == chunksz); | |
8626 | ||
8627 | /* Make sure that we do not perform an undefined shift operation. | |
8628 | We know that size == chunksz so there will only be one iteration | |
8629 | of the loop below. */ | |
8630 | shift = 0; | |
8631 | } | |
8632 | else | |
8633 | shift = 8 * chunksz; | |
8634 | ||
a0c8462f | 8635 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
8636 | { |
8637 | switch (chunksz) | |
8638 | { | |
d9352518 | 8639 | case 1: |
9b239e0e | 8640 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
8641 | break; |
8642 | case 2: | |
9b239e0e | 8643 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
8644 | break; |
8645 | case 4: | |
9b239e0e | 8646 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 8647 | break; |
d9352518 | 8648 | #ifdef BFD64 |
9b239e0e NC |
8649 | case 8: |
8650 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 8651 | break; |
9b239e0e NC |
8652 | #endif |
8653 | default: | |
8654 | abort (); | |
d9352518 DB |
8655 | } |
8656 | } | |
8657 | return x; | |
8658 | } | |
8659 | ||
a0c8462f AM |
8660 | static void |
8661 | decode_complex_addend (unsigned long *start, /* in bits */ | |
8662 | unsigned long *oplen, /* in bits */ | |
8663 | unsigned long *len, /* in bits */ | |
8664 | unsigned long *wordsz, /* in bytes */ | |
8665 | unsigned long *chunksz, /* in bytes */ | |
8666 | unsigned long *lsb0_p, | |
8667 | unsigned long *signed_p, | |
8668 | unsigned long *trunc_p, | |
8669 | unsigned long encoded) | |
d9352518 | 8670 | { |
07d6d2b8 AM |
8671 | * start = encoded & 0x3F; |
8672 | * len = (encoded >> 6) & 0x3F; | |
d9352518 DB |
8673 | * oplen = (encoded >> 12) & 0x3F; |
8674 | * wordsz = (encoded >> 18) & 0xF; | |
8675 | * chunksz = (encoded >> 22) & 0xF; | |
8676 | * lsb0_p = (encoded >> 27) & 1; | |
8677 | * signed_p = (encoded >> 28) & 1; | |
8678 | * trunc_p = (encoded >> 29) & 1; | |
8679 | } | |
8680 | ||
cdfeee4f | 8681 | bfd_reloc_status_type |
0f02bbd9 | 8682 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
cdfeee4f | 8683 | asection *input_section ATTRIBUTE_UNUSED, |
0f02bbd9 AM |
8684 | bfd_byte *contents, |
8685 | Elf_Internal_Rela *rel, | |
8686 | bfd_vma relocation) | |
d9352518 | 8687 | { |
0f02bbd9 AM |
8688 | bfd_vma shift, x, mask; |
8689 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 8690 | bfd_reloc_status_type r; |
d9352518 DB |
8691 | |
8692 | /* Perform this reloc, since it is complex. | |
8693 | (this is not to say that it necessarily refers to a complex | |
8694 | symbol; merely that it is a self-describing CGEN based reloc. | |
8695 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 8696 | word size, etc) encoded within it.). */ |
d9352518 | 8697 | |
a0c8462f AM |
8698 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
8699 | &chunksz, &lsb0_p, &signed_p, | |
8700 | &trunc_p, rel->r_addend); | |
d9352518 DB |
8701 | |
8702 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
8703 | ||
8704 | if (lsb0_p) | |
8705 | shift = (start + 1) - len; | |
8706 | else | |
8707 | shift = (8 * wordsz) - (start + len); | |
8708 | ||
37b01f6a DG |
8709 | x = get_value (wordsz, chunksz, input_bfd, |
8710 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
d9352518 DB |
8711 | |
8712 | #ifdef DEBUG | |
8713 | printf ("Doing complex reloc: " | |
8714 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
8715 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
8716 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
8717 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
8718 | oplen, (unsigned long) x, (unsigned long) mask, |
8719 | (unsigned long) relocation); | |
d9352518 DB |
8720 | #endif |
8721 | ||
cdfeee4f | 8722 | r = bfd_reloc_ok; |
d9352518 | 8723 | if (! trunc_p) |
cdfeee4f AM |
8724 | /* Now do an overflow check. */ |
8725 | r = bfd_check_overflow ((signed_p | |
8726 | ? complain_overflow_signed | |
8727 | : complain_overflow_unsigned), | |
8728 | len, 0, (8 * wordsz), | |
8729 | relocation); | |
a0c8462f | 8730 | |
d9352518 DB |
8731 | /* Do the deed. */ |
8732 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
8733 | ||
8734 | #ifdef DEBUG | |
8735 | printf (" relocation: %8.8lx\n" | |
8736 | " shifted mask: %8.8lx\n" | |
8737 | " shifted/masked reloc: %8.8lx\n" | |
8738 | " result: %8.8lx\n", | |
9ccb8af9 AM |
8739 | (unsigned long) relocation, (unsigned long) (mask << shift), |
8740 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 8741 | #endif |
37b01f6a DG |
8742 | put_value (wordsz, chunksz, input_bfd, x, |
8743 | contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); | |
cdfeee4f | 8744 | return r; |
d9352518 DB |
8745 | } |
8746 | ||
0e287786 AM |
8747 | /* Functions to read r_offset from external (target order) reloc |
8748 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
8749 | know the value is aligned. */ | |
53df40a4 | 8750 | |
0e287786 AM |
8751 | static bfd_vma |
8752 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
8753 | { |
8754 | union aligned32 | |
8755 | { | |
8756 | uint32_t v; | |
8757 | unsigned char c[4]; | |
8758 | }; | |
8759 | const union aligned32 *a | |
0e287786 | 8760 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8761 | |
8762 | uint32_t aval = ( (uint32_t) a->c[0] | |
8763 | | (uint32_t) a->c[1] << 8 | |
8764 | | (uint32_t) a->c[2] << 16 | |
8765 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 8766 | return aval; |
53df40a4 AM |
8767 | } |
8768 | ||
0e287786 AM |
8769 | static bfd_vma |
8770 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
8771 | { |
8772 | union aligned32 | |
8773 | { | |
8774 | uint32_t v; | |
8775 | unsigned char c[4]; | |
8776 | }; | |
8777 | const union aligned32 *a | |
0e287786 | 8778 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
8779 | |
8780 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
8781 | | (uint32_t) a->c[1] << 16 | |
8782 | | (uint32_t) a->c[2] << 8 | |
8783 | | (uint32_t) a->c[3]); | |
0e287786 | 8784 | return aval; |
53df40a4 AM |
8785 | } |
8786 | ||
8787 | #ifdef BFD_HOST_64_BIT | |
0e287786 AM |
8788 | static bfd_vma |
8789 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
8790 | { |
8791 | union aligned64 | |
8792 | { | |
8793 | uint64_t v; | |
8794 | unsigned char c[8]; | |
8795 | }; | |
8796 | const union aligned64 *a | |
0e287786 | 8797 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8798 | |
8799 | uint64_t aval = ( (uint64_t) a->c[0] | |
8800 | | (uint64_t) a->c[1] << 8 | |
8801 | | (uint64_t) a->c[2] << 16 | |
8802 | | (uint64_t) a->c[3] << 24 | |
8803 | | (uint64_t) a->c[4] << 32 | |
8804 | | (uint64_t) a->c[5] << 40 | |
8805 | | (uint64_t) a->c[6] << 48 | |
8806 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 8807 | return aval; |
53df40a4 AM |
8808 | } |
8809 | ||
0e287786 AM |
8810 | static bfd_vma |
8811 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
8812 | { |
8813 | union aligned64 | |
8814 | { | |
8815 | uint64_t v; | |
8816 | unsigned char c[8]; | |
8817 | }; | |
8818 | const union aligned64 *a | |
0e287786 | 8819 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
8820 | |
8821 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
8822 | | (uint64_t) a->c[1] << 48 | |
8823 | | (uint64_t) a->c[2] << 40 | |
8824 | | (uint64_t) a->c[3] << 32 | |
8825 | | (uint64_t) a->c[4] << 24 | |
8826 | | (uint64_t) a->c[5] << 16 | |
8827 | | (uint64_t) a->c[6] << 8 | |
8828 | | (uint64_t) a->c[7]); | |
0e287786 | 8829 | return aval; |
53df40a4 AM |
8830 | } |
8831 | #endif | |
8832 | ||
c152c796 AM |
8833 | /* When performing a relocatable link, the input relocations are |
8834 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
8835 | referenced must be updated. Update all the relocations found in |
8836 | RELDATA. */ | |
c152c796 | 8837 | |
bca6d0e3 | 8838 | static bfd_boolean |
c152c796 | 8839 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 8840 | asection *sec, |
28dbcedc | 8841 | struct bfd_elf_section_reloc_data *reldata, |
10bbbc1d NC |
8842 | bfd_boolean sort, |
8843 | struct bfd_link_info *info) | |
c152c796 AM |
8844 | { |
8845 | unsigned int i; | |
8846 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8847 | bfd_byte *erela; | |
8848 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
8849 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
8850 | bfd_vma r_type_mask; | |
8851 | int r_sym_shift; | |
d4730f92 BS |
8852 | unsigned int count = reldata->count; |
8853 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 8854 | |
d4730f92 | 8855 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
8856 | { |
8857 | swap_in = bed->s->swap_reloc_in; | |
8858 | swap_out = bed->s->swap_reloc_out; | |
8859 | } | |
d4730f92 | 8860 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
8861 | { |
8862 | swap_in = bed->s->swap_reloca_in; | |
8863 | swap_out = bed->s->swap_reloca_out; | |
8864 | } | |
8865 | else | |
8866 | abort (); | |
8867 | ||
8868 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
8869 | abort (); | |
8870 | ||
8871 | if (bed->s->arch_size == 32) | |
8872 | { | |
8873 | r_type_mask = 0xff; | |
8874 | r_sym_shift = 8; | |
8875 | } | |
8876 | else | |
8877 | { | |
8878 | r_type_mask = 0xffffffff; | |
8879 | r_sym_shift = 32; | |
8880 | } | |
8881 | ||
d4730f92 BS |
8882 | erela = reldata->hdr->contents; |
8883 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
8884 | { |
8885 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
8886 | unsigned int j; | |
8887 | ||
8888 | if (*rel_hash == NULL) | |
8889 | continue; | |
8890 | ||
10bbbc1d NC |
8891 | if ((*rel_hash)->indx == -2 |
8892 | && info->gc_sections | |
8893 | && ! info->gc_keep_exported) | |
8894 | { | |
8895 | /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ | |
9793eb77 | 8896 | _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"), |
10bbbc1d NC |
8897 | abfd, sec, |
8898 | (*rel_hash)->root.root.string); | |
9793eb77 | 8899 | _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"), |
d42c267e | 8900 | abfd, sec); |
10bbbc1d NC |
8901 | bfd_set_error (bfd_error_invalid_operation); |
8902 | return FALSE; | |
8903 | } | |
c152c796 AM |
8904 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
8905 | ||
8906 | (*swap_in) (abfd, erela, irela); | |
8907 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
8908 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
8909 | | (irela[j].r_info & r_type_mask)); | |
8910 | (*swap_out) (abfd, irela, erela); | |
8911 | } | |
53df40a4 | 8912 | |
9eaff861 AO |
8913 | if (bed->elf_backend_update_relocs) |
8914 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
8915 | ||
0e287786 | 8916 | if (sort && count != 0) |
53df40a4 | 8917 | { |
0e287786 AM |
8918 | bfd_vma (*ext_r_off) (const void *); |
8919 | bfd_vma r_off; | |
8920 | size_t elt_size; | |
8921 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 8922 | bfd_byte *buf = NULL; |
28dbcedc AM |
8923 | |
8924 | if (bed->s->arch_size == 32) | |
8925 | { | |
8926 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 8927 | ext_r_off = ext32l_r_offset; |
28dbcedc | 8928 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8929 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
8930 | else |
8931 | abort (); | |
8932 | } | |
53df40a4 | 8933 | else |
28dbcedc | 8934 | { |
53df40a4 | 8935 | #ifdef BFD_HOST_64_BIT |
28dbcedc | 8936 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 8937 | ext_r_off = ext64l_r_offset; |
28dbcedc | 8938 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 8939 | ext_r_off = ext64b_r_offset; |
28dbcedc | 8940 | else |
53df40a4 | 8941 | #endif |
28dbcedc AM |
8942 | abort (); |
8943 | } | |
0e287786 | 8944 | |
bca6d0e3 AM |
8945 | /* Must use a stable sort here. A modified insertion sort, |
8946 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
8947 | elt_size = reldata->hdr->sh_entsize; |
8948 | base = reldata->hdr->contents; | |
8949 | end = base + count * elt_size; | |
bca6d0e3 | 8950 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
8951 | abort (); |
8952 | ||
8953 | /* Ensure the first element is lowest. This acts as a sentinel, | |
8954 | speeding the main loop below. */ | |
8955 | r_off = (*ext_r_off) (base); | |
8956 | for (p = loc = base; (p += elt_size) < end; ) | |
8957 | { | |
8958 | bfd_vma r_off2 = (*ext_r_off) (p); | |
8959 | if (r_off > r_off2) | |
8960 | { | |
8961 | r_off = r_off2; | |
8962 | loc = p; | |
8963 | } | |
8964 | } | |
8965 | if (loc != base) | |
8966 | { | |
8967 | /* Don't just swap *base and *loc as that changes the order | |
8968 | of the original base[0] and base[1] if they happen to | |
8969 | have the same r_offset. */ | |
bca6d0e3 AM |
8970 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
8971 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 8972 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 8973 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
8974 | } |
8975 | ||
b29b8669 | 8976 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
8977 | { |
8978 | /* base to p is sorted, *p is next to insert. */ | |
8979 | r_off = (*ext_r_off) (p); | |
8980 | /* Search the sorted region for location to insert. */ | |
8981 | loc = p - elt_size; | |
8982 | while (r_off < (*ext_r_off) (loc)) | |
8983 | loc -= elt_size; | |
8984 | loc += elt_size; | |
8985 | if (loc != p) | |
8986 | { | |
bca6d0e3 AM |
8987 | /* Chances are there is a run of relocs to insert here, |
8988 | from one of more input files. Files are not always | |
8989 | linked in order due to the way elf_link_input_bfd is | |
8990 | called. See pr17666. */ | |
8991 | size_t sortlen = p - loc; | |
8992 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
8993 | size_t runlen = elt_size; | |
8994 | size_t buf_size = 96 * 1024; | |
8995 | while (p + runlen < end | |
8996 | && (sortlen <= buf_size | |
8997 | || runlen + elt_size <= buf_size) | |
8998 | && r_off2 > (*ext_r_off) (p + runlen)) | |
8999 | runlen += elt_size; | |
9000 | if (buf == NULL) | |
9001 | { | |
9002 | buf = bfd_malloc (buf_size); | |
9003 | if (buf == NULL) | |
9004 | return FALSE; | |
9005 | } | |
9006 | if (runlen < sortlen) | |
9007 | { | |
9008 | memcpy (buf, p, runlen); | |
9009 | memmove (loc + runlen, loc, sortlen); | |
9010 | memcpy (loc, buf, runlen); | |
9011 | } | |
9012 | else | |
9013 | { | |
9014 | memcpy (buf, loc, sortlen); | |
9015 | memmove (loc, p, runlen); | |
9016 | memcpy (loc + runlen, buf, sortlen); | |
9017 | } | |
b29b8669 | 9018 | p += runlen - elt_size; |
0e287786 AM |
9019 | } |
9020 | } | |
9021 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
9022 | free (reldata->hashes); |
9023 | reldata->hashes = NULL; | |
bca6d0e3 | 9024 | free (buf); |
53df40a4 | 9025 | } |
bca6d0e3 | 9026 | return TRUE; |
c152c796 AM |
9027 | } |
9028 | ||
9029 | struct elf_link_sort_rela | |
9030 | { | |
9031 | union { | |
9032 | bfd_vma offset; | |
9033 | bfd_vma sym_mask; | |
9034 | } u; | |
9035 | enum elf_reloc_type_class type; | |
9036 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
9037 | Elf_Internal_Rela rela[1]; | |
9038 | }; | |
9039 | ||
9040 | static int | |
9041 | elf_link_sort_cmp1 (const void *A, const void *B) | |
9042 | { | |
a50b1753 NC |
9043 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
9044 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
9045 | int relativea, relativeb; |
9046 | ||
9047 | relativea = a->type == reloc_class_relative; | |
9048 | relativeb = b->type == reloc_class_relative; | |
9049 | ||
9050 | if (relativea < relativeb) | |
9051 | return 1; | |
9052 | if (relativea > relativeb) | |
9053 | return -1; | |
9054 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
9055 | return -1; | |
9056 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
9057 | return 1; | |
9058 | if (a->rela->r_offset < b->rela->r_offset) | |
9059 | return -1; | |
9060 | if (a->rela->r_offset > b->rela->r_offset) | |
9061 | return 1; | |
9062 | return 0; | |
9063 | } | |
9064 | ||
9065 | static int | |
9066 | elf_link_sort_cmp2 (const void *A, const void *B) | |
9067 | { | |
a50b1753 NC |
9068 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
9069 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 9070 | |
7e612e98 | 9071 | if (a->type < b->type) |
c152c796 | 9072 | return -1; |
7e612e98 | 9073 | if (a->type > b->type) |
c152c796 | 9074 | return 1; |
7e612e98 | 9075 | if (a->u.offset < b->u.offset) |
c152c796 | 9076 | return -1; |
7e612e98 | 9077 | if (a->u.offset > b->u.offset) |
c152c796 AM |
9078 | return 1; |
9079 | if (a->rela->r_offset < b->rela->r_offset) | |
9080 | return -1; | |
9081 | if (a->rela->r_offset > b->rela->r_offset) | |
9082 | return 1; | |
9083 | return 0; | |
9084 | } | |
9085 | ||
9086 | static size_t | |
9087 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
9088 | { | |
3410fea8 | 9089 | asection *dynamic_relocs; |
fc66a176 L |
9090 | asection *rela_dyn; |
9091 | asection *rel_dyn; | |
c152c796 AM |
9092 | bfd_size_type count, size; |
9093 | size_t i, ret, sort_elt, ext_size; | |
9094 | bfd_byte *sort, *s_non_relative, *p; | |
9095 | struct elf_link_sort_rela *sq; | |
9096 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9097 | int i2e = bed->s->int_rels_per_ext_rel; | |
c8e44c6d | 9098 | unsigned int opb = bfd_octets_per_byte (abfd); |
c152c796 AM |
9099 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
9100 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
9101 | struct bfd_link_order *lo; | |
9102 | bfd_vma r_sym_mask; | |
3410fea8 | 9103 | bfd_boolean use_rela; |
c152c796 | 9104 | |
3410fea8 NC |
9105 | /* Find a dynamic reloc section. */ |
9106 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
9107 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
9108 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
9109 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 9110 | { |
3410fea8 NC |
9111 | bfd_boolean use_rela_initialised = FALSE; |
9112 | ||
9113 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 9114 | Its initialization checking code is not perfect. */ |
3410fea8 NC |
9115 | use_rela = TRUE; |
9116 | ||
9117 | /* Both sections are present. Examine the sizes | |
9118 | of the indirect sections to help us choose. */ | |
9119 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9120 | if (lo->type == bfd_indirect_link_order) | |
9121 | { | |
9122 | asection *o = lo->u.indirect.section; | |
9123 | ||
9124 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9125 | { | |
9126 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9127 | /* Section size is divisible by both rel and rela sizes. | |
9128 | It is of no help to us. */ | |
9129 | ; | |
9130 | else | |
9131 | { | |
9132 | /* Section size is only divisible by rela. */ | |
535b785f | 9133 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9134 | { |
9793eb77 | 9135 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9136 | "they are in more than one size"), |
9137 | abfd); | |
3410fea8 NC |
9138 | bfd_set_error (bfd_error_invalid_operation); |
9139 | return 0; | |
9140 | } | |
9141 | else | |
9142 | { | |
9143 | use_rela = TRUE; | |
9144 | use_rela_initialised = TRUE; | |
9145 | } | |
9146 | } | |
9147 | } | |
9148 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9149 | { | |
9150 | /* Section size is only divisible by rel. */ | |
535b785f | 9151 | if (use_rela_initialised && use_rela) |
3410fea8 | 9152 | { |
9793eb77 | 9153 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9154 | "they are in more than one size"), |
9155 | abfd); | |
3410fea8 NC |
9156 | bfd_set_error (bfd_error_invalid_operation); |
9157 | return 0; | |
9158 | } | |
9159 | else | |
9160 | { | |
9161 | use_rela = FALSE; | |
9162 | use_rela_initialised = TRUE; | |
9163 | } | |
9164 | } | |
9165 | else | |
9166 | { | |
c8e44c6d AM |
9167 | /* The section size is not divisible by either - |
9168 | something is wrong. */ | |
9793eb77 | 9169 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9170 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9171 | bfd_set_error (bfd_error_invalid_operation); |
9172 | return 0; | |
9173 | } | |
9174 | } | |
9175 | ||
9176 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9177 | if (lo->type == bfd_indirect_link_order) | |
9178 | { | |
9179 | asection *o = lo->u.indirect.section; | |
9180 | ||
9181 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9182 | { | |
9183 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9184 | /* Section size is divisible by both rel and rela sizes. | |
9185 | It is of no help to us. */ | |
9186 | ; | |
9187 | else | |
9188 | { | |
9189 | /* Section size is only divisible by rela. */ | |
535b785f | 9190 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9191 | { |
9793eb77 | 9192 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9193 | "they are in more than one size"), |
9194 | abfd); | |
3410fea8 NC |
9195 | bfd_set_error (bfd_error_invalid_operation); |
9196 | return 0; | |
9197 | } | |
9198 | else | |
9199 | { | |
9200 | use_rela = TRUE; | |
9201 | use_rela_initialised = TRUE; | |
9202 | } | |
9203 | } | |
9204 | } | |
9205 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9206 | { | |
9207 | /* Section size is only divisible by rel. */ | |
535b785f | 9208 | if (use_rela_initialised && use_rela) |
3410fea8 | 9209 | { |
9793eb77 | 9210 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9211 | "they are in more than one size"), |
9212 | abfd); | |
3410fea8 NC |
9213 | bfd_set_error (bfd_error_invalid_operation); |
9214 | return 0; | |
9215 | } | |
9216 | else | |
9217 | { | |
9218 | use_rela = FALSE; | |
9219 | use_rela_initialised = TRUE; | |
9220 | } | |
9221 | } | |
9222 | else | |
9223 | { | |
c8e44c6d AM |
9224 | /* The section size is not divisible by either - |
9225 | something is wrong. */ | |
9793eb77 | 9226 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9227 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9228 | bfd_set_error (bfd_error_invalid_operation); |
9229 | return 0; | |
9230 | } | |
9231 | } | |
9232 | ||
9233 | if (! use_rela_initialised) | |
9234 | /* Make a guess. */ | |
9235 | use_rela = TRUE; | |
c152c796 | 9236 | } |
fc66a176 L |
9237 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
9238 | use_rela = TRUE; | |
9239 | else if (rel_dyn != NULL && rel_dyn->size > 0) | |
3410fea8 | 9240 | use_rela = FALSE; |
c152c796 | 9241 | else |
fc66a176 | 9242 | return 0; |
3410fea8 NC |
9243 | |
9244 | if (use_rela) | |
c152c796 | 9245 | { |
3410fea8 | 9246 | dynamic_relocs = rela_dyn; |
c152c796 AM |
9247 | ext_size = bed->s->sizeof_rela; |
9248 | swap_in = bed->s->swap_reloca_in; | |
9249 | swap_out = bed->s->swap_reloca_out; | |
9250 | } | |
3410fea8 NC |
9251 | else |
9252 | { | |
9253 | dynamic_relocs = rel_dyn; | |
9254 | ext_size = bed->s->sizeof_rel; | |
9255 | swap_in = bed->s->swap_reloc_in; | |
9256 | swap_out = bed->s->swap_reloc_out; | |
9257 | } | |
c152c796 AM |
9258 | |
9259 | size = 0; | |
3410fea8 | 9260 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 9261 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 9262 | size += lo->u.indirect.section->size; |
c152c796 | 9263 | |
3410fea8 | 9264 | if (size != dynamic_relocs->size) |
c152c796 AM |
9265 | return 0; |
9266 | ||
9267 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
9268 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
9269 | |
9270 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
9271 | if (count == 0) |
9272 | return 0; | |
a50b1753 | 9273 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 9274 | |
c152c796 AM |
9275 | if (sort == NULL) |
9276 | { | |
9277 | (*info->callbacks->warning) | |
9793eb77 | 9278 | (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0); |
c152c796 AM |
9279 | return 0; |
9280 | } | |
9281 | ||
9282 | if (bed->s->arch_size == 32) | |
9283 | r_sym_mask = ~(bfd_vma) 0xff; | |
9284 | else | |
9285 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
9286 | ||
3410fea8 | 9287 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9288 | if (lo->type == bfd_indirect_link_order) |
9289 | { | |
9290 | bfd_byte *erel, *erelend; | |
9291 | asection *o = lo->u.indirect.section; | |
9292 | ||
1da212d6 AM |
9293 | if (o->contents == NULL && o->size != 0) |
9294 | { | |
9295 | /* This is a reloc section that is being handled as a normal | |
9296 | section. See bfd_section_from_shdr. We can't combine | |
9297 | relocs in this case. */ | |
9298 | free (sort); | |
9299 | return 0; | |
9300 | } | |
c152c796 | 9301 | erel = o->contents; |
eea6121a | 9302 | erelend = o->contents + o->size; |
c8e44c6d | 9303 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 9304 | |
c152c796 AM |
9305 | while (erel < erelend) |
9306 | { | |
9307 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 9308 | |
c152c796 | 9309 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 9310 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
9311 | s->u.sym_mask = r_sym_mask; |
9312 | p += sort_elt; | |
9313 | erel += ext_size; | |
9314 | } | |
9315 | } | |
9316 | ||
9317 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
9318 | ||
9319 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
9320 | { | |
9321 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9322 | if (s->type != reloc_class_relative) | |
9323 | break; | |
9324 | } | |
9325 | ret = i; | |
9326 | s_non_relative = p; | |
9327 | ||
9328 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
9329 | for (; i < count; i++, p += sort_elt) | |
9330 | { | |
9331 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
9332 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
9333 | sq = sp; | |
9334 | sp->u.offset = sq->rela->r_offset; | |
9335 | } | |
9336 | ||
9337 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
9338 | ||
c8e44c6d AM |
9339 | struct elf_link_hash_table *htab = elf_hash_table (info); |
9340 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
9341 | { | |
9342 | /* We have plt relocs in .rela.dyn. */ | |
9343 | sq = (struct elf_link_sort_rela *) sort; | |
9344 | for (i = 0; i < count; i++) | |
9345 | if (sq[count - i - 1].type != reloc_class_plt) | |
9346 | break; | |
9347 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
9348 | { | |
9349 | struct bfd_link_order **plo; | |
9350 | /* Put srelplt link_order last. This is so the output_offset | |
9351 | set in the next loop is correct for DT_JMPREL. */ | |
9352 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
9353 | if ((*plo)->type == bfd_indirect_link_order | |
9354 | && (*plo)->u.indirect.section == htab->srelplt) | |
9355 | { | |
9356 | lo = *plo; | |
9357 | *plo = lo->next; | |
9358 | } | |
9359 | else | |
9360 | plo = &(*plo)->next; | |
9361 | *plo = lo; | |
9362 | lo->next = NULL; | |
9363 | dynamic_relocs->map_tail.link_order = lo; | |
9364 | } | |
9365 | } | |
9366 | ||
9367 | p = sort; | |
3410fea8 | 9368 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9369 | if (lo->type == bfd_indirect_link_order) |
9370 | { | |
9371 | bfd_byte *erel, *erelend; | |
9372 | asection *o = lo->u.indirect.section; | |
9373 | ||
9374 | erel = o->contents; | |
eea6121a | 9375 | erelend = o->contents + o->size; |
c8e44c6d | 9376 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
9377 | while (erel < erelend) |
9378 | { | |
9379 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9380 | (*swap_out) (abfd, s->rela, erel); | |
9381 | p += sort_elt; | |
9382 | erel += ext_size; | |
9383 | } | |
9384 | } | |
9385 | ||
9386 | free (sort); | |
3410fea8 | 9387 | *psec = dynamic_relocs; |
c152c796 AM |
9388 | return ret; |
9389 | } | |
9390 | ||
ef10c3ac | 9391 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 9392 | |
6e0b88f1 | 9393 | static int |
ef10c3ac L |
9394 | elf_link_output_symstrtab (struct elf_final_link_info *flinfo, |
9395 | const char *name, | |
9396 | Elf_Internal_Sym *elfsym, | |
9397 | asection *input_sec, | |
9398 | struct elf_link_hash_entry *h) | |
c152c796 | 9399 | { |
6e0b88f1 | 9400 | int (*output_symbol_hook) |
c152c796 AM |
9401 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
9402 | struct elf_link_hash_entry *); | |
ef10c3ac | 9403 | struct elf_link_hash_table *hash_table; |
c152c796 | 9404 | const struct elf_backend_data *bed; |
ef10c3ac | 9405 | bfd_size_type strtabsize; |
c152c796 | 9406 | |
8539e4e8 AM |
9407 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
9408 | ||
8b127cbc | 9409 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
9410 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
9411 | if (output_symbol_hook != NULL) | |
9412 | { | |
8b127cbc | 9413 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
9414 | if (ret != 1) |
9415 | return ret; | |
c152c796 AM |
9416 | } |
9417 | ||
06f44071 AM |
9418 | if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC) |
9419 | elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc; | |
9420 | if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE) | |
9421 | elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique; | |
9422 | ||
ef10c3ac L |
9423 | if (name == NULL |
9424 | || *name == '\0' | |
9425 | || (input_sec->flags & SEC_EXCLUDE)) | |
9426 | elfsym->st_name = (unsigned long) -1; | |
c152c796 AM |
9427 | else |
9428 | { | |
ef10c3ac L |
9429 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
9430 | to get the final offset for st_name. */ | |
9431 | elfsym->st_name | |
9432 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
9433 | name, FALSE); | |
c152c796 | 9434 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 9435 | return 0; |
c152c796 AM |
9436 | } |
9437 | ||
ef10c3ac L |
9438 | hash_table = elf_hash_table (flinfo->info); |
9439 | strtabsize = hash_table->strtabsize; | |
9440 | if (strtabsize <= hash_table->strtabcount) | |
c152c796 | 9441 | { |
ef10c3ac L |
9442 | strtabsize += strtabsize; |
9443 | hash_table->strtabsize = strtabsize; | |
9444 | strtabsize *= sizeof (*hash_table->strtab); | |
9445 | hash_table->strtab | |
9446 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
9447 | strtabsize); | |
9448 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 9449 | return 0; |
c152c796 | 9450 | } |
ef10c3ac L |
9451 | hash_table->strtab[hash_table->strtabcount].sym = *elfsym; |
9452 | hash_table->strtab[hash_table->strtabcount].dest_index | |
9453 | = hash_table->strtabcount; | |
9454 | hash_table->strtab[hash_table->strtabcount].destshndx_index | |
9455 | = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; | |
9456 | ||
9457 | bfd_get_symcount (flinfo->output_bfd) += 1; | |
9458 | hash_table->strtabcount += 1; | |
9459 | ||
9460 | return 1; | |
9461 | } | |
9462 | ||
9463 | /* Swap symbols out to the symbol table and flush the output symbols to | |
9464 | the file. */ | |
9465 | ||
9466 | static bfd_boolean | |
9467 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) | |
9468 | { | |
9469 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
ef53be89 AM |
9470 | bfd_size_type amt; |
9471 | size_t i; | |
ef10c3ac L |
9472 | const struct elf_backend_data *bed; |
9473 | bfd_byte *symbuf; | |
9474 | Elf_Internal_Shdr *hdr; | |
9475 | file_ptr pos; | |
9476 | bfd_boolean ret; | |
9477 | ||
9478 | if (!hash_table->strtabcount) | |
9479 | return TRUE; | |
9480 | ||
9481 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
9482 | ||
9483 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 9484 | |
ef10c3ac L |
9485 | amt = bed->s->sizeof_sym * hash_table->strtabcount; |
9486 | symbuf = (bfd_byte *) bfd_malloc (amt); | |
9487 | if (symbuf == NULL) | |
9488 | return FALSE; | |
1b786873 | 9489 | |
ef10c3ac | 9490 | if (flinfo->symshndxbuf) |
c152c796 | 9491 | { |
ef53be89 AM |
9492 | amt = sizeof (Elf_External_Sym_Shndx); |
9493 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
9494 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
9495 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 9496 | { |
ef10c3ac L |
9497 | free (symbuf); |
9498 | return FALSE; | |
c152c796 | 9499 | } |
c152c796 AM |
9500 | } |
9501 | ||
ef10c3ac L |
9502 | for (i = 0; i < hash_table->strtabcount; i++) |
9503 | { | |
9504 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
9505 | if (elfsym->sym.st_name == (unsigned long) -1) | |
9506 | elfsym->sym.st_name = 0; | |
9507 | else | |
9508 | elfsym->sym.st_name | |
9509 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
9510 | elfsym->sym.st_name); | |
9511 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, | |
9512 | ((bfd_byte *) symbuf | |
9513 | + (elfsym->dest_index | |
9514 | * bed->s->sizeof_sym)), | |
9515 | (flinfo->symshndxbuf | |
9516 | + elfsym->destshndx_index)); | |
9517 | } | |
9518 | ||
9519 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
9520 | pos = hdr->sh_offset + hdr->sh_size; | |
9521 | amt = hash_table->strtabcount * bed->s->sizeof_sym; | |
9522 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 | |
9523 | && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) | |
9524 | { | |
9525 | hdr->sh_size += amt; | |
9526 | ret = TRUE; | |
9527 | } | |
9528 | else | |
9529 | ret = FALSE; | |
c152c796 | 9530 | |
ef10c3ac L |
9531 | free (symbuf); |
9532 | ||
9533 | free (hash_table->strtab); | |
9534 | hash_table->strtab = NULL; | |
9535 | ||
9536 | return ret; | |
c152c796 AM |
9537 | } |
9538 | ||
c0d5a53d L |
9539 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
9540 | ||
9541 | static bfd_boolean | |
9542 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) | |
9543 | { | |
4fbb74a6 AM |
9544 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
9545 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
9546 | { |
9547 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 9548 | beyond 64k. */ |
4eca0228 | 9549 | _bfd_error_handler |
695344c0 | 9550 | /* xgettext:c-format */ |
9793eb77 | 9551 | (_("%pB: too many sections: %d (>= %d)"), |
4fbb74a6 | 9552 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d L |
9553 | bfd_set_error (bfd_error_nonrepresentable_section); |
9554 | return FALSE; | |
9555 | } | |
9556 | return TRUE; | |
9557 | } | |
9558 | ||
c152c796 AM |
9559 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
9560 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
9561 | versioned symbol that would normally require an explicit version. | |
9562 | We also handle the case that a DSO references a hidden symbol | |
9563 | which may be satisfied by a versioned symbol in another DSO. */ | |
9564 | ||
9565 | static bfd_boolean | |
9566 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
9567 | const struct elf_backend_data *bed, | |
9568 | struct elf_link_hash_entry *h) | |
9569 | { | |
9570 | bfd *abfd; | |
9571 | struct elf_link_loaded_list *loaded; | |
9572 | ||
9573 | if (!is_elf_hash_table (info->hash)) | |
9574 | return FALSE; | |
9575 | ||
90c984fc L |
9576 | /* Check indirect symbol. */ |
9577 | while (h->root.type == bfd_link_hash_indirect) | |
9578 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9579 | ||
c152c796 AM |
9580 | switch (h->root.type) |
9581 | { | |
9582 | default: | |
9583 | abfd = NULL; | |
9584 | break; | |
9585 | ||
9586 | case bfd_link_hash_undefined: | |
9587 | case bfd_link_hash_undefweak: | |
9588 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
9589 | if (abfd == NULL |
9590 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 9591 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
9592 | return FALSE; |
9593 | break; | |
9594 | ||
9595 | case bfd_link_hash_defined: | |
9596 | case bfd_link_hash_defweak: | |
9597 | abfd = h->root.u.def.section->owner; | |
9598 | break; | |
9599 | ||
9600 | case bfd_link_hash_common: | |
9601 | abfd = h->root.u.c.p->section->owner; | |
9602 | break; | |
9603 | } | |
9604 | BFD_ASSERT (abfd != NULL); | |
9605 | ||
9606 | for (loaded = elf_hash_table (info)->loaded; | |
9607 | loaded != NULL; | |
9608 | loaded = loaded->next) | |
9609 | { | |
9610 | bfd *input; | |
9611 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
9612 | size_t symcount; |
9613 | size_t extsymcount; | |
9614 | size_t extsymoff; | |
c152c796 AM |
9615 | Elf_Internal_Shdr *versymhdr; |
9616 | Elf_Internal_Sym *isym; | |
9617 | Elf_Internal_Sym *isymend; | |
9618 | Elf_Internal_Sym *isymbuf; | |
9619 | Elf_External_Versym *ever; | |
9620 | Elf_External_Versym *extversym; | |
9621 | ||
9622 | input = loaded->abfd; | |
9623 | ||
9624 | /* We check each DSO for a possible hidden versioned definition. */ | |
9625 | if (input == abfd | |
9626 | || (input->flags & DYNAMIC) == 0 | |
9627 | || elf_dynversym (input) == 0) | |
9628 | continue; | |
9629 | ||
9630 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
9631 | ||
9632 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
9633 | if (elf_bad_symtab (input)) | |
9634 | { | |
9635 | extsymcount = symcount; | |
9636 | extsymoff = 0; | |
9637 | } | |
9638 | else | |
9639 | { | |
9640 | extsymcount = symcount - hdr->sh_info; | |
9641 | extsymoff = hdr->sh_info; | |
9642 | } | |
9643 | ||
9644 | if (extsymcount == 0) | |
9645 | continue; | |
9646 | ||
9647 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
9648 | NULL, NULL, NULL); | |
9649 | if (isymbuf == NULL) | |
9650 | return FALSE; | |
9651 | ||
9652 | /* Read in any version definitions. */ | |
9653 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
a50b1753 | 9654 | extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); |
c152c796 AM |
9655 | if (extversym == NULL) |
9656 | goto error_ret; | |
9657 | ||
9658 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
9659 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
9660 | != versymhdr->sh_size)) | |
9661 | { | |
9662 | free (extversym); | |
9663 | error_ret: | |
9664 | free (isymbuf); | |
9665 | return FALSE; | |
9666 | } | |
9667 | ||
9668 | ever = extversym + extsymoff; | |
9669 | isymend = isymbuf + extsymcount; | |
9670 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
9671 | { | |
9672 | const char *name; | |
9673 | Elf_Internal_Versym iver; | |
9674 | unsigned short version_index; | |
9675 | ||
9676 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
9677 | || isym->st_shndx == SHN_UNDEF) | |
9678 | continue; | |
9679 | ||
9680 | name = bfd_elf_string_from_elf_section (input, | |
9681 | hdr->sh_link, | |
9682 | isym->st_name); | |
9683 | if (strcmp (name, h->root.root.string) != 0) | |
9684 | continue; | |
9685 | ||
9686 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
9687 | ||
d023c380 L |
9688 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
9689 | && !(h->def_regular | |
9690 | && h->forced_local)) | |
c152c796 AM |
9691 | { |
9692 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
9693 | have provided a definition for the undefined sym unless |
9694 | it is defined in a non-shared object and forced local. | |
9695 | */ | |
c152c796 AM |
9696 | abort (); |
9697 | } | |
9698 | ||
9699 | version_index = iver.vs_vers & VERSYM_VERSION; | |
9700 | if (version_index == 1 || version_index == 2) | |
9701 | { | |
9702 | /* This is the base or first version. We can use it. */ | |
9703 | free (extversym); | |
9704 | free (isymbuf); | |
9705 | return TRUE; | |
9706 | } | |
9707 | } | |
9708 | ||
9709 | free (extversym); | |
9710 | free (isymbuf); | |
9711 | } | |
9712 | ||
9713 | return FALSE; | |
9714 | } | |
9715 | ||
b8871f35 L |
9716 | /* Convert ELF common symbol TYPE. */ |
9717 | ||
9718 | static int | |
9719 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
9720 | { | |
9721 | /* Commom symbol can only appear in relocatable link. */ | |
9722 | if (!bfd_link_relocatable (info)) | |
9723 | abort (); | |
9724 | switch (info->elf_stt_common) | |
9725 | { | |
9726 | case unchanged: | |
9727 | break; | |
9728 | case elf_stt_common: | |
9729 | type = STT_COMMON; | |
9730 | break; | |
9731 | case no_elf_stt_common: | |
9732 | type = STT_OBJECT; | |
9733 | break; | |
9734 | } | |
9735 | return type; | |
9736 | } | |
9737 | ||
c152c796 AM |
9738 | /* Add an external symbol to the symbol table. This is called from |
9739 | the hash table traversal routine. When generating a shared object, | |
9740 | we go through the symbol table twice. The first time we output | |
9741 | anything that might have been forced to local scope in a version | |
9742 | script. The second time we output the symbols that are still | |
9743 | global symbols. */ | |
9744 | ||
9745 | static bfd_boolean | |
7686d77d | 9746 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 9747 | { |
7686d77d | 9748 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 9749 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 9750 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
c152c796 AM |
9751 | bfd_boolean strip; |
9752 | Elf_Internal_Sym sym; | |
9753 | asection *input_sec; | |
9754 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
9755 | long indx; |
9756 | int ret; | |
b8871f35 | 9757 | unsigned int type; |
c152c796 AM |
9758 | |
9759 | if (h->root.type == bfd_link_hash_warning) | |
9760 | { | |
9761 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9762 | if (h->root.type == bfd_link_hash_new) | |
9763 | return TRUE; | |
9764 | } | |
9765 | ||
9766 | /* Decide whether to output this symbol in this pass. */ | |
9767 | if (eoinfo->localsyms) | |
9768 | { | |
4deb8f71 | 9769 | if (!h->forced_local) |
c152c796 AM |
9770 | return TRUE; |
9771 | } | |
9772 | else | |
9773 | { | |
4deb8f71 | 9774 | if (h->forced_local) |
c152c796 AM |
9775 | return TRUE; |
9776 | } | |
9777 | ||
8b127cbc | 9778 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 9779 | |
12ac1cf5 | 9780 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 9781 | { |
12ac1cf5 NC |
9782 | /* If we have an undefined symbol reference here then it must have |
9783 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
9784 | references in regular files have already been handled unless |
9785 | they are in unreferenced sections which are removed by garbage | |
9786 | collection). */ | |
12ac1cf5 NC |
9787 | bfd_boolean ignore_undef = FALSE; |
9788 | ||
9789 | /* Some symbols may be special in that the fact that they're | |
9790 | undefined can be safely ignored - let backend determine that. */ | |
9791 | if (bed->elf_backend_ignore_undef_symbol) | |
9792 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
9793 | ||
9794 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 9795 | if (!ignore_undef |
c54f1524 | 9796 | && h->ref_dynamic_nonweak |
8b127cbc AM |
9797 | && (!h->ref_regular || flinfo->info->gc_sections) |
9798 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
9799 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
1a72702b AM |
9800 | (*flinfo->info->callbacks->undefined_symbol) |
9801 | (flinfo->info, h->root.root.string, | |
9802 | h->ref_regular ? NULL : h->root.u.undef.abfd, | |
9803 | NULL, 0, | |
9804 | flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); | |
97196564 L |
9805 | |
9806 | /* Strip a global symbol defined in a discarded section. */ | |
9807 | if (h->indx == -3) | |
9808 | return TRUE; | |
c152c796 AM |
9809 | } |
9810 | ||
9811 | /* We should also warn if a forced local symbol is referenced from | |
9812 | shared libraries. */ | |
0e1862bb | 9813 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
9814 | && h->forced_local |
9815 | && h->ref_dynamic | |
371a5866 | 9816 | && h->def_regular |
f5385ebf | 9817 | && !h->dynamic_def |
ee659f1f | 9818 | && h->ref_dynamic_nonweak |
8b127cbc | 9819 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 9820 | { |
17d078c5 AM |
9821 | bfd *def_bfd; |
9822 | const char *msg; | |
90c984fc L |
9823 | struct elf_link_hash_entry *hi = h; |
9824 | ||
9825 | /* Check indirect symbol. */ | |
9826 | while (hi->root.type == bfd_link_hash_indirect) | |
9827 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
9828 | |
9829 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 9830 | /* xgettext:c-format */ |
871b3ab2 | 9831 | msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 9832 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) |
695344c0 | 9833 | /* xgettext:c-format */ |
871b3ab2 | 9834 | msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 9835 | else |
695344c0 | 9836 | /* xgettext:c-format */ |
871b3ab2 | 9837 | msg = _("%pB: local symbol `%s' in %pB is referenced by DSO"); |
8b127cbc | 9838 | def_bfd = flinfo->output_bfd; |
90c984fc L |
9839 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
9840 | def_bfd = hi->root.u.def.section->owner; | |
c08bb8dd AM |
9841 | _bfd_error_handler (msg, flinfo->output_bfd, |
9842 | h->root.root.string, def_bfd); | |
17d078c5 | 9843 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
9844 | eoinfo->failed = TRUE; |
9845 | return FALSE; | |
9846 | } | |
9847 | ||
9848 | /* We don't want to output symbols that have never been mentioned by | |
9849 | a regular file, or that we have been told to strip. However, if | |
9850 | h->indx is set to -2, the symbol is used by a reloc and we must | |
9851 | output it. */ | |
d983c8c5 | 9852 | strip = FALSE; |
c152c796 | 9853 | if (h->indx == -2) |
d983c8c5 | 9854 | ; |
f5385ebf | 9855 | else if ((h->def_dynamic |
77cfaee6 AM |
9856 | || h->ref_dynamic |
9857 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
9858 | && !h->def_regular |
9859 | && !h->ref_regular) | |
c152c796 | 9860 | strip = TRUE; |
8b127cbc | 9861 | else if (flinfo->info->strip == strip_all) |
c152c796 | 9862 | strip = TRUE; |
8b127cbc AM |
9863 | else if (flinfo->info->strip == strip_some |
9864 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
c152c796 AM |
9865 | h->root.root.string, FALSE, FALSE) == NULL) |
9866 | strip = TRUE; | |
d56d55e7 AM |
9867 | else if ((h->root.type == bfd_link_hash_defined |
9868 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 9869 | && ((flinfo->info->strip_discarded |
dbaa2011 | 9870 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
9871 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
9872 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 9873 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
c152c796 | 9874 | strip = TRUE; |
9e2278f5 AM |
9875 | else if ((h->root.type == bfd_link_hash_undefined |
9876 | || h->root.type == bfd_link_hash_undefweak) | |
9877 | && h->root.u.undef.abfd != NULL | |
9878 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
9879 | strip = TRUE; | |
c152c796 | 9880 | |
b8871f35 L |
9881 | type = h->type; |
9882 | ||
c152c796 | 9883 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
9884 | nothing else to do. However, if it is a forced local symbol or |
9885 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
9886 | function a chance to make it dynamic. */ | |
c152c796 AM |
9887 | if (strip |
9888 | && h->dynindx == -1 | |
b8871f35 | 9889 | && type != STT_GNU_IFUNC |
f5385ebf | 9890 | && !h->forced_local) |
c152c796 AM |
9891 | return TRUE; |
9892 | ||
9893 | sym.st_value = 0; | |
9894 | sym.st_size = h->size; | |
9895 | sym.st_other = h->other; | |
c152c796 AM |
9896 | switch (h->root.type) |
9897 | { | |
9898 | default: | |
9899 | case bfd_link_hash_new: | |
9900 | case bfd_link_hash_warning: | |
9901 | abort (); | |
9902 | return FALSE; | |
9903 | ||
9904 | case bfd_link_hash_undefined: | |
9905 | case bfd_link_hash_undefweak: | |
9906 | input_sec = bfd_und_section_ptr; | |
9907 | sym.st_shndx = SHN_UNDEF; | |
9908 | break; | |
9909 | ||
9910 | case bfd_link_hash_defined: | |
9911 | case bfd_link_hash_defweak: | |
9912 | { | |
9913 | input_sec = h->root.u.def.section; | |
9914 | if (input_sec->output_section != NULL) | |
9915 | { | |
9916 | sym.st_shndx = | |
8b127cbc | 9917 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
9918 | input_sec->output_section); |
9919 | if (sym.st_shndx == SHN_BAD) | |
9920 | { | |
4eca0228 | 9921 | _bfd_error_handler |
695344c0 | 9922 | /* xgettext:c-format */ |
871b3ab2 | 9923 | (_("%pB: could not find output section %pA for input section %pA"), |
8b127cbc | 9924 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 9925 | bfd_set_error (bfd_error_nonrepresentable_section); |
c152c796 AM |
9926 | eoinfo->failed = TRUE; |
9927 | return FALSE; | |
9928 | } | |
9929 | ||
9930 | /* ELF symbols in relocatable files are section relative, | |
9931 | but in nonrelocatable files they are virtual | |
9932 | addresses. */ | |
9933 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 9934 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
9935 | { |
9936 | sym.st_value += input_sec->output_section->vma; | |
9937 | if (h->type == STT_TLS) | |
9938 | { | |
8b127cbc | 9939 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
9940 | if (tls_sec != NULL) |
9941 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
9942 | } |
9943 | } | |
9944 | } | |
9945 | else | |
9946 | { | |
9947 | BFD_ASSERT (input_sec->owner == NULL | |
9948 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
9949 | sym.st_shndx = SHN_UNDEF; | |
9950 | input_sec = bfd_und_section_ptr; | |
9951 | } | |
9952 | } | |
9953 | break; | |
9954 | ||
9955 | case bfd_link_hash_common: | |
9956 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 9957 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
9958 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
9959 | break; | |
9960 | ||
9961 | case bfd_link_hash_indirect: | |
9962 | /* These symbols are created by symbol versioning. They point | |
9963 | to the decorated version of the name. For example, if the | |
9964 | symbol foo@@GNU_1.2 is the default, which should be used when | |
9965 | foo is used with no version, then we add an indirect symbol | |
9966 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
9967 | since the indirected symbol is already in the hash table. */ | |
9968 | return TRUE; | |
9969 | } | |
9970 | ||
b8871f35 L |
9971 | if (type == STT_COMMON || type == STT_OBJECT) |
9972 | switch (h->root.type) | |
9973 | { | |
9974 | case bfd_link_hash_common: | |
9975 | type = elf_link_convert_common_type (flinfo->info, type); | |
9976 | break; | |
9977 | case bfd_link_hash_defined: | |
9978 | case bfd_link_hash_defweak: | |
9979 | if (bed->common_definition (&sym)) | |
9980 | type = elf_link_convert_common_type (flinfo->info, type); | |
9981 | else | |
9982 | type = STT_OBJECT; | |
9983 | break; | |
9984 | case bfd_link_hash_undefined: | |
9985 | case bfd_link_hash_undefweak: | |
9986 | break; | |
9987 | default: | |
9988 | abort (); | |
9989 | } | |
9990 | ||
4deb8f71 | 9991 | if (h->forced_local) |
b8871f35 L |
9992 | { |
9993 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
9994 | /* Turn off visibility on local symbol. */ | |
9995 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
9996 | } | |
9997 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
9998 | else if (h->unique_global && h->def_regular) | |
9999 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
10000 | else if (h->root.type == bfd_link_hash_undefweak | |
10001 | || h->root.type == bfd_link_hash_defweak) | |
10002 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
10003 | else | |
10004 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
10005 | sym.st_target_internal = h->target_internal; | |
10006 | ||
c152c796 AM |
10007 | /* Give the processor backend a chance to tweak the symbol value, |
10008 | and also to finish up anything that needs to be done for this | |
10009 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 10010 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 10011 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 10012 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 10013 | && h->def_regular |
0e1862bb | 10014 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
10015 | || ((h->dynindx != -1 |
10016 | || h->forced_local) | |
0e1862bb | 10017 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
10018 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
10019 | || h->root.type != bfd_link_hash_undefweak)) | |
10020 | || !h->forced_local) | |
8b127cbc | 10021 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
10022 | { |
10023 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 10024 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 AM |
10025 | { |
10026 | eoinfo->failed = TRUE; | |
10027 | return FALSE; | |
10028 | } | |
10029 | } | |
10030 | ||
10031 | /* If we are marking the symbol as undefined, and there are no | |
10032 | non-weak references to this symbol from a regular object, then | |
10033 | mark the symbol as weak undefined; if there are non-weak | |
10034 | references, mark the symbol as strong. We can't do this earlier, | |
10035 | because it might not be marked as undefined until the | |
10036 | finish_dynamic_symbol routine gets through with it. */ | |
10037 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 10038 | && h->ref_regular |
c152c796 AM |
10039 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
10040 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
10041 | { | |
10042 | int bindtype; | |
b8871f35 | 10043 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
10044 | |
10045 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
10046 | if (type == STT_GNU_IFUNC) | |
10047 | type = STT_FUNC; | |
c152c796 | 10048 | |
f5385ebf | 10049 | if (h->ref_regular_nonweak) |
c152c796 AM |
10050 | bindtype = STB_GLOBAL; |
10051 | else | |
10052 | bindtype = STB_WEAK; | |
2955ec4c | 10053 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
10054 | } |
10055 | ||
bda987c2 CD |
10056 | /* If this is a symbol defined in a dynamic library, don't use the |
10057 | symbol size from the dynamic library. Relinking an executable | |
10058 | against a new library may introduce gratuitous changes in the | |
10059 | executable's symbols if we keep the size. */ | |
10060 | if (sym.st_shndx == SHN_UNDEF | |
10061 | && !h->def_regular | |
10062 | && h->def_dynamic) | |
10063 | sym.st_size = 0; | |
10064 | ||
c152c796 AM |
10065 | /* If a non-weak symbol with non-default visibility is not defined |
10066 | locally, it is a fatal error. */ | |
0e1862bb | 10067 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
10068 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
10069 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
10070 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 10071 | && !h->def_regular) |
c152c796 | 10072 | { |
17d078c5 AM |
10073 | const char *msg; |
10074 | ||
10075 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 10076 | /* xgettext:c-format */ |
871b3ab2 | 10077 | msg = _("%pB: protected symbol `%s' isn't defined"); |
17d078c5 | 10078 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) |
695344c0 | 10079 | /* xgettext:c-format */ |
871b3ab2 | 10080 | msg = _("%pB: internal symbol `%s' isn't defined"); |
17d078c5 | 10081 | else |
695344c0 | 10082 | /* xgettext:c-format */ |
871b3ab2 | 10083 | msg = _("%pB: hidden symbol `%s' isn't defined"); |
4eca0228 | 10084 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 10085 | bfd_set_error (bfd_error_bad_value); |
c152c796 AM |
10086 | eoinfo->failed = TRUE; |
10087 | return FALSE; | |
10088 | } | |
10089 | ||
10090 | /* If this symbol should be put in the .dynsym section, then put it | |
10091 | there now. We already know the symbol index. We also fill in | |
10092 | the entry in the .hash section. */ | |
1c2649ed EB |
10093 | if (h->dynindx != -1 |
10094 | && elf_hash_table (flinfo->info)->dynamic_sections_created | |
10095 | && elf_hash_table (flinfo->info)->dynsym != NULL | |
10096 | && !discarded_section (elf_hash_table (flinfo->info)->dynsym)) | |
c152c796 | 10097 | { |
c152c796 AM |
10098 | bfd_byte *esym; |
10099 | ||
90c984fc L |
10100 | /* Since there is no version information in the dynamic string, |
10101 | if there is no version info in symbol version section, we will | |
1659f720 | 10102 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 10103 | by shared library, or not bound locally. */ |
1659f720 | 10104 | if (h->verinfo.verdef == NULL |
0e1862bb | 10105 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
10106 | || h->ref_dynamic |
10107 | || !h->def_regular)) | |
90c984fc L |
10108 | { |
10109 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
10110 | ||
10111 | if (p && p [1] != '\0') | |
10112 | { | |
4eca0228 | 10113 | _bfd_error_handler |
695344c0 | 10114 | /* xgettext:c-format */ |
9793eb77 | 10115 | (_("%pB: no symbol version section for versioned symbol `%s'"), |
90c984fc L |
10116 | flinfo->output_bfd, h->root.root.string); |
10117 | eoinfo->failed = TRUE; | |
10118 | return FALSE; | |
10119 | } | |
10120 | } | |
10121 | ||
c152c796 | 10122 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
10123 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
10124 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 10125 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d L |
10126 | { |
10127 | eoinfo->failed = TRUE; | |
10128 | return FALSE; | |
10129 | } | |
8b127cbc | 10130 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 10131 | |
8b127cbc | 10132 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
10133 | { |
10134 | size_t hash_entry_size; | |
10135 | bfd_byte *bucketpos; | |
10136 | bfd_vma chain; | |
41198d0c L |
10137 | size_t bucketcount; |
10138 | size_t bucket; | |
10139 | ||
8b127cbc | 10140 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 10141 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
10142 | |
10143 | hash_entry_size | |
8b127cbc AM |
10144 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
10145 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 10146 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
10147 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
10148 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
10149 | bucketpos); | |
10150 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
10151 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
10152 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
10153 | } | |
c152c796 | 10154 | |
8b127cbc | 10155 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
10156 | { |
10157 | Elf_Internal_Versym iversym; | |
10158 | Elf_External_Versym *eversym; | |
10159 | ||
f5385ebf | 10160 | if (!h->def_regular) |
c152c796 | 10161 | { |
7b20f099 AM |
10162 | if (h->verinfo.verdef == NULL |
10163 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
10164 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
c152c796 AM |
10165 | iversym.vs_vers = 0; |
10166 | else | |
10167 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
10168 | } | |
10169 | else | |
10170 | { | |
10171 | if (h->verinfo.vertree == NULL) | |
10172 | iversym.vs_vers = 1; | |
10173 | else | |
10174 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 10175 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 10176 | iversym.vs_vers++; |
c152c796 AM |
10177 | } |
10178 | ||
422f1182 | 10179 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 10180 | defined locally. */ |
422f1182 | 10181 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
10182 | iversym.vs_vers |= VERSYM_HIDDEN; |
10183 | ||
8b127cbc | 10184 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 10185 | eversym += h->dynindx; |
8b127cbc | 10186 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
10187 | } |
10188 | } | |
10189 | ||
d983c8c5 AM |
10190 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
10191 | strip it from .symtab too. Obviously we can't do this for | |
10192 | relocatable output or when needed for --emit-relocs. */ | |
10193 | else if (input_sec == bfd_und_section_ptr | |
10194 | && h->indx != -2 | |
66cae560 NC |
10195 | /* PR 22319 Do not strip global undefined symbols marked as being needed. */ |
10196 | && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) | |
0e1862bb | 10197 | && !bfd_link_relocatable (flinfo->info)) |
d983c8c5 | 10198 | return TRUE; |
66cae560 | 10199 | |
d983c8c5 AM |
10200 | /* Also strip others that we couldn't earlier due to dynamic symbol |
10201 | processing. */ | |
10202 | if (strip) | |
10203 | return TRUE; | |
10204 | if ((input_sec->flags & SEC_EXCLUDE) != 0) | |
c152c796 AM |
10205 | return TRUE; |
10206 | ||
2ec55de3 AM |
10207 | /* Output a FILE symbol so that following locals are not associated |
10208 | with the wrong input file. We need one for forced local symbols | |
10209 | if we've seen more than one FILE symbol or when we have exactly | |
10210 | one FILE symbol but global symbols are present in a file other | |
10211 | than the one with the FILE symbol. We also need one if linker | |
10212 | defined symbols are present. In practice these conditions are | |
10213 | always met, so just emit the FILE symbol unconditionally. */ | |
10214 | if (eoinfo->localsyms | |
10215 | && !eoinfo->file_sym_done | |
10216 | && eoinfo->flinfo->filesym_count != 0) | |
10217 | { | |
10218 | Elf_Internal_Sym fsym; | |
10219 | ||
10220 | memset (&fsym, 0, sizeof (fsym)); | |
10221 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10222 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10223 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
10224 | bfd_und_section_ptr, NULL)) | |
2ec55de3 AM |
10225 | return FALSE; |
10226 | ||
10227 | eoinfo->file_sym_done = TRUE; | |
10228 | } | |
10229 | ||
8b127cbc | 10230 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
10231 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
10232 | input_sec, h); | |
6e0b88f1 | 10233 | if (ret == 0) |
c152c796 AM |
10234 | { |
10235 | eoinfo->failed = TRUE; | |
10236 | return FALSE; | |
10237 | } | |
6e0b88f1 AM |
10238 | else if (ret == 1) |
10239 | h->indx = indx; | |
10240 | else if (h->indx == -2) | |
10241 | abort(); | |
c152c796 AM |
10242 | |
10243 | return TRUE; | |
10244 | } | |
10245 | ||
cdd3575c AM |
10246 | /* Return TRUE if special handling is done for relocs in SEC against |
10247 | symbols defined in discarded sections. */ | |
10248 | ||
c152c796 AM |
10249 | static bfd_boolean |
10250 | elf_section_ignore_discarded_relocs (asection *sec) | |
10251 | { | |
10252 | const struct elf_backend_data *bed; | |
10253 | ||
cdd3575c AM |
10254 | switch (sec->sec_info_type) |
10255 | { | |
dbaa2011 AM |
10256 | case SEC_INFO_TYPE_STABS: |
10257 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 10258 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cdd3575c AM |
10259 | return TRUE; |
10260 | default: | |
10261 | break; | |
10262 | } | |
c152c796 AM |
10263 | |
10264 | bed = get_elf_backend_data (sec->owner); | |
10265 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
10266 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
10267 | return TRUE; | |
10268 | ||
10269 | return FALSE; | |
10270 | } | |
10271 | ||
9e66c942 AM |
10272 | /* Return a mask saying how ld should treat relocations in SEC against |
10273 | symbols defined in discarded sections. If this function returns | |
10274 | COMPLAIN set, ld will issue a warning message. If this function | |
10275 | returns PRETEND set, and the discarded section was link-once and the | |
10276 | same size as the kept link-once section, ld will pretend that the | |
10277 | symbol was actually defined in the kept section. Otherwise ld will | |
10278 | zero the reloc (at least that is the intent, but some cooperation by | |
10279 | the target dependent code is needed, particularly for REL targets). */ | |
10280 | ||
8a696751 AM |
10281 | unsigned int |
10282 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 10283 | { |
9e66c942 | 10284 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 10285 | return PRETEND; |
cdd3575c AM |
10286 | |
10287 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 10288 | return 0; |
cdd3575c AM |
10289 | |
10290 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 10291 | return 0; |
cdd3575c | 10292 | |
9e66c942 | 10293 | return COMPLAIN | PRETEND; |
cdd3575c AM |
10294 | } |
10295 | ||
3d7f7666 L |
10296 | /* Find a match between a section and a member of a section group. */ |
10297 | ||
10298 | static asection * | |
c0f00686 L |
10299 | match_group_member (asection *sec, asection *group, |
10300 | struct bfd_link_info *info) | |
3d7f7666 L |
10301 | { |
10302 | asection *first = elf_next_in_group (group); | |
10303 | asection *s = first; | |
10304 | ||
10305 | while (s != NULL) | |
10306 | { | |
c0f00686 | 10307 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
10308 | return s; |
10309 | ||
83180ade | 10310 | s = elf_next_in_group (s); |
3d7f7666 L |
10311 | if (s == first) |
10312 | break; | |
10313 | } | |
10314 | ||
10315 | return NULL; | |
10316 | } | |
10317 | ||
01b3c8ab | 10318 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
10319 | to replace it. Return the replacement if it is OK. Otherwise return |
10320 | NULL. */ | |
01b3c8ab L |
10321 | |
10322 | asection * | |
c0f00686 | 10323 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
10324 | { |
10325 | asection *kept; | |
10326 | ||
10327 | kept = sec->kept_section; | |
10328 | if (kept != NULL) | |
10329 | { | |
c2370991 | 10330 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 10331 | kept = match_group_member (sec, kept, info); |
1dd2625f BW |
10332 | if (kept != NULL |
10333 | && ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
10334 | != (kept->rawsize != 0 ? kept->rawsize : kept->size))) | |
01b3c8ab | 10335 | kept = NULL; |
c2370991 | 10336 | sec->kept_section = kept; |
01b3c8ab L |
10337 | } |
10338 | return kept; | |
10339 | } | |
10340 | ||
c152c796 AM |
10341 | /* Link an input file into the linker output file. This function |
10342 | handles all the sections and relocations of the input file at once. | |
10343 | This is so that we only have to read the local symbols once, and | |
10344 | don't have to keep them in memory. */ | |
10345 | ||
10346 | static bfd_boolean | |
8b127cbc | 10347 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 10348 | { |
ece5ef60 | 10349 | int (*relocate_section) |
c152c796 AM |
10350 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
10351 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
10352 | bfd *output_bfd; | |
10353 | Elf_Internal_Shdr *symtab_hdr; | |
10354 | size_t locsymcount; | |
10355 | size_t extsymoff; | |
10356 | Elf_Internal_Sym *isymbuf; | |
10357 | Elf_Internal_Sym *isym; | |
10358 | Elf_Internal_Sym *isymend; | |
10359 | long *pindex; | |
10360 | asection **ppsection; | |
10361 | asection *o; | |
10362 | const struct elf_backend_data *bed; | |
c152c796 | 10363 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
10364 | bfd_size_type address_size; |
10365 | bfd_vma r_type_mask; | |
10366 | int r_sym_shift; | |
ffbc01cc | 10367 | bfd_boolean have_file_sym = FALSE; |
c152c796 | 10368 | |
8b127cbc | 10369 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
10370 | bed = get_elf_backend_data (output_bfd); |
10371 | relocate_section = bed->elf_backend_relocate_section; | |
10372 | ||
10373 | /* If this is a dynamic object, we don't want to do anything here: | |
10374 | we don't want the local symbols, and we don't want the section | |
10375 | contents. */ | |
10376 | if ((input_bfd->flags & DYNAMIC) != 0) | |
10377 | return TRUE; | |
10378 | ||
c152c796 AM |
10379 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
10380 | if (elf_bad_symtab (input_bfd)) | |
10381 | { | |
10382 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
10383 | extsymoff = 0; | |
10384 | } | |
10385 | else | |
10386 | { | |
10387 | locsymcount = symtab_hdr->sh_info; | |
10388 | extsymoff = symtab_hdr->sh_info; | |
10389 | } | |
10390 | ||
10391 | /* Read the local symbols. */ | |
10392 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
10393 | if (isymbuf == NULL && locsymcount != 0) | |
10394 | { | |
10395 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
10396 | flinfo->internal_syms, |
10397 | flinfo->external_syms, | |
10398 | flinfo->locsym_shndx); | |
c152c796 AM |
10399 | if (isymbuf == NULL) |
10400 | return FALSE; | |
10401 | } | |
10402 | ||
10403 | /* Find local symbol sections and adjust values of symbols in | |
10404 | SEC_MERGE sections. Write out those local symbols we know are | |
10405 | going into the output file. */ | |
10406 | isymend = isymbuf + locsymcount; | |
8b127cbc | 10407 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
10408 | isym < isymend; |
10409 | isym++, pindex++, ppsection++) | |
10410 | { | |
10411 | asection *isec; | |
10412 | const char *name; | |
10413 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
10414 | long indx; |
10415 | int ret; | |
c152c796 AM |
10416 | |
10417 | *pindex = -1; | |
10418 | ||
10419 | if (elf_bad_symtab (input_bfd)) | |
10420 | { | |
10421 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
10422 | { | |
10423 | *ppsection = NULL; | |
10424 | continue; | |
10425 | } | |
10426 | } | |
10427 | ||
10428 | if (isym->st_shndx == SHN_UNDEF) | |
10429 | isec = bfd_und_section_ptr; | |
c152c796 AM |
10430 | else if (isym->st_shndx == SHN_ABS) |
10431 | isec = bfd_abs_section_ptr; | |
10432 | else if (isym->st_shndx == SHN_COMMON) | |
10433 | isec = bfd_com_section_ptr; | |
10434 | else | |
10435 | { | |
cb33740c AM |
10436 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
10437 | if (isec == NULL) | |
10438 | { | |
10439 | /* Don't attempt to output symbols with st_shnx in the | |
10440 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
6835821b | 10441 | isec = bfd_und_section_ptr; |
cb33740c | 10442 | } |
dbaa2011 | 10443 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
10444 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
10445 | isym->st_value = | |
10446 | _bfd_merged_section_offset (output_bfd, &isec, | |
10447 | elf_section_data (isec)->sec_info, | |
10448 | isym->st_value); | |
c152c796 AM |
10449 | } |
10450 | ||
10451 | *ppsection = isec; | |
10452 | ||
d983c8c5 AM |
10453 | /* Don't output the first, undefined, symbol. In fact, don't |
10454 | output any undefined local symbol. */ | |
10455 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
10456 | continue; |
10457 | ||
10458 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
10459 | { | |
10460 | /* We never output section symbols. Instead, we use the | |
10461 | section symbol of the corresponding section in the output | |
10462 | file. */ | |
10463 | continue; | |
10464 | } | |
10465 | ||
10466 | /* If we are stripping all symbols, we don't want to output this | |
10467 | one. */ | |
8b127cbc | 10468 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
10469 | continue; |
10470 | ||
10471 | /* If we are discarding all local symbols, we don't want to | |
10472 | output this one. If we are generating a relocatable output | |
10473 | file, then some of the local symbols may be required by | |
10474 | relocs; we output them below as we discover that they are | |
10475 | needed. */ | |
8b127cbc | 10476 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
10477 | continue; |
10478 | ||
10479 | /* If this symbol is defined in a section which we are | |
f02571c5 AM |
10480 | discarding, we don't need to keep it. */ |
10481 | if (isym->st_shndx != SHN_UNDEF | |
4fbb74a6 AM |
10482 | && isym->st_shndx < SHN_LORESERVE |
10483 | && bfd_section_removed_from_list (output_bfd, | |
10484 | isec->output_section)) | |
e75a280b L |
10485 | continue; |
10486 | ||
c152c796 AM |
10487 | /* Get the name of the symbol. */ |
10488 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
10489 | isym->st_name); | |
10490 | if (name == NULL) | |
10491 | return FALSE; | |
10492 | ||
10493 | /* See if we are discarding symbols with this name. */ | |
8b127cbc AM |
10494 | if ((flinfo->info->strip == strip_some |
10495 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) | |
c152c796 | 10496 | == NULL)) |
8b127cbc | 10497 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
10498 | && (isec->flags & SEC_MERGE) |
10499 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 10500 | || flinfo->info->discard == discard_l) |
c152c796 AM |
10501 | && bfd_is_local_label_name (input_bfd, name))) |
10502 | continue; | |
10503 | ||
ffbc01cc AM |
10504 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
10505 | { | |
ce875075 AM |
10506 | if (input_bfd->lto_output) |
10507 | /* -flto puts a temp file name here. This means builds | |
10508 | are not reproducible. Discard the symbol. */ | |
10509 | continue; | |
ffbc01cc AM |
10510 | have_file_sym = TRUE; |
10511 | flinfo->filesym_count += 1; | |
10512 | } | |
10513 | if (!have_file_sym) | |
10514 | { | |
10515 | /* In the absence of debug info, bfd_find_nearest_line uses | |
10516 | FILE symbols to determine the source file for local | |
10517 | function symbols. Provide a FILE symbol here if input | |
10518 | files lack such, so that their symbols won't be | |
10519 | associated with a previous input file. It's not the | |
10520 | source file, but the best we can do. */ | |
10521 | have_file_sym = TRUE; | |
10522 | flinfo->filesym_count += 1; | |
10523 | memset (&osym, 0, sizeof (osym)); | |
10524 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10525 | osym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10526 | if (!elf_link_output_symstrtab (flinfo, |
10527 | (input_bfd->lto_output ? NULL | |
10528 | : input_bfd->filename), | |
10529 | &osym, bfd_abs_section_ptr, | |
10530 | NULL)) | |
ffbc01cc AM |
10531 | return FALSE; |
10532 | } | |
10533 | ||
c152c796 AM |
10534 | osym = *isym; |
10535 | ||
10536 | /* Adjust the section index for the output file. */ | |
10537 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10538 | isec->output_section); | |
10539 | if (osym.st_shndx == SHN_BAD) | |
10540 | return FALSE; | |
10541 | ||
c152c796 AM |
10542 | /* ELF symbols in relocatable files are section relative, but |
10543 | in executable files they are virtual addresses. Note that | |
10544 | this code assumes that all ELF sections have an associated | |
10545 | BFD section with a reasonable value for output_offset; below | |
10546 | we assume that they also have a reasonable value for | |
10547 | output_section. Any special sections must be set up to meet | |
10548 | these requirements. */ | |
10549 | osym.st_value += isec->output_offset; | |
0e1862bb | 10550 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10551 | { |
10552 | osym.st_value += isec->output_section->vma; | |
10553 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
10554 | { | |
10555 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
102def4d AM |
10556 | if (elf_hash_table (flinfo->info)->tls_sec != NULL) |
10557 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
10558 | else | |
10559 | osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info), | |
10560 | STT_NOTYPE); | |
c152c796 AM |
10561 | } |
10562 | } | |
10563 | ||
6e0b88f1 | 10564 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 10565 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 10566 | if (ret == 0) |
c152c796 | 10567 | return FALSE; |
6e0b88f1 AM |
10568 | else if (ret == 1) |
10569 | *pindex = indx; | |
c152c796 AM |
10570 | } |
10571 | ||
310fd250 L |
10572 | if (bed->s->arch_size == 32) |
10573 | { | |
10574 | r_type_mask = 0xff; | |
10575 | r_sym_shift = 8; | |
10576 | address_size = 4; | |
10577 | } | |
10578 | else | |
10579 | { | |
10580 | r_type_mask = 0xffffffff; | |
10581 | r_sym_shift = 32; | |
10582 | address_size = 8; | |
10583 | } | |
10584 | ||
c152c796 AM |
10585 | /* Relocate the contents of each section. */ |
10586 | sym_hashes = elf_sym_hashes (input_bfd); | |
10587 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
10588 | { | |
10589 | bfd_byte *contents; | |
10590 | ||
10591 | if (! o->linker_mark) | |
10592 | { | |
10593 | /* This section was omitted from the link. */ | |
10594 | continue; | |
10595 | } | |
10596 | ||
7bdf4127 | 10597 | if (!flinfo->info->resolve_section_groups |
bcacc0f5 AM |
10598 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
10599 | { | |
10600 | /* Deal with the group signature symbol. */ | |
10601 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
10602 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
10603 | asection *osec = o->output_section; | |
10604 | ||
7bdf4127 | 10605 | BFD_ASSERT (bfd_link_relocatable (flinfo->info)); |
bcacc0f5 AM |
10606 | if (symndx >= locsymcount |
10607 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10608 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
10609 | { |
10610 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
10611 | while (h->root.type == bfd_link_hash_indirect | |
10612 | || h->root.type == bfd_link_hash_warning) | |
10613 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10614 | /* Arrange for symbol to be output. */ | |
10615 | h->indx = -2; | |
10616 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
10617 | } | |
10618 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
10619 | { | |
10620 | /* We'll use the output section target_index. */ | |
8b127cbc | 10621 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
10622 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
10623 | } | |
10624 | else | |
10625 | { | |
8b127cbc | 10626 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
10627 | { |
10628 | /* Otherwise output the local symbol now. */ | |
10629 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 10630 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 10631 | const char *name; |
6e0b88f1 AM |
10632 | long indx; |
10633 | int ret; | |
bcacc0f5 AM |
10634 | |
10635 | name = bfd_elf_string_from_elf_section (input_bfd, | |
10636 | symtab_hdr->sh_link, | |
10637 | sym.st_name); | |
10638 | if (name == NULL) | |
10639 | return FALSE; | |
10640 | ||
10641 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
10642 | sec); | |
10643 | if (sym.st_shndx == SHN_BAD) | |
10644 | return FALSE; | |
10645 | ||
10646 | sym.st_value += o->output_offset; | |
10647 | ||
6e0b88f1 | 10648 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
10649 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
10650 | NULL); | |
6e0b88f1 | 10651 | if (ret == 0) |
bcacc0f5 | 10652 | return FALSE; |
6e0b88f1 | 10653 | else if (ret == 1) |
8b127cbc | 10654 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
10655 | else |
10656 | abort (); | |
bcacc0f5 AM |
10657 | } |
10658 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 10659 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
10660 | } |
10661 | } | |
10662 | ||
c152c796 | 10663 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 10664 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
10665 | continue; |
10666 | ||
10667 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
10668 | { | |
10669 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
10670 | or somesuch. */ | |
10671 | continue; | |
10672 | } | |
10673 | ||
10674 | /* Get the contents of the section. They have been cached by a | |
10675 | relaxation routine. Note that o is a section in an input | |
10676 | file, so the contents field will not have been set by any of | |
10677 | the routines which work on output files. */ | |
10678 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
10679 | { |
10680 | contents = elf_section_data (o)->this_hdr.contents; | |
10681 | if (bed->caches_rawsize | |
10682 | && o->rawsize != 0 | |
10683 | && o->rawsize < o->size) | |
10684 | { | |
10685 | memcpy (flinfo->contents, contents, o->rawsize); | |
10686 | contents = flinfo->contents; | |
10687 | } | |
10688 | } | |
c152c796 AM |
10689 | else |
10690 | { | |
8b127cbc | 10691 | contents = flinfo->contents; |
4a114e3e | 10692 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
c152c796 AM |
10693 | return FALSE; |
10694 | } | |
10695 | ||
10696 | if ((o->flags & SEC_RELOC) != 0) | |
10697 | { | |
10698 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 10699 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 10700 | int action_discarded; |
ece5ef60 | 10701 | int ret; |
c152c796 AM |
10702 | |
10703 | /* Get the swapped relocs. */ | |
10704 | internal_relocs | |
8b127cbc AM |
10705 | = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, |
10706 | flinfo->internal_relocs, FALSE); | |
c152c796 AM |
10707 | if (internal_relocs == NULL |
10708 | && o->reloc_count > 0) | |
10709 | return FALSE; | |
10710 | ||
310fd250 L |
10711 | /* We need to reverse-copy input .ctors/.dtors sections if |
10712 | they are placed in .init_array/.finit_array for output. */ | |
10713 | if (o->size > address_size | |
10714 | && ((strncmp (o->name, ".ctors", 6) == 0 | |
10715 | && strcmp (o->output_section->name, | |
10716 | ".init_array") == 0) | |
10717 | || (strncmp (o->name, ".dtors", 6) == 0 | |
10718 | && strcmp (o->output_section->name, | |
10719 | ".fini_array") == 0)) | |
10720 | && (o->name[6] == 0 || o->name[6] == '.')) | |
c152c796 | 10721 | { |
056bafd4 MR |
10722 | if (o->size * bed->s->int_rels_per_ext_rel |
10723 | != o->reloc_count * address_size) | |
310fd250 | 10724 | { |
4eca0228 | 10725 | _bfd_error_handler |
695344c0 | 10726 | /* xgettext:c-format */ |
871b3ab2 | 10727 | (_("error: %pB: size of section %pA is not " |
310fd250 L |
10728 | "multiple of address size"), |
10729 | input_bfd, o); | |
8c6716e5 | 10730 | bfd_set_error (bfd_error_bad_value); |
310fd250 L |
10731 | return FALSE; |
10732 | } | |
10733 | o->flags |= SEC_ELF_REVERSE_COPY; | |
c152c796 AM |
10734 | } |
10735 | ||
0f02bbd9 | 10736 | action_discarded = -1; |
c152c796 | 10737 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
10738 | action_discarded = (*bed->action_discarded) (o); |
10739 | ||
10740 | /* Run through the relocs evaluating complex reloc symbols and | |
10741 | looking for relocs against symbols from discarded sections | |
10742 | or section symbols from removed link-once sections. | |
10743 | Complain about relocs against discarded sections. Zero | |
10744 | relocs against removed link-once sections. */ | |
10745 | ||
10746 | rel = internal_relocs; | |
056bafd4 | 10747 | relend = rel + o->reloc_count; |
0f02bbd9 | 10748 | for ( ; rel < relend; rel++) |
c152c796 | 10749 | { |
0f02bbd9 AM |
10750 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
10751 | unsigned int s_type; | |
10752 | asection **ps, *sec; | |
10753 | struct elf_link_hash_entry *h = NULL; | |
10754 | const char *sym_name; | |
c152c796 | 10755 | |
0f02bbd9 AM |
10756 | if (r_symndx == STN_UNDEF) |
10757 | continue; | |
c152c796 | 10758 | |
0f02bbd9 AM |
10759 | if (r_symndx >= locsymcount |
10760 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10761 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
10762 | { |
10763 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 10764 | |
0f02bbd9 AM |
10765 | /* Badly formatted input files can contain relocs that |
10766 | reference non-existant symbols. Check here so that | |
10767 | we do not seg fault. */ | |
10768 | if (h == NULL) | |
c152c796 | 10769 | { |
4eca0228 | 10770 | _bfd_error_handler |
695344c0 | 10771 | /* xgettext:c-format */ |
2dcf00ce | 10772 | (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA " |
0f02bbd9 | 10773 | "that references a non-existent global symbol"), |
2dcf00ce | 10774 | input_bfd, (uint64_t) rel->r_info, o); |
0f02bbd9 AM |
10775 | bfd_set_error (bfd_error_bad_value); |
10776 | return FALSE; | |
10777 | } | |
3b36f7e6 | 10778 | |
0f02bbd9 AM |
10779 | while (h->root.type == bfd_link_hash_indirect |
10780 | || h->root.type == bfd_link_hash_warning) | |
10781 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 10782 | |
0f02bbd9 | 10783 | s_type = h->type; |
cdd3575c | 10784 | |
9e2dec47 | 10785 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
10786 | mark the symbol as undefined. Note that the |
10787 | linker may attach linker created dynamic sections | |
10788 | to the plugin bfd. Symbols defined in linker | |
10789 | created sections are not plugin symbols. */ | |
bc4e12de | 10790 | if ((h->root.non_ir_ref_regular |
4070765b | 10791 | || h->root.non_ir_ref_dynamic) |
9e2dec47 L |
10792 | && (h->root.type == bfd_link_hash_defined |
10793 | || h->root.type == bfd_link_hash_defweak) | |
10794 | && (h->root.u.def.section->flags | |
10795 | & SEC_LINKER_CREATED) == 0 | |
10796 | && h->root.u.def.section->owner != NULL | |
10797 | && (h->root.u.def.section->owner->flags | |
10798 | & BFD_PLUGIN) != 0) | |
10799 | { | |
10800 | h->root.type = bfd_link_hash_undefined; | |
10801 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
10802 | } | |
10803 | ||
0f02bbd9 AM |
10804 | ps = NULL; |
10805 | if (h->root.type == bfd_link_hash_defined | |
10806 | || h->root.type == bfd_link_hash_defweak) | |
10807 | ps = &h->root.u.def.section; | |
10808 | ||
10809 | sym_name = h->root.root.string; | |
10810 | } | |
10811 | else | |
10812 | { | |
10813 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
10814 | ||
10815 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 10816 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
10817 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
10818 | sym, *ps); | |
10819 | } | |
c152c796 | 10820 | |
c301e700 | 10821 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 10822 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
10823 | { |
10824 | bfd_vma val; | |
10825 | bfd_vma dot = (rel->r_offset | |
10826 | + o->output_offset + o->output_section->vma); | |
10827 | #ifdef DEBUG | |
10828 | printf ("Encountered a complex symbol!"); | |
10829 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
9ccb8af9 AM |
10830 | input_bfd->filename, o->name, |
10831 | (long) (rel - internal_relocs)); | |
0f02bbd9 AM |
10832 | printf (" symbol: idx %8.8lx, name %s\n", |
10833 | r_symndx, sym_name); | |
10834 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
10835 | (unsigned long) rel->r_info, | |
10836 | (unsigned long) rel->r_offset); | |
10837 | #endif | |
8b127cbc | 10838 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 AM |
10839 | isymbuf, locsymcount, s_type == STT_SRELC)) |
10840 | return FALSE; | |
10841 | ||
10842 | /* Symbol evaluated OK. Update to absolute value. */ | |
10843 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
10844 | r_symndx, val); | |
10845 | continue; | |
10846 | } | |
10847 | ||
10848 | if (action_discarded != -1 && ps != NULL) | |
10849 | { | |
cdd3575c AM |
10850 | /* Complain if the definition comes from a |
10851 | discarded section. */ | |
dbaa2011 | 10852 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 10853 | { |
cf35638d | 10854 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 10855 | if (action_discarded & COMPLAIN) |
8b127cbc | 10856 | (*flinfo->info->callbacks->einfo) |
695344c0 | 10857 | /* xgettext:c-format */ |
871b3ab2 AM |
10858 | (_("%X`%s' referenced in section `%pA' of %pB: " |
10859 | "defined in discarded section `%pA' of %pB\n"), | |
e1fffbe6 | 10860 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 10861 | |
87e5235d | 10862 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 10863 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
10864 | really defined in the kept linkonce section. |
10865 | FIXME: This is quite broken. Modifying the | |
10866 | symbol here means we will be changing all later | |
e0ae6d6f | 10867 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 10868 | if (action_discarded & PRETEND) |
87e5235d | 10869 | { |
01b3c8ab L |
10870 | asection *kept; |
10871 | ||
c0f00686 | 10872 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 10873 | flinfo->info); |
01b3c8ab | 10874 | if (kept != NULL) |
87e5235d AM |
10875 | { |
10876 | *ps = kept; | |
10877 | continue; | |
10878 | } | |
10879 | } | |
c152c796 AM |
10880 | } |
10881 | } | |
10882 | } | |
10883 | ||
10884 | /* Relocate the section by invoking a back end routine. | |
10885 | ||
10886 | The back end routine is responsible for adjusting the | |
10887 | section contents as necessary, and (if using Rela relocs | |
10888 | and generating a relocatable output file) adjusting the | |
10889 | reloc addend as necessary. | |
10890 | ||
10891 | The back end routine does not have to worry about setting | |
10892 | the reloc address or the reloc symbol index. | |
10893 | ||
10894 | The back end routine is given a pointer to the swapped in | |
10895 | internal symbols, and can access the hash table entries | |
10896 | for the external symbols via elf_sym_hashes (input_bfd). | |
10897 | ||
10898 | When generating relocatable output, the back end routine | |
10899 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
10900 | output symbol is going to be a section symbol | |
10901 | corresponding to the output section, which will require | |
10902 | the addend to be adjusted. */ | |
10903 | ||
8b127cbc | 10904 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
10905 | input_bfd, o, contents, |
10906 | internal_relocs, | |
10907 | isymbuf, | |
8b127cbc | 10908 | flinfo->sections); |
ece5ef60 | 10909 | if (!ret) |
c152c796 AM |
10910 | return FALSE; |
10911 | ||
ece5ef60 | 10912 | if (ret == 2 |
0e1862bb | 10913 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 10914 | || flinfo->info->emitrelocations) |
c152c796 AM |
10915 | { |
10916 | Elf_Internal_Rela *irela; | |
d4730f92 | 10917 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
10918 | bfd_vma last_offset; |
10919 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
10920 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
10921 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 10922 | unsigned int next_erel; |
c152c796 | 10923 | bfd_boolean rela_normal; |
d4730f92 | 10924 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 10925 | |
d4730f92 BS |
10926 | esdi = elf_section_data (o); |
10927 | esdo = elf_section_data (o->output_section); | |
10928 | rela_normal = FALSE; | |
c152c796 AM |
10929 | |
10930 | /* Adjust the reloc addresses and symbol indices. */ | |
10931 | ||
10932 | irela = internal_relocs; | |
056bafd4 | 10933 | irelaend = irela + o->reloc_count; |
d4730f92 BS |
10934 | rel_hash = esdo->rel.hashes + esdo->rel.count; |
10935 | /* We start processing the REL relocs, if any. When we reach | |
10936 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
10937 | irelamid = irela; | |
10938 | if (esdi->rel.hdr != NULL) | |
10939 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
10940 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 10941 | rel_hash_list = rel_hash; |
d4730f92 | 10942 | rela_hash_list = NULL; |
c152c796 | 10943 | last_offset = o->output_offset; |
0e1862bb | 10944 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10945 | last_offset += o->output_section->vma; |
10946 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
10947 | { | |
10948 | unsigned long r_symndx; | |
10949 | asection *sec; | |
10950 | Elf_Internal_Sym sym; | |
10951 | ||
10952 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
10953 | { | |
10954 | rel_hash++; | |
10955 | next_erel = 0; | |
10956 | } | |
10957 | ||
d4730f92 BS |
10958 | if (irela == irelamid) |
10959 | { | |
10960 | rel_hash = esdo->rela.hashes + esdo->rela.count; | |
10961 | rela_hash_list = rel_hash; | |
10962 | rela_normal = bed->rela_normal; | |
10963 | } | |
10964 | ||
c152c796 | 10965 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 10966 | flinfo->info, o, |
c152c796 AM |
10967 | irela->r_offset); |
10968 | if (irela->r_offset >= (bfd_vma) -2) | |
10969 | { | |
10970 | /* This is a reloc for a deleted entry or somesuch. | |
10971 | Turn it into an R_*_NONE reloc, at the same | |
10972 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 10973 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
10974 | being ordered. */ |
10975 | irela->r_offset = last_offset; | |
10976 | irela->r_info = 0; | |
10977 | irela->r_addend = 0; | |
10978 | continue; | |
10979 | } | |
10980 | ||
10981 | irela->r_offset += o->output_offset; | |
10982 | ||
10983 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 10984 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10985 | irela->r_offset += o->output_section->vma; |
10986 | ||
10987 | last_offset = irela->r_offset; | |
10988 | ||
10989 | r_symndx = irela->r_info >> r_sym_shift; | |
10990 | if (r_symndx == STN_UNDEF) | |
10991 | continue; | |
10992 | ||
10993 | if (r_symndx >= locsymcount | |
10994 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 10995 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
10996 | { |
10997 | struct elf_link_hash_entry *rh; | |
10998 | unsigned long indx; | |
10999 | ||
11000 | /* This is a reloc against a global symbol. We | |
11001 | have not yet output all the local symbols, so | |
11002 | we do not know the symbol index of any global | |
11003 | symbol. We set the rel_hash entry for this | |
11004 | reloc to point to the global hash table entry | |
11005 | for this symbol. The symbol index is then | |
ee75fd95 | 11006 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
11007 | indx = r_symndx - extsymoff; |
11008 | rh = elf_sym_hashes (input_bfd)[indx]; | |
11009 | while (rh->root.type == bfd_link_hash_indirect | |
11010 | || rh->root.type == bfd_link_hash_warning) | |
11011 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
11012 | ||
11013 | /* Setting the index to -2 tells | |
11014 | elf_link_output_extsym that this symbol is | |
11015 | used by a reloc. */ | |
11016 | BFD_ASSERT (rh->indx < 0); | |
11017 | rh->indx = -2; | |
c152c796 AM |
11018 | *rel_hash = rh; |
11019 | ||
11020 | continue; | |
11021 | } | |
11022 | ||
11023 | /* This is a reloc against a local symbol. */ | |
11024 | ||
11025 | *rel_hash = NULL; | |
11026 | sym = isymbuf[r_symndx]; | |
8b127cbc | 11027 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
11028 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
11029 | { | |
11030 | /* I suppose the backend ought to fill in the | |
11031 | section of any STT_SECTION symbol against a | |
6a8d1586 | 11032 | processor specific section. */ |
cf35638d | 11033 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
11034 | if (bfd_is_abs_section (sec)) |
11035 | ; | |
c152c796 AM |
11036 | else if (sec == NULL || sec->owner == NULL) |
11037 | { | |
11038 | bfd_set_error (bfd_error_bad_value); | |
11039 | return FALSE; | |
11040 | } | |
11041 | else | |
11042 | { | |
6a8d1586 AM |
11043 | asection *osec = sec->output_section; |
11044 | ||
11045 | /* If we have discarded a section, the output | |
11046 | section will be the absolute section. In | |
ab96bf03 AM |
11047 | case of discarded SEC_MERGE sections, use |
11048 | the kept section. relocate_section should | |
11049 | have already handled discarded linkonce | |
11050 | sections. */ | |
6a8d1586 AM |
11051 | if (bfd_is_abs_section (osec) |
11052 | && sec->kept_section != NULL | |
11053 | && sec->kept_section->output_section != NULL) | |
11054 | { | |
11055 | osec = sec->kept_section->output_section; | |
11056 | irela->r_addend -= osec->vma; | |
11057 | } | |
11058 | ||
11059 | if (!bfd_is_abs_section (osec)) | |
11060 | { | |
11061 | r_symndx = osec->target_index; | |
cf35638d | 11062 | if (r_symndx == STN_UNDEF) |
74541ad4 | 11063 | { |
051d833a AM |
11064 | irela->r_addend += osec->vma; |
11065 | osec = _bfd_nearby_section (output_bfd, osec, | |
11066 | osec->vma); | |
11067 | irela->r_addend -= osec->vma; | |
11068 | r_symndx = osec->target_index; | |
74541ad4 | 11069 | } |
6a8d1586 | 11070 | } |
c152c796 AM |
11071 | } |
11072 | ||
11073 | /* Adjust the addend according to where the | |
11074 | section winds up in the output section. */ | |
11075 | if (rela_normal) | |
11076 | irela->r_addend += sec->output_offset; | |
11077 | } | |
11078 | else | |
11079 | { | |
8b127cbc | 11080 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
11081 | { |
11082 | unsigned long shlink; | |
11083 | const char *name; | |
11084 | asection *osec; | |
6e0b88f1 | 11085 | long indx; |
c152c796 | 11086 | |
8b127cbc | 11087 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
11088 | { |
11089 | /* You can't do ld -r -s. */ | |
11090 | bfd_set_error (bfd_error_invalid_operation); | |
11091 | return FALSE; | |
11092 | } | |
11093 | ||
11094 | /* This symbol was skipped earlier, but | |
11095 | since it is needed by a reloc, we | |
11096 | must output it now. */ | |
11097 | shlink = symtab_hdr->sh_link; | |
11098 | name = (bfd_elf_string_from_elf_section | |
11099 | (input_bfd, shlink, sym.st_name)); | |
11100 | if (name == NULL) | |
11101 | return FALSE; | |
11102 | ||
11103 | osec = sec->output_section; | |
11104 | sym.st_shndx = | |
11105 | _bfd_elf_section_from_bfd_section (output_bfd, | |
11106 | osec); | |
11107 | if (sym.st_shndx == SHN_BAD) | |
11108 | return FALSE; | |
11109 | ||
11110 | sym.st_value += sec->output_offset; | |
0e1862bb | 11111 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11112 | { |
11113 | sym.st_value += osec->vma; | |
11114 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
11115 | { | |
102def4d AM |
11116 | struct elf_link_hash_table *htab |
11117 | = elf_hash_table (flinfo->info); | |
11118 | ||
c152c796 AM |
11119 | /* STT_TLS symbols are relative to PT_TLS |
11120 | segment base. */ | |
102def4d AM |
11121 | if (htab->tls_sec != NULL) |
11122 | sym.st_value -= htab->tls_sec->vma; | |
11123 | else | |
11124 | sym.st_info | |
11125 | = ELF_ST_INFO (ELF_ST_BIND (sym.st_info), | |
11126 | STT_NOTYPE); | |
c152c796 AM |
11127 | } |
11128 | } | |
11129 | ||
6e0b88f1 | 11130 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
11131 | ret = elf_link_output_symstrtab (flinfo, name, |
11132 | &sym, sec, | |
11133 | NULL); | |
6e0b88f1 | 11134 | if (ret == 0) |
c152c796 | 11135 | return FALSE; |
6e0b88f1 | 11136 | else if (ret == 1) |
8b127cbc | 11137 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
11138 | else |
11139 | abort (); | |
c152c796 AM |
11140 | } |
11141 | ||
8b127cbc | 11142 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
11143 | } |
11144 | ||
11145 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
11146 | | (irela->r_info & r_type_mask)); | |
11147 | } | |
11148 | ||
11149 | /* Swap out the relocs. */ | |
d4730f92 BS |
11150 | input_rel_hdr = esdi->rel.hdr; |
11151 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 11152 | { |
d4730f92 BS |
11153 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
11154 | input_rel_hdr, | |
11155 | internal_relocs, | |
11156 | rel_hash_list)) | |
11157 | return FALSE; | |
c152c796 AM |
11158 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
11159 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 11160 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
11161 | } |
11162 | ||
11163 | input_rela_hdr = esdi->rela.hdr; | |
11164 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
11165 | { | |
eac338cf | 11166 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 11167 | input_rela_hdr, |
eac338cf | 11168 | internal_relocs, |
d4730f92 | 11169 | rela_hash_list)) |
c152c796 AM |
11170 | return FALSE; |
11171 | } | |
11172 | } | |
11173 | } | |
11174 | ||
11175 | /* Write out the modified section contents. */ | |
11176 | if (bed->elf_backend_write_section | |
8b127cbc | 11177 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 11178 | contents)) |
c152c796 AM |
11179 | { |
11180 | /* Section written out. */ | |
11181 | } | |
11182 | else switch (o->sec_info_type) | |
11183 | { | |
dbaa2011 | 11184 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
11185 | if (! (_bfd_write_section_stabs |
11186 | (output_bfd, | |
8b127cbc | 11187 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 AM |
11188 | o, &elf_section_data (o)->sec_info, contents))) |
11189 | return FALSE; | |
11190 | break; | |
dbaa2011 | 11191 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
11192 | if (! _bfd_write_merged_section (output_bfd, o, |
11193 | elf_section_data (o)->sec_info)) | |
11194 | return FALSE; | |
11195 | break; | |
dbaa2011 | 11196 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 11197 | { |
8b127cbc | 11198 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 AM |
11199 | o, contents)) |
11200 | return FALSE; | |
11201 | } | |
11202 | break; | |
2f0c68f2 CM |
11203 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
11204 | { | |
11205 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
11206 | flinfo->info, | |
11207 | o, contents)) | |
11208 | return FALSE; | |
11209 | } | |
11210 | break; | |
c152c796 AM |
11211 | default: |
11212 | { | |
310fd250 L |
11213 | if (! (o->flags & SEC_EXCLUDE)) |
11214 | { | |
11215 | file_ptr offset = (file_ptr) o->output_offset; | |
11216 | bfd_size_type todo = o->size; | |
37b01f6a DG |
11217 | |
11218 | offset *= bfd_octets_per_byte (output_bfd); | |
11219 | ||
310fd250 L |
11220 | if ((o->flags & SEC_ELF_REVERSE_COPY)) |
11221 | { | |
11222 | /* Reverse-copy input section to output. */ | |
11223 | do | |
11224 | { | |
11225 | todo -= address_size; | |
11226 | if (! bfd_set_section_contents (output_bfd, | |
11227 | o->output_section, | |
11228 | contents + todo, | |
11229 | offset, | |
11230 | address_size)) | |
11231 | return FALSE; | |
11232 | if (todo == 0) | |
11233 | break; | |
11234 | offset += address_size; | |
11235 | } | |
11236 | while (1); | |
11237 | } | |
11238 | else if (! bfd_set_section_contents (output_bfd, | |
11239 | o->output_section, | |
11240 | contents, | |
11241 | offset, todo)) | |
11242 | return FALSE; | |
11243 | } | |
c152c796 AM |
11244 | } |
11245 | break; | |
11246 | } | |
11247 | } | |
11248 | ||
11249 | return TRUE; | |
11250 | } | |
11251 | ||
11252 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 11253 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
11254 | is used to build constructor and destructor tables when linking |
11255 | with -Ur. */ | |
11256 | ||
11257 | static bfd_boolean | |
11258 | elf_reloc_link_order (bfd *output_bfd, | |
11259 | struct bfd_link_info *info, | |
11260 | asection *output_section, | |
11261 | struct bfd_link_order *link_order) | |
11262 | { | |
11263 | reloc_howto_type *howto; | |
11264 | long indx; | |
11265 | bfd_vma offset; | |
11266 | bfd_vma addend; | |
d4730f92 | 11267 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
11268 | struct elf_link_hash_entry **rel_hash_ptr; |
11269 | Elf_Internal_Shdr *rel_hdr; | |
11270 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
11271 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
11272 | bfd_byte *erel; | |
11273 | unsigned int i; | |
d4730f92 | 11274 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
11275 | |
11276 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
11277 | if (howto == NULL) | |
11278 | { | |
11279 | bfd_set_error (bfd_error_bad_value); | |
11280 | return FALSE; | |
11281 | } | |
11282 | ||
11283 | addend = link_order->u.reloc.p->addend; | |
11284 | ||
d4730f92 BS |
11285 | if (esdo->rel.hdr) |
11286 | reldata = &esdo->rel; | |
11287 | else if (esdo->rela.hdr) | |
11288 | reldata = &esdo->rela; | |
11289 | else | |
11290 | { | |
11291 | reldata = NULL; | |
11292 | BFD_ASSERT (0); | |
11293 | } | |
11294 | ||
c152c796 | 11295 | /* Figure out the symbol index. */ |
d4730f92 | 11296 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
11297 | if (link_order->type == bfd_section_reloc_link_order) |
11298 | { | |
11299 | indx = link_order->u.reloc.p->u.section->target_index; | |
11300 | BFD_ASSERT (indx != 0); | |
11301 | *rel_hash_ptr = NULL; | |
11302 | } | |
11303 | else | |
11304 | { | |
11305 | struct elf_link_hash_entry *h; | |
11306 | ||
11307 | /* Treat a reloc against a defined symbol as though it were | |
11308 | actually against the section. */ | |
11309 | h = ((struct elf_link_hash_entry *) | |
11310 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
11311 | link_order->u.reloc.p->u.name, | |
11312 | FALSE, FALSE, TRUE)); | |
11313 | if (h != NULL | |
11314 | && (h->root.type == bfd_link_hash_defined | |
11315 | || h->root.type == bfd_link_hash_defweak)) | |
11316 | { | |
11317 | asection *section; | |
11318 | ||
11319 | section = h->root.u.def.section; | |
11320 | indx = section->output_section->target_index; | |
11321 | *rel_hash_ptr = NULL; | |
11322 | /* It seems that we ought to add the symbol value to the | |
11323 | addend here, but in practice it has already been added | |
11324 | because it was passed to constructor_callback. */ | |
11325 | addend += section->output_section->vma + section->output_offset; | |
11326 | } | |
11327 | else if (h != NULL) | |
11328 | { | |
11329 | /* Setting the index to -2 tells elf_link_output_extsym that | |
11330 | this symbol is used by a reloc. */ | |
11331 | h->indx = -2; | |
11332 | *rel_hash_ptr = h; | |
11333 | indx = 0; | |
11334 | } | |
11335 | else | |
11336 | { | |
1a72702b AM |
11337 | (*info->callbacks->unattached_reloc) |
11338 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
11339 | indx = 0; |
11340 | } | |
11341 | } | |
11342 | ||
11343 | /* If this is an inplace reloc, we must write the addend into the | |
11344 | object file. */ | |
11345 | if (howto->partial_inplace && addend != 0) | |
11346 | { | |
11347 | bfd_size_type size; | |
11348 | bfd_reloc_status_type rstat; | |
11349 | bfd_byte *buf; | |
11350 | bfd_boolean ok; | |
11351 | const char *sym_name; | |
11352 | ||
a50b1753 NC |
11353 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
11354 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 11355 | if (buf == NULL && size != 0) |
c152c796 AM |
11356 | return FALSE; |
11357 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
11358 | switch (rstat) | |
11359 | { | |
11360 | case bfd_reloc_ok: | |
11361 | break; | |
11362 | ||
11363 | default: | |
11364 | case bfd_reloc_outofrange: | |
11365 | abort (); | |
11366 | ||
11367 | case bfd_reloc_overflow: | |
11368 | if (link_order->type == bfd_section_reloc_link_order) | |
11369 | sym_name = bfd_section_name (output_bfd, | |
11370 | link_order->u.reloc.p->u.section); | |
11371 | else | |
11372 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
11373 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
11374 | howto->name, addend, NULL, NULL, | |
11375 | (bfd_vma) 0); | |
c152c796 AM |
11376 | break; |
11377 | } | |
37b01f6a | 11378 | |
c152c796 | 11379 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
37b01f6a DG |
11380 | link_order->offset |
11381 | * bfd_octets_per_byte (output_bfd), | |
11382 | size); | |
c152c796 AM |
11383 | free (buf); |
11384 | if (! ok) | |
11385 | return FALSE; | |
11386 | } | |
11387 | ||
11388 | /* The address of a reloc is relative to the section in a | |
11389 | relocatable file, and is a virtual address in an executable | |
11390 | file. */ | |
11391 | offset = link_order->offset; | |
0e1862bb | 11392 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
11393 | offset += output_section->vma; |
11394 | ||
11395 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
11396 | { | |
11397 | irel[i].r_offset = offset; | |
11398 | irel[i].r_info = 0; | |
11399 | irel[i].r_addend = 0; | |
11400 | } | |
11401 | if (bed->s->arch_size == 32) | |
11402 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
11403 | else | |
11404 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
11405 | ||
d4730f92 | 11406 | rel_hdr = reldata->hdr; |
c152c796 AM |
11407 | erel = rel_hdr->contents; |
11408 | if (rel_hdr->sh_type == SHT_REL) | |
11409 | { | |
d4730f92 | 11410 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
11411 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
11412 | } | |
11413 | else | |
11414 | { | |
11415 | irel[0].r_addend = addend; | |
d4730f92 | 11416 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
11417 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
11418 | } | |
11419 | ||
d4730f92 | 11420 | ++reldata->count; |
c152c796 AM |
11421 | |
11422 | return TRUE; | |
11423 | } | |
11424 | ||
0b52efa6 PB |
11425 | |
11426 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
11427 | ||
11428 | static bfd_vma | |
11429 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
11430 | { | |
11431 | Elf_Internal_Shdr **elf_shdrp; | |
11432 | asection *s; | |
11433 | int elfsec; | |
11434 | ||
11435 | s = p->u.indirect.section; | |
11436 | elf_shdrp = elf_elfsections (s->owner); | |
11437 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
11438 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
11439 | /* PR 290: |
11440 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
e04bcc6d | 11441 | SHF_LINK_ORDER. But it doesn't set the sh_link or |
185d09ad L |
11442 | sh_info fields. Hence we could get the situation |
11443 | where elfsec is 0. */ | |
11444 | if (elfsec == 0) | |
11445 | { | |
11446 | const struct elf_backend_data *bed | |
11447 | = get_elf_backend_data (s->owner); | |
11448 | if (bed->link_order_error_handler) | |
d003868e | 11449 | bed->link_order_error_handler |
695344c0 | 11450 | /* xgettext:c-format */ |
871b3ab2 | 11451 | (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s); |
185d09ad L |
11452 | return 0; |
11453 | } | |
11454 | else | |
11455 | { | |
11456 | s = elf_shdrp[elfsec]->bfd_section; | |
11457 | return s->output_section->vma + s->output_offset; | |
11458 | } | |
0b52efa6 PB |
11459 | } |
11460 | ||
11461 | ||
11462 | /* Compare two sections based on the locations of the sections they are | |
11463 | linked to. Used by elf_fixup_link_order. */ | |
11464 | ||
11465 | static int | |
11466 | compare_link_order (const void * a, const void * b) | |
11467 | { | |
11468 | bfd_vma apos; | |
11469 | bfd_vma bpos; | |
11470 | ||
11471 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
11472 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
11473 | if (apos < bpos) | |
11474 | return -1; | |
11475 | return apos > bpos; | |
11476 | } | |
11477 | ||
11478 | ||
11479 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
11480 | order as their linked sections. Returns false if this could not be done | |
11481 | because an output section includes both ordered and unordered | |
11482 | sections. Ideally we'd do this in the linker proper. */ | |
11483 | ||
11484 | static bfd_boolean | |
11485 | elf_fixup_link_order (bfd *abfd, asection *o) | |
11486 | { | |
11487 | int seen_linkorder; | |
11488 | int seen_other; | |
11489 | int n; | |
11490 | struct bfd_link_order *p; | |
11491 | bfd *sub; | |
11492 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b761a207 | 11493 | unsigned elfsec; |
0b52efa6 | 11494 | struct bfd_link_order **sections; |
d33cdfe3 | 11495 | asection *s, *other_sec, *linkorder_sec; |
0b52efa6 | 11496 | bfd_vma offset; |
3b36f7e6 | 11497 | |
d33cdfe3 L |
11498 | other_sec = NULL; |
11499 | linkorder_sec = NULL; | |
0b52efa6 PB |
11500 | seen_other = 0; |
11501 | seen_linkorder = 0; | |
8423293d | 11502 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 | 11503 | { |
d33cdfe3 | 11504 | if (p->type == bfd_indirect_link_order) |
0b52efa6 PB |
11505 | { |
11506 | s = p->u.indirect.section; | |
d33cdfe3 L |
11507 | sub = s->owner; |
11508 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
11509 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass | |
b761a207 BE |
11510 | && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) |
11511 | && elfsec < elf_numsections (sub) | |
4fbb74a6 AM |
11512 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER |
11513 | && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) | |
d33cdfe3 L |
11514 | { |
11515 | seen_linkorder++; | |
11516 | linkorder_sec = s; | |
11517 | } | |
0b52efa6 | 11518 | else |
d33cdfe3 L |
11519 | { |
11520 | seen_other++; | |
11521 | other_sec = s; | |
11522 | } | |
0b52efa6 PB |
11523 | } |
11524 | else | |
11525 | seen_other++; | |
d33cdfe3 L |
11526 | |
11527 | if (seen_other && seen_linkorder) | |
11528 | { | |
11529 | if (other_sec && linkorder_sec) | |
4eca0228 | 11530 | _bfd_error_handler |
695344c0 | 11531 | /* xgettext:c-format */ |
871b3ab2 AM |
11532 | (_("%pA has both ordered [`%pA' in %pB] " |
11533 | "and unordered [`%pA' in %pB] sections"), | |
63a5468a AM |
11534 | o, linkorder_sec, linkorder_sec->owner, |
11535 | other_sec, other_sec->owner); | |
d33cdfe3 | 11536 | else |
4eca0228 | 11537 | _bfd_error_handler |
871b3ab2 | 11538 | (_("%pA has both ordered and unordered sections"), o); |
d33cdfe3 L |
11539 | bfd_set_error (bfd_error_bad_value); |
11540 | return FALSE; | |
11541 | } | |
0b52efa6 PB |
11542 | } |
11543 | ||
11544 | if (!seen_linkorder) | |
11545 | return TRUE; | |
11546 | ||
0b52efa6 | 11547 | sections = (struct bfd_link_order **) |
14b1c01e AM |
11548 | bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); |
11549 | if (sections == NULL) | |
11550 | return FALSE; | |
0b52efa6 | 11551 | seen_linkorder = 0; |
3b36f7e6 | 11552 | |
8423293d | 11553 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
0b52efa6 PB |
11554 | { |
11555 | sections[seen_linkorder++] = p; | |
11556 | } | |
11557 | /* Sort the input sections in the order of their linked section. */ | |
11558 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
11559 | compare_link_order); | |
11560 | ||
11561 | /* Change the offsets of the sections. */ | |
11562 | offset = 0; | |
11563 | for (n = 0; n < seen_linkorder; n++) | |
11564 | { | |
11565 | s = sections[n]->u.indirect.section; | |
461686a3 | 11566 | offset &= ~(bfd_vma) 0 << s->alignment_power; |
37b01f6a | 11567 | s->output_offset = offset / bfd_octets_per_byte (abfd); |
0b52efa6 PB |
11568 | sections[n]->offset = offset; |
11569 | offset += sections[n]->size; | |
11570 | } | |
11571 | ||
4dd07732 | 11572 | free (sections); |
0b52efa6 PB |
11573 | return TRUE; |
11574 | } | |
11575 | ||
76359541 TP |
11576 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
11577 | Returns TRUE upon success, FALSE otherwise. */ | |
11578 | ||
11579 | static bfd_boolean | |
11580 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) | |
11581 | { | |
11582 | bfd_boolean ret = FALSE; | |
11583 | bfd *implib_bfd; | |
11584 | const struct elf_backend_data *bed; | |
11585 | flagword flags; | |
11586 | enum bfd_architecture arch; | |
11587 | unsigned int mach; | |
11588 | asymbol **sympp = NULL; | |
11589 | long symsize; | |
11590 | long symcount; | |
11591 | long src_count; | |
11592 | elf_symbol_type *osymbuf; | |
11593 | ||
11594 | implib_bfd = info->out_implib_bfd; | |
11595 | bed = get_elf_backend_data (abfd); | |
11596 | ||
11597 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
11598 | return FALSE; | |
11599 | ||
046734ff | 11600 | /* Use flag from executable but make it a relocatable object. */ |
76359541 TP |
11601 | flags = bfd_get_file_flags (abfd); |
11602 | flags &= ~HAS_RELOC; | |
11603 | if (!bfd_set_start_address (implib_bfd, 0) | |
046734ff | 11604 | || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) |
76359541 TP |
11605 | return FALSE; |
11606 | ||
11607 | /* Copy architecture of output file to import library file. */ | |
11608 | arch = bfd_get_arch (abfd); | |
11609 | mach = bfd_get_mach (abfd); | |
11610 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
11611 | && (abfd->target_defaulted | |
11612 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
11613 | return FALSE; | |
11614 | ||
11615 | /* Get symbol table size. */ | |
11616 | symsize = bfd_get_symtab_upper_bound (abfd); | |
11617 | if (symsize < 0) | |
11618 | return FALSE; | |
11619 | ||
11620 | /* Read in the symbol table. */ | |
11621 | sympp = (asymbol **) xmalloc (symsize); | |
11622 | symcount = bfd_canonicalize_symtab (abfd, sympp); | |
11623 | if (symcount < 0) | |
11624 | goto free_sym_buf; | |
11625 | ||
11626 | /* Allow the BFD backend to copy any private header data it | |
11627 | understands from the output BFD to the import library BFD. */ | |
11628 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
11629 | goto free_sym_buf; | |
11630 | ||
11631 | /* Filter symbols to appear in the import library. */ | |
11632 | if (bed->elf_backend_filter_implib_symbols) | |
11633 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
11634 | symcount); | |
11635 | else | |
11636 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
11637 | if (symcount == 0) | |
11638 | { | |
5df1bc57 | 11639 | bfd_set_error (bfd_error_no_symbols); |
871b3ab2 | 11640 | _bfd_error_handler (_("%pB: no symbol found for import library"), |
4eca0228 | 11641 | implib_bfd); |
76359541 TP |
11642 | goto free_sym_buf; |
11643 | } | |
11644 | ||
11645 | ||
11646 | /* Make symbols absolute. */ | |
11647 | osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, | |
11648 | sizeof (*osymbuf)); | |
11649 | for (src_count = 0; src_count < symcount; src_count++) | |
11650 | { | |
11651 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
11652 | sizeof (*osymbuf)); | |
11653 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
11654 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
11655 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
11656 | osymbuf[src_count].internal_elf_sym.st_value = | |
11657 | osymbuf[src_count].symbol.value; | |
11658 | sympp[src_count] = &osymbuf[src_count].symbol; | |
11659 | } | |
11660 | ||
11661 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
11662 | ||
11663 | /* Allow the BFD backend to copy any private data it understands | |
11664 | from the output BFD to the import library BFD. This is done last | |
11665 | to permit the routine to look at the filtered symbol table. */ | |
11666 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
11667 | goto free_sym_buf; | |
11668 | ||
11669 | if (!bfd_close (implib_bfd)) | |
11670 | goto free_sym_buf; | |
11671 | ||
11672 | ret = TRUE; | |
11673 | ||
11674 | free_sym_buf: | |
11675 | free (sympp); | |
11676 | return ret; | |
11677 | } | |
11678 | ||
9f7c3e5e AM |
11679 | static void |
11680 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
11681 | { | |
11682 | asection *o; | |
11683 | ||
11684 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 11685 | _bfd_elf_strtab_free (flinfo->symstrtab); |
9f7c3e5e AM |
11686 | if (flinfo->contents != NULL) |
11687 | free (flinfo->contents); | |
11688 | if (flinfo->external_relocs != NULL) | |
11689 | free (flinfo->external_relocs); | |
11690 | if (flinfo->internal_relocs != NULL) | |
11691 | free (flinfo->internal_relocs); | |
11692 | if (flinfo->external_syms != NULL) | |
11693 | free (flinfo->external_syms); | |
11694 | if (flinfo->locsym_shndx != NULL) | |
11695 | free (flinfo->locsym_shndx); | |
11696 | if (flinfo->internal_syms != NULL) | |
11697 | free (flinfo->internal_syms); | |
11698 | if (flinfo->indices != NULL) | |
11699 | free (flinfo->indices); | |
11700 | if (flinfo->sections != NULL) | |
11701 | free (flinfo->sections); | |
a0f6fd21 AM |
11702 | if (flinfo->symshndxbuf != NULL |
11703 | && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1) | |
9f7c3e5e AM |
11704 | free (flinfo->symshndxbuf); |
11705 | for (o = obfd->sections; o != NULL; o = o->next) | |
11706 | { | |
11707 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11708 | if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) | |
11709 | free (esdo->rel.hashes); | |
11710 | if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) | |
11711 | free (esdo->rela.hashes); | |
11712 | } | |
11713 | } | |
0b52efa6 | 11714 | |
c152c796 AM |
11715 | /* Do the final step of an ELF link. */ |
11716 | ||
11717 | bfd_boolean | |
11718 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
11719 | { | |
11720 | bfd_boolean dynamic; | |
11721 | bfd_boolean emit_relocs; | |
11722 | bfd *dynobj; | |
8b127cbc | 11723 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
11724 | asection *o; |
11725 | struct bfd_link_order *p; | |
11726 | bfd *sub; | |
c152c796 AM |
11727 | bfd_size_type max_contents_size; |
11728 | bfd_size_type max_external_reloc_size; | |
11729 | bfd_size_type max_internal_reloc_count; | |
11730 | bfd_size_type max_sym_count; | |
11731 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
11732 | Elf_Internal_Sym elfsym; |
11733 | unsigned int i; | |
11734 | Elf_Internal_Shdr *symtab_hdr; | |
11735 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
11736 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11737 | struct elf_outext_info eoinfo; | |
11738 | bfd_boolean merged; | |
11739 | size_t relativecount = 0; | |
11740 | asection *reldyn = 0; | |
11741 | bfd_size_type amt; | |
104d59d1 JM |
11742 | asection *attr_section = NULL; |
11743 | bfd_vma attr_size = 0; | |
11744 | const char *std_attrs_section; | |
64f52338 | 11745 | struct elf_link_hash_table *htab = elf_hash_table (info); |
c152c796 | 11746 | |
64f52338 | 11747 | if (!is_elf_hash_table (htab)) |
c152c796 AM |
11748 | return FALSE; |
11749 | ||
0e1862bb | 11750 | if (bfd_link_pic (info)) |
c152c796 AM |
11751 | abfd->flags |= DYNAMIC; |
11752 | ||
64f52338 AM |
11753 | dynamic = htab->dynamic_sections_created; |
11754 | dynobj = htab->dynobj; | |
c152c796 | 11755 | |
0e1862bb | 11756 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 11757 | || info->emitrelocations); |
c152c796 | 11758 | |
8b127cbc AM |
11759 | flinfo.info = info; |
11760 | flinfo.output_bfd = abfd; | |
ef10c3ac | 11761 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 11762 | if (flinfo.symstrtab == NULL) |
c152c796 AM |
11763 | return FALSE; |
11764 | ||
11765 | if (! dynamic) | |
11766 | { | |
8b127cbc AM |
11767 | flinfo.hash_sec = NULL; |
11768 | flinfo.symver_sec = NULL; | |
c152c796 AM |
11769 | } |
11770 | else | |
11771 | { | |
3d4d4302 | 11772 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 11773 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 11774 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
11775 | /* Note that it is OK if symver_sec is NULL. */ |
11776 | } | |
11777 | ||
8b127cbc AM |
11778 | flinfo.contents = NULL; |
11779 | flinfo.external_relocs = NULL; | |
11780 | flinfo.internal_relocs = NULL; | |
11781 | flinfo.external_syms = NULL; | |
11782 | flinfo.locsym_shndx = NULL; | |
11783 | flinfo.internal_syms = NULL; | |
11784 | flinfo.indices = NULL; | |
11785 | flinfo.sections = NULL; | |
8b127cbc | 11786 | flinfo.symshndxbuf = NULL; |
ffbc01cc | 11787 | flinfo.filesym_count = 0; |
c152c796 | 11788 | |
104d59d1 JM |
11789 | /* The object attributes have been merged. Remove the input |
11790 | sections from the link, and set the contents of the output | |
11791 | secton. */ | |
11792 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; | |
11793 | for (o = abfd->sections; o != NULL; o = o->next) | |
11794 | { | |
5270eddc | 11795 | bfd_boolean remove_section = FALSE; |
b8a6ced7 | 11796 | |
104d59d1 JM |
11797 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) |
11798 | || strcmp (o->name, ".gnu.attributes") == 0) | |
11799 | { | |
11800 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
11801 | { | |
11802 | asection *input_section; | |
11803 | ||
11804 | if (p->type != bfd_indirect_link_order) | |
11805 | continue; | |
11806 | input_section = p->u.indirect.section; | |
11807 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
11808 | elf_link_input_bfd ignores this section. */ | |
11809 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
11810 | } | |
a0c8462f | 11811 | |
104d59d1 | 11812 | attr_size = bfd_elf_obj_attr_size (abfd); |
b8a6ced7 AM |
11813 | bfd_set_section_size (abfd, o, attr_size); |
11814 | /* Skip this section later on. */ | |
11815 | o->map_head.link_order = NULL; | |
104d59d1 | 11816 | if (attr_size) |
b8a6ced7 | 11817 | attr_section = o; |
104d59d1 | 11818 | else |
5270eddc | 11819 | remove_section = TRUE; |
104d59d1 | 11820 | } |
6e5e9d58 AM |
11821 | else if ((o->flags & SEC_GROUP) != 0 && o->size == 0) |
11822 | { | |
11823 | /* Remove empty group section from linker output. */ | |
5270eddc | 11824 | remove_section = TRUE; |
b8a6ced7 | 11825 | } |
5270eddc | 11826 | if (remove_section) |
b8a6ced7 | 11827 | { |
6e5e9d58 AM |
11828 | o->flags |= SEC_EXCLUDE; |
11829 | bfd_section_list_remove (abfd, o); | |
11830 | abfd->section_count--; | |
11831 | } | |
104d59d1 JM |
11832 | } |
11833 | ||
c152c796 AM |
11834 | /* Count up the number of relocations we will output for each output |
11835 | section, so that we know the sizes of the reloc sections. We | |
11836 | also figure out some maximum sizes. */ | |
11837 | max_contents_size = 0; | |
11838 | max_external_reloc_size = 0; | |
11839 | max_internal_reloc_count = 0; | |
11840 | max_sym_count = 0; | |
11841 | max_sym_shndx_count = 0; | |
11842 | merged = FALSE; | |
11843 | for (o = abfd->sections; o != NULL; o = o->next) | |
11844 | { | |
11845 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
11846 | o->reloc_count = 0; | |
11847 | ||
8423293d | 11848 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
11849 | { |
11850 | unsigned int reloc_count = 0; | |
9eaff861 | 11851 | unsigned int additional_reloc_count = 0; |
c152c796 | 11852 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
11853 | |
11854 | if (p->type == bfd_section_reloc_link_order | |
11855 | || p->type == bfd_symbol_reloc_link_order) | |
11856 | reloc_count = 1; | |
11857 | else if (p->type == bfd_indirect_link_order) | |
11858 | { | |
11859 | asection *sec; | |
11860 | ||
11861 | sec = p->u.indirect.section; | |
c152c796 AM |
11862 | |
11863 | /* Mark all sections which are to be included in the | |
11864 | link. This will normally be every section. We need | |
11865 | to do this so that we can identify any sections which | |
11866 | the linker has decided to not include. */ | |
11867 | sec->linker_mark = TRUE; | |
11868 | ||
11869 | if (sec->flags & SEC_MERGE) | |
11870 | merged = TRUE; | |
11871 | ||
eea6121a AM |
11872 | if (sec->rawsize > max_contents_size) |
11873 | max_contents_size = sec->rawsize; | |
11874 | if (sec->size > max_contents_size) | |
11875 | max_contents_size = sec->size; | |
c152c796 | 11876 | |
c152c796 AM |
11877 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
11878 | && (sec->owner->flags & DYNAMIC) == 0) | |
11879 | { | |
11880 | size_t sym_count; | |
11881 | ||
a961cdd5 AM |
11882 | /* We are interested in just local symbols, not all |
11883 | symbols. */ | |
c152c796 AM |
11884 | if (elf_bad_symtab (sec->owner)) |
11885 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
11886 | / bed->s->sizeof_sym); | |
11887 | else | |
11888 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
11889 | ||
11890 | if (sym_count > max_sym_count) | |
11891 | max_sym_count = sym_count; | |
11892 | ||
11893 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 11894 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
11895 | max_sym_shndx_count = sym_count; |
11896 | ||
a961cdd5 AM |
11897 | if (esdo->this_hdr.sh_type == SHT_REL |
11898 | || esdo->this_hdr.sh_type == SHT_RELA) | |
11899 | /* Some backends use reloc_count in relocation sections | |
11900 | to count particular types of relocs. Of course, | |
11901 | reloc sections themselves can't have relocations. */ | |
11902 | ; | |
11903 | else if (emit_relocs) | |
11904 | { | |
11905 | reloc_count = sec->reloc_count; | |
11906 | if (bed->elf_backend_count_additional_relocs) | |
11907 | { | |
11908 | int c; | |
11909 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
11910 | additional_reloc_count += c; | |
11911 | } | |
11912 | } | |
11913 | else if (bed->elf_backend_count_relocs) | |
11914 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
11915 | ||
11916 | esdi = elf_section_data (sec); | |
11917 | ||
c152c796 AM |
11918 | if ((sec->flags & SEC_RELOC) != 0) |
11919 | { | |
d4730f92 | 11920 | size_t ext_size = 0; |
c152c796 | 11921 | |
d4730f92 BS |
11922 | if (esdi->rel.hdr != NULL) |
11923 | ext_size = esdi->rel.hdr->sh_size; | |
11924 | if (esdi->rela.hdr != NULL) | |
11925 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 11926 | |
c152c796 AM |
11927 | if (ext_size > max_external_reloc_size) |
11928 | max_external_reloc_size = ext_size; | |
11929 | if (sec->reloc_count > max_internal_reloc_count) | |
11930 | max_internal_reloc_count = sec->reloc_count; | |
11931 | } | |
11932 | } | |
11933 | } | |
11934 | ||
11935 | if (reloc_count == 0) | |
11936 | continue; | |
11937 | ||
9eaff861 | 11938 | reloc_count += additional_reloc_count; |
c152c796 AM |
11939 | o->reloc_count += reloc_count; |
11940 | ||
0e1862bb | 11941 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 11942 | { |
d4730f92 | 11943 | if (esdi->rel.hdr) |
9eaff861 | 11944 | { |
491d01d3 | 11945 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
11946 | esdo->rel.count += additional_reloc_count; |
11947 | } | |
d4730f92 | 11948 | if (esdi->rela.hdr) |
9eaff861 | 11949 | { |
491d01d3 | 11950 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
11951 | esdo->rela.count += additional_reloc_count; |
11952 | } | |
d4730f92 BS |
11953 | } |
11954 | else | |
11955 | { | |
11956 | if (o->use_rela_p) | |
11957 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 11958 | else |
d4730f92 | 11959 | esdo->rel.count += reloc_count; |
c152c796 | 11960 | } |
c152c796 AM |
11961 | } |
11962 | ||
9eaff861 | 11963 | if (o->reloc_count > 0) |
c152c796 AM |
11964 | o->flags |= SEC_RELOC; |
11965 | else | |
11966 | { | |
11967 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
11968 | set it (this is probably a bug) and if it is set | |
11969 | assign_section_numbers will create a reloc section. */ | |
11970 | o->flags &=~ SEC_RELOC; | |
11971 | } | |
11972 | ||
11973 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
11974 | zero. This is done in elf_fake_sections as well, but forcing | |
11975 | the VMA to 0 here will ensure that relocs against these | |
11976 | sections are handled correctly. */ | |
11977 | if ((o->flags & SEC_ALLOC) == 0 | |
11978 | && ! o->user_set_vma) | |
11979 | o->vma = 0; | |
11980 | } | |
11981 | ||
0e1862bb | 11982 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 11983 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
11984 | |
11985 | /* Figure out the file positions for everything but the symbol table | |
11986 | and the relocs. We set symcount to force assign_section_numbers | |
11987 | to create a symbol table. */ | |
8539e4e8 | 11988 | bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; |
c152c796 AM |
11989 | BFD_ASSERT (! abfd->output_has_begun); |
11990 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
11991 | goto error_return; | |
11992 | ||
ee75fd95 | 11993 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
11994 | for (o = abfd->sections; o != NULL; o = o->next) |
11995 | { | |
d4730f92 | 11996 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
11997 | if ((o->flags & SEC_RELOC) != 0) |
11998 | { | |
d4730f92 | 11999 | if (esdo->rel.hdr |
9eaff861 | 12000 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
12001 | goto error_return; |
12002 | ||
d4730f92 | 12003 | if (esdo->rela.hdr |
9eaff861 | 12004 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
12005 | goto error_return; |
12006 | } | |
12007 | ||
12008 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
12009 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
12010 | esdo->rel.count = 0; |
12011 | esdo->rela.count = 0; | |
0ce398f1 L |
12012 | |
12013 | if (esdo->this_hdr.sh_offset == (file_ptr) -1) | |
12014 | { | |
12015 | /* Cache the section contents so that they can be compressed | |
12016 | later. Use bfd_malloc since it will be freed by | |
12017 | bfd_compress_section_contents. */ | |
12018 | unsigned char *contents = esdo->this_hdr.contents; | |
12019 | if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) | |
12020 | abort (); | |
12021 | contents | |
12022 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
12023 | if (contents == NULL) | |
12024 | goto error_return; | |
12025 | esdo->this_hdr.contents = contents; | |
12026 | } | |
c152c796 AM |
12027 | } |
12028 | ||
c152c796 | 12029 | /* We have now assigned file positions for all the sections except |
a485e98e AM |
12030 | .symtab, .strtab, and non-loaded reloc sections. We start the |
12031 | .symtab section at the current file position, and write directly | |
12032 | to it. We build the .strtab section in memory. */ | |
c152c796 AM |
12033 | bfd_get_symcount (abfd) = 0; |
12034 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12035 | /* sh_name is set in prep_headers. */ | |
12036 | symtab_hdr->sh_type = SHT_SYMTAB; | |
12037 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
12038 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
12039 | /* sh_link is set in assign_section_numbers. */ | |
12040 | /* sh_info is set below. */ | |
12041 | /* sh_offset is set just below. */ | |
72de5009 | 12042 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 12043 | |
ef10c3ac L |
12044 | if (max_sym_count < 20) |
12045 | max_sym_count = 20; | |
64f52338 | 12046 | htab->strtabsize = max_sym_count; |
ef10c3ac | 12047 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
12048 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
12049 | if (htab->strtab == NULL) | |
c152c796 | 12050 | goto error_return; |
ef10c3ac L |
12051 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
12052 | flinfo.symshndxbuf | |
12053 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
12054 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 12055 | |
8539e4e8 | 12056 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 12057 | { |
8539e4e8 AM |
12058 | file_ptr off = elf_next_file_pos (abfd); |
12059 | ||
12060 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
12061 | ||
12062 | /* Note that at this point elf_next_file_pos (abfd) is | |
12063 | incorrect. We do not yet know the size of the .symtab section. | |
12064 | We correct next_file_pos below, after we do know the size. */ | |
12065 | ||
12066 | /* Start writing out the symbol table. The first symbol is always a | |
12067 | dummy symbol. */ | |
c152c796 AM |
12068 | elfsym.st_value = 0; |
12069 | elfsym.st_size = 0; | |
12070 | elfsym.st_info = 0; | |
12071 | elfsym.st_other = 0; | |
12072 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 12073 | elfsym.st_target_internal = 0; |
ef10c3ac L |
12074 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
12075 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 12076 | goto error_return; |
c152c796 | 12077 | |
8539e4e8 AM |
12078 | /* Output a symbol for each section. We output these even if we are |
12079 | discarding local symbols, since they are used for relocs. These | |
12080 | symbols have no names. We store the index of each one in the | |
12081 | index field of the section, so that we can find it again when | |
12082 | outputting relocs. */ | |
12083 | ||
c152c796 AM |
12084 | elfsym.st_size = 0; |
12085 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12086 | elfsym.st_other = 0; | |
f0b5bb34 | 12087 | elfsym.st_value = 0; |
35fc36a8 | 12088 | elfsym.st_target_internal = 0; |
c152c796 AM |
12089 | for (i = 1; i < elf_numsections (abfd); i++) |
12090 | { | |
12091 | o = bfd_section_from_elf_index (abfd, i); | |
12092 | if (o != NULL) | |
f0b5bb34 AM |
12093 | { |
12094 | o->target_index = bfd_get_symcount (abfd); | |
12095 | elfsym.st_shndx = i; | |
0e1862bb | 12096 | if (!bfd_link_relocatable (info)) |
f0b5bb34 | 12097 | elfsym.st_value = o->vma; |
ef10c3ac L |
12098 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, |
12099 | NULL) != 1) | |
f0b5bb34 AM |
12100 | goto error_return; |
12101 | } | |
c152c796 AM |
12102 | } |
12103 | } | |
12104 | ||
12105 | /* Allocate some memory to hold information read in from the input | |
12106 | files. */ | |
12107 | if (max_contents_size != 0) | |
12108 | { | |
8b127cbc AM |
12109 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
12110 | if (flinfo.contents == NULL) | |
c152c796 AM |
12111 | goto error_return; |
12112 | } | |
12113 | ||
12114 | if (max_external_reloc_size != 0) | |
12115 | { | |
8b127cbc AM |
12116 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
12117 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
12118 | goto error_return; |
12119 | } | |
12120 | ||
12121 | if (max_internal_reloc_count != 0) | |
12122 | { | |
056bafd4 | 12123 | amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); |
8b127cbc AM |
12124 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
12125 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
12126 | goto error_return; |
12127 | } | |
12128 | ||
12129 | if (max_sym_count != 0) | |
12130 | { | |
12131 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
12132 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
12133 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
12134 | goto error_return; |
12135 | ||
12136 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
12137 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
12138 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
12139 | goto error_return; |
12140 | ||
12141 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
12142 | flinfo.indices = (long int *) bfd_malloc (amt); |
12143 | if (flinfo.indices == NULL) | |
c152c796 AM |
12144 | goto error_return; |
12145 | ||
12146 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
12147 | flinfo.sections = (asection **) bfd_malloc (amt); |
12148 | if (flinfo.sections == NULL) | |
c152c796 AM |
12149 | goto error_return; |
12150 | } | |
12151 | ||
12152 | if (max_sym_shndx_count != 0) | |
12153 | { | |
12154 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
12155 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
12156 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
12157 | goto error_return; |
12158 | } | |
12159 | ||
64f52338 | 12160 | if (htab->tls_sec) |
c152c796 AM |
12161 | { |
12162 | bfd_vma base, end = 0; | |
12163 | asection *sec; | |
12164 | ||
64f52338 | 12165 | for (sec = htab->tls_sec; |
c152c796 AM |
12166 | sec && (sec->flags & SEC_THREAD_LOCAL); |
12167 | sec = sec->next) | |
12168 | { | |
3a800eb9 | 12169 | bfd_size_type size = sec->size; |
c152c796 | 12170 | |
3a800eb9 AM |
12171 | if (size == 0 |
12172 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 12173 | { |
91d6fa6a NC |
12174 | struct bfd_link_order *ord = sec->map_tail.link_order; |
12175 | ||
12176 | if (ord != NULL) | |
12177 | size = ord->offset + ord->size; | |
c152c796 AM |
12178 | } |
12179 | end = sec->vma + size; | |
12180 | } | |
64f52338 | 12181 | base = htab->tls_sec->vma; |
7dc98aea RO |
12182 | /* Only align end of TLS section if static TLS doesn't have special |
12183 | alignment requirements. */ | |
12184 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
12185 | end = align_power (end, htab->tls_sec->alignment_power); |
12186 | htab->tls_size = end - base; | |
c152c796 AM |
12187 | } |
12188 | ||
0b52efa6 PB |
12189 | /* Reorder SHF_LINK_ORDER sections. */ |
12190 | for (o = abfd->sections; o != NULL; o = o->next) | |
12191 | { | |
12192 | if (!elf_fixup_link_order (abfd, o)) | |
12193 | return FALSE; | |
12194 | } | |
12195 | ||
2f0c68f2 CM |
12196 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
12197 | return FALSE; | |
12198 | ||
c152c796 AM |
12199 | /* Since ELF permits relocations to be against local symbols, we |
12200 | must have the local symbols available when we do the relocations. | |
12201 | Since we would rather only read the local symbols once, and we | |
12202 | would rather not keep them in memory, we handle all the | |
12203 | relocations for a single input file at the same time. | |
12204 | ||
12205 | Unfortunately, there is no way to know the total number of local | |
12206 | symbols until we have seen all of them, and the local symbol | |
12207 | indices precede the global symbol indices. This means that when | |
12208 | we are generating relocatable output, and we see a reloc against | |
12209 | a global symbol, we can not know the symbol index until we have | |
12210 | finished examining all the local symbols to see which ones we are | |
12211 | going to output. To deal with this, we keep the relocations in | |
12212 | memory, and don't output them until the end of the link. This is | |
12213 | an unfortunate waste of memory, but I don't see a good way around | |
12214 | it. Fortunately, it only happens when performing a relocatable | |
12215 | link, which is not the common case. FIXME: If keep_memory is set | |
12216 | we could write the relocs out and then read them again; I don't | |
12217 | know how bad the memory loss will be. */ | |
12218 | ||
c72f2fb2 | 12219 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
12220 | sub->output_has_begun = FALSE; |
12221 | for (o = abfd->sections; o != NULL; o = o->next) | |
12222 | { | |
8423293d | 12223 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12224 | { |
12225 | if (p->type == bfd_indirect_link_order | |
12226 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
12227 | == bfd_target_elf_flavour) | |
12228 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
12229 | { | |
12230 | if (! sub->output_has_begun) | |
12231 | { | |
8b127cbc | 12232 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 AM |
12233 | goto error_return; |
12234 | sub->output_has_begun = TRUE; | |
12235 | } | |
12236 | } | |
12237 | else if (p->type == bfd_section_reloc_link_order | |
12238 | || p->type == bfd_symbol_reloc_link_order) | |
12239 | { | |
12240 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
12241 | goto error_return; | |
12242 | } | |
12243 | else | |
12244 | { | |
12245 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
12246 | { |
12247 | if (p->type == bfd_indirect_link_order | |
12248 | && (bfd_get_flavour (sub) | |
12249 | == bfd_target_elf_flavour) | |
12250 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
12251 | != bed->s->elfclass)) | |
12252 | { | |
12253 | const char *iclass, *oclass; | |
12254 | ||
aebf9be7 | 12255 | switch (bed->s->elfclass) |
351f65ca | 12256 | { |
aebf9be7 NC |
12257 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
12258 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
12259 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
12260 | default: abort (); | |
351f65ca | 12261 | } |
aebf9be7 NC |
12262 | |
12263 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 12264 | { |
aebf9be7 NC |
12265 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
12266 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
12267 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
12268 | default: abort (); | |
351f65ca L |
12269 | } |
12270 | ||
12271 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 12272 | _bfd_error_handler |
695344c0 | 12273 | /* xgettext:c-format */ |
871b3ab2 | 12274 | (_("%pB: file class %s incompatible with %s"), |
351f65ca L |
12275 | sub, iclass, oclass); |
12276 | } | |
12277 | ||
12278 | goto error_return; | |
12279 | } | |
c152c796 AM |
12280 | } |
12281 | } | |
12282 | } | |
12283 | ||
c0f00686 L |
12284 | /* Free symbol buffer if needed. */ |
12285 | if (!info->reduce_memory_overheads) | |
12286 | { | |
c72f2fb2 | 12287 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
3fcd97f1 JJ |
12288 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour |
12289 | && elf_tdata (sub)->symbuf) | |
c0f00686 L |
12290 | { |
12291 | free (elf_tdata (sub)->symbuf); | |
12292 | elf_tdata (sub)->symbuf = NULL; | |
12293 | } | |
12294 | } | |
12295 | ||
c152c796 AM |
12296 | /* Output any global symbols that got converted to local in a |
12297 | version script or due to symbol visibility. We do this in a | |
12298 | separate step since ELF requires all local symbols to appear | |
12299 | prior to any global symbols. FIXME: We should only do this if | |
12300 | some global symbols were, in fact, converted to become local. | |
12301 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
12302 | eoinfo.failed = FALSE; | |
8b127cbc | 12303 | eoinfo.flinfo = &flinfo; |
c152c796 | 12304 | eoinfo.localsyms = TRUE; |
34a79995 | 12305 | eoinfo.file_sym_done = FALSE; |
7686d77d | 12306 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12307 | if (eoinfo.failed) |
12308 | return FALSE; | |
12309 | ||
4e617b1e PB |
12310 | /* If backend needs to output some local symbols not present in the hash |
12311 | table, do it now. */ | |
8539e4e8 AM |
12312 | if (bed->elf_backend_output_arch_local_syms |
12313 | && (info->strip != strip_all || emit_relocs)) | |
4e617b1e | 12314 | { |
6e0b88f1 | 12315 | typedef int (*out_sym_func) |
4e617b1e PB |
12316 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12317 | struct elf_link_hash_entry *); | |
12318 | ||
12319 | if (! ((*bed->elf_backend_output_arch_local_syms) | |
ef10c3ac L |
12320 | (abfd, info, &flinfo, |
12321 | (out_sym_func) elf_link_output_symstrtab))) | |
4e617b1e PB |
12322 | return FALSE; |
12323 | } | |
12324 | ||
c152c796 AM |
12325 | /* That wrote out all the local symbols. Finish up the symbol table |
12326 | with the global symbols. Even if we want to strip everything we | |
12327 | can, we still need to deal with those global symbols that got | |
12328 | converted to local in a version script. */ | |
12329 | ||
12330 | /* The sh_info field records the index of the first non local symbol. */ | |
12331 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
12332 | ||
12333 | if (dynamic | |
64f52338 AM |
12334 | && htab->dynsym != NULL |
12335 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
12336 | { |
12337 | Elf_Internal_Sym sym; | |
64f52338 | 12338 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 12339 | |
64f52338 AM |
12340 | o = htab->dynsym->output_section; |
12341 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
12342 | |
12343 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 12344 | if (bfd_link_pic (info) |
64f52338 | 12345 | || htab->is_relocatable_executable) |
c152c796 AM |
12346 | { |
12347 | asection *s; | |
12348 | ||
12349 | sym.st_size = 0; | |
12350 | sym.st_name = 0; | |
12351 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12352 | sym.st_other = 0; | |
35fc36a8 | 12353 | sym.st_target_internal = 0; |
c152c796 AM |
12354 | |
12355 | for (s = abfd->sections; s != NULL; s = s->next) | |
12356 | { | |
12357 | int indx; | |
12358 | bfd_byte *dest; | |
12359 | long dynindx; | |
12360 | ||
c152c796 | 12361 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
12362 | if (dynindx <= 0) |
12363 | continue; | |
12364 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
12365 | BFD_ASSERT (indx > 0); |
12366 | sym.st_shndx = indx; | |
c0d5a53d L |
12367 | if (! check_dynsym (abfd, &sym)) |
12368 | return FALSE; | |
c152c796 AM |
12369 | sym.st_value = s->vma; |
12370 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
12371 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12372 | } | |
c152c796 AM |
12373 | } |
12374 | ||
12375 | /* Write out the local dynsyms. */ | |
64f52338 | 12376 | if (htab->dynlocal) |
c152c796 AM |
12377 | { |
12378 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 12379 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
12380 | { |
12381 | asection *s; | |
12382 | bfd_byte *dest; | |
12383 | ||
935bd1e0 | 12384 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
12385 | Note that we saved a word of storage and overwrote |
12386 | the original st_name with the dynstr_index. */ | |
12387 | sym = e->isym; | |
935bd1e0 | 12388 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
c152c796 | 12389 | |
cb33740c AM |
12390 | s = bfd_section_from_elf_index (e->input_bfd, |
12391 | e->isym.st_shndx); | |
12392 | if (s != NULL) | |
c152c796 | 12393 | { |
c152c796 AM |
12394 | sym.st_shndx = |
12395 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d L |
12396 | if (! check_dynsym (abfd, &sym)) |
12397 | return FALSE; | |
c152c796 AM |
12398 | sym.st_value = (s->output_section->vma |
12399 | + s->output_offset | |
12400 | + e->isym.st_value); | |
12401 | } | |
12402 | ||
c152c796 AM |
12403 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
12404 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
12405 | } | |
12406 | } | |
c152c796 AM |
12407 | } |
12408 | ||
12409 | /* We get the global symbols from the hash table. */ | |
12410 | eoinfo.failed = FALSE; | |
12411 | eoinfo.localsyms = FALSE; | |
8b127cbc | 12412 | eoinfo.flinfo = &flinfo; |
7686d77d | 12413 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 AM |
12414 | if (eoinfo.failed) |
12415 | return FALSE; | |
12416 | ||
12417 | /* If backend needs to output some symbols not present in the hash | |
12418 | table, do it now. */ | |
8539e4e8 AM |
12419 | if (bed->elf_backend_output_arch_syms |
12420 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 12421 | { |
6e0b88f1 | 12422 | typedef int (*out_sym_func) |
c152c796 AM |
12423 | (void *, const char *, Elf_Internal_Sym *, asection *, |
12424 | struct elf_link_hash_entry *); | |
12425 | ||
12426 | if (! ((*bed->elf_backend_output_arch_syms) | |
ef10c3ac L |
12427 | (abfd, info, &flinfo, |
12428 | (out_sym_func) elf_link_output_symstrtab))) | |
c152c796 AM |
12429 | return FALSE; |
12430 | } | |
12431 | ||
ef10c3ac L |
12432 | /* Finalize the .strtab section. */ |
12433 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
12434 | ||
12435 | /* Swap out the .strtab section. */ | |
12436 | if (!elf_link_swap_symbols_out (&flinfo)) | |
c152c796 AM |
12437 | return FALSE; |
12438 | ||
12439 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
12440 | if (bfd_get_symcount (abfd) > 0) |
12441 | { | |
ee3b52e9 L |
12442 | /* Finish up and write out the symbol string table (.strtab) |
12443 | section. */ | |
ad32986f | 12444 | Elf_Internal_Shdr *symstrtab_hdr = NULL; |
8539e4e8 AM |
12445 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
12446 | ||
ad32986f | 12447 | if (elf_symtab_shndx_list (abfd)) |
8539e4e8 | 12448 | { |
ad32986f | 12449 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8539e4e8 | 12450 | |
ad32986f NC |
12451 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) |
12452 | { | |
12453 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
12454 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
12455 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
12456 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
12457 | symtab_shndx_hdr->sh_size = amt; | |
8539e4e8 | 12458 | |
ad32986f NC |
12459 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, |
12460 | off, TRUE); | |
12461 | ||
12462 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
12463 | || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) | |
12464 | return FALSE; | |
12465 | } | |
8539e4e8 | 12466 | } |
ee3b52e9 L |
12467 | |
12468 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
12469 | /* sh_name was set in prep_headers. */ | |
12470 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 12471 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 12472 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 12473 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
12474 | symstrtab_hdr->sh_entsize = 0; |
12475 | symstrtab_hdr->sh_link = 0; | |
12476 | symstrtab_hdr->sh_info = 0; | |
12477 | /* sh_offset is set just below. */ | |
12478 | symstrtab_hdr->sh_addralign = 1; | |
12479 | ||
12480 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
12481 | off, TRUE); | |
12482 | elf_next_file_pos (abfd) = off; | |
12483 | ||
c152c796 | 12484 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 12485 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
c152c796 AM |
12486 | return FALSE; |
12487 | } | |
12488 | ||
76359541 TP |
12489 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
12490 | { | |
871b3ab2 | 12491 | _bfd_error_handler (_("%pB: failed to generate import library"), |
4eca0228 | 12492 | info->out_implib_bfd); |
76359541 TP |
12493 | return FALSE; |
12494 | } | |
12495 | ||
c152c796 AM |
12496 | /* Adjust the relocs to have the correct symbol indices. */ |
12497 | for (o = abfd->sections; o != NULL; o = o->next) | |
12498 | { | |
d4730f92 | 12499 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
28dbcedc | 12500 | bfd_boolean sort; |
10bbbc1d | 12501 | |
c152c796 AM |
12502 | if ((o->flags & SEC_RELOC) == 0) |
12503 | continue; | |
12504 | ||
28dbcedc | 12505 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 12506 | if (esdo->rel.hdr != NULL |
10bbbc1d | 12507 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) |
bca6d0e3 AM |
12508 | return FALSE; |
12509 | if (esdo->rela.hdr != NULL | |
10bbbc1d | 12510 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) |
bca6d0e3 | 12511 | return FALSE; |
c152c796 AM |
12512 | |
12513 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
12514 | trying to swap the relocs out itself. */ | |
12515 | o->reloc_count = 0; | |
12516 | } | |
12517 | ||
12518 | if (dynamic && info->combreloc && dynobj != NULL) | |
12519 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
12520 | ||
12521 | /* If we are linking against a dynamic object, or generating a | |
12522 | shared library, finish up the dynamic linking information. */ | |
12523 | if (dynamic) | |
12524 | { | |
12525 | bfd_byte *dyncon, *dynconend; | |
12526 | ||
12527 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 12528 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
12529 | BFD_ASSERT (o != NULL); |
12530 | ||
12531 | dyncon = o->contents; | |
eea6121a | 12532 | dynconend = o->contents + o->size; |
c152c796 AM |
12533 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
12534 | { | |
12535 | Elf_Internal_Dyn dyn; | |
12536 | const char *name; | |
12537 | unsigned int type; | |
64487780 AM |
12538 | bfd_size_type sh_size; |
12539 | bfd_vma sh_addr; | |
c152c796 AM |
12540 | |
12541 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12542 | ||
12543 | switch (dyn.d_tag) | |
12544 | { | |
12545 | default: | |
12546 | continue; | |
12547 | case DT_NULL: | |
12548 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
12549 | { | |
12550 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
12551 | { | |
12552 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
12553 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
12554 | default: continue; | |
12555 | } | |
12556 | dyn.d_un.d_val = relativecount; | |
12557 | relativecount = 0; | |
12558 | break; | |
12559 | } | |
12560 | continue; | |
12561 | ||
12562 | case DT_INIT: | |
12563 | name = info->init_function; | |
12564 | goto get_sym; | |
12565 | case DT_FINI: | |
12566 | name = info->fini_function; | |
12567 | get_sym: | |
12568 | { | |
12569 | struct elf_link_hash_entry *h; | |
12570 | ||
64f52338 | 12571 | h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); |
c152c796 AM |
12572 | if (h != NULL |
12573 | && (h->root.type == bfd_link_hash_defined | |
12574 | || h->root.type == bfd_link_hash_defweak)) | |
12575 | { | |
bef26483 | 12576 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
12577 | o = h->root.u.def.section; |
12578 | if (o->output_section != NULL) | |
bef26483 | 12579 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
12580 | + o->output_offset); |
12581 | else | |
12582 | { | |
12583 | /* The symbol is imported from another shared | |
12584 | library and does not apply to this one. */ | |
bef26483 | 12585 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
12586 | } |
12587 | break; | |
12588 | } | |
12589 | } | |
12590 | continue; | |
12591 | ||
12592 | case DT_PREINIT_ARRAYSZ: | |
12593 | name = ".preinit_array"; | |
4ade44b7 | 12594 | goto get_out_size; |
c152c796 AM |
12595 | case DT_INIT_ARRAYSZ: |
12596 | name = ".init_array"; | |
4ade44b7 | 12597 | goto get_out_size; |
c152c796 AM |
12598 | case DT_FINI_ARRAYSZ: |
12599 | name = ".fini_array"; | |
4ade44b7 | 12600 | get_out_size: |
c152c796 AM |
12601 | o = bfd_get_section_by_name (abfd, name); |
12602 | if (o == NULL) | |
12603 | { | |
4eca0228 | 12604 | _bfd_error_handler |
4ade44b7 | 12605 | (_("could not find section %s"), name); |
c152c796 AM |
12606 | goto error_return; |
12607 | } | |
eea6121a | 12608 | if (o->size == 0) |
4eca0228 | 12609 | _bfd_error_handler |
c152c796 | 12610 | (_("warning: %s section has zero size"), name); |
eea6121a | 12611 | dyn.d_un.d_val = o->size; |
c152c796 AM |
12612 | break; |
12613 | ||
12614 | case DT_PREINIT_ARRAY: | |
12615 | name = ".preinit_array"; | |
4ade44b7 | 12616 | goto get_out_vma; |
c152c796 AM |
12617 | case DT_INIT_ARRAY: |
12618 | name = ".init_array"; | |
4ade44b7 | 12619 | goto get_out_vma; |
c152c796 AM |
12620 | case DT_FINI_ARRAY: |
12621 | name = ".fini_array"; | |
4ade44b7 AM |
12622 | get_out_vma: |
12623 | o = bfd_get_section_by_name (abfd, name); | |
12624 | goto do_vma; | |
c152c796 AM |
12625 | |
12626 | case DT_HASH: | |
12627 | name = ".hash"; | |
12628 | goto get_vma; | |
fdc90cb4 JJ |
12629 | case DT_GNU_HASH: |
12630 | name = ".gnu.hash"; | |
12631 | goto get_vma; | |
c152c796 AM |
12632 | case DT_STRTAB: |
12633 | name = ".dynstr"; | |
12634 | goto get_vma; | |
12635 | case DT_SYMTAB: | |
12636 | name = ".dynsym"; | |
12637 | goto get_vma; | |
12638 | case DT_VERDEF: | |
12639 | name = ".gnu.version_d"; | |
12640 | goto get_vma; | |
12641 | case DT_VERNEED: | |
12642 | name = ".gnu.version_r"; | |
12643 | goto get_vma; | |
12644 | case DT_VERSYM: | |
12645 | name = ".gnu.version"; | |
12646 | get_vma: | |
4ade44b7 AM |
12647 | o = bfd_get_linker_section (dynobj, name); |
12648 | do_vma: | |
b3293efa | 12649 | if (o == NULL || bfd_is_abs_section (o->output_section)) |
c152c796 | 12650 | { |
4eca0228 | 12651 | _bfd_error_handler |
4ade44b7 | 12652 | (_("could not find section %s"), name); |
c152c796 AM |
12653 | goto error_return; |
12654 | } | |
894891db NC |
12655 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
12656 | { | |
4eca0228 | 12657 | _bfd_error_handler |
894891db NC |
12658 | (_("warning: section '%s' is being made into a note"), name); |
12659 | bfd_set_error (bfd_error_nonrepresentable_section); | |
12660 | goto error_return; | |
12661 | } | |
4ade44b7 | 12662 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
12663 | break; |
12664 | ||
12665 | case DT_REL: | |
12666 | case DT_RELA: | |
12667 | case DT_RELSZ: | |
12668 | case DT_RELASZ: | |
12669 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
12670 | type = SHT_REL; | |
12671 | else | |
12672 | type = SHT_RELA; | |
64487780 AM |
12673 | sh_size = 0; |
12674 | sh_addr = 0; | |
c152c796 AM |
12675 | for (i = 1; i < elf_numsections (abfd); i++) |
12676 | { | |
12677 | Elf_Internal_Shdr *hdr; | |
12678 | ||
12679 | hdr = elf_elfsections (abfd)[i]; | |
12680 | if (hdr->sh_type == type | |
12681 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
12682 | { | |
64487780 AM |
12683 | sh_size += hdr->sh_size; |
12684 | if (sh_addr == 0 | |
12685 | || sh_addr > hdr->sh_addr) | |
12686 | sh_addr = hdr->sh_addr; | |
c152c796 AM |
12687 | } |
12688 | } | |
64487780 | 12689 | |
64f52338 AM |
12690 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
12691 | { | |
12692 | /* Don't count procedure linkage table relocs in the | |
12693 | overall reloc count. */ | |
64487780 AM |
12694 | sh_size -= htab->srelplt->size; |
12695 | if (sh_size == 0) | |
12696 | /* If the size is zero, make the address zero too. | |
12697 | This is to avoid a glibc bug. If the backend | |
12698 | emits DT_RELA/DT_RELASZ even when DT_RELASZ is | |
12699 | zero, then we'll put DT_RELA at the end of | |
12700 | DT_JMPREL. glibc will interpret the end of | |
12701 | DT_RELA matching the end of DT_JMPREL as the | |
12702 | case where DT_RELA includes DT_JMPREL, and for | |
12703 | LD_BIND_NOW will decide that processing DT_RELA | |
12704 | will process the PLT relocs too. Net result: | |
12705 | No PLT relocs applied. */ | |
12706 | sh_addr = 0; | |
12707 | ||
64f52338 AM |
12708 | /* If .rela.plt is the first .rela section, exclude |
12709 | it from DT_RELA. */ | |
64487780 AM |
12710 | else if (sh_addr == (htab->srelplt->output_section->vma |
12711 | + htab->srelplt->output_offset)) | |
12712 | sh_addr += htab->srelplt->size; | |
64f52338 | 12713 | } |
64487780 AM |
12714 | |
12715 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
12716 | dyn.d_un.d_val = sh_size; | |
12717 | else | |
12718 | dyn.d_un.d_ptr = sh_addr; | |
c152c796 AM |
12719 | break; |
12720 | } | |
12721 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
12722 | } | |
12723 | } | |
12724 | ||
12725 | /* If we have created any dynamic sections, then output them. */ | |
12726 | if (dynobj != NULL) | |
12727 | { | |
12728 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
12729 | goto error_return; | |
12730 | ||
943284cc | 12731 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
0e1862bb | 12732 | if (((info->warn_shared_textrel && bfd_link_pic (info)) |
be7b303d | 12733 | || info->error_textrel) |
3d4d4302 | 12734 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) |
943284cc DJ |
12735 | { |
12736 | bfd_byte *dyncon, *dynconend; | |
12737 | ||
943284cc DJ |
12738 | dyncon = o->contents; |
12739 | dynconend = o->contents + o->size; | |
12740 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
12741 | { | |
12742 | Elf_Internal_Dyn dyn; | |
12743 | ||
12744 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
12745 | ||
12746 | if (dyn.d_tag == DT_TEXTREL) | |
12747 | { | |
c192a133 AM |
12748 | if (info->error_textrel) |
12749 | info->callbacks->einfo | |
9793eb77 | 12750 | (_("%P%X: read-only segment has dynamic relocations\n")); |
c192a133 AM |
12751 | else |
12752 | info->callbacks->einfo | |
9793eb77 | 12753 | (_("%P: warning: creating a DT_TEXTREL in a shared object\n")); |
943284cc DJ |
12754 | break; |
12755 | } | |
12756 | } | |
12757 | } | |
12758 | ||
c152c796 AM |
12759 | for (o = dynobj->sections; o != NULL; o = o->next) |
12760 | { | |
12761 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 12762 | || o->size == 0 |
c152c796 AM |
12763 | || o->output_section == bfd_abs_section_ptr) |
12764 | continue; | |
12765 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
12766 | { | |
12767 | /* At this point, we are only interested in sections | |
12768 | created by _bfd_elf_link_create_dynamic_sections. */ | |
12769 | continue; | |
12770 | } | |
64f52338 | 12771 | if (htab->stab_info.stabstr == o) |
3722b82f | 12772 | continue; |
64f52338 | 12773 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 12774 | continue; |
3d4d4302 | 12775 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 AM |
12776 | { |
12777 | if (! bfd_set_section_contents (abfd, o->output_section, | |
12778 | o->contents, | |
37b01f6a DG |
12779 | (file_ptr) o->output_offset |
12780 | * bfd_octets_per_byte (abfd), | |
eea6121a | 12781 | o->size)) |
c152c796 AM |
12782 | goto error_return; |
12783 | } | |
12784 | else | |
12785 | { | |
12786 | /* The contents of the .dynstr section are actually in a | |
12787 | stringtab. */ | |
8539e4e8 AM |
12788 | file_ptr off; |
12789 | ||
c152c796 AM |
12790 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
12791 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 12792 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
12793 | goto error_return; |
12794 | } | |
12795 | } | |
12796 | } | |
12797 | ||
7bdf4127 | 12798 | if (!info->resolve_section_groups) |
c152c796 AM |
12799 | { |
12800 | bfd_boolean failed = FALSE; | |
12801 | ||
7bdf4127 | 12802 | BFD_ASSERT (bfd_link_relocatable (info)); |
c152c796 AM |
12803 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
12804 | if (failed) | |
12805 | goto error_return; | |
12806 | } | |
12807 | ||
12808 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 12809 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 12810 | { |
64f52338 | 12811 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
12812 | goto error_return; |
12813 | } | |
12814 | ||
9f7c3e5e AM |
12815 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
12816 | goto error_return; | |
c152c796 | 12817 | |
9f7c3e5e | 12818 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 12819 | |
104d59d1 JM |
12820 | if (attr_section) |
12821 | { | |
a50b1753 | 12822 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 12823 | if (contents == NULL) |
d0f16d5e | 12824 | return FALSE; /* Bail out and fail. */ |
104d59d1 JM |
12825 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
12826 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
12827 | free (contents); | |
12828 | } | |
12829 | ||
c152c796 AM |
12830 | return TRUE; |
12831 | ||
12832 | error_return: | |
9f7c3e5e | 12833 | elf_final_link_free (abfd, &flinfo); |
c152c796 AM |
12834 | return FALSE; |
12835 | } | |
12836 | \f | |
5241d853 RS |
12837 | /* Initialize COOKIE for input bfd ABFD. */ |
12838 | ||
12839 | static bfd_boolean | |
12840 | init_reloc_cookie (struct elf_reloc_cookie *cookie, | |
12841 | struct bfd_link_info *info, bfd *abfd) | |
12842 | { | |
12843 | Elf_Internal_Shdr *symtab_hdr; | |
12844 | const struct elf_backend_data *bed; | |
12845 | ||
12846 | bed = get_elf_backend_data (abfd); | |
12847 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12848 | ||
12849 | cookie->abfd = abfd; | |
12850 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
12851 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
12852 | if (cookie->bad_symtab) | |
12853 | { | |
12854 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
12855 | cookie->extsymoff = 0; | |
12856 | } | |
12857 | else | |
12858 | { | |
12859 | cookie->locsymcount = symtab_hdr->sh_info; | |
12860 | cookie->extsymoff = symtab_hdr->sh_info; | |
12861 | } | |
12862 | ||
12863 | if (bed->s->arch_size == 32) | |
12864 | cookie->r_sym_shift = 8; | |
12865 | else | |
12866 | cookie->r_sym_shift = 32; | |
12867 | ||
12868 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12869 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
12870 | { | |
12871 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
12872 | cookie->locsymcount, 0, | |
12873 | NULL, NULL, NULL); | |
12874 | if (cookie->locsyms == NULL) | |
12875 | { | |
12876 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
12877 | return FALSE; | |
12878 | } | |
12879 | if (info->keep_memory) | |
12880 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
12881 | } | |
12882 | return TRUE; | |
12883 | } | |
12884 | ||
12885 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
12886 | ||
12887 | static void | |
12888 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
12889 | { | |
12890 | Elf_Internal_Shdr *symtab_hdr; | |
12891 | ||
12892 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
12893 | if (cookie->locsyms != NULL | |
12894 | && symtab_hdr->contents != (unsigned char *) cookie->locsyms) | |
12895 | free (cookie->locsyms); | |
12896 | } | |
12897 | ||
12898 | /* Initialize the relocation information in COOKIE for input section SEC | |
12899 | of input bfd ABFD. */ | |
12900 | ||
12901 | static bfd_boolean | |
12902 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12903 | struct bfd_link_info *info, bfd *abfd, | |
12904 | asection *sec) | |
12905 | { | |
5241d853 RS |
12906 | if (sec->reloc_count == 0) |
12907 | { | |
12908 | cookie->rels = NULL; | |
12909 | cookie->relend = NULL; | |
12910 | } | |
12911 | else | |
12912 | { | |
5241d853 RS |
12913 | cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, |
12914 | info->keep_memory); | |
12915 | if (cookie->rels == NULL) | |
12916 | return FALSE; | |
12917 | cookie->rel = cookie->rels; | |
056bafd4 | 12918 | cookie->relend = cookie->rels + sec->reloc_count; |
5241d853 RS |
12919 | } |
12920 | cookie->rel = cookie->rels; | |
12921 | return TRUE; | |
12922 | } | |
12923 | ||
12924 | /* Free the memory allocated by init_reloc_cookie_rels, | |
12925 | if appropriate. */ | |
12926 | ||
12927 | static void | |
12928 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
12929 | asection *sec) | |
12930 | { | |
12931 | if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) | |
12932 | free (cookie->rels); | |
12933 | } | |
12934 | ||
12935 | /* Initialize the whole of COOKIE for input section SEC. */ | |
12936 | ||
12937 | static bfd_boolean | |
12938 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12939 | struct bfd_link_info *info, | |
12940 | asection *sec) | |
12941 | { | |
12942 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
12943 | goto error1; | |
12944 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
12945 | goto error2; | |
12946 | return TRUE; | |
12947 | ||
12948 | error2: | |
12949 | fini_reloc_cookie (cookie, sec->owner); | |
12950 | error1: | |
12951 | return FALSE; | |
12952 | } | |
12953 | ||
12954 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
12955 | if appropriate. */ | |
12956 | ||
12957 | static void | |
12958 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
12959 | asection *sec) | |
12960 | { | |
12961 | fini_reloc_cookie_rels (cookie, sec); | |
12962 | fini_reloc_cookie (cookie, sec->owner); | |
12963 | } | |
12964 | \f | |
c152c796 AM |
12965 | /* Garbage collect unused sections. */ |
12966 | ||
07adf181 AM |
12967 | /* Default gc_mark_hook. */ |
12968 | ||
12969 | asection * | |
12970 | _bfd_elf_gc_mark_hook (asection *sec, | |
12971 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
12972 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
12973 | struct elf_link_hash_entry *h, | |
12974 | Elf_Internal_Sym *sym) | |
12975 | { | |
12976 | if (h != NULL) | |
12977 | { | |
12978 | switch (h->root.type) | |
12979 | { | |
12980 | case bfd_link_hash_defined: | |
12981 | case bfd_link_hash_defweak: | |
12982 | return h->root.u.def.section; | |
12983 | ||
12984 | case bfd_link_hash_common: | |
12985 | return h->root.u.c.p->section; | |
12986 | ||
12987 | default: | |
12988 | break; | |
12989 | } | |
12990 | } | |
12991 | else | |
12992 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
12993 | ||
12994 | return NULL; | |
12995 | } | |
12996 | ||
9e223787 | 12997 | /* Return the debug definition section. */ |
b7c871ed L |
12998 | |
12999 | static asection * | |
13000 | elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, | |
13001 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13002 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
13003 | struct elf_link_hash_entry *h, | |
9e223787 | 13004 | Elf_Internal_Sym *sym) |
b7c871ed | 13005 | { |
9e223787 L |
13006 | if (h != NULL) |
13007 | { | |
13008 | /* Return the global debug definition section. */ | |
13009 | if ((h->root.type == bfd_link_hash_defined | |
13010 | || h->root.type == bfd_link_hash_defweak) | |
13011 | && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) | |
13012 | return h->root.u.def.section; | |
13013 | } | |
13014 | else | |
13015 | { | |
13016 | /* Return the local debug definition section. */ | |
13017 | asection *isec = bfd_section_from_elf_index (sec->owner, | |
13018 | sym->st_shndx); | |
13019 | if ((isec->flags & SEC_DEBUGGING) != 0) | |
13020 | return isec; | |
13021 | } | |
b7c871ed L |
13022 | |
13023 | return NULL; | |
13024 | } | |
13025 | ||
5241d853 RS |
13026 | /* COOKIE->rel describes a relocation against section SEC, which is |
13027 | a section we've decided to keep. Return the section that contains | |
13028 | the relocation symbol, or NULL if no section contains it. */ | |
13029 | ||
13030 | asection * | |
13031 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
13032 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 AM |
13033 | struct elf_reloc_cookie *cookie, |
13034 | bfd_boolean *start_stop) | |
5241d853 RS |
13035 | { |
13036 | unsigned long r_symndx; | |
13037 | struct elf_link_hash_entry *h; | |
13038 | ||
13039 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 13040 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
13041 | return NULL; |
13042 | ||
13043 | if (r_symndx >= cookie->locsymcount | |
13044 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13045 | { | |
13046 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
263ddf68 L |
13047 | if (h == NULL) |
13048 | { | |
871b3ab2 | 13049 | info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"), |
263ddf68 L |
13050 | sec->owner); |
13051 | return NULL; | |
13052 | } | |
5241d853 RS |
13053 | while (h->root.type == bfd_link_hash_indirect |
13054 | || h->root.type == bfd_link_hash_warning) | |
13055 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1d5316ab | 13056 | h->mark = 1; |
4e6b54a6 AM |
13057 | /* If this symbol is weak and there is a non-weak definition, we |
13058 | keep the non-weak definition because many backends put | |
13059 | dynamic reloc info on the non-weak definition for code | |
13060 | handling copy relocs. */ | |
60d67dc8 AM |
13061 | if (h->is_weakalias) |
13062 | weakdef (h)->mark = 1; | |
1cce69b9 | 13063 | |
a6a4679f | 13064 | if (start_stop != NULL) |
1cce69b9 | 13065 | { |
7dba9362 AM |
13066 | /* To work around a glibc bug, mark XXX input sections |
13067 | when there is a reference to __start_XXX or __stop_XXX | |
13068 | symbols. */ | |
cbd0eecf | 13069 | if (h->start_stop) |
1cce69b9 | 13070 | { |
cbd0eecf | 13071 | asection *s = h->u2.start_stop_section; |
a6a4679f AM |
13072 | *start_stop = !s->gc_mark; |
13073 | return s; | |
1cce69b9 AM |
13074 | } |
13075 | } | |
13076 | ||
5241d853 RS |
13077 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
13078 | } | |
13079 | ||
13080 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
13081 | &cookie->locsyms[r_symndx]); | |
13082 | } | |
13083 | ||
13084 | /* COOKIE->rel describes a relocation against section SEC, which is | |
13085 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 13086 | the relocation symbol. */ |
5241d853 RS |
13087 | |
13088 | bfd_boolean | |
13089 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, | |
13090 | asection *sec, | |
13091 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 13092 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
13093 | { |
13094 | asection *rsec; | |
1cce69b9 | 13095 | bfd_boolean start_stop = FALSE; |
5241d853 | 13096 | |
1cce69b9 AM |
13097 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
13098 | while (rsec != NULL) | |
5241d853 | 13099 | { |
1cce69b9 AM |
13100 | if (!rsec->gc_mark) |
13101 | { | |
13102 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
13103 | || (rsec->owner->flags & DYNAMIC) != 0) | |
13104 | rsec->gc_mark = 1; | |
13105 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
13106 | return FALSE; | |
13107 | } | |
13108 | if (!start_stop) | |
13109 | break; | |
199af150 | 13110 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 RS |
13111 | } |
13112 | return TRUE; | |
13113 | } | |
13114 | ||
07adf181 AM |
13115 | /* The mark phase of garbage collection. For a given section, mark |
13116 | it and any sections in this section's group, and all the sections | |
13117 | which define symbols to which it refers. */ | |
13118 | ||
ccfa59ea AM |
13119 | bfd_boolean |
13120 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
13121 | asection *sec, | |
6a5bb875 | 13122 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 AM |
13123 | { |
13124 | bfd_boolean ret; | |
9d0a14d3 | 13125 | asection *group_sec, *eh_frame; |
c152c796 AM |
13126 | |
13127 | sec->gc_mark = 1; | |
13128 | ||
13129 | /* Mark all the sections in the group. */ | |
13130 | group_sec = elf_section_data (sec)->next_in_group; | |
13131 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 13132 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
13133 | return FALSE; |
13134 | ||
13135 | /* Look through the section relocs. */ | |
13136 | ret = TRUE; | |
9d0a14d3 RS |
13137 | eh_frame = elf_eh_frame_section (sec->owner); |
13138 | if ((sec->flags & SEC_RELOC) != 0 | |
13139 | && sec->reloc_count > 0 | |
13140 | && sec != eh_frame) | |
c152c796 | 13141 | { |
5241d853 | 13142 | struct elf_reloc_cookie cookie; |
c152c796 | 13143 | |
5241d853 RS |
13144 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
13145 | ret = FALSE; | |
c152c796 | 13146 | else |
c152c796 | 13147 | { |
5241d853 | 13148 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 13149 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 RS |
13150 | { |
13151 | ret = FALSE; | |
13152 | break; | |
13153 | } | |
13154 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
13155 | } |
13156 | } | |
9d0a14d3 RS |
13157 | |
13158 | if (ret && eh_frame && elf_fde_list (sec)) | |
13159 | { | |
13160 | struct elf_reloc_cookie cookie; | |
13161 | ||
13162 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
13163 | ret = FALSE; | |
13164 | else | |
13165 | { | |
13166 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
13167 | gc_mark_hook, &cookie)) | |
13168 | ret = FALSE; | |
13169 | fini_reloc_cookie_for_section (&cookie, eh_frame); | |
13170 | } | |
13171 | } | |
13172 | ||
2f0c68f2 CM |
13173 | eh_frame = elf_section_eh_frame_entry (sec); |
13174 | if (ret && eh_frame && !eh_frame->gc_mark) | |
13175 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
13176 | ret = FALSE; | |
13177 | ||
c152c796 AM |
13178 | return ret; |
13179 | } | |
13180 | ||
3c758495 TG |
13181 | /* Scan and mark sections in a special or debug section group. */ |
13182 | ||
13183 | static void | |
13184 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
13185 | { | |
13186 | /* Point to first section of section group. */ | |
13187 | asection *ssec; | |
13188 | /* Used to iterate the section group. */ | |
13189 | asection *msec; | |
13190 | ||
13191 | bfd_boolean is_special_grp = TRUE; | |
13192 | bfd_boolean is_debug_grp = TRUE; | |
13193 | ||
13194 | /* First scan to see if group contains any section other than debug | |
13195 | and special section. */ | |
13196 | ssec = msec = elf_next_in_group (grp); | |
13197 | do | |
13198 | { | |
13199 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
13200 | is_debug_grp = FALSE; | |
13201 | ||
13202 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
13203 | is_special_grp = FALSE; | |
13204 | ||
13205 | msec = elf_next_in_group (msec); | |
13206 | } | |
13207 | while (msec != ssec); | |
13208 | ||
13209 | /* If this is a pure debug section group or pure special section group, | |
13210 | keep all sections in this group. */ | |
13211 | if (is_debug_grp || is_special_grp) | |
13212 | { | |
13213 | do | |
13214 | { | |
13215 | msec->gc_mark = 1; | |
13216 | msec = elf_next_in_group (msec); | |
13217 | } | |
13218 | while (msec != ssec); | |
13219 | } | |
13220 | } | |
13221 | ||
7f6ab9f8 AM |
13222 | /* Keep debug and special sections. */ |
13223 | ||
13224 | bfd_boolean | |
13225 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, | |
13226 | elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) | |
13227 | { | |
13228 | bfd *ibfd; | |
13229 | ||
c72f2fb2 | 13230 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
13231 | { |
13232 | asection *isec; | |
13233 | bfd_boolean some_kept; | |
b40bf0a2 | 13234 | bfd_boolean debug_frag_seen; |
b7c871ed | 13235 | bfd_boolean has_kept_debug_info; |
7f6ab9f8 AM |
13236 | |
13237 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13238 | continue; | |
57963c05 AM |
13239 | isec = ibfd->sections; |
13240 | if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13241 | continue; | |
7f6ab9f8 | 13242 | |
b40bf0a2 NC |
13243 | /* Ensure all linker created sections are kept, |
13244 | see if any other section is already marked, | |
13245 | and note if we have any fragmented debug sections. */ | |
b7c871ed | 13246 | debug_frag_seen = some_kept = has_kept_debug_info = FALSE; |
7f6ab9f8 AM |
13247 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
13248 | { | |
13249 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
13250 | isec->gc_mark = 1; | |
eb026f09 AM |
13251 | else if (isec->gc_mark |
13252 | && (isec->flags & SEC_ALLOC) != 0 | |
13253 | && elf_section_type (isec) != SHT_NOTE) | |
7f6ab9f8 | 13254 | some_kept = TRUE; |
b40bf0a2 | 13255 | |
535b785f | 13256 | if (!debug_frag_seen |
b40bf0a2 NC |
13257 | && (isec->flags & SEC_DEBUGGING) |
13258 | && CONST_STRNEQ (isec->name, ".debug_line.")) | |
13259 | debug_frag_seen = TRUE; | |
7f6ab9f8 AM |
13260 | } |
13261 | ||
eb026f09 AM |
13262 | /* If no non-note alloc section in this file will be kept, then |
13263 | we can toss out the debug and special sections. */ | |
7f6ab9f8 AM |
13264 | if (!some_kept) |
13265 | continue; | |
13266 | ||
13267 | /* Keep debug and special sections like .comment when they are | |
3c758495 TG |
13268 | not part of a group. Also keep section groups that contain |
13269 | just debug sections or special sections. */ | |
7f6ab9f8 | 13270 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
13271 | { |
13272 | if ((isec->flags & SEC_GROUP) != 0) | |
13273 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
13274 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
13275 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
13276 | && elf_next_in_group (isec) == NULL) | |
13277 | isec->gc_mark = 1; | |
b7c871ed L |
13278 | if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) |
13279 | has_kept_debug_info = TRUE; | |
3c758495 | 13280 | } |
b40bf0a2 | 13281 | |
b40bf0a2 NC |
13282 | /* Look for CODE sections which are going to be discarded, |
13283 | and find and discard any fragmented debug sections which | |
13284 | are associated with that code section. */ | |
b7c871ed L |
13285 | if (debug_frag_seen) |
13286 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13287 | if ((isec->flags & SEC_CODE) != 0 | |
13288 | && isec->gc_mark == 0) | |
13289 | { | |
13290 | unsigned int ilen; | |
13291 | asection *dsec; | |
b40bf0a2 | 13292 | |
b7c871ed | 13293 | ilen = strlen (isec->name); |
b40bf0a2 | 13294 | |
b7c871ed | 13295 | /* Association is determined by the name of the debug |
07d6d2b8 | 13296 | section containing the name of the code section as |
b7c871ed L |
13297 | a suffix. For example .debug_line.text.foo is a |
13298 | debug section associated with .text.foo. */ | |
13299 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
13300 | { | |
13301 | unsigned int dlen; | |
b40bf0a2 | 13302 | |
b7c871ed L |
13303 | if (dsec->gc_mark == 0 |
13304 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
13305 | continue; | |
b40bf0a2 | 13306 | |
b7c871ed | 13307 | dlen = strlen (dsec->name); |
b40bf0a2 | 13308 | |
b7c871ed L |
13309 | if (dlen > ilen |
13310 | && strncmp (dsec->name + (dlen - ilen), | |
13311 | isec->name, ilen) == 0) | |
b40bf0a2 | 13312 | dsec->gc_mark = 0; |
b7c871ed | 13313 | } |
b40bf0a2 | 13314 | } |
b7c871ed L |
13315 | |
13316 | /* Mark debug sections referenced by kept debug sections. */ | |
13317 | if (has_kept_debug_info) | |
13318 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
13319 | if (isec->gc_mark | |
13320 | && (isec->flags & SEC_DEBUGGING) != 0) | |
13321 | if (!_bfd_elf_gc_mark (info, isec, | |
13322 | elf_gc_mark_debug_section)) | |
13323 | return FALSE; | |
7f6ab9f8 AM |
13324 | } |
13325 | return TRUE; | |
13326 | } | |
13327 | ||
c152c796 | 13328 | static bfd_boolean |
ccabcbe5 | 13329 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
13330 | { |
13331 | bfd *sub; | |
ccabcbe5 | 13332 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 13333 | |
c72f2fb2 | 13334 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13335 | { |
13336 | asection *o; | |
13337 | ||
b19a8f85 | 13338 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13339 | || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) |
b19a8f85 | 13340 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 | 13341 | continue; |
57963c05 AM |
13342 | o = sub->sections; |
13343 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13344 | continue; | |
c152c796 AM |
13345 | |
13346 | for (o = sub->sections; o != NULL; o = o->next) | |
13347 | { | |
a33dafc3 L |
13348 | /* When any section in a section group is kept, we keep all |
13349 | sections in the section group. If the first member of | |
13350 | the section group is excluded, we will also exclude the | |
13351 | group section. */ | |
13352 | if (o->flags & SEC_GROUP) | |
13353 | { | |
13354 | asection *first = elf_next_in_group (o); | |
13355 | o->gc_mark = first->gc_mark; | |
13356 | } | |
c152c796 | 13357 | |
1e7eae0d | 13358 | if (o->gc_mark) |
c152c796 AM |
13359 | continue; |
13360 | ||
13361 | /* Skip sweeping sections already excluded. */ | |
13362 | if (o->flags & SEC_EXCLUDE) | |
13363 | continue; | |
13364 | ||
13365 | /* Since this is early in the link process, it is simple | |
13366 | to remove a section from the output. */ | |
13367 | o->flags |= SEC_EXCLUDE; | |
13368 | ||
c55fe096 | 13369 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 13370 | /* xgettext:c-format */ |
9793eb77 | 13371 | _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), |
c08bb8dd | 13372 | o, sub); |
c152c796 AM |
13373 | } |
13374 | } | |
13375 | ||
c152c796 AM |
13376 | return TRUE; |
13377 | } | |
13378 | ||
13379 | /* Propagate collected vtable information. This is called through | |
13380 | elf_link_hash_traverse. */ | |
13381 | ||
13382 | static bfd_boolean | |
13383 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
13384 | { | |
c152c796 | 13385 | /* Those that are not vtables. */ |
cbd0eecf L |
13386 | if (h->start_stop |
13387 | || h->u2.vtable == NULL | |
13388 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13389 | return TRUE; |
13390 | ||
13391 | /* Those vtables that do not have parents, we cannot merge. */ | |
cbd0eecf | 13392 | if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
13393 | return TRUE; |
13394 | ||
13395 | /* If we've already been done, exit. */ | |
cbd0eecf | 13396 | if (h->u2.vtable->used && h->u2.vtable->used[-1]) |
c152c796 AM |
13397 | return TRUE; |
13398 | ||
13399 | /* Make sure the parent's table is up to date. */ | |
cbd0eecf | 13400 | elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); |
c152c796 | 13401 | |
cbd0eecf | 13402 | if (h->u2.vtable->used == NULL) |
c152c796 AM |
13403 | { |
13404 | /* None of this table's entries were referenced. Re-use the | |
13405 | parent's table. */ | |
cbd0eecf L |
13406 | h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; |
13407 | h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; | |
c152c796 AM |
13408 | } |
13409 | else | |
13410 | { | |
13411 | size_t n; | |
13412 | bfd_boolean *cu, *pu; | |
13413 | ||
13414 | /* Or the parent's entries into ours. */ | |
cbd0eecf | 13415 | cu = h->u2.vtable->used; |
c152c796 | 13416 | cu[-1] = TRUE; |
cbd0eecf | 13417 | pu = h->u2.vtable->parent->u2.vtable->used; |
c152c796 AM |
13418 | if (pu != NULL) |
13419 | { | |
13420 | const struct elf_backend_data *bed; | |
13421 | unsigned int log_file_align; | |
13422 | ||
13423 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
13424 | log_file_align = bed->s->log_file_align; | |
cbd0eecf | 13425 | n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; |
c152c796 AM |
13426 | while (n--) |
13427 | { | |
13428 | if (*pu) | |
13429 | *cu = TRUE; | |
13430 | pu++; | |
13431 | cu++; | |
13432 | } | |
13433 | } | |
13434 | } | |
13435 | ||
13436 | return TRUE; | |
13437 | } | |
13438 | ||
13439 | static bfd_boolean | |
13440 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
13441 | { | |
13442 | asection *sec; | |
13443 | bfd_vma hstart, hend; | |
13444 | Elf_Internal_Rela *relstart, *relend, *rel; | |
13445 | const struct elf_backend_data *bed; | |
13446 | unsigned int log_file_align; | |
13447 | ||
c152c796 AM |
13448 | /* Take care of both those symbols that do not describe vtables as |
13449 | well as those that are not loaded. */ | |
cbd0eecf L |
13450 | if (h->start_stop |
13451 | || h->u2.vtable == NULL | |
13452 | || h->u2.vtable->parent == NULL) | |
c152c796 AM |
13453 | return TRUE; |
13454 | ||
13455 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
13456 | || h->root.type == bfd_link_hash_defweak); | |
13457 | ||
13458 | sec = h->root.u.def.section; | |
13459 | hstart = h->root.u.def.value; | |
13460 | hend = hstart + h->size; | |
13461 | ||
13462 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
13463 | if (!relstart) | |
13464 | return *(bfd_boolean *) okp = FALSE; | |
13465 | bed = get_elf_backend_data (sec->owner); | |
13466 | log_file_align = bed->s->log_file_align; | |
13467 | ||
056bafd4 | 13468 | relend = relstart + sec->reloc_count; |
c152c796 AM |
13469 | |
13470 | for (rel = relstart; rel < relend; ++rel) | |
13471 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
13472 | { | |
13473 | /* If the entry is in use, do nothing. */ | |
cbd0eecf L |
13474 | if (h->u2.vtable->used |
13475 | && (rel->r_offset - hstart) < h->u2.vtable->size) | |
c152c796 AM |
13476 | { |
13477 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
cbd0eecf | 13478 | if (h->u2.vtable->used[entry]) |
c152c796 AM |
13479 | continue; |
13480 | } | |
13481 | /* Otherwise, kill it. */ | |
13482 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
13483 | } | |
13484 | ||
13485 | return TRUE; | |
13486 | } | |
13487 | ||
87538722 AM |
13488 | /* Mark sections containing dynamically referenced symbols. When |
13489 | building shared libraries, we must assume that any visible symbol is | |
13490 | referenced. */ | |
715df9b8 | 13491 | |
64d03ab5 AM |
13492 | bfd_boolean |
13493 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) | |
715df9b8 | 13494 | { |
87538722 | 13495 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 13496 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 13497 | |
715df9b8 EB |
13498 | if ((h->root.type == bfd_link_hash_defined |
13499 | || h->root.type == bfd_link_hash_defweak) | |
d664fd41 | 13500 | && ((h->ref_dynamic && !h->forced_local) |
c4621b33 | 13501 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 13502 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 13503 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 13504 | && (!bfd_link_executable (info) |
22185505 | 13505 | || info->gc_keep_exported |
b407645f AM |
13506 | || info->export_dynamic |
13507 | || (h->dynamic | |
13508 | && d != NULL | |
13509 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 13510 | && (h->versioned >= versioned |
54e8959c L |
13511 | || !bfd_hide_sym_by_version (info->version_info, |
13512 | h->root.root.string))))) | |
715df9b8 EB |
13513 | h->root.u.def.section->flags |= SEC_KEEP; |
13514 | ||
13515 | return TRUE; | |
13516 | } | |
3b36f7e6 | 13517 | |
74f0fb50 AM |
13518 | /* Keep all sections containing symbols undefined on the command-line, |
13519 | and the section containing the entry symbol. */ | |
13520 | ||
13521 | void | |
13522 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
13523 | { | |
13524 | struct bfd_sym_chain *sym; | |
13525 | ||
13526 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
13527 | { | |
13528 | struct elf_link_hash_entry *h; | |
13529 | ||
13530 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
13531 | FALSE, FALSE, FALSE); | |
13532 | ||
13533 | if (h != NULL | |
13534 | && (h->root.type == bfd_link_hash_defined | |
13535 | || h->root.type == bfd_link_hash_defweak) | |
f02cb058 AM |
13536 | && !bfd_is_abs_section (h->root.u.def.section) |
13537 | && !bfd_is_und_section (h->root.u.def.section)) | |
74f0fb50 AM |
13538 | h->root.u.def.section->flags |= SEC_KEEP; |
13539 | } | |
13540 | } | |
13541 | ||
2f0c68f2 CM |
13542 | bfd_boolean |
13543 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, | |
13544 | struct bfd_link_info *info) | |
13545 | { | |
13546 | bfd *ibfd = info->input_bfds; | |
13547 | ||
13548 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
13549 | { | |
13550 | asection *sec; | |
13551 | struct elf_reloc_cookie cookie; | |
13552 | ||
13553 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13554 | continue; | |
57963c05 AM |
13555 | sec = ibfd->sections; |
13556 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13557 | continue; | |
2f0c68f2 CM |
13558 | |
13559 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
13560 | return FALSE; | |
13561 | ||
13562 | for (sec = ibfd->sections; sec; sec = sec->next) | |
13563 | { | |
13564 | if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") | |
13565 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) | |
13566 | { | |
13567 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
13568 | fini_reloc_cookie_rels (&cookie, sec); | |
13569 | } | |
13570 | } | |
13571 | } | |
13572 | return TRUE; | |
13573 | } | |
13574 | ||
c152c796 AM |
13575 | /* Do mark and sweep of unused sections. */ |
13576 | ||
13577 | bfd_boolean | |
13578 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
13579 | { | |
13580 | bfd_boolean ok = TRUE; | |
13581 | bfd *sub; | |
6a5bb875 | 13582 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 13583 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 13584 | struct elf_link_hash_table *htab; |
c152c796 | 13585 | |
64d03ab5 | 13586 | if (!bed->can_gc_sections |
715df9b8 | 13587 | || !is_elf_hash_table (info->hash)) |
c152c796 | 13588 | { |
9793eb77 | 13589 | _bfd_error_handler(_("warning: gc-sections option ignored")); |
c152c796 AM |
13590 | return TRUE; |
13591 | } | |
13592 | ||
74f0fb50 | 13593 | bed->gc_keep (info); |
da44f4e5 | 13594 | htab = elf_hash_table (info); |
74f0fb50 | 13595 | |
9d0a14d3 RS |
13596 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
13597 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
13598 | for (sub = info->input_bfds; |
13599 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
13600 | sub = sub->link.next) | |
9d0a14d3 RS |
13601 | { |
13602 | asection *sec; | |
13603 | struct elf_reloc_cookie cookie; | |
13604 | ||
57963c05 AM |
13605 | sec = sub->sections; |
13606 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13607 | continue; | |
9d0a14d3 | 13608 | sec = bfd_get_section_by_name (sub, ".eh_frame"); |
9a2a56cc | 13609 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
13610 | { |
13611 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
13612 | if (elf_section_data (sec)->sec_info |
13613 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
13614 | elf_eh_frame_section (sub) = sec; |
13615 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 13616 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
13617 | } |
13618 | } | |
9d0a14d3 | 13619 | |
c152c796 | 13620 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 13621 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 AM |
13622 | if (!ok) |
13623 | return FALSE; | |
13624 | ||
13625 | /* Kill the vtable relocations that were not used. */ | |
da44f4e5 | 13626 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); |
c152c796 AM |
13627 | if (!ok) |
13628 | return FALSE; | |
13629 | ||
715df9b8 | 13630 | /* Mark dynamically referenced symbols. */ |
22185505 | 13631 | if (htab->dynamic_sections_created || info->gc_keep_exported) |
da44f4e5 | 13632 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); |
c152c796 | 13633 | |
715df9b8 | 13634 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 13635 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 13636 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
13637 | { |
13638 | asection *o; | |
13639 | ||
b19a8f85 | 13640 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 13641 | || elf_object_id (sub) != elf_hash_table_id (htab) |
b19a8f85 | 13642 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 AM |
13643 | continue; |
13644 | ||
57963c05 AM |
13645 | o = sub->sections; |
13646 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13647 | continue; | |
13648 | ||
7f6ab9f8 AM |
13649 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
13650 | Also treat note sections as a root, if the section is not part | |
8b6f4cd3 L |
13651 | of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as |
13652 | well as FINI_ARRAY sections for ld -r. */ | |
c152c796 | 13653 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
13654 | if (!o->gc_mark |
13655 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 13656 | && ((o->flags & SEC_KEEP) != 0 |
8b6f4cd3 L |
13657 | || (bfd_link_relocatable (info) |
13658 | && ((elf_section_data (o)->this_hdr.sh_type | |
13659 | == SHT_PREINIT_ARRAY) | |
13660 | || (elf_section_data (o)->this_hdr.sh_type | |
13661 | == SHT_INIT_ARRAY) | |
13662 | || (elf_section_data (o)->this_hdr.sh_type | |
13663 | == SHT_FINI_ARRAY))) | |
7f6ab9f8 AM |
13664 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
13665 | && elf_next_in_group (o) == NULL ))) | |
13666 | { | |
13667 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
13668 | return FALSE; | |
13669 | } | |
c152c796 AM |
13670 | } |
13671 | ||
6a5bb875 | 13672 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 13673 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 13674 | |
c152c796 | 13675 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 13676 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
13677 | } |
13678 | \f | |
13679 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
13680 | ||
13681 | bfd_boolean | |
13682 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
13683 | asection *sec, | |
13684 | struct elf_link_hash_entry *h, | |
13685 | bfd_vma offset) | |
13686 | { | |
13687 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
13688 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 13689 | size_t extsymcount; |
c152c796 AM |
13690 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
13691 | ||
13692 | /* The sh_info field of the symtab header tells us where the | |
13693 | external symbols start. We don't care about the local symbols at | |
13694 | this point. */ | |
13695 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
13696 | if (!elf_bad_symtab (abfd)) | |
13697 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
13698 | ||
13699 | sym_hashes = elf_sym_hashes (abfd); | |
13700 | sym_hashes_end = sym_hashes + extsymcount; | |
13701 | ||
13702 | /* Hunt down the child symbol, which is in this section at the same | |
13703 | offset as the relocation. */ | |
13704 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
13705 | { | |
13706 | if ((child = *search) != NULL | |
13707 | && (child->root.type == bfd_link_hash_defined | |
13708 | || child->root.type == bfd_link_hash_defweak) | |
13709 | && child->root.u.def.section == sec | |
13710 | && child->root.u.def.value == offset) | |
13711 | goto win; | |
13712 | } | |
13713 | ||
695344c0 | 13714 | /* xgettext:c-format */ |
9793eb77 | 13715 | _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"), |
2dcf00ce | 13716 | abfd, sec, (uint64_t) offset); |
c152c796 AM |
13717 | bfd_set_error (bfd_error_invalid_operation); |
13718 | return FALSE; | |
13719 | ||
13720 | win: | |
cbd0eecf | 13721 | if (!child->u2.vtable) |
f6e332e6 | 13722 | { |
cbd0eecf L |
13723 | child->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13724 | bfd_zalloc (abfd, sizeof (*child->u2.vtable))); | |
13725 | if (!child->u2.vtable) | |
f6e332e6 AM |
13726 | return FALSE; |
13727 | } | |
c152c796 AM |
13728 | if (!h) |
13729 | { | |
13730 | /* This *should* only be the absolute section. It could potentially | |
13731 | be that someone has defined a non-global vtable though, which | |
13732 | would be bad. It isn't worth paging in the local symbols to be | |
13733 | sure though; that case should simply be handled by the assembler. */ | |
13734 | ||
cbd0eecf | 13735 | child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
13736 | } |
13737 | else | |
cbd0eecf | 13738 | child->u2.vtable->parent = h; |
c152c796 AM |
13739 | |
13740 | return TRUE; | |
13741 | } | |
13742 | ||
13743 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
13744 | ||
13745 | bfd_boolean | |
a0ea3a14 | 13746 | bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, |
c152c796 AM |
13747 | struct elf_link_hash_entry *h, |
13748 | bfd_vma addend) | |
13749 | { | |
13750 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13751 | unsigned int log_file_align = bed->s->log_file_align; | |
13752 | ||
a0ea3a14 L |
13753 | if (!h) |
13754 | { | |
13755 | /* xgettext:c-format */ | |
13756 | _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"), | |
13757 | abfd, sec); | |
13758 | bfd_set_error (bfd_error_bad_value); | |
13759 | return FALSE; | |
13760 | } | |
13761 | ||
cbd0eecf | 13762 | if (!h->u2.vtable) |
f6e332e6 | 13763 | { |
cbd0eecf L |
13764 | h->u2.vtable = ((struct elf_link_virtual_table_entry *) |
13765 | bfd_zalloc (abfd, sizeof (*h->u2.vtable))); | |
13766 | if (!h->u2.vtable) | |
f6e332e6 AM |
13767 | return FALSE; |
13768 | } | |
13769 | ||
cbd0eecf | 13770 | if (addend >= h->u2.vtable->size) |
c152c796 AM |
13771 | { |
13772 | size_t size, bytes, file_align; | |
cbd0eecf | 13773 | bfd_boolean *ptr = h->u2.vtable->used; |
c152c796 AM |
13774 | |
13775 | /* While the symbol is undefined, we have to be prepared to handle | |
13776 | a zero size. */ | |
13777 | file_align = 1 << log_file_align; | |
13778 | if (h->root.type == bfd_link_hash_undefined) | |
13779 | size = addend + file_align; | |
13780 | else | |
13781 | { | |
13782 | size = h->size; | |
13783 | if (addend >= size) | |
13784 | { | |
13785 | /* Oops! We've got a reference past the defined end of | |
13786 | the table. This is probably a bug -- shall we warn? */ | |
13787 | size = addend + file_align; | |
13788 | } | |
13789 | } | |
13790 | size = (size + file_align - 1) & -file_align; | |
13791 | ||
13792 | /* Allocate one extra entry for use as a "done" flag for the | |
13793 | consolidation pass. */ | |
13794 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
13795 | ||
13796 | if (ptr) | |
13797 | { | |
a50b1753 | 13798 | ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
13799 | |
13800 | if (ptr != NULL) | |
13801 | { | |
13802 | size_t oldbytes; | |
13803 | ||
cbd0eecf | 13804 | oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) |
c152c796 AM |
13805 | * sizeof (bfd_boolean)); |
13806 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
13807 | } | |
13808 | } | |
13809 | else | |
a50b1753 | 13810 | ptr = (bfd_boolean *) bfd_zmalloc (bytes); |
c152c796 AM |
13811 | |
13812 | if (ptr == NULL) | |
13813 | return FALSE; | |
13814 | ||
13815 | /* And arrange for that done flag to be at index -1. */ | |
cbd0eecf L |
13816 | h->u2.vtable->used = ptr + 1; |
13817 | h->u2.vtable->size = size; | |
c152c796 AM |
13818 | } |
13819 | ||
cbd0eecf | 13820 | h->u2.vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
13821 | |
13822 | return TRUE; | |
13823 | } | |
13824 | ||
ae17ab41 CM |
13825 | /* Map an ELF section header flag to its corresponding string. */ |
13826 | typedef struct | |
13827 | { | |
13828 | char *flag_name; | |
13829 | flagword flag_value; | |
13830 | } elf_flags_to_name_table; | |
13831 | ||
13832 | static elf_flags_to_name_table elf_flags_to_names [] = | |
13833 | { | |
13834 | { "SHF_WRITE", SHF_WRITE }, | |
13835 | { "SHF_ALLOC", SHF_ALLOC }, | |
13836 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
13837 | { "SHF_MERGE", SHF_MERGE }, | |
13838 | { "SHF_STRINGS", SHF_STRINGS }, | |
13839 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
13840 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
13841 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
13842 | { "SHF_GROUP", SHF_GROUP }, | |
13843 | { "SHF_TLS", SHF_TLS }, | |
13844 | { "SHF_MASKOS", SHF_MASKOS }, | |
13845 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
13846 | }; | |
13847 | ||
b9c361e0 JL |
13848 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
13849 | bfd_boolean | |
ae17ab41 | 13850 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 13851 | struct flag_info *flaginfo, |
b9c361e0 | 13852 | asection *section) |
ae17ab41 | 13853 | { |
8b127cbc | 13854 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 13855 | |
8b127cbc | 13856 | if (!flaginfo->flags_initialized) |
ae17ab41 | 13857 | { |
8b127cbc AM |
13858 | bfd *obfd = info->output_bfd; |
13859 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
13860 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
13861 | int with_hex = 0; |
13862 | int without_hex = 0; | |
13863 | ||
8b127cbc | 13864 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 13865 | { |
b9c361e0 | 13866 | unsigned i; |
8b127cbc | 13867 | flagword (*lookup) (char *); |
ae17ab41 | 13868 | |
8b127cbc AM |
13869 | lookup = bed->elf_backend_lookup_section_flags_hook; |
13870 | if (lookup != NULL) | |
ae17ab41 | 13871 | { |
8b127cbc | 13872 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
13873 | |
13874 | if (hexval != 0) | |
13875 | { | |
13876 | if (tf->with == with_flags) | |
13877 | with_hex |= hexval; | |
13878 | else if (tf->with == without_flags) | |
13879 | without_hex |= hexval; | |
13880 | tf->valid = TRUE; | |
13881 | continue; | |
13882 | } | |
ae17ab41 | 13883 | } |
8b127cbc | 13884 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 13885 | { |
8b127cbc | 13886 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
13887 | { |
13888 | if (tf->with == with_flags) | |
13889 | with_hex |= elf_flags_to_names[i].flag_value; | |
13890 | else if (tf->with == without_flags) | |
13891 | without_hex |= elf_flags_to_names[i].flag_value; | |
13892 | tf->valid = TRUE; | |
13893 | break; | |
13894 | } | |
13895 | } | |
8b127cbc | 13896 | if (!tf->valid) |
b9c361e0 | 13897 | { |
68ffbac6 | 13898 | info->callbacks->einfo |
9793eb77 | 13899 | (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
b9c361e0 | 13900 | return FALSE; |
ae17ab41 CM |
13901 | } |
13902 | } | |
8b127cbc AM |
13903 | flaginfo->flags_initialized = TRUE; |
13904 | flaginfo->only_with_flags |= with_hex; | |
13905 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 13906 | } |
ae17ab41 | 13907 | |
8b127cbc | 13908 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
b9c361e0 JL |
13909 | return FALSE; |
13910 | ||
8b127cbc | 13911 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
b9c361e0 JL |
13912 | return FALSE; |
13913 | ||
13914 | return TRUE; | |
ae17ab41 CM |
13915 | } |
13916 | ||
c152c796 AM |
13917 | struct alloc_got_off_arg { |
13918 | bfd_vma gotoff; | |
10455f89 | 13919 | struct bfd_link_info *info; |
c152c796 AM |
13920 | }; |
13921 | ||
13922 | /* We need a special top-level link routine to convert got reference counts | |
13923 | to real got offsets. */ | |
13924 | ||
13925 | static bfd_boolean | |
13926 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
13927 | { | |
a50b1753 | 13928 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
13929 | bfd *obfd = gofarg->info->output_bfd; |
13930 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 13931 | |
c152c796 AM |
13932 | if (h->got.refcount > 0) |
13933 | { | |
13934 | h->got.offset = gofarg->gotoff; | |
10455f89 | 13935 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
13936 | } |
13937 | else | |
13938 | h->got.offset = (bfd_vma) -1; | |
13939 | ||
13940 | return TRUE; | |
13941 | } | |
13942 | ||
13943 | /* And an accompanying bit to work out final got entry offsets once | |
13944 | we're done. Should be called from final_link. */ | |
13945 | ||
13946 | bfd_boolean | |
13947 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
13948 | struct bfd_link_info *info) | |
13949 | { | |
13950 | bfd *i; | |
13951 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
13952 | bfd_vma gotoff; | |
c152c796 AM |
13953 | struct alloc_got_off_arg gofarg; |
13954 | ||
10455f89 HPN |
13955 | BFD_ASSERT (abfd == info->output_bfd); |
13956 | ||
c152c796 AM |
13957 | if (! is_elf_hash_table (info->hash)) |
13958 | return FALSE; | |
13959 | ||
13960 | /* The GOT offset is relative to the .got section, but the GOT header is | |
13961 | put into the .got.plt section, if the backend uses it. */ | |
13962 | if (bed->want_got_plt) | |
13963 | gotoff = 0; | |
13964 | else | |
13965 | gotoff = bed->got_header_size; | |
13966 | ||
13967 | /* Do the local .got entries first. */ | |
c72f2fb2 | 13968 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
13969 | { |
13970 | bfd_signed_vma *local_got; | |
ef53be89 | 13971 | size_t j, locsymcount; |
c152c796 AM |
13972 | Elf_Internal_Shdr *symtab_hdr; |
13973 | ||
13974 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
13975 | continue; | |
13976 | ||
13977 | local_got = elf_local_got_refcounts (i); | |
13978 | if (!local_got) | |
13979 | continue; | |
13980 | ||
13981 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
13982 | if (elf_bad_symtab (i)) | |
13983 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13984 | else | |
13985 | locsymcount = symtab_hdr->sh_info; | |
13986 | ||
13987 | for (j = 0; j < locsymcount; ++j) | |
13988 | { | |
13989 | if (local_got[j] > 0) | |
13990 | { | |
13991 | local_got[j] = gotoff; | |
10455f89 | 13992 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
13993 | } |
13994 | else | |
13995 | local_got[j] = (bfd_vma) -1; | |
13996 | } | |
13997 | } | |
13998 | ||
13999 | /* Then the global .got entries. .plt refcounts are handled by | |
14000 | adjust_dynamic_symbol */ | |
14001 | gofarg.gotoff = gotoff; | |
10455f89 | 14002 | gofarg.info = info; |
c152c796 AM |
14003 | elf_link_hash_traverse (elf_hash_table (info), |
14004 | elf_gc_allocate_got_offsets, | |
14005 | &gofarg); | |
14006 | return TRUE; | |
14007 | } | |
14008 | ||
14009 | /* Many folk need no more in the way of final link than this, once | |
14010 | got entry reference counting is enabled. */ | |
14011 | ||
14012 | bfd_boolean | |
14013 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
14014 | { | |
14015 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
14016 | return FALSE; | |
14017 | ||
14018 | /* Invoke the regular ELF backend linker to do all the work. */ | |
14019 | return bfd_elf_final_link (abfd, info); | |
14020 | } | |
14021 | ||
14022 | bfd_boolean | |
14023 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
14024 | { | |
a50b1753 | 14025 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
14026 | |
14027 | if (rcookie->bad_symtab) | |
14028 | rcookie->rel = rcookie->rels; | |
14029 | ||
14030 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
14031 | { | |
14032 | unsigned long r_symndx; | |
14033 | ||
14034 | if (! rcookie->bad_symtab) | |
14035 | if (rcookie->rel->r_offset > offset) | |
14036 | return FALSE; | |
14037 | if (rcookie->rel->r_offset != offset) | |
14038 | continue; | |
14039 | ||
14040 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 14041 | if (r_symndx == STN_UNDEF) |
c152c796 AM |
14042 | return TRUE; |
14043 | ||
14044 | if (r_symndx >= rcookie->locsymcount | |
14045 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
14046 | { | |
14047 | struct elf_link_hash_entry *h; | |
14048 | ||
14049 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
14050 | ||
14051 | while (h->root.type == bfd_link_hash_indirect | |
14052 | || h->root.type == bfd_link_hash_warning) | |
14053 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
14054 | ||
14055 | if ((h->root.type == bfd_link_hash_defined | |
14056 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
14057 | && (h->root.u.def.section->owner != rcookie->abfd |
14058 | || h->root.u.def.section->kept_section != NULL | |
14059 | || discarded_section (h->root.u.def.section))) | |
c152c796 | 14060 | return TRUE; |
c152c796 AM |
14061 | } |
14062 | else | |
14063 | { | |
14064 | /* It's not a relocation against a global symbol, | |
14065 | but it could be a relocation against a local | |
14066 | symbol for a discarded section. */ | |
14067 | asection *isec; | |
14068 | Elf_Internal_Sym *isym; | |
14069 | ||
14070 | /* Need to: get the symbol; get the section. */ | |
14071 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 14072 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
14073 | if (isec != NULL |
14074 | && (isec->kept_section != NULL | |
14075 | || discarded_section (isec))) | |
cb33740c | 14076 | return TRUE; |
c152c796 AM |
14077 | } |
14078 | return FALSE; | |
14079 | } | |
14080 | return FALSE; | |
14081 | } | |
14082 | ||
14083 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
14084 | Returns -1 on error, 1 if any section's size was changed, 0 if |
14085 | nothing changed. This function assumes that the relocations are in | |
14086 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 14087 | |
75938853 | 14088 | int |
c152c796 AM |
14089 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
14090 | { | |
14091 | struct elf_reloc_cookie cookie; | |
18cd5bce | 14092 | asection *o; |
c152c796 | 14093 | bfd *abfd; |
75938853 | 14094 | int changed = 0; |
c152c796 AM |
14095 | |
14096 | if (info->traditional_format | |
14097 | || !is_elf_hash_table (info->hash)) | |
75938853 | 14098 | return 0; |
c152c796 | 14099 | |
18cd5bce AM |
14100 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
14101 | if (o != NULL) | |
c152c796 | 14102 | { |
18cd5bce | 14103 | asection *i; |
c152c796 | 14104 | |
18cd5bce | 14105 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 14106 | { |
18cd5bce AM |
14107 | if (i->size == 0 |
14108 | || i->reloc_count == 0 | |
14109 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
14110 | continue; | |
c152c796 | 14111 | |
18cd5bce AM |
14112 | abfd = i->owner; |
14113 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14114 | continue; | |
c152c796 | 14115 | |
18cd5bce | 14116 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 14117 | return -1; |
c152c796 | 14118 | |
18cd5bce AM |
14119 | if (_bfd_discard_section_stabs (abfd, i, |
14120 | elf_section_data (i)->sec_info, | |
5241d853 RS |
14121 | bfd_elf_reloc_symbol_deleted_p, |
14122 | &cookie)) | |
75938853 | 14123 | changed = 1; |
18cd5bce AM |
14124 | |
14125 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14126 | } |
18cd5bce AM |
14127 | } |
14128 | ||
2f0c68f2 CM |
14129 | o = NULL; |
14130 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
14131 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
14132 | if (o != NULL) |
14133 | { | |
14134 | asection *i; | |
d7153c4a | 14135 | int eh_changed = 0; |
79a94a2a | 14136 | unsigned int eh_alignment; |
c152c796 | 14137 | |
18cd5bce | 14138 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 14139 | { |
18cd5bce AM |
14140 | if (i->size == 0) |
14141 | continue; | |
14142 | ||
14143 | abfd = i->owner; | |
14144 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14145 | continue; | |
14146 | ||
14147 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 14148 | return -1; |
18cd5bce AM |
14149 | |
14150 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
14151 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
14152 | bfd_elf_reloc_symbol_deleted_p, |
14153 | &cookie)) | |
d7153c4a AM |
14154 | { |
14155 | eh_changed = 1; | |
14156 | if (i->size != i->rawsize) | |
14157 | changed = 1; | |
14158 | } | |
18cd5bce AM |
14159 | |
14160 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14161 | } |
9866ffe2 | 14162 | |
79a94a2a | 14163 | eh_alignment = 1 << o->alignment_power; |
9866ffe2 AM |
14164 | /* Skip over zero terminator, and prevent empty sections from |
14165 | adding alignment padding at the end. */ | |
14166 | for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) | |
14167 | if (i->size == 0) | |
14168 | i->flags |= SEC_EXCLUDE; | |
14169 | else if (i->size > 4) | |
14170 | break; | |
14171 | /* The last non-empty eh_frame section doesn't need padding. */ | |
14172 | if (i != NULL) | |
14173 | i = i->map_tail.s; | |
14174 | /* Any prior sections must pad the last FDE out to the output | |
14175 | section alignment. Otherwise we might have zero padding | |
14176 | between sections, which would be seen as a terminator. */ | |
14177 | for (; i != NULL; i = i->map_tail.s) | |
14178 | if (i->size == 4) | |
14179 | /* All but the last zero terminator should have been removed. */ | |
14180 | BFD_FAIL (); | |
14181 | else | |
14182 | { | |
14183 | bfd_size_type size | |
14184 | = (i->size + eh_alignment - 1) & -eh_alignment; | |
14185 | if (i->size != size) | |
af471f82 | 14186 | { |
9866ffe2 AM |
14187 | i->size = size; |
14188 | changed = 1; | |
14189 | eh_changed = 1; | |
af471f82 | 14190 | } |
9866ffe2 | 14191 | } |
d7153c4a AM |
14192 | if (eh_changed) |
14193 | elf_link_hash_traverse (elf_hash_table (info), | |
14194 | _bfd_elf_adjust_eh_frame_global_symbol, NULL); | |
18cd5bce | 14195 | } |
c152c796 | 14196 | |
18cd5bce AM |
14197 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
14198 | { | |
14199 | const struct elf_backend_data *bed; | |
57963c05 | 14200 | asection *s; |
c152c796 | 14201 | |
18cd5bce AM |
14202 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
14203 | continue; | |
57963c05 AM |
14204 | s = abfd->sections; |
14205 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14206 | continue; | |
18cd5bce AM |
14207 | |
14208 | bed = get_elf_backend_data (abfd); | |
14209 | ||
14210 | if (bed->elf_backend_discard_info != NULL) | |
14211 | { | |
14212 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 14213 | return -1; |
18cd5bce AM |
14214 | |
14215 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 14216 | changed = 1; |
18cd5bce AM |
14217 | |
14218 | fini_reloc_cookie (&cookie, abfd); | |
14219 | } | |
c152c796 AM |
14220 | } |
14221 | ||
2f0c68f2 CM |
14222 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
14223 | _bfd_elf_end_eh_frame_parsing (info); | |
14224 | ||
14225 | if (info->eh_frame_hdr_type | |
0e1862bb | 14226 | && !bfd_link_relocatable (info) |
c152c796 | 14227 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) |
75938853 | 14228 | changed = 1; |
c152c796 | 14229 | |
75938853 | 14230 | return changed; |
c152c796 | 14231 | } |
082b7297 | 14232 | |
43e1669b | 14233 | bfd_boolean |
0c511000 | 14234 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 14235 | asection *sec, |
c0f00686 | 14236 | struct bfd_link_info *info) |
082b7297 L |
14237 | { |
14238 | flagword flags; | |
c77ec726 | 14239 | const char *name, *key; |
082b7297 L |
14240 | struct bfd_section_already_linked *l; |
14241 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 14242 | |
c77ec726 AM |
14243 | if (sec->output_section == bfd_abs_section_ptr) |
14244 | return FALSE; | |
0c511000 | 14245 | |
c77ec726 | 14246 | flags = sec->flags; |
0c511000 | 14247 | |
c77ec726 AM |
14248 | /* Return if it isn't a linkonce section. A comdat group section |
14249 | also has SEC_LINK_ONCE set. */ | |
14250 | if ((flags & SEC_LINK_ONCE) == 0) | |
14251 | return FALSE; | |
0c511000 | 14252 | |
c77ec726 AM |
14253 | /* Don't put group member sections on our list of already linked |
14254 | sections. They are handled as a group via their group section. */ | |
14255 | if (elf_sec_group (sec) != NULL) | |
14256 | return FALSE; | |
0c511000 | 14257 | |
c77ec726 AM |
14258 | /* For a SHT_GROUP section, use the group signature as the key. */ |
14259 | name = sec->name; | |
14260 | if ((flags & SEC_GROUP) != 0 | |
14261 | && elf_next_in_group (sec) != NULL | |
14262 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
14263 | key = elf_group_name (elf_next_in_group (sec)); | |
14264 | else | |
14265 | { | |
14266 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
0c511000 | 14267 | if (CONST_STRNEQ (name, ".gnu.linkonce.") |
c77ec726 AM |
14268 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
14269 | key++; | |
0c511000 | 14270 | else |
c77ec726 AM |
14271 | /* Must be a user linkonce section that doesn't follow gcc's |
14272 | naming convention. In this case we won't be matching | |
14273 | single member groups. */ | |
14274 | key = name; | |
0c511000 | 14275 | } |
6d2cd210 | 14276 | |
c77ec726 | 14277 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
14278 | |
14279 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14280 | { | |
c2370991 | 14281 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
14282 | sections with a signature of <key> (<key> is some string), |
14283 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
14284 | Match like sections. LTO plugin sections are an exception. | |
14285 | They are always named .gnu.linkonce.t.<key> and match either | |
14286 | type of section. */ | |
14287 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
14288 | && ((flags & SEC_GROUP) != 0 | |
14289 | || strcmp (name, l->sec->name) == 0)) | |
14290 | || (l->sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
14291 | { |
14292 | /* The section has already been linked. See if we should | |
6d2cd210 | 14293 | issue a warning. */ |
c77ec726 AM |
14294 | if (!_bfd_handle_already_linked (sec, l, info)) |
14295 | return FALSE; | |
082b7297 | 14296 | |
c77ec726 | 14297 | if (flags & SEC_GROUP) |
3d7f7666 | 14298 | { |
c77ec726 AM |
14299 | asection *first = elf_next_in_group (sec); |
14300 | asection *s = first; | |
3d7f7666 | 14301 | |
c77ec726 | 14302 | while (s != NULL) |
3d7f7666 | 14303 | { |
c77ec726 AM |
14304 | s->output_section = bfd_abs_section_ptr; |
14305 | /* Record which group discards it. */ | |
14306 | s->kept_section = l->sec; | |
14307 | s = elf_next_in_group (s); | |
14308 | /* These lists are circular. */ | |
14309 | if (s == first) | |
14310 | break; | |
3d7f7666 L |
14311 | } |
14312 | } | |
082b7297 | 14313 | |
43e1669b | 14314 | return TRUE; |
082b7297 L |
14315 | } |
14316 | } | |
14317 | ||
c77ec726 AM |
14318 | /* A single member comdat group section may be discarded by a |
14319 | linkonce section and vice versa. */ | |
14320 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 14321 | { |
c77ec726 | 14322 | asection *first = elf_next_in_group (sec); |
c2370991 | 14323 | |
c77ec726 AM |
14324 | if (first != NULL && elf_next_in_group (first) == first) |
14325 | /* Check this single member group against linkonce sections. */ | |
14326 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14327 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14328 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
14329 | { | |
14330 | first->output_section = bfd_abs_section_ptr; | |
14331 | first->kept_section = l->sec; | |
14332 | sec->output_section = bfd_abs_section_ptr; | |
14333 | break; | |
14334 | } | |
14335 | } | |
14336 | else | |
14337 | /* Check this linkonce section against single member groups. */ | |
14338 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14339 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 14340 | { |
c77ec726 | 14341 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 14342 | |
c77ec726 AM |
14343 | if (first != NULL |
14344 | && elf_next_in_group (first) == first | |
14345 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
14346 | { | |
14347 | sec->output_section = bfd_abs_section_ptr; | |
14348 | sec->kept_section = first; | |
14349 | break; | |
14350 | } | |
6d2cd210 | 14351 | } |
0c511000 | 14352 | |
c77ec726 AM |
14353 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
14354 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
14355 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
14356 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
14357 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
14358 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
14359 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
14360 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
14361 | The reverse order cannot happen as there is never a bfd with only the | |
14362 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
14363 | matter as here were are looking only for cross-bfd sections. */ | |
14364 | ||
14365 | if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) | |
14366 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
14367 | if ((l->sec->flags & SEC_GROUP) == 0 | |
14368 | && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) | |
14369 | { | |
14370 | if (abfd != l->sec->owner) | |
14371 | sec->output_section = bfd_abs_section_ptr; | |
14372 | break; | |
14373 | } | |
80c29487 | 14374 | |
082b7297 | 14375 | /* This is the first section with this name. Record it. */ |
c77ec726 | 14376 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 14377 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 14378 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 14379 | } |
81e1b023 | 14380 | |
a4d8e49b L |
14381 | bfd_boolean |
14382 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) | |
14383 | { | |
14384 | return sym->st_shndx == SHN_COMMON; | |
14385 | } | |
14386 | ||
14387 | unsigned int | |
14388 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
14389 | { | |
14390 | return SHN_COMMON; | |
14391 | } | |
14392 | ||
14393 | asection * | |
14394 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
14395 | { | |
14396 | return bfd_com_section_ptr; | |
14397 | } | |
10455f89 HPN |
14398 | |
14399 | bfd_vma | |
14400 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
14401 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
14402 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
14403 | bfd *ibfd ATTRIBUTE_UNUSED, | |
14404 | unsigned long symndx ATTRIBUTE_UNUSED) | |
14405 | { | |
14406 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14407 | return bed->s->arch_size / 8; | |
14408 | } | |
83bac4b0 NC |
14409 | |
14410 | /* Routines to support the creation of dynamic relocs. */ | |
14411 | ||
83bac4b0 NC |
14412 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
14413 | ||
14414 | static const char * | |
14415 | get_dynamic_reloc_section_name (bfd * abfd, | |
14416 | asection * sec, | |
14417 | bfd_boolean is_rela) | |
14418 | { | |
ddcf1fcf BS |
14419 | char *name; |
14420 | const char *old_name = bfd_get_section_name (NULL, sec); | |
14421 | const char *prefix = is_rela ? ".rela" : ".rel"; | |
83bac4b0 | 14422 | |
ddcf1fcf | 14423 | if (old_name == NULL) |
83bac4b0 NC |
14424 | return NULL; |
14425 | ||
ddcf1fcf | 14426 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 14427 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
14428 | |
14429 | return name; | |
14430 | } | |
14431 | ||
14432 | /* Returns the dynamic reloc section associated with SEC. | |
14433 | If necessary compute the name of the dynamic reloc section based | |
14434 | on SEC's name (looked up in ABFD's string table) and the setting | |
14435 | of IS_RELA. */ | |
14436 | ||
14437 | asection * | |
14438 | _bfd_elf_get_dynamic_reloc_section (bfd * abfd, | |
14439 | asection * sec, | |
14440 | bfd_boolean is_rela) | |
14441 | { | |
14442 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14443 | ||
14444 | if (reloc_sec == NULL) | |
14445 | { | |
14446 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14447 | ||
14448 | if (name != NULL) | |
14449 | { | |
3d4d4302 | 14450 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
14451 | |
14452 | if (reloc_sec != NULL) | |
14453 | elf_section_data (sec)->sreloc = reloc_sec; | |
14454 | } | |
14455 | } | |
14456 | ||
14457 | return reloc_sec; | |
14458 | } | |
14459 | ||
14460 | /* Returns the dynamic reloc section associated with SEC. If the | |
14461 | section does not exist it is created and attached to the DYNOBJ | |
14462 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
14463 | structure. | |
f8076f98 | 14464 | |
83bac4b0 NC |
14465 | ALIGNMENT is the alignment for the newly created section and |
14466 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
14467 | or .rel.<SEC's name>. The section name is looked up in the | |
14468 | string table associated with ABFD. */ | |
14469 | ||
14470 | asection * | |
ca4be51c AM |
14471 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
14472 | bfd *dynobj, | |
14473 | unsigned int alignment, | |
14474 | bfd *abfd, | |
14475 | bfd_boolean is_rela) | |
83bac4b0 NC |
14476 | { |
14477 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
14478 | ||
14479 | if (reloc_sec == NULL) | |
14480 | { | |
14481 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
14482 | ||
14483 | if (name == NULL) | |
14484 | return NULL; | |
14485 | ||
3d4d4302 | 14486 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
14487 | |
14488 | if (reloc_sec == NULL) | |
14489 | { | |
3d4d4302 AM |
14490 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
14491 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
14492 | if ((sec->flags & SEC_ALLOC) != 0) |
14493 | flags |= SEC_ALLOC | SEC_LOAD; | |
14494 | ||
3d4d4302 | 14495 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
14496 | if (reloc_sec != NULL) |
14497 | { | |
8877b5e5 AM |
14498 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
14499 | name. Override as it may be wrong, eg. for a user | |
14500 | section named "auto" we'll get ".relauto" which is | |
14501 | seen to be a .rela section. */ | |
14502 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
83bac4b0 NC |
14503 | if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) |
14504 | reloc_sec = NULL; | |
14505 | } | |
14506 | } | |
14507 | ||
14508 | elf_section_data (sec)->sreloc = reloc_sec; | |
14509 | } | |
14510 | ||
14511 | return reloc_sec; | |
14512 | } | |
1338dd10 | 14513 | |
bffebb6b AM |
14514 | /* Copy the ELF symbol type and other attributes for a linker script |
14515 | assignment from HSRC to HDEST. Generally this should be treated as | |
14516 | if we found a strong non-dynamic definition for HDEST (except that | |
14517 | ld ignores multiple definition errors). */ | |
1338dd10 | 14518 | void |
bffebb6b AM |
14519 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
14520 | struct bfd_link_hash_entry *hdest, | |
14521 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 14522 | { |
bffebb6b AM |
14523 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
14524 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
14525 | Elf_Internal_Sym isym; | |
1338dd10 PB |
14526 | |
14527 | ehdest->type = ehsrc->type; | |
35fc36a8 | 14528 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
14529 | |
14530 | isym.st_other = ehsrc->other; | |
b8417128 | 14531 | elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); |
1338dd10 | 14532 | } |
351f65ca L |
14533 | |
14534 | /* Append a RELA relocation REL to section S in BFD. */ | |
14535 | ||
14536 | void | |
14537 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14538 | { | |
14539 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14540 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
14541 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
14542 | bed->s->swap_reloca_out (abfd, rel, loc); | |
14543 | } | |
14544 | ||
14545 | /* Append a REL relocation REL to section S in BFD. */ | |
14546 | ||
14547 | void | |
14548 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
14549 | { | |
14550 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14551 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
14552 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 14553 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 14554 | } |
7dba9362 AM |
14555 | |
14556 | /* Define __start, __stop, .startof. or .sizeof. symbol. */ | |
14557 | ||
14558 | struct bfd_link_hash_entry * | |
14559 | bfd_elf_define_start_stop (struct bfd_link_info *info, | |
14560 | const char *symbol, asection *sec) | |
14561 | { | |
487b6440 | 14562 | struct elf_link_hash_entry *h; |
7dba9362 | 14563 | |
487b6440 AM |
14564 | h = elf_link_hash_lookup (elf_hash_table (info), symbol, |
14565 | FALSE, FALSE, TRUE); | |
14566 | if (h != NULL | |
14567 | && (h->root.type == bfd_link_hash_undefined | |
14568 | || h->root.type == bfd_link_hash_undefweak | |
bf3077a6 | 14569 | || ((h->ref_regular || h->def_dynamic) && !h->def_regular))) |
7dba9362 | 14570 | { |
bf3077a6 | 14571 | bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic; |
487b6440 AM |
14572 | h->root.type = bfd_link_hash_defined; |
14573 | h->root.u.def.section = sec; | |
14574 | h->root.u.def.value = 0; | |
14575 | h->def_regular = 1; | |
14576 | h->def_dynamic = 0; | |
14577 | h->start_stop = 1; | |
14578 | h->u2.start_stop_section = sec; | |
14579 | if (symbol[0] == '.') | |
14580 | { | |
14581 | /* .startof. and .sizeof. symbols are local. */ | |
559192d8 AM |
14582 | const struct elf_backend_data *bed; |
14583 | bed = get_elf_backend_data (info->output_bfd); | |
14584 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
487b6440 | 14585 | } |
36b8fda5 AM |
14586 | else |
14587 | { | |
14588 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
14589 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; | |
bf3077a6 | 14590 | if (was_dynamic) |
36b8fda5 AM |
14591 | bfd_elf_link_record_dynamic_symbol (info, h); |
14592 | } | |
487b6440 | 14593 | return &h->root; |
7dba9362 | 14594 | } |
487b6440 | 14595 | return NULL; |
7dba9362 | 14596 | } |