]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
fd67aa11 | 2 | Copyright (C) 1995-2024 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 | |
4b69ce9b | 35 | #include <limits.h> |
4b69ce9b AM |
36 | #ifndef CHAR_BIT |
37 | #define CHAR_BIT 8 | |
38 | #endif | |
39 | ||
28caa186 AM |
40 | /* This struct is used to pass information to routines called via |
41 | elf_link_hash_traverse which must return failure. */ | |
42 | ||
43 | struct elf_info_failed | |
44 | { | |
45 | struct bfd_link_info *info; | |
0a1b45a2 | 46 | bool failed; |
28caa186 AM |
47 | }; |
48 | ||
49 | /* This structure is used to pass information to | |
50 | _bfd_elf_link_find_version_dependencies. */ | |
51 | ||
52 | struct elf_find_verdep_info | |
53 | { | |
54 | /* General link information. */ | |
55 | struct bfd_link_info *info; | |
56 | /* The number of dependencies. */ | |
57 | unsigned int vers; | |
58 | /* Whether we had a failure. */ | |
0a1b45a2 | 59 | bool failed; |
28caa186 AM |
60 | }; |
61 | ||
0a1b45a2 | 62 | static bool _bfd_elf_fix_symbol_flags |
28caa186 AM |
63 | (struct elf_link_hash_entry *, struct elf_info_failed *); |
64 | ||
2f0c68f2 CM |
65 | asection * |
66 | _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, | |
67 | unsigned long r_symndx, | |
0a1b45a2 | 68 | bool discard) |
2f0c68f2 CM |
69 | { |
70 | if (r_symndx >= cookie->locsymcount | |
71 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
72 | { | |
73 | struct elf_link_hash_entry *h; | |
74 | ||
75 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; | |
76 | ||
77 | while (h->root.type == bfd_link_hash_indirect | |
78 | || h->root.type == bfd_link_hash_warning) | |
79 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
80 | ||
81 | if ((h->root.type == bfd_link_hash_defined | |
82 | || h->root.type == bfd_link_hash_defweak) | |
83 | && discarded_section (h->root.u.def.section)) | |
07d6d2b8 | 84 | return h->root.u.def.section; |
2f0c68f2 CM |
85 | else |
86 | return NULL; | |
87 | } | |
88 | else | |
89 | { | |
90 | /* It's not a relocation against a global symbol, | |
91 | but it could be a relocation against a local | |
92 | symbol for a discarded section. */ | |
93 | asection *isec; | |
94 | Elf_Internal_Sym *isym; | |
95 | ||
96 | /* Need to: get the symbol; get the section. */ | |
97 | isym = &cookie->locsyms[r_symndx]; | |
98 | isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); | |
99 | if (isec != NULL | |
100 | && discard ? discarded_section (isec) : 1) | |
101 | return isec; | |
102 | } | |
103 | return NULL; | |
104 | } | |
105 | ||
d98685ac AM |
106 | /* Define a symbol in a dynamic linkage section. */ |
107 | ||
108 | struct elf_link_hash_entry * | |
109 | _bfd_elf_define_linkage_sym (bfd *abfd, | |
110 | struct bfd_link_info *info, | |
111 | asection *sec, | |
112 | const char *name) | |
113 | { | |
114 | struct elf_link_hash_entry *h; | |
115 | struct bfd_link_hash_entry *bh; | |
ccabcbe5 | 116 | const struct elf_backend_data *bed; |
d98685ac | 117 | |
0a1b45a2 | 118 | h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false); |
d98685ac AM |
119 | if (h != NULL) |
120 | { | |
121 | /* Zap symbol defined in an as-needed lib that wasn't linked. | |
122 | This is a symptom of a larger problem: Absolute symbols | |
123 | defined in shared libraries can't be overridden, because we | |
124 | lose the link to the bfd which is via the symbol section. */ | |
125 | h->root.type = bfd_link_hash_new; | |
ad32986f | 126 | bh = &h->root; |
d98685ac | 127 | } |
ad32986f NC |
128 | else |
129 | bh = NULL; | |
d98685ac | 130 | |
cf18fda4 | 131 | bed = get_elf_backend_data (abfd); |
d98685ac | 132 | if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, |
0a1b45a2 | 133 | sec, 0, NULL, false, bed->collect, |
d98685ac AM |
134 | &bh)) |
135 | return NULL; | |
136 | h = (struct elf_link_hash_entry *) bh; | |
ad32986f | 137 | BFD_ASSERT (h != NULL); |
d98685ac | 138 | h->def_regular = 1; |
e28df02b | 139 | h->non_elf = 0; |
12b2843a | 140 | h->root.linker_def = 1; |
d98685ac | 141 | h->type = STT_OBJECT; |
00b7642b AM |
142 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
143 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
d98685ac | 144 | |
0a1b45a2 | 145 | (*bed->elf_backend_hide_symbol) (info, h, true); |
d98685ac AM |
146 | return h; |
147 | } | |
148 | ||
0a1b45a2 | 149 | bool |
268b6b39 | 150 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
151 | { |
152 | flagword flags; | |
aad5d350 | 153 | asection *s; |
252b5132 | 154 | struct elf_link_hash_entry *h; |
9c5bfbb7 | 155 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 156 | struct elf_link_hash_table *htab = elf_hash_table (info); |
252b5132 RH |
157 | |
158 | /* This function may be called more than once. */ | |
ce558b89 | 159 | if (htab->sgot != NULL) |
0a1b45a2 | 160 | return true; |
252b5132 | 161 | |
e5a52504 | 162 | flags = bed->dynamic_sec_flags; |
252b5132 | 163 | |
14b2f831 AM |
164 | s = bfd_make_section_anyway_with_flags (abfd, |
165 | (bed->rela_plts_and_copies_p | |
166 | ? ".rela.got" : ".rel.got"), | |
167 | (bed->dynamic_sec_flags | |
168 | | SEC_READONLY)); | |
6de2ae4a | 169 | if (s == NULL |
fd361982 | 170 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 171 | return false; |
6de2ae4a | 172 | htab->srelgot = s; |
252b5132 | 173 | |
14b2f831 | 174 | s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); |
64e77c6d | 175 | if (s == NULL |
fd361982 | 176 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 177 | return false; |
64e77c6d L |
178 | htab->sgot = s; |
179 | ||
252b5132 RH |
180 | if (bed->want_got_plt) |
181 | { | |
14b2f831 | 182 | s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); |
252b5132 | 183 | if (s == NULL |
fd361982 | 184 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 185 | return false; |
6de2ae4a | 186 | htab->sgotplt = s; |
252b5132 RH |
187 | } |
188 | ||
64e77c6d L |
189 | /* The first bit of the global offset table is the header. */ |
190 | s->size += bed->got_header_size; | |
191 | ||
2517a57f AM |
192 | if (bed->want_got_sym) |
193 | { | |
194 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
195 | (or .got.plt) section. We don't do this in the linker script | |
196 | because we don't want to define the symbol if we are not creating | |
197 | a global offset table. */ | |
6de2ae4a L |
198 | h = _bfd_elf_define_linkage_sym (abfd, info, s, |
199 | "_GLOBAL_OFFSET_TABLE_"); | |
2517a57f | 200 | elf_hash_table (info)->hgot = h; |
d98685ac | 201 | if (h == NULL) |
0a1b45a2 | 202 | return false; |
2517a57f | 203 | } |
252b5132 | 204 | |
0a1b45a2 | 205 | return true; |
252b5132 RH |
206 | } |
207 | \f | |
7e9f0867 | 208 | /* Create a strtab to hold the dynamic symbol names. */ |
0a1b45a2 | 209 | static bool |
7e9f0867 AM |
210 | _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) |
211 | { | |
212 | struct elf_link_hash_table *hash_table; | |
213 | ||
214 | hash_table = elf_hash_table (info); | |
215 | if (hash_table->dynobj == NULL) | |
6cd255ca L |
216 | { |
217 | /* We may not set dynobj, an input file holding linker created | |
218 | dynamic sections to abfd, which may be a dynamic object with | |
219 | its own dynamic sections. We need to find a normal input file | |
220 | to hold linker created sections if possible. */ | |
221 | if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) | |
222 | { | |
223 | bfd *ibfd; | |
57963c05 | 224 | asection *s; |
6cd255ca | 225 | for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) |
6645479e | 226 | if ((ibfd->flags |
57963c05 AM |
227 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0 |
228 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
4de5434b | 229 | && elf_object_id (ibfd) == elf_hash_table_id (hash_table) |
57963c05 AM |
230 | && !((s = ibfd->sections) != NULL |
231 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)) | |
6cd255ca L |
232 | { |
233 | abfd = ibfd; | |
234 | break; | |
235 | } | |
236 | } | |
237 | hash_table->dynobj = abfd; | |
238 | } | |
7e9f0867 AM |
239 | |
240 | if (hash_table->dynstr == NULL) | |
241 | { | |
242 | hash_table->dynstr = _bfd_elf_strtab_init (); | |
243 | if (hash_table->dynstr == NULL) | |
0a1b45a2 | 244 | return false; |
7e9f0867 | 245 | } |
0a1b45a2 | 246 | return true; |
7e9f0867 AM |
247 | } |
248 | ||
45d6a902 AM |
249 | /* Create some sections which will be filled in with dynamic linking |
250 | information. ABFD is an input file which requires dynamic sections | |
251 | to be created. The dynamic sections take up virtual memory space | |
252 | when the final executable is run, so we need to create them before | |
253 | addresses are assigned to the output sections. We work out the | |
254 | actual contents and size of these sections later. */ | |
252b5132 | 255 | |
0a1b45a2 | 256 | bool |
268b6b39 | 257 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 258 | { |
45d6a902 | 259 | flagword flags; |
91d6fa6a | 260 | asection *s; |
9c5bfbb7 | 261 | const struct elf_backend_data *bed; |
9637f6ef | 262 | struct elf_link_hash_entry *h; |
252b5132 | 263 | |
0eddce27 | 264 | if (! is_elf_hash_table (info->hash)) |
0a1b45a2 | 265 | return false; |
45d6a902 AM |
266 | |
267 | if (elf_hash_table (info)->dynamic_sections_created) | |
0a1b45a2 | 268 | return true; |
45d6a902 | 269 | |
7e9f0867 | 270 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
0a1b45a2 | 271 | return false; |
45d6a902 | 272 | |
7e9f0867 | 273 | abfd = elf_hash_table (info)->dynobj; |
e5a52504 MM |
274 | bed = get_elf_backend_data (abfd); |
275 | ||
276 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
277 | |
278 | /* A dynamically linked executable has a .interp section, but a | |
279 | shared library does not. */ | |
9b8b325a | 280 | if (bfd_link_executable (info) && !info->nointerp) |
252b5132 | 281 | { |
14b2f831 AM |
282 | s = bfd_make_section_anyway_with_flags (abfd, ".interp", |
283 | flags | SEC_READONLY); | |
3496cb2a | 284 | if (s == NULL) |
0a1b45a2 | 285 | return false; |
45d6a902 | 286 | } |
bb0deeff | 287 | |
45d6a902 AM |
288 | /* Create sections to hold version informations. These are removed |
289 | if they are not needed. */ | |
14b2f831 AM |
290 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", |
291 | flags | SEC_READONLY); | |
45d6a902 | 292 | if (s == NULL |
fd361982 | 293 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 294 | return false; |
45d6a902 | 295 | |
14b2f831 AM |
296 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", |
297 | flags | SEC_READONLY); | |
45d6a902 | 298 | if (s == NULL |
fd361982 | 299 | || !bfd_set_section_alignment (s, 1)) |
0a1b45a2 | 300 | return false; |
45d6a902 | 301 | |
14b2f831 AM |
302 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", |
303 | flags | SEC_READONLY); | |
45d6a902 | 304 | if (s == NULL |
fd361982 | 305 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 306 | return false; |
45d6a902 | 307 | |
14b2f831 AM |
308 | s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", |
309 | flags | SEC_READONLY); | |
45d6a902 | 310 | if (s == NULL |
fd361982 | 311 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 312 | return false; |
cae1fbbb | 313 | elf_hash_table (info)->dynsym = s; |
45d6a902 | 314 | |
14b2f831 AM |
315 | s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", |
316 | flags | SEC_READONLY); | |
3496cb2a | 317 | if (s == NULL) |
0a1b45a2 | 318 | return false; |
45d6a902 | 319 | |
14b2f831 | 320 | s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); |
45d6a902 | 321 | if (s == NULL |
fd361982 | 322 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 323 | return false; |
45d6a902 AM |
324 | |
325 | /* The special symbol _DYNAMIC is always set to the start of the | |
77cfaee6 AM |
326 | .dynamic section. We could set _DYNAMIC in a linker script, but we |
327 | only want to define it if we are, in fact, creating a .dynamic | |
328 | section. We don't want to define it if there is no .dynamic | |
329 | section, since on some ELF platforms the start up code examines it | |
330 | to decide how to initialize the process. */ | |
9637f6ef L |
331 | h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); |
332 | elf_hash_table (info)->hdynamic = h; | |
333 | if (h == NULL) | |
0a1b45a2 | 334 | return false; |
45d6a902 | 335 | |
fdc90cb4 JJ |
336 | if (info->emit_hash) |
337 | { | |
14b2f831 AM |
338 | s = bfd_make_section_anyway_with_flags (abfd, ".hash", |
339 | flags | SEC_READONLY); | |
fdc90cb4 | 340 | if (s == NULL |
fd361982 | 341 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 342 | return false; |
fdc90cb4 JJ |
343 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; |
344 | } | |
345 | ||
f16a9783 | 346 | if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL) |
fdc90cb4 | 347 | { |
14b2f831 AM |
348 | s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", |
349 | flags | SEC_READONLY); | |
fdc90cb4 | 350 | if (s == NULL |
fd361982 | 351 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 352 | return false; |
fdc90cb4 JJ |
353 | /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: |
354 | 4 32-bit words followed by variable count of 64-bit words, then | |
355 | variable count of 32-bit words. */ | |
356 | if (bed->s->arch_size == 64) | |
357 | elf_section_data (s)->this_hdr.sh_entsize = 0; | |
358 | else | |
359 | elf_section_data (s)->this_hdr.sh_entsize = 4; | |
360 | } | |
45d6a902 | 361 | |
6a91be86 L |
362 | if (info->enable_dt_relr) |
363 | { | |
364 | s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn", | |
365 | (bed->dynamic_sec_flags | |
366 | | SEC_READONLY)); | |
367 | if (s == NULL | |
368 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) | |
369 | return false; | |
370 | elf_hash_table (info)->srelrdyn = s; | |
371 | } | |
372 | ||
45d6a902 AM |
373 | /* Let the backend create the rest of the sections. This lets the |
374 | backend set the right flags. The backend will normally create | |
375 | the .got and .plt sections. */ | |
894891db NC |
376 | if (bed->elf_backend_create_dynamic_sections == NULL |
377 | || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
0a1b45a2 | 378 | return false; |
45d6a902 | 379 | |
0a1b45a2 | 380 | elf_hash_table (info)->dynamic_sections_created = true; |
45d6a902 | 381 | |
0a1b45a2 | 382 | return true; |
45d6a902 AM |
383 | } |
384 | ||
385 | /* Create dynamic sections when linking against a dynamic object. */ | |
386 | ||
0a1b45a2 | 387 | bool |
268b6b39 | 388 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
389 | { |
390 | flagword flags, pltflags; | |
7325306f | 391 | struct elf_link_hash_entry *h; |
45d6a902 | 392 | asection *s; |
9c5bfbb7 | 393 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6de2ae4a | 394 | struct elf_link_hash_table *htab = elf_hash_table (info); |
45d6a902 | 395 | |
252b5132 RH |
396 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
397 | .rel[a].bss sections. */ | |
e5a52504 | 398 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
399 | |
400 | pltflags = flags; | |
252b5132 | 401 | if (bed->plt_not_loaded) |
6df4d94c MM |
402 | /* We do not clear SEC_ALLOC here because we still want the OS to |
403 | allocate space for the section; it's just that there's nothing | |
404 | to read in from the object file. */ | |
5d1634d7 | 405 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
406 | else |
407 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
408 | if (bed->plt_readonly) |
409 | pltflags |= SEC_READONLY; | |
410 | ||
14b2f831 | 411 | s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); |
252b5132 | 412 | if (s == NULL |
fd361982 | 413 | || !bfd_set_section_alignment (s, bed->plt_alignment)) |
0a1b45a2 | 414 | return false; |
6de2ae4a | 415 | htab->splt = s; |
252b5132 | 416 | |
d98685ac AM |
417 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the |
418 | .plt section. */ | |
7325306f RS |
419 | if (bed->want_plt_sym) |
420 | { | |
421 | h = _bfd_elf_define_linkage_sym (abfd, info, s, | |
422 | "_PROCEDURE_LINKAGE_TABLE_"); | |
423 | elf_hash_table (info)->hplt = h; | |
424 | if (h == NULL) | |
0a1b45a2 | 425 | return false; |
7325306f | 426 | } |
252b5132 | 427 | |
14b2f831 AM |
428 | s = bfd_make_section_anyway_with_flags (abfd, |
429 | (bed->rela_plts_and_copies_p | |
430 | ? ".rela.plt" : ".rel.plt"), | |
431 | flags | SEC_READONLY); | |
252b5132 | 432 | if (s == NULL |
fd361982 | 433 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 434 | return false; |
6de2ae4a | 435 | htab->srelplt = s; |
252b5132 RH |
436 | |
437 | if (! _bfd_elf_create_got_section (abfd, info)) | |
0a1b45a2 | 438 | return false; |
252b5132 | 439 | |
3018b441 RH |
440 | if (bed->want_dynbss) |
441 | { | |
442 | /* The .dynbss section is a place to put symbols which are defined | |
443 | by dynamic objects, are referenced by regular objects, and are | |
444 | not functions. We must allocate space for them in the process | |
445 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
446 | initialize them at run time. The linker script puts the .dynbss | |
447 | section into the .bss section of the final image. */ | |
14b2f831 | 448 | s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", |
afbf7e8e | 449 | SEC_ALLOC | SEC_LINKER_CREATED); |
3496cb2a | 450 | if (s == NULL) |
0a1b45a2 | 451 | return false; |
9d19e4fd | 452 | htab->sdynbss = s; |
252b5132 | 453 | |
5474d94f AM |
454 | if (bed->want_dynrelro) |
455 | { | |
456 | /* Similarly, but for symbols that were originally in read-only | |
afbf7e8e AM |
457 | sections. This section doesn't really need to have contents, |
458 | but make it like other .data.rel.ro sections. */ | |
5474d94f | 459 | s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro", |
afbf7e8e | 460 | flags); |
5474d94f | 461 | if (s == NULL) |
0a1b45a2 | 462 | return false; |
5474d94f AM |
463 | htab->sdynrelro = s; |
464 | } | |
465 | ||
3018b441 | 466 | /* The .rel[a].bss section holds copy relocs. This section is not |
77cfaee6 AM |
467 | normally needed. We need to create it here, though, so that the |
468 | linker will map it to an output section. We can't just create it | |
469 | only if we need it, because we will not know whether we need it | |
470 | until we have seen all the input files, and the first time the | |
471 | main linker code calls BFD after examining all the input files | |
472 | (size_dynamic_sections) the input sections have already been | |
473 | mapped to the output sections. If the section turns out not to | |
474 | be needed, we can discard it later. We will never need this | |
475 | section when generating a shared object, since they do not use | |
476 | copy relocs. */ | |
9d19e4fd | 477 | if (bfd_link_executable (info)) |
3018b441 | 478 | { |
14b2f831 AM |
479 | s = bfd_make_section_anyway_with_flags (abfd, |
480 | (bed->rela_plts_and_copies_p | |
481 | ? ".rela.bss" : ".rel.bss"), | |
482 | flags | SEC_READONLY); | |
3018b441 | 483 | if (s == NULL |
fd361982 | 484 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 485 | return false; |
9d19e4fd | 486 | htab->srelbss = s; |
5474d94f AM |
487 | |
488 | if (bed->want_dynrelro) | |
489 | { | |
490 | s = (bfd_make_section_anyway_with_flags | |
491 | (abfd, (bed->rela_plts_and_copies_p | |
492 | ? ".rela.data.rel.ro" : ".rel.data.rel.ro"), | |
493 | flags | SEC_READONLY)); | |
494 | if (s == NULL | |
fd361982 | 495 | || !bfd_set_section_alignment (s, bed->s->log_file_align)) |
0a1b45a2 | 496 | return false; |
5474d94f AM |
497 | htab->sreldynrelro = s; |
498 | } | |
3018b441 | 499 | } |
252b5132 RH |
500 | } |
501 | ||
0a1b45a2 | 502 | return true; |
252b5132 RH |
503 | } |
504 | \f | |
252b5132 RH |
505 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
506 | read the input files, since we need to have a list of all of them | |
507 | before we can determine the final sizes of the output sections. | |
508 | Note that we may actually call this function even though we are not | |
509 | going to output any dynamic symbols; in some cases we know that a | |
510 | symbol should be in the dynamic symbol table, but only if there is | |
511 | one. */ | |
512 | ||
0a1b45a2 | 513 | bool |
c152c796 AM |
514 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
515 | struct elf_link_hash_entry *h) | |
252b5132 RH |
516 | { |
517 | if (h->dynindx == -1) | |
518 | { | |
2b0f7ef9 | 519 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 520 | char *p; |
252b5132 | 521 | const char *name; |
ef53be89 | 522 | size_t indx; |
252b5132 | 523 | |
a896df97 AM |
524 | if (h->root.type == bfd_link_hash_defined |
525 | || h->root.type == bfd_link_hash_defweak) | |
526 | { | |
527 | /* An IR symbol should not be made dynamic. */ | |
528 | if (h->root.u.def.section != NULL | |
529 | && h->root.u.def.section->owner != NULL | |
530 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0) | |
0a1b45a2 | 531 | return true; |
a896df97 AM |
532 | } |
533 | ||
7a13edea NC |
534 | /* XXX: The ABI draft says the linker must turn hidden and |
535 | internal symbols into STB_LOCAL symbols when producing the | |
536 | DSO. However, if ld.so honors st_other in the dynamic table, | |
537 | this would not be necessary. */ | |
538 | switch (ELF_ST_VISIBILITY (h->other)) | |
539 | { | |
540 | case STV_INTERNAL: | |
541 | case STV_HIDDEN: | |
9d6eee78 L |
542 | if (h->root.type != bfd_link_hash_undefined |
543 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 544 | { |
f5385ebf | 545 | h->forced_local = 1; |
c8bad65e AM |
546 | if (!elf_hash_table (info)->is_relocatable_executable |
547 | || ((h->root.type == bfd_link_hash_defined | |
548 | || h->root.type == bfd_link_hash_defweak) | |
c4566785 | 549 | && h->root.u.def.section->owner != NULL |
c8bad65e AM |
550 | && h->root.u.def.section->owner->no_export) |
551 | || (h->root.type == bfd_link_hash_common | |
c4566785 | 552 | && h->root.u.c.p->section->owner != NULL |
c8bad65e | 553 | && h->root.u.c.p->section->owner->no_export)) |
0a1b45a2 | 554 | return true; |
7a13edea | 555 | } |
0444bdd4 | 556 | |
7a13edea NC |
557 | default: |
558 | break; | |
559 | } | |
560 | ||
252b5132 RH |
561 | h->dynindx = elf_hash_table (info)->dynsymcount; |
562 | ++elf_hash_table (info)->dynsymcount; | |
563 | ||
564 | dynstr = elf_hash_table (info)->dynstr; | |
565 | if (dynstr == NULL) | |
566 | { | |
567 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 568 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 569 | if (dynstr == NULL) |
0a1b45a2 | 570 | return false; |
252b5132 RH |
571 | } |
572 | ||
573 | /* We don't put any version information in the dynamic string | |
aad5d350 | 574 | table. */ |
252b5132 RH |
575 | name = h->root.root.string; |
576 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
577 | if (p != NULL) |
578 | /* We know that the p points into writable memory. In fact, | |
579 | there are only a few symbols that have read-only names, being | |
580 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
581 | by the backends. Most symbols will have names pointing into | |
582 | an ELF string table read from a file, or to objalloc memory. */ | |
583 | *p = 0; | |
584 | ||
585 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
586 | ||
587 | if (p != NULL) | |
588 | *p = ELF_VER_CHR; | |
252b5132 | 589 | |
ef53be89 | 590 | if (indx == (size_t) -1) |
0a1b45a2 | 591 | return false; |
252b5132 RH |
592 | h->dynstr_index = indx; |
593 | } | |
594 | ||
0a1b45a2 | 595 | return true; |
252b5132 | 596 | } |
45d6a902 | 597 | \f |
55255dae L |
598 | /* Mark a symbol dynamic. */ |
599 | ||
28caa186 | 600 | static void |
55255dae | 601 | bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, |
40b36307 L |
602 | struct elf_link_hash_entry *h, |
603 | Elf_Internal_Sym *sym) | |
55255dae | 604 | { |
40b36307 | 605 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
55255dae | 606 | |
40b36307 | 607 | /* It may be called more than once on the same H. */ |
0e1862bb | 608 | if(h->dynamic || bfd_link_relocatable (info)) |
55255dae L |
609 | return; |
610 | ||
40b36307 L |
611 | if ((info->dynamic_data |
612 | && (h->type == STT_OBJECT | |
b8871f35 | 613 | || h->type == STT_COMMON |
40b36307 | 614 | || (sym != NULL |
b8871f35 L |
615 | && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT |
616 | || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) | |
a0c8462f | 617 | || (d != NULL |
73ec947d | 618 | && h->non_elf |
40b36307 | 619 | && (*d->match) (&d->head, NULL, h->root.root.string))) |
416c34d6 L |
620 | { |
621 | h->dynamic = 1; | |
622 | /* NB: If a symbol is made dynamic by --dynamic-list, it has | |
623 | non-IR reference. */ | |
624 | h->root.non_ir_ref_dynamic = 1; | |
625 | } | |
55255dae L |
626 | } |
627 | ||
45d6a902 AM |
628 | /* Record an assignment to a symbol made by a linker script. We need |
629 | this in case some dynamic object refers to this symbol. */ | |
630 | ||
0a1b45a2 | 631 | bool |
fe21a8fc L |
632 | bfd_elf_record_link_assignment (bfd *output_bfd, |
633 | struct bfd_link_info *info, | |
268b6b39 | 634 | const char *name, |
0a1b45a2 AM |
635 | bool provide, |
636 | bool hidden) | |
45d6a902 | 637 | { |
00cbee0a | 638 | struct elf_link_hash_entry *h, *hv; |
4ea42fb7 | 639 | struct elf_link_hash_table *htab; |
00cbee0a | 640 | const struct elf_backend_data *bed; |
45d6a902 | 641 | |
0eddce27 | 642 | if (!is_elf_hash_table (info->hash)) |
0a1b45a2 | 643 | return true; |
45d6a902 | 644 | |
4ea42fb7 | 645 | htab = elf_hash_table (info); |
0a1b45a2 | 646 | h = elf_link_hash_lookup (htab, name, !provide, true, false); |
45d6a902 | 647 | if (h == NULL) |
4ea42fb7 | 648 | return provide; |
45d6a902 | 649 | |
8e2a4f11 AM |
650 | if (h->root.type == bfd_link_hash_warning) |
651 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
652 | ||
0f550b3d L |
653 | if (h->versioned == unknown) |
654 | { | |
655 | /* Set versioned if symbol version is unknown. */ | |
656 | char *version = strrchr (name, ELF_VER_CHR); | |
657 | if (version) | |
658 | { | |
659 | if (version > name && version[-1] != ELF_VER_CHR) | |
660 | h->versioned = versioned_hidden; | |
661 | else | |
662 | h->versioned = versioned; | |
663 | } | |
664 | } | |
665 | ||
73ec947d AM |
666 | /* Symbols defined in a linker script but not referenced anywhere |
667 | else will have non_elf set. */ | |
668 | if (h->non_elf) | |
669 | { | |
670 | bfd_elf_link_mark_dynamic_symbol (info, h, NULL); | |
671 | h->non_elf = 0; | |
672 | } | |
673 | ||
00cbee0a | 674 | switch (h->root.type) |
77cfaee6 | 675 | { |
00cbee0a L |
676 | case bfd_link_hash_defined: |
677 | case bfd_link_hash_defweak: | |
678 | case bfd_link_hash_common: | |
679 | break; | |
680 | case bfd_link_hash_undefweak: | |
681 | case bfd_link_hash_undefined: | |
682 | /* Since we're defining the symbol, don't let it seem to have not | |
683 | been defined. record_dynamic_symbol and size_dynamic_sections | |
684 | may depend on this. */ | |
4ea42fb7 | 685 | h->root.type = bfd_link_hash_new; |
77cfaee6 AM |
686 | if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) |
687 | bfd_link_repair_undef_list (&htab->root); | |
00cbee0a L |
688 | break; |
689 | case bfd_link_hash_new: | |
00cbee0a L |
690 | break; |
691 | case bfd_link_hash_indirect: | |
692 | /* We had a versioned symbol in a dynamic library. We make the | |
a0c8462f | 693 | the versioned symbol point to this one. */ |
00cbee0a L |
694 | bed = get_elf_backend_data (output_bfd); |
695 | hv = h; | |
696 | while (hv->root.type == bfd_link_hash_indirect | |
697 | || hv->root.type == bfd_link_hash_warning) | |
698 | hv = (struct elf_link_hash_entry *) hv->root.u.i.link; | |
699 | /* We don't need to update h->root.u since linker will set them | |
700 | later. */ | |
701 | h->root.type = bfd_link_hash_undefined; | |
702 | hv->root.type = bfd_link_hash_indirect; | |
703 | hv->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
704 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); | |
705 | break; | |
8e2a4f11 AM |
706 | default: |
707 | BFD_FAIL (); | |
0a1b45a2 | 708 | return false; |
55255dae | 709 | } |
45d6a902 AM |
710 | |
711 | /* If this symbol is being provided by the linker script, and it is | |
712 | currently defined by a dynamic object, but not by a regular | |
713 | object, then mark it as undefined so that the generic linker will | |
714 | force the correct value. */ | |
715 | if (provide | |
f5385ebf AM |
716 | && h->def_dynamic |
717 | && !h->def_regular) | |
45d6a902 AM |
718 | h->root.type = bfd_link_hash_undefined; |
719 | ||
48e30f52 L |
720 | /* If this symbol is currently defined by a dynamic object, but not |
721 | by a regular object, then clear out any version information because | |
722 | the symbol will not be associated with the dynamic object any | |
723 | more. */ | |
724 | if (h->def_dynamic && !h->def_regular) | |
b531344c MR |
725 | h->verinfo.verdef = NULL; |
726 | ||
727 | /* Make sure this symbol is not garbage collected. */ | |
728 | h->mark = 1; | |
45d6a902 | 729 | |
f5385ebf | 730 | h->def_regular = 1; |
45d6a902 | 731 | |
eb8476a6 | 732 | if (hidden) |
fe21a8fc | 733 | { |
91d6fa6a | 734 | bed = get_elf_backend_data (output_bfd); |
b8297068 AM |
735 | if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) |
736 | h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; | |
0a1b45a2 | 737 | (*bed->elf_backend_hide_symbol) (info, h, true); |
fe21a8fc L |
738 | } |
739 | ||
6fa3860b PB |
740 | /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects |
741 | and executables. */ | |
0e1862bb | 742 | if (!bfd_link_relocatable (info) |
6fa3860b PB |
743 | && h->dynindx != -1 |
744 | && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
745 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) | |
746 | h->forced_local = 1; | |
747 | ||
f5385ebf AM |
748 | if ((h->def_dynamic |
749 | || h->ref_dynamic | |
6b3b0ab8 L |
750 | || bfd_link_dll (info) |
751 | || elf_hash_table (info)->is_relocatable_executable) | |
34a87bb0 | 752 | && !h->forced_local |
45d6a902 AM |
753 | && h->dynindx == -1) |
754 | { | |
c152c796 | 755 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
0a1b45a2 | 756 | return false; |
45d6a902 AM |
757 | |
758 | /* If this is a weak defined symbol, and we know a corresponding | |
759 | real symbol from the same dynamic object, make sure the real | |
760 | symbol is also made into a dynamic symbol. */ | |
60d67dc8 | 761 | if (h->is_weakalias) |
45d6a902 | 762 | { |
60d67dc8 AM |
763 | struct elf_link_hash_entry *def = weakdef (h); |
764 | ||
765 | if (def->dynindx == -1 | |
766 | && !bfd_elf_link_record_dynamic_symbol (info, def)) | |
0a1b45a2 | 767 | return false; |
45d6a902 AM |
768 | } |
769 | } | |
770 | ||
0a1b45a2 | 771 | return true; |
45d6a902 | 772 | } |
42751cf3 | 773 | |
8c58d23b AM |
774 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
775 | success, and 2 on a failure caused by attempting to record a symbol | |
776 | in a discarded section, eg. a discarded link-once section symbol. */ | |
777 | ||
778 | int | |
c152c796 AM |
779 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
780 | bfd *input_bfd, | |
781 | long input_indx) | |
8c58d23b | 782 | { |
986f0783 | 783 | size_t amt; |
8c58d23b AM |
784 | struct elf_link_local_dynamic_entry *entry; |
785 | struct elf_link_hash_table *eht; | |
786 | struct elf_strtab_hash *dynstr; | |
ef53be89 | 787 | size_t dynstr_index; |
8c58d23b AM |
788 | char *name; |
789 | Elf_External_Sym_Shndx eshndx; | |
790 | char esym[sizeof (Elf64_External_Sym)]; | |
791 | ||
0eddce27 | 792 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
793 | return 0; |
794 | ||
795 | /* See if the entry exists already. */ | |
796 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
797 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
798 | return 1; | |
799 | ||
800 | amt = sizeof (*entry); | |
a50b1753 | 801 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); |
8c58d23b AM |
802 | if (entry == NULL) |
803 | return 0; | |
804 | ||
805 | /* Go find the symbol, so that we can find it's name. */ | |
806 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 807 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
808 | { |
809 | bfd_release (input_bfd, entry); | |
810 | return 0; | |
811 | } | |
812 | ||
813 | if (entry->isym.st_shndx != SHN_UNDEF | |
4fbb74a6 | 814 | && entry->isym.st_shndx < SHN_LORESERVE) |
8c58d23b AM |
815 | { |
816 | asection *s; | |
817 | ||
818 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
819 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
820 | { | |
821 | /* We can still bfd_release here as nothing has done another | |
822 | bfd_alloc. We can't do this later in this function. */ | |
823 | bfd_release (input_bfd, entry); | |
824 | return 2; | |
825 | } | |
826 | } | |
827 | ||
828 | name = (bfd_elf_string_from_elf_section | |
829 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
830 | entry->isym.st_name)); | |
831 | ||
832 | dynstr = elf_hash_table (info)->dynstr; | |
833 | if (dynstr == NULL) | |
834 | { | |
835 | /* Create a strtab to hold the dynamic symbol names. */ | |
836 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
837 | if (dynstr == NULL) | |
838 | return 0; | |
839 | } | |
840 | ||
0a1b45a2 | 841 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, false); |
ef53be89 | 842 | if (dynstr_index == (size_t) -1) |
8c58d23b AM |
843 | return 0; |
844 | entry->isym.st_name = dynstr_index; | |
845 | ||
846 | eht = elf_hash_table (info); | |
847 | ||
848 | entry->next = eht->dynlocal; | |
849 | eht->dynlocal = entry; | |
850 | entry->input_bfd = input_bfd; | |
851 | entry->input_indx = input_indx; | |
852 | eht->dynsymcount++; | |
853 | ||
854 | /* Whatever binding the symbol had before, it's now local. */ | |
855 | entry->isym.st_info | |
856 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
857 | ||
858 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
859 | ||
860 | return 1; | |
861 | } | |
862 | ||
30b30c21 | 863 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 864 | |
30b30c21 | 865 | long |
268b6b39 AM |
866 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
867 | bfd *input_bfd, | |
868 | long input_indx) | |
30b30c21 RH |
869 | { |
870 | struct elf_link_local_dynamic_entry *e; | |
871 | ||
872 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
873 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
874 | return e->dynindx; | |
875 | return -1; | |
876 | } | |
877 | ||
878 | /* This function is used to renumber the dynamic symbols, if some of | |
879 | them are removed because they are marked as local. This is called | |
880 | via elf_link_hash_traverse. */ | |
881 | ||
0a1b45a2 | 882 | static bool |
268b6b39 AM |
883 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
884 | void *data) | |
42751cf3 | 885 | { |
a50b1753 | 886 | size_t *count = (size_t *) data; |
30b30c21 | 887 | |
6fa3860b | 888 | if (h->forced_local) |
0a1b45a2 | 889 | return true; |
6fa3860b PB |
890 | |
891 | if (h->dynindx != -1) | |
892 | h->dynindx = ++(*count); | |
893 | ||
0a1b45a2 | 894 | return true; |
6fa3860b PB |
895 | } |
896 | ||
897 | ||
898 | /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with | |
899 | STB_LOCAL binding. */ | |
900 | ||
0a1b45a2 | 901 | static bool |
6fa3860b PB |
902 | elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, |
903 | void *data) | |
904 | { | |
a50b1753 | 905 | size_t *count = (size_t *) data; |
6fa3860b | 906 | |
6fa3860b | 907 | if (!h->forced_local) |
0a1b45a2 | 908 | return true; |
6fa3860b | 909 | |
42751cf3 | 910 | if (h->dynindx != -1) |
30b30c21 RH |
911 | h->dynindx = ++(*count); |
912 | ||
0a1b45a2 | 913 | return true; |
42751cf3 | 914 | } |
30b30c21 | 915 | |
aee6f5b4 AO |
916 | /* Return true if the dynamic symbol for a given section should be |
917 | omitted when creating a shared library. */ | |
0a1b45a2 | 918 | bool |
d00dd7dc AM |
919 | _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED, |
920 | struct bfd_link_info *info, | |
921 | asection *p) | |
aee6f5b4 | 922 | { |
74541ad4 | 923 | struct elf_link_hash_table *htab; |
ca55926c | 924 | asection *ip; |
74541ad4 | 925 | |
aee6f5b4 AO |
926 | switch (elf_section_data (p)->this_hdr.sh_type) |
927 | { | |
928 | case SHT_PROGBITS: | |
929 | case SHT_NOBITS: | |
930 | /* If sh_type is yet undecided, assume it could be | |
931 | SHT_PROGBITS/SHT_NOBITS. */ | |
932 | case SHT_NULL: | |
74541ad4 | 933 | htab = elf_hash_table (info); |
74541ad4 AM |
934 | if (htab->text_index_section != NULL) |
935 | return p != htab->text_index_section && p != htab->data_index_section; | |
936 | ||
ca55926c | 937 | return (htab->dynobj != NULL |
3d4d4302 | 938 | && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL |
ca55926c | 939 | && ip->output_section == p); |
aee6f5b4 AO |
940 | |
941 | /* There shouldn't be section relative relocations | |
942 | against any other section. */ | |
943 | default: | |
0a1b45a2 | 944 | return true; |
aee6f5b4 AO |
945 | } |
946 | } | |
947 | ||
0a1b45a2 | 948 | bool |
d00dd7dc AM |
949 | _bfd_elf_omit_section_dynsym_all |
950 | (bfd *output_bfd ATTRIBUTE_UNUSED, | |
951 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
952 | asection *p ATTRIBUTE_UNUSED) | |
953 | { | |
0a1b45a2 | 954 | return true; |
d00dd7dc AM |
955 | } |
956 | ||
062e2358 | 957 | /* Assign dynsym indices. In a shared library we generate a section |
6fa3860b PB |
958 | symbol for each output section, which come first. Next come symbols |
959 | which have been forced to local binding. Then all of the back-end | |
960 | allocated local dynamic syms, followed by the rest of the global | |
63f452a8 AM |
961 | symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. |
962 | (This prevents the early call before elf_backend_init_index_section | |
963 | and strip_excluded_output_sections setting dynindx for sections | |
964 | that are stripped.) */ | |
30b30c21 | 965 | |
554220db AM |
966 | static unsigned long |
967 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, | |
968 | struct bfd_link_info *info, | |
969 | unsigned long *section_sym_count) | |
30b30c21 RH |
970 | { |
971 | unsigned long dynsymcount = 0; | |
0a1b45a2 | 972 | bool do_sec = section_sym_count != NULL; |
30b30c21 | 973 | |
0e1862bb L |
974 | if (bfd_link_pic (info) |
975 | || elf_hash_table (info)->is_relocatable_executable) | |
30b30c21 | 976 | { |
aee6f5b4 | 977 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
978 | asection *p; |
979 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 980 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 | 981 | && (p->flags & SEC_ALLOC) != 0 |
7f923b7f | 982 | && elf_hash_table (info)->dynamic_relocs |
aee6f5b4 | 983 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) |
63f452a8 AM |
984 | { |
985 | ++dynsymcount; | |
986 | if (do_sec) | |
987 | elf_section_data (p)->dynindx = dynsymcount; | |
988 | } | |
989 | else if (do_sec) | |
74541ad4 | 990 | elf_section_data (p)->dynindx = 0; |
30b30c21 | 991 | } |
63f452a8 AM |
992 | if (do_sec) |
993 | *section_sym_count = dynsymcount; | |
30b30c21 | 994 | |
6fa3860b PB |
995 | elf_link_hash_traverse (elf_hash_table (info), |
996 | elf_link_renumber_local_hash_table_dynsyms, | |
997 | &dynsymcount); | |
998 | ||
30b30c21 RH |
999 | if (elf_hash_table (info)->dynlocal) |
1000 | { | |
1001 | struct elf_link_local_dynamic_entry *p; | |
1002 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
1003 | p->dynindx = ++dynsymcount; | |
1004 | } | |
90ac2420 | 1005 | elf_hash_table (info)->local_dynsymcount = dynsymcount; |
30b30c21 RH |
1006 | |
1007 | elf_link_hash_traverse (elf_hash_table (info), | |
1008 | elf_link_renumber_hash_table_dynsyms, | |
1009 | &dynsymcount); | |
1010 | ||
d5486c43 L |
1011 | /* There is an unused NULL entry at the head of the table which we |
1012 | must account for in our count even if the table is empty since it | |
1013 | is intended for the mandatory DT_SYMTAB tag (.dynsym section) in | |
1014 | .dynamic section. */ | |
1015 | dynsymcount++; | |
30b30c21 | 1016 | |
ccabcbe5 AM |
1017 | elf_hash_table (info)->dynsymcount = dynsymcount; |
1018 | return dynsymcount; | |
30b30c21 | 1019 | } |
252b5132 | 1020 | |
54ac0771 L |
1021 | /* Merge st_other field. */ |
1022 | ||
1023 | static void | |
1024 | elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, | |
5160d0f3 | 1025 | unsigned int st_other, asection *sec, |
0a1b45a2 | 1026 | bool definition, bool dynamic) |
54ac0771 L |
1027 | { |
1028 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1029 | ||
1030 | /* If st_other has a processor-specific meaning, specific | |
cd3416da | 1031 | code might be needed here. */ |
54ac0771 | 1032 | if (bed->elf_backend_merge_symbol_attribute) |
5160d0f3 | 1033 | (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition, |
54ac0771 L |
1034 | dynamic); |
1035 | ||
cd3416da | 1036 | if (!dynamic) |
54ac0771 | 1037 | { |
5160d0f3 | 1038 | unsigned symvis = ELF_ST_VISIBILITY (st_other); |
cd3416da | 1039 | unsigned hvis = ELF_ST_VISIBILITY (h->other); |
54ac0771 | 1040 | |
cd3416da AM |
1041 | /* Keep the most constraining visibility. Leave the remainder |
1042 | of the st_other field to elf_backend_merge_symbol_attribute. */ | |
1043 | if (symvis - 1 < hvis - 1) | |
1044 | h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); | |
54ac0771 | 1045 | } |
b8417128 | 1046 | else if (definition |
5160d0f3 | 1047 | && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT |
b8417128 | 1048 | && (sec->flags & SEC_READONLY) == 0) |
6cabe1ea | 1049 | h->protected_def = 1; |
54ac0771 L |
1050 | } |
1051 | ||
4f3fedcf AM |
1052 | /* This function is called when we want to merge a new symbol with an |
1053 | existing symbol. It handles the various cases which arise when we | |
1054 | find a definition in a dynamic object, or when there is already a | |
1055 | definition in a dynamic object. The new symbol is described by | |
1056 | NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table | |
1057 | entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK | |
1058 | if the old symbol was weak. We set POLD_ALIGNMENT to the alignment | |
1059 | of an old common symbol. We set OVERRIDE if the old symbol is | |
1060 | overriding a new definition. We set TYPE_CHANGE_OK if it is OK for | |
1061 | the type to change. We set SIZE_CHANGE_OK if it is OK for the size | |
1062 | to change. By OK to change, we mean that we shouldn't warn if the | |
1063 | type or size does change. */ | |
45d6a902 | 1064 | |
0a1b45a2 | 1065 | static bool |
268b6b39 AM |
1066 | _bfd_elf_merge_symbol (bfd *abfd, |
1067 | struct bfd_link_info *info, | |
1068 | const char *name, | |
1069 | Elf_Internal_Sym *sym, | |
1070 | asection **psec, | |
1071 | bfd_vma *pvalue, | |
4f3fedcf AM |
1072 | struct elf_link_hash_entry **sym_hash, |
1073 | bfd **poldbfd, | |
0a1b45a2 | 1074 | bool *pold_weak, |
af44c138 | 1075 | unsigned int *pold_alignment, |
0a1b45a2 | 1076 | bool *skip, |
7ba11550 | 1077 | bfd **override, |
0a1b45a2 AM |
1078 | bool *type_change_ok, |
1079 | bool *size_change_ok, | |
1080 | bool *matched) | |
252b5132 | 1081 | { |
7479dfd4 | 1082 | asection *sec, *oldsec; |
45d6a902 | 1083 | struct elf_link_hash_entry *h; |
90c984fc | 1084 | struct elf_link_hash_entry *hi; |
45d6a902 AM |
1085 | struct elf_link_hash_entry *flip; |
1086 | int bind; | |
1087 | bfd *oldbfd; | |
0a1b45a2 AM |
1088 | bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; |
1089 | bool newweak, oldweak, newfunc, oldfunc; | |
a4d8e49b | 1090 | const struct elf_backend_data *bed; |
6e33951e | 1091 | char *new_version; |
0a1b45a2 | 1092 | bool default_sym = *matched; |
320fdefe | 1093 | struct elf_link_hash_table *htab; |
45d6a902 | 1094 | |
0a1b45a2 | 1095 | *skip = false; |
7ba11550 | 1096 | *override = NULL; |
45d6a902 AM |
1097 | |
1098 | sec = *psec; | |
1099 | bind = ELF_ST_BIND (sym->st_info); | |
1100 | ||
1101 | if (! bfd_is_und_section (sec)) | |
0a1b45a2 | 1102 | h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false); |
45d6a902 AM |
1103 | else |
1104 | h = ((struct elf_link_hash_entry *) | |
0a1b45a2 | 1105 | bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false)); |
45d6a902 | 1106 | if (h == NULL) |
0a1b45a2 | 1107 | return false; |
45d6a902 | 1108 | *sym_hash = h; |
252b5132 | 1109 | |
88ba32a0 L |
1110 | bed = get_elf_backend_data (abfd); |
1111 | ||
6e33951e | 1112 | /* NEW_VERSION is the symbol version of the new symbol. */ |
422f1182 | 1113 | if (h->versioned != unversioned) |
6e33951e | 1114 | { |
422f1182 L |
1115 | /* Symbol version is unknown or versioned. */ |
1116 | new_version = strrchr (name, ELF_VER_CHR); | |
1117 | if (new_version) | |
1118 | { | |
1119 | if (h->versioned == unknown) | |
1120 | { | |
1121 | if (new_version > name && new_version[-1] != ELF_VER_CHR) | |
1122 | h->versioned = versioned_hidden; | |
1123 | else | |
1124 | h->versioned = versioned; | |
1125 | } | |
1126 | new_version += 1; | |
1127 | if (new_version[0] == '\0') | |
1128 | new_version = NULL; | |
1129 | } | |
1130 | else | |
1131 | h->versioned = unversioned; | |
6e33951e | 1132 | } |
422f1182 L |
1133 | else |
1134 | new_version = NULL; | |
6e33951e | 1135 | |
90c984fc L |
1136 | /* For merging, we only care about real symbols. But we need to make |
1137 | sure that indirect symbol dynamic flags are updated. */ | |
1138 | hi = h; | |
45d6a902 AM |
1139 | while (h->root.type == bfd_link_hash_indirect |
1140 | || h->root.type == bfd_link_hash_warning) | |
1141 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1142 | ||
6e33951e L |
1143 | if (!*matched) |
1144 | { | |
1145 | if (hi == h || h->root.type == bfd_link_hash_new) | |
0a1b45a2 | 1146 | *matched = true; |
6e33951e L |
1147 | else |
1148 | { | |
ae7683d2 | 1149 | /* OLD_HIDDEN is true if the existing symbol is only visible |
6e33951e | 1150 | to the symbol with the same symbol version. NEW_HIDDEN is |
ae7683d2 | 1151 | true if the new symbol is only visible to the symbol with |
6e33951e | 1152 | the same symbol version. */ |
0a1b45a2 AM |
1153 | bool old_hidden = h->versioned == versioned_hidden; |
1154 | bool new_hidden = hi->versioned == versioned_hidden; | |
6e33951e L |
1155 | if (!old_hidden && !new_hidden) |
1156 | /* The new symbol matches the existing symbol if both | |
1157 | aren't hidden. */ | |
0a1b45a2 | 1158 | *matched = true; |
6e33951e L |
1159 | else |
1160 | { | |
1161 | /* OLD_VERSION is the symbol version of the existing | |
1162 | symbol. */ | |
422f1182 L |
1163 | char *old_version; |
1164 | ||
1165 | if (h->versioned >= versioned) | |
1166 | old_version = strrchr (h->root.root.string, | |
1167 | ELF_VER_CHR) + 1; | |
1168 | else | |
1169 | old_version = NULL; | |
6e33951e L |
1170 | |
1171 | /* The new symbol matches the existing symbol if they | |
1172 | have the same symbol version. */ | |
1173 | *matched = (old_version == new_version | |
1174 | || (old_version != NULL | |
1175 | && new_version != NULL | |
1176 | && strcmp (old_version, new_version) == 0)); | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
934bce08 AM |
1181 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
1182 | existing symbol. */ | |
1183 | ||
1184 | oldbfd = NULL; | |
1185 | oldsec = NULL; | |
1186 | switch (h->root.type) | |
1187 | { | |
1188 | default: | |
1189 | break; | |
1190 | ||
1191 | case bfd_link_hash_undefined: | |
1192 | case bfd_link_hash_undefweak: | |
1193 | oldbfd = h->root.u.undef.abfd; | |
1194 | break; | |
1195 | ||
1196 | case bfd_link_hash_defined: | |
1197 | case bfd_link_hash_defweak: | |
1198 | oldbfd = h->root.u.def.section->owner; | |
1199 | oldsec = h->root.u.def.section; | |
1200 | break; | |
1201 | ||
1202 | case bfd_link_hash_common: | |
1203 | oldbfd = h->root.u.c.p->section->owner; | |
1204 | oldsec = h->root.u.c.p->section; | |
1205 | if (pold_alignment) | |
1206 | *pold_alignment = h->root.u.c.p->alignment_power; | |
1207 | break; | |
1208 | } | |
1209 | if (poldbfd && *poldbfd == NULL) | |
1210 | *poldbfd = oldbfd; | |
1211 | ||
1212 | /* Differentiate strong and weak symbols. */ | |
1213 | newweak = bind == STB_WEAK; | |
1214 | oldweak = (h->root.type == bfd_link_hash_defweak | |
1215 | || h->root.type == bfd_link_hash_undefweak); | |
1216 | if (pold_weak) | |
1217 | *pold_weak = oldweak; | |
1218 | ||
40b36307 | 1219 | /* We have to check it for every instance since the first few may be |
ee659f1f | 1220 | references and not all compilers emit symbol type for undefined |
40b36307 L |
1221 | symbols. */ |
1222 | bfd_elf_link_mark_dynamic_symbol (info, h, sym); | |
1223 | ||
320fdefe L |
1224 | htab = elf_hash_table (info); |
1225 | ||
ee659f1f AM |
1226 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, |
1227 | respectively, is from a dynamic object. */ | |
1228 | ||
1229 | newdyn = (abfd->flags & DYNAMIC) != 0; | |
1230 | ||
1231 | /* ref_dynamic_nonweak and dynamic_def flags track actual undefined | |
1232 | syms and defined syms in dynamic libraries respectively. | |
1233 | ref_dynamic on the other hand can be set for a symbol defined in | |
1234 | a dynamic library, and def_dynamic may not be set; When the | |
1235 | definition in a dynamic lib is overridden by a definition in the | |
1236 | executable use of the symbol in the dynamic lib becomes a | |
1237 | reference to the executable symbol. */ | |
1238 | if (newdyn) | |
1239 | { | |
1240 | if (bfd_is_und_section (sec)) | |
1241 | { | |
1242 | if (bind != STB_WEAK) | |
1243 | { | |
1244 | h->ref_dynamic_nonweak = 1; | |
1245 | hi->ref_dynamic_nonweak = 1; | |
1246 | } | |
1247 | } | |
1248 | else | |
1249 | { | |
6e33951e L |
1250 | /* Update the existing symbol only if they match. */ |
1251 | if (*matched) | |
1252 | h->dynamic_def = 1; | |
ee659f1f AM |
1253 | hi->dynamic_def = 1; |
1254 | } | |
1255 | } | |
1256 | ||
45d6a902 AM |
1257 | /* If we just created the symbol, mark it as being an ELF symbol. |
1258 | Other than that, there is nothing to do--there is no merge issue | |
1259 | with a newly defined symbol--so we just return. */ | |
1260 | ||
1261 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 1262 | { |
f5385ebf | 1263 | h->non_elf = 0; |
0a1b45a2 | 1264 | return true; |
45d6a902 | 1265 | } |
252b5132 | 1266 | |
45d6a902 AM |
1267 | /* In cases involving weak versioned symbols, we may wind up trying |
1268 | to merge a symbol with itself. Catch that here, to avoid the | |
1269 | confusion that results if we try to override a symbol with | |
1270 | itself. The additional tests catch cases like | |
1271 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
1272 | dynamic object, which we do want to handle here. */ | |
1273 | if (abfd == oldbfd | |
895fa45f | 1274 | && (newweak || oldweak) |
45d6a902 | 1275 | && ((abfd->flags & DYNAMIC) == 0 |
f5385ebf | 1276 | || !h->def_regular)) |
0a1b45a2 | 1277 | return true; |
45d6a902 | 1278 | |
0a1b45a2 | 1279 | olddyn = false; |
45d6a902 AM |
1280 | if (oldbfd != NULL) |
1281 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
707bba77 | 1282 | else if (oldsec != NULL) |
45d6a902 | 1283 | { |
707bba77 | 1284 | /* This handles the special SHN_MIPS_{TEXT,DATA} section |
45d6a902 | 1285 | indices used by MIPS ELF. */ |
707bba77 | 1286 | olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; |
45d6a902 | 1287 | } |
252b5132 | 1288 | |
320fdefe L |
1289 | /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */ |
1290 | if (!htab->handling_dt_needed | |
1291 | && oldbfd != NULL | |
7de7786b | 1292 | && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)) |
1a3b5c34 | 1293 | { |
7de7786b L |
1294 | if (newdyn != olddyn) |
1295 | { | |
1296 | /* Handle a case where plugin_notice won't be called and thus | |
1297 | won't set the non_ir_ref flags on the first pass over | |
1298 | symbols. */ | |
1299 | h->root.non_ir_ref_dynamic = true; | |
1300 | hi->root.non_ir_ref_dynamic = true; | |
1301 | } | |
20ea3acc L |
1302 | else if ((oldbfd->flags & BFD_PLUGIN) != 0 |
1303 | && hi->root.type == bfd_link_hash_indirect) | |
7de7786b L |
1304 | { |
1305 | /* Change indirect symbol from IR to undefined. */ | |
1306 | hi->root.type = bfd_link_hash_undefined; | |
1307 | hi->root.u.undef.abfd = oldbfd; | |
1308 | } | |
1a3b5c34 AM |
1309 | } |
1310 | ||
45d6a902 AM |
1311 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
1312 | respectively, appear to be a definition rather than reference. */ | |
1313 | ||
707bba77 | 1314 | newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); |
45d6a902 | 1315 | |
707bba77 AM |
1316 | olddef = (h->root.type != bfd_link_hash_undefined |
1317 | && h->root.type != bfd_link_hash_undefweak | |
202ac193 | 1318 | && h->root.type != bfd_link_hash_common); |
45d6a902 | 1319 | |
0a36a439 L |
1320 | /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, |
1321 | respectively, appear to be a function. */ | |
1322 | ||
1323 | newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1324 | && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); | |
1325 | ||
1326 | oldfunc = (h->type != STT_NOTYPE | |
1327 | && bed->is_function_type (h->type)); | |
1328 | ||
c5d37467 | 1329 | if (!(newfunc && oldfunc) |
5b677558 AM |
1330 | && ELF_ST_TYPE (sym->st_info) != h->type |
1331 | && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE | |
1332 | && h->type != STT_NOTYPE | |
c5d37467 AM |
1333 | && (newdef || bfd_is_com_section (sec)) |
1334 | && (olddef || h->root.type == bfd_link_hash_common)) | |
580a2b6e | 1335 | { |
c5d37467 AM |
1336 | /* If creating a default indirect symbol ("foo" or "foo@") from |
1337 | a dynamic versioned definition ("foo@@") skip doing so if | |
1338 | there is an existing regular definition with a different | |
1339 | type. We don't want, for example, a "time" variable in the | |
1340 | executable overriding a "time" function in a shared library. */ | |
1341 | if (newdyn | |
1342 | && !olddyn) | |
1343 | { | |
0a1b45a2 AM |
1344 | *skip = true; |
1345 | return true; | |
c5d37467 AM |
1346 | } |
1347 | ||
1348 | /* When adding a symbol from a regular object file after we have | |
1349 | created indirect symbols, undo the indirection and any | |
1350 | dynamic state. */ | |
1351 | if (hi != h | |
1352 | && !newdyn | |
1353 | && olddyn) | |
1354 | { | |
1355 | h = hi; | |
0a1b45a2 | 1356 | (*bed->elf_backend_hide_symbol) (info, h, true); |
c5d37467 AM |
1357 | h->forced_local = 0; |
1358 | h->ref_dynamic = 0; | |
1359 | h->def_dynamic = 0; | |
1360 | h->dynamic_def = 0; | |
1361 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1362 | { | |
1363 | h->root.type = bfd_link_hash_undefined; | |
1364 | h->root.u.undef.abfd = abfd; | |
1365 | } | |
1366 | else | |
1367 | { | |
1368 | h->root.type = bfd_link_hash_new; | |
1369 | h->root.u.undef.abfd = NULL; | |
1370 | } | |
0a1b45a2 | 1371 | return true; |
c5d37467 | 1372 | } |
580a2b6e L |
1373 | } |
1374 | ||
4c34aff8 AM |
1375 | /* Check TLS symbols. We don't check undefined symbols introduced |
1376 | by "ld -u" which have no type (and oldbfd NULL), and we don't | |
1377 | check symbols from plugins because they also have no type. */ | |
1378 | if (oldbfd != NULL | |
1379 | && (oldbfd->flags & BFD_PLUGIN) == 0 | |
1380 | && (abfd->flags & BFD_PLUGIN) == 0 | |
1381 | && ELF_ST_TYPE (sym->st_info) != h->type | |
1382 | && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) | |
7479dfd4 L |
1383 | { |
1384 | bfd *ntbfd, *tbfd; | |
0a1b45a2 | 1385 | bool ntdef, tdef; |
7479dfd4 L |
1386 | asection *ntsec, *tsec; |
1387 | ||
1388 | if (h->type == STT_TLS) | |
1389 | { | |
3b36f7e6 | 1390 | ntbfd = abfd; |
7479dfd4 L |
1391 | ntsec = sec; |
1392 | ntdef = newdef; | |
1393 | tbfd = oldbfd; | |
1394 | tsec = oldsec; | |
1395 | tdef = olddef; | |
1396 | } | |
1397 | else | |
1398 | { | |
1399 | ntbfd = oldbfd; | |
1400 | ntsec = oldsec; | |
1401 | ntdef = olddef; | |
1402 | tbfd = abfd; | |
1403 | tsec = sec; | |
1404 | tdef = newdef; | |
1405 | } | |
1406 | ||
1407 | if (tdef && ntdef) | |
4eca0228 | 1408 | _bfd_error_handler |
695344c0 | 1409 | /* xgettext:c-format */ |
871b3ab2 AM |
1410 | (_("%s: TLS definition in %pB section %pA " |
1411 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1412 | h->root.root.string, tbfd, tsec, ntbfd, ntsec); |
7479dfd4 | 1413 | else if (!tdef && !ntdef) |
4eca0228 | 1414 | _bfd_error_handler |
695344c0 | 1415 | /* xgettext:c-format */ |
871b3ab2 AM |
1416 | (_("%s: TLS reference in %pB " |
1417 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1418 | h->root.root.string, tbfd, ntbfd); |
7479dfd4 | 1419 | else if (tdef) |
4eca0228 | 1420 | _bfd_error_handler |
695344c0 | 1421 | /* xgettext:c-format */ |
871b3ab2 AM |
1422 | (_("%s: TLS definition in %pB section %pA " |
1423 | "mismatches non-TLS reference in %pB"), | |
c08bb8dd | 1424 | h->root.root.string, tbfd, tsec, ntbfd); |
7479dfd4 | 1425 | else |
4eca0228 | 1426 | _bfd_error_handler |
695344c0 | 1427 | /* xgettext:c-format */ |
871b3ab2 AM |
1428 | (_("%s: TLS reference in %pB " |
1429 | "mismatches non-TLS definition in %pB section %pA"), | |
c08bb8dd | 1430 | h->root.root.string, tbfd, ntbfd, ntsec); |
7479dfd4 L |
1431 | |
1432 | bfd_set_error (bfd_error_bad_value); | |
0a1b45a2 | 1433 | return false; |
7479dfd4 L |
1434 | } |
1435 | ||
45d6a902 AM |
1436 | /* If the old symbol has non-default visibility, we ignore the new |
1437 | definition from a dynamic object. */ | |
1438 | if (newdyn | |
9c7a29a3 | 1439 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
1440 | && !bfd_is_und_section (sec)) |
1441 | { | |
0a1b45a2 | 1442 | *skip = true; |
45d6a902 | 1443 | /* Make sure this symbol is dynamic. */ |
f5385ebf | 1444 | h->ref_dynamic = 1; |
90c984fc | 1445 | hi->ref_dynamic = 1; |
45d6a902 AM |
1446 | /* A protected symbol has external availability. Make sure it is |
1447 | recorded as dynamic. | |
1448 | ||
1449 | FIXME: Should we check type and size for protected symbol? */ | |
1450 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 1451 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 | 1452 | else |
0a1b45a2 | 1453 | return true; |
45d6a902 AM |
1454 | } |
1455 | else if (!newdyn | |
9c7a29a3 | 1456 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 1457 | && h->def_dynamic) |
45d6a902 AM |
1458 | { |
1459 | /* If the new symbol with non-default visibility comes from a | |
1460 | relocatable file and the old definition comes from a dynamic | |
1461 | object, we remove the old definition. */ | |
6c9b78e6 | 1462 | if (hi->root.type == bfd_link_hash_indirect) |
d2dee3b2 L |
1463 | { |
1464 | /* Handle the case where the old dynamic definition is | |
1465 | default versioned. We need to copy the symbol info from | |
1466 | the symbol with default version to the normal one if it | |
1467 | was referenced before. */ | |
1468 | if (h->ref_regular) | |
1469 | { | |
6c9b78e6 | 1470 | hi->root.type = h->root.type; |
d2dee3b2 | 1471 | h->root.type = bfd_link_hash_indirect; |
6c9b78e6 | 1472 | (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); |
aed81c4e | 1473 | |
6c9b78e6 | 1474 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; |
aed81c4e | 1475 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
d2dee3b2 | 1476 | { |
aed81c4e MR |
1477 | /* If the new symbol is hidden or internal, completely undo |
1478 | any dynamic link state. */ | |
0a1b45a2 | 1479 | (*bed->elf_backend_hide_symbol) (info, h, true); |
aed81c4e MR |
1480 | h->forced_local = 0; |
1481 | h->ref_dynamic = 0; | |
d2dee3b2 L |
1482 | } |
1483 | else | |
aed81c4e MR |
1484 | h->ref_dynamic = 1; |
1485 | ||
1486 | h->def_dynamic = 0; | |
aed81c4e MR |
1487 | /* FIXME: Should we check type and size for protected symbol? */ |
1488 | h->size = 0; | |
1489 | h->type = 0; | |
1490 | ||
6c9b78e6 | 1491 | h = hi; |
d2dee3b2 L |
1492 | } |
1493 | else | |
6c9b78e6 | 1494 | h = hi; |
d2dee3b2 | 1495 | } |
1de1a317 | 1496 | |
f5eda473 AM |
1497 | /* If the old symbol was undefined before, then it will still be |
1498 | on the undefs list. If the new symbol is undefined or | |
1499 | common, we can't make it bfd_link_hash_new here, because new | |
1500 | undefined or common symbols will be added to the undefs list | |
1501 | by _bfd_generic_link_add_one_symbol. Symbols may not be | |
1502 | added twice to the undefs list. Also, if the new symbol is | |
1503 | undefweak then we don't want to lose the strong undef. */ | |
1504 | if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) | |
1de1a317 | 1505 | { |
1de1a317 | 1506 | h->root.type = bfd_link_hash_undefined; |
1de1a317 L |
1507 | h->root.u.undef.abfd = abfd; |
1508 | } | |
1509 | else | |
1510 | { | |
1511 | h->root.type = bfd_link_hash_new; | |
1512 | h->root.u.undef.abfd = NULL; | |
1513 | } | |
1514 | ||
f5eda473 | 1515 | if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) |
252b5132 | 1516 | { |
f5eda473 AM |
1517 | /* If the new symbol is hidden or internal, completely undo |
1518 | any dynamic link state. */ | |
0a1b45a2 | 1519 | (*bed->elf_backend_hide_symbol) (info, h, true); |
f5eda473 AM |
1520 | h->forced_local = 0; |
1521 | h->ref_dynamic = 0; | |
45d6a902 | 1522 | } |
f5eda473 AM |
1523 | else |
1524 | h->ref_dynamic = 1; | |
1525 | h->def_dynamic = 0; | |
45d6a902 AM |
1526 | /* FIXME: Should we check type and size for protected symbol? */ |
1527 | h->size = 0; | |
1528 | h->type = 0; | |
0a1b45a2 | 1529 | return true; |
45d6a902 | 1530 | } |
14a793b2 | 1531 | |
15b43f48 AM |
1532 | /* If a new weak symbol definition comes from a regular file and the |
1533 | old symbol comes from a dynamic library, we treat the new one as | |
1534 | strong. Similarly, an old weak symbol definition from a regular | |
1535 | file is treated as strong when the new symbol comes from a dynamic | |
1536 | library. Further, an old weak symbol from a dynamic library is | |
1537 | treated as strong if the new symbol is from a dynamic library. | |
1538 | This reflects the way glibc's ld.so works. | |
1539 | ||
165f707a AM |
1540 | Also allow a weak symbol to override a linker script symbol |
1541 | defined by an early pass over the script. This is done so the | |
1542 | linker knows the symbol is defined in an object file, for the | |
1543 | DEFINED script function. | |
1544 | ||
15b43f48 AM |
1545 | Do this before setting *type_change_ok or *size_change_ok so that |
1546 | we warn properly when dynamic library symbols are overridden. */ | |
1547 | ||
165f707a | 1548 | if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) |
0a1b45a2 | 1549 | newweak = false; |
15b43f48 | 1550 | if (olddef && newdyn) |
0a1b45a2 | 1551 | oldweak = false; |
0f8a2703 | 1552 | |
d334575b | 1553 | /* Allow changes between different types of function symbol. */ |
0a36a439 | 1554 | if (newfunc && oldfunc) |
0a1b45a2 | 1555 | *type_change_ok = true; |
fcb93ecf | 1556 | |
79349b09 AM |
1557 | /* It's OK to change the type if either the existing symbol or the |
1558 | new symbol is weak. A type change is also OK if the old symbol | |
1559 | is undefined and the new symbol is defined. */ | |
252b5132 | 1560 | |
79349b09 AM |
1561 | if (oldweak |
1562 | || newweak | |
1563 | || (newdef | |
1564 | && h->root.type == bfd_link_hash_undefined)) | |
0a1b45a2 | 1565 | *type_change_ok = true; |
79349b09 AM |
1566 | |
1567 | /* It's OK to change the size if either the existing symbol or the | |
1568 | new symbol is weak, or if the old symbol is undefined. */ | |
1569 | ||
1570 | if (*type_change_ok | |
1571 | || h->root.type == bfd_link_hash_undefined) | |
0a1b45a2 | 1572 | *size_change_ok = true; |
45d6a902 | 1573 | |
45d6a902 AM |
1574 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1575 | symbol, respectively, appears to be a common symbol in a dynamic | |
1576 | object. If a symbol appears in an uninitialized section, and is | |
1577 | not weak, and is not a function, then it may be a common symbol | |
1578 | which was resolved when the dynamic object was created. We want | |
1579 | to treat such symbols specially, because they raise special | |
1580 | considerations when setting the symbol size: if the symbol | |
1581 | appears as a common symbol in a regular object, and the size in | |
1582 | the regular object is larger, we must make sure that we use the | |
1583 | larger size. This problematic case can always be avoided in C, | |
1584 | but it must be handled correctly when using Fortran shared | |
1585 | libraries. | |
1586 | ||
1587 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1588 | likewise for OLDDYNCOMMON and OLDDEF. | |
1589 | ||
1590 | Note that this test is just a heuristic, and that it is quite | |
1591 | possible to have an uninitialized symbol in a shared object which | |
1592 | is really a definition, rather than a common symbol. This could | |
1593 | lead to some minor confusion when the symbol really is a common | |
1594 | symbol in some regular object. However, I think it will be | |
1595 | harmless. */ | |
1596 | ||
1597 | if (newdyn | |
1598 | && newdef | |
79349b09 | 1599 | && !newweak |
45d6a902 AM |
1600 | && (sec->flags & SEC_ALLOC) != 0 |
1601 | && (sec->flags & SEC_LOAD) == 0 | |
1602 | && sym->st_size > 0 | |
0a36a439 | 1603 | && !newfunc) |
0a1b45a2 | 1604 | newdyncommon = true; |
45d6a902 | 1605 | else |
0a1b45a2 | 1606 | newdyncommon = false; |
45d6a902 AM |
1607 | |
1608 | if (olddyn | |
1609 | && olddef | |
1610 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1611 | && h->def_dynamic |
45d6a902 AM |
1612 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1613 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1614 | && h->size > 0 | |
0a36a439 | 1615 | && !oldfunc) |
0a1b45a2 | 1616 | olddyncommon = true; |
45d6a902 | 1617 | else |
0a1b45a2 | 1618 | olddyncommon = false; |
45d6a902 | 1619 | |
a4d8e49b L |
1620 | /* We now know everything about the old and new symbols. We ask the |
1621 | backend to check if we can merge them. */ | |
5d13b3b3 AM |
1622 | if (bed->merge_symbol != NULL) |
1623 | { | |
1624 | if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) | |
0a1b45a2 | 1625 | return false; |
5d13b3b3 AM |
1626 | sec = *psec; |
1627 | } | |
a4d8e49b | 1628 | |
a83ef4d1 L |
1629 | /* There are multiple definitions of a normal symbol. Skip the |
1630 | default symbol as well as definition from an IR object. */ | |
93f4de39 | 1631 | if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak |
a83ef4d1 L |
1632 | && !default_sym && h->def_regular |
1633 | && !(oldbfd != NULL | |
1634 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
1635 | && (abfd->flags & BFD_PLUGIN) == 0)) | |
93f4de39 RL |
1636 | { |
1637 | /* Handle a multiple definition. */ | |
1638 | (*info->callbacks->multiple_definition) (info, &h->root, | |
1639 | abfd, sec, *pvalue); | |
0a1b45a2 AM |
1640 | *skip = true; |
1641 | return true; | |
93f4de39 RL |
1642 | } |
1643 | ||
45d6a902 AM |
1644 | /* If both the old and the new symbols look like common symbols in a |
1645 | dynamic object, set the size of the symbol to the larger of the | |
1646 | two. */ | |
1647 | ||
1648 | if (olddyncommon | |
1649 | && newdyncommon | |
1650 | && sym->st_size != h->size) | |
1651 | { | |
1652 | /* Since we think we have two common symbols, issue a multiple | |
1653 | common warning if desired. Note that we only warn if the | |
1654 | size is different. If the size is the same, we simply let | |
1655 | the old symbol override the new one as normally happens with | |
1656 | symbols defined in dynamic objects. */ | |
1657 | ||
1a72702b AM |
1658 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1659 | bfd_link_hash_common, sym->st_size); | |
45d6a902 AM |
1660 | if (sym->st_size > h->size) |
1661 | h->size = sym->st_size; | |
252b5132 | 1662 | |
0a1b45a2 | 1663 | *size_change_ok = true; |
252b5132 RH |
1664 | } |
1665 | ||
45d6a902 AM |
1666 | /* If we are looking at a dynamic object, and we have found a |
1667 | definition, we need to see if the symbol was already defined by | |
1668 | some other object. If so, we want to use the existing | |
1669 | definition, and we do not want to report a multiple symbol | |
1670 | definition error; we do this by clobbering *PSEC to be | |
1671 | bfd_und_section_ptr. | |
1672 | ||
1673 | We treat a common symbol as a definition if the symbol in the | |
1674 | shared library is a function, since common symbols always | |
1675 | represent variables; this can cause confusion in principle, but | |
1676 | any such confusion would seem to indicate an erroneous program or | |
1677 | shared library. We also permit a common symbol in a regular | |
8170f769 | 1678 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1679 | |
1680 | if (newdyn | |
1681 | && newdef | |
77cfaee6 | 1682 | && (olddef |
45d6a902 | 1683 | || (h->root.type == bfd_link_hash_common |
8170f769 | 1684 | && (newweak || newfunc)))) |
45d6a902 | 1685 | { |
7ba11550 | 1686 | *override = abfd; |
0a1b45a2 AM |
1687 | newdef = false; |
1688 | newdyncommon = false; | |
252b5132 | 1689 | |
45d6a902 | 1690 | *psec = sec = bfd_und_section_ptr; |
0a1b45a2 | 1691 | *size_change_ok = true; |
252b5132 | 1692 | |
45d6a902 AM |
1693 | /* If we get here when the old symbol is a common symbol, then |
1694 | we are explicitly letting it override a weak symbol or | |
1695 | function in a dynamic object, and we don't want to warn about | |
1696 | a type change. If the old symbol is a defined symbol, a type | |
1697 | change warning may still be appropriate. */ | |
252b5132 | 1698 | |
45d6a902 | 1699 | if (h->root.type == bfd_link_hash_common) |
0a1b45a2 | 1700 | *type_change_ok = true; |
45d6a902 AM |
1701 | } |
1702 | ||
1703 | /* Handle the special case of an old common symbol merging with a | |
1704 | new symbol which looks like a common symbol in a shared object. | |
1705 | We change *PSEC and *PVALUE to make the new symbol look like a | |
91134c82 L |
1706 | common symbol, and let _bfd_generic_link_add_one_symbol do the |
1707 | right thing. */ | |
45d6a902 AM |
1708 | |
1709 | if (newdyncommon | |
1710 | && h->root.type == bfd_link_hash_common) | |
1711 | { | |
7ba11550 | 1712 | *override = oldbfd; |
0a1b45a2 AM |
1713 | newdef = false; |
1714 | newdyncommon = false; | |
45d6a902 | 1715 | *pvalue = sym->st_size; |
a4d8e49b | 1716 | *psec = sec = bed->common_section (oldsec); |
0a1b45a2 | 1717 | *size_change_ok = true; |
45d6a902 AM |
1718 | } |
1719 | ||
c5e2cead | 1720 | /* Skip weak definitions of symbols that are already defined. */ |
f41d945b | 1721 | if (newdef && olddef && newweak) |
54ac0771 | 1722 | { |
35ed3f94 | 1723 | /* Don't skip new non-IR weak syms. */ |
3a5dbfb2 AM |
1724 | if (!(oldbfd != NULL |
1725 | && (oldbfd->flags & BFD_PLUGIN) != 0 | |
35ed3f94 | 1726 | && (abfd->flags & BFD_PLUGIN) == 0)) |
57fa7b8c | 1727 | { |
0a1b45a2 AM |
1728 | newdef = false; |
1729 | *skip = true; | |
57fa7b8c | 1730 | } |
54ac0771 L |
1731 | |
1732 | /* Merge st_other. If the symbol already has a dynamic index, | |
1733 | but visibility says it should not be visible, turn it into a | |
1734 | local symbol. */ | |
5160d0f3 | 1735 | elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn); |
54ac0771 L |
1736 | if (h->dynindx != -1) |
1737 | switch (ELF_ST_VISIBILITY (h->other)) | |
1738 | { | |
1739 | case STV_INTERNAL: | |
1740 | case STV_HIDDEN: | |
0a1b45a2 | 1741 | (*bed->elf_backend_hide_symbol) (info, h, true); |
54ac0771 L |
1742 | break; |
1743 | } | |
1744 | } | |
c5e2cead | 1745 | |
45d6a902 AM |
1746 | /* If the old symbol is from a dynamic object, and the new symbol is |
1747 | a definition which is not from a dynamic object, then the new | |
1748 | symbol overrides the old symbol. Symbols from regular files | |
1749 | always take precedence over symbols from dynamic objects, even if | |
1750 | they are defined after the dynamic object in the link. | |
1751 | ||
1752 | As above, we again permit a common symbol in a regular object to | |
1753 | override a definition in a shared object if the shared object | |
0f8a2703 | 1754 | symbol is a function or is weak. */ |
45d6a902 AM |
1755 | |
1756 | flip = NULL; | |
77cfaee6 | 1757 | if (!newdyn |
45d6a902 AM |
1758 | && (newdef |
1759 | || (bfd_is_com_section (sec) | |
0a36a439 | 1760 | && (oldweak || oldfunc))) |
45d6a902 AM |
1761 | && olddyn |
1762 | && olddef | |
f5385ebf | 1763 | && h->def_dynamic) |
45d6a902 AM |
1764 | { |
1765 | /* Change the hash table entry to undefined, and let | |
1766 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1767 | new definition. */ | |
1768 | ||
1769 | h->root.type = bfd_link_hash_undefined; | |
1770 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
0a1b45a2 | 1771 | *size_change_ok = true; |
45d6a902 | 1772 | |
0a1b45a2 AM |
1773 | olddef = false; |
1774 | olddyncommon = false; | |
45d6a902 AM |
1775 | |
1776 | /* We again permit a type change when a common symbol may be | |
1777 | overriding a function. */ | |
1778 | ||
1779 | if (bfd_is_com_section (sec)) | |
0a36a439 L |
1780 | { |
1781 | if (oldfunc) | |
1782 | { | |
1783 | /* If a common symbol overrides a function, make sure | |
1784 | that it isn't defined dynamically nor has type | |
1785 | function. */ | |
1786 | h->def_dynamic = 0; | |
1787 | h->type = STT_NOTYPE; | |
1788 | } | |
0a1b45a2 | 1789 | *type_change_ok = true; |
0a36a439 | 1790 | } |
45d6a902 | 1791 | |
6c9b78e6 AM |
1792 | if (hi->root.type == bfd_link_hash_indirect) |
1793 | flip = hi; | |
45d6a902 AM |
1794 | else |
1795 | /* This union may have been set to be non-NULL when this symbol | |
1796 | was seen in a dynamic object. We must force the union to be | |
1797 | NULL, so that it is correct for a regular symbol. */ | |
1798 | h->verinfo.vertree = NULL; | |
1799 | } | |
1800 | ||
1801 | /* Handle the special case of a new common symbol merging with an | |
1802 | old symbol that looks like it might be a common symbol defined in | |
1803 | a shared object. Note that we have already handled the case in | |
1804 | which a new common symbol should simply override the definition | |
1805 | in the shared library. */ | |
1806 | ||
1807 | if (! newdyn | |
1808 | && bfd_is_com_section (sec) | |
1809 | && olddyncommon) | |
1810 | { | |
1811 | /* It would be best if we could set the hash table entry to a | |
1812 | common symbol, but we don't know what to use for the section | |
1813 | or the alignment. */ | |
1a72702b AM |
1814 | (*info->callbacks->multiple_common) (info, &h->root, abfd, |
1815 | bfd_link_hash_common, sym->st_size); | |
45d6a902 | 1816 | |
4cc11e76 | 1817 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1818 | larger, pretend that the new symbol has its size. */ |
1819 | ||
1820 | if (h->size > *pvalue) | |
1821 | *pvalue = h->size; | |
1822 | ||
af44c138 L |
1823 | /* We need to remember the alignment required by the symbol |
1824 | in the dynamic object. */ | |
1825 | BFD_ASSERT (pold_alignment); | |
1826 | *pold_alignment = h->root.u.def.section->alignment_power; | |
45d6a902 | 1827 | |
0a1b45a2 AM |
1828 | olddef = false; |
1829 | olddyncommon = false; | |
45d6a902 AM |
1830 | |
1831 | h->root.type = bfd_link_hash_undefined; | |
1832 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1833 | ||
0a1b45a2 AM |
1834 | *size_change_ok = true; |
1835 | *type_change_ok = true; | |
45d6a902 | 1836 | |
6c9b78e6 AM |
1837 | if (hi->root.type == bfd_link_hash_indirect) |
1838 | flip = hi; | |
45d6a902 AM |
1839 | else |
1840 | h->verinfo.vertree = NULL; | |
1841 | } | |
1842 | ||
1843 | if (flip != NULL) | |
1844 | { | |
1845 | /* Handle the case where we had a versioned symbol in a dynamic | |
1846 | library and now find a definition in a normal object. In this | |
1847 | case, we make the versioned symbol point to the normal one. */ | |
45d6a902 | 1848 | flip->root.type = h->root.type; |
00cbee0a | 1849 | flip->root.u.undef.abfd = h->root.u.undef.abfd; |
45d6a902 AM |
1850 | h->root.type = bfd_link_hash_indirect; |
1851 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
fcfa13d2 | 1852 | (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); |
f5385ebf | 1853 | if (h->def_dynamic) |
45d6a902 | 1854 | { |
f5385ebf AM |
1855 | h->def_dynamic = 0; |
1856 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1857 | } |
1858 | } | |
1859 | ||
0a1b45a2 | 1860 | return true; |
45d6a902 AM |
1861 | } |
1862 | ||
1863 | /* This function is called to create an indirect symbol from the | |
1864 | default for the symbol with the default version if needed. The | |
4f3fedcf | 1865 | symbol is described by H, NAME, SYM, SEC, and VALUE. We |
0f8a2703 | 1866 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 | 1867 | |
0a1b45a2 | 1868 | static bool |
268b6b39 AM |
1869 | _bfd_elf_add_default_symbol (bfd *abfd, |
1870 | struct bfd_link_info *info, | |
1871 | struct elf_link_hash_entry *h, | |
1872 | const char *name, | |
1873 | Elf_Internal_Sym *sym, | |
4f3fedcf AM |
1874 | asection *sec, |
1875 | bfd_vma value, | |
1876 | bfd **poldbfd, | |
0a1b45a2 | 1877 | bool *dynsym) |
45d6a902 | 1878 | { |
0a1b45a2 AM |
1879 | bool type_change_ok; |
1880 | bool size_change_ok; | |
1881 | bool skip; | |
45d6a902 AM |
1882 | char *shortname; |
1883 | struct elf_link_hash_entry *hi; | |
1884 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1885 | const struct elf_backend_data *bed; |
0a1b45a2 AM |
1886 | bool collect; |
1887 | bool dynamic; | |
f01fb44c | 1888 | bfd *override; |
45d6a902 AM |
1889 | char *p; |
1890 | size_t len, shortlen; | |
ffd65175 | 1891 | asection *tmp_sec; |
0a1b45a2 | 1892 | bool matched; |
45d6a902 | 1893 | |
422f1182 | 1894 | if (h->versioned == unversioned || h->versioned == versioned_hidden) |
0a1b45a2 | 1895 | return true; |
422f1182 | 1896 | |
45d6a902 AM |
1897 | /* If this symbol has a version, and it is the default version, we |
1898 | create an indirect symbol from the default name to the fully | |
1899 | decorated name. This will cause external references which do not | |
1900 | specify a version to be bound to this version of the symbol. */ | |
1901 | p = strchr (name, ELF_VER_CHR); | |
422f1182 L |
1902 | if (h->versioned == unknown) |
1903 | { | |
1904 | if (p == NULL) | |
1905 | { | |
1906 | h->versioned = unversioned; | |
0a1b45a2 | 1907 | return true; |
422f1182 L |
1908 | } |
1909 | else | |
1910 | { | |
1911 | if (p[1] != ELF_VER_CHR) | |
1912 | { | |
1913 | h->versioned = versioned_hidden; | |
0a1b45a2 | 1914 | return true; |
422f1182 L |
1915 | } |
1916 | else | |
1917 | h->versioned = versioned; | |
1918 | } | |
1919 | } | |
4373f8af L |
1920 | else |
1921 | { | |
1922 | /* PR ld/19073: We may see an unversioned definition after the | |
1923 | default version. */ | |
1924 | if (p == NULL) | |
0a1b45a2 | 1925 | return true; |
4373f8af | 1926 | } |
45d6a902 | 1927 | |
45d6a902 AM |
1928 | bed = get_elf_backend_data (abfd); |
1929 | collect = bed->collect; | |
1930 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1931 | ||
1932 | shortlen = p - name; | |
a50b1753 | 1933 | shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); |
45d6a902 | 1934 | if (shortname == NULL) |
0a1b45a2 | 1935 | return false; |
45d6a902 AM |
1936 | memcpy (shortname, name, shortlen); |
1937 | shortname[shortlen] = '\0'; | |
1938 | ||
1939 | /* We are going to create a new symbol. Merge it with any existing | |
1940 | symbol with this name. For the purposes of the merge, act as | |
1941 | though we were defining the symbol we just defined, although we | |
1942 | actually going to define an indirect symbol. */ | |
0a1b45a2 AM |
1943 | type_change_ok = false; |
1944 | size_change_ok = false; | |
1945 | matched = true; | |
ffd65175 AM |
1946 | tmp_sec = sec; |
1947 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
f01fb44c | 1948 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 1949 | &type_change_ok, &size_change_ok, &matched)) |
0a1b45a2 | 1950 | return false; |
45d6a902 AM |
1951 | |
1952 | if (skip) | |
1953 | goto nondefault; | |
1954 | ||
5fa370e4 | 1955 | if (hi->def_regular || ELF_COMMON_DEF_P (hi)) |
5b677558 AM |
1956 | { |
1957 | /* If the undecorated symbol will have a version added by a | |
1958 | script different to H, then don't indirect to/from the | |
1959 | undecorated symbol. This isn't ideal because we may not yet | |
1960 | have seen symbol versions, if given by a script on the | |
1961 | command line rather than via --version-script. */ | |
1962 | if (hi->verinfo.vertree == NULL && info->version_info != NULL) | |
1963 | { | |
0a1b45a2 | 1964 | bool hide; |
5b677558 AM |
1965 | |
1966 | hi->verinfo.vertree | |
1967 | = bfd_find_version_for_sym (info->version_info, | |
1968 | hi->root.root.string, &hide); | |
1969 | if (hi->verinfo.vertree != NULL && hide) | |
1970 | { | |
0a1b45a2 | 1971 | (*bed->elf_backend_hide_symbol) (info, hi, true); |
5b677558 AM |
1972 | goto nondefault; |
1973 | } | |
1974 | } | |
1975 | if (hi->verinfo.vertree != NULL | |
1976 | && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) | |
f01fb44c | 1977 | goto nondefault; |
5b677558 AM |
1978 | } |
1979 | ||
f01fb44c | 1980 | if (! override) |
45d6a902 | 1981 | { |
c6e8a9a8 | 1982 | /* Add the default symbol if not performing a relocatable link. */ |
0e1862bb | 1983 | if (! bfd_link_relocatable (info)) |
c6e8a9a8 L |
1984 | { |
1985 | bh = &hi->root; | |
fbcc8baf | 1986 | if (bh->type == bfd_link_hash_defined |
6cc71b82 | 1987 | && bh->u.def.section->owner != NULL |
fbcc8baf L |
1988 | && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0) |
1989 | { | |
1990 | /* Mark the previous definition from IR object as | |
1991 | undefined so that the generic linker will override | |
1992 | it. */ | |
1993 | bh->type = bfd_link_hash_undefined; | |
1994 | bh->u.undef.abfd = bh->u.def.section->owner; | |
1995 | } | |
c6e8a9a8 L |
1996 | if (! (_bfd_generic_link_add_one_symbol |
1997 | (info, abfd, shortname, BSF_INDIRECT, | |
1998 | bfd_ind_section_ptr, | |
0a1b45a2 AM |
1999 | 0, name, false, collect, &bh))) |
2000 | return false; | |
c6e8a9a8 L |
2001 | hi = (struct elf_link_hash_entry *) bh; |
2002 | } | |
45d6a902 AM |
2003 | } |
2004 | else | |
2005 | { | |
2006 | /* In this case the symbol named SHORTNAME is overriding the | |
2007 | indirect symbol we want to add. We were planning on making | |
2008 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
2009 | is the name without a version. NAME is the fully versioned | |
2010 | name, and it is the default version. | |
2011 | ||
2012 | Overriding means that we already saw a definition for the | |
2013 | symbol SHORTNAME in a regular object, and it is overriding | |
2014 | the symbol defined in the dynamic object. | |
2015 | ||
2016 | When this happens, we actually want to change NAME, the | |
2017 | symbol we just added, to refer to SHORTNAME. This will cause | |
2018 | references to NAME in the shared object to become references | |
2019 | to SHORTNAME in the regular object. This is what we expect | |
2020 | when we override a function in a shared object: that the | |
2021 | references in the shared object will be mapped to the | |
2022 | definition in the regular object. */ | |
2023 | ||
2024 | while (hi->root.type == bfd_link_hash_indirect | |
2025 | || hi->root.type == bfd_link_hash_warning) | |
2026 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
2027 | ||
2028 | h->root.type = bfd_link_hash_indirect; | |
2029 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 2030 | if (h->def_dynamic) |
45d6a902 | 2031 | { |
f5385ebf AM |
2032 | h->def_dynamic = 0; |
2033 | hi->ref_dynamic = 1; | |
2034 | if (hi->ref_regular | |
2035 | || hi->def_regular) | |
45d6a902 | 2036 | { |
c152c796 | 2037 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
0a1b45a2 | 2038 | return false; |
45d6a902 AM |
2039 | } |
2040 | } | |
2041 | ||
2042 | /* Now set HI to H, so that the following code will set the | |
2043 | other fields correctly. */ | |
2044 | hi = h; | |
2045 | } | |
2046 | ||
fab4a87f L |
2047 | /* Check if HI is a warning symbol. */ |
2048 | if (hi->root.type == bfd_link_hash_warning) | |
2049 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
2050 | ||
45d6a902 AM |
2051 | /* If there is a duplicate definition somewhere, then HI may not |
2052 | point to an indirect symbol. We will have reported an error to | |
2053 | the user in that case. */ | |
2054 | ||
2055 | if (hi->root.type == bfd_link_hash_indirect) | |
2056 | { | |
2057 | struct elf_link_hash_entry *ht; | |
2058 | ||
45d6a902 | 2059 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
fcfa13d2 | 2060 | (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); |
45d6a902 | 2061 | |
5160d0f3 AM |
2062 | /* If we first saw a reference to SHORTNAME with non-default |
2063 | visibility, merge that visibility to the @@VER symbol. */ | |
0a1b45a2 | 2064 | elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic); |
5160d0f3 | 2065 | |
68c88cd4 AM |
2066 | /* A reference to the SHORTNAME symbol from a dynamic library |
2067 | will be satisfied by the versioned symbol at runtime. In | |
2068 | effect, we have a reference to the versioned symbol. */ | |
2069 | ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
2070 | hi->dynamic_def |= ht->dynamic_def; | |
2071 | ||
45d6a902 AM |
2072 | /* See if the new flags lead us to realize that the symbol must |
2073 | be dynamic. */ | |
2074 | if (! *dynsym) | |
2075 | { | |
2076 | if (! dynamic) | |
2077 | { | |
0e1862bb | 2078 | if (! bfd_link_executable (info) |
90c984fc | 2079 | || hi->def_dynamic |
f5385ebf | 2080 | || hi->ref_dynamic) |
0a1b45a2 | 2081 | *dynsym = true; |
45d6a902 AM |
2082 | } |
2083 | else | |
2084 | { | |
f5385ebf | 2085 | if (hi->ref_regular) |
0a1b45a2 | 2086 | *dynsym = true; |
45d6a902 AM |
2087 | } |
2088 | } | |
2089 | } | |
2090 | ||
2091 | /* We also need to define an indirection from the nondefault version | |
2092 | of the symbol. */ | |
2093 | ||
dc1e8a47 | 2094 | nondefault: |
45d6a902 | 2095 | len = strlen (name); |
a50b1753 | 2096 | shortname = (char *) bfd_hash_allocate (&info->hash->table, len); |
45d6a902 | 2097 | if (shortname == NULL) |
0a1b45a2 | 2098 | return false; |
45d6a902 AM |
2099 | memcpy (shortname, name, shortlen); |
2100 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
2101 | ||
2102 | /* Once again, merge with any existing symbol. */ | |
0a1b45a2 AM |
2103 | type_change_ok = false; |
2104 | size_change_ok = false; | |
ffd65175 AM |
2105 | tmp_sec = sec; |
2106 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, | |
f01fb44c | 2107 | &hi, poldbfd, NULL, NULL, &skip, &override, |
6e33951e | 2108 | &type_change_ok, &size_change_ok, &matched)) |
0a1b45a2 | 2109 | return false; |
45d6a902 AM |
2110 | |
2111 | if (skip) | |
726d7d1e AM |
2112 | { |
2113 | if (!dynamic | |
2114 | && h->root.type == bfd_link_hash_defweak | |
2115 | && hi->root.type == bfd_link_hash_defined) | |
2116 | { | |
2117 | /* We are handling a weak sym@@ver and attempting to define | |
2118 | a weak sym@ver, but _bfd_elf_merge_symbol said to skip the | |
2119 | new weak sym@ver because there is already a strong sym@ver. | |
2120 | However, sym@ver and sym@@ver are really the same symbol. | |
2121 | The existing strong sym@ver ought to override sym@@ver. */ | |
2122 | h->root.type = bfd_link_hash_defined; | |
2123 | h->root.u.def.section = hi->root.u.def.section; | |
2124 | h->root.u.def.value = hi->root.u.def.value; | |
2125 | hi->root.type = bfd_link_hash_indirect; | |
2126 | hi->root.u.i.link = &h->root; | |
2127 | } | |
2128 | else | |
0a1b45a2 | 2129 | return true; |
726d7d1e | 2130 | } |
f01fb44c | 2131 | else if (override) |
45d6a902 AM |
2132 | { |
2133 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
2134 | the type of override we do in the case above unless it is | |
4cc11e76 | 2135 | overridden by a versioned definition. */ |
45d6a902 AM |
2136 | if (hi->root.type != bfd_link_hash_defined |
2137 | && hi->root.type != bfd_link_hash_defweak) | |
4eca0228 | 2138 | _bfd_error_handler |
695344c0 | 2139 | /* xgettext:c-format */ |
871b3ab2 | 2140 | (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"), |
d003868e | 2141 | abfd, shortname); |
0a1b45a2 | 2142 | return true; |
45d6a902 AM |
2143 | } |
2144 | else | |
2145 | { | |
2146 | bh = &hi->root; | |
2147 | if (! (_bfd_generic_link_add_one_symbol | |
2148 | (info, abfd, shortname, BSF_INDIRECT, | |
0a1b45a2 AM |
2149 | bfd_ind_section_ptr, 0, name, false, collect, &bh))) |
2150 | return false; | |
45d6a902 | 2151 | hi = (struct elf_link_hash_entry *) bh; |
726d7d1e | 2152 | } |
45d6a902 | 2153 | |
726d7d1e AM |
2154 | /* If there is a duplicate definition somewhere, then HI may not |
2155 | point to an indirect symbol. We will have reported an error | |
2156 | to the user in that case. */ | |
2157 | if (hi->root.type == bfd_link_hash_indirect) | |
2158 | { | |
2159 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); | |
2160 | h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; | |
2161 | hi->dynamic_def |= h->dynamic_def; | |
45d6a902 | 2162 | |
726d7d1e AM |
2163 | /* If we first saw a reference to @VER symbol with |
2164 | non-default visibility, merge that visibility to the | |
2165 | @@VER symbol. */ | |
0a1b45a2 | 2166 | elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic); |
726d7d1e AM |
2167 | |
2168 | /* See if the new flags lead us to realize that the symbol | |
2169 | must be dynamic. */ | |
2170 | if (! *dynsym) | |
45d6a902 | 2171 | { |
726d7d1e | 2172 | if (! dynamic) |
45d6a902 | 2173 | { |
726d7d1e AM |
2174 | if (! bfd_link_executable (info) |
2175 | || hi->ref_dynamic) | |
0a1b45a2 | 2176 | *dynsym = true; |
726d7d1e AM |
2177 | } |
2178 | else | |
2179 | { | |
2180 | if (hi->ref_regular) | |
0a1b45a2 | 2181 | *dynsym = true; |
45d6a902 AM |
2182 | } |
2183 | } | |
2184 | } | |
2185 | ||
0a1b45a2 | 2186 | return true; |
45d6a902 AM |
2187 | } |
2188 | \f | |
2189 | /* This routine is used to export all defined symbols into the dynamic | |
2190 | symbol table. It is called via elf_link_hash_traverse. */ | |
2191 | ||
0a1b45a2 | 2192 | static bool |
268b6b39 | 2193 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2194 | { |
a50b1753 | 2195 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
45d6a902 AM |
2196 | |
2197 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2198 | if (h->root.type == bfd_link_hash_indirect) | |
0a1b45a2 | 2199 | return true; |
45d6a902 | 2200 | |
7686d77d AM |
2201 | /* Ignore this if we won't export it. */ |
2202 | if (!eif->info->export_dynamic && !h->dynamic) | |
0a1b45a2 | 2203 | return true; |
45d6a902 AM |
2204 | |
2205 | if (h->dynindx == -1 | |
fd91d419 L |
2206 | && (h->def_regular || h->ref_regular) |
2207 | && ! bfd_hide_sym_by_version (eif->info->version_info, | |
2208 | h->root.root.string)) | |
45d6a902 | 2209 | { |
fd91d419 | 2210 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2211 | { |
0a1b45a2 AM |
2212 | eif->failed = true; |
2213 | return false; | |
45d6a902 AM |
2214 | } |
2215 | } | |
2216 | ||
0a1b45a2 | 2217 | return true; |
45d6a902 AM |
2218 | } |
2219 | \f | |
72aa8173 L |
2220 | /* Return true if GLIBC_ABI_DT_RELR is added to the list of version |
2221 | dependencies successfully. GLIBC_ABI_DT_RELR will be put into the | |
2222 | .gnu.version_r section. */ | |
2223 | ||
2224 | static bool | |
2225 | elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo) | |
2226 | { | |
2227 | bfd *glibc_bfd = NULL; | |
2228 | Elf_Internal_Verneed *t; | |
2229 | Elf_Internal_Vernaux *a; | |
2230 | size_t amt; | |
2231 | const char *relr = "GLIBC_ABI_DT_RELR"; | |
2232 | ||
2233 | /* See if we already know about GLIBC_PRIVATE_DT_RELR. */ | |
2234 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; | |
2235 | t != NULL; | |
2236 | t = t->vn_nextref) | |
2237 | { | |
2238 | const char *soname = bfd_elf_get_dt_soname (t->vn_bfd); | |
2239 | /* Skip the shared library if it isn't libc.so. */ | |
2240 | if (!soname || !startswith (soname, "libc.so.")) | |
2241 | continue; | |
2242 | ||
2243 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2244 | { | |
2245 | /* Return if GLIBC_PRIVATE_DT_RELR dependency has been | |
2246 | added. */ | |
2247 | if (a->vna_nodename == relr | |
2248 | || strcmp (a->vna_nodename, relr) == 0) | |
2249 | return true; | |
2250 | ||
2251 | /* Check if libc.so provides GLIBC_2.XX version. */ | |
2252 | if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2.")) | |
2253 | glibc_bfd = t->vn_bfd; | |
2254 | } | |
2255 | ||
2256 | break; | |
2257 | } | |
2258 | ||
2259 | /* Skip if it isn't linked against glibc. */ | |
2260 | if (glibc_bfd == NULL) | |
2261 | return true; | |
2262 | ||
2263 | /* This is a new version. Add it to tree we are building. */ | |
2264 | if (t == NULL) | |
2265 | { | |
2266 | amt = sizeof *t; | |
2267 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, | |
2268 | amt); | |
2269 | if (t == NULL) | |
2270 | { | |
2271 | rinfo->failed = true; | |
2272 | return false; | |
2273 | } | |
2274 | ||
2275 | t->vn_bfd = glibc_bfd; | |
2276 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; | |
2277 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
2278 | } | |
2279 | ||
2280 | amt = sizeof *a; | |
2281 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); | |
2282 | if (a == NULL) | |
2283 | { | |
2284 | rinfo->failed = true; | |
2285 | return false; | |
2286 | } | |
2287 | ||
2288 | a->vna_nodename = relr; | |
2289 | a->vna_flags = 0; | |
2290 | a->vna_nextptr = t->vn_auxptr; | |
2291 | a->vna_other = rinfo->vers + 1; | |
2292 | ++rinfo->vers; | |
2293 | ||
2294 | t->vn_auxptr = a; | |
2295 | ||
2296 | return true; | |
2297 | } | |
2298 | ||
45d6a902 AM |
2299 | /* Look through the symbols which are defined in other shared |
2300 | libraries and referenced here. Update the list of version | |
2301 | dependencies. This will be put into the .gnu.version_r section. | |
2302 | This function is called via elf_link_hash_traverse. */ | |
2303 | ||
0a1b45a2 | 2304 | static bool |
268b6b39 AM |
2305 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
2306 | void *data) | |
45d6a902 | 2307 | { |
a50b1753 | 2308 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; |
45d6a902 AM |
2309 | Elf_Internal_Verneed *t; |
2310 | Elf_Internal_Vernaux *a; | |
986f0783 | 2311 | size_t amt; |
45d6a902 | 2312 | |
45d6a902 AM |
2313 | /* We only care about symbols defined in shared objects with version |
2314 | information. */ | |
f5385ebf AM |
2315 | if (!h->def_dynamic |
2316 | || h->def_regular | |
45d6a902 | 2317 | || h->dynindx == -1 |
7b20f099 AM |
2318 | || h->verinfo.verdef == NULL |
2319 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
2320 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
0a1b45a2 | 2321 | return true; |
45d6a902 AM |
2322 | |
2323 | /* See if we already know about this version. */ | |
28caa186 AM |
2324 | for (t = elf_tdata (rinfo->info->output_bfd)->verref; |
2325 | t != NULL; | |
2326 | t = t->vn_nextref) | |
45d6a902 AM |
2327 | { |
2328 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
2329 | continue; | |
2330 | ||
2331 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2332 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
0a1b45a2 | 2333 | return true; |
45d6a902 AM |
2334 | |
2335 | break; | |
2336 | } | |
2337 | ||
2338 | /* This is a new version. Add it to tree we are building. */ | |
2339 | ||
2340 | if (t == NULL) | |
2341 | { | |
2342 | amt = sizeof *t; | |
a50b1753 | 2343 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); |
45d6a902 AM |
2344 | if (t == NULL) |
2345 | { | |
0a1b45a2 AM |
2346 | rinfo->failed = true; |
2347 | return false; | |
45d6a902 AM |
2348 | } |
2349 | ||
2350 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
28caa186 AM |
2351 | t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; |
2352 | elf_tdata (rinfo->info->output_bfd)->verref = t; | |
45d6a902 AM |
2353 | } |
2354 | ||
2355 | amt = sizeof *a; | |
a50b1753 | 2356 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); |
14b1c01e AM |
2357 | if (a == NULL) |
2358 | { | |
0a1b45a2 AM |
2359 | rinfo->failed = true; |
2360 | return false; | |
14b1c01e | 2361 | } |
45d6a902 AM |
2362 | |
2363 | /* Note that we are copying a string pointer here, and testing it | |
2364 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2365 | discard the string data when low in memory, this will have to be | |
2366 | fixed. */ | |
2367 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2368 | ||
2369 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2370 | a->vna_nextptr = t->vn_auxptr; | |
2371 | ||
2372 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2373 | ++rinfo->vers; | |
2374 | ||
2375 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2376 | ||
2377 | t->vn_auxptr = a; | |
2378 | ||
0a1b45a2 | 2379 | return true; |
45d6a902 AM |
2380 | } |
2381 | ||
099bb8fb L |
2382 | /* Return TRUE and set *HIDE to TRUE if the versioned symbol is |
2383 | hidden. Set *T_P to NULL if there is no match. */ | |
2384 | ||
0a1b45a2 | 2385 | static bool |
099bb8fb L |
2386 | _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info, |
2387 | struct elf_link_hash_entry *h, | |
2388 | const char *version_p, | |
2389 | struct bfd_elf_version_tree **t_p, | |
0a1b45a2 | 2390 | bool *hide) |
099bb8fb L |
2391 | { |
2392 | struct bfd_elf_version_tree *t; | |
2393 | ||
2394 | /* Look for the version. If we find it, it is no longer weak. */ | |
2395 | for (t = info->version_info; t != NULL; t = t->next) | |
2396 | { | |
2397 | if (strcmp (t->name, version_p) == 0) | |
2398 | { | |
2399 | size_t len; | |
2400 | char *alc; | |
2401 | struct bfd_elf_version_expr *d; | |
2402 | ||
2403 | len = version_p - h->root.root.string; | |
2404 | alc = (char *) bfd_malloc (len); | |
2405 | if (alc == NULL) | |
0a1b45a2 | 2406 | return false; |
099bb8fb L |
2407 | memcpy (alc, h->root.root.string, len - 1); |
2408 | alc[len - 1] = '\0'; | |
2409 | if (alc[len - 2] == ELF_VER_CHR) | |
2410 | alc[len - 2] = '\0'; | |
2411 | ||
2412 | h->verinfo.vertree = t; | |
0a1b45a2 | 2413 | t->used = true; |
099bb8fb L |
2414 | d = NULL; |
2415 | ||
2416 | if (t->globals.list != NULL) | |
2417 | d = (*t->match) (&t->globals, NULL, alc); | |
2418 | ||
2419 | /* See if there is anything to force this symbol to | |
2420 | local scope. */ | |
2421 | if (d == NULL && t->locals.list != NULL) | |
2422 | { | |
2423 | d = (*t->match) (&t->locals, NULL, alc); | |
2424 | if (d != NULL | |
2425 | && h->dynindx != -1 | |
2426 | && ! info->export_dynamic) | |
0a1b45a2 | 2427 | *hide = true; |
099bb8fb L |
2428 | } |
2429 | ||
2430 | free (alc); | |
2431 | break; | |
2432 | } | |
2433 | } | |
2434 | ||
2435 | *t_p = t; | |
2436 | ||
0a1b45a2 | 2437 | return true; |
099bb8fb L |
2438 | } |
2439 | ||
2440 | /* Return TRUE if the symbol H is hidden by version script. */ | |
2441 | ||
0a1b45a2 | 2442 | bool |
099bb8fb L |
2443 | _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info, |
2444 | struct elf_link_hash_entry *h) | |
2445 | { | |
2446 | const char *p; | |
0a1b45a2 | 2447 | bool hide = false; |
099bb8fb L |
2448 | const struct elf_backend_data *bed |
2449 | = get_elf_backend_data (info->output_bfd); | |
2450 | ||
2451 | /* Version script only hides symbols defined in regular objects. */ | |
2452 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) | |
0a1b45a2 | 2453 | return true; |
099bb8fb L |
2454 | |
2455 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
2456 | if (p != NULL && h->verinfo.vertree == NULL) | |
2457 | { | |
2458 | struct bfd_elf_version_tree *t; | |
2459 | ||
2460 | ++p; | |
2461 | if (*p == ELF_VER_CHR) | |
2462 | ++p; | |
2463 | ||
2464 | if (*p != '\0' | |
2465 | && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide) | |
2466 | && hide) | |
2467 | { | |
2468 | if (hide) | |
0a1b45a2 AM |
2469 | (*bed->elf_backend_hide_symbol) (info, h, true); |
2470 | return true; | |
099bb8fb L |
2471 | } |
2472 | } | |
2473 | ||
2474 | /* If we don't have a version for this symbol, see if we can find | |
2475 | something. */ | |
2476 | if (h->verinfo.vertree == NULL && info->version_info != NULL) | |
2477 | { | |
2478 | h->verinfo.vertree | |
2479 | = bfd_find_version_for_sym (info->version_info, | |
2480 | h->root.root.string, &hide); | |
2481 | if (h->verinfo.vertree != NULL && hide) | |
2482 | { | |
0a1b45a2 AM |
2483 | (*bed->elf_backend_hide_symbol) (info, h, true); |
2484 | return true; | |
099bb8fb L |
2485 | } |
2486 | } | |
2487 | ||
0a1b45a2 | 2488 | return false; |
099bb8fb L |
2489 | } |
2490 | ||
45d6a902 AM |
2491 | /* Figure out appropriate versions for all the symbols. We may not |
2492 | have the version number script until we have read all of the input | |
2493 | files, so until that point we don't know which symbols should be | |
2494 | local. This function is called via elf_link_hash_traverse. */ | |
2495 | ||
0a1b45a2 | 2496 | static bool |
268b6b39 | 2497 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2498 | { |
28caa186 | 2499 | struct elf_info_failed *sinfo; |
45d6a902 | 2500 | struct bfd_link_info *info; |
9c5bfbb7 | 2501 | const struct elf_backend_data *bed; |
45d6a902 AM |
2502 | struct elf_info_failed eif; |
2503 | char *p; | |
0a1b45a2 | 2504 | bool hide; |
45d6a902 | 2505 | |
a50b1753 | 2506 | sinfo = (struct elf_info_failed *) data; |
45d6a902 AM |
2507 | info = sinfo->info; |
2508 | ||
45d6a902 | 2509 | /* Fix the symbol flags. */ |
0a1b45a2 | 2510 | eif.failed = false; |
45d6a902 AM |
2511 | eif.info = info; |
2512 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
2513 | { | |
2514 | if (eif.failed) | |
0a1b45a2 AM |
2515 | sinfo->failed = true; |
2516 | return false; | |
45d6a902 AM |
2517 | } |
2518 | ||
0a640d71 L |
2519 | bed = get_elf_backend_data (info->output_bfd); |
2520 | ||
45d6a902 AM |
2521 | /* We only need version numbers for symbols defined in regular |
2522 | objects. */ | |
5fa370e4 | 2523 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
0a640d71 L |
2524 | { |
2525 | /* Hide symbols defined in discarded input sections. */ | |
2526 | if ((h->root.type == bfd_link_hash_defined | |
2527 | || h->root.type == bfd_link_hash_defweak) | |
2528 | && discarded_section (h->root.u.def.section)) | |
0a1b45a2 AM |
2529 | (*bed->elf_backend_hide_symbol) (info, h, true); |
2530 | return true; | |
0a640d71 | 2531 | } |
45d6a902 | 2532 | |
0a1b45a2 | 2533 | hide = false; |
45d6a902 AM |
2534 | p = strchr (h->root.root.string, ELF_VER_CHR); |
2535 | if (p != NULL && h->verinfo.vertree == NULL) | |
2536 | { | |
2537 | struct bfd_elf_version_tree *t; | |
45d6a902 | 2538 | |
45d6a902 AM |
2539 | ++p; |
2540 | if (*p == ELF_VER_CHR) | |
6e33951e | 2541 | ++p; |
45d6a902 AM |
2542 | |
2543 | /* If there is no version string, we can just return out. */ | |
2544 | if (*p == '\0') | |
0a1b45a2 | 2545 | return true; |
45d6a902 | 2546 | |
099bb8fb | 2547 | if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)) |
45d6a902 | 2548 | { |
0a1b45a2 AM |
2549 | sinfo->failed = true; |
2550 | return false; | |
45d6a902 AM |
2551 | } |
2552 | ||
099bb8fb | 2553 | if (hide) |
0a1b45a2 | 2554 | (*bed->elf_backend_hide_symbol) (info, h, true); |
099bb8fb | 2555 | |
45d6a902 AM |
2556 | /* If we are building an application, we need to create a |
2557 | version node for this version. */ | |
0e1862bb | 2558 | if (t == NULL && bfd_link_executable (info)) |
45d6a902 AM |
2559 | { |
2560 | struct bfd_elf_version_tree **pp; | |
2561 | int version_index; | |
2562 | ||
2563 | /* If we aren't going to export this symbol, we don't need | |
2564 | to worry about it. */ | |
2565 | if (h->dynindx == -1) | |
0a1b45a2 | 2566 | return true; |
45d6a902 | 2567 | |
ef53be89 AM |
2568 | t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, |
2569 | sizeof *t); | |
45d6a902 AM |
2570 | if (t == NULL) |
2571 | { | |
0a1b45a2 AM |
2572 | sinfo->failed = true; |
2573 | return false; | |
45d6a902 AM |
2574 | } |
2575 | ||
45d6a902 | 2576 | t->name = p; |
45d6a902 | 2577 | t->name_indx = (unsigned int) -1; |
0a1b45a2 | 2578 | t->used = true; |
45d6a902 AM |
2579 | |
2580 | version_index = 1; | |
2581 | /* Don't count anonymous version tag. */ | |
fd91d419 L |
2582 | if (sinfo->info->version_info != NULL |
2583 | && sinfo->info->version_info->vernum == 0) | |
45d6a902 | 2584 | version_index = 0; |
fd91d419 L |
2585 | for (pp = &sinfo->info->version_info; |
2586 | *pp != NULL; | |
2587 | pp = &(*pp)->next) | |
45d6a902 AM |
2588 | ++version_index; |
2589 | t->vernum = version_index; | |
2590 | ||
2591 | *pp = t; | |
2592 | ||
2593 | h->verinfo.vertree = t; | |
2594 | } | |
2595 | else if (t == NULL) | |
2596 | { | |
2597 | /* We could not find the version for a symbol when | |
2598 | generating a shared archive. Return an error. */ | |
4eca0228 | 2599 | _bfd_error_handler |
695344c0 | 2600 | /* xgettext:c-format */ |
871b3ab2 | 2601 | (_("%pB: version node not found for symbol %s"), |
28caa186 | 2602 | info->output_bfd, h->root.root.string); |
45d6a902 | 2603 | bfd_set_error (bfd_error_bad_value); |
0a1b45a2 AM |
2604 | sinfo->failed = true; |
2605 | return false; | |
45d6a902 | 2606 | } |
45d6a902 AM |
2607 | } |
2608 | ||
2609 | /* If we don't have a version for this symbol, see if we can find | |
2610 | something. */ | |
099bb8fb L |
2611 | if (!hide |
2612 | && h->verinfo.vertree == NULL | |
2613 | && sinfo->info->version_info != NULL) | |
45d6a902 | 2614 | { |
fd91d419 L |
2615 | h->verinfo.vertree |
2616 | = bfd_find_version_for_sym (sinfo->info->version_info, | |
2617 | h->root.root.string, &hide); | |
1e8fa21e | 2618 | if (h->verinfo.vertree != NULL && hide) |
0a1b45a2 | 2619 | (*bed->elf_backend_hide_symbol) (info, h, true); |
45d6a902 AM |
2620 | } |
2621 | ||
0a1b45a2 | 2622 | return true; |
45d6a902 AM |
2623 | } |
2624 | \f | |
45d6a902 AM |
2625 | /* Read and swap the relocs from the section indicated by SHDR. This |
2626 | may be either a REL or a RELA section. The relocations are | |
2627 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
2628 | which should have already been allocated to contain enough space. | |
2629 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
2630 | relocations should be stored. | |
2631 | ||
2632 | Returns FALSE if something goes wrong. */ | |
2633 | ||
0a1b45a2 | 2634 | static bool |
268b6b39 | 2635 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 2636 | asection *sec, |
268b6b39 AM |
2637 | Elf_Internal_Shdr *shdr, |
2638 | void *external_relocs, | |
2639 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 2640 | { |
9c5bfbb7 | 2641 | const struct elf_backend_data *bed; |
268b6b39 | 2642 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
2643 | const bfd_byte *erela; |
2644 | const bfd_byte *erelaend; | |
2645 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
2646 | Elf_Internal_Shdr *symtab_hdr; |
2647 | size_t nsyms; | |
45d6a902 | 2648 | |
45d6a902 AM |
2649 | /* Position ourselves at the start of the section. */ |
2650 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
0a1b45a2 | 2651 | return false; |
45d6a902 AM |
2652 | |
2653 | /* Read the relocations. */ | |
226f9f4f | 2654 | if (bfd_read (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) |
0a1b45a2 | 2655 | return false; |
45d6a902 | 2656 | |
243ef1e0 | 2657 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
ce98a316 | 2658 | nsyms = NUM_SHDR_ENTRIES (symtab_hdr); |
243ef1e0 | 2659 | |
45d6a902 AM |
2660 | bed = get_elf_backend_data (abfd); |
2661 | ||
2662 | /* Convert the external relocations to the internal format. */ | |
2663 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
2664 | swap_in = bed->s->swap_reloc_in; | |
2665 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
2666 | swap_in = bed->s->swap_reloca_in; | |
2667 | else | |
2668 | { | |
2669 | bfd_set_error (bfd_error_wrong_format); | |
0a1b45a2 | 2670 | return false; |
45d6a902 AM |
2671 | } |
2672 | ||
a50b1753 | 2673 | erela = (const bfd_byte *) external_relocs; |
f55b1e32 AM |
2674 | /* Setting erelaend like this and comparing with <= handles case of |
2675 | a fuzzed object with sh_size not a multiple of sh_entsize. */ | |
2676 | erelaend = erela + shdr->sh_size - shdr->sh_entsize; | |
45d6a902 | 2677 | irela = internal_relocs; |
f55b1e32 | 2678 | while (erela <= erelaend) |
45d6a902 | 2679 | { |
243ef1e0 L |
2680 | bfd_vma r_symndx; |
2681 | ||
45d6a902 | 2682 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
2683 | r_symndx = ELF32_R_SYM (irela->r_info); |
2684 | if (bed->s->arch_size == 64) | |
2685 | r_symndx >>= 24; | |
ce98a316 NC |
2686 | if (nsyms > 0) |
2687 | { | |
2688 | if ((size_t) r_symndx >= nsyms) | |
2689 | { | |
4eca0228 | 2690 | _bfd_error_handler |
695344c0 | 2691 | /* xgettext:c-format */ |
2dcf00ce AM |
2692 | (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)" |
2693 | " for offset %#" PRIx64 " in section `%pA'"), | |
2694 | abfd, (uint64_t) r_symndx, (unsigned long) nsyms, | |
2695 | (uint64_t) irela->r_offset, sec); | |
ce98a316 | 2696 | bfd_set_error (bfd_error_bad_value); |
0a1b45a2 | 2697 | return false; |
ce98a316 NC |
2698 | } |
2699 | } | |
cf35638d | 2700 | else if (r_symndx != STN_UNDEF) |
243ef1e0 | 2701 | { |
4eca0228 | 2702 | _bfd_error_handler |
695344c0 | 2703 | /* xgettext:c-format */ |
2dcf00ce AM |
2704 | (_("%pB: non-zero symbol index (%#" PRIx64 ")" |
2705 | " for offset %#" PRIx64 " in section `%pA'" | |
ce98a316 | 2706 | " when the object file has no symbol table"), |
2dcf00ce AM |
2707 | abfd, (uint64_t) r_symndx, |
2708 | (uint64_t) irela->r_offset, sec); | |
243ef1e0 | 2709 | bfd_set_error (bfd_error_bad_value); |
0a1b45a2 | 2710 | return false; |
243ef1e0 | 2711 | } |
45d6a902 AM |
2712 | irela += bed->s->int_rels_per_ext_rel; |
2713 | erela += shdr->sh_entsize; | |
2714 | } | |
2715 | ||
0a1b45a2 | 2716 | return true; |
45d6a902 AM |
2717 | } |
2718 | ||
2719 | /* Read and swap the relocs for a section O. They may have been | |
2720 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
2721 | not NULL, they are used as buffers to read into. They are known to | |
2722 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
2723 | the return value is allocated using either malloc or bfd_alloc, | |
2724 | according to the KEEP_MEMORY argument. If O has two relocation | |
2725 | sections (both REL and RELA relocations), then the REL_HDR | |
2726 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
a8dde0a2 L |
2727 | RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true, |
2728 | update cache_size. */ | |
45d6a902 AM |
2729 | |
2730 | Elf_Internal_Rela * | |
a8dde0a2 L |
2731 | _bfd_elf_link_info_read_relocs (bfd *abfd, |
2732 | struct bfd_link_info *info, | |
2733 | asection *o, | |
2734 | void *external_relocs, | |
2735 | Elf_Internal_Rela *internal_relocs, | |
2736 | bool keep_memory) | |
45d6a902 | 2737 | { |
268b6b39 | 2738 | void *alloc1 = NULL; |
45d6a902 | 2739 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 2740 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2741 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
2742 | Elf_Internal_Rela *internal_rela_relocs; | |
45d6a902 | 2743 | |
d4730f92 BS |
2744 | if (esdo->relocs != NULL) |
2745 | return esdo->relocs; | |
45d6a902 AM |
2746 | |
2747 | if (o->reloc_count == 0) | |
2748 | return NULL; | |
2749 | ||
45d6a902 AM |
2750 | if (internal_relocs == NULL) |
2751 | { | |
2752 | bfd_size_type size; | |
2753 | ||
056bafd4 | 2754 | size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); |
45d6a902 | 2755 | if (keep_memory) |
a8dde0a2 L |
2756 | { |
2757 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); | |
2758 | if (info) | |
2759 | info->cache_size += size; | |
2760 | } | |
45d6a902 | 2761 | else |
a50b1753 | 2762 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
45d6a902 AM |
2763 | if (internal_relocs == NULL) |
2764 | goto error_return; | |
2765 | } | |
2766 | ||
2767 | if (external_relocs == NULL) | |
2768 | { | |
d4730f92 BS |
2769 | bfd_size_type size = 0; |
2770 | ||
2771 | if (esdo->rel.hdr) | |
2772 | size += esdo->rel.hdr->sh_size; | |
2773 | if (esdo->rela.hdr) | |
2774 | size += esdo->rela.hdr->sh_size; | |
45d6a902 | 2775 | |
268b6b39 | 2776 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
2777 | if (alloc1 == NULL) |
2778 | goto error_return; | |
2779 | external_relocs = alloc1; | |
2780 | } | |
2781 | ||
d4730f92 BS |
2782 | internal_rela_relocs = internal_relocs; |
2783 | if (esdo->rel.hdr) | |
2784 | { | |
2785 | if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, | |
2786 | external_relocs, | |
2787 | internal_relocs)) | |
2788 | goto error_return; | |
2789 | external_relocs = (((bfd_byte *) external_relocs) | |
2790 | + esdo->rel.hdr->sh_size); | |
2791 | internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) | |
2792 | * bed->s->int_rels_per_ext_rel); | |
2793 | } | |
2794 | ||
2795 | if (esdo->rela.hdr | |
2796 | && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, | |
2797 | external_relocs, | |
2798 | internal_rela_relocs))) | |
45d6a902 AM |
2799 | goto error_return; |
2800 | ||
2801 | /* Cache the results for next time, if we can. */ | |
2802 | if (keep_memory) | |
d4730f92 | 2803 | esdo->relocs = internal_relocs; |
45d6a902 | 2804 | |
c9594989 | 2805 | free (alloc1); |
45d6a902 AM |
2806 | |
2807 | /* Don't free alloc2, since if it was allocated we are passing it | |
2808 | back (under the name of internal_relocs). */ | |
2809 | ||
2810 | return internal_relocs; | |
2811 | ||
2812 | error_return: | |
c9594989 | 2813 | free (alloc1); |
45d6a902 | 2814 | if (alloc2 != NULL) |
4dd07732 AM |
2815 | { |
2816 | if (keep_memory) | |
2817 | bfd_release (abfd, alloc2); | |
2818 | else | |
2819 | free (alloc2); | |
2820 | } | |
45d6a902 AM |
2821 | return NULL; |
2822 | } | |
2823 | ||
a8dde0a2 L |
2824 | /* This is similar to _bfd_elf_link_info_read_relocs, except for that |
2825 | NULL is passed to _bfd_elf_link_info_read_relocs for pointer to | |
2826 | struct bfd_link_info. */ | |
2827 | ||
2828 | Elf_Internal_Rela * | |
2829 | _bfd_elf_link_read_relocs (bfd *abfd, | |
2830 | asection *o, | |
2831 | void *external_relocs, | |
2832 | Elf_Internal_Rela *internal_relocs, | |
2833 | bool keep_memory) | |
2834 | { | |
2835 | return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs, | |
2836 | internal_relocs, keep_memory); | |
2837 | ||
2838 | } | |
2839 | ||
45d6a902 AM |
2840 | /* Compute the size of, and allocate space for, REL_HDR which is the |
2841 | section header for a section containing relocations for O. */ | |
2842 | ||
0a1b45a2 | 2843 | static bool |
9eaff861 AO |
2844 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2845 | struct bfd_elf_section_reloc_data *reldata) | |
45d6a902 | 2846 | { |
9eaff861 | 2847 | Elf_Internal_Shdr *rel_hdr = reldata->hdr; |
45d6a902 AM |
2848 | |
2849 | /* That allows us to calculate the size of the section. */ | |
9eaff861 | 2850 | rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; |
45d6a902 AM |
2851 | |
2852 | /* The contents field must last into write_object_contents, so we | |
2853 | allocate it with bfd_alloc rather than malloc. Also since we | |
2854 | cannot be sure that the contents will actually be filled in, | |
2855 | we zero the allocated space. */ | |
a50b1753 | 2856 | rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 | 2857 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
0a1b45a2 | 2858 | return false; |
45d6a902 | 2859 | |
d4730f92 | 2860 | if (reldata->hashes == NULL && reldata->count) |
45d6a902 AM |
2861 | { |
2862 | struct elf_link_hash_entry **p; | |
2863 | ||
ca4be51c AM |
2864 | p = ((struct elf_link_hash_entry **) |
2865 | bfd_zmalloc (reldata->count * sizeof (*p))); | |
45d6a902 | 2866 | if (p == NULL) |
0a1b45a2 | 2867 | return false; |
45d6a902 | 2868 | |
d4730f92 | 2869 | reldata->hashes = p; |
45d6a902 AM |
2870 | } |
2871 | ||
0a1b45a2 | 2872 | return true; |
45d6a902 AM |
2873 | } |
2874 | ||
2875 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2876 | originated from the section given by INPUT_REL_HDR) to the | |
2877 | OUTPUT_BFD. */ | |
2878 | ||
0a1b45a2 | 2879 | bool |
268b6b39 AM |
2880 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2881 | asection *input_section, | |
2882 | Elf_Internal_Shdr *input_rel_hdr, | |
eac338cf PB |
2883 | Elf_Internal_Rela *internal_relocs, |
2884 | struct elf_link_hash_entry **rel_hash | |
2885 | ATTRIBUTE_UNUSED) | |
45d6a902 AM |
2886 | { |
2887 | Elf_Internal_Rela *irela; | |
2888 | Elf_Internal_Rela *irelaend; | |
2889 | bfd_byte *erel; | |
d4730f92 | 2890 | struct bfd_elf_section_reloc_data *output_reldata; |
45d6a902 | 2891 | asection *output_section; |
9c5bfbb7 | 2892 | const struct elf_backend_data *bed; |
268b6b39 | 2893 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
d4730f92 | 2894 | struct bfd_elf_section_data *esdo; |
45d6a902 AM |
2895 | |
2896 | output_section = input_section->output_section; | |
45d6a902 | 2897 | |
d4730f92 BS |
2898 | bed = get_elf_backend_data (output_bfd); |
2899 | esdo = elf_section_data (output_section); | |
2900 | if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2901 | { |
d4730f92 BS |
2902 | output_reldata = &esdo->rel; |
2903 | swap_out = bed->s->swap_reloc_out; | |
45d6a902 | 2904 | } |
d4730f92 BS |
2905 | else if (esdo->rela.hdr |
2906 | && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) | |
45d6a902 | 2907 | { |
d4730f92 BS |
2908 | output_reldata = &esdo->rela; |
2909 | swap_out = bed->s->swap_reloca_out; | |
45d6a902 AM |
2910 | } |
2911 | else | |
2912 | { | |
4eca0228 | 2913 | _bfd_error_handler |
695344c0 | 2914 | /* xgettext:c-format */ |
871b3ab2 | 2915 | (_("%pB: relocation size mismatch in %pB section %pA"), |
d003868e | 2916 | output_bfd, input_section->owner, input_section); |
297d8443 | 2917 | bfd_set_error (bfd_error_wrong_format); |
0a1b45a2 | 2918 | return false; |
45d6a902 AM |
2919 | } |
2920 | ||
d4730f92 BS |
2921 | erel = output_reldata->hdr->contents; |
2922 | erel += output_reldata->count * input_rel_hdr->sh_entsize; | |
45d6a902 AM |
2923 | irela = internal_relocs; |
2924 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2925 | * bed->s->int_rels_per_ext_rel); | |
2926 | while (irela < irelaend) | |
2927 | { | |
2928 | (*swap_out) (output_bfd, irela, erel); | |
2929 | irela += bed->s->int_rels_per_ext_rel; | |
2930 | erel += input_rel_hdr->sh_entsize; | |
2931 | } | |
2932 | ||
2933 | /* Bump the counter, so that we know where to add the next set of | |
2934 | relocations. */ | |
d4730f92 | 2935 | output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); |
45d6a902 | 2936 | |
0a1b45a2 | 2937 | return true; |
45d6a902 AM |
2938 | } |
2939 | \f | |
508c3946 L |
2940 | /* Make weak undefined symbols in PIE dynamic. */ |
2941 | ||
0a1b45a2 | 2942 | bool |
508c3946 L |
2943 | _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, |
2944 | struct elf_link_hash_entry *h) | |
2945 | { | |
0e1862bb | 2946 | if (bfd_link_pie (info) |
508c3946 L |
2947 | && h->dynindx == -1 |
2948 | && h->root.type == bfd_link_hash_undefweak) | |
2949 | return bfd_elf_link_record_dynamic_symbol (info, h); | |
2950 | ||
0a1b45a2 | 2951 | return true; |
508c3946 L |
2952 | } |
2953 | ||
45d6a902 AM |
2954 | /* Fix up the flags for a symbol. This handles various cases which |
2955 | can only be fixed after all the input files are seen. This is | |
2956 | currently called by both adjust_dynamic_symbol and | |
2957 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2958 | the face of future changes. */ | |
2959 | ||
0a1b45a2 | 2960 | static bool |
268b6b39 AM |
2961 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2962 | struct elf_info_failed *eif) | |
45d6a902 | 2963 | { |
33774f08 | 2964 | const struct elf_backend_data *bed; |
508c3946 | 2965 | |
45d6a902 AM |
2966 | /* If this symbol was mentioned in a non-ELF file, try to set |
2967 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2968 | permit a non-ELF file to correctly refer to a symbol defined in | |
2969 | an ELF dynamic object. */ | |
f5385ebf | 2970 | if (h->non_elf) |
45d6a902 AM |
2971 | { |
2972 | while (h->root.type == bfd_link_hash_indirect) | |
2973 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2974 | ||
2975 | if (h->root.type != bfd_link_hash_defined | |
2976 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2977 | { |
2978 | h->ref_regular = 1; | |
2979 | h->ref_regular_nonweak = 1; | |
2980 | } | |
45d6a902 AM |
2981 | else |
2982 | { | |
2983 | if (h->root.u.def.section->owner != NULL | |
2984 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2985 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2986 | { |
2987 | h->ref_regular = 1; | |
2988 | h->ref_regular_nonweak = 1; | |
2989 | } | |
45d6a902 | 2990 | else |
f5385ebf | 2991 | h->def_regular = 1; |
45d6a902 AM |
2992 | } |
2993 | ||
2994 | if (h->dynindx == -1 | |
f5385ebf AM |
2995 | && (h->def_dynamic |
2996 | || h->ref_dynamic)) | |
45d6a902 | 2997 | { |
c152c796 | 2998 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 | 2999 | { |
0a1b45a2 AM |
3000 | eif->failed = true; |
3001 | return false; | |
45d6a902 AM |
3002 | } |
3003 | } | |
3004 | } | |
3005 | else | |
3006 | { | |
f5385ebf | 3007 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
3008 | was first seen in a non-ELF file. Fortunately, if the symbol |
3009 | was first seen in an ELF file, we're probably OK unless the | |
3010 | symbol was defined in a non-ELF file. Catch that case here. | |
3011 | FIXME: We're still in trouble if the symbol was first seen in | |
3012 | a dynamic object, and then later in a non-ELF regular object. */ | |
3013 | if ((h->root.type == bfd_link_hash_defined | |
3014 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 3015 | && !h->def_regular |
45d6a902 AM |
3016 | && (h->root.u.def.section->owner != NULL |
3017 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
3018 | != bfd_target_elf_flavour) | |
3019 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
3020 | && !h->def_dynamic))) |
3021 | h->def_regular = 1; | |
45d6a902 AM |
3022 | } |
3023 | ||
508c3946 | 3024 | /* Backend specific symbol fixup. */ |
33774f08 AM |
3025 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
3026 | if (bed->elf_backend_fixup_symbol | |
3027 | && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) | |
0a1b45a2 | 3028 | return false; |
508c3946 | 3029 | |
45d6a902 AM |
3030 | /* If this is a final link, and the symbol was defined as a common |
3031 | symbol in a regular object file, and there was no definition in | |
3032 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 3033 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
3034 | flag will not have been set. */ |
3035 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
3036 | && !h->def_regular |
3037 | && h->ref_regular | |
3038 | && !h->def_dynamic | |
96f29d96 | 3039 | && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) |
f5385ebf | 3040 | h->def_regular = 1; |
45d6a902 | 3041 | |
af0bfb9c AM |
3042 | /* Symbols defined in discarded sections shouldn't be dynamic. */ |
3043 | if (h->root.type == bfd_link_hash_undefined && h->indx == -3) | |
0a1b45a2 | 3044 | (*bed->elf_backend_hide_symbol) (eif->info, h, true); |
af0bfb9c | 3045 | |
4deb8f71 L |
3046 | /* If a weak undefined symbol has non-default visibility, we also |
3047 | hide it from the dynamic linker. */ | |
af0bfb9c AM |
3048 | else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
3049 | && h->root.type == bfd_link_hash_undefweak) | |
0a1b45a2 | 3050 | (*bed->elf_backend_hide_symbol) (eif->info, h, true); |
4deb8f71 L |
3051 | |
3052 | /* A hidden versioned symbol in executable should be forced local if | |
3053 | it is is locally defined, not referenced by shared library and not | |
3054 | exported. */ | |
3055 | else if (bfd_link_executable (eif->info) | |
3056 | && h->versioned == versioned_hidden | |
3057 | && !eif->info->export_dynamic | |
3058 | && !h->dynamic | |
3059 | && !h->ref_dynamic | |
3060 | && h->def_regular) | |
0a1b45a2 | 3061 | (*bed->elf_backend_hide_symbol) (eif->info, h, true); |
4deb8f71 | 3062 | |
45d6a902 AM |
3063 | /* If -Bsymbolic was used (which means to bind references to global |
3064 | symbols to the definition within the shared object), and this | |
3065 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
3066 | need a PLT entry. Likewise, if the symbol has non-default |
3067 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 3068 | will force it local. */ |
4deb8f71 L |
3069 | else if (h->needs_plt |
3070 | && bfd_link_pic (eif->info) | |
3071 | && is_elf_hash_table (eif->info->hash) | |
3072 | && (SYMBOLIC_BIND (eif->info, h) | |
3073 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
3074 | && h->def_regular) | |
45d6a902 | 3075 | { |
0a1b45a2 | 3076 | bool force_local; |
45d6a902 | 3077 | |
45d6a902 AM |
3078 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
3079 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
3080 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
3081 | } | |
3082 | ||
45d6a902 AM |
3083 | /* If this is a weak defined symbol in a dynamic object, and we know |
3084 | the real definition in the dynamic object, copy interesting flags | |
3085 | over to the real definition. */ | |
60d67dc8 | 3086 | if (h->is_weakalias) |
45d6a902 | 3087 | { |
60d67dc8 AM |
3088 | struct elf_link_hash_entry *def = weakdef (h); |
3089 | ||
45d6a902 AM |
3090 | /* If the real definition is defined by a regular object file, |
3091 | don't do anything special. See the longer description in | |
5b9d7a9a AM |
3092 | _bfd_elf_adjust_dynamic_symbol, below. If the def is not |
3093 | bfd_link_hash_defined as it was when put on the alias list | |
3094 | then it must have originally been a versioned symbol (for | |
3095 | which a non-versioned indirect symbol is created) and later | |
3096 | a definition for the non-versioned symbol is found. In that | |
3097 | case the indirection is flipped with the versioned symbol | |
3098 | becoming an indirect pointing at the non-versioned symbol. | |
3099 | Thus, not an alias any more. */ | |
3100 | if (def->def_regular | |
3101 | || def->root.type != bfd_link_hash_defined) | |
60d67dc8 AM |
3102 | { |
3103 | h = def; | |
3104 | while ((h = h->u.alias) != def) | |
3105 | h->is_weakalias = 0; | |
3106 | } | |
45d6a902 | 3107 | else |
a26587ba | 3108 | { |
4e6b54a6 AM |
3109 | while (h->root.type == bfd_link_hash_indirect) |
3110 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4e6b54a6 AM |
3111 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
3112 | || h->root.type == bfd_link_hash_defweak); | |
60d67dc8 | 3113 | BFD_ASSERT (def->def_dynamic); |
60d67dc8 | 3114 | (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); |
a26587ba | 3115 | } |
45d6a902 AM |
3116 | } |
3117 | ||
0a1b45a2 | 3118 | return true; |
45d6a902 AM |
3119 | } |
3120 | ||
3121 | /* Make the backend pick a good value for a dynamic symbol. This is | |
3122 | called via elf_link_hash_traverse, and also calls itself | |
3123 | recursively. */ | |
3124 | ||
0a1b45a2 | 3125 | static bool |
268b6b39 | 3126 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 3127 | { |
a50b1753 | 3128 | struct elf_info_failed *eif = (struct elf_info_failed *) data; |
559192d8 | 3129 | struct elf_link_hash_table *htab; |
9c5bfbb7 | 3130 | const struct elf_backend_data *bed; |
45d6a902 | 3131 | |
0eddce27 | 3132 | if (! is_elf_hash_table (eif->info->hash)) |
0a1b45a2 | 3133 | return false; |
45d6a902 | 3134 | |
45d6a902 AM |
3135 | /* Ignore indirect symbols. These are added by the versioning code. */ |
3136 | if (h->root.type == bfd_link_hash_indirect) | |
0a1b45a2 | 3137 | return true; |
45d6a902 AM |
3138 | |
3139 | /* Fix the symbol flags. */ | |
3140 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
0a1b45a2 | 3141 | return false; |
45d6a902 | 3142 | |
559192d8 AM |
3143 | htab = elf_hash_table (eif->info); |
3144 | bed = get_elf_backend_data (htab->dynobj); | |
3145 | ||
954b63d4 AM |
3146 | if (h->root.type == bfd_link_hash_undefweak) |
3147 | { | |
3148 | if (eif->info->dynamic_undefined_weak == 0) | |
0a1b45a2 | 3149 | (*bed->elf_backend_hide_symbol) (eif->info, h, true); |
954b63d4 AM |
3150 | else if (eif->info->dynamic_undefined_weak > 0 |
3151 | && h->ref_regular | |
3152 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
3153 | && !bfd_hide_sym_by_version (eif->info->version_info, | |
3154 | h->root.root.string)) | |
3155 | { | |
3156 | if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
3157 | { | |
0a1b45a2 AM |
3158 | eif->failed = true; |
3159 | return false; | |
954b63d4 AM |
3160 | } |
3161 | } | |
3162 | } | |
3163 | ||
45d6a902 AM |
3164 | /* If this symbol does not require a PLT entry, and it is not |
3165 | defined by a dynamic object, or is not referenced by a regular | |
3166 | object, ignore it. We do have to handle a weak defined symbol, | |
3167 | even if no regular object refers to it, if we decided to add it | |
3168 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
3169 | about symbols which are defined by one dynamic object and | |
3170 | referenced by another one? */ | |
f5385ebf | 3171 | if (!h->needs_plt |
91e21fb7 | 3172 | && h->type != STT_GNU_IFUNC |
f5385ebf AM |
3173 | && (h->def_regular |
3174 | || !h->def_dynamic | |
3175 | || (!h->ref_regular | |
60d67dc8 | 3176 | && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) |
45d6a902 | 3177 | { |
a6aa5195 | 3178 | h->plt = elf_hash_table (eif->info)->init_plt_offset; |
0a1b45a2 | 3179 | return true; |
45d6a902 AM |
3180 | } |
3181 | ||
3182 | /* If we've already adjusted this symbol, don't do it again. This | |
3183 | can happen via a recursive call. */ | |
f5385ebf | 3184 | if (h->dynamic_adjusted) |
0a1b45a2 | 3185 | return true; |
45d6a902 AM |
3186 | |
3187 | /* Don't look at this symbol again. Note that we must set this | |
3188 | after checking the above conditions, because we may look at a | |
3189 | symbol once, decide not to do anything, and then get called | |
3190 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 3191 | h->dynamic_adjusted = 1; |
45d6a902 AM |
3192 | |
3193 | /* If this is a weak definition, and we know a real definition, and | |
3194 | the real symbol is not itself defined by a regular object file, | |
3195 | then get a good value for the real definition. We handle the | |
3196 | real symbol first, for the convenience of the backend routine. | |
3197 | ||
3198 | Note that there is a confusing case here. If the real definition | |
3199 | is defined by a regular object file, we don't get the real symbol | |
3200 | from the dynamic object, but we do get the weak symbol. If the | |
3201 | processor backend uses a COPY reloc, then if some routine in the | |
3202 | dynamic object changes the real symbol, we will not see that | |
3203 | change in the corresponding weak symbol. This is the way other | |
3204 | ELF linkers work as well, and seems to be a result of the shared | |
3205 | library model. | |
3206 | ||
3207 | I will clarify this issue. Most SVR4 shared libraries define the | |
3208 | variable _timezone and define timezone as a weak synonym. The | |
3209 | tzset call changes _timezone. If you write | |
3210 | extern int timezone; | |
3211 | int _timezone = 5; | |
3212 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
3213 | you might expect that, since timezone is a synonym for _timezone, | |
3214 | the same number will print both times. However, if the processor | |
3215 | backend uses a COPY reloc, then actually timezone will be copied | |
3216 | into your process image, and, since you define _timezone | |
3217 | yourself, _timezone will not. Thus timezone and _timezone will | |
3218 | wind up at different memory locations. The tzset call will set | |
3219 | _timezone, leaving timezone unchanged. */ | |
3220 | ||
60d67dc8 | 3221 | if (h->is_weakalias) |
45d6a902 | 3222 | { |
60d67dc8 AM |
3223 | struct elf_link_hash_entry *def = weakdef (h); |
3224 | ||
ec24dc88 | 3225 | /* If we get to this point, there is an implicit reference to |
60d67dc8 AM |
3226 | the alias by a regular object file via the weak symbol H. */ |
3227 | def->ref_regular = 1; | |
45d6a902 | 3228 | |
ec24dc88 | 3229 | /* Ensure that the backend adjust_dynamic_symbol function sees |
60d67dc8 AM |
3230 | the strong alias before H by recursively calling ourselves. */ |
3231 | if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) | |
0a1b45a2 | 3232 | return false; |
45d6a902 AM |
3233 | } |
3234 | ||
3235 | /* If a symbol has no type and no size and does not require a PLT | |
3236 | entry, then we are probably about to do the wrong thing here: we | |
3237 | are probably going to create a COPY reloc for an empty object. | |
3238 | This case can arise when a shared object is built with assembly | |
3239 | code, and the assembly code fails to set the symbol type. */ | |
3240 | if (h->size == 0 | |
3241 | && h->type == STT_NOTYPE | |
f5385ebf | 3242 | && !h->needs_plt) |
4eca0228 | 3243 | _bfd_error_handler |
45d6a902 AM |
3244 | (_("warning: type and size of dynamic symbol `%s' are not defined"), |
3245 | h->root.root.string); | |
3246 | ||
45d6a902 AM |
3247 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) |
3248 | { | |
0a1b45a2 AM |
3249 | eif->failed = true; |
3250 | return false; | |
45d6a902 AM |
3251 | } |
3252 | ||
0a1b45a2 | 3253 | return true; |
45d6a902 AM |
3254 | } |
3255 | ||
027297b7 L |
3256 | /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, |
3257 | DYNBSS. */ | |
3258 | ||
0a1b45a2 | 3259 | bool |
6cabe1ea AM |
3260 | _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, |
3261 | struct elf_link_hash_entry *h, | |
027297b7 L |
3262 | asection *dynbss) |
3263 | { | |
91ac5911 | 3264 | unsigned int power_of_two; |
027297b7 L |
3265 | bfd_vma mask; |
3266 | asection *sec = h->root.u.def.section; | |
3267 | ||
de194d85 | 3268 | /* The section alignment of the definition is the maximum alignment |
91ac5911 L |
3269 | requirement of symbols defined in the section. Since we don't |
3270 | know the symbol alignment requirement, we start with the | |
3271 | maximum alignment and check low bits of the symbol address | |
3272 | for the minimum alignment. */ | |
fd361982 | 3273 | power_of_two = bfd_section_alignment (sec); |
91ac5911 L |
3274 | mask = ((bfd_vma) 1 << power_of_two) - 1; |
3275 | while ((h->root.u.def.value & mask) != 0) | |
3276 | { | |
3277 | mask >>= 1; | |
3278 | --power_of_two; | |
3279 | } | |
027297b7 | 3280 | |
fd361982 | 3281 | if (power_of_two > bfd_section_alignment (dynbss)) |
027297b7 L |
3282 | { |
3283 | /* Adjust the section alignment if needed. */ | |
fd361982 | 3284 | if (!bfd_set_section_alignment (dynbss, power_of_two)) |
0a1b45a2 | 3285 | return false; |
027297b7 L |
3286 | } |
3287 | ||
91ac5911 | 3288 | /* We make sure that the symbol will be aligned properly. */ |
027297b7 L |
3289 | dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); |
3290 | ||
3291 | /* Define the symbol as being at this point in DYNBSS. */ | |
3292 | h->root.u.def.section = dynbss; | |
3293 | h->root.u.def.value = dynbss->size; | |
3294 | ||
3295 | /* Increment the size of DYNBSS to make room for the symbol. */ | |
3296 | dynbss->size += h->size; | |
3297 | ||
f7483970 L |
3298 | /* No error if extern_protected_data is true. */ |
3299 | if (h->protected_def | |
889c2a67 L |
3300 | && (!info->extern_protected_data |
3301 | || (info->extern_protected_data < 0 | |
3302 | && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) | |
d07a1b05 | 3303 | info->callbacks->einfo |
c1c8c1ef | 3304 | (_("%P: copy reloc against protected `%pT' is dangerous\n"), |
d07a1b05 | 3305 | h->root.root.string); |
6cabe1ea | 3306 | |
0a1b45a2 | 3307 | return true; |
027297b7 L |
3308 | } |
3309 | ||
45d6a902 AM |
3310 | /* Adjust all external symbols pointing into SEC_MERGE sections |
3311 | to reflect the object merging within the sections. */ | |
3312 | ||
0a1b45a2 | 3313 | static bool |
268b6b39 | 3314 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
3315 | { |
3316 | asection *sec; | |
3317 | ||
45d6a902 AM |
3318 | if ((h->root.type == bfd_link_hash_defined |
3319 | || h->root.type == bfd_link_hash_defweak) | |
3320 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
dbaa2011 | 3321 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
45d6a902 | 3322 | { |
a50b1753 | 3323 | bfd *output_bfd = (bfd *) data; |
45d6a902 AM |
3324 | |
3325 | h->root.u.def.value = | |
3326 | _bfd_merged_section_offset (output_bfd, | |
3327 | &h->root.u.def.section, | |
3328 | elf_section_data (sec)->sec_info, | |
753731ee | 3329 | h->root.u.def.value); |
45d6a902 AM |
3330 | } |
3331 | ||
0a1b45a2 | 3332 | return true; |
45d6a902 | 3333 | } |
986a241f RH |
3334 | |
3335 | /* Returns false if the symbol referred to by H should be considered | |
3336 | to resolve local to the current module, and true if it should be | |
3337 | considered to bind dynamically. */ | |
3338 | ||
0a1b45a2 | 3339 | bool |
268b6b39 AM |
3340 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
3341 | struct bfd_link_info *info, | |
0a1b45a2 | 3342 | bool not_local_protected) |
986a241f | 3343 | { |
0a1b45a2 | 3344 | bool binding_stays_local_p; |
fcb93ecf PB |
3345 | const struct elf_backend_data *bed; |
3346 | struct elf_link_hash_table *hash_table; | |
986a241f RH |
3347 | |
3348 | if (h == NULL) | |
0a1b45a2 | 3349 | return false; |
986a241f RH |
3350 | |
3351 | while (h->root.type == bfd_link_hash_indirect | |
3352 | || h->root.type == bfd_link_hash_warning) | |
3353 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3354 | ||
3355 | /* If it was forced local, then clearly it's not dynamic. */ | |
3356 | if (h->dynindx == -1) | |
0a1b45a2 | 3357 | return false; |
f5385ebf | 3358 | if (h->forced_local) |
0a1b45a2 | 3359 | return false; |
986a241f RH |
3360 | |
3361 | /* Identify the cases where name binding rules say that a | |
3362 | visible symbol resolves locally. */ | |
0e1862bb L |
3363 | binding_stays_local_p = (bfd_link_executable (info) |
3364 | || SYMBOLIC_BIND (info, h)); | |
986a241f RH |
3365 | |
3366 | switch (ELF_ST_VISIBILITY (h->other)) | |
3367 | { | |
3368 | case STV_INTERNAL: | |
3369 | case STV_HIDDEN: | |
0a1b45a2 | 3370 | return false; |
986a241f RH |
3371 | |
3372 | case STV_PROTECTED: | |
fcb93ecf | 3373 | hash_table = elf_hash_table (info); |
2cc15b10 | 3374 | if (!is_elf_hash_table (&hash_table->root)) |
0a1b45a2 | 3375 | return false; |
fcb93ecf PB |
3376 | |
3377 | bed = get_elf_backend_data (hash_table->dynobj); | |
3378 | ||
986a241f RH |
3379 | /* Proper resolution for function pointer equality may require |
3380 | that these symbols perhaps be resolved dynamically, even though | |
3381 | we should be resolving them to the current module. */ | |
89a2ee5a | 3382 | if (!not_local_protected || !bed->is_function_type (h->type)) |
0a1b45a2 | 3383 | binding_stays_local_p = true; |
986a241f RH |
3384 | break; |
3385 | ||
3386 | default: | |
986a241f RH |
3387 | break; |
3388 | } | |
3389 | ||
aa37626c | 3390 | /* If it isn't defined locally, then clearly it's dynamic. */ |
89a2ee5a | 3391 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
0a1b45a2 | 3392 | return true; |
aa37626c | 3393 | |
986a241f RH |
3394 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
3395 | us that it remains local. */ | |
3396 | return !binding_stays_local_p; | |
3397 | } | |
f6c52c13 AM |
3398 | |
3399 | /* Return true if the symbol referred to by H should be considered | |
3400 | to resolve local to the current module, and false otherwise. Differs | |
3401 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2e76e85a | 3402 | undefined symbols. The two functions are virtually identical except |
0fad2956 MR |
3403 | for the place where dynindx == -1 is tested. If that test is true, |
3404 | _bfd_elf_dynamic_symbol_p will say the symbol is local, while | |
3405 | _bfd_elf_symbol_refs_local_p will say the symbol is local only for | |
3406 | defined symbols. | |
89a2ee5a AM |
3407 | It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as |
3408 | !_bfd_elf_symbol_refs_local_p, except that targets differ in their | |
3409 | treatment of undefined weak symbols. For those that do not make | |
3410 | undefined weak symbols dynamic, both functions may return false. */ | |
f6c52c13 | 3411 | |
0a1b45a2 | 3412 | bool |
268b6b39 AM |
3413 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
3414 | struct bfd_link_info *info, | |
0a1b45a2 | 3415 | bool local_protected) |
f6c52c13 | 3416 | { |
fcb93ecf PB |
3417 | const struct elf_backend_data *bed; |
3418 | struct elf_link_hash_table *hash_table; | |
3419 | ||
f6c52c13 AM |
3420 | /* If it's a local sym, of course we resolve locally. */ |
3421 | if (h == NULL) | |
0a1b45a2 | 3422 | return true; |
f6c52c13 | 3423 | |
d95edcac L |
3424 | /* STV_HIDDEN or STV_INTERNAL ones must be local. */ |
3425 | if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
3426 | || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
0a1b45a2 | 3427 | return true; |
d95edcac | 3428 | |
0fad2956 MR |
3429 | /* Forced local symbols resolve locally. */ |
3430 | if (h->forced_local) | |
0a1b45a2 | 3431 | return true; |
0fad2956 | 3432 | |
7e2294f9 AO |
3433 | /* Common symbols that become definitions don't get the DEF_REGULAR |
3434 | flag set, so test it first, and don't bail out. */ | |
3435 | if (ELF_COMMON_DEF_P (h)) | |
3436 | /* Do nothing. */; | |
f6c52c13 | 3437 | /* If we don't have a definition in a regular file, then we can't |
49ff44d6 L |
3438 | resolve locally. The sym is either undefined or dynamic. */ |
3439 | else if (!h->def_regular) | |
0a1b45a2 | 3440 | return false; |
f6c52c13 | 3441 | |
0fad2956 | 3442 | /* Non-dynamic symbols resolve locally. */ |
f6c52c13 | 3443 | if (h->dynindx == -1) |
0a1b45a2 | 3444 | return true; |
f6c52c13 AM |
3445 | |
3446 | /* At this point, we know the symbol is defined and dynamic. In an | |
3447 | executable it must resolve locally, likewise when building symbolic | |
3448 | shared libraries. */ | |
0e1862bb | 3449 | if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) |
0a1b45a2 | 3450 | return true; |
f6c52c13 AM |
3451 | |
3452 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
3453 | with default visibility might not resolve locally. */ | |
3454 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
0a1b45a2 | 3455 | return false; |
f6c52c13 | 3456 | |
fcb93ecf | 3457 | hash_table = elf_hash_table (info); |
2cc15b10 | 3458 | if (!is_elf_hash_table (&hash_table->root)) |
0a1b45a2 | 3459 | return true; |
fcb93ecf | 3460 | |
6f365fda L |
3461 | /* STV_PROTECTED symbols with indirect external access are local. */ |
3462 | if (info->indirect_extern_access > 0) | |
3463 | return true; | |
3464 | ||
fcb93ecf PB |
3465 | bed = get_elf_backend_data (hash_table->dynobj); |
3466 | ||
f7483970 L |
3467 | /* If extern_protected_data is false, STV_PROTECTED non-function |
3468 | symbols are local. */ | |
889c2a67 L |
3469 | if ((!info->extern_protected_data |
3470 | || (info->extern_protected_data < 0 | |
3471 | && !bed->extern_protected_data)) | |
3472 | && !bed->is_function_type (h->type)) | |
0a1b45a2 | 3473 | return true; |
1c16dfa5 | 3474 | |
f6c52c13 | 3475 | /* Function pointer equality tests may require that STV_PROTECTED |
2676a7d9 AM |
3476 | symbols be treated as dynamic symbols. If the address of a |
3477 | function not defined in an executable is set to that function's | |
3478 | plt entry in the executable, then the address of the function in | |
3479 | a shared library must also be the plt entry in the executable. */ | |
f6c52c13 AM |
3480 | return local_protected; |
3481 | } | |
e1918d23 AM |
3482 | |
3483 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
3484 | aligned. Returns the first TLS output section. */ | |
3485 | ||
3486 | struct bfd_section * | |
3487 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
3488 | { | |
3489 | struct bfd_section *sec, *tls; | |
3490 | unsigned int align = 0; | |
3491 | ||
3492 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
3493 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
3494 | break; | |
3495 | tls = sec; | |
3496 | ||
3497 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
3498 | if (sec->alignment_power > align) | |
3499 | align = sec->alignment_power; | |
3500 | ||
3501 | elf_hash_table (info)->tls_sec = tls; | |
3502 | ||
fdde2fb6 SH |
3503 | /* Ensure the alignment of the first section (usually .tdata) is the largest |
3504 | alignment, so that the tls segment starts aligned. */ | |
e1918d23 AM |
3505 | if (tls != NULL) |
3506 | tls->alignment_power = align; | |
3507 | ||
3508 | return tls; | |
3509 | } | |
0ad989f9 L |
3510 | |
3511 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
0a1b45a2 | 3512 | static bool |
0ad989f9 L |
3513 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, |
3514 | Elf_Internal_Sym *sym) | |
3515 | { | |
a4d8e49b L |
3516 | const struct elf_backend_data *bed; |
3517 | ||
0ad989f9 L |
3518 | /* Local symbols do not count, but target specific ones might. */ |
3519 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
3520 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
0a1b45a2 | 3521 | return false; |
0ad989f9 | 3522 | |
fcb93ecf | 3523 | bed = get_elf_backend_data (abfd); |
0ad989f9 | 3524 | /* Function symbols do not count. */ |
fcb93ecf | 3525 | if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) |
0a1b45a2 | 3526 | return false; |
0ad989f9 L |
3527 | |
3528 | /* If the section is undefined, then so is the symbol. */ | |
3529 | if (sym->st_shndx == SHN_UNDEF) | |
0a1b45a2 | 3530 | return false; |
0ad989f9 L |
3531 | |
3532 | /* If the symbol is defined in the common section, then | |
3533 | it is a common definition and so does not count. */ | |
a4d8e49b | 3534 | if (bed->common_definition (sym)) |
0a1b45a2 | 3535 | return false; |
0ad989f9 L |
3536 | |
3537 | /* If the symbol is in a target specific section then we | |
3538 | must rely upon the backend to tell us what it is. */ | |
3539 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
3540 | /* FIXME - this function is not coded yet: | |
3541 | ||
3542 | return _bfd_is_global_symbol_definition (abfd, sym); | |
3543 | ||
3544 | Instead for now assume that the definition is not global, | |
3545 | Even if this is wrong, at least the linker will behave | |
3546 | in the same way that it used to do. */ | |
0a1b45a2 | 3547 | return false; |
0ad989f9 | 3548 | |
0a1b45a2 | 3549 | return true; |
0ad989f9 L |
3550 | } |
3551 | ||
3552 | /* Search the symbol table of the archive element of the archive ABFD | |
3553 | whose archive map contains a mention of SYMDEF, and determine if | |
3554 | the symbol is defined in this element. */ | |
0a1b45a2 | 3555 | static bool |
0ad989f9 L |
3556 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) |
3557 | { | |
3558 | Elf_Internal_Shdr * hdr; | |
ef53be89 AM |
3559 | size_t symcount; |
3560 | size_t extsymcount; | |
3561 | size_t extsymoff; | |
0ad989f9 L |
3562 | Elf_Internal_Sym *isymbuf; |
3563 | Elf_Internal_Sym *isym; | |
3564 | Elf_Internal_Sym *isymend; | |
0a1b45a2 | 3565 | bool result; |
0ad989f9 | 3566 | |
6395a102 | 3567 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL); |
0ad989f9 | 3568 | if (abfd == NULL) |
0a1b45a2 | 3569 | return false; |
0ad989f9 L |
3570 | |
3571 | if (! bfd_check_format (abfd, bfd_object)) | |
0a1b45a2 | 3572 | return false; |
0ad989f9 | 3573 | |
7dc3990e L |
3574 | /* Select the appropriate symbol table. If we don't know if the |
3575 | object file is an IR object, give linker LTO plugin a chance to | |
3576 | get the correct symbol table. */ | |
3577 | if (abfd->plugin_format == bfd_plugin_yes | |
08ce1d72 | 3578 | #if BFD_SUPPORTS_PLUGINS |
7dc3990e L |
3579 | || (abfd->plugin_format == bfd_plugin_unknown |
3580 | && bfd_link_plugin_object_p (abfd)) | |
3581 | #endif | |
3582 | ) | |
3583 | { | |
3584 | /* Use the IR symbol table if the object has been claimed by | |
3585 | plugin. */ | |
3586 | abfd = abfd->plugin_dummy_bfd; | |
3587 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3588 | } | |
0ad989f9 | 3589 | else |
bac5753c L |
3590 | { |
3591 | if (elf_use_dt_symtab_p (abfd)) | |
3592 | { | |
3593 | bfd_set_error (bfd_error_wrong_format); | |
3594 | return false; | |
3595 | } | |
3596 | ||
3597 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
3598 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3599 | else | |
3600 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3601 | } | |
0ad989f9 L |
3602 | |
3603 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3604 | ||
3605 | /* The sh_info field of the symtab header tells us where the | |
3606 | external symbols start. We don't care about the local symbols. */ | |
3607 | if (elf_bad_symtab (abfd)) | |
3608 | { | |
3609 | extsymcount = symcount; | |
3610 | extsymoff = 0; | |
3611 | } | |
3612 | else | |
3613 | { | |
3614 | extsymcount = symcount - hdr->sh_info; | |
3615 | extsymoff = hdr->sh_info; | |
3616 | } | |
3617 | ||
3618 | if (extsymcount == 0) | |
0a1b45a2 | 3619 | return false; |
0ad989f9 L |
3620 | |
3621 | /* Read in the symbol table. */ | |
3622 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3623 | NULL, NULL, NULL); | |
3624 | if (isymbuf == NULL) | |
0a1b45a2 | 3625 | return false; |
0ad989f9 L |
3626 | |
3627 | /* Scan the symbol table looking for SYMDEF. */ | |
0a1b45a2 | 3628 | result = false; |
0ad989f9 L |
3629 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) |
3630 | { | |
3631 | const char *name; | |
3632 | ||
3633 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3634 | isym->st_name); | |
3635 | if (name == NULL) | |
3636 | break; | |
3637 | ||
3638 | if (strcmp (name, symdef->name) == 0) | |
3639 | { | |
3640 | result = is_global_data_symbol_definition (abfd, isym); | |
3641 | break; | |
3642 | } | |
3643 | } | |
3644 | ||
3645 | free (isymbuf); | |
3646 | ||
3647 | return result; | |
3648 | } | |
3649 | \f | |
5a580b3a AM |
3650 | /* Add an entry to the .dynamic table. */ |
3651 | ||
0a1b45a2 | 3652 | bool |
5a580b3a AM |
3653 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, |
3654 | bfd_vma tag, | |
3655 | bfd_vma val) | |
3656 | { | |
3657 | struct elf_link_hash_table *hash_table; | |
3658 | const struct elf_backend_data *bed; | |
3659 | asection *s; | |
3660 | bfd_size_type newsize; | |
3661 | bfd_byte *newcontents; | |
3662 | Elf_Internal_Dyn dyn; | |
3663 | ||
3664 | hash_table = elf_hash_table (info); | |
2cc15b10 | 3665 | if (! is_elf_hash_table (&hash_table->root)) |
0a1b45a2 | 3666 | return false; |
5a580b3a | 3667 | |
7f923b7f | 3668 | if (tag == DT_RELA || tag == DT_REL) |
0a1b45a2 | 3669 | hash_table->dynamic_relocs = true; |
7f923b7f | 3670 | |
5a580b3a | 3671 | bed = get_elf_backend_data (hash_table->dynobj); |
3d4d4302 | 3672 | s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
5a580b3a AM |
3673 | BFD_ASSERT (s != NULL); |
3674 | ||
eea6121a | 3675 | newsize = s->size + bed->s->sizeof_dyn; |
a50b1753 | 3676 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
5a580b3a | 3677 | if (newcontents == NULL) |
0a1b45a2 | 3678 | return false; |
5a580b3a AM |
3679 | |
3680 | dyn.d_tag = tag; | |
3681 | dyn.d_un.d_val = val; | |
eea6121a | 3682 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 3683 | |
eea6121a | 3684 | s->size = newsize; |
5a580b3a AM |
3685 | s->contents = newcontents; |
3686 | ||
0a1b45a2 | 3687 | return true; |
5a580b3a AM |
3688 | } |
3689 | ||
6f6fd151 L |
3690 | /* Strip zero-sized dynamic sections. */ |
3691 | ||
0a1b45a2 | 3692 | bool |
6f6fd151 L |
3693 | _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info) |
3694 | { | |
3695 | struct elf_link_hash_table *hash_table; | |
3696 | const struct elf_backend_data *bed; | |
3697 | asection *s, *sdynamic, **pp; | |
3698 | asection *rela_dyn, *rel_dyn; | |
3699 | Elf_Internal_Dyn dyn; | |
3700 | bfd_byte *extdyn, *next; | |
3701 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
0a1b45a2 AM |
3702 | bool strip_zero_sized; |
3703 | bool strip_zero_sized_plt; | |
6f6fd151 L |
3704 | |
3705 | if (bfd_link_relocatable (info)) | |
0a1b45a2 | 3706 | return true; |
6f6fd151 L |
3707 | |
3708 | hash_table = elf_hash_table (info); | |
2cc15b10 | 3709 | if (!is_elf_hash_table (&hash_table->root)) |
0a1b45a2 | 3710 | return false; |
6f6fd151 L |
3711 | |
3712 | if (!hash_table->dynobj) | |
0a1b45a2 | 3713 | return true; |
6f6fd151 L |
3714 | |
3715 | sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic"); | |
3716 | if (!sdynamic) | |
0a1b45a2 | 3717 | return true; |
6f6fd151 L |
3718 | |
3719 | bed = get_elf_backend_data (hash_table->dynobj); | |
3720 | swap_dyn_in = bed->s->swap_dyn_in; | |
3721 | ||
0a1b45a2 AM |
3722 | strip_zero_sized = false; |
3723 | strip_zero_sized_plt = false; | |
6f6fd151 L |
3724 | |
3725 | /* Strip zero-sized dynamic sections. */ | |
3726 | rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn"); | |
3727 | rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn"); | |
3728 | for (pp = &info->output_bfd->sections; (s = *pp) != NULL;) | |
3729 | if (s->size == 0 | |
3730 | && (s == rela_dyn | |
3731 | || s == rel_dyn | |
3732 | || s == hash_table->srelplt->output_section | |
3733 | || s == hash_table->splt->output_section)) | |
3734 | { | |
3735 | *pp = s->next; | |
3736 | info->output_bfd->section_count--; | |
0a1b45a2 | 3737 | strip_zero_sized = true; |
6f6fd151 L |
3738 | if (s == rela_dyn) |
3739 | s = rela_dyn; | |
3740 | if (s == rel_dyn) | |
3741 | s = rel_dyn; | |
3742 | else if (s == hash_table->splt->output_section) | |
3743 | { | |
3744 | s = hash_table->splt; | |
0a1b45a2 | 3745 | strip_zero_sized_plt = true; |
6f6fd151 L |
3746 | } |
3747 | else | |
3748 | s = hash_table->srelplt; | |
3749 | s->flags |= SEC_EXCLUDE; | |
3750 | s->output_section = bfd_abs_section_ptr; | |
3751 | } | |
3752 | else | |
3753 | pp = &s->next; | |
3754 | ||
36f61bf2 | 3755 | if (strip_zero_sized_plt && sdynamic->size != 0) |
6f6fd151 L |
3756 | for (extdyn = sdynamic->contents; |
3757 | extdyn < sdynamic->contents + sdynamic->size; | |
3758 | extdyn = next) | |
3759 | { | |
3760 | next = extdyn + bed->s->sizeof_dyn; | |
3761 | swap_dyn_in (hash_table->dynobj, extdyn, &dyn); | |
3762 | switch (dyn.d_tag) | |
3763 | { | |
3764 | default: | |
3765 | break; | |
3766 | case DT_JMPREL: | |
3767 | case DT_PLTRELSZ: | |
3768 | case DT_PLTREL: | |
3769 | /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if | |
3770 | the procedure linkage table (the .plt section) has been | |
3771 | removed. */ | |
3772 | memmove (extdyn, next, | |
3773 | sdynamic->size - (next - sdynamic->contents)); | |
3774 | next = extdyn; | |
3775 | } | |
3776 | } | |
3777 | ||
3778 | if (strip_zero_sized) | |
3779 | { | |
3780 | /* Regenerate program headers. */ | |
3781 | elf_seg_map (info->output_bfd) = NULL; | |
e2cbf4df L |
3782 | return _bfd_elf_map_sections_to_segments (info->output_bfd, info, |
3783 | NULL); | |
6f6fd151 L |
3784 | } |
3785 | ||
0a1b45a2 | 3786 | return true; |
6f6fd151 L |
3787 | } |
3788 | ||
e310298c | 3789 | /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error, |
5a580b3a AM |
3790 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ |
3791 | ||
e310298c AM |
3792 | int |
3793 | bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info) | |
5a580b3a AM |
3794 | { |
3795 | struct elf_link_hash_table *hash_table; | |
ef53be89 | 3796 | size_t strindex; |
e310298c | 3797 | const char *soname; |
5a580b3a | 3798 | |
7e9f0867 AM |
3799 | if (!_bfd_elf_link_create_dynstrtab (abfd, info)) |
3800 | return -1; | |
3801 | ||
5a580b3a | 3802 | hash_table = elf_hash_table (info); |
e310298c | 3803 | soname = elf_dt_name (abfd); |
0a1b45a2 | 3804 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false); |
ef53be89 | 3805 | if (strindex == (size_t) -1) |
5a580b3a AM |
3806 | return -1; |
3807 | ||
02be4619 | 3808 | if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) |
5a580b3a AM |
3809 | { |
3810 | asection *sdyn; | |
3811 | const struct elf_backend_data *bed; | |
3812 | bfd_byte *extdyn; | |
3813 | ||
3814 | bed = get_elf_backend_data (hash_table->dynobj); | |
3d4d4302 | 3815 | sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); |
36f61bf2 | 3816 | if (sdyn != NULL && sdyn->size != 0) |
7e9f0867 AM |
3817 | for (extdyn = sdyn->contents; |
3818 | extdyn < sdyn->contents + sdyn->size; | |
3819 | extdyn += bed->s->sizeof_dyn) | |
3820 | { | |
3821 | Elf_Internal_Dyn dyn; | |
5a580b3a | 3822 | |
7e9f0867 AM |
3823 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); |
3824 | if (dyn.d_tag == DT_NEEDED | |
3825 | && dyn.d_un.d_val == strindex) | |
3826 | { | |
3827 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
3828 | return 1; | |
3829 | } | |
3830 | } | |
5a580b3a AM |
3831 | } |
3832 | ||
e310298c AM |
3833 | if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) |
3834 | return -1; | |
7e9f0867 | 3835 | |
e310298c AM |
3836 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) |
3837 | return -1; | |
5a580b3a AM |
3838 | |
3839 | return 0; | |
3840 | } | |
3841 | ||
7b15fa7a AM |
3842 | /* Return true if SONAME is on the needed list between NEEDED and STOP |
3843 | (or the end of list if STOP is NULL), and needed by a library that | |
3844 | will be loaded. */ | |
3845 | ||
0a1b45a2 | 3846 | static bool |
7b15fa7a AM |
3847 | on_needed_list (const char *soname, |
3848 | struct bfd_link_needed_list *needed, | |
3849 | struct bfd_link_needed_list *stop) | |
010e5ae2 | 3850 | { |
7b15fa7a AM |
3851 | struct bfd_link_needed_list *look; |
3852 | for (look = needed; look != stop; look = look->next) | |
3853 | if (strcmp (soname, look->name) == 0 | |
3854 | && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 | |
3855 | /* If needed by a library that itself is not directly | |
3856 | needed, recursively check whether that library is | |
3857 | indirectly needed. Since we add DT_NEEDED entries to | |
3858 | the end of the list, library dependencies appear after | |
3859 | the library. Therefore search prior to the current | |
3860 | LOOK, preventing possible infinite recursion. */ | |
3861 | || on_needed_list (elf_dt_name (look->by), needed, look))) | |
0a1b45a2 | 3862 | return true; |
010e5ae2 | 3863 | |
0a1b45a2 | 3864 | return false; |
010e5ae2 AM |
3865 | } |
3866 | ||
3a3f4bf7 | 3867 | /* Sort symbol by value, section, size, and type. */ |
4ad4eba5 AM |
3868 | static int |
3869 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
3870 | { |
3871 | const struct elf_link_hash_entry *h1; | |
3872 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 3873 | bfd_signed_vma vdiff; |
3a3f4bf7 AM |
3874 | int sdiff; |
3875 | const char *n1; | |
3876 | const char *n2; | |
5a580b3a AM |
3877 | |
3878 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
3879 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
3880 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
3881 | if (vdiff != 0) | |
3882 | return vdiff > 0 ? 1 : -1; | |
3a3f4bf7 AM |
3883 | |
3884 | sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
3885 | if (sdiff != 0) | |
3886 | return sdiff; | |
3887 | ||
3888 | /* Sort so that sized symbols are selected over zero size symbols. */ | |
3889 | vdiff = h1->size - h2->size; | |
3890 | if (vdiff != 0) | |
3891 | return vdiff > 0 ? 1 : -1; | |
3892 | ||
3893 | /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */ | |
3894 | if (h1->type != h2->type) | |
3895 | return h1->type - h2->type; | |
3896 | ||
3897 | /* If symbols are properly sized and typed, and multiple strong | |
3898 | aliases are not defined in a shared library by the user we | |
3899 | shouldn't get here. Unfortunately linker script symbols like | |
3900 | __bss_start sometimes match a user symbol defined at the start of | |
3901 | .bss without proper size and type. We'd like to preference the | |
3902 | user symbol over reserved system symbols. Sort on leading | |
3903 | underscores. */ | |
3904 | n1 = h1->root.root.string; | |
3905 | n2 = h2->root.root.string; | |
3906 | while (*n1 == *n2) | |
10b7e05b | 3907 | { |
3a3f4bf7 AM |
3908 | if (*n1 == 0) |
3909 | break; | |
3910 | ++n1; | |
3911 | ++n2; | |
10b7e05b | 3912 | } |
3a3f4bf7 AM |
3913 | if (*n1 == '_') |
3914 | return -1; | |
3915 | if (*n2 == '_') | |
3916 | return 1; | |
3917 | ||
3918 | /* Final sort on name selects user symbols like '_u' over reserved | |
3919 | system symbols like '_Z' and also will avoid qsort instability. */ | |
3920 | return *n1 - *n2; | |
5a580b3a | 3921 | } |
4ad4eba5 | 3922 | |
5a580b3a AM |
3923 | /* This function is used to adjust offsets into .dynstr for |
3924 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
3925 | ||
0a1b45a2 | 3926 | static bool |
5a580b3a AM |
3927 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) |
3928 | { | |
a50b1753 | 3929 | struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; |
5a580b3a | 3930 | |
5a580b3a AM |
3931 | if (h->dynindx != -1) |
3932 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
0a1b45a2 | 3933 | return true; |
5a580b3a AM |
3934 | } |
3935 | ||
3936 | /* Assign string offsets in .dynstr, update all structures referencing | |
3937 | them. */ | |
3938 | ||
0a1b45a2 | 3939 | static bool |
4ad4eba5 | 3940 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) |
5a580b3a AM |
3941 | { |
3942 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
3943 | struct elf_link_local_dynamic_entry *entry; | |
3944 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
3945 | bfd *dynobj = hash_table->dynobj; | |
3946 | asection *sdyn; | |
3947 | bfd_size_type size; | |
3948 | const struct elf_backend_data *bed; | |
3949 | bfd_byte *extdyn; | |
3950 | ||
3951 | _bfd_elf_strtab_finalize (dynstr); | |
3952 | size = _bfd_elf_strtab_size (dynstr); | |
3953 | ||
3d16b64e NA |
3954 | /* Allow the linker to examine the dynsymtab now it's fully populated. */ |
3955 | ||
3956 | if (info->callbacks->examine_strtab) | |
3957 | info->callbacks->examine_strtab (dynstr); | |
3958 | ||
5a580b3a | 3959 | bed = get_elf_backend_data (dynobj); |
3d4d4302 | 3960 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
5a580b3a AM |
3961 | BFD_ASSERT (sdyn != NULL); |
3962 | ||
3963 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
3964 | for (extdyn = sdyn->contents; | |
36f61bf2 | 3965 | extdyn < PTR_ADD (sdyn->contents, sdyn->size); |
5a580b3a AM |
3966 | extdyn += bed->s->sizeof_dyn) |
3967 | { | |
3968 | Elf_Internal_Dyn dyn; | |
3969 | ||
3970 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
3971 | switch (dyn.d_tag) | |
3972 | { | |
3973 | case DT_STRSZ: | |
3974 | dyn.d_un.d_val = size; | |
3975 | break; | |
3976 | case DT_NEEDED: | |
3977 | case DT_SONAME: | |
3978 | case DT_RPATH: | |
3979 | case DT_RUNPATH: | |
3980 | case DT_FILTER: | |
3981 | case DT_AUXILIARY: | |
7ee314fa AM |
3982 | case DT_AUDIT: |
3983 | case DT_DEPAUDIT: | |
5a580b3a AM |
3984 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); |
3985 | break; | |
3986 | default: | |
3987 | continue; | |
3988 | } | |
3989 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
3990 | } | |
3991 | ||
3992 | /* Now update local dynamic symbols. */ | |
3993 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
3994 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
3995 | entry->isym.st_name); | |
3996 | ||
3997 | /* And the rest of dynamic symbols. */ | |
3998 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
3999 | ||
4000 | /* Adjust version definitions. */ | |
4001 | if (elf_tdata (output_bfd)->cverdefs) | |
4002 | { | |
4003 | asection *s; | |
4004 | bfd_byte *p; | |
ef53be89 | 4005 | size_t i; |
5a580b3a AM |
4006 | Elf_Internal_Verdef def; |
4007 | Elf_Internal_Verdaux defaux; | |
4008 | ||
3d4d4302 | 4009 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); |
5a580b3a AM |
4010 | p = s->contents; |
4011 | do | |
4012 | { | |
4013 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
4014 | &def); | |
4015 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
4016 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
4017 | continue; | |
5a580b3a AM |
4018 | for (i = 0; i < def.vd_cnt; ++i) |
4019 | { | |
4020 | _bfd_elf_swap_verdaux_in (output_bfd, | |
4021 | (Elf_External_Verdaux *) p, &defaux); | |
4022 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
4023 | defaux.vda_name); | |
4024 | _bfd_elf_swap_verdaux_out (output_bfd, | |
4025 | &defaux, (Elf_External_Verdaux *) p); | |
4026 | p += sizeof (Elf_External_Verdaux); | |
4027 | } | |
4028 | } | |
4029 | while (def.vd_next); | |
4030 | } | |
4031 | ||
4032 | /* Adjust version references. */ | |
4033 | if (elf_tdata (output_bfd)->verref) | |
4034 | { | |
4035 | asection *s; | |
4036 | bfd_byte *p; | |
ef53be89 | 4037 | size_t i; |
5a580b3a AM |
4038 | Elf_Internal_Verneed need; |
4039 | Elf_Internal_Vernaux needaux; | |
4040 | ||
3d4d4302 | 4041 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); |
5a580b3a AM |
4042 | p = s->contents; |
4043 | do | |
4044 | { | |
4045 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
4046 | &need); | |
4047 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
4048 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
4049 | (Elf_External_Verneed *) p); | |
4050 | p += sizeof (Elf_External_Verneed); | |
4051 | for (i = 0; i < need.vn_cnt; ++i) | |
4052 | { | |
4053 | _bfd_elf_swap_vernaux_in (output_bfd, | |
4054 | (Elf_External_Vernaux *) p, &needaux); | |
4055 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
4056 | needaux.vna_name); | |
4057 | _bfd_elf_swap_vernaux_out (output_bfd, | |
4058 | &needaux, | |
4059 | (Elf_External_Vernaux *) p); | |
4060 | p += sizeof (Elf_External_Vernaux); | |
4061 | } | |
4062 | } | |
4063 | while (need.vn_next); | |
4064 | } | |
4065 | ||
0a1b45a2 | 4066 | return true; |
5a580b3a AM |
4067 | } |
4068 | \f | |
13285a1b AM |
4069 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. |
4070 | The default is to only match when the INPUT and OUTPUT are exactly | |
4071 | the same target. */ | |
4072 | ||
0a1b45a2 | 4073 | bool |
13285a1b AM |
4074 | _bfd_elf_default_relocs_compatible (const bfd_target *input, |
4075 | const bfd_target *output) | |
4076 | { | |
4077 | return input == output; | |
4078 | } | |
4079 | ||
4080 | /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. | |
4081 | This version is used when different targets for the same architecture | |
4082 | are virtually identical. */ | |
4083 | ||
0a1b45a2 | 4084 | bool |
13285a1b AM |
4085 | _bfd_elf_relocs_compatible (const bfd_target *input, |
4086 | const bfd_target *output) | |
4087 | { | |
4088 | const struct elf_backend_data *obed, *ibed; | |
4089 | ||
4090 | if (input == output) | |
0a1b45a2 | 4091 | return true; |
13285a1b AM |
4092 | |
4093 | ibed = xvec_get_elf_backend_data (input); | |
4094 | obed = xvec_get_elf_backend_data (output); | |
4095 | ||
4096 | if (ibed->arch != obed->arch) | |
0a1b45a2 | 4097 | return false; |
13285a1b AM |
4098 | |
4099 | /* If both backends are using this function, deem them compatible. */ | |
4100 | return ibed->relocs_compatible == obed->relocs_compatible; | |
4101 | } | |
4102 | ||
e5034e59 AM |
4103 | /* Make a special call to the linker "notice" function to tell it that |
4104 | we are about to handle an as-needed lib, or have finished | |
1b786873 | 4105 | processing the lib. */ |
e5034e59 | 4106 | |
0a1b45a2 | 4107 | bool |
e5034e59 AM |
4108 | _bfd_elf_notice_as_needed (bfd *ibfd, |
4109 | struct bfd_link_info *info, | |
4110 | enum notice_asneeded_action act) | |
4111 | { | |
46135103 | 4112 | return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); |
e5034e59 AM |
4113 | } |
4114 | ||
3747999c | 4115 | /* Call ACTION on each relocation in an ELF object file. */ |
d9689752 | 4116 | |
0a1b45a2 | 4117 | bool |
3747999c L |
4118 | _bfd_elf_link_iterate_on_relocs |
4119 | (bfd *abfd, struct bfd_link_info *info, | |
4120 | bool (*action) (bfd *, struct bfd_link_info *, asection *, | |
4121 | const Elf_Internal_Rela *)) | |
d9689752 L |
4122 | { |
4123 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4124 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
4125 | ||
4126 | /* If this object is the same format as the output object, and it is | |
4127 | not a shared library, then let the backend look through the | |
4128 | relocs. | |
4129 | ||
4130 | This is required to build global offset table entries and to | |
4131 | arrange for dynamic relocs. It is not required for the | |
4132 | particular common case of linking non PIC code, even when linking | |
4133 | against shared libraries, but unfortunately there is no way of | |
4134 | knowing whether an object file has been compiled PIC or not. | |
4135 | Looking through the relocs is not particularly time consuming. | |
4136 | The problem is that we must either (1) keep the relocs in memory, | |
4137 | which causes the linker to require additional runtime memory or | |
4138 | (2) read the relocs twice from the input file, which wastes time. | |
4139 | This would be a good case for using mmap. | |
4140 | ||
4141 | I have no idea how to handle linking PIC code into a file of a | |
4142 | different format. It probably can't be done. */ | |
4143 | if ((abfd->flags & DYNAMIC) == 0 | |
2cc15b10 | 4144 | && is_elf_hash_table (&htab->root) |
d9689752 L |
4145 | && elf_object_id (abfd) == elf_hash_table_id (htab) |
4146 | && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) | |
4147 | { | |
4148 | asection *o; | |
4149 | ||
4150 | for (o = abfd->sections; o != NULL; o = o->next) | |
4151 | { | |
4152 | Elf_Internal_Rela *internal_relocs; | |
0a1b45a2 | 4153 | bool ok; |
d9689752 | 4154 | |
c4b126b8 L |
4155 | /* Don't check relocations in excluded sections. Don't do |
4156 | anything special with non-loaded, non-alloced sections. | |
4157 | In particular, any relocs in such sections should not | |
4158 | affect GOT and PLT reference counting (ie. we don't | |
4159 | allow them to create GOT or PLT entries), there's no | |
4160 | possibility or desire to optimize TLS relocs, and | |
4161 | there's not much point in propagating relocs to shared | |
4162 | libs that the dynamic linker won't relocate. */ | |
4163 | if ((o->flags & SEC_ALLOC) == 0 | |
4164 | || (o->flags & SEC_RELOC) == 0 | |
5ce03cea | 4165 | || (o->flags & SEC_EXCLUDE) != 0 |
d9689752 L |
4166 | || o->reloc_count == 0 |
4167 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4168 | && (o->flags & SEC_DEBUGGING) != 0) | |
4169 | || bfd_is_abs_section (o->output_section)) | |
4170 | continue; | |
4171 | ||
a8dde0a2 L |
4172 | internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info, |
4173 | o, NULL, | |
4174 | NULL, | |
4175 | _bfd_link_keep_memory (info)); | |
d9689752 | 4176 | if (internal_relocs == NULL) |
0a1b45a2 | 4177 | return false; |
d9689752 | 4178 | |
3747999c | 4179 | ok = action (abfd, info, o, internal_relocs); |
d9689752 L |
4180 | |
4181 | if (elf_section_data (o)->relocs != internal_relocs) | |
4182 | free (internal_relocs); | |
4183 | ||
4184 | if (! ok) | |
0a1b45a2 | 4185 | return false; |
d9689752 L |
4186 | } |
4187 | } | |
4188 | ||
0a1b45a2 | 4189 | return true; |
d9689752 L |
4190 | } |
4191 | ||
3747999c L |
4192 | /* Check relocations in an ELF object file. This is called after |
4193 | all input files have been opened. */ | |
4194 | ||
4195 | bool | |
4196 | _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) | |
4197 | { | |
4198 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4199 | if (bed->check_relocs != NULL) | |
4200 | return _bfd_elf_link_iterate_on_relocs (abfd, info, | |
4201 | bed->check_relocs); | |
4202 | return true; | |
4203 | } | |
4204 | ||
4ad4eba5 AM |
4205 | /* Add symbols from an ELF object file to the linker hash table. */ |
4206 | ||
0a1b45a2 | 4207 | static bool |
4ad4eba5 AM |
4208 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) |
4209 | { | |
a0c402a5 | 4210 | Elf_Internal_Ehdr *ehdr; |
4ad4eba5 | 4211 | Elf_Internal_Shdr *hdr; |
ef53be89 AM |
4212 | size_t symcount; |
4213 | size_t extsymcount; | |
4214 | size_t extsymoff; | |
4ad4eba5 | 4215 | struct elf_link_hash_entry **sym_hash; |
0a1b45a2 | 4216 | bool dynamic; |
4ad4eba5 | 4217 | Elf_External_Versym *extversym = NULL; |
be22c732 | 4218 | Elf_External_Versym *extversym_end = NULL; |
4ad4eba5 AM |
4219 | Elf_External_Versym *ever; |
4220 | struct elf_link_hash_entry *weaks; | |
4221 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
ef53be89 | 4222 | size_t nondeflt_vers_cnt = 0; |
4ad4eba5 AM |
4223 | Elf_Internal_Sym *isymbuf = NULL; |
4224 | Elf_Internal_Sym *isym; | |
4225 | Elf_Internal_Sym *isymend; | |
4226 | const struct elf_backend_data *bed; | |
0a1b45a2 | 4227 | bool add_needed; |
66eb6687 | 4228 | struct elf_link_hash_table *htab; |
66eb6687 | 4229 | void *alloc_mark = NULL; |
4f87808c AM |
4230 | struct bfd_hash_entry **old_table = NULL; |
4231 | unsigned int old_size = 0; | |
4232 | unsigned int old_count = 0; | |
66eb6687 | 4233 | void *old_tab = NULL; |
66eb6687 AM |
4234 | void *old_ent; |
4235 | struct bfd_link_hash_entry *old_undefs = NULL; | |
4236 | struct bfd_link_hash_entry *old_undefs_tail = NULL; | |
5b677558 | 4237 | void *old_strtab = NULL; |
66eb6687 | 4238 | size_t tabsize = 0; |
db6a5d5f | 4239 | asection *s; |
0a1b45a2 | 4240 | bool just_syms; |
4ad4eba5 | 4241 | |
66eb6687 | 4242 | htab = elf_hash_table (info); |
4ad4eba5 | 4243 | bed = get_elf_backend_data (abfd); |
4ad4eba5 | 4244 | |
46675b6b L |
4245 | if (elf_use_dt_symtab_p (abfd)) |
4246 | { | |
4247 | bfd_set_error (bfd_error_wrong_format); | |
4248 | return false; | |
4249 | } | |
4250 | ||
4ad4eba5 | 4251 | if ((abfd->flags & DYNAMIC) == 0) |
0a1b45a2 | 4252 | dynamic = false; |
4ad4eba5 AM |
4253 | else |
4254 | { | |
0a1b45a2 | 4255 | dynamic = true; |
4ad4eba5 AM |
4256 | |
4257 | /* You can't use -r against a dynamic object. Also, there's no | |
4258 | hope of using a dynamic object which does not exactly match | |
4259 | the format of the output file. */ | |
0e1862bb | 4260 | if (bfd_link_relocatable (info) |
2cc15b10 | 4261 | || !is_elf_hash_table (&htab->root) |
f13a99db | 4262 | || info->output_bfd->xvec != abfd->xvec) |
4ad4eba5 | 4263 | { |
0e1862bb | 4264 | if (bfd_link_relocatable (info)) |
9a0789ec NC |
4265 | bfd_set_error (bfd_error_invalid_operation); |
4266 | else | |
4267 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
4268 | goto error_return; |
4269 | } | |
4270 | } | |
4271 | ||
a0c402a5 L |
4272 | ehdr = elf_elfheader (abfd); |
4273 | if (info->warn_alternate_em | |
4274 | && bed->elf_machine_code != ehdr->e_machine | |
4275 | && ((bed->elf_machine_alt1 != 0 | |
4276 | && ehdr->e_machine == bed->elf_machine_alt1) | |
4277 | || (bed->elf_machine_alt2 != 0 | |
4278 | && ehdr->e_machine == bed->elf_machine_alt2))) | |
9793eb77 | 4279 | _bfd_error_handler |
695344c0 | 4280 | /* xgettext:c-format */ |
9793eb77 | 4281 | (_("alternate ELF machine code found (%d) in %pB, expecting %d"), |
a0c402a5 L |
4282 | ehdr->e_machine, abfd, bed->elf_machine_code); |
4283 | ||
4ad4eba5 AM |
4284 | /* As a GNU extension, any input sections which are named |
4285 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
4286 | symbol. This differs from .gnu.warning sections, which generate | |
4287 | warnings when they are included in an output file. */ | |
dd98f8d2 | 4288 | /* PR 12761: Also generate this warning when building shared libraries. */ |
db6a5d5f | 4289 | for (s = abfd->sections; s != NULL; s = s->next) |
4ad4eba5 | 4290 | { |
db6a5d5f | 4291 | const char *name; |
4ad4eba5 | 4292 | |
fd361982 | 4293 | name = bfd_section_name (s); |
08dedd66 | 4294 | if (startswith (name, ".gnu.warning.")) |
4ad4eba5 | 4295 | { |
db6a5d5f AM |
4296 | char *msg; |
4297 | bfd_size_type sz; | |
4298 | ||
4299 | name += sizeof ".gnu.warning." - 1; | |
4300 | ||
4301 | /* If this is a shared object, then look up the symbol | |
4302 | in the hash table. If it is there, and it is already | |
4303 | been defined, then we will not be using the entry | |
4304 | from this shared object, so we don't need to warn. | |
4305 | FIXME: If we see the definition in a regular object | |
4306 | later on, we will warn, but we shouldn't. The only | |
4307 | fix is to keep track of what warnings we are supposed | |
4308 | to emit, and then handle them all at the end of the | |
4309 | link. */ | |
4310 | if (dynamic) | |
4ad4eba5 | 4311 | { |
db6a5d5f AM |
4312 | struct elf_link_hash_entry *h; |
4313 | ||
0a1b45a2 | 4314 | h = elf_link_hash_lookup (htab, name, false, false, true); |
db6a5d5f AM |
4315 | |
4316 | /* FIXME: What about bfd_link_hash_common? */ | |
4317 | if (h != NULL | |
4318 | && (h->root.type == bfd_link_hash_defined | |
4319 | || h->root.type == bfd_link_hash_defweak)) | |
4320 | continue; | |
4321 | } | |
4ad4eba5 | 4322 | |
db6a5d5f AM |
4323 | sz = s->size; |
4324 | msg = (char *) bfd_alloc (abfd, sz + 1); | |
4325 | if (msg == NULL) | |
4326 | goto error_return; | |
4ad4eba5 | 4327 | |
db6a5d5f AM |
4328 | if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) |
4329 | goto error_return; | |
4ad4eba5 | 4330 | |
db6a5d5f | 4331 | msg[sz] = '\0'; |
4ad4eba5 | 4332 | |
db6a5d5f AM |
4333 | if (! (_bfd_generic_link_add_one_symbol |
4334 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
0a1b45a2 | 4335 | false, bed->collect, NULL))) |
db6a5d5f | 4336 | goto error_return; |
4ad4eba5 | 4337 | |
0e1862bb | 4338 | if (bfd_link_executable (info)) |
db6a5d5f AM |
4339 | { |
4340 | /* Clobber the section size so that the warning does | |
4341 | not get copied into the output file. */ | |
4342 | s->size = 0; | |
11d2f718 | 4343 | |
db6a5d5f AM |
4344 | /* Also set SEC_EXCLUDE, so that symbols defined in |
4345 | the warning section don't get copied to the output. */ | |
4346 | s->flags |= SEC_EXCLUDE; | |
4ad4eba5 AM |
4347 | } |
4348 | } | |
4349 | } | |
4350 | ||
29a9f53e L |
4351 | just_syms = ((s = abfd->sections) != NULL |
4352 | && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); | |
4353 | ||
0a1b45a2 | 4354 | add_needed = true; |
4ad4eba5 AM |
4355 | if (! dynamic) |
4356 | { | |
4357 | /* If we are creating a shared library, create all the dynamic | |
4358 | sections immediately. We need to attach them to something, | |
4359 | so we attach them to this BFD, provided it is the right | |
bf89386a L |
4360 | format and is not from ld --just-symbols. Always create the |
4361 | dynamic sections for -E/--dynamic-list. FIXME: If there | |
29a9f53e L |
4362 | are no input BFD's of the same format as the output, we can't |
4363 | make a shared library. */ | |
4364 | if (!just_syms | |
bf89386a | 4365 | && (bfd_link_pic (info) |
9c1d7a08 | 4366 | || (!bfd_link_relocatable (info) |
3c5fce9b | 4367 | && info->nointerp |
9c1d7a08 | 4368 | && (info->export_dynamic || info->dynamic))) |
2cc15b10 | 4369 | && is_elf_hash_table (&htab->root) |
f13a99db | 4370 | && info->output_bfd->xvec == abfd->xvec |
66eb6687 | 4371 | && !htab->dynamic_sections_created) |
4ad4eba5 AM |
4372 | { |
4373 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
4374 | goto error_return; | |
4375 | } | |
4376 | } | |
2cc15b10 | 4377 | else if (!is_elf_hash_table (&htab->root)) |
4ad4eba5 AM |
4378 | goto error_return; |
4379 | else | |
4380 | { | |
4ad4eba5 | 4381 | const char *soname = NULL; |
7ee314fa | 4382 | char *audit = NULL; |
4ad4eba5 | 4383 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; |
9acc85a6 | 4384 | const Elf_Internal_Phdr *phdr; |
e310298c | 4385 | struct elf_link_loaded_list *loaded_lib; |
4ad4eba5 AM |
4386 | |
4387 | /* ld --just-symbols and dynamic objects don't mix very well. | |
92fd189d | 4388 | ld shouldn't allow it. */ |
29a9f53e | 4389 | if (just_syms) |
92fd189d | 4390 | abort (); |
4ad4eba5 AM |
4391 | |
4392 | /* If this dynamic lib was specified on the command line with | |
4393 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
4394 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
4395 | in by another lib's DT_NEEDED. When --no-add-needed is used |
4396 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
4397 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
4398 | all. */ | |
4399 | add_needed = (elf_dyn_lib_class (abfd) | |
4400 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
4401 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
4402 | |
4403 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
81ff113f | 4404 | if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0) |
4ad4eba5 AM |
4405 | { |
4406 | bfd_byte *dynbuf; | |
4407 | bfd_byte *extdyn; | |
cb33740c | 4408 | unsigned int elfsec; |
4ad4eba5 AM |
4409 | unsigned long shlink; |
4410 | ||
eea6121a | 4411 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
f8703194 | 4412 | { |
dc1e8a47 | 4413 | error_free_dyn: |
f8703194 L |
4414 | free (dynbuf); |
4415 | goto error_return; | |
4416 | } | |
4ad4eba5 AM |
4417 | |
4418 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 4419 | if (elfsec == SHN_BAD) |
4ad4eba5 AM |
4420 | goto error_free_dyn; |
4421 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
4422 | ||
4423 | for (extdyn = dynbuf; | |
37c59664 | 4424 | (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn; |
4ad4eba5 AM |
4425 | extdyn += bed->s->sizeof_dyn) |
4426 | { | |
4427 | Elf_Internal_Dyn dyn; | |
4428 | ||
4429 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
4430 | if (dyn.d_tag == DT_SONAME) | |
4431 | { | |
4432 | unsigned int tagv = dyn.d_un.d_val; | |
4433 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4434 | if (soname == NULL) | |
4435 | goto error_free_dyn; | |
4436 | } | |
4437 | if (dyn.d_tag == DT_NEEDED) | |
4438 | { | |
4439 | struct bfd_link_needed_list *n, **pn; | |
4440 | char *fnm, *anm; | |
4441 | unsigned int tagv = dyn.d_un.d_val; | |
986f0783 | 4442 | size_t amt = sizeof (struct bfd_link_needed_list); |
4ad4eba5 | 4443 | |
a50b1753 | 4444 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4445 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4446 | if (n == NULL || fnm == NULL) | |
4447 | goto error_free_dyn; | |
4448 | amt = strlen (fnm) + 1; | |
a50b1753 | 4449 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4450 | if (anm == NULL) |
4451 | goto error_free_dyn; | |
4452 | memcpy (anm, fnm, amt); | |
4453 | n->name = anm; | |
4454 | n->by = abfd; | |
4455 | n->next = NULL; | |
66eb6687 | 4456 | for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4457 | ; |
4458 | *pn = n; | |
4459 | } | |
4460 | if (dyn.d_tag == DT_RUNPATH) | |
4461 | { | |
4462 | struct bfd_link_needed_list *n, **pn; | |
4463 | char *fnm, *anm; | |
4464 | unsigned int tagv = dyn.d_un.d_val; | |
986f0783 | 4465 | size_t amt = sizeof (struct bfd_link_needed_list); |
4ad4eba5 | 4466 | |
a50b1753 | 4467 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4468 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4469 | if (n == NULL || fnm == NULL) | |
4470 | goto error_free_dyn; | |
4471 | amt = strlen (fnm) + 1; | |
a50b1753 | 4472 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4473 | if (anm == NULL) |
4474 | goto error_free_dyn; | |
4475 | memcpy (anm, fnm, amt); | |
4476 | n->name = anm; | |
4477 | n->by = abfd; | |
4478 | n->next = NULL; | |
4479 | for (pn = & runpath; | |
4480 | *pn != NULL; | |
4481 | pn = &(*pn)->next) | |
4482 | ; | |
4483 | *pn = n; | |
4484 | } | |
4485 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
4486 | if (!runpath && dyn.d_tag == DT_RPATH) | |
4487 | { | |
4488 | struct bfd_link_needed_list *n, **pn; | |
4489 | char *fnm, *anm; | |
4490 | unsigned int tagv = dyn.d_un.d_val; | |
986f0783 | 4491 | size_t amt = sizeof (struct bfd_link_needed_list); |
4ad4eba5 | 4492 | |
a50b1753 | 4493 | n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4ad4eba5 AM |
4494 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
4495 | if (n == NULL || fnm == NULL) | |
4496 | goto error_free_dyn; | |
4497 | amt = strlen (fnm) + 1; | |
a50b1753 | 4498 | anm = (char *) bfd_alloc (abfd, amt); |
4ad4eba5 | 4499 | if (anm == NULL) |
f8703194 | 4500 | goto error_free_dyn; |
4ad4eba5 AM |
4501 | memcpy (anm, fnm, amt); |
4502 | n->name = anm; | |
4503 | n->by = abfd; | |
4504 | n->next = NULL; | |
4505 | for (pn = & rpath; | |
4506 | *pn != NULL; | |
4507 | pn = &(*pn)->next) | |
4508 | ; | |
4509 | *pn = n; | |
4510 | } | |
7ee314fa AM |
4511 | if (dyn.d_tag == DT_AUDIT) |
4512 | { | |
4513 | unsigned int tagv = dyn.d_un.d_val; | |
4514 | audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
4515 | } | |
f64b9b13 AM |
4516 | if (dyn.d_tag == DT_FLAGS_1) |
4517 | elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0; | |
4ad4eba5 AM |
4518 | } |
4519 | ||
4520 | free (dynbuf); | |
4521 | } | |
4522 | ||
4523 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
4524 | frees all more recently bfd_alloc'd blocks as well. */ | |
4525 | if (runpath) | |
4526 | rpath = runpath; | |
4527 | ||
4528 | if (rpath) | |
4529 | { | |
4530 | struct bfd_link_needed_list **pn; | |
66eb6687 | 4531 | for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) |
4ad4eba5 AM |
4532 | ; |
4533 | *pn = rpath; | |
4534 | } | |
4535 | ||
9acc85a6 AM |
4536 | /* If we have a PT_GNU_RELRO program header, mark as read-only |
4537 | all sections contained fully therein. This makes relro | |
4538 | shared library sections appear as they will at run-time. */ | |
4539 | phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; | |
54025d58 | 4540 | while (phdr-- > elf_tdata (abfd)->phdr) |
9acc85a6 AM |
4541 | if (phdr->p_type == PT_GNU_RELRO) |
4542 | { | |
4543 | for (s = abfd->sections; s != NULL; s = s->next) | |
502794d4 CE |
4544 | { |
4545 | unsigned int opb = bfd_octets_per_byte (abfd, s); | |
4546 | ||
4547 | if ((s->flags & SEC_ALLOC) != 0 | |
4548 | && s->vma * opb >= phdr->p_vaddr | |
4549 | && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz) | |
4550 | s->flags |= SEC_READONLY; | |
4551 | } | |
9acc85a6 AM |
4552 | break; |
4553 | } | |
4554 | ||
4ad4eba5 AM |
4555 | /* We do not want to include any of the sections in a dynamic |
4556 | object in the output file. We hack by simply clobbering the | |
4557 | list of sections in the BFD. This could be handled more | |
4558 | cleanly by, say, a new section flag; the existing | |
4559 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4560 | still implies that the section takes up space in the output | |
4561 | file. */ | |
4562 | bfd_section_list_clear (abfd); | |
4563 | ||
4ad4eba5 AM |
4564 | /* Find the name to use in a DT_NEEDED entry that refers to this |
4565 | object. If the object has a DT_SONAME entry, we use it. | |
4566 | Otherwise, if the generic linker stuck something in | |
4567 | elf_dt_name, we use that. Otherwise, we just use the file | |
4568 | name. */ | |
4569 | if (soname == NULL || *soname == '\0') | |
4570 | { | |
4571 | soname = elf_dt_name (abfd); | |
4572 | if (soname == NULL || *soname == '\0') | |
4573 | soname = bfd_get_filename (abfd); | |
4574 | } | |
4575 | ||
4576 | /* Save the SONAME because sometimes the linker emulation code | |
4577 | will need to know it. */ | |
4578 | elf_dt_name (abfd) = soname; | |
4579 | ||
4ad4eba5 AM |
4580 | /* If we have already included this dynamic object in the |
4581 | link, just ignore it. There is no reason to include a | |
4582 | particular dynamic object more than once. */ | |
e310298c AM |
4583 | for (loaded_lib = htab->dyn_loaded; |
4584 | loaded_lib != NULL; | |
4585 | loaded_lib = loaded_lib->next) | |
4586 | { | |
4587 | if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0) | |
0a1b45a2 | 4588 | return true; |
e310298c AM |
4589 | } |
4590 | ||
4591 | /* Create dynamic sections for backends that require that be done | |
4592 | before setup_gnu_properties. */ | |
4593 | if (add_needed | |
4594 | && !_bfd_elf_link_create_dynamic_sections (abfd, info)) | |
0a1b45a2 | 4595 | return false; |
7ee314fa AM |
4596 | |
4597 | /* Save the DT_AUDIT entry for the linker emulation code. */ | |
68ffbac6 | 4598 | elf_dt_audit (abfd) = audit; |
4ad4eba5 AM |
4599 | } |
4600 | ||
4601 | /* If this is a dynamic object, we always link against the .dynsym | |
4602 | symbol table, not the .symtab symbol table. The dynamic linker | |
4603 | will only see the .dynsym symbol table, so there is no reason to | |
4604 | look at .symtab for a dynamic object. */ | |
4605 | ||
4606 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
4607 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4608 | else | |
4609 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4610 | ||
4611 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
4612 | ||
4613 | /* The sh_info field of the symtab header tells us where the | |
4614 | external symbols start. We don't care about the local symbols at | |
4615 | this point. */ | |
4616 | if (elf_bad_symtab (abfd)) | |
4617 | { | |
4618 | extsymcount = symcount; | |
4619 | extsymoff = 0; | |
4620 | } | |
4621 | else | |
4622 | { | |
4623 | extsymcount = symcount - hdr->sh_info; | |
4624 | extsymoff = hdr->sh_info; | |
4625 | } | |
4626 | ||
f45794cb | 4627 | sym_hash = elf_sym_hashes (abfd); |
012b2306 | 4628 | if (extsymcount != 0) |
4ad4eba5 AM |
4629 | { |
4630 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
4631 | NULL, NULL, NULL); | |
4632 | if (isymbuf == NULL) | |
4633 | goto error_return; | |
4634 | ||
4ad4eba5 | 4635 | if (sym_hash == NULL) |
012b2306 AM |
4636 | { |
4637 | /* We store a pointer to the hash table entry for each | |
4638 | external symbol. */ | |
986f0783 | 4639 | size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *); |
012b2306 AM |
4640 | sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); |
4641 | if (sym_hash == NULL) | |
4642 | goto error_free_sym; | |
4643 | elf_sym_hashes (abfd) = sym_hash; | |
4644 | } | |
4ad4eba5 AM |
4645 | } |
4646 | ||
4647 | if (dynamic) | |
4648 | { | |
4649 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
4650 | if (!_bfd_elf_slurp_version_tables (abfd, |
4651 | info->default_imported_symver)) | |
4ad4eba5 AM |
4652 | goto error_free_sym; |
4653 | ||
4654 | /* Read in the symbol versions, but don't bother to convert them | |
4655 | to internal format. */ | |
4656 | if (elf_dynversym (abfd) != 0) | |
4657 | { | |
986f0783 AM |
4658 | Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr; |
4659 | bfd_size_type amt = versymhdr->sh_size; | |
4ad4eba5 | 4660 | |
2bb3687b AM |
4661 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0) |
4662 | goto error_free_sym; | |
4663 | extversym = (Elf_External_Versym *) | |
4664 | _bfd_malloc_and_read (abfd, amt, amt); | |
4ad4eba5 AM |
4665 | if (extversym == NULL) |
4666 | goto error_free_sym; | |
986f0783 | 4667 | extversym_end = extversym + amt / sizeof (*extversym); |
4ad4eba5 AM |
4668 | } |
4669 | } | |
4670 | ||
66eb6687 AM |
4671 | /* If we are loading an as-needed shared lib, save the symbol table |
4672 | state before we start adding symbols. If the lib turns out | |
4673 | to be unneeded, restore the state. */ | |
4674 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
4675 | { | |
4676 | unsigned int i; | |
4677 | size_t entsize; | |
4678 | ||
4679 | for (entsize = 0, i = 0; i < htab->root.table.size; i++) | |
4680 | { | |
4681 | struct bfd_hash_entry *p; | |
2de92251 | 4682 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4683 | |
4684 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
2de92251 AM |
4685 | { |
4686 | h = (struct elf_link_hash_entry *) p; | |
4687 | entsize += htab->root.table.entsize; | |
4688 | if (h->root.type == bfd_link_hash_warning) | |
7ba11550 AM |
4689 | { |
4690 | entsize += htab->root.table.entsize; | |
4691 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4692 | } | |
4693 | if (h->root.type == bfd_link_hash_common) | |
4694 | entsize += sizeof (*h->root.u.c.p); | |
2de92251 | 4695 | } |
66eb6687 AM |
4696 | } |
4697 | ||
4698 | tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); | |
f45794cb | 4699 | old_tab = bfd_malloc (tabsize + entsize); |
66eb6687 AM |
4700 | if (old_tab == NULL) |
4701 | goto error_free_vers; | |
4702 | ||
4703 | /* Remember the current objalloc pointer, so that all mem for | |
4704 | symbols added can later be reclaimed. */ | |
4705 | alloc_mark = bfd_hash_allocate (&htab->root.table, 1); | |
4706 | if (alloc_mark == NULL) | |
4707 | goto error_free_vers; | |
4708 | ||
5061a885 AM |
4709 | /* Make a special call to the linker "notice" function to |
4710 | tell it that we are about to handle an as-needed lib. */ | |
e5034e59 | 4711 | if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) |
9af2a943 | 4712 | goto error_free_vers; |
5061a885 | 4713 | |
f45794cb AM |
4714 | /* Clone the symbol table. Remember some pointers into the |
4715 | symbol table, and dynamic symbol count. */ | |
4716 | old_ent = (char *) old_tab + tabsize; | |
66eb6687 | 4717 | memcpy (old_tab, htab->root.table.table, tabsize); |
66eb6687 AM |
4718 | old_undefs = htab->root.undefs; |
4719 | old_undefs_tail = htab->root.undefs_tail; | |
4f87808c AM |
4720 | old_table = htab->root.table.table; |
4721 | old_size = htab->root.table.size; | |
4722 | old_count = htab->root.table.count; | |
e310298c AM |
4723 | old_strtab = NULL; |
4724 | if (htab->dynstr != NULL) | |
4725 | { | |
4726 | old_strtab = _bfd_elf_strtab_save (htab->dynstr); | |
4727 | if (old_strtab == NULL) | |
4728 | goto error_free_vers; | |
4729 | } | |
66eb6687 AM |
4730 | |
4731 | for (i = 0; i < htab->root.table.size; i++) | |
4732 | { | |
4733 | struct bfd_hash_entry *p; | |
2de92251 | 4734 | struct elf_link_hash_entry *h; |
66eb6687 AM |
4735 | |
4736 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
4737 | { | |
2de92251 | 4738 | h = (struct elf_link_hash_entry *) p; |
7ba11550 AM |
4739 | memcpy (old_ent, h, htab->root.table.entsize); |
4740 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
4741 | if (h->root.type == bfd_link_hash_warning) |
4742 | { | |
7ba11550 AM |
4743 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
4744 | memcpy (old_ent, h, htab->root.table.entsize); | |
2de92251 AM |
4745 | old_ent = (char *) old_ent + htab->root.table.entsize; |
4746 | } | |
7ba11550 AM |
4747 | if (h->root.type == bfd_link_hash_common) |
4748 | { | |
4749 | memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p)); | |
4750 | old_ent = (char *) old_ent + sizeof (*h->root.u.c.p); | |
4751 | } | |
66eb6687 AM |
4752 | } |
4753 | } | |
4754 | } | |
4ad4eba5 | 4755 | |
66eb6687 | 4756 | weaks = NULL; |
be22c732 NC |
4757 | if (extversym == NULL) |
4758 | ever = NULL; | |
4759 | else if (extversym + extsymoff < extversym_end) | |
4760 | ever = extversym + extsymoff; | |
4761 | else | |
4762 | { | |
4763 | /* xgettext:c-format */ | |
4764 | _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"), | |
4765 | abfd, (long) extsymoff, | |
4766 | (long) (extversym_end - extversym) / sizeof (* extversym)); | |
4767 | bfd_set_error (bfd_error_bad_value); | |
4768 | goto error_free_vers; | |
4769 | } | |
4770 | ||
b4c555cf ML |
4771 | if (!bfd_link_relocatable (info) |
4772 | && abfd->lto_slim_object) | |
cc5277b1 ML |
4773 | { |
4774 | _bfd_error_handler | |
4775 | (_("%pB: plugin needed to handle lto object"), abfd); | |
4776 | } | |
4777 | ||
36f61bf2 | 4778 | for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount); |
4ad4eba5 AM |
4779 | isym < isymend; |
4780 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
4781 | { | |
4782 | int bind; | |
4783 | bfd_vma value; | |
af44c138 | 4784 | asection *sec, *new_sec; |
4ad4eba5 AM |
4785 | flagword flags; |
4786 | const char *name; | |
4787 | struct elf_link_hash_entry *h; | |
90c984fc | 4788 | struct elf_link_hash_entry *hi; |
0a1b45a2 AM |
4789 | bool definition; |
4790 | bool size_change_ok; | |
4791 | bool type_change_ok; | |
4792 | bool new_weak; | |
4793 | bool old_weak; | |
7ba11550 | 4794 | bfd *override; |
0a1b45a2 AM |
4795 | bool common; |
4796 | bool discarded; | |
4ad4eba5 | 4797 | unsigned int old_alignment; |
4538d1c7 | 4798 | unsigned int shindex; |
4ad4eba5 | 4799 | bfd *old_bfd; |
0a1b45a2 | 4800 | bool matched; |
4ad4eba5 | 4801 | |
7ba11550 | 4802 | override = NULL; |
4ad4eba5 AM |
4803 | |
4804 | flags = BSF_NO_FLAGS; | |
4805 | sec = NULL; | |
4806 | value = isym->st_value; | |
a4d8e49b | 4807 | common = bed->common_definition (isym); |
2980ccad L |
4808 | if (common && info->inhibit_common_definition) |
4809 | { | |
4810 | /* Treat common symbol as undefined for --no-define-common. */ | |
4811 | isym->st_shndx = SHN_UNDEF; | |
0a1b45a2 | 4812 | common = false; |
2980ccad | 4813 | } |
0a1b45a2 | 4814 | discarded = false; |
4ad4eba5 AM |
4815 | |
4816 | bind = ELF_ST_BIND (isym->st_info); | |
3e7a7d11 | 4817 | switch (bind) |
4ad4eba5 | 4818 | { |
3e7a7d11 | 4819 | case STB_LOCAL: |
4ad4eba5 AM |
4820 | /* This should be impossible, since ELF requires that all |
4821 | global symbols follow all local symbols, and that sh_info | |
4822 | point to the first global symbol. Unfortunately, Irix 5 | |
4823 | screws this up. */ | |
fe3fef62 AM |
4824 | if (elf_bad_symtab (abfd)) |
4825 | continue; | |
4826 | ||
4827 | /* If we aren't prepared to handle locals within the globals | |
4538d1c7 AM |
4828 | then we'll likely segfault on a NULL symbol hash if the |
4829 | symbol is ever referenced in relocations. */ | |
4830 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
4831 | name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name); | |
4832 | _bfd_error_handler (_("%pB: %s local symbol at index %lu" | |
4833 | " (>= sh_info of %lu)"), | |
4834 | abfd, name, (long) (isym - isymbuf + extsymoff), | |
4835 | (long) extsymoff); | |
4836 | ||
4837 | /* Dynamic object relocations are not processed by ld, so | |
4838 | ld won't run into the problem mentioned above. */ | |
4839 | if (dynamic) | |
4840 | continue; | |
fe3fef62 AM |
4841 | bfd_set_error (bfd_error_bad_value); |
4842 | goto error_free_vers; | |
3e7a7d11 NC |
4843 | |
4844 | case STB_GLOBAL: | |
a4d8e49b | 4845 | if (isym->st_shndx != SHN_UNDEF && !common) |
4ad4eba5 | 4846 | flags = BSF_GLOBAL; |
3e7a7d11 NC |
4847 | break; |
4848 | ||
4849 | case STB_WEAK: | |
4850 | flags = BSF_WEAK; | |
4851 | break; | |
4852 | ||
4853 | case STB_GNU_UNIQUE: | |
4854 | flags = BSF_GNU_UNIQUE; | |
4855 | break; | |
4856 | ||
4857 | default: | |
4ad4eba5 | 4858 | /* Leave it up to the processor backend. */ |
3e7a7d11 | 4859 | break; |
4ad4eba5 AM |
4860 | } |
4861 | ||
4862 | if (isym->st_shndx == SHN_UNDEF) | |
4863 | sec = bfd_und_section_ptr; | |
cb33740c AM |
4864 | else if (isym->st_shndx == SHN_ABS) |
4865 | sec = bfd_abs_section_ptr; | |
4866 | else if (isym->st_shndx == SHN_COMMON) | |
4867 | { | |
4868 | sec = bfd_com_section_ptr; | |
4869 | /* What ELF calls the size we call the value. What ELF | |
4870 | calls the value we call the alignment. */ | |
4871 | value = isym->st_size; | |
4872 | } | |
4873 | else | |
4ad4eba5 AM |
4874 | { |
4875 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
4876 | if (sec == NULL) | |
4877 | sec = bfd_abs_section_ptr; | |
dbaa2011 | 4878 | else if (discarded_section (sec)) |
529fcb95 | 4879 | { |
e5d08002 L |
4880 | /* Symbols from discarded section are undefined. We keep |
4881 | its visibility. */ | |
529fcb95 | 4882 | sec = bfd_und_section_ptr; |
0a1b45a2 | 4883 | discarded = true; |
529fcb95 PB |
4884 | isym->st_shndx = SHN_UNDEF; |
4885 | } | |
4ad4eba5 AM |
4886 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
4887 | value -= sec->vma; | |
4888 | } | |
4ad4eba5 AM |
4889 | |
4890 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4891 | isym->st_name); | |
4892 | if (name == NULL) | |
4893 | goto error_free_vers; | |
4894 | ||
4895 | if (isym->st_shndx == SHN_COMMON | |
02d00247 AM |
4896 | && (abfd->flags & BFD_PLUGIN) != 0) |
4897 | { | |
4898 | asection *xc = bfd_get_section_by_name (abfd, "COMMON"); | |
4899 | ||
4900 | if (xc == NULL) | |
4901 | { | |
4902 | flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP | |
4903 | | SEC_EXCLUDE); | |
4904 | xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); | |
4905 | if (xc == NULL) | |
4906 | goto error_free_vers; | |
4907 | } | |
4908 | sec = xc; | |
4909 | } | |
4910 | else if (isym->st_shndx == SHN_COMMON | |
4911 | && ELF_ST_TYPE (isym->st_info) == STT_TLS | |
0e1862bb | 4912 | && !bfd_link_relocatable (info)) |
4ad4eba5 AM |
4913 | { |
4914 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
4915 | ||
4916 | if (tcomm == NULL) | |
4917 | { | |
02d00247 AM |
4918 | flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON |
4919 | | SEC_LINKER_CREATED); | |
4920 | tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); | |
3496cb2a | 4921 | if (tcomm == NULL) |
4ad4eba5 AM |
4922 | goto error_free_vers; |
4923 | } | |
4924 | sec = tcomm; | |
4925 | } | |
66eb6687 | 4926 | else if (bed->elf_add_symbol_hook) |
4ad4eba5 | 4927 | { |
66eb6687 AM |
4928 | if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, |
4929 | &sec, &value)) | |
4ad4eba5 AM |
4930 | goto error_free_vers; |
4931 | ||
4932 | /* The hook function sets the name to NULL if this symbol | |
4933 | should be skipped for some reason. */ | |
4934 | if (name == NULL) | |
4935 | continue; | |
4936 | } | |
4937 | ||
4938 | /* Sanity check that all possibilities were handled. */ | |
4939 | if (sec == NULL) | |
4538d1c7 | 4940 | abort (); |
4ad4eba5 | 4941 | |
191c0c42 AM |
4942 | /* Silently discard TLS symbols from --just-syms. There's |
4943 | no way to combine a static TLS block with a new TLS block | |
4944 | for this executable. */ | |
4945 | if (ELF_ST_TYPE (isym->st_info) == STT_TLS | |
4946 | && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
4947 | continue; | |
4948 | ||
4ad4eba5 AM |
4949 | if (bfd_is_und_section (sec) |
4950 | || bfd_is_com_section (sec)) | |
0a1b45a2 | 4951 | definition = false; |
4ad4eba5 | 4952 | else |
0a1b45a2 | 4953 | definition = true; |
4ad4eba5 | 4954 | |
0a1b45a2 | 4955 | size_change_ok = false; |
66eb6687 | 4956 | type_change_ok = bed->type_change_ok; |
0a1b45a2 AM |
4957 | old_weak = false; |
4958 | matched = false; | |
4ad4eba5 AM |
4959 | old_alignment = 0; |
4960 | old_bfd = NULL; | |
af44c138 | 4961 | new_sec = sec; |
4ad4eba5 | 4962 | |
2cc15b10 | 4963 | if (is_elf_hash_table (&htab->root)) |
4ad4eba5 AM |
4964 | { |
4965 | Elf_Internal_Versym iver; | |
4966 | unsigned int vernum = 0; | |
0a1b45a2 | 4967 | bool skip; |
4ad4eba5 | 4968 | |
fc0e6df6 | 4969 | if (ever == NULL) |
4ad4eba5 | 4970 | { |
fc0e6df6 PB |
4971 | if (info->default_imported_symver) |
4972 | /* Use the default symbol version created earlier. */ | |
4973 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
4974 | else | |
4975 | iver.vs_vers = 0; | |
4976 | } | |
be22c732 NC |
4977 | else if (ever >= extversym_end) |
4978 | { | |
4979 | /* xgettext:c-format */ | |
4980 | _bfd_error_handler (_("%pB: not enough version information"), | |
4981 | abfd); | |
4982 | bfd_set_error (bfd_error_bad_value); | |
4983 | goto error_free_vers; | |
4984 | } | |
fc0e6df6 PB |
4985 | else |
4986 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
4987 | ||
4988 | vernum = iver.vs_vers & VERSYM_VERSION; | |
4989 | ||
4990 | /* If this is a hidden symbol, or if it is not version | |
4991 | 1, we append the version name to the symbol name. | |
cc86ff91 EB |
4992 | However, we do not modify a non-hidden absolute symbol |
4993 | if it is not a function, because it might be the version | |
4994 | symbol itself. FIXME: What if it isn't? */ | |
fc0e6df6 | 4995 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 |
fcb93ecf PB |
4996 | || (vernum > 1 |
4997 | && (!bfd_is_abs_section (sec) | |
4998 | || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) | |
fc0e6df6 PB |
4999 | { |
5000 | const char *verstr; | |
5001 | size_t namelen, verlen, newlen; | |
5002 | char *newname, *p; | |
5003 | ||
5004 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 5005 | { |
fc0e6df6 PB |
5006 | if (vernum > elf_tdata (abfd)->cverdefs) |
5007 | verstr = NULL; | |
5008 | else if (vernum > 1) | |
5009 | verstr = | |
5010 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
5011 | else | |
5012 | verstr = ""; | |
4ad4eba5 | 5013 | |
fc0e6df6 | 5014 | if (verstr == NULL) |
4ad4eba5 | 5015 | { |
4eca0228 | 5016 | _bfd_error_handler |
695344c0 | 5017 | /* xgettext:c-format */ |
871b3ab2 | 5018 | (_("%pB: %s: invalid version %u (max %d)"), |
fc0e6df6 PB |
5019 | abfd, name, vernum, |
5020 | elf_tdata (abfd)->cverdefs); | |
5021 | bfd_set_error (bfd_error_bad_value); | |
5022 | goto error_free_vers; | |
4ad4eba5 | 5023 | } |
fc0e6df6 PB |
5024 | } |
5025 | else | |
5026 | { | |
5027 | /* We cannot simply test for the number of | |
5028 | entries in the VERNEED section since the | |
5029 | numbers for the needed versions do not start | |
5030 | at 0. */ | |
5031 | Elf_Internal_Verneed *t; | |
5032 | ||
5033 | verstr = NULL; | |
5034 | for (t = elf_tdata (abfd)->verref; | |
5035 | t != NULL; | |
5036 | t = t->vn_nextref) | |
4ad4eba5 | 5037 | { |
fc0e6df6 | 5038 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 5039 | |
fc0e6df6 PB |
5040 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
5041 | { | |
5042 | if (a->vna_other == vernum) | |
4ad4eba5 | 5043 | { |
fc0e6df6 PB |
5044 | verstr = a->vna_nodename; |
5045 | break; | |
4ad4eba5 | 5046 | } |
4ad4eba5 | 5047 | } |
fc0e6df6 PB |
5048 | if (a != NULL) |
5049 | break; | |
5050 | } | |
5051 | if (verstr == NULL) | |
5052 | { | |
4eca0228 | 5053 | _bfd_error_handler |
695344c0 | 5054 | /* xgettext:c-format */ |
871b3ab2 | 5055 | (_("%pB: %s: invalid needed version %d"), |
fc0e6df6 PB |
5056 | abfd, name, vernum); |
5057 | bfd_set_error (bfd_error_bad_value); | |
5058 | goto error_free_vers; | |
4ad4eba5 | 5059 | } |
4ad4eba5 | 5060 | } |
fc0e6df6 PB |
5061 | |
5062 | namelen = strlen (name); | |
5063 | verlen = strlen (verstr); | |
5064 | newlen = namelen + verlen + 2; | |
5065 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
5066 | && isym->st_shndx != SHN_UNDEF) | |
5067 | ++newlen; | |
5068 | ||
a50b1753 | 5069 | newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); |
fc0e6df6 PB |
5070 | if (newname == NULL) |
5071 | goto error_free_vers; | |
5072 | memcpy (newname, name, namelen); | |
5073 | p = newname + namelen; | |
5074 | *p++ = ELF_VER_CHR; | |
5075 | /* If this is a defined non-hidden version symbol, | |
5076 | we add another @ to the name. This indicates the | |
5077 | default version of the symbol. */ | |
5078 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
5079 | && isym->st_shndx != SHN_UNDEF) | |
5080 | *p++ = ELF_VER_CHR; | |
5081 | memcpy (p, verstr, verlen + 1); | |
5082 | ||
5083 | name = newname; | |
4ad4eba5 AM |
5084 | } |
5085 | ||
cd3416da AM |
5086 | /* If this symbol has default visibility and the user has |
5087 | requested we not re-export it, then mark it as hidden. */ | |
a0d49154 | 5088 | if (!bfd_is_und_section (sec) |
cd3416da | 5089 | && !dynamic |
ce875075 | 5090 | && abfd->no_export |
cd3416da AM |
5091 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) |
5092 | isym->st_other = (STV_HIDDEN | |
5093 | | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); | |
5094 | ||
4f3fedcf AM |
5095 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, |
5096 | sym_hash, &old_bfd, &old_weak, | |
5097 | &old_alignment, &skip, &override, | |
6e33951e L |
5098 | &type_change_ok, &size_change_ok, |
5099 | &matched)) | |
4ad4eba5 AM |
5100 | goto error_free_vers; |
5101 | ||
5102 | if (skip) | |
5103 | continue; | |
5104 | ||
6e33951e L |
5105 | /* Override a definition only if the new symbol matches the |
5106 | existing one. */ | |
5107 | if (override && matched) | |
0a1b45a2 | 5108 | definition = false; |
4ad4eba5 AM |
5109 | |
5110 | h = *sym_hash; | |
5111 | while (h->root.type == bfd_link_hash_indirect | |
5112 | || h->root.type == bfd_link_hash_warning) | |
5113 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5114 | ||
e4675a58 L |
5115 | if (h->versioned != unversioned |
5116 | && elf_tdata (abfd)->verdef != NULL | |
4ad4eba5 AM |
5117 | && vernum > 1 |
5118 | && definition) | |
5119 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
5120 | } | |
5121 | ||
5122 | if (! (_bfd_generic_link_add_one_symbol | |
7ba11550 | 5123 | (info, override ? override : abfd, name, flags, sec, value, |
0a1b45a2 | 5124 | NULL, false, bed->collect, |
4ad4eba5 AM |
5125 | (struct bfd_link_hash_entry **) sym_hash))) |
5126 | goto error_free_vers; | |
5127 | ||
5128 | h = *sym_hash; | |
90c984fc L |
5129 | /* We need to make sure that indirect symbol dynamic flags are |
5130 | updated. */ | |
5131 | hi = h; | |
4ad4eba5 AM |
5132 | while (h->root.type == bfd_link_hash_indirect |
5133 | || h->root.type == bfd_link_hash_warning) | |
5134 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3e7a7d11 | 5135 | |
d1cbe007 AM |
5136 | *sym_hash = h; |
5137 | ||
97196564 L |
5138 | /* Setting the index to -3 tells elf_link_output_extsym that |
5139 | this symbol is defined in a discarded section. */ | |
2cc15b10 | 5140 | if (discarded && is_elf_hash_table (&htab->root)) |
97196564 L |
5141 | h->indx = -3; |
5142 | ||
37a9e49a | 5143 | new_weak = (flags & BSF_WEAK) != 0; |
4ad4eba5 AM |
5144 | if (dynamic |
5145 | && definition | |
37a9e49a | 5146 | && new_weak |
fcb93ecf | 5147 | && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) |
2cc15b10 | 5148 | && is_elf_hash_table (&htab->root) |
60d67dc8 | 5149 | && h->u.alias == NULL) |
4ad4eba5 AM |
5150 | { |
5151 | /* Keep a list of all weak defined non function symbols from | |
60d67dc8 AM |
5152 | a dynamic object, using the alias field. Later in this |
5153 | function we will set the alias field to the correct | |
4ad4eba5 AM |
5154 | value. We only put non-function symbols from dynamic |
5155 | objects on this list, because that happens to be the only | |
5156 | time we need to know the normal symbol corresponding to a | |
5157 | weak symbol, and the information is time consuming to | |
60d67dc8 | 5158 | figure out. If the alias field is not already NULL, |
4ad4eba5 AM |
5159 | then this symbol was already defined by some previous |
5160 | dynamic object, and we will be using that previous | |
5161 | definition anyhow. */ | |
5162 | ||
60d67dc8 | 5163 | h->u.alias = weaks; |
4ad4eba5 | 5164 | weaks = h; |
4ad4eba5 AM |
5165 | } |
5166 | ||
5167 | /* Set the alignment of a common symbol. */ | |
a4d8e49b | 5168 | if ((common || bfd_is_com_section (sec)) |
4ad4eba5 AM |
5169 | && h->root.type == bfd_link_hash_common) |
5170 | { | |
5171 | unsigned int align; | |
5172 | ||
a4d8e49b | 5173 | if (common) |
af44c138 L |
5174 | align = bfd_log2 (isym->st_value); |
5175 | else | |
5176 | { | |
5177 | /* The new symbol is a common symbol in a shared object. | |
5178 | We need to get the alignment from the section. */ | |
5179 | align = new_sec->alignment_power; | |
5180 | } | |
595213d4 | 5181 | if (align > old_alignment) |
4ad4eba5 AM |
5182 | h->root.u.c.p->alignment_power = align; |
5183 | else | |
5184 | h->root.u.c.p->alignment_power = old_alignment; | |
5185 | } | |
5186 | ||
2cc15b10 | 5187 | if (is_elf_hash_table (&htab->root)) |
4ad4eba5 | 5188 | { |
4f3fedcf AM |
5189 | /* Set a flag in the hash table entry indicating the type of |
5190 | reference or definition we just found. A dynamic symbol | |
5191 | is one which is referenced or defined by both a regular | |
5192 | object and a shared object. */ | |
0a1b45a2 | 5193 | bool dynsym = false; |
4f3fedcf | 5194 | |
b1a92c63 AM |
5195 | /* Plugin symbols aren't normal. Don't set def/ref flags. */ |
5196 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
bbaddd4b AM |
5197 | { |
5198 | /* Except for this flag to track nonweak references. */ | |
5199 | if (!definition | |
5200 | && bind != STB_WEAK) | |
5201 | h->ref_ir_nonweak = 1; | |
5202 | } | |
b1a92c63 | 5203 | else if (!dynamic) |
4f3fedcf AM |
5204 | { |
5205 | if (! definition) | |
5206 | { | |
5207 | h->ref_regular = 1; | |
5208 | if (bind != STB_WEAK) | |
5209 | h->ref_regular_nonweak = 1; | |
5210 | } | |
5211 | else | |
5212 | { | |
5213 | h->def_regular = 1; | |
5214 | if (h->def_dynamic) | |
5215 | { | |
5216 | h->def_dynamic = 0; | |
5217 | h->ref_dynamic = 1; | |
5218 | } | |
5219 | } | |
4f3fedcf AM |
5220 | } |
5221 | else | |
5222 | { | |
5223 | if (! definition) | |
5224 | { | |
5225 | h->ref_dynamic = 1; | |
5226 | hi->ref_dynamic = 1; | |
5227 | } | |
5228 | else | |
5229 | { | |
5230 | h->def_dynamic = 1; | |
5231 | hi->def_dynamic = 1; | |
5232 | } | |
b1a92c63 | 5233 | } |
4f3fedcf | 5234 | |
b1a92c63 AM |
5235 | /* If an indirect symbol has been forced local, don't |
5236 | make the real symbol dynamic. */ | |
5237 | if (h != hi && hi->forced_local) | |
5238 | ; | |
5239 | else if (!dynamic) | |
5240 | { | |
5241 | if (bfd_link_dll (info) | |
5242 | || h->def_dynamic | |
5243 | || h->ref_dynamic) | |
0a1b45a2 | 5244 | dynsym = true; |
b1a92c63 AM |
5245 | } |
5246 | else | |
5247 | { | |
5248 | if (h->def_regular | |
5249 | || h->ref_regular | |
5250 | || (h->is_weakalias | |
5251 | && weakdef (h)->dynindx != -1)) | |
0a1b45a2 | 5252 | dynsym = true; |
4f3fedcf AM |
5253 | } |
5254 | ||
5255 | /* Check to see if we need to add an indirect symbol for | |
5256 | the default name. */ | |
726d7d1e AM |
5257 | if ((definition |
5258 | || (!override && h->root.type == bfd_link_hash_common)) | |
5259 | && !(hi != h | |
5260 | && hi->versioned == versioned_hidden)) | |
4f3fedcf | 5261 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, |
f01fb44c | 5262 | sec, value, &old_bfd, &dynsym)) |
4f3fedcf | 5263 | goto error_free_vers; |
4ad4eba5 AM |
5264 | |
5265 | /* Check the alignment when a common symbol is involved. This | |
5266 | can change when a common symbol is overridden by a normal | |
5267 | definition or a common symbol is ignored due to the old | |
5268 | normal definition. We need to make sure the maximum | |
5269 | alignment is maintained. */ | |
a4d8e49b | 5270 | if ((old_alignment || common) |
4ad4eba5 AM |
5271 | && h->root.type != bfd_link_hash_common) |
5272 | { | |
5273 | unsigned int common_align; | |
5274 | unsigned int normal_align; | |
5275 | unsigned int symbol_align; | |
5276 | bfd *normal_bfd; | |
5277 | bfd *common_bfd; | |
5278 | ||
3a81e825 AM |
5279 | BFD_ASSERT (h->root.type == bfd_link_hash_defined |
5280 | || h->root.type == bfd_link_hash_defweak); | |
5281 | ||
4ad4eba5 AM |
5282 | symbol_align = ffs (h->root.u.def.value) - 1; |
5283 | if (h->root.u.def.section->owner != NULL | |
0616a280 AM |
5284 | && (h->root.u.def.section->owner->flags |
5285 | & (DYNAMIC | BFD_PLUGIN)) == 0) | |
4ad4eba5 AM |
5286 | { |
5287 | normal_align = h->root.u.def.section->alignment_power; | |
5288 | if (normal_align > symbol_align) | |
5289 | normal_align = symbol_align; | |
5290 | } | |
5291 | else | |
5292 | normal_align = symbol_align; | |
5293 | ||
5294 | if (old_alignment) | |
5295 | { | |
5296 | common_align = old_alignment; | |
5297 | common_bfd = old_bfd; | |
5298 | normal_bfd = abfd; | |
5299 | } | |
5300 | else | |
5301 | { | |
5302 | common_align = bfd_log2 (isym->st_value); | |
5303 | common_bfd = abfd; | |
5304 | normal_bfd = old_bfd; | |
5305 | } | |
5306 | ||
5307 | if (normal_align < common_align) | |
d07676f8 NC |
5308 | { |
5309 | /* PR binutils/2735 */ | |
5310 | if (normal_bfd == NULL) | |
4eca0228 | 5311 | _bfd_error_handler |
695344c0 | 5312 | /* xgettext:c-format */ |
9793eb77 | 5313 | (_("warning: alignment %u of common symbol `%s' in %pB is" |
871b3ab2 | 5314 | " greater than the alignment (%u) of its section %pA"), |
c08bb8dd AM |
5315 | 1 << common_align, name, common_bfd, |
5316 | 1 << normal_align, h->root.u.def.section); | |
d07676f8 | 5317 | else |
4eca0228 | 5318 | _bfd_error_handler |
695344c0 | 5319 | /* xgettext:c-format */ |
064ad3ea NC |
5320 | (_("warning: alignment %u of normal symbol `%s' in %pB" |
5321 | " is smaller than %u used by the common definition in %pB"), | |
c08bb8dd AM |
5322 | 1 << normal_align, name, normal_bfd, |
5323 | 1 << common_align, common_bfd); | |
064ad3ea NC |
5324 | |
5325 | /* PR 30499: make sure that users understand that this warning is serious. */ | |
5326 | _bfd_error_handler | |
5327 | (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised.")); | |
d07676f8 | 5328 | } |
4ad4eba5 AM |
5329 | } |
5330 | ||
83ad0046 | 5331 | /* Remember the symbol size if it isn't undefined. */ |
3a81e825 AM |
5332 | if (isym->st_size != 0 |
5333 | && isym->st_shndx != SHN_UNDEF | |
4ad4eba5 AM |
5334 | && (definition || h->size == 0)) |
5335 | { | |
83ad0046 L |
5336 | if (h->size != 0 |
5337 | && h->size != isym->st_size | |
5338 | && ! size_change_ok) | |
064ad3ea NC |
5339 | { |
5340 | _bfd_error_handler | |
5341 | /* xgettext:c-format */ | |
5342 | (_("warning: size of symbol `%s' changed" | |
5343 | " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"), | |
5344 | name, (uint64_t) h->size, old_bfd, | |
5345 | (uint64_t) isym->st_size, abfd); | |
5346 | ||
5347 | /* PR 30499: make sure that users understand that this warning is serious. */ | |
5348 | _bfd_error_handler | |
5349 | (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised.")); | |
5350 | } | |
4ad4eba5 AM |
5351 | |
5352 | h->size = isym->st_size; | |
5353 | } | |
5354 | ||
5355 | /* If this is a common symbol, then we always want H->SIZE | |
5356 | to be the size of the common symbol. The code just above | |
5357 | won't fix the size if a common symbol becomes larger. We | |
5358 | don't warn about a size change here, because that is | |
4f3fedcf | 5359 | covered by --warn-common. Allow changes between different |
fcb93ecf | 5360 | function types. */ |
4ad4eba5 AM |
5361 | if (h->root.type == bfd_link_hash_common) |
5362 | h->size = h->root.u.c.size; | |
5363 | ||
5364 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
37a9e49a L |
5365 | && ((definition && !new_weak) |
5366 | || (old_weak && h->root.type == bfd_link_hash_common) | |
5367 | || h->type == STT_NOTYPE)) | |
4ad4eba5 | 5368 | { |
2955ec4c L |
5369 | unsigned int type = ELF_ST_TYPE (isym->st_info); |
5370 | ||
5371 | /* Turn an IFUNC symbol from a DSO into a normal FUNC | |
5372 | symbol. */ | |
5373 | if (type == STT_GNU_IFUNC | |
5374 | && (abfd->flags & DYNAMIC) != 0) | |
5375 | type = STT_FUNC; | |
4ad4eba5 | 5376 | |
2955ec4c L |
5377 | if (h->type != type) |
5378 | { | |
5379 | if (h->type != STT_NOTYPE && ! type_change_ok) | |
695344c0 | 5380 | /* xgettext:c-format */ |
4eca0228 | 5381 | _bfd_error_handler |
9793eb77 | 5382 | (_("warning: type of symbol `%s' changed" |
871b3ab2 | 5383 | " from %d to %d in %pB"), |
c08bb8dd | 5384 | name, h->type, type, abfd); |
2955ec4c L |
5385 | |
5386 | h->type = type; | |
5387 | } | |
4ad4eba5 AM |
5388 | } |
5389 | ||
54ac0771 | 5390 | /* Merge st_other field. */ |
5160d0f3 AM |
5391 | elf_merge_st_other (abfd, h, isym->st_other, sec, |
5392 | definition, dynamic); | |
4ad4eba5 | 5393 | |
c3df8c14 | 5394 | /* We don't want to make debug symbol dynamic. */ |
0e1862bb L |
5395 | if (definition |
5396 | && (sec->flags & SEC_DEBUGGING) | |
5397 | && !bfd_link_relocatable (info)) | |
0a1b45a2 | 5398 | dynsym = false; |
c3df8c14 | 5399 | |
b1a92c63 AM |
5400 | /* Nor should we make plugin symbols dynamic. */ |
5401 | if ((abfd->flags & BFD_PLUGIN) != 0) | |
0a1b45a2 | 5402 | dynsym = false; |
b1a92c63 | 5403 | |
35fc36a8 | 5404 | if (definition) |
35399224 L |
5405 | { |
5406 | h->target_internal = isym->st_target_internal; | |
5407 | h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; | |
5408 | } | |
35fc36a8 | 5409 | |
79cfb928 L |
5410 | /* Don't add indirect symbols for .symver x, x@FOO aliases |
5411 | in IR. Since all data or text symbols in IR have the | |
5412 | same type, value and section, we can't tell if a symbol | |
5413 | is an alias of another symbol by their types, values and | |
5414 | sections. */ | |
5415 | if (definition | |
5416 | && !dynamic | |
5417 | && (abfd->flags & BFD_PLUGIN) == 0) | |
4ad4eba5 AM |
5418 | { |
5419 | char *p = strchr (name, ELF_VER_CHR); | |
5420 | if (p != NULL && p[1] != ELF_VER_CHR) | |
5421 | { | |
5422 | /* Queue non-default versions so that .symver x, x@FOO | |
5423 | aliases can be checked. */ | |
66eb6687 | 5424 | if (!nondeflt_vers) |
4ad4eba5 | 5425 | { |
986f0783 AM |
5426 | size_t amt = ((isymend - isym + 1) |
5427 | * sizeof (struct elf_link_hash_entry *)); | |
ca4be51c AM |
5428 | nondeflt_vers |
5429 | = (struct elf_link_hash_entry **) bfd_malloc (amt); | |
14b1c01e AM |
5430 | if (!nondeflt_vers) |
5431 | goto error_free_vers; | |
4ad4eba5 | 5432 | } |
66eb6687 | 5433 | nondeflt_vers[nondeflt_vers_cnt++] = h; |
4ad4eba5 AM |
5434 | } |
5435 | } | |
5436 | ||
b1a92c63 | 5437 | if (dynsym && h->dynindx == -1) |
4ad4eba5 | 5438 | { |
c152c796 | 5439 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 5440 | goto error_free_vers; |
60d67dc8 AM |
5441 | if (h->is_weakalias |
5442 | && weakdef (h)->dynindx == -1) | |
4ad4eba5 | 5443 | { |
60d67dc8 | 5444 | if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) |
4ad4eba5 AM |
5445 | goto error_free_vers; |
5446 | } | |
5447 | } | |
1f599d0e | 5448 | else if (h->dynindx != -1) |
4ad4eba5 AM |
5449 | /* If the symbol already has a dynamic index, but |
5450 | visibility says it should not be visible, turn it into | |
5451 | a local symbol. */ | |
5452 | switch (ELF_ST_VISIBILITY (h->other)) | |
5453 | { | |
5454 | case STV_INTERNAL: | |
5455 | case STV_HIDDEN: | |
0a1b45a2 AM |
5456 | (*bed->elf_backend_hide_symbol) (info, h, true); |
5457 | dynsym = false; | |
4ad4eba5 AM |
5458 | break; |
5459 | } | |
5460 | ||
5461 | if (!add_needed | |
aef28989 | 5462 | && matched |
4ad4eba5 | 5463 | && definition |
f01fb44c | 5464 | && h->root.type != bfd_link_hash_indirect |
010e5ae2 | 5465 | && ((dynsym |
a896df97 | 5466 | && h->ref_regular_nonweak) |
b1a92c63 AM |
5467 | || (old_bfd != NULL |
5468 | && (old_bfd->flags & BFD_PLUGIN) != 0 | |
bbaddd4b AM |
5469 | && h->ref_ir_nonweak |
5470 | && !info->lto_all_symbols_read) | |
ffa9430d | 5471 | || (h->ref_dynamic_nonweak |
010e5ae2 | 5472 | && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 |
7b15fa7a AM |
5473 | && !on_needed_list (elf_dt_name (abfd), |
5474 | htab->needed, NULL)))) | |
4ad4eba5 | 5475 | { |
4ad4eba5 AM |
5476 | const char *soname = elf_dt_name (abfd); |
5477 | ||
16e4ecc0 AM |
5478 | info->callbacks->minfo ("%!", soname, old_bfd, |
5479 | h->root.root.string); | |
5480 | ||
4ad4eba5 AM |
5481 | /* A symbol from a library loaded via DT_NEEDED of some |
5482 | other library is referenced by a regular object. | |
e56f61be | 5483 | Add a DT_NEEDED entry for it. Issue an error if |
b918acf9 NC |
5484 | --no-add-needed is used and the reference was not |
5485 | a weak one. */ | |
4f3fedcf | 5486 | if (old_bfd != NULL |
b918acf9 | 5487 | && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) |
e56f61be | 5488 | { |
4eca0228 | 5489 | _bfd_error_handler |
695344c0 | 5490 | /* xgettext:c-format */ |
871b3ab2 | 5491 | (_("%pB: undefined reference to symbol '%s'"), |
4f3fedcf | 5492 | old_bfd, name); |
ff5ac77b | 5493 | bfd_set_error (bfd_error_missing_dso); |
e56f61be L |
5494 | goto error_free_vers; |
5495 | } | |
5496 | ||
a50b1753 | 5497 | elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) |
ca4be51c | 5498 | (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); |
a5db907e | 5499 | |
e310298c AM |
5500 | /* Create dynamic sections for backends that require |
5501 | that be done before setup_gnu_properties. */ | |
5502 | if (!_bfd_elf_link_create_dynamic_sections (abfd, info)) | |
0a1b45a2 AM |
5503 | return false; |
5504 | add_needed = true; | |
4ad4eba5 AM |
5505 | } |
5506 | } | |
5507 | } | |
5508 | ||
a83ef4d1 L |
5509 | if (info->lto_plugin_active |
5510 | && !bfd_link_relocatable (info) | |
5511 | && (abfd->flags & BFD_PLUGIN) == 0 | |
5512 | && !just_syms | |
5513 | && extsymcount) | |
5514 | { | |
5515 | int r_sym_shift; | |
5516 | ||
5517 | if (bed->s->arch_size == 32) | |
5518 | r_sym_shift = 8; | |
5519 | else | |
5520 | r_sym_shift = 32; | |
5521 | ||
5522 | /* If linker plugin is enabled, set non_ir_ref_regular on symbols | |
5523 | referenced in regular objects so that linker plugin will get | |
5524 | the correct symbol resolution. */ | |
5525 | ||
5526 | sym_hash = elf_sym_hashes (abfd); | |
5527 | for (s = abfd->sections; s != NULL; s = s->next) | |
5528 | { | |
5529 | Elf_Internal_Rela *internal_relocs; | |
5530 | Elf_Internal_Rela *rel, *relend; | |
5531 | ||
5532 | /* Don't check relocations in excluded sections. */ | |
5533 | if ((s->flags & SEC_RELOC) == 0 | |
5534 | || s->reloc_count == 0 | |
5535 | || (s->flags & SEC_EXCLUDE) != 0 | |
5536 | || ((info->strip == strip_all | |
5537 | || info->strip == strip_debugger) | |
5538 | && (s->flags & SEC_DEBUGGING) != 0)) | |
5539 | continue; | |
5540 | ||
a8dde0a2 L |
5541 | internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info, |
5542 | s, NULL, | |
5543 | NULL, | |
5544 | _bfd_link_keep_memory (info)); | |
a83ef4d1 L |
5545 | if (internal_relocs == NULL) |
5546 | goto error_free_vers; | |
5547 | ||
5548 | rel = internal_relocs; | |
5549 | relend = rel + s->reloc_count; | |
5550 | for ( ; rel < relend; rel++) | |
5551 | { | |
5552 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
5553 | struct elf_link_hash_entry *h; | |
5554 | ||
5555 | /* Skip local symbols. */ | |
5556 | if (r_symndx < extsymoff) | |
5557 | continue; | |
5558 | ||
5559 | h = sym_hash[r_symndx - extsymoff]; | |
5560 | if (h != NULL) | |
5561 | h->root.non_ir_ref_regular = 1; | |
5562 | } | |
5563 | ||
5564 | if (elf_section_data (s)->relocs != internal_relocs) | |
5565 | free (internal_relocs); | |
5566 | } | |
5567 | } | |
5568 | ||
c9594989 AM |
5569 | free (extversym); |
5570 | extversym = NULL; | |
5571 | free (isymbuf); | |
5572 | isymbuf = NULL; | |
66eb6687 AM |
5573 | |
5574 | if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) | |
5575 | { | |
5576 | unsigned int i; | |
5577 | ||
5578 | /* Restore the symbol table. */ | |
f45794cb AM |
5579 | old_ent = (char *) old_tab + tabsize; |
5580 | memset (elf_sym_hashes (abfd), 0, | |
5581 | extsymcount * sizeof (struct elf_link_hash_entry *)); | |
4f87808c AM |
5582 | htab->root.table.table = old_table; |
5583 | htab->root.table.size = old_size; | |
5584 | htab->root.table.count = old_count; | |
66eb6687 | 5585 | memcpy (htab->root.table.table, old_tab, tabsize); |
66eb6687 AM |
5586 | htab->root.undefs = old_undefs; |
5587 | htab->root.undefs_tail = old_undefs_tail; | |
e310298c AM |
5588 | if (htab->dynstr != NULL) |
5589 | _bfd_elf_strtab_restore (htab->dynstr, old_strtab); | |
5b677558 AM |
5590 | free (old_strtab); |
5591 | old_strtab = NULL; | |
66eb6687 AM |
5592 | for (i = 0; i < htab->root.table.size; i++) |
5593 | { | |
5594 | struct bfd_hash_entry *p; | |
5595 | struct elf_link_hash_entry *h; | |
4070765b | 5596 | unsigned int non_ir_ref_dynamic; |
66eb6687 AM |
5597 | |
5598 | for (p = htab->root.table.table[i]; p != NULL; p = p->next) | |
5599 | { | |
4070765b | 5600 | /* Preserve non_ir_ref_dynamic so that this symbol |
59fa66c5 L |
5601 | will be exported when the dynamic lib becomes needed |
5602 | in the second pass. */ | |
7ba11550 AM |
5603 | h = (struct elf_link_hash_entry *) p; |
5604 | if (h->root.type == bfd_link_hash_warning) | |
5605 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4070765b | 5606 | non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; |
7ba11550 | 5607 | |
2de92251 | 5608 | h = (struct elf_link_hash_entry *) p; |
7ba11550 AM |
5609 | memcpy (h, old_ent, htab->root.table.entsize); |
5610 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 AM |
5611 | if (h->root.type == bfd_link_hash_warning) |
5612 | { | |
a4542f1b | 5613 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
7ba11550 AM |
5614 | memcpy (h, old_ent, htab->root.table.entsize); |
5615 | old_ent = (char *) old_ent + htab->root.table.entsize; | |
2de92251 | 5616 | } |
a4542f1b | 5617 | if (h->root.type == bfd_link_hash_common) |
3e0882af | 5618 | { |
7ba11550 AM |
5619 | memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p)); |
5620 | old_ent = (char *) old_ent + sizeof (*h->root.u.c.p); | |
3e0882af | 5621 | } |
4070765b | 5622 | h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; |
66eb6687 AM |
5623 | } |
5624 | } | |
5625 | ||
5061a885 AM |
5626 | /* Make a special call to the linker "notice" function to |
5627 | tell it that symbols added for crefs may need to be removed. */ | |
e5034e59 | 5628 | if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) |
9af2a943 | 5629 | goto error_free_vers; |
5061a885 | 5630 | |
66eb6687 AM |
5631 | free (old_tab); |
5632 | objalloc_free_block ((struct objalloc *) htab->root.table.memory, | |
5633 | alloc_mark); | |
c9594989 | 5634 | free (nondeflt_vers); |
0a1b45a2 | 5635 | return true; |
66eb6687 | 5636 | } |
2de92251 | 5637 | |
66eb6687 AM |
5638 | if (old_tab != NULL) |
5639 | { | |
e5034e59 | 5640 | if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) |
9af2a943 | 5641 | goto error_free_vers; |
66eb6687 AM |
5642 | free (old_tab); |
5643 | old_tab = NULL; | |
5644 | } | |
5645 | ||
c6e8a9a8 L |
5646 | /* Now that all the symbols from this input file are created, if |
5647 | not performing a relocatable link, handle .symver foo, foo@BAR | |
5648 | such that any relocs against foo become foo@BAR. */ | |
0e1862bb | 5649 | if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) |
4ad4eba5 | 5650 | { |
ef53be89 | 5651 | size_t cnt, symidx; |
4ad4eba5 AM |
5652 | |
5653 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
5654 | { | |
5655 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
5656 | char *shortname, *p; | |
986f0783 | 5657 | size_t amt; |
4ad4eba5 AM |
5658 | |
5659 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
5660 | if (p == NULL | |
5661 | || (h->root.type != bfd_link_hash_defined | |
5662 | && h->root.type != bfd_link_hash_defweak)) | |
5663 | continue; | |
5664 | ||
5665 | amt = p - h->root.root.string; | |
a50b1753 | 5666 | shortname = (char *) bfd_malloc (amt + 1); |
14b1c01e AM |
5667 | if (!shortname) |
5668 | goto error_free_vers; | |
4ad4eba5 AM |
5669 | memcpy (shortname, h->root.root.string, amt); |
5670 | shortname[amt] = '\0'; | |
5671 | ||
5672 | hi = (struct elf_link_hash_entry *) | |
66eb6687 | 5673 | bfd_link_hash_lookup (&htab->root, shortname, |
0a1b45a2 | 5674 | false, false, false); |
4ad4eba5 AM |
5675 | if (hi != NULL |
5676 | && hi->root.type == h->root.type | |
5677 | && hi->root.u.def.value == h->root.u.def.value | |
5678 | && hi->root.u.def.section == h->root.u.def.section) | |
5679 | { | |
0a1b45a2 | 5680 | (*bed->elf_backend_hide_symbol) (info, hi, true); |
4ad4eba5 AM |
5681 | hi->root.type = bfd_link_hash_indirect; |
5682 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
fcfa13d2 | 5683 | (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); |
4ad4eba5 AM |
5684 | sym_hash = elf_sym_hashes (abfd); |
5685 | if (sym_hash) | |
5686 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
5687 | if (sym_hash[symidx] == hi) | |
5688 | { | |
5689 | sym_hash[symidx] = h; | |
5690 | break; | |
5691 | } | |
5692 | } | |
5693 | free (shortname); | |
5694 | } | |
5695 | free (nondeflt_vers); | |
5696 | nondeflt_vers = NULL; | |
5697 | } | |
5698 | ||
60d67dc8 | 5699 | /* Now set the alias field correctly for all the weak defined |
4ad4eba5 AM |
5700 | symbols we found. The only way to do this is to search all the |
5701 | symbols. Since we only need the information for non functions in | |
5702 | dynamic objects, that's the only time we actually put anything on | |
5703 | the list WEAKS. We need this information so that if a regular | |
5704 | object refers to a symbol defined weakly in a dynamic object, the | |
5705 | real symbol in the dynamic object is also put in the dynamic | |
5706 | symbols; we also must arrange for both symbols to point to the | |
5707 | same memory location. We could handle the general case of symbol | |
5708 | aliasing, but a general symbol alias can only be generated in | |
5709 | assembler code, handling it correctly would be very time | |
5710 | consuming, and other ELF linkers don't handle general aliasing | |
5711 | either. */ | |
5712 | if (weaks != NULL) | |
5713 | { | |
5714 | struct elf_link_hash_entry **hpp; | |
5715 | struct elf_link_hash_entry **hppend; | |
5716 | struct elf_link_hash_entry **sorted_sym_hash; | |
5717 | struct elf_link_hash_entry *h; | |
986f0783 | 5718 | size_t sym_count, amt; |
4ad4eba5 AM |
5719 | |
5720 | /* Since we have to search the whole symbol list for each weak | |
5721 | defined symbol, search time for N weak defined symbols will be | |
5722 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
986f0783 | 5723 | amt = extsymcount * sizeof (*sorted_sym_hash); |
3a3f4bf7 | 5724 | sorted_sym_hash = bfd_malloc (amt); |
4ad4eba5 AM |
5725 | if (sorted_sym_hash == NULL) |
5726 | goto error_return; | |
5727 | sym_hash = sorted_sym_hash; | |
5728 | hpp = elf_sym_hashes (abfd); | |
5729 | hppend = hpp + extsymcount; | |
5730 | sym_count = 0; | |
5731 | for (; hpp < hppend; hpp++) | |
5732 | { | |
5733 | h = *hpp; | |
5734 | if (h != NULL | |
5735 | && h->root.type == bfd_link_hash_defined | |
fcb93ecf | 5736 | && !bed->is_function_type (h->type)) |
4ad4eba5 AM |
5737 | { |
5738 | *sym_hash = h; | |
5739 | sym_hash++; | |
5740 | sym_count++; | |
5741 | } | |
5742 | } | |
5743 | ||
3a3f4bf7 | 5744 | qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash), |
4ad4eba5 AM |
5745 | elf_sort_symbol); |
5746 | ||
5747 | while (weaks != NULL) | |
5748 | { | |
5749 | struct elf_link_hash_entry *hlook; | |
5750 | asection *slook; | |
5751 | bfd_vma vlook; | |
ed54588d | 5752 | size_t i, j, idx = 0; |
4ad4eba5 AM |
5753 | |
5754 | hlook = weaks; | |
60d67dc8 AM |
5755 | weaks = hlook->u.alias; |
5756 | hlook->u.alias = NULL; | |
4ad4eba5 | 5757 | |
e3e53eed AM |
5758 | if (hlook->root.type != bfd_link_hash_defined |
5759 | && hlook->root.type != bfd_link_hash_defweak) | |
5760 | continue; | |
5761 | ||
4ad4eba5 AM |
5762 | slook = hlook->root.u.def.section; |
5763 | vlook = hlook->root.u.def.value; | |
5764 | ||
4ad4eba5 AM |
5765 | i = 0; |
5766 | j = sym_count; | |
14160578 | 5767 | while (i != j) |
4ad4eba5 AM |
5768 | { |
5769 | bfd_signed_vma vdiff; | |
5770 | idx = (i + j) / 2; | |
14160578 | 5771 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5772 | vdiff = vlook - h->root.u.def.value; |
5773 | if (vdiff < 0) | |
5774 | j = idx; | |
5775 | else if (vdiff > 0) | |
5776 | i = idx + 1; | |
5777 | else | |
5778 | { | |
d3435ae8 | 5779 | int sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
5780 | if (sdiff < 0) |
5781 | j = idx; | |
5782 | else if (sdiff > 0) | |
5783 | i = idx + 1; | |
5784 | else | |
14160578 | 5785 | break; |
4ad4eba5 AM |
5786 | } |
5787 | } | |
5788 | ||
5789 | /* We didn't find a value/section match. */ | |
14160578 | 5790 | if (i == j) |
4ad4eba5 AM |
5791 | continue; |
5792 | ||
14160578 AM |
5793 | /* With multiple aliases, or when the weak symbol is already |
5794 | strongly defined, we have multiple matching symbols and | |
5795 | the binary search above may land on any of them. Step | |
5796 | one past the matching symbol(s). */ | |
5797 | while (++idx != j) | |
5798 | { | |
5799 | h = sorted_sym_hash[idx]; | |
5800 | if (h->root.u.def.section != slook | |
5801 | || h->root.u.def.value != vlook) | |
5802 | break; | |
5803 | } | |
5804 | ||
5805 | /* Now look back over the aliases. Since we sorted by size | |
5806 | as well as value and section, we'll choose the one with | |
5807 | the largest size. */ | |
5808 | while (idx-- != i) | |
4ad4eba5 | 5809 | { |
14160578 | 5810 | h = sorted_sym_hash[idx]; |
4ad4eba5 AM |
5811 | |
5812 | /* Stop if value or section doesn't match. */ | |
14160578 AM |
5813 | if (h->root.u.def.section != slook |
5814 | || h->root.u.def.value != vlook) | |
4ad4eba5 AM |
5815 | break; |
5816 | else if (h != hlook) | |
5817 | { | |
60d67dc8 AM |
5818 | struct elf_link_hash_entry *t; |
5819 | ||
5820 | hlook->u.alias = h; | |
5821 | hlook->is_weakalias = 1; | |
5822 | t = h; | |
5823 | if (t->u.alias != NULL) | |
5824 | while (t->u.alias != h) | |
5825 | t = t->u.alias; | |
5826 | t->u.alias = hlook; | |
4ad4eba5 AM |
5827 | |
5828 | /* If the weak definition is in the list of dynamic | |
5829 | symbols, make sure the real definition is put | |
5830 | there as well. */ | |
5831 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
5832 | { | |
c152c796 | 5833 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4dd07732 AM |
5834 | { |
5835 | err_free_sym_hash: | |
5836 | free (sorted_sym_hash); | |
5837 | goto error_return; | |
5838 | } | |
4ad4eba5 AM |
5839 | } |
5840 | ||
5841 | /* If the real definition is in the list of dynamic | |
5842 | symbols, make sure the weak definition is put | |
5843 | there as well. If we don't do this, then the | |
5844 | dynamic loader might not merge the entries for the | |
5845 | real definition and the weak definition. */ | |
5846 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
5847 | { | |
c152c796 | 5848 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4dd07732 | 5849 | goto err_free_sym_hash; |
4ad4eba5 AM |
5850 | } |
5851 | break; | |
5852 | } | |
5853 | } | |
5854 | } | |
5855 | ||
5856 | free (sorted_sym_hash); | |
5857 | } | |
5858 | ||
33177bb1 AM |
5859 | if (bed->check_directives |
5860 | && !(*bed->check_directives) (abfd, info)) | |
0a1b45a2 | 5861 | return false; |
85fbca6a | 5862 | |
4ad4eba5 AM |
5863 | /* If this is a non-traditional link, try to optimize the handling |
5864 | of the .stab/.stabstr sections. */ | |
5865 | if (! dynamic | |
5866 | && ! info->traditional_format | |
2cc15b10 | 5867 | && is_elf_hash_table (&htab->root) |
4ad4eba5 AM |
5868 | && (info->strip != strip_all && info->strip != strip_debugger)) |
5869 | { | |
5870 | asection *stabstr; | |
5871 | ||
5872 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
5873 | if (stabstr != NULL) | |
5874 | { | |
5875 | bfd_size_type string_offset = 0; | |
5876 | asection *stab; | |
5877 | ||
5878 | for (stab = abfd->sections; stab; stab = stab->next) | |
08dedd66 | 5879 | if (startswith (stab->name, ".stab") |
4ad4eba5 AM |
5880 | && (!stab->name[5] || |
5881 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
5882 | && (stab->flags & SEC_MERGE) == 0 | |
5883 | && !bfd_is_abs_section (stab->output_section)) | |
5884 | { | |
5885 | struct bfd_elf_section_data *secdata; | |
5886 | ||
5887 | secdata = elf_section_data (stab); | |
66eb6687 AM |
5888 | if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, |
5889 | stabstr, &secdata->sec_info, | |
4ad4eba5 AM |
5890 | &string_offset)) |
5891 | goto error_return; | |
5892 | if (secdata->sec_info) | |
dbaa2011 | 5893 | stab->sec_info_type = SEC_INFO_TYPE_STABS; |
4ad4eba5 AM |
5894 | } |
5895 | } | |
5896 | } | |
5897 | ||
e310298c | 5898 | if (dynamic && add_needed) |
4ad4eba5 AM |
5899 | { |
5900 | /* Add this bfd to the loaded list. */ | |
5901 | struct elf_link_loaded_list *n; | |
5902 | ||
ca4be51c | 5903 | n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); |
4ad4eba5 AM |
5904 | if (n == NULL) |
5905 | goto error_return; | |
5906 | n->abfd = abfd; | |
e310298c AM |
5907 | n->next = htab->dyn_loaded; |
5908 | htab->dyn_loaded = n; | |
4ad4eba5 | 5909 | } |
e310298c AM |
5910 | if (dynamic && !add_needed |
5911 | && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0) | |
5912 | elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED; | |
4ad4eba5 | 5913 | |
0a1b45a2 | 5914 | return true; |
4ad4eba5 AM |
5915 | |
5916 | error_free_vers: | |
c9594989 AM |
5917 | free (old_tab); |
5918 | free (old_strtab); | |
5919 | free (nondeflt_vers); | |
5920 | free (extversym); | |
4ad4eba5 | 5921 | error_free_sym: |
c9594989 | 5922 | free (isymbuf); |
4ad4eba5 | 5923 | error_return: |
0a1b45a2 | 5924 | return false; |
4ad4eba5 AM |
5925 | } |
5926 | ||
8387904d AM |
5927 | /* Return the linker hash table entry of a symbol that might be |
5928 | satisfied by an archive symbol. Return -1 on error. */ | |
5929 | ||
b585e899 | 5930 | struct bfd_link_hash_entry * |
8387904d AM |
5931 | _bfd_elf_archive_symbol_lookup (bfd *abfd, |
5932 | struct bfd_link_info *info, | |
5933 | const char *name) | |
5934 | { | |
b585e899 | 5935 | struct bfd_link_hash_entry *h; |
8387904d AM |
5936 | char *p, *copy; |
5937 | size_t len, first; | |
5938 | ||
b585e899 | 5939 | h = bfd_link_hash_lookup (info->hash, name, false, false, true); |
8387904d AM |
5940 | if (h != NULL) |
5941 | return h; | |
5942 | ||
5943 | /* If this is a default version (the name contains @@), look up the | |
5944 | symbol again with only one `@' as well as without the version. | |
5945 | The effect is that references to the symbol with and without the | |
5946 | version will be matched by the default symbol in the archive. */ | |
5947 | ||
5948 | p = strchr (name, ELF_VER_CHR); | |
5949 | if (p == NULL || p[1] != ELF_VER_CHR) | |
5950 | return h; | |
5951 | ||
5952 | /* First check with only one `@'. */ | |
5953 | len = strlen (name); | |
a50b1753 | 5954 | copy = (char *) bfd_alloc (abfd, len); |
8387904d | 5955 | if (copy == NULL) |
b585e899 | 5956 | return (struct bfd_link_hash_entry *) -1; |
8387904d AM |
5957 | |
5958 | first = p - name + 1; | |
5959 | memcpy (copy, name, first); | |
5960 | memcpy (copy + first, name + first + 1, len - first); | |
5961 | ||
b585e899 | 5962 | h = bfd_link_hash_lookup (info->hash, copy, false, false, true); |
8387904d AM |
5963 | if (h == NULL) |
5964 | { | |
5965 | /* We also need to check references to the symbol without the | |
5966 | version. */ | |
5967 | copy[first - 1] = '\0'; | |
b585e899 | 5968 | h = bfd_link_hash_lookup (info->hash, copy, false, false, true); |
8387904d AM |
5969 | } |
5970 | ||
5971 | bfd_release (abfd, copy); | |
5972 | return h; | |
5973 | } | |
5974 | ||
0ad989f9 | 5975 | /* Add symbols from an ELF archive file to the linker hash table. We |
13e570f8 AM |
5976 | don't use _bfd_generic_link_add_archive_symbols because we need to |
5977 | handle versioned symbols. | |
0ad989f9 L |
5978 | |
5979 | Fortunately, ELF archive handling is simpler than that done by | |
5980 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
5981 | oddities. In ELF, if we find a symbol in the archive map, and the | |
5982 | symbol is currently undefined, we know that we must pull in that | |
5983 | object file. | |
5984 | ||
5985 | Unfortunately, we do have to make multiple passes over the symbol | |
5986 | table until nothing further is resolved. */ | |
5987 | ||
0a1b45a2 | 5988 | static bool |
4ad4eba5 | 5989 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) |
0ad989f9 L |
5990 | { |
5991 | symindex c; | |
13e570f8 | 5992 | unsigned char *included = NULL; |
0ad989f9 | 5993 | carsym *symdefs; |
0a1b45a2 | 5994 | bool loop; |
986f0783 | 5995 | size_t amt; |
8387904d | 5996 | const struct elf_backend_data *bed; |
b585e899 | 5997 | struct bfd_link_hash_entry * (*archive_symbol_lookup) |
8387904d | 5998 | (bfd *, struct bfd_link_info *, const char *); |
0ad989f9 L |
5999 | |
6000 | if (! bfd_has_map (abfd)) | |
6001 | { | |
6002 | /* An empty archive is a special case. */ | |
6003 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
0a1b45a2 | 6004 | return true; |
0ad989f9 | 6005 | bfd_set_error (bfd_error_no_armap); |
0a1b45a2 | 6006 | return false; |
0ad989f9 L |
6007 | } |
6008 | ||
6009 | /* Keep track of all symbols we know to be already defined, and all | |
6010 | files we know to be already included. This is to speed up the | |
6011 | second and subsequent passes. */ | |
6012 | c = bfd_ardata (abfd)->symdef_count; | |
6013 | if (c == 0) | |
0a1b45a2 | 6014 | return true; |
986f0783 | 6015 | amt = c * sizeof (*included); |
13e570f8 AM |
6016 | included = (unsigned char *) bfd_zmalloc (amt); |
6017 | if (included == NULL) | |
0a1b45a2 | 6018 | return false; |
0ad989f9 L |
6019 | |
6020 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
6021 | bed = get_elf_backend_data (abfd); |
6022 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
6023 | |
6024 | do | |
6025 | { | |
6026 | file_ptr last; | |
6027 | symindex i; | |
6028 | carsym *symdef; | |
6029 | carsym *symdefend; | |
6030 | ||
0a1b45a2 | 6031 | loop = false; |
0ad989f9 L |
6032 | last = -1; |
6033 | ||
6034 | symdef = symdefs; | |
6035 | symdefend = symdef + c; | |
6036 | for (i = 0; symdef < symdefend; symdef++, i++) | |
6037 | { | |
b585e899 | 6038 | struct bfd_link_hash_entry *h; |
0ad989f9 L |
6039 | bfd *element; |
6040 | struct bfd_link_hash_entry *undefs_tail; | |
6041 | symindex mark; | |
6042 | ||
13e570f8 | 6043 | if (included[i]) |
0ad989f9 L |
6044 | continue; |
6045 | if (symdef->file_offset == last) | |
6046 | { | |
0a1b45a2 | 6047 | included[i] = true; |
0ad989f9 L |
6048 | continue; |
6049 | } | |
6050 | ||
8387904d | 6051 | h = archive_symbol_lookup (abfd, info, symdef->name); |
b585e899 | 6052 | if (h == (struct bfd_link_hash_entry *) -1) |
8387904d | 6053 | goto error_return; |
0ad989f9 L |
6054 | |
6055 | if (h == NULL) | |
6056 | continue; | |
6057 | ||
b585e899 | 6058 | if (h->type == bfd_link_hash_undefined) |
75cfe082 AM |
6059 | { |
6060 | /* If the archive element has already been loaded then one | |
6061 | of the symbols defined by that element might have been | |
6062 | made undefined due to being in a discarded section. */ | |
b585e899 AM |
6063 | if (is_elf_hash_table (info->hash) |
6064 | && ((struct elf_link_hash_entry *) h)->indx == -3) | |
75cfe082 AM |
6065 | continue; |
6066 | } | |
b585e899 | 6067 | else if (h->type == bfd_link_hash_common) |
0ad989f9 L |
6068 | { |
6069 | /* We currently have a common symbol. The archive map contains | |
6070 | a reference to this symbol, so we may want to include it. We | |
6071 | only want to include it however, if this archive element | |
6072 | contains a definition of the symbol, not just another common | |
6073 | declaration of it. | |
6074 | ||
6075 | Unfortunately some archivers (including GNU ar) will put | |
6076 | declarations of common symbols into their archive maps, as | |
6077 | well as real definitions, so we cannot just go by the archive | |
6078 | map alone. Instead we must read in the element's symbol | |
6079 | table and check that to see what kind of symbol definition | |
6080 | this is. */ | |
6081 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
6082 | continue; | |
6083 | } | |
75cfe082 | 6084 | else |
0ad989f9 | 6085 | { |
b585e899 | 6086 | if (h->type != bfd_link_hash_undefweak) |
13e570f8 | 6087 | /* Symbol must be defined. Don't check it again. */ |
0a1b45a2 | 6088 | included[i] = true; |
0ad989f9 L |
6089 | continue; |
6090 | } | |
6091 | ||
6092 | /* We need to include this archive member. */ | |
6395a102 L |
6093 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, |
6094 | info); | |
0ad989f9 L |
6095 | if (element == NULL) |
6096 | goto error_return; | |
6097 | ||
6098 | if (! bfd_check_format (element, bfd_object)) | |
6099 | goto error_return; | |
6100 | ||
0ad989f9 L |
6101 | undefs_tail = info->hash->undefs_tail; |
6102 | ||
0e144ba7 AM |
6103 | if (!(*info->callbacks |
6104 | ->add_archive_element) (info, element, symdef->name, &element)) | |
b95a0a31 | 6105 | continue; |
0e144ba7 | 6106 | if (!bfd_link_add_symbols (element, info)) |
0ad989f9 L |
6107 | goto error_return; |
6108 | ||
6109 | /* If there are any new undefined symbols, we need to make | |
6110 | another pass through the archive in order to see whether | |
6111 | they can be defined. FIXME: This isn't perfect, because | |
6112 | common symbols wind up on undefs_tail and because an | |
6113 | undefined symbol which is defined later on in this pass | |
6114 | does not require another pass. This isn't a bug, but it | |
6115 | does make the code less efficient than it could be. */ | |
6116 | if (undefs_tail != info->hash->undefs_tail) | |
0a1b45a2 | 6117 | loop = true; |
0ad989f9 L |
6118 | |
6119 | /* Look backward to mark all symbols from this object file | |
6120 | which we have already seen in this pass. */ | |
6121 | mark = i; | |
6122 | do | |
6123 | { | |
0a1b45a2 | 6124 | included[mark] = true; |
0ad989f9 L |
6125 | if (mark == 0) |
6126 | break; | |
6127 | --mark; | |
6128 | } | |
6129 | while (symdefs[mark].file_offset == symdef->file_offset); | |
6130 | ||
6131 | /* We mark subsequent symbols from this object file as we go | |
6132 | on through the loop. */ | |
6133 | last = symdef->file_offset; | |
6134 | } | |
6135 | } | |
6136 | while (loop); | |
6137 | ||
0ad989f9 | 6138 | free (included); |
0a1b45a2 | 6139 | return true; |
0ad989f9 L |
6140 | |
6141 | error_return: | |
c9594989 | 6142 | free (included); |
0a1b45a2 | 6143 | return false; |
0ad989f9 | 6144 | } |
4ad4eba5 AM |
6145 | |
6146 | /* Given an ELF BFD, add symbols to the global hash table as | |
6147 | appropriate. */ | |
6148 | ||
0a1b45a2 | 6149 | bool |
4ad4eba5 AM |
6150 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) |
6151 | { | |
6152 | switch (bfd_get_format (abfd)) | |
6153 | { | |
6154 | case bfd_object: | |
6155 | return elf_link_add_object_symbols (abfd, info); | |
6156 | case bfd_archive: | |
6157 | return elf_link_add_archive_symbols (abfd, info); | |
6158 | default: | |
6159 | bfd_set_error (bfd_error_wrong_format); | |
0a1b45a2 | 6160 | return false; |
4ad4eba5 AM |
6161 | } |
6162 | } | |
5a580b3a | 6163 | \f |
14b1c01e AM |
6164 | struct hash_codes_info |
6165 | { | |
6166 | unsigned long *hashcodes; | |
0a1b45a2 | 6167 | bool error; |
14b1c01e | 6168 | }; |
a0c8462f | 6169 | |
5a580b3a AM |
6170 | /* This function will be called though elf_link_hash_traverse to store |
6171 | all hash value of the exported symbols in an array. */ | |
6172 | ||
0a1b45a2 | 6173 | static bool |
5a580b3a AM |
6174 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) |
6175 | { | |
a50b1753 | 6176 | struct hash_codes_info *inf = (struct hash_codes_info *) data; |
5a580b3a | 6177 | const char *name; |
5a580b3a AM |
6178 | unsigned long ha; |
6179 | char *alc = NULL; | |
6180 | ||
5a580b3a AM |
6181 | /* Ignore indirect symbols. These are added by the versioning code. */ |
6182 | if (h->dynindx == -1) | |
0a1b45a2 | 6183 | return true; |
5a580b3a AM |
6184 | |
6185 | name = h->root.root.string; | |
422f1182 | 6186 | if (h->versioned >= versioned) |
5a580b3a | 6187 | { |
422f1182 L |
6188 | char *p = strchr (name, ELF_VER_CHR); |
6189 | if (p != NULL) | |
14b1c01e | 6190 | { |
422f1182 L |
6191 | alc = (char *) bfd_malloc (p - name + 1); |
6192 | if (alc == NULL) | |
6193 | { | |
0a1b45a2 AM |
6194 | inf->error = true; |
6195 | return false; | |
422f1182 L |
6196 | } |
6197 | memcpy (alc, name, p - name); | |
6198 | alc[p - name] = '\0'; | |
6199 | name = alc; | |
14b1c01e | 6200 | } |
5a580b3a AM |
6201 | } |
6202 | ||
6203 | /* Compute the hash value. */ | |
6204 | ha = bfd_elf_hash (name); | |
6205 | ||
6206 | /* Store the found hash value in the array given as the argument. */ | |
14b1c01e | 6207 | *(inf->hashcodes)++ = ha; |
5a580b3a AM |
6208 | |
6209 | /* And store it in the struct so that we can put it in the hash table | |
6210 | later. */ | |
f6e332e6 | 6211 | h->u.elf_hash_value = ha; |
5a580b3a | 6212 | |
c9594989 | 6213 | free (alc); |
0a1b45a2 | 6214 | return true; |
5a580b3a AM |
6215 | } |
6216 | ||
fdc90cb4 JJ |
6217 | struct collect_gnu_hash_codes |
6218 | { | |
6219 | bfd *output_bfd; | |
6220 | const struct elf_backend_data *bed; | |
6221 | unsigned long int nsyms; | |
6222 | unsigned long int maskbits; | |
6223 | unsigned long int *hashcodes; | |
6224 | unsigned long int *hashval; | |
6225 | unsigned long int *indx; | |
6226 | unsigned long int *counts; | |
6227 | bfd_vma *bitmask; | |
6228 | bfd_byte *contents; | |
f16a9783 | 6229 | bfd_size_type xlat; |
fdc90cb4 JJ |
6230 | long int min_dynindx; |
6231 | unsigned long int bucketcount; | |
6232 | unsigned long int symindx; | |
6233 | long int local_indx; | |
6234 | long int shift1, shift2; | |
6235 | unsigned long int mask; | |
0a1b45a2 | 6236 | bool error; |
fdc90cb4 JJ |
6237 | }; |
6238 | ||
6239 | /* This function will be called though elf_link_hash_traverse to store | |
6240 | all hash value of the exported symbols in an array. */ | |
6241 | ||
0a1b45a2 | 6242 | static bool |
fdc90cb4 JJ |
6243 | elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) |
6244 | { | |
a50b1753 | 6245 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 | 6246 | const char *name; |
fdc90cb4 JJ |
6247 | unsigned long ha; |
6248 | char *alc = NULL; | |
6249 | ||
fdc90cb4 JJ |
6250 | /* Ignore indirect symbols. These are added by the versioning code. */ |
6251 | if (h->dynindx == -1) | |
0a1b45a2 | 6252 | return true; |
fdc90cb4 JJ |
6253 | |
6254 | /* Ignore also local symbols and undefined symbols. */ | |
6255 | if (! (*s->bed->elf_hash_symbol) (h)) | |
0a1b45a2 | 6256 | return true; |
fdc90cb4 JJ |
6257 | |
6258 | name = h->root.root.string; | |
422f1182 | 6259 | if (h->versioned >= versioned) |
fdc90cb4 | 6260 | { |
422f1182 L |
6261 | char *p = strchr (name, ELF_VER_CHR); |
6262 | if (p != NULL) | |
14b1c01e | 6263 | { |
422f1182 L |
6264 | alc = (char *) bfd_malloc (p - name + 1); |
6265 | if (alc == NULL) | |
6266 | { | |
0a1b45a2 AM |
6267 | s->error = true; |
6268 | return false; | |
422f1182 L |
6269 | } |
6270 | memcpy (alc, name, p - name); | |
6271 | alc[p - name] = '\0'; | |
6272 | name = alc; | |
14b1c01e | 6273 | } |
fdc90cb4 JJ |
6274 | } |
6275 | ||
6276 | /* Compute the hash value. */ | |
6277 | ha = bfd_elf_gnu_hash (name); | |
6278 | ||
6279 | /* Store the found hash value in the array for compute_bucket_count, | |
6280 | and also for .dynsym reordering purposes. */ | |
6281 | s->hashcodes[s->nsyms] = ha; | |
6282 | s->hashval[h->dynindx] = ha; | |
6283 | ++s->nsyms; | |
6284 | if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) | |
6285 | s->min_dynindx = h->dynindx; | |
6286 | ||
c9594989 | 6287 | free (alc); |
0a1b45a2 | 6288 | return true; |
fdc90cb4 JJ |
6289 | } |
6290 | ||
6291 | /* This function will be called though elf_link_hash_traverse to do | |
f16a9783 MS |
6292 | final dynamic symbol renumbering in case of .gnu.hash. |
6293 | If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index | |
6294 | to the translation table. */ | |
fdc90cb4 | 6295 | |
0a1b45a2 | 6296 | static bool |
f16a9783 | 6297 | elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data) |
fdc90cb4 | 6298 | { |
a50b1753 | 6299 | struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; |
fdc90cb4 JJ |
6300 | unsigned long int bucket; |
6301 | unsigned long int val; | |
6302 | ||
fdc90cb4 JJ |
6303 | /* Ignore indirect symbols. */ |
6304 | if (h->dynindx == -1) | |
0a1b45a2 | 6305 | return true; |
fdc90cb4 JJ |
6306 | |
6307 | /* Ignore also local symbols and undefined symbols. */ | |
6308 | if (! (*s->bed->elf_hash_symbol) (h)) | |
6309 | { | |
6310 | if (h->dynindx >= s->min_dynindx) | |
f16a9783 MS |
6311 | { |
6312 | if (s->bed->record_xhash_symbol != NULL) | |
6313 | { | |
6314 | (*s->bed->record_xhash_symbol) (h, 0); | |
6315 | s->local_indx++; | |
6316 | } | |
6317 | else | |
6318 | h->dynindx = s->local_indx++; | |
6319 | } | |
0a1b45a2 | 6320 | return true; |
fdc90cb4 JJ |
6321 | } |
6322 | ||
6323 | bucket = s->hashval[h->dynindx] % s->bucketcount; | |
6324 | val = (s->hashval[h->dynindx] >> s->shift1) | |
6325 | & ((s->maskbits >> s->shift1) - 1); | |
6326 | s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); | |
6327 | s->bitmask[val] | |
6328 | |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); | |
6329 | val = s->hashval[h->dynindx] & ~(unsigned long int) 1; | |
6330 | if (s->counts[bucket] == 1) | |
6331 | /* Last element terminates the chain. */ | |
6332 | val |= 1; | |
6333 | bfd_put_32 (s->output_bfd, val, | |
6334 | s->contents + (s->indx[bucket] - s->symindx) * 4); | |
6335 | --s->counts[bucket]; | |
f16a9783 MS |
6336 | if (s->bed->record_xhash_symbol != NULL) |
6337 | { | |
6338 | bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4; | |
6339 | ||
6340 | (*s->bed->record_xhash_symbol) (h, xlat_loc); | |
6341 | } | |
6342 | else | |
6343 | h->dynindx = s->indx[bucket]++; | |
0a1b45a2 | 6344 | return true; |
fdc90cb4 JJ |
6345 | } |
6346 | ||
6347 | /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ | |
6348 | ||
0a1b45a2 | 6349 | bool |
fdc90cb4 JJ |
6350 | _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) |
6351 | { | |
6352 | return !(h->forced_local | |
6353 | || h->root.type == bfd_link_hash_undefined | |
6354 | || h->root.type == bfd_link_hash_undefweak | |
6355 | || ((h->root.type == bfd_link_hash_defined | |
6356 | || h->root.type == bfd_link_hash_defweak) | |
6357 | && h->root.u.def.section->output_section == NULL)); | |
6358 | } | |
6359 | ||
5a580b3a AM |
6360 | /* Array used to determine the number of hash table buckets to use |
6361 | based on the number of symbols there are. If there are fewer than | |
6362 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
6363 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
6364 | than 32771 buckets. */ | |
6365 | ||
6366 | static const size_t elf_buckets[] = | |
6367 | { | |
6368 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
6369 | 16411, 32771, 0 | |
6370 | }; | |
6371 | ||
6372 | /* Compute bucket count for hashing table. We do not use a static set | |
6373 | of possible tables sizes anymore. Instead we determine for all | |
6374 | possible reasonable sizes of the table the outcome (i.e., the | |
6375 | number of collisions etc) and choose the best solution. The | |
6376 | weighting functions are not too simple to allow the table to grow | |
6377 | without bounds. Instead one of the weighting factors is the size. | |
6378 | Therefore the result is always a good payoff between few collisions | |
6379 | (= short chain lengths) and table size. */ | |
6380 | static size_t | |
b20dd2ce | 6381 | compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, |
d40f3da9 AM |
6382 | unsigned long int *hashcodes ATTRIBUTE_UNUSED, |
6383 | unsigned long int nsyms, | |
6384 | int gnu_hash) | |
5a580b3a | 6385 | { |
5a580b3a | 6386 | size_t best_size = 0; |
5a580b3a | 6387 | unsigned long int i; |
5a580b3a | 6388 | |
5a580b3a AM |
6389 | if (info->optimize) |
6390 | { | |
5a580b3a AM |
6391 | size_t minsize; |
6392 | size_t maxsize; | |
0e3c1eeb | 6393 | uint64_t best_chlen = ~((uint64_t) 0); |
5a580b3a | 6394 | bfd *dynobj = elf_hash_table (info)->dynobj; |
d40f3da9 | 6395 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; |
5a580b3a | 6396 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); |
fdc90cb4 | 6397 | unsigned long int *counts; |
d40f3da9 | 6398 | bfd_size_type amt; |
0883b6e0 | 6399 | unsigned int no_improvement_count = 0; |
5a580b3a AM |
6400 | |
6401 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
6402 | that the hashing table must at least have NSYMS/4 and at most | |
6403 | 2*NSYMS buckets. */ | |
6404 | minsize = nsyms / 4; | |
6405 | if (minsize == 0) | |
6406 | minsize = 1; | |
6407 | best_size = maxsize = nsyms * 2; | |
fdc90cb4 JJ |
6408 | if (gnu_hash) |
6409 | { | |
6410 | if (minsize < 2) | |
6411 | minsize = 2; | |
6412 | if ((best_size & 31) == 0) | |
6413 | ++best_size; | |
6414 | } | |
5a580b3a AM |
6415 | |
6416 | /* Create array where we count the collisions in. We must use bfd_malloc | |
6417 | since the size could be large. */ | |
6418 | amt = maxsize; | |
6419 | amt *= sizeof (unsigned long int); | |
a50b1753 | 6420 | counts = (unsigned long int *) bfd_malloc (amt); |
5a580b3a | 6421 | if (counts == NULL) |
fdc90cb4 | 6422 | return 0; |
5a580b3a AM |
6423 | |
6424 | /* Compute the "optimal" size for the hash table. The criteria is a | |
6425 | minimal chain length. The minor criteria is (of course) the size | |
6426 | of the table. */ | |
6427 | for (i = minsize; i < maxsize; ++i) | |
6428 | { | |
6429 | /* Walk through the array of hashcodes and count the collisions. */ | |
0e3c1eeb | 6430 | uint64_t max; |
5a580b3a AM |
6431 | unsigned long int j; |
6432 | unsigned long int fact; | |
6433 | ||
fdc90cb4 JJ |
6434 | if (gnu_hash && (i & 31) == 0) |
6435 | continue; | |
6436 | ||
5a580b3a AM |
6437 | memset (counts, '\0', i * sizeof (unsigned long int)); |
6438 | ||
6439 | /* Determine how often each hash bucket is used. */ | |
6440 | for (j = 0; j < nsyms; ++j) | |
6441 | ++counts[hashcodes[j] % i]; | |
6442 | ||
6443 | /* For the weight function we need some information about the | |
6444 | pagesize on the target. This is information need not be 100% | |
6445 | accurate. Since this information is not available (so far) we | |
6446 | define it here to a reasonable default value. If it is crucial | |
6447 | to have a better value some day simply define this value. */ | |
6448 | # ifndef BFD_TARGET_PAGESIZE | |
6449 | # define BFD_TARGET_PAGESIZE (4096) | |
6450 | # endif | |
6451 | ||
fdc90cb4 JJ |
6452 | /* We in any case need 2 + DYNSYMCOUNT entries for the size values |
6453 | and the chains. */ | |
6454 | max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; | |
5a580b3a AM |
6455 | |
6456 | # if 1 | |
6457 | /* Variant 1: optimize for short chains. We add the squares | |
6458 | of all the chain lengths (which favors many small chain | |
6459 | over a few long chains). */ | |
6460 | for (j = 0; j < i; ++j) | |
6461 | max += counts[j] * counts[j]; | |
6462 | ||
6463 | /* This adds penalties for the overall size of the table. */ | |
fdc90cb4 | 6464 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6465 | max *= fact * fact; |
6466 | # else | |
6467 | /* Variant 2: Optimize a lot more for small table. Here we | |
6468 | also add squares of the size but we also add penalties for | |
6469 | empty slots (the +1 term). */ | |
6470 | for (j = 0; j < i; ++j) | |
6471 | max += (1 + counts[j]) * (1 + counts[j]); | |
6472 | ||
6473 | /* The overall size of the table is considered, but not as | |
6474 | strong as in variant 1, where it is squared. */ | |
fdc90cb4 | 6475 | fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; |
5a580b3a AM |
6476 | max *= fact; |
6477 | # endif | |
6478 | ||
6479 | /* Compare with current best results. */ | |
6480 | if (max < best_chlen) | |
6481 | { | |
6482 | best_chlen = max; | |
6483 | best_size = i; | |
ca4be51c | 6484 | no_improvement_count = 0; |
5a580b3a | 6485 | } |
0883b6e0 NC |
6486 | /* PR 11843: Avoid futile long searches for the best bucket size |
6487 | when there are a large number of symbols. */ | |
6488 | else if (++no_improvement_count == 100) | |
6489 | break; | |
5a580b3a AM |
6490 | } |
6491 | ||
6492 | free (counts); | |
6493 | } | |
6494 | else | |
5a580b3a | 6495 | { |
5a580b3a AM |
6496 | for (i = 0; elf_buckets[i] != 0; i++) |
6497 | { | |
6498 | best_size = elf_buckets[i]; | |
fdc90cb4 | 6499 | if (nsyms < elf_buckets[i + 1]) |
5a580b3a AM |
6500 | break; |
6501 | } | |
fdc90cb4 JJ |
6502 | if (gnu_hash && best_size < 2) |
6503 | best_size = 2; | |
5a580b3a AM |
6504 | } |
6505 | ||
5a580b3a AM |
6506 | return best_size; |
6507 | } | |
6508 | ||
d0bf826b AM |
6509 | /* Size any SHT_GROUP section for ld -r. */ |
6510 | ||
0a1b45a2 | 6511 | bool |
d0bf826b AM |
6512 | _bfd_elf_size_group_sections (struct bfd_link_info *info) |
6513 | { | |
6514 | bfd *ibfd; | |
57963c05 | 6515 | asection *s; |
d0bf826b | 6516 | |
c72f2fb2 | 6517 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
d0bf826b | 6518 | if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour |
57963c05 AM |
6519 | && (s = ibfd->sections) != NULL |
6520 | && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
d0bf826b | 6521 | && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) |
0a1b45a2 AM |
6522 | return false; |
6523 | return true; | |
d0bf826b AM |
6524 | } |
6525 | ||
04c3a755 NS |
6526 | /* Set a default stack segment size. The value in INFO wins. If it |
6527 | is unset, LEGACY_SYMBOL's value is used, and if that symbol is | |
6528 | undefined it is initialized. */ | |
6529 | ||
0a1b45a2 | 6530 | bool |
04c3a755 NS |
6531 | bfd_elf_stack_segment_size (bfd *output_bfd, |
6532 | struct bfd_link_info *info, | |
6533 | const char *legacy_symbol, | |
6534 | bfd_vma default_size) | |
6535 | { | |
6536 | struct elf_link_hash_entry *h = NULL; | |
6537 | ||
6538 | /* Look for legacy symbol. */ | |
6539 | if (legacy_symbol) | |
6540 | h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, | |
0a1b45a2 | 6541 | false, false, false); |
04c3a755 NS |
6542 | if (h && (h->root.type == bfd_link_hash_defined |
6543 | || h->root.type == bfd_link_hash_defweak) | |
6544 | && h->def_regular | |
6545 | && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) | |
6546 | { | |
6547 | /* The symbol has no type if specified on the command line. */ | |
6548 | h->type = STT_OBJECT; | |
6549 | if (info->stacksize) | |
695344c0 | 6550 | /* xgettext:c-format */ |
871b3ab2 | 6551 | _bfd_error_handler (_("%pB: stack size specified and %s set"), |
4eca0228 | 6552 | output_bfd, legacy_symbol); |
04c3a755 | 6553 | else if (h->root.u.def.section != bfd_abs_section_ptr) |
695344c0 | 6554 | /* xgettext:c-format */ |
871b3ab2 | 6555 | _bfd_error_handler (_("%pB: %s not absolute"), |
4eca0228 | 6556 | output_bfd, legacy_symbol); |
04c3a755 NS |
6557 | else |
6558 | info->stacksize = h->root.u.def.value; | |
6559 | } | |
6560 | ||
6561 | if (!info->stacksize) | |
6562 | /* If the user didn't set a size, or explicitly inhibit the | |
6563 | size, set it now. */ | |
6564 | info->stacksize = default_size; | |
6565 | ||
6566 | /* Provide the legacy symbol, if it is referenced. */ | |
6567 | if (h && (h->root.type == bfd_link_hash_undefined | |
6568 | || h->root.type == bfd_link_hash_undefweak)) | |
6569 | { | |
6570 | struct bfd_link_hash_entry *bh = NULL; | |
6571 | ||
6572 | if (!(_bfd_generic_link_add_one_symbol | |
6573 | (info, output_bfd, legacy_symbol, | |
6574 | BSF_GLOBAL, bfd_abs_section_ptr, | |
6575 | info->stacksize >= 0 ? info->stacksize : 0, | |
0a1b45a2 AM |
6576 | NULL, false, get_elf_backend_data (output_bfd)->collect, &bh))) |
6577 | return false; | |
04c3a755 NS |
6578 | |
6579 | h = (struct elf_link_hash_entry *) bh; | |
6580 | h->def_regular = 1; | |
6581 | h->type = STT_OBJECT; | |
6582 | } | |
6583 | ||
0a1b45a2 | 6584 | return true; |
04c3a755 NS |
6585 | } |
6586 | ||
b531344c MR |
6587 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ |
6588 | ||
6589 | struct elf_gc_sweep_symbol_info | |
6590 | { | |
6591 | struct bfd_link_info *info; | |
6592 | void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, | |
0a1b45a2 | 6593 | bool); |
b531344c MR |
6594 | }; |
6595 | ||
0a1b45a2 | 6596 | static bool |
b531344c MR |
6597 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) |
6598 | { | |
6599 | if (!h->mark | |
6600 | && (((h->root.type == bfd_link_hash_defined | |
6601 | || h->root.type == bfd_link_hash_defweak) | |
6602 | && !((h->def_regular || ELF_COMMON_DEF_P (h)) | |
6603 | && h->root.u.def.section->gc_mark)) | |
6604 | || h->root.type == bfd_link_hash_undefined | |
6605 | || h->root.type == bfd_link_hash_undefweak)) | |
6606 | { | |
6607 | struct elf_gc_sweep_symbol_info *inf; | |
6608 | ||
6609 | inf = (struct elf_gc_sweep_symbol_info *) data; | |
0a1b45a2 | 6610 | (*inf->hide_symbol) (inf->info, h, true); |
b531344c MR |
6611 | h->def_regular = 0; |
6612 | h->ref_regular = 0; | |
6613 | h->ref_regular_nonweak = 0; | |
6614 | } | |
6615 | ||
0a1b45a2 | 6616 | return true; |
b531344c MR |
6617 | } |
6618 | ||
5a580b3a AM |
6619 | /* Set up the sizes and contents of the ELF dynamic sections. This is |
6620 | called by the ELF linker emulation before_allocation routine. We | |
6621 | must set the sizes of the sections before the linker sets the | |
6622 | addresses of the various sections. */ | |
6623 | ||
0a1b45a2 | 6624 | bool |
5a580b3a AM |
6625 | bfd_elf_size_dynamic_sections (bfd *output_bfd, |
6626 | const char *soname, | |
6627 | const char *rpath, | |
6628 | const char *filter_shlib, | |
7ee314fa AM |
6629 | const char *audit, |
6630 | const char *depaudit, | |
5a580b3a AM |
6631 | const char * const *auxiliary_filters, |
6632 | struct bfd_link_info *info, | |
fd91d419 | 6633 | asection **sinterpptr) |
5a580b3a | 6634 | { |
5a580b3a AM |
6635 | bfd *dynobj; |
6636 | const struct elf_backend_data *bed; | |
5a580b3a AM |
6637 | |
6638 | *sinterpptr = NULL; | |
6639 | ||
5a580b3a | 6640 | if (!is_elf_hash_table (info->hash)) |
0a1b45a2 | 6641 | return true; |
5a580b3a | 6642 | |
6540edd5 AM |
6643 | /* Any syms created from now on start with -1 in |
6644 | got.refcount/offset and plt.refcount/offset. */ | |
6645 | elf_hash_table (info)->init_got_refcount | |
6646 | = elf_hash_table (info)->init_got_offset; | |
6647 | elf_hash_table (info)->init_plt_refcount | |
6648 | = elf_hash_table (info)->init_plt_offset; | |
6649 | ||
6650 | bed = get_elf_backend_data (output_bfd); | |
6651 | ||
6652 | /* The backend may have to create some sections regardless of whether | |
6653 | we're dynamic or not. */ | |
6654 | if (bed->elf_backend_always_size_sections | |
6655 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
6656 | return false; | |
6657 | ||
5a580b3a AM |
6658 | dynobj = elf_hash_table (info)->dynobj; |
6659 | ||
9a2a56cc | 6660 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) |
5a580b3a | 6661 | { |
902e9fc7 MR |
6662 | struct bfd_elf_version_tree *verdefs; |
6663 | struct elf_info_failed asvinfo; | |
5a580b3a AM |
6664 | struct bfd_elf_version_tree *t; |
6665 | struct bfd_elf_version_expr *d; | |
902e9fc7 | 6666 | asection *s; |
e6699019 | 6667 | size_t soname_indx; |
7ee314fa | 6668 | |
5a580b3a AM |
6669 | /* If we are supposed to export all symbols into the dynamic symbol |
6670 | table (this is not the normal case), then do so. */ | |
55255dae | 6671 | if (info->export_dynamic |
0e1862bb | 6672 | || (bfd_link_executable (info) && info->dynamic)) |
5a580b3a | 6673 | { |
3d13f3e9 AM |
6674 | struct elf_info_failed eif; |
6675 | ||
6676 | eif.info = info; | |
0a1b45a2 | 6677 | eif.failed = false; |
5a580b3a AM |
6678 | elf_link_hash_traverse (elf_hash_table (info), |
6679 | _bfd_elf_export_symbol, | |
6680 | &eif); | |
6681 | if (eif.failed) | |
0a1b45a2 | 6682 | return false; |
5a580b3a AM |
6683 | } |
6684 | ||
e6699019 L |
6685 | if (soname != NULL) |
6686 | { | |
6687 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
0a1b45a2 | 6688 | soname, true); |
e6699019 L |
6689 | if (soname_indx == (size_t) -1 |
6690 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
0a1b45a2 | 6691 | return false; |
e6699019 L |
6692 | } |
6693 | else | |
6694 | soname_indx = (size_t) -1; | |
6695 | ||
5a580b3a | 6696 | /* Make all global versions with definition. */ |
fd91d419 | 6697 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6698 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6699 | if (!d->symver && d->literal) |
5a580b3a AM |
6700 | { |
6701 | const char *verstr, *name; | |
6702 | size_t namelen, verlen, newlen; | |
93252b1c | 6703 | char *newname, *p, leading_char; |
5a580b3a AM |
6704 | struct elf_link_hash_entry *newh; |
6705 | ||
93252b1c | 6706 | leading_char = bfd_get_symbol_leading_char (output_bfd); |
ae5a3597 | 6707 | name = d->pattern; |
93252b1c | 6708 | namelen = strlen (name) + (leading_char != '\0'); |
5a580b3a AM |
6709 | verstr = t->name; |
6710 | verlen = strlen (verstr); | |
6711 | newlen = namelen + verlen + 3; | |
6712 | ||
a50b1753 | 6713 | newname = (char *) bfd_malloc (newlen); |
5a580b3a | 6714 | if (newname == NULL) |
0a1b45a2 | 6715 | return false; |
93252b1c MF |
6716 | newname[0] = leading_char; |
6717 | memcpy (newname + (leading_char != '\0'), name, namelen); | |
5a580b3a AM |
6718 | |
6719 | /* Check the hidden versioned definition. */ | |
6720 | p = newname + namelen; | |
6721 | *p++ = ELF_VER_CHR; | |
6722 | memcpy (p, verstr, verlen + 1); | |
6723 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
0a1b45a2 AM |
6724 | newname, false, false, |
6725 | false); | |
5a580b3a AM |
6726 | if (newh == NULL |
6727 | || (newh->root.type != bfd_link_hash_defined | |
6728 | && newh->root.type != bfd_link_hash_defweak)) | |
6729 | { | |
6730 | /* Check the default versioned definition. */ | |
6731 | *p++ = ELF_VER_CHR; | |
6732 | memcpy (p, verstr, verlen + 1); | |
6733 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
0a1b45a2 AM |
6734 | newname, false, false, |
6735 | false); | |
5a580b3a AM |
6736 | } |
6737 | free (newname); | |
6738 | ||
6739 | /* Mark this version if there is a definition and it is | |
6740 | not defined in a shared object. */ | |
6741 | if (newh != NULL | |
f5385ebf | 6742 | && !newh->def_dynamic |
5a580b3a AM |
6743 | && (newh->root.type == bfd_link_hash_defined |
6744 | || newh->root.type == bfd_link_hash_defweak)) | |
6745 | d->symver = 1; | |
6746 | } | |
6747 | ||
6748 | /* Attach all the symbols to their version information. */ | |
5a580b3a | 6749 | asvinfo.info = info; |
0a1b45a2 | 6750 | asvinfo.failed = false; |
5a580b3a AM |
6751 | |
6752 | elf_link_hash_traverse (elf_hash_table (info), | |
6753 | _bfd_elf_link_assign_sym_version, | |
6754 | &asvinfo); | |
6755 | if (asvinfo.failed) | |
0a1b45a2 | 6756 | return false; |
5a580b3a AM |
6757 | |
6758 | if (!info->allow_undefined_version) | |
6759 | { | |
6760 | /* Check if all global versions have a definition. */ | |
0a1b45a2 | 6761 | bool all_defined = true; |
fd91d419 | 6762 | for (t = info->version_info; t != NULL; t = t->next) |
5a580b3a | 6763 | for (d = t->globals.list; d != NULL; d = d->next) |
ae5a3597 | 6764 | if (d->literal && !d->symver && !d->script) |
5a580b3a | 6765 | { |
4eca0228 | 6766 | _bfd_error_handler |
5a580b3a AM |
6767 | (_("%s: undefined version: %s"), |
6768 | d->pattern, t->name); | |
0a1b45a2 | 6769 | all_defined = false; |
5a580b3a AM |
6770 | } |
6771 | ||
6772 | if (!all_defined) | |
6773 | { | |
6774 | bfd_set_error (bfd_error_bad_value); | |
0a1b45a2 | 6775 | return false; |
5a580b3a AM |
6776 | } |
6777 | } | |
6778 | ||
902e9fc7 MR |
6779 | /* Set up the version definition section. */ |
6780 | s = bfd_get_linker_section (dynobj, ".gnu.version_d"); | |
6781 | BFD_ASSERT (s != NULL); | |
5a580b3a | 6782 | |
902e9fc7 MR |
6783 | /* We may have created additional version definitions if we are |
6784 | just linking a regular application. */ | |
6785 | verdefs = info->version_info; | |
5a580b3a | 6786 | |
902e9fc7 MR |
6787 | /* Skip anonymous version tag. */ |
6788 | if (verdefs != NULL && verdefs->vernum == 0) | |
6789 | verdefs = verdefs->next; | |
5a580b3a | 6790 | |
902e9fc7 MR |
6791 | if (verdefs == NULL && !info->create_default_symver) |
6792 | s->flags |= SEC_EXCLUDE; | |
6793 | else | |
5a580b3a | 6794 | { |
902e9fc7 MR |
6795 | unsigned int cdefs; |
6796 | bfd_size_type size; | |
6797 | bfd_byte *p; | |
6798 | Elf_Internal_Verdef def; | |
6799 | Elf_Internal_Verdaux defaux; | |
6800 | struct bfd_link_hash_entry *bh; | |
6801 | struct elf_link_hash_entry *h; | |
6802 | const char *name; | |
5a580b3a | 6803 | |
902e9fc7 MR |
6804 | cdefs = 0; |
6805 | size = 0; | |
5a580b3a | 6806 | |
902e9fc7 MR |
6807 | /* Make space for the base version. */ |
6808 | size += sizeof (Elf_External_Verdef); | |
6809 | size += sizeof (Elf_External_Verdaux); | |
6810 | ++cdefs; | |
6811 | ||
6812 | /* Make space for the default version. */ | |
6813 | if (info->create_default_symver) | |
6814 | { | |
6815 | size += sizeof (Elf_External_Verdef); | |
6816 | ++cdefs; | |
3e3b46e5 PB |
6817 | } |
6818 | ||
5a580b3a AM |
6819 | for (t = verdefs; t != NULL; t = t->next) |
6820 | { | |
6821 | struct bfd_elf_version_deps *n; | |
6822 | ||
a6cc6b3b RO |
6823 | /* Don't emit base version twice. */ |
6824 | if (t->vernum == 0) | |
6825 | continue; | |
6826 | ||
5a580b3a AM |
6827 | size += sizeof (Elf_External_Verdef); |
6828 | size += sizeof (Elf_External_Verdaux); | |
6829 | ++cdefs; | |
6830 | ||
6831 | for (n = t->deps; n != NULL; n = n->next) | |
6832 | size += sizeof (Elf_External_Verdaux); | |
6833 | } | |
6834 | ||
eea6121a | 6835 | s->size = size; |
a50b1753 | 6836 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
eea6121a | 6837 | if (s->contents == NULL && s->size != 0) |
0a1b45a2 | 6838 | return false; |
5a580b3a AM |
6839 | |
6840 | /* Fill in the version definition section. */ | |
6841 | ||
6842 | p = s->contents; | |
6843 | ||
6844 | def.vd_version = VER_DEF_CURRENT; | |
6845 | def.vd_flags = VER_FLG_BASE; | |
6846 | def.vd_ndx = 1; | |
6847 | def.vd_cnt = 1; | |
3e3b46e5 PB |
6848 | if (info->create_default_symver) |
6849 | { | |
6850 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
6851 | def.vd_next = sizeof (Elf_External_Verdef); | |
6852 | } | |
6853 | else | |
6854 | { | |
6855 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6856 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6857 | + sizeof (Elf_External_Verdaux)); | |
6858 | } | |
5a580b3a | 6859 | |
ef53be89 | 6860 | if (soname_indx != (size_t) -1) |
5a580b3a AM |
6861 | { |
6862 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6863 | soname_indx); | |
6864 | def.vd_hash = bfd_elf_hash (soname); | |
6865 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 6866 | name = soname; |
5a580b3a AM |
6867 | } |
6868 | else | |
6869 | { | |
ef53be89 | 6870 | size_t indx; |
5a580b3a | 6871 | |
765cf5f6 | 6872 | name = lbasename (bfd_get_filename (output_bfd)); |
5a580b3a AM |
6873 | def.vd_hash = bfd_elf_hash (name); |
6874 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
0a1b45a2 | 6875 | name, false); |
ef53be89 | 6876 | if (indx == (size_t) -1) |
0a1b45a2 | 6877 | return false; |
5a580b3a AM |
6878 | defaux.vda_name = indx; |
6879 | } | |
6880 | defaux.vda_next = 0; | |
6881 | ||
6882 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6883 | (Elf_External_Verdef *) p); | |
6884 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
6885 | if (info->create_default_symver) |
6886 | { | |
6887 | /* Add a symbol representing this version. */ | |
6888 | bh = NULL; | |
6889 | if (! (_bfd_generic_link_add_one_symbol | |
6890 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
0a1b45a2 | 6891 | 0, NULL, false, |
3e3b46e5 | 6892 | get_elf_backend_data (dynobj)->collect, &bh))) |
0a1b45a2 | 6893 | return false; |
3e3b46e5 PB |
6894 | h = (struct elf_link_hash_entry *) bh; |
6895 | h->non_elf = 0; | |
6896 | h->def_regular = 1; | |
6897 | h->type = STT_OBJECT; | |
6898 | h->verinfo.vertree = NULL; | |
6899 | ||
6900 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
0a1b45a2 | 6901 | return false; |
3e3b46e5 PB |
6902 | |
6903 | /* Create a duplicate of the base version with the same | |
6904 | aux block, but different flags. */ | |
6905 | def.vd_flags = 0; | |
6906 | def.vd_ndx = 2; | |
6907 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6908 | if (verdefs) | |
6909 | def.vd_next = (sizeof (Elf_External_Verdef) | |
6910 | + sizeof (Elf_External_Verdaux)); | |
6911 | else | |
6912 | def.vd_next = 0; | |
6913 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6914 | (Elf_External_Verdef *) p); | |
6915 | p += sizeof (Elf_External_Verdef); | |
6916 | } | |
5a580b3a AM |
6917 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
6918 | (Elf_External_Verdaux *) p); | |
6919 | p += sizeof (Elf_External_Verdaux); | |
6920 | ||
6921 | for (t = verdefs; t != NULL; t = t->next) | |
6922 | { | |
6923 | unsigned int cdeps; | |
6924 | struct bfd_elf_version_deps *n; | |
5a580b3a | 6925 | |
a6cc6b3b RO |
6926 | /* Don't emit the base version twice. */ |
6927 | if (t->vernum == 0) | |
6928 | continue; | |
6929 | ||
5a580b3a AM |
6930 | cdeps = 0; |
6931 | for (n = t->deps; n != NULL; n = n->next) | |
6932 | ++cdeps; | |
6933 | ||
6934 | /* Add a symbol representing this version. */ | |
6935 | bh = NULL; | |
6936 | if (! (_bfd_generic_link_add_one_symbol | |
6937 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
0a1b45a2 | 6938 | 0, NULL, false, |
5a580b3a | 6939 | get_elf_backend_data (dynobj)->collect, &bh))) |
0a1b45a2 | 6940 | return false; |
5a580b3a | 6941 | h = (struct elf_link_hash_entry *) bh; |
f5385ebf AM |
6942 | h->non_elf = 0; |
6943 | h->def_regular = 1; | |
5a580b3a AM |
6944 | h->type = STT_OBJECT; |
6945 | h->verinfo.vertree = t; | |
6946 | ||
c152c796 | 6947 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
0a1b45a2 | 6948 | return false; |
5a580b3a AM |
6949 | |
6950 | def.vd_version = VER_DEF_CURRENT; | |
6951 | def.vd_flags = 0; | |
6952 | if (t->globals.list == NULL | |
6953 | && t->locals.list == NULL | |
6954 | && ! t->used) | |
6955 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 6956 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
6957 | def.vd_cnt = cdeps + 1; |
6958 | def.vd_hash = bfd_elf_hash (t->name); | |
6959 | def.vd_aux = sizeof (Elf_External_Verdef); | |
6960 | def.vd_next = 0; | |
a6cc6b3b RO |
6961 | |
6962 | /* If a basever node is next, it *must* be the last node in | |
6963 | the chain, otherwise Verdef construction breaks. */ | |
6964 | if (t->next != NULL && t->next->vernum == 0) | |
6965 | BFD_ASSERT (t->next->next == NULL); | |
6966 | ||
6967 | if (t->next != NULL && t->next->vernum != 0) | |
5a580b3a AM |
6968 | def.vd_next = (sizeof (Elf_External_Verdef) |
6969 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
6970 | ||
6971 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
6972 | (Elf_External_Verdef *) p); | |
6973 | p += sizeof (Elf_External_Verdef); | |
6974 | ||
6975 | defaux.vda_name = h->dynstr_index; | |
6976 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6977 | h->dynstr_index); | |
6978 | defaux.vda_next = 0; | |
6979 | if (t->deps != NULL) | |
6980 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
6981 | t->name_indx = defaux.vda_name; | |
6982 | ||
6983 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
6984 | (Elf_External_Verdaux *) p); | |
6985 | p += sizeof (Elf_External_Verdaux); | |
6986 | ||
6987 | for (n = t->deps; n != NULL; n = n->next) | |
6988 | { | |
6989 | if (n->version_needed == NULL) | |
6990 | { | |
6991 | /* This can happen if there was an error in the | |
6992 | version script. */ | |
6993 | defaux.vda_name = 0; | |
6994 | } | |
6995 | else | |
6996 | { | |
6997 | defaux.vda_name = n->version_needed->name_indx; | |
6998 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
6999 | defaux.vda_name); | |
7000 | } | |
7001 | if (n->next == NULL) | |
7002 | defaux.vda_next = 0; | |
7003 | else | |
7004 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
7005 | ||
7006 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
7007 | (Elf_External_Verdaux *) p); | |
7008 | p += sizeof (Elf_External_Verdaux); | |
7009 | } | |
7010 | } | |
7011 | ||
5a580b3a AM |
7012 | elf_tdata (output_bfd)->cverdefs = cdefs; |
7013 | } | |
902e9fc7 MR |
7014 | } |
7015 | ||
902e9fc7 MR |
7016 | if (info->gc_sections && bed->can_gc_sections) |
7017 | { | |
7018 | struct elf_gc_sweep_symbol_info sweep_info; | |
902e9fc7 MR |
7019 | |
7020 | /* Remove the symbols that were in the swept sections from the | |
3d13f3e9 | 7021 | dynamic symbol table. */ |
902e9fc7 MR |
7022 | sweep_info.info = info; |
7023 | sweep_info.hide_symbol = bed->elf_backend_hide_symbol; | |
7024 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, | |
7025 | &sweep_info); | |
3d13f3e9 AM |
7026 | } |
7027 | ||
7028 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
7029 | { | |
7030 | asection *s; | |
7031 | struct elf_find_verdep_info sinfo; | |
7032 | ||
7033 | /* Work out the size of the version reference section. */ | |
7034 | ||
7035 | s = bfd_get_linker_section (dynobj, ".gnu.version_r"); | |
7036 | BFD_ASSERT (s != NULL); | |
902e9fc7 | 7037 | |
3d13f3e9 AM |
7038 | sinfo.info = info; |
7039 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
7040 | if (sinfo.vers == 0) | |
7041 | sinfo.vers = 1; | |
0a1b45a2 | 7042 | sinfo.failed = false; |
3d13f3e9 AM |
7043 | |
7044 | elf_link_hash_traverse (elf_hash_table (info), | |
7045 | _bfd_elf_link_find_version_dependencies, | |
7046 | &sinfo); | |
7047 | if (sinfo.failed) | |
0a1b45a2 | 7048 | return false; |
3d13f3e9 | 7049 | |
72aa8173 L |
7050 | if (info->enable_dt_relr) |
7051 | { | |
7052 | elf_link_add_dt_relr_dependency (&sinfo); | |
7053 | if (sinfo.failed) | |
7054 | return false; | |
7055 | } | |
7056 | ||
3d13f3e9 AM |
7057 | if (elf_tdata (output_bfd)->verref == NULL) |
7058 | s->flags |= SEC_EXCLUDE; | |
7059 | else | |
7060 | { | |
7061 | Elf_Internal_Verneed *vn; | |
7062 | unsigned int size; | |
7063 | unsigned int crefs; | |
7064 | bfd_byte *p; | |
7065 | ||
7066 | /* Build the version dependency section. */ | |
7067 | size = 0; | |
7068 | crefs = 0; | |
7069 | for (vn = elf_tdata (output_bfd)->verref; | |
7070 | vn != NULL; | |
7071 | vn = vn->vn_nextref) | |
7072 | { | |
7073 | Elf_Internal_Vernaux *a; | |
7074 | ||
7075 | size += sizeof (Elf_External_Verneed); | |
7076 | ++crefs; | |
7077 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
7078 | size += sizeof (Elf_External_Vernaux); | |
7079 | } | |
7080 | ||
7081 | s->size = size; | |
7082 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); | |
7083 | if (s->contents == NULL) | |
0a1b45a2 | 7084 | return false; |
3d13f3e9 AM |
7085 | |
7086 | p = s->contents; | |
7087 | for (vn = elf_tdata (output_bfd)->verref; | |
7088 | vn != NULL; | |
7089 | vn = vn->vn_nextref) | |
7090 | { | |
7091 | unsigned int caux; | |
7092 | Elf_Internal_Vernaux *a; | |
7093 | size_t indx; | |
7094 | ||
7095 | caux = 0; | |
7096 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
7097 | ++caux; | |
7098 | ||
7099 | vn->vn_version = VER_NEED_CURRENT; | |
7100 | vn->vn_cnt = caux; | |
7101 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
7102 | elf_dt_name (vn->vn_bfd) != NULL | |
7103 | ? elf_dt_name (vn->vn_bfd) | |
765cf5f6 AM |
7104 | : lbasename (bfd_get_filename |
7105 | (vn->vn_bfd)), | |
0a1b45a2 | 7106 | false); |
3d13f3e9 | 7107 | if (indx == (size_t) -1) |
0a1b45a2 | 7108 | return false; |
3d13f3e9 AM |
7109 | vn->vn_file = indx; |
7110 | vn->vn_aux = sizeof (Elf_External_Verneed); | |
7111 | if (vn->vn_nextref == NULL) | |
7112 | vn->vn_next = 0; | |
7113 | else | |
7114 | vn->vn_next = (sizeof (Elf_External_Verneed) | |
7115 | + caux * sizeof (Elf_External_Vernaux)); | |
7116 | ||
7117 | _bfd_elf_swap_verneed_out (output_bfd, vn, | |
7118 | (Elf_External_Verneed *) p); | |
7119 | p += sizeof (Elf_External_Verneed); | |
7120 | ||
7121 | for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
7122 | { | |
7123 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
7124 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
0a1b45a2 | 7125 | a->vna_nodename, false); |
3d13f3e9 | 7126 | if (indx == (size_t) -1) |
0a1b45a2 | 7127 | return false; |
3d13f3e9 AM |
7128 | a->vna_name = indx; |
7129 | if (a->vna_nextptr == NULL) | |
7130 | a->vna_next = 0; | |
7131 | else | |
7132 | a->vna_next = sizeof (Elf_External_Vernaux); | |
7133 | ||
7134 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
7135 | (Elf_External_Vernaux *) p); | |
7136 | p += sizeof (Elf_External_Vernaux); | |
7137 | } | |
7138 | } | |
7139 | ||
7140 | elf_tdata (output_bfd)->cverrefs = crefs; | |
7141 | } | |
902e9fc7 MR |
7142 | } |
7143 | ||
902e9fc7 MR |
7144 | if (bfd_link_relocatable (info) |
7145 | && !_bfd_elf_size_group_sections (info)) | |
0a1b45a2 | 7146 | return false; |
902e9fc7 | 7147 | |
902e9fc7 MR |
7148 | /* Determine any GNU_STACK segment requirements, after the backend |
7149 | has had a chance to set a default segment size. */ | |
7150 | if (info->execstack) | |
65daf5be NC |
7151 | { |
7152 | /* If the user has explicitly requested warnings, then generate one even | |
7153 | though the choice is the result of another command line option. */ | |
7154 | if (info->warn_execstack == 1) | |
e922d1ea NC |
7155 | { |
7156 | if (info->error_execstack) | |
7157 | { | |
7158 | _bfd_error_handler | |
7159 | (_("\ | |
7160 | error: creating an executable stack because of -z execstack command line option")); | |
7161 | return false; | |
7162 | } | |
7163 | ||
7164 | _bfd_error_handler | |
7165 | (_("\ | |
65daf5be | 7166 | warning: enabling an executable stack because of -z execstack command line option")); |
e922d1ea NC |
7167 | } |
7168 | ||
65daf5be NC |
7169 | elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; |
7170 | } | |
902e9fc7 MR |
7171 | else if (info->noexecstack) |
7172 | elf_stack_flags (output_bfd) = PF_R | PF_W; | |
7173 | else | |
7174 | { | |
7175 | bfd *inputobj; | |
7176 | asection *notesec = NULL; | |
65daf5be NC |
7177 | bfd *noteobj = NULL; |
7178 | bfd *emptyobj = NULL; | |
902e9fc7 MR |
7179 | int exec = 0; |
7180 | ||
7181 | for (inputobj = info->input_bfds; | |
7182 | inputobj; | |
7183 | inputobj = inputobj->link.next) | |
7184 | { | |
7185 | asection *s; | |
7186 | ||
7187 | if (inputobj->flags | |
7188 | & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) | |
7189 | continue; | |
57963c05 AM |
7190 | s = inputobj->sections; |
7191 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
7192 | continue; | |
7193 | ||
902e9fc7 MR |
7194 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); |
7195 | if (s) | |
7196 | { | |
902e9fc7 | 7197 | notesec = s; |
65daf5be NC |
7198 | if (s->flags & SEC_CODE) |
7199 | { | |
7200 | noteobj = inputobj; | |
7201 | exec = PF_X; | |
7202 | /* There is no point in scanning the remaining bfds. */ | |
7203 | break; | |
7204 | } | |
902e9fc7 | 7205 | } |
ba951afb | 7206 | else if (bed->default_execstack && info->default_execstack) |
65daf5be NC |
7207 | { |
7208 | exec = PF_X; | |
7209 | emptyobj = inputobj; | |
7210 | } | |
902e9fc7 | 7211 | } |
65daf5be | 7212 | |
902e9fc7 | 7213 | if (notesec || info->stacksize > 0) |
65daf5be NC |
7214 | { |
7215 | if (exec) | |
7216 | { | |
bd7d326d | 7217 | if (info->warn_execstack != 0) |
65daf5be NC |
7218 | { |
7219 | /* PR 29072: Because an executable stack is a serious | |
7220 | security risk, make sure that the user knows that it is | |
7221 | being enabled despite the fact that it was not requested | |
7222 | on the command line. */ | |
7223 | if (noteobj) | |
e922d1ea NC |
7224 | { |
7225 | if (info->error_execstack) | |
7226 | { | |
7227 | _bfd_error_handler (_("\ | |
7228 | error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"), | |
7229 | bfd_get_filename (noteobj)); | |
7230 | return false; | |
7231 | } | |
7232 | ||
7233 | _bfd_error_handler (_("\ | |
65daf5be NC |
7234 | warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"), |
7235 | bfd_get_filename (noteobj)); | |
e922d1ea | 7236 | } |
65daf5be | 7237 | else if (emptyobj) |
0d38576a | 7238 | { |
e922d1ea NC |
7239 | if (info->error_execstack) |
7240 | { | |
7241 | _bfd_error_handler (_("\ | |
7242 | error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"), | |
7243 | bfd_get_filename (emptyobj)); | |
7244 | return false; | |
7245 | } | |
7246 | ||
0d38576a | 7247 | _bfd_error_handler (_("\ |
65daf5be | 7248 | warning: %s: missing .note.GNU-stack section implies executable stack"), |
0d38576a NC |
7249 | bfd_get_filename (emptyobj)); |
7250 | _bfd_error_handler (_("\ | |
7251 | NOTE: This behaviour is deprecated and will be removed in a future version of the linker")); | |
7252 | } | |
65daf5be NC |
7253 | } |
7254 | } | |
7255 | elf_stack_flags (output_bfd) = PF_R | PF_W | exec; | |
7256 | } | |
7257 | ||
902e9fc7 MR |
7258 | if (notesec && exec && bfd_link_relocatable (info) |
7259 | && notesec->output_section != bfd_abs_section_ptr) | |
7260 | notesec->output_section->flags |= SEC_CODE; | |
7261 | } | |
7262 | ||
7263 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
7264 | { | |
7265 | struct elf_info_failed eif; | |
7266 | struct elf_link_hash_entry *h; | |
7267 | asection *dynstr; | |
7268 | asection *s; | |
7269 | ||
7270 | *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); | |
7271 | BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); | |
7272 | ||
902e9fc7 MR |
7273 | if (info->symbolic) |
7274 | { | |
7275 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
0a1b45a2 | 7276 | return false; |
902e9fc7 MR |
7277 | info->flags |= DF_SYMBOLIC; |
7278 | } | |
7279 | ||
7280 | if (rpath != NULL) | |
7281 | { | |
7282 | size_t indx; | |
7283 | bfd_vma tag; | |
7284 | ||
7285 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
0a1b45a2 | 7286 | true); |
902e9fc7 | 7287 | if (indx == (size_t) -1) |
0a1b45a2 | 7288 | return false; |
902e9fc7 MR |
7289 | |
7290 | tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; | |
7291 | if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) | |
0a1b45a2 | 7292 | return false; |
902e9fc7 MR |
7293 | } |
7294 | ||
7295 | if (filter_shlib != NULL) | |
7296 | { | |
7297 | size_t indx; | |
7298 | ||
7299 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
0a1b45a2 | 7300 | filter_shlib, true); |
902e9fc7 MR |
7301 | if (indx == (size_t) -1 |
7302 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
0a1b45a2 | 7303 | return false; |
902e9fc7 MR |
7304 | } |
7305 | ||
7306 | if (auxiliary_filters != NULL) | |
7307 | { | |
7308 | const char * const *p; | |
7309 | ||
7310 | for (p = auxiliary_filters; *p != NULL; p++) | |
7311 | { | |
7312 | size_t indx; | |
7313 | ||
7314 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
0a1b45a2 | 7315 | *p, true); |
902e9fc7 MR |
7316 | if (indx == (size_t) -1 |
7317 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
0a1b45a2 | 7318 | return false; |
902e9fc7 MR |
7319 | } |
7320 | } | |
7321 | ||
7322 | if (audit != NULL) | |
7323 | { | |
7324 | size_t indx; | |
7325 | ||
7326 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, | |
0a1b45a2 | 7327 | true); |
902e9fc7 MR |
7328 | if (indx == (size_t) -1 |
7329 | || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) | |
0a1b45a2 | 7330 | return false; |
902e9fc7 MR |
7331 | } |
7332 | ||
7333 | if (depaudit != NULL) | |
7334 | { | |
7335 | size_t indx; | |
7336 | ||
7337 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, | |
0a1b45a2 | 7338 | true); |
902e9fc7 MR |
7339 | if (indx == (size_t) -1 |
7340 | || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) | |
0a1b45a2 | 7341 | return false; |
902e9fc7 MR |
7342 | } |
7343 | ||
7344 | eif.info = info; | |
0a1b45a2 | 7345 | eif.failed = false; |
902e9fc7 MR |
7346 | |
7347 | /* Find all symbols which were defined in a dynamic object and make | |
7348 | the backend pick a reasonable value for them. */ | |
7349 | elf_link_hash_traverse (elf_hash_table (info), | |
7350 | _bfd_elf_adjust_dynamic_symbol, | |
7351 | &eif); | |
7352 | if (eif.failed) | |
0a1b45a2 | 7353 | return false; |
902e9fc7 MR |
7354 | |
7355 | /* Add some entries to the .dynamic section. We fill in some of the | |
7356 | values later, in bfd_elf_final_link, but we must add the entries | |
7357 | now so that we know the final size of the .dynamic section. */ | |
7358 | ||
7359 | /* If there are initialization and/or finalization functions to | |
7360 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
7361 | h = (info->init_function | |
7362 | ? elf_link_hash_lookup (elf_hash_table (info), | |
0a1b45a2 AM |
7363 | info->init_function, false, |
7364 | false, false) | |
902e9fc7 MR |
7365 | : NULL); |
7366 | if (h != NULL | |
7367 | && (h->ref_regular | |
7368 | || h->def_regular)) | |
7369 | { | |
7370 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
0a1b45a2 | 7371 | return false; |
902e9fc7 MR |
7372 | } |
7373 | h = (info->fini_function | |
7374 | ? elf_link_hash_lookup (elf_hash_table (info), | |
0a1b45a2 AM |
7375 | info->fini_function, false, |
7376 | false, false) | |
902e9fc7 MR |
7377 | : NULL); |
7378 | if (h != NULL | |
7379 | && (h->ref_regular | |
7380 | || h->def_regular)) | |
7381 | { | |
7382 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
0a1b45a2 | 7383 | return false; |
902e9fc7 MR |
7384 | } |
7385 | ||
7386 | s = bfd_get_section_by_name (output_bfd, ".preinit_array"); | |
7387 | if (s != NULL && s->linker_has_input) | |
7388 | { | |
7389 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
7390 | if (! bfd_link_executable (info)) | |
7391 | { | |
7392 | bfd *sub; | |
7393 | asection *o; | |
7394 | ||
57963c05 AM |
7395 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
7396 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour | |
7397 | && (o = sub->sections) != NULL | |
7398 | && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) | |
902e9fc7 MR |
7399 | for (o = sub->sections; o != NULL; o = o->next) |
7400 | if (elf_section_data (o)->this_hdr.sh_type | |
7401 | == SHT_PREINIT_ARRAY) | |
7402 | { | |
7403 | _bfd_error_handler | |
871b3ab2 | 7404 | (_("%pB: .preinit_array section is not allowed in DSO"), |
902e9fc7 MR |
7405 | sub); |
7406 | break; | |
7407 | } | |
7408 | ||
7409 | bfd_set_error (bfd_error_nonrepresentable_section); | |
0a1b45a2 | 7410 | return false; |
902e9fc7 MR |
7411 | } |
7412 | ||
7413 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
7414 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
0a1b45a2 | 7415 | return false; |
902e9fc7 MR |
7416 | } |
7417 | s = bfd_get_section_by_name (output_bfd, ".init_array"); | |
7418 | if (s != NULL && s->linker_has_input) | |
7419 | { | |
7420 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
7421 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
0a1b45a2 | 7422 | return false; |
902e9fc7 MR |
7423 | } |
7424 | s = bfd_get_section_by_name (output_bfd, ".fini_array"); | |
7425 | if (s != NULL && s->linker_has_input) | |
7426 | { | |
7427 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
7428 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
0a1b45a2 | 7429 | return false; |
902e9fc7 MR |
7430 | } |
7431 | ||
7432 | dynstr = bfd_get_linker_section (dynobj, ".dynstr"); | |
7433 | /* If .dynstr is excluded from the link, we don't want any of | |
7434 | these tags. Strictly, we should be checking each section | |
7435 | individually; This quick check covers for the case where | |
7436 | someone does a /DISCARD/ : { *(*) }. */ | |
7437 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
7438 | { | |
7439 | bfd_size_type strsize; | |
7440 | ||
7441 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
7442 | if ((info->emit_hash | |
7443 | && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) | |
7444 | || (info->emit_gnu_hash | |
f16a9783 MS |
7445 | && (bed->record_xhash_symbol == NULL |
7446 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))) | |
902e9fc7 MR |
7447 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) |
7448 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
7449 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
7450 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
6a0a0dd0 VDM |
7451 | bed->s->sizeof_sym) |
7452 | || (info->gnu_flags_1 | |
7453 | && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1, | |
7454 | info->gnu_flags_1))) | |
0a1b45a2 | 7455 | return false; |
902e9fc7 MR |
7456 | } |
7457 | } | |
7458 | ||
7459 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
0a1b45a2 | 7460 | return false; |
902e9fc7 MR |
7461 | |
7462 | /* The backend must work out the sizes of all the other dynamic | |
7463 | sections. */ | |
7464 | if (dynobj != NULL | |
7465 | && bed->elf_backend_size_dynamic_sections != NULL | |
7466 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
0a1b45a2 | 7467 | return false; |
902e9fc7 MR |
7468 | |
7469 | if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) | |
7470 | { | |
902e9fc7 MR |
7471 | if (elf_tdata (output_bfd)->cverdefs) |
7472 | { | |
7473 | unsigned int crefs = elf_tdata (output_bfd)->cverdefs; | |
7474 | ||
7475 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
7476 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) | |
0a1b45a2 | 7477 | return false; |
902e9fc7 MR |
7478 | } |
7479 | ||
7480 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
7481 | { | |
7482 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
0a1b45a2 | 7483 | return false; |
902e9fc7 MR |
7484 | } |
7485 | else if (info->flags & DF_BIND_NOW) | |
7486 | { | |
7487 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
0a1b45a2 | 7488 | return false; |
902e9fc7 MR |
7489 | } |
7490 | ||
7491 | if (info->flags_1) | |
7492 | { | |
7493 | if (bfd_link_executable (info)) | |
7494 | info->flags_1 &= ~ (DF_1_INITFIRST | |
7495 | | DF_1_NODELETE | |
7496 | | DF_1_NOOPEN); | |
7497 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
0a1b45a2 | 7498 | return false; |
902e9fc7 MR |
7499 | } |
7500 | ||
7501 | if (elf_tdata (output_bfd)->cverrefs) | |
7502 | { | |
7503 | unsigned int crefs = elf_tdata (output_bfd)->cverrefs; | |
7504 | ||
7505 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
7506 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
0a1b45a2 | 7507 | return false; |
902e9fc7 | 7508 | } |
5a580b3a | 7509 | |
8423293d AM |
7510 | if ((elf_tdata (output_bfd)->cverrefs == 0 |
7511 | && elf_tdata (output_bfd)->cverdefs == 0) | |
63f452a8 | 7512 | || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) |
8423293d | 7513 | { |
902e9fc7 MR |
7514 | asection *s; |
7515 | ||
3d4d4302 | 7516 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
8423293d AM |
7517 | s->flags |= SEC_EXCLUDE; |
7518 | } | |
7519 | } | |
0a1b45a2 | 7520 | return true; |
8423293d AM |
7521 | } |
7522 | ||
74541ad4 AM |
7523 | /* Find the first non-excluded output section. We'll use its |
7524 | section symbol for some emitted relocs. */ | |
7525 | void | |
7526 | _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) | |
7527 | { | |
7528 | asection *s; | |
f26a3287 | 7529 | asection *found = NULL; |
74541ad4 AM |
7530 | |
7531 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
7532 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC | |
d00dd7dc | 7533 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7534 | { |
f26a3287 AM |
7535 | found = s; |
7536 | if ((s->flags & SEC_THREAD_LOCAL) == 0) | |
7537 | break; | |
74541ad4 | 7538 | } |
f26a3287 | 7539 | elf_hash_table (info)->text_index_section = found; |
74541ad4 AM |
7540 | } |
7541 | ||
7542 | /* Find two non-excluded output sections, one for code, one for data. | |
7543 | We'll use their section symbols for some emitted relocs. */ | |
7544 | void | |
7545 | _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) | |
7546 | { | |
7547 | asection *s; | |
f26a3287 | 7548 | asection *found = NULL; |
74541ad4 | 7549 | |
266b05cf | 7550 | /* Data first, since setting text_index_section changes |
7f923b7f | 7551 | _bfd_elf_omit_section_dynsym_default. */ |
74541ad4 | 7552 | for (s = output_bfd->sections; s != NULL; s = s->next) |
f26a3287 AM |
7553 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC |
7554 | && !(s->flags & SEC_READONLY) | |
d00dd7dc | 7555 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7556 | { |
f26a3287 AM |
7557 | found = s; |
7558 | if ((s->flags & SEC_THREAD_LOCAL) == 0) | |
7559 | break; | |
74541ad4 | 7560 | } |
f26a3287 | 7561 | elf_hash_table (info)->data_index_section = found; |
74541ad4 AM |
7562 | |
7563 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
f26a3287 AM |
7564 | if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC |
7565 | && (s->flags & SEC_READONLY) | |
d00dd7dc | 7566 | && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) |
74541ad4 | 7567 | { |
f26a3287 | 7568 | found = s; |
74541ad4 AM |
7569 | break; |
7570 | } | |
f26a3287 | 7571 | elf_hash_table (info)->text_index_section = found; |
74541ad4 AM |
7572 | } |
7573 | ||
f16a9783 MS |
7574 | #define GNU_HASH_SECTION_NAME(bed) \ |
7575 | (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash" | |
7576 | ||
0a1b45a2 | 7577 | bool |
8423293d AM |
7578 | bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) |
7579 | { | |
74541ad4 | 7580 | const struct elf_backend_data *bed; |
23ec1e32 | 7581 | unsigned long section_sym_count; |
96d01d93 | 7582 | bfd_size_type dynsymcount = 0; |
74541ad4 | 7583 | |
8423293d | 7584 | if (!is_elf_hash_table (info->hash)) |
0a1b45a2 | 7585 | return true; |
8423293d | 7586 | |
74541ad4 AM |
7587 | bed = get_elf_backend_data (output_bfd); |
7588 | (*bed->elf_backend_init_index_section) (output_bfd, info); | |
7589 | ||
23ec1e32 MR |
7590 | /* Assign dynsym indices. In a shared library we generate a section |
7591 | symbol for each output section, which come first. Next come all | |
7592 | of the back-end allocated local dynamic syms, followed by the rest | |
7593 | of the global symbols. | |
7594 | ||
7595 | This is usually not needed for static binaries, however backends | |
7596 | can request to always do it, e.g. the MIPS backend uses dynamic | |
7597 | symbol counts to lay out GOT, which will be produced in the | |
7598 | presence of GOT relocations even in static binaries (holding fixed | |
7599 | data in that case, to satisfy those relocations). */ | |
7600 | ||
7601 | if (elf_hash_table (info)->dynamic_sections_created | |
7602 | || bed->always_renumber_dynsyms) | |
7603 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, | |
7604 | §ion_sym_count); | |
7605 | ||
8423293d AM |
7606 | if (elf_hash_table (info)->dynamic_sections_created) |
7607 | { | |
7608 | bfd *dynobj; | |
8423293d | 7609 | asection *s; |
8423293d AM |
7610 | unsigned int dtagcount; |
7611 | ||
7612 | dynobj = elf_hash_table (info)->dynobj; | |
7613 | ||
5a580b3a | 7614 | /* Work out the size of the symbol version section. */ |
3d4d4302 | 7615 | s = bfd_get_linker_section (dynobj, ".gnu.version"); |
5a580b3a | 7616 | BFD_ASSERT (s != NULL); |
d5486c43 | 7617 | if ((s->flags & SEC_EXCLUDE) == 0) |
5a580b3a | 7618 | { |
eea6121a | 7619 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
a50b1753 | 7620 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
5a580b3a | 7621 | if (s->contents == NULL) |
0a1b45a2 | 7622 | return false; |
5a580b3a AM |
7623 | |
7624 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
0a1b45a2 | 7625 | return false; |
5a580b3a AM |
7626 | } |
7627 | ||
7628 | /* Set the size of the .dynsym and .hash sections. We counted | |
7629 | the number of dynamic symbols in elf_link_add_object_symbols. | |
7630 | We will build the contents of .dynsym and .hash when we build | |
7631 | the final symbol table, because until then we do not know the | |
7632 | correct value to give the symbols. We built the .dynstr | |
7633 | section as we went along in elf_link_add_object_symbols. */ | |
cae1fbbb | 7634 | s = elf_hash_table (info)->dynsym; |
5a580b3a | 7635 | BFD_ASSERT (s != NULL); |
eea6121a | 7636 | s->size = dynsymcount * bed->s->sizeof_sym; |
5a580b3a | 7637 | |
d5486c43 L |
7638 | s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); |
7639 | if (s->contents == NULL) | |
0a1b45a2 | 7640 | return false; |
5a580b3a | 7641 | |
d5486c43 L |
7642 | /* The first entry in .dynsym is a dummy symbol. Clear all the |
7643 | section syms, in case we don't output them all. */ | |
7644 | ++section_sym_count; | |
7645 | memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); | |
5a580b3a | 7646 | |
fdc90cb4 JJ |
7647 | elf_hash_table (info)->bucketcount = 0; |
7648 | ||
5a580b3a AM |
7649 | /* Compute the size of the hashing table. As a side effect this |
7650 | computes the hash values for all the names we export. */ | |
fdc90cb4 JJ |
7651 | if (info->emit_hash) |
7652 | { | |
7653 | unsigned long int *hashcodes; | |
14b1c01e | 7654 | struct hash_codes_info hashinf; |
fdc90cb4 JJ |
7655 | bfd_size_type amt; |
7656 | unsigned long int nsyms; | |
7657 | size_t bucketcount; | |
7658 | size_t hash_entry_size; | |
7659 | ||
7660 | /* Compute the hash values for all exported symbols. At the same | |
7661 | time store the values in an array so that we could use them for | |
7662 | optimizations. */ | |
7663 | amt = dynsymcount * sizeof (unsigned long int); | |
a50b1753 | 7664 | hashcodes = (unsigned long int *) bfd_malloc (amt); |
fdc90cb4 | 7665 | if (hashcodes == NULL) |
0a1b45a2 | 7666 | return false; |
14b1c01e | 7667 | hashinf.hashcodes = hashcodes; |
0a1b45a2 | 7668 | hashinf.error = false; |
5a580b3a | 7669 | |
fdc90cb4 JJ |
7670 | /* Put all hash values in HASHCODES. */ |
7671 | elf_link_hash_traverse (elf_hash_table (info), | |
14b1c01e AM |
7672 | elf_collect_hash_codes, &hashinf); |
7673 | if (hashinf.error) | |
4dd07732 AM |
7674 | { |
7675 | free (hashcodes); | |
0a1b45a2 | 7676 | return false; |
4dd07732 | 7677 | } |
5a580b3a | 7678 | |
14b1c01e | 7679 | nsyms = hashinf.hashcodes - hashcodes; |
fdc90cb4 JJ |
7680 | bucketcount |
7681 | = compute_bucket_count (info, hashcodes, nsyms, 0); | |
7682 | free (hashcodes); | |
7683 | ||
4b48e2f6 | 7684 | if (bucketcount == 0 && nsyms > 0) |
0a1b45a2 | 7685 | return false; |
5a580b3a | 7686 | |
fdc90cb4 JJ |
7687 | elf_hash_table (info)->bucketcount = bucketcount; |
7688 | ||
3d4d4302 | 7689 | s = bfd_get_linker_section (dynobj, ".hash"); |
fdc90cb4 JJ |
7690 | BFD_ASSERT (s != NULL); |
7691 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
7692 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
a50b1753 | 7693 | s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 | 7694 | if (s->contents == NULL) |
0a1b45a2 | 7695 | return false; |
fdc90cb4 JJ |
7696 | |
7697 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
7698 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
7699 | s->contents + hash_entry_size); | |
7700 | } | |
7701 | ||
7702 | if (info->emit_gnu_hash) | |
7703 | { | |
7704 | size_t i, cnt; | |
7705 | unsigned char *contents; | |
7706 | struct collect_gnu_hash_codes cinfo; | |
7707 | bfd_size_type amt; | |
7708 | size_t bucketcount; | |
7709 | ||
7710 | memset (&cinfo, 0, sizeof (cinfo)); | |
7711 | ||
7712 | /* Compute the hash values for all exported symbols. At the same | |
7713 | time store the values in an array so that we could use them for | |
7714 | optimizations. */ | |
7715 | amt = dynsymcount * 2 * sizeof (unsigned long int); | |
a50b1753 | 7716 | cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); |
fdc90cb4 | 7717 | if (cinfo.hashcodes == NULL) |
0a1b45a2 | 7718 | return false; |
fdc90cb4 JJ |
7719 | |
7720 | cinfo.hashval = cinfo.hashcodes + dynsymcount; | |
7721 | cinfo.min_dynindx = -1; | |
7722 | cinfo.output_bfd = output_bfd; | |
7723 | cinfo.bed = bed; | |
7724 | ||
7725 | /* Put all hash values in HASHCODES. */ | |
7726 | elf_link_hash_traverse (elf_hash_table (info), | |
7727 | elf_collect_gnu_hash_codes, &cinfo); | |
14b1c01e | 7728 | if (cinfo.error) |
4dd07732 AM |
7729 | { |
7730 | free (cinfo.hashcodes); | |
0a1b45a2 | 7731 | return false; |
4dd07732 | 7732 | } |
fdc90cb4 JJ |
7733 | |
7734 | bucketcount | |
7735 | = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); | |
7736 | ||
7737 | if (bucketcount == 0) | |
7738 | { | |
7739 | free (cinfo.hashcodes); | |
0a1b45a2 | 7740 | return false; |
fdc90cb4 JJ |
7741 | } |
7742 | ||
f16a9783 | 7743 | s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed)); |
fdc90cb4 JJ |
7744 | BFD_ASSERT (s != NULL); |
7745 | ||
7746 | if (cinfo.nsyms == 0) | |
7747 | { | |
f16a9783 | 7748 | /* Empty .gnu.hash or .MIPS.xhash section is special. */ |
fdc90cb4 JJ |
7749 | BFD_ASSERT (cinfo.min_dynindx == -1); |
7750 | free (cinfo.hashcodes); | |
7751 | s->size = 5 * 4 + bed->s->arch_size / 8; | |
a50b1753 | 7752 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 | 7753 | if (contents == NULL) |
0a1b45a2 | 7754 | return false; |
fdc90cb4 JJ |
7755 | s->contents = contents; |
7756 | /* 1 empty bucket. */ | |
7757 | bfd_put_32 (output_bfd, 1, contents); | |
7758 | /* SYMIDX above the special symbol 0. */ | |
7759 | bfd_put_32 (output_bfd, 1, contents + 4); | |
7760 | /* Just one word for bitmask. */ | |
7761 | bfd_put_32 (output_bfd, 1, contents + 8); | |
7762 | /* Only hash fn bloom filter. */ | |
7763 | bfd_put_32 (output_bfd, 0, contents + 12); | |
7764 | /* No hashes are valid - empty bitmask. */ | |
7765 | bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); | |
7766 | /* No hashes in the only bucket. */ | |
7767 | bfd_put_32 (output_bfd, 0, | |
7768 | contents + 16 + bed->s->arch_size / 8); | |
7769 | } | |
7770 | else | |
7771 | { | |
9e6619e2 | 7772 | unsigned long int maskwords, maskbitslog2, x; |
0b33793d | 7773 | BFD_ASSERT (cinfo.min_dynindx != -1); |
fdc90cb4 | 7774 | |
9e6619e2 AM |
7775 | x = cinfo.nsyms; |
7776 | maskbitslog2 = 1; | |
7777 | while ((x >>= 1) != 0) | |
7778 | ++maskbitslog2; | |
fdc90cb4 JJ |
7779 | if (maskbitslog2 < 3) |
7780 | maskbitslog2 = 5; | |
7781 | else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) | |
7782 | maskbitslog2 = maskbitslog2 + 3; | |
7783 | else | |
7784 | maskbitslog2 = maskbitslog2 + 2; | |
7785 | if (bed->s->arch_size == 64) | |
7786 | { | |
7787 | if (maskbitslog2 == 5) | |
7788 | maskbitslog2 = 6; | |
7789 | cinfo.shift1 = 6; | |
7790 | } | |
7791 | else | |
7792 | cinfo.shift1 = 5; | |
7793 | cinfo.mask = (1 << cinfo.shift1) - 1; | |
2ccdbfcc | 7794 | cinfo.shift2 = maskbitslog2; |
fdc90cb4 JJ |
7795 | cinfo.maskbits = 1 << maskbitslog2; |
7796 | maskwords = 1 << (maskbitslog2 - cinfo.shift1); | |
7797 | amt = bucketcount * sizeof (unsigned long int) * 2; | |
7798 | amt += maskwords * sizeof (bfd_vma); | |
a50b1753 | 7799 | cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); |
fdc90cb4 JJ |
7800 | if (cinfo.bitmask == NULL) |
7801 | { | |
7802 | free (cinfo.hashcodes); | |
0a1b45a2 | 7803 | return false; |
fdc90cb4 JJ |
7804 | } |
7805 | ||
a50b1753 | 7806 | cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); |
fdc90cb4 JJ |
7807 | cinfo.indx = cinfo.counts + bucketcount; |
7808 | cinfo.symindx = dynsymcount - cinfo.nsyms; | |
7809 | memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); | |
7810 | ||
7811 | /* Determine how often each hash bucket is used. */ | |
7812 | memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); | |
7813 | for (i = 0; i < cinfo.nsyms; ++i) | |
7814 | ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; | |
7815 | ||
7816 | for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) | |
7817 | if (cinfo.counts[i] != 0) | |
7818 | { | |
7819 | cinfo.indx[i] = cnt; | |
7820 | cnt += cinfo.counts[i]; | |
7821 | } | |
7822 | BFD_ASSERT (cnt == dynsymcount); | |
7823 | cinfo.bucketcount = bucketcount; | |
7824 | cinfo.local_indx = cinfo.min_dynindx; | |
7825 | ||
7826 | s->size = (4 + bucketcount + cinfo.nsyms) * 4; | |
7827 | s->size += cinfo.maskbits / 8; | |
f16a9783 MS |
7828 | if (bed->record_xhash_symbol != NULL) |
7829 | s->size += cinfo.nsyms * 4; | |
a50b1753 | 7830 | contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); |
fdc90cb4 JJ |
7831 | if (contents == NULL) |
7832 | { | |
7833 | free (cinfo.bitmask); | |
7834 | free (cinfo.hashcodes); | |
0a1b45a2 | 7835 | return false; |
fdc90cb4 JJ |
7836 | } |
7837 | ||
7838 | s->contents = contents; | |
7839 | bfd_put_32 (output_bfd, bucketcount, contents); | |
7840 | bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); | |
7841 | bfd_put_32 (output_bfd, maskwords, contents + 8); | |
7842 | bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); | |
7843 | contents += 16 + cinfo.maskbits / 8; | |
7844 | ||
7845 | for (i = 0; i < bucketcount; ++i) | |
7846 | { | |
7847 | if (cinfo.counts[i] == 0) | |
7848 | bfd_put_32 (output_bfd, 0, contents); | |
7849 | else | |
7850 | bfd_put_32 (output_bfd, cinfo.indx[i], contents); | |
7851 | contents += 4; | |
7852 | } | |
7853 | ||
7854 | cinfo.contents = contents; | |
7855 | ||
f16a9783 MS |
7856 | cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents; |
7857 | /* Renumber dynamic symbols, if populating .gnu.hash section. | |
7858 | If using .MIPS.xhash, populate the translation table. */ | |
fdc90cb4 | 7859 | elf_link_hash_traverse (elf_hash_table (info), |
f16a9783 | 7860 | elf_gnu_hash_process_symidx, &cinfo); |
fdc90cb4 JJ |
7861 | |
7862 | contents = s->contents + 16; | |
7863 | for (i = 0; i < maskwords; ++i) | |
7864 | { | |
7865 | bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], | |
7866 | contents); | |
7867 | contents += bed->s->arch_size / 8; | |
7868 | } | |
7869 | ||
7870 | free (cinfo.bitmask); | |
7871 | free (cinfo.hashcodes); | |
7872 | } | |
7873 | } | |
5a580b3a | 7874 | |
3d4d4302 | 7875 | s = bfd_get_linker_section (dynobj, ".dynstr"); |
5a580b3a AM |
7876 | BFD_ASSERT (s != NULL); |
7877 | ||
4ad4eba5 | 7878 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 7879 | |
eea6121a | 7880 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
7881 | |
7882 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
7883 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
0a1b45a2 | 7884 | return false; |
5a580b3a AM |
7885 | } |
7886 | ||
0a1b45a2 | 7887 | return true; |
5a580b3a | 7888 | } |
4d269e42 | 7889 | \f |
4d269e42 AM |
7890 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
7891 | ||
7892 | static void | |
7893 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, | |
7894 | asection *sec) | |
7895 | { | |
dbaa2011 AM |
7896 | BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); |
7897 | sec->sec_info_type = SEC_INFO_TYPE_NONE; | |
4d269e42 AM |
7898 | } |
7899 | ||
7900 | /* Finish SHF_MERGE section merging. */ | |
7901 | ||
0a1b45a2 | 7902 | bool |
630993ec | 7903 | _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) |
4d269e42 AM |
7904 | { |
7905 | bfd *ibfd; | |
7906 | asection *sec; | |
7907 | ||
7908 | if (!is_elf_hash_table (info->hash)) | |
0a1b45a2 | 7909 | return false; |
4d269e42 | 7910 | |
c72f2fb2 | 7911 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
630993ec AM |
7912 | if ((ibfd->flags & DYNAMIC) == 0 |
7913 | && bfd_get_flavour (ibfd) == bfd_target_elf_flavour | |
017e6bce AM |
7914 | && (elf_elfheader (ibfd)->e_ident[EI_CLASS] |
7915 | == get_elf_backend_data (obfd)->s->elfclass)) | |
4d269e42 AM |
7916 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) |
7917 | if ((sec->flags & SEC_MERGE) != 0 | |
7918 | && !bfd_is_abs_section (sec->output_section)) | |
7919 | { | |
7920 | struct bfd_elf_section_data *secdata; | |
7921 | ||
7922 | secdata = elf_section_data (sec); | |
630993ec | 7923 | if (! _bfd_add_merge_section (obfd, |
4d269e42 AM |
7924 | &elf_hash_table (info)->merge_info, |
7925 | sec, &secdata->sec_info)) | |
0a1b45a2 | 7926 | return false; |
4d269e42 | 7927 | else if (secdata->sec_info) |
dbaa2011 | 7928 | sec->sec_info_type = SEC_INFO_TYPE_MERGE; |
4d269e42 AM |
7929 | } |
7930 | ||
7931 | if (elf_hash_table (info)->merge_info != NULL) | |
630993ec | 7932 | _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, |
4d269e42 | 7933 | merge_sections_remove_hook); |
0a1b45a2 | 7934 | return true; |
4d269e42 AM |
7935 | } |
7936 | ||
7937 | /* Create an entry in an ELF linker hash table. */ | |
7938 | ||
7939 | struct bfd_hash_entry * | |
7940 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, | |
7941 | struct bfd_hash_table *table, | |
7942 | const char *string) | |
7943 | { | |
7944 | /* Allocate the structure if it has not already been allocated by a | |
7945 | subclass. */ | |
7946 | if (entry == NULL) | |
7947 | { | |
a50b1753 | 7948 | entry = (struct bfd_hash_entry *) |
ca4be51c | 7949 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); |
4d269e42 AM |
7950 | if (entry == NULL) |
7951 | return entry; | |
7952 | } | |
7953 | ||
7954 | /* Call the allocation method of the superclass. */ | |
7955 | entry = _bfd_link_hash_newfunc (entry, table, string); | |
7956 | if (entry != NULL) | |
7957 | { | |
7958 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
7959 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
7960 | ||
7961 | /* Set local fields. */ | |
7962 | ret->indx = -1; | |
7963 | ret->dynindx = -1; | |
7964 | ret->got = htab->init_got_refcount; | |
7965 | ret->plt = htab->init_plt_refcount; | |
7966 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) | |
7967 | - offsetof (struct elf_link_hash_entry, size))); | |
7968 | /* Assume that we have been called by a non-ELF symbol reader. | |
7969 | This flag is then reset by the code which reads an ELF input | |
7970 | file. This ensures that a symbol created by a non-ELF symbol | |
7971 | reader will have the flag set correctly. */ | |
7972 | ret->non_elf = 1; | |
7973 | } | |
7974 | ||
7975 | return entry; | |
7976 | } | |
7977 | ||
7978 | /* Copy data from an indirect symbol to its direct symbol, hiding the | |
7979 | old indirect symbol. Also used for copying flags to a weakdef. */ | |
7980 | ||
7981 | void | |
7982 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, | |
7983 | struct elf_link_hash_entry *dir, | |
7984 | struct elf_link_hash_entry *ind) | |
7985 | { | |
7986 | struct elf_link_hash_table *htab; | |
7987 | ||
ad172eaa L |
7988 | if (ind->dyn_relocs != NULL) |
7989 | { | |
7990 | if (dir->dyn_relocs != NULL) | |
7991 | { | |
7992 | struct elf_dyn_relocs **pp; | |
7993 | struct elf_dyn_relocs *p; | |
7994 | ||
7995 | /* Add reloc counts against the indirect sym to the direct sym | |
7996 | list. Merge any entries against the same section. */ | |
7997 | for (pp = &ind->dyn_relocs; (p = *pp) != NULL; ) | |
7998 | { | |
7999 | struct elf_dyn_relocs *q; | |
8000 | ||
8001 | for (q = dir->dyn_relocs; q != NULL; q = q->next) | |
8002 | if (q->sec == p->sec) | |
8003 | { | |
8004 | q->pc_count += p->pc_count; | |
8005 | q->count += p->count; | |
8006 | *pp = p->next; | |
8007 | break; | |
8008 | } | |
8009 | if (q == NULL) | |
8010 | pp = &p->next; | |
8011 | } | |
8012 | *pp = dir->dyn_relocs; | |
8013 | } | |
8014 | ||
8015 | dir->dyn_relocs = ind->dyn_relocs; | |
8016 | ind->dyn_relocs = NULL; | |
8017 | } | |
8018 | ||
4d269e42 | 8019 | /* Copy down any references that we may have already seen to the |
e81830c5 | 8020 | symbol which just became indirect. */ |
4d269e42 | 8021 | |
422f1182 | 8022 | if (dir->versioned != versioned_hidden) |
e81830c5 AM |
8023 | dir->ref_dynamic |= ind->ref_dynamic; |
8024 | dir->ref_regular |= ind->ref_regular; | |
8025 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
8026 | dir->non_got_ref |= ind->non_got_ref; | |
8027 | dir->needs_plt |= ind->needs_plt; | |
8028 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
4d269e42 AM |
8029 | |
8030 | if (ind->root.type != bfd_link_hash_indirect) | |
8031 | return; | |
8032 | ||
8033 | /* Copy over the global and procedure linkage table refcount entries. | |
8034 | These may have been already set up by a check_relocs routine. */ | |
8035 | htab = elf_hash_table (info); | |
8036 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
8037 | { | |
8038 | if (dir->got.refcount < 0) | |
8039 | dir->got.refcount = 0; | |
8040 | dir->got.refcount += ind->got.refcount; | |
8041 | ind->got.refcount = htab->init_got_refcount.refcount; | |
8042 | } | |
8043 | ||
8044 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) | |
8045 | { | |
8046 | if (dir->plt.refcount < 0) | |
8047 | dir->plt.refcount = 0; | |
8048 | dir->plt.refcount += ind->plt.refcount; | |
8049 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
8050 | } | |
8051 | ||
8052 | if (ind->dynindx != -1) | |
8053 | { | |
8054 | if (dir->dynindx != -1) | |
8055 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
8056 | dir->dynindx = ind->dynindx; | |
8057 | dir->dynstr_index = ind->dynstr_index; | |
8058 | ind->dynindx = -1; | |
8059 | ind->dynstr_index = 0; | |
8060 | } | |
8061 | } | |
8062 | ||
8063 | void | |
8064 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, | |
8065 | struct elf_link_hash_entry *h, | |
0a1b45a2 | 8066 | bool force_local) |
4d269e42 | 8067 | { |
3aa14d16 L |
8068 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
8069 | if (h->type != STT_GNU_IFUNC) | |
8070 | { | |
8071 | h->plt = elf_hash_table (info)->init_plt_offset; | |
8072 | h->needs_plt = 0; | |
8073 | } | |
4d269e42 AM |
8074 | if (force_local) |
8075 | { | |
8076 | h->forced_local = 1; | |
8077 | if (h->dynindx != -1) | |
8078 | { | |
4d269e42 AM |
8079 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, |
8080 | h->dynstr_index); | |
641338d8 AM |
8081 | h->dynindx = -1; |
8082 | h->dynstr_index = 0; | |
4d269e42 AM |
8083 | } |
8084 | } | |
8085 | } | |
8086 | ||
34a87bb0 L |
8087 | /* Hide a symbol. */ |
8088 | ||
8089 | void | |
8090 | _bfd_elf_link_hide_symbol (bfd *output_bfd, | |
8091 | struct bfd_link_info *info, | |
8092 | struct bfd_link_hash_entry *h) | |
8093 | { | |
8094 | if (is_elf_hash_table (info->hash)) | |
8095 | { | |
8096 | const struct elf_backend_data *bed | |
8097 | = get_elf_backend_data (output_bfd); | |
8098 | struct elf_link_hash_entry *eh | |
8099 | = (struct elf_link_hash_entry *) h; | |
0a1b45a2 | 8100 | bed->elf_backend_hide_symbol (info, eh, true); |
34a87bb0 L |
8101 | eh->def_dynamic = 0; |
8102 | eh->ref_dynamic = 0; | |
8103 | eh->dynamic_def = 0; | |
8104 | } | |
8105 | } | |
8106 | ||
7bf52ea2 AM |
8107 | /* Initialize an ELF linker hash table. *TABLE has been zeroed by our |
8108 | caller. */ | |
4d269e42 | 8109 | |
0a1b45a2 | 8110 | bool |
4d269e42 AM |
8111 | _bfd_elf_link_hash_table_init |
8112 | (struct elf_link_hash_table *table, | |
8113 | bfd *abfd, | |
8114 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
8115 | struct bfd_hash_table *, | |
8116 | const char *), | |
4dfe6ac6 NC |
8117 | unsigned int entsize, |
8118 | enum elf_target_id target_id) | |
4d269e42 | 8119 | { |
0a1b45a2 | 8120 | bool ret; |
4d269e42 AM |
8121 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; |
8122 | ||
4d269e42 AM |
8123 | table->init_got_refcount.refcount = can_refcount - 1; |
8124 | table->init_plt_refcount.refcount = can_refcount - 1; | |
8125 | table->init_got_offset.offset = -(bfd_vma) 1; | |
8126 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
8127 | /* The first dynamic symbol is a dummy. */ | |
8128 | table->dynsymcount = 1; | |
8129 | ||
8130 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); | |
4dfe6ac6 | 8131 | |
4d269e42 | 8132 | table->root.type = bfd_link_elf_hash_table; |
4dfe6ac6 | 8133 | table->hash_table_id = target_id; |
90c14f0c | 8134 | table->target_os = get_elf_backend_data (abfd)->target_os; |
4d269e42 AM |
8135 | |
8136 | return ret; | |
8137 | } | |
8138 | ||
8139 | /* Create an ELF linker hash table. */ | |
8140 | ||
8141 | struct bfd_link_hash_table * | |
8142 | _bfd_elf_link_hash_table_create (bfd *abfd) | |
8143 | { | |
8144 | struct elf_link_hash_table *ret; | |
986f0783 | 8145 | size_t amt = sizeof (struct elf_link_hash_table); |
4d269e42 | 8146 | |
7bf52ea2 | 8147 | ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); |
4d269e42 AM |
8148 | if (ret == NULL) |
8149 | return NULL; | |
8150 | ||
8151 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, | |
4dfe6ac6 NC |
8152 | sizeof (struct elf_link_hash_entry), |
8153 | GENERIC_ELF_DATA)) | |
4d269e42 AM |
8154 | { |
8155 | free (ret); | |
8156 | return NULL; | |
8157 | } | |
d495ab0d | 8158 | ret->root.hash_table_free = _bfd_elf_link_hash_table_free; |
4d269e42 AM |
8159 | |
8160 | return &ret->root; | |
8161 | } | |
8162 | ||
9f7c3e5e AM |
8163 | /* Destroy an ELF linker hash table. */ |
8164 | ||
8165 | void | |
d495ab0d | 8166 | _bfd_elf_link_hash_table_free (bfd *obfd) |
9f7c3e5e | 8167 | { |
d495ab0d AM |
8168 | struct elf_link_hash_table *htab; |
8169 | ||
8170 | htab = (struct elf_link_hash_table *) obfd->link.hash; | |
9f7c3e5e AM |
8171 | if (htab->dynstr != NULL) |
8172 | _bfd_elf_strtab_free (htab->dynstr); | |
8173 | _bfd_merge_sections_free (htab->merge_info); | |
d495ab0d | 8174 | _bfd_generic_link_hash_table_free (obfd); |
9f7c3e5e AM |
8175 | } |
8176 | ||
4d269e42 AM |
8177 | /* This is a hook for the ELF emulation code in the generic linker to |
8178 | tell the backend linker what file name to use for the DT_NEEDED | |
8179 | entry for a dynamic object. */ | |
8180 | ||
8181 | void | |
8182 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) | |
8183 | { | |
8184 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
8185 | && bfd_get_format (abfd) == bfd_object) | |
8186 | elf_dt_name (abfd) = name; | |
8187 | } | |
8188 | ||
8189 | int | |
8190 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
8191 | { | |
8192 | int lib_class; | |
8193 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
8194 | && bfd_get_format (abfd) == bfd_object) | |
8195 | lib_class = elf_dyn_lib_class (abfd); | |
8196 | else | |
8197 | lib_class = 0; | |
8198 | return lib_class; | |
8199 | } | |
8200 | ||
8201 | void | |
8202 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) | |
8203 | { | |
8204 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
8205 | && bfd_get_format (abfd) == bfd_object) | |
8206 | elf_dyn_lib_class (abfd) = lib_class; | |
8207 | } | |
8208 | ||
8209 | /* Get the list of DT_NEEDED entries for a link. This is a hook for | |
8210 | the linker ELF emulation code. */ | |
8211 | ||
8212 | struct bfd_link_needed_list * | |
8213 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, | |
8214 | struct bfd_link_info *info) | |
8215 | { | |
8216 | if (! is_elf_hash_table (info->hash)) | |
8217 | return NULL; | |
8218 | return elf_hash_table (info)->needed; | |
8219 | } | |
8220 | ||
8221 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a | |
8222 | hook for the linker ELF emulation code. */ | |
8223 | ||
8224 | struct bfd_link_needed_list * | |
8225 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, | |
8226 | struct bfd_link_info *info) | |
8227 | { | |
8228 | if (! is_elf_hash_table (info->hash)) | |
8229 | return NULL; | |
8230 | return elf_hash_table (info)->runpath; | |
8231 | } | |
8232 | ||
8233 | /* Get the name actually used for a dynamic object for a link. This | |
8234 | is the SONAME entry if there is one. Otherwise, it is the string | |
8235 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
8236 | ||
8237 | const char * | |
8238 | bfd_elf_get_dt_soname (bfd *abfd) | |
8239 | { | |
8240 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
8241 | && bfd_get_format (abfd) == bfd_object) | |
8242 | return elf_dt_name (abfd); | |
8243 | return NULL; | |
8244 | } | |
8245 | ||
8246 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
8247 | the ELF linker emulation code. */ | |
8248 | ||
0a1b45a2 | 8249 | bool |
4d269e42 AM |
8250 | bfd_elf_get_bfd_needed_list (bfd *abfd, |
8251 | struct bfd_link_needed_list **pneeded) | |
8252 | { | |
8253 | asection *s; | |
8254 | bfd_byte *dynbuf = NULL; | |
cb33740c | 8255 | unsigned int elfsec; |
4d269e42 AM |
8256 | unsigned long shlink; |
8257 | bfd_byte *extdyn, *extdynend; | |
8258 | size_t extdynsize; | |
8259 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
8260 | ||
8261 | *pneeded = NULL; | |
8262 | ||
8263 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
8264 | || bfd_get_format (abfd) != bfd_object) | |
0a1b45a2 | 8265 | return true; |
4d269e42 AM |
8266 | |
8267 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
81ff113f | 8268 | if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0) |
0a1b45a2 | 8269 | return true; |
4d269e42 AM |
8270 | |
8271 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
8272 | goto error_return; | |
8273 | ||
8274 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 8275 | if (elfsec == SHN_BAD) |
4d269e42 AM |
8276 | goto error_return; |
8277 | ||
8278 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
c152c796 | 8279 | |
4d269e42 AM |
8280 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; |
8281 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
8282 | ||
37c59664 AM |
8283 | for (extdyn = dynbuf, extdynend = dynbuf + s->size; |
8284 | (size_t) (extdynend - extdyn) >= extdynsize; | |
8285 | extdyn += extdynsize) | |
4d269e42 AM |
8286 | { |
8287 | Elf_Internal_Dyn dyn; | |
8288 | ||
8289 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
8290 | ||
8291 | if (dyn.d_tag == DT_NULL) | |
8292 | break; | |
8293 | ||
8294 | if (dyn.d_tag == DT_NEEDED) | |
8295 | { | |
8296 | const char *string; | |
8297 | struct bfd_link_needed_list *l; | |
8298 | unsigned int tagv = dyn.d_un.d_val; | |
986f0783 | 8299 | size_t amt; |
4d269e42 AM |
8300 | |
8301 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
8302 | if (string == NULL) | |
8303 | goto error_return; | |
8304 | ||
8305 | amt = sizeof *l; | |
a50b1753 | 8306 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); |
4d269e42 AM |
8307 | if (l == NULL) |
8308 | goto error_return; | |
8309 | ||
8310 | l->by = abfd; | |
8311 | l->name = string; | |
8312 | l->next = *pneeded; | |
8313 | *pneeded = l; | |
8314 | } | |
8315 | } | |
8316 | ||
8317 | free (dynbuf); | |
8318 | ||
0a1b45a2 | 8319 | return true; |
4d269e42 AM |
8320 | |
8321 | error_return: | |
c9594989 | 8322 | free (dynbuf); |
0a1b45a2 | 8323 | return false; |
4d269e42 AM |
8324 | } |
8325 | ||
8326 | struct elf_symbuf_symbol | |
8327 | { | |
8328 | unsigned long st_name; /* Symbol name, index in string tbl */ | |
8329 | unsigned char st_info; /* Type and binding attributes */ | |
8330 | unsigned char st_other; /* Visibilty, and target specific */ | |
8331 | }; | |
8332 | ||
8333 | struct elf_symbuf_head | |
8334 | { | |
8335 | struct elf_symbuf_symbol *ssym; | |
ef53be89 | 8336 | size_t count; |
4d269e42 AM |
8337 | unsigned int st_shndx; |
8338 | }; | |
8339 | ||
8340 | struct elf_symbol | |
8341 | { | |
8342 | union | |
8343 | { | |
8344 | Elf_Internal_Sym *isym; | |
8345 | struct elf_symbuf_symbol *ssym; | |
dcea6a95 | 8346 | void *p; |
4d269e42 AM |
8347 | } u; |
8348 | const char *name; | |
8349 | }; | |
8350 | ||
8351 | /* Sort references to symbols by ascending section number. */ | |
8352 | ||
8353 | static int | |
8354 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
8355 | { | |
8356 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
8357 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
8358 | ||
dcea6a95 AM |
8359 | if (s1->st_shndx != s2->st_shndx) |
8360 | return s1->st_shndx > s2->st_shndx ? 1 : -1; | |
8361 | /* Final sort by the address of the sym in the symbuf ensures | |
8362 | a stable sort. */ | |
8363 | if (s1 != s2) | |
8364 | return s1 > s2 ? 1 : -1; | |
8365 | return 0; | |
4d269e42 AM |
8366 | } |
8367 | ||
8368 | static int | |
8369 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
8370 | { | |
8371 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
8372 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
dcea6a95 AM |
8373 | int ret = strcmp (s1->name, s2->name); |
8374 | if (ret != 0) | |
8375 | return ret; | |
8376 | if (s1->u.p != s2->u.p) | |
8377 | return s1->u.p > s2->u.p ? 1 : -1; | |
8378 | return 0; | |
4d269e42 AM |
8379 | } |
8380 | ||
8381 | static struct elf_symbuf_head * | |
ef53be89 | 8382 | elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) |
4d269e42 | 8383 | { |
14b1c01e | 8384 | Elf_Internal_Sym **ind, **indbufend, **indbuf; |
4d269e42 AM |
8385 | struct elf_symbuf_symbol *ssym; |
8386 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
446f7ed5 | 8387 | size_t i, shndx_count, total_size, amt; |
4d269e42 | 8388 | |
446f7ed5 AM |
8389 | amt = symcount * sizeof (*indbuf); |
8390 | indbuf = (Elf_Internal_Sym **) bfd_malloc (amt); | |
4d269e42 AM |
8391 | if (indbuf == NULL) |
8392 | return NULL; | |
8393 | ||
8394 | for (ind = indbuf, i = 0; i < symcount; i++) | |
3818d4ab | 8395 | if (isymbuf[i].st_shndx != SHN_UNDEF) |
4d269e42 AM |
8396 | *ind++ = &isymbuf[i]; |
8397 | indbufend = ind; | |
8398 | ||
8399 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
8400 | elf_sort_elf_symbol); | |
8401 | ||
8402 | shndx_count = 0; | |
8403 | if (indbufend > indbuf) | |
8404 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
8405 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
8406 | shndx_count++; | |
8407 | ||
3ae181ee L |
8408 | total_size = ((shndx_count + 1) * sizeof (*ssymbuf) |
8409 | + (indbufend - indbuf) * sizeof (*ssym)); | |
a50b1753 | 8410 | ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); |
4d269e42 AM |
8411 | if (ssymbuf == NULL) |
8412 | { | |
8413 | free (indbuf); | |
8414 | return NULL; | |
8415 | } | |
8416 | ||
3ae181ee | 8417 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); |
4d269e42 AM |
8418 | ssymbuf->ssym = NULL; |
8419 | ssymbuf->count = shndx_count; | |
8420 | ssymbuf->st_shndx = 0; | |
8421 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
8422 | { | |
8423 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
8424 | { | |
8425 | ssymhead++; | |
8426 | ssymhead->ssym = ssym; | |
8427 | ssymhead->count = 0; | |
8428 | ssymhead->st_shndx = (*ind)->st_shndx; | |
8429 | } | |
8430 | ssym->st_name = (*ind)->st_name; | |
8431 | ssym->st_info = (*ind)->st_info; | |
8432 | ssym->st_other = (*ind)->st_other; | |
8433 | ssymhead->count++; | |
8434 | } | |
ef53be89 | 8435 | BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count |
60159858 | 8436 | && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size); |
4d269e42 AM |
8437 | |
8438 | free (indbuf); | |
8439 | return ssymbuf; | |
8440 | } | |
8441 | ||
8442 | /* Check if 2 sections define the same set of local and global | |
8443 | symbols. */ | |
8444 | ||
0a1b45a2 | 8445 | static bool |
4d269e42 AM |
8446 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
8447 | struct bfd_link_info *info) | |
8448 | { | |
8449 | bfd *bfd1, *bfd2; | |
8450 | const struct elf_backend_data *bed1, *bed2; | |
8451 | Elf_Internal_Shdr *hdr1, *hdr2; | |
ef53be89 | 8452 | size_t symcount1, symcount2; |
4d269e42 AM |
8453 | Elf_Internal_Sym *isymbuf1, *isymbuf2; |
8454 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; | |
8455 | Elf_Internal_Sym *isym, *isymend; | |
8456 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
3818d4ab | 8457 | size_t count1, count2, sec_count1, sec_count2, i; |
cb33740c | 8458 | unsigned int shndx1, shndx2; |
0a1b45a2 AM |
8459 | bool result; |
8460 | bool ignore_section_symbol_p; | |
4d269e42 AM |
8461 | |
8462 | bfd1 = sec1->owner; | |
8463 | bfd2 = sec2->owner; | |
8464 | ||
4d269e42 AM |
8465 | /* Both sections have to be in ELF. */ |
8466 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
8467 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
0a1b45a2 | 8468 | return false; |
4d269e42 AM |
8469 | |
8470 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
0a1b45a2 | 8471 | return false; |
4d269e42 | 8472 | |
4d269e42 AM |
8473 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); |
8474 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
cb33740c | 8475 | if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) |
0a1b45a2 | 8476 | return false; |
4d269e42 AM |
8477 | |
8478 | bed1 = get_elf_backend_data (bfd1); | |
8479 | bed2 = get_elf_backend_data (bfd2); | |
8480 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
8481 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
8482 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
8483 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
8484 | ||
8485 | if (symcount1 == 0 || symcount2 == 0) | |
0a1b45a2 | 8486 | return false; |
4d269e42 | 8487 | |
0a1b45a2 | 8488 | result = false; |
4d269e42 AM |
8489 | isymbuf1 = NULL; |
8490 | isymbuf2 = NULL; | |
a50b1753 NC |
8491 | ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; |
8492 | ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; | |
4d269e42 | 8493 | |
3818d4ab L |
8494 | /* Ignore section symbols only when matching non-debugging sections |
8495 | or linkonce section with comdat section. */ | |
8496 | ignore_section_symbol_p | |
8497 | = ((sec1->flags & SEC_DEBUGGING) == 0 | |
8498 | || ((elf_section_flags (sec1) & SHF_GROUP) | |
8499 | != (elf_section_flags (sec2) & SHF_GROUP))); | |
8500 | ||
4d269e42 AM |
8501 | if (ssymbuf1 == NULL) |
8502 | { | |
8503 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
8504 | NULL, NULL, NULL); | |
8505 | if (isymbuf1 == NULL) | |
8506 | goto done; | |
8507 | ||
67411cbf | 8508 | if (info != NULL && !info->reduce_memory_overheads) |
dcea6a95 AM |
8509 | { |
8510 | ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1); | |
8511 | elf_tdata (bfd1)->symbuf = ssymbuf1; | |
8512 | } | |
4d269e42 AM |
8513 | } |
8514 | ||
8515 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) | |
8516 | { | |
8517 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
8518 | NULL, NULL, NULL); | |
8519 | if (isymbuf2 == NULL) | |
8520 | goto done; | |
8521 | ||
67411cbf | 8522 | if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads) |
dcea6a95 AM |
8523 | { |
8524 | ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2); | |
8525 | elf_tdata (bfd2)->symbuf = ssymbuf2; | |
8526 | } | |
4d269e42 AM |
8527 | } |
8528 | ||
8529 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) | |
8530 | { | |
8531 | /* Optimized faster version. */ | |
ef53be89 | 8532 | size_t lo, hi, mid; |
4d269e42 AM |
8533 | struct elf_symbol *symp; |
8534 | struct elf_symbuf_symbol *ssym, *ssymend; | |
8535 | ||
8536 | lo = 0; | |
8537 | hi = ssymbuf1->count; | |
8538 | ssymbuf1++; | |
8539 | count1 = 0; | |
3818d4ab | 8540 | sec_count1 = 0; |
4d269e42 AM |
8541 | while (lo < hi) |
8542 | { | |
8543 | mid = (lo + hi) / 2; | |
cb33740c | 8544 | if (shndx1 < ssymbuf1[mid].st_shndx) |
4d269e42 | 8545 | hi = mid; |
cb33740c | 8546 | else if (shndx1 > ssymbuf1[mid].st_shndx) |
4d269e42 AM |
8547 | lo = mid + 1; |
8548 | else | |
8549 | { | |
8550 | count1 = ssymbuf1[mid].count; | |
8551 | ssymbuf1 += mid; | |
8552 | break; | |
8553 | } | |
8554 | } | |
3818d4ab L |
8555 | if (ignore_section_symbol_p) |
8556 | { | |
8557 | for (i = 0; i < count1; i++) | |
8558 | if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION) | |
8559 | sec_count1++; | |
8560 | count1 -= sec_count1; | |
8561 | } | |
4d269e42 AM |
8562 | |
8563 | lo = 0; | |
8564 | hi = ssymbuf2->count; | |
8565 | ssymbuf2++; | |
8566 | count2 = 0; | |
3818d4ab | 8567 | sec_count2 = 0; |
4d269e42 AM |
8568 | while (lo < hi) |
8569 | { | |
8570 | mid = (lo + hi) / 2; | |
cb33740c | 8571 | if (shndx2 < ssymbuf2[mid].st_shndx) |
4d269e42 | 8572 | hi = mid; |
cb33740c | 8573 | else if (shndx2 > ssymbuf2[mid].st_shndx) |
4d269e42 AM |
8574 | lo = mid + 1; |
8575 | else | |
8576 | { | |
8577 | count2 = ssymbuf2[mid].count; | |
8578 | ssymbuf2 += mid; | |
8579 | break; | |
8580 | } | |
8581 | } | |
3818d4ab L |
8582 | if (ignore_section_symbol_p) |
8583 | { | |
8584 | for (i = 0; i < count2; i++) | |
8585 | if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION) | |
8586 | sec_count2++; | |
8587 | count2 -= sec_count2; | |
8588 | } | |
4d269e42 AM |
8589 | |
8590 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8591 | goto done; | |
8592 | ||
ca4be51c AM |
8593 | symtable1 |
8594 | = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); | |
8595 | symtable2 | |
8596 | = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); | |
4d269e42 AM |
8597 | if (symtable1 == NULL || symtable2 == NULL) |
8598 | goto done; | |
8599 | ||
8600 | symp = symtable1; | |
3818d4ab L |
8601 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1; |
8602 | ssym < ssymend; ssym++) | |
8603 | if (sec_count1 == 0 | |
8604 | || ELF_ST_TYPE (ssym->st_info) != STT_SECTION) | |
8605 | { | |
8606 | symp->u.ssym = ssym; | |
8607 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
8608 | hdr1->sh_link, | |
8609 | ssym->st_name); | |
8610 | symp++; | |
8611 | } | |
4d269e42 AM |
8612 | |
8613 | symp = symtable2; | |
3818d4ab L |
8614 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2; |
8615 | ssym < ssymend; ssym++) | |
8616 | if (sec_count2 == 0 | |
8617 | || ELF_ST_TYPE (ssym->st_info) != STT_SECTION) | |
8618 | { | |
8619 | symp->u.ssym = ssym; | |
8620 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
8621 | hdr2->sh_link, | |
8622 | ssym->st_name); | |
8623 | symp++; | |
8624 | } | |
4d269e42 AM |
8625 | |
8626 | /* Sort symbol by name. */ | |
8627 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8628 | elf_sym_name_compare); | |
8629 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8630 | elf_sym_name_compare); | |
8631 | ||
8632 | for (i = 0; i < count1; i++) | |
8633 | /* Two symbols must have the same binding, type and name. */ | |
8634 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
8635 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
8636 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8637 | goto done; | |
8638 | ||
0a1b45a2 | 8639 | result = true; |
4d269e42 AM |
8640 | goto done; |
8641 | } | |
8642 | ||
a50b1753 NC |
8643 | symtable1 = (struct elf_symbol *) |
8644 | bfd_malloc (symcount1 * sizeof (struct elf_symbol)); | |
8645 | symtable2 = (struct elf_symbol *) | |
8646 | bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
4d269e42 AM |
8647 | if (symtable1 == NULL || symtable2 == NULL) |
8648 | goto done; | |
8649 | ||
8650 | /* Count definitions in the section. */ | |
8651 | count1 = 0; | |
8652 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
3818d4ab L |
8653 | if (isym->st_shndx == shndx1 |
8654 | && (!ignore_section_symbol_p | |
8655 | || ELF_ST_TYPE (isym->st_info) != STT_SECTION)) | |
4d269e42 AM |
8656 | symtable1[count1++].u.isym = isym; |
8657 | ||
8658 | count2 = 0; | |
8659 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
3818d4ab L |
8660 | if (isym->st_shndx == shndx2 |
8661 | && (!ignore_section_symbol_p | |
8662 | || ELF_ST_TYPE (isym->st_info) != STT_SECTION)) | |
4d269e42 AM |
8663 | symtable2[count2++].u.isym = isym; |
8664 | ||
8665 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
8666 | goto done; | |
8667 | ||
8668 | for (i = 0; i < count1; i++) | |
8669 | symtable1[i].name | |
8670 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
8671 | symtable1[i].u.isym->st_name); | |
8672 | ||
8673 | for (i = 0; i < count2; i++) | |
8674 | symtable2[i].name | |
8675 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
8676 | symtable2[i].u.isym->st_name); | |
8677 | ||
8678 | /* Sort symbol by name. */ | |
8679 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
8680 | elf_sym_name_compare); | |
8681 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
8682 | elf_sym_name_compare); | |
8683 | ||
8684 | for (i = 0; i < count1; i++) | |
8685 | /* Two symbols must have the same binding, type and name. */ | |
8686 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info | |
8687 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
8688 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
8689 | goto done; | |
8690 | ||
0a1b45a2 | 8691 | result = true; |
4d269e42 | 8692 | |
dc1e8a47 | 8693 | done: |
c9594989 AM |
8694 | free (symtable1); |
8695 | free (symtable2); | |
8696 | free (isymbuf1); | |
8697 | free (isymbuf2); | |
4d269e42 AM |
8698 | |
8699 | return result; | |
8700 | } | |
8701 | ||
8702 | /* Return TRUE if 2 section types are compatible. */ | |
8703 | ||
0a1b45a2 | 8704 | bool |
4d269e42 AM |
8705 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, |
8706 | bfd *bbfd, const asection *bsec) | |
8707 | { | |
8708 | if (asec == NULL | |
8709 | || bsec == NULL | |
8710 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
8711 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
0a1b45a2 | 8712 | return true; |
4d269e42 AM |
8713 | |
8714 | return elf_section_type (asec) == elf_section_type (bsec); | |
8715 | } | |
8716 | \f | |
c152c796 AM |
8717 | /* Final phase of ELF linker. */ |
8718 | ||
8719 | /* A structure we use to avoid passing large numbers of arguments. */ | |
8720 | ||
8721 | struct elf_final_link_info | |
8722 | { | |
8723 | /* General link information. */ | |
8724 | struct bfd_link_info *info; | |
8725 | /* Output BFD. */ | |
8726 | bfd *output_bfd; | |
8727 | /* Symbol string table. */ | |
ef10c3ac | 8728 | struct elf_strtab_hash *symstrtab; |
c152c796 AM |
8729 | /* .hash section. */ |
8730 | asection *hash_sec; | |
8731 | /* symbol version section (.gnu.version). */ | |
8732 | asection *symver_sec; | |
8733 | /* Buffer large enough to hold contents of any section. */ | |
8734 | bfd_byte *contents; | |
8735 | /* Buffer large enough to hold external relocs of any section. */ | |
8736 | void *external_relocs; | |
8737 | /* Buffer large enough to hold internal relocs of any section. */ | |
8738 | Elf_Internal_Rela *internal_relocs; | |
8739 | /* Buffer large enough to hold external local symbols of any input | |
8740 | BFD. */ | |
8741 | bfd_byte *external_syms; | |
8742 | /* And a buffer for symbol section indices. */ | |
8743 | Elf_External_Sym_Shndx *locsym_shndx; | |
8744 | /* Buffer large enough to hold internal local symbols of any input | |
8745 | BFD. */ | |
8746 | Elf_Internal_Sym *internal_syms; | |
8747 | /* Array large enough to hold a symbol index for each local symbol | |
8748 | of any input BFD. */ | |
8749 | long *indices; | |
8750 | /* Array large enough to hold a section pointer for each local | |
8751 | symbol of any input BFD. */ | |
8752 | asection **sections; | |
ef10c3ac | 8753 | /* Buffer for SHT_SYMTAB_SHNDX section. */ |
c152c796 | 8754 | Elf_External_Sym_Shndx *symshndxbuf; |
ffbc01cc AM |
8755 | /* Number of STT_FILE syms seen. */ |
8756 | size_t filesym_count; | |
496afd17 L |
8757 | /* Local symbol hash table. */ |
8758 | struct bfd_hash_table local_hash_table; | |
c152c796 AM |
8759 | }; |
8760 | ||
496afd17 L |
8761 | struct local_hash_entry |
8762 | { | |
8763 | /* Base hash table entry structure. */ | |
8764 | struct bfd_hash_entry root; | |
8765 | /* Size of the local symbol name. */ | |
8766 | size_t size; | |
8767 | /* Number of the duplicated local symbol names. */ | |
8768 | long count; | |
8769 | }; | |
8770 | ||
8771 | /* Create an entry in the local symbol hash table. */ | |
8772 | ||
8773 | static struct bfd_hash_entry * | |
8774 | local_hash_newfunc (struct bfd_hash_entry *entry, | |
8775 | struct bfd_hash_table *table, | |
8776 | const char *string) | |
8777 | { | |
8778 | ||
8779 | /* Allocate the structure if it has not already been allocated by a | |
8780 | subclass. */ | |
8781 | if (entry == NULL) | |
8782 | { | |
8783 | entry = bfd_hash_allocate (table, | |
8784 | sizeof (struct local_hash_entry)); | |
8785 | if (entry == NULL) | |
8786 | return entry; | |
8787 | } | |
8788 | ||
8789 | /* Call the allocation method of the superclass. */ | |
8790 | entry = bfd_hash_newfunc (entry, table, string); | |
8791 | if (entry != NULL) | |
8792 | { | |
8793 | ((struct local_hash_entry *) entry)->count = 0; | |
8794 | ((struct local_hash_entry *) entry)->size = 0; | |
8795 | } | |
8796 | ||
8797 | return entry; | |
8798 | } | |
8799 | ||
c152c796 AM |
8800 | /* This struct is used to pass information to elf_link_output_extsym. */ |
8801 | ||
8802 | struct elf_outext_info | |
8803 | { | |
0a1b45a2 AM |
8804 | bool failed; |
8805 | bool localsyms; | |
8806 | bool file_sym_done; | |
8b127cbc | 8807 | struct elf_final_link_info *flinfo; |
c152c796 AM |
8808 | }; |
8809 | ||
d9352518 DB |
8810 | |
8811 | /* Support for evaluating a complex relocation. | |
8812 | ||
8813 | Complex relocations are generalized, self-describing relocations. The | |
8814 | implementation of them consists of two parts: complex symbols, and the | |
a0c8462f | 8815 | relocations themselves. |
d9352518 | 8816 | |
4b69ce9b | 8817 | The relocations use a reserved elf-wide relocation type code (R_RELC |
d9352518 DB |
8818 | external / BFD_RELOC_RELC internal) and an encoding of relocation field |
8819 | information (start bit, end bit, word width, etc) into the addend. This | |
8820 | information is extracted from CGEN-generated operand tables within gas. | |
8821 | ||
4b69ce9b | 8822 | Complex symbols are mangled symbols (STT_RELC external / BSF_RELC |
d9352518 DB |
8823 | internal) representing prefix-notation expressions, including but not |
8824 | limited to those sorts of expressions normally encoded as addends in the | |
8825 | addend field. The symbol mangling format is: | |
8826 | ||
8827 | <node> := <literal> | |
07d6d2b8 AM |
8828 | | <unary-operator> ':' <node> |
8829 | | <binary-operator> ':' <node> ':' <node> | |
d9352518 DB |
8830 | ; |
8831 | ||
8832 | <literal> := 's' <digits=N> ':' <N character symbol name> | |
07d6d2b8 | 8833 | | 'S' <digits=N> ':' <N character section name> |
d9352518 DB |
8834 | | '#' <hexdigits> |
8835 | ; | |
8836 | ||
8837 | <binary-operator> := as in C | |
8838 | <unary-operator> := as in C, plus "0-" for unambiguous negation. */ | |
8839 | ||
8840 | static void | |
a0c8462f AM |
8841 | set_symbol_value (bfd *bfd_with_globals, |
8842 | Elf_Internal_Sym *isymbuf, | |
8843 | size_t locsymcount, | |
8844 | size_t symidx, | |
8845 | bfd_vma val) | |
d9352518 | 8846 | { |
8977835c AM |
8847 | struct elf_link_hash_entry **sym_hashes; |
8848 | struct elf_link_hash_entry *h; | |
8849 | size_t extsymoff = locsymcount; | |
d9352518 | 8850 | |
8977835c | 8851 | if (symidx < locsymcount) |
d9352518 | 8852 | { |
8977835c AM |
8853 | Elf_Internal_Sym *sym; |
8854 | ||
8855 | sym = isymbuf + symidx; | |
8856 | if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) | |
8857 | { | |
8858 | /* It is a local symbol: move it to the | |
8859 | "absolute" section and give it a value. */ | |
8860 | sym->st_shndx = SHN_ABS; | |
8861 | sym->st_value = val; | |
8862 | return; | |
8863 | } | |
8864 | BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); | |
8865 | extsymoff = 0; | |
d9352518 | 8866 | } |
8977835c AM |
8867 | |
8868 | /* It is a global symbol: set its link type | |
8869 | to "defined" and give it a value. */ | |
8870 | ||
8871 | sym_hashes = elf_sym_hashes (bfd_with_globals); | |
8872 | h = sym_hashes [symidx - extsymoff]; | |
8873 | while (h->root.type == bfd_link_hash_indirect | |
8874 | || h->root.type == bfd_link_hash_warning) | |
8875 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8876 | h->root.type = bfd_link_hash_defined; | |
8877 | h->root.u.def.value = val; | |
8878 | h->root.u.def.section = bfd_abs_section_ptr; | |
d9352518 DB |
8879 | } |
8880 | ||
0a1b45a2 | 8881 | static bool |
a0c8462f AM |
8882 | resolve_symbol (const char *name, |
8883 | bfd *input_bfd, | |
8b127cbc | 8884 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
8885 | bfd_vma *result, |
8886 | Elf_Internal_Sym *isymbuf, | |
8887 | size_t locsymcount) | |
d9352518 | 8888 | { |
a0c8462f AM |
8889 | Elf_Internal_Sym *sym; |
8890 | struct bfd_link_hash_entry *global_entry; | |
8891 | const char *candidate = NULL; | |
8892 | Elf_Internal_Shdr *symtab_hdr; | |
8893 | size_t i; | |
8894 | ||
d9352518 DB |
8895 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
8896 | ||
8897 | for (i = 0; i < locsymcount; ++ i) | |
8898 | { | |
8977835c | 8899 | sym = isymbuf + i; |
d9352518 DB |
8900 | |
8901 | if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
8902 | continue; | |
8903 | ||
8904 | candidate = bfd_elf_string_from_elf_section (input_bfd, | |
8905 | symtab_hdr->sh_link, | |
8906 | sym->st_name); | |
8907 | #ifdef DEBUG | |
0f02bbd9 AM |
8908 | printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", |
8909 | name, candidate, (unsigned long) sym->st_value); | |
d9352518 DB |
8910 | #endif |
8911 | if (candidate && strcmp (candidate, name) == 0) | |
8912 | { | |
8b127cbc | 8913 | asection *sec = flinfo->sections [i]; |
d9352518 | 8914 | |
0f02bbd9 AM |
8915 | *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); |
8916 | *result += sec->output_offset + sec->output_section->vma; | |
d9352518 | 8917 | #ifdef DEBUG |
0f02bbd9 AM |
8918 | printf ("Found symbol with value %8.8lx\n", |
8919 | (unsigned long) *result); | |
d9352518 | 8920 | #endif |
0a1b45a2 | 8921 | return true; |
d9352518 DB |
8922 | } |
8923 | } | |
8924 | ||
8925 | /* Hmm, haven't found it yet. perhaps it is a global. */ | |
8b127cbc | 8926 | global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, |
0a1b45a2 | 8927 | false, false, true); |
d9352518 | 8928 | if (!global_entry) |
0a1b45a2 | 8929 | return false; |
a0c8462f | 8930 | |
d9352518 DB |
8931 | if (global_entry->type == bfd_link_hash_defined |
8932 | || global_entry->type == bfd_link_hash_defweak) | |
8933 | { | |
a0c8462f AM |
8934 | *result = (global_entry->u.def.value |
8935 | + global_entry->u.def.section->output_section->vma | |
8936 | + global_entry->u.def.section->output_offset); | |
d9352518 | 8937 | #ifdef DEBUG |
0f02bbd9 AM |
8938 | printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", |
8939 | global_entry->root.string, (unsigned long) *result); | |
d9352518 | 8940 | #endif |
0a1b45a2 | 8941 | return true; |
a0c8462f | 8942 | } |
d9352518 | 8943 | |
0a1b45a2 | 8944 | return false; |
d9352518 DB |
8945 | } |
8946 | ||
37b01f6a DG |
8947 | /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in |
8948 | bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section | |
8949 | names like "foo.end" which is the end address of section "foo". */ | |
07d6d2b8 | 8950 | |
0a1b45a2 | 8951 | static bool |
a0c8462f AM |
8952 | resolve_section (const char *name, |
8953 | asection *sections, | |
37b01f6a DG |
8954 | bfd_vma *result, |
8955 | bfd * abfd) | |
d9352518 | 8956 | { |
a0c8462f AM |
8957 | asection *curr; |
8958 | unsigned int len; | |
d9352518 | 8959 | |
a0c8462f | 8960 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8961 | if (strcmp (curr->name, name) == 0) |
8962 | { | |
8963 | *result = curr->vma; | |
0a1b45a2 | 8964 | return true; |
d9352518 DB |
8965 | } |
8966 | ||
8967 | /* Hmm. still haven't found it. try pseudo-section names. */ | |
37b01f6a | 8968 | /* FIXME: This could be coded more efficiently... */ |
a0c8462f | 8969 | for (curr = sections; curr; curr = curr->next) |
d9352518 DB |
8970 | { |
8971 | len = strlen (curr->name); | |
a0c8462f | 8972 | if (len > strlen (name)) |
d9352518 DB |
8973 | continue; |
8974 | ||
8975 | if (strncmp (curr->name, name, len) == 0) | |
8976 | { | |
3f3328b8 | 8977 | if (startswith (name + len, ".end")) |
d9352518 | 8978 | { |
61826503 | 8979 | *result = (curr->vma |
bb294208 | 8980 | + curr->size / bfd_octets_per_byte (abfd, curr)); |
0a1b45a2 | 8981 | return true; |
d9352518 DB |
8982 | } |
8983 | ||
8984 | /* Insert more pseudo-section names here, if you like. */ | |
8985 | } | |
8986 | } | |
a0c8462f | 8987 | |
0a1b45a2 | 8988 | return false; |
d9352518 DB |
8989 | } |
8990 | ||
8991 | static void | |
a0c8462f | 8992 | undefined_reference (const char *reftype, const char *name) |
d9352518 | 8993 | { |
695344c0 | 8994 | /* xgettext:c-format */ |
a0c8462f AM |
8995 | _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), |
8996 | reftype, name); | |
4b69ce9b | 8997 | bfd_set_error (bfd_error_bad_value); |
d9352518 DB |
8998 | } |
8999 | ||
0a1b45a2 | 9000 | static bool |
a0c8462f AM |
9001 | eval_symbol (bfd_vma *result, |
9002 | const char **symp, | |
9003 | bfd *input_bfd, | |
8b127cbc | 9004 | struct elf_final_link_info *flinfo, |
a0c8462f AM |
9005 | bfd_vma dot, |
9006 | Elf_Internal_Sym *isymbuf, | |
9007 | size_t locsymcount, | |
9008 | int signed_p) | |
d9352518 | 9009 | { |
4b93929b NC |
9010 | size_t len; |
9011 | size_t symlen; | |
a0c8462f AM |
9012 | bfd_vma a; |
9013 | bfd_vma b; | |
4b93929b | 9014 | char symbuf[4096]; |
0f02bbd9 | 9015 | const char *sym = *symp; |
a0c8462f | 9016 | const char *symend; |
0a1b45a2 | 9017 | bool symbol_is_section = false; |
d9352518 DB |
9018 | |
9019 | len = strlen (sym); | |
9020 | symend = sym + len; | |
9021 | ||
4b93929b | 9022 | if (len < 1 || len > sizeof (symbuf)) |
d9352518 DB |
9023 | { |
9024 | bfd_set_error (bfd_error_invalid_operation); | |
0a1b45a2 | 9025 | return false; |
d9352518 | 9026 | } |
a0c8462f | 9027 | |
d9352518 DB |
9028 | switch (* sym) |
9029 | { | |
9030 | case '.': | |
0f02bbd9 AM |
9031 | *result = dot; |
9032 | *symp = sym + 1; | |
0a1b45a2 | 9033 | return true; |
d9352518 DB |
9034 | |
9035 | case '#': | |
0f02bbd9 AM |
9036 | ++sym; |
9037 | *result = strtoul (sym, (char **) symp, 16); | |
0a1b45a2 | 9038 | return true; |
d9352518 DB |
9039 | |
9040 | case 'S': | |
0a1b45a2 | 9041 | symbol_is_section = true; |
1a0670f3 | 9042 | /* Fall through. */ |
a0c8462f | 9043 | case 's': |
0f02bbd9 AM |
9044 | ++sym; |
9045 | symlen = strtol (sym, (char **) symp, 10); | |
9046 | sym = *symp + 1; /* Skip the trailing ':'. */ | |
d9352518 | 9047 | |
4b93929b | 9048 | if (symend < sym || symlen + 1 > sizeof (symbuf)) |
d9352518 DB |
9049 | { |
9050 | bfd_set_error (bfd_error_invalid_operation); | |
0a1b45a2 | 9051 | return false; |
d9352518 DB |
9052 | } |
9053 | ||
9054 | memcpy (symbuf, sym, symlen); | |
a0c8462f | 9055 | symbuf[symlen] = '\0'; |
0f02bbd9 | 9056 | *symp = sym + symlen; |
a0c8462f AM |
9057 | |
9058 | /* Is it always possible, with complex symbols, that gas "mis-guessed" | |
d9352518 DB |
9059 | the symbol as a section, or vice-versa. so we're pretty liberal in our |
9060 | interpretation here; section means "try section first", not "must be a | |
9061 | section", and likewise with symbol. */ | |
9062 | ||
a0c8462f | 9063 | if (symbol_is_section) |
d9352518 | 9064 | { |
37b01f6a | 9065 | if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) |
8b127cbc | 9066 | && !resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 9067 | isymbuf, locsymcount)) |
d9352518 DB |
9068 | { |
9069 | undefined_reference ("section", symbuf); | |
0a1b45a2 | 9070 | return false; |
d9352518 | 9071 | } |
a0c8462f AM |
9072 | } |
9073 | else | |
d9352518 | 9074 | { |
8b127cbc | 9075 | if (!resolve_symbol (symbuf, input_bfd, flinfo, result, |
8977835c | 9076 | isymbuf, locsymcount) |
8b127cbc | 9077 | && !resolve_section (symbuf, flinfo->output_bfd->sections, |
37b01f6a | 9078 | result, input_bfd)) |
d9352518 DB |
9079 | { |
9080 | undefined_reference ("symbol", symbuf); | |
0a1b45a2 | 9081 | return false; |
d9352518 DB |
9082 | } |
9083 | } | |
9084 | ||
0a1b45a2 | 9085 | return true; |
a0c8462f | 9086 | |
d9352518 DB |
9087 | /* All that remains are operators. */ |
9088 | ||
9089 | #define UNARY_OP(op) \ | |
3f3328b8 | 9090 | if (startswith (sym, #op)) \ |
d9352518 DB |
9091 | { \ |
9092 | sym += strlen (#op); \ | |
a0c8462f AM |
9093 | if (*sym == ':') \ |
9094 | ++sym; \ | |
0f02bbd9 | 9095 | *symp = sym; \ |
8b127cbc | 9096 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 9097 | isymbuf, locsymcount, signed_p)) \ |
0a1b45a2 | 9098 | return false; \ |
a0c8462f | 9099 | if (signed_p) \ |
0f02bbd9 | 9100 | *result = op ((bfd_signed_vma) a); \ |
a0c8462f AM |
9101 | else \ |
9102 | *result = op a; \ | |
0a1b45a2 | 9103 | return true; \ |
d9352518 DB |
9104 | } |
9105 | ||
4b69ce9b | 9106 | #define BINARY_OP_HEAD(op) \ |
3f3328b8 | 9107 | if (startswith (sym, #op)) \ |
d9352518 DB |
9108 | { \ |
9109 | sym += strlen (#op); \ | |
a0c8462f AM |
9110 | if (*sym == ':') \ |
9111 | ++sym; \ | |
0f02bbd9 | 9112 | *symp = sym; \ |
8b127cbc | 9113 | if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 9114 | isymbuf, locsymcount, signed_p)) \ |
0a1b45a2 | 9115 | return false; \ |
0f02bbd9 | 9116 | ++*symp; \ |
8b127cbc | 9117 | if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ |
0f02bbd9 | 9118 | isymbuf, locsymcount, signed_p)) \ |
0a1b45a2 | 9119 | return false; |
4b69ce9b | 9120 | #define BINARY_OP_TAIL(op) \ |
a0c8462f | 9121 | if (signed_p) \ |
0f02bbd9 | 9122 | *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ |
a0c8462f AM |
9123 | else \ |
9124 | *result = a op b; \ | |
0a1b45a2 | 9125 | return true; \ |
d9352518 | 9126 | } |
4b69ce9b | 9127 | #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op) |
d9352518 DB |
9128 | |
9129 | default: | |
9130 | UNARY_OP (0-); | |
4b69ce9b AM |
9131 | BINARY_OP_HEAD (<<); |
9132 | if (b >= sizeof (a) * CHAR_BIT) | |
9133 | { | |
9134 | *result = 0; | |
0a1b45a2 | 9135 | return true; |
4b69ce9b AM |
9136 | } |
9137 | signed_p = 0; | |
9138 | BINARY_OP_TAIL (<<); | |
9139 | BINARY_OP_HEAD (>>); | |
9140 | if (b >= sizeof (a) * CHAR_BIT) | |
9141 | { | |
9142 | *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0; | |
0a1b45a2 | 9143 | return true; |
4b69ce9b AM |
9144 | } |
9145 | BINARY_OP_TAIL (>>); | |
d9352518 DB |
9146 | BINARY_OP (==); |
9147 | BINARY_OP (!=); | |
9148 | BINARY_OP (<=); | |
9149 | BINARY_OP (>=); | |
9150 | BINARY_OP (&&); | |
9151 | BINARY_OP (||); | |
9152 | UNARY_OP (~); | |
9153 | UNARY_OP (!); | |
9154 | BINARY_OP (*); | |
4b69ce9b AM |
9155 | BINARY_OP_HEAD (/); |
9156 | if (b == 0) | |
9157 | { | |
9158 | _bfd_error_handler (_("division by zero")); | |
9159 | bfd_set_error (bfd_error_bad_value); | |
0a1b45a2 | 9160 | return false; |
4b69ce9b AM |
9161 | } |
9162 | BINARY_OP_TAIL (/); | |
9163 | BINARY_OP_HEAD (%); | |
9164 | if (b == 0) | |
9165 | { | |
9166 | _bfd_error_handler (_("division by zero")); | |
9167 | bfd_set_error (bfd_error_bad_value); | |
0a1b45a2 | 9168 | return false; |
4b69ce9b AM |
9169 | } |
9170 | BINARY_OP_TAIL (%); | |
d9352518 DB |
9171 | BINARY_OP (^); |
9172 | BINARY_OP (|); | |
9173 | BINARY_OP (&); | |
9174 | BINARY_OP (+); | |
9175 | BINARY_OP (-); | |
9176 | BINARY_OP (<); | |
9177 | BINARY_OP (>); | |
9178 | #undef UNARY_OP | |
9179 | #undef BINARY_OP | |
9180 | _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); | |
9181 | bfd_set_error (bfd_error_invalid_operation); | |
0a1b45a2 | 9182 | return false; |
d9352518 DB |
9183 | } |
9184 | } | |
9185 | ||
d9352518 | 9186 | static void |
a0c8462f AM |
9187 | put_value (bfd_vma size, |
9188 | unsigned long chunksz, | |
9189 | bfd *input_bfd, | |
9190 | bfd_vma x, | |
9191 | bfd_byte *location) | |
d9352518 DB |
9192 | { |
9193 | location += (size - chunksz); | |
9194 | ||
41cd1ad1 | 9195 | for (; size; size -= chunksz, location -= chunksz) |
d9352518 DB |
9196 | { |
9197 | switch (chunksz) | |
9198 | { | |
d9352518 DB |
9199 | case 1: |
9200 | bfd_put_8 (input_bfd, x, location); | |
41cd1ad1 | 9201 | x >>= 8; |
d9352518 DB |
9202 | break; |
9203 | case 2: | |
9204 | bfd_put_16 (input_bfd, x, location); | |
41cd1ad1 | 9205 | x >>= 16; |
d9352518 DB |
9206 | break; |
9207 | case 4: | |
9208 | bfd_put_32 (input_bfd, x, location); | |
65164438 NC |
9209 | /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ |
9210 | x >>= 16; | |
9211 | x >>= 16; | |
d9352518 | 9212 | break; |
d9352518 | 9213 | #ifdef BFD64 |
41cd1ad1 | 9214 | case 8: |
d9352518 | 9215 | bfd_put_64 (input_bfd, x, location); |
41cd1ad1 NC |
9216 | /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ |
9217 | x >>= 32; | |
9218 | x >>= 32; | |
9219 | break; | |
d9352518 | 9220 | #endif |
41cd1ad1 NC |
9221 | default: |
9222 | abort (); | |
d9352518 DB |
9223 | break; |
9224 | } | |
9225 | } | |
9226 | } | |
9227 | ||
a0c8462f AM |
9228 | static bfd_vma |
9229 | get_value (bfd_vma size, | |
9230 | unsigned long chunksz, | |
9231 | bfd *input_bfd, | |
9232 | bfd_byte *location) | |
d9352518 | 9233 | { |
9b239e0e | 9234 | int shift; |
d9352518 DB |
9235 | bfd_vma x = 0; |
9236 | ||
9b239e0e NC |
9237 | /* Sanity checks. */ |
9238 | BFD_ASSERT (chunksz <= sizeof (x) | |
9239 | && size >= chunksz | |
9240 | && chunksz != 0 | |
9241 | && (size % chunksz) == 0 | |
9242 | && input_bfd != NULL | |
9243 | && location != NULL); | |
9244 | ||
9245 | if (chunksz == sizeof (x)) | |
9246 | { | |
9247 | BFD_ASSERT (size == chunksz); | |
9248 | ||
9249 | /* Make sure that we do not perform an undefined shift operation. | |
9250 | We know that size == chunksz so there will only be one iteration | |
9251 | of the loop below. */ | |
9252 | shift = 0; | |
9253 | } | |
9254 | else | |
9255 | shift = 8 * chunksz; | |
9256 | ||
a0c8462f | 9257 | for (; size; size -= chunksz, location += chunksz) |
d9352518 DB |
9258 | { |
9259 | switch (chunksz) | |
9260 | { | |
d9352518 | 9261 | case 1: |
9b239e0e | 9262 | x = (x << shift) | bfd_get_8 (input_bfd, location); |
d9352518 DB |
9263 | break; |
9264 | case 2: | |
9b239e0e | 9265 | x = (x << shift) | bfd_get_16 (input_bfd, location); |
d9352518 DB |
9266 | break; |
9267 | case 4: | |
9b239e0e | 9268 | x = (x << shift) | bfd_get_32 (input_bfd, location); |
d9352518 | 9269 | break; |
d9352518 | 9270 | #ifdef BFD64 |
9b239e0e NC |
9271 | case 8: |
9272 | x = (x << shift) | bfd_get_64 (input_bfd, location); | |
d9352518 | 9273 | break; |
9b239e0e NC |
9274 | #endif |
9275 | default: | |
9276 | abort (); | |
d9352518 DB |
9277 | } |
9278 | } | |
9279 | return x; | |
9280 | } | |
9281 | ||
a0c8462f AM |
9282 | static void |
9283 | decode_complex_addend (unsigned long *start, /* in bits */ | |
9284 | unsigned long *oplen, /* in bits */ | |
9285 | unsigned long *len, /* in bits */ | |
9286 | unsigned long *wordsz, /* in bytes */ | |
9287 | unsigned long *chunksz, /* in bytes */ | |
9288 | unsigned long *lsb0_p, | |
9289 | unsigned long *signed_p, | |
9290 | unsigned long *trunc_p, | |
9291 | unsigned long encoded) | |
d9352518 | 9292 | { |
07d6d2b8 AM |
9293 | * start = encoded & 0x3F; |
9294 | * len = (encoded >> 6) & 0x3F; | |
d9352518 DB |
9295 | * oplen = (encoded >> 12) & 0x3F; |
9296 | * wordsz = (encoded >> 18) & 0xF; | |
9297 | * chunksz = (encoded >> 22) & 0xF; | |
9298 | * lsb0_p = (encoded >> 27) & 1; | |
9299 | * signed_p = (encoded >> 28) & 1; | |
9300 | * trunc_p = (encoded >> 29) & 1; | |
9301 | } | |
9302 | ||
cdfeee4f | 9303 | bfd_reloc_status_type |
0f02bbd9 | 9304 | bfd_elf_perform_complex_relocation (bfd *input_bfd, |
bb294208 | 9305 | asection *input_section, |
0f02bbd9 AM |
9306 | bfd_byte *contents, |
9307 | Elf_Internal_Rela *rel, | |
9308 | bfd_vma relocation) | |
d9352518 | 9309 | { |
0f02bbd9 AM |
9310 | bfd_vma shift, x, mask; |
9311 | unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; | |
cdfeee4f | 9312 | bfd_reloc_status_type r; |
bb294208 | 9313 | bfd_size_type octets; |
d9352518 DB |
9314 | |
9315 | /* Perform this reloc, since it is complex. | |
9316 | (this is not to say that it necessarily refers to a complex | |
9317 | symbol; merely that it is a self-describing CGEN based reloc. | |
9318 | i.e. the addend has the complete reloc information (bit start, end, | |
a0c8462f | 9319 | word size, etc) encoded within it.). */ |
d9352518 | 9320 | |
a0c8462f AM |
9321 | decode_complex_addend (&start, &oplen, &len, &wordsz, |
9322 | &chunksz, &lsb0_p, &signed_p, | |
9323 | &trunc_p, rel->r_addend); | |
d9352518 DB |
9324 | |
9325 | mask = (((1L << (len - 1)) - 1) << 1) | 1; | |
9326 | ||
9327 | if (lsb0_p) | |
9328 | shift = (start + 1) - len; | |
9329 | else | |
9330 | shift = (8 * wordsz) - (start + len); | |
9331 | ||
bb294208 AM |
9332 | octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section); |
9333 | x = get_value (wordsz, chunksz, input_bfd, contents + octets); | |
d9352518 DB |
9334 | |
9335 | #ifdef DEBUG | |
9336 | printf ("Doing complex reloc: " | |
9337 | "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " | |
9338 | "chunksz %ld, start %ld, len %ld, oplen %ld\n" | |
9339 | " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", | |
9340 | lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, | |
9ccb8af9 AM |
9341 | oplen, (unsigned long) x, (unsigned long) mask, |
9342 | (unsigned long) relocation); | |
d9352518 DB |
9343 | #endif |
9344 | ||
cdfeee4f | 9345 | r = bfd_reloc_ok; |
d9352518 | 9346 | if (! trunc_p) |
cdfeee4f AM |
9347 | /* Now do an overflow check. */ |
9348 | r = bfd_check_overflow ((signed_p | |
9349 | ? complain_overflow_signed | |
9350 | : complain_overflow_unsigned), | |
9351 | len, 0, (8 * wordsz), | |
9352 | relocation); | |
a0c8462f | 9353 | |
d9352518 DB |
9354 | /* Do the deed. */ |
9355 | x = (x & ~(mask << shift)) | ((relocation & mask) << shift); | |
9356 | ||
9357 | #ifdef DEBUG | |
9358 | printf (" relocation: %8.8lx\n" | |
9359 | " shifted mask: %8.8lx\n" | |
9360 | " shifted/masked reloc: %8.8lx\n" | |
9361 | " result: %8.8lx\n", | |
9ccb8af9 AM |
9362 | (unsigned long) relocation, (unsigned long) (mask << shift), |
9363 | (unsigned long) ((relocation & mask) << shift), (unsigned long) x); | |
d9352518 | 9364 | #endif |
bb294208 | 9365 | put_value (wordsz, chunksz, input_bfd, x, contents + octets); |
cdfeee4f | 9366 | return r; |
d9352518 DB |
9367 | } |
9368 | ||
0e287786 AM |
9369 | /* Functions to read r_offset from external (target order) reloc |
9370 | entry. Faster than bfd_getl32 et al, because we let the compiler | |
9371 | know the value is aligned. */ | |
53df40a4 | 9372 | |
0e287786 AM |
9373 | static bfd_vma |
9374 | ext32l_r_offset (const void *p) | |
53df40a4 AM |
9375 | { |
9376 | union aligned32 | |
9377 | { | |
9378 | uint32_t v; | |
9379 | unsigned char c[4]; | |
9380 | }; | |
9381 | const union aligned32 *a | |
0e287786 | 9382 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
9383 | |
9384 | uint32_t aval = ( (uint32_t) a->c[0] | |
9385 | | (uint32_t) a->c[1] << 8 | |
9386 | | (uint32_t) a->c[2] << 16 | |
9387 | | (uint32_t) a->c[3] << 24); | |
0e287786 | 9388 | return aval; |
53df40a4 AM |
9389 | } |
9390 | ||
0e287786 AM |
9391 | static bfd_vma |
9392 | ext32b_r_offset (const void *p) | |
53df40a4 AM |
9393 | { |
9394 | union aligned32 | |
9395 | { | |
9396 | uint32_t v; | |
9397 | unsigned char c[4]; | |
9398 | }; | |
9399 | const union aligned32 *a | |
0e287786 | 9400 | = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; |
53df40a4 AM |
9401 | |
9402 | uint32_t aval = ( (uint32_t) a->c[0] << 24 | |
9403 | | (uint32_t) a->c[1] << 16 | |
9404 | | (uint32_t) a->c[2] << 8 | |
9405 | | (uint32_t) a->c[3]); | |
0e287786 | 9406 | return aval; |
53df40a4 AM |
9407 | } |
9408 | ||
0e287786 AM |
9409 | static bfd_vma |
9410 | ext64l_r_offset (const void *p) | |
53df40a4 AM |
9411 | { |
9412 | union aligned64 | |
9413 | { | |
9414 | uint64_t v; | |
9415 | unsigned char c[8]; | |
9416 | }; | |
9417 | const union aligned64 *a | |
0e287786 | 9418 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
9419 | |
9420 | uint64_t aval = ( (uint64_t) a->c[0] | |
9421 | | (uint64_t) a->c[1] << 8 | |
9422 | | (uint64_t) a->c[2] << 16 | |
9423 | | (uint64_t) a->c[3] << 24 | |
9424 | | (uint64_t) a->c[4] << 32 | |
9425 | | (uint64_t) a->c[5] << 40 | |
9426 | | (uint64_t) a->c[6] << 48 | |
9427 | | (uint64_t) a->c[7] << 56); | |
0e287786 | 9428 | return aval; |
53df40a4 AM |
9429 | } |
9430 | ||
0e287786 AM |
9431 | static bfd_vma |
9432 | ext64b_r_offset (const void *p) | |
53df40a4 AM |
9433 | { |
9434 | union aligned64 | |
9435 | { | |
9436 | uint64_t v; | |
9437 | unsigned char c[8]; | |
9438 | }; | |
9439 | const union aligned64 *a | |
0e287786 | 9440 | = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; |
53df40a4 AM |
9441 | |
9442 | uint64_t aval = ( (uint64_t) a->c[0] << 56 | |
9443 | | (uint64_t) a->c[1] << 48 | |
9444 | | (uint64_t) a->c[2] << 40 | |
9445 | | (uint64_t) a->c[3] << 32 | |
9446 | | (uint64_t) a->c[4] << 24 | |
9447 | | (uint64_t) a->c[5] << 16 | |
9448 | | (uint64_t) a->c[6] << 8 | |
9449 | | (uint64_t) a->c[7]); | |
0e287786 | 9450 | return aval; |
53df40a4 | 9451 | } |
53df40a4 | 9452 | |
c152c796 AM |
9453 | /* When performing a relocatable link, the input relocations are |
9454 | preserved. But, if they reference global symbols, the indices | |
d4730f92 BS |
9455 | referenced must be updated. Update all the relocations found in |
9456 | RELDATA. */ | |
c152c796 | 9457 | |
0a1b45a2 | 9458 | static bool |
c152c796 | 9459 | elf_link_adjust_relocs (bfd *abfd, |
9eaff861 | 9460 | asection *sec, |
28dbcedc | 9461 | struct bfd_elf_section_reloc_data *reldata, |
0a1b45a2 | 9462 | bool sort, |
10bbbc1d | 9463 | struct bfd_link_info *info) |
c152c796 AM |
9464 | { |
9465 | unsigned int i; | |
9466 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9467 | bfd_byte *erela; | |
9468 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
9469 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
9470 | bfd_vma r_type_mask; | |
9471 | int r_sym_shift; | |
d4730f92 BS |
9472 | unsigned int count = reldata->count; |
9473 | struct elf_link_hash_entry **rel_hash = reldata->hashes; | |
c152c796 | 9474 | |
d4730f92 | 9475 | if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) |
c152c796 AM |
9476 | { |
9477 | swap_in = bed->s->swap_reloc_in; | |
9478 | swap_out = bed->s->swap_reloc_out; | |
9479 | } | |
d4730f92 | 9480 | else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) |
c152c796 AM |
9481 | { |
9482 | swap_in = bed->s->swap_reloca_in; | |
9483 | swap_out = bed->s->swap_reloca_out; | |
9484 | } | |
9485 | else | |
9486 | abort (); | |
9487 | ||
9488 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
9489 | abort (); | |
9490 | ||
9491 | if (bed->s->arch_size == 32) | |
9492 | { | |
9493 | r_type_mask = 0xff; | |
9494 | r_sym_shift = 8; | |
9495 | } | |
9496 | else | |
9497 | { | |
9498 | r_type_mask = 0xffffffff; | |
9499 | r_sym_shift = 32; | |
9500 | } | |
9501 | ||
d4730f92 BS |
9502 | erela = reldata->hdr->contents; |
9503 | for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) | |
c152c796 AM |
9504 | { |
9505 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
9506 | unsigned int j; | |
9507 | ||
9508 | if (*rel_hash == NULL) | |
9509 | continue; | |
9510 | ||
10bbbc1d NC |
9511 | if ((*rel_hash)->indx == -2 |
9512 | && info->gc_sections | |
9513 | && ! info->gc_keep_exported) | |
9514 | { | |
9515 | /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ | |
9793eb77 | 9516 | _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"), |
10bbbc1d NC |
9517 | abfd, sec, |
9518 | (*rel_hash)->root.root.string); | |
9793eb77 | 9519 | _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"), |
d42c267e | 9520 | abfd, sec); |
10bbbc1d | 9521 | bfd_set_error (bfd_error_invalid_operation); |
0a1b45a2 | 9522 | return false; |
10bbbc1d | 9523 | } |
c152c796 AM |
9524 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
9525 | ||
9526 | (*swap_in) (abfd, erela, irela); | |
9527 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
9528 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
9529 | | (irela[j].r_info & r_type_mask)); | |
9530 | (*swap_out) (abfd, irela, erela); | |
9531 | } | |
53df40a4 | 9532 | |
9eaff861 AO |
9533 | if (bed->elf_backend_update_relocs) |
9534 | (*bed->elf_backend_update_relocs) (sec, reldata); | |
9535 | ||
0e287786 | 9536 | if (sort && count != 0) |
53df40a4 | 9537 | { |
0e287786 AM |
9538 | bfd_vma (*ext_r_off) (const void *); |
9539 | bfd_vma r_off; | |
9540 | size_t elt_size; | |
9541 | bfd_byte *base, *end, *p, *loc; | |
bca6d0e3 | 9542 | bfd_byte *buf = NULL; |
28dbcedc AM |
9543 | |
9544 | if (bed->s->arch_size == 32) | |
9545 | { | |
9546 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) | |
0e287786 | 9547 | ext_r_off = ext32l_r_offset; |
28dbcedc | 9548 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 9549 | ext_r_off = ext32b_r_offset; |
28dbcedc AM |
9550 | else |
9551 | abort (); | |
9552 | } | |
53df40a4 | 9553 | else |
28dbcedc | 9554 | { |
28dbcedc | 9555 | if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) |
0e287786 | 9556 | ext_r_off = ext64l_r_offset; |
28dbcedc | 9557 | else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) |
0e287786 | 9558 | ext_r_off = ext64b_r_offset; |
28dbcedc | 9559 | else |
28dbcedc AM |
9560 | abort (); |
9561 | } | |
0e287786 | 9562 | |
bca6d0e3 AM |
9563 | /* Must use a stable sort here. A modified insertion sort, |
9564 | since the relocs are mostly sorted already. */ | |
0e287786 AM |
9565 | elt_size = reldata->hdr->sh_entsize; |
9566 | base = reldata->hdr->contents; | |
9567 | end = base + count * elt_size; | |
bca6d0e3 | 9568 | if (elt_size > sizeof (Elf64_External_Rela)) |
0e287786 AM |
9569 | abort (); |
9570 | ||
9571 | /* Ensure the first element is lowest. This acts as a sentinel, | |
9572 | speeding the main loop below. */ | |
9573 | r_off = (*ext_r_off) (base); | |
9574 | for (p = loc = base; (p += elt_size) < end; ) | |
9575 | { | |
9576 | bfd_vma r_off2 = (*ext_r_off) (p); | |
9577 | if (r_off > r_off2) | |
9578 | { | |
9579 | r_off = r_off2; | |
9580 | loc = p; | |
9581 | } | |
9582 | } | |
9583 | if (loc != base) | |
9584 | { | |
9585 | /* Don't just swap *base and *loc as that changes the order | |
9586 | of the original base[0] and base[1] if they happen to | |
9587 | have the same r_offset. */ | |
bca6d0e3 AM |
9588 | bfd_byte onebuf[sizeof (Elf64_External_Rela)]; |
9589 | memcpy (onebuf, loc, elt_size); | |
0e287786 | 9590 | memmove (base + elt_size, base, loc - base); |
bca6d0e3 | 9591 | memcpy (base, onebuf, elt_size); |
0e287786 AM |
9592 | } |
9593 | ||
b29b8669 | 9594 | for (p = base + elt_size; (p += elt_size) < end; ) |
0e287786 AM |
9595 | { |
9596 | /* base to p is sorted, *p is next to insert. */ | |
9597 | r_off = (*ext_r_off) (p); | |
9598 | /* Search the sorted region for location to insert. */ | |
9599 | loc = p - elt_size; | |
9600 | while (r_off < (*ext_r_off) (loc)) | |
9601 | loc -= elt_size; | |
9602 | loc += elt_size; | |
9603 | if (loc != p) | |
9604 | { | |
bca6d0e3 AM |
9605 | /* Chances are there is a run of relocs to insert here, |
9606 | from one of more input files. Files are not always | |
9607 | linked in order due to the way elf_link_input_bfd is | |
9608 | called. See pr17666. */ | |
9609 | size_t sortlen = p - loc; | |
9610 | bfd_vma r_off2 = (*ext_r_off) (loc); | |
9611 | size_t runlen = elt_size; | |
fba1ac87 TK |
9612 | bfd_vma r_off_runend = r_off; |
9613 | bfd_vma r_off_runend_next; | |
bca6d0e3 AM |
9614 | size_t buf_size = 96 * 1024; |
9615 | while (p + runlen < end | |
9616 | && (sortlen <= buf_size | |
9617 | || runlen + elt_size <= buf_size) | |
fba1ac87 TK |
9618 | /* run must not break the ordering of base..loc+1 */ |
9619 | && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen)) | |
9620 | /* run must be already sorted */ | |
9621 | && r_off_runend_next >= r_off_runend) | |
9622 | { | |
9623 | runlen += elt_size; | |
9624 | r_off_runend = r_off_runend_next; | |
9625 | } | |
bca6d0e3 AM |
9626 | if (buf == NULL) |
9627 | { | |
9628 | buf = bfd_malloc (buf_size); | |
9629 | if (buf == NULL) | |
0a1b45a2 | 9630 | return false; |
bca6d0e3 AM |
9631 | } |
9632 | if (runlen < sortlen) | |
9633 | { | |
9634 | memcpy (buf, p, runlen); | |
9635 | memmove (loc + runlen, loc, sortlen); | |
9636 | memcpy (loc, buf, runlen); | |
9637 | } | |
9638 | else | |
9639 | { | |
9640 | memcpy (buf, loc, sortlen); | |
9641 | memmove (loc, p, runlen); | |
9642 | memcpy (loc + runlen, buf, sortlen); | |
9643 | } | |
b29b8669 | 9644 | p += runlen - elt_size; |
0e287786 AM |
9645 | } |
9646 | } | |
9647 | /* Hashes are no longer valid. */ | |
28dbcedc AM |
9648 | free (reldata->hashes); |
9649 | reldata->hashes = NULL; | |
bca6d0e3 | 9650 | free (buf); |
53df40a4 | 9651 | } |
0a1b45a2 | 9652 | return true; |
c152c796 AM |
9653 | } |
9654 | ||
9655 | struct elf_link_sort_rela | |
9656 | { | |
9657 | union { | |
9658 | bfd_vma offset; | |
9659 | bfd_vma sym_mask; | |
9660 | } u; | |
9661 | enum elf_reloc_type_class type; | |
9662 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
9663 | Elf_Internal_Rela rela[1]; | |
9664 | }; | |
9665 | ||
dcea6a95 AM |
9666 | /* qsort stability here and for cmp2 is only an issue if multiple |
9667 | dynamic relocations are emitted at the same address. But targets | |
9668 | that apply a series of dynamic relocations each operating on the | |
9669 | result of the prior relocation can't use -z combreloc as | |
9670 | implemented anyway. Such schemes tend to be broken by sorting on | |
9671 | symbol index. That leaves dynamic NONE relocs as the only other | |
9672 | case where ld might emit multiple relocs at the same address, and | |
9673 | those are only emitted due to target bugs. */ | |
9674 | ||
c152c796 AM |
9675 | static int |
9676 | elf_link_sort_cmp1 (const void *A, const void *B) | |
9677 | { | |
a50b1753 NC |
9678 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
9679 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 AM |
9680 | int relativea, relativeb; |
9681 | ||
9682 | relativea = a->type == reloc_class_relative; | |
9683 | relativeb = b->type == reloc_class_relative; | |
9684 | ||
9685 | if (relativea < relativeb) | |
9686 | return 1; | |
9687 | if (relativea > relativeb) | |
9688 | return -1; | |
9689 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
9690 | return -1; | |
9691 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
9692 | return 1; | |
9693 | if (a->rela->r_offset < b->rela->r_offset) | |
9694 | return -1; | |
9695 | if (a->rela->r_offset > b->rela->r_offset) | |
9696 | return 1; | |
9697 | return 0; | |
9698 | } | |
9699 | ||
9700 | static int | |
9701 | elf_link_sort_cmp2 (const void *A, const void *B) | |
9702 | { | |
a50b1753 NC |
9703 | const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; |
9704 | const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; | |
c152c796 | 9705 | |
7e612e98 | 9706 | if (a->type < b->type) |
c152c796 | 9707 | return -1; |
7e612e98 | 9708 | if (a->type > b->type) |
c152c796 | 9709 | return 1; |
7e612e98 | 9710 | if (a->u.offset < b->u.offset) |
c152c796 | 9711 | return -1; |
7e612e98 | 9712 | if (a->u.offset > b->u.offset) |
c152c796 AM |
9713 | return 1; |
9714 | if (a->rela->r_offset < b->rela->r_offset) | |
9715 | return -1; | |
9716 | if (a->rela->r_offset > b->rela->r_offset) | |
9717 | return 1; | |
9718 | return 0; | |
9719 | } | |
9720 | ||
9721 | static size_t | |
9722 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
9723 | { | |
3410fea8 | 9724 | asection *dynamic_relocs; |
fc66a176 L |
9725 | asection *rela_dyn; |
9726 | asection *rel_dyn; | |
c152c796 AM |
9727 | bfd_size_type count, size; |
9728 | size_t i, ret, sort_elt, ext_size; | |
9729 | bfd_byte *sort, *s_non_relative, *p; | |
9730 | struct elf_link_sort_rela *sq; | |
9731 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9732 | int i2e = bed->s->int_rels_per_ext_rel; | |
61826503 | 9733 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
c152c796 AM |
9734 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
9735 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
9736 | struct bfd_link_order *lo; | |
9737 | bfd_vma r_sym_mask; | |
0a1b45a2 | 9738 | bool use_rela; |
c152c796 | 9739 | |
3410fea8 NC |
9740 | /* Find a dynamic reloc section. */ |
9741 | rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
9742 | rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
9743 | if (rela_dyn != NULL && rela_dyn->size > 0 | |
9744 | && rel_dyn != NULL && rel_dyn->size > 0) | |
c152c796 | 9745 | { |
0a1b45a2 | 9746 | bool use_rela_initialised = false; |
3410fea8 NC |
9747 | |
9748 | /* This is just here to stop gcc from complaining. | |
c8e44c6d | 9749 | Its initialization checking code is not perfect. */ |
0a1b45a2 | 9750 | use_rela = true; |
3410fea8 NC |
9751 | |
9752 | /* Both sections are present. Examine the sizes | |
9753 | of the indirect sections to help us choose. */ | |
9754 | for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9755 | if (lo->type == bfd_indirect_link_order) | |
9756 | { | |
9757 | asection *o = lo->u.indirect.section; | |
9758 | ||
9759 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9760 | { | |
9761 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9762 | /* Section size is divisible by both rel and rela sizes. | |
9763 | It is of no help to us. */ | |
9764 | ; | |
9765 | else | |
9766 | { | |
9767 | /* Section size is only divisible by rela. */ | |
535b785f | 9768 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9769 | { |
9793eb77 | 9770 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9771 | "they are in more than one size"), |
9772 | abfd); | |
3410fea8 NC |
9773 | bfd_set_error (bfd_error_invalid_operation); |
9774 | return 0; | |
9775 | } | |
9776 | else | |
9777 | { | |
0a1b45a2 AM |
9778 | use_rela = true; |
9779 | use_rela_initialised = true; | |
3410fea8 NC |
9780 | } |
9781 | } | |
9782 | } | |
9783 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9784 | { | |
9785 | /* Section size is only divisible by rel. */ | |
535b785f | 9786 | if (use_rela_initialised && use_rela) |
3410fea8 | 9787 | { |
9793eb77 | 9788 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9789 | "they are in more than one size"), |
9790 | abfd); | |
3410fea8 NC |
9791 | bfd_set_error (bfd_error_invalid_operation); |
9792 | return 0; | |
9793 | } | |
9794 | else | |
9795 | { | |
0a1b45a2 AM |
9796 | use_rela = false; |
9797 | use_rela_initialised = true; | |
3410fea8 NC |
9798 | } |
9799 | } | |
9800 | else | |
9801 | { | |
c8e44c6d AM |
9802 | /* The section size is not divisible by either - |
9803 | something is wrong. */ | |
9793eb77 | 9804 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9805 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9806 | bfd_set_error (bfd_error_invalid_operation); |
9807 | return 0; | |
9808 | } | |
9809 | } | |
9810 | ||
9811 | for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) | |
9812 | if (lo->type == bfd_indirect_link_order) | |
9813 | { | |
9814 | asection *o = lo->u.indirect.section; | |
9815 | ||
9816 | if ((o->size % bed->s->sizeof_rela) == 0) | |
9817 | { | |
9818 | if ((o->size % bed->s->sizeof_rel) == 0) | |
9819 | /* Section size is divisible by both rel and rela sizes. | |
9820 | It is of no help to us. */ | |
9821 | ; | |
9822 | else | |
9823 | { | |
9824 | /* Section size is only divisible by rela. */ | |
535b785f | 9825 | if (use_rela_initialised && !use_rela) |
3410fea8 | 9826 | { |
9793eb77 | 9827 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9828 | "they are in more than one size"), |
9829 | abfd); | |
3410fea8 NC |
9830 | bfd_set_error (bfd_error_invalid_operation); |
9831 | return 0; | |
9832 | } | |
9833 | else | |
9834 | { | |
0a1b45a2 AM |
9835 | use_rela = true; |
9836 | use_rela_initialised = true; | |
3410fea8 NC |
9837 | } |
9838 | } | |
9839 | } | |
9840 | else if ((o->size % bed->s->sizeof_rel) == 0) | |
9841 | { | |
9842 | /* Section size is only divisible by rel. */ | |
535b785f | 9843 | if (use_rela_initialised && use_rela) |
3410fea8 | 9844 | { |
9793eb77 | 9845 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d AM |
9846 | "they are in more than one size"), |
9847 | abfd); | |
3410fea8 NC |
9848 | bfd_set_error (bfd_error_invalid_operation); |
9849 | return 0; | |
9850 | } | |
9851 | else | |
9852 | { | |
0a1b45a2 AM |
9853 | use_rela = false; |
9854 | use_rela_initialised = true; | |
3410fea8 NC |
9855 | } |
9856 | } | |
9857 | else | |
9858 | { | |
c8e44c6d AM |
9859 | /* The section size is not divisible by either - |
9860 | something is wrong. */ | |
9793eb77 | 9861 | _bfd_error_handler (_("%pB: unable to sort relocs - " |
c8e44c6d | 9862 | "they are of an unknown size"), abfd); |
3410fea8 NC |
9863 | bfd_set_error (bfd_error_invalid_operation); |
9864 | return 0; | |
9865 | } | |
9866 | } | |
9867 | ||
9868 | if (! use_rela_initialised) | |
9869 | /* Make a guess. */ | |
0a1b45a2 | 9870 | use_rela = true; |
c152c796 | 9871 | } |
fc66a176 | 9872 | else if (rela_dyn != NULL && rela_dyn->size > 0) |
0a1b45a2 | 9873 | use_rela = true; |
fc66a176 | 9874 | else if (rel_dyn != NULL && rel_dyn->size > 0) |
0a1b45a2 | 9875 | use_rela = false; |
c152c796 | 9876 | else |
fc66a176 | 9877 | return 0; |
3410fea8 NC |
9878 | |
9879 | if (use_rela) | |
c152c796 | 9880 | { |
3410fea8 | 9881 | dynamic_relocs = rela_dyn; |
c152c796 AM |
9882 | ext_size = bed->s->sizeof_rela; |
9883 | swap_in = bed->s->swap_reloca_in; | |
9884 | swap_out = bed->s->swap_reloca_out; | |
9885 | } | |
3410fea8 NC |
9886 | else |
9887 | { | |
9888 | dynamic_relocs = rel_dyn; | |
9889 | ext_size = bed->s->sizeof_rel; | |
9890 | swap_in = bed->s->swap_reloc_in; | |
9891 | swap_out = bed->s->swap_reloc_out; | |
9892 | } | |
c152c796 AM |
9893 | |
9894 | size = 0; | |
3410fea8 | 9895 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 | 9896 | if (lo->type == bfd_indirect_link_order) |
3410fea8 | 9897 | size += lo->u.indirect.section->size; |
c152c796 | 9898 | |
3410fea8 | 9899 | if (size != dynamic_relocs->size) |
c152c796 AM |
9900 | return 0; |
9901 | ||
9902 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
9903 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
3410fea8 NC |
9904 | |
9905 | count = dynamic_relocs->size / ext_size; | |
5e486aa1 NC |
9906 | if (count == 0) |
9907 | return 0; | |
a50b1753 | 9908 | sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); |
3410fea8 | 9909 | |
c152c796 AM |
9910 | if (sort == NULL) |
9911 | { | |
9912 | (*info->callbacks->warning) | |
9793eb77 | 9913 | (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0); |
c152c796 AM |
9914 | return 0; |
9915 | } | |
9916 | ||
9917 | if (bed->s->arch_size == 32) | |
9918 | r_sym_mask = ~(bfd_vma) 0xff; | |
9919 | else | |
9920 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
9921 | ||
3410fea8 | 9922 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
9923 | if (lo->type == bfd_indirect_link_order) |
9924 | { | |
9925 | bfd_byte *erel, *erelend; | |
9926 | asection *o = lo->u.indirect.section; | |
9927 | ||
1da212d6 AM |
9928 | if (o->contents == NULL && o->size != 0) |
9929 | { | |
9930 | /* This is a reloc section that is being handled as a normal | |
9931 | section. See bfd_section_from_shdr. We can't combine | |
9932 | relocs in this case. */ | |
9933 | free (sort); | |
9934 | return 0; | |
9935 | } | |
c152c796 | 9936 | erel = o->contents; |
eea6121a | 9937 | erelend = o->contents + o->size; |
c8e44c6d | 9938 | p = sort + o->output_offset * opb / ext_size * sort_elt; |
3410fea8 | 9939 | |
c152c796 AM |
9940 | while (erel < erelend) |
9941 | { | |
9942 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
3410fea8 | 9943 | |
c152c796 | 9944 | (*swap_in) (abfd, erel, s->rela); |
7e612e98 | 9945 | s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); |
c152c796 AM |
9946 | s->u.sym_mask = r_sym_mask; |
9947 | p += sort_elt; | |
9948 | erel += ext_size; | |
9949 | } | |
9950 | } | |
9951 | ||
9952 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
9953 | ||
9954 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
9955 | { | |
9956 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
9957 | if (s->type != reloc_class_relative) | |
9958 | break; | |
9959 | } | |
9960 | ret = i; | |
9961 | s_non_relative = p; | |
9962 | ||
9963 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
9964 | for (; i < count; i++, p += sort_elt) | |
9965 | { | |
9966 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
9967 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
9968 | sq = sp; | |
9969 | sp->u.offset = sq->rela->r_offset; | |
9970 | } | |
9971 | ||
9972 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
9973 | ||
c8e44c6d AM |
9974 | struct elf_link_hash_table *htab = elf_hash_table (info); |
9975 | if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) | |
9976 | { | |
9977 | /* We have plt relocs in .rela.dyn. */ | |
9978 | sq = (struct elf_link_sort_rela *) sort; | |
9979 | for (i = 0; i < count; i++) | |
9980 | if (sq[count - i - 1].type != reloc_class_plt) | |
9981 | break; | |
9982 | if (i != 0 && htab->srelplt->size == i * ext_size) | |
9983 | { | |
9984 | struct bfd_link_order **plo; | |
9985 | /* Put srelplt link_order last. This is so the output_offset | |
9986 | set in the next loop is correct for DT_JMPREL. */ | |
9987 | for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) | |
9988 | if ((*plo)->type == bfd_indirect_link_order | |
9989 | && (*plo)->u.indirect.section == htab->srelplt) | |
9990 | { | |
9991 | lo = *plo; | |
9992 | *plo = lo->next; | |
9993 | } | |
9994 | else | |
9995 | plo = &(*plo)->next; | |
9996 | *plo = lo; | |
9997 | lo->next = NULL; | |
9998 | dynamic_relocs->map_tail.link_order = lo; | |
9999 | } | |
10000 | } | |
10001 | ||
10002 | p = sort; | |
3410fea8 | 10003 | for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) |
c152c796 AM |
10004 | if (lo->type == bfd_indirect_link_order) |
10005 | { | |
10006 | bfd_byte *erel, *erelend; | |
10007 | asection *o = lo->u.indirect.section; | |
10008 | ||
10009 | erel = o->contents; | |
eea6121a | 10010 | erelend = o->contents + o->size; |
c8e44c6d | 10011 | o->output_offset = (p - sort) / sort_elt * ext_size / opb; |
c152c796 AM |
10012 | while (erel < erelend) |
10013 | { | |
10014 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
10015 | (*swap_out) (abfd, s->rela, erel); | |
10016 | p += sort_elt; | |
10017 | erel += ext_size; | |
10018 | } | |
10019 | } | |
10020 | ||
10021 | free (sort); | |
3410fea8 | 10022 | *psec = dynamic_relocs; |
c152c796 AM |
10023 | return ret; |
10024 | } | |
10025 | ||
ef10c3ac | 10026 | /* Add a symbol to the output symbol string table. */ |
c152c796 | 10027 | |
6e0b88f1 | 10028 | static int |
37bb890f | 10029 | elf_link_output_symstrtab (void *finf, |
ef10c3ac L |
10030 | const char *name, |
10031 | Elf_Internal_Sym *elfsym, | |
10032 | asection *input_sec, | |
10033 | struct elf_link_hash_entry *h) | |
c152c796 | 10034 | { |
37bb890f | 10035 | struct elf_final_link_info *flinfo = finf; |
6e0b88f1 | 10036 | int (*output_symbol_hook) |
c152c796 AM |
10037 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, |
10038 | struct elf_link_hash_entry *); | |
ef10c3ac | 10039 | struct elf_link_hash_table *hash_table; |
c152c796 | 10040 | const struct elf_backend_data *bed; |
ef10c3ac | 10041 | bfd_size_type strtabsize; |
c152c796 | 10042 | |
8539e4e8 AM |
10043 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); |
10044 | ||
8b127cbc | 10045 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 AM |
10046 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; |
10047 | if (output_symbol_hook != NULL) | |
10048 | { | |
8b127cbc | 10049 | int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); |
6e0b88f1 AM |
10050 | if (ret != 1) |
10051 | return ret; | |
c152c796 AM |
10052 | } |
10053 | ||
06f44071 AM |
10054 | if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC) |
10055 | elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc; | |
10056 | if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE) | |
10057 | elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique; | |
10058 | ||
e9a20a41 | 10059 | if (name == NULL || *name == '\0') |
ef10c3ac | 10060 | elfsym->st_name = (unsigned long) -1; |
c152c796 AM |
10061 | else |
10062 | { | |
ef10c3ac L |
10063 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
10064 | to get the final offset for st_name. */ | |
3f2e9699 | 10065 | char *versioned_name = (char *) name; |
496afd17 L |
10066 | if (h != NULL) |
10067 | { | |
10068 | if (h->versioned == versioned && h->def_dynamic) | |
10069 | { | |
10070 | /* Keep only one '@' for versioned symbols defined in | |
10071 | shared objects. */ | |
10072 | char *version = strrchr (name, ELF_VER_CHR); | |
10073 | char *base_end = strchr (name, ELF_VER_CHR); | |
10074 | if (version != base_end) | |
10075 | { | |
10076 | size_t base_len; | |
10077 | size_t len = strlen (name); | |
10078 | versioned_name = bfd_alloc (flinfo->output_bfd, len); | |
10079 | if (versioned_name == NULL) | |
10080 | return 0; | |
10081 | base_len = base_end - name; | |
10082 | memcpy (versioned_name, name, base_len); | |
10083 | memcpy (versioned_name + base_len, version, | |
10084 | len - base_len); | |
10085 | } | |
10086 | } | |
10087 | } | |
10088 | else if (flinfo->info->unique_symbol | |
10089 | && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL) | |
3f2e9699 | 10090 | { |
496afd17 | 10091 | struct local_hash_entry *lh; |
4467df35 L |
10092 | size_t count_len; |
10093 | size_t base_len; | |
10094 | char buf[30]; | |
496afd17 | 10095 | switch (ELF_ST_TYPE (elfsym->st_info)) |
3f2e9699 | 10096 | { |
496afd17 L |
10097 | case STT_FILE: |
10098 | case STT_SECTION: | |
10099 | break; | |
10100 | default: | |
10101 | lh = (struct local_hash_entry *) bfd_hash_lookup | |
0a1b45a2 | 10102 | (&flinfo->local_hash_table, name, true, false); |
496afd17 | 10103 | if (lh == NULL) |
3f2e9699 | 10104 | return 0; |
4467df35 L |
10105 | /* Always append ".COUNT" to local symbols to avoid |
10106 | potential conflicts with local symbol "XXX.COUNT". */ | |
10107 | sprintf (buf, "%lx", lh->count); | |
10108 | base_len = lh->size; | |
10109 | if (!base_len) | |
496afd17 | 10110 | { |
4467df35 L |
10111 | base_len = strlen (name); |
10112 | lh->size = base_len; | |
496afd17 | 10113 | } |
4467df35 L |
10114 | count_len = strlen (buf); |
10115 | versioned_name = bfd_alloc (flinfo->output_bfd, | |
10116 | base_len + count_len + 2); | |
10117 | if (versioned_name == NULL) | |
10118 | return 0; | |
10119 | memcpy (versioned_name, name, base_len); | |
10120 | versioned_name[base_len] = '.'; | |
10121 | memcpy (versioned_name + base_len + 1, buf, | |
10122 | count_len + 1); | |
496afd17 L |
10123 | lh->count++; |
10124 | break; | |
3f2e9699 L |
10125 | } |
10126 | } | |
ef10c3ac L |
10127 | elfsym->st_name |
10128 | = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, | |
0a1b45a2 | 10129 | versioned_name, false); |
c152c796 | 10130 | if (elfsym->st_name == (unsigned long) -1) |
6e0b88f1 | 10131 | return 0; |
c152c796 AM |
10132 | } |
10133 | ||
ef10c3ac L |
10134 | hash_table = elf_hash_table (flinfo->info); |
10135 | strtabsize = hash_table->strtabsize; | |
b03b65e2 | 10136 | if (strtabsize <= flinfo->output_bfd->symcount) |
c152c796 | 10137 | { |
ef10c3ac L |
10138 | strtabsize += strtabsize; |
10139 | hash_table->strtabsize = strtabsize; | |
10140 | strtabsize *= sizeof (*hash_table->strtab); | |
10141 | hash_table->strtab | |
10142 | = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, | |
10143 | strtabsize); | |
10144 | if (hash_table->strtab == NULL) | |
6e0b88f1 | 10145 | return 0; |
c152c796 | 10146 | } |
b03b65e2 AM |
10147 | hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym; |
10148 | hash_table->strtab[flinfo->output_bfd->symcount].dest_index | |
10149 | = flinfo->output_bfd->symcount; | |
ed48ec2e | 10150 | flinfo->output_bfd->symcount += 1; |
ef10c3ac L |
10151 | |
10152 | return 1; | |
10153 | } | |
10154 | ||
10155 | /* Swap symbols out to the symbol table and flush the output symbols to | |
10156 | the file. */ | |
10157 | ||
0a1b45a2 | 10158 | static bool |
ef10c3ac L |
10159 | elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) |
10160 | { | |
10161 | struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); | |
986f0783 | 10162 | size_t amt; |
ef53be89 | 10163 | size_t i; |
ef10c3ac L |
10164 | const struct elf_backend_data *bed; |
10165 | bfd_byte *symbuf; | |
10166 | Elf_Internal_Shdr *hdr; | |
10167 | file_ptr pos; | |
0a1b45a2 | 10168 | bool ret; |
ef10c3ac | 10169 | |
b03b65e2 | 10170 | if (flinfo->output_bfd->symcount == 0) |
0a1b45a2 | 10171 | return true; |
ef10c3ac L |
10172 | |
10173 | BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); | |
10174 | ||
10175 | bed = get_elf_backend_data (flinfo->output_bfd); | |
c152c796 | 10176 | |
b03b65e2 | 10177 | amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount; |
ef10c3ac L |
10178 | symbuf = (bfd_byte *) bfd_malloc (amt); |
10179 | if (symbuf == NULL) | |
0a1b45a2 | 10180 | return false; |
1b786873 | 10181 | |
ef10c3ac | 10182 | if (flinfo->symshndxbuf) |
c152c796 | 10183 | { |
ef53be89 AM |
10184 | amt = sizeof (Elf_External_Sym_Shndx); |
10185 | amt *= bfd_get_symcount (flinfo->output_bfd); | |
ef10c3ac L |
10186 | flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); |
10187 | if (flinfo->symshndxbuf == NULL) | |
c152c796 | 10188 | { |
ef10c3ac | 10189 | free (symbuf); |
0a1b45a2 | 10190 | return false; |
c152c796 | 10191 | } |
c152c796 AM |
10192 | } |
10193 | ||
3d16b64e | 10194 | /* Now swap out the symbols. */ |
b03b65e2 | 10195 | for (i = 0; i < flinfo->output_bfd->symcount; i++) |
ef10c3ac L |
10196 | { |
10197 | struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; | |
10198 | if (elfsym->sym.st_name == (unsigned long) -1) | |
10199 | elfsym->sym.st_name = 0; | |
10200 | else | |
10201 | elfsym->sym.st_name | |
10202 | = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, | |
10203 | elfsym->sym.st_name); | |
3d16b64e NA |
10204 | |
10205 | /* Inform the linker of the addition of this symbol. */ | |
10206 | ||
10207 | if (flinfo->info->callbacks->ctf_new_symbol) | |
10208 | flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index, | |
10209 | &elfsym->sym); | |
10210 | ||
ef10c3ac L |
10211 | bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, |
10212 | ((bfd_byte *) symbuf | |
10213 | + (elfsym->dest_index | |
10214 | * bed->s->sizeof_sym)), | |
b03b65e2 AM |
10215 | NPTR_ADD (flinfo->symshndxbuf, |
10216 | elfsym->dest_index)); | |
ef10c3ac L |
10217 | } |
10218 | ||
10219 | hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; | |
10220 | pos = hdr->sh_offset + hdr->sh_size; | |
b03b65e2 | 10221 | amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount; |
ef10c3ac | 10222 | if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 |
226f9f4f | 10223 | && bfd_write (symbuf, amt, flinfo->output_bfd) == amt) |
ef10c3ac L |
10224 | { |
10225 | hdr->sh_size += amt; | |
0a1b45a2 | 10226 | ret = true; |
ef10c3ac L |
10227 | } |
10228 | else | |
0a1b45a2 | 10229 | ret = false; |
c152c796 | 10230 | |
ef10c3ac L |
10231 | free (symbuf); |
10232 | ||
10233 | free (hash_table->strtab); | |
10234 | hash_table->strtab = NULL; | |
10235 | ||
10236 | return ret; | |
c152c796 AM |
10237 | } |
10238 | ||
c0d5a53d L |
10239 | /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ |
10240 | ||
0a1b45a2 | 10241 | static bool |
c0d5a53d L |
10242 | check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) |
10243 | { | |
4fbb74a6 AM |
10244 | if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) |
10245 | && sym->st_shndx < SHN_LORESERVE) | |
c0d5a53d L |
10246 | { |
10247 | /* The gABI doesn't support dynamic symbols in output sections | |
a0c8462f | 10248 | beyond 64k. */ |
4eca0228 | 10249 | _bfd_error_handler |
695344c0 | 10250 | /* xgettext:c-format */ |
9793eb77 | 10251 | (_("%pB: too many sections: %d (>= %d)"), |
4fbb74a6 | 10252 | abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); |
c0d5a53d | 10253 | bfd_set_error (bfd_error_nonrepresentable_section); |
0a1b45a2 | 10254 | return false; |
c0d5a53d | 10255 | } |
0a1b45a2 | 10256 | return true; |
c0d5a53d L |
10257 | } |
10258 | ||
c152c796 AM |
10259 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in |
10260 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
10261 | versioned symbol that would normally require an explicit version. | |
10262 | We also handle the case that a DSO references a hidden symbol | |
10263 | which may be satisfied by a versioned symbol in another DSO. */ | |
10264 | ||
0a1b45a2 | 10265 | static bool |
c152c796 AM |
10266 | elf_link_check_versioned_symbol (struct bfd_link_info *info, |
10267 | const struct elf_backend_data *bed, | |
10268 | struct elf_link_hash_entry *h) | |
10269 | { | |
10270 | bfd *abfd; | |
10271 | struct elf_link_loaded_list *loaded; | |
10272 | ||
10273 | if (!is_elf_hash_table (info->hash)) | |
0a1b45a2 | 10274 | return false; |
c152c796 | 10275 | |
90c984fc L |
10276 | /* Check indirect symbol. */ |
10277 | while (h->root.type == bfd_link_hash_indirect) | |
10278 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10279 | ||
c152c796 AM |
10280 | switch (h->root.type) |
10281 | { | |
10282 | default: | |
10283 | abfd = NULL; | |
10284 | break; | |
10285 | ||
10286 | case bfd_link_hash_undefined: | |
10287 | case bfd_link_hash_undefweak: | |
10288 | abfd = h->root.u.undef.abfd; | |
f4ab0e2d L |
10289 | if (abfd == NULL |
10290 | || (abfd->flags & DYNAMIC) == 0 | |
e56f61be | 10291 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
0a1b45a2 | 10292 | return false; |
c152c796 AM |
10293 | break; |
10294 | ||
10295 | case bfd_link_hash_defined: | |
10296 | case bfd_link_hash_defweak: | |
10297 | abfd = h->root.u.def.section->owner; | |
10298 | break; | |
10299 | ||
10300 | case bfd_link_hash_common: | |
10301 | abfd = h->root.u.c.p->section->owner; | |
10302 | break; | |
10303 | } | |
10304 | BFD_ASSERT (abfd != NULL); | |
10305 | ||
e310298c | 10306 | for (loaded = elf_hash_table (info)->dyn_loaded; |
c152c796 AM |
10307 | loaded != NULL; |
10308 | loaded = loaded->next) | |
10309 | { | |
10310 | bfd *input; | |
10311 | Elf_Internal_Shdr *hdr; | |
ef53be89 AM |
10312 | size_t symcount; |
10313 | size_t extsymcount; | |
10314 | size_t extsymoff; | |
c152c796 AM |
10315 | Elf_Internal_Shdr *versymhdr; |
10316 | Elf_Internal_Sym *isym; | |
10317 | Elf_Internal_Sym *isymend; | |
10318 | Elf_Internal_Sym *isymbuf; | |
10319 | Elf_External_Versym *ever; | |
10320 | Elf_External_Versym *extversym; | |
10321 | ||
10322 | input = loaded->abfd; | |
10323 | ||
10324 | /* We check each DSO for a possible hidden versioned definition. */ | |
10325 | if (input == abfd | |
c152c796 AM |
10326 | || elf_dynversym (input) == 0) |
10327 | continue; | |
10328 | ||
10329 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
10330 | ||
10331 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
10332 | if (elf_bad_symtab (input)) | |
10333 | { | |
10334 | extsymcount = symcount; | |
10335 | extsymoff = 0; | |
10336 | } | |
10337 | else | |
10338 | { | |
10339 | extsymcount = symcount - hdr->sh_info; | |
10340 | extsymoff = hdr->sh_info; | |
10341 | } | |
10342 | ||
10343 | if (extsymcount == 0) | |
10344 | continue; | |
10345 | ||
10346 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
10347 | NULL, NULL, NULL); | |
10348 | if (isymbuf == NULL) | |
0a1b45a2 | 10349 | return false; |
c152c796 AM |
10350 | |
10351 | /* Read in any version definitions. */ | |
10352 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
c152c796 | 10353 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 |
2bb3687b AM |
10354 | || (extversym = (Elf_External_Versym *) |
10355 | _bfd_malloc_and_read (input, versymhdr->sh_size, | |
10356 | versymhdr->sh_size)) == NULL) | |
c152c796 | 10357 | { |
c152c796 | 10358 | free (isymbuf); |
0a1b45a2 | 10359 | return false; |
c152c796 AM |
10360 | } |
10361 | ||
10362 | ever = extversym + extsymoff; | |
10363 | isymend = isymbuf + extsymcount; | |
10364 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
10365 | { | |
10366 | const char *name; | |
10367 | Elf_Internal_Versym iver; | |
10368 | unsigned short version_index; | |
10369 | ||
10370 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
10371 | || isym->st_shndx == SHN_UNDEF) | |
10372 | continue; | |
10373 | ||
10374 | name = bfd_elf_string_from_elf_section (input, | |
10375 | hdr->sh_link, | |
10376 | isym->st_name); | |
10377 | if (strcmp (name, h->root.root.string) != 0) | |
10378 | continue; | |
10379 | ||
10380 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
10381 | ||
d023c380 L |
10382 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 |
10383 | && !(h->def_regular | |
10384 | && h->forced_local)) | |
c152c796 AM |
10385 | { |
10386 | /* If we have a non-hidden versioned sym, then it should | |
d023c380 L |
10387 | have provided a definition for the undefined sym unless |
10388 | it is defined in a non-shared object and forced local. | |
10389 | */ | |
c152c796 AM |
10390 | abort (); |
10391 | } | |
10392 | ||
10393 | version_index = iver.vs_vers & VERSYM_VERSION; | |
10394 | if (version_index == 1 || version_index == 2) | |
10395 | { | |
10396 | /* This is the base or first version. We can use it. */ | |
10397 | free (extversym); | |
10398 | free (isymbuf); | |
0a1b45a2 | 10399 | return true; |
c152c796 AM |
10400 | } |
10401 | } | |
10402 | ||
10403 | free (extversym); | |
10404 | free (isymbuf); | |
10405 | } | |
10406 | ||
0a1b45a2 | 10407 | return false; |
c152c796 AM |
10408 | } |
10409 | ||
b8871f35 L |
10410 | /* Convert ELF common symbol TYPE. */ |
10411 | ||
10412 | static int | |
10413 | elf_link_convert_common_type (struct bfd_link_info *info, int type) | |
10414 | { | |
10415 | /* Commom symbol can only appear in relocatable link. */ | |
10416 | if (!bfd_link_relocatable (info)) | |
10417 | abort (); | |
10418 | switch (info->elf_stt_common) | |
10419 | { | |
10420 | case unchanged: | |
10421 | break; | |
10422 | case elf_stt_common: | |
10423 | type = STT_COMMON; | |
10424 | break; | |
10425 | case no_elf_stt_common: | |
10426 | type = STT_OBJECT; | |
10427 | break; | |
10428 | } | |
10429 | return type; | |
10430 | } | |
10431 | ||
c152c796 AM |
10432 | /* Add an external symbol to the symbol table. This is called from |
10433 | the hash table traversal routine. When generating a shared object, | |
10434 | we go through the symbol table twice. The first time we output | |
10435 | anything that might have been forced to local scope in a version | |
10436 | script. The second time we output the symbols that are still | |
10437 | global symbols. */ | |
10438 | ||
0a1b45a2 | 10439 | static bool |
7686d77d | 10440 | elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) |
c152c796 | 10441 | { |
7686d77d | 10442 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; |
a50b1753 | 10443 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
8b127cbc | 10444 | struct elf_final_link_info *flinfo = eoinfo->flinfo; |
0a1b45a2 | 10445 | bool strip; |
c152c796 AM |
10446 | Elf_Internal_Sym sym; |
10447 | asection *input_sec; | |
10448 | const struct elf_backend_data *bed; | |
6e0b88f1 AM |
10449 | long indx; |
10450 | int ret; | |
b8871f35 | 10451 | unsigned int type; |
c152c796 AM |
10452 | |
10453 | if (h->root.type == bfd_link_hash_warning) | |
10454 | { | |
10455 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
10456 | if (h->root.type == bfd_link_hash_new) | |
0a1b45a2 | 10457 | return true; |
c152c796 AM |
10458 | } |
10459 | ||
10460 | /* Decide whether to output this symbol in this pass. */ | |
10461 | if (eoinfo->localsyms) | |
10462 | { | |
4deb8f71 | 10463 | if (!h->forced_local) |
0a1b45a2 | 10464 | return true; |
c152c796 AM |
10465 | } |
10466 | else | |
10467 | { | |
4deb8f71 | 10468 | if (h->forced_local) |
0a1b45a2 | 10469 | return true; |
c152c796 AM |
10470 | } |
10471 | ||
8b127cbc | 10472 | bed = get_elf_backend_data (flinfo->output_bfd); |
c152c796 | 10473 | |
12ac1cf5 | 10474 | if (h->root.type == bfd_link_hash_undefined) |
c152c796 | 10475 | { |
12ac1cf5 NC |
10476 | /* If we have an undefined symbol reference here then it must have |
10477 | come from a shared library that is being linked in. (Undefined | |
98da7939 L |
10478 | references in regular files have already been handled unless |
10479 | they are in unreferenced sections which are removed by garbage | |
10480 | collection). */ | |
0a1b45a2 | 10481 | bool ignore_undef = false; |
12ac1cf5 NC |
10482 | |
10483 | /* Some symbols may be special in that the fact that they're | |
10484 | undefined can be safely ignored - let backend determine that. */ | |
10485 | if (bed->elf_backend_ignore_undef_symbol) | |
10486 | ignore_undef = bed->elf_backend_ignore_undef_symbol (h); | |
10487 | ||
10488 | /* If we are reporting errors for this situation then do so now. */ | |
89a2ee5a | 10489 | if (!ignore_undef |
c54f1524 | 10490 | && h->ref_dynamic_nonweak |
8b127cbc AM |
10491 | && (!h->ref_regular || flinfo->info->gc_sections) |
10492 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h) | |
10493 | && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
95a51568 FS |
10494 | { |
10495 | flinfo->info->callbacks->undefined_symbol | |
10496 | (flinfo->info, h->root.root.string, | |
10497 | h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0, | |
10498 | flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE | |
10499 | && !flinfo->info->warn_unresolved_syms); | |
10500 | } | |
97196564 L |
10501 | |
10502 | /* Strip a global symbol defined in a discarded section. */ | |
10503 | if (h->indx == -3) | |
0a1b45a2 | 10504 | return true; |
c152c796 AM |
10505 | } |
10506 | ||
10507 | /* We should also warn if a forced local symbol is referenced from | |
10508 | shared libraries. */ | |
0e1862bb | 10509 | if (bfd_link_executable (flinfo->info) |
f5385ebf AM |
10510 | && h->forced_local |
10511 | && h->ref_dynamic | |
371a5866 | 10512 | && h->def_regular |
f5385ebf | 10513 | && !h->dynamic_def |
ee659f1f | 10514 | && h->ref_dynamic_nonweak |
8b127cbc | 10515 | && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) |
c152c796 | 10516 | { |
17d078c5 AM |
10517 | bfd *def_bfd; |
10518 | const char *msg; | |
90c984fc L |
10519 | struct elf_link_hash_entry *hi = h; |
10520 | ||
10521 | /* Check indirect symbol. */ | |
10522 | while (hi->root.type == bfd_link_hash_indirect) | |
10523 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
17d078c5 AM |
10524 | |
10525 | if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) | |
695344c0 | 10526 | /* xgettext:c-format */ |
871b3ab2 | 10527 | msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 10528 | else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) |
695344c0 | 10529 | /* xgettext:c-format */ |
871b3ab2 | 10530 | msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO"); |
17d078c5 | 10531 | else |
695344c0 | 10532 | /* xgettext:c-format */ |
871b3ab2 | 10533 | msg = _("%pB: local symbol `%s' in %pB is referenced by DSO"); |
8b127cbc | 10534 | def_bfd = flinfo->output_bfd; |
90c984fc L |
10535 | if (hi->root.u.def.section != bfd_abs_section_ptr) |
10536 | def_bfd = hi->root.u.def.section->owner; | |
c08bb8dd AM |
10537 | _bfd_error_handler (msg, flinfo->output_bfd, |
10538 | h->root.root.string, def_bfd); | |
17d078c5 | 10539 | bfd_set_error (bfd_error_bad_value); |
0a1b45a2 AM |
10540 | eoinfo->failed = true; |
10541 | return false; | |
c152c796 AM |
10542 | } |
10543 | ||
10544 | /* We don't want to output symbols that have never been mentioned by | |
10545 | a regular file, or that we have been told to strip. However, if | |
10546 | h->indx is set to -2, the symbol is used by a reloc and we must | |
10547 | output it. */ | |
0a1b45a2 | 10548 | strip = false; |
c152c796 | 10549 | if (h->indx == -2) |
d983c8c5 | 10550 | ; |
f5385ebf | 10551 | else if ((h->def_dynamic |
77cfaee6 AM |
10552 | || h->ref_dynamic |
10553 | || h->root.type == bfd_link_hash_new) | |
f5385ebf AM |
10554 | && !h->def_regular |
10555 | && !h->ref_regular) | |
0a1b45a2 | 10556 | strip = true; |
8b127cbc | 10557 | else if (flinfo->info->strip == strip_all) |
0a1b45a2 | 10558 | strip = true; |
8b127cbc AM |
10559 | else if (flinfo->info->strip == strip_some |
10560 | && bfd_hash_lookup (flinfo->info->keep_hash, | |
0a1b45a2 AM |
10561 | h->root.root.string, false, false) == NULL) |
10562 | strip = true; | |
d56d55e7 AM |
10563 | else if ((h->root.type == bfd_link_hash_defined |
10564 | || h->root.type == bfd_link_hash_defweak) | |
8b127cbc | 10565 | && ((flinfo->info->strip_discarded |
dbaa2011 | 10566 | && discarded_section (h->root.u.def.section)) |
ca4be51c AM |
10567 | || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 |
10568 | && h->root.u.def.section->owner != NULL | |
d56d55e7 | 10569 | && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) |
0a1b45a2 | 10570 | strip = true; |
9e2278f5 AM |
10571 | else if ((h->root.type == bfd_link_hash_undefined |
10572 | || h->root.type == bfd_link_hash_undefweak) | |
10573 | && h->root.u.undef.abfd != NULL | |
10574 | && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) | |
0a1b45a2 | 10575 | strip = true; |
c152c796 | 10576 | |
b8871f35 L |
10577 | type = h->type; |
10578 | ||
c152c796 | 10579 | /* If we're stripping it, and it's not a dynamic symbol, there's |
d983c8c5 AM |
10580 | nothing else to do. However, if it is a forced local symbol or |
10581 | an ifunc symbol we need to give the backend finish_dynamic_symbol | |
10582 | function a chance to make it dynamic. */ | |
c152c796 AM |
10583 | if (strip |
10584 | && h->dynindx == -1 | |
b8871f35 | 10585 | && type != STT_GNU_IFUNC |
f5385ebf | 10586 | && !h->forced_local) |
0a1b45a2 | 10587 | return true; |
c152c796 AM |
10588 | |
10589 | sym.st_value = 0; | |
10590 | sym.st_size = h->size; | |
10591 | sym.st_other = h->other; | |
c152c796 AM |
10592 | switch (h->root.type) |
10593 | { | |
10594 | default: | |
10595 | case bfd_link_hash_new: | |
10596 | case bfd_link_hash_warning: | |
10597 | abort (); | |
0a1b45a2 | 10598 | return false; |
c152c796 AM |
10599 | |
10600 | case bfd_link_hash_undefined: | |
10601 | case bfd_link_hash_undefweak: | |
10602 | input_sec = bfd_und_section_ptr; | |
10603 | sym.st_shndx = SHN_UNDEF; | |
10604 | break; | |
10605 | ||
10606 | case bfd_link_hash_defined: | |
10607 | case bfd_link_hash_defweak: | |
10608 | { | |
10609 | input_sec = h->root.u.def.section; | |
10610 | if (input_sec->output_section != NULL) | |
10611 | { | |
10612 | sym.st_shndx = | |
8b127cbc | 10613 | _bfd_elf_section_from_bfd_section (flinfo->output_bfd, |
c152c796 AM |
10614 | input_sec->output_section); |
10615 | if (sym.st_shndx == SHN_BAD) | |
10616 | { | |
4eca0228 | 10617 | _bfd_error_handler |
695344c0 | 10618 | /* xgettext:c-format */ |
871b3ab2 | 10619 | (_("%pB: could not find output section %pA for input section %pA"), |
8b127cbc | 10620 | flinfo->output_bfd, input_sec->output_section, input_sec); |
17d078c5 | 10621 | bfd_set_error (bfd_error_nonrepresentable_section); |
0a1b45a2 AM |
10622 | eoinfo->failed = true; |
10623 | return false; | |
c152c796 AM |
10624 | } |
10625 | ||
10626 | /* ELF symbols in relocatable files are section relative, | |
10627 | but in nonrelocatable files they are virtual | |
10628 | addresses. */ | |
10629 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
0e1862bb | 10630 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
10631 | { |
10632 | sym.st_value += input_sec->output_section->vma; | |
10633 | if (h->type == STT_TLS) | |
10634 | { | |
8b127cbc | 10635 | asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; |
430a16a5 NC |
10636 | if (tls_sec != NULL) |
10637 | sym.st_value -= tls_sec->vma; | |
c152c796 AM |
10638 | } |
10639 | } | |
10640 | } | |
10641 | else | |
10642 | { | |
10643 | BFD_ASSERT (input_sec->owner == NULL | |
10644 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
10645 | sym.st_shndx = SHN_UNDEF; | |
10646 | input_sec = bfd_und_section_ptr; | |
10647 | } | |
10648 | } | |
10649 | break; | |
10650 | ||
10651 | case bfd_link_hash_common: | |
10652 | input_sec = h->root.u.c.p->section; | |
a4d8e49b | 10653 | sym.st_shndx = bed->common_section_index (input_sec); |
c152c796 AM |
10654 | sym.st_value = 1 << h->root.u.c.p->alignment_power; |
10655 | break; | |
10656 | ||
10657 | case bfd_link_hash_indirect: | |
10658 | /* These symbols are created by symbol versioning. They point | |
10659 | to the decorated version of the name. For example, if the | |
10660 | symbol foo@@GNU_1.2 is the default, which should be used when | |
10661 | foo is used with no version, then we add an indirect symbol | |
10662 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
10663 | since the indirected symbol is already in the hash table. */ | |
0a1b45a2 | 10664 | return true; |
c152c796 AM |
10665 | } |
10666 | ||
b8871f35 L |
10667 | if (type == STT_COMMON || type == STT_OBJECT) |
10668 | switch (h->root.type) | |
10669 | { | |
10670 | case bfd_link_hash_common: | |
10671 | type = elf_link_convert_common_type (flinfo->info, type); | |
10672 | break; | |
10673 | case bfd_link_hash_defined: | |
10674 | case bfd_link_hash_defweak: | |
10675 | if (bed->common_definition (&sym)) | |
10676 | type = elf_link_convert_common_type (flinfo->info, type); | |
10677 | else | |
10678 | type = STT_OBJECT; | |
10679 | break; | |
10680 | case bfd_link_hash_undefined: | |
10681 | case bfd_link_hash_undefweak: | |
10682 | break; | |
10683 | default: | |
10684 | abort (); | |
10685 | } | |
10686 | ||
4deb8f71 | 10687 | if (h->forced_local) |
b8871f35 L |
10688 | { |
10689 | sym.st_info = ELF_ST_INFO (STB_LOCAL, type); | |
10690 | /* Turn off visibility on local symbol. */ | |
10691 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); | |
10692 | } | |
10693 | /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ | |
10694 | else if (h->unique_global && h->def_regular) | |
10695 | sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); | |
10696 | else if (h->root.type == bfd_link_hash_undefweak | |
10697 | || h->root.type == bfd_link_hash_defweak) | |
10698 | sym.st_info = ELF_ST_INFO (STB_WEAK, type); | |
10699 | else | |
10700 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
10701 | sym.st_target_internal = h->target_internal; | |
10702 | ||
c152c796 AM |
10703 | /* Give the processor backend a chance to tweak the symbol value, |
10704 | and also to finish up anything that needs to be done for this | |
10705 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
3aa14d16 | 10706 | forced local syms when non-shared is due to a historical quirk. |
5f35ea9c | 10707 | STT_GNU_IFUNC symbol must go through PLT. */ |
3aa14d16 | 10708 | if ((h->type == STT_GNU_IFUNC |
5f35ea9c | 10709 | && h->def_regular |
0e1862bb | 10710 | && !bfd_link_relocatable (flinfo->info)) |
3aa14d16 L |
10711 | || ((h->dynindx != -1 |
10712 | || h->forced_local) | |
0e1862bb | 10713 | && ((bfd_link_pic (flinfo->info) |
3aa14d16 L |
10714 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
10715 | || h->root.type != bfd_link_hash_undefweak)) | |
10716 | || !h->forced_local) | |
8b127cbc | 10717 | && elf_hash_table (flinfo->info)->dynamic_sections_created)) |
c152c796 AM |
10718 | { |
10719 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
8b127cbc | 10720 | (flinfo->output_bfd, flinfo->info, h, &sym))) |
c152c796 | 10721 | { |
0a1b45a2 AM |
10722 | eoinfo->failed = true; |
10723 | return false; | |
c152c796 AM |
10724 | } |
10725 | } | |
10726 | ||
10727 | /* If we are marking the symbol as undefined, and there are no | |
10728 | non-weak references to this symbol from a regular object, then | |
10729 | mark the symbol as weak undefined; if there are non-weak | |
10730 | references, mark the symbol as strong. We can't do this earlier, | |
10731 | because it might not be marked as undefined until the | |
10732 | finish_dynamic_symbol routine gets through with it. */ | |
10733 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 10734 | && h->ref_regular |
c152c796 AM |
10735 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
10736 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
10737 | { | |
10738 | int bindtype; | |
b8871f35 | 10739 | type = ELF_ST_TYPE (sym.st_info); |
2955ec4c L |
10740 | |
10741 | /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ | |
10742 | if (type == STT_GNU_IFUNC) | |
10743 | type = STT_FUNC; | |
c152c796 | 10744 | |
f5385ebf | 10745 | if (h->ref_regular_nonweak) |
c152c796 AM |
10746 | bindtype = STB_GLOBAL; |
10747 | else | |
10748 | bindtype = STB_WEAK; | |
2955ec4c | 10749 | sym.st_info = ELF_ST_INFO (bindtype, type); |
c152c796 AM |
10750 | } |
10751 | ||
bda987c2 CD |
10752 | /* If this is a symbol defined in a dynamic library, don't use the |
10753 | symbol size from the dynamic library. Relinking an executable | |
10754 | against a new library may introduce gratuitous changes in the | |
10755 | executable's symbols if we keep the size. */ | |
10756 | if (sym.st_shndx == SHN_UNDEF | |
10757 | && !h->def_regular | |
10758 | && h->def_dynamic) | |
10759 | sym.st_size = 0; | |
10760 | ||
c152c796 AM |
10761 | /* If a non-weak symbol with non-default visibility is not defined |
10762 | locally, it is a fatal error. */ | |
0e1862bb | 10763 | if (!bfd_link_relocatable (flinfo->info) |
c152c796 AM |
10764 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT |
10765 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
10766 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 10767 | && !h->def_regular) |
c152c796 | 10768 | { |
17d078c5 AM |
10769 | const char *msg; |
10770 | ||
10771 | if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) | |
695344c0 | 10772 | /* xgettext:c-format */ |
871b3ab2 | 10773 | msg = _("%pB: protected symbol `%s' isn't defined"); |
17d078c5 | 10774 | else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) |
695344c0 | 10775 | /* xgettext:c-format */ |
871b3ab2 | 10776 | msg = _("%pB: internal symbol `%s' isn't defined"); |
17d078c5 | 10777 | else |
695344c0 | 10778 | /* xgettext:c-format */ |
871b3ab2 | 10779 | msg = _("%pB: hidden symbol `%s' isn't defined"); |
4eca0228 | 10780 | _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); |
17d078c5 | 10781 | bfd_set_error (bfd_error_bad_value); |
0a1b45a2 AM |
10782 | eoinfo->failed = true; |
10783 | return false; | |
c152c796 AM |
10784 | } |
10785 | ||
10786 | /* If this symbol should be put in the .dynsym section, then put it | |
10787 | there now. We already know the symbol index. We also fill in | |
10788 | the entry in the .hash section. */ | |
1c2649ed EB |
10789 | if (h->dynindx != -1 |
10790 | && elf_hash_table (flinfo->info)->dynamic_sections_created | |
10791 | && elf_hash_table (flinfo->info)->dynsym != NULL | |
10792 | && !discarded_section (elf_hash_table (flinfo->info)->dynsym)) | |
c152c796 | 10793 | { |
c152c796 AM |
10794 | bfd_byte *esym; |
10795 | ||
90c984fc L |
10796 | /* Since there is no version information in the dynamic string, |
10797 | if there is no version info in symbol version section, we will | |
1659f720 | 10798 | have a run-time problem if not linking executable, referenced |
4deb8f71 | 10799 | by shared library, or not bound locally. */ |
1659f720 | 10800 | if (h->verinfo.verdef == NULL |
0e1862bb | 10801 | && (!bfd_link_executable (flinfo->info) |
1659f720 L |
10802 | || h->ref_dynamic |
10803 | || !h->def_regular)) | |
90c984fc L |
10804 | { |
10805 | char *p = strrchr (h->root.root.string, ELF_VER_CHR); | |
10806 | ||
10807 | if (p && p [1] != '\0') | |
10808 | { | |
4eca0228 | 10809 | _bfd_error_handler |
695344c0 | 10810 | /* xgettext:c-format */ |
9793eb77 | 10811 | (_("%pB: no symbol version section for versioned symbol `%s'"), |
90c984fc | 10812 | flinfo->output_bfd, h->root.root.string); |
0a1b45a2 AM |
10813 | eoinfo->failed = true; |
10814 | return false; | |
90c984fc L |
10815 | } |
10816 | } | |
10817 | ||
c152c796 | 10818 | sym.st_name = h->dynstr_index; |
cae1fbbb L |
10819 | esym = (elf_hash_table (flinfo->info)->dynsym->contents |
10820 | + h->dynindx * bed->s->sizeof_sym); | |
8b127cbc | 10821 | if (!check_dynsym (flinfo->output_bfd, &sym)) |
c0d5a53d | 10822 | { |
0a1b45a2 AM |
10823 | eoinfo->failed = true; |
10824 | return false; | |
c0d5a53d | 10825 | } |
3d16b64e NA |
10826 | |
10827 | /* Inform the linker of the addition of this symbol. */ | |
10828 | ||
10829 | if (flinfo->info->callbacks->ctf_new_dynsym) | |
10830 | flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym); | |
10831 | ||
8b127cbc | 10832 | bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); |
c152c796 | 10833 | |
8b127cbc | 10834 | if (flinfo->hash_sec != NULL) |
fdc90cb4 JJ |
10835 | { |
10836 | size_t hash_entry_size; | |
10837 | bfd_byte *bucketpos; | |
10838 | bfd_vma chain; | |
41198d0c L |
10839 | size_t bucketcount; |
10840 | size_t bucket; | |
10841 | ||
8b127cbc | 10842 | bucketcount = elf_hash_table (flinfo->info)->bucketcount; |
41198d0c | 10843 | bucket = h->u.elf_hash_value % bucketcount; |
fdc90cb4 JJ |
10844 | |
10845 | hash_entry_size | |
8b127cbc AM |
10846 | = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; |
10847 | bucketpos = ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 | 10848 | + (bucket + 2) * hash_entry_size); |
8b127cbc AM |
10849 | chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); |
10850 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, | |
10851 | bucketpos); | |
10852 | bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, | |
10853 | ((bfd_byte *) flinfo->hash_sec->contents | |
fdc90cb4 JJ |
10854 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); |
10855 | } | |
c152c796 | 10856 | |
8b127cbc | 10857 | if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) |
c152c796 AM |
10858 | { |
10859 | Elf_Internal_Versym iversym; | |
10860 | Elf_External_Versym *eversym; | |
10861 | ||
5fa370e4 | 10862 | if (!h->def_regular && !ELF_COMMON_DEF_P (h)) |
c152c796 | 10863 | { |
7b20f099 AM |
10864 | if (h->verinfo.verdef == NULL |
10865 | || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) | |
10866 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) | |
eb6e6af8 | 10867 | iversym.vs_vers = 1; |
c152c796 AM |
10868 | else |
10869 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
10870 | } | |
10871 | else | |
10872 | { | |
10873 | if (h->verinfo.vertree == NULL) | |
10874 | iversym.vs_vers = 1; | |
10875 | else | |
10876 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
8b127cbc | 10877 | if (flinfo->info->create_default_symver) |
3e3b46e5 | 10878 | iversym.vs_vers++; |
c152c796 AM |
10879 | } |
10880 | ||
422f1182 | 10881 | /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is |
6e33951e | 10882 | defined locally. */ |
422f1182 | 10883 | if (h->versioned == versioned_hidden && h->def_regular) |
c152c796 AM |
10884 | iversym.vs_vers |= VERSYM_HIDDEN; |
10885 | ||
8b127cbc | 10886 | eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; |
c152c796 | 10887 | eversym += h->dynindx; |
8b127cbc | 10888 | _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); |
c152c796 AM |
10889 | } |
10890 | } | |
10891 | ||
d983c8c5 AM |
10892 | /* If the symbol is undefined, and we didn't output it to .dynsym, |
10893 | strip it from .symtab too. Obviously we can't do this for | |
10894 | relocatable output or when needed for --emit-relocs. */ | |
10895 | else if (input_sec == bfd_und_section_ptr | |
10896 | && h->indx != -2 | |
66cae560 NC |
10897 | /* PR 22319 Do not strip global undefined symbols marked as being needed. */ |
10898 | && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) | |
0e1862bb | 10899 | && !bfd_link_relocatable (flinfo->info)) |
0a1b45a2 | 10900 | return true; |
66cae560 | 10901 | |
d983c8c5 AM |
10902 | /* Also strip others that we couldn't earlier due to dynamic symbol |
10903 | processing. */ | |
10904 | if (strip) | |
0a1b45a2 | 10905 | return true; |
d983c8c5 | 10906 | if ((input_sec->flags & SEC_EXCLUDE) != 0) |
0a1b45a2 | 10907 | return true; |
c152c796 | 10908 | |
2ec55de3 AM |
10909 | /* Output a FILE symbol so that following locals are not associated |
10910 | with the wrong input file. We need one for forced local symbols | |
10911 | if we've seen more than one FILE symbol or when we have exactly | |
10912 | one FILE symbol but global symbols are present in a file other | |
10913 | than the one with the FILE symbol. We also need one if linker | |
10914 | defined symbols are present. In practice these conditions are | |
10915 | always met, so just emit the FILE symbol unconditionally. */ | |
10916 | if (eoinfo->localsyms | |
10917 | && !eoinfo->file_sym_done | |
10918 | && eoinfo->flinfo->filesym_count != 0) | |
10919 | { | |
10920 | Elf_Internal_Sym fsym; | |
10921 | ||
10922 | memset (&fsym, 0, sizeof (fsym)); | |
10923 | fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
10924 | fsym.st_shndx = SHN_ABS; | |
ef10c3ac L |
10925 | if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, |
10926 | bfd_und_section_ptr, NULL)) | |
0a1b45a2 | 10927 | return false; |
2ec55de3 | 10928 | |
0a1b45a2 | 10929 | eoinfo->file_sym_done = true; |
2ec55de3 AM |
10930 | } |
10931 | ||
8b127cbc | 10932 | indx = bfd_get_symcount (flinfo->output_bfd); |
ef10c3ac L |
10933 | ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, |
10934 | input_sec, h); | |
6e0b88f1 | 10935 | if (ret == 0) |
c152c796 | 10936 | { |
0a1b45a2 AM |
10937 | eoinfo->failed = true; |
10938 | return false; | |
c152c796 | 10939 | } |
6e0b88f1 AM |
10940 | else if (ret == 1) |
10941 | h->indx = indx; | |
10942 | else if (h->indx == -2) | |
10943 | abort(); | |
c152c796 | 10944 | |
0a1b45a2 | 10945 | return true; |
c152c796 AM |
10946 | } |
10947 | ||
cdd3575c AM |
10948 | /* Return TRUE if special handling is done for relocs in SEC against |
10949 | symbols defined in discarded sections. */ | |
10950 | ||
0a1b45a2 | 10951 | static bool |
c152c796 AM |
10952 | elf_section_ignore_discarded_relocs (asection *sec) |
10953 | { | |
10954 | const struct elf_backend_data *bed; | |
10955 | ||
cdd3575c AM |
10956 | switch (sec->sec_info_type) |
10957 | { | |
dbaa2011 AM |
10958 | case SEC_INFO_TYPE_STABS: |
10959 | case SEC_INFO_TYPE_EH_FRAME: | |
2f0c68f2 | 10960 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
cf0e0a0b | 10961 | case SEC_INFO_TYPE_SFRAME: |
0a1b45a2 | 10962 | return true; |
cdd3575c AM |
10963 | default: |
10964 | break; | |
10965 | } | |
c152c796 AM |
10966 | |
10967 | bed = get_elf_backend_data (sec->owner); | |
10968 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
10969 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
0a1b45a2 | 10970 | return true; |
c152c796 | 10971 | |
0a1b45a2 | 10972 | return false; |
c152c796 AM |
10973 | } |
10974 | ||
9e66c942 AM |
10975 | /* Return a mask saying how ld should treat relocations in SEC against |
10976 | symbols defined in discarded sections. If this function returns | |
10977 | COMPLAIN set, ld will issue a warning message. If this function | |
10978 | returns PRETEND set, and the discarded section was link-once and the | |
10979 | same size as the kept link-once section, ld will pretend that the | |
10980 | symbol was actually defined in the kept section. Otherwise ld will | |
10981 | zero the reloc (at least that is the intent, but some cooperation by | |
10982 | the target dependent code is needed, particularly for REL targets). */ | |
10983 | ||
8a696751 AM |
10984 | unsigned int |
10985 | _bfd_elf_default_action_discarded (asection *sec) | |
cdd3575c | 10986 | { |
853ba678 J |
10987 | const struct elf_backend_data *bed; |
10988 | bed = get_elf_backend_data (sec->owner); | |
10989 | ||
9e66c942 | 10990 | if (sec->flags & SEC_DEBUGGING) |
69d54b1b | 10991 | return PRETEND; |
cdd3575c AM |
10992 | |
10993 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 10994 | return 0; |
cdd3575c | 10995 | |
853ba678 J |
10996 | if (bed->elf_backend_can_make_multiple_eh_frame |
10997 | && strncmp (sec->name, ".eh_frame.", 10) == 0) | |
10998 | return 0; | |
10999 | ||
cf0e0a0b IB |
11000 | if (strcmp (".sframe", sec->name) == 0) |
11001 | return 0; | |
11002 | ||
cdd3575c | 11003 | if (strcmp (".gcc_except_table", sec->name) == 0) |
9e66c942 | 11004 | return 0; |
cdd3575c | 11005 | |
9e66c942 | 11006 | return COMPLAIN | PRETEND; |
cdd3575c AM |
11007 | } |
11008 | ||
3d7f7666 L |
11009 | /* Find a match between a section and a member of a section group. */ |
11010 | ||
11011 | static asection * | |
c0f00686 L |
11012 | match_group_member (asection *sec, asection *group, |
11013 | struct bfd_link_info *info) | |
3d7f7666 L |
11014 | { |
11015 | asection *first = elf_next_in_group (group); | |
11016 | asection *s = first; | |
11017 | ||
11018 | while (s != NULL) | |
11019 | { | |
c0f00686 | 11020 | if (bfd_elf_match_symbols_in_sections (s, sec, info)) |
3d7f7666 L |
11021 | return s; |
11022 | ||
83180ade | 11023 | s = elf_next_in_group (s); |
3d7f7666 L |
11024 | if (s == first) |
11025 | break; | |
11026 | } | |
11027 | ||
11028 | return NULL; | |
11029 | } | |
11030 | ||
01b3c8ab | 11031 | /* Check if the kept section of a discarded section SEC can be used |
c2370991 AM |
11032 | to replace it. Return the replacement if it is OK. Otherwise return |
11033 | NULL. */ | |
01b3c8ab L |
11034 | |
11035 | asection * | |
c0f00686 | 11036 | _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) |
01b3c8ab L |
11037 | { |
11038 | asection *kept; | |
11039 | ||
11040 | kept = sec->kept_section; | |
11041 | if (kept != NULL) | |
11042 | { | |
c2370991 | 11043 | if ((kept->flags & SEC_GROUP) != 0) |
c0f00686 | 11044 | kept = match_group_member (sec, kept, info); |
58349d00 L |
11045 | if (kept != NULL) |
11046 | { | |
11047 | if ((sec->rawsize != 0 ? sec->rawsize : sec->size) | |
11048 | != (kept->rawsize != 0 ? kept->rawsize : kept->size)) | |
11049 | kept = NULL; | |
11050 | else | |
11051 | { | |
11052 | /* Get the real kept section. */ | |
11053 | asection *next; | |
11054 | for (next = kept->kept_section; | |
11055 | next != NULL; | |
11056 | next = next->kept_section) | |
11057 | kept = next; | |
11058 | } | |
11059 | } | |
c2370991 | 11060 | sec->kept_section = kept; |
01b3c8ab L |
11061 | } |
11062 | return kept; | |
11063 | } | |
11064 | ||
c152c796 AM |
11065 | /* Link an input file into the linker output file. This function |
11066 | handles all the sections and relocations of the input file at once. | |
11067 | This is so that we only have to read the local symbols once, and | |
11068 | don't have to keep them in memory. */ | |
11069 | ||
0a1b45a2 | 11070 | static bool |
8b127cbc | 11071 | elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) |
c152c796 | 11072 | { |
ece5ef60 | 11073 | int (*relocate_section) |
c152c796 AM |
11074 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
11075 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
11076 | bfd *output_bfd; | |
11077 | Elf_Internal_Shdr *symtab_hdr; | |
11078 | size_t locsymcount; | |
11079 | size_t extsymoff; | |
11080 | Elf_Internal_Sym *isymbuf; | |
11081 | Elf_Internal_Sym *isym; | |
11082 | Elf_Internal_Sym *isymend; | |
11083 | long *pindex; | |
11084 | asection **ppsection; | |
11085 | asection *o; | |
11086 | const struct elf_backend_data *bed; | |
c152c796 | 11087 | struct elf_link_hash_entry **sym_hashes; |
310fd250 L |
11088 | bfd_size_type address_size; |
11089 | bfd_vma r_type_mask; | |
11090 | int r_sym_shift; | |
0a1b45a2 | 11091 | bool have_file_sym = false; |
c152c796 | 11092 | |
8b127cbc | 11093 | output_bfd = flinfo->output_bfd; |
c152c796 AM |
11094 | bed = get_elf_backend_data (output_bfd); |
11095 | relocate_section = bed->elf_backend_relocate_section; | |
11096 | ||
11097 | /* If this is a dynamic object, we don't want to do anything here: | |
11098 | we don't want the local symbols, and we don't want the section | |
11099 | contents. */ | |
11100 | if ((input_bfd->flags & DYNAMIC) != 0) | |
0a1b45a2 | 11101 | return true; |
c152c796 | 11102 | |
c152c796 AM |
11103 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
11104 | if (elf_bad_symtab (input_bfd)) | |
11105 | { | |
11106 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
11107 | extsymoff = 0; | |
11108 | } | |
11109 | else | |
11110 | { | |
11111 | locsymcount = symtab_hdr->sh_info; | |
11112 | extsymoff = symtab_hdr->sh_info; | |
11113 | } | |
11114 | ||
99fabbc9 JL |
11115 | /* Enable GNU OSABI features in the output BFD that are used in the input |
11116 | BFD. */ | |
11117 | if (bed->elf_osabi == ELFOSABI_NONE | |
11118 | || bed->elf_osabi == ELFOSABI_GNU | |
11119 | || bed->elf_osabi == ELFOSABI_FREEBSD) | |
11120 | elf_tdata (output_bfd)->has_gnu_osabi | |
04f89674 L |
11121 | |= (elf_tdata (input_bfd)->has_gnu_osabi |
11122 | & (bfd_link_relocatable (flinfo->info) | |
11123 | ? -1 : ~elf_gnu_osabi_retain)); | |
99fabbc9 | 11124 | |
c152c796 AM |
11125 | /* Read the local symbols. */ |
11126 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
11127 | if (isymbuf == NULL && locsymcount != 0) | |
11128 | { | |
11129 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
8b127cbc AM |
11130 | flinfo->internal_syms, |
11131 | flinfo->external_syms, | |
11132 | flinfo->locsym_shndx); | |
c152c796 | 11133 | if (isymbuf == NULL) |
0a1b45a2 | 11134 | return false; |
c152c796 AM |
11135 | } |
11136 | ||
11137 | /* Find local symbol sections and adjust values of symbols in | |
11138 | SEC_MERGE sections. Write out those local symbols we know are | |
11139 | going into the output file. */ | |
36f61bf2 | 11140 | isymend = PTR_ADD (isymbuf, locsymcount); |
8b127cbc | 11141 | for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; |
c152c796 AM |
11142 | isym < isymend; |
11143 | isym++, pindex++, ppsection++) | |
11144 | { | |
11145 | asection *isec; | |
11146 | const char *name; | |
11147 | Elf_Internal_Sym osym; | |
6e0b88f1 AM |
11148 | long indx; |
11149 | int ret; | |
c152c796 AM |
11150 | |
11151 | *pindex = -1; | |
11152 | ||
11153 | if (elf_bad_symtab (input_bfd)) | |
11154 | { | |
11155 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
11156 | { | |
11157 | *ppsection = NULL; | |
11158 | continue; | |
11159 | } | |
11160 | } | |
11161 | ||
11162 | if (isym->st_shndx == SHN_UNDEF) | |
11163 | isec = bfd_und_section_ptr; | |
c152c796 AM |
11164 | else if (isym->st_shndx == SHN_ABS) |
11165 | isec = bfd_abs_section_ptr; | |
11166 | else if (isym->st_shndx == SHN_COMMON) | |
11167 | isec = bfd_com_section_ptr; | |
11168 | else | |
11169 | { | |
cb33740c AM |
11170 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
11171 | if (isec == NULL) | |
11172 | { | |
11173 | /* Don't attempt to output symbols with st_shnx in the | |
11174 | reserved range other than SHN_ABS and SHN_COMMON. */ | |
6835821b | 11175 | isec = bfd_und_section_ptr; |
cb33740c | 11176 | } |
dbaa2011 | 11177 | else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE |
cb33740c AM |
11178 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) |
11179 | isym->st_value = | |
11180 | _bfd_merged_section_offset (output_bfd, &isec, | |
11181 | elf_section_data (isec)->sec_info, | |
11182 | isym->st_value); | |
c152c796 AM |
11183 | } |
11184 | ||
11185 | *ppsection = isec; | |
11186 | ||
d983c8c5 AM |
11187 | /* Don't output the first, undefined, symbol. In fact, don't |
11188 | output any undefined local symbol. */ | |
11189 | if (isec == bfd_und_section_ptr) | |
c152c796 AM |
11190 | continue; |
11191 | ||
11192 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
11193 | { | |
11194 | /* We never output section symbols. Instead, we use the | |
11195 | section symbol of the corresponding section in the output | |
11196 | file. */ | |
11197 | continue; | |
11198 | } | |
11199 | ||
11200 | /* If we are stripping all symbols, we don't want to output this | |
11201 | one. */ | |
8b127cbc | 11202 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
11203 | continue; |
11204 | ||
11205 | /* If we are discarding all local symbols, we don't want to | |
11206 | output this one. If we are generating a relocatable output | |
11207 | file, then some of the local symbols may be required by | |
11208 | relocs; we output them below as we discover that they are | |
11209 | needed. */ | |
8b127cbc | 11210 | if (flinfo->info->discard == discard_all) |
c152c796 AM |
11211 | continue; |
11212 | ||
11213 | /* If this symbol is defined in a section which we are | |
f02571c5 | 11214 | discarding, we don't need to keep it. */ |
c63d4862 AM |
11215 | if (isym->st_shndx < SHN_LORESERVE |
11216 | && (isec->output_section == NULL | |
11217 | || bfd_section_removed_from_list (output_bfd, | |
11218 | isec->output_section))) | |
e75a280b L |
11219 | continue; |
11220 | ||
c152c796 AM |
11221 | /* Get the name of the symbol. */ |
11222 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
11223 | isym->st_name); | |
11224 | if (name == NULL) | |
0a1b45a2 | 11225 | return false; |
c152c796 AM |
11226 | |
11227 | /* See if we are discarding symbols with this name. */ | |
8b127cbc | 11228 | if ((flinfo->info->strip == strip_some |
0a1b45a2 | 11229 | && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false) |
c152c796 | 11230 | == NULL)) |
8b127cbc | 11231 | || (((flinfo->info->discard == discard_sec_merge |
0e1862bb L |
11232 | && (isec->flags & SEC_MERGE) |
11233 | && !bfd_link_relocatable (flinfo->info)) | |
8b127cbc | 11234 | || flinfo->info->discard == discard_l) |
c152c796 AM |
11235 | && bfd_is_local_label_name (input_bfd, name))) |
11236 | continue; | |
11237 | ||
ffbc01cc AM |
11238 | if (ELF_ST_TYPE (isym->st_info) == STT_FILE) |
11239 | { | |
ce875075 AM |
11240 | if (input_bfd->lto_output) |
11241 | /* -flto puts a temp file name here. This means builds | |
11242 | are not reproducible. Discard the symbol. */ | |
11243 | continue; | |
0a1b45a2 | 11244 | have_file_sym = true; |
ffbc01cc AM |
11245 | flinfo->filesym_count += 1; |
11246 | } | |
11247 | if (!have_file_sym) | |
11248 | { | |
11249 | /* In the absence of debug info, bfd_find_nearest_line uses | |
11250 | FILE symbols to determine the source file for local | |
11251 | function symbols. Provide a FILE symbol here if input | |
11252 | files lack such, so that their symbols won't be | |
11253 | associated with a previous input file. It's not the | |
11254 | source file, but the best we can do. */ | |
5b4293ba | 11255 | const char *filename; |
0a1b45a2 | 11256 | have_file_sym = true; |
ffbc01cc AM |
11257 | flinfo->filesym_count += 1; |
11258 | memset (&osym, 0, sizeof (osym)); | |
11259 | osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
11260 | osym.st_shndx = SHN_ABS; | |
5b4293ba AM |
11261 | if (input_bfd->lto_output) |
11262 | filename = NULL; | |
11263 | else | |
11264 | filename = lbasename (bfd_get_filename (input_bfd)); | |
11265 | if (!elf_link_output_symstrtab (flinfo, filename, &osym, | |
11266 | bfd_abs_section_ptr, NULL)) | |
0a1b45a2 | 11267 | return false; |
ffbc01cc AM |
11268 | } |
11269 | ||
c152c796 AM |
11270 | osym = *isym; |
11271 | ||
11272 | /* Adjust the section index for the output file. */ | |
11273 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
11274 | isec->output_section); | |
11275 | if (osym.st_shndx == SHN_BAD) | |
0a1b45a2 | 11276 | return false; |
c152c796 | 11277 | |
c152c796 AM |
11278 | /* ELF symbols in relocatable files are section relative, but |
11279 | in executable files they are virtual addresses. Note that | |
11280 | this code assumes that all ELF sections have an associated | |
11281 | BFD section with a reasonable value for output_offset; below | |
11282 | we assume that they also have a reasonable value for | |
11283 | output_section. Any special sections must be set up to meet | |
11284 | these requirements. */ | |
11285 | osym.st_value += isec->output_offset; | |
0e1862bb | 11286 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11287 | { |
11288 | osym.st_value += isec->output_section->vma; | |
11289 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
11290 | { | |
11291 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
102def4d AM |
11292 | if (elf_hash_table (flinfo->info)->tls_sec != NULL) |
11293 | osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; | |
11294 | else | |
11295 | osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info), | |
11296 | STT_NOTYPE); | |
c152c796 AM |
11297 | } |
11298 | } | |
11299 | ||
6e0b88f1 | 11300 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac | 11301 | ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); |
6e0b88f1 | 11302 | if (ret == 0) |
0a1b45a2 | 11303 | return false; |
6e0b88f1 AM |
11304 | else if (ret == 1) |
11305 | *pindex = indx; | |
c152c796 AM |
11306 | } |
11307 | ||
310fd250 L |
11308 | if (bed->s->arch_size == 32) |
11309 | { | |
11310 | r_type_mask = 0xff; | |
11311 | r_sym_shift = 8; | |
11312 | address_size = 4; | |
11313 | } | |
11314 | else | |
11315 | { | |
11316 | r_type_mask = 0xffffffff; | |
11317 | r_sym_shift = 32; | |
11318 | address_size = 8; | |
11319 | } | |
11320 | ||
c152c796 AM |
11321 | /* Relocate the contents of each section. */ |
11322 | sym_hashes = elf_sym_hashes (input_bfd); | |
11323 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
11324 | { | |
11325 | bfd_byte *contents; | |
11326 | ||
11327 | if (! o->linker_mark) | |
11328 | { | |
11329 | /* This section was omitted from the link. */ | |
11330 | continue; | |
11331 | } | |
11332 | ||
7bdf4127 | 11333 | if (!flinfo->info->resolve_section_groups |
bcacc0f5 AM |
11334 | && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) |
11335 | { | |
11336 | /* Deal with the group signature symbol. */ | |
11337 | struct bfd_elf_section_data *sec_data = elf_section_data (o); | |
11338 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
11339 | asection *osec = o->output_section; | |
11340 | ||
7bdf4127 | 11341 | BFD_ASSERT (bfd_link_relocatable (flinfo->info)); |
bcacc0f5 AM |
11342 | if (symndx >= locsymcount |
11343 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 11344 | && flinfo->sections[symndx] == NULL)) |
bcacc0f5 AM |
11345 | { |
11346 | struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; | |
11347 | while (h->root.type == bfd_link_hash_indirect | |
11348 | || h->root.type == bfd_link_hash_warning) | |
11349 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
11350 | /* Arrange for symbol to be output. */ | |
11351 | h->indx = -2; | |
11352 | elf_section_data (osec)->this_hdr.sh_info = -2; | |
11353 | } | |
11354 | else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) | |
11355 | { | |
11356 | /* We'll use the output section target_index. */ | |
8b127cbc | 11357 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 AM |
11358 | elf_section_data (osec)->this_hdr.sh_info = sec->target_index; |
11359 | } | |
11360 | else | |
11361 | { | |
8b127cbc | 11362 | if (flinfo->indices[symndx] == -1) |
bcacc0f5 AM |
11363 | { |
11364 | /* Otherwise output the local symbol now. */ | |
11365 | Elf_Internal_Sym sym = isymbuf[symndx]; | |
8b127cbc | 11366 | asection *sec = flinfo->sections[symndx]->output_section; |
bcacc0f5 | 11367 | const char *name; |
6e0b88f1 AM |
11368 | long indx; |
11369 | int ret; | |
bcacc0f5 AM |
11370 | |
11371 | name = bfd_elf_string_from_elf_section (input_bfd, | |
11372 | symtab_hdr->sh_link, | |
11373 | sym.st_name); | |
11374 | if (name == NULL) | |
0a1b45a2 | 11375 | return false; |
bcacc0f5 AM |
11376 | |
11377 | sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
11378 | sec); | |
11379 | if (sym.st_shndx == SHN_BAD) | |
0a1b45a2 | 11380 | return false; |
bcacc0f5 AM |
11381 | |
11382 | sym.st_value += o->output_offset; | |
11383 | ||
6e0b88f1 | 11384 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
11385 | ret = elf_link_output_symstrtab (flinfo, name, &sym, o, |
11386 | NULL); | |
6e0b88f1 | 11387 | if (ret == 0) |
0a1b45a2 | 11388 | return false; |
6e0b88f1 | 11389 | else if (ret == 1) |
8b127cbc | 11390 | flinfo->indices[symndx] = indx; |
6e0b88f1 AM |
11391 | else |
11392 | abort (); | |
bcacc0f5 AM |
11393 | } |
11394 | elf_section_data (osec)->this_hdr.sh_info | |
8b127cbc | 11395 | = flinfo->indices[symndx]; |
bcacc0f5 AM |
11396 | } |
11397 | } | |
11398 | ||
c152c796 | 11399 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
eea6121a | 11400 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
11401 | continue; |
11402 | ||
11403 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
11404 | { | |
11405 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
11406 | or somesuch. */ | |
11407 | continue; | |
11408 | } | |
11409 | ||
11410 | /* Get the contents of the section. They have been cached by a | |
11411 | relaxation routine. Note that o is a section in an input | |
11412 | file, so the contents field will not have been set by any of | |
11413 | the routines which work on output files. */ | |
11414 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
53291d1f AM |
11415 | { |
11416 | contents = elf_section_data (o)->this_hdr.contents; | |
11417 | if (bed->caches_rawsize | |
11418 | && o->rawsize != 0 | |
11419 | && o->rawsize < o->size) | |
11420 | { | |
11421 | memcpy (flinfo->contents, contents, o->rawsize); | |
11422 | contents = flinfo->contents; | |
11423 | } | |
11424 | } | |
1a528d3e MM |
11425 | else if (!(o->flags & SEC_RELOC) |
11426 | && !bed->elf_backend_write_section | |
11427 | && o->sec_info_type == SEC_INFO_TYPE_MERGE) | |
11428 | /* A MERGE section that has no relocations doesn't need the | |
11429 | contents anymore, they have been recorded earlier. Except | |
11430 | if the backend has special provisions for writing sections. */ | |
11431 | contents = NULL; | |
c152c796 AM |
11432 | else |
11433 | { | |
8b127cbc | 11434 | contents = flinfo->contents; |
4a114e3e | 11435 | if (! bfd_get_full_section_contents (input_bfd, o, &contents)) |
0a1b45a2 | 11436 | return false; |
c152c796 AM |
11437 | } |
11438 | ||
11439 | if ((o->flags & SEC_RELOC) != 0) | |
11440 | { | |
11441 | Elf_Internal_Rela *internal_relocs; | |
0f02bbd9 | 11442 | Elf_Internal_Rela *rel, *relend; |
0f02bbd9 | 11443 | int action_discarded; |
ece5ef60 | 11444 | int ret; |
c152c796 AM |
11445 | |
11446 | /* Get the swapped relocs. */ | |
11447 | internal_relocs | |
a8dde0a2 L |
11448 | = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o, |
11449 | flinfo->external_relocs, | |
11450 | flinfo->internal_relocs, | |
11451 | false); | |
c152c796 AM |
11452 | if (internal_relocs == NULL |
11453 | && o->reloc_count > 0) | |
0a1b45a2 | 11454 | return false; |
c152c796 | 11455 | |
0f02bbd9 | 11456 | action_discarded = -1; |
c152c796 | 11457 | if (!elf_section_ignore_discarded_relocs (o)) |
0f02bbd9 AM |
11458 | action_discarded = (*bed->action_discarded) (o); |
11459 | ||
11460 | /* Run through the relocs evaluating complex reloc symbols and | |
11461 | looking for relocs against symbols from discarded sections | |
11462 | or section symbols from removed link-once sections. | |
11463 | Complain about relocs against discarded sections. Zero | |
11464 | relocs against removed link-once sections. */ | |
11465 | ||
11466 | rel = internal_relocs; | |
056bafd4 | 11467 | relend = rel + o->reloc_count; |
0f02bbd9 | 11468 | for ( ; rel < relend; rel++) |
c152c796 | 11469 | { |
0f02bbd9 AM |
11470 | unsigned long r_symndx = rel->r_info >> r_sym_shift; |
11471 | unsigned int s_type; | |
11472 | asection **ps, *sec; | |
11473 | struct elf_link_hash_entry *h = NULL; | |
11474 | const char *sym_name; | |
c152c796 | 11475 | |
0f02bbd9 AM |
11476 | if (r_symndx == STN_UNDEF) |
11477 | continue; | |
c152c796 | 11478 | |
0f02bbd9 AM |
11479 | if (r_symndx >= locsymcount |
11480 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 11481 | && flinfo->sections[r_symndx] == NULL)) |
0f02bbd9 AM |
11482 | { |
11483 | h = sym_hashes[r_symndx - extsymoff]; | |
ee75fd95 | 11484 | |
0f02bbd9 AM |
11485 | /* Badly formatted input files can contain relocs that |
11486 | reference non-existant symbols. Check here so that | |
11487 | we do not seg fault. */ | |
11488 | if (h == NULL) | |
c152c796 | 11489 | { |
4eca0228 | 11490 | _bfd_error_handler |
695344c0 | 11491 | /* xgettext:c-format */ |
2dcf00ce | 11492 | (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA " |
0f02bbd9 | 11493 | "that references a non-existent global symbol"), |
2dcf00ce | 11494 | input_bfd, (uint64_t) rel->r_info, o); |
0f02bbd9 | 11495 | bfd_set_error (bfd_error_bad_value); |
0a1b45a2 | 11496 | return false; |
0f02bbd9 | 11497 | } |
3b36f7e6 | 11498 | |
0f02bbd9 AM |
11499 | while (h->root.type == bfd_link_hash_indirect |
11500 | || h->root.type == bfd_link_hash_warning) | |
11501 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 | 11502 | |
0f02bbd9 | 11503 | s_type = h->type; |
cdd3575c | 11504 | |
9e2dec47 | 11505 | /* If a plugin symbol is referenced from a non-IR file, |
ca4be51c AM |
11506 | mark the symbol as undefined. Note that the |
11507 | linker may attach linker created dynamic sections | |
11508 | to the plugin bfd. Symbols defined in linker | |
11509 | created sections are not plugin symbols. */ | |
bc4e12de | 11510 | if ((h->root.non_ir_ref_regular |
4070765b | 11511 | || h->root.non_ir_ref_dynamic) |
9e2dec47 L |
11512 | && (h->root.type == bfd_link_hash_defined |
11513 | || h->root.type == bfd_link_hash_defweak) | |
11514 | && (h->root.u.def.section->flags | |
11515 | & SEC_LINKER_CREATED) == 0 | |
11516 | && h->root.u.def.section->owner != NULL | |
11517 | && (h->root.u.def.section->owner->flags | |
11518 | & BFD_PLUGIN) != 0) | |
11519 | { | |
11520 | h->root.type = bfd_link_hash_undefined; | |
11521 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
11522 | } | |
11523 | ||
0f02bbd9 AM |
11524 | ps = NULL; |
11525 | if (h->root.type == bfd_link_hash_defined | |
11526 | || h->root.type == bfd_link_hash_defweak) | |
11527 | ps = &h->root.u.def.section; | |
11528 | ||
11529 | sym_name = h->root.root.string; | |
11530 | } | |
11531 | else | |
11532 | { | |
11533 | Elf_Internal_Sym *sym = isymbuf + r_symndx; | |
11534 | ||
11535 | s_type = ELF_ST_TYPE (sym->st_info); | |
8b127cbc | 11536 | ps = &flinfo->sections[r_symndx]; |
0f02bbd9 AM |
11537 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, |
11538 | sym, *ps); | |
11539 | } | |
c152c796 | 11540 | |
c301e700 | 11541 | if ((s_type == STT_RELC || s_type == STT_SRELC) |
0e1862bb | 11542 | && !bfd_link_relocatable (flinfo->info)) |
0f02bbd9 AM |
11543 | { |
11544 | bfd_vma val; | |
11545 | bfd_vma dot = (rel->r_offset | |
11546 | + o->output_offset + o->output_section->vma); | |
11547 | #ifdef DEBUG | |
11548 | printf ("Encountered a complex symbol!"); | |
11549 | printf (" (input_bfd %s, section %s, reloc %ld\n", | |
765cf5f6 | 11550 | bfd_get_filename (input_bfd), o->name, |
9ccb8af9 | 11551 | (long) (rel - internal_relocs)); |
0f02bbd9 AM |
11552 | printf (" symbol: idx %8.8lx, name %s\n", |
11553 | r_symndx, sym_name); | |
11554 | printf (" reloc : info %8.8lx, addr %8.8lx\n", | |
11555 | (unsigned long) rel->r_info, | |
11556 | (unsigned long) rel->r_offset); | |
11557 | #endif | |
8b127cbc | 11558 | if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, |
0f02bbd9 | 11559 | isymbuf, locsymcount, s_type == STT_SRELC)) |
0a1b45a2 | 11560 | return false; |
0f02bbd9 AM |
11561 | |
11562 | /* Symbol evaluated OK. Update to absolute value. */ | |
11563 | set_symbol_value (input_bfd, isymbuf, locsymcount, | |
11564 | r_symndx, val); | |
11565 | continue; | |
11566 | } | |
11567 | ||
11568 | if (action_discarded != -1 && ps != NULL) | |
11569 | { | |
cdd3575c AM |
11570 | /* Complain if the definition comes from a |
11571 | discarded section. */ | |
dbaa2011 | 11572 | if ((sec = *ps) != NULL && discarded_section (sec)) |
cdd3575c | 11573 | { |
cf35638d | 11574 | BFD_ASSERT (r_symndx != STN_UNDEF); |
0f02bbd9 | 11575 | if (action_discarded & COMPLAIN) |
8b127cbc | 11576 | (*flinfo->info->callbacks->einfo) |
695344c0 | 11577 | /* xgettext:c-format */ |
871b3ab2 AM |
11578 | (_("%X`%s' referenced in section `%pA' of %pB: " |
11579 | "defined in discarded section `%pA' of %pB\n"), | |
e1fffbe6 | 11580 | sym_name, o, input_bfd, sec, sec->owner); |
cdd3575c | 11581 | |
87e5235d | 11582 | /* Try to do the best we can to support buggy old |
e0ae6d6f | 11583 | versions of gcc. Pretend that the symbol is |
87e5235d AM |
11584 | really defined in the kept linkonce section. |
11585 | FIXME: This is quite broken. Modifying the | |
11586 | symbol here means we will be changing all later | |
e0ae6d6f | 11587 | uses of the symbol, not just in this section. */ |
0f02bbd9 | 11588 | if (action_discarded & PRETEND) |
87e5235d | 11589 | { |
01b3c8ab L |
11590 | asection *kept; |
11591 | ||
c0f00686 | 11592 | kept = _bfd_elf_check_kept_section (sec, |
8b127cbc | 11593 | flinfo->info); |
01b3c8ab | 11594 | if (kept != NULL) |
87e5235d AM |
11595 | { |
11596 | *ps = kept; | |
11597 | continue; | |
11598 | } | |
11599 | } | |
c152c796 AM |
11600 | } |
11601 | } | |
11602 | } | |
11603 | ||
11604 | /* Relocate the section by invoking a back end routine. | |
11605 | ||
11606 | The back end routine is responsible for adjusting the | |
11607 | section contents as necessary, and (if using Rela relocs | |
11608 | and generating a relocatable output file) adjusting the | |
11609 | reloc addend as necessary. | |
11610 | ||
11611 | The back end routine does not have to worry about setting | |
11612 | the reloc address or the reloc symbol index. | |
11613 | ||
11614 | The back end routine is given a pointer to the swapped in | |
11615 | internal symbols, and can access the hash table entries | |
11616 | for the external symbols via elf_sym_hashes (input_bfd). | |
11617 | ||
11618 | When generating relocatable output, the back end routine | |
11619 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
11620 | output symbol is going to be a section symbol | |
11621 | corresponding to the output section, which will require | |
11622 | the addend to be adjusted. */ | |
11623 | ||
8b127cbc | 11624 | ret = (*relocate_section) (output_bfd, flinfo->info, |
c152c796 AM |
11625 | input_bfd, o, contents, |
11626 | internal_relocs, | |
11627 | isymbuf, | |
8b127cbc | 11628 | flinfo->sections); |
ece5ef60 | 11629 | if (!ret) |
0a1b45a2 | 11630 | return false; |
c152c796 | 11631 | |
ece5ef60 | 11632 | if (ret == 2 |
0e1862bb | 11633 | || bfd_link_relocatable (flinfo->info) |
8b127cbc | 11634 | || flinfo->info->emitrelocations) |
c152c796 AM |
11635 | { |
11636 | Elf_Internal_Rela *irela; | |
d4730f92 | 11637 | Elf_Internal_Rela *irelaend, *irelamid; |
c152c796 AM |
11638 | bfd_vma last_offset; |
11639 | struct elf_link_hash_entry **rel_hash; | |
d4730f92 BS |
11640 | struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; |
11641 | Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; | |
c152c796 | 11642 | unsigned int next_erel; |
0a1b45a2 | 11643 | bool rela_normal; |
d4730f92 | 11644 | struct bfd_elf_section_data *esdi, *esdo; |
c152c796 | 11645 | |
d4730f92 BS |
11646 | esdi = elf_section_data (o); |
11647 | esdo = elf_section_data (o->output_section); | |
0a1b45a2 | 11648 | rela_normal = false; |
c152c796 AM |
11649 | |
11650 | /* Adjust the reloc addresses and symbol indices. */ | |
11651 | ||
11652 | irela = internal_relocs; | |
056bafd4 | 11653 | irelaend = irela + o->reloc_count; |
36f61bf2 | 11654 | rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count); |
d4730f92 BS |
11655 | /* We start processing the REL relocs, if any. When we reach |
11656 | IRELAMID in the loop, we switch to the RELA relocs. */ | |
11657 | irelamid = irela; | |
11658 | if (esdi->rel.hdr != NULL) | |
11659 | irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) | |
11660 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 11661 | rel_hash_list = rel_hash; |
d4730f92 | 11662 | rela_hash_list = NULL; |
c152c796 | 11663 | last_offset = o->output_offset; |
0e1862bb | 11664 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11665 | last_offset += o->output_section->vma; |
11666 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
11667 | { | |
11668 | unsigned long r_symndx; | |
11669 | asection *sec; | |
11670 | Elf_Internal_Sym sym; | |
11671 | ||
11672 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
11673 | { | |
11674 | rel_hash++; | |
11675 | next_erel = 0; | |
11676 | } | |
11677 | ||
d4730f92 BS |
11678 | if (irela == irelamid) |
11679 | { | |
36f61bf2 | 11680 | rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count); |
d4730f92 BS |
11681 | rela_hash_list = rel_hash; |
11682 | rela_normal = bed->rela_normal; | |
11683 | } | |
11684 | ||
c152c796 | 11685 | irela->r_offset = _bfd_elf_section_offset (output_bfd, |
8b127cbc | 11686 | flinfo->info, o, |
c152c796 AM |
11687 | irela->r_offset); |
11688 | if (irela->r_offset >= (bfd_vma) -2) | |
11689 | { | |
11690 | /* This is a reloc for a deleted entry or somesuch. | |
11691 | Turn it into an R_*_NONE reloc, at the same | |
11692 | offset as the last reloc. elf_eh_frame.c and | |
e460dd0d | 11693 | bfd_elf_discard_info rely on reloc offsets |
c152c796 AM |
11694 | being ordered. */ |
11695 | irela->r_offset = last_offset; | |
11696 | irela->r_info = 0; | |
11697 | irela->r_addend = 0; | |
11698 | continue; | |
11699 | } | |
11700 | ||
11701 | irela->r_offset += o->output_offset; | |
11702 | ||
11703 | /* Relocs in an executable have to be virtual addresses. */ | |
0e1862bb | 11704 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11705 | irela->r_offset += o->output_section->vma; |
11706 | ||
11707 | last_offset = irela->r_offset; | |
11708 | ||
11709 | r_symndx = irela->r_info >> r_sym_shift; | |
11710 | if (r_symndx == STN_UNDEF) | |
11711 | continue; | |
11712 | ||
11713 | if (r_symndx >= locsymcount | |
11714 | || (elf_bad_symtab (input_bfd) | |
8b127cbc | 11715 | && flinfo->sections[r_symndx] == NULL)) |
c152c796 AM |
11716 | { |
11717 | struct elf_link_hash_entry *rh; | |
11718 | unsigned long indx; | |
11719 | ||
11720 | /* This is a reloc against a global symbol. We | |
11721 | have not yet output all the local symbols, so | |
11722 | we do not know the symbol index of any global | |
11723 | symbol. We set the rel_hash entry for this | |
11724 | reloc to point to the global hash table entry | |
11725 | for this symbol. The symbol index is then | |
ee75fd95 | 11726 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
11727 | indx = r_symndx - extsymoff; |
11728 | rh = elf_sym_hashes (input_bfd)[indx]; | |
11729 | while (rh->root.type == bfd_link_hash_indirect | |
11730 | || rh->root.type == bfd_link_hash_warning) | |
11731 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
11732 | ||
11733 | /* Setting the index to -2 tells | |
11734 | elf_link_output_extsym that this symbol is | |
11735 | used by a reloc. */ | |
11736 | BFD_ASSERT (rh->indx < 0); | |
11737 | rh->indx = -2; | |
c152c796 AM |
11738 | *rel_hash = rh; |
11739 | ||
11740 | continue; | |
11741 | } | |
11742 | ||
11743 | /* This is a reloc against a local symbol. */ | |
11744 | ||
11745 | *rel_hash = NULL; | |
11746 | sym = isymbuf[r_symndx]; | |
8b127cbc | 11747 | sec = flinfo->sections[r_symndx]; |
c152c796 AM |
11748 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) |
11749 | { | |
11750 | /* I suppose the backend ought to fill in the | |
11751 | section of any STT_SECTION symbol against a | |
6a8d1586 | 11752 | processor specific section. */ |
cf35638d | 11753 | r_symndx = STN_UNDEF; |
6a8d1586 AM |
11754 | if (bfd_is_abs_section (sec)) |
11755 | ; | |
c152c796 AM |
11756 | else if (sec == NULL || sec->owner == NULL) |
11757 | { | |
11758 | bfd_set_error (bfd_error_bad_value); | |
0a1b45a2 | 11759 | return false; |
c152c796 AM |
11760 | } |
11761 | else | |
11762 | { | |
6a8d1586 AM |
11763 | asection *osec = sec->output_section; |
11764 | ||
11765 | /* If we have discarded a section, the output | |
11766 | section will be the absolute section. In | |
ab96bf03 AM |
11767 | case of discarded SEC_MERGE sections, use |
11768 | the kept section. relocate_section should | |
11769 | have already handled discarded linkonce | |
11770 | sections. */ | |
6a8d1586 AM |
11771 | if (bfd_is_abs_section (osec) |
11772 | && sec->kept_section != NULL | |
11773 | && sec->kept_section->output_section != NULL) | |
11774 | { | |
11775 | osec = sec->kept_section->output_section; | |
11776 | irela->r_addend -= osec->vma; | |
11777 | } | |
11778 | ||
11779 | if (!bfd_is_abs_section (osec)) | |
11780 | { | |
11781 | r_symndx = osec->target_index; | |
cf35638d | 11782 | if (r_symndx == STN_UNDEF) |
74541ad4 | 11783 | { |
051d833a AM |
11784 | irela->r_addend += osec->vma; |
11785 | osec = _bfd_nearby_section (output_bfd, osec, | |
11786 | osec->vma); | |
11787 | irela->r_addend -= osec->vma; | |
11788 | r_symndx = osec->target_index; | |
74541ad4 | 11789 | } |
6a8d1586 | 11790 | } |
c152c796 AM |
11791 | } |
11792 | ||
11793 | /* Adjust the addend according to where the | |
11794 | section winds up in the output section. */ | |
11795 | if (rela_normal) | |
11796 | irela->r_addend += sec->output_offset; | |
11797 | } | |
11798 | else | |
11799 | { | |
8b127cbc | 11800 | if (flinfo->indices[r_symndx] == -1) |
c152c796 AM |
11801 | { |
11802 | unsigned long shlink; | |
11803 | const char *name; | |
11804 | asection *osec; | |
6e0b88f1 | 11805 | long indx; |
c152c796 | 11806 | |
8b127cbc | 11807 | if (flinfo->info->strip == strip_all) |
c152c796 AM |
11808 | { |
11809 | /* You can't do ld -r -s. */ | |
11810 | bfd_set_error (bfd_error_invalid_operation); | |
0a1b45a2 | 11811 | return false; |
c152c796 AM |
11812 | } |
11813 | ||
11814 | /* This symbol was skipped earlier, but | |
11815 | since it is needed by a reloc, we | |
11816 | must output it now. */ | |
11817 | shlink = symtab_hdr->sh_link; | |
11818 | name = (bfd_elf_string_from_elf_section | |
11819 | (input_bfd, shlink, sym.st_name)); | |
11820 | if (name == NULL) | |
0a1b45a2 | 11821 | return false; |
c152c796 AM |
11822 | |
11823 | osec = sec->output_section; | |
11824 | sym.st_shndx = | |
11825 | _bfd_elf_section_from_bfd_section (output_bfd, | |
11826 | osec); | |
11827 | if (sym.st_shndx == SHN_BAD) | |
0a1b45a2 | 11828 | return false; |
c152c796 AM |
11829 | |
11830 | sym.st_value += sec->output_offset; | |
0e1862bb | 11831 | if (!bfd_link_relocatable (flinfo->info)) |
c152c796 AM |
11832 | { |
11833 | sym.st_value += osec->vma; | |
11834 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
11835 | { | |
102def4d AM |
11836 | struct elf_link_hash_table *htab |
11837 | = elf_hash_table (flinfo->info); | |
11838 | ||
c152c796 AM |
11839 | /* STT_TLS symbols are relative to PT_TLS |
11840 | segment base. */ | |
102def4d AM |
11841 | if (htab->tls_sec != NULL) |
11842 | sym.st_value -= htab->tls_sec->vma; | |
11843 | else | |
11844 | sym.st_info | |
11845 | = ELF_ST_INFO (ELF_ST_BIND (sym.st_info), | |
11846 | STT_NOTYPE); | |
c152c796 AM |
11847 | } |
11848 | } | |
11849 | ||
6e0b88f1 | 11850 | indx = bfd_get_symcount (output_bfd); |
ef10c3ac L |
11851 | ret = elf_link_output_symstrtab (flinfo, name, |
11852 | &sym, sec, | |
11853 | NULL); | |
6e0b88f1 | 11854 | if (ret == 0) |
0a1b45a2 | 11855 | return false; |
6e0b88f1 | 11856 | else if (ret == 1) |
8b127cbc | 11857 | flinfo->indices[r_symndx] = indx; |
6e0b88f1 AM |
11858 | else |
11859 | abort (); | |
c152c796 AM |
11860 | } |
11861 | ||
8b127cbc | 11862 | r_symndx = flinfo->indices[r_symndx]; |
c152c796 AM |
11863 | } |
11864 | ||
11865 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
11866 | | (irela->r_info & r_type_mask)); | |
11867 | } | |
11868 | ||
11869 | /* Swap out the relocs. */ | |
d4730f92 BS |
11870 | input_rel_hdr = esdi->rel.hdr; |
11871 | if (input_rel_hdr && input_rel_hdr->sh_size != 0) | |
c152c796 | 11872 | { |
d4730f92 BS |
11873 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
11874 | input_rel_hdr, | |
11875 | internal_relocs, | |
11876 | rel_hash_list)) | |
0a1b45a2 | 11877 | return false; |
c152c796 AM |
11878 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) |
11879 | * bed->s->int_rels_per_ext_rel); | |
eac338cf | 11880 | rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); |
d4730f92 BS |
11881 | } |
11882 | ||
11883 | input_rela_hdr = esdi->rela.hdr; | |
11884 | if (input_rela_hdr && input_rela_hdr->sh_size != 0) | |
11885 | { | |
eac338cf | 11886 | if (!bed->elf_backend_emit_relocs (output_bfd, o, |
d4730f92 | 11887 | input_rela_hdr, |
eac338cf | 11888 | internal_relocs, |
d4730f92 | 11889 | rela_hash_list)) |
0a1b45a2 | 11890 | return false; |
c152c796 AM |
11891 | } |
11892 | } | |
11893 | } | |
11894 | ||
11895 | /* Write out the modified section contents. */ | |
11896 | if (bed->elf_backend_write_section | |
8b127cbc | 11897 | && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, |
c7b8f16e | 11898 | contents)) |
c152c796 AM |
11899 | { |
11900 | /* Section written out. */ | |
11901 | } | |
11902 | else switch (o->sec_info_type) | |
11903 | { | |
dbaa2011 | 11904 | case SEC_INFO_TYPE_STABS: |
c152c796 AM |
11905 | if (! (_bfd_write_section_stabs |
11906 | (output_bfd, | |
8b127cbc | 11907 | &elf_hash_table (flinfo->info)->stab_info, |
c152c796 | 11908 | o, &elf_section_data (o)->sec_info, contents))) |
0a1b45a2 | 11909 | return false; |
c152c796 | 11910 | break; |
dbaa2011 | 11911 | case SEC_INFO_TYPE_MERGE: |
c152c796 AM |
11912 | if (! _bfd_write_merged_section (output_bfd, o, |
11913 | elf_section_data (o)->sec_info)) | |
0a1b45a2 | 11914 | return false; |
c152c796 | 11915 | break; |
dbaa2011 | 11916 | case SEC_INFO_TYPE_EH_FRAME: |
c152c796 | 11917 | { |
8b127cbc | 11918 | if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, |
c152c796 | 11919 | o, contents)) |
0a1b45a2 | 11920 | return false; |
c152c796 AM |
11921 | } |
11922 | break; | |
2f0c68f2 CM |
11923 | case SEC_INFO_TYPE_EH_FRAME_ENTRY: |
11924 | { | |
11925 | if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, | |
11926 | flinfo->info, | |
11927 | o, contents)) | |
0a1b45a2 | 11928 | return false; |
2f0c68f2 CM |
11929 | } |
11930 | break; | |
cf0e0a0b IB |
11931 | case SEC_INFO_TYPE_SFRAME: |
11932 | { | |
11933 | /* Merge .sframe sections into the ctf frame encoder | |
11934 | context of the output_bfd's section. The final .sframe | |
11935 | output section will be written out later. */ | |
11936 | if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info, | |
11937 | o, contents)) | |
11938 | return false; | |
11939 | } | |
11940 | break; | |
c152c796 AM |
11941 | default: |
11942 | { | |
310fd250 L |
11943 | if (! (o->flags & SEC_EXCLUDE)) |
11944 | { | |
11945 | file_ptr offset = (file_ptr) o->output_offset; | |
11946 | bfd_size_type todo = o->size; | |
37b01f6a | 11947 | |
bb294208 | 11948 | offset *= bfd_octets_per_byte (output_bfd, o); |
37b01f6a | 11949 | |
b02db378 AM |
11950 | if ((o->flags & SEC_ELF_REVERSE_COPY) |
11951 | && o->size > address_size) | |
310fd250 L |
11952 | { |
11953 | /* Reverse-copy input section to output. */ | |
b02db378 | 11954 | |
0ae12467 AM |
11955 | if ((o->size & (address_size - 1)) != 0 |
11956 | || (o->reloc_count != 0 | |
11957 | && (o->size * bed->s->int_rels_per_ext_rel | |
11958 | != o->reloc_count * address_size))) | |
b02db378 AM |
11959 | { |
11960 | _bfd_error_handler | |
11961 | /* xgettext:c-format */ | |
11962 | (_("error: %pB: size of section %pA is not " | |
11963 | "multiple of address size"), | |
11964 | input_bfd, o); | |
11965 | bfd_set_error (bfd_error_bad_value); | |
11966 | return false; | |
11967 | } | |
11968 | ||
310fd250 L |
11969 | do |
11970 | { | |
11971 | todo -= address_size; | |
11972 | if (! bfd_set_section_contents (output_bfd, | |
11973 | o->output_section, | |
11974 | contents + todo, | |
11975 | offset, | |
11976 | address_size)) | |
0a1b45a2 | 11977 | return false; |
310fd250 L |
11978 | if (todo == 0) |
11979 | break; | |
11980 | offset += address_size; | |
11981 | } | |
11982 | while (1); | |
11983 | } | |
11984 | else if (! bfd_set_section_contents (output_bfd, | |
11985 | o->output_section, | |
11986 | contents, | |
11987 | offset, todo)) | |
0a1b45a2 | 11988 | return false; |
310fd250 | 11989 | } |
c152c796 AM |
11990 | } |
11991 | break; | |
11992 | } | |
11993 | } | |
11994 | ||
0a1b45a2 | 11995 | return true; |
c152c796 AM |
11996 | } |
11997 | ||
11998 | /* Generate a reloc when linking an ELF file. This is a reloc | |
3a800eb9 | 11999 | requested by the linker, and does not come from any input file. This |
c152c796 AM |
12000 | is used to build constructor and destructor tables when linking |
12001 | with -Ur. */ | |
12002 | ||
0a1b45a2 | 12003 | static bool |
c152c796 AM |
12004 | elf_reloc_link_order (bfd *output_bfd, |
12005 | struct bfd_link_info *info, | |
12006 | asection *output_section, | |
12007 | struct bfd_link_order *link_order) | |
12008 | { | |
12009 | reloc_howto_type *howto; | |
12010 | long indx; | |
12011 | bfd_vma offset; | |
12012 | bfd_vma addend; | |
d4730f92 | 12013 | struct bfd_elf_section_reloc_data *reldata; |
c152c796 AM |
12014 | struct elf_link_hash_entry **rel_hash_ptr; |
12015 | Elf_Internal_Shdr *rel_hdr; | |
12016 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
12017 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
12018 | bfd_byte *erel; | |
12019 | unsigned int i; | |
d4730f92 | 12020 | struct bfd_elf_section_data *esdo = elf_section_data (output_section); |
c152c796 AM |
12021 | |
12022 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
12023 | if (howto == NULL) | |
12024 | { | |
12025 | bfd_set_error (bfd_error_bad_value); | |
0a1b45a2 | 12026 | return false; |
c152c796 AM |
12027 | } |
12028 | ||
12029 | addend = link_order->u.reloc.p->addend; | |
12030 | ||
d4730f92 BS |
12031 | if (esdo->rel.hdr) |
12032 | reldata = &esdo->rel; | |
12033 | else if (esdo->rela.hdr) | |
12034 | reldata = &esdo->rela; | |
12035 | else | |
12036 | { | |
12037 | reldata = NULL; | |
12038 | BFD_ASSERT (0); | |
12039 | } | |
12040 | ||
c152c796 | 12041 | /* Figure out the symbol index. */ |
d4730f92 | 12042 | rel_hash_ptr = reldata->hashes + reldata->count; |
c152c796 AM |
12043 | if (link_order->type == bfd_section_reloc_link_order) |
12044 | { | |
12045 | indx = link_order->u.reloc.p->u.section->target_index; | |
12046 | BFD_ASSERT (indx != 0); | |
12047 | *rel_hash_ptr = NULL; | |
12048 | } | |
12049 | else | |
12050 | { | |
12051 | struct elf_link_hash_entry *h; | |
12052 | ||
12053 | /* Treat a reloc against a defined symbol as though it were | |
12054 | actually against the section. */ | |
12055 | h = ((struct elf_link_hash_entry *) | |
12056 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
12057 | link_order->u.reloc.p->u.name, | |
0a1b45a2 | 12058 | false, false, true)); |
c152c796 AM |
12059 | if (h != NULL |
12060 | && (h->root.type == bfd_link_hash_defined | |
12061 | || h->root.type == bfd_link_hash_defweak)) | |
12062 | { | |
12063 | asection *section; | |
12064 | ||
12065 | section = h->root.u.def.section; | |
12066 | indx = section->output_section->target_index; | |
12067 | *rel_hash_ptr = NULL; | |
12068 | /* It seems that we ought to add the symbol value to the | |
12069 | addend here, but in practice it has already been added | |
12070 | because it was passed to constructor_callback. */ | |
12071 | addend += section->output_section->vma + section->output_offset; | |
12072 | } | |
12073 | else if (h != NULL) | |
12074 | { | |
12075 | /* Setting the index to -2 tells elf_link_output_extsym that | |
12076 | this symbol is used by a reloc. */ | |
12077 | h->indx = -2; | |
12078 | *rel_hash_ptr = h; | |
12079 | indx = 0; | |
12080 | } | |
12081 | else | |
12082 | { | |
1a72702b AM |
12083 | (*info->callbacks->unattached_reloc) |
12084 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); | |
c152c796 AM |
12085 | indx = 0; |
12086 | } | |
12087 | } | |
12088 | ||
12089 | /* If this is an inplace reloc, we must write the addend into the | |
12090 | object file. */ | |
12091 | if (howto->partial_inplace && addend != 0) | |
12092 | { | |
12093 | bfd_size_type size; | |
12094 | bfd_reloc_status_type rstat; | |
12095 | bfd_byte *buf; | |
0a1b45a2 | 12096 | bool ok; |
c152c796 | 12097 | const char *sym_name; |
bb294208 | 12098 | bfd_size_type octets; |
c152c796 | 12099 | |
a50b1753 NC |
12100 | size = (bfd_size_type) bfd_get_reloc_size (howto); |
12101 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6346d5ca | 12102 | if (buf == NULL && size != 0) |
0a1b45a2 | 12103 | return false; |
c152c796 AM |
12104 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); |
12105 | switch (rstat) | |
12106 | { | |
12107 | case bfd_reloc_ok: | |
12108 | break; | |
12109 | ||
12110 | default: | |
12111 | case bfd_reloc_outofrange: | |
12112 | abort (); | |
12113 | ||
12114 | case bfd_reloc_overflow: | |
12115 | if (link_order->type == bfd_section_reloc_link_order) | |
fd361982 | 12116 | sym_name = bfd_section_name (link_order->u.reloc.p->u.section); |
c152c796 AM |
12117 | else |
12118 | sym_name = link_order->u.reloc.p->u.name; | |
1a72702b AM |
12119 | (*info->callbacks->reloc_overflow) (info, NULL, sym_name, |
12120 | howto->name, addend, NULL, NULL, | |
12121 | (bfd_vma) 0); | |
c152c796 AM |
12122 | break; |
12123 | } | |
37b01f6a | 12124 | |
bb294208 AM |
12125 | octets = link_order->offset * bfd_octets_per_byte (output_bfd, |
12126 | output_section); | |
c152c796 | 12127 | ok = bfd_set_section_contents (output_bfd, output_section, buf, |
bb294208 | 12128 | octets, size); |
c152c796 AM |
12129 | free (buf); |
12130 | if (! ok) | |
0a1b45a2 | 12131 | return false; |
c152c796 AM |
12132 | } |
12133 | ||
12134 | /* The address of a reloc is relative to the section in a | |
12135 | relocatable file, and is a virtual address in an executable | |
12136 | file. */ | |
12137 | offset = link_order->offset; | |
0e1862bb | 12138 | if (! bfd_link_relocatable (info)) |
c152c796 AM |
12139 | offset += output_section->vma; |
12140 | ||
12141 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
12142 | { | |
12143 | irel[i].r_offset = offset; | |
12144 | irel[i].r_info = 0; | |
12145 | irel[i].r_addend = 0; | |
12146 | } | |
12147 | if (bed->s->arch_size == 32) | |
12148 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
12149 | else | |
12150 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
12151 | ||
d4730f92 | 12152 | rel_hdr = reldata->hdr; |
c152c796 AM |
12153 | erel = rel_hdr->contents; |
12154 | if (rel_hdr->sh_type == SHT_REL) | |
12155 | { | |
d4730f92 | 12156 | erel += reldata->count * bed->s->sizeof_rel; |
c152c796 AM |
12157 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); |
12158 | } | |
12159 | else | |
12160 | { | |
12161 | irel[0].r_addend = addend; | |
d4730f92 | 12162 | erel += reldata->count * bed->s->sizeof_rela; |
c152c796 AM |
12163 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); |
12164 | } | |
12165 | ||
d4730f92 | 12166 | ++reldata->count; |
c152c796 | 12167 | |
0a1b45a2 | 12168 | return true; |
c152c796 AM |
12169 | } |
12170 | ||
76359541 TP |
12171 | /* Generate an import library in INFO->implib_bfd from symbols in ABFD. |
12172 | Returns TRUE upon success, FALSE otherwise. */ | |
12173 | ||
0a1b45a2 | 12174 | static bool |
76359541 TP |
12175 | elf_output_implib (bfd *abfd, struct bfd_link_info *info) |
12176 | { | |
0a1b45a2 | 12177 | bool ret = false; |
76359541 TP |
12178 | bfd *implib_bfd; |
12179 | const struct elf_backend_data *bed; | |
12180 | flagword flags; | |
12181 | enum bfd_architecture arch; | |
12182 | unsigned int mach; | |
12183 | asymbol **sympp = NULL; | |
12184 | long symsize; | |
12185 | long symcount; | |
12186 | long src_count; | |
12187 | elf_symbol_type *osymbuf; | |
446f7ed5 | 12188 | size_t amt; |
76359541 TP |
12189 | |
12190 | implib_bfd = info->out_implib_bfd; | |
12191 | bed = get_elf_backend_data (abfd); | |
12192 | ||
12193 | if (!bfd_set_format (implib_bfd, bfd_object)) | |
0a1b45a2 | 12194 | return false; |
76359541 | 12195 | |
046734ff | 12196 | /* Use flag from executable but make it a relocatable object. */ |
76359541 TP |
12197 | flags = bfd_get_file_flags (abfd); |
12198 | flags &= ~HAS_RELOC; | |
12199 | if (!bfd_set_start_address (implib_bfd, 0) | |
046734ff | 12200 | || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) |
0a1b45a2 | 12201 | return false; |
76359541 TP |
12202 | |
12203 | /* Copy architecture of output file to import library file. */ | |
12204 | arch = bfd_get_arch (abfd); | |
12205 | mach = bfd_get_mach (abfd); | |
12206 | if (!bfd_set_arch_mach (implib_bfd, arch, mach) | |
12207 | && (abfd->target_defaulted | |
12208 | || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) | |
0a1b45a2 | 12209 | return false; |
76359541 TP |
12210 | |
12211 | /* Get symbol table size. */ | |
12212 | symsize = bfd_get_symtab_upper_bound (abfd); | |
12213 | if (symsize < 0) | |
0a1b45a2 | 12214 | return false; |
76359541 TP |
12215 | |
12216 | /* Read in the symbol table. */ | |
ec9bd0a2 AM |
12217 | sympp = (asymbol **) bfd_malloc (symsize); |
12218 | if (sympp == NULL) | |
0a1b45a2 | 12219 | return false; |
ec9bd0a2 | 12220 | |
76359541 TP |
12221 | symcount = bfd_canonicalize_symtab (abfd, sympp); |
12222 | if (symcount < 0) | |
12223 | goto free_sym_buf; | |
12224 | ||
12225 | /* Allow the BFD backend to copy any private header data it | |
12226 | understands from the output BFD to the import library BFD. */ | |
12227 | if (! bfd_copy_private_header_data (abfd, implib_bfd)) | |
12228 | goto free_sym_buf; | |
12229 | ||
12230 | /* Filter symbols to appear in the import library. */ | |
12231 | if (bed->elf_backend_filter_implib_symbols) | |
12232 | symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, | |
12233 | symcount); | |
12234 | else | |
12235 | symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); | |
12236 | if (symcount == 0) | |
12237 | { | |
5df1bc57 | 12238 | bfd_set_error (bfd_error_no_symbols); |
871b3ab2 | 12239 | _bfd_error_handler (_("%pB: no symbol found for import library"), |
4eca0228 | 12240 | implib_bfd); |
76359541 TP |
12241 | goto free_sym_buf; |
12242 | } | |
12243 | ||
12244 | ||
12245 | /* Make symbols absolute. */ | |
446f7ed5 AM |
12246 | amt = symcount * sizeof (*osymbuf); |
12247 | osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt); | |
ec9bd0a2 AM |
12248 | if (osymbuf == NULL) |
12249 | goto free_sym_buf; | |
12250 | ||
76359541 TP |
12251 | for (src_count = 0; src_count < symcount; src_count++) |
12252 | { | |
12253 | memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], | |
12254 | sizeof (*osymbuf)); | |
12255 | osymbuf[src_count].symbol.section = bfd_abs_section_ptr; | |
12256 | osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; | |
12257 | osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; | |
12258 | osymbuf[src_count].internal_elf_sym.st_value = | |
12259 | osymbuf[src_count].symbol.value; | |
12260 | sympp[src_count] = &osymbuf[src_count].symbol; | |
12261 | } | |
12262 | ||
12263 | bfd_set_symtab (implib_bfd, sympp, symcount); | |
12264 | ||
12265 | /* Allow the BFD backend to copy any private data it understands | |
12266 | from the output BFD to the import library BFD. This is done last | |
12267 | to permit the routine to look at the filtered symbol table. */ | |
12268 | if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) | |
12269 | goto free_sym_buf; | |
12270 | ||
12271 | if (!bfd_close (implib_bfd)) | |
12272 | goto free_sym_buf; | |
12273 | ||
0a1b45a2 | 12274 | ret = true; |
76359541 | 12275 | |
dc1e8a47 | 12276 | free_sym_buf: |
76359541 TP |
12277 | free (sympp); |
12278 | return ret; | |
12279 | } | |
12280 | ||
9f7c3e5e AM |
12281 | static void |
12282 | elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) | |
12283 | { | |
12284 | asection *o; | |
12285 | ||
12286 | if (flinfo->symstrtab != NULL) | |
ef10c3ac | 12287 | _bfd_elf_strtab_free (flinfo->symstrtab); |
c9594989 AM |
12288 | free (flinfo->contents); |
12289 | free (flinfo->external_relocs); | |
12290 | free (flinfo->internal_relocs); | |
12291 | free (flinfo->external_syms); | |
12292 | free (flinfo->locsym_shndx); | |
12293 | free (flinfo->internal_syms); | |
12294 | free (flinfo->indices); | |
12295 | free (flinfo->sections); | |
12296 | if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1) | |
9f7c3e5e AM |
12297 | free (flinfo->symshndxbuf); |
12298 | for (o = obfd->sections; o != NULL; o = o->next) | |
12299 | { | |
12300 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
c9594989 AM |
12301 | free (esdo->rel.hashes); |
12302 | free (esdo->rela.hashes); | |
9f7c3e5e AM |
12303 | } |
12304 | } | |
0b52efa6 | 12305 | |
c152c796 AM |
12306 | /* Do the final step of an ELF link. */ |
12307 | ||
0a1b45a2 | 12308 | bool |
c152c796 AM |
12309 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) |
12310 | { | |
0a1b45a2 AM |
12311 | bool dynamic; |
12312 | bool emit_relocs; | |
c152c796 | 12313 | bfd *dynobj; |
8b127cbc | 12314 | struct elf_final_link_info flinfo; |
91d6fa6a NC |
12315 | asection *o; |
12316 | struct bfd_link_order *p; | |
12317 | bfd *sub; | |
c152c796 AM |
12318 | bfd_size_type max_contents_size; |
12319 | bfd_size_type max_external_reloc_size; | |
12320 | bfd_size_type max_internal_reloc_count; | |
12321 | bfd_size_type max_sym_count; | |
12322 | bfd_size_type max_sym_shndx_count; | |
c152c796 AM |
12323 | Elf_Internal_Sym elfsym; |
12324 | unsigned int i; | |
12325 | Elf_Internal_Shdr *symtab_hdr; | |
12326 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
c152c796 AM |
12327 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
12328 | struct elf_outext_info eoinfo; | |
0a1b45a2 | 12329 | bool merged; |
9c3d7d9b AM |
12330 | size_t relativecount; |
12331 | size_t relr_entsize; | |
c152c796 AM |
12332 | asection *reldyn = 0; |
12333 | bfd_size_type amt; | |
104d59d1 JM |
12334 | asection *attr_section = NULL; |
12335 | bfd_vma attr_size = 0; | |
12336 | const char *std_attrs_section; | |
64f52338 | 12337 | struct elf_link_hash_table *htab = elf_hash_table (info); |
0a1b45a2 AM |
12338 | bool sections_removed; |
12339 | bool ret; | |
c152c796 | 12340 | |
2cc15b10 | 12341 | if (!is_elf_hash_table (&htab->root)) |
0a1b45a2 | 12342 | return false; |
c152c796 | 12343 | |
0e1862bb | 12344 | if (bfd_link_pic (info)) |
c152c796 AM |
12345 | abfd->flags |= DYNAMIC; |
12346 | ||
64f52338 AM |
12347 | dynamic = htab->dynamic_sections_created; |
12348 | dynobj = htab->dynobj; | |
c152c796 | 12349 | |
0e1862bb | 12350 | emit_relocs = (bfd_link_relocatable (info) |
a4676736 | 12351 | || info->emitrelocations); |
c152c796 | 12352 | |
496afd17 | 12353 | memset (&flinfo, 0, sizeof (flinfo)); |
8b127cbc AM |
12354 | flinfo.info = info; |
12355 | flinfo.output_bfd = abfd; | |
ef10c3ac | 12356 | flinfo.symstrtab = _bfd_elf_strtab_init (); |
8b127cbc | 12357 | if (flinfo.symstrtab == NULL) |
0a1b45a2 | 12358 | return false; |
c152c796 AM |
12359 | |
12360 | if (! dynamic) | |
12361 | { | |
8b127cbc AM |
12362 | flinfo.hash_sec = NULL; |
12363 | flinfo.symver_sec = NULL; | |
c152c796 AM |
12364 | } |
12365 | else | |
12366 | { | |
3d4d4302 | 12367 | flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); |
202e2356 | 12368 | /* Note that dynsym_sec can be NULL (on VMS). */ |
3d4d4302 | 12369 | flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); |
c152c796 AM |
12370 | /* Note that it is OK if symver_sec is NULL. */ |
12371 | } | |
12372 | ||
496afd17 L |
12373 | if (info->unique_symbol |
12374 | && !bfd_hash_table_init (&flinfo.local_hash_table, | |
12375 | local_hash_newfunc, | |
12376 | sizeof (struct local_hash_entry))) | |
0a1b45a2 | 12377 | return false; |
c152c796 | 12378 | |
104d59d1 JM |
12379 | /* The object attributes have been merged. Remove the input |
12380 | sections from the link, and set the contents of the output | |
1ff6de03 | 12381 | section. */ |
0a1b45a2 | 12382 | sections_removed = false; |
104d59d1 JM |
12383 | std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; |
12384 | for (o = abfd->sections; o != NULL; o = o->next) | |
12385 | { | |
0a1b45a2 | 12386 | bool remove_section = false; |
b8a6ced7 | 12387 | |
104d59d1 JM |
12388 | if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) |
12389 | || strcmp (o->name, ".gnu.attributes") == 0) | |
12390 | { | |
12391 | for (p = o->map_head.link_order; p != NULL; p = p->next) | |
12392 | { | |
12393 | asection *input_section; | |
12394 | ||
12395 | if (p->type != bfd_indirect_link_order) | |
12396 | continue; | |
12397 | input_section = p->u.indirect.section; | |
12398 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
12399 | elf_link_input_bfd ignores this section. */ | |
12400 | input_section->flags &= ~SEC_HAS_CONTENTS; | |
12401 | } | |
a0c8462f | 12402 | |
104d59d1 | 12403 | attr_size = bfd_elf_obj_attr_size (abfd); |
fd361982 | 12404 | bfd_set_section_size (o, attr_size); |
b8a6ced7 AM |
12405 | /* Skip this section later on. */ |
12406 | o->map_head.link_order = NULL; | |
104d59d1 | 12407 | if (attr_size) |
b8a6ced7 | 12408 | attr_section = o; |
104d59d1 | 12409 | else |
0a1b45a2 | 12410 | remove_section = true; |
104d59d1 | 12411 | } |
6e5e9d58 AM |
12412 | else if ((o->flags & SEC_GROUP) != 0 && o->size == 0) |
12413 | { | |
12414 | /* Remove empty group section from linker output. */ | |
0a1b45a2 | 12415 | remove_section = true; |
b8a6ced7 | 12416 | } |
5270eddc | 12417 | if (remove_section) |
b8a6ced7 | 12418 | { |
6e5e9d58 AM |
12419 | o->flags |= SEC_EXCLUDE; |
12420 | bfd_section_list_remove (abfd, o); | |
12421 | abfd->section_count--; | |
0a1b45a2 | 12422 | sections_removed = true; |
6e5e9d58 | 12423 | } |
104d59d1 | 12424 | } |
4c6ee646 AM |
12425 | if (sections_removed) |
12426 | _bfd_fix_excluded_sec_syms (abfd, info); | |
104d59d1 | 12427 | |
c152c796 AM |
12428 | /* Count up the number of relocations we will output for each output |
12429 | section, so that we know the sizes of the reloc sections. We | |
12430 | also figure out some maximum sizes. */ | |
12431 | max_contents_size = 0; | |
12432 | max_external_reloc_size = 0; | |
12433 | max_internal_reloc_count = 0; | |
12434 | max_sym_count = 0; | |
12435 | max_sym_shndx_count = 0; | |
0a1b45a2 | 12436 | merged = false; |
c152c796 AM |
12437 | for (o = abfd->sections; o != NULL; o = o->next) |
12438 | { | |
12439 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
12440 | o->reloc_count = 0; | |
12441 | ||
8423293d | 12442 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12443 | { |
12444 | unsigned int reloc_count = 0; | |
9eaff861 | 12445 | unsigned int additional_reloc_count = 0; |
c152c796 | 12446 | struct bfd_elf_section_data *esdi = NULL; |
c152c796 AM |
12447 | |
12448 | if (p->type == bfd_section_reloc_link_order | |
12449 | || p->type == bfd_symbol_reloc_link_order) | |
12450 | reloc_count = 1; | |
12451 | else if (p->type == bfd_indirect_link_order) | |
12452 | { | |
12453 | asection *sec; | |
12454 | ||
12455 | sec = p->u.indirect.section; | |
c152c796 AM |
12456 | |
12457 | /* Mark all sections which are to be included in the | |
12458 | link. This will normally be every section. We need | |
12459 | to do this so that we can identify any sections which | |
12460 | the linker has decided to not include. */ | |
0a1b45a2 | 12461 | sec->linker_mark = true; |
c152c796 AM |
12462 | |
12463 | if (sec->flags & SEC_MERGE) | |
0a1b45a2 | 12464 | merged = true; |
c152c796 | 12465 | |
eea6121a AM |
12466 | if (sec->rawsize > max_contents_size) |
12467 | max_contents_size = sec->rawsize; | |
12468 | if (sec->size > max_contents_size) | |
12469 | max_contents_size = sec->size; | |
c152c796 | 12470 | |
c152c796 AM |
12471 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
12472 | && (sec->owner->flags & DYNAMIC) == 0) | |
12473 | { | |
12474 | size_t sym_count; | |
12475 | ||
a961cdd5 AM |
12476 | /* We are interested in just local symbols, not all |
12477 | symbols. */ | |
c152c796 AM |
12478 | if (elf_bad_symtab (sec->owner)) |
12479 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
12480 | / bed->s->sizeof_sym); | |
12481 | else | |
12482 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
12483 | ||
12484 | if (sym_count > max_sym_count) | |
12485 | max_sym_count = sym_count; | |
12486 | ||
12487 | if (sym_count > max_sym_shndx_count | |
6a40cf0c | 12488 | && elf_symtab_shndx_list (sec->owner) != NULL) |
c152c796 AM |
12489 | max_sym_shndx_count = sym_count; |
12490 | ||
35c5dcc6 AM |
12491 | esdi = elf_section_data (sec); |
12492 | ||
12493 | if (esdi->this_hdr.sh_type == SHT_REL | |
12494 | || esdi->this_hdr.sh_type == SHT_RELA) | |
a961cdd5 AM |
12495 | /* Some backends use reloc_count in relocation sections |
12496 | to count particular types of relocs. Of course, | |
12497 | reloc sections themselves can't have relocations. */ | |
12498 | ; | |
12499 | else if (emit_relocs) | |
12500 | { | |
12501 | reloc_count = sec->reloc_count; | |
12502 | if (bed->elf_backend_count_additional_relocs) | |
12503 | { | |
12504 | int c; | |
12505 | c = (*bed->elf_backend_count_additional_relocs) (sec); | |
12506 | additional_reloc_count += c; | |
12507 | } | |
12508 | } | |
12509 | else if (bed->elf_backend_count_relocs) | |
12510 | reloc_count = (*bed->elf_backend_count_relocs) (info, sec); | |
12511 | ||
c152c796 AM |
12512 | if ((sec->flags & SEC_RELOC) != 0) |
12513 | { | |
d4730f92 | 12514 | size_t ext_size = 0; |
c152c796 | 12515 | |
d4730f92 BS |
12516 | if (esdi->rel.hdr != NULL) |
12517 | ext_size = esdi->rel.hdr->sh_size; | |
12518 | if (esdi->rela.hdr != NULL) | |
12519 | ext_size += esdi->rela.hdr->sh_size; | |
7326c758 | 12520 | |
c152c796 AM |
12521 | if (ext_size > max_external_reloc_size) |
12522 | max_external_reloc_size = ext_size; | |
12523 | if (sec->reloc_count > max_internal_reloc_count) | |
12524 | max_internal_reloc_count = sec->reloc_count; | |
12525 | } | |
12526 | } | |
12527 | } | |
12528 | ||
12529 | if (reloc_count == 0) | |
12530 | continue; | |
12531 | ||
9eaff861 | 12532 | reloc_count += additional_reloc_count; |
c152c796 AM |
12533 | o->reloc_count += reloc_count; |
12534 | ||
0e1862bb | 12535 | if (p->type == bfd_indirect_link_order && emit_relocs) |
c152c796 | 12536 | { |
d4730f92 | 12537 | if (esdi->rel.hdr) |
9eaff861 | 12538 | { |
491d01d3 | 12539 | esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); |
9eaff861 AO |
12540 | esdo->rel.count += additional_reloc_count; |
12541 | } | |
d4730f92 | 12542 | if (esdi->rela.hdr) |
9eaff861 | 12543 | { |
491d01d3 | 12544 | esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); |
9eaff861 AO |
12545 | esdo->rela.count += additional_reloc_count; |
12546 | } | |
d4730f92 BS |
12547 | } |
12548 | else | |
12549 | { | |
12550 | if (o->use_rela_p) | |
12551 | esdo->rela.count += reloc_count; | |
2c2b4ed4 | 12552 | else |
d4730f92 | 12553 | esdo->rel.count += reloc_count; |
c152c796 | 12554 | } |
c152c796 AM |
12555 | } |
12556 | ||
9eaff861 | 12557 | if (o->reloc_count > 0) |
c152c796 AM |
12558 | o->flags |= SEC_RELOC; |
12559 | else | |
12560 | { | |
12561 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
12562 | set it (this is probably a bug) and if it is set | |
12563 | assign_section_numbers will create a reloc section. */ | |
12564 | o->flags &=~ SEC_RELOC; | |
12565 | } | |
12566 | ||
12567 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
12568 | zero. This is done in elf_fake_sections as well, but forcing | |
12569 | the VMA to 0 here will ensure that relocs against these | |
12570 | sections are handled correctly. */ | |
12571 | if ((o->flags & SEC_ALLOC) == 0 | |
12572 | && ! o->user_set_vma) | |
12573 | o->vma = 0; | |
12574 | } | |
12575 | ||
0e1862bb | 12576 | if (! bfd_link_relocatable (info) && merged) |
64f52338 | 12577 | elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); |
c152c796 AM |
12578 | |
12579 | /* Figure out the file positions for everything but the symbol table | |
12580 | and the relocs. We set symcount to force assign_section_numbers | |
12581 | to create a symbol table. */ | |
ed48ec2e | 12582 | abfd->symcount = info->strip != strip_all || emit_relocs; |
c152c796 AM |
12583 | BFD_ASSERT (! abfd->output_has_begun); |
12584 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
12585 | goto error_return; | |
12586 | ||
ee75fd95 | 12587 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
12588 | for (o = abfd->sections; o != NULL; o = o->next) |
12589 | { | |
d4730f92 | 12590 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
c152c796 AM |
12591 | if ((o->flags & SEC_RELOC) != 0) |
12592 | { | |
d4730f92 | 12593 | if (esdo->rel.hdr |
9eaff861 | 12594 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) |
c152c796 AM |
12595 | goto error_return; |
12596 | ||
d4730f92 | 12597 | if (esdo->rela.hdr |
9eaff861 | 12598 | && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) |
c152c796 AM |
12599 | goto error_return; |
12600 | } | |
12601 | ||
48db3297 AM |
12602 | /* _bfd_elf_compute_section_file_positions makes temporary use |
12603 | of target_index. Reset it. */ | |
12604 | o->target_index = 0; | |
12605 | ||
c152c796 AM |
12606 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them |
12607 | to count upwards while actually outputting the relocations. */ | |
d4730f92 BS |
12608 | esdo->rel.count = 0; |
12609 | esdo->rela.count = 0; | |
0ce398f1 | 12610 | |
1ff6de03 NA |
12611 | if ((esdo->this_hdr.sh_offset == (file_ptr) -1) |
12612 | && !bfd_section_is_ctf (o)) | |
0ce398f1 L |
12613 | { |
12614 | /* Cache the section contents so that they can be compressed | |
12615 | later. Use bfd_malloc since it will be freed by | |
12616 | bfd_compress_section_contents. */ | |
12617 | unsigned char *contents = esdo->this_hdr.contents; | |
da16cc96 | 12618 | if (contents != NULL) |
0ce398f1 L |
12619 | abort (); |
12620 | contents | |
12621 | = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); | |
12622 | if (contents == NULL) | |
12623 | goto error_return; | |
12624 | esdo->this_hdr.contents = contents; | |
12625 | } | |
c152c796 AM |
12626 | } |
12627 | ||
1ff6de03 NA |
12628 | /* We have now assigned file positions for all the sections except .symtab, |
12629 | .strtab, and non-loaded reloc and compressed debugging sections. We start | |
12630 | the .symtab section at the current file position, and write directly to it. | |
12631 | We build the .strtab section in memory. */ | |
ed48ec2e | 12632 | abfd->symcount = 0; |
c152c796 AM |
12633 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
12634 | /* sh_name is set in prep_headers. */ | |
12635 | symtab_hdr->sh_type = SHT_SYMTAB; | |
12636 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
12637 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
12638 | /* sh_link is set in assign_section_numbers. */ | |
12639 | /* sh_info is set below. */ | |
12640 | /* sh_offset is set just below. */ | |
72de5009 | 12641 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
c152c796 | 12642 | |
ef10c3ac L |
12643 | if (max_sym_count < 20) |
12644 | max_sym_count = 20; | |
64f52338 | 12645 | htab->strtabsize = max_sym_count; |
ef10c3ac | 12646 | amt = max_sym_count * sizeof (struct elf_sym_strtab); |
64f52338 AM |
12647 | htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); |
12648 | if (htab->strtab == NULL) | |
c152c796 | 12649 | goto error_return; |
ef10c3ac L |
12650 | /* The real buffer will be allocated in elf_link_swap_symbols_out. */ |
12651 | flinfo.symshndxbuf | |
12652 | = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) | |
12653 | ? (Elf_External_Sym_Shndx *) -1 : NULL); | |
c152c796 | 12654 | |
8539e4e8 | 12655 | if (info->strip != strip_all || emit_relocs) |
c152c796 | 12656 | { |
8539e4e8 AM |
12657 | file_ptr off = elf_next_file_pos (abfd); |
12658 | ||
0a1b45a2 | 12659 | _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true); |
8539e4e8 AM |
12660 | |
12661 | /* Note that at this point elf_next_file_pos (abfd) is | |
12662 | incorrect. We do not yet know the size of the .symtab section. | |
12663 | We correct next_file_pos below, after we do know the size. */ | |
12664 | ||
12665 | /* Start writing out the symbol table. The first symbol is always a | |
12666 | dummy symbol. */ | |
c152c796 AM |
12667 | elfsym.st_value = 0; |
12668 | elfsym.st_size = 0; | |
12669 | elfsym.st_info = 0; | |
12670 | elfsym.st_other = 0; | |
12671 | elfsym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 12672 | elfsym.st_target_internal = 0; |
ef10c3ac L |
12673 | if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, |
12674 | bfd_und_section_ptr, NULL) != 1) | |
c152c796 | 12675 | goto error_return; |
c152c796 | 12676 | |
d1bcae83 L |
12677 | /* Output a symbol for each section if asked or they are used for |
12678 | relocs. These symbols usually have no names. We store the | |
12679 | index of each one in the index field of the section, so that | |
12680 | we can find it again when outputting relocs. */ | |
c77cb2a0 | 12681 | |
d1bcae83 | 12682 | if (bfd_keep_unused_section_symbols (abfd) || emit_relocs) |
c152c796 | 12683 | { |
0a1b45a2 | 12684 | bool name_local_sections |
d1bcae83 L |
12685 | = (bed->elf_backend_name_local_section_symbols |
12686 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
12687 | const char *name = NULL; | |
12688 | ||
12689 | elfsym.st_size = 0; | |
12690 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12691 | elfsym.st_other = 0; | |
12692 | elfsym.st_value = 0; | |
12693 | elfsym.st_target_internal = 0; | |
12694 | for (i = 1; i < elf_numsections (abfd); i++) | |
f0b5bb34 | 12695 | { |
d1bcae83 L |
12696 | o = bfd_section_from_elf_index (abfd, i); |
12697 | if (o != NULL) | |
12698 | { | |
12699 | o->target_index = bfd_get_symcount (abfd); | |
12700 | elfsym.st_shndx = i; | |
12701 | if (!bfd_link_relocatable (info)) | |
12702 | elfsym.st_value = o->vma; | |
12703 | if (name_local_sections) | |
12704 | name = o->name; | |
12705 | if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o, | |
12706 | NULL) != 1) | |
12707 | goto error_return; | |
12708 | } | |
f0b5bb34 | 12709 | } |
c152c796 AM |
12710 | } |
12711 | } | |
12712 | ||
3f1b17bb MR |
12713 | /* On some targets like Irix 5 the symbol split between local and global |
12714 | ones recorded in the sh_info field needs to be done between section | |
12715 | and all other symbols. */ | |
12716 | if (bed->elf_backend_elfsym_local_is_section | |
12717 | && bed->elf_backend_elfsym_local_is_section (abfd)) | |
12718 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
12719 | ||
c152c796 AM |
12720 | /* Allocate some memory to hold information read in from the input |
12721 | files. */ | |
12722 | if (max_contents_size != 0) | |
12723 | { | |
8b127cbc AM |
12724 | flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
12725 | if (flinfo.contents == NULL) | |
c152c796 AM |
12726 | goto error_return; |
12727 | } | |
12728 | ||
12729 | if (max_external_reloc_size != 0) | |
12730 | { | |
8b127cbc AM |
12731 | flinfo.external_relocs = bfd_malloc (max_external_reloc_size); |
12732 | if (flinfo.external_relocs == NULL) | |
c152c796 AM |
12733 | goto error_return; |
12734 | } | |
12735 | ||
12736 | if (max_internal_reloc_count != 0) | |
12737 | { | |
056bafd4 | 12738 | amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); |
8b127cbc AM |
12739 | flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); |
12740 | if (flinfo.internal_relocs == NULL) | |
c152c796 AM |
12741 | goto error_return; |
12742 | } | |
12743 | ||
12744 | if (max_sym_count != 0) | |
12745 | { | |
12746 | amt = max_sym_count * bed->s->sizeof_sym; | |
8b127cbc AM |
12747 | flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); |
12748 | if (flinfo.external_syms == NULL) | |
c152c796 AM |
12749 | goto error_return; |
12750 | ||
12751 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
8b127cbc AM |
12752 | flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); |
12753 | if (flinfo.internal_syms == NULL) | |
c152c796 AM |
12754 | goto error_return; |
12755 | ||
12756 | amt = max_sym_count * sizeof (long); | |
8b127cbc AM |
12757 | flinfo.indices = (long int *) bfd_malloc (amt); |
12758 | if (flinfo.indices == NULL) | |
c152c796 AM |
12759 | goto error_return; |
12760 | ||
12761 | amt = max_sym_count * sizeof (asection *); | |
8b127cbc AM |
12762 | flinfo.sections = (asection **) bfd_malloc (amt); |
12763 | if (flinfo.sections == NULL) | |
c152c796 AM |
12764 | goto error_return; |
12765 | } | |
12766 | ||
12767 | if (max_sym_shndx_count != 0) | |
12768 | { | |
12769 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
8b127cbc AM |
12770 | flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
12771 | if (flinfo.locsym_shndx == NULL) | |
c152c796 AM |
12772 | goto error_return; |
12773 | } | |
12774 | ||
64f52338 | 12775 | if (htab->tls_sec) |
c152c796 | 12776 | { |
66631823 | 12777 | bfd_vma base, end = 0; /* Both bytes. */ |
c152c796 AM |
12778 | asection *sec; |
12779 | ||
64f52338 | 12780 | for (sec = htab->tls_sec; |
c152c796 AM |
12781 | sec && (sec->flags & SEC_THREAD_LOCAL); |
12782 | sec = sec->next) | |
12783 | { | |
3a800eb9 | 12784 | bfd_size_type size = sec->size; |
66631823 | 12785 | unsigned int opb = bfd_octets_per_byte (abfd, sec); |
c152c796 | 12786 | |
3a800eb9 AM |
12787 | if (size == 0 |
12788 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
c152c796 | 12789 | { |
91d6fa6a NC |
12790 | struct bfd_link_order *ord = sec->map_tail.link_order; |
12791 | ||
12792 | if (ord != NULL) | |
66631823 | 12793 | size = ord->offset * opb + ord->size; |
c152c796 | 12794 | } |
66631823 | 12795 | end = sec->vma + size / opb; |
c152c796 | 12796 | } |
64f52338 | 12797 | base = htab->tls_sec->vma; |
7dc98aea RO |
12798 | /* Only align end of TLS section if static TLS doesn't have special |
12799 | alignment requirements. */ | |
12800 | if (bed->static_tls_alignment == 1) | |
64f52338 AM |
12801 | end = align_power (end, htab->tls_sec->alignment_power); |
12802 | htab->tls_size = end - base; | |
c152c796 AM |
12803 | } |
12804 | ||
2f0c68f2 | 12805 | if (!_bfd_elf_fixup_eh_frame_hdr (info)) |
0a1b45a2 | 12806 | return false; |
2f0c68f2 | 12807 | |
23cc1de5 L |
12808 | /* Finish relative relocations here after regular symbol processing |
12809 | is finished if DT_RELR is enabled. */ | |
12810 | if (info->enable_dt_relr | |
12811 | && bed->finish_relative_relocs | |
12812 | && !bed->finish_relative_relocs (info)) | |
12813 | info->callbacks->einfo | |
12814 | (_("%F%P: %pB: failed to finish relative relocations\n"), abfd); | |
12815 | ||
c152c796 AM |
12816 | /* Since ELF permits relocations to be against local symbols, we |
12817 | must have the local symbols available when we do the relocations. | |
12818 | Since we would rather only read the local symbols once, and we | |
12819 | would rather not keep them in memory, we handle all the | |
12820 | relocations for a single input file at the same time. | |
12821 | ||
12822 | Unfortunately, there is no way to know the total number of local | |
12823 | symbols until we have seen all of them, and the local symbol | |
12824 | indices precede the global symbol indices. This means that when | |
12825 | we are generating relocatable output, and we see a reloc against | |
12826 | a global symbol, we can not know the symbol index until we have | |
12827 | finished examining all the local symbols to see which ones we are | |
12828 | going to output. To deal with this, we keep the relocations in | |
12829 | memory, and don't output them until the end of the link. This is | |
12830 | an unfortunate waste of memory, but I don't see a good way around | |
12831 | it. Fortunately, it only happens when performing a relocatable | |
12832 | link, which is not the common case. FIXME: If keep_memory is set | |
12833 | we could write the relocs out and then read them again; I don't | |
12834 | know how bad the memory loss will be. */ | |
12835 | ||
c72f2fb2 | 12836 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
0a1b45a2 | 12837 | sub->output_has_begun = false; |
c152c796 AM |
12838 | for (o = abfd->sections; o != NULL; o = o->next) |
12839 | { | |
8423293d | 12840 | for (p = o->map_head.link_order; p != NULL; p = p->next) |
c152c796 AM |
12841 | { |
12842 | if (p->type == bfd_indirect_link_order | |
12843 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
12844 | == bfd_target_elf_flavour) | |
12845 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
12846 | { | |
12847 | if (! sub->output_has_begun) | |
12848 | { | |
8b127cbc | 12849 | if (! elf_link_input_bfd (&flinfo, sub)) |
c152c796 | 12850 | goto error_return; |
0a1b45a2 | 12851 | sub->output_has_begun = true; |
c152c796 AM |
12852 | } |
12853 | } | |
12854 | else if (p->type == bfd_section_reloc_link_order | |
12855 | || p->type == bfd_symbol_reloc_link_order) | |
12856 | { | |
12857 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
12858 | goto error_return; | |
12859 | } | |
12860 | else | |
12861 | { | |
12862 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
351f65ca L |
12863 | { |
12864 | if (p->type == bfd_indirect_link_order | |
12865 | && (bfd_get_flavour (sub) | |
12866 | == bfd_target_elf_flavour) | |
12867 | && (elf_elfheader (sub)->e_ident[EI_CLASS] | |
12868 | != bed->s->elfclass)) | |
12869 | { | |
12870 | const char *iclass, *oclass; | |
12871 | ||
aebf9be7 | 12872 | switch (bed->s->elfclass) |
351f65ca | 12873 | { |
aebf9be7 NC |
12874 | case ELFCLASS64: oclass = "ELFCLASS64"; break; |
12875 | case ELFCLASS32: oclass = "ELFCLASS32"; break; | |
12876 | case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; | |
12877 | default: abort (); | |
351f65ca | 12878 | } |
aebf9be7 NC |
12879 | |
12880 | switch (elf_elfheader (sub)->e_ident[EI_CLASS]) | |
351f65ca | 12881 | { |
aebf9be7 NC |
12882 | case ELFCLASS64: iclass = "ELFCLASS64"; break; |
12883 | case ELFCLASS32: iclass = "ELFCLASS32"; break; | |
12884 | case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; | |
12885 | default: abort (); | |
351f65ca L |
12886 | } |
12887 | ||
12888 | bfd_set_error (bfd_error_wrong_format); | |
4eca0228 | 12889 | _bfd_error_handler |
695344c0 | 12890 | /* xgettext:c-format */ |
871b3ab2 | 12891 | (_("%pB: file class %s incompatible with %s"), |
351f65ca L |
12892 | sub, iclass, oclass); |
12893 | } | |
12894 | ||
12895 | goto error_return; | |
12896 | } | |
c152c796 AM |
12897 | } |
12898 | } | |
12899 | } | |
12900 | ||
c0f00686 L |
12901 | /* Free symbol buffer if needed. */ |
12902 | if (!info->reduce_memory_overheads) | |
12903 | { | |
c72f2fb2 | 12904 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c9594989 | 12905 | if (bfd_get_flavour (sub) == bfd_target_elf_flavour) |
c0f00686 L |
12906 | { |
12907 | free (elf_tdata (sub)->symbuf); | |
12908 | elf_tdata (sub)->symbuf = NULL; | |
12909 | } | |
12910 | } | |
12911 | ||
0a1b45a2 | 12912 | ret = true; |
496afd17 | 12913 | |
c152c796 AM |
12914 | /* Output any global symbols that got converted to local in a |
12915 | version script or due to symbol visibility. We do this in a | |
12916 | separate step since ELF requires all local symbols to appear | |
12917 | prior to any global symbols. FIXME: We should only do this if | |
12918 | some global symbols were, in fact, converted to become local. | |
12919 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
0a1b45a2 | 12920 | eoinfo.failed = false; |
8b127cbc | 12921 | eoinfo.flinfo = &flinfo; |
0a1b45a2 AM |
12922 | eoinfo.localsyms = true; |
12923 | eoinfo.file_sym_done = false; | |
7686d77d | 12924 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 | 12925 | if (eoinfo.failed) |
496afd17 | 12926 | { |
0a1b45a2 | 12927 | ret = false; |
496afd17 L |
12928 | goto return_local_hash_table; |
12929 | } | |
c152c796 | 12930 | |
4e617b1e PB |
12931 | /* If backend needs to output some local symbols not present in the hash |
12932 | table, do it now. */ | |
25d17459 | 12933 | if (bed->elf_backend_output_arch_local_syms) |
4e617b1e | 12934 | { |
4e617b1e | 12935 | if (! ((*bed->elf_backend_output_arch_local_syms) |
37bb890f | 12936 | (abfd, info, &flinfo, elf_link_output_symstrtab))) |
496afd17 | 12937 | { |
0a1b45a2 | 12938 | ret = false; |
496afd17 L |
12939 | goto return_local_hash_table; |
12940 | } | |
4e617b1e PB |
12941 | } |
12942 | ||
c152c796 AM |
12943 | /* That wrote out all the local symbols. Finish up the symbol table |
12944 | with the global symbols. Even if we want to strip everything we | |
12945 | can, we still need to deal with those global symbols that got | |
12946 | converted to local in a version script. */ | |
12947 | ||
12948 | /* The sh_info field records the index of the first non local symbol. */ | |
3f1b17bb MR |
12949 | if (!symtab_hdr->sh_info) |
12950 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
c152c796 AM |
12951 | |
12952 | if (dynamic | |
64f52338 AM |
12953 | && htab->dynsym != NULL |
12954 | && htab->dynsym->output_section != bfd_abs_section_ptr) | |
c152c796 AM |
12955 | { |
12956 | Elf_Internal_Sym sym; | |
64f52338 | 12957 | bfd_byte *dynsym = htab->dynsym->contents; |
90ac2420 | 12958 | |
64f52338 AM |
12959 | o = htab->dynsym->output_section; |
12960 | elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; | |
c152c796 AM |
12961 | |
12962 | /* Write out the section symbols for the output sections. */ | |
0e1862bb | 12963 | if (bfd_link_pic (info) |
64f52338 | 12964 | || htab->is_relocatable_executable) |
c152c796 AM |
12965 | { |
12966 | asection *s; | |
12967 | ||
12968 | sym.st_size = 0; | |
12969 | sym.st_name = 0; | |
12970 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
12971 | sym.st_other = 0; | |
35fc36a8 | 12972 | sym.st_target_internal = 0; |
c152c796 AM |
12973 | |
12974 | for (s = abfd->sections; s != NULL; s = s->next) | |
12975 | { | |
12976 | int indx; | |
12977 | bfd_byte *dest; | |
12978 | long dynindx; | |
12979 | ||
c152c796 | 12980 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
12981 | if (dynindx <= 0) |
12982 | continue; | |
12983 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
12984 | BFD_ASSERT (indx > 0); |
12985 | sym.st_shndx = indx; | |
c0d5a53d | 12986 | if (! check_dynsym (abfd, &sym)) |
496afd17 | 12987 | { |
0a1b45a2 | 12988 | ret = false; |
496afd17 L |
12989 | goto return_local_hash_table; |
12990 | } | |
c152c796 AM |
12991 | sym.st_value = s->vma; |
12992 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
3d16b64e NA |
12993 | |
12994 | /* Inform the linker of the addition of this symbol. */ | |
12995 | ||
12996 | if (info->callbacks->ctf_new_dynsym) | |
12997 | info->callbacks->ctf_new_dynsym (dynindx, &sym); | |
12998 | ||
c152c796 AM |
12999 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
13000 | } | |
c152c796 AM |
13001 | } |
13002 | ||
13003 | /* Write out the local dynsyms. */ | |
64f52338 | 13004 | if (htab->dynlocal) |
c152c796 AM |
13005 | { |
13006 | struct elf_link_local_dynamic_entry *e; | |
64f52338 | 13007 | for (e = htab->dynlocal; e ; e = e->next) |
c152c796 AM |
13008 | { |
13009 | asection *s; | |
13010 | bfd_byte *dest; | |
13011 | ||
935bd1e0 | 13012 | /* Copy the internal symbol and turn off visibility. |
c152c796 AM |
13013 | Note that we saved a word of storage and overwrote |
13014 | the original st_name with the dynstr_index. */ | |
13015 | sym = e->isym; | |
935bd1e0 | 13016 | sym.st_other &= ~ELF_ST_VISIBILITY (-1); |
4d68fd75 | 13017 | sym.st_shndx = SHN_UNDEF; |
c152c796 | 13018 | |
cb33740c AM |
13019 | s = bfd_section_from_elf_index (e->input_bfd, |
13020 | e->isym.st_shndx); | |
4d68fd75 AM |
13021 | if (s != NULL |
13022 | && s->output_section != NULL | |
13023 | && elf_section_data (s->output_section) != NULL) | |
c152c796 | 13024 | { |
c152c796 AM |
13025 | sym.st_shndx = |
13026 | elf_section_data (s->output_section)->this_idx; | |
c0d5a53d | 13027 | if (! check_dynsym (abfd, &sym)) |
496afd17 | 13028 | { |
0a1b45a2 | 13029 | ret = false; |
496afd17 L |
13030 | goto return_local_hash_table; |
13031 | } | |
c152c796 AM |
13032 | sym.st_value = (s->output_section->vma |
13033 | + s->output_offset | |
13034 | + e->isym.st_value); | |
13035 | } | |
13036 | ||
3d16b64e NA |
13037 | /* Inform the linker of the addition of this symbol. */ |
13038 | ||
13039 | if (info->callbacks->ctf_new_dynsym) | |
13040 | info->callbacks->ctf_new_dynsym (e->dynindx, &sym); | |
13041 | ||
c152c796 AM |
13042 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; |
13043 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
13044 | } | |
13045 | } | |
c152c796 AM |
13046 | } |
13047 | ||
13048 | /* We get the global symbols from the hash table. */ | |
0a1b45a2 AM |
13049 | eoinfo.failed = false; |
13050 | eoinfo.localsyms = false; | |
8b127cbc | 13051 | eoinfo.flinfo = &flinfo; |
7686d77d | 13052 | bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); |
c152c796 | 13053 | if (eoinfo.failed) |
496afd17 | 13054 | { |
0a1b45a2 | 13055 | ret = false; |
496afd17 L |
13056 | goto return_local_hash_table; |
13057 | } | |
c152c796 AM |
13058 | |
13059 | /* If backend needs to output some symbols not present in the hash | |
13060 | table, do it now. */ | |
8539e4e8 AM |
13061 | if (bed->elf_backend_output_arch_syms |
13062 | && (info->strip != strip_all || emit_relocs)) | |
c152c796 | 13063 | { |
c152c796 | 13064 | if (! ((*bed->elf_backend_output_arch_syms) |
37bb890f | 13065 | (abfd, info, &flinfo, elf_link_output_symstrtab))) |
496afd17 | 13066 | { |
0a1b45a2 | 13067 | ret = false; |
496afd17 L |
13068 | goto return_local_hash_table; |
13069 | } | |
c152c796 AM |
13070 | } |
13071 | ||
ef10c3ac L |
13072 | /* Finalize the .strtab section. */ |
13073 | _bfd_elf_strtab_finalize (flinfo.symstrtab); | |
13074 | ||
13075 | /* Swap out the .strtab section. */ | |
13076 | if (!elf_link_swap_symbols_out (&flinfo)) | |
496afd17 | 13077 | { |
0a1b45a2 | 13078 | ret = false; |
496afd17 L |
13079 | goto return_local_hash_table; |
13080 | } | |
c152c796 AM |
13081 | |
13082 | /* Now we know the size of the symtab section. */ | |
c152c796 AM |
13083 | if (bfd_get_symcount (abfd) > 0) |
13084 | { | |
ee3b52e9 L |
13085 | /* Finish up and write out the symbol string table (.strtab) |
13086 | section. */ | |
ad32986f | 13087 | Elf_Internal_Shdr *symstrtab_hdr = NULL; |
8539e4e8 AM |
13088 | file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; |
13089 | ||
ad32986f | 13090 | if (elf_symtab_shndx_list (abfd)) |
8539e4e8 | 13091 | { |
ad32986f | 13092 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8539e4e8 | 13093 | |
ad32986f NC |
13094 | if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) |
13095 | { | |
13096 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
13097 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
13098 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
13099 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
13100 | symtab_shndx_hdr->sh_size = amt; | |
8539e4e8 | 13101 | |
ad32986f | 13102 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, |
0a1b45a2 | 13103 | off, true); |
ad32986f NC |
13104 | |
13105 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
226f9f4f | 13106 | || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt)) |
496afd17 | 13107 | { |
0a1b45a2 | 13108 | ret = false; |
496afd17 L |
13109 | goto return_local_hash_table; |
13110 | } | |
ad32986f | 13111 | } |
8539e4e8 | 13112 | } |
ee3b52e9 L |
13113 | |
13114 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
13115 | /* sh_name was set in prep_headers. */ | |
13116 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 13117 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
ee3b52e9 | 13118 | symstrtab_hdr->sh_addr = 0; |
ef10c3ac | 13119 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); |
ee3b52e9 L |
13120 | symstrtab_hdr->sh_entsize = 0; |
13121 | symstrtab_hdr->sh_link = 0; | |
13122 | symstrtab_hdr->sh_info = 0; | |
13123 | /* sh_offset is set just below. */ | |
13124 | symstrtab_hdr->sh_addralign = 1; | |
13125 | ||
13126 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, | |
0a1b45a2 | 13127 | off, true); |
ee3b52e9 L |
13128 | elf_next_file_pos (abfd) = off; |
13129 | ||
c152c796 | 13130 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 13131 | || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) |
496afd17 | 13132 | { |
0a1b45a2 | 13133 | ret = false; |
496afd17 L |
13134 | goto return_local_hash_table; |
13135 | } | |
c152c796 AM |
13136 | } |
13137 | ||
76359541 TP |
13138 | if (info->out_implib_bfd && !elf_output_implib (abfd, info)) |
13139 | { | |
871b3ab2 | 13140 | _bfd_error_handler (_("%pB: failed to generate import library"), |
4eca0228 | 13141 | info->out_implib_bfd); |
0a1b45a2 | 13142 | ret = false; |
496afd17 | 13143 | goto return_local_hash_table; |
76359541 TP |
13144 | } |
13145 | ||
c152c796 AM |
13146 | /* Adjust the relocs to have the correct symbol indices. */ |
13147 | for (o = abfd->sections; o != NULL; o = o->next) | |
13148 | { | |
d4730f92 | 13149 | struct bfd_elf_section_data *esdo = elf_section_data (o); |
0a1b45a2 | 13150 | bool sort; |
10bbbc1d | 13151 | |
c152c796 AM |
13152 | if ((o->flags & SEC_RELOC) == 0) |
13153 | continue; | |
13154 | ||
28dbcedc | 13155 | sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); |
bca6d0e3 | 13156 | if (esdo->rel.hdr != NULL |
10bbbc1d | 13157 | && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) |
496afd17 | 13158 | { |
0a1b45a2 | 13159 | ret = false; |
496afd17 L |
13160 | goto return_local_hash_table; |
13161 | } | |
bca6d0e3 | 13162 | if (esdo->rela.hdr != NULL |
10bbbc1d | 13163 | && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) |
496afd17 | 13164 | { |
0a1b45a2 | 13165 | ret = false; |
496afd17 L |
13166 | goto return_local_hash_table; |
13167 | } | |
c152c796 AM |
13168 | |
13169 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
13170 | trying to swap the relocs out itself. */ | |
13171 | o->reloc_count = 0; | |
13172 | } | |
13173 | ||
9c3d7d9b | 13174 | relativecount = 0; |
c152c796 AM |
13175 | if (dynamic && info->combreloc && dynobj != NULL) |
13176 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
13177 | ||
9c3d7d9b AM |
13178 | relr_entsize = 0; |
13179 | if (htab->srelrdyn != NULL | |
13180 | && htab->srelrdyn->output_section != NULL | |
13181 | && htab->srelrdyn->size != 0) | |
13182 | { | |
13183 | asection *s = htab->srelrdyn->output_section; | |
13184 | relr_entsize = elf_section_data (s)->this_hdr.sh_entsize; | |
13185 | if (relr_entsize == 0) | |
13186 | { | |
13187 | relr_entsize = bed->s->arch_size / 8; | |
13188 | elf_section_data (s)->this_hdr.sh_entsize = relr_entsize; | |
13189 | } | |
13190 | } | |
13191 | ||
c152c796 AM |
13192 | /* If we are linking against a dynamic object, or generating a |
13193 | shared library, finish up the dynamic linking information. */ | |
13194 | if (dynamic) | |
13195 | { | |
13196 | bfd_byte *dyncon, *dynconend; | |
13197 | ||
13198 | /* Fix up .dynamic entries. */ | |
3d4d4302 | 13199 | o = bfd_get_linker_section (dynobj, ".dynamic"); |
c152c796 AM |
13200 | BFD_ASSERT (o != NULL); |
13201 | ||
13202 | dyncon = o->contents; | |
36f61bf2 | 13203 | dynconend = PTR_ADD (o->contents, o->size); |
c152c796 AM |
13204 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
13205 | { | |
13206 | Elf_Internal_Dyn dyn; | |
13207 | const char *name; | |
13208 | unsigned int type; | |
64487780 AM |
13209 | bfd_size_type sh_size; |
13210 | bfd_vma sh_addr; | |
c152c796 AM |
13211 | |
13212 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
13213 | ||
13214 | switch (dyn.d_tag) | |
13215 | { | |
13216 | default: | |
13217 | continue; | |
13218 | case DT_NULL: | |
9c3d7d9b | 13219 | if (relativecount != 0) |
c152c796 AM |
13220 | { |
13221 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
13222 | { | |
13223 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
13224 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
c152c796 | 13225 | } |
9c3d7d9b AM |
13226 | if (dyn.d_tag != DT_NULL |
13227 | && dynconend - dyncon >= bed->s->sizeof_dyn) | |
13228 | { | |
13229 | dyn.d_un.d_val = relativecount; | |
13230 | relativecount = 0; | |
13231 | break; | |
13232 | } | |
c152c796 | 13233 | relativecount = 0; |
9c3d7d9b AM |
13234 | } |
13235 | if (relr_entsize != 0) | |
13236 | { | |
13237 | if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn) | |
13238 | { | |
13239 | asection *s = htab->srelrdyn; | |
13240 | dyn.d_tag = DT_RELR; | |
13241 | dyn.d_un.d_ptr | |
13242 | = s->output_section->vma + s->output_offset; | |
13243 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
13244 | dyncon += bed->s->sizeof_dyn; | |
13245 | ||
13246 | dyn.d_tag = DT_RELRSZ; | |
13247 | dyn.d_un.d_val = s->size; | |
13248 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
13249 | dyncon += bed->s->sizeof_dyn; | |
13250 | ||
13251 | dyn.d_tag = DT_RELRENT; | |
13252 | dyn.d_un.d_val = relr_entsize; | |
13253 | relr_entsize = 0; | |
13254 | break; | |
13255 | } | |
13256 | relr_entsize = 0; | |
c152c796 AM |
13257 | } |
13258 | continue; | |
13259 | ||
13260 | case DT_INIT: | |
13261 | name = info->init_function; | |
13262 | goto get_sym; | |
13263 | case DT_FINI: | |
13264 | name = info->fini_function; | |
13265 | get_sym: | |
13266 | { | |
13267 | struct elf_link_hash_entry *h; | |
13268 | ||
0a1b45a2 | 13269 | h = elf_link_hash_lookup (htab, name, false, false, true); |
c152c796 AM |
13270 | if (h != NULL |
13271 | && (h->root.type == bfd_link_hash_defined | |
13272 | || h->root.type == bfd_link_hash_defweak)) | |
13273 | { | |
bef26483 | 13274 | dyn.d_un.d_ptr = h->root.u.def.value; |
c152c796 AM |
13275 | o = h->root.u.def.section; |
13276 | if (o->output_section != NULL) | |
bef26483 | 13277 | dyn.d_un.d_ptr += (o->output_section->vma |
c152c796 AM |
13278 | + o->output_offset); |
13279 | else | |
13280 | { | |
13281 | /* The symbol is imported from another shared | |
13282 | library and does not apply to this one. */ | |
bef26483 | 13283 | dyn.d_un.d_ptr = 0; |
c152c796 AM |
13284 | } |
13285 | break; | |
13286 | } | |
13287 | } | |
13288 | continue; | |
13289 | ||
13290 | case DT_PREINIT_ARRAYSZ: | |
13291 | name = ".preinit_array"; | |
4ade44b7 | 13292 | goto get_out_size; |
c152c796 AM |
13293 | case DT_INIT_ARRAYSZ: |
13294 | name = ".init_array"; | |
4ade44b7 | 13295 | goto get_out_size; |
c152c796 AM |
13296 | case DT_FINI_ARRAYSZ: |
13297 | name = ".fini_array"; | |
4ade44b7 | 13298 | get_out_size: |
c152c796 AM |
13299 | o = bfd_get_section_by_name (abfd, name); |
13300 | if (o == NULL) | |
13301 | { | |
4eca0228 | 13302 | _bfd_error_handler |
4ade44b7 | 13303 | (_("could not find section %s"), name); |
c152c796 AM |
13304 | goto error_return; |
13305 | } | |
eea6121a | 13306 | if (o->size == 0) |
4eca0228 | 13307 | _bfd_error_handler |
c152c796 | 13308 | (_("warning: %s section has zero size"), name); |
eea6121a | 13309 | dyn.d_un.d_val = o->size; |
c152c796 AM |
13310 | break; |
13311 | ||
13312 | case DT_PREINIT_ARRAY: | |
13313 | name = ".preinit_array"; | |
4ade44b7 | 13314 | goto get_out_vma; |
c152c796 AM |
13315 | case DT_INIT_ARRAY: |
13316 | name = ".init_array"; | |
4ade44b7 | 13317 | goto get_out_vma; |
c152c796 AM |
13318 | case DT_FINI_ARRAY: |
13319 | name = ".fini_array"; | |
4ade44b7 AM |
13320 | get_out_vma: |
13321 | o = bfd_get_section_by_name (abfd, name); | |
13322 | goto do_vma; | |
c152c796 AM |
13323 | |
13324 | case DT_HASH: | |
13325 | name = ".hash"; | |
13326 | goto get_vma; | |
fdc90cb4 JJ |
13327 | case DT_GNU_HASH: |
13328 | name = ".gnu.hash"; | |
13329 | goto get_vma; | |
c152c796 AM |
13330 | case DT_STRTAB: |
13331 | name = ".dynstr"; | |
13332 | goto get_vma; | |
13333 | case DT_SYMTAB: | |
13334 | name = ".dynsym"; | |
13335 | goto get_vma; | |
13336 | case DT_VERDEF: | |
13337 | name = ".gnu.version_d"; | |
13338 | goto get_vma; | |
13339 | case DT_VERNEED: | |
13340 | name = ".gnu.version_r"; | |
13341 | goto get_vma; | |
13342 | case DT_VERSYM: | |
13343 | name = ".gnu.version"; | |
13344 | get_vma: | |
4ade44b7 AM |
13345 | o = bfd_get_linker_section (dynobj, name); |
13346 | do_vma: | |
b3293efa | 13347 | if (o == NULL || bfd_is_abs_section (o->output_section)) |
c152c796 | 13348 | { |
4eca0228 | 13349 | _bfd_error_handler |
4ade44b7 | 13350 | (_("could not find section %s"), name); |
c152c796 AM |
13351 | goto error_return; |
13352 | } | |
894891db NC |
13353 | if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) |
13354 | { | |
4eca0228 | 13355 | _bfd_error_handler |
894891db NC |
13356 | (_("warning: section '%s' is being made into a note"), name); |
13357 | bfd_set_error (bfd_error_nonrepresentable_section); | |
13358 | goto error_return; | |
13359 | } | |
4ade44b7 | 13360 | dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; |
c152c796 AM |
13361 | break; |
13362 | ||
13363 | case DT_REL: | |
13364 | case DT_RELA: | |
13365 | case DT_RELSZ: | |
13366 | case DT_RELASZ: | |
13367 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
13368 | type = SHT_REL; | |
13369 | else | |
13370 | type = SHT_RELA; | |
64487780 AM |
13371 | sh_size = 0; |
13372 | sh_addr = 0; | |
c152c796 AM |
13373 | for (i = 1; i < elf_numsections (abfd); i++) |
13374 | { | |
13375 | Elf_Internal_Shdr *hdr; | |
13376 | ||
13377 | hdr = elf_elfsections (abfd)[i]; | |
13378 | if (hdr->sh_type == type | |
13379 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
13380 | { | |
64487780 AM |
13381 | sh_size += hdr->sh_size; |
13382 | if (sh_addr == 0 | |
13383 | || sh_addr > hdr->sh_addr) | |
13384 | sh_addr = hdr->sh_addr; | |
c152c796 AM |
13385 | } |
13386 | } | |
64487780 | 13387 | |
64f52338 AM |
13388 | if (bed->dtrel_excludes_plt && htab->srelplt != NULL) |
13389 | { | |
66631823 CE |
13390 | unsigned int opb = bfd_octets_per_byte (abfd, o); |
13391 | ||
64f52338 AM |
13392 | /* Don't count procedure linkage table relocs in the |
13393 | overall reloc count. */ | |
64487780 AM |
13394 | sh_size -= htab->srelplt->size; |
13395 | if (sh_size == 0) | |
13396 | /* If the size is zero, make the address zero too. | |
13397 | This is to avoid a glibc bug. If the backend | |
13398 | emits DT_RELA/DT_RELASZ even when DT_RELASZ is | |
13399 | zero, then we'll put DT_RELA at the end of | |
13400 | DT_JMPREL. glibc will interpret the end of | |
13401 | DT_RELA matching the end of DT_JMPREL as the | |
13402 | case where DT_RELA includes DT_JMPREL, and for | |
13403 | LD_BIND_NOW will decide that processing DT_RELA | |
13404 | will process the PLT relocs too. Net result: | |
13405 | No PLT relocs applied. */ | |
13406 | sh_addr = 0; | |
13407 | ||
64f52338 AM |
13408 | /* If .rela.plt is the first .rela section, exclude |
13409 | it from DT_RELA. */ | |
64487780 | 13410 | else if (sh_addr == (htab->srelplt->output_section->vma |
66631823 | 13411 | + htab->srelplt->output_offset) * opb) |
64487780 | 13412 | sh_addr += htab->srelplt->size; |
64f52338 | 13413 | } |
64487780 AM |
13414 | |
13415 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
13416 | dyn.d_un.d_val = sh_size; | |
13417 | else | |
13418 | dyn.d_un.d_ptr = sh_addr; | |
c152c796 AM |
13419 | break; |
13420 | } | |
13421 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
13422 | } | |
13423 | } | |
13424 | ||
13425 | /* If we have created any dynamic sections, then output them. */ | |
13426 | if (dynobj != NULL) | |
13427 | { | |
13428 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
13429 | goto error_return; | |
13430 | ||
943284cc | 13431 | /* Check for DT_TEXTREL (late, in case the backend removes it). */ |
a6dbf402 | 13432 | if (bfd_link_textrel_check (info) |
36f61bf2 AM |
13433 | && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL |
13434 | && o->size != 0) | |
943284cc DJ |
13435 | { |
13436 | bfd_byte *dyncon, *dynconend; | |
13437 | ||
943284cc DJ |
13438 | dyncon = o->contents; |
13439 | dynconend = o->contents + o->size; | |
13440 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) | |
13441 | { | |
13442 | Elf_Internal_Dyn dyn; | |
13443 | ||
13444 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
13445 | ||
13446 | if (dyn.d_tag == DT_TEXTREL) | |
13447 | { | |
a6dbf402 | 13448 | if (info->textrel_check == textrel_check_error) |
c192a133 | 13449 | info->callbacks->einfo |
9793eb77 | 13450 | (_("%P%X: read-only segment has dynamic relocations\n")); |
a6dbf402 L |
13451 | else if (bfd_link_dll (info)) |
13452 | info->callbacks->einfo | |
13453 | (_("%P: warning: creating DT_TEXTREL in a shared object\n")); | |
83b1d8f4 L |
13454 | else if (bfd_link_pde (info)) |
13455 | info->callbacks->einfo | |
13456 | (_("%P: warning: creating DT_TEXTREL in a PDE\n")); | |
c192a133 AM |
13457 | else |
13458 | info->callbacks->einfo | |
a6dbf402 | 13459 | (_("%P: warning: creating DT_TEXTREL in a PIE\n")); |
943284cc DJ |
13460 | break; |
13461 | } | |
13462 | } | |
13463 | } | |
13464 | ||
c152c796 AM |
13465 | for (o = dynobj->sections; o != NULL; o = o->next) |
13466 | { | |
13467 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 13468 | || o->size == 0 |
c152c796 AM |
13469 | || o->output_section == bfd_abs_section_ptr) |
13470 | continue; | |
13471 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
13472 | { | |
13473 | /* At this point, we are only interested in sections | |
13474 | created by _bfd_elf_link_create_dynamic_sections. */ | |
13475 | continue; | |
13476 | } | |
64f52338 | 13477 | if (htab->stab_info.stabstr == o) |
3722b82f | 13478 | continue; |
64f52338 | 13479 | if (htab->eh_info.hdr_sec == o) |
eea6121a | 13480 | continue; |
3d4d4302 | 13481 | if (strcmp (o->name, ".dynstr") != 0) |
c152c796 | 13482 | { |
bb294208 AM |
13483 | bfd_size_type octets = ((file_ptr) o->output_offset |
13484 | * bfd_octets_per_byte (abfd, o)); | |
13485 | if (!bfd_set_section_contents (abfd, o->output_section, | |
13486 | o->contents, octets, o->size)) | |
c152c796 AM |
13487 | goto error_return; |
13488 | } | |
13489 | else | |
13490 | { | |
13491 | /* The contents of the .dynstr section are actually in a | |
13492 | stringtab. */ | |
8539e4e8 AM |
13493 | file_ptr off; |
13494 | ||
c152c796 AM |
13495 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; |
13496 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
64f52338 | 13497 | || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) |
c152c796 AM |
13498 | goto error_return; |
13499 | } | |
13500 | } | |
13501 | } | |
13502 | ||
7bdf4127 | 13503 | if (!info->resolve_section_groups) |
c152c796 | 13504 | { |
0a1b45a2 | 13505 | bool failed = false; |
c152c796 | 13506 | |
7bdf4127 | 13507 | BFD_ASSERT (bfd_link_relocatable (info)); |
c152c796 AM |
13508 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
13509 | if (failed) | |
13510 | goto error_return; | |
13511 | } | |
13512 | ||
13513 | /* If we have optimized stabs strings, output them. */ | |
64f52338 | 13514 | if (htab->stab_info.stabstr != NULL) |
c152c796 | 13515 | { |
64f52338 | 13516 | if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) |
c152c796 AM |
13517 | goto error_return; |
13518 | } | |
13519 | ||
9f7c3e5e AM |
13520 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) |
13521 | goto error_return; | |
c152c796 | 13522 | |
cf0e0a0b IB |
13523 | if (! _bfd_elf_write_section_sframe (abfd, info)) |
13524 | goto error_return; | |
13525 | ||
1ff6de03 NA |
13526 | if (info->callbacks->emit_ctf) |
13527 | info->callbacks->emit_ctf (); | |
13528 | ||
9f7c3e5e | 13529 | elf_final_link_free (abfd, &flinfo); |
c152c796 | 13530 | |
104d59d1 JM |
13531 | if (attr_section) |
13532 | { | |
a50b1753 | 13533 | bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); |
104d59d1 | 13534 | if (contents == NULL) |
496afd17 L |
13535 | { |
13536 | /* Bail out and fail. */ | |
0a1b45a2 | 13537 | ret = false; |
496afd17 L |
13538 | goto return_local_hash_table; |
13539 | } | |
104d59d1 JM |
13540 | bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); |
13541 | bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); | |
13542 | free (contents); | |
13543 | } | |
13544 | ||
496afd17 L |
13545 | return_local_hash_table: |
13546 | if (info->unique_symbol) | |
13547 | bfd_hash_table_free (&flinfo.local_hash_table); | |
13548 | return ret; | |
c152c796 AM |
13549 | |
13550 | error_return: | |
9f7c3e5e | 13551 | elf_final_link_free (abfd, &flinfo); |
0a1b45a2 | 13552 | ret = false; |
496afd17 | 13553 | goto return_local_hash_table; |
c152c796 AM |
13554 | } |
13555 | \f | |
5241d853 RS |
13556 | /* Initialize COOKIE for input bfd ABFD. */ |
13557 | ||
0a1b45a2 | 13558 | static bool |
5241d853 RS |
13559 | init_reloc_cookie (struct elf_reloc_cookie *cookie, |
13560 | struct bfd_link_info *info, bfd *abfd) | |
13561 | { | |
13562 | Elf_Internal_Shdr *symtab_hdr; | |
13563 | const struct elf_backend_data *bed; | |
13564 | ||
13565 | bed = get_elf_backend_data (abfd); | |
13566 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
13567 | ||
13568 | cookie->abfd = abfd; | |
13569 | cookie->sym_hashes = elf_sym_hashes (abfd); | |
13570 | cookie->bad_symtab = elf_bad_symtab (abfd); | |
13571 | if (cookie->bad_symtab) | |
13572 | { | |
13573 | cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
13574 | cookie->extsymoff = 0; | |
13575 | } | |
13576 | else | |
13577 | { | |
13578 | cookie->locsymcount = symtab_hdr->sh_info; | |
13579 | cookie->extsymoff = symtab_hdr->sh_info; | |
13580 | } | |
13581 | ||
13582 | if (bed->s->arch_size == 32) | |
13583 | cookie->r_sym_shift = 8; | |
13584 | else | |
13585 | cookie->r_sym_shift = 32; | |
13586 | ||
13587 | cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
13588 | if (cookie->locsyms == NULL && cookie->locsymcount != 0) | |
13589 | { | |
13590 | cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
13591 | cookie->locsymcount, 0, | |
13592 | NULL, NULL, NULL); | |
13593 | if (cookie->locsyms == NULL) | |
13594 | { | |
13595 | info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); | |
0a1b45a2 | 13596 | return false; |
5241d853 | 13597 | } |
a8dde0a2 L |
13598 | if (_bfd_link_keep_memory (info) ) |
13599 | { | |
13600 | symtab_hdr->contents = (bfd_byte *) cookie->locsyms; | |
13601 | info->cache_size += (cookie->locsymcount | |
13602 | * sizeof (Elf_External_Sym_Shndx)); | |
13603 | } | |
5241d853 | 13604 | } |
0a1b45a2 | 13605 | return true; |
5241d853 RS |
13606 | } |
13607 | ||
13608 | /* Free the memory allocated by init_reloc_cookie, if appropriate. */ | |
13609 | ||
13610 | static void | |
13611 | fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) | |
13612 | { | |
13613 | Elf_Internal_Shdr *symtab_hdr; | |
13614 | ||
13615 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
c9594989 | 13616 | if (symtab_hdr->contents != (unsigned char *) cookie->locsyms) |
5241d853 RS |
13617 | free (cookie->locsyms); |
13618 | } | |
13619 | ||
13620 | /* Initialize the relocation information in COOKIE for input section SEC | |
13621 | of input bfd ABFD. */ | |
13622 | ||
0a1b45a2 | 13623 | static bool |
5241d853 RS |
13624 | init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, |
13625 | struct bfd_link_info *info, bfd *abfd, | |
13626 | asection *sec) | |
13627 | { | |
5241d853 RS |
13628 | if (sec->reloc_count == 0) |
13629 | { | |
13630 | cookie->rels = NULL; | |
13631 | cookie->relend = NULL; | |
13632 | } | |
13633 | else | |
13634 | { | |
a8dde0a2 L |
13635 | cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec, |
13636 | NULL, NULL, | |
13637 | _bfd_link_keep_memory (info)); | |
5241d853 | 13638 | if (cookie->rels == NULL) |
0a1b45a2 | 13639 | return false; |
5241d853 | 13640 | cookie->rel = cookie->rels; |
056bafd4 | 13641 | cookie->relend = cookie->rels + sec->reloc_count; |
5241d853 RS |
13642 | } |
13643 | cookie->rel = cookie->rels; | |
0a1b45a2 | 13644 | return true; |
5241d853 RS |
13645 | } |
13646 | ||
13647 | /* Free the memory allocated by init_reloc_cookie_rels, | |
13648 | if appropriate. */ | |
13649 | ||
13650 | static void | |
13651 | fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, | |
13652 | asection *sec) | |
13653 | { | |
c9594989 | 13654 | if (elf_section_data (sec)->relocs != cookie->rels) |
5241d853 RS |
13655 | free (cookie->rels); |
13656 | } | |
13657 | ||
13658 | /* Initialize the whole of COOKIE for input section SEC. */ | |
13659 | ||
0a1b45a2 | 13660 | static bool |
5241d853 RS |
13661 | init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, |
13662 | struct bfd_link_info *info, | |
13663 | asection *sec) | |
13664 | { | |
13665 | if (!init_reloc_cookie (cookie, info, sec->owner)) | |
13666 | goto error1; | |
13667 | if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) | |
13668 | goto error2; | |
0a1b45a2 | 13669 | return true; |
5241d853 RS |
13670 | |
13671 | error2: | |
13672 | fini_reloc_cookie (cookie, sec->owner); | |
13673 | error1: | |
0a1b45a2 | 13674 | return false; |
5241d853 RS |
13675 | } |
13676 | ||
13677 | /* Free the memory allocated by init_reloc_cookie_for_section, | |
13678 | if appropriate. */ | |
13679 | ||
13680 | static void | |
13681 | fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, | |
13682 | asection *sec) | |
13683 | { | |
13684 | fini_reloc_cookie_rels (cookie, sec); | |
13685 | fini_reloc_cookie (cookie, sec->owner); | |
13686 | } | |
13687 | \f | |
c152c796 AM |
13688 | /* Garbage collect unused sections. */ |
13689 | ||
07adf181 AM |
13690 | /* Default gc_mark_hook. */ |
13691 | ||
13692 | asection * | |
13693 | _bfd_elf_gc_mark_hook (asection *sec, | |
13694 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13695 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
13696 | struct elf_link_hash_entry *h, | |
13697 | Elf_Internal_Sym *sym) | |
13698 | { | |
13699 | if (h != NULL) | |
13700 | { | |
13701 | switch (h->root.type) | |
13702 | { | |
13703 | case bfd_link_hash_defined: | |
13704 | case bfd_link_hash_defweak: | |
13705 | return h->root.u.def.section; | |
13706 | ||
13707 | case bfd_link_hash_common: | |
13708 | return h->root.u.c.p->section; | |
13709 | ||
13710 | default: | |
13711 | break; | |
13712 | } | |
13713 | } | |
13714 | else | |
13715 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
13716 | ||
13717 | return NULL; | |
13718 | } | |
13719 | ||
9e223787 | 13720 | /* Return the debug definition section. */ |
b7c871ed L |
13721 | |
13722 | static asection * | |
13723 | elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, | |
13724 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
13725 | Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, | |
13726 | struct elf_link_hash_entry *h, | |
9e223787 | 13727 | Elf_Internal_Sym *sym) |
b7c871ed | 13728 | { |
9e223787 L |
13729 | if (h != NULL) |
13730 | { | |
13731 | /* Return the global debug definition section. */ | |
13732 | if ((h->root.type == bfd_link_hash_defined | |
13733 | || h->root.type == bfd_link_hash_defweak) | |
13734 | && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) | |
13735 | return h->root.u.def.section; | |
13736 | } | |
13737 | else | |
13738 | { | |
13739 | /* Return the local debug definition section. */ | |
13740 | asection *isec = bfd_section_from_elf_index (sec->owner, | |
13741 | sym->st_shndx); | |
e7c3fa04 | 13742 | if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0) |
9e223787 L |
13743 | return isec; |
13744 | } | |
b7c871ed L |
13745 | |
13746 | return NULL; | |
13747 | } | |
13748 | ||
5241d853 RS |
13749 | /* COOKIE->rel describes a relocation against section SEC, which is |
13750 | a section we've decided to keep. Return the section that contains | |
13751 | the relocation symbol, or NULL if no section contains it. */ | |
13752 | ||
13753 | asection * | |
13754 | _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, | |
13755 | elf_gc_mark_hook_fn gc_mark_hook, | |
1cce69b9 | 13756 | struct elf_reloc_cookie *cookie, |
0a1b45a2 | 13757 | bool *start_stop) |
5241d853 RS |
13758 | { |
13759 | unsigned long r_symndx; | |
3024a17a | 13760 | struct elf_link_hash_entry *h, *hw; |
5241d853 RS |
13761 | |
13762 | r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; | |
cf35638d | 13763 | if (r_symndx == STN_UNDEF) |
5241d853 RS |
13764 | return NULL; |
13765 | ||
13766 | if (r_symndx >= cookie->locsymcount | |
13767 | || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
13768 | { | |
0a1b45a2 | 13769 | bool was_marked; |
5789f845 | 13770 | |
5241d853 | 13771 | h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; |
263ddf68 L |
13772 | if (h == NULL) |
13773 | { | |
871b3ab2 | 13774 | info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"), |
263ddf68 L |
13775 | sec->owner); |
13776 | return NULL; | |
13777 | } | |
5241d853 RS |
13778 | while (h->root.type == bfd_link_hash_indirect |
13779 | || h->root.type == bfd_link_hash_warning) | |
13780 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
5789f845 AM |
13781 | |
13782 | was_marked = h->mark; | |
1d5316ab | 13783 | h->mark = 1; |
3024a17a AM |
13784 | /* Keep all aliases of the symbol too. If an object symbol |
13785 | needs to be copied into .dynbss then all of its aliases | |
13786 | should be present as dynamic symbols, not just the one used | |
13787 | on the copy relocation. */ | |
13788 | hw = h; | |
13789 | while (hw->is_weakalias) | |
13790 | { | |
13791 | hw = hw->u.alias; | |
13792 | hw->mark = 1; | |
13793 | } | |
1cce69b9 | 13794 | |
5789f845 | 13795 | if (!was_marked && h->start_stop && !h->root.ldscript_def) |
1cce69b9 | 13796 | { |
8ee10e86 AM |
13797 | if (info->start_stop_gc) |
13798 | return NULL; | |
13799 | ||
7dba9362 AM |
13800 | /* To work around a glibc bug, mark XXX input sections |
13801 | when there is a reference to __start_XXX or __stop_XXX | |
13802 | symbols. */ | |
8ee10e86 | 13803 | else if (start_stop != NULL) |
1cce69b9 | 13804 | { |
cbd0eecf | 13805 | asection *s = h->u2.start_stop_section; |
0a1b45a2 | 13806 | *start_stop = true; |
a6a4679f | 13807 | return s; |
1cce69b9 AM |
13808 | } |
13809 | } | |
13810 | ||
5241d853 RS |
13811 | return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); |
13812 | } | |
13813 | ||
13814 | return (*gc_mark_hook) (sec, info, cookie->rel, NULL, | |
13815 | &cookie->locsyms[r_symndx]); | |
13816 | } | |
13817 | ||
13818 | /* COOKIE->rel describes a relocation against section SEC, which is | |
13819 | a section we've decided to keep. Mark the section that contains | |
9d0a14d3 | 13820 | the relocation symbol. */ |
5241d853 | 13821 | |
0a1b45a2 | 13822 | bool |
5241d853 RS |
13823 | _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, |
13824 | asection *sec, | |
13825 | elf_gc_mark_hook_fn gc_mark_hook, | |
9d0a14d3 | 13826 | struct elf_reloc_cookie *cookie) |
5241d853 RS |
13827 | { |
13828 | asection *rsec; | |
0a1b45a2 | 13829 | bool start_stop = false; |
5241d853 | 13830 | |
1cce69b9 AM |
13831 | rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); |
13832 | while (rsec != NULL) | |
5241d853 | 13833 | { |
1cce69b9 AM |
13834 | if (!rsec->gc_mark) |
13835 | { | |
13836 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour | |
13837 | || (rsec->owner->flags & DYNAMIC) != 0) | |
13838 | rsec->gc_mark = 1; | |
13839 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) | |
0a1b45a2 | 13840 | return false; |
1cce69b9 AM |
13841 | } |
13842 | if (!start_stop) | |
13843 | break; | |
199af150 | 13844 | rsec = bfd_get_next_section_by_name (rsec->owner, rsec); |
5241d853 | 13845 | } |
0a1b45a2 | 13846 | return true; |
5241d853 RS |
13847 | } |
13848 | ||
07adf181 AM |
13849 | /* The mark phase of garbage collection. For a given section, mark |
13850 | it and any sections in this section's group, and all the sections | |
13851 | which define symbols to which it refers. */ | |
13852 | ||
0a1b45a2 | 13853 | bool |
ccfa59ea AM |
13854 | _bfd_elf_gc_mark (struct bfd_link_info *info, |
13855 | asection *sec, | |
6a5bb875 | 13856 | elf_gc_mark_hook_fn gc_mark_hook) |
c152c796 | 13857 | { |
0a1b45a2 | 13858 | bool ret; |
9d0a14d3 | 13859 | asection *group_sec, *eh_frame; |
c152c796 AM |
13860 | |
13861 | sec->gc_mark = 1; | |
13862 | ||
13863 | /* Mark all the sections in the group. */ | |
13864 | group_sec = elf_section_data (sec)->next_in_group; | |
13865 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 13866 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
0a1b45a2 | 13867 | return false; |
c152c796 AM |
13868 | |
13869 | /* Look through the section relocs. */ | |
0a1b45a2 | 13870 | ret = true; |
9d0a14d3 RS |
13871 | eh_frame = elf_eh_frame_section (sec->owner); |
13872 | if ((sec->flags & SEC_RELOC) != 0 | |
13873 | && sec->reloc_count > 0 | |
13874 | && sec != eh_frame) | |
c152c796 | 13875 | { |
5241d853 | 13876 | struct elf_reloc_cookie cookie; |
c152c796 | 13877 | |
5241d853 | 13878 | if (!init_reloc_cookie_for_section (&cookie, info, sec)) |
0a1b45a2 | 13879 | ret = false; |
c152c796 | 13880 | else |
c152c796 | 13881 | { |
5241d853 | 13882 | for (; cookie.rel < cookie.relend; cookie.rel++) |
9d0a14d3 | 13883 | if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) |
5241d853 | 13884 | { |
0a1b45a2 | 13885 | ret = false; |
5241d853 RS |
13886 | break; |
13887 | } | |
13888 | fini_reloc_cookie_for_section (&cookie, sec); | |
c152c796 AM |
13889 | } |
13890 | } | |
9d0a14d3 RS |
13891 | |
13892 | if (ret && eh_frame && elf_fde_list (sec)) | |
13893 | { | |
13894 | struct elf_reloc_cookie cookie; | |
13895 | ||
13896 | if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) | |
0a1b45a2 | 13897 | ret = false; |
9d0a14d3 RS |
13898 | else |
13899 | { | |
13900 | if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, | |
13901 | gc_mark_hook, &cookie)) | |
0a1b45a2 | 13902 | ret = false; |
9d0a14d3 RS |
13903 | fini_reloc_cookie_for_section (&cookie, eh_frame); |
13904 | } | |
13905 | } | |
13906 | ||
2f0c68f2 CM |
13907 | eh_frame = elf_section_eh_frame_entry (sec); |
13908 | if (ret && eh_frame && !eh_frame->gc_mark) | |
13909 | if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) | |
0a1b45a2 | 13910 | ret = false; |
2f0c68f2 | 13911 | |
c152c796 AM |
13912 | return ret; |
13913 | } | |
13914 | ||
3c758495 TG |
13915 | /* Scan and mark sections in a special or debug section group. */ |
13916 | ||
13917 | static void | |
13918 | _bfd_elf_gc_mark_debug_special_section_group (asection *grp) | |
13919 | { | |
13920 | /* Point to first section of section group. */ | |
13921 | asection *ssec; | |
13922 | /* Used to iterate the section group. */ | |
13923 | asection *msec; | |
13924 | ||
0a1b45a2 AM |
13925 | bool is_special_grp = true; |
13926 | bool is_debug_grp = true; | |
3c758495 TG |
13927 | |
13928 | /* First scan to see if group contains any section other than debug | |
13929 | and special section. */ | |
13930 | ssec = msec = elf_next_in_group (grp); | |
13931 | do | |
13932 | { | |
13933 | if ((msec->flags & SEC_DEBUGGING) == 0) | |
0a1b45a2 | 13934 | is_debug_grp = false; |
3c758495 TG |
13935 | |
13936 | if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) | |
0a1b45a2 | 13937 | is_special_grp = false; |
3c758495 TG |
13938 | |
13939 | msec = elf_next_in_group (msec); | |
13940 | } | |
13941 | while (msec != ssec); | |
13942 | ||
13943 | /* If this is a pure debug section group or pure special section group, | |
13944 | keep all sections in this group. */ | |
13945 | if (is_debug_grp || is_special_grp) | |
13946 | { | |
13947 | do | |
13948 | { | |
13949 | msec->gc_mark = 1; | |
13950 | msec = elf_next_in_group (msec); | |
13951 | } | |
13952 | while (msec != ssec); | |
13953 | } | |
13954 | } | |
13955 | ||
7f6ab9f8 AM |
13956 | /* Keep debug and special sections. */ |
13957 | ||
0a1b45a2 | 13958 | bool |
7f6ab9f8 | 13959 | _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, |
b7d07216 | 13960 | elf_gc_mark_hook_fn mark_hook) |
7f6ab9f8 AM |
13961 | { |
13962 | bfd *ibfd; | |
13963 | ||
c72f2fb2 | 13964 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
7f6ab9f8 AM |
13965 | { |
13966 | asection *isec; | |
0a1b45a2 AM |
13967 | bool some_kept; |
13968 | bool debug_frag_seen; | |
13969 | bool has_kept_debug_info; | |
7f6ab9f8 AM |
13970 | |
13971 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
13972 | continue; | |
57963c05 AM |
13973 | isec = ibfd->sections; |
13974 | if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
13975 | continue; | |
7f6ab9f8 | 13976 | |
b40bf0a2 NC |
13977 | /* Ensure all linker created sections are kept, |
13978 | see if any other section is already marked, | |
13979 | and note if we have any fragmented debug sections. */ | |
0a1b45a2 | 13980 | debug_frag_seen = some_kept = has_kept_debug_info = false; |
7f6ab9f8 AM |
13981 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
13982 | { | |
13983 | if ((isec->flags & SEC_LINKER_CREATED) != 0) | |
13984 | isec->gc_mark = 1; | |
eb026f09 AM |
13985 | else if (isec->gc_mark |
13986 | && (isec->flags & SEC_ALLOC) != 0 | |
13987 | && elf_section_type (isec) != SHT_NOTE) | |
0a1b45a2 | 13988 | some_kept = true; |
b7d07216 L |
13989 | else |
13990 | { | |
13991 | /* Since all sections, except for backend specific ones, | |
13992 | have been garbage collected, call mark_hook on this | |
13993 | section if any of its linked-to sections is marked. */ | |
def97fb9 AM |
13994 | asection *linked_to_sec; |
13995 | for (linked_to_sec = elf_linked_to_section (isec); | |
13996 | linked_to_sec != NULL && !linked_to_sec->linker_mark; | |
b7d07216 | 13997 | linked_to_sec = elf_linked_to_section (linked_to_sec)) |
def97fb9 AM |
13998 | { |
13999 | if (linked_to_sec->gc_mark) | |
14000 | { | |
14001 | if (!_bfd_elf_gc_mark (info, isec, mark_hook)) | |
0a1b45a2 | 14002 | return false; |
def97fb9 AM |
14003 | break; |
14004 | } | |
14005 | linked_to_sec->linker_mark = 1; | |
14006 | } | |
14007 | for (linked_to_sec = elf_linked_to_section (isec); | |
14008 | linked_to_sec != NULL && linked_to_sec->linker_mark; | |
14009 | linked_to_sec = elf_linked_to_section (linked_to_sec)) | |
14010 | linked_to_sec->linker_mark = 0; | |
b7d07216 | 14011 | } |
b40bf0a2 | 14012 | |
535b785f | 14013 | if (!debug_frag_seen |
b40bf0a2 | 14014 | && (isec->flags & SEC_DEBUGGING) |
08dedd66 | 14015 | && startswith (isec->name, ".debug_line.")) |
0a1b45a2 | 14016 | debug_frag_seen = true; |
5242a0a0 L |
14017 | else if (strcmp (bfd_section_name (isec), |
14018 | "__patchable_function_entries") == 0 | |
14019 | && elf_linked_to_section (isec) == NULL) | |
14020 | info->callbacks->einfo (_("%F%P: %pB(%pA): error: " | |
14021 | "need linked-to section " | |
14022 | "for --gc-sections\n"), | |
14023 | isec->owner, isec); | |
7f6ab9f8 AM |
14024 | } |
14025 | ||
eb026f09 AM |
14026 | /* If no non-note alloc section in this file will be kept, then |
14027 | we can toss out the debug and special sections. */ | |
7f6ab9f8 AM |
14028 | if (!some_kept) |
14029 | continue; | |
14030 | ||
14031 | /* Keep debug and special sections like .comment when they are | |
3c758495 | 14032 | not part of a group. Also keep section groups that contain |
b7d07216 L |
14033 | just debug sections or special sections. NB: Sections with |
14034 | linked-to section has been handled above. */ | |
7f6ab9f8 | 14035 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
3c758495 TG |
14036 | { |
14037 | if ((isec->flags & SEC_GROUP) != 0) | |
14038 | _bfd_elf_gc_mark_debug_special_section_group (isec); | |
14039 | else if (((isec->flags & SEC_DEBUGGING) != 0 | |
14040 | || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) | |
b7d07216 L |
14041 | && elf_next_in_group (isec) == NULL |
14042 | && elf_linked_to_section (isec) == NULL) | |
3c758495 | 14043 | isec->gc_mark = 1; |
b7c871ed | 14044 | if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) |
0a1b45a2 | 14045 | has_kept_debug_info = true; |
3c758495 | 14046 | } |
b40bf0a2 | 14047 | |
b40bf0a2 NC |
14048 | /* Look for CODE sections which are going to be discarded, |
14049 | and find and discard any fragmented debug sections which | |
14050 | are associated with that code section. */ | |
b7c871ed L |
14051 | if (debug_frag_seen) |
14052 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
14053 | if ((isec->flags & SEC_CODE) != 0 | |
14054 | && isec->gc_mark == 0) | |
14055 | { | |
14056 | unsigned int ilen; | |
14057 | asection *dsec; | |
b40bf0a2 | 14058 | |
b7c871ed | 14059 | ilen = strlen (isec->name); |
b40bf0a2 | 14060 | |
b7c871ed | 14061 | /* Association is determined by the name of the debug |
07d6d2b8 | 14062 | section containing the name of the code section as |
b7c871ed L |
14063 | a suffix. For example .debug_line.text.foo is a |
14064 | debug section associated with .text.foo. */ | |
14065 | for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) | |
14066 | { | |
14067 | unsigned int dlen; | |
b40bf0a2 | 14068 | |
b7c871ed L |
14069 | if (dsec->gc_mark == 0 |
14070 | || (dsec->flags & SEC_DEBUGGING) == 0) | |
14071 | continue; | |
b40bf0a2 | 14072 | |
b7c871ed | 14073 | dlen = strlen (dsec->name); |
b40bf0a2 | 14074 | |
b7c871ed L |
14075 | if (dlen > ilen |
14076 | && strncmp (dsec->name + (dlen - ilen), | |
14077 | isec->name, ilen) == 0) | |
b40bf0a2 | 14078 | dsec->gc_mark = 0; |
b7c871ed | 14079 | } |
b40bf0a2 | 14080 | } |
b7c871ed L |
14081 | |
14082 | /* Mark debug sections referenced by kept debug sections. */ | |
14083 | if (has_kept_debug_info) | |
14084 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
14085 | if (isec->gc_mark | |
14086 | && (isec->flags & SEC_DEBUGGING) != 0) | |
14087 | if (!_bfd_elf_gc_mark (info, isec, | |
14088 | elf_gc_mark_debug_section)) | |
0a1b45a2 | 14089 | return false; |
7f6ab9f8 | 14090 | } |
0a1b45a2 | 14091 | return true; |
7f6ab9f8 AM |
14092 | } |
14093 | ||
0a1b45a2 | 14094 | static bool |
ccabcbe5 | 14095 | elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) |
c152c796 AM |
14096 | { |
14097 | bfd *sub; | |
ccabcbe5 | 14098 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
c152c796 | 14099 | |
c72f2fb2 | 14100 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
14101 | { |
14102 | asection *o; | |
14103 | ||
b19a8f85 | 14104 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 14105 | || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) |
b19a8f85 | 14106 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 | 14107 | continue; |
57963c05 AM |
14108 | o = sub->sections; |
14109 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14110 | continue; | |
c152c796 AM |
14111 | |
14112 | for (o = sub->sections; o != NULL; o = o->next) | |
14113 | { | |
a33dafc3 L |
14114 | /* When any section in a section group is kept, we keep all |
14115 | sections in the section group. If the first member of | |
14116 | the section group is excluded, we will also exclude the | |
14117 | group section. */ | |
14118 | if (o->flags & SEC_GROUP) | |
14119 | { | |
14120 | asection *first = elf_next_in_group (o); | |
14121 | o->gc_mark = first->gc_mark; | |
14122 | } | |
c152c796 | 14123 | |
1e7eae0d | 14124 | if (o->gc_mark) |
c152c796 AM |
14125 | continue; |
14126 | ||
14127 | /* Skip sweeping sections already excluded. */ | |
14128 | if (o->flags & SEC_EXCLUDE) | |
14129 | continue; | |
14130 | ||
14131 | /* Since this is early in the link process, it is simple | |
14132 | to remove a section from the output. */ | |
14133 | o->flags |= SEC_EXCLUDE; | |
14134 | ||
c55fe096 | 14135 | if (info->print_gc_sections && o->size != 0) |
695344c0 | 14136 | /* xgettext:c-format */ |
9793eb77 | 14137 | _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), |
c08bb8dd | 14138 | o, sub); |
c152c796 AM |
14139 | } |
14140 | } | |
14141 | ||
0a1b45a2 | 14142 | return true; |
c152c796 AM |
14143 | } |
14144 | ||
14145 | /* Propagate collected vtable information. This is called through | |
14146 | elf_link_hash_traverse. */ | |
14147 | ||
0a1b45a2 | 14148 | static bool |
c152c796 AM |
14149 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) |
14150 | { | |
c152c796 | 14151 | /* Those that are not vtables. */ |
cbd0eecf L |
14152 | if (h->start_stop |
14153 | || h->u2.vtable == NULL | |
14154 | || h->u2.vtable->parent == NULL) | |
0a1b45a2 | 14155 | return true; |
c152c796 AM |
14156 | |
14157 | /* Those vtables that do not have parents, we cannot merge. */ | |
cbd0eecf | 14158 | if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) |
0a1b45a2 | 14159 | return true; |
c152c796 AM |
14160 | |
14161 | /* If we've already been done, exit. */ | |
cbd0eecf | 14162 | if (h->u2.vtable->used && h->u2.vtable->used[-1]) |
0a1b45a2 | 14163 | return true; |
c152c796 AM |
14164 | |
14165 | /* Make sure the parent's table is up to date. */ | |
cbd0eecf | 14166 | elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); |
c152c796 | 14167 | |
cbd0eecf | 14168 | if (h->u2.vtable->used == NULL) |
c152c796 AM |
14169 | { |
14170 | /* None of this table's entries were referenced. Re-use the | |
14171 | parent's table. */ | |
cbd0eecf L |
14172 | h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; |
14173 | h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; | |
c152c796 AM |
14174 | } |
14175 | else | |
14176 | { | |
14177 | size_t n; | |
0a1b45a2 | 14178 | bool *cu, *pu; |
c152c796 AM |
14179 | |
14180 | /* Or the parent's entries into ours. */ | |
cbd0eecf | 14181 | cu = h->u2.vtable->used; |
0a1b45a2 | 14182 | cu[-1] = true; |
cbd0eecf | 14183 | pu = h->u2.vtable->parent->u2.vtable->used; |
c152c796 AM |
14184 | if (pu != NULL) |
14185 | { | |
14186 | const struct elf_backend_data *bed; | |
14187 | unsigned int log_file_align; | |
14188 | ||
14189 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
14190 | log_file_align = bed->s->log_file_align; | |
cbd0eecf | 14191 | n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; |
c152c796 AM |
14192 | while (n--) |
14193 | { | |
14194 | if (*pu) | |
0a1b45a2 | 14195 | *cu = true; |
c152c796 AM |
14196 | pu++; |
14197 | cu++; | |
14198 | } | |
14199 | } | |
14200 | } | |
14201 | ||
0a1b45a2 | 14202 | return true; |
c152c796 AM |
14203 | } |
14204 | ||
a8dde0a2 L |
14205 | struct link_info_ok |
14206 | { | |
14207 | struct bfd_link_info *info; | |
14208 | bool ok; | |
14209 | }; | |
14210 | ||
0a1b45a2 | 14211 | static bool |
a8dde0a2 L |
14212 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, |
14213 | void *ptr) | |
c152c796 AM |
14214 | { |
14215 | asection *sec; | |
14216 | bfd_vma hstart, hend; | |
14217 | Elf_Internal_Rela *relstart, *relend, *rel; | |
14218 | const struct elf_backend_data *bed; | |
14219 | unsigned int log_file_align; | |
a8dde0a2 | 14220 | struct link_info_ok *info = (struct link_info_ok *) ptr; |
c152c796 | 14221 | |
c152c796 AM |
14222 | /* Take care of both those symbols that do not describe vtables as |
14223 | well as those that are not loaded. */ | |
cbd0eecf L |
14224 | if (h->start_stop |
14225 | || h->u2.vtable == NULL | |
14226 | || h->u2.vtable->parent == NULL) | |
0a1b45a2 | 14227 | return true; |
c152c796 AM |
14228 | |
14229 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
14230 | || h->root.type == bfd_link_hash_defweak); | |
14231 | ||
14232 | sec = h->root.u.def.section; | |
14233 | hstart = h->root.u.def.value; | |
14234 | hend = hstart + h->size; | |
14235 | ||
a8dde0a2 L |
14236 | relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info, |
14237 | sec, NULL, NULL, true); | |
c152c796 | 14238 | if (!relstart) |
a8dde0a2 | 14239 | return info->ok = false; |
c152c796 AM |
14240 | bed = get_elf_backend_data (sec->owner); |
14241 | log_file_align = bed->s->log_file_align; | |
14242 | ||
056bafd4 | 14243 | relend = relstart + sec->reloc_count; |
c152c796 AM |
14244 | |
14245 | for (rel = relstart; rel < relend; ++rel) | |
14246 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
14247 | { | |
14248 | /* If the entry is in use, do nothing. */ | |
cbd0eecf L |
14249 | if (h->u2.vtable->used |
14250 | && (rel->r_offset - hstart) < h->u2.vtable->size) | |
c152c796 AM |
14251 | { |
14252 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
cbd0eecf | 14253 | if (h->u2.vtable->used[entry]) |
c152c796 AM |
14254 | continue; |
14255 | } | |
14256 | /* Otherwise, kill it. */ | |
14257 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
14258 | } | |
14259 | ||
0a1b45a2 | 14260 | return true; |
c152c796 AM |
14261 | } |
14262 | ||
87538722 AM |
14263 | /* Mark sections containing dynamically referenced symbols. When |
14264 | building shared libraries, we must assume that any visible symbol is | |
14265 | referenced. */ | |
715df9b8 | 14266 | |
0a1b45a2 | 14267 | bool |
64d03ab5 | 14268 | bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) |
715df9b8 | 14269 | { |
87538722 | 14270 | struct bfd_link_info *info = (struct bfd_link_info *) inf; |
d6f6f455 | 14271 | struct bfd_elf_dynamic_list *d = info->dynamic_list; |
87538722 | 14272 | |
715df9b8 EB |
14273 | if ((h->root.type == bfd_link_hash_defined |
14274 | || h->root.type == bfd_link_hash_defweak) | |
8ee10e86 AM |
14275 | && (!h->start_stop |
14276 | || h->root.ldscript_def | |
14277 | || !info->start_stop_gc) | |
d664fd41 | 14278 | && ((h->ref_dynamic && !h->forced_local) |
c4621b33 | 14279 | || ((h->def_regular || ELF_COMMON_DEF_P (h)) |
87538722 | 14280 | && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL |
fd91d419 | 14281 | && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN |
0e1862bb | 14282 | && (!bfd_link_executable (info) |
22185505 | 14283 | || info->gc_keep_exported |
b407645f AM |
14284 | || info->export_dynamic |
14285 | || (h->dynamic | |
14286 | && d != NULL | |
14287 | && (*d->match) (&d->head, NULL, h->root.root.string))) | |
422f1182 | 14288 | && (h->versioned >= versioned |
54e8959c L |
14289 | || !bfd_hide_sym_by_version (info->version_info, |
14290 | h->root.root.string))))) | |
715df9b8 EB |
14291 | h->root.u.def.section->flags |= SEC_KEEP; |
14292 | ||
0a1b45a2 | 14293 | return true; |
715df9b8 | 14294 | } |
3b36f7e6 | 14295 | |
74f0fb50 AM |
14296 | /* Keep all sections containing symbols undefined on the command-line, |
14297 | and the section containing the entry symbol. */ | |
14298 | ||
14299 | void | |
14300 | _bfd_elf_gc_keep (struct bfd_link_info *info) | |
14301 | { | |
14302 | struct bfd_sym_chain *sym; | |
14303 | ||
14304 | for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) | |
14305 | { | |
14306 | struct elf_link_hash_entry *h; | |
14307 | ||
14308 | h = elf_link_hash_lookup (elf_hash_table (info), sym->name, | |
0a1b45a2 | 14309 | false, false, false); |
74f0fb50 AM |
14310 | |
14311 | if (h != NULL | |
14312 | && (h->root.type == bfd_link_hash_defined | |
14313 | || h->root.type == bfd_link_hash_defweak) | |
2f5541f3 | 14314 | && !bfd_is_const_section (h->root.u.def.section)) |
74f0fb50 AM |
14315 | h->root.u.def.section->flags |= SEC_KEEP; |
14316 | } | |
14317 | } | |
14318 | ||
0a1b45a2 | 14319 | bool |
2f0c68f2 CM |
14320 | bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, |
14321 | struct bfd_link_info *info) | |
14322 | { | |
14323 | bfd *ibfd = info->input_bfds; | |
14324 | ||
14325 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) | |
14326 | { | |
14327 | asection *sec; | |
14328 | struct elf_reloc_cookie cookie; | |
14329 | ||
14330 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
14331 | continue; | |
57963c05 AM |
14332 | sec = ibfd->sections; |
14333 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14334 | continue; | |
2f0c68f2 CM |
14335 | |
14336 | if (!init_reloc_cookie (&cookie, info, ibfd)) | |
0a1b45a2 | 14337 | return false; |
2f0c68f2 CM |
14338 | |
14339 | for (sec = ibfd->sections; sec; sec = sec->next) | |
14340 | { | |
08dedd66 | 14341 | if (startswith (bfd_section_name (sec), ".eh_frame_entry") |
2f0c68f2 CM |
14342 | && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) |
14343 | { | |
14344 | _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); | |
14345 | fini_reloc_cookie_rels (&cookie, sec); | |
14346 | } | |
14347 | } | |
14348 | } | |
0a1b45a2 | 14349 | return true; |
2f0c68f2 CM |
14350 | } |
14351 | ||
c152c796 AM |
14352 | /* Do mark and sweep of unused sections. */ |
14353 | ||
0a1b45a2 | 14354 | bool |
c152c796 AM |
14355 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) |
14356 | { | |
0a1b45a2 | 14357 | bool ok = true; |
c152c796 | 14358 | bfd *sub; |
6a5bb875 | 14359 | elf_gc_mark_hook_fn gc_mark_hook; |
64d03ab5 | 14360 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
da44f4e5 | 14361 | struct elf_link_hash_table *htab; |
a8dde0a2 | 14362 | struct link_info_ok info_ok; |
c152c796 | 14363 | |
64d03ab5 | 14364 | if (!bed->can_gc_sections |
715df9b8 | 14365 | || !is_elf_hash_table (info->hash)) |
c152c796 | 14366 | { |
9793eb77 | 14367 | _bfd_error_handler(_("warning: gc-sections option ignored")); |
0a1b45a2 | 14368 | return true; |
c152c796 AM |
14369 | } |
14370 | ||
74f0fb50 | 14371 | bed->gc_keep (info); |
da44f4e5 | 14372 | htab = elf_hash_table (info); |
74f0fb50 | 14373 | |
9d0a14d3 RS |
14374 | /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section |
14375 | at the .eh_frame section if we can mark the FDEs individually. */ | |
2f0c68f2 CM |
14376 | for (sub = info->input_bfds; |
14377 | info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; | |
14378 | sub = sub->link.next) | |
9d0a14d3 RS |
14379 | { |
14380 | asection *sec; | |
14381 | struct elf_reloc_cookie cookie; | |
14382 | ||
57963c05 AM |
14383 | sec = sub->sections; |
14384 | if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14385 | continue; | |
9d0a14d3 | 14386 | sec = bfd_get_section_by_name (sub, ".eh_frame"); |
9a2a56cc | 14387 | while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) |
9d0a14d3 RS |
14388 | { |
14389 | _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); | |
9a2a56cc AM |
14390 | if (elf_section_data (sec)->sec_info |
14391 | && (sec->flags & SEC_LINKER_CREATED) == 0) | |
9d0a14d3 RS |
14392 | elf_eh_frame_section (sub) = sec; |
14393 | fini_reloc_cookie_for_section (&cookie, sec); | |
199af150 | 14394 | sec = bfd_get_next_section_by_name (NULL, sec); |
9d0a14d3 RS |
14395 | } |
14396 | } | |
9d0a14d3 | 14397 | |
c152c796 | 14398 | /* Apply transitive closure to the vtable entry usage info. */ |
da44f4e5 | 14399 | elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); |
c152c796 | 14400 | if (!ok) |
0a1b45a2 | 14401 | return false; |
c152c796 AM |
14402 | |
14403 | /* Kill the vtable relocations that were not used. */ | |
a8dde0a2 L |
14404 | info_ok.info = info; |
14405 | info_ok.ok = true; | |
14406 | elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok); | |
14407 | if (!info_ok.ok) | |
0a1b45a2 | 14408 | return false; |
c152c796 | 14409 | |
715df9b8 | 14410 | /* Mark dynamically referenced symbols. */ |
22185505 | 14411 | if (htab->dynamic_sections_created || info->gc_keep_exported) |
da44f4e5 | 14412 | elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); |
c152c796 | 14413 | |
715df9b8 | 14414 | /* Grovel through relocs to find out who stays ... */ |
64d03ab5 | 14415 | gc_mark_hook = bed->gc_mark_hook; |
c72f2fb2 | 14416 | for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) |
c152c796 AM |
14417 | { |
14418 | asection *o; | |
14419 | ||
b19a8f85 | 14420 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour |
81742b83 | 14421 | || elf_object_id (sub) != elf_hash_table_id (htab) |
b19a8f85 | 14422 | || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) |
c152c796 AM |
14423 | continue; |
14424 | ||
57963c05 AM |
14425 | o = sub->sections; |
14426 | if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
14427 | continue; | |
14428 | ||
7f6ab9f8 AM |
14429 | /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). |
14430 | Also treat note sections as a root, if the section is not part | |
8b6f4cd3 L |
14431 | of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as |
14432 | well as FINI_ARRAY sections for ld -r. */ | |
c152c796 | 14433 | for (o = sub->sections; o != NULL; o = o->next) |
7f6ab9f8 AM |
14434 | if (!o->gc_mark |
14435 | && (o->flags & SEC_EXCLUDE) == 0 | |
24007750 | 14436 | && ((o->flags & SEC_KEEP) != 0 |
8b6f4cd3 L |
14437 | || (bfd_link_relocatable (info) |
14438 | && ((elf_section_data (o)->this_hdr.sh_type | |
14439 | == SHT_PREINIT_ARRAY) | |
14440 | || (elf_section_data (o)->this_hdr.sh_type | |
14441 | == SHT_INIT_ARRAY) | |
14442 | || (elf_section_data (o)->this_hdr.sh_type | |
14443 | == SHT_FINI_ARRAY))) | |
7f6ab9f8 | 14444 | || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE |
7026832e | 14445 | && elf_next_in_group (o) == NULL |
99fabbc9 JL |
14446 | && elf_linked_to_section (o) == NULL) |
14447 | || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain) | |
14448 | && (elf_section_flags (o) & SHF_GNU_RETAIN)))) | |
7f6ab9f8 AM |
14449 | { |
14450 | if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) | |
0a1b45a2 | 14451 | return false; |
7f6ab9f8 | 14452 | } |
c152c796 AM |
14453 | } |
14454 | ||
6a5bb875 | 14455 | /* Allow the backend to mark additional target specific sections. */ |
7f6ab9f8 | 14456 | bed->gc_mark_extra_sections (info, gc_mark_hook); |
6a5bb875 | 14457 | |
c152c796 | 14458 | /* ... and mark SEC_EXCLUDE for those that go. */ |
ccabcbe5 | 14459 | return elf_gc_sweep (abfd, info); |
c152c796 AM |
14460 | } |
14461 | \f | |
14462 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
14463 | ||
0a1b45a2 | 14464 | bool |
c152c796 AM |
14465 | bfd_elf_gc_record_vtinherit (bfd *abfd, |
14466 | asection *sec, | |
14467 | struct elf_link_hash_entry *h, | |
14468 | bfd_vma offset) | |
14469 | { | |
14470 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
14471 | struct elf_link_hash_entry **search, *child; | |
ef53be89 | 14472 | size_t extsymcount; |
c152c796 AM |
14473 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
14474 | ||
14475 | /* The sh_info field of the symtab header tells us where the | |
14476 | external symbols start. We don't care about the local symbols at | |
14477 | this point. */ | |
14478 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
14479 | if (!elf_bad_symtab (abfd)) | |
14480 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
14481 | ||
14482 | sym_hashes = elf_sym_hashes (abfd); | |
36f61bf2 | 14483 | sym_hashes_end = PTR_ADD (sym_hashes, extsymcount); |
c152c796 AM |
14484 | |
14485 | /* Hunt down the child symbol, which is in this section at the same | |
14486 | offset as the relocation. */ | |
14487 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
14488 | { | |
14489 | if ((child = *search) != NULL | |
14490 | && (child->root.type == bfd_link_hash_defined | |
14491 | || child->root.type == bfd_link_hash_defweak) | |
14492 | && child->root.u.def.section == sec | |
14493 | && child->root.u.def.value == offset) | |
14494 | goto win; | |
14495 | } | |
14496 | ||
695344c0 | 14497 | /* xgettext:c-format */ |
9793eb77 | 14498 | _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"), |
2dcf00ce | 14499 | abfd, sec, (uint64_t) offset); |
c152c796 | 14500 | bfd_set_error (bfd_error_invalid_operation); |
0a1b45a2 | 14501 | return false; |
c152c796 AM |
14502 | |
14503 | win: | |
cbd0eecf | 14504 | if (!child->u2.vtable) |
f6e332e6 | 14505 | { |
cbd0eecf L |
14506 | child->u2.vtable = ((struct elf_link_virtual_table_entry *) |
14507 | bfd_zalloc (abfd, sizeof (*child->u2.vtable))); | |
14508 | if (!child->u2.vtable) | |
0a1b45a2 | 14509 | return false; |
f6e332e6 | 14510 | } |
c152c796 AM |
14511 | if (!h) |
14512 | { | |
14513 | /* This *should* only be the absolute section. It could potentially | |
14514 | be that someone has defined a non-global vtable though, which | |
14515 | would be bad. It isn't worth paging in the local symbols to be | |
14516 | sure though; that case should simply be handled by the assembler. */ | |
14517 | ||
cbd0eecf | 14518 | child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
14519 | } |
14520 | else | |
cbd0eecf | 14521 | child->u2.vtable->parent = h; |
c152c796 | 14522 | |
0a1b45a2 | 14523 | return true; |
c152c796 AM |
14524 | } |
14525 | ||
14526 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
14527 | ||
0a1b45a2 | 14528 | bool |
a0ea3a14 | 14529 | bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, |
c152c796 AM |
14530 | struct elf_link_hash_entry *h, |
14531 | bfd_vma addend) | |
14532 | { | |
14533 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14534 | unsigned int log_file_align = bed->s->log_file_align; | |
14535 | ||
a0ea3a14 L |
14536 | if (!h) |
14537 | { | |
14538 | /* xgettext:c-format */ | |
14539 | _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"), | |
14540 | abfd, sec); | |
14541 | bfd_set_error (bfd_error_bad_value); | |
0a1b45a2 | 14542 | return false; |
a0ea3a14 L |
14543 | } |
14544 | ||
cbd0eecf | 14545 | if (!h->u2.vtable) |
f6e332e6 | 14546 | { |
cbd0eecf L |
14547 | h->u2.vtable = ((struct elf_link_virtual_table_entry *) |
14548 | bfd_zalloc (abfd, sizeof (*h->u2.vtable))); | |
14549 | if (!h->u2.vtable) | |
0a1b45a2 | 14550 | return false; |
f6e332e6 AM |
14551 | } |
14552 | ||
cbd0eecf | 14553 | if (addend >= h->u2.vtable->size) |
c152c796 AM |
14554 | { |
14555 | size_t size, bytes, file_align; | |
0a1b45a2 | 14556 | bool *ptr = h->u2.vtable->used; |
c152c796 AM |
14557 | |
14558 | /* While the symbol is undefined, we have to be prepared to handle | |
14559 | a zero size. */ | |
14560 | file_align = 1 << log_file_align; | |
14561 | if (h->root.type == bfd_link_hash_undefined) | |
14562 | size = addend + file_align; | |
14563 | else | |
14564 | { | |
14565 | size = h->size; | |
14566 | if (addend >= size) | |
14567 | { | |
14568 | /* Oops! We've got a reference past the defined end of | |
14569 | the table. This is probably a bug -- shall we warn? */ | |
14570 | size = addend + file_align; | |
14571 | } | |
14572 | } | |
14573 | size = (size + file_align - 1) & -file_align; | |
14574 | ||
14575 | /* Allocate one extra entry for use as a "done" flag for the | |
14576 | consolidation pass. */ | |
0a1b45a2 | 14577 | bytes = ((size >> log_file_align) + 1) * sizeof (bool); |
c152c796 AM |
14578 | |
14579 | if (ptr) | |
14580 | { | |
0a1b45a2 | 14581 | ptr = (bool *) bfd_realloc (ptr - 1, bytes); |
c152c796 AM |
14582 | |
14583 | if (ptr != NULL) | |
14584 | { | |
14585 | size_t oldbytes; | |
14586 | ||
cbd0eecf | 14587 | oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) |
0a1b45a2 | 14588 | * sizeof (bool)); |
c152c796 AM |
14589 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); |
14590 | } | |
14591 | } | |
14592 | else | |
0a1b45a2 | 14593 | ptr = (bool *) bfd_zmalloc (bytes); |
c152c796 AM |
14594 | |
14595 | if (ptr == NULL) | |
0a1b45a2 | 14596 | return false; |
c152c796 AM |
14597 | |
14598 | /* And arrange for that done flag to be at index -1. */ | |
cbd0eecf L |
14599 | h->u2.vtable->used = ptr + 1; |
14600 | h->u2.vtable->size = size; | |
c152c796 AM |
14601 | } |
14602 | ||
0a1b45a2 | 14603 | h->u2.vtable->used[addend >> log_file_align] = true; |
c152c796 | 14604 | |
0a1b45a2 | 14605 | return true; |
c152c796 AM |
14606 | } |
14607 | ||
ae17ab41 CM |
14608 | /* Map an ELF section header flag to its corresponding string. */ |
14609 | typedef struct | |
14610 | { | |
14611 | char *flag_name; | |
14612 | flagword flag_value; | |
14613 | } elf_flags_to_name_table; | |
14614 | ||
3f75e1d6 | 14615 | static const elf_flags_to_name_table elf_flags_to_names [] = |
ae17ab41 CM |
14616 | { |
14617 | { "SHF_WRITE", SHF_WRITE }, | |
14618 | { "SHF_ALLOC", SHF_ALLOC }, | |
14619 | { "SHF_EXECINSTR", SHF_EXECINSTR }, | |
14620 | { "SHF_MERGE", SHF_MERGE }, | |
14621 | { "SHF_STRINGS", SHF_STRINGS }, | |
14622 | { "SHF_INFO_LINK", SHF_INFO_LINK}, | |
14623 | { "SHF_LINK_ORDER", SHF_LINK_ORDER}, | |
14624 | { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, | |
14625 | { "SHF_GROUP", SHF_GROUP }, | |
14626 | { "SHF_TLS", SHF_TLS }, | |
14627 | { "SHF_MASKOS", SHF_MASKOS }, | |
14628 | { "SHF_EXCLUDE", SHF_EXCLUDE }, | |
14629 | }; | |
14630 | ||
b9c361e0 | 14631 | /* Returns TRUE if the section is to be included, otherwise FALSE. */ |
0a1b45a2 | 14632 | bool |
ae17ab41 | 14633 | bfd_elf_lookup_section_flags (struct bfd_link_info *info, |
8b127cbc | 14634 | struct flag_info *flaginfo, |
b9c361e0 | 14635 | asection *section) |
ae17ab41 | 14636 | { |
8b127cbc | 14637 | const bfd_vma sh_flags = elf_section_flags (section); |
ae17ab41 | 14638 | |
8b127cbc | 14639 | if (!flaginfo->flags_initialized) |
ae17ab41 | 14640 | { |
8b127cbc AM |
14641 | bfd *obfd = info->output_bfd; |
14642 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
14643 | struct flag_info_list *tf = flaginfo->flag_list; | |
b9c361e0 JL |
14644 | int with_hex = 0; |
14645 | int without_hex = 0; | |
14646 | ||
8b127cbc | 14647 | for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) |
ae17ab41 | 14648 | { |
b9c361e0 | 14649 | unsigned i; |
8b127cbc | 14650 | flagword (*lookup) (char *); |
ae17ab41 | 14651 | |
8b127cbc AM |
14652 | lookup = bed->elf_backend_lookup_section_flags_hook; |
14653 | if (lookup != NULL) | |
ae17ab41 | 14654 | { |
8b127cbc | 14655 | flagword hexval = (*lookup) ((char *) tf->name); |
b9c361e0 JL |
14656 | |
14657 | if (hexval != 0) | |
14658 | { | |
14659 | if (tf->with == with_flags) | |
14660 | with_hex |= hexval; | |
14661 | else if (tf->with == without_flags) | |
14662 | without_hex |= hexval; | |
0a1b45a2 | 14663 | tf->valid = true; |
b9c361e0 JL |
14664 | continue; |
14665 | } | |
ae17ab41 | 14666 | } |
8b127cbc | 14667 | for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) |
ae17ab41 | 14668 | { |
8b127cbc | 14669 | if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) |
b9c361e0 JL |
14670 | { |
14671 | if (tf->with == with_flags) | |
14672 | with_hex |= elf_flags_to_names[i].flag_value; | |
14673 | else if (tf->with == without_flags) | |
14674 | without_hex |= elf_flags_to_names[i].flag_value; | |
0a1b45a2 | 14675 | tf->valid = true; |
b9c361e0 JL |
14676 | break; |
14677 | } | |
14678 | } | |
8b127cbc | 14679 | if (!tf->valid) |
b9c361e0 | 14680 | { |
68ffbac6 | 14681 | info->callbacks->einfo |
9793eb77 | 14682 | (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); |
0a1b45a2 | 14683 | return false; |
ae17ab41 CM |
14684 | } |
14685 | } | |
0a1b45a2 | 14686 | flaginfo->flags_initialized = true; |
8b127cbc AM |
14687 | flaginfo->only_with_flags |= with_hex; |
14688 | flaginfo->not_with_flags |= without_hex; | |
ae17ab41 | 14689 | } |
ae17ab41 | 14690 | |
8b127cbc | 14691 | if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) |
0a1b45a2 | 14692 | return false; |
b9c361e0 | 14693 | |
8b127cbc | 14694 | if ((flaginfo->not_with_flags & sh_flags) != 0) |
0a1b45a2 | 14695 | return false; |
b9c361e0 | 14696 | |
0a1b45a2 | 14697 | return true; |
ae17ab41 CM |
14698 | } |
14699 | ||
c152c796 AM |
14700 | struct alloc_got_off_arg { |
14701 | bfd_vma gotoff; | |
10455f89 | 14702 | struct bfd_link_info *info; |
c152c796 AM |
14703 | }; |
14704 | ||
14705 | /* We need a special top-level link routine to convert got reference counts | |
14706 | to real got offsets. */ | |
14707 | ||
0a1b45a2 | 14708 | static bool |
c152c796 AM |
14709 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) |
14710 | { | |
a50b1753 | 14711 | struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; |
10455f89 HPN |
14712 | bfd *obfd = gofarg->info->output_bfd; |
14713 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
c152c796 | 14714 | |
c152c796 AM |
14715 | if (h->got.refcount > 0) |
14716 | { | |
14717 | h->got.offset = gofarg->gotoff; | |
10455f89 | 14718 | gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); |
c152c796 AM |
14719 | } |
14720 | else | |
14721 | h->got.offset = (bfd_vma) -1; | |
14722 | ||
0a1b45a2 | 14723 | return true; |
c152c796 AM |
14724 | } |
14725 | ||
14726 | /* And an accompanying bit to work out final got entry offsets once | |
14727 | we're done. Should be called from final_link. */ | |
14728 | ||
0a1b45a2 | 14729 | bool |
c152c796 AM |
14730 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, |
14731 | struct bfd_link_info *info) | |
14732 | { | |
14733 | bfd *i; | |
14734 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
14735 | bfd_vma gotoff; | |
c152c796 AM |
14736 | struct alloc_got_off_arg gofarg; |
14737 | ||
10455f89 HPN |
14738 | BFD_ASSERT (abfd == info->output_bfd); |
14739 | ||
c152c796 | 14740 | if (! is_elf_hash_table (info->hash)) |
0a1b45a2 | 14741 | return false; |
c152c796 AM |
14742 | |
14743 | /* The GOT offset is relative to the .got section, but the GOT header is | |
14744 | put into the .got.plt section, if the backend uses it. */ | |
14745 | if (bed->want_got_plt) | |
14746 | gotoff = 0; | |
14747 | else | |
14748 | gotoff = bed->got_header_size; | |
14749 | ||
14750 | /* Do the local .got entries first. */ | |
c72f2fb2 | 14751 | for (i = info->input_bfds; i; i = i->link.next) |
c152c796 AM |
14752 | { |
14753 | bfd_signed_vma *local_got; | |
ef53be89 | 14754 | size_t j, locsymcount; |
c152c796 AM |
14755 | Elf_Internal_Shdr *symtab_hdr; |
14756 | ||
14757 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
14758 | continue; | |
14759 | ||
14760 | local_got = elf_local_got_refcounts (i); | |
14761 | if (!local_got) | |
14762 | continue; | |
14763 | ||
14764 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
14765 | if (elf_bad_symtab (i)) | |
14766 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
14767 | else | |
14768 | locsymcount = symtab_hdr->sh_info; | |
14769 | ||
14770 | for (j = 0; j < locsymcount; ++j) | |
14771 | { | |
14772 | if (local_got[j] > 0) | |
14773 | { | |
14774 | local_got[j] = gotoff; | |
10455f89 | 14775 | gotoff += bed->got_elt_size (abfd, info, NULL, i, j); |
c152c796 AM |
14776 | } |
14777 | else | |
14778 | local_got[j] = (bfd_vma) -1; | |
14779 | } | |
14780 | } | |
14781 | ||
14782 | /* Then the global .got entries. .plt refcounts are handled by | |
14783 | adjust_dynamic_symbol */ | |
14784 | gofarg.gotoff = gotoff; | |
10455f89 | 14785 | gofarg.info = info; |
c152c796 AM |
14786 | elf_link_hash_traverse (elf_hash_table (info), |
14787 | elf_gc_allocate_got_offsets, | |
14788 | &gofarg); | |
0a1b45a2 | 14789 | return true; |
c152c796 AM |
14790 | } |
14791 | ||
14792 | /* Many folk need no more in the way of final link than this, once | |
14793 | got entry reference counting is enabled. */ | |
14794 | ||
0a1b45a2 | 14795 | bool |
c152c796 AM |
14796 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) |
14797 | { | |
14798 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
0a1b45a2 | 14799 | return false; |
c152c796 AM |
14800 | |
14801 | /* Invoke the regular ELF backend linker to do all the work. */ | |
14802 | return bfd_elf_final_link (abfd, info); | |
14803 | } | |
14804 | ||
0a1b45a2 | 14805 | bool |
c152c796 AM |
14806 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) |
14807 | { | |
a50b1753 | 14808 | struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; |
c152c796 AM |
14809 | |
14810 | if (rcookie->bad_symtab) | |
14811 | rcookie->rel = rcookie->rels; | |
14812 | ||
14813 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
14814 | { | |
14815 | unsigned long r_symndx; | |
14816 | ||
14817 | if (! rcookie->bad_symtab) | |
14818 | if (rcookie->rel->r_offset > offset) | |
0a1b45a2 | 14819 | return false; |
c152c796 AM |
14820 | if (rcookie->rel->r_offset != offset) |
14821 | continue; | |
14822 | ||
14823 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
2c2fa401 | 14824 | if (r_symndx == STN_UNDEF) |
0a1b45a2 | 14825 | return true; |
c152c796 AM |
14826 | |
14827 | if (r_symndx >= rcookie->locsymcount | |
14828 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
14829 | { | |
14830 | struct elf_link_hash_entry *h; | |
14831 | ||
14832 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
14833 | ||
14834 | while (h->root.type == bfd_link_hash_indirect | |
14835 | || h->root.type == bfd_link_hash_warning) | |
14836 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
14837 | ||
14838 | if ((h->root.type == bfd_link_hash_defined | |
14839 | || h->root.type == bfd_link_hash_defweak) | |
5b69e357 AM |
14840 | && (h->root.u.def.section->owner != rcookie->abfd |
14841 | || h->root.u.def.section->kept_section != NULL | |
14842 | || discarded_section (h->root.u.def.section))) | |
0a1b45a2 | 14843 | return true; |
c152c796 AM |
14844 | } |
14845 | else | |
14846 | { | |
14847 | /* It's not a relocation against a global symbol, | |
14848 | but it could be a relocation against a local | |
14849 | symbol for a discarded section. */ | |
14850 | asection *isec; | |
14851 | Elf_Internal_Sym *isym; | |
14852 | ||
14853 | /* Need to: get the symbol; get the section. */ | |
14854 | isym = &rcookie->locsyms[r_symndx]; | |
cb33740c | 14855 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); |
5b69e357 AM |
14856 | if (isec != NULL |
14857 | && (isec->kept_section != NULL | |
14858 | || discarded_section (isec))) | |
0a1b45a2 | 14859 | return true; |
c152c796 | 14860 | } |
0a1b45a2 | 14861 | return false; |
c152c796 | 14862 | } |
0a1b45a2 | 14863 | return false; |
c152c796 AM |
14864 | } |
14865 | ||
14866 | /* Discard unneeded references to discarded sections. | |
75938853 AM |
14867 | Returns -1 on error, 1 if any section's size was changed, 0 if |
14868 | nothing changed. This function assumes that the relocations are in | |
14869 | sorted order, which is true for all known assemblers. */ | |
c152c796 | 14870 | |
75938853 | 14871 | int |
c152c796 AM |
14872 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) |
14873 | { | |
14874 | struct elf_reloc_cookie cookie; | |
18cd5bce | 14875 | asection *o; |
c152c796 | 14876 | bfd *abfd; |
75938853 | 14877 | int changed = 0; |
c152c796 AM |
14878 | |
14879 | if (info->traditional_format | |
14880 | || !is_elf_hash_table (info->hash)) | |
75938853 | 14881 | return 0; |
c152c796 | 14882 | |
18cd5bce AM |
14883 | o = bfd_get_section_by_name (output_bfd, ".stab"); |
14884 | if (o != NULL) | |
c152c796 | 14885 | { |
18cd5bce | 14886 | asection *i; |
c152c796 | 14887 | |
18cd5bce | 14888 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
8da3dbc5 | 14889 | { |
18cd5bce AM |
14890 | if (i->size == 0 |
14891 | || i->reloc_count == 0 | |
14892 | || i->sec_info_type != SEC_INFO_TYPE_STABS) | |
14893 | continue; | |
c152c796 | 14894 | |
18cd5bce AM |
14895 | abfd = i->owner; |
14896 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14897 | continue; | |
c152c796 | 14898 | |
18cd5bce | 14899 | if (!init_reloc_cookie_for_section (&cookie, info, i)) |
75938853 | 14900 | return -1; |
c152c796 | 14901 | |
18cd5bce AM |
14902 | if (_bfd_discard_section_stabs (abfd, i, |
14903 | elf_section_data (i)->sec_info, | |
5241d853 RS |
14904 | bfd_elf_reloc_symbol_deleted_p, |
14905 | &cookie)) | |
75938853 | 14906 | changed = 1; |
18cd5bce AM |
14907 | |
14908 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14909 | } |
18cd5bce AM |
14910 | } |
14911 | ||
2f0c68f2 CM |
14912 | o = NULL; |
14913 | if (info->eh_frame_hdr_type != COMPACT_EH_HDR) | |
14914 | o = bfd_get_section_by_name (output_bfd, ".eh_frame"); | |
18cd5bce AM |
14915 | if (o != NULL) |
14916 | { | |
14917 | asection *i; | |
d7153c4a | 14918 | int eh_changed = 0; |
66631823 | 14919 | unsigned int eh_alignment; /* Octets. */ |
c152c796 | 14920 | |
18cd5bce | 14921 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) |
c152c796 | 14922 | { |
18cd5bce AM |
14923 | if (i->size == 0) |
14924 | continue; | |
14925 | ||
14926 | abfd = i->owner; | |
14927 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14928 | continue; | |
14929 | ||
14930 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
75938853 | 14931 | return -1; |
18cd5bce AM |
14932 | |
14933 | _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); | |
14934 | if (_bfd_elf_discard_section_eh_frame (abfd, info, i, | |
c152c796 AM |
14935 | bfd_elf_reloc_symbol_deleted_p, |
14936 | &cookie)) | |
d7153c4a AM |
14937 | { |
14938 | eh_changed = 1; | |
14939 | if (i->size != i->rawsize) | |
14940 | changed = 1; | |
14941 | } | |
18cd5bce AM |
14942 | |
14943 | fini_reloc_cookie_for_section (&cookie, i); | |
c152c796 | 14944 | } |
9866ffe2 | 14945 | |
66631823 CE |
14946 | eh_alignment = ((1 << o->alignment_power) |
14947 | * bfd_octets_per_byte (output_bfd, o)); | |
9866ffe2 AM |
14948 | /* Skip over zero terminator, and prevent empty sections from |
14949 | adding alignment padding at the end. */ | |
14950 | for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) | |
14951 | if (i->size == 0) | |
14952 | i->flags |= SEC_EXCLUDE; | |
14953 | else if (i->size > 4) | |
14954 | break; | |
14955 | /* The last non-empty eh_frame section doesn't need padding. */ | |
14956 | if (i != NULL) | |
14957 | i = i->map_tail.s; | |
14958 | /* Any prior sections must pad the last FDE out to the output | |
14959 | section alignment. Otherwise we might have zero padding | |
14960 | between sections, which would be seen as a terminator. */ | |
14961 | for (; i != NULL; i = i->map_tail.s) | |
14962 | if (i->size == 4) | |
14963 | /* All but the last zero terminator should have been removed. */ | |
14964 | BFD_FAIL (); | |
14965 | else | |
14966 | { | |
14967 | bfd_size_type size | |
14968 | = (i->size + eh_alignment - 1) & -eh_alignment; | |
14969 | if (i->size != size) | |
af471f82 | 14970 | { |
9866ffe2 AM |
14971 | i->size = size; |
14972 | changed = 1; | |
14973 | eh_changed = 1; | |
af471f82 | 14974 | } |
9866ffe2 | 14975 | } |
d7153c4a AM |
14976 | if (eh_changed) |
14977 | elf_link_hash_traverse (elf_hash_table (info), | |
14978 | _bfd_elf_adjust_eh_frame_global_symbol, NULL); | |
18cd5bce | 14979 | } |
c152c796 | 14980 | |
cf0e0a0b IB |
14981 | o = bfd_get_section_by_name (output_bfd, ".sframe"); |
14982 | if (o != NULL) | |
14983 | { | |
14984 | asection *i; | |
14985 | ||
14986 | for (i = o->map_head.s; i != NULL; i = i->map_head.s) | |
14987 | { | |
14988 | if (i->size == 0) | |
14989 | continue; | |
14990 | ||
14991 | abfd = i->owner; | |
14992 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
14993 | continue; | |
14994 | ||
14995 | if (!init_reloc_cookie_for_section (&cookie, info, i)) | |
14996 | return -1; | |
14997 | ||
14998 | if (_bfd_elf_parse_sframe (abfd, info, i, &cookie)) | |
14999 | { | |
15000 | if (_bfd_elf_discard_section_sframe (i, | |
15001 | bfd_elf_reloc_symbol_deleted_p, | |
15002 | &cookie)) | |
15003 | { | |
15004 | if (i->size != i->rawsize) | |
15005 | changed = 1; | |
15006 | } | |
15007 | } | |
15008 | fini_reloc_cookie_for_section (&cookie, i); | |
15009 | } | |
15010 | /* Update the reference to the output .sframe section. Used to | |
15011 | determine later if PT_GNU_SFRAME segment is to be generated. */ | |
15012 | if (!_bfd_elf_set_section_sframe (output_bfd, info)) | |
15013 | return -1; | |
15014 | } | |
15015 | ||
18cd5bce AM |
15016 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) |
15017 | { | |
15018 | const struct elf_backend_data *bed; | |
57963c05 | 15019 | asection *s; |
c152c796 | 15020 | |
18cd5bce AM |
15021 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
15022 | continue; | |
57963c05 AM |
15023 | s = abfd->sections; |
15024 | if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) | |
15025 | continue; | |
18cd5bce AM |
15026 | |
15027 | bed = get_elf_backend_data (abfd); | |
15028 | ||
15029 | if (bed->elf_backend_discard_info != NULL) | |
15030 | { | |
15031 | if (!init_reloc_cookie (&cookie, info, abfd)) | |
75938853 | 15032 | return -1; |
18cd5bce AM |
15033 | |
15034 | if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
75938853 | 15035 | changed = 1; |
18cd5bce AM |
15036 | |
15037 | fini_reloc_cookie (&cookie, abfd); | |
15038 | } | |
c152c796 AM |
15039 | } |
15040 | ||
2f0c68f2 CM |
15041 | if (info->eh_frame_hdr_type == COMPACT_EH_HDR) |
15042 | _bfd_elf_end_eh_frame_parsing (info); | |
15043 | ||
15044 | if (info->eh_frame_hdr_type | |
0e1862bb | 15045 | && !bfd_link_relocatable (info) |
8df52eee | 15046 | && _bfd_elf_discard_section_eh_frame_hdr (info)) |
75938853 | 15047 | changed = 1; |
c152c796 | 15048 | |
75938853 | 15049 | return changed; |
c152c796 | 15050 | } |
082b7297 | 15051 | |
0a1b45a2 | 15052 | bool |
0c511000 | 15053 | _bfd_elf_section_already_linked (bfd *abfd, |
c77ec726 | 15054 | asection *sec, |
c0f00686 | 15055 | struct bfd_link_info *info) |
082b7297 L |
15056 | { |
15057 | flagword flags; | |
c77ec726 | 15058 | const char *name, *key; |
082b7297 L |
15059 | struct bfd_section_already_linked *l; |
15060 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
0c511000 | 15061 | |
c77ec726 | 15062 | if (sec->output_section == bfd_abs_section_ptr) |
0a1b45a2 | 15063 | return false; |
0c511000 | 15064 | |
c77ec726 | 15065 | flags = sec->flags; |
0c511000 | 15066 | |
c77ec726 AM |
15067 | /* Return if it isn't a linkonce section. A comdat group section |
15068 | also has SEC_LINK_ONCE set. */ | |
15069 | if ((flags & SEC_LINK_ONCE) == 0) | |
0a1b45a2 | 15070 | return false; |
0c511000 | 15071 | |
c77ec726 AM |
15072 | /* Don't put group member sections on our list of already linked |
15073 | sections. They are handled as a group via their group section. */ | |
15074 | if (elf_sec_group (sec) != NULL) | |
0a1b45a2 | 15075 | return false; |
0c511000 | 15076 | |
c77ec726 AM |
15077 | /* For a SHT_GROUP section, use the group signature as the key. */ |
15078 | name = sec->name; | |
15079 | if ((flags & SEC_GROUP) != 0 | |
15080 | && elf_next_in_group (sec) != NULL | |
15081 | && elf_group_name (elf_next_in_group (sec)) != NULL) | |
15082 | key = elf_group_name (elf_next_in_group (sec)); | |
15083 | else | |
15084 | { | |
15085 | /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ | |
08dedd66 | 15086 | if (startswith (name, ".gnu.linkonce.") |
c77ec726 AM |
15087 | && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) |
15088 | key++; | |
0c511000 | 15089 | else |
c77ec726 AM |
15090 | /* Must be a user linkonce section that doesn't follow gcc's |
15091 | naming convention. In this case we won't be matching | |
15092 | single member groups. */ | |
15093 | key = name; | |
0c511000 | 15094 | } |
6d2cd210 | 15095 | |
c77ec726 | 15096 | already_linked_list = bfd_section_already_linked_table_lookup (key); |
082b7297 L |
15097 | |
15098 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
15099 | { | |
c2370991 | 15100 | /* We may have 2 different types of sections on the list: group |
c77ec726 AM |
15101 | sections with a signature of <key> (<key> is some string), |
15102 | and linkonce sections named .gnu.linkonce.<type>.<key>. | |
15103 | Match like sections. LTO plugin sections are an exception. | |
15104 | They are always named .gnu.linkonce.t.<key> and match either | |
15105 | type of section. */ | |
15106 | if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) | |
15107 | && ((flags & SEC_GROUP) != 0 | |
15108 | || strcmp (name, l->sec->name) == 0)) | |
e8a83e93 MB |
15109 | || (l->sec->owner->flags & BFD_PLUGIN) != 0 |
15110 | || (sec->owner->flags & BFD_PLUGIN) != 0) | |
082b7297 L |
15111 | { |
15112 | /* The section has already been linked. See if we should | |
6d2cd210 | 15113 | issue a warning. */ |
c77ec726 | 15114 | if (!_bfd_handle_already_linked (sec, l, info)) |
0a1b45a2 | 15115 | return false; |
082b7297 | 15116 | |
c77ec726 | 15117 | if (flags & SEC_GROUP) |
3d7f7666 | 15118 | { |
c77ec726 AM |
15119 | asection *first = elf_next_in_group (sec); |
15120 | asection *s = first; | |
3d7f7666 | 15121 | |
c77ec726 | 15122 | while (s != NULL) |
3d7f7666 | 15123 | { |
c77ec726 AM |
15124 | s->output_section = bfd_abs_section_ptr; |
15125 | /* Record which group discards it. */ | |
15126 | s->kept_section = l->sec; | |
15127 | s = elf_next_in_group (s); | |
15128 | /* These lists are circular. */ | |
15129 | if (s == first) | |
15130 | break; | |
3d7f7666 L |
15131 | } |
15132 | } | |
082b7297 | 15133 | |
0a1b45a2 | 15134 | return true; |
082b7297 L |
15135 | } |
15136 | } | |
15137 | ||
c77ec726 AM |
15138 | /* A single member comdat group section may be discarded by a |
15139 | linkonce section and vice versa. */ | |
15140 | if ((flags & SEC_GROUP) != 0) | |
3d7f7666 | 15141 | { |
c77ec726 | 15142 | asection *first = elf_next_in_group (sec); |
c2370991 | 15143 | |
c77ec726 AM |
15144 | if (first != NULL && elf_next_in_group (first) == first) |
15145 | /* Check this single member group against linkonce sections. */ | |
15146 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
15147 | if ((l->sec->flags & SEC_GROUP) == 0 | |
15148 | && bfd_elf_match_symbols_in_sections (l->sec, first, info)) | |
15149 | { | |
15150 | first->output_section = bfd_abs_section_ptr; | |
15151 | first->kept_section = l->sec; | |
15152 | sec->output_section = bfd_abs_section_ptr; | |
15153 | break; | |
15154 | } | |
15155 | } | |
15156 | else | |
15157 | /* Check this linkonce section against single member groups. */ | |
15158 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
15159 | if (l->sec->flags & SEC_GROUP) | |
6d2cd210 | 15160 | { |
c77ec726 | 15161 | asection *first = elf_next_in_group (l->sec); |
6d2cd210 | 15162 | |
c77ec726 AM |
15163 | if (first != NULL |
15164 | && elf_next_in_group (first) == first | |
15165 | && bfd_elf_match_symbols_in_sections (first, sec, info)) | |
15166 | { | |
15167 | sec->output_section = bfd_abs_section_ptr; | |
15168 | sec->kept_section = first; | |
15169 | break; | |
15170 | } | |
6d2cd210 | 15171 | } |
0c511000 | 15172 | |
c77ec726 AM |
15173 | /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' |
15174 | referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 | |
15175 | specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' | |
15176 | prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its | |
15177 | matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded | |
15178 | but its `.gnu.linkonce.t.F' is discarded means we chose one-only | |
15179 | `.gnu.linkonce.t.F' section from a different bfd not requiring any | |
15180 | `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. | |
15181 | The reverse order cannot happen as there is never a bfd with only the | |
15182 | `.gnu.linkonce.r.F' section. The order of sections in a bfd does not | |
15183 | matter as here were are looking only for cross-bfd sections. */ | |
15184 | ||
08dedd66 | 15185 | if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r.")) |
c77ec726 AM |
15186 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
15187 | if ((l->sec->flags & SEC_GROUP) == 0 | |
08dedd66 | 15188 | && startswith (l->sec->name, ".gnu.linkonce.t.")) |
c77ec726 AM |
15189 | { |
15190 | if (abfd != l->sec->owner) | |
15191 | sec->output_section = bfd_abs_section_ptr; | |
15192 | break; | |
15193 | } | |
80c29487 | 15194 | |
082b7297 | 15195 | /* This is the first section with this name. Record it. */ |
c77ec726 | 15196 | if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) |
bb6198d2 | 15197 | info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); |
c77ec726 | 15198 | return sec->output_section == bfd_abs_section_ptr; |
082b7297 | 15199 | } |
81e1b023 | 15200 | |
0a1b45a2 | 15201 | bool |
a4d8e49b L |
15202 | _bfd_elf_common_definition (Elf_Internal_Sym *sym) |
15203 | { | |
15204 | return sym->st_shndx == SHN_COMMON; | |
15205 | } | |
15206 | ||
15207 | unsigned int | |
15208 | _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) | |
15209 | { | |
15210 | return SHN_COMMON; | |
15211 | } | |
15212 | ||
15213 | asection * | |
15214 | _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) | |
15215 | { | |
15216 | return bfd_com_section_ptr; | |
15217 | } | |
10455f89 HPN |
15218 | |
15219 | bfd_vma | |
15220 | _bfd_elf_default_got_elt_size (bfd *abfd, | |
15221 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
15222 | struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, | |
15223 | bfd *ibfd ATTRIBUTE_UNUSED, | |
15224 | unsigned long symndx ATTRIBUTE_UNUSED) | |
15225 | { | |
15226 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
15227 | return bed->s->arch_size / 8; | |
15228 | } | |
83bac4b0 NC |
15229 | |
15230 | /* Routines to support the creation of dynamic relocs. */ | |
15231 | ||
83bac4b0 NC |
15232 | /* Returns the name of the dynamic reloc section associated with SEC. */ |
15233 | ||
15234 | static const char * | |
15235 | get_dynamic_reloc_section_name (bfd * abfd, | |
15236 | asection * sec, | |
0a1b45a2 | 15237 | bool is_rela) |
83bac4b0 | 15238 | { |
ddcf1fcf | 15239 | char *name; |
fd361982 | 15240 | const char *old_name = bfd_section_name (sec); |
ddcf1fcf | 15241 | const char *prefix = is_rela ? ".rela" : ".rel"; |
83bac4b0 | 15242 | |
ddcf1fcf | 15243 | if (old_name == NULL) |
83bac4b0 NC |
15244 | return NULL; |
15245 | ||
ddcf1fcf | 15246 | name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); |
68ffbac6 | 15247 | sprintf (name, "%s%s", prefix, old_name); |
83bac4b0 NC |
15248 | |
15249 | return name; | |
15250 | } | |
15251 | ||
15252 | /* Returns the dynamic reloc section associated with SEC. | |
15253 | If necessary compute the name of the dynamic reloc section based | |
15254 | on SEC's name (looked up in ABFD's string table) and the setting | |
15255 | of IS_RELA. */ | |
15256 | ||
15257 | asection * | |
0a1b45a2 AM |
15258 | _bfd_elf_get_dynamic_reloc_section (bfd *abfd, |
15259 | asection *sec, | |
15260 | bool is_rela) | |
83bac4b0 | 15261 | { |
0a1b45a2 | 15262 | asection *reloc_sec = elf_section_data (sec)->sreloc; |
83bac4b0 NC |
15263 | |
15264 | if (reloc_sec == NULL) | |
15265 | { | |
0a1b45a2 | 15266 | const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela); |
83bac4b0 NC |
15267 | |
15268 | if (name != NULL) | |
15269 | { | |
3d4d4302 | 15270 | reloc_sec = bfd_get_linker_section (abfd, name); |
83bac4b0 NC |
15271 | |
15272 | if (reloc_sec != NULL) | |
15273 | elf_section_data (sec)->sreloc = reloc_sec; | |
15274 | } | |
15275 | } | |
15276 | ||
15277 | return reloc_sec; | |
15278 | } | |
15279 | ||
15280 | /* Returns the dynamic reloc section associated with SEC. If the | |
15281 | section does not exist it is created and attached to the DYNOBJ | |
15282 | bfd and stored in the SRELOC field of SEC's elf_section_data | |
15283 | structure. | |
f8076f98 | 15284 | |
83bac4b0 NC |
15285 | ALIGNMENT is the alignment for the newly created section and |
15286 | IS_RELA defines whether the name should be .rela.<SEC's name> | |
15287 | or .rel.<SEC's name>. The section name is looked up in the | |
15288 | string table associated with ABFD. */ | |
15289 | ||
15290 | asection * | |
ca4be51c AM |
15291 | _bfd_elf_make_dynamic_reloc_section (asection *sec, |
15292 | bfd *dynobj, | |
15293 | unsigned int alignment, | |
15294 | bfd *abfd, | |
0a1b45a2 | 15295 | bool is_rela) |
83bac4b0 NC |
15296 | { |
15297 | asection * reloc_sec = elf_section_data (sec)->sreloc; | |
15298 | ||
15299 | if (reloc_sec == NULL) | |
15300 | { | |
15301 | const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); | |
15302 | ||
15303 | if (name == NULL) | |
15304 | return NULL; | |
15305 | ||
3d4d4302 | 15306 | reloc_sec = bfd_get_linker_section (dynobj, name); |
83bac4b0 NC |
15307 | |
15308 | if (reloc_sec == NULL) | |
15309 | { | |
3d4d4302 AM |
15310 | flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY |
15311 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
83bac4b0 NC |
15312 | if ((sec->flags & SEC_ALLOC) != 0) |
15313 | flags |= SEC_ALLOC | SEC_LOAD; | |
15314 | ||
3d4d4302 | 15315 | reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); |
83bac4b0 NC |
15316 | if (reloc_sec != NULL) |
15317 | { | |
8877b5e5 AM |
15318 | /* _bfd_elf_get_sec_type_attr chooses a section type by |
15319 | name. Override as it may be wrong, eg. for a user | |
15320 | section named "auto" we'll get ".relauto" which is | |
15321 | seen to be a .rela section. */ | |
15322 | elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; | |
fd361982 | 15323 | if (!bfd_set_section_alignment (reloc_sec, alignment)) |
83bac4b0 NC |
15324 | reloc_sec = NULL; |
15325 | } | |
15326 | } | |
15327 | ||
15328 | elf_section_data (sec)->sreloc = reloc_sec; | |
15329 | } | |
15330 | ||
15331 | return reloc_sec; | |
15332 | } | |
1338dd10 | 15333 | |
bffebb6b AM |
15334 | /* Copy the ELF symbol type and other attributes for a linker script |
15335 | assignment from HSRC to HDEST. Generally this should be treated as | |
15336 | if we found a strong non-dynamic definition for HDEST (except that | |
15337 | ld ignores multiple definition errors). */ | |
1338dd10 | 15338 | void |
bffebb6b AM |
15339 | _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, |
15340 | struct bfd_link_hash_entry *hdest, | |
15341 | struct bfd_link_hash_entry *hsrc) | |
1338dd10 | 15342 | { |
bffebb6b AM |
15343 | struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; |
15344 | struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; | |
15345 | Elf_Internal_Sym isym; | |
1338dd10 PB |
15346 | |
15347 | ehdest->type = ehsrc->type; | |
35fc36a8 | 15348 | ehdest->target_internal = ehsrc->target_internal; |
bffebb6b AM |
15349 | |
15350 | isym.st_other = ehsrc->other; | |
0a1b45a2 | 15351 | elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false); |
1338dd10 | 15352 | } |
351f65ca L |
15353 | |
15354 | /* Append a RELA relocation REL to section S in BFD. */ | |
15355 | ||
15356 | void | |
15357 | elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
15358 | { | |
15359 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
15360 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); | |
15361 | BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); | |
15362 | bed->s->swap_reloca_out (abfd, rel, loc); | |
15363 | } | |
15364 | ||
15365 | /* Append a REL relocation REL to section S in BFD. */ | |
15366 | ||
15367 | void | |
15368 | elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) | |
15369 | { | |
15370 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
15371 | bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); | |
15372 | BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); | |
59d6ffb2 | 15373 | bed->s->swap_reloc_out (abfd, rel, loc); |
351f65ca | 15374 | } |
7dba9362 AM |
15375 | |
15376 | /* Define __start, __stop, .startof. or .sizeof. symbol. */ | |
15377 | ||
15378 | struct bfd_link_hash_entry * | |
15379 | bfd_elf_define_start_stop (struct bfd_link_info *info, | |
15380 | const char *symbol, asection *sec) | |
15381 | { | |
487b6440 | 15382 | struct elf_link_hash_entry *h; |
7dba9362 | 15383 | |
487b6440 | 15384 | h = elf_link_hash_lookup (elf_hash_table (info), symbol, |
0a1b45a2 | 15385 | false, false, true); |
e1b5d517 | 15386 | /* NB: Common symbols will be turned into definition later. */ |
487b6440 | 15387 | if (h != NULL |
8ee10e86 | 15388 | && !h->root.ldscript_def |
487b6440 AM |
15389 | && (h->root.type == bfd_link_hash_undefined |
15390 | || h->root.type == bfd_link_hash_undefweak | |
e1b5d517 L |
15391 | || ((h->ref_regular || h->def_dynamic) |
15392 | && !h->def_regular | |
15393 | && h->root.type != bfd_link_hash_common))) | |
7dba9362 | 15394 | { |
0a1b45a2 | 15395 | bool was_dynamic = h->ref_dynamic || h->def_dynamic; |
e1b5d517 | 15396 | h->verinfo.verdef = NULL; |
487b6440 AM |
15397 | h->root.type = bfd_link_hash_defined; |
15398 | h->root.u.def.section = sec; | |
15399 | h->root.u.def.value = 0; | |
15400 | h->def_regular = 1; | |
15401 | h->def_dynamic = 0; | |
15402 | h->start_stop = 1; | |
15403 | h->u2.start_stop_section = sec; | |
15404 | if (symbol[0] == '.') | |
15405 | { | |
15406 | /* .startof. and .sizeof. symbols are local. */ | |
559192d8 AM |
15407 | const struct elf_backend_data *bed; |
15408 | bed = get_elf_backend_data (info->output_bfd); | |
0a1b45a2 | 15409 | (*bed->elf_backend_hide_symbol) (info, h, true); |
487b6440 | 15410 | } |
36b8fda5 AM |
15411 | else |
15412 | { | |
15413 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
cae64165 RM |
15414 | h->other = ((h->other & ~ELF_ST_VISIBILITY (-1)) |
15415 | | info->start_stop_visibility); | |
bf3077a6 | 15416 | if (was_dynamic) |
36b8fda5 AM |
15417 | bfd_elf_link_record_dynamic_symbol (info, h); |
15418 | } | |
487b6440 | 15419 | return &h->root; |
7dba9362 | 15420 | } |
487b6440 | 15421 | return NULL; |
7dba9362 | 15422 | } |
5dbc8b37 L |
15423 | |
15424 | /* Find dynamic relocs for H that apply to read-only sections. */ | |
15425 | ||
15426 | asection * | |
15427 | _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h) | |
15428 | { | |
15429 | struct elf_dyn_relocs *p; | |
15430 | ||
15431 | for (p = h->dyn_relocs; p != NULL; p = p->next) | |
15432 | { | |
15433 | asection *s = p->sec->output_section; | |
15434 | ||
15435 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
15436 | return p->sec; | |
15437 | } | |
15438 | return NULL; | |
15439 | } | |
d49e5065 L |
15440 | |
15441 | /* Set DF_TEXTREL if we find any dynamic relocs that apply to | |
15442 | read-only sections. */ | |
15443 | ||
0a1b45a2 | 15444 | bool |
d49e5065 L |
15445 | _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf) |
15446 | { | |
15447 | asection *sec; | |
15448 | ||
15449 | if (h->root.type == bfd_link_hash_indirect) | |
0a1b45a2 | 15450 | return true; |
d49e5065 L |
15451 | |
15452 | sec = _bfd_elf_readonly_dynrelocs (h); | |
15453 | if (sec != NULL) | |
15454 | { | |
15455 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
15456 | ||
15457 | info->flags |= DF_TEXTREL; | |
15458 | /* xgettext:c-format */ | |
15459 | info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' " | |
15460 | "in read-only section `%pA'\n"), | |
15461 | sec->owner, h->root.root.string, sec); | |
15462 | ||
15463 | if (bfd_link_textrel_check (info)) | |
15464 | /* xgettext:c-format */ | |
15465 | info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' " | |
15466 | "in read-only section `%pA'\n"), | |
15467 | sec->owner, h->root.root.string, sec); | |
15468 | ||
15469 | /* Not an error, just cut short the traversal. */ | |
0a1b45a2 | 15470 | return false; |
d49e5065 | 15471 | } |
0a1b45a2 | 15472 | return true; |
d49e5065 | 15473 | } |
3084d7a2 L |
15474 | |
15475 | /* Add dynamic tags. */ | |
15476 | ||
0a1b45a2 | 15477 | bool |
3084d7a2 | 15478 | _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info, |
0a1b45a2 | 15479 | bool need_dynamic_reloc) |
3084d7a2 L |
15480 | { |
15481 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
15482 | ||
15483 | if (htab->dynamic_sections_created) | |
15484 | { | |
15485 | /* Add some entries to the .dynamic section. We fill in the | |
15486 | values later, in finish_dynamic_sections, but we must add | |
15487 | the entries now so that we get the correct size for the | |
15488 | .dynamic section. The DT_DEBUG entry is filled in by the | |
15489 | dynamic linker and used by the debugger. */ | |
15490 | #define add_dynamic_entry(TAG, VAL) \ | |
15491 | _bfd_elf_add_dynamic_entry (info, TAG, VAL) | |
15492 | ||
15493 | const struct elf_backend_data *bed | |
15494 | = get_elf_backend_data (output_bfd); | |
15495 | ||
15496 | if (bfd_link_executable (info)) | |
15497 | { | |
15498 | if (!add_dynamic_entry (DT_DEBUG, 0)) | |
0a1b45a2 | 15499 | return false; |
3084d7a2 L |
15500 | } |
15501 | ||
15502 | if (htab->dt_pltgot_required || htab->splt->size != 0) | |
15503 | { | |
15504 | /* DT_PLTGOT is used by prelink even if there is no PLT | |
15505 | relocation. */ | |
15506 | if (!add_dynamic_entry (DT_PLTGOT, 0)) | |
0a1b45a2 | 15507 | return false; |
3084d7a2 L |
15508 | } |
15509 | ||
15510 | if (htab->dt_jmprel_required || htab->srelplt->size != 0) | |
15511 | { | |
15512 | if (!add_dynamic_entry (DT_PLTRELSZ, 0) | |
15513 | || !add_dynamic_entry (DT_PLTREL, | |
15514 | (bed->rela_plts_and_copies_p | |
15515 | ? DT_RELA : DT_REL)) | |
15516 | || !add_dynamic_entry (DT_JMPREL, 0)) | |
0a1b45a2 | 15517 | return false; |
3084d7a2 L |
15518 | } |
15519 | ||
15520 | if (htab->tlsdesc_plt | |
15521 | && (!add_dynamic_entry (DT_TLSDESC_PLT, 0) | |
15522 | || !add_dynamic_entry (DT_TLSDESC_GOT, 0))) | |
0a1b45a2 | 15523 | return false; |
3084d7a2 L |
15524 | |
15525 | if (need_dynamic_reloc) | |
15526 | { | |
15527 | if (bed->rela_plts_and_copies_p) | |
15528 | { | |
15529 | if (!add_dynamic_entry (DT_RELA, 0) | |
15530 | || !add_dynamic_entry (DT_RELASZ, 0) | |
15531 | || !add_dynamic_entry (DT_RELAENT, | |
15532 | bed->s->sizeof_rela)) | |
0a1b45a2 | 15533 | return false; |
3084d7a2 L |
15534 | } |
15535 | else | |
15536 | { | |
15537 | if (!add_dynamic_entry (DT_REL, 0) | |
15538 | || !add_dynamic_entry (DT_RELSZ, 0) | |
15539 | || !add_dynamic_entry (DT_RELENT, | |
15540 | bed->s->sizeof_rel)) | |
0a1b45a2 | 15541 | return false; |
3084d7a2 L |
15542 | } |
15543 | ||
15544 | /* If any dynamic relocs apply to a read-only section, | |
15545 | then we need a DT_TEXTREL entry. */ | |
15546 | if ((info->flags & DF_TEXTREL) == 0) | |
15547 | elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel, | |
15548 | info); | |
15549 | ||
15550 | if ((info->flags & DF_TEXTREL) != 0) | |
15551 | { | |
15552 | if (htab->ifunc_resolvers) | |
15553 | info->callbacks->einfo | |
15554 | (_("%P: warning: GNU indirect functions with DT_TEXTREL " | |
15555 | "may result in a segfault at runtime; recompile with %s\n"), | |
15556 | bfd_link_dll (info) ? "-fPIC" : "-fPIE"); | |
15557 | ||
15558 | if (!add_dynamic_entry (DT_TEXTREL, 0)) | |
0a1b45a2 | 15559 | return false; |
3084d7a2 L |
15560 | } |
15561 | } | |
15562 | } | |
15563 | #undef add_dynamic_entry | |
15564 | ||
0a1b45a2 | 15565 | return true; |
3084d7a2 | 15566 | } |